155714Skris/* crypto/lhash/lhash.h */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296341Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296341Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296341Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296341Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296341Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59296341Sdelphij/*
60296341Sdelphij * Header for dynamic hash table routines Author - Eric Young
6155714Skris */
6255714Skris
6355714Skris#ifndef HEADER_LHASH_H
64296341Sdelphij# define HEADER_LHASH_H
6555714Skris
66296341Sdelphij# include <openssl/e_os2.h>
67296341Sdelphij# ifndef OPENSSL_NO_FP_API
68296341Sdelphij#  include <stdio.h>
69296341Sdelphij# endif
7068651Skris
71296341Sdelphij# ifndef OPENSSL_NO_BIO
72296341Sdelphij#  include <openssl/bio.h>
73296341Sdelphij# endif
7468651Skris
7555714Skris#ifdef  __cplusplus
7655714Skrisextern "C" {
7755714Skris#endif
7855714Skris
79296341Sdelphijtypedef struct lhash_node_st {
80296341Sdelphij    void *data;
81296341Sdelphij    struct lhash_node_st *next;
82296341Sdelphij# ifndef OPENSSL_NO_HASH_COMP
83296341Sdelphij    unsigned long hash;
84296341Sdelphij# endif
85296341Sdelphij} LHASH_NODE;
8655714Skris
87296341Sdelphijtypedef int (*LHASH_COMP_FN_TYPE) (const void *, const void *);
88296341Sdelphijtypedef unsigned long (*LHASH_HASH_FN_TYPE) (const void *);
89296341Sdelphijtypedef void (*LHASH_DOALL_FN_TYPE) (void *);
90296341Sdelphijtypedef void (*LHASH_DOALL_ARG_FN_TYPE) (void *, void *);
91109998Smarkm
92296341Sdelphij/*
93296341Sdelphij * Macros for declaring and implementing type-safe wrappers for LHASH
94296341Sdelphij * callbacks. This way, callbacks can be provided to LHASH structures without
95296341Sdelphij * function pointer casting and the macro-defined callbacks provide
96296341Sdelphij * per-variable casting before deferring to the underlying type-specific
97296341Sdelphij * callbacks. NB: It is possible to place a "static" in front of both the
98296341Sdelphij * DECLARE and IMPLEMENT macros if the functions are strictly internal.
99296341Sdelphij */
100109998Smarkm
101109998Smarkm/* First: "hash" functions */
102296341Sdelphij# define DECLARE_LHASH_HASH_FN(name, o_type) \
103296341Sdelphij        unsigned long name##_LHASH_HASH(const void *);
104296341Sdelphij# define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
105296341Sdelphij        unsigned long name##_LHASH_HASH(const void *arg) { \
106296341Sdelphij                const o_type *a = arg; \
107296341Sdelphij                return name##_hash(a); }
108296341Sdelphij# define LHASH_HASH_FN(name) name##_LHASH_HASH
109109998Smarkm
110109998Smarkm/* Second: "compare" functions */
111296341Sdelphij# define DECLARE_LHASH_COMP_FN(name, o_type) \
112296341Sdelphij        int name##_LHASH_COMP(const void *, const void *);
113296341Sdelphij# define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
114296341Sdelphij        int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
115296341Sdelphij                const o_type *a = arg1;             \
116296341Sdelphij                const o_type *b = arg2; \
117296341Sdelphij                return name##_cmp(a,b); }
118296341Sdelphij# define LHASH_COMP_FN(name) name##_LHASH_COMP
119109998Smarkm
120109998Smarkm/* Third: "doall" functions */
121296341Sdelphij# define DECLARE_LHASH_DOALL_FN(name, o_type) \
122296341Sdelphij        void name##_LHASH_DOALL(void *);
123296341Sdelphij# define IMPLEMENT_LHASH_DOALL_FN(name, o_type) \
124296341Sdelphij        void name##_LHASH_DOALL(void *arg) { \
125296341Sdelphij                o_type *a = arg; \
126296341Sdelphij                name##_doall(a); }
127296341Sdelphij# define LHASH_DOALL_FN(name) name##_LHASH_DOALL
128109998Smarkm
129109998Smarkm/* Fourth: "doall_arg" functions */
130296341Sdelphij# define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
131296341Sdelphij        void name##_LHASH_DOALL_ARG(void *, void *);
132296341Sdelphij# define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
133296341Sdelphij        void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
134296341Sdelphij                o_type *a = arg1; \
135296341Sdelphij                a_type *b = arg2; \
136296341Sdelphij                name##_doall_arg(a, b); }
137296341Sdelphij# define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
138109998Smarkm
139296341Sdelphijtypedef struct lhash_st {
140296341Sdelphij    LHASH_NODE **b;
141296341Sdelphij    LHASH_COMP_FN_TYPE comp;
142296341Sdelphij    LHASH_HASH_FN_TYPE hash;
143296341Sdelphij    unsigned int num_nodes;
144296341Sdelphij    unsigned int num_alloc_nodes;
145296341Sdelphij    unsigned int p;
146296341Sdelphij    unsigned int pmax;
147296341Sdelphij    unsigned long up_load;      /* load times 256 */
148296341Sdelphij    unsigned long down_load;    /* load times 256 */
149296341Sdelphij    unsigned long num_items;
150296341Sdelphij    unsigned long num_expands;
151296341Sdelphij    unsigned long num_expand_reallocs;
152296341Sdelphij    unsigned long num_contracts;
153296341Sdelphij    unsigned long num_contract_reallocs;
154296341Sdelphij    unsigned long num_hash_calls;
155296341Sdelphij    unsigned long num_comp_calls;
156296341Sdelphij    unsigned long num_insert;
157296341Sdelphij    unsigned long num_replace;
158296341Sdelphij    unsigned long num_delete;
159296341Sdelphij    unsigned long num_no_delete;
160296341Sdelphij    unsigned long num_retrieve;
161296341Sdelphij    unsigned long num_retrieve_miss;
162296341Sdelphij    unsigned long num_hash_comps;
163296341Sdelphij    int error;
164296341Sdelphij} _LHASH;                       /* Do not use _LHASH directly, use LHASH_OF
165296341Sdelphij                                 * and friends */
16655714Skris
167296341Sdelphij# define LH_LOAD_MULT    256
16855714Skris
169296341Sdelphij/*
170296341Sdelphij * Indicates a malloc() error in the last call, this is only bad in
171296341Sdelphij * lh_insert().
172296341Sdelphij */
173296341Sdelphij# define lh_error(lh)    ((lh)->error)
17455714Skris
175238405Sjkim_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c);
176238405Sjkimvoid lh_free(_LHASH *lh);
177238405Sjkimvoid *lh_insert(_LHASH *lh, void *data);
178238405Sjkimvoid *lh_delete(_LHASH *lh, const void *data);
179238405Sjkimvoid *lh_retrieve(_LHASH *lh, const void *data);
180238405Sjkimvoid lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func);
181238405Sjkimvoid lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg);
18255714Skrisunsigned long lh_strhash(const char *c);
183238405Sjkimunsigned long lh_num_items(const _LHASH *lh);
18455714Skris
185296341Sdelphij# ifndef OPENSSL_NO_FP_API
186238405Sjkimvoid lh_stats(const _LHASH *lh, FILE *out);
187238405Sjkimvoid lh_node_stats(const _LHASH *lh, FILE *out);
188238405Sjkimvoid lh_node_usage_stats(const _LHASH *lh, FILE *out);
189296341Sdelphij# endif
19055714Skris
191296341Sdelphij# ifndef OPENSSL_NO_BIO
192238405Sjkimvoid lh_stats_bio(const _LHASH *lh, BIO *out);
193238405Sjkimvoid lh_node_stats_bio(const _LHASH *lh, BIO *out);
194238405Sjkimvoid lh_node_usage_stats_bio(const _LHASH *lh, BIO *out);
195296341Sdelphij# endif
196238405Sjkim
197238405Sjkim/* Type checking... */
198238405Sjkim
199296341Sdelphij# define LHASH_OF(type) struct lhash_st_##type
200238405Sjkim
201296341Sdelphij# define DECLARE_LHASH_OF(type) LHASH_OF(type) { int dummy; }
202238405Sjkim
203296341Sdelphij# define CHECKED_LHASH_OF(type,lh) \
204238405Sjkim  ((_LHASH *)CHECKED_PTR_OF(LHASH_OF(type),lh))
205238405Sjkim
206238405Sjkim/* Define wrapper functions. */
207296341Sdelphij# define LHM_lh_new(type, name) \
208238405Sjkim  ((LHASH_OF(type) *)lh_new(LHASH_HASH_FN(name), LHASH_COMP_FN(name)))
209296341Sdelphij# define LHM_lh_error(type, lh) \
210238405Sjkim  lh_error(CHECKED_LHASH_OF(type,lh))
211296341Sdelphij# define LHM_lh_insert(type, lh, inst) \
212238405Sjkim  ((type *)lh_insert(CHECKED_LHASH_OF(type, lh), \
213296341Sdelphij                     CHECKED_PTR_OF(type, inst)))
214296341Sdelphij# define LHM_lh_retrieve(type, lh, inst) \
215238405Sjkim  ((type *)lh_retrieve(CHECKED_LHASH_OF(type, lh), \
216296341Sdelphij                       CHECKED_PTR_OF(type, inst)))
217296341Sdelphij# define LHM_lh_delete(type, lh, inst) \
218296341Sdelphij  ((type *)lh_delete(CHECKED_LHASH_OF(type, lh),                        \
219296341Sdelphij                     CHECKED_PTR_OF(type, inst)))
220296341Sdelphij# define LHM_lh_doall(type, lh,fn) lh_doall(CHECKED_LHASH_OF(type, lh), fn)
221296341Sdelphij# define LHM_lh_doall_arg(type, lh, fn, arg_type, arg) \
222238405Sjkim  lh_doall_arg(CHECKED_LHASH_OF(type, lh), fn, CHECKED_PTR_OF(arg_type, arg))
223296341Sdelphij# define LHM_lh_num_items(type, lh) lh_num_items(CHECKED_LHASH_OF(type, lh))
224296341Sdelphij# define LHM_lh_down_load(type, lh) (CHECKED_LHASH_OF(type, lh)->down_load)
225296341Sdelphij# define LHM_lh_node_stats_bio(type, lh, out) \
226238405Sjkim  lh_node_stats_bio(CHECKED_LHASH_OF(type, lh), out)
227296341Sdelphij# define LHM_lh_node_usage_stats_bio(type, lh, out) \
228238405Sjkim  lh_node_usage_stats_bio(CHECKED_LHASH_OF(type, lh), out)
229296341Sdelphij# define LHM_lh_stats_bio(type, lh, out) \
230238405Sjkim  lh_stats_bio(CHECKED_LHASH_OF(type, lh), out)
231296341Sdelphij# define LHM_lh_free(type, lh) lh_free(CHECKED_LHASH_OF(type, lh))
232238405Sjkim
233238405SjkimDECLARE_LHASH_OF(OPENSSL_STRING);
234238405SjkimDECLARE_LHASH_OF(OPENSSL_CSTRING);
235238405Sjkim
23655714Skris#ifdef  __cplusplus
23755714Skris}
23855714Skris#endif
23955714Skris
24055714Skris#endif
241