Browse code

reduce memory usage of AC nodes

git-svn: trunk@4363

Török Edvin authored on 2008/11/10 19:46:24
Showing 3 changed files
... ...
@@ -1,3 +1,8 @@
1
+Mon Nov 10 11:46:14 EET 2008 (edwin)
2
+------------------------------------
3
+ * libclamav/matcher-ac.c, libclamav/matcher-ac.h: reduce memory
4
+ usage of AC nodes
5
+
1 6
 Sun Nov  9 22:37:29 EET 2008 (edwin)
2 7
 ------------------------------------
3 8
  * contrib/clamdtop/Makefile, contrib/clamdtop/clamdtop.c: clamdtop:
... ...
@@ -93,8 +93,6 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
93 93
 		    mp_free(root->mempool, next);
94 94
 		    return CL_EMEM;
95 95
 		}
96
-	    } else {
97
-		next->leaf = 1;
98 96
 	    }
99 97
 
100 98
 	    root->ac_nodes++;
... ...
@@ -111,7 +109,6 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
111 111
 	    root->ac_nodetable[root->ac_nodes - 1] = next;
112 112
 
113 113
 	    pt->trans[(unsigned char) (pattern->pattern[i] & 0xff)] = next;
114
-	    pt->leaf = 0;
115 114
 	}
116 115
 
117 116
 	pt = next;
... ...
@@ -127,7 +124,6 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
127 127
     root->ac_pattable = (struct cli_ac_patt **) newtable;
128 128
     root->ac_pattable[root->ac_patterns - 1] = pattern;
129 129
 
130
-    pt->final = 1;
131 130
     pattern->depth = i;
132 131
 
133 132
     ph = pt->list;
... ...
@@ -253,9 +249,9 @@ static int ac_maketrans(struct cli_matcher *root)
253 253
     }
254 254
 
255 255
     while((node = bfs_dequeue(&bfs, &bfs_last))) {
256
-	if(node->leaf) {
256
+	if(IS_LEAF(node)) {
257 257
 	    struct cli_ac_node *failtarget = node->fail;
258
-	    while(failtarget->leaf)
258
+	    while(IS_LEAF(failtarget))
259 259
 		failtarget = failtarget->fail;
260 260
 	    node->fail = failtarget;
261 261
 	    continue;
... ...
@@ -265,7 +261,7 @@ static int ac_maketrans(struct cli_matcher *root)
265 265
 	    child = node->trans[i];
266 266
 	    if(child) {
267 267
 		fail = node->fail;
268
-		while(fail->leaf || !fail->trans[i])
268
+		while(IS_LEAF(fail) || !fail->trans[i])
269 269
 		    fail = fail->fail;
270 270
 
271 271
 		child->fail = fail->trans[i];
... ...
@@ -280,9 +276,6 @@ static int ac_maketrans(struct cli_matcher *root)
280 280
 		    child->list = child->fail->list;
281 281
 		}
282 282
 
283
-		if(child->list)
284
-		    child->final = 1;
285
-
286 283
 		if((ret = bfs_enqueue(&bfs, &bfs_last, child)) != 0)
287 284
 		    return ret;
288 285
 	    }
... ...
@@ -298,13 +291,13 @@ static int ac_maketrans(struct cli_matcher *root)
298 298
 	}
299 299
     }
300 300
     while((node = bfs_dequeue(&bfs, &bfs_last))) {
301
-	if(node->leaf)
301
+	if(IS_LEAF(node))
302 302
 	    continue;
303 303
 	for(i = 0; i < 256; i++) {
304 304
 	    child = node->trans[i];
305 305
 	    if(!child) {
306 306
 		struct cli_ac_node *failtarget = node->fail;
307
-		while(failtarget->leaf || !failtarget->trans[i])
307
+		while(IS_LEAF(failtarget) || !failtarget->trans[i])
308 308
 		    failtarget = failtarget->fail;
309 309
 		node->trans[i] = failtarget->trans[i];
310 310
 	    } else {
... ...
@@ -403,7 +396,7 @@ void cli_ac_free(struct cli_matcher *root)
403 403
 	mp_free(root->mempool, root->ac_pattable);
404 404
 
405 405
     for(i = 0; i < root->ac_nodes; i++) {
406
-	if(!root->ac_nodetable[i]->leaf)
406
+	if(!IS_LEAF(root->ac_nodetable[i]))
407 407
 	    mp_free(root->mempool, root->ac_nodetable[i]->trans);
408 408
 	mp_free(root->mempool, root->ac_nodetable[i]);
409 409
     }
... ...
@@ -900,12 +893,12 @@ int cli_ac_scanbuff(const unsigned char *buffer, uint32_t length, const char **v
900 900
 
901 901
     for(i = 0; i < length; i++)  {
902 902
 
903
-	if(current->leaf)
903
+	if(IS_LEAF(current))
904 904
 	    current = current->fail;
905 905
 
906 906
 	current = current->trans[buffer[i]];
907 907
 
908
-	if(current->final) {
908
+	if(IS_FINAL(current)) {
909 909
 	    patt = current->list;
910 910
 	    while(patt) {
911 911
 		bp = i + 1 - patt->depth;
... ...
@@ -68,9 +68,11 @@ struct cli_ac_patt {
68 68
 struct cli_ac_node {
69 69
     struct cli_ac_patt *list;
70 70
     struct cli_ac_node **trans, *fail;
71
-    uint8_t leaf, final;
72 71
 };
73 72
 
73
+#define IS_LEAF(node) (!node->trans)
74
+#define IS_FINAL(node) (!!node->list)
75
+
74 76
 struct cli_ac_result {
75 77
     const char *virname;
76 78
     void *customdata;