1 /* Lzma86Dec.c -- LZMA + x86 (BCJ) Filter Decoder
2 2009-08-14 : Igor Pavlov : Public domain */
3 
4 #include "Lzma86.h"
5 
6 #include "Alloc.h"
7 #include "Bra.h"
8 #include "LzmaDec.h"
9 
SzAlloc(void * p,size_t size)10 static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); }
SzFree(void * p,void * address)11 static void SzFree(void *p, void *address) { p = p; MyFree(address); }
12 
Lzma86_GetUnpackSize(const Byte * src,SizeT srcLen,UInt64 * unpackSize)13 SRes Lzma86_GetUnpackSize(const Byte *src, SizeT srcLen, UInt64 *unpackSize)
14 {
15   unsigned i;
16   if (srcLen < LZMA86_HEADER_SIZE)
17     return SZ_ERROR_INPUT_EOF;
18   *unpackSize = 0;
19   for (i = 0; i < sizeof(UInt64); i++)
20     *unpackSize += ((UInt64)src[LZMA86_SIZE_OFFSET + i]) << (8 * i);
21   return SZ_OK;
22 }
23 
Lzma86_Decode(Byte * dest,SizeT * destLen,const Byte * src,SizeT * srcLen)24 SRes Lzma86_Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen)
25 {
26   ISzAlloc g_Alloc = { SzAlloc, SzFree };
27   SRes res;
28   int useFilter;
29   SizeT inSizePure;
30   ELzmaStatus status;
31 
32   if (*srcLen < LZMA86_HEADER_SIZE)
33     return SZ_ERROR_INPUT_EOF;
34 
35   useFilter = src[0];
36 
37   if (useFilter > 1)
38   {
39     *destLen = 0;
40     return SZ_ERROR_UNSUPPORTED;
41   }
42 
43   inSizePure = *srcLen - LZMA86_HEADER_SIZE;
44   res = LzmaDecode(dest, destLen, src + LZMA86_HEADER_SIZE, &inSizePure,
45       src + 1, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &g_Alloc);
46   *srcLen = inSizePure + LZMA86_HEADER_SIZE;
47   if (res != SZ_OK)
48     return res;
49   if (useFilter == 1)
50   {
51     UInt32 x86State;
52     x86_Convert_Init(x86State);
53     x86_Convert(dest, *destLen, 0, &x86State, 0);
54   }
55   return SZ_OK;
56 }
57