libclamav/lzma_iface.c
f377e052
 /*
e1cbc270
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
2023340a
  *
7dd5d4c9
  *  Authors: aCaB
6289eda8
  * 
  *  Acknowledgements: This contains an implementation of the LZMA algorithm 
  *                    from Igor Pavlov (see COPYING.lzma).
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
 
60d8d2c3
 #include "clamav.h"
f377e052
 #include "lzma_iface.h"
 
288057e9
 void *__lzma_wrap_alloc(void *unused, size_t size)
 {
cd94be7a
     UNUSEDPARAM(unused);
288057e9
     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;
a91a4459
     }
 
52b451cc
     return cli_calloc(1, size);
7dd5d4c9
 }
288057e9
 void __lzma_wrap_free(void *unused, void *freeme)
 {
cd94be7a
     UNUSEDPARAM(unused);
7dd5d4c9
     free(freeme);
 }
288057e9
 static ISzAlloc g_Alloc = {__lzma_wrap_alloc, __lzma_wrap_free};
7dd5d4c9
 
288057e9
 static unsigned char lzma_getbyte(struct CLI_LZMA *L, int *fail)
 {
87787e76
     unsigned char c;
288057e9
     if (!L->next_in || !L->avail_in) {
         *fail = 1;
         return 0;
7dd5d4c9
     }
     *fail = 0;
288057e9
     c     = L->next_in[0];
87787e76
     L->next_in++;
7dd5d4c9
     L->avail_in--;
87787e76
     return c;
7dd5d4c9
 }
f377e052
 
288057e9
 int cli_LzmaInit(struct CLI_LZMA *L, uint64_t size_override)
 {
7dd5d4c9
     int fail;
 
288057e9
     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--;
7dd5d4c9
     }
 
288057e9
     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--;
7dd5d4c9
     }
f377e052
 
7dd5d4c9
     LzmaDec_Construct(&L->state);
288057e9
     if (LzmaDec_Allocate(&L->state, L->header, LZMA_PROPS_SIZE, &g_Alloc) != SZ_OK)
         return LZMA_RESULT_DATA_ERROR;
87787e76
     LzmaDec_Init(&L->state);
f377e052
 
7dd5d4c9
     L->freeme = 1;
     return LZMA_RESULT_OK;
f377e052
 }
 
288057e9
 void cli_LzmaShutdown(struct CLI_LZMA *L)
 {
     if (L->freeme)
         LzmaDec_Free(&L->state, &g_Alloc);
7dd5d4c9
     return;
f377e052
 }
 
288057e9
 int cli_LzmaDecode(struct CLI_LZMA *L)
 {
7dd5d4c9
     SRes res;
87787e76
     SizeT outbytes, inbytes;
7dd5d4c9
     ELzmaStatus status;
5e1d2c28
     ELzmaFinishMode finish;
87787e76
 
288057e9
     if (!L->freeme) return cli_LzmaInit(L, 0);
87787e76
 
     inbytes = L->avail_in;
288057e9
     if (~L->usize && L->avail_out > L->usize) {
         outbytes = L->usize;
         finish   = LZMA_FINISH_END;
5e1d2c28
     } else {
288057e9
         outbytes = L->avail_out;
         finish   = LZMA_FINISH_ANY;
5e1d2c28
     }
     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;
288057e9
     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;
5e1d2c28
     return LZMA_RESULT_OK;
f377e052
 }