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