OPENSSL_LH_COMPFUNC(3SSL)

πŸ¦‹ OPENSSL_LH_COMPFUNC πŸ¦‹

okay bestie, this is the OpenSSL dynamic hash table page πŸ’– (it lives in the 3SSL manual, obvi). Buckle up, because there is a LOT of good stuff here.


✨ NAME ✨

So this one page covers a whole squad of things: LHASH, DECLARE_LHASH_OF, OPENSSL_LH_COMPFUNC, OPENSSL_LH_HASHFUNC, OPENSSL_LH_DOALL_FUNC, LHASH_DOALL_ARG_FN_TYPE, IMPLEMENT_LHASH_HASH_FN, IMPLEMENT_LHASH_COMP_FN, lh_TYPE_new, lh_TYPE_free, lh_TYPE_flush, lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve, lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_num_items, lh_TYPE_get_down_load, lh_TYPE_set_down_load, lh_TYPE_error, OPENSSL_LH_new, OPENSSL_LH_free, OPENSSL_LH_flush, OPENSSL_LH_insert, OPENSSL_LH_delete, OPENSSL_LH_retrieve, OPENSSL_LH_doall, OPENSSL_LH_doall_arg, OPENSSL_LH_num_items, OPENSSL_LH_get_down_load, OPENSSL_LH_set_down_load, OPENSSL_LH_error β€” and all of them are about a dynamic hash table. slay. πŸŽ€


🌸 SYNOPSIS 🌸

#include <openssl/lhash.h>

DECLARE_LHASH_OF(TYPE);

LHASH_OF(TYPE) *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
void lh_TYPE_free(LHASH_OF(TYPE) *table);
void lh_TYPE_flush(LHASH_OF(TYPE) *table);

TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data);
TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data);
TYPE *lh_TYPE_retrieve(LHASH_OF(TYPE) *table, TYPE *data);

void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func);
void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
                       TYPE *arg);

unsigned long lh_TYPE_num_items(OPENSSL_LHASH *lh);
unsigned long lh_TYPE_get_down_load(OPENSSL_LHASH *lh);
void lh_TYPE_set_down_load(OPENSSL_LHASH *lh, unsigned long dl);

int lh_TYPE_error(LHASH_OF(TYPE) *table);

typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);

OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
void OPENSSL_LH_free(OPENSSL_LHASH *lh);
void OPENSSL_LH_flush(OPENSSL_LHASH *lh);

void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);

void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);

unsigned long OPENSSL_LH_num_items(OPENSSL_LHASH *lh);
unsigned long OPENSSL_LH_get_down_load(OPENSSL_LHASH *lh);
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long dl);

int OPENSSL_LH_error(OPENSSL_LHASH *lh);

#define LH_LOAD_MULT   /* integer constant */

πŸ’… DESCRIPTION πŸ’…

okay so this library implements type-checked dynamic hash tables. the hash table entries can be literally any structures you want β€” usually they're like, a key field and a value field, classic. throughout this whole description, TYPE is just a placeholder for any of the OpenSSL datatypes, like SSL_SESSION. iykyk. 🫢

lh_TYPE_new() creates a brand new LHASH_OF(TYPE) structure for storing your arbitrary data entries, and it wants you to hand over the 'hash' and 'compare' callbacks that it'll use to organise the table's entries. so:

  • the hash callback takes a pointer to a table entry as its argument and returns an unsigned long hash value for its key field. heads up tho β€” the hash value is normally truncated to a power of 2, so make sure your hash function returns well mixed low order bits. no pressure bestie πŸ’€
  • the compare callback takes two arguments (pointers to two hash table entries) and returns 0 if their keys are equal, and nonzero otherwise.

if your hash table is gonna hold items of some particular type, and your hash and compare callbacks hash/compare those types, then the IMPLEMENT_LHASH_HASH_FN and IMPLEMENT_LHASH_COMP_FN macros can be used to create callback wrappers of the prototypes that lh_TYPE_new() needs. like this:

/*
 * Implement the hash and compare functions; "stuff" can be any word.
 */
static unsigned long stuff_hash(const TYPE *a)
{
    ...
}
static int stuff_cmp(const TYPE *a, const TYPE *b)
{
    ...
}

/*
 * Implement the wrapper functions.
 */
static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)

and if that type is gonna show up in several places, you can use these macros in a common header file to declare the function wrappers:

DECLARE_LHASH_HASH_FN(stuff, TYPE)
DECLARE_LHASH_COMP_FN(stuff, TYPE)

then a hash table of TYPE objects gets created like this:

LHASH_OF(TYPE) *htable;

htable = B<lh_I<TYPE>_new>(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));

lh_TYPE_free() frees the LHASH_OF(TYPE) structure table. but bestie, listen β€” the allocated hash table entries will NOT be freed. consider using lh_TYPE_doall() to deallocate any remaining entries in the hash table (see below πŸ‘‡). and if the argument is NULL, nothing is done. iconic behaviour honestly.

lh_TYPE_flush() empties the LHASH_OF(TYPE) structure table. new entries can be added to the flushed table. same deal as above tho: allocated hash table entries will not be freed, so consider using lh_TYPE_doall() to deallocate any remaining entries (see below).

lh_TYPE_insert() inserts the structure pointed to by data into table. if there's already an entry with the same key, the old value gets replaced. also note that lh_TYPE_insert() stores pointers β€” the data are not copied. so don't be out here freeing stuff you already handed over. πŸ™ˆ

lh_TYPE_delete() deletes an entry from table.

lh_TYPE_retrieve() looks up an entry in table. normally, data is a structure with the key field(s) set, and the function returns a pointer to a fully populated structure. cute.

lh_TYPE_doall() will, for every single entry in the hash table, call func with the data item as its parameter. for example:

/* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
void TYPE_cleanup_doall(TYPE *a);

/* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)

/* Call "TYPE_cleanup" against all items in a hash table. */
lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));

/* Then the hash table itself can be deallocated */
lh_TYPE_free(hashtable);

lh_TYPE_doall_arg() is the same as lh_TYPE_doall() except that func gets called with arg as the second argument, and func should be of type LHASH_DOALL_ARG_FN(TYPE) β€” a callback prototype that's passed both the table entry and an extra argument. and same as with lh_doall(), you can instead declare your callback with a prototype matching the types you're dealing with, and use the declare/implement macros to create compatible wrappers that cast variables before calling your type-specific callbacks. here's a demo of that (printing all hash table entries to a BIO provided by the caller):

/* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);

/* Implement a prototype-compatible wrapper for "TYPE_print" */
static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)

/* Print out the entire hashtable to a particular BIO */
lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
                  logging_bio);
🚨 okay IMPORTANT warning time 🚨 note that by default it is NOT safe to use lh_TYPE_delete() inside a callback passed to lh_TYPE_doall() or lh_TYPE_doall_arg(). the reason? deleting an item from the hash table may result in the hash table being contracted to a smaller size and rehashed. lh_TYPE_doall() and lh_TYPE_doall_arg() are unsafe and will exhibit undefined behaviour under these conditions, because these functions assume the hash table size and bucket pointers do not change during the call. πŸ’€

if you really wanna use lh_TYPE_doall() or lh_TYPE_doall_arg() together with lh_TYPE_delete(), it is essential that you call lh_TYPE_set_down_load() with a down_load argument of 0 first. this disables hash table contraction and guarantees that it'll be safe to delete items from a hash table during a call to lh_TYPE_doall() or lh_TYPE_doall_arg(). periodt. ✨

and it is NEVER safe to call lh_TYPE_insert() during a call to lh_TYPE_doall() or lh_TYPE_doall_arg(). not even once. don't do it. πŸ’…

lh_TYPE_error() can be used to determine if an error occurred in the last operation.

lh_TYPE_num_items() returns the number of items in the hash table.

lh_TYPE_get_down_load() and lh_TYPE_set_down_load() get and set the factor used to determine when the hash table is contracted. the factor is the load factor at or below which hash table contraction will occur, multiplied by LH_LOAD_MULT, where the load factor is the number of items divided by the number of nodes. setting this value to 0 disables hash table contraction. so basically that's the lil switch that keeps your doall-delete era safe 🫢

OPENSSL_LH_new() is the same as lh_TYPE_new() except that it is not type specific. so instead of returning an LHASH_OF(TYPE) value it returns a void *. and in the same way, the functions OPENSSL_LH_free(), OPENSSL_LH_flush(), OPENSSL_LH_insert(), OPENSSL_LH_delete(), OPENSSL_LH_retrieve(), OPENSSL_LH_doall(), OPENSSL_LH_doall_arg(), OPENSSL_LH_num_items(), OPENSSL_LH_get_down_load(), OPENSSL_LH_set_down_load() and OPENSSL_LH_error() are equivalent to the similarly named lh_TYPE functions, except that they return or use a void * where the equivalent lh_TYPE function returns or uses a TYPE * or LHASH_OF(TYPE) *. the lh_TYPE functions are implemented as type checked wrappers around the OPENSSL_LH functions. and ngl, most applications should not call the OPENSSL_LH functions directly.


🌈 RETURN VALUES 🌈

lh_TYPE_new() and OPENSSL_LH_new() return NULL on error, otherwise a pointer to the new LHASH structure. πŸŽ€

when a hash table entry is replaced, lh_TYPE_insert() or OPENSSL_LH_insert() return the value being replaced. NULL is returned on normal operation and on error.

lh_TYPE_delete() and OPENSSL_LH_delete() return the entry being deleted. NULL is returned if there is no such value in the hash table.

lh_TYPE_retrieve() and OPENSSL_LH_retrieve() return the hash table entry if it has been found, NULL otherwise.

lh_TYPE_error() and OPENSSL_LH_error() return 1 if an error occurred in the last operation, 0 otherwise. it's meaningful only after non-retrieve operations. (noted bestie πŸ“)

lh_TYPE_free(), OPENSSL_LH_free(), lh_TYPE_flush(), OPENSSL_LH_flush(), lh_TYPE_doall() OPENSSL_LH_doall(), lh_TYPE_doall_arg() and OPENSSL_LH_doall_arg() return no values. they just do the thing and dip πŸ’…

πŸ’– NOTE πŸ’–

⚠️ Real talk bestie, the LHASH code is not thread safe. Not even a little. Lock your stuff or perish, I'm so serious. 😭

Here's the lock lore, no cap:

  • Every updating operation β€” all of them β€” plus any call to lh_TYPE_error() or OPENSSL_LH_error(), must go down under a write lock. Periodt. πŸ’…
  • Every retrieve operation should happen under a read lock… unless you're out here wanting accurate usage statistics πŸ‘€. In that case you use a write lock for the retrieves too, bestie. Yes really.
  • For spitting out those usage statistics β€” the functions from OPENSSL_LH_stats(3) β€” a read lock is enough. Slay. ✨

Now, the const drama. The LHASH code treats table entries as constant data. So internally it represents items you've lh_insert()'d with a "const void *" pointer type. That's literally why the callbacks β€” like the ones lh_doall() and lh_doall_arg() use β€” declare their prototypes with "const", even for the parameters that pass back the table items' data pointers. For consistency, user-provided data is "const" at all times as far as the LHASH code is concerned. But here's the tea πŸ«–: since the callers are the ones providing those pointers, they get to choose whether they wanna treat all such parameters as constant too.

Take this example, bestie. A hash table might be maintained by code that, for encapsulation reasons, only has "const" access to the data being indexed in the hash table (i.e. it's returned as "const" from elsewhere in their code) β€” in that case the LHASH prototypes are appropriate as-is. πŸ’― Conversely, if the caller is responsible for the life-time of the data in question, then they may well wanna make modifications to the table item passed back in the lh_doall() or lh_doall_arg() callbacks (peep the "TYPE_cleanup" example above πŸ‘†). If so, the caller can either cast the "const" away (if they're providing the raw callbacks themselves) or use the macros to declare/implement the wrapper functions without "const" types. Both are valid, pick your fighter. πŸ₯Š

And now the callout nobody asked for: callers that only have "const" access to data they're indexing in a table, yet declare callbacks without constant types (or cast the "const" away themselves), are therefore creating their own risks/bugs without being encouraged to do so by the API. πŸ™ˆ On a related note, those auditing code should pay special attention to any instances of DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types without any "const" qualifiers. Do your homework, iykyk. πŸ”


πŸ’€ BUGS πŸ’€

Okay so this one's a little cursed: lh_TYPE_insert() and OPENSSL_LH_insert() return NULL both for success and error. Yeah. Same value. Two meanings. Not me getting betrayed by the return type. 😭 Keep that in mind before you write your error handling, bestie.


πŸ¦‹ SEE ALSO πŸ¦‹


πŸ‘‘ HISTORY πŸ‘‘

In OpenSSL 1.0.0, the lhash interface was revamped for better type checking. Glow-up of the century, honestly. ✨


Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


And that's a wrap on OPENSSL_LH_COMPFUNC(3SSL), bestie 🫢 β€” this was the 3.0.22 version, dated 2026-08-25, and yes the whole thing ate. Go forth and hash responsibly. πŸ’…πŸŽ€

πŸ“¦ where this page comes from