add /etc/openwrt_version
[openwrt/svn-archive/archive.git] / package / uboot-ifxmips / files / lib_bootstrap / LzmaWrapper.c
1 /******************************************************************************
2 **
3 ** FILE NAME : LzmaWrapper.c
4 ** PROJECT : bootloader
5 ** MODULES : U-boot
6 **
7 ** DATE : 2 Nov 2006
8 ** AUTHOR : Lin Mars
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
13 **
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.
18 **
19 ** HISTORY
20 ** $Date $Author $Comment
21 ** 2 Nov 2006 Lin Mars init version which derived from LzmaTest.c from
22 ** LZMA v4.43 SDK
23 *******************************************************************************/
24 #define LZMA_NO_STDIO
25 #ifndef LZMA_NO_STDIO
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #endif
30
31 #include <config.h>
32 #include <common.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/ctype.h>
36 #include <malloc.h>
37
38 #ifdef CONFIG_LZMA
39
40 #include "LzmaDecode.h"
41 #include "LzmaWrapper.h"
42
43 static const char *kCantReadMessage = "Can not read from source buffer";
44 static const char *kCantAllocateMessage = "Not enough buffer for decompression";
45
46 static size_t rpos=0, dpos=0;
47
48 static int MyReadFileAndCheck(unsigned char *src, void *dest, size_t size)
49 {
50 if (size == 0)
51 return 0;
52 memcpy(dest, src + rpos, size);
53 rpos += size;
54 return 1;
55 }
56
57 int lzma_inflate(unsigned char *source, int s_len, unsigned char *dest, int *d_len)
58 {
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*/
62 UInt32 outSize = 0;
63 UInt32 outSizeHigh = 0;
64 SizeT outSizeFull;
65 unsigned char *outStream;
66
67 int waitEOS = 1;
68 /* waitEOS = 1, if there is no uncompressed size in headers,
69 so decoder will wait EOS (End of Stream Marker) in compressed stream */
70
71 SizeT compressedSize;
72 unsigned char *inStream;
73
74 CLzmaDecoderState state; /* it's about 24-80 bytes structure, if int is 32-bit */
75 unsigned char properties[LZMA_PROPERTIES_SIZE];
76
77 int res;
78
79 if (sizeof(UInt32) < 4)
80 {
81 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
82 printf("LZMA decoder needs correct UInt32\n");
83 #endif
84 return LZMA_RESULT_DATA_ERROR;
85 }
86
87 {
88 long length=s_len;
89 if ((long)(SizeT)length != length)
90 {
91
92 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
93 printf("Too big compressed stream\n");
94 #endif
95 return LZMA_RESULT_DATA_ERROR;
96 }
97 compressedSize = (SizeT)(length - (LZMA_PROPERTIES_SIZE + 8));
98 }
99
100 /* Read LZMA properties for compressed stream */
101
102 if (!MyReadFileAndCheck(source, properties, sizeof(properties)))
103 {
104
105 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
106 printf("%s\n", kCantReadMessage);
107 #endif
108 return LZMA_RESULT_DATA_ERROR;
109 }
110
111 /* Read uncompressed size */
112 {
113 int i;
114 for (i = 0; i < 8; i++)
115 {
116 unsigned char b;
117 if (!MyReadFileAndCheck(source, &b, 1))
118 {
119
120 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
121 printf("%s\n", kCantReadMessage);
122 #endif
123 return LZMA_RESULT_DATA_ERROR;
124 }
125 if (b != 0xFF)
126 waitEOS = 0;
127 if (i < 4)
128 outSize += (UInt32)(b) << (i * 8);
129 else
130 outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
131 }
132
133 if (waitEOS)
134 {
135
136 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
137 printf("Stream with EOS marker is not supported");
138 #endif
139 return LZMA_RESULT_DATA_ERROR;
140 }
141 outSizeFull = (SizeT)outSize;
142 if (sizeof(SizeT) >= 8)
143 outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
144 else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize)
145 {
146
147 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
148 printf("Too big uncompressed stream");
149 #endif
150 return LZMA_RESULT_DATA_ERROR;
151 }
152 }
153
154 /* Decode LZMA properties and allocate memory */
155 if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
156 {
157
158 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
159 printf("Incorrect stream properties");
160 #endif
161 return LZMA_RESULT_DATA_ERROR;
162 }
163 state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
164
165 if (outSizeFull == 0)
166 outStream = 0;
167 else
168 {
169 if (outSizeFull > d_len)
170 outStream = 0;
171 else
172 outStream = dest;
173 }
174
175 if (compressedSize == 0)
176 inStream = 0;
177 else
178 {
179 if ((compressedSize+rpos) > s_len )
180 inStream = 0;
181 else
182 inStream = source + rpos;
183 }
184
185 if (state.Probs == 0
186 || (outStream == 0 && outSizeFull != 0)
187 || (inStream == 0 && compressedSize != 0)
188 )
189 {
190 free(state.Probs);
191
192 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
193 printf("%s\n", kCantAllocateMessage);
194 #endif
195 return LZMA_RESULT_DATA_ERROR;
196 }
197
198 /* Decompress */
199 {
200 SizeT inProcessed;
201 SizeT outProcessed;
202 res = LzmaDecode(&state,
203 inStream, compressedSize, &inProcessed,
204 outStream, outSizeFull, &outProcessed);
205 if (res != 0)
206 {
207
208 #ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF
209 printf("\nDecoding error = %d\n", res);
210 #endif
211 res = 1;
212 }
213 else
214 {
215 *d_len = outProcessed;
216 }
217 }
218
219 free(state.Probs);
220 return res;
221 }
222
223 #endif /* CONFIG_LZMA */