libclamav/matcher.h
e3aaff8e
 /*
33872a43
  *  Copyright (C) 2007-2009 Sourcefire, Inc.
4addba22
  *
2023340a
  *  Authors: Tomasz Kojm
e3aaff8e
  *
  *  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.
e3aaff8e
  *
  *  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.
e3aaff8e
  */
 
 #ifndef __MATCHER_H
 #define __MATCHER_H
 
841161e0
 #include <sys/types.h>
 
e3aaff8e
 #include "clamav.h"
a85bd2ac
 #include "filetypes.h"
3c91998b
 #include "others.h"
841161e0
 #include "execs.h"
 #include "cltypes.h"
53721687
 #include "md5.h"
e3aaff8e
 
bedc58de
 #include "matcher-ac.h"
 #include "matcher-bm.h"
c3671221
 #include "hashtab.h"
bedc58de
 
b94e66c4
 #include "mpool.h"
 
bedc58de
 #define CLI_MATCH_WILDCARD	0xff00
ecf5865b
 #define CLI_MATCH_CHAR		0x0000
bedc58de
 #define CLI_MATCH_IGNORE	0x0100
 #define CLI_MATCH_ALTERNATIVE	0x0200
 #define CLI_MATCH_NIBBLE_HIGH	0x0300
 #define CLI_MATCH_NIBBLE_LOW	0x0400
 
677fc4ba
 struct cli_lsig_tdb {
 #define CLI_TDB_UINT	0
 #define CLI_TDB_RANGE	1
 #define CLI_TDB_STR	2
 #define CLI_TDB_RANGE2	3
     uint32_t *val, *range;
     char *str;
     uint32_t cnt[3];
 
     const uint32_t *target;
     const uint32_t *engine, *nos, *ep;
     const uint32_t *sectoff, *sectrva, *sectvsz, *sectraw, *sectrsz,
 		   *secturva, *sectuvsz, *secturaw, *sectursz;
0d9dbdef
 #ifdef USE_MPOOL
47d40feb
     mpool_t *mempool;
0d9dbdef
 #endif
677fc4ba
 };
 
 struct cli_ac_lsig {
     uint32_t id;
     char *logic;
     const char *virname;
     struct cli_lsig_tdb tdb;
 };
 
bedc58de
 struct cli_matcher {
33872a43
     unsigned int type;
 
bedc58de
     /* Extended Boyer-Moore */
ab1db3b3
     uint8_t *bm_shift;
bedc58de
     struct cli_bm_patt **bm_suffix;
cc447ac8
     struct cli_hashset md5_sizes_hs;
0a3d4094
     uint32_t *soff, soff_len; /* for PE section sigs */
aca9ea82
     uint32_t bm_patterns, bm_reloff_num, bm_absoff_num;
bedc58de
 
     /* Extended Aho-Corasick */
677fc4ba
     uint32_t ac_partsigs, ac_nodes, ac_patterns, ac_lsigs;
     struct cli_ac_lsig **ac_lsigtable;
bedc58de
     struct cli_ac_node *ac_root, **ac_nodetable;
fbcef1b0
     struct cli_ac_patt **ac_pattable;
33872a43
     struct cli_ac_patt **ac_reloff;
aca9ea82
     uint32_t ac_reloff_num, ac_absoff_num;
b5231f5f
     uint8_t ac_mindepth, ac_maxdepth;
 
     uint16_t maxpatlen;
     uint8_t ac_only;
0728972e
 #ifdef USE_MPOOL
47d40feb
     mpool_t *mempool;
0728972e
 #endif
bedc58de
 };
 
a7ac5978
 struct cli_meta_node {
     char *filename, *virname;
     struct cli_meta_node *next;
9381324a
     int csize, size, method;
     unsigned int crc32, fileno, encrypted, maxdepth;
a7ac5978
 };
 
4addba22
 struct cli_mtarget {
     cli_file_t target;
     const char *name;
     uint8_t idx;    /* idx of matcher */
     uint8_t ac_only;
 };
 
8af7ccd0
 #define CLI_MTARGETS 10
4addba22
 static const struct cli_mtarget cli_mtargets[CLI_MTARGETS] =  {
     { 0,		    "GENERIC",	    0,	0   },
     { CL_TYPE_MSEXE,	    "PE",	    1,	0   },
     { CL_TYPE_MSOLE2,	    "OLE2",	    2,	1   },
     { CL_TYPE_HTML,	    "HTML",	    3,	1   },
     { CL_TYPE_MAIL,	    "MAIL",	    4,	1   },
     { CL_TYPE_GRAPHICS,	    "GRAPHICS",	    5,	1   },
     { CL_TYPE_ELF,	    "ELF",	    6,	1   },
bc86a381
     { CL_TYPE_TEXT_ASCII,   "ASCII",	    7,	1   },
8af7ccd0
     { CL_TYPE_PE_DISASM,    "DISASM",	    8,	1   },
     { CL_TYPE_MACHO,	    "MACH-O",	    9,	1   }
4addba22
 };
b68d11d2
 
841161e0
 struct cli_target_info {
     off_t fsize;
     struct cli_exe_info exeinfo;
     int8_t status; /* 0 == not initialised, 1 == initialised OK, -1 == error */
 };
 
33872a43
 #define CLI_OFF_ANY         0xffffffff
 #define CLI_OFF_NONE	    0xfffffffe
 #define CLI_OFF_ABSOLUTE    1
 #define CLI_OFF_EOF_MINUS   2
 #define CLI_OFF_EP_PLUS     3
 #define CLI_OFF_EP_MINUS    4
 #define CLI_OFF_SL_PLUS     5
 #define CLI_OFF_SX_PLUS     6
 
e06afe8e
 int cli_scanbuff(const unsigned char *buffer, uint32_t length, uint32_t offset, cli_ctx *ctx, cli_file_t ftype, struct cli_ac_data **acdata);
5612732c
 
6038397e
 int cli_scandesc(int desc, cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli_matched_type **ftoffset, unsigned int acmode);
7ec67e94
 
33872a43
 int cli_caloff(const char *offstr, struct cli_target_info *info, int fd, unsigned int target, uint32_t *offdata, uint32_t *offset_min, uint32_t *offset_max);
53721687
 
e4101980
 int cli_checkfp(int fd, cli_ctx *ctx);
 
e3aaff8e
 #endif