update LZMA code
authorGabor Juhos <juhosg@openwrt.org>
Fri, 14 Dec 2007 20:35:22 +0000 (20:35 +0000)
committerGabor Juhos <juhosg@openwrt.org>
Fri, 14 Dec 2007 20:35:22 +0000 (20:35 +0000)
SVN-Revision: 9756

target/linux/adm5120/image/lzma-loader/src/LzmaDecode.c
target/linux/adm5120/image/lzma-loader/src/LzmaDecode.h
target/linux/adm5120/image/lzma-loader/src/LzmaTypes.h [new file with mode: 0644]
target/linux/adm5120/image/lzma-loader/src/Makefile
target/linux/adm5120/image/lzma-loader/src/decompress.c

index 75a358a9ee82710376a23ae7f0a4782fef244e44..cb8345377ea8a0d14e305073620f3c8d80c66288 100644 (file)
@@ -2,7 +2,7 @@
   LzmaDecode.c
   LZMA Decoder (optimized for Speed version)
   
-  LZMA SDK 4.16 Copyright (c) 1999-2005 Igor Pavlov (2005-03-18)
+  LZMA SDK 4.40 Copyright (c) 1999-2006 Igor Pavlov (2006-05-01)
   http://www.7-zip.org/
 
   LZMA SDK is licensed under two licenses:
 
 #include "LzmaDecode.h"
 
-#ifndef Byte
-#define Byte unsigned char
-#endif
-
 #define kNumTopBits 24
 #define kTopValue ((UInt32)1 << kNumTopBits)
 
@@ -40,7 +36,7 @@
 #ifdef _LZMA_IN_CB
 
 #define RC_TEST { if (Buffer == BufferLim) \
-  { UInt32 size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) return result; \
+  { SizeT size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) return result; \
   BufferLim = Buffer + size; if (size == 0) return LZMA_RESULT_DATA_ERROR; }}
 
 #define RC_INIT Buffer = BufferLim = 0; RC_INIT2
 StopCompilingDueBUG
 #endif
 
-#ifdef _LZMA_OUT_READ
-
-typedef struct _LzmaVarState
-{
-  Byte *Buffer;
-  Byte *BufferLim;
-  UInt32 Range;
-  UInt32 Code;
-  #ifdef _LZMA_IN_CB
-  ILzmaInCallback *InCallback;
-  #endif
-  Byte *Dictionary;
-  UInt32 DictionarySize;
-  UInt32 DictionaryPos;
-  UInt32 GlobalPos;
-  UInt32 Reps[4];
-  int lc;
-  int lp;
-  int pb;
-  int State;
-  int RemainLen;
-  Byte TempDictionary[4];
-} LzmaVarState;
-
-int LzmaDecoderInit(
-    unsigned char *buffer, UInt32 bufferSize,
-    int lc, int lp, int pb,
-    unsigned char *dictionary, UInt32 dictionarySize,
-    #ifdef _LZMA_IN_CB
-    ILzmaInCallback *InCallback
-    #else
-    unsigned char *inStream, UInt32 inSize
-    #endif
-    )
+int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size)
 {
-  Byte *Buffer;
-  Byte *BufferLim;
-  UInt32 Range;
-  UInt32 Code;
-  LzmaVarState *vs = (LzmaVarState *)buffer;
-  CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
-  UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
-  UInt32 i;
-  if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
-    return LZMA_RESULT_NOT_ENOUGH_MEM;
-  vs->Dictionary = dictionary;
-  vs->DictionarySize = dictionarySize;
-  vs->DictionaryPos = 0;
-  vs->GlobalPos = 0;
-  vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
-  vs->lc = lc;
-  vs->lp = lp;
-  vs->pb = pb;
-  vs->State = 0;
-  vs->RemainLen = 0;
-  dictionary[dictionarySize - 1] = 0;
-  for (i = 0; i < numProbs; i++)
-    p[i] = kBitModelTotal >> 1; 
+  unsigned char prop0;
+  if (size < LZMA_PROPERTIES_SIZE)
+    return LZMA_RESULT_DATA_ERROR;
+  prop0 = propsData[0];
+  if (prop0 >= (9 * 5 * 5))
+    return LZMA_RESULT_DATA_ERROR;
+  {
+    for (propsRes->pb = 0; prop0 >= (9 * 5); propsRes->pb++, prop0 -= (9 * 5));
+    for (propsRes->lp = 0; prop0 >= 9; propsRes->lp++, prop0 -= 9);
+    propsRes->lc = prop0;
+    /*
+    unsigned char remainder = (unsigned char)(prop0 / 9);
+    propsRes->lc = prop0 % 9;
+    propsRes->pb = remainder / 5;
+    propsRes->lp = remainder % 5;
+    */
+  }
 
-  #ifdef _LZMA_IN_CB
-  RC_INIT;
-  #else
-  RC_INIT(inStream, inSize);
-  #endif
-  vs->Buffer = Buffer;
-  vs->BufferLim = BufferLim;
-  vs->Range = Range;
-  vs->Code = Code;
-  #ifdef _LZMA_IN_CB
-  vs->InCallback = InCallback;
+  #ifdef _LZMA_OUT_READ
+  {
+    int i;
+    propsRes->DictionarySize = 0;
+    for (i = 0; i < 4; i++)
+      propsRes->DictionarySize += (UInt32)(propsData[1 + i]) << (i * 8);
+    if (propsRes->DictionarySize == 0)
+      propsRes->DictionarySize = 1;
+  }
   #endif
-
   return LZMA_RESULT_OK;
 }
 
-int LzmaDecode(unsigned char *buffer, 
-    unsigned char *outStream, UInt32 outSize,
-    UInt32 *outSizeProcessed)
+#define kLzmaStreamWasFinishedId (-1)
+
+int LzmaDecode(CLzmaDecoderState *vs,
+    #ifdef _LZMA_IN_CB
+    ILzmaInCallback *InCallback,
+    #else
+    const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
+    #endif
+    unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed)
 {
-  LzmaVarState *vs = (LzmaVarState *)buffer;
-  Byte *Buffer = vs->Buffer;
-  Byte *BufferLim = vs->BufferLim;
+  CProb *p = vs->Probs;
+  SizeT nowPos = 0;
+  Byte previousByte = 0;
+  UInt32 posStateMask = (1 << (vs->Properties.pb)) - 1;
+  UInt32 literalPosMask = (1 << (vs->Properties.lp)) - 1;
+  int lc = vs->Properties.lc;
+
+  #ifdef _LZMA_OUT_READ
+  
   UInt32 Range = vs->Range;
   UInt32 Code = vs->Code;
   #ifdef _LZMA_IN_CB
-  ILzmaInCallback *InCallback = vs->InCallback;
+  const Byte *Buffer = vs->Buffer;
+  const Byte *BufferLim = vs->BufferLim;
+  #else
+  const Byte *Buffer = inStream;
+  const Byte *BufferLim = inStream + inSize;
   #endif
-  CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
   int state = vs->State;
-  Byte previousByte;
   UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
-  UInt32 nowPos = 0;
-  UInt32 posStateMask = (1 << (vs->pb)) - 1;
-  UInt32 literalPosMask = (1 << (vs->lp)) - 1;
-  int lc = vs->lc;
   int len = vs->RemainLen;
   UInt32 globalPos = vs->GlobalPos;
+  UInt32 distanceLimit = vs->DistanceLimit;
 
   Byte *dictionary = vs->Dictionary;
-  UInt32 dictionarySize = vs->DictionarySize;
+  UInt32 dictionarySize = vs->Properties.DictionarySize;
   UInt32 dictionaryPos = vs->DictionaryPos;
 
   Byte tempDictionary[4];
+
+  #ifndef _LZMA_IN_CB
+  *inSizeProcessed = 0;
+  #endif
+  *outSizeProcessed = 0;
+  if (len == kLzmaStreamWasFinishedId)
+    return LZMA_RESULT_OK;
+
   if (dictionarySize == 0)
   {
     dictionary = tempDictionary;
@@ -231,12 +204,27 @@ int LzmaDecode(unsigned char *buffer,
     tempDictionary[0] = vs->TempDictionary[0];
   }
 
-  if (len == -1)
+  if (len == kLzmaNeedInitId)
   {
-    *outSizeProcessed = 0;
-    return LZMA_RESULT_OK;
+    {
+      UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
+      UInt32 i;
+      for (i = 0; i < numProbs; i++)
+        p[i] = kBitModelTotal >> 1; 
+      rep0 = rep1 = rep2 = rep3 = 1;
+      state = 0;
+      globalPos = 0;
+      distanceLimit = 0;
+      dictionaryPos = 0;
+      dictionary[dictionarySize - 1] = 0;
+      #ifdef _LZMA_IN_CB
+      RC_INIT;
+      #else
+      RC_INIT(inStream, inSize);
+      #endif
+    }
+    len = 0;
   }
-
   while(len != 0 && nowPos < outSize)
   {
     UInt32 pos = dictionaryPos - rep0;
@@ -251,50 +239,37 @@ int LzmaDecode(unsigned char *buffer,
     previousByte = dictionary[dictionarySize - 1];
   else
     previousByte = dictionary[dictionaryPos - 1];
-#else
 
-int LzmaDecode(
-    Byte *buffer, UInt32 bufferSize,
-    int lc, int lp, int pb,
-    #ifdef _LZMA_IN_CB
-    ILzmaInCallback *InCallback,
-    #else
-    unsigned char *inStream, UInt32 inSize,
-    #endif
-    unsigned char *outStream, UInt32 outSize,
-    UInt32 *outSizeProcessed)
-{
-  UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
-  CProb *p = (CProb *)buffer;
+  #else /* if !_LZMA_OUT_READ */
 
-  UInt32 i;
   int state = 0;
-  Byte previousByte = 0;
   UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
-  UInt32 nowPos = 0;
-  UInt32 posStateMask = (1 << pb) - 1;
-  UInt32 literalPosMask = (1 << lp) - 1;
   int len = 0;
-  
-  Byte *Buffer;
-  Byte *BufferLim;
+  const Byte *Buffer;
+  const Byte *BufferLim;
   UInt32 Range;
   UInt32 Code;
-  
-  if (bufferSize < numProbs * sizeof(CProb))
-    return LZMA_RESULT_NOT_ENOUGH_MEM;
-  for (i = 0; i < numProbs; i++)
-    p[i] = kBitModelTotal >> 1;
-  
 
+  #ifndef _LZMA_IN_CB
+  *inSizeProcessed = 0;
+  #endif
+  *outSizeProcessed = 0;
+
+  {
+    UInt32 i;
+    UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
+    for (i = 0; i < numProbs; i++)
+      p[i] = kBitModelTotal >> 1;
+  }
+  
   #ifdef _LZMA_IN_CB
   RC_INIT;
   #else
   RC_INIT(inStream, inSize);
   #endif
-#endif
 
-  *outSizeProcessed = 0;
+  #endif /* _LZMA_OUT_READ */
+
   while(nowPos < outSize)
   {
     CProb *prob;
@@ -332,7 +307,6 @@ int LzmaDecode(
         #else
         matchByte = outStream[nowPos - rep0];
         #endif
-        // prob += 0x100;
         do
         {
           int bit;
@@ -343,7 +317,6 @@ int LzmaDecode(
           RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
         }
         while (symbol < 0x100);
-        // prob -= 0x100;
       }
       while (symbol < 0x100)
       {
@@ -354,6 +327,9 @@ int LzmaDecode(
 
       outStream[nowPos++] = previousByte;
       #ifdef _LZMA_OUT_READ
+      if (distanceLimit < dictionarySize)
+        distanceLimit++;
+
       dictionary[dictionaryPos] = previousByte;
       if (++dictionaryPos == dictionarySize)
         dictionaryPos = 0;
@@ -364,7 +340,6 @@ int LzmaDecode(
     }
     else             
     {
-      // int isItRep;
       UpdateBit1(prob);
       prob = p + IsRep + state;
       IfBit0(prob)
@@ -390,12 +365,14 @@ int LzmaDecode(
             UInt32 pos;
             #endif
             UpdateBit0(prob);
-            if (nowPos 
-                #ifdef _LZMA_OUT_READ
-                + globalPos
-                #endif
-                == 0)
+            
+            #ifdef _LZMA_OUT_READ
+            if (distanceLimit == 0)
+            #else
+            if (nowPos == 0)
+            #endif
               return LZMA_RESULT_DATA_ERROR;
+            
             state = state < kNumLitStates ? 9 : 11;
             #ifdef _LZMA_OUT_READ
             pos = dictionaryPos - rep0;
@@ -409,6 +386,11 @@ int LzmaDecode(
             previousByte = outStream[nowPos - rep0];
             #endif
             outStream[nowPos++] = previousByte;
+            #ifdef _LZMA_OUT_READ
+            if (distanceLimit < dictionarySize)
+              distanceLimit++;
+            #endif
+
             continue;
           }
           else
@@ -535,18 +517,26 @@ int LzmaDecode(
         if (++rep0 == (UInt32)(0))
         {
           /* it's for stream version */
-          len = -1;
+          len = kLzmaStreamWasFinishedId;
           break;
         }
       }
 
       len += kMatchMinLen;
-      if (rep0 > nowPos 
-        #ifdef _LZMA_OUT_READ
-        + globalPos || rep0 > dictionarySize
-        #endif
-        ) 
+      #ifdef _LZMA_OUT_READ
+      if (rep0 > distanceLimit) 
+      #else
+      if (rep0 > nowPos)
+      #endif
         return LZMA_RESULT_DATA_ERROR;
+
+      #ifdef _LZMA_OUT_READ
+      if (dictionarySize - distanceLimit > (UInt32)len)
+        distanceLimit += len;
+      else
+        distanceLimit = dictionarySize;
+      #endif
+
       do
       {
         #ifdef _LZMA_OUT_READ
@@ -569,12 +559,11 @@ int LzmaDecode(
   RC_NORMALIZE;
 
   #ifdef _LZMA_OUT_READ
-  vs->Buffer = Buffer;
-  vs->BufferLim = BufferLim;
   vs->Range = Range;
   vs->Code = Code;
   vs->DictionaryPos = dictionaryPos;
-  vs->GlobalPos = globalPos + nowPos;
+  vs->GlobalPos = globalPos + (UInt32)nowPos;
+  vs->DistanceLimit = distanceLimit;
   vs->Reps[0] = rep0;
   vs->Reps[1] = rep1;
   vs->Reps[2] = rep2;
@@ -584,6 +573,12 @@ int LzmaDecode(
   vs->TempDictionary[0] = tempDictionary[0];
   #endif
 
+  #ifdef _LZMA_IN_CB
+  vs->Buffer = Buffer;
+  vs->BufferLim = BufferLim;
+  #else
+  *inSizeProcessed = (SizeT)(Buffer - inStream);
+  #endif
   *outSizeProcessed = nowPos;
   return LZMA_RESULT_OK;
 }
index 53677350ae4fa4887d40f80e805ae32e8287b5e8..2870eeb9c9c1150d05a8dafe4c0b9101cb55050a 100644 (file)
@@ -2,7 +2,7 @@
   LzmaDecode.h
   LZMA Decoder interface
 
-  LZMA SDK 4.16 Copyright (c) 1999-2005 Igor Pavlov (2005-03-18)
+  LZMA SDK 4.40 Copyright (c) 1999-2006 Igor Pavlov (2006-05-01)
   http://www.7-zip.org/
 
   LZMA SDK is licensed under two licenses:
@@ -22,6 +22,8 @@
 #ifndef __LZMADECODE_H
 #define __LZMADECODE_H
 
+#include "LzmaTypes.h"
+
 /* #define _LZMA_IN_CB */
 /* Use callback for input data */
 
 /* #define _LZMA_LOC_OPT */
 /* Enable local speed optimizations inside code */
 
-#ifndef UInt32
-#ifdef _LZMA_UINT32_IS_ULONG
-#define UInt32 unsigned long
-#else
-#define UInt32 unsigned int
-#endif
-#endif
-
 #ifdef _LZMA_PROB32
 #define CProb UInt32
 #else
-#define CProb unsigned short
+#define CProb UInt16
 #endif
 
 #define LZMA_RESULT_OK 0
 #define LZMA_RESULT_DATA_ERROR 1
-#define LZMA_RESULT_NOT_ENOUGH_MEM 2
 
 #ifdef _LZMA_IN_CB
 typedef struct _ILzmaInCallback
 {
-  int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
+  int (*Read)(void *object, const unsigned char **buffer, SizeT *bufferSize);
 } ILzmaInCallback;
 #endif
 
 #define LZMA_BASE_SIZE 1846
 #define LZMA_LIT_SIZE 768
 
-/* 
-bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
-bufferSize += 100 in case of _LZMA_OUT_READ
-by default CProb is unsigned short, 
-but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
-*/
+#define LZMA_PROPERTIES_SIZE 5
 
-#ifdef _LZMA_OUT_READ
-int LzmaDecoderInit(
-    unsigned char *buffer, UInt32 bufferSize,
-    int lc, int lp, int pb,
-    unsigned char *dictionary, UInt32 dictionarySize,
-  #ifdef _LZMA_IN_CB
-    ILzmaInCallback *inCallback
-  #else
-    unsigned char *inStream, UInt32 inSize
+typedef struct _CLzmaProperties
+{
+  int lc;
+  int lp;
+  int pb;
+  #ifdef _LZMA_OUT_READ
+  UInt32 DictionarySize;
   #endif
-);
-#endif
+}CLzmaProperties;
+
+int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size);
+
+#define LzmaGetNumProbs(Properties) (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((Properties)->lc + (Properties)->lp)))
+
+#define kLzmaNeedInitId (-2)
+
+typedef struct _CLzmaDecoderState
+{
+  CLzmaProperties Properties;
+  CProb *Probs;
 
-int LzmaDecode(
-    unsigned char *buffer, 
-  #ifndef _LZMA_OUT_READ
-    UInt32 bufferSize,
-    int lc, int lp, int pb,
   #ifdef _LZMA_IN_CB
-    ILzmaInCallback *inCallback,
-  #else
-    unsigned char *inStream, UInt32 inSize,
+  const unsigned char *Buffer;
+  const unsigned char *BufferLim;
   #endif
+
+  #ifdef _LZMA_OUT_READ
+  unsigned char *Dictionary;
+  UInt32 Range;
+  UInt32 Code;
+  UInt32 DictionaryPos;
+  UInt32 GlobalPos;
+  UInt32 DistanceLimit;
+  UInt32 Reps[4];
+  int State;
+  int RemainLen;
+  unsigned char TempDictionary[4];
   #endif
-    unsigned char *outStream, UInt32 outSize,
-    UInt32 *outSizeProcessed);
+} CLzmaDecoderState;
+
+#ifdef _LZMA_OUT_READ
+#define LzmaDecoderInit(vs) { (vs)->RemainLen = kLzmaNeedInitId; }
+#endif
+
+int LzmaDecode(CLzmaDecoderState *vs,
+    #ifdef _LZMA_IN_CB
+    ILzmaInCallback *inCallback,
+    #else
+    const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
+    #endif
+    unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed);
 
 #endif
diff --git a/target/linux/adm5120/image/lzma-loader/src/LzmaTypes.h b/target/linux/adm5120/image/lzma-loader/src/LzmaTypes.h
new file mode 100644 (file)
index 0000000..9c27290
--- /dev/null
@@ -0,0 +1,45 @@
+/* 
+LzmaTypes.h 
+
+Types for LZMA Decoder
+
+This file written and distributed to public domain by Igor Pavlov.
+This file is part of LZMA SDK 4.40 (2006-05-01)
+*/
+
+#ifndef __LZMATYPES_H
+#define __LZMATYPES_H
+
+#ifndef _7ZIP_BYTE_DEFINED
+#define _7ZIP_BYTE_DEFINED
+typedef unsigned char Byte;
+#endif 
+
+#ifndef _7ZIP_UINT16_DEFINED
+#define _7ZIP_UINT16_DEFINED
+typedef unsigned short UInt16;
+#endif 
+
+#ifndef _7ZIP_UINT32_DEFINED
+#define _7ZIP_UINT32_DEFINED
+#ifdef _LZMA_UINT32_IS_ULONG
+typedef unsigned long UInt32;
+#else
+typedef unsigned int UInt32;
+#endif
+#endif 
+
+/* #define _LZMA_NO_SYSTEM_SIZE_T */
+/* You can use it, if you don't want <stddef.h> */
+
+#ifndef _7ZIP_SIZET_DEFINED
+#define _7ZIP_SIZET_DEFINED
+#ifdef _LZMA_NO_SYSTEM_SIZE_T
+typedef UInt32 SizeT;
+#else
+#include <stddef.h>
+typedef size_t SizeT;
+#endif
+#endif
+
+#endif
index a553b7fdef490c774cc6951215602ca6ca872146..aad845ae07512c281a3eb4dcb2f63ab55377e184 100644 (file)
@@ -40,7 +40,7 @@ CFLAGS                = -D__KERNEL__ -Wall -Wstrict-prototypes -Wno-trigraphs -Os \
                  -ffunction-sections -pipe -mlong-calls -fno-common \
                  -ffreestanding \
                  -mabi=32 -march=mips32 -Wa,-32 -Wa,-march=mips32 -Wa,-mips32 -Wa,--trap
-CFLAGS         += -DLOADADDR=$(LOADADDR)
+CFLAGS         += -DLOADADDR=$(LOADADDR) -D_LZMA_PROB32
 
 ASFLAGS                = $(CFLAGS) -D__ASSEMBLY__ -DLZMA_STARTUP_ORG=$(LZMA_STARTUP_ORG)
 
index 9e2a04c994e2d72a1ef37890b6eee99521a6f5b2..4a20a6b4252d373dc0819c4d8ef2ab0e7fe8092f 100644 (file)
@@ -118,12 +118,13 @@ struct env_var {
 extern unsigned char workspace[];
 extern void board_init(void);
 
+static CLzmaDecoderState lzma_state;
+
 typedef void (*kernel_entry)(unsigned long reg_a0, unsigned long reg_a1,
        unsigned long reg_a2, unsigned long reg_a3);
 
-static int decompress_data(unsigned char *buffer, UInt32 bufferSize,
-       int lc, int lp, int pb, unsigned char *outStream, UInt32 outSize,
-       UInt32 *outSizeProcessed);
+static int decompress_data(CLzmaDecoderState *vs, unsigned char *outStream,
+                       UInt32 outSize);
 
 #ifdef CONFIG_PASS_KARGS
 #define ENVV(n,v)      {.name = (n), .value = (v)}
@@ -139,7 +140,7 @@ static void halt(void)
        for(;;);
 }
 
-#if LZMA_WRAPPER
+#if (LZMA_WRAPPER)
 extern unsigned char _lzma_data_start[];
 extern unsigned char _lzma_data_end[];
 
@@ -158,12 +159,12 @@ static void decompress_init(void)
        datalen = _lzma_data_end - _lzma_data_start;
 }
 
-static int decompress_data(unsigned char *buffer, UInt32 bufferSize,
-       int lc, int lp, int pb, unsigned char *outStream, UInt32 outSize,
-       UInt32 *outSizeProcessed)
+static int decompress_data(CLzmaDecoderState *vs, unsigned char *outStream,
+                       SizeT outSize)
 {
-       return LzmaDecode(buffer, bufferSize, lc, lp, pb, data, datalen,
-               outStream, outSize, outSizeProcessed);
+       SizeT ip, op;
+
+       return LzmaDecode(vs, data, datalen, &ip, outStream, outSize, &op);
 }
 #endif /* LZMA_WRAPPER */
 
@@ -181,7 +182,8 @@ static __inline__ unsigned char get_byte(void)
        return *(flash_base+flash_ofs++);
 }
 
-static int lzma_read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
+static int lzma_read_byte(void *object, const unsigned char **buffer,
+                               SizeT *bufferSize)
 {
        unsigned long len;
 
@@ -263,12 +265,17 @@ static void decompress_init(void)
        flash_max = flash_ofs+klen;
 }
 
-static int decompress_data(unsigned char *buffer, UInt32 bufferSize,
-       int lc, int lp, int pb, unsigned char *outStream, UInt32 outSize,
-       UInt32 *outSizeProcessed)
+static int decompress_data(CLzmaDecoderState *vs, unsigned char *outStream,
+                       SizeT outSize)
 {
-       return LzmaDecode(buffer, bufferSize, lc, lp, pb, &lzma_callback,
-               outStream, outSize, outSizeProcessed);
+       SizeT op;
+
+#if 0
+       vs->Buffer = data;
+       vs->BufferLim = datalen;
+#endif
+
+       return LzmaDecode(vs, &lzma_callback, outStream, outSize, &op);
 }
 #endif /* !(LZMA_WRAPPER) */
 
@@ -278,11 +285,9 @@ void decompress_entry(unsigned long reg_a0, unsigned long reg_a1,
        unsigned long icache_size, unsigned long icache_lsize,
        unsigned long dcache_size, unsigned long dcache_lsize)
 {
+       unsigned char props[LZMA_PROPERTIES_SIZE];
        unsigned int i;  /* temp value */
-       unsigned int lc; /* literal context bits */
-       unsigned int lp; /* literal pos state bits */
-       unsigned int pb; /* pos state bits */
-       unsigned int osize; /* uncompressed size */
+       SizeT osize; /* uncompressed size */
        int res;
 
        board_init();
@@ -293,29 +298,31 @@ void decompress_entry(unsigned long reg_a0, unsigned long reg_a1,
        decompress_init();
 
        /* lzma args */
-       i = get_byte();
-       lc = i % 9, i = i / 9;
-       lp = i % 5, pb = i / 5;
+       for (i = 0; i < LZMA_PROPERTIES_SIZE; i++)
+               props[i] = get_byte();
 
        /* skip rest of the LZMA coder property */
-       for (i = 0; i < 4; i++)
-               get_byte();
-
        /* read the lower half of uncompressed size in the header */
-       osize = ((unsigned int)get_byte()) +
-               ((unsigned int)get_byte() << 8) +
-               ((unsigned int)get_byte() << 16) +
-               ((unsigned int)get_byte() << 24);
+       osize = ((SizeT)get_byte()) +
+               ((SizeT)get_byte() << 8) +
+               ((SizeT)get_byte() << 16) +
+               ((SizeT)get_byte() << 24);
 
        /* skip rest of the header (upper half of uncompressed size) */
        for (i = 0; i < 4; i++)
                get_byte();
 
+       res = LzmaDecodeProperties(&lzma_state.Properties, props,
+                                       LZMA_PROPERTIES_SIZE);
+       if (res != LZMA_RESULT_OK) {
+               printf("Incorrect LZMA stream properties!\n");
+               halt();
+       }
+
        printf("decompressing kernel... ");
 
-       /* decompress kernel */
-       res = decompress_data(workspace, ~0, lc, lp, pb,
-               (unsigned char *)LOADADDR, osize, &i);
+       lzma_state.Probs = (CProb *)workspace;
+       res = decompress_data(&lzma_state, (unsigned char *)LOADADDR, osize);
 
        if (res != LZMA_RESULT_OK) {
                printf("failed, ");
@@ -323,9 +330,6 @@ void decompress_entry(unsigned long reg_a0, unsigned long reg_a1,
                case LZMA_RESULT_DATA_ERROR:
                        printf("data error!\n");
                        break;
-               case LZMA_RESULT_NOT_ENOUGH_MEM:
-                       printf("not enough memory!\n");
-                       break;
                default:
                        printf("unknown error %d!\n", res);
                }