Subversion Repositories Games.Chess Giants

Rev

Details | Last modification | View Log | RSS feed

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