1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
* Copyright (C) 2009 Sourcefire, Inc.
*
* Authors: aCaB
*
* 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.
*/
/* zlib-alike state interface to LZMA */
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include "clamav.h"
#include "lzma_iface.h"
void *__lzma_wrap_alloc(void *unused, size_t size) {
UNUSEDPARAM(unused);
if(!size || size > CLI_MAX_ALLOCATION)
return NULL;
if(!size || size > CLI_MAX_ALLOCATION) {
cli_dbgmsg("lzma_wrap_alloc(): Attempt to allocate %lu bytes.\n", (unsigned long int) size);
return NULL;
}
return cli_malloc(size);
}
void __lzma_wrap_free(void *unused, void *freeme) {
UNUSEDPARAM(unused);
free(freeme);
}
static ISzAlloc g_Alloc = { __lzma_wrap_alloc, __lzma_wrap_free };
static unsigned char lzma_getbyte(struct CLI_LZMA *L, int *fail) {
unsigned char c;
if(!L->next_in || !L->avail_in) {
*fail = 1;
return 0;
}
*fail = 0;
c = L->next_in[0];
L->next_in++;
L->avail_in--;
return c;
}
int cli_LzmaInit(struct CLI_LZMA *L, uint64_t size_override) {
int fail;
if(!L->init) {
L->p_cnt = LZMA_PROPS_SIZE;
if(size_override)
L->usize = size_override;
else
L->s_cnt = 8;
L->init = 1;
} 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--;
}
LzmaDec_Construct(&L->state);
if(LzmaDec_Allocate(&L->state, L->header, LZMA_PROPS_SIZE, &g_Alloc) != SZ_OK)
return LZMA_RESULT_DATA_ERROR;
LzmaDec_Init(&L->state);
L->freeme = 1;
return LZMA_RESULT_OK;
}
void cli_LzmaShutdown(struct CLI_LZMA *L) {
if(L->freeme)
LzmaDec_Free(&L->state, &g_Alloc);
return;
}
int cli_LzmaDecode(struct CLI_LZMA *L) {
SRes res;
SizeT outbytes, inbytes;
ELzmaStatus status;
ELzmaFinishMode finish;
if(!L->freeme) return cli_LzmaInit(L, 0);
inbytes = L->avail_in;
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;
L->next_in += inbytes;
L->avail_out -= outbytes;
L->next_out += outbytes;
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;
}
|