You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

283 line
16 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #ifndef OPENSSL_HEADER_LHASH_H
  57. #define OPENSSL_HEADER_LHASH_H
  58. #include <openssl/base.h>
  59. #include <openssl/type_check.h>
  60. #if defined(__cplusplus)
  61. extern "C" {
  62. #endif
  63. // lhash is a traditional, chaining hash table that automatically expands and
  64. // contracts as needed. One should not use the lh_* functions directly, rather
  65. // use the type-safe macro wrappers:
  66. //
  67. // A hash table of a specific type of object has type |LHASH_OF(type)|. This
  68. // can be defined (once) with |DEFINE_LHASH_OF(type)| and declared where needed
  69. // with |DECLARE_LHASH_OF(type)|. For example:
  70. //
  71. // struct foo {
  72. // int bar;
  73. // };
  74. //
  75. // DEFINE_LHASH_OF(struct foo);
  76. //
  77. // Although note that the hash table will contain /pointers/ to |foo|.
  78. //
  79. // A macro will be defined for each of the lh_* functions below. For
  80. // LHASH_OF(foo), the macros would be lh_foo_new, lh_foo_num_items etc.
  81. #define LHASH_OF(type) struct lhash_st_##type
  82. #define DECLARE_LHASH_OF(type) LHASH_OF(type);
  83. // lhash_item_st is an element of a hash chain. It points to the opaque data
  84. // for this element and to the next item in the chain. The linked-list is NULL
  85. // terminated.
  86. typedef struct lhash_item_st {
  87. void *data;
  88. struct lhash_item_st *next;
  89. // hash contains the cached, hash value of |data|.
  90. uint32_t hash;
  91. } LHASH_ITEM;
  92. // lhash_cmp_func is a comparison function that returns a value equal, or not
  93. // equal, to zero depending on whether |*a| is equal, or not equal to |*b|,
  94. // respectively. Note the difference between this and |stack_cmp_func| in that
  95. // this takes pointers to the objects directly.
  96. //
  97. // This function's actual type signature is int (*)(const T*, const T*). The
  98. // low-level |lh_*| functions will be passed a type-specific wrapper to call it
  99. // correctly.
  100. typedef int (*lhash_cmp_func)(const void *a, const void *b);
  101. typedef int (*lhash_cmp_func_helper)(lhash_cmp_func func, const void *a,
  102. const void *b);
  103. // lhash_hash_func is a function that maps an object to a uniformly distributed
  104. // uint32_t.
  105. //
  106. // This function's actual type signature is uint32_t (*)(const T*). The
  107. // low-level |lh_*| functions will be passed a type-specific wrapper to call it
  108. // correctly.
  109. typedef uint32_t (*lhash_hash_func)(const void *a);
  110. typedef uint32_t (*lhash_hash_func_helper)(lhash_hash_func func, const void *a);
  111. typedef struct lhash_st _LHASH;
  112. // lh_new returns a new, empty hash table or NULL on error.
  113. OPENSSL_EXPORT _LHASH *lh_new(lhash_hash_func hash, lhash_cmp_func comp);
  114. // lh_free frees the hash table itself but none of the elements. See
  115. // |lh_doall|.
  116. OPENSSL_EXPORT void lh_free(_LHASH *lh);
  117. // lh_num_items returns the number of items in |lh|.
  118. OPENSSL_EXPORT size_t lh_num_items(const _LHASH *lh);
  119. // lh_retrieve finds an element equal to |data| in the hash table and returns
  120. // it. If no such element exists, it returns NULL.
  121. OPENSSL_EXPORT void *lh_retrieve(const _LHASH *lh, const void *data,
  122. lhash_hash_func_helper call_hash_func,
  123. lhash_cmp_func_helper call_cmp_func);
  124. // lh_retrieve_key finds an element matching |key|, given the specified hash and
  125. // comparison function. This differs from |lh_retrieve| in that the key may be a
  126. // different type than the values stored in |lh|. |key_hash| and |cmp_key| must
  127. // be compatible with the functions passed into |lh_new|.
  128. OPENSSL_EXPORT void *lh_retrieve_key(const _LHASH *lh, const void *key,
  129. uint32_t key_hash,
  130. int (*cmp_key)(const void *key,
  131. const void *value));
  132. // lh_insert inserts |data| into the hash table. If an existing element is
  133. // equal to |data| (with respect to the comparison function) then |*old_data|
  134. // will be set to that value and it will be replaced. Otherwise, or in the
  135. // event of an error, |*old_data| will be set to NULL. It returns one on
  136. // success or zero in the case of an allocation error.
  137. OPENSSL_EXPORT int lh_insert(_LHASH *lh, void **old_data, void *data,
  138. lhash_hash_func_helper call_hash_func,
  139. lhash_cmp_func_helper call_cmp_func);
  140. // lh_delete removes an element equal to |data| from the hash table and returns
  141. // it. If no such element is found, it returns NULL.
  142. OPENSSL_EXPORT void *lh_delete(_LHASH *lh, const void *data,
  143. lhash_hash_func_helper call_hash_func,
  144. lhash_cmp_func_helper call_cmp_func);
  145. // lh_doall_arg calls |func| on each element of the hash table and also passes
  146. // |arg| as the second argument.
  147. // TODO(fork): rename this
  148. OPENSSL_EXPORT void lh_doall_arg(_LHASH *lh, void (*func)(void *, void *),
  149. void *arg);
  150. // lh_strhash is the default hash function which processes NUL-terminated
  151. // strings.
  152. OPENSSL_EXPORT uint32_t lh_strhash(const char *c);
  153. #define DEFINE_LHASH_OF(type) \
  154. DECLARE_LHASH_OF(type) \
  155. \
  156. typedef int (*lhash_##type##_cmp_func)(const type *, const type *); \
  157. typedef uint32_t (*lhash_##type##_hash_func)(const type *); \
  158. \
  159. OPENSSL_INLINE int lh_##type##_call_cmp_func(lhash_cmp_func func, \
  160. const void *a, const void *b) { \
  161. return ((lhash_##type##_cmp_func)func)((const type *)a, (const type *)b); \
  162. } \
  163. \
  164. OPENSSL_INLINE uint32_t lh_##type##_call_hash_func(lhash_hash_func func, \
  165. const void *a) { \
  166. return ((lhash_##type##_hash_func)func)((const type *)a); \
  167. } \
  168. \
  169. OPENSSL_INLINE LHASH_OF(type) * \
  170. lh_##type##_new(lhash_##type##_hash_func hash, \
  171. lhash_##type##_cmp_func comp) { \
  172. return (LHASH_OF(type) *)lh_new((lhash_hash_func)hash, \
  173. (lhash_cmp_func)comp); \
  174. } \
  175. \
  176. OPENSSL_INLINE void lh_##type##_free(LHASH_OF(type) *lh) { \
  177. lh_free((_LHASH *)lh); \
  178. } \
  179. \
  180. OPENSSL_INLINE size_t lh_##type##_num_items(const LHASH_OF(type) *lh) { \
  181. return lh_num_items((const _LHASH *)lh); \
  182. } \
  183. \
  184. OPENSSL_INLINE type *lh_##type##_retrieve(const LHASH_OF(type) *lh, \
  185. const type *data) { \
  186. return (type *)lh_retrieve((const _LHASH *)lh, data, \
  187. lh_##type##_call_hash_func, \
  188. lh_##type##_call_cmp_func); \
  189. } \
  190. \
  191. typedef struct { \
  192. int (*cmp_key)(const void *key, const type *value); \
  193. const void *key; \
  194. } LHASH_CMP_KEY_##type; \
  195. \
  196. OPENSSL_INLINE int lh_##type##_call_cmp_key(const void *key, \
  197. const void *value) { \
  198. const LHASH_CMP_KEY_##type *cb = (const LHASH_CMP_KEY_##type *)key; \
  199. return cb->cmp_key(cb->key, (const type *)value); \
  200. } \
  201. \
  202. OPENSSL_INLINE type *lh_##type##_retrieve_key( \
  203. const LHASH_OF(type) *lh, const void *key, uint32_t key_hash, \
  204. int (*cmp_key)(const void *key, const type *value)) { \
  205. LHASH_CMP_KEY_##type cb = {cmp_key, key}; \
  206. return (type *)lh_retrieve_key((const _LHASH *)lh, &cb, key_hash, \
  207. lh_##type##_call_cmp_key); \
  208. } \
  209. \
  210. OPENSSL_INLINE int lh_##type##_insert(LHASH_OF(type) *lh, type **old_data, \
  211. type *data) { \
  212. void *old_data_void = NULL; \
  213. int ret = \
  214. lh_insert((_LHASH *)lh, &old_data_void, data, \
  215. lh_##type##_call_hash_func, lh_##type##_call_cmp_func); \
  216. *old_data = (type *)old_data_void; \
  217. return ret; \
  218. } \
  219. \
  220. OPENSSL_INLINE type *lh_##type##_delete(LHASH_OF(type) *lh, \
  221. const type *data) { \
  222. return (type *)lh_delete((_LHASH *)lh, data, lh_##type##_call_hash_func, \
  223. lh_##type##_call_cmp_func); \
  224. } \
  225. \
  226. typedef struct { \
  227. void (*doall)(type *); \
  228. void (*doall_arg)(type *, void *); \
  229. void *arg; \
  230. } LHASH_DOALL_##type; \
  231. \
  232. OPENSSL_INLINE void lh_##type##_call_doall(void *value, void *arg) { \
  233. const LHASH_DOALL_##type *cb = (const LHASH_DOALL_##type *)arg; \
  234. cb->doall((type *)value); \
  235. } \
  236. \
  237. OPENSSL_INLINE void lh_##type##_call_doall_arg(void *value, void *arg) { \
  238. const LHASH_DOALL_##type *cb = (const LHASH_DOALL_##type *)arg; \
  239. cb->doall_arg((type *)value, cb->arg); \
  240. } \
  241. \
  242. OPENSSL_INLINE void lh_##type##_doall(LHASH_OF(type) *lh, \
  243. void (*func)(type *)) { \
  244. LHASH_DOALL_##type cb = {func, NULL, NULL}; \
  245. lh_doall_arg((_LHASH *)lh, lh_##type##_call_doall, &cb); \
  246. } \
  247. \
  248. OPENSSL_INLINE void lh_##type##_doall_arg( \
  249. LHASH_OF(type) *lh, void (*func)(type *, void *), void *arg) { \
  250. LHASH_DOALL_##type cb = {NULL, func, arg}; \
  251. lh_doall_arg((_LHASH *)lh, lh_##type##_call_doall_arg, &cb); \
  252. }
  253. #if defined(__cplusplus)
  254. } // extern C
  255. #endif
  256. #endif // OPENSSL_HEADER_LHASH_H