libclamav/others.h
e3aaff8e
 /*
8f84357e
  *  Copyright (C) 1999 - 2005 Tomasz Kojm <tkojm@clamav.net>
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
  */
 
8000d078
 #ifndef __OTHERS_H_LC
 #define __OTHERS_H_LC
e3aaff8e
 
a087f040
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
d9e258d5
 #include <stdio.h>
e3aaff8e
 #include <stdlib.h>
5ae8bdd5
 #include "cltypes.h"
e3aaff8e
 
375beda4
 #include "clamav.h"
bc93eda0
 #include "dconf.h"
375beda4
 
9c291186
 /*
  * CLI_ISCONTAINED(buf1, size1, buf2, size2) checks if buf2 is contained
  * within buf1.
  *
  * buf1 and buf2 are pointers (or offsets) for the main buffer and the
  * sub-buffer respectively, and size1/2 are their sizes
  *
  * The macro can be used to protect against wraps.
  */
1f280d21
 #define CLI_ISCONTAINED(bb, bb_size, sb, sb_size)	\
     (bb_size > 0 && sb_size > 0 && sb_size <= bb_size	\
      && sb >= bb && sb + sb_size <= bb + bb_size && sb + sb_size > bb)
 
941c735c
 #define CLI_ISCONTAINED2(bb, bb_size, sb, sb_size)	\
     (bb_size > 0 && sb_size >= 0 && sb_size <= bb_size	\
29c36b67
      && sb >= bb && sb + sb_size <= bb + bb_size && sb + sb_size >= bb)
941c735c
 
573cac8f
 #define CLI_MAX_ALLOCATION 184549376
1e9c4900
 
a087f040
 #ifdef	HAVE_SYS_PARAM_H
 #include <sys/param.h>	/* for NAME_MAX */
 #endif
 
06646acf
 /* Maximum filenames under various systems - njh */
 #ifndef	NAME_MAX	/* e.g. Linux */
 # ifdef	MAXNAMELEN	/* e.g. Solaris */
 #   define	NAME_MAX	MAXNAMELEN
 # else
 #   ifdef	FILENAME_MAX	/* e.g. SCO */
 #     define	NAME_MAX	FILENAME_MAX
7d34797b
 #   else
 #     define    NAME_MAX        256
06646acf
 #   endif
 # endif
 #endif
 
7d34797b
 #if NAME_MAX < 256
 #undef NAME_MAX
 #define NAME_MAX 256
 #endif
 
3c91998b
 /* internal clamav context */
 typedef struct {
     const char **virname;
     unsigned long int *scanned;
     const struct cli_matcher *root;
     const struct cl_engine *engine;
     const struct cl_limits *limits;
     unsigned int options;
     unsigned int arec;
     unsigned int mrec;
bc93eda0
     struct cli_dconf *dconf;
3c91998b
 } cli_ctx;
 
 #define SCAN_ARCHIVE	    (ctx->options & CL_SCAN_ARCHIVE)
 #define SCAN_MAIL	    (ctx->options & CL_SCAN_MAIL)
 #define SCAN_OLE2	    (ctx->options & CL_SCAN_OLE2)
c5107e70
 #define SCAN_PDF	    (ctx->options & CL_SCAN_PDF)
3c91998b
 #define SCAN_HTML	    (ctx->options & CL_SCAN_HTML)
 #define SCAN_PE		    (ctx->options & CL_SCAN_PE)
3f97a1e7
 #define SCAN_ELF	    (ctx->options & CL_SCAN_ELF)
6fd2fb47
 #define SCAN_ALGO 	    (ctx->options & CL_SCAN_ALGORITHMIC)
3c91998b
 #define DETECT_ENCRYPTED    (ctx->options & CL_SCAN_BLOCKENCRYPTED)
 #define BLOCKMAX	    (ctx->options & CL_SCAN_BLOCKMAX)
 #define DETECT_BROKEN	    (ctx->options & CL_SCAN_BLOCKBROKEN)
 
75282b5c
 #if WORDS_BIGENDIAN == 0
 /* new macros from A. Melnikoff */
 #define le16_to_host(v)	(v)
 #define le32_to_host(v)	(v)
 #define le64_to_host(v)	(v)
38cb3f8f
 #define	be16_to_host(v)	((v >> 8) | ((v & 0xFF) << 8))
75282b5c
 #define	be32_to_host(v)	((v >> 24) | ((v & 0x00FF0000) >> 8) | \
 				((v & 0x0000FF00) << 8) | (v << 24))
 #define be64_to_host(v)	((v >> 56) | ((v & 0x00FF000000000000LL) >> 40) | \
 				((v & 0x0000FF0000000000LL) >> 24) | \
 				((v & 0x000000FF00000000LL) >> 8) |  \
 				((v & 0x00000000FF000000LL) << 8) |  \
 				((v & 0x0000000000FF0000LL) << 24) | \
 				((v & 0x000000000000FF00LL) << 40) | \
 				(v << 56))
 #else
38cb3f8f
 #define	le16_to_host(v)	((v >> 8) | ((v & 0xFF) << 8))
75282b5c
 #define	le32_to_host(v)	((v >> 24) | ((v & 0x00FF0000) >> 8) | \
 				((v & 0x0000FF00) << 8) | (v << 24))
 #define le64_to_host(v)	((v >> 56) | ((v & 0x00FF000000000000LL) >> 40) | \
 				((v & 0x0000FF0000000000LL) >> 24) | \
 				((v & 0x000000FF00000000LL) >> 8) |  \
 				((v & 0x00000000FF000000LL) << 8) |  \
 				((v & 0x0000000000FF0000LL) << 24) | \
 				((v & 0x000000000000FF00LL) << 40) | \
 				(v << 56))
 #define be16_to_host(v)	(v)
 #define be32_to_host(v)	(v)
 #define be64_to_host(v)	(v)
 #endif
 
 /* used by: spin, yc (C) aCaB */
 #define ROL(a,b) a = ( a << (b % (sizeof(a)<<3) ))  |  (a >> (  (sizeof(a)<<3)  -  (b % (sizeof(a)<<3 )) ) )
 #define ROR(a,b) a = ( a >> (b % (sizeof(a)<<3) ))  |  (a << (  (sizeof(a)<<3)  -  (b % (sizeof(a)<<3 )) ) )
 
b58fdfc2
 #ifndef	FALSE
75282b5c
 #define FALSE (0)
b58fdfc2
 #endif
 
 #ifndef	TRUE
75282b5c
 #define TRUE (1)
b58fdfc2
 #endif
75282b5c
 
 #ifndef MIN
 #define MIN(a, b)	(((a) < (b)) ? (a) : (b))
 #endif
 #ifndef MAX
 #define MAX(a,b)	(((a) > (b)) ? (a) : (b))
 #endif
 
8a9c2d19
 typedef struct bitset_tag
 {
         unsigned char *bitset;
         unsigned long length;
 } bitset_t;
 
3c30b19b
 #ifdef __GNUC__
 void cli_warnmsg(const char *str, ...) __attribute__((format(printf, 1, 2)));
 #else
e3aaff8e
 void cli_warnmsg(const char *str, ...);
3c30b19b
 #endif
 
 #ifdef __GNUC__
 void cli_errmsg(const char *str, ...) __attribute__((format(printf, 1, 2)));
 #else
e3aaff8e
 void cli_errmsg(const char *str, ...);
3c30b19b
 #endif
 
 #ifdef __GNUC__
 void cli_dbgmsg(const char *str, ...) __attribute__((format(printf, 1, 2)));
 #else
e3aaff8e
 void cli_dbgmsg(const char *str, ...);
3c30b19b
 #endif
 
e3aaff8e
 void *cli_malloc(size_t nmemb);
 void *cli_calloc(size_t nmemb, size_t size);
8139fd99
 void *cli_realloc(void *ptr, size_t size);
786b43b6
 char *cli_strdup(const char *s);
e3aaff8e
 int cli_rmdirs(const char *dirname);
db65451b
 unsigned char *cli_md5digest(int desc);
335d1663
 char *cli_md5stream(FILE *fs, unsigned char *digcpy);
 char *cli_md5file(const char *filename);
5b25b5e8
 int cli_readn(int fd, void *buff, unsigned int count);
5d2d2d77
 int cli_writen(int fd, const void *buff, unsigned int count);
8000d078
 char *cli_gentemp(const char *dir);
0e621e7d
 char *cli_gentempdir(const char *dir);
 char *cli_gentempdesc(const char *dir, int *fd);
 char *cli_gentempstream(const char *dir, FILE **fs);
8000d078
 unsigned int cli_rndnum(unsigned int max);
b067a7dd
 int cli_filecopy(const char *src, const char *dest);
079229d6
 bitset_t *cli_bitset_init(void);
8a9c2d19
 void cli_bitset_free(bitset_t *bs);
 int cli_bitset_set(bitset_t *bs, unsigned long bit_offset);
 int cli_bitset_test(bitset_t *bs, unsigned long bit_offset);
c0f4d1d7
 
 #if WORDS_BIGENDIAN == 0
 #define cli_readint32(buff) (*(const int32_t *)(buff))
 #define cli_writeint32(offset, value) (*(uint32_t *)(offset)=(uint32_t)(value))
 #else
 static inline int32_t cli_readint32(const char *buff)
 {
 	int32_t ret;
     ret = buff[0] & 0xff;
     ret |= (buff[1] & 0xff) << 8;
     ret |= (buff[2] & 0xff) << 16;
     ret |= (buff[3] & 0xff) << 24;
     return ret;
 }
 
 static inline void cli_writeint32(char *offset, uint32_t value)
 {
     offset[0] = value & 0xff;
     offset[1] = (value & 0xff00) >> 8;
     offset[2] = (value & 0xff0000) >> 16;
     offset[3] = (value & 0xff000000) >> 24;
 }
 #endif /* WORDS_BIGENDIAN == 0 */
 
e3aaff8e
 #endif