-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel0.c
More file actions
367 lines (294 loc) · 8.6 KB
/
level0.c
File metadata and controls
367 lines (294 loc) · 8.6 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// http://www.splicd.com/AAEJedBQk7s/19/43
//
//#define GEN_FILTER 0x5ca1ab1e
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
/* config options */
/* 2^FILTER_SIZE is the size of the filter in bits, i.e.,
* size 20 = 2^20 bits = 1 048 576 bits = 131 072 bytes = 128 KB */
#define FILTER_SIZE 23
#define NUM_HASHES 7
#define WORD_BUF_SIZE 32
#define FILTER_SIZE_BYTES (1 << (FILTER_SIZE - 3))
#define FILTER_BITMASK ((1 << FILTER_SIZE) - 1)
/* hash functions */
unsigned int RSHash (unsigned char *, unsigned int);
unsigned int DJBHash (unsigned char *, unsigned int);
unsigned int FNVHash (unsigned char *, unsigned int);
unsigned int JSHash (unsigned char *, unsigned int);
unsigned int PJWHash (unsigned char *, unsigned int);
uint32_t SFHash (unsigned char *, unsigned int);
unsigned int SDBMHash(unsigned char *, unsigned int);
unsigned int DEKHash (unsigned char *, unsigned int);
/* helper functions */
void err(char *msg, ...);
void load_words(unsigned char[], char *);
void insert_word(unsigned char[], char *);
unsigned int in_dict(unsigned char[], char *);
void get_hashes(unsigned int[], char *);
int main(void)
{
register unsigned int n;
#ifdef GEN_FILTER
printf("/* Bloom filter generated with 2^%d filter size.\n", FILTER_SIZE);
printf(" And %d hash functions. */\n", NUM_HASHES);
#include "dict.c"
char *p;
unsigned char filter[FILTER_SIZE_BYTES];
n = 0;
while (n<234936) {
if ((p=strchr(dict[n], '\r'))) *p='\0';
if ((p=strchr(dict[n], '\n'))) *p='\0';
insert_word(filter, dict[n]);
n++;
}
n = 0;
printf("unsigned char filter[FILTER_SIZE_BYTES] = {");
for (n = 0; n < sizeof(filter); n++) {
printf("0x%x,", filter[n]);
}
printf("};\n");
#else
#include "filter.c"
#endif
#define I_BUFSIZE (int)(4096 * 4)
#define O_BUFSIZE (int)(4096 * 2)
char output[O_BUFSIZE];
setvbuf(stdout, output, _IOFBF, sizeof(output));
char wordbuf[40];
register unsigned int wordbuf_i = 0;
char checkbuf[40];
register unsigned int checkbuf_i = 0;
char * buf = malloc(I_BUFSIZE);
char c;
n = fread(buf, 1, I_BUFSIZE, stdin);
register unsigned int i;
for (i=0; i<n; i++) {
c = buf[i];
if (c == '\0') {
break;
}
wordbuf[wordbuf_i] = c;
checkbuf[checkbuf_i] = c | (char)0x20;
if (c == ' ' || c == '\n') {
if (wordbuf_i != 0) {
wordbuf[wordbuf_i] = '\0';
checkbuf[checkbuf_i] = '\0';
if (!in_dict(filter, checkbuf)) {
putc('<', stdout);
fwrite(wordbuf, wordbuf_i, 1, stdout);
putc('>', stdout);
} else {
fwrite(wordbuf, wordbuf_i, 1, stdout);
}
// Compiler probably already does this
wordbuf_i ^= wordbuf_i;
checkbuf_i ^= checkbuf_i;
}
putc(c, stdout);
} else {
wordbuf_i++;
checkbuf_i++;
}
}
fflush(stdout);
return 0;
}
void err(char *msg, ...)
{
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
exit(-1);
}
void insert_word(unsigned char filter[], char *str)
{
unsigned int hash[NUM_HASHES];
int i;
get_hashes(hash, str);
for (i = 0; i < NUM_HASHES; i++) {
/* xor-fold the hash into FILTER_SIZE bits */
hash[i] = (hash[i] >> FILTER_SIZE) ^
(hash[i] & FILTER_BITMASK);
/* set the bit in the filter */
filter[hash[i] >> 3] |= 1 << (hash[i] & 7);
}
}
inline unsigned int in_dict(unsigned char filter[], char *str)
{
unsigned int hash[NUM_HASHES];
register unsigned int i;
get_hashes(hash, str);
for (i = 0; i < NUM_HASHES; i++) {
hash[i] = (hash[i] >> FILTER_SIZE) ^
(hash[i] & FILTER_BITMASK);
if (!(filter[hash[i] >> 3] & (1 << (hash[i] & 7))))
return 0;
}
return 1;
}
void get_hashes(unsigned int hash[], char *in)
{
unsigned char *str = (unsigned char *)in;
register unsigned int pos = strlen(in);
hash[0] = RSHash (str, pos);
hash[1] = DJBHash (str, pos);
hash[2] = FNVHash (str, pos);
hash[3] = JSHash (str, pos);
hash[4] = SFHash (str, pos);
hash[5] = SDBMHash(str, pos);
hash[6] = DEKHash (str, pos);
hash[7] = PJWHash (str, pos);
}
inline unsigned int RSHash(unsigned char *str, unsigned int len)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = hash * a + (*str);
a = a * b;
}
return hash;
}
inline unsigned int JSHash(unsigned char *str, unsigned int len)
{
unsigned int hash = 1315423911;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash ^= ((hash << 5) + (*str) + (hash >> 2));
}
return hash;
}
inline unsigned int PJWHash(unsigned char *str, unsigned int len)
{
const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
const unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
const unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
const unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (hash << OneEighth) + (*str);
if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return hash;
}
inline unsigned int SDBMHash(unsigned char *str, unsigned int len)
{
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (*str) + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
inline unsigned int DJBHash(unsigned char *str, unsigned int len)
{
unsigned int hash = 5381;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) + hash) + (*str);
}
return hash;
}
inline unsigned int DEKHash(unsigned char *str, unsigned int len)
{
unsigned int hash = len;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
}
return hash;
}
inline unsigned int FNVHash(unsigned char *str, unsigned int len)
{
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash *= fnv_prime;
hash ^= (*str);
}
return hash;
}
#undef get16bits
#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
|| defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
#define get16bits(d) (*((const uint16_t *) (d)))
#endif
#if !defined (get16bits)
#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
+(uint32_t)(((const uint8_t *)(d))[0]) )
#endif
// SuperFastHash (sounds legit)
// http://www.azillionmonkeys.com/qed/hash.html
uint32_t SFHash (unsigned char * data, unsigned int len) {
uint32_t hash = len, tmp;
int rem;
if (len <= 0 || data == NULL) return 0;
rem = len & 3;
len >>= 2;
/* Main loop */
for (; len > 0; len--) {
hash += get16bits (data);
tmp = (get16bits (data+2) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
data += 2*sizeof (uint16_t);
hash += hash >> 11;
}
/* Handle end cases */
switch (rem) {
case 3:
hash += get16bits (data);
hash ^= hash << 16;
hash ^= ((signed char)data[sizeof (uint16_t)]) << 18;
hash += hash >> 11;
break;
case 2:
hash += get16bits (data);
hash ^= hash << 11;
hash += hash >> 17;
break;
case 1:
hash += (signed char)*data;
hash ^= hash << 10;
hash += hash >> 1;
}
/* Force "avalanching" of final 127 bits */
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
uint32_t toupper2(uint32_t eax)
{
uint32_t ebx = (0x7f7f7f7ful & eax) + 0x05050505ul;
ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
ebx = ((ebx & ~eax) >> 2 ) & 0x20202020ul;
return eax - ebx;
}
//
// http://www.splicd.com/gwx87LLZib8/20/30
//