1 From d37394e9ca1b85407408719feb2468c5affefa4a Mon Sep 17 00:00:00 2001
2 From: Gabor Juhos <juhosg at openwrt.org>
3 Date: Fri, 9 May 2008 17:26:14 +0200
4 Subject: [PATCH] add LZMA decompression support
7 common/cmd_bootm.c | 18 ++
9 include/LzmaWrapper.h | 36 +++
11 lib_generic/LzmaDecode.c | 602 +++++++++++++++++++++++++++++++++++++++++++++
12 lib_generic/LzmaDecode.h | 113 +++++++++
13 lib_generic/LzmaTypes.h | 45 ++++
14 lib_generic/LzmaWrapper.c | 197 +++++++++++++++
15 lib_generic/Makefile | 2 +
16 9 files changed, 1015 insertions(+), 0 deletions(-)
17 create mode 100644 include/LzmaWrapper.h
18 create mode 100644 lib_generic/LzmaDecode.c
19 create mode 100644 lib_generic/LzmaDecode.h
20 create mode 100644 lib_generic/LzmaTypes.h
21 create mode 100644 lib_generic/LzmaWrapper.c
23 diff --git a/common/cmd_bootm.c b/common/cmd_bootm.c
24 index 0d67132..6071817 100644
25 --- a/common/cmd_bootm.c
26 +++ b/common/cmd_bootm.c
31 +#include <LzmaWrapper.h>
32 #include <environment.h>
34 #include <asm/byteorder.h>
35 @@ -237,6 +238,7 @@ int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
39 +#ifndef CONFIG_NO_GZIP
41 printf (" Uncompressing %s ... ", type_name);
42 if (gunzip ((void *)load_start, unc_len,
43 @@ -249,6 +251,7 @@ int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
45 load_end = load_start + os_len;
50 printf (" Uncompressing %s ... ", type_name);
51 @@ -270,6 +273,21 @@ int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
52 load_end = load_start + unc_len;
54 #endif /* CONFIG_BZIP2 */
57 + printf (" Uncompressing %s ... ", type_name);
58 + int i = lzma_inflate ((unsigned char *)os_data, os_len,
59 + (unsigned char *)load_start, &unc_len);
60 + if (i != LZMA_RESULT_OK) {
61 + printf ("LZMA: uncompress or overwrite error %d "
62 + "- must RESET board to recover\n", i);
63 + show_boot_progress (-6);
64 + do_reset (cmdtp, flag, argc, argv);
67 + load_end = load_start + unc_len;
69 +#endif /* CONFIG_LZMA */
73 diff --git a/common/image.c b/common/image.c
74 index 67e594d..9b42ae9 100644
77 @@ -148,6 +148,7 @@ static table_entry_t uimage_comp[] = {
78 { IH_COMP_NONE, "none", "uncompressed", },
79 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
80 { IH_COMP_GZIP, "gzip", "gzip compressed", },
81 + { IH_COMP_LZMA, "lzma", "lzma compressed", },
85 diff --git a/include/LzmaWrapper.h b/include/LzmaWrapper.h
87 index 0000000..2f9a3ff
89 +++ b/include/LzmaWrapper.h
91 +/******************************************************************************
93 +** FILE NAME : LzmaWrapper.h
94 +** PROJECT : bootloader
99 +** DESCRIPTION : LZMA decoder support for U-boot 1.1.5
100 +** COPYRIGHT : Copyright (c) 2006
101 +** Infineon Technologies AG
102 +** Am Campeon 1-12, 85579 Neubiberg, Germany
104 +** This program is free software; you can redistribute it and/or modify
105 +** it under the terms of the GNU General Public License as published by
106 +** the Free Software Foundation; either version 2 of the License, or
107 +** (at your option) any later version.
110 +** $Date $Author $Comment
111 +** 2 Nov 2006 Lin Mars init version which derived from LzmaTest.c from
113 +*******************************************************************************/
114 +#ifndef __LZMA_WRAPPER_H__
115 +#define __LZMA_WRAPPER_H__
117 +#ifndef LZMA_RESULT_OK
118 +#define LZMA_RESULT_OK 0
120 +#ifndef LZMA_RESULT_DATA_ERROR
121 +#define LZMA_RESULT_DATA_ERROR 1
124 +extern int lzma_inflate(unsigned char *source, int s_len, unsigned char *dest, int *d_len);
126 +#endif /*__LZMA_WRAPPER_H__*/
127 diff --git a/include/image.h b/include/image.h
128 index 664e51e..6de51c4 100644
129 --- a/include/image.h
130 +++ b/include/image.h
132 #define IH_COMP_NONE 0 /* No Compression Used */
133 #define IH_COMP_GZIP 1 /* gzip Compression Used */
134 #define IH_COMP_BZIP2 2 /* bzip2 Compression Used */
135 +#define IH_COMP_LZMA 3 /* lzma Compression used */
137 #define IH_MAGIC 0x27051956 /* Image Magic Number */
138 #define IH_NMLEN 32 /* Image Name Length */
139 diff --git a/lib_generic/LzmaDecode.c b/lib_generic/LzmaDecode.c
141 index 0000000..d876e8b
143 +++ b/lib_generic/LzmaDecode.c
147 + LZMA Decoder (optimized for Speed version)
149 + LZMA SDK 4.40 Copyright (c) 1999-2006 Igor Pavlov (2006-05-01)
150 + http://www.7-zip.org/
152 + LZMA SDK is licensed under two licenses:
153 + 1) GNU Lesser General Public License (GNU LGPL)
154 + 2) Common Public License (CPL)
155 + It means that you can select one of these two licenses and
156 + follow rules of that license.
159 + Igor Pavlov, as the author of this Code, expressly permits you to
160 + statically or dynamically link your Code (or bind by name) to the
161 + interfaces of this file without subjecting your linked Code to the
162 + terms of the CPL or GNU LGPL. Any modifications or additions
163 + to this file, however, are subject to the LGPL or CPL terms.
170 +#include "LzmaDecode.h"
172 +#define kNumTopBits 24
173 +#define kTopValue ((UInt32)1 << kNumTopBits)
175 +#define kNumBitModelTotalBits 11
176 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
177 +#define kNumMoveBits 5
179 +#define RC_READ_BYTE (*Buffer++)
181 +#define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \
182 + { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
186 +#define RC_TEST { if (Buffer == BufferLim) \
187 + { SizeT size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) { printf("ERROR, %s, %d\n", __FILE__, __LINE__); return result; } \
188 + BufferLim = Buffer + size; if (size == 0) { printf("ERROR, %s, %d\n", __FILE__, __LINE__); return LZMA_RESULT_DATA_ERROR; } }}
190 +#define RC_INIT Buffer = BufferLim = 0; RC_INIT2
194 +#define RC_TEST { if (Buffer == BufferLim) { printf("ERROR, %s, %d\n", __FILE__, __LINE__); return LZMA_RESULT_DATA_ERROR; } }
196 +#define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2
200 +#define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
202 +#define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
203 +#define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
204 +#define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
206 +#define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
207 + { UpdateBit0(p); mi <<= 1; A0; } else \
208 + { UpdateBit1(p); mi = (mi + mi) + 1; A1; }
210 +#define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)
212 +#define RangeDecoderBitTreeDecode(probs, numLevels, res) \
213 + { int i = numLevels; res = 1; \
214 + do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
215 + res -= (1 << numLevels); }
218 +#define kNumPosBitsMax 4
219 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
221 +#define kLenNumLowBits 3
222 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
223 +#define kLenNumMidBits 3
224 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
225 +#define kLenNumHighBits 8
226 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
229 +#define LenChoice2 (LenChoice + 1)
230 +#define LenLow (LenChoice2 + 1)
231 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
232 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
233 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
236 +#define kNumStates 12
237 +#define kNumLitStates 7
239 +#define kStartPosModelIndex 4
240 +#define kEndPosModelIndex 14
241 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
243 +#define kNumPosSlotBits 6
244 +#define kNumLenToPosStates 4
246 +#define kNumAlignBits 4
247 +#define kAlignTableSize (1 << kNumAlignBits)
249 +#define kMatchMinLen 2
252 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
253 +#define IsRepG0 (IsRep + kNumStates)
254 +#define IsRepG1 (IsRepG0 + kNumStates)
255 +#define IsRepG2 (IsRepG1 + kNumStates)
256 +#define IsRep0Long (IsRepG2 + kNumStates)
257 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
258 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
259 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
260 +#define LenCoder (Align + kAlignTableSize)
261 +#define RepLenCoder (LenCoder + kNumLenProbs)
262 +#define Literal (RepLenCoder + kNumLenProbs)
264 +#if Literal != LZMA_BASE_SIZE
268 +int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size)
270 + unsigned char prop0;
271 + if (size < LZMA_PROPERTIES_SIZE)
273 + printf("ERROR: %s, %d\n", __FILE__, __LINE__);
274 + return LZMA_RESULT_DATA_ERROR;
276 + prop0 = propsData[0];
277 + if (prop0 >= (9 * 5 * 5))
279 + printf("ERROR: %s, %d\n", __FILE__, __LINE__);
280 + return LZMA_RESULT_DATA_ERROR;
283 + for (propsRes->pb = 0; prop0 >= (9 * 5); propsRes->pb++, prop0 -= (9 * 5));
284 + for (propsRes->lp = 0; prop0 >= 9; propsRes->lp++, prop0 -= 9);
285 + propsRes->lc = prop0;
287 + unsigned char remainder = (unsigned char)(prop0 / 9);
288 + propsRes->lc = prop0 % 9;
289 + propsRes->pb = remainder / 5;
290 + propsRes->lp = remainder % 5;
294 + #ifdef _LZMA_OUT_READ
297 + propsRes->DictionarySize = 0;
298 + for (i = 0; i < 4; i++)
299 + propsRes->DictionarySize += (UInt32)(propsData[1 + i]) << (i * 8);
300 + if (propsRes->DictionarySize == 0)
301 + propsRes->DictionarySize = 1;
304 + return LZMA_RESULT_OK;
307 +#define kLzmaStreamWasFinishedId (-1)
309 +int LzmaDecode(CLzmaDecoderState *vs,
311 + ILzmaInCallback *InCallback,
313 + const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
315 + unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed)
317 + CProb *p = vs->Probs;
319 + Byte previousByte = 0;
320 + UInt32 posStateMask = (1 << (vs->Properties.pb)) - 1;
321 + UInt32 literalPosMask = (1 << (vs->Properties.lp)) - 1;
322 + int lc = vs->Properties.lc;
324 + #ifdef _LZMA_OUT_READ
326 + UInt32 Range = vs->Range;
327 + UInt32 Code = vs->Code;
329 + const Byte *Buffer = vs->Buffer;
330 + const Byte *BufferLim = vs->BufferLim;
332 + const Byte *Buffer = inStream;
333 + const Byte *BufferLim = inStream + inSize;
335 + int state = vs->State;
336 + UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
337 + int len = vs->RemainLen;
338 + UInt32 globalPos = vs->GlobalPos;
339 + UInt32 distanceLimit = vs->DistanceLimit;
341 + Byte *dictionary = vs->Dictionary;
342 + UInt32 dictionarySize = vs->Properties.DictionarySize;
343 + UInt32 dictionaryPos = vs->DictionaryPos;
345 + Byte tempDictionary[4];
347 + #ifndef _LZMA_IN_CB
348 + *inSizeProcessed = 0;
350 + *outSizeProcessed = 0;
351 + if (len == kLzmaStreamWasFinishedId)
352 + return LZMA_RESULT_OK;
354 + if (dictionarySize == 0)
356 + dictionary = tempDictionary;
357 + dictionarySize = 1;
358 + tempDictionary[0] = vs->TempDictionary[0];
361 + if (len == kLzmaNeedInitId)
364 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
366 + for (i = 0; i < numProbs; i++)
367 + p[i] = kBitModelTotal >> 1;
368 + rep0 = rep1 = rep2 = rep3 = 1;
373 + dictionary[dictionarySize - 1] = 0;
377 + RC_INIT(inStream, inSize);
382 + while(len != 0 && nowPos < outSize)
384 + UInt32 pos = dictionaryPos - rep0;
385 + if (pos >= dictionarySize)
386 + pos += dictionarySize;
387 + outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
388 + if (++dictionaryPos == dictionarySize)
392 + if (dictionaryPos == 0)
393 + previousByte = dictionary[dictionarySize - 1];
395 + previousByte = dictionary[dictionaryPos - 1];
397 + #else /* if !_LZMA_OUT_READ */
400 + UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
402 + const Byte *Buffer;
403 + const Byte *BufferLim;
407 + #ifndef _LZMA_IN_CB
408 + *inSizeProcessed = 0;
410 + *outSizeProcessed = 0;
414 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
415 + for (i = 0; i < numProbs; i++)
416 + p[i] = kBitModelTotal >> 1;
422 + RC_INIT(inStream, inSize);
425 + #endif /* _LZMA_OUT_READ */
427 + while(nowPos < outSize)
431 + int posState = (int)(
433 + #ifdef _LZMA_OUT_READ
439 + prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
444 + prob = p + Literal + (LZMA_LIT_SIZE *
447 + #ifdef _LZMA_OUT_READ
451 + & literalPosMask) << lc) + (previousByte >> (8 - lc))));
453 + if (state >= kNumLitStates)
456 + #ifdef _LZMA_OUT_READ
457 + UInt32 pos = dictionaryPos - rep0;
458 + if (pos >= dictionarySize)
459 + pos += dictionarySize;
460 + matchByte = dictionary[pos];
462 + matchByte = outStream[nowPos - rep0];
469 + bit = (matchByte & 0x100);
470 + probLit = prob + 0x100 + bit + symbol;
471 + RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
473 + while (symbol < 0x100);
475 + while (symbol < 0x100)
477 + CProb *probLit = prob + symbol;
478 + RC_GET_BIT(probLit, symbol)
480 + previousByte = (Byte)symbol;
482 + outStream[nowPos++] = previousByte;
483 + #ifdef _LZMA_OUT_READ
484 + if (distanceLimit < dictionarySize)
487 + dictionary[dictionaryPos] = previousByte;
488 + if (++dictionaryPos == dictionarySize)
491 + if (state < 4) state = 0;
492 + else if (state < 10) state -= 3;
498 + prob = p + IsRep + state;
505 + state = state < kNumLitStates ? 0 : 3;
506 + prob = p + LenCoder;
511 + prob = p + IsRepG0 + state;
515 + prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
518 + #ifdef _LZMA_OUT_READ
523 + #ifdef _LZMA_OUT_READ
524 + if (distanceLimit == 0)
529 + printf("ERROR: %s, %d\n", __FILE__, __LINE__);
530 + return LZMA_RESULT_DATA_ERROR;
533 + state = state < kNumLitStates ? 9 : 11;
534 + #ifdef _LZMA_OUT_READ
535 + pos = dictionaryPos - rep0;
536 + if (pos >= dictionarySize)
537 + pos += dictionarySize;
538 + previousByte = dictionary[pos];
539 + dictionary[dictionaryPos] = previousByte;
540 + if (++dictionaryPos == dictionarySize)
543 + previousByte = outStream[nowPos - rep0];
545 + outStream[nowPos++] = previousByte;
546 + #ifdef _LZMA_OUT_READ
547 + if (distanceLimit < dictionarySize)
562 + prob = p + IsRepG1 + state;
571 + prob = p + IsRepG2 + state;
588 + state = state < kNumLitStates ? 8 : 11;
589 + prob = p + RepLenCoder;
592 + int numBits, offset;
593 + CProb *probLen = prob + LenChoice;
596 + UpdateBit0(probLen);
597 + probLen = prob + LenLow + (posState << kLenNumLowBits);
599 + numBits = kLenNumLowBits;
603 + UpdateBit1(probLen);
604 + probLen = prob + LenChoice2;
607 + UpdateBit0(probLen);
608 + probLen = prob + LenMid + (posState << kLenNumMidBits);
609 + offset = kLenNumLowSymbols;
610 + numBits = kLenNumMidBits;
614 + UpdateBit1(probLen);
615 + probLen = prob + LenHigh;
616 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
617 + numBits = kLenNumHighBits;
620 + RangeDecoderBitTreeDecode(probLen, numBits, len);
627 + state += kNumLitStates;
628 + prob = p + PosSlot +
629 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
631 + RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
632 + if (posSlot >= kStartPosModelIndex)
634 + int numDirectBits = ((posSlot >> 1) - 1);
635 + rep0 = (2 | ((UInt32)posSlot & 1));
636 + if (posSlot < kEndPosModelIndex)
638 + rep0 <<= numDirectBits;
639 + prob = p + SpecPos + rep0 - posSlot - 1;
643 + numDirectBits -= kNumAlignBits;
655 + while (--numDirectBits != 0);
657 + rep0 <<= kNumAlignBits;
658 + numDirectBits = kNumAlignBits;
665 + CProb *prob3 = prob + mi;
666 + RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
669 + while(--numDirectBits != 0);
674 + if (++rep0 == (UInt32)(0))
676 + /* it's for stream version */
677 + len = kLzmaStreamWasFinishedId;
682 + len += kMatchMinLen;
683 + #ifdef _LZMA_OUT_READ
684 + if (rep0 > distanceLimit)
689 + printf("ERROR: %s, %d\n", __FILE__, __LINE__);
690 + return LZMA_RESULT_DATA_ERROR;
693 + #ifdef _LZMA_OUT_READ
694 + if (dictionarySize - distanceLimit > (UInt32)len)
695 + distanceLimit += len;
697 + distanceLimit = dictionarySize;
702 + #ifdef _LZMA_OUT_READ
703 + UInt32 pos = dictionaryPos - rep0;
704 + if (pos >= dictionarySize)
705 + pos += dictionarySize;
706 + previousByte = dictionary[pos];
707 + dictionary[dictionaryPos] = previousByte;
708 + if (++dictionaryPos == dictionarySize)
711 + previousByte = outStream[nowPos - rep0];
714 + outStream[nowPos++] = previousByte;
716 + while(len != 0 && nowPos < outSize);
721 + #ifdef _LZMA_OUT_READ
724 + vs->DictionaryPos = dictionaryPos;
725 + vs->GlobalPos = globalPos + (UInt32)nowPos;
726 + vs->DistanceLimit = distanceLimit;
727 + vs->Reps[0] = rep0;
728 + vs->Reps[1] = rep1;
729 + vs->Reps[2] = rep2;
730 + vs->Reps[3] = rep3;
732 + vs->RemainLen = len;
733 + vs->TempDictionary[0] = tempDictionary[0];
737 + vs->Buffer = Buffer;
738 + vs->BufferLim = BufferLim;
740 + *inSizeProcessed = (SizeT)(Buffer - inStream);
742 + *outSizeProcessed = nowPos;
743 + return LZMA_RESULT_OK;
746 +#endif /* CONFIG_LZMA */
747 diff --git a/lib_generic/LzmaDecode.h b/lib_generic/LzmaDecode.h
749 index 0000000..2870eeb
751 +++ b/lib_generic/LzmaDecode.h
755 + LZMA Decoder interface
757 + LZMA SDK 4.40 Copyright (c) 1999-2006 Igor Pavlov (2006-05-01)
758 + http://www.7-zip.org/
760 + LZMA SDK is licensed under two licenses:
761 + 1) GNU Lesser General Public License (GNU LGPL)
762 + 2) Common Public License (CPL)
763 + It means that you can select one of these two licenses and
764 + follow rules of that license.
767 + Igor Pavlov, as the author of this code, expressly permits you to
768 + statically or dynamically link your code (or bind by name) to the
769 + interfaces of this file without subjecting your linked code to the
770 + terms of the CPL or GNU LGPL. Any modifications or additions
771 + to this file, however, are subject to the LGPL or CPL terms.
774 +#ifndef __LZMADECODE_H
775 +#define __LZMADECODE_H
777 +#include "LzmaTypes.h"
779 +/* #define _LZMA_IN_CB */
780 +/* Use callback for input data */
782 +/* #define _LZMA_OUT_READ */
783 +/* Use read function for output data */
785 +/* #define _LZMA_PROB32 */
786 +/* It can increase speed on some 32-bit CPUs,
787 + but memory usage will be doubled in that case */
789 +/* #define _LZMA_LOC_OPT */
790 +/* Enable local speed optimizations inside code */
793 +#define CProb UInt32
795 +#define CProb UInt16
798 +#define LZMA_RESULT_OK 0
799 +#define LZMA_RESULT_DATA_ERROR 1
802 +typedef struct _ILzmaInCallback
804 + int (*Read)(void *object, const unsigned char **buffer, SizeT *bufferSize);
808 +#define LZMA_BASE_SIZE 1846
809 +#define LZMA_LIT_SIZE 768
811 +#define LZMA_PROPERTIES_SIZE 5
813 +typedef struct _CLzmaProperties
818 + #ifdef _LZMA_OUT_READ
819 + UInt32 DictionarySize;
823 +int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size);
825 +#define LzmaGetNumProbs(Properties) (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((Properties)->lc + (Properties)->lp)))
827 +#define kLzmaNeedInitId (-2)
829 +typedef struct _CLzmaDecoderState
831 + CLzmaProperties Properties;
835 + const unsigned char *Buffer;
836 + const unsigned char *BufferLim;
839 + #ifdef _LZMA_OUT_READ
840 + unsigned char *Dictionary;
843 + UInt32 DictionaryPos;
845 + UInt32 DistanceLimit;
849 + unsigned char TempDictionary[4];
851 +} CLzmaDecoderState;
853 +#ifdef _LZMA_OUT_READ
854 +#define LzmaDecoderInit(vs) { (vs)->RemainLen = kLzmaNeedInitId; }
857 +int LzmaDecode(CLzmaDecoderState *vs,
859 + ILzmaInCallback *inCallback,
861 + const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
863 + unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed);
866 diff --git a/lib_generic/LzmaTypes.h b/lib_generic/LzmaTypes.h
868 index 0000000..288c5e4
870 +++ b/lib_generic/LzmaTypes.h
875 +Types for LZMA Decoder
877 +This file written and distributed to public domain by Igor Pavlov.
878 +This file is part of LZMA SDK 4.40 (2006-05-01)
881 +#ifndef __LZMATYPES_H
882 +#define __LZMATYPES_H
884 +#ifndef _7ZIP_BYTE_DEFINED
885 +#define _7ZIP_BYTE_DEFINED
886 +typedef unsigned char Byte;
889 +#ifndef _7ZIP_UINT16_DEFINED
890 +#define _7ZIP_UINT16_DEFINED
891 +typedef unsigned short UInt16;
894 +#ifndef _7ZIP_UINT32_DEFINED
895 +#define _7ZIP_UINT32_DEFINED
896 +#ifdef _LZMA_UINT32_IS_ULONG
897 +typedef unsigned long UInt32;
899 +typedef unsigned int UInt32;
903 +/* #define _LZMA_SYSTEM_SIZE_T */
904 +/* Use system's size_t. You can use it to enable 64-bit sizes supporting */
906 +#ifndef _7ZIP_SIZET_DEFINED
907 +#define _7ZIP_SIZET_DEFINED
908 +#ifdef _LZMA_SYSTEM_SIZE_T
910 +typedef size_t SizeT;
912 +typedef UInt32 SizeT;
917 diff --git a/lib_generic/LzmaWrapper.c b/lib_generic/LzmaWrapper.c
919 index 0000000..6c3d702
921 +++ b/lib_generic/LzmaWrapper.c
923 +/******************************************************************************
925 +** FILE NAME : LzmaWrapper.c
926 +** PROJECT : bootloader
929 +** DATE : 2 Nov 2006
930 +** AUTHOR : Lin Mars
931 +** DESCRIPTION : LZMA decoder support for U-boot 1.1.5
932 +** COPYRIGHT : Copyright (c) 2006
933 +** Infineon Technologies AG
934 +** Am Campeon 1-12, 85579 Neubiberg, Germany
936 +** This program is free software; you can redistribute it and/or modify
937 +** it under the terms of the GNU General Public License as published by
938 +** the Free Software Foundation; either version 2 of the License, or
939 +** (at your option) any later version.
942 +** $Date $Author $Comment
943 +** 2 Nov 2006 Lin Mars init version which derived from LzmaTest.c from
945 +*******************************************************************************/
946 +#define LZMA_NO_STDIO
947 +#ifndef LZMA_NO_STDIO
955 +#include <linux/types.h>
956 +#include <linux/string.h>
957 +#include <linux/ctype.h>
962 +#include "LzmaDecode.h"
963 +#include "LzmaWrapper.h"
965 +static const char *kCantReadMessage = "Can not read from source buffer";
966 +static const char *kCantAllocateMessage = "Not enough buffer for decompression";
968 +static size_t rpos=0, dpos=0;
970 +static int MyReadFileAndCheck(unsigned char *src, void *dest, size_t size)
974 + memcpy(dest, src + rpos, size);
979 +int lzma_inflate(unsigned char *source, int s_len, unsigned char *dest, int *d_len)
981 + /* We use two 32-bit integers to construct 64-bit integer for file size.
982 + You can remove outSizeHigh, if you don't need >= 4GB supporting,
983 + or you can use UInt64 outSize, if your compiler supports 64-bit integers*/
984 + UInt32 outSize = 0;
985 + UInt32 outSizeHigh = 0;
987 + unsigned char *outStream;
990 + /* waitEOS = 1, if there is no uncompressed size in headers,
991 + so decoder will wait EOS (End of Stream Marker) in compressed stream */
993 + SizeT compressedSize;
994 + unsigned char *inStream;
996 + CLzmaDecoderState state; /* it's about 24-80 bytes structure, if int is 32-bit */
997 + unsigned char properties[LZMA_PROPERTIES_SIZE];
1001 + if (sizeof(UInt32) < 4)
1003 + printf("LZMA decoder needs correct UInt32\n");
1004 + return LZMA_RESULT_DATA_ERROR;
1008 + long length=s_len;
1009 + if ((long)(SizeT)length != length)
1011 + printf("Too big compressed stream\n");
1012 + return LZMA_RESULT_DATA_ERROR;
1014 + compressedSize = (SizeT)(length - (LZMA_PROPERTIES_SIZE + 8));
1017 + /* Read LZMA properties for compressed stream */
1019 + if (!MyReadFileAndCheck(source, properties, sizeof(properties)))
1021 + printf("%s\n", kCantReadMessage);
1022 + return LZMA_RESULT_DATA_ERROR;
1025 + /* Read uncompressed size */
1028 + for (i = 0; i < 8; i++)
1031 + if (!MyReadFileAndCheck(source, &b, 1))
1033 + printf("%s\n", kCantReadMessage);
1034 + return LZMA_RESULT_DATA_ERROR;
1039 + outSize += (UInt32)(b) << (i * 8);
1041 + outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
1046 + printf("Stream with EOS marker is not supported");
1047 + return LZMA_RESULT_DATA_ERROR;
1049 + outSizeFull = (SizeT)outSize;
1050 + if (sizeof(SizeT) >= 8)
1051 + outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
1052 + else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize)
1054 + printf("Too big uncompressed stream");
1055 + return LZMA_RESULT_DATA_ERROR;
1059 + /* Decode LZMA properties and allocate memory */
1060 + if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
1062 + printf("Incorrect stream properties");
1063 + return LZMA_RESULT_DATA_ERROR;
1065 + state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
1067 + if (outSizeFull == 0)
1071 + if (outSizeFull > d_len)
1077 + if (compressedSize == 0)
1081 + if ((compressedSize+rpos) > s_len )
1084 + inStream = source + rpos;
1087 + if (state.Probs == 0
1088 + || (outStream == 0 && outSizeFull != 0)
1089 + || (inStream == 0 && compressedSize != 0)
1092 + free(state.Probs);
1093 + printf("%s\n", kCantAllocateMessage);
1094 + return LZMA_RESULT_DATA_ERROR;
1099 + SizeT inProcessed;
1100 + SizeT outProcessed;
1101 + res = LzmaDecode(&state,
1102 + inStream, compressedSize, &inProcessed,
1103 + outStream, outSizeFull, &outProcessed);
1106 + printf("\nDecoding error = %d\n", res);
1111 + *d_len = outProcessed;
1115 + free(state.Probs);
1119 +#endif /* CONFIG_LZMA */
1120 diff --git a/lib_generic/Makefile b/lib_generic/Makefile
1121 index abee19a..449e994 100644
1122 --- a/lib_generic/Makefile
1123 +++ b/lib_generic/Makefile
1124 @@ -41,6 +41,8 @@ COBJS-y += sha1.o
1126 COBJS-y += vsprintf.o
1128 +COBJS-y += LzmaDecode.o
1129 +COBJS-y += LzmaWrapper.o
1132 SRCS := $(COBJS:.o=.c)