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