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