-
Notifications
You must be signed in to change notification settings - Fork 0
/
searcher.c
executable file
·430 lines (415 loc) · 12.3 KB
/
searcher.c
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* File: main.c
* Author: hp1
*
* Created on 2012年2月3日, 下午3:27
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <zlib.h>
#include "searcher.h"
#if DEBUG
void showList(void *c) {
int i = 0;
unsigned int *head = (unsigned int *) c;
printf("====List start=====\n");
for (i = 0; i < 256; ++i) {
if (*(head + i) > 0) {
printf("[%c](%d) -> [%d]\n", (unsigned char) i, i, *(head + i));
}
}
printf("====List end=====\n");
}
#endif
int init(void **c, long len) {
size_t full_block = sizeof (unsigned int) * 256 + sizeof (node) * ((len - 256) + 1 + 256);
*c = (unsigned char *) malloc(full_block);
memset(*c, 0, full_block);
return full_block;
}
int release(void *c) {
free(c);
}
void* parseRuleFile(unsigned char* fileName, int *nn) {
void *c = NULL;
FILE *fp = fopen(fileName, "rt");
unsigned char line[1024];
//
fseek(fp, 0, SEEK_END);
long len = ftell(fp);
#if DEBUG
printf("filesize:%d\n", len);
#endif
rewind(fp);
#if DEBUG
printf("eof:%d\n", feof(fp));
#endif
init(&c, len);
int np = 1;
while (fgets(line, 124, fp) != NULL) {
#if DEBUG
printf("line: %s \n", line);
#endif
parseRuleLine(c, line, strlen(line), &np);
#if DEBUG
printf("np : %d\n", np);
#endif
}
fclose(fp);
*nn = np;
return c;
}
int parseRuleLine(void *c, unsigned char* line, int len, int *np) {
//
int i = 0;
unsigned char n = 0;
node* p = NULL;
int tmp = 0;
unsigned int point = 0;
unsigned int *head = (unsigned int *) c;
node *nhead = (node *) (head + 256);
int stat = 0;
for (i = 0; i < len; ++i) {
if (*(line + i) == '#') {
break;
}
if (*(line + i) == ' ') {
stat = 1;
continue;
}
if (stat == 1) {
p->data = atoi(line + i);
#if DEBUG
printf("levels : %d\n", p->data);
printf("index : %d\n", p - nhead);
printf("char : %c\n", p->c);
#endif
#if DEBUG
//showList(c);
#endif
break;
}
n = *(line + i);
if (p == NULL) {
point = *(head + n);
if (point == 0) {
*(head + n) = *np;
p = (nhead + *np - 1);
#if DEBUG
printf("+head : [%c] -> %d\n", n, *np + 1);
showList(c);
#endif
p->c = n;
*np += 1;
} else {
p = (nhead + point - 1);
#if DEBUG
printf("levels : %d\n", p->data);
printf("index : %d\n", p - nhead);
printf("char : %c\n", p->c);
#endif
}
} else {
if (p->next == 0) {
p->next = *np;
*np += 1;
}
if (p->c > 0) {
p = nhead + p->next - 1;
}
if (p->c == n) {
/*
if (p->next == 0) {
p->next = *np;
//p->c = n;
*np += 1;
}
p = nhead + p->next - 1;
* */
#if DEBUG
printf("levels : %d\n", p->data);
printf("index : %d\n", p - nhead);
printf("char : %c\n", p->c);
#endif
} else {
if (p->c == 0) {
p->c = n;
if (p->next == 0) {
p->next = *np;
//p->c = n;
*np += 1;
}
//p = nhead + p->next - 1;
} else {
tmp = p->friend;
while (tmp != 0) {
if ((nhead + tmp - 1)->c == n) {
break;
}
p = (nhead + tmp - 1);
#if DEBUG
printf("levels : %d\n", p->data);
printf("index : %d\n", p - nhead);
printf("char : %c\n", p->c);
#endif
tmp = p->friend;
}
if (tmp != 0) {
p = (nhead + tmp - 1);
#if DEBUG
printf("levels : %d\n", p->data);
printf("index : %d\n", p - nhead);
printf("char : %c\n", p->c);
#endif
} else {
//添加friend node
p->friend = *np;
(nhead + *np - 1)->c = n;
p = (nhead + *np - 1);
*np += 1;
#if DEBUG
printf("levels : %d\n", p->data);
printf("index : %d\n", p - nhead);
printf("char : %c\n", p->c);
#endif
}
/*
if (p->next == 0) {
p->next = *np;
//p->c = n;
*np += 1;
}
p = nhead + p->next - 1;
*/
}
}
#if DEBUG
showList(c);
#endif
}
}
}
int search(void *c, unsigned char *s, int len, int level,callback_t cbk) {
int i = 0;
int pos = 0;
int ret = 0;
for (i = 0; i < len; ++i) {
ret = searchOne(c, s, i, len, &pos);
if (ret > 0) {
#if DEBUG
printf("pos :%d - %d -level: %d\n", i, pos, ret);
#endif
if(cbk){
cbk(s,i,pos,ret);
#if DEBUG
printf("after callback :%c\n", s[i]);
#endif
}
if (ret > level) {
i = pos;
}
ret = 0;
}
}
return 0;
}
int searchOne(void *c, unsigned char *s, int pos, int len, int *m) {
int i = 0;
node *p = NULL;
node *tmp = NULL;
unsigned char n = 0;
unsigned int *head = (unsigned int *) c;
node *nhead = (node *) (head + 256);
int last_pos = 0;
int last_level = 0;
//for (i = pos; i < len; ++i) {
i = pos;
n = *(s + i);
#if DEBUG
printf("char : [%c]\n", n);
#endif
if (p == NULL) {
#if DEBUG
printf("head : %d\n", *(head + n));
#endif
if (*(head + n) > 0) {
p = nhead + *(head + n) - 1;
#if DEBUG
printf("-index : [%d]\n", *(head + n));
printf("-char : [%c]\n", p->c);
printf("-level : [%d]\n", p->data);
#endif
/*
while (p != NULL && i < len) {
if (p->data > last_level) {
last_pos = i;
last_level = p->data;
}
if (p->next == 0) {
break;
}
//printf("-index : [%d]\n", p->next );
tmp = nhead + p->next - 1;
++i;
n = *(s + i);
//#if DEBUG
//printf("-char : [%c]\n", p->c);
//#endif
//friend
p = NULL;
#if DEBUG
printf("--char : [%c]\n", n);
printf("--char =: [%c]\n", tmp->c);
printf("--index =: [%d]\n", tmp - nhead);
#endif
if (tmp->c == n) {
p = tmp;
} else {
while (tmp->friend > 0) {
//printf("-index : [%d]\n", tmp->friend);
tmp = nhead + tmp->friend - 1;
if (tmp->c == n) {
p = tmp;
break;
}
}
}
}
* */
bestResult(s, pos, len, nhead, p, &last_pos, &last_level);
if (last_level > 0) {
*m = last_pos;
return last_level;
}
} else {
return 0;
}
}
//}
return 0;
}
int bestResult(unsigned char *s, int pos, int len, node *head, node *n, int * best_pos, int *best_level) {
node *tmp = n;
unsigned char c = *(s + pos);
if (pos >= len) {
return 0;
}
while (tmp != NULL) {
#if DEBUG
printf("pos -> char :[%d] -> [%c] | [%c]\n", pos, c, tmp->c);
printf("friend :[%d] \n", tmp->friend);
#endif
if (c == tmp->c) {
#if DEBUG
printf("pos -> char :[%d] -> [%c] | [%c]\n", pos, c, tmp->c);
printf("friend :[%d] \n", tmp->friend);
printf("level :[%d] \n", tmp->data);
printf("index :[%d] \n", tmp - head);
printf("next :[%d] \n", tmp->next);
#endif
if (tmp->data > *best_level) {
*best_level = tmp->data;
*best_pos = pos;
} else if (tmp->data == *best_level) {
if (pos > *best_pos) {
*best_level = tmp->data;
*best_pos = pos;
}
}
if (tmp->next > 0) {
bestResult(s, pos + 1, len, head, head + tmp->next - 1, best_pos, best_level);
}
}
if (tmp->friend > 0) {
tmp = head + tmp->friend - 1;
#if DEBUG
printf("pos -> char :[%d] -> [%c] | [%c]\n", pos, c, tmp->c);
printf("friend :[%d] \n", tmp->friend);
printf("level :[%d] \n", tmp->data);
printf("index :[%d] \n", tmp->friend);
#endif
} else {
tmp = NULL;
}
}
return 0;
}
int saveToFile(void *c, int nn, unsigned char *fileName) {
size_t length = 256 * sizeof (unsigned int) +nn * sizeof (node);
unsigned long lengthCompressed = compressBound(length);
unsigned long *s = malloc(lengthCompressed + sizeof (unsigned long));
unsigned long lengthCompressedR = lengthCompressed + sizeof (unsigned long);
memset(s, 0, lengthCompressed + sizeof (unsigned long));
if (compress((Bytef *) (s + 1), &lengthCompressedR, (Bytef *) c, length) != Z_OK) {
#if DEBUG
printf("%d\n", length);
printf("%d\n", lengthCompressed);
printf("%d\n", lengthCompressed + sizeof (unsigned long));
printf("%d\n", compress((Bytef *) s, &lengthCompressedR, "xx", 2));
#endif
return 0;
}
*s = length;
#if DEBUG
printf("length :[%d] \n", length);
printf("before Compressed compute :[%d] \n", lengthCompressed);
printf("after Compressed true :[%d] \n", lengthCompressedR);
printf("nn :[%d] \n", nn);
#endif
FILE *fp = fopen(fileName, "wb");
fwrite(s, lengthCompressedR + sizeof (unsigned long), 1, fp);
fclose(fp);
free(s);
}
long releaseFromFile(unsigned char *fileName, void **c) {
FILE *fp = fopen(fileName, "rb");
unsigned long *s = NULL;
unsigned long beforeUncompress = 0;
unsigned long afterUncompress = 0;
fseek(fp, 0, SEEK_END);
long len = ftell(fp);
rewind(fp);
s = malloc(len + 1);
memset(s, 0, len + 1);
fread(s, len, 1, fp);
beforeUncompress = *s;
*c = malloc(beforeUncompress);
memset(*c, 0, beforeUncompress);
afterUncompress = beforeUncompress;
int ret = 0;
if ((ret = uncompress((Bytef *) * c, &afterUncompress, (Bytef *) (s + 1), len - sizeof (unsigned long))) != Z_OK) {
printf("ret :[%d] \n", ret);
return 0;
}
#if DEBUG
printf("len :[%d] \n", len);
printf("before Uncompressed compute :[%d] \n", beforeUncompress);
printf("after Uncompressed true :[%d] \n", afterUncompress);
#endif
free(s);
return len;
}
#if DEBUG
/*
*
*/
int main(int argc, char** argv) {
if (argc < 2) {
printf("argument error!\n");
exit(1);
}
void *c = NULL;
int nn = 0;
unsigned char * compiled = "compiled.cd.gz";
unsigned char* s = "abcdefghabdafacdadadas89dabcdeaafkasdasdasdas89";
c = parseRuleFile(argv[1], &nn);
saveToFile(c, nn, compiled);
c = NULL;
releaseFromFile(compiled, &c);
showList(c);
search(c, s, strlen(s), 2,NULL);
return (EXIT_SUCCESS);
}
#endif