reduce memory usage of AC nodes

git-svn: trunk@4363
0.95
Török Edvin 17 years ago
parent ce9368870d
commit fc0493d2ca
  1. 5
      ChangeLog
  2. 23
      libclamav/matcher-ac.c
  3. 4
      libclamav/matcher-ac.h

@ -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:

@ -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;

@ -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;

Loading…
Cancel
Save