From 87787e76a800f4e089a2f0f967cb673045554eed Mon Sep 17 00:00:00 2001 From: aCaB Date: Wed, 5 Aug 2009 16:51:09 +0200 Subject: [PATCH] compiles, upx-lzma b0rked --- libclamav/lzma_iface.c | 61 ++++++++++++++--------------------------- libclamav/lzma_iface.h | 38 +++++++++++++++++-------- libclamav/nsis/nulsft.c | 13 +++++++-- libclamav/upx.c | 11 ++++---- 4 files changed, 65 insertions(+), 58 deletions(-) diff --git a/libclamav/lzma_iface.c b/libclamav/lzma_iface.c index 2e99cc2d7..994e7dbd0 100644 --- a/libclamav/lzma_iface.c +++ b/libclamav/lzma_iface.c @@ -25,55 +25,42 @@ #endif #include "lzma_iface.h" -#include "7z/LzmaDec.h" -#include "cltypes.h" -#include "others.h" static void *__wrap_alloc(void *unused, size_t size) { unused = unused; return cli_malloc(size); } -static void *__wrap_free(void *unused, void *freeme) { +static void __wrap_free(void *unused, void *freeme) { unused = unused; free(freeme); } static ISzAlloc g_Alloc = { __wrap_alloc, __wrap_free }; -struct CLI_LZMA { - CLzmaDec state; - unsigned char header[LZMA_PROPS_SIZE]; - unsigned int p_cnt; - unsigned int s_cnt; - unsigned int freeme; - uint64_t usize; - ELzmaFinishMode finish; -}; -static unsigned char lzma_getbyte(CLI_LZMA *L, int *fail) { - unsigned char *c = (unsigned char *)L->next_in; - if(!c || !L->avail_in) { +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; - L->next_in = &c[1]; + c = L->next_in[0]; + L->next_in++; L->avail_in--; - return *c; + return c; } -int cli_LzmaInit(CLI_LZMA **Lp, uint64_t size_override) { - CLI_LZMA *L = *Lp; +int cli_LzmaInit(struct CLI_LZMA *L, uint64_t size_override) { int fail; - if(!L) { - *Lp = L = cli_calloc(sizeof(*L), 1); - if(!L) return CL_EMEM; + 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"); @@ -95,42 +82,36 @@ int cli_LzmaInit(CLI_LZMA **Lp, uint64_t size_override) { LzmaDec_Construct(&L->state); if(LzmaDec_Allocate(&L->state, L->header, LZMA_PROPS_SIZE, &g_Alloc) != SZ_OK) return CL_EMEM; - LzmaDec_Init(&state); + LzmaDec_Init(&L->state); L->freeme = 1; - if(~L-usize) L->finish = LZMA_FINISH_END; + if(~L->usize) L->finish = LZMA_FINISH_END; else L->finish = LZMA_FINISH_ANY; return LZMA_RESULT_OK; } -void cli_LzmaShutdown(CLI_LZMA **Lp) { - CLI_LZMA *L; - - if(!Lp) return; - L = *Lp; +void cli_LzmaShutdown(struct CLI_LZMA *L) { if(L->freeme) LzmaDec_Free(&L->state, &g_Alloc); - free(L); - *Lp = NULL; return; } -int cli_LzmaDecode(CLI_LZMA **Lp) { - CLI_LZMA *L = *Lp; - - if(!L->freeme) return cli_LzmaInit(LP, 0); - +int cli_LzmaDecode(struct CLI_LZMA *L) { SRes res; - SizeT outbytes = L->avail_out; - SizeT inbytes = L->avail_in; + SizeT outbytes, inbytes; ELzmaStatus status; + + if(!L->freeme) return cli_LzmaInit(L, 0); + + outbytes = L->avail_out; + inbytes = L->avail_in; res = LzmaDec_DecodeToBuf(&L->state, L->next_out, &outbytes, L->next_in, &inbytes, L->finish, &status); L->next_in += inbytes; L->next_out += outbytes; L->usize -= outbytes; - + return 0; /* FIXMELZMA */ } /* int cli_LzmaInitUPX(CLI_LZMA **Lp, uint32_t dictsz) { */ diff --git a/libclamav/lzma_iface.h b/libclamav/lzma_iface.h index 6833f1a31..4ee5d08ea 100644 --- a/libclamav/lzma_iface.h +++ b/libclamav/lzma_iface.h @@ -23,21 +23,37 @@ #ifndef __LZMA_IFACE_H #define __LZMA_IFACE_H +#include "7z/LzmaDec.h" #include "cltypes.h" +#include "others.h" -typedef struct CLI_LZMA_tag CLI_LZMA; - -struct stream_state { - uint32_t avail_in; - unsigned char *next_in; - uint32_t avail_out; - unsigned char *next_out; +struct CLI_LZMA { + CLzmaDec state; + unsigned char header[LZMA_PROPS_SIZE]; + unsigned int p_cnt; + unsigned int s_cnt; + unsigned int freeme; + unsigned int init; + uint64_t usize; + ELzmaFinishMode finish; + unsigned char *next_in; + unsigned char *next_out; + SizeT avail_in; + SizeT avail_out; }; -int cli_LzmaInit(CLI_LZMA **, uint64_t); -void cli_LzmaShutdown(CLI_LZMA **); -int cli_LzmaDecode(CLI_LZMA **, struct stream_state*); -int cli_LzmaInitUPX(CLI_LZMA **, uint32_t); + +struct stream_state { + uint32_t avail_in; + unsigned char *next_in; + uint32_t avail_out; + unsigned char *next_out; +}; + +int cli_LzmaInit(struct CLI_LZMA *, uint64_t); +void cli_LzmaShutdown(struct CLI_LZMA *); +int cli_LzmaDecode(struct CLI_LZMA *); +/* int cli_LzmaInitUPX(struct CLI_LZMA **, uint32_t); FIXMELZMA */ #define LZMA_STREAM_END 2 #define LZMA_RESULT_OK 0 diff --git a/libclamav/nsis/nulsft.c b/libclamav/nsis/nulsft.c index 97628eb7d..bd50ac617 100644 --- a/libclamav/nsis/nulsft.c +++ b/libclamav/nsis/nulsft.c @@ -76,7 +76,7 @@ struct nsis_st { uint8_t eof; struct stream_state nsis; nsis_bzstream bz; - CLI_LZMA* lz; + struct CLI_LZMA lz; /* z_stream z; */ nsis_z_stream z; unsigned char *freeme; @@ -97,6 +97,7 @@ static int nsis_init(struct nsis_st *n) { n->freecomp=1; break; case COMP_LZMA: + memset(&n->bz, 0, sizeof(struct CLI_LZMA)); cli_LzmaInit(&n->lz, 0xffffffffffffffffULL); n->freecomp=1; break; @@ -150,13 +151,21 @@ static int nsis_decomp(struct nsis_st *n) { n->nsis.next_out = n->bz.next_out; break; case COMP_LZMA: - switch (cli_LzmaDecode(&n->lz, &n->nsis)) { + n->lz.avail_in = n->nsis.avail_in; + n->lz.next_in = n->nsis.next_in; + n->lz.avail_out = n->nsis.avail_out; + n->lz.next_out = n->nsis.next_out; + switch (cli_LzmaDecode(&n->lz)) { case LZMA_RESULT_OK: ret = CL_SUCCESS; break; case LZMA_STREAM_END: ret = CL_BREAK; } + n->nsis.avail_in = n->lz.avail_in; + n->nsis.next_in = n->lz.next_in; + n->nsis.avail_out = n->lz.avail_out; + n->nsis.next_out = n->lz.next_out; break; case COMP_ZLIB: n->z.avail_in = n->nsis.avail_in; diff --git a/libclamav/upx.c b/libclamav/upx.c index 424135a27..d7dc814ea 100644 --- a/libclamav/upx.c +++ b/libclamav/upx.c @@ -523,22 +523,23 @@ int upx_inflate2e(char *src, uint32_t ssize, char *dst, uint32_t *dsize, uint32_ } int upx_inflatelzma(char *src, uint32_t ssize, char *dst, uint32_t *dsize, uint32_t upx0, uint32_t upx1, uint32_t ep) { - CLI_LZMA *lz = NULL; + struct CLI_LZMA l; struct stream_state s; uint32_t magic[]={0xb16,0xb1e,0}; - cli_LzmaInitUPX(&lz, *dsize); + memset(&l, 0, sizeof(l)); + //cli_LzmaInitUPX(&lz, *dsize); /* FIXMELZMA: make func and check return value */ s.avail_in = ssize; s.avail_out = *dsize; s.next_in = (unsigned char*)src+2; s.next_out = (unsigned char*)dst; - if(cli_LzmaDecode(&lz, &s)==LZMA_RESULT_DATA_ERROR) { + if(cli_LzmaDecode(&l)==LZMA_RESULT_DATA_ERROR) { /* __asm__ __volatile__("int3"); */ - cli_LzmaShutdown(&lz); + cli_LzmaShutdown(&l); return -1; } - cli_LzmaShutdown(&lz); + cli_LzmaShutdown(&l); return pefromupx (src, ssize, dst, dsize, ep, upx0, upx1, magic, *dsize); }