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