xref: /third_party/lzma/C/Lzma86Dec.c (revision 370b324c)
1370b324cSopenharmony_ci/* Lzma86Dec.c -- LZMA + x86 (BCJ) Filter Decoder
2370b324cSopenharmony_ci2023-03-03 : Igor Pavlov : Public domain */
3370b324cSopenharmony_ci
4370b324cSopenharmony_ci#include "Precomp.h"
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#include "Lzma86.h"
7370b324cSopenharmony_ci
8370b324cSopenharmony_ci#include "Alloc.h"
9370b324cSopenharmony_ci#include "Bra.h"
10370b324cSopenharmony_ci#include "LzmaDec.h"
11370b324cSopenharmony_ci
12370b324cSopenharmony_ciSRes Lzma86_GetUnpackSize(const Byte *src, SizeT srcLen, UInt64 *unpackSize)
13370b324cSopenharmony_ci{
14370b324cSopenharmony_ci  unsigned i;
15370b324cSopenharmony_ci  if (srcLen < LZMA86_HEADER_SIZE)
16370b324cSopenharmony_ci    return SZ_ERROR_INPUT_EOF;
17370b324cSopenharmony_ci  *unpackSize = 0;
18370b324cSopenharmony_ci  for (i = 0; i < sizeof(UInt64); i++)
19370b324cSopenharmony_ci    *unpackSize += ((UInt64)src[LZMA86_SIZE_OFFSET + i]) << (8 * i);
20370b324cSopenharmony_ci  return SZ_OK;
21370b324cSopenharmony_ci}
22370b324cSopenharmony_ci
23370b324cSopenharmony_ciSRes Lzma86_Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen)
24370b324cSopenharmony_ci{
25370b324cSopenharmony_ci  SRes res;
26370b324cSopenharmony_ci  int useFilter;
27370b324cSopenharmony_ci  SizeT inSizePure;
28370b324cSopenharmony_ci  ELzmaStatus status;
29370b324cSopenharmony_ci
30370b324cSopenharmony_ci  if (*srcLen < LZMA86_HEADER_SIZE)
31370b324cSopenharmony_ci    return SZ_ERROR_INPUT_EOF;
32370b324cSopenharmony_ci
33370b324cSopenharmony_ci  useFilter = src[0];
34370b324cSopenharmony_ci
35370b324cSopenharmony_ci  if (useFilter > 1)
36370b324cSopenharmony_ci  {
37370b324cSopenharmony_ci    *destLen = 0;
38370b324cSopenharmony_ci    return SZ_ERROR_UNSUPPORTED;
39370b324cSopenharmony_ci  }
40370b324cSopenharmony_ci
41370b324cSopenharmony_ci  inSizePure = *srcLen - LZMA86_HEADER_SIZE;
42370b324cSopenharmony_ci  res = LzmaDecode(dest, destLen, src + LZMA86_HEADER_SIZE, &inSizePure,
43370b324cSopenharmony_ci      src + 1, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &g_Alloc);
44370b324cSopenharmony_ci  *srcLen = inSizePure + LZMA86_HEADER_SIZE;
45370b324cSopenharmony_ci  if (res != SZ_OK)
46370b324cSopenharmony_ci    return res;
47370b324cSopenharmony_ci  if (useFilter == 1)
48370b324cSopenharmony_ci  {
49370b324cSopenharmony_ci    UInt32 x86State = Z7_BRANCH_CONV_ST_X86_STATE_INIT_VAL;
50370b324cSopenharmony_ci    z7_BranchConvSt_X86_Dec(dest, *destLen, 0, &x86State);
51370b324cSopenharmony_ci  }
52370b324cSopenharmony_ci  return SZ_OK;
53370b324cSopenharmony_ci}
54