1/* LzmaRamDecode.c */
2
3#include "LzmaRamDecode.h"
4#ifdef _SZ_ONE_DIRECTORY
5#include "LzmaDecode.h"
6#include "BranchX86.h"
7#else
8#include "../../../../C/Compress/Lzma/LzmaDecode.h"
9#include "../../../../C/Compress/Branch/BranchX86.h"
10#endif
11
12#define LZMA_PROPS_SIZE 14
13#define LZMA_SIZE_OFFSET 6
14
15int LzmaRamGetUncompressedSize(
16    const unsigned char *inBuffer,
17    size_t inSize,
18    size_t *outSize)
19{
20  unsigned int i;
21  if (inSize < LZMA_PROPS_SIZE)
22    return 1;
23  *outSize = 0;
24  for(i = 0; i < sizeof(size_t); i++)
25    *outSize += ((size_t)inBuffer[LZMA_SIZE_OFFSET + i]) << (8 * i);
26  for(; i < 8; i++)
27    if (inBuffer[LZMA_SIZE_OFFSET + i] != 0)
28      return 1;
29  return 0;
30}
31
32#define SZE_DATA_ERROR (1)
33#define SZE_OUTOFMEMORY (2)
34
35int LzmaRamDecompress(
36    const unsigned char *inBuffer,
37    size_t inSize,
38    unsigned char *outBuffer,
39    size_t outSize,
40    size_t *outSizeProcessed,
41    void * (*allocFunc)(size_t size),
42    void (*freeFunc)(void *))
43{
44  CLzmaDecoderState state;  /* it's about 24 bytes structure, if int is 32-bit */
45  int result;
46  SizeT outSizeProcessedLoc;
47  SizeT inProcessed;
48  int useFilter;
49
50  if (inSize < LZMA_PROPS_SIZE)
51    return 1;
52  useFilter = inBuffer[0];
53
54  *outSizeProcessed = 0;
55  if (useFilter > 1)
56    return 1;
57
58  if (LzmaDecodeProperties(&state.Properties, inBuffer + 1, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
59    return 1;
60  state.Probs = (CProb *)allocFunc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
61  if (state.Probs == 0)
62    return SZE_OUTOFMEMORY;
63
64  result = LzmaDecode(&state,
65    inBuffer + LZMA_PROPS_SIZE, (SizeT)inSize - LZMA_PROPS_SIZE, &inProcessed,
66    outBuffer, (SizeT)outSize, &outSizeProcessedLoc);
67  freeFunc(state.Probs);
68  if (result != LZMA_RESULT_OK)
69    return 1;
70  *outSizeProcessed = (size_t)outSizeProcessedLoc;
71  if (useFilter == 1)
72  {
73    UInt32 x86State;
74    x86_Convert_Init(x86State);
75    x86_Convert(outBuffer, (SizeT)outSizeProcessedLoc, 0, &x86State, 0);
76  }
77  return 0;
78}
79