25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

192 lines
7.9 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved.
  2. *
  3. * This package is an SSL implementation written
  4. * by Eric Young (eay@cryptsoft.com).
  5. * The implementation was written so as to conform with Netscapes SSL.
  6. *
  7. * This library is free for commercial and non-commercial use as long as
  8. * the following conditions are aheared to. The following conditions
  9. * apply to all code found in this distribution, be it the RC4, RSA,
  10. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  11. * included with this distribution is covered by the same copyright terms
  12. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  13. *
  14. * Copyright remains Eric Young's, and as such any Copyright notices in
  15. * the code are not to be removed.
  16. * If this package is used in a product, Eric Young should be given attribution
  17. * as the author of the parts of the library used.
  18. * This can be in the form of a textual message at program startup or
  19. * in documentation (online or textual) provided with the package.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. * 1. Redistributions of source code must retain the copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. * 3. All advertising materials mentioning features or use of this software
  30. * must display the following acknowledgement:
  31. * "This product includes cryptographic software written by
  32. * Eric Young (eay@cryptsoft.com)"
  33. * The word 'cryptographic' can be left out if the rouines from the library
  34. * being used are not cryptographic related :-).
  35. * 4. If you include any Windows specific code (or a derivative thereof) from
  36. * the apps directory (application code) you must include an acknowledgement:
  37. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  40. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  43. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  45. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  48. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. *
  51. * The licence and distribution terms for any publically available version or
  52. * derivative of this code cannot be changed. i.e. this code cannot simply be
  53. * copied and put under another distribution licence
  54. * [including the GNU Public Licence.] */
  55. #ifndef OPENSSL_HEADER_LHASH_H
  56. #define OPENSSL_HEADER_LHASH_H
  57. #include <openssl/base.h>
  58. #include <openssl/type_check.h>
  59. #if defined(__cplusplus)
  60. extern "C" {
  61. #endif
  62. /* lhash is a traditional, chaining hash table that automatically expands and
  63. * contracts as needed. One should not use the lh_* functions directly, rather
  64. * use the type-safe macro wrappers:
  65. *
  66. * A hash table of a specific type of object has type |LHASH_OF(type)|. This
  67. * can be defined (once) with |DEFINE_LHASH_OF(type)| and declared where needed
  68. * with |DECLARE_LHASH_OF(type)|. For example:
  69. *
  70. * struct foo {
  71. * int bar;
  72. * };
  73. *
  74. * DEFINE_LHASH_OF(struct foo);
  75. *
  76. * Although note that the hash table will contain /pointers/ to |foo|.
  77. *
  78. * A macro will be defined for each of the lh_* functions below. For
  79. * LHASH_OF(foo), the macros would be lh_foo_new, lh_foo_num_items etc. */
  80. #define LHASH_OF(type) struct lhash_st_##type
  81. #define DEFINE_LHASH_OF(type) LHASH_OF(type) { int dummy; }
  82. #define DECLARE_LHASH_OF(type) LHASH_OF(type);
  83. /* The make_macros.sh script in this directory parses the following lines and
  84. * generates the lhash_macros.h file that contains macros for the following
  85. * types of stacks:
  86. *
  87. * LHASH_OF:ASN1_OBJECT
  88. * LHASH_OF:CONF_VALUE
  89. * LHASH_OF:SSL_SESSION */
  90. #define IN_LHASH_H
  91. #include <openssl/lhash_macros.h>
  92. #undef IN_LHASH_H
  93. /* lhash_item_st is an element of a hash chain. It points to the opaque data
  94. * for this element and to the next item in the chain. The linked-list is NULL
  95. * terminated. */
  96. typedef struct lhash_item_st {
  97. void *data;
  98. struct lhash_item_st *next;
  99. /* hash contains the cached, hash value of |data|. */
  100. uint32_t hash;
  101. } LHASH_ITEM;
  102. /* lhash_cmp_func is a comparison function that returns a value equal, or not
  103. * equal, to zero depending on whether |*a| is equal, or not equal to |*b|,
  104. * respectively. Note the difference between this and |stack_cmp_func| in that
  105. * this takes pointers to the objects directly. */
  106. typedef int (*lhash_cmp_func)(const void *a, const void *b);
  107. /* lhash_hash_func is a function that maps an object to a uniformly distributed
  108. * uint32_t. */
  109. typedef uint32_t (*lhash_hash_func)(const void *a);
  110. typedef struct lhash_st {
  111. /* num_items contains the total number of items in the hash table. */
  112. size_t num_items;
  113. /* buckets is an array of |num_buckets| pointers. Each points to the head of
  114. * a chain of LHASH_ITEM objects that have the same hash value, mod
  115. * |num_buckets|. */
  116. LHASH_ITEM **buckets;
  117. /* num_buckets contains the length of |buckets|. This value is always >=
  118. * kMinNumBuckets. */
  119. size_t num_buckets;
  120. /* callback_depth contains the current depth of |lh_doall| or |lh_doall_arg|
  121. * calls. If non-zero then this suppresses resizing of the |buckets| array,
  122. * which would otherwise disrupt the iteration. */
  123. unsigned callback_depth;
  124. lhash_cmp_func comp;
  125. lhash_hash_func hash;
  126. } _LHASH;
  127. /* lh_new returns a new, empty hash table or NULL on error. If |comp| is NULL,
  128. * |strcmp| will be used. If |hash| is NULL, a generic hash function will be
  129. * used. */
  130. OPENSSL_EXPORT _LHASH *lh_new(lhash_hash_func hash, lhash_cmp_func comp);
  131. /* lh_free frees the hash table itself but none of the elements. See
  132. * |lh_doall|. */
  133. OPENSSL_EXPORT void lh_free(_LHASH *lh);
  134. /* lh_num_items returns the number of items in |lh|. */
  135. OPENSSL_EXPORT size_t lh_num_items(const _LHASH *lh);
  136. /* lh_retrieve finds an element equal to |data| in the hash table and returns
  137. * it. If no such element exists, it returns NULL. */
  138. OPENSSL_EXPORT void *lh_retrieve(const _LHASH *lh, const void *data);
  139. /* lh_insert inserts |data| into the hash table. If an existing element is
  140. * equal to |data| (with respect to the comparison function) then |*old_data|
  141. * will be set to that value and it will be replaced. Otherwise, or in the
  142. * event of an error, |*old_data| will be set to NULL. It returns one on
  143. * success or zero in the case of an allocation error. */
  144. OPENSSL_EXPORT int lh_insert(_LHASH *lh, void **old_data, void *data);
  145. /* lh_delete removes an element equal to |data| from the hash table and returns
  146. * it. If no such element is found, it returns NULL. */
  147. OPENSSL_EXPORT void *lh_delete(_LHASH *lh, const void *data);
  148. /* lh_doall calls |func| on each element of the hash table.
  149. * TODO(fork): rename this */
  150. OPENSSL_EXPORT void lh_doall(_LHASH *lh, void (*func)(void *));
  151. /* lh_doall_arg calls |func| on each element of the hash table and also passes
  152. * |arg| as the second argument.
  153. * TODO(fork): rename this */
  154. OPENSSL_EXPORT void lh_doall_arg(_LHASH *lh, void (*func)(void *, void *),
  155. void *arg);
  156. /* lh_strhash is the default hash function which processes NUL-terminated
  157. * strings. */
  158. OPENSSL_EXPORT uint32_t lh_strhash(const char *c);
  159. #if defined(__cplusplus)
  160. } /* extern C */
  161. #endif
  162. #endif /* OPENSSL_HEADER_STACK_H */