]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - secure/lib/libcrypto/man/OPENSSL_LH_COMPFUNC.3
Merge the remainder of the projects/openssl111 branch to head.
[FreeBSD/FreeBSD.git] / secure / lib / libcrypto / man / OPENSSL_LH_COMPFUNC.3
1 .\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35)
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 >0, 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 .if !\nF .nr F 0
58 .if \nF>0 \{\
59 .    de IX
60 .    tm Index:\\$1\t\\n%\t"\\$2"
61 ..
62 .    if !\nF==2 \{\
63 .        nr % 0
64 .        nr F 2
65 .    \}
66 .\}
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 "OPENSSL_LH_COMPFUNC 3"
132 .TH OPENSSL_LH_COMPFUNC 3 "2018-09-11" "1.1.1" "OpenSSL"
133 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
134 .\" way too many mistakes in technical documents.
135 .if n .ad l
136 .nh
137 .SH "NAME"
138 LHASH, DECLARE_LHASH_OF, OPENSSL_LH_COMPFUNC, OPENSSL_LH_HASHFUNC, OPENSSL_LH_DOALL_FUNC, LHASH_DOALL_ARG_FN_TYPE, IMPLEMENT_LHASH_HASH_FN, IMPLEMENT_LHASH_COMP_FN, lh_TYPE_new, lh_TYPE_free, lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve, lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_error \- dynamic hash table
139 .SH "SYNOPSIS"
140 .IX Header "SYNOPSIS"
141 .Vb 1
142 \& #include <openssl/lhash.h>
143 \&
144 \& DECLARE_LHASH_OF(TYPE);
145 \&
146 \& LHASH *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
147 \& void lh_TYPE_free(LHASH_OF(TYPE) *table);
148 \&
149 \& TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data);
150 \& TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data);
151 \& TYPE *lh_retrieve(LHASH_OF(TYPE) *table, TYPE *data);
152 \&
153 \& void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func);
154 \& void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
155 \&                        TYPE *arg);
156 \&
157 \& int lh_TYPE_error(LHASH_OF(TYPE) *table);
158 \&
159 \& typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
160 \& typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
161 \& typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
162 \& typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
163 .Ve
164 .SH "DESCRIPTION"
165 .IX Header "DESCRIPTION"
166 This library implements type-checked dynamic hash tables. The hash
167 table entries can be arbitrary structures. Usually they consist of key
168 and value fields.  In the description here, \fI\s-1TYPE\s0\fR is used a placeholder
169 for any of the OpenSSL datatypes, such as \fI\s-1SSL_SESSION\s0\fR.
170 .PP
171 \&\fIlh_TYPE_new()\fR creates a new \fB\s-1LHASH_OF\s0(\s-1TYPE\s0)\fR structure to store
172 arbitrary data entries, and specifies the 'hash' and 'compare'
173 callbacks to be used in organising the table's entries.  The \fBhash\fR
174 callback takes a pointer to a table entry as its argument and returns
175 an unsigned long hash value for its key field.  The hash value is
176 normally truncated to a power of 2, so make sure that your hash
177 function returns well mixed low order bits.  The \fBcompare\fR callback
178 takes two arguments (pointers to two hash table entries), and returns
179 0 if their keys are equal, non-zero otherwise.
180 .PP
181 If your hash table
182 will contain items of some particular type and the \fBhash\fR and
183 \&\fBcompare\fR callbacks hash/compare these types, then the
184 \&\fB\s-1IMPLEMENT_LHASH_HASH_FN\s0\fR and \fB\s-1IMPLEMENT_LHASH_COMP_FN\s0\fR macros can be
185 used to create callback wrappers of the prototypes required by
186 \&\fIlh_TYPE_new()\fR as shown in this example:
187 .PP
188 .Vb 11
189 \& /*
190 \&  * Implement the hash and compare functions; "stuff" can be any word.
191 \&  */
192 \& static unsigned long stuff_hash(const TYPE *a)
193 \& {
194 \&     ...
195 \& }
196 \& static int stuff_cmp(const TYPE *a, const TYPE *b)
197 \& {
198 \&     ...
199 \& }
200 \&
201 \& /*
202 \&  * Implement the wrapper functions.
203 \&  */
204 \& static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
205 \& static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
206 .Ve
207 .PP
208 If the type is going to be used in several places, the following macros
209 can be used in a common header file to declare the function wrappers:
210 .PP
211 .Vb 2
212 \& DECLARE_LHASH_HASH_FN(stuff, TYPE)
213 \& DECLARE_LHASH_COMP_FN(stuff, TYPE)
214 .Ve
215 .PP
216 Then a hash table of \s-1TYPE\s0 objects can be created using this:
217 .PP
218 .Vb 1
219 \& LHASH_OF(TYPE) *htable;
220 \&
221 \& htable = lh_TYPE_new(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
222 .Ve
223 .PP
224 \&\fIlh_TYPE_free()\fR frees the \fB\s-1LHASH_OF\s0(\s-1TYPE\s0)\fR structure
225 \&\fBtable\fR. Allocated hash table entries will not be freed; consider
226 using \fIlh_TYPE_doall()\fR to deallocate any remaining entries in the
227 hash table (see below).
228 .PP
229 \&\fIlh_TYPE_insert()\fR inserts the structure pointed to by \fBdata\fR into
230 \&\fBtable\fR.  If there already is an entry with the same key, the old
231 value is replaced. Note that \fIlh_TYPE_insert()\fR stores pointers, the
232 data are not copied.
233 .PP
234 \&\fIlh_TYPE_delete()\fR deletes an entry from \fBtable\fR.
235 .PP
236 \&\fIlh_TYPE_retrieve()\fR looks up an entry in \fBtable\fR. Normally, \fBdata\fR
237 is a structure with the key field(s) set; the function will return a
238 pointer to a fully populated structure.
239 .PP
240 \&\fIlh_TYPE_doall()\fR will, for every entry in the hash table, call
241 \&\fBfunc\fR with the data item as its parameter.
242 For example:
243 .PP
244 .Vb 2
245 \& /* Cleans up resources belonging to \*(Aqa\*(Aq (this is implemented elsewhere) */
246 \& void TYPE_cleanup_doall(TYPE *a);
247 \&
248 \& /* Implement a prototype\-compatible wrapper for "TYPE_cleanup" */
249 \& IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
250 \&
251 \& /* Call "TYPE_cleanup" against all items in a hash table. */
252 \& lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
253 \&
254 \& /* Then the hash table itself can be deallocated */
255 \& lh_TYPE_free(hashtable);
256 .Ve
257 .PP
258 When doing this, be careful if you delete entries from the hash table
259 in your callbacks: the table may decrease in size, moving the item
260 that you are currently on down lower in the hash table \- this could
261 cause some entries to be skipped during the iteration.  The second
262 best solution to this problem is to set hash\->down_load=0 before
263 you start (which will stop the hash table ever decreasing in size).
264 The best solution is probably to avoid deleting items from the hash
265 table inside a \*(L"doall\*(R" callback!
266 .PP
267 \&\fIlh_TYPE_doall_arg()\fR is the same as \fIlh_TYPE_doall()\fR except that
268 \&\fBfunc\fR will be called with \fBarg\fR as the second argument and \fBfunc\fR
269 should be of type \fB\s-1LHASH_DOALL_ARG_FN_TYPE\s0\fR (a callback prototype
270 that is passed both the table entry and an extra argument).  As with
271 \&\fIlh_doall()\fR, you can instead choose to declare your callback with a
272 prototype matching the types you are dealing with and use the
273 declare/implement macros to create compatible wrappers that cast
274 variables before calling your type-specific callbacks.  An example of
275 this is demonstrated here (printing all hash table entries to a \s-1BIO\s0
276 that is provided by the caller):
277 .PP
278 .Vb 2
279 \& /* Prints item \*(Aqa\*(Aq to \*(Aqoutput_bio\*(Aq (this is implemented elsewhere) */
280 \& void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
281 \&
282 \& /* Implement a prototype\-compatible wrapper for "TYPE_print" */
283 \& static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
284 \&
285 \& /* Print out the entire hashtable to a particular BIO */
286 \& lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
287 \&                   logging_bio);
288 .Ve
289 .PP
290 \&\fIlh_TYPE_error()\fR can be used to determine if an error occurred in the last
291 operation.
292 .SH "RETURN VALUES"
293 .IX Header "RETURN VALUES"
294 \&\fIlh_TYPE_new()\fR returns \fB\s-1NULL\s0\fR on error, otherwise a pointer to the new
295 \&\fB\s-1LHASH\s0\fR structure.
296 .PP
297 When a hash table entry is replaced, \fIlh_TYPE_insert()\fR returns the value
298 being replaced. \fB\s-1NULL\s0\fR is returned on normal operation and on error.
299 .PP
300 \&\fIlh_TYPE_delete()\fR returns the entry being deleted.  \fB\s-1NULL\s0\fR is returned if
301 there is no such value in the hash table.
302 .PP
303 \&\fIlh_TYPE_retrieve()\fR returns the hash table entry if it has been found,
304 \&\fB\s-1NULL\s0\fR otherwise.
305 .PP
306 \&\fIlh_TYPE_error()\fR returns 1 if an error occurred in the last operation, 0
307 otherwise. It's meaningful only after non-retrieve operations.
308 .PP
309 \&\fIlh_TYPE_free()\fR, \fIlh_TYPE_doall()\fR and \fIlh_TYPE_doall_arg()\fR return no values.
310 .SH "NOTE"
311 .IX Header "NOTE"
312 The \s-1LHASH\s0 code is not thread safe. All updating operations, as well as
313 lh_TYPE_error call must be performed under a write lock. All retrieve
314 operations should be performed under a read lock, \fIunless\fR accurate
315 usage statistics are desired. In which case, a write lock should be used
316 for retrieve operations as well. For output of the usage statistics,
317 using the functions from \fIOPENSSL_LH_stats\fR\|(3), a read lock suffices.
318 .PP
319 The \s-1LHASH\s0 code regards table entries as constant data.  As such, it
320 internally represents \fIlh_insert()\fR'd items with a \*(L"const void *\*(R"
321 pointer type.  This is why callbacks such as those used by \fIlh_doall()\fR
322 and \fIlh_doall_arg()\fR declare their prototypes with \*(L"const\*(R", even for the
323 parameters that pass back the table items' data pointers \- for
324 consistency, user-provided data is \*(L"const\*(R" at all times as far as the
325 \&\s-1LHASH\s0 code is concerned.  However, as callers are themselves providing
326 these pointers, they can choose whether they too should be treating
327 all such parameters as constant.
328 .PP
329 As an example, a hash table may be maintained by code that, for
330 reasons of encapsulation, has only \*(L"const\*(R" access to the data being
331 indexed in the hash table (ie. it is returned as \*(L"const\*(R" from
332 elsewhere in their code) \- in this case the \s-1LHASH\s0 prototypes are
333 appropriate as-is.  Conversely, if the caller is responsible for the
334 life-time of the data in question, then they may well wish to make
335 modifications to table item passed back in the \fIlh_doall()\fR or
336 \&\fIlh_doall_arg()\fR callbacks (see the \*(L"TYPE_cleanup\*(R" example above).  If
337 so, the caller can either cast the \*(L"const\*(R" away (if they're providing
338 the raw callbacks themselves) or use the macros to declare/implement
339 the wrapper functions without \*(L"const\*(R" types.
340 .PP
341 Callers that only have \*(L"const\*(R" access to data they're indexing in a
342 table, yet declare callbacks without constant types (or cast the
343 \&\*(L"const\*(R" away themselves), are therefore creating their own risks/bugs
344 without being encouraged to do so by the \s-1API.\s0  On a related note,
345 those auditing code should pay special attention to any instances of
346 DECLARE/IMPLEMENT_LHASH_DOALL_[\s-1ARG_\s0]_FN macros that provide types
347 without any \*(L"const\*(R" qualifiers.
348 .SH "BUGS"
349 .IX Header "BUGS"
350 \&\fIlh_TYPE_insert()\fR returns \fB\s-1NULL\s0\fR both for success and error.
351 .SH "SEE ALSO"
352 .IX Header "SEE ALSO"
353 \&\fIOPENSSL_LH_stats\fR\|(3)
354 .SH "HISTORY"
355 .IX Header "HISTORY"
356 In OpenSSL 1.0.0, the lhash interface was revamped for better
357 type checking.
358 .SH "COPYRIGHT"
359 .IX Header "COPYRIGHT"
360 Copyright 2000\-2018 The OpenSSL Project Authors. All Rights Reserved.
361 .PP
362 Licensed under the OpenSSL license (the \*(L"License\*(R").  You may not use
363 this file except in compliance with the License.  You can obtain a copy
364 in the file \s-1LICENSE\s0 in the source distribution or at
365 <https://www.openssl.org/source/license.html>.