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