1 /****************************************************************************
2  *
3  * Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
4  * Copyright (C) 2003-2013 Sourcefire, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License Version 2 as
8  * published by the Free Software Foundation.  You may not use, modify or
9  * distribute this program under any other version of the GNU General
10  * Public License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *
21  ****************************************************************************/
22 
23 /*
24 *
25 *  sfghash.h
26 *
27 *  generic hash table - stores and maps key + data pairs
28 *
29 *  Author: Marc Norton
30 *
31 */
32 
33 #ifndef _SFGHASH_
34 #define _SFGHASH_
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 
40 #include "sfhashfcn.h"
41 
42 /*
43 *   ERROR DEFINES
44 */
45 #define SFGHASH_NOMEM    -2
46 #define SFGHASH_ERR      -1
47 #define SFGHASH_OK        0
48 #define SFGHASH_INTABLE   1
49 
50 /*
51 *  Flags for ghash_new: userkeys
52 */
53 #define GH_COPYKEYS 0
54 #define GH_USERKEYS 1
55 
56 /*
57 *   Generic HASH NODE
58 */
59 typedef struct _sfghash_node
60 {
61   struct _sfghash_node * next, * prev;
62 
63   const void * key;   /* Copy of, or Pointer to, the Users key */
64   void *data;  /* The users data, this is never copied! */
65 
66 } SFGHASH_NODE;
67 
68 /*
69 *    Generic HASH table
70 */
71 typedef struct _sfghash
72 {
73   SFHASHFCN    * sfhashfcn;
74   int          keysize;   /* bytes in key, if < 0 -> keys are strings */
75   int          userkey;   /* user owns the key */
76 
77   SFGHASH_NODE ** table;  /* array of node ptr's */
78   int             nrows;  /* # rows int the hash table use a prime number 211, 9871 */
79 
80   unsigned       count;  /* total # nodes in table */
81 
82   void         (*userfree)( void * );
83 
84   int            crow;    /* findfirst/next row in table */
85   SFGHASH_NODE * cnode; /* findfirst/next node ptr */
86 
87   int splay;
88 
89 } SFGHASH, SFDICT;
90 
91 
92 /*
93 *   HASH PROTOTYPES
94 */
95 SFGHASH * sfghash_new( int nrows, int keysize, int userkeys, void (*userfree)(void*p) );
96 void      sfghash_delete( SFGHASH * h );
97 int       sfghash_add( SFGHASH * t, const void * const key, void * const data );
98 int       sfghash_remove( SFGHASH * h, const void * const key);
99 int       sfghash_count( SFGHASH * h);
100 void    * sfghash_find( SFGHASH * h, const void * const key );
101 SFGHASH_NODE * sfghash_find_node( SFGHASH * t, const void * const key);
102 int       sfghash_find2(SFGHASH *, void *, void **);
103 SFGHASH_NODE * sfghash_findfirst( SFGHASH * h );
104 SFGHASH_NODE * sfghash_findnext ( SFGHASH * h );
105 
106 int sfghash_set_keyops( SFGHASH *h ,
107                         unsigned (*hash_fcn)( SFHASHFCN * p,
108                                               unsigned char *d,
109                                               int n),
110                         int (*keycmp_fcn)( const void *s1,
111                                            const void *s2,
112                                            size_t n));
113 
114 
115 #endif
116 
117