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