libclamav/msexpand.c
341e5433
 /*
c442ca9c
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
2023340a
  *
  *  Authors: Tomasz Kojm
6289eda8
  * 
  *  Acknowledgements: Decompression scheme by M. Winterhoff.
341e5433
  *
  *  This program is free software; you can redistribute it and/or modify
6e47a652
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
341e5433
  *
  *  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.
341e5433
  */
 
7dcdec59
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
341e5433
 #include <stdio.h>
6e47a652
 #include <stddef.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
d72f003e
 #include <string.h>
341e5433
 
6e47a652
 #include "clamav.h"
341e5433
 #include "others.h"
079229d6
 #include "msexpand.h"
786a7e91
 #include "fmap.h"
341e5433
 
6e47a652
 #ifndef HAVE_ATTRIB_PACKED
 #define __attribute__(x)
 #endif
341e5433
 
343dd145
 #ifdef HAVE_PRAGMA_PACK
 #pragma pack(1)
 #endif
 
 #ifdef HAVE_PRAGMA_PACK_HPPA
 #pragma pack 1
 #endif
 
6e47a652
 #define EC32(x) le32_to_host(x)
 #define EC16(x) le16_to_host(x)
 
 #define MAGIC1	0x44445a53
 #define MAGIC2	0x3327f088
 #define MAGIC3	0x0041
 
 struct msexp_hdr {
     uint32_t magic1;
     uint32_t magic2;
     uint16_t magic3;
     uint32_t fsize;
343dd145
 } __attribute__((packed));
 
 #ifdef HAVE_PRAGMA_PACK
 #pragma pack()
 #endif
 
 #ifdef HAVE_PRAGMA_PACK_HPPA
 #pragma pack
 #endif
6e47a652
 
c3ba5156
 #define B_SIZE 4096
 #define RW_SIZE 2048
6e47a652
 
 #define READBYTES				\
786a7e91
     rbytes = MIN(RW_SIZE, map->len - cur_off);  \
     if(!rbytes)					\
6e47a652
 	break;					\
786a7e91
     rbuff = fmap_need_off_once(map, cur_off, rbytes); \
     if(!rbuff)					\
 	return CL_EREAD;			\
     cur_off += rbytes;				\
6e47a652
     r = 0;
 
 #define WRITEBYTES				\
     ret = cli_writen(ofd, wbuff, w);		\
     if(ret == -1 || (unsigned int) ret != w)	\
871177cd
 	return CL_EWRITE;			\
6e47a652
     wbytes += w;				\
786a7e91
     if(wbytes >= fsize)				\
6e47a652
 	return CL_SUCCESS;			\
     w = 0;
 
 
786a7e91
 int cli_msexpand(cli_ctx *ctx, int ofd)
6e47a652
 {
f304dc68
 	const struct msexp_hdr *hdr;
6e47a652
 	uint8_t i, mask, bits;
786a7e91
 	unsigned char buff[B_SIZE], wbuff[RW_SIZE];
927b2bab
 	const unsigned char *rbuff = NULL; 	// rbuff will be set to a real address by READBYTES
 										// in the first iteration of the loop.
c3ba5156
 	unsigned int j = B_SIZE - 16, k, l, r = 0, w = 0, rbytes = 0, wbytes = 0;
786a7e91
 	fmap_t *map = *ctx->fmap;
 	off_t cur_off = sizeof(*hdr);
 	unsigned int fsize;
6e47a652
 	int ret;
341e5433
 
786a7e91
     if(!(hdr = fmap_need_off_once(map, 0, sizeof(*hdr))))
871177cd
 	return CL_EREAD;
341e5433
 
786a7e91
     if(EC32(hdr->magic1) != MAGIC1 || EC32(hdr->magic2) != MAGIC2 || EC16(hdr->magic3) != MAGIC3) {
6e47a652
 	cli_dbgmsg("MSEXPAND: Not supported file format\n");
 	return CL_EFORMAT;
     }
341e5433
 
786a7e91
     fsize = EC32(hdr->fsize);
     cli_dbgmsg("MSEXPAND: File size from header: %u\n", fsize);
341e5433
 
786a7e91
     if(cli_checklimits("MSEXPAND", ctx, fsize, 0, 0)!=CL_CLEAN)
d91ab809
         return CL_SUCCESS;
341e5433
 
c3ba5156
     memset(buff, 0, B_SIZE);
6e47a652
     while(1) {
341e5433
 
6e47a652
 	if(!rbytes || (r == rbytes)) {
 	    READBYTES;
341e5433
 	}
 
6e47a652
 	bits = rbuff[r]; r++;
341e5433
 
6e47a652
 	mask = 1;
 	for(i = 0; i < 8; i++) {
 	    if(bits & mask) {
 		if(r == rbytes) {
 		    READBYTES;
 		}
341e5433
 
c3ba5156
 		if(w == RW_SIZE) {
6e47a652
 		    WRITEBYTES;
 		}
341e5433
 
6e47a652
 		wbuff[w] = buff[j] = rbuff[r];
 		r++; w++;
c3ba5156
 		j++; j %= B_SIZE;
6e47a652
 	    } else {
 		if(r == rbytes) {
 		    READBYTES;
 		}
 		k = rbuff[r]; r++;
341e5433
 
6e47a652
 		if(r == rbytes) {
 		    READBYTES;
341e5433
 		}
6e47a652
 		l = rbuff[r]; r++;
341e5433
 
6e47a652
 		k += (l & 0xf0) << 4;
 		l = (l & 0x0f) + 3;
 		while(l--) {
c3ba5156
 		    if(w == RW_SIZE) {
6e47a652
 			WRITEBYTES;
 		    }
 		    wbuff[w] = buff[j] = buff[k];
 		    w++;
c3ba5156
 		    k++; k %= B_SIZE;
 		    j++; j %= B_SIZE;
341e5433
 		}
 	    }
6e47a652
 	    mask *= 2;
341e5433
 	}
     }
 
6e47a652
     if(w) {
 	WRITEBYTES;
     }
 
     return CL_SUCCESS;
341e5433
 }