1 | =pod
|
---|
2 |
|
---|
3 | =head1 NAME
|
---|
4 |
|
---|
5 | LHASH, DECLARE_LHASH_OF,
|
---|
6 | OPENSSL_LH_COMPFUNC, OPENSSL_LH_HASHFUNC, OPENSSL_LH_DOALL_FUNC,
|
---|
7 | LHASH_DOALL_ARG_FN_TYPE,
|
---|
8 | IMPLEMENT_LHASH_HASH_FN, IMPLEMENT_LHASH_COMP_FN,
|
---|
9 | lh_TYPE_new, lh_TYPE_free, lh_TYPE_flush,
|
---|
10 | lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve,
|
---|
11 | lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_error,
|
---|
12 | OPENSSL_LH_new, OPENSSL_LH_free, OPENSSL_LH_flush,
|
---|
13 | OPENSSL_LH_insert, OPENSSL_LH_delete, OPENSSL_LH_retrieve,
|
---|
14 | OPENSSL_LH_doall, OPENSSL_LH_doall_arg, OPENSSL_LH_error
|
---|
15 | - dynamic hash table
|
---|
16 |
|
---|
17 | =head1 SYNOPSIS
|
---|
18 |
|
---|
19 | =for openssl generic
|
---|
20 |
|
---|
21 | #include <openssl/lhash.h>
|
---|
22 |
|
---|
23 | DECLARE_LHASH_OF(TYPE);
|
---|
24 |
|
---|
25 | LHASH_OF(TYPE) *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
|
---|
26 | void lh_TYPE_free(LHASH_OF(TYPE) *table);
|
---|
27 | void lh_TYPE_flush(LHASH_OF(TYPE) *table);
|
---|
28 |
|
---|
29 | TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data);
|
---|
30 | TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data);
|
---|
31 | TYPE *lh_TYPE_retrieve(LHASH_OF(TYPE) *table, TYPE *data);
|
---|
32 |
|
---|
33 | void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func);
|
---|
34 | void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
|
---|
35 | TYPE *arg);
|
---|
36 |
|
---|
37 | int lh_TYPE_error(LHASH_OF(TYPE) *table);
|
---|
38 |
|
---|
39 | typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
|
---|
40 | typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
|
---|
41 | typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
|
---|
42 | typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
|
---|
43 |
|
---|
44 | OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
|
---|
45 | void OPENSSL_LH_free(OPENSSL_LHASH *lh);
|
---|
46 | void OPENSSL_LH_flush(OPENSSL_LHASH *lh);
|
---|
47 |
|
---|
48 | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
|
---|
49 | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
|
---|
50 | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);
|
---|
51 |
|
---|
52 | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
|
---|
53 | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);
|
---|
54 |
|
---|
55 | int OPENSSL_LH_error(OPENSSL_LHASH *lh);
|
---|
56 |
|
---|
57 | =head1 DESCRIPTION
|
---|
58 |
|
---|
59 | This library implements type-checked dynamic hash tables. The hash
|
---|
60 | table entries can be arbitrary structures. Usually they consist of key
|
---|
61 | and value fields. In the description here, B<I<TYPE>> is used a placeholder
|
---|
62 | for any of the OpenSSL datatypes, such as I<SSL_SESSION>.
|
---|
63 |
|
---|
64 | B<lh_I<TYPE>_new>() creates a new B<LHASH_OF>(B<I<TYPE>>) structure to store
|
---|
65 | arbitrary data entries, and specifies the 'hash' and 'compare'
|
---|
66 | callbacks to be used in organising the table's entries. The I<hash>
|
---|
67 | callback takes a pointer to a table entry as its argument and returns
|
---|
68 | an unsigned long hash value for its key field. The hash value is
|
---|
69 | normally truncated to a power of 2, so make sure that your hash
|
---|
70 | function returns well mixed low order bits. The I<compare> callback
|
---|
71 | takes two arguments (pointers to two hash table entries), and returns
|
---|
72 | 0 if their keys are equal, nonzero otherwise.
|
---|
73 |
|
---|
74 | If your hash table
|
---|
75 | will contain items of some particular type and the I<hash> and
|
---|
76 | I<compare> callbacks hash/compare these types, then the
|
---|
77 | B<IMPLEMENT_LHASH_HASH_FN> and B<IMPLEMENT_LHASH_COMP_FN> macros can be
|
---|
78 | used to create callback wrappers of the prototypes required by
|
---|
79 | B<lh_I<TYPE>_new>() as shown in this example:
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Implement the hash and compare functions; "stuff" can be any word.
|
---|
83 | */
|
---|
84 | static unsigned long stuff_hash(const TYPE *a)
|
---|
85 | {
|
---|
86 | ...
|
---|
87 | }
|
---|
88 | static int stuff_cmp(const TYPE *a, const TYPE *b)
|
---|
89 | {
|
---|
90 | ...
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * Implement the wrapper functions.
|
---|
95 | */
|
---|
96 | static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
|
---|
97 | static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
|
---|
98 |
|
---|
99 | If the type is going to be used in several places, the following macros
|
---|
100 | can be used in a common header file to declare the function wrappers:
|
---|
101 |
|
---|
102 | DECLARE_LHASH_HASH_FN(stuff, TYPE)
|
---|
103 | DECLARE_LHASH_COMP_FN(stuff, TYPE)
|
---|
104 |
|
---|
105 | Then a hash table of B<I<TYPE>> objects can be created using this:
|
---|
106 |
|
---|
107 | LHASH_OF(TYPE) *htable;
|
---|
108 |
|
---|
109 | htable = B<lh_I<TYPE>_new>(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
|
---|
110 |
|
---|
111 | B<lh_I<TYPE>_free>() frees the B<LHASH_OF>(B<I<TYPE>>) structure
|
---|
112 | I<table>. Allocated hash table entries will not be freed; consider
|
---|
113 | using B<lh_I<TYPE>_doall>() to deallocate any remaining entries in the
|
---|
114 | hash table (see below).
|
---|
115 |
|
---|
116 | B<lh_I<TYPE>_flush>() empties the B<LHASH_OF>(B<I<TYPE>>) structure I<table>. New
|
---|
117 | entries can be added to the flushed table. Allocated hash table entries
|
---|
118 | will not be freed; consider using B<lh_I<TYPE>_doall>() to deallocate any
|
---|
119 | remaining entries in the hash table (see below).
|
---|
120 |
|
---|
121 | B<lh_I<TYPE>_insert>() inserts the structure pointed to by I<data> into
|
---|
122 | I<table>. If there already is an entry with the same key, the old
|
---|
123 | value is replaced. Note that B<lh_I<TYPE>_insert>() stores pointers, the
|
---|
124 | data are not copied.
|
---|
125 |
|
---|
126 | B<lh_I<TYPE>_delete>() deletes an entry from I<table>.
|
---|
127 |
|
---|
128 | B<lh_I<TYPE>_retrieve>() looks up an entry in I<table>. Normally, I<data>
|
---|
129 | is a structure with the key field(s) set; the function will return a
|
---|
130 | pointer to a fully populated structure.
|
---|
131 |
|
---|
132 | B<lh_I<TYPE>_doall>() will, for every entry in the hash table, call
|
---|
133 | I<func> with the data item as its parameter.
|
---|
134 | For example:
|
---|
135 |
|
---|
136 | /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
|
---|
137 | void TYPE_cleanup_doall(TYPE *a);
|
---|
138 |
|
---|
139 | /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
|
---|
140 | IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
|
---|
141 |
|
---|
142 | /* Call "TYPE_cleanup" against all items in a hash table. */
|
---|
143 | lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
|
---|
144 |
|
---|
145 | /* Then the hash table itself can be deallocated */
|
---|
146 | lh_TYPE_free(hashtable);
|
---|
147 |
|
---|
148 | When doing this, be careful if you delete entries from the hash table
|
---|
149 | in your callbacks: the table may decrease in size, moving the item
|
---|
150 | that you are currently on down lower in the hash table - this could
|
---|
151 | cause some entries to be skipped during the iteration. The second
|
---|
152 | best solution to this problem is to set hash-E<gt>down_load=0 before
|
---|
153 | you start (which will stop the hash table ever decreasing in size).
|
---|
154 | The best solution is probably to avoid deleting items from the hash
|
---|
155 | table inside a "doall" callback!
|
---|
156 |
|
---|
157 | B<lh_I<TYPE>_doall_arg>() is the same as B<lh_I<TYPE>_doall>() except that
|
---|
158 | I<func> will be called with I<arg> as the second argument and I<func>
|
---|
159 | should be of type B<LHASH_DOALL_ARG_FN>(B<I<TYPE>>) (a callback prototype
|
---|
160 | that is passed both the table entry and an extra argument). As with
|
---|
161 | lh_doall(), you can instead choose to declare your callback with a
|
---|
162 | prototype matching the types you are dealing with and use the
|
---|
163 | declare/implement macros to create compatible wrappers that cast
|
---|
164 | variables before calling your type-specific callbacks. An example of
|
---|
165 | this is demonstrated here (printing all hash table entries to a BIO
|
---|
166 | that is provided by the caller):
|
---|
167 |
|
---|
168 | /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
|
---|
169 | void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
|
---|
170 |
|
---|
171 | /* Implement a prototype-compatible wrapper for "TYPE_print" */
|
---|
172 | static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
|
---|
173 |
|
---|
174 | /* Print out the entire hashtable to a particular BIO */
|
---|
175 | lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
|
---|
176 | logging_bio);
|
---|
177 |
|
---|
178 |
|
---|
179 | B<lh_I<TYPE>_error>() can be used to determine if an error occurred in the last
|
---|
180 | operation.
|
---|
181 |
|
---|
182 | OPENSSL_LH_new() is the same as the B<lh_I<TYPE>_new>() except that it is not
|
---|
183 | type specific. So instead of returning an B<LHASH_OF(I<TYPE>)> value it returns
|
---|
184 | a B<void *>. In the same way the functions OPENSSL_LH_free(),
|
---|
185 | OPENSSL_LH_flush(), OPENSSL_LH_insert(), OPENSSL_LH_delete(),
|
---|
186 | OPENSSL_LH_retrieve(), OPENSSL_LH_doall(), OPENSSL_LH_doall_arg(), and
|
---|
187 | OPENSSL_LH_error() are equivalent to the similarly named B<lh_I<TYPE>> functions
|
---|
188 | except that they return or use a B<void *> where the equivalent B<lh_I<TYPE>>
|
---|
189 | function returns or uses a B<I<TYPE> *> or B<LHASH_OF(I<TYPE>) *>. B<lh_I<TYPE>>
|
---|
190 | functions are implemented as type checked wrappers around the B<OPENSSL_LH>
|
---|
191 | functions. Most applications should not call the B<OPENSSL_LH> functions
|
---|
192 | directly.
|
---|
193 |
|
---|
194 | =head1 RETURN VALUES
|
---|
195 |
|
---|
196 | B<lh_I<TYPE>_new>() and OPENSSL_LH_new() return NULL on error, otherwise a
|
---|
197 | pointer to the new B<LHASH> structure.
|
---|
198 |
|
---|
199 | When a hash table entry is replaced, B<lh_I<TYPE>_insert>() or
|
---|
200 | OPENSSL_LH_insert() return the value being replaced. NULL is returned on normal
|
---|
201 | operation and on error.
|
---|
202 |
|
---|
203 | B<lh_I<TYPE>_delete>() and OPENSSL_LH_delete() return the entry being deleted.
|
---|
204 | NULL is returned if there is no such value in the hash table.
|
---|
205 |
|
---|
206 | B<lh_I<TYPE>_retrieve>() and OPENSSL_LH_retrieve() return the hash table entry
|
---|
207 | if it has been found, NULL otherwise.
|
---|
208 |
|
---|
209 | B<lh_I<TYPE>_error>() and OPENSSL_LH_error() return 1 if an error occurred in
|
---|
210 | the last operation, 0 otherwise. It's meaningful only after non-retrieve
|
---|
211 | operations.
|
---|
212 |
|
---|
213 | B<lh_I<TYPE>_free>(), OPENSSL_LH_free(), B<lh_I<TYPE>_flush>(),
|
---|
214 | OPENSSL_LH_flush(), B<lh_I<TYPE>_doall>() OPENSSL_LH_doall(),
|
---|
215 | B<lh_I<TYPE>_doall_arg>() and OPENSSL_LH_doall_arg() return no values.
|
---|
216 |
|
---|
217 | =head1 NOTE
|
---|
218 |
|
---|
219 | The LHASH code is not thread safe. All updating operations, as well as
|
---|
220 | B<lh_I<TYPE>_error>() or OPENSSL_LH_error() calls must be performed under
|
---|
221 | a write lock. All retrieve operations should be performed under a read lock,
|
---|
222 | I<unless> accurate usage statistics are desired. In which case, a write lock
|
---|
223 | should be used for retrieve operations as well. For output of the usage
|
---|
224 | statistics, using the functions from L<OPENSSL_LH_stats(3)>, a read lock
|
---|
225 | suffices.
|
---|
226 |
|
---|
227 | The LHASH code regards table entries as constant data. As such, it
|
---|
228 | internally represents lh_insert()'d items with a "const void *"
|
---|
229 | pointer type. This is why callbacks such as those used by lh_doall()
|
---|
230 | and lh_doall_arg() declare their prototypes with "const", even for the
|
---|
231 | parameters that pass back the table items' data pointers - for
|
---|
232 | consistency, user-provided data is "const" at all times as far as the
|
---|
233 | LHASH code is concerned. However, as callers are themselves providing
|
---|
234 | these pointers, they can choose whether they too should be treating
|
---|
235 | all such parameters as constant.
|
---|
236 |
|
---|
237 | As an example, a hash table may be maintained by code that, for
|
---|
238 | reasons of encapsulation, has only "const" access to the data being
|
---|
239 | indexed in the hash table (i.e. it is returned as "const" from
|
---|
240 | elsewhere in their code) - in this case the LHASH prototypes are
|
---|
241 | appropriate as-is. Conversely, if the caller is responsible for the
|
---|
242 | life-time of the data in question, then they may well wish to make
|
---|
243 | modifications to table item passed back in the lh_doall() or
|
---|
244 | lh_doall_arg() callbacks (see the "TYPE_cleanup" example above). If
|
---|
245 | so, the caller can either cast the "const" away (if they're providing
|
---|
246 | the raw callbacks themselves) or use the macros to declare/implement
|
---|
247 | the wrapper functions without "const" types.
|
---|
248 |
|
---|
249 | Callers that only have "const" access to data they're indexing in a
|
---|
250 | table, yet declare callbacks without constant types (or cast the
|
---|
251 | "const" away themselves), are therefore creating their own risks/bugs
|
---|
252 | without being encouraged to do so by the API. On a related note,
|
---|
253 | those auditing code should pay special attention to any instances of
|
---|
254 | DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
|
---|
255 | without any "const" qualifiers.
|
---|
256 |
|
---|
257 | =head1 BUGS
|
---|
258 |
|
---|
259 | B<lh_I<TYPE>_insert>() and OPENSSL_LH_insert() return NULL both for success
|
---|
260 | and error.
|
---|
261 |
|
---|
262 | =head1 SEE ALSO
|
---|
263 |
|
---|
264 | L<OPENSSL_LH_stats(3)>
|
---|
265 |
|
---|
266 | =head1 HISTORY
|
---|
267 |
|
---|
268 | In OpenSSL 1.0.0, the lhash interface was revamped for better
|
---|
269 | type checking.
|
---|
270 |
|
---|
271 | =head1 COPYRIGHT
|
---|
272 |
|
---|
273 | Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
|
---|
274 |
|
---|
275 | Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
276 | this file except in compliance with the License. You can obtain a copy
|
---|
277 | in the file LICENSE in the source distribution or at
|
---|
278 | L<https://www.openssl.org/source/license.html>.
|
---|
279 |
|
---|
280 | =cut
|
---|