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