Merge from vendor branch LESS:
[dragonfly.git] / secure / lib / libcrypto / man / lhash.3
1 .\" Automatically generated by Pod::Man 2.12 (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 .\" 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 .\" 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 "2007-10-24" "0.9.8g" "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 \& LHASH *lh_new(LHASH_HASH_FN_TYPE hash, LHASH_COMP_FN_TYPE compare);
140 \& void lh_free(LHASH *table);
141 \&
142 \& void *lh_insert(LHASH *table, void *data);
143 \& void *lh_delete(LHASH *table, void *data);
144 \& void *lh_retrieve(LHASH *table, void *data);
145 \&
146 \& void lh_doall(LHASH *table, LHASH_DOALL_FN_TYPE func);
147 \& void lh_doall_arg(LHASH *table, LHASH_DOALL_ARG_FN_TYPE func,
148 \&          void *arg);
149 \&
150 \& int lh_error(LHASH *table);
151 \&
152 \& typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *);
153 \& typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *);
154 \& typedef void (*LHASH_DOALL_FN_TYPE)(const void *);
155 \& typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
156 .Ve
157 .SH "DESCRIPTION"
158 .IX Header "DESCRIPTION"
159 This library implements dynamic hash tables. The hash table entries
160 can be arbitrary structures. Usually they consist of key and value
161 fields.
162 .PP
163 \&\fIlh_new()\fR creates a new \fB\s-1LHASH\s0\fR structure to store arbitrary data
164 entries, and provides the 'hash' and 'compare' callbacks to be used in
165 organising the table's entries.  The \fBhash\fR callback takes a pointer
166 to a table entry as its argument and returns an unsigned long hash
167 value for its key field.  The hash value is normally truncated to a
168 power of 2, so make sure that your hash function returns well mixed
169 low order bits.  The \fBcompare\fR callback takes two arguments (pointers
170 to two hash table entries), and returns 0 if their keys are equal,
171 non-zero otherwise.  If your hash table will contain items of some
172 particular type and the \fBhash\fR and \fBcompare\fR callbacks hash/compare
173 these types, then the \fB\s-1DECLARE_LHASH_HASH_FN\s0\fR and
174 \&\fB\s-1IMPLEMENT_LHASH_COMP_FN\s0\fR macros can be used to create callback
175 wrappers of the prototypes required by \fIlh_new()\fR.  These provide
176 per-variable casts before calling the type-specific callbacks written
177 by the application author.  These macros, as well as those used for
178 the \*(L"doall\*(R" callbacks, are defined as;
179 .PP
180 .Vb 7
181 \& #define DECLARE_LHASH_HASH_FN(f_name,o_type) \e
182 \&         unsigned long f_name##_LHASH_HASH(const void *);
183 \& #define IMPLEMENT_LHASH_HASH_FN(f_name,o_type) \e
184 \&         unsigned long f_name##_LHASH_HASH(const void *arg) { \e
185 \&                 o_type a = (o_type)arg; \e
186 \&                 return f_name(a); }
187 \& #define LHASH_HASH_FN(f_name) f_name##_LHASH_HASH
188 \&
189 \& #define DECLARE_LHASH_COMP_FN(f_name,o_type) \e
190 \&         int f_name##_LHASH_COMP(const void *, const void *);
191 \& #define IMPLEMENT_LHASH_COMP_FN(f_name,o_type) \e
192 \&         int f_name##_LHASH_COMP(const void *arg1, const void *arg2) { \e
193 \&                 o_type a = (o_type)arg1; \e
194 \&                 o_type b = (o_type)arg2; \e
195 \&                 return f_name(a,b); }
196 \& #define LHASH_COMP_FN(f_name) f_name##_LHASH_COMP
197 \&
198 \& #define DECLARE_LHASH_DOALL_FN(f_name,o_type) \e
199 \&         void f_name##_LHASH_DOALL(const void *);
200 \& #define IMPLEMENT_LHASH_DOALL_FN(f_name,o_type) \e
201 \&         void f_name##_LHASH_DOALL(const void *arg) { \e
202 \&                 o_type a = (o_type)arg; \e
203 \&                 f_name(a); }
204 \& #define LHASH_DOALL_FN(f_name) f_name##_LHASH_DOALL
205 \&
206 \& #define DECLARE_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \e
207 \&         void f_name##_LHASH_DOALL_ARG(const void *, const void *);
208 \& #define IMPLEMENT_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \e
209 \&         void f_name##_LHASH_DOALL_ARG(const void *arg1, const void *arg2) { \e
210 \&                 o_type a = (o_type)arg1; \e
211 \&                 a_type b = (a_type)arg2; \e
212 \&                 f_name(a,b); }
213 \& #define LHASH_DOALL_ARG_FN(f_name) f_name##_LHASH_DOALL_ARG
214 .Ve
215 .PP
216 An example of a hash table storing (pointers to) structures of type '\s-1STUFF\s0'
217 could be defined as follows;
218 .PP
219 .Vb 10
220 \& /* Calculates the hash value of 'tohash' (implemented elsewhere) */
221 \& unsigned long STUFF_hash(const STUFF *tohash);
222 \& /* Orders 'arg1' and 'arg2' (implemented elsewhere) */
223 \& int STUFF_cmp(const STUFF *arg1, const STUFF *arg2);
224 \& /* Create the type\-safe wrapper functions for use in the LHASH internals */
225 \& static IMPLEMENT_LHASH_HASH_FN(STUFF_hash, const STUFF *)
226 \& static IMPLEMENT_LHASH_COMP_FN(STUFF_cmp, const STUFF *);
227 \& /* ... */
228 \& int main(int argc, char *argv[]) {
229 \&         /* Create the new hash table using the hash/compare wrappers */
230 \&         LHASH *hashtable = lh_new(LHASH_HASH_FN(STUFF_hash),
231 \&                                   LHASH_COMP_FN(STUFF_cmp));
232 \&         /* ... */
233 \& }
234 .Ve
235 .PP
236 \&\fIlh_free()\fR frees the \fB\s-1LHASH\s0\fR structure \fBtable\fR. Allocated hash table
237 entries will not be freed; consider using \fIlh_doall()\fR to deallocate any
238 remaining entries in the hash table (see below).
239 .PP
240 \&\fIlh_insert()\fR inserts the structure pointed to by \fBdata\fR into \fBtable\fR.
241 If there already is an entry with the same key, the old value is
242 replaced. Note that \fIlh_insert()\fR stores pointers, the data are not
243 copied.
244 .PP
245 \&\fIlh_delete()\fR deletes an entry from \fBtable\fR.
246 .PP
247 \&\fIlh_retrieve()\fR looks up an entry in \fBtable\fR. Normally, \fBdata\fR is
248 a structure with the key field(s) set; the function will return a
249 pointer to a fully populated structure.
250 .PP
251 \&\fIlh_doall()\fR will, for every entry in the hash table, call \fBfunc\fR with
252 the data item as its parameter.  For \fIlh_doall()\fR and \fIlh_doall_arg()\fR,
253 function pointer casting should be avoided in the callbacks (see
254 \&\fB\s-1NOTE\s0\fR) \- instead, either declare the callbacks to match the
255 prototype required in \fIlh_new()\fR or use the declare/implement macros to
256 create type-safe wrappers that cast variables prior to calling your
257 type-specific callbacks.  An example of this is illustrated here where
258 the callback is used to cleanup resources for items in the hash table
259 prior to the hashtable itself being deallocated:
260 .PP
261 .Vb 9
262 \& /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
263 \& void STUFF_cleanup(STUFF *a);
264 \& /* Implement a prototype\-compatible wrapper for "STUFF_cleanup" */
265 \& IMPLEMENT_LHASH_DOALL_FN(STUFF_cleanup, STUFF *)
266 \&         /* ... then later in the code ... */
267 \& /* So to run "STUFF_cleanup" against all items in a hash table ... */
268 \& lh_doall(hashtable, LHASH_DOALL_FN(STUFF_cleanup));
269 \& /* Then the hash table itself can be deallocated */
270 \& lh_free(hashtable);
271 .Ve
272 .PP
273 When doing this, be careful if you delete entries from the hash table
274 in your callbacks: the table may decrease in size, moving the item
275 that you are currently on down lower in the hash table \- this could
276 cause some entries to be skipped during the iteration.  The second
277 best solution to this problem is to set hash\->down_load=0 before
278 you start (which will stop the hash table ever decreasing in size).
279 The best solution is probably to avoid deleting items from the hash
280 table inside a \*(L"doall\*(R" callback!
281 .PP
282 \&\fIlh_doall_arg()\fR is the same as \fIlh_doall()\fR except that \fBfunc\fR will be
283 called with \fBarg\fR as the second argument and \fBfunc\fR should be of
284 type \fB\s-1LHASH_DOALL_ARG_FN_TYPE\s0\fR (a callback prototype that is passed
285 both the table entry and an extra argument).  As with \fIlh_doall()\fR, you
286 can instead choose to declare your callback with a prototype matching
287 the types you are dealing with and use the declare/implement macros to
288 create compatible wrappers that cast variables before calling your
289 type-specific callbacks.  An example of this is demonstrated here
290 (printing all hash table entries to a \s-1BIO\s0 that is provided by the
291 caller):
292 .PP
293 .Vb 7
294 \& /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
295 \& void STUFF_print(const STUFF *a, BIO *output_bio);
296 \& /* Implement a prototype\-compatible wrapper for "STUFF_print" */
297 \& static IMPLEMENT_LHASH_DOALL_ARG_FN(STUFF_print, const STUFF *, BIO *)
298 \&         /* ... then later in the code ... */
299 \& /* Print out the entire hashtable to a particular BIO */
300 \& lh_doall_arg(hashtable, LHASH_DOALL_ARG_FN(STUFF_print), logging_bio);
301 .Ve
302 .PP
303 \&\fIlh_error()\fR can be used to determine if an error occurred in the last
304 operation. \fIlh_error()\fR is a macro.
305 .SH "RETURN VALUES"
306 .IX Header "RETURN VALUES"
307 \&\fIlh_new()\fR returns \fB\s-1NULL\s0\fR on error, otherwise a pointer to the new
308 \&\fB\s-1LHASH\s0\fR structure.
309 .PP
310 When a hash table entry is replaced, \fIlh_insert()\fR returns the value
311 being replaced. \fB\s-1NULL\s0\fR is returned on normal operation and on error.
312 .PP
313 \&\fIlh_delete()\fR returns the entry being deleted.  \fB\s-1NULL\s0\fR is returned if
314 there is no such value in the hash table.
315 .PP
316 \&\fIlh_retrieve()\fR returns the hash table entry if it has been found,
317 \&\fB\s-1NULL\s0\fR otherwise.
318 .PP
319 \&\fIlh_error()\fR returns 1 if an error occurred in the last operation, 0
320 otherwise.
321 .PP
322 \&\fIlh_free()\fR, \fIlh_doall()\fR and \fIlh_doall_arg()\fR return no values.
323 .SH "NOTE"
324 .IX Header "NOTE"
325 The various \s-1LHASH\s0 macros and callback types exist to make it possible
326 to write type-safe code without resorting to function-prototype
327 casting \- an evil that makes application code much harder to
328 audit/verify and also opens the window of opportunity for stack
329 corruption and other hard-to-find bugs.  It also, apparently, violates
330 ANSI-C.
331 .PP
332 The \s-1LHASH\s0 code regards table entries as constant data.  As such, it
333 internally represents \fIlh_insert()\fR'd items with a \*(L"const void *\*(R"
334 pointer type.  This is why callbacks such as those used by \fIlh_doall()\fR
335 and \fIlh_doall_arg()\fR declare their prototypes with \*(L"const\*(R", even for the
336 parameters that pass back the table items' data pointers \- for
337 consistency, user-provided data is \*(L"const\*(R" at all times as far as the
338 \&\s-1LHASH\s0 code is concerned.  However, as callers are themselves providing
339 these pointers, they can choose whether they too should be treating
340 all such parameters as constant.
341 .PP
342 As an example, a hash table may be maintained by code that, for
343 reasons of encapsulation, has only \*(L"const\*(R" access to the data being
344 indexed in the hash table (ie. it is returned as \*(L"const\*(R" from
345 elsewhere in their code) \- in this case the \s-1LHASH\s0 prototypes are
346 appropriate as-is.  Conversely, if the caller is responsible for the
347 life-time of the data in question, then they may well wish to make
348 modifications to table item passed back in the \fIlh_doall()\fR or
349 \&\fIlh_doall_arg()\fR callbacks (see the \*(L"STUFF_cleanup\*(R" example above).  If
350 so, the caller can either cast the \*(L"const\*(R" away (if they're providing
351 the raw callbacks themselves) or use the macros to declare/implement
352 the wrapper functions without \*(L"const\*(R" types.
353 .PP
354 Callers that only have \*(L"const\*(R" access to data they're indexing in a
355 table, yet declare callbacks without constant types (or cast the
356 \&\*(L"const\*(R" away themselves), are therefore creating their own risks/bugs
357 without being encouraged to do so by the \s-1API\s0.  On a related note,
358 those auditing code should pay special attention to any instances of
359 DECLARE/IMPLEMENT_LHASH_DOALL_[\s-1ARG_\s0]_FN macros that provide types
360 without any \*(L"const\*(R" qualifiers.
361 .SH "BUGS"
362 .IX Header "BUGS"
363 \&\fIlh_insert()\fR returns \fB\s-1NULL\s0\fR both for success and error.
364 .SH "INTERNALS"
365 .IX Header "INTERNALS"
366 The following description is based on the SSLeay documentation:
367 .PP
368 The \fBlhash\fR library implements a hash table described in the
369 \&\fICommunications of the \s-1ACM\s0\fR in 1991.  What makes this hash table
370 different is that as the table fills, the hash table is increased (or
371 decreased) in size via \fIOPENSSL_realloc()\fR.  When a 'resize' is done, instead of
372 all hashes being redistributed over twice as many 'buckets', one
373 bucket is split.  So when an 'expand' is done, there is only a minimal
374 cost to redistribute some values.  Subsequent inserts will cause more
375 single 'bucket' redistributions but there will never be a sudden large
376 cost due to redistributing all the 'buckets'.
377 .PP
378 The state for a particular hash table is kept in the \fB\s-1LHASH\s0\fR structure.
379 The decision to increase or decrease the hash table size is made
380 depending on the 'load' of the hash table.  The load is the number of
381 items in the hash table divided by the size of the hash table.  The
382 default values are as follows.  If (hash\->up_load < load) =>
383 expand.  if (hash\->down_load > load) => contract.  The
384 \&\fBup_load\fR has a default value of 1 and \fBdown_load\fR has a default value
385 of 2.  These numbers can be modified by the application by just
386 playing with the \fBup_load\fR and \fBdown_load\fR variables.  The 'load' is
387 kept in a form which is multiplied by 256.  So
388 hash\->up_load=8*256; will cause a load of 8 to be set.
389 .PP
390 If you are interested in performance the field to watch is
391 num_comp_calls.  The hash library keeps track of the 'hash' value for
392 each item so when a lookup is done, the 'hashes' are compared, if
393 there is a match, then a full compare is done, and
394 hash\->num_comp_calls is incremented.  If num_comp_calls is not equal
395 to num_delete plus num_retrieve it means that your hash function is
396 generating hashes that are the same for different values.  It is
397 probably worth changing your hash function if this is the case because
398 even if your hash table has 10 items in a 'bucket', it can be searched
399 with 10 \fBunsigned long\fR compares and 10 linked list traverses.  This
400 will be much less expensive that 10 calls to your compare function.
401 .PP
402 \&\fIlh_strhash()\fR is a demo string hashing function:
403 .PP
404 .Vb 1
405 \& unsigned long lh_strhash(const char *c);
406 .Ve
407 .PP
408 Since the \fB\s-1LHASH\s0\fR routines would normally be passed structures, this
409 routine would not normally be passed to \fIlh_new()\fR, rather it would be
410 used in the function passed to \fIlh_new()\fR.
411 .SH "SEE ALSO"
412 .IX Header "SEE ALSO"
413 \&\fIlh_stats\fR\|(3)
414 .SH "HISTORY"
415 .IX Header "HISTORY"
416 The \fBlhash\fR library is available in all versions of SSLeay and OpenSSL.
417 \&\fIlh_error()\fR was added in SSLeay 0.9.1b.
418 .PP
419 This manpage is derived from the SSLeay documentation.
420 .PP
421 In OpenSSL 0.9.7, all lhash functions that were passed function pointers
422 were changed for better type safety, and the function types \s-1LHASH_COMP_FN_TYPE\s0,
423 \&\s-1LHASH_HASH_FN_TYPE\s0, \s-1LHASH_DOALL_FN_TYPE\s0 and \s-1LHASH_DOALL_ARG_FN_TYPE\s0 
424 became available.