libclamav/lzma_iface.c
f377e052
 /*
7dd5d4c9
  *  Copyright (C) 2009 Sourcefire, Inc.
2023340a
  *
7dd5d4c9
  *  Authors: aCaB
f377e052
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
  *
  *  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
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
  */
 
7dd5d4c9
 /* zlib-alike state interface to LZMA */
f377e052
 
7dd5d4c9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
f377e052
 
 #include "lzma_iface.h"
 
81fded11
 void *__lzma_wrap_alloc(void *unused, size_t size) { 
7dd5d4c9
     unused = unused;
     return cli_malloc(size);
 }
81fded11
 void __lzma_wrap_free(void *unused, void *freeme) {
7dd5d4c9
     unused = unused;
     free(freeme);
 }
81fded11
 static ISzAlloc g_Alloc = { __lzma_wrap_alloc, __lzma_wrap_free };
7dd5d4c9
 
87787e76
 
 static unsigned char lzma_getbyte(struct CLI_LZMA *L, int *fail) {
     unsigned char c;
     if(!L->next_in || !L->avail_in) {
7dd5d4c9
 	*fail = 1;
 	return 0;
     }
     *fail = 0;
87787e76
     c = L->next_in[0];
     L->next_in++;
7dd5d4c9
     L->avail_in--;
87787e76
     return c;
7dd5d4c9
 }
f377e052
     
 
87787e76
 int cli_LzmaInit(struct CLI_LZMA *L, uint64_t size_override) {
7dd5d4c9
     int fail;
 
87787e76
     if(!L->init) {
7dd5d4c9
 	L->p_cnt = LZMA_PROPS_SIZE;
 	if(size_override)
 	    L->usize = size_override;
 	else
 	    L->s_cnt = 8;
87787e76
 	L->init = 1;
7dd5d4c9
     } else if(size_override)
 	cli_warnmsg("cli_LzmaInit: ignoring late size override\n");
 
     if(L->freeme) return LZMA_RESULT_OK;
 
     while(L->p_cnt) {
 	L->header[LZMA_PROPS_SIZE - L->p_cnt] = lzma_getbyte(L, &fail);
 	if(fail) return LZMA_RESULT_OK;
 	L->p_cnt--;
     }
 
     while(L->s_cnt) {
 	uint64_t c = (uint64_t)lzma_getbyte(L, &fail);
 	if(fail) return LZMA_RESULT_OK;
 	L->usize = c << (8 * (8 - L->s_cnt));
 	L->s_cnt--;
     }
f377e052
 
7dd5d4c9
     LzmaDec_Construct(&L->state);
     if(LzmaDec_Allocate(&L->state, L->header, LZMA_PROPS_SIZE, &g_Alloc) != SZ_OK)
5e1d2c28
 	return LZMA_RESULT_DATA_ERROR;
87787e76
     LzmaDec_Init(&L->state);
f377e052
 
7dd5d4c9
     L->freeme = 1;
     return LZMA_RESULT_OK;
f377e052
 }
7dd5d4c9
 	
f377e052
 
87787e76
 void cli_LzmaShutdown(struct CLI_LZMA *L) {
7dd5d4c9
     if(L->freeme)
 	LzmaDec_Free(&L->state, &g_Alloc);
     return;
f377e052
 }
 
 
87787e76
 int cli_LzmaDecode(struct CLI_LZMA *L) {
7dd5d4c9
     SRes res;
87787e76
     SizeT outbytes, inbytes;
7dd5d4c9
     ELzmaStatus status;
5e1d2c28
     ELzmaFinishMode finish;
87787e76
 
     if(!L->freeme) return cli_LzmaInit(L, 0);
 
     inbytes = L->avail_in;
5e1d2c28
     if(~L->usize && L->avail_out > L->usize) {
 	outbytes = L->usize;
 	finish = LZMA_FINISH_END;
     } else {
 	outbytes = L->avail_out;
 	finish = LZMA_FINISH_ANY;
     }
     res = LzmaDec_DecodeToBuf(&L->state, L->next_out, &outbytes, L->next_in, &inbytes, finish, &status);
     L->avail_in -= inbytes;
7dd5d4c9
     L->next_in += inbytes;
5e1d2c28
     L->avail_out -= outbytes;
7dd5d4c9
     L->next_out += outbytes;
5e1d2c28
     if(~L->usize) L->usize -= outbytes;
     if(res != SZ_OK)
 	return LZMA_RESULT_DATA_ERROR;
     if(!L->usize || status == LZMA_STATUS_FINISHED_WITH_MARK)
 	return LZMA_STREAM_END;
     return LZMA_RESULT_OK;
f377e052
 }