1 .\" $NetBSD: openssl_lhash.3,v 1.14 2015/06/12 17:01:15 christos Exp $
3 .\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28)
6 .\" ========================================================================
7 .de Sp \" Vertical space (when we can't use .PP)
11 .de Vb \" Begin verbatim text
16 .de Ve \" End verbatim text
20 .\" Set up some character translations and predefined strings. \*(-- will
21 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
22 .\" double quote, and \*(R" will give a right double quote. \*(C+ will
23 .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
24 .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
25 .\" nothing in troff, for use with C<>.
27 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
31 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
32 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
47 .\" Escape single quotes in literal strings from groff's Unicode transform.
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
53 .\" entries marked with X<> in POD. Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
56 .\" Avoid warning from groff about undefined register 'F'.
60 .if \n(.g .if rF .nr rF 1
61 .if (\n(rF:(\n(.g==0)) \{
64 . tm Index:\\$1\t\\n%\t"\\$2"
74 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
75 .\" Fear. Run. Save yourself. No user-serviceable parts.
76 . \" fudge factors for nroff and troff
85 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
91 . \" simple accents for nroff and troff
101 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
102 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
103 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
104 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
105 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
106 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
108 . \" troff and (daisy-wheel) nroff accents
109 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
110 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
111 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
112 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
113 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
114 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
115 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
116 .ds ae a\h'-(\w'a'u*4/10)'e
117 .ds Ae A\h'-(\w'A'u*4/10)'E
118 . \" corrections for vroff
119 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
120 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
121 . \" for low resolution devices (crt and lpr)
122 .if \n(.H>23 .if \n(.V>19 \
135 .\" ========================================================================
138 .TH lhash 3 "2009-12-26" "1.0.1n" "OpenSSL"
139 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
140 .\" way too many mistakes in technical documents.
144 lh_new, lh_free, lh_insert, lh_delete, lh_retrieve, lh_doall, lh_doall_arg, lh_error \- dynamic hash table
148 .IX Header "SYNOPSIS"
150 \& #include <openssl/lhash.h>
152 \& DECLARE_LHASH_OF(<type>);
154 \& LHASH *lh_<type>_new();
155 \& void lh_<type>_free(LHASH_OF(<type> *table);
157 \& <type> *lh_<type>_insert(LHASH_OF(<type> *table, <type> *data);
158 \& <type> *lh_<type>_delete(LHASH_OF(<type> *table, <type> *data);
159 \& <type> *lh_retrieve(LHASH_OF<type> *table, <type> *data);
161 \& void lh_<type>_doall(LHASH_OF(<type> *table, LHASH_DOALL_FN_TYPE func);
162 \& void lh_<type>_doall_arg(LHASH_OF(<type> *table, LHASH_DOALL_ARG_FN_TYPE func,
163 \& <type2>, <type2> *arg);
165 \& int lh_<type>_error(LHASH_OF(<type> *table);
167 \& typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *);
168 \& typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *);
169 \& typedef void (*LHASH_DOALL_FN_TYPE)(const void *);
170 \& typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
173 .IX Header "DESCRIPTION"
174 This library implements type-checked dynamic hash tables. The hash
175 table entries can be arbitrary structures. Usually they consist of key
178 lh_<type>\fI_new()\fR creates a new \fB\s-1LHASH_OF\s0(<type\fR> structure to store
179 arbitrary data entries, and provides the 'hash' and 'compare'
180 callbacks to be used in organising the table's entries. The \fBhash\fR
181 callback takes a pointer to a table entry as its argument and returns
182 an unsigned long hash value for its key field. The hash value is
183 normally truncated to a power of 2, so make sure that your hash
184 function returns well mixed low order bits. The \fBcompare\fR callback
185 takes two arguments (pointers to two hash table entries), and returns
186 0 if their keys are equal, non-zero otherwise. If your hash table
187 will contain items of some particular type and the \fBhash\fR and
188 \&\fBcompare\fR callbacks hash/compare these types, then the
189 \&\fB\s-1DECLARE_LHASH_HASH_FN\s0\fR and \fB\s-1IMPLEMENT_LHASH_COMP_FN\s0\fR macros can be
190 used to create callback wrappers of the prototypes required by
191 lh_<type>\fI_new()\fR. These provide per-variable casts before calling the
192 type-specific callbacks written by the application author. These
193 macros, as well as those used for the \*(L"doall\*(R" callbacks, are defined
197 \& #define DECLARE_LHASH_HASH_FN(name, o_type) \e
198 \& unsigned long name##_LHASH_HASH(const void *);
199 \& #define IMPLEMENT_LHASH_HASH_FN(name, o_type) \e
200 \& unsigned long name##_LHASH_HASH(const void *arg) { \e
201 \& const o_type *a = arg; \e
202 \& return name##_hash(a); }
203 \& #define LHASH_HASH_FN(name) name##_LHASH_HASH
205 \& #define DECLARE_LHASH_COMP_FN(name, o_type) \e
206 \& int name##_LHASH_COMP(const void *, const void *);
207 \& #define IMPLEMENT_LHASH_COMP_FN(name, o_type) \e
208 \& int name##_LHASH_COMP(const void *arg1, const void *arg2) { \e
209 \& const o_type *a = arg1; \e
210 \& const o_type *b = arg2; \e
211 \& return name##_cmp(a,b); }
212 \& #define LHASH_COMP_FN(name) name##_LHASH_COMP
214 \& #define DECLARE_LHASH_DOALL_FN(name, o_type) \e
215 \& void name##_LHASH_DOALL(void *);
216 \& #define IMPLEMENT_LHASH_DOALL_FN(name, o_type) \e
217 \& void name##_LHASH_DOALL(void *arg) { \e
218 \& o_type *a = arg; \e
219 \& name##_doall(a); }
220 \& #define LHASH_DOALL_FN(name) name##_LHASH_DOALL
222 \& #define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \e
223 \& void name##_LHASH_DOALL_ARG(void *, void *);
224 \& #define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \e
225 \& void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \e
226 \& o_type *a = arg1; \e
227 \& a_type *b = arg2; \e
228 \& name##_doall_arg(a, b); }
229 \& #define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
231 \& An example of a hash table storing (pointers to) structures of type \*(AqSTUFF\*(Aq
232 \& could be defined as follows;
234 \& /* Calculates the hash value of \*(Aqtohash\*(Aq (implemented elsewhere) */
235 \& unsigned long STUFF_hash(const STUFF *tohash);
236 \& /* Orders \*(Aqarg1\*(Aq and \*(Aqarg2\*(Aq (implemented elsewhere) */
237 \& int stuff_cmp(const STUFF *arg1, const STUFF *arg2);
238 \& /* Create the type\-safe wrapper functions for use in the LHASH internals */
239 \& static IMPLEMENT_LHASH_HASH_FN(stuff, STUFF);
240 \& static IMPLEMENT_LHASH_COMP_FN(stuff, STUFF);
242 \& int main(int argc, char *argv[]) {
243 \& /* Create the new hash table using the hash/compare wrappers */
244 \& LHASH_OF(STUFF) *hashtable = lh_STUFF_new(LHASH_HASH_FN(STUFF_hash),
245 \& LHASH_COMP_FN(STUFF_cmp));
250 lh_<type>\fI_free()\fR frees the \fB\s-1LHASH_OF\s0(<type\fR> structure
251 \&\fBtable\fR. Allocated hash table entries will not be freed; consider
252 using lh_<type>\fI_doall()\fR to deallocate any remaining entries in the
253 hash table (see below).
255 lh_<type>\fI_insert()\fR inserts the structure pointed to by \fBdata\fR into
256 \&\fBtable\fR. If there already is an entry with the same key, the old
257 value is replaced. Note that lh_<type>\fI_insert()\fR stores pointers, the
260 lh_<type>\fI_delete()\fR deletes an entry from \fBtable\fR.
262 lh_<type>\fI_retrieve()\fR looks up an entry in \fBtable\fR. Normally, \fBdata\fR
263 is a structure with the key field(s) set; the function will return a
264 pointer to a fully populated structure.
266 lh_<type>\fI_doall()\fR will, for every entry in the hash table, call
267 \&\fBfunc\fR with the data item as its parameter. For lh_<type>\fI_doall()\fR
268 and lh_<type>\fI_doall_arg()\fR, function pointer casting should be avoided
269 in the callbacks (see \fB\s-1NOTE\s0\fR) \- instead use the declare/implement
270 macros to create type-checked wrappers that cast variables prior to
271 calling your type-specific callbacks. An example of this is
272 illustrated here where the callback is used to cleanup resources for
273 items in the hash table prior to the hashtable itself being
277 \& /* Cleans up resources belonging to \*(Aqa\*(Aq (this is implemented elsewhere) */
278 \& void STUFF_cleanup_doall(STUFF *a);
279 \& /* Implement a prototype\-compatible wrapper for "STUFF_cleanup" */
280 \& IMPLEMENT_LHASH_DOALL_FN(STUFF_cleanup, STUFF)
281 \& /* ... then later in the code ... */
282 \& /* So to run "STUFF_cleanup" against all items in a hash table ... */
283 \& lh_STUFF_doall(hashtable, LHASH_DOALL_FN(STUFF_cleanup));
284 \& /* Then the hash table itself can be deallocated */
285 \& lh_STUFF_free(hashtable);
288 When doing this, be careful if you delete entries from the hash table
289 in your callbacks: the table may decrease in size, moving the item
290 that you are currently on down lower in the hash table \- this could
291 cause some entries to be skipped during the iteration. The second
292 best solution to this problem is to set hash\->down_load=0 before
293 you start (which will stop the hash table ever decreasing in size).
294 The best solution is probably to avoid deleting items from the hash
295 table inside a \*(L"doall\*(R" callback!
297 lh_<type>\fI_doall_arg()\fR is the same as lh_<type>\fI_doall()\fR except that
298 \&\fBfunc\fR will be called with \fBarg\fR as the second argument and \fBfunc\fR
299 should be of type \fB\s-1LHASH_DOALL_ARG_FN_TYPE\s0\fR (a callback prototype
300 that is passed both the table entry and an extra argument). As with
301 \&\fIlh_doall()\fR, you can instead choose to declare your callback with a
302 prototype matching the types you are dealing with and use the
303 declare/implement macros to create compatible wrappers that cast
304 variables before calling your type-specific callbacks. An example of
305 this is demonstrated here (printing all hash table entries to a \s-1BIO\s0
306 that is provided by the caller):
309 \& /* Prints item \*(Aqa\*(Aq to \*(Aqoutput_bio\*(Aq (this is implemented elsewhere) */
310 \& void STUFF_print_doall_arg(const STUFF *a, BIO *output_bio);
311 \& /* Implement a prototype\-compatible wrapper for "STUFF_print" */
312 \& static IMPLEMENT_LHASH_DOALL_ARG_FN(STUFF, const STUFF, BIO)
313 \& /* ... then later in the code ... */
314 \& /* Print out the entire hashtable to a particular BIO */
315 \& lh_STUFF_doall_arg(hashtable, LHASH_DOALL_ARG_FN(STUFF_print), BIO,
319 lh_<type>\fI_error()\fR can be used to determine if an error occurred in the last
320 operation. lh_<type>\fI_error()\fR is a macro.
322 .IX Header "RETURN VALUES"
323 lh_<type>\fI_new()\fR returns \fB\s-1NULL\s0\fR on error, otherwise a pointer to the new
324 \&\fB\s-1LHASH\s0\fR structure.
326 When a hash table entry is replaced, lh_<type>\fI_insert()\fR returns the value
327 being replaced. \fB\s-1NULL\s0\fR is returned on normal operation and on error.
329 lh_<type>\fI_delete()\fR returns the entry being deleted. \fB\s-1NULL\s0\fR is returned if
330 there is no such value in the hash table.
332 lh_<type>\fI_retrieve()\fR returns the hash table entry if it has been found,
333 \&\fB\s-1NULL\s0\fR otherwise.
335 lh_<type>\fI_error()\fR returns 1 if an error occurred in the last operation, 0
338 lh_<type>\fI_free()\fR, lh_<type>\fI_doall()\fR and lh_<type>\fI_doall_arg()\fR return no values.
341 The various \s-1LHASH\s0 macros and callback types exist to make it possible
342 to write type-checked code without resorting to function-prototype
343 casting \- an evil that makes application code much harder to
344 audit/verify and also opens the window of opportunity for stack
345 corruption and other hard-to-find bugs. It also, apparently, violates
348 The \s-1LHASH\s0 code regards table entries as constant data. As such, it
349 internally represents \fIlh_insert()\fR'd items with a \*(L"const void *\*(R"
350 pointer type. This is why callbacks such as those used by \fIlh_doall()\fR
351 and \fIlh_doall_arg()\fR declare their prototypes with \*(L"const\*(R", even for the
352 parameters that pass back the table items' data pointers \- for
353 consistency, user-provided data is \*(L"const\*(R" at all times as far as the
354 \&\s-1LHASH\s0 code is concerned. However, as callers are themselves providing
355 these pointers, they can choose whether they too should be treating
356 all such parameters as constant.
358 As an example, a hash table may be maintained by code that, for
359 reasons of encapsulation, has only \*(L"const\*(R" access to the data being
360 indexed in the hash table (ie. it is returned as \*(L"const\*(R" from
361 elsewhere in their code) \- in this case the \s-1LHASH\s0 prototypes are
362 appropriate as-is. Conversely, if the caller is responsible for the
363 life-time of the data in question, then they may well wish to make
364 modifications to table item passed back in the \fIlh_doall()\fR or
365 \&\fIlh_doall_arg()\fR callbacks (see the \*(L"STUFF_cleanup\*(R" example above). If
366 so, the caller can either cast the \*(L"const\*(R" away (if they're providing
367 the raw callbacks themselves) or use the macros to declare/implement
368 the wrapper functions without \*(L"const\*(R" types.
370 Callers that only have \*(L"const\*(R" access to data they're indexing in a
371 table, yet declare callbacks without constant types (or cast the
372 \&\*(L"const\*(R" away themselves), are therefore creating their own risks/bugs
373 without being encouraged to do so by the \s-1API. \s0 On a related note,
374 those auditing code should pay special attention to any instances of
375 DECLARE/IMPLEMENT_LHASH_DOALL_[\s-1ARG_\s0]_FN macros that provide types
376 without any \*(L"const\*(R" qualifiers.
379 lh_<type>\fI_insert()\fR returns \fB\s-1NULL\s0\fR both for success and error.
381 .IX Header "INTERNALS"
382 The following description is based on the SSLeay documentation:
384 The \fBlhash\fR library implements a hash table described in the
385 \&\fICommunications of the \s-1ACM\s0\fR in 1991. What makes this hash table
386 different is that as the table fills, the hash table is increased (or
387 decreased) in size via \fIOPENSSL_realloc()\fR. When a 'resize' is done, instead of
388 all hashes being redistributed over twice as many 'buckets', one
389 bucket is split. So when an 'expand' is done, there is only a minimal
390 cost to redistribute some values. Subsequent inserts will cause more
391 single 'bucket' redistributions but there will never be a sudden large
392 cost due to redistributing all the 'buckets'.
394 The state for a particular hash table is kept in the \fB\s-1LHASH\s0\fR structure.
395 The decision to increase or decrease the hash table size is made
396 depending on the 'load' of the hash table. The load is the number of
397 items in the hash table divided by the size of the hash table. The
398 default values are as follows. If (hash\->up_load < load) =>
399 expand. if (hash\->down_load > load) => contract. The
400 \&\fBup_load\fR has a default value of 1 and \fBdown_load\fR has a default value
401 of 2. These numbers can be modified by the application by just
402 playing with the \fBup_load\fR and \fBdown_load\fR variables. The 'load' is
403 kept in a form which is multiplied by 256. So
404 hash\->up_load=8*256; will cause a load of 8 to be set.
406 If you are interested in performance the field to watch is
407 num_comp_calls. The hash library keeps track of the 'hash' value for
408 each item so when a lookup is done, the 'hashes' are compared, if
409 there is a match, then a full compare is done, and
410 hash\->num_comp_calls is incremented. If num_comp_calls is not equal
411 to num_delete plus num_retrieve it means that your hash function is
412 generating hashes that are the same for different values. It is
413 probably worth changing your hash function if this is the case because
414 even if your hash table has 10 items in a 'bucket', it can be searched
415 with 10 \fBunsigned long\fR compares and 10 linked list traverses. This
416 will be much less expensive that 10 calls to your compare function.
418 \&\fIlh_strhash()\fR is a demo string hashing function:
421 \& unsigned long lh_strhash(const char *c);
424 Since the \fB\s-1LHASH\s0\fR routines would normally be passed structures, this
425 routine would not normally be passed to lh_<type>\fI_new()\fR, rather it would be
426 used in the function passed to lh_<type>\fI_new()\fR.
428 .IX Header "SEE ALSO"
429 \&\fIlh_stats\fR\|(3)
432 The \fBlhash\fR library is available in all versions of SSLeay and OpenSSL.
433 \&\fIlh_error()\fR was added in SSLeay 0.9.1b.
435 This manpage is derived from the SSLeay documentation.
437 In OpenSSL 0.9.7, all lhash functions that were passed function pointers
438 were changed for better type safety, and the function types \s-1LHASH_COMP_FN_TYPE,
439 LHASH_HASH_FN_TYPE, LHASH_DOALL_FN_TYPE\s0 and \s-1LHASH_DOALL_ARG_FN_TYPE \s0
442 In OpenSSL 1.0.0, the lhash interface was revamped for even better