1 /******************************************************************************
3 ** FILE NAME : LzmaWrapper.c
4 ** PROJECT : bootloader
9 ** DESCRIPTION : LZMA decoder support for U-boot 1.1.5
10 ** COPYRIGHT : Copyright (c) 2006
11 ** Infineon Technologies AG
12 ** Am Campeon 1-12, 85579 Neubiberg, Germany
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License as published by
16 ** the Free Software Foundation; either version 2 of the License, or
17 ** (at your option) any later version.
20 ** $Date $Author $Comment
21 ** 2 Nov 2006 Lin Mars init version which derived from LzmaTest.c from
23 *******************************************************************************/
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
40 #include "LzmaDecode.h"
41 #include "LzmaWrapper.h"
43 static const char *kCantReadMessage = "Can not read from source buffer";
44 static const char *kCantAllocateMessage = "Not enough buffer for decompression";
46 static size_t rpos=0, dpos=0;
48 static int MyReadFileAndCheck(unsigned char *src, void *dest, size_t size)
52 memcpy(dest, src + rpos, size);
57 int lzma_inflate(unsigned char *source, int s_len, unsigned char *dest, int *d_len)
59 /* We use two 32-bit integers to construct 64-bit integer for file size.
60 You can remove outSizeHigh, if you don't need >= 4GB supporting,
61 or you can use UInt64 outSize, if your compiler supports 64-bit integers*/
63 UInt32 outSizeHigh = 0;
65 unsigned char *outStream;
68 /* waitEOS = 1, if there is no uncompressed size in headers,
69 so decoder will wait EOS (End of Stream Marker) in compressed stream */
72 unsigned char *inStream;
74 CLzmaDecoderState state; /* it's about 24-80 bytes structure, if int is 32-bit */
75 unsigned char properties[LZMA_PROPERTIES_SIZE];
79 if (sizeof(UInt32) < 4)
81 printf("LZMA decoder needs correct UInt32\n");
82 return LZMA_RESULT_DATA_ERROR;
87 if ((long)(SizeT)length != length)
89 printf("Too big compressed stream\n");
90 return LZMA_RESULT_DATA_ERROR;
92 compressedSize = (SizeT)(length - (LZMA_PROPERTIES_SIZE + 8));
95 /* Read LZMA properties for compressed stream */
97 if (!MyReadFileAndCheck(source, properties, sizeof(properties)))
99 printf("%s\n", kCantReadMessage);
100 return LZMA_RESULT_DATA_ERROR;
103 /* Read uncompressed size */
106 for (i = 0; i < 8; i++)
109 if (!MyReadFileAndCheck(source, &b, 1))
111 printf("%s\n", kCantReadMessage);
112 return LZMA_RESULT_DATA_ERROR;
117 outSize += (UInt32)(b) << (i * 8);
119 outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
124 printf("Stream with EOS marker is not supported");
125 return LZMA_RESULT_DATA_ERROR;
127 outSizeFull = (SizeT)outSize;
128 if (sizeof(SizeT) >= 8)
129 outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
130 else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize)
132 printf("Too big uncompressed stream");
133 return LZMA_RESULT_DATA_ERROR;
137 /* Decode LZMA properties and allocate memory */
138 if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
140 printf("Incorrect stream properties");
141 return LZMA_RESULT_DATA_ERROR;
143 state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
145 if (outSizeFull == 0)
149 if (outSizeFull > d_len)
155 if (compressedSize == 0)
159 if ((compressedSize+rpos) > s_len )
162 inStream = source + rpos;
166 || (outStream == 0 && outSizeFull != 0)
167 || (inStream == 0 && compressedSize != 0)
171 printf("%s\n", kCantAllocateMessage);
172 return LZMA_RESULT_DATA_ERROR;
179 res = LzmaDecode(&state,
180 inStream, compressedSize, &inProcessed,
181 outStream, outSizeFull, &outProcessed);
184 printf("\nDecoding error = %d\n", res);
189 *d_len = outProcessed;
197 #endif /* CONFIG_LZMA */