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. 8280304Sjkim * 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). 15280304Sjkim * 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. 22280304Sjkim * 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 :-). 37280304Sjkim * 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)" 40280304Sjkim * 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. 52280304Sjkim * 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 59280304Sjkim/* 60280304Sjkim * Header for dynamic hash table routines Author - Eric Young 6155714Skris */ 6255714Skris 6355714Skris#ifndef HEADER_LHASH_H 64280304Sjkim# define HEADER_LHASH_H 6555714Skris 66280304Sjkim# include <openssl/e_os2.h> 67280304Sjkim# ifndef OPENSSL_NO_FP_API 68280304Sjkim# include <stdio.h> 69280304Sjkim# endif 7068651Skris 71280304Sjkim# ifndef OPENSSL_NO_BIO 72280304Sjkim# include <openssl/bio.h> 73280304Sjkim# endif 7468651Skris 7555714Skris#ifdef __cplusplus 7655714Skrisextern "C" { 7755714Skris#endif 7855714Skris 79280304Sjkimtypedef struct lhash_node_st { 80280304Sjkim void *data; 81280304Sjkim struct lhash_node_st *next; 82280304Sjkim# ifndef OPENSSL_NO_HASH_COMP 83280304Sjkim unsigned long hash; 84280304Sjkim# endif 85280304Sjkim} LHASH_NODE; 8655714Skris 87280304Sjkimtypedef int (*LHASH_COMP_FN_TYPE) (const void *, const void *); 88280304Sjkimtypedef unsigned long (*LHASH_HASH_FN_TYPE) (const void *); 89280304Sjkimtypedef void (*LHASH_DOALL_FN_TYPE) (void *); 90280304Sjkimtypedef void (*LHASH_DOALL_ARG_FN_TYPE) (void *, void *); 91109998Smarkm 92280304Sjkim/* 93280304Sjkim * Macros for declaring and implementing type-safe wrappers for LHASH 94280304Sjkim * callbacks. This way, callbacks can be provided to LHASH structures without 95280304Sjkim * function pointer casting and the macro-defined callbacks provide 96280304Sjkim * per-variable casting before deferring to the underlying type-specific 97280304Sjkim * callbacks. NB: It is possible to place a "static" in front of both the 98280304Sjkim * DECLARE and IMPLEMENT macros if the functions are strictly internal. 99280304Sjkim */ 100109998Smarkm 101109998Smarkm/* First: "hash" functions */ 102280304Sjkim# define DECLARE_LHASH_HASH_FN(name, o_type) \ 103280304Sjkim unsigned long name##_LHASH_HASH(const void *); 104280304Sjkim# define IMPLEMENT_LHASH_HASH_FN(name, o_type) \ 105280304Sjkim unsigned long name##_LHASH_HASH(const void *arg) { \ 106280304Sjkim const o_type *a = arg; \ 107280304Sjkim return name##_hash(a); } 108280304Sjkim# define LHASH_HASH_FN(name) name##_LHASH_HASH 109109998Smarkm 110109998Smarkm/* Second: "compare" functions */ 111280304Sjkim# define DECLARE_LHASH_COMP_FN(name, o_type) \ 112280304Sjkim int name##_LHASH_COMP(const void *, const void *); 113280304Sjkim# define IMPLEMENT_LHASH_COMP_FN(name, o_type) \ 114280304Sjkim int name##_LHASH_COMP(const void *arg1, const void *arg2) { \ 115280304Sjkim const o_type *a = arg1; \ 116280304Sjkim const o_type *b = arg2; \ 117280304Sjkim return name##_cmp(a,b); } 118280304Sjkim# define LHASH_COMP_FN(name) name##_LHASH_COMP 119109998Smarkm 120109998Smarkm/* Third: "doall" functions */ 121280304Sjkim# define DECLARE_LHASH_DOALL_FN(name, o_type) \ 122280304Sjkim void name##_LHASH_DOALL(void *); 123280304Sjkim# define IMPLEMENT_LHASH_DOALL_FN(name, o_type) \ 124280304Sjkim void name##_LHASH_DOALL(void *arg) { \ 125280304Sjkim o_type *a = arg; \ 126280304Sjkim name##_doall(a); } 127280304Sjkim# define LHASH_DOALL_FN(name) name##_LHASH_DOALL 128109998Smarkm 129109998Smarkm/* Fourth: "doall_arg" functions */ 130280304Sjkim# define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \ 131280304Sjkim void name##_LHASH_DOALL_ARG(void *, void *); 132280304Sjkim# define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \ 133280304Sjkim void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \ 134280304Sjkim o_type *a = arg1; \ 135280304Sjkim a_type *b = arg2; \ 136280304Sjkim name##_doall_arg(a, b); } 137280304Sjkim# define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG 138109998Smarkm 139280304Sjkimtypedef struct lhash_st { 140280304Sjkim LHASH_NODE **b; 141280304Sjkim LHASH_COMP_FN_TYPE comp; 142280304Sjkim LHASH_HASH_FN_TYPE hash; 143280304Sjkim unsigned int num_nodes; 144280304Sjkim unsigned int num_alloc_nodes; 145280304Sjkim unsigned int p; 146280304Sjkim unsigned int pmax; 147280304Sjkim unsigned long up_load; /* load times 256 */ 148280304Sjkim unsigned long down_load; /* load times 256 */ 149280304Sjkim unsigned long num_items; 150280304Sjkim unsigned long num_expands; 151280304Sjkim unsigned long num_expand_reallocs; 152280304Sjkim unsigned long num_contracts; 153280304Sjkim unsigned long num_contract_reallocs; 154280304Sjkim unsigned long num_hash_calls; 155280304Sjkim unsigned long num_comp_calls; 156280304Sjkim unsigned long num_insert; 157280304Sjkim unsigned long num_replace; 158280304Sjkim unsigned long num_delete; 159280304Sjkim unsigned long num_no_delete; 160280304Sjkim unsigned long num_retrieve; 161280304Sjkim unsigned long num_retrieve_miss; 162280304Sjkim unsigned long num_hash_comps; 163280304Sjkim int error; 164280304Sjkim} _LHASH; /* Do not use _LHASH directly, use LHASH_OF 165280304Sjkim * and friends */ 16655714Skris 167280304Sjkim# define LH_LOAD_MULT 256 16855714Skris 169280304Sjkim/* 170280304Sjkim * Indicates a malloc() error in the last call, this is only bad in 171280304Sjkim * lh_insert(). 172280304Sjkim */ 173280304Sjkim# 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 185280304Sjkim# 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); 189280304Sjkim# endif 19055714Skris 191280304Sjkim# 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); 195280304Sjkim# endif 196238405Sjkim 197238405Sjkim/* Type checking... */ 198238405Sjkim 199280304Sjkim# define LHASH_OF(type) struct lhash_st_##type 200238405Sjkim 201280304Sjkim# define DECLARE_LHASH_OF(type) LHASH_OF(type) { int dummy; } 202238405Sjkim 203280304Sjkim# define CHECKED_LHASH_OF(type,lh) \ 204238405Sjkim ((_LHASH *)CHECKED_PTR_OF(LHASH_OF(type),lh)) 205238405Sjkim 206238405Sjkim/* Define wrapper functions. */ 207280304Sjkim# define LHM_lh_new(type, name) \ 208238405Sjkim ((LHASH_OF(type) *)lh_new(LHASH_HASH_FN(name), LHASH_COMP_FN(name))) 209280304Sjkim# define LHM_lh_error(type, lh) \ 210238405Sjkim lh_error(CHECKED_LHASH_OF(type,lh)) 211280304Sjkim# define LHM_lh_insert(type, lh, inst) \ 212238405Sjkim ((type *)lh_insert(CHECKED_LHASH_OF(type, lh), \ 213280304Sjkim CHECKED_PTR_OF(type, inst))) 214280304Sjkim# define LHM_lh_retrieve(type, lh, inst) \ 215238405Sjkim ((type *)lh_retrieve(CHECKED_LHASH_OF(type, lh), \ 216280304Sjkim CHECKED_PTR_OF(type, inst))) 217280304Sjkim# define LHM_lh_delete(type, lh, inst) \ 218280304Sjkim ((type *)lh_delete(CHECKED_LHASH_OF(type, lh), \ 219280304Sjkim CHECKED_PTR_OF(type, inst))) 220280304Sjkim# define LHM_lh_doall(type, lh,fn) lh_doall(CHECKED_LHASH_OF(type, lh), fn) 221280304Sjkim# 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)) 223280304Sjkim# define LHM_lh_num_items(type, lh) lh_num_items(CHECKED_LHASH_OF(type, lh)) 224280304Sjkim# define LHM_lh_down_load(type, lh) (CHECKED_LHASH_OF(type, lh)->down_load) 225280304Sjkim# define LHM_lh_node_stats_bio(type, lh, out) \ 226238405Sjkim lh_node_stats_bio(CHECKED_LHASH_OF(type, lh), out) 227280304Sjkim# define LHM_lh_node_usage_stats_bio(type, lh, out) \ 228238405Sjkim lh_node_usage_stats_bio(CHECKED_LHASH_OF(type, lh), out) 229280304Sjkim# define LHM_lh_stats_bio(type, lh, out) \ 230238405Sjkim lh_stats_bio(CHECKED_LHASH_OF(type, lh), out) 231280304Sjkim# 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