~ubuntu-branches/ubuntu/saucy/clamav/saucy

« back to all changes in this revision

Viewing changes to libclamav/hashtab.h

  • Committer: Bazaar Package Importer
  • Author(s): Leonel Nunez
  • Date: 2008-02-11 22:52:13 UTC
  • mfrom: (1.1.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20080211225213-p2uwj4czso1w2f8h
Tags: upstream-0.92~dfsg
Import upstream version 0.92~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  HTML Entity & Encoding normalization.
3
3
 *
4
 
 *  Copyright (C) 2007-2008 Sourcefire, Inc.
5
 
 *
6
 
 *  Authors: Török Edvin
 
4
 *  Copyright (C) 2006 T�r�k Edvin <edwin@clamav.net>
7
5
 *
8
6
 *  This program is free software; you can redistribute it and/or modify
9
 
 *  it under the terms of the GNU General Public License version 2 as
 
7
 *  it under the terms of the GNU General Public License version 2 as 
10
8
 *  published by the Free Software Foundation.
11
9
 *
12
10
 *  This program is distributed in the hope that it will be useful,
18
16
 *  along with this program; if not, write to the Free Software
19
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
18
 *  MA 02110-1301, USA.
 
19
 *
21
20
 */
22
21
 
 
22
#include <stdio.h>
 
23
#include <stddef.h>
23
24
#ifndef _HASHTAB_H
24
25
#define _HASHTAB_H
25
 
#include <stdio.h>
26
 
#include <stddef.h>
27
 
#include <sys/types.h>
28
 
#include "cltypes.h"
29
 
#include "mpool.h"
30
 
typedef long cli_element_data;
 
26
 
 
27
typedef long element_data;
31
28
 
32
29
/* define this for debugging/profiling purposes only, NOT in production/release code */
33
30
#ifdef PROFILE_HASHTABLE
60
57
#define STRUCT_PROFILE
61
58
 
62
59
#endif
63
 
struct cli_element
 
60
struct element 
64
61
{
65
 
        const char* key;
66
 
        cli_element_data data;
67
 
        size_t len;
 
62
        const unsigned char* key;
 
63
        element_data data;
68
64
};
69
65
 
70
 
struct cli_hashtable {
71
 
        struct cli_element* htable;
 
66
struct hashtable {
 
67
        struct element* htable;
72
68
        size_t capacity;
73
69
        size_t used;
74
70
        size_t maxfill;/* 80% */
76
72
        STRUCT_PROFILE
77
73
};
78
74
 
79
 
int cli_hashtab_generate_c(const struct cli_hashtable *s,const char* name);
80
 
struct cli_element* cli_hashtab_find(const struct cli_hashtable *s, const char* key, const size_t len);
81
 
int cli_hashtab_init(struct cli_hashtable *s,size_t capacity);
82
 
const struct cli_element* cli_hashtab_insert(struct cli_hashtable *s, const char* key, const size_t len, const cli_element_data data);
83
 
void cli_hashtab_delete(struct cli_hashtable *s,const char* key,const size_t len);
84
 
void cli_hashtab_clear(struct cli_hashtable *s);
85
 
void cli_hashtab_free(struct cli_hashtable *s);
86
 
int cli_hashtab_load(FILE* in, struct cli_hashtable *s);
87
 
int cli_hashtab_store(const struct cli_hashtable *s,FILE* out);
88
 
 
89
 
 
90
 
struct cli_htu32_element {
91
 
    uint32_t key;
92
 
    union {
93
 
        unsigned long as_ulong;
94
 
        void *as_ptr;
95
 
    } data;
96
 
};
97
 
 
98
 
struct cli_htu32 {
99
 
    struct cli_htu32_element* htable;
100
 
    size_t capacity;
101
 
    size_t used;
102
 
    size_t maxfill;/* 80% */
103
 
 
104
 
    STRUCT_PROFILE
105
 
};
106
 
 
107
 
#ifndef USE_MPOOL
108
 
#define cli_htu32_init(A, B, C) cli_htu32_init(A, B)
109
 
#define cli_htu32_insert(A, B, C) cli_htu32_insert(A, B)
110
 
#define cli_htu32_free(A, B) cli_htu32_free(A)
111
 
#endif
112
 
int cli_htu32_init(struct cli_htu32 *s, size_t capacity, mpool_t *mempool);
113
 
int cli_htu32_insert(struct cli_htu32 *s, const struct cli_htu32_element *item, mpool_t *mempool);
114
 
const struct cli_htu32_element *cli_htu32_find(const struct cli_htu32 *s, uint32_t key);
115
 
void cli_htu32_delete(struct cli_htu32 *s, uint32_t key);
116
 
void cli_htu32_clear(struct cli_htu32 *s);
117
 
void cli_htu32_free(struct cli_htu32 *s, mpool_t *mempool);
118
 
const struct cli_htu32_element *cli_htu32_next(const struct cli_htu32 *s, const struct cli_htu32_element *current);
119
 
size_t cli_htu32_numitems(struct cli_htu32 *s);
120
 
 
121
 
 
122
 
/* a hashtable that stores the values too */
123
 
struct cli_map_value {
124
 
    void *value;
125
 
    int32_t valuesize;
126
 
};
127
 
 
128
 
struct cli_map {
129
 
    struct cli_hashtable htab;
130
 
    union {
131
 
        struct cli_map_value *unsized_values;
132
 
        void *sized_values;
133
 
    } u;
134
 
    uint32_t nvalues;
135
 
    int32_t keysize;
136
 
    int32_t valuesize;
137
 
    int32_t last_insert;
138
 
    int32_t last_find;
139
 
};
140
 
int cli_map_init(struct cli_map *m, int32_t keysize, int32_t valuesize,
141
 
                  int32_t capacity);
142
 
int  cli_map_addkey(struct cli_map *m, const void *key, int32_t keysize);
143
 
int  cli_map_removekey(struct cli_map *m, const void *key, int32_t keysize);
144
 
int  cli_map_setvalue(struct cli_map *m, const void* value, int32_t valuesize);
145
 
int  cli_map_find(struct cli_map *m, const void *key, int32_t keysize);
146
 
int  cli_map_getvalue_size(struct cli_map *m);
147
 
void*  cli_map_getvalue(struct cli_map *m);
148
 
void cli_map_delete(struct cli_map *m);
149
 
 
150
 
/* A set of unique keys. */
151
 
struct cli_hashset {
152
 
        uint32_t* keys;
153
 
        uint32_t* bitmap;
154
 
        mpool_t* mempool;
155
 
        uint32_t capacity;
156
 
        uint32_t mask;
157
 
        uint32_t count;
158
 
        uint32_t limit;
159
 
};
160
 
 
161
 
int cli_hashset_init(struct cli_hashset* hs, size_t initial_capacity, uint8_t load_factor);
162
 
int cli_hashset_init_pool(struct cli_hashset* hs, size_t initial_capacity, uint8_t load_factor, mpool_t *mempool);
163
 
int cli_hashset_addkey(struct cli_hashset* hs, const uint32_t key);
164
 
int cli_hashset_removekey(struct cli_hashset* hs, const uint32_t key);
165
 
int cli_hashset_contains(const struct cli_hashset* hs, const uint32_t key);
166
 
int cli_hashset_clear(struct cli_hashset* hs);
167
 
void cli_hashset_destroy(struct cli_hashset* hs);
168
 
ssize_t cli_hashset_toarray(const struct cli_hashset* hs, uint32_t** array);
169
 
int cli_hashset_removekey(struct cli_hashset* hs, const uint32_t key);
170
 
 
171
 
/* Initializes the set without allocating memory, you can do lookups on it
172
 
 * using _contains_maybe_noalloc. You need to initialize it using _init
173
 
 * before using _addkey or _removekey though */
174
 
void cli_hashset_init_noalloc(struct cli_hashset *hs);
175
 
/* this works like its _contains counterpart above, except that the hashset may
176
 
 * have not been initialized by _init, only by _init_noalloc */
177
 
int cli_hashset_contains_maybe_noalloc(const struct cli_hashset *hs, const uint32_t key);
 
75
 
 
76
 
 
77
int hashtab_generate_c(const struct hashtable *s,const char* name);
 
78
struct element* hashtab_find(const struct hashtable *s,const unsigned char* key,const size_t len);
 
79
int hashtab_init(struct hashtable *s,size_t capacity);
 
80
int hashtab_insert(struct hashtable *s,const unsigned char* key,size_t len,element_data data);
 
81
void hashtab_delete(struct hashtable *s,const unsigned char* key,const size_t len);
 
82
void hashtab_clear(struct hashtable *s);
 
83
 
 
84
int hashtab_load(FILE* in, struct hashtable *s);
 
85
int hashtab_store(const struct hashtable *s,FILE* out);
 
86
 
178
87
#endif
179
88