summaryrefslogtreecommitdiff
path: root/src/utils/dict.c
blob: b4ee11a815f89973f47596a8c56e61ff3fd9fbdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * This file is part of LibParserUtils.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <assert.h>
#include <string.h>

#include <parserutils/utils/dict.h>

#include "utils/rbtree.h"
#include "utils/utils.h"

/**
 * Dictionary object
 */
struct parserutils_dict
{
#define TABLE_SIZE (79)
	parserutils_rbtree *table[TABLE_SIZE]; /**< Hashtable of entries */

	parserutils_alloc alloc;	/**< Memory (de)allocation function */
	void *pw;			/**< Client-specific data */
};

static inline uint32_t dict_hash(const uint8_t *data, size_t len);
static int dict_cmp(const void *a, const void *b);
static void dict_del(void *key, void *value, void *pw);

/**
 * Create a dictionary
 *
 * \param alloc  Memory (de)allocation function
 * \param pw     Pointer to client-specific private data
 * \param dict   Pointer to location to receive dictionary instance
 * \return PARSERUTILS_OK on success,
 *         PARSERUTILS_NOMEM on memory exhaustion,
 *         PARSERUTILS_BADPARM on bad parameters.
 */
parserutils_error parserutils_dict_create(parserutils_alloc alloc, void *pw,
		parserutils_dict **dict)
{
	parserutils_dict *d;

	if (alloc == NULL || dict == NULL)
		return PARSERUTILS_BADPARM;

	d = alloc(NULL, sizeof(parserutils_dict), pw);
	if (d == NULL)
		return PARSERUTILS_NOMEM;

	memset(d->table, 0, TABLE_SIZE * sizeof(parserutils_rbtree *));

	d->alloc = alloc;
	d->pw = pw;

	*dict = d;

	return PARSERUTILS_OK;
}

/**
 * Destroy a dictionary
 *
 * \param dict  The dictionary instance to destroy
 * \return CSS_OK on success, appropriate error otherwise
 */
parserutils_error parserutils_dict_destroy(parserutils_dict *dict)
{
	int i;
	if (dict == NULL)
		return PARSERUTILS_BADPARM;

	for (i = 0; i < TABLE_SIZE; i++) {
		parserutils_rbtree_destroy(dict->table[i], dict_del, dict);
	}

	dict->alloc(dict, 0, dict->pw);

	return PARSERUTILS_OK;
}

/**
 * Insert an item into a dictionary
 *
 * \param dict    The dictionary to insert into
 * \param data    Pointer to data
 * \param len     Length, in bytes, of data
 * \param result  Pointer to location to receive pointer to data in dictionary
 */
parserutils_error parserutils_dict_insert(parserutils_dict *dict,
	       const uint8_t *data, size_t len, 
	       const parserutils_dict_entry **result)
{
	parserutils_error error;
	parserutils_dict_entry *entry = NULL;
	void *old_value;
	uint32_t index;

	if (dict == NULL || data == NULL || len == 0)
		return PARSERUTILS_BADPARM;

	index = dict_hash(data, len) % TABLE_SIZE;

	if (dict->table[index] != NULL) {
		parserutils_dict_entry search;
		search.len = len;
		search.data = (uint8_t *) data;

		error = parserutils_rbtree_find(dict->table[index], 
				(void *) &search, (void *) &entry);
		if (error != PARSERUTILS_OK)
			return error;

		if (entry != NULL) {
			*result = entry;
			return PARSERUTILS_OK;
		}
	} else {
		error = parserutils_rbtree_create(dict_cmp, 
				dict->alloc, dict->pw, &dict->table[index]);
		if (error != PARSERUTILS_OK)
			return error;
	}

	entry = dict->alloc(NULL, sizeof(parserutils_dict_entry) + len, 
			dict->pw);
	if (entry == NULL)
		return PARSERUTILS_NOMEM;

	/* Do-it-yourself variable-sized data member (simplifies the 
	 * manufacture of an entry to search for, above) */
	memcpy(((uint8_t *) entry) + sizeof(parserutils_dict_entry), data, len);
	entry->data = ((uint8_t *) entry) + sizeof(parserutils_dict_entry);
	entry->len = len;

	error = parserutils_rbtree_insert(dict->table[index], (void *) entry, 
			(void *) entry, &old_value);
	if (error != PARSERUTILS_OK) {
		dict->alloc(entry, 0, dict->pw);
		return error;
	}
	assert(old_value == NULL);

	*result = entry;

	return PARSERUTILS_OK;
}

/**
 * Hsieh hash function
 *
 * \param data  Pointer to data to hash
 * \param len   Length, in bytes, of data
 * \return hash value
 */
uint32_t dict_hash(const uint8_t *data, size_t len)
{
	uint32_t hash = len, tmp;
	int rem = len & 3;

#define read16(d) ((((uint32_t)((d)[1])) << 8) | ((uint32_t)((d)[0])))

	for (len = len / 4; len > 0; len--) {
		hash += read16(data);
		tmp   = (read16(data + 2) << 11) ^ hash;
		hash  = (hash << 16) ^ tmp;
		data += 4;
		hash += hash >> 11;
	}

	switch (rem) {
	case 3:
		hash += read16(data);
		hash ^= hash << 16;
		hash ^= data[2] << 18;
		hash += hash >> 11;
		break;
	case 2:
		hash += read16(data);
		hash ^= hash << 11;
		hash += hash >> 17;
		break;
	case 1:
		hash += data[0];
		hash ^= hash << 10;
		hash += hash >> 1;
	}

#undef read16

	hash ^= hash << 3;
	hash += hash >> 5;
	hash ^= hash << 4;
	hash += hash >> 17;
	hash ^= hash << 25;
	hash += hash >> 6;

	return hash;
}

/**
 * Comparison function for dictionary entries
 *
 * \param a  First entry to consider
 * \param b  Second entry to consider
 * \return <0 iff a<b, ==0 iff a=b, >0 iff a>b
 */
int dict_cmp(const void *a, const void *b)
{
	const parserutils_dict_entry *aa = (const parserutils_dict_entry *) a;
	const parserutils_dict_entry *bb = (const parserutils_dict_entry *) b;
	int result = aa->len - bb->len;

	/* Sort first by length, and then by data equality */
	if (result == 0) {
		result = memcmp(aa->data, bb->data, aa->len);
	}

	return result;
}

/**
 * Destructor for dictionary entries
 *
 * \param key    Key
 * \param value  Value
 * \param pw     Dictionary instance
 */
void dict_del(void *key, void *value, void *pw)
{
	parserutils_dict *dict = (parserutils_dict *) pw;

	UNUSED(value);

	dict->alloc(key, 0, dict->pw);
}

#ifndef NDEBUG
#include <stdio.h>

extern void parserutils_dict_dump(parserutils_dict *dict);

/**
 * Print out a dictionary entry
 *
 * \param key    The key
 * \param value  The value
 * \param depth  Depth in tree
 */
static void dict_print(const void *key, const void *value, int depth)
{
	const parserutils_dict_entry *e = (const parserutils_dict_entry *) key;

	UNUSED(value);

	while (depth-- > 0)
		putchar(' ');

	printf("'%.*s'\n", (int)e->len, e->data);
}

/**
 * Print out a dictionary
 * 
 * \param dict  The dictionary to print
 */
void parserutils_dict_dump(parserutils_dict *dict)
{
	int i;

	if (dict == NULL)
		return;

	for (i = 0; i < TABLE_SIZE; i++) {
		printf("%d:\n", i);
		parserutils_rbtree_dump(dict->table[i], dict_print);
	}
}
#endif