libclamav/matcher-ac.c
8000d078
 /*
2023340a
  *  Copyright (C) 2007-2008 Sourcefire, Inc.
1a2906f4
  *
2023340a
  *  Authors: Tomasz Kojm
8000d078
  *
  *  This program is free software; you can redistribute it and/or modify
bb34cb31
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
8000d078
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
8000d078
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
b58fdfc2
 #ifdef	HAVE_UNISTD_H
8000d078
 #include <unistd.h>
b58fdfc2
 #endif
8000d078
 
 #include "clamav.h"
 #include "others.h"
b68d11d2
 #include "matcher.h"
8000d078
 #include "matcher-ac.h"
 #include "filetypes.h"
227f8f7c
 #include "cltypes.h"
fbcef1b0
 #include "str.h"
8000d078
 
3d53538b
 uint8_t cli_ac_mindepth = AC_DEFAULT_MIN_DEPTH;
 uint8_t cli_ac_maxdepth = AC_DEFAULT_MAX_DEPTH;
83fa5305
 
5612732c
 int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
8000d078
 {
65064125
 	struct cli_ac_node *pt, *next;
e38ab7c1
 	struct cli_ac_patt *ph;
65064125
 	void *newtable;
1a648b37
 	struct cli_ac_alt *a1, *a2;
9187ef90
 	uint8_t i, match;
fbcef1b0
 	uint16_t len = MIN(root->ac_maxdepth, pattern->length);
8000d078
 
fbcef1b0
 
     for(i = 0; i < len; i++) {
 	if(pattern->pattern[i] & CLI_MATCH_WILDCARD) {
 	    len = i;
 	    break;
 	}
     }
 
     if(len < root->ac_mindepth)
8000d078
 	return CL_EPATSHORT;
 
fbcef1b0
     pt = root->ac_root;
 
     for(i = 0; i < len; i++) {
 	if(!pt->trans) {
 	    pt->trans = (struct cli_ac_node **) cli_calloc(256, sizeof(struct cli_ac_node *));
 	    if(!pt->trans) {
 		cli_errmsg("cli_ac_addpatt: Can't allocate memory for pt->trans\n");
 		return CL_EMEM;
 	    }
 	}
8000d078
 
fbcef1b0
 	next = pt->trans[(unsigned char) (pattern->pattern[i] & 0xff)]; 
8000d078
 
 	if(!next) {
 	    next = (struct cli_ac_node *) cli_calloc(1, sizeof(struct cli_ac_node));
 	    if(!next) {
fbcef1b0
 		cli_errmsg("cli_ac_addpatt: Can't allocate memory for AC node\n");
8000d078
 		return CL_EMEM;
 	    }
 
fbcef1b0
 	    if(i != len - 1) {
 		next->trans = (struct cli_ac_node **) cli_calloc(256, sizeof(struct cli_ac_node *));
 		if(!next->trans) {
 		    cli_errmsg("cli_ac_addpatt: Can't allocate memory for next->trans\n");
 		    free(next);
 		    return CL_EMEM;
 		}
 	    } else {
 		next->leaf = 1;
 	    }
 
8000d078
 	    root->ac_nodes++;
65064125
 	    newtable = cli_realloc(root->ac_nodetable, root->ac_nodes * sizeof(struct cli_ac_node *));
cdfcca0c
 	    if(!newtable) {
 		root->ac_nodes--;
fbcef1b0
 		cli_errmsg("cli_ac_addpatt: Can't realloc ac_nodetable\n");
 		if(next->trans)
 		    free(next->trans);
 		free(next);
8000d078
 		return CL_EMEM;
 	    }
65064125
 	    root->ac_nodetable = (struct cli_ac_node **) newtable;
 	    root->ac_nodetable[root->ac_nodes - 1] = next;
8000d078
 
fbcef1b0
 	    pt->trans[(unsigned char) (pattern->pattern[i] & 0xff)] = next;
 	    pt->leaf = 0;
8000d078
 	}
 
fbcef1b0
 	pt = next;
8000d078
     }
 
fbcef1b0
     root->ac_patterns++;
65064125
     newtable = cli_realloc(root->ac_pattable, root->ac_patterns * sizeof(struct cli_ac_patt *));
     if(!newtable) {
 	root->ac_patterns--;
fbcef1b0
 	cli_errmsg("cli_ac_addpatt: Can't realloc ac_pattable\n");
 	return CL_EMEM;
     }
65064125
     root->ac_pattable = (struct cli_ac_patt **) newtable;
fbcef1b0
     root->ac_pattable[root->ac_patterns - 1] = pattern;
8000d078
 
fbcef1b0
     pt->final = 1;
     pattern->depth = i;
e38ab7c1
 
     ph = pt->list;
     while(ph) {
 	if((ph->length == pattern->length) && (ph->prefix_length == pattern->prefix_length)) {
 	    if(!memcmp(ph->pattern, pattern->pattern, ph->length * sizeof(uint16_t)) && !memcmp(ph->prefix, pattern->prefix, ph->prefix_length * sizeof(uint16_t))) {
9187ef90
 		if(!ph->alt && !pattern->alt) {
 		    match = 1;
 		} else if(ph->alt == pattern->alt) {
 		    match = 1;
 		    for(i = 0; i < ph->alt; i++) {
1a648b37
 			a1 = ph->alttable[i];
 			a2 = pattern->alttable[i];
 
 			if(a1->num != a2->num) {
9187ef90
 			    match = 0;
 			    break;
 			}
1a648b37
 			if(a1->chmode != a2->chmode) {
 			    match = 0;
 			    break;
 			} else if(a1->chmode) {
 			    if(memcmp(a1->str, a2->str, a1->num)) {
 				match = 0;
 				break;
 			    }
 			} else {
 			    while(a1 && a2) {
 				if((a1->len != a2->len) || memcmp(a1->str, a2->str, a1->len))
 				    break;
 				a1 = a1->next;
 				a2 = a2->next;
 			    }
 			    if(a1 || a2) {
 				match = 0;
 				break;
 			    }
 			}
9187ef90
 		    }
 		} else {
 		    match = 0;
 		}
 
 		if(match) {
 		    pattern->next_same = ph->next_same;
 		    ph->next_same = pattern;
 		    return CL_SUCCESS;
 		}
e38ab7c1
 	    }
 	}
 	ph = ph->next;
     }
 
fbcef1b0
     pattern->next = pt->list;
     pt->list = pattern;
8000d078
 
8d3aca30
     return CL_SUCCESS;
8000d078
 }
 
fbcef1b0
 struct bfs_list {
     struct cli_ac_node *node;
     struct bfs_list *next;
 };
 
1a648b37
 static int bfs_enqueue(struct bfs_list **bfs, struct bfs_list **last, struct cli_ac_node *n)
8000d078
 {
fbcef1b0
 	struct bfs_list *new;
 
8000d078
 
fbcef1b0
     new = (struct bfs_list *) cli_malloc(sizeof(struct bfs_list));
     if(!new) {
 	cli_errmsg("bfs_enqueue: Can't allocate memory for bfs_list\n");
8000d078
 	return CL_EMEM;
     }
1a648b37
     new->next = NULL;
8000d078
     new->node = n;
1a648b37
 
     if(*last) {
 	(*last)->next = new;
 	*last = new;
     } else {
 	*bfs = *last = new;
     }
fbcef1b0
 
8d3aca30
     return CL_SUCCESS;
8000d078
 }
 
a2f97877
 static struct cli_ac_node *bfs_dequeue(struct bfs_list **bfs, struct bfs_list **last)
8000d078
 {
1a648b37
 	struct bfs_list *lpt;
8000d078
 	struct cli_ac_node *pt;
 
 
1a648b37
     if(!(lpt = *bfs)) {
8000d078
 	return NULL;
     } else {
1a648b37
 	*bfs = (*bfs)->next;
fbcef1b0
 	pt = lpt->node;
a2f97877
 	if(lpt == *last)
 	    *last = NULL;
fbcef1b0
 	free(lpt);
8000d078
 	return pt;
     }
 }
 
fbcef1b0
 static int ac_maketrans(struct cli_matcher *root)
8000d078
 {
1a648b37
 	struct bfs_list *bfs = NULL, *bfs_last = NULL;
fbcef1b0
 	struct cli_ac_node *ac_root = root->ac_root, *child, *node, *fail;
 	struct cli_ac_patt *patt;
8000d078
 	int i, ret;
 
 
fbcef1b0
     for(i = 0; i < 256; i++) {
 	node = ac_root->trans[i];
 	if(!node) {
 	    ac_root->trans[i] = ac_root;
 	} else {
 	    node->fail = ac_root;
1a648b37
 	    if((ret = bfs_enqueue(&bfs, &bfs_last, node)))
fbcef1b0
 		return ret;
 	}
8000d078
     }
 
a2f97877
     while((node = bfs_dequeue(&bfs, &bfs_last))) {
fbcef1b0
 	if(node->leaf)
8000d078
 	    continue;
 
 	for(i = 0; i < 256; i++) {
 	    child = node->trans[i];
fbcef1b0
 	    if(child) {
 		fail = node->fail;
 		while(fail->leaf || !fail->trans[i])
 		    fail = fail->fail;
8000d078
 
fbcef1b0
 		child->fail = fail->trans[i];
 
 		if(child->list) {
 		    patt = child->list;
 		    while(patt->next)
 			patt = patt->next;
 
 		    patt->next = child->fail->list;
 		} else {
 		    child->list = child->fail->list;
8000d078
 		}
fbcef1b0
 
 		if(child->list)
 		    child->final = 1;
 
1a648b37
 		if((ret = bfs_enqueue(&bfs, &bfs_last, child)) != 0)
fbcef1b0
 		    return ret;
8000d078
 	    }
 	}
     }
fbcef1b0
 
8d3aca30
     return CL_SUCCESS;
8000d078
 }
 
5612732c
 int cli_ac_buildtrie(struct cli_matcher *root)
8000d078
 {
     if(!root)
 	return CL_EMALFDB;
 
     if(!root->ac_root) {
fbcef1b0
 	cli_dbgmsg("cli_ac_buildtrie: AC pattern matcher is not initialised\n");
8d3aca30
 	return CL_SUCCESS;
8000d078
     }
 
fbcef1b0
     return ac_maketrans(root);
8000d078
 }
 
fbcef1b0
 int cli_ac_init(struct cli_matcher *root, uint8_t mindepth, uint8_t maxdepth)
8000d078
 {
 
fbcef1b0
     root->ac_root = (struct cli_ac_node *) cli_calloc(1, sizeof(struct cli_ac_node));
     if(!root->ac_root) {
 	cli_errmsg("cli_ac_init: Can't allocate memory for ac_root\n");
 	return CL_EMEM;
     }
8000d078
 
fbcef1b0
     root->ac_root->trans = (struct cli_ac_node **) cli_calloc(256, sizeof(struct cli_ac_node *));
     if(!root->ac_root->trans) {
 	cli_errmsg("cli_ac_init: Can't allocate memory for ac_root->trans\n");
 	free(root->ac_root);
 	return CL_EMEM;
8000d078
     }
fbcef1b0
 
     root->ac_mindepth = mindepth;
     root->ac_maxdepth = maxdepth;
 
     return CL_SUCCESS;
8000d078
 }
 
1a648b37
 static void ac_free_alt(struct cli_ac_patt *p)
 {
 	uint16_t i;
 	struct cli_ac_alt *a1, *a2;
 
 
     if(!p->alt)
 	return;
 
     for(i = 0; i < p->alt; i++) {
 	a1 = p->alttable[i];
 	while(a1) {
 	    a2 = a1;
 	    a1 = a1->next;
 	    if(a2->str)
 		free(a2->str);
 	    free(a2);
 	}
     }
     free(p->alttable);
 }
 
5612732c
 void cli_ac_free(struct cli_matcher *root)
8000d078
 {
20c2455d
 	uint32_t i;
fbcef1b0
 	struct cli_ac_patt *patt;
8000d078
 
 
fbcef1b0
     for(i = 0; i < root->ac_patterns; i++) {
 	patt = root->ac_pattable[i];
1a2906f4
 	patt->prefix ? free(patt->prefix) : free(patt->pattern);
fbcef1b0
 	free(patt->virname);
 	if(patt->offset)
 	    free(patt->offset);
1a648b37
 	if(patt->alt)
 	    ac_free_alt(patt);
fbcef1b0
 	free(patt);
     }
     if(root->ac_pattable)
 	free(root->ac_pattable);
 
8000d078
     for(i = 0; i < root->ac_nodes; i++) {
fbcef1b0
 	if(!root->ac_nodetable[i]->leaf)
 	    free(root->ac_nodetable[i]->trans);
8000d078
 	free(root->ac_nodetable[i]);
     }
 
     if(root->ac_nodetable)
 	free(root->ac_nodetable);
 
fbcef1b0
     if(root->ac_root) {
 	free(root->ac_root->trans);
8000d078
 	free(root->ac_root);
fbcef1b0
     }
8000d078
 }
 
1a648b37
 /* 
  * FIXME: the current support for string alternatives uses a brute-force
  *        approach and doesn't perform any kind of verification and
  *        backtracking. This may easily lead to false negatives, eg. when
  *        an alternative contains strings of different lengths and 
  *        more than one of them can match at the current position.
  */
 
fbcef1b0
 #define AC_MATCH_CHAR(p,b)						\
     switch(wc = p & CLI_MATCH_WILDCARD) {				\
ecf5865b
 	case CLI_MATCH_CHAR:						\
 	    if((unsigned char) p != b)					\
1a2906f4
 		match = 0;						\
ecf5865b
 	    break;							\
 									\
 	case CLI_MATCH_IGNORE:						\
 	    break;							\
 									\
fbcef1b0
 	case CLI_MATCH_ALTERNATIVE:					\
1a2906f4
 	    match = 0;							\
1a648b37
 	    alt = pattern->alttable[altcnt];				\
 	    if(alt->chmode) {						\
 		for(j = 0; j < alt->num; j++) {				\
 		    if(alt->str[j] == b) {				\
1a2906f4
 			match = 1;					\
1a648b37
 			break;						\
 		    }							\
 		}							\
 	    } else {							\
 		while(alt) {						\
 		    if(bp + alt->len <= length) {			\
 			if(!memcmp(&buffer[bp], alt->str, alt->len)) {	\
1a2906f4
 			    match = 1;					\
1a648b37
 			    bp += alt->len - 1;				\
 			    break;					\
 			}						\
 		    }							\
 		    alt = alt->next;					\
fbcef1b0
 		}							\
 	    }								\
1a648b37
 	    altcnt++;							\
fbcef1b0
 	    break;							\
 									\
 	case CLI_MATCH_NIBBLE_HIGH:					\
 	    if((unsigned char) (p & 0x00f0) != (b & 0xf0))		\
1a2906f4
 		match = 0;						\
fbcef1b0
 	    break;							\
 									\
 	case CLI_MATCH_NIBBLE_LOW:					\
 	    if((unsigned char) (p & 0x000f) != (b & 0x0f))		\
1a2906f4
 		match = 0;						\
fbcef1b0
 	    break;							\
 									\
 	default:							\
ecf5865b
 	    cli_errmsg("ac_findmatch: Unknown wildcard 0x%x\n", wc);	\
1a2906f4
 	    match = 0;							\
fbcef1b0
     }
 
42860228
 inline static int ac_findmatch(const unsigned char *buffer, uint32_t offset, uint32_t length, const struct cli_ac_patt *pattern, uint32_t *end)
8000d078
 {
1a2906f4
 	uint32_t bp, match;
1a648b37
 	uint16_t wc, i, j, altcnt = pattern->alt_pattern;
 	struct cli_ac_alt *alt;
f1f75e19
 
8000d078
 
e38ab7c1
     if((offset + pattern->length > length) || (pattern->prefix_length > offset))
2242da43
 	return 0;
 
fbcef1b0
     bp = offset + pattern->depth;
8000d078
 
1a2906f4
     match = 1;
42860228
     for(i = pattern->depth; i < pattern->length && bp < length; i++) {
fbcef1b0
 	AC_MATCH_CHAR(pattern->pattern[i],buffer[bp]);
1a2906f4
 	if(!match)
 	    return 0;
fbcef1b0
 	bp++;
8000d078
     }
42860228
     *end = bp;
8000d078
 
1a2906f4
     if(!(pattern->ch[1] & CLI_MATCH_IGNORE)) {
 	bp += pattern->ch_mindist[1];
 	for(i = pattern->ch_mindist[1]; i <= pattern->ch_maxdist[1]; i++) {
 	    if(bp >= length)
 		return 0;
 	    match = 1;
 	    AC_MATCH_CHAR(pattern->ch[1],buffer[bp]);
 	    if(match)
 		break;
 	    bp++;
 	}
 	if(!match)
 	    return 0;
     }
 
f1f75e19
     if(pattern->prefix) {
1a648b37
 	altcnt = 0;
fbcef1b0
 	bp = offset - pattern->prefix_length;
1a2906f4
 	match = 1;
f1f75e19
 	for(i = 0; i < pattern->prefix_length; i++) {
fbcef1b0
 	    AC_MATCH_CHAR(pattern->prefix[i],buffer[bp]);
1a2906f4
 	    if(!match)
 		return 0;
fbcef1b0
 	    bp++;
f1f75e19
 	}
     }
 
1a2906f4
     if(!(pattern->ch[0] & CLI_MATCH_IGNORE)) {
 	bp = offset - pattern->prefix_length;
 	if(pattern->ch_mindist[0] + 1 > bp)
 	    return 0;
 	bp -= pattern->ch_mindist[0] + 1;
 	for(i = pattern->ch_mindist[0]; i <= pattern->ch_maxdist[0]; i++) {
 	    match = 1;
 	    AC_MATCH_CHAR(pattern->ch[0],buffer[bp]);
 	    if(match)
 		break;
 	    if(!bp)
 		return 0;
 	    else
 		bp--;
 	}
 	if(!match)
 	    return 0;
     }
 
8000d078
     return 1;
 }
 
bedc58de
 int cli_ac_initdata(struct cli_ac_data *data, uint32_t partsigs, uint8_t tracklen)
4e9ab8ed
 {
 
     if(!data) {
fbcef1b0
 	cli_errmsg("cli_ac_init: data == NULL\n");
4e9ab8ed
 	return CL_ENULLARG;
     }
 
     data->partsigs = partsigs;
 
     if(!partsigs)
 	return CL_SUCCESS;
 
fbcef1b0
     data->offmatrix = (int32_t ***) cli_calloc(partsigs, sizeof(int32_t **));
     if(!data->offmatrix) {
 	cli_errmsg("cli_ac_init: Can't allocate memory for data->offmatrix\n");
4e9ab8ed
 	return CL_EMEM;
     }
 
fbcef1b0
     return CL_SUCCESS;
 }
4e9ab8ed
 
fbcef1b0
 void cli_ac_freedata(struct cli_ac_data *data)
 {
 	uint32_t i;
4e9ab8ed
 
227f8f7c
 
fbcef1b0
     if(data && data->partsigs) {
 	for(i = 0; i < data->partsigs; i++) {
 	    if(data->offmatrix[i]) {
 		free(data->offmatrix[i][0]);
 		free(data->offmatrix[i]);
 	    }
 	}
 	free(data->offmatrix);
227f8f7c
     }
fbcef1b0
 }
227f8f7c
 
5025967e
 inline static int ac_addtype(struct cli_matched_type **list, cli_file_t type, off_t offset, const cli_ctx *ctx)
fbcef1b0
 {
 	struct cli_matched_type *tnode, *tnode_last;
4e9ab8ed
 
 
5025967e
     if(type == CL_TYPE_ZIPSFX) {
 	if(*list && ctx && ctx->limits && ctx->limits->maxfiles && (*list)->cnt > ctx->limits->maxfiles)
 	    return CL_SUCCESS;
     } else if(*list && (*list)->cnt >= MAX_EMBEDDED_OBJ)
fbcef1b0
 	return CL_SUCCESS;
4e9ab8ed
 
fbcef1b0
     if(!(tnode = cli_calloc(1, sizeof(struct cli_matched_type)))) {
 	cli_errmsg("cli_ac_addtype: Can't allocate memory for new type node\n");
4e9ab8ed
 	return CL_EMEM;
     }
 
fbcef1b0
     tnode->type = type;
     tnode->offset = offset;
4e9ab8ed
 
fbcef1b0
     tnode_last = *list;
     while(tnode_last && tnode_last->next)
 	tnode_last = tnode_last->next;
4e9ab8ed
 
fbcef1b0
     if(tnode_last)
 	tnode_last->next = tnode;
     else
 	*list = tnode;
4e9ab8ed
 
fbcef1b0
     (*list)->cnt++;
4e9ab8ed
     return CL_SUCCESS;
 }
 
5025967e
 int cli_ac_scanbuff(const unsigned char *buffer, uint32_t length, const char **virname, const struct cli_matcher *root, struct cli_ac_data *mdata, uint32_t offset, cli_file_t ftype, int fd, struct cli_matched_type **ftoffset, unsigned int mode, const cli_ctx *ctx)
8000d078
 {
 	struct cli_ac_node *current;
e38ab7c1
 	struct cli_ac_patt *patt, *pt;
42860228
         uint32_t i, bp, realoff, matchend;
fbcef1b0
 	uint16_t j;
 	int32_t **offmatrix;
 	uint8_t found;
841161e0
 	struct cli_target_info info;
fbcef1b0
 	int type = CL_CLEAN;
8000d078
 
 
cdbf8c8e
     if(!root->ac_root)
8000d078
 	return CL_CLEAN;
 
4e9ab8ed
     if(!mdata) {
fbcef1b0
 	cli_errmsg("cli_ac_scanbuff: mdata == NULL\n");
8000d078
 	return CL_ENULLARG;
     }
 
841161e0
     memset(&info, 0, sizeof(info));
8000d078
     current = root->ac_root;
 
     for(i = 0; i < length; i++)  {
 
fbcef1b0
 	while(current->leaf || !current->trans[buffer[i]])
 	    current = current->fail;
8000d078
 
fbcef1b0
 	current = current->trans[buffer[i]];
 
 	if(current->final) {
e38ab7c1
 	    patt = current->list;
 	    while(patt) {
 		bp = i + 1 - patt->depth;
42860228
 		if(ac_findmatch(buffer, bp, length, patt, &matchend)) {
e38ab7c1
 		    pt = patt;
 		    while(pt) {
6038397e
 
 			if((pt->type && !(mode & AC_SCAN_FT)) || (!pt->type && !(mode & AC_SCAN_VIR))) {
 			    pt = pt->next_same;
 			    continue;
 			}
 
e38ab7c1
 			realoff = offset + bp - pt->prefix_length;
 
 			if((pt->offset || pt->target) && (!pt->sigid || pt->partno == 1)) {
 			    if((fd == -1 && !ftype) || !cli_validatesig(ftype, pt->offset, realoff, &info, fd, pt->virname)) {
 				pt = pt->next_same;
 				continue;
 			    }
7ec67e94
 			}
 
e38ab7c1
 			if(pt->sigid) { /* it's a partial signature */
8000d078
 
e38ab7c1
 			    if(pt->partno != 1 && (!mdata->offmatrix[pt->sigid - 1] || !mdata->offmatrix[pt->sigid - 1][pt->partno - 2][0])) {
 				pt = pt->next_same;
 				continue;
fbcef1b0
 			    }
227f8f7c
 
e38ab7c1
 			    if(!mdata->offmatrix[pt->sigid - 1]) {
 				mdata->offmatrix[pt->sigid - 1] = cli_malloc(pt->parts * sizeof(int32_t *));
 				if(!mdata->offmatrix[pt->sigid - 1]) {
 				    cli_errmsg("cli_ac_scanbuff: Can't allocate memory for mdata->offmatrix[%u]\n", pt->sigid - 1);
 				    return CL_EMEM;
 				}
 
 				mdata->offmatrix[pt->sigid - 1][0] = cli_malloc(pt->parts * (AC_DEFAULT_TRACKLEN + 1) * sizeof(int32_t));
 				if(!mdata->offmatrix[pt->sigid - 1][0]) {
 				    cli_errmsg("cli_ac_scanbuff: Can't allocate memory for mdata->offmatrix[%u][0]\n", pt->sigid - 1);
 				    free(mdata->offmatrix[pt->sigid - 1]);
 				    mdata->offmatrix[pt->sigid - 1] = NULL;
 				    return CL_EMEM;
 				}
 				memset(mdata->offmatrix[pt->sigid - 1][0], -1, pt->parts * (AC_DEFAULT_TRACKLEN + 1) * sizeof(int32_t));
 				mdata->offmatrix[pt->sigid - 1][0][0] = 0;
 				for(j = 1; j < pt->parts; j++) {
 				    mdata->offmatrix[pt->sigid - 1][j] = mdata->offmatrix[pt->sigid - 1][0] + j * (AC_DEFAULT_TRACKLEN + 1);
 				    mdata->offmatrix[pt->sigid - 1][j][0] = 0;
 				}
4e9ab8ed
 			    }
e38ab7c1
 			    offmatrix = mdata->offmatrix[pt->sigid - 1];
 
 			    if(pt->partno != 1) {
 				found = 0;
 				for(j = 1; j <= AC_DEFAULT_TRACKLEN && offmatrix[pt->partno - 2][j] != -1; j++) {
 				    found = 1;
 				    if(pt->maxdist)
 					if(realoff - offmatrix[pt->partno - 2][j] > pt->maxdist)
 					    found = 0;
 
 				    if(found && pt->mindist)
 					if(realoff - offmatrix[pt->partno - 2][j] < pt->mindist)
 					    found = 0;
 
 				    if(found)
 					break;
 				}
4e9ab8ed
 			    }
 
e38ab7c1
 			    if(pt->partno == 1 || (found && (pt->partno != pt->parts))) {
 				offmatrix[pt->partno - 1][0] %= AC_DEFAULT_TRACKLEN;
 				offmatrix[pt->partno - 1][0]++;
42860228
 				offmatrix[pt->partno - 1][offmatrix[pt->partno - 1][0]] = offset + matchend;
e38ab7c1
 
 				if(pt->partno == 1) /* save realoff for the first part */
 				    offmatrix[pt->parts - 1][offmatrix[pt->partno - 1][0]] = realoff;
 			    } else if(found && pt->partno == pt->parts) {
 				if(pt->type) {
6038397e
 
 				    if(pt->type == CL_TYPE_IGNORED && (!pt->rtype || ftype == pt->rtype)) {
 					if(info.exeinfo.section)
 					    free(info.exeinfo.section);
 
 					return CL_TYPE_IGNORED;
 				    }
 
 				    if((pt->type > type || pt->type >= CL_TYPE_SFX || pt->type == CL_TYPE_MSEXE) && (!pt->rtype || ftype == pt->rtype)) {
 					cli_dbgmsg("Matched signature for file type %s\n", pt->virname);
 					type = pt->type;
5d763f04
 					if(ftoffset && (!*ftoffset || (*ftoffset)->cnt < MAX_EMBEDDED_OBJ || type == CL_TYPE_ZIPSFX) && ((ftype == CL_TYPE_MSEXE && type >= CL_TYPE_SFX) || ((ftype == CL_TYPE_MSEXE || ftype == CL_TYPE_ZIP || ftype == CL_TYPE_MSOLE2) && type == CL_TYPE_MSEXE)))  {
6038397e
 					    /* FIXME: we don't know which offset of the first part is the correct one */
 					    for(j = 1; j <= AC_DEFAULT_TRACKLEN && offmatrix[0][j] != -1; j++) {
5025967e
 						if(ac_addtype(ftoffset, type, offmatrix[pt->parts - 1][j], ctx)) {
6038397e
 						    if(info.exeinfo.section)
 							free(info.exeinfo.section);
 						    return CL_EMEM;
e38ab7c1
 						}
 					    }
 					}
6038397e
 
 					memset(offmatrix[0], -1, pt->parts * (AC_DEFAULT_TRACKLEN + 1) * sizeof(int32_t));
 					for(j = 0; j < pt->parts; j++)
 					    offmatrix[j][0] = 0;
e38ab7c1
 				    }
 
 				} else { /* !pt->type */
 				    if(virname)
 					*virname = pt->virname;
 
 				    if(info.exeinfo.section)
 					free(info.exeinfo.section);
 
 				    return CL_VIRUS;
 				}
 			    }
 
 			} else { /* old type signature */
fbcef1b0
 			    if(pt->type) {
6038397e
 				if(pt->type == CL_TYPE_IGNORED && (!pt->rtype || ftype == pt->rtype)) {
 				    if(info.exeinfo.section)
 					free(info.exeinfo.section);
e38ab7c1
 
6038397e
 				    return CL_TYPE_IGNORED;
 				}
 				if((pt->type > type || pt->type >= CL_TYPE_SFX || pt->type == CL_TYPE_MSEXE) && (!pt->rtype || ftype == pt->rtype)) {
 				    cli_dbgmsg("Matched signature for file type %s at %u\n", pt->virname, realoff);
 				    type = pt->type;
5d763f04
 				    if(ftoffset && (!*ftoffset || (*ftoffset)->cnt < MAX_EMBEDDED_OBJ || type == CL_TYPE_ZIPSFX) && ((ftype == CL_TYPE_MSEXE && type >= CL_TYPE_SFX) || ((ftype == CL_TYPE_MSEXE || ftype == CL_TYPE_ZIP || ftype == CL_TYPE_MSOLE2) && type == CL_TYPE_MSEXE)))  {
6038397e
 
5025967e
 					if(ac_addtype(ftoffset, type, realoff, ctx)) {
6038397e
 					    if(info.exeinfo.section)
 						free(info.exeinfo.section);
 					    return CL_EMEM;
8000d078
 					}
 				    }
 				}
e38ab7c1
 			    } else {
fbcef1b0
 				if(virname)
 				    *virname = pt->virname;
 
 				if(info.exeinfo.section)
 				    free(info.exeinfo.section);
 				return CL_VIRUS;
8000d078
 			    }
 			}
e38ab7c1
 			pt = pt->next_same;
8000d078
 		    }
 		}
e38ab7c1
 		patt = patt->next;
8000d078
 	    }
 	}
     }
 
841161e0
     if(info.exeinfo.section)
 	free(info.exeinfo.section);
 
6038397e
     return (mode & AC_SCAN_FT) ? type : CL_CLEAN;
8000d078
 }
fbcef1b0
 
 /* FIXME: clean up the code */
6038397e
 int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hexsig, uint32_t sigid, uint16_t parts, uint16_t partno, uint16_t rtype, uint16_t type, uint32_t mindist, uint32_t maxdist, const char *offset, uint8_t target)
fbcef1b0
 {
 	struct cli_ac_patt *new;
1a2906f4
 	char *pt, *pt2, *hex = NULL, *hexcpy = NULL;
 	uint16_t i, j, ppos = 0, pend, *dec;
1a648b37
 	uint8_t wprefix = 0, zprefix = 1, namelen, plen = 0;
 	struct cli_ac_alt *newalt, *altpt, **newtable;
 	int ret, error = CL_SUCCESS;
fbcef1b0
 
 
     if(strlen(hexsig) / 2 < root->ac_mindepth)
 	return CL_EPATSHORT;
 
     if((new = (struct cli_ac_patt *) cli_calloc(1, sizeof(struct cli_ac_patt))) == NULL)
 	return CL_EMEM;
 
6038397e
     new->rtype = rtype;
fbcef1b0
     new->type = type;
     new->sigid = sigid;
     new->parts = parts;
     new->partno = partno;
     new->mindist = mindist;
     new->maxdist = maxdist;
     new->target = target;
1a2906f4
     new->ch[0] |= CLI_MATCH_IGNORE;
     new->ch[1] |= CLI_MATCH_IGNORE;
 
     if(strchr(hexsig, '[')) {
 	if(!(hexcpy = cli_strdup(hexsig))) {
 	    free(new);
 	    return CL_EMEM;
 	}
 
 	hex = hexcpy;
 	for(i = 0; i < 2; i++) {
 		unsigned int n1, n2;
 
 	    if(!(pt = strchr(hex, '[')))
 		break;
 	    *pt++ = 0;
 
 	    if(!(pt2 = strchr(pt, ']'))) {
 		cli_dbgmsg("cli_ac_addsig: missing closing square bracket\n");
 		error = CL_EMALFDB;
 		break;
 	    }
 	    *pt2++ = 0;
 
             if(sscanf(pt, "%u-%u", &n1, &n2) != 2) {
 		cli_dbgmsg("cli_ac_addsig: incorrect range inside square brackets\n");
 		error = CL_EMALFDB;
 		break;
 	    }
 
 	    if((n1 > n2) || (n2 > AC_CH_MAXDIST)) {
 		cli_dbgmsg("cli_ac_addsig: incorrect range inside square brackets\n");
 		error = CL_EMALFDB;
 		break;
 	    }
 
 	    if(strlen(hex) == 2) {
 		if(i) {
 		    error = CL_EMALFDB;
 		    break;
 		}
 		dec = cli_hex2ui(hex);
 		if(!dec) {
 		    error = CL_EMALFDB;
 		    break;
 		}
 		new->ch[i] = *dec;
 		free(dec);
 		new->ch_mindist[i] = n1;
 		new->ch_maxdist[i] = n2;
 		hex = pt2;
 	    } else if(strlen(pt2) == 2) {
 		i = 1;
 		dec = cli_hex2ui(pt2);
 		if(!dec) {
 		    error = CL_EMALFDB;
 		    break;
 		}
 		new->ch[i] = *dec;
 		free(dec);
 		new->ch_mindist[i] = n1;
 		new->ch_maxdist[i] = n2;
 	    } else {
 		error = CL_EMALFDB;
 		break;
 	    }
 	}
 
 	if(error) {
 	    free(hexcpy);
 	    free(new);
 	    return error;
 	}
 
 	hex = cli_strdup(hex);
 	free(hexcpy);
 	if(!hex) {
 	    free(new);
 	    return CL_EMEM;
 	}
     }
fbcef1b0
 
     if(strchr(hexsig, '(')) {
1a2906f4
 	    char *hexnew, *start, *h, *c;
fbcef1b0
 
1a2906f4
 	if(hex) {
 	    hexcpy = hex;
 	} else if(!(hexcpy = cli_strdup(hexsig))) {
fbcef1b0
 	    free(new);
 	    return CL_EMEM;
 	}
 
 	if(!(hexnew = (char *) cli_calloc(strlen(hexsig) + 1, 1))) {
 	    free(hexcpy);
 	    free(new);
 	    return CL_EMEM;
 	}
 
 	start = pt = hexcpy;
 	while((pt = strchr(start, '('))) {
 	    *pt++ = 0;
 
 	    if(!start) {
1a648b37
 		error = CL_EMALFDB;
fbcef1b0
 		break;
 	    }
 
 	    strcat(hexnew, start);
 	    strcat(hexnew, "()");
 
 	    if(!(start = strchr(pt, ')'))) {
1a648b37
 		error = CL_EMALFDB;
fbcef1b0
 		break;
 	    }
 	    *start++ = 0;
 
1a648b37
 	    newalt = (struct cli_ac_alt *) cli_calloc(1, sizeof(struct cli_ac_alt));
 	    if(!newalt) {
 		cli_errmsg("cli_ac_addsig: Can't allocate newalt\n");
 		error = CL_EMEM;
 		break;
 	    }
 
fbcef1b0
 	    new->alt++;
1a648b37
 	    newtable = (struct cli_ac_alt **) cli_realloc(new->alttable, new->alt * sizeof(struct cli_ac_alt *));
 	    if(!newtable) {
 		new->alt--;
 		free(newalt);
 		cli_errmsg("cli_ac_addsig: Can't realloc new->alttable\n");
 		error = CL_EMEM;
 		break;
 	    }
 	    newtable[new->alt - 1] = newalt;
 	    new->alttable = newtable;
fbcef1b0
 
 	    for(i = 0; i < strlen(pt); i++)
 		if(pt[i] == '|')
1a648b37
 		    newalt->num++;
 
             if(!newalt->num) {
                 error = CL_EMALFDB;
                 break;
             } else
                 newalt->num++;
 
 	    if(3 * newalt->num - 1 == (uint16_t) strlen(pt)) {
 		newalt->chmode = 1;
20c2455d
 		newalt->str = (unsigned char *) cli_malloc(newalt->num);
1a648b37
 		if(!newalt->str) {
 		    cli_errmsg("cli_ac_addsig: Can't allocate newalt->str\n");
 		    error = CL_EMEM;
 		    break;
 		}
fbcef1b0
 	    }
 
1a648b37
 	    for(i = 0; i < newalt->num; i++) {
 		if(!(h = cli_strtok(pt, i, "|"))) {
 		    error = CL_EMALFDB;
fbcef1b0
 		    break;
 		}
 
1a648b37
 		if(!(c = cli_hex2str(h))) {
fbcef1b0
 		    free(h);
1a648b37
 		    error = CL_EMALFDB;
fbcef1b0
 		    break;
 		}
 
1a648b37
 		if(newalt->chmode) {
 		    newalt->str[i] = *c;
 		    free(c);
 		} else {
 		    if(i) {
 			altpt = newalt;
 			while(altpt->next)
 			    altpt = altpt->next;
 
 			altpt->next = (struct cli_ac_alt *) cli_calloc(1, sizeof(struct cli_ac_alt));
 			if(!altpt->next) {
 			    cli_errmsg("cli_ac_addsig: Can't allocate altpt->next\n");
 			    error = CL_EMEM;
 			    free(c);
 			    free(h);
 			    break;
 			}
 
20c2455d
 			altpt->next->str = (unsigned char *) c;
1a648b37
 			altpt->next->len = strlen(h) / 2;
 		    } else {
20c2455d
 			newalt->str = (unsigned char *) c;
1a648b37
 			newalt->len = strlen(h) / 2;
 		    }
 		}
 
fbcef1b0
 		free(h);
 	    }
 
 	    if(error)
 		break;
 	}
 
 	if(start)
 	    strcat(hexnew, start);
 
 	hex = hexnew;
 	free(hexcpy);
 
 	if(error) {
1a648b37
 	    if(new->alt) {
 		free(hex);
 		ac_free_alt(new);
 	    }
fbcef1b0
 	    free(new);
1a648b37
 	    return error;
fbcef1b0
 	}
     }
 
1a2906f4
     if((new->pattern = cli_hex2ui(hex ? hex : hexsig)) == NULL) {
 	if(new->alt)
1a648b37
 	    ac_free_alt(new);
1a2906f4
 	free(hex);
fbcef1b0
 	free(new);
 	return CL_EMALFDB;
     }
1a2906f4
     new->length = strlen(hex ? hex : hexsig) / 2;
     free(hex);
fbcef1b0
 
     for(i = 0; i < root->ac_maxdepth && i < new->length; i++) {
 	if(new->pattern[i] & CLI_MATCH_WILDCARD) {
 	    wprefix = 1;
 	    break;
 	}
c01ff22d
 	if(zprefix && new->pattern[i])
 	    zprefix = 0;
fbcef1b0
     }
 
c01ff22d
     if(wprefix || zprefix) {
fbcef1b0
 	pend = new->length - root->ac_mindepth + 1;
 	for(i = 0; i < pend; i++) {
 	    for(j = i; j < i + root->ac_maxdepth && j < new->length; j++) {
 		if(new->pattern[j] & CLI_MATCH_WILDCARD) {
 		    break;
 		} else {
e38ab7c1
 		    if(j - i + 1 >= plen) {
fbcef1b0
 			plen = j - i + 1;
 			ppos = i;
 		    }
 		}
e38ab7c1
 		if(plen >= root->ac_maxdepth && (new->pattern[ppos] || new->pattern[ppos + 1]))
fbcef1b0
 		    break;
 	    }
e38ab7c1
 	    if(plen >= root->ac_maxdepth && (new->pattern[ppos] || new->pattern[ppos + 1]))
fbcef1b0
 		break;
 	}
 
 	if(plen < root->ac_mindepth) {
 	    cli_errmsg("cli_ac_addsig: Can't find a static subpattern of length %u\n", root->ac_mindepth);
1a648b37
 	    ac_free_alt(new);
fbcef1b0
 	    free(new->pattern);
 	    free(new);
 	    return CL_EMALFDB;
 	}
 
 	new->prefix = new->pattern;
 	new->prefix_length = ppos;
 	new->pattern = &new->prefix[ppos];
 	new->length -= ppos;
 
 	for(i = 0; i < new->prefix_length; i++)
 	    if((new->prefix[i] & CLI_MATCH_WILDCARD) == CLI_MATCH_ALTERNATIVE)
 		new->alt_pattern++;
     }
 
     if(new->length > root->maxpatlen)
 	root->maxpatlen = new->length;
 
     if((pt = strstr(virname, " (Clam)")))
 	namelen = strlen(virname) - strlen(pt);
     else
 	namelen = strlen(virname);
 
     if(!namelen) {
 	cli_errmsg("cli_ac_addsig: No virus name\n");
1a2906f4
 	new->prefix ? free(new->prefix) : free(new->pattern);
1a648b37
 	ac_free_alt(new);
fbcef1b0
 	free(new);
 	return CL_EMALFDB;
     }
 
     if((new->virname = cli_calloc(namelen + 1, sizeof(char))) == NULL) {
1a2906f4
 	new->prefix ? free(new->prefix) : free(new->pattern);
1a648b37
 	ac_free_alt(new);
fbcef1b0
 	free(new);
 	return CL_EMEM;
     }
     strncpy(new->virname, virname, namelen);
72ce4b70
     new->virname[namelen]='\0';
fbcef1b0
 
1a648b37
     if(offset) {
 	new->offset = cli_strdup(offset);
 	if(!new->offset) {
1a2906f4
 	    new->prefix ? free(new->prefix) : free(new->pattern);
1a648b37
 	    ac_free_alt(new);
 	    free(new->virname);
 	    free(new);
 	    return CL_EMEM;
 	}
     }
 
fbcef1b0
     if((ret = cli_ac_addpatt(root, new))) {
1a2906f4
 	new->prefix ? free(new->prefix) : free(new->pattern);
fbcef1b0
 	free(new->virname);
1a648b37
 	ac_free_alt(new);
fbcef1b0
 	if(new->offset)
 	    free(new->offset);
 	free(new);
 	return ret;
     }
 
     return CL_SUCCESS;
 }
3d53538b
 
 void cli_ac_setdepth(uint8_t mindepth, uint8_t maxdepth)
 {
     cli_ac_mindepth = mindepth;
     cli_ac_maxdepth = maxdepth;
 }