Signed receipt request function documentation.
[openssl.git] / doc / crypto / lhash.pod
1 =pod
2
3 =head1 NAME
4
5 lh_new, lh_free, lh_insert, lh_delete, lh_retrieve, lh_doall, lh_doall_arg, lh_error - dynamic hash table
6
7 =head1 SYNOPSIS
8
9  #include <openssl/lhash.h>
10
11  LHASH *lh_new(LHASH_HASH_FN_TYPE hash, LHASH_COMP_FN_TYPE compare);
12  void lh_free(LHASH *table);
13
14  void *lh_insert(LHASH *table, void *data);
15  void *lh_delete(LHASH *table, void *data);
16  void *lh_retrieve(LHASH *table, void *data);
17
18  void lh_doall(LHASH *table, LHASH_DOALL_FN_TYPE func);
19  void lh_doall_arg(LHASH *table, LHASH_DOALL_ARG_FN_TYPE func,
20           void *arg);
21
22  int lh_error(LHASH *table);
23
24  typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *);
25  typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *);
26  typedef void (*LHASH_DOALL_FN_TYPE)(const void *);
27  typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
28
29 =head1 DESCRIPTION
30
31 This library implements dynamic hash tables. The hash table entries
32 can be arbitrary structures. Usually they consist of key and value
33 fields.
34
35 lh_new() creates a new B<LHASH> structure to store arbitrary data
36 entries, and provides the 'hash' and 'compare' callbacks to be used in
37 organising the table's entries.  The B<hash> callback takes a pointer
38 to a table entry as its argument and returns an unsigned long hash
39 value for its key field.  The hash value is normally truncated to a
40 power of 2, so make sure that your hash function returns well mixed
41 low order bits.  The B<compare> callback takes two arguments (pointers
42 to two hash table entries), and returns 0 if their keys are equal,
43 non-zero otherwise.  If your hash table will contain items of some
44 particular type and the B<hash> and B<compare> callbacks hash/compare
45 these types, then the B<DECLARE_LHASH_HASH_FN> and
46 B<IMPLEMENT_LHASH_COMP_FN> macros can be used to create callback
47 wrappers of the prototypes required by lh_new().  These provide
48 per-variable casts before calling the type-specific callbacks written
49 by the application author.  These macros, as well as those used for
50 the "doall" callbacks, are defined as;
51
52  #define DECLARE_LHASH_HASH_FN(f_name,o_type) \
53          unsigned long f_name##_LHASH_HASH(const void *);
54  #define IMPLEMENT_LHASH_HASH_FN(f_name,o_type) \
55          unsigned long f_name##_LHASH_HASH(const void *arg) { \
56                  o_type a = (o_type)arg; \
57                  return f_name(a); }
58  #define LHASH_HASH_FN(f_name) f_name##_LHASH_HASH
59
60  #define DECLARE_LHASH_COMP_FN(f_name,o_type) \
61          int f_name##_LHASH_COMP(const void *, const void *);
62  #define IMPLEMENT_LHASH_COMP_FN(f_name,o_type) \
63          int f_name##_LHASH_COMP(const void *arg1, const void *arg2) { \
64                  o_type a = (o_type)arg1; \
65                  o_type b = (o_type)arg2; \
66                  return f_name(a,b); }
67  #define LHASH_COMP_FN(f_name) f_name##_LHASH_COMP
68
69  #define DECLARE_LHASH_DOALL_FN(f_name,o_type) \
70          void f_name##_LHASH_DOALL(const void *);
71  #define IMPLEMENT_LHASH_DOALL_FN(f_name,o_type) \
72          void f_name##_LHASH_DOALL(const void *arg) { \
73                  o_type a = (o_type)arg; \
74                  f_name(a); }
75  #define LHASH_DOALL_FN(f_name) f_name##_LHASH_DOALL
76
77  #define DECLARE_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \
78          void f_name##_LHASH_DOALL_ARG(const void *, const void *);
79  #define IMPLEMENT_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \
80          void f_name##_LHASH_DOALL_ARG(const void *arg1, const void *arg2) { \
81                  o_type a = (o_type)arg1; \
82                  a_type b = (a_type)arg2; \
83                  f_name(a,b); }
84  #define LHASH_DOALL_ARG_FN(f_name) f_name##_LHASH_DOALL_ARG
85
86 An example of a hash table storing (pointers to) structures of type 'STUFF'
87 could be defined as follows;
88
89  /* Calculates the hash value of 'tohash' (implemented elsewhere) */
90  unsigned long STUFF_hash(const STUFF *tohash);
91  /* Orders 'arg1' and 'arg2' (implemented elsewhere) */
92  int STUFF_cmp(const STUFF *arg1, const STUFF *arg2);
93  /* Create the type-safe wrapper functions for use in the LHASH internals */
94  static IMPLEMENT_LHASH_HASH_FN(STUFF_hash, const STUFF *)
95  static IMPLEMENT_LHASH_COMP_FN(STUFF_cmp, const STUFF *);
96  /* ... */
97  int main(int argc, char *argv[]) {
98          /* Create the new hash table using the hash/compare wrappers */
99          LHASH *hashtable = lh_new(LHASH_HASH_FN(STUFF_hash),
100                                    LHASH_COMP_FN(STUFF_cmp));
101          /* ... */
102  }
103
104 lh_free() frees the B<LHASH> structure B<table>. Allocated hash table
105 entries will not be freed; consider using lh_doall() to deallocate any
106 remaining entries in the hash table (see below).
107
108 lh_insert() inserts the structure pointed to by B<data> into B<table>.
109 If there already is an entry with the same key, the old value is
110 replaced. Note that lh_insert() stores pointers, the data are not
111 copied.
112
113 lh_delete() deletes an entry from B<table>.
114
115 lh_retrieve() looks up an entry in B<table>. Normally, B<data> is
116 a structure with the key field(s) set; the function will return a
117 pointer to a fully populated structure.
118
119 lh_doall() will, for every entry in the hash table, call B<func> with
120 the data item as its parameter.  For lh_doall() and lh_doall_arg(),
121 function pointer casting should be avoided in the callbacks (see
122 B<NOTE>) - instead, either declare the callbacks to match the
123 prototype required in lh_new() or use the declare/implement macros to
124 create type-safe wrappers that cast variables prior to calling your
125 type-specific callbacks.  An example of this is illustrated here where
126 the callback is used to cleanup resources for items in the hash table
127 prior to the hashtable itself being deallocated:
128
129  /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
130  void STUFF_cleanup(STUFF *a);
131  /* Implement a prototype-compatible wrapper for "STUFF_cleanup" */
132  IMPLEMENT_LHASH_DOALL_FN(STUFF_cleanup, STUFF *)
133          /* ... then later in the code ... */
134  /* So to run "STUFF_cleanup" against all items in a hash table ... */
135  lh_doall(hashtable, LHASH_DOALL_FN(STUFF_cleanup));
136  /* Then the hash table itself can be deallocated */
137  lh_free(hashtable);
138
139 When doing this, be careful if you delete entries from the hash table
140 in your callbacks: the table may decrease in size, moving the item
141 that you are currently on down lower in the hash table - this could
142 cause some entries to be skipped during the iteration.  The second
143 best solution to this problem is to set hash-E<gt>down_load=0 before
144 you start (which will stop the hash table ever decreasing in size).
145 The best solution is probably to avoid deleting items from the hash
146 table inside a "doall" callback!
147
148 lh_doall_arg() is the same as lh_doall() except that B<func> will be
149 called with B<arg> as the second argument and B<func> should be of
150 type B<LHASH_DOALL_ARG_FN_TYPE> (a callback prototype that is passed
151 both the table entry and an extra argument).  As with lh_doall(), you
152 can instead choose to declare your callback with a prototype matching
153 the types you are dealing with and use the declare/implement macros to
154 create compatible wrappers that cast variables before calling your
155 type-specific callbacks.  An example of this is demonstrated here
156 (printing all hash table entries to a BIO that is provided by the
157 caller):
158
159  /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
160  void STUFF_print(const STUFF *a, BIO *output_bio);
161  /* Implement a prototype-compatible wrapper for "STUFF_print" */
162  static IMPLEMENT_LHASH_DOALL_ARG_FN(STUFF_print, const STUFF *, BIO *)
163          /* ... then later in the code ... */
164  /* Print out the entire hashtable to a particular BIO */
165  lh_doall_arg(hashtable, LHASH_DOALL_ARG_FN(STUFF_print), logging_bio);
166  
167 lh_error() can be used to determine if an error occurred in the last
168 operation. lh_error() is a macro.
169
170 =head1 RETURN VALUES
171
172 lh_new() returns B<NULL> on error, otherwise a pointer to the new
173 B<LHASH> structure.
174
175 When a hash table entry is replaced, lh_insert() returns the value
176 being replaced. B<NULL> is returned on normal operation and on error.
177
178 lh_delete() returns the entry being deleted.  B<NULL> is returned if
179 there is no such value in the hash table.
180
181 lh_retrieve() returns the hash table entry if it has been found,
182 B<NULL> otherwise.
183
184 lh_error() returns 1 if an error occurred in the last operation, 0
185 otherwise.
186
187 lh_free(), lh_doall() and lh_doall_arg() return no values.
188
189 =head1 NOTE
190
191 The various LHASH macros and callback types exist to make it possible
192 to write type-safe code without resorting to function-prototype
193 casting - an evil that makes application code much harder to
194 audit/verify and also opens the window of opportunity for stack
195 corruption and other hard-to-find bugs.  It also, apparently, violates
196 ANSI-C.
197
198 The LHASH code regards table entries as constant data.  As such, it
199 internally represents lh_insert()'d items with a "const void *"
200 pointer type.  This is why callbacks such as those used by lh_doall()
201 and lh_doall_arg() declare their prototypes with "const", even for the
202 parameters that pass back the table items' data pointers - for
203 consistency, user-provided data is "const" at all times as far as the
204 LHASH code is concerned.  However, as callers are themselves providing
205 these pointers, they can choose whether they too should be treating
206 all such parameters as constant.
207
208 As an example, a hash table may be maintained by code that, for
209 reasons of encapsulation, has only "const" access to the data being
210 indexed in the hash table (ie. it is returned as "const" from
211 elsewhere in their code) - in this case the LHASH prototypes are
212 appropriate as-is.  Conversely, if the caller is responsible for the
213 life-time of the data in question, then they may well wish to make
214 modifications to table item passed back in the lh_doall() or
215 lh_doall_arg() callbacks (see the "STUFF_cleanup" example above).  If
216 so, the caller can either cast the "const" away (if they're providing
217 the raw callbacks themselves) or use the macros to declare/implement
218 the wrapper functions without "const" types.
219
220 Callers that only have "const" access to data they're indexing in a
221 table, yet declare callbacks without constant types (or cast the
222 "const" away themselves), are therefore creating their own risks/bugs
223 without being encouraged to do so by the API.  On a related note,
224 those auditing code should pay special attention to any instances of
225 DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
226 without any "const" qualifiers.
227
228 =head1 BUGS
229
230 lh_insert() returns B<NULL> both for success and error.
231
232 =head1 INTERNALS
233
234 The following description is based on the SSLeay documentation:
235
236 The B<lhash> library implements a hash table described in the
237 I<Communications of the ACM> in 1991.  What makes this hash table
238 different is that as the table fills, the hash table is increased (or
239 decreased) in size via OPENSSL_realloc().  When a 'resize' is done, instead of
240 all hashes being redistributed over twice as many 'buckets', one
241 bucket is split.  So when an 'expand' is done, there is only a minimal
242 cost to redistribute some values.  Subsequent inserts will cause more
243 single 'bucket' redistributions but there will never be a sudden large
244 cost due to redistributing all the 'buckets'.
245
246 The state for a particular hash table is kept in the B<LHASH> structure.
247 The decision to increase or decrease the hash table size is made
248 depending on the 'load' of the hash table.  The load is the number of
249 items in the hash table divided by the size of the hash table.  The
250 default values are as follows.  If (hash->up_load E<lt> load) =E<gt>
251 expand.  if (hash-E<gt>down_load E<gt> load) =E<gt> contract.  The
252 B<up_load> has a default value of 1 and B<down_load> has a default value
253 of 2.  These numbers can be modified by the application by just
254 playing with the B<up_load> and B<down_load> variables.  The 'load' is
255 kept in a form which is multiplied by 256.  So
256 hash-E<gt>up_load=8*256; will cause a load of 8 to be set.
257
258 If you are interested in performance the field to watch is
259 num_comp_calls.  The hash library keeps track of the 'hash' value for
260 each item so when a lookup is done, the 'hashes' are compared, if
261 there is a match, then a full compare is done, and
262 hash-E<gt>num_comp_calls is incremented.  If num_comp_calls is not equal
263 to num_delete plus num_retrieve it means that your hash function is
264 generating hashes that are the same for different values.  It is
265 probably worth changing your hash function if this is the case because
266 even if your hash table has 10 items in a 'bucket', it can be searched
267 with 10 B<unsigned long> compares and 10 linked list traverses.  This
268 will be much less expensive that 10 calls to your compare function.
269
270 lh_strhash() is a demo string hashing function:
271
272  unsigned long lh_strhash(const char *c);
273
274 Since the B<LHASH> routines would normally be passed structures, this
275 routine would not normally be passed to lh_new(), rather it would be
276 used in the function passed to lh_new().
277
278 =head1 SEE ALSO
279
280 L<lh_stats(3)|lh_stats(3)>
281
282 =head1 HISTORY
283
284 The B<lhash> library is available in all versions of SSLeay and OpenSSL.
285 lh_error() was added in SSLeay 0.9.1b.
286
287 This manpage is derived from the SSLeay documentation.
288
289 In OpenSSL 0.9.7, all lhash functions that were passed function pointers
290 were changed for better type safety, and the function types LHASH_COMP_FN_TYPE,
291 LHASH_HASH_FN_TYPE, LHASH_DOALL_FN_TYPE and LHASH_DOALL_ARG_FN_TYPE 
292 became available.
293
294 =cut