From fc0493d2ca87710c037ecc12fc38f9b55efa125e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B6r=C3=B6k=20Edvin?= Date: Mon, 10 Nov 2008 10:46:24 +0000 Subject: [PATCH] reduce memory usage of AC nodes git-svn: trunk@4363 --- ChangeLog | 5 +++++ libclamav/matcher-ac.c | 23 ++++++++--------------- libclamav/matcher-ac.h | 4 +++- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index ade23af14..90597f1a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Nov 10 11:46:14 EET 2008 (edwin) +------------------------------------ + * libclamav/matcher-ac.c, libclamav/matcher-ac.h: reduce memory + usage of AC nodes + Sun Nov 9 22:37:29 EET 2008 (edwin) ------------------------------------ * contrib/clamdtop/Makefile, contrib/clamdtop/clamdtop.c: clamdtop: diff --git a/libclamav/matcher-ac.c b/libclamav/matcher-ac.c index f43cffba9..ec50dce60 100644 --- a/libclamav/matcher-ac.c +++ b/libclamav/matcher-ac.c @@ -93,8 +93,6 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern) mp_free(root->mempool, next); return CL_EMEM; } - } else { - next->leaf = 1; } root->ac_nodes++; @@ -111,7 +109,6 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern) root->ac_nodetable[root->ac_nodes - 1] = next; pt->trans[(unsigned char) (pattern->pattern[i] & 0xff)] = next; - pt->leaf = 0; } pt = next; @@ -127,7 +124,6 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern) root->ac_pattable = (struct cli_ac_patt **) newtable; root->ac_pattable[root->ac_patterns - 1] = pattern; - pt->final = 1; pattern->depth = i; ph = pt->list; @@ -253,9 +249,9 @@ static int ac_maketrans(struct cli_matcher *root) } while((node = bfs_dequeue(&bfs, &bfs_last))) { - if(node->leaf) { + if(IS_LEAF(node)) { struct cli_ac_node *failtarget = node->fail; - while(failtarget->leaf) + while(IS_LEAF(failtarget)) failtarget = failtarget->fail; node->fail = failtarget; continue; @@ -265,7 +261,7 @@ static int ac_maketrans(struct cli_matcher *root) child = node->trans[i]; if(child) { fail = node->fail; - while(fail->leaf || !fail->trans[i]) + while(IS_LEAF(fail) || !fail->trans[i]) fail = fail->fail; child->fail = fail->trans[i]; @@ -280,9 +276,6 @@ static int ac_maketrans(struct cli_matcher *root) child->list = child->fail->list; } - if(child->list) - child->final = 1; - if((ret = bfs_enqueue(&bfs, &bfs_last, child)) != 0) return ret; } @@ -298,13 +291,13 @@ static int ac_maketrans(struct cli_matcher *root) } } while((node = bfs_dequeue(&bfs, &bfs_last))) { - if(node->leaf) + if(IS_LEAF(node)) continue; for(i = 0; i < 256; i++) { child = node->trans[i]; if(!child) { struct cli_ac_node *failtarget = node->fail; - while(failtarget->leaf || !failtarget->trans[i]) + while(IS_LEAF(failtarget) || !failtarget->trans[i]) failtarget = failtarget->fail; node->trans[i] = failtarget->trans[i]; } else { @@ -403,7 +396,7 @@ void cli_ac_free(struct cli_matcher *root) mp_free(root->mempool, root->ac_pattable); for(i = 0; i < root->ac_nodes; i++) { - if(!root->ac_nodetable[i]->leaf) + if(!IS_LEAF(root->ac_nodetable[i])) mp_free(root->mempool, root->ac_nodetable[i]->trans); mp_free(root->mempool, root->ac_nodetable[i]); } @@ -900,12 +893,12 @@ int cli_ac_scanbuff(const unsigned char *buffer, uint32_t length, const char **v for(i = 0; i < length; i++) { - if(current->leaf) + if(IS_LEAF(current)) current = current->fail; current = current->trans[buffer[i]]; - if(current->final) { + if(IS_FINAL(current)) { patt = current->list; while(patt) { bp = i + 1 - patt->depth; diff --git a/libclamav/matcher-ac.h b/libclamav/matcher-ac.h index c0256cc17..33d9e0fc8 100644 --- a/libclamav/matcher-ac.h +++ b/libclamav/matcher-ac.h @@ -68,9 +68,11 @@ struct cli_ac_patt { struct cli_ac_node { struct cli_ac_patt *list; struct cli_ac_node **trans, *fail; - uint8_t leaf, final; }; +#define IS_LEAF(node) (!node->trans) +#define IS_FINAL(node) (!!node->list) + struct cli_ac_result { const char *virname; void *customdata;