1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#ifdef STATIC
34#include "lzo/lzo1x_decompress.c"
35#else
36#include <linux/decompress/unlzo.h>
37#endif
38
39#include <linux/types.h>
40#include <linux/lzo.h>
41#include <linux/decompress/mm.h>
42
43#include <linux/compiler.h>
44#include <asm/unaligned.h>
45
46static const unsigned char lzop_magic[] = {
47 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
48
49#define LZO_BLOCK_SIZE (256*1024l)
50#define HEADER_HAS_FILTER 0x00000800L
51#define HEADER_SIZE_MIN (9 + 7 + 4 + 8 + 1 + 4)
52#define HEADER_SIZE_MAX (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4)
53
54STATIC inline int INIT parse_header(u8 *input, int *skip, int in_len)
55{
56 int l;
57 u8 *parse = input;
58 u8 *end = input + in_len;
59 u8 level = 0;
60 u16 version;
61
62
63
64
65
66
67 if (in_len < HEADER_SIZE_MIN)
68 return 0;
69
70
71 for (l = 0; l < 9; l++) {
72 if (*parse++ != lzop_magic[l])
73 return 0;
74 }
75
76
77
78 version = get_unaligned_be16(parse);
79 parse += 7;
80 if (version >= 0x0940)
81 level = *parse++;
82 if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
83 parse += 8;
84 else
85 parse += 4;
86
87
88
89
90
91
92
93 if (end - parse < 8 + 1 + 4)
94 return 0;
95
96
97 parse += 8;
98 if (version >= 0x0940)
99 parse += 4;
100
101 l = *parse++;
102
103 if (end - parse < l + 4)
104 return 0;
105 parse += l + 4;
106
107 *skip = parse - input;
108 return 1;
109}
110
111STATIC inline int INIT unlzo(u8 *input, int in_len,
112 int (*fill) (void *, unsigned int),
113 int (*flush) (void *, unsigned int),
114 u8 *output, int *posp,
115 void (*error) (char *x))
116{
117 u8 r = 0;
118 int skip = 0;
119 u32 src_len, dst_len;
120 size_t tmp;
121 u8 *in_buf, *in_buf_save, *out_buf;
122 int ret = -1;
123
124 if (output) {
125 out_buf = output;
126 } else if (!flush) {
127 error("NULL output pointer and no flush function provided");
128 goto exit;
129 } else {
130 out_buf = malloc(LZO_BLOCK_SIZE);
131 if (!out_buf) {
132 error("Could not allocate output buffer");
133 goto exit;
134 }
135 }
136
137 if (input && fill) {
138 error("Both input pointer and fill function provided, don't know what to do");
139 goto exit_1;
140 } else if (input) {
141 in_buf = input;
142 } else if (!fill) {
143 error("NULL input pointer and missing fill function");
144 goto exit_1;
145 } else {
146 in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
147 if (!in_buf) {
148 error("Could not allocate input buffer");
149 goto exit_1;
150 }
151 }
152 in_buf_save = in_buf;
153
154 if (posp)
155 *posp = 0;
156
157 if (fill) {
158
159
160
161
162
163
164 in_buf += HEADER_SIZE_MAX;
165 in_len = fill(in_buf, HEADER_SIZE_MAX);
166 }
167
168 if (!parse_header(in_buf, &skip, in_len)) {
169 error("invalid header");
170 goto exit_2;
171 }
172 in_buf += skip;
173 in_len -= skip;
174
175 if (fill) {
176
177 memcpy(in_buf_save, in_buf, in_len);
178 in_buf = in_buf_save;
179 }
180
181 if (posp)
182 *posp = skip;
183
184 for (;;) {
185
186 if (fill && in_len < 4) {
187 skip = fill(in_buf + in_len, 4 - in_len);
188 if (skip > 0)
189 in_len += skip;
190 }
191 if (in_len < 4) {
192 error("file corrupted");
193 goto exit_2;
194 }
195 dst_len = get_unaligned_be32(in_buf);
196 in_buf += 4;
197 in_len -= 4;
198
199
200 if (dst_len == 0) {
201 if (posp)
202 *posp += 4;
203 break;
204 }
205
206 if (dst_len > LZO_BLOCK_SIZE) {
207 error("dest len longer than block size");
208 goto exit_2;
209 }
210
211
212 if (fill && in_len < 8) {
213 skip = fill(in_buf + in_len, 8 - in_len);
214 if (skip > 0)
215 in_len += skip;
216 }
217 if (in_len < 8) {
218 error("file corrupted");
219 goto exit_2;
220 }
221 src_len = get_unaligned_be32(in_buf);
222 in_buf += 8;
223 in_len -= 8;
224
225 if (src_len <= 0 || src_len > dst_len) {
226 error("file corrupted");
227 goto exit_2;
228 }
229
230
231 if (fill && in_len < src_len) {
232 skip = fill(in_buf + in_len, src_len - in_len);
233 if (skip > 0)
234 in_len += skip;
235 }
236 if (in_len < src_len) {
237 error("file corrupted");
238 goto exit_2;
239 }
240 tmp = dst_len;
241
242
243
244
245 if (unlikely(dst_len == src_len))
246 memcpy(out_buf, in_buf, src_len);
247 else {
248 r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
249 out_buf, &tmp);
250
251 if (r != LZO_E_OK || dst_len != tmp) {
252 error("Compressed data violation");
253 goto exit_2;
254 }
255 }
256
257 if (flush && flush(out_buf, dst_len) != dst_len)
258 goto exit_2;
259 if (output)
260 out_buf += dst_len;
261 if (posp)
262 *posp += src_len + 12;
263
264 in_buf += src_len;
265 in_len -= src_len;
266 if (fill) {
267
268
269
270
271
272 if (in_len > 0)
273 for (skip = 0; skip < in_len; ++skip)
274 in_buf_save[skip] = in_buf[skip];
275 in_buf = in_buf_save;
276 }
277 }
278
279 ret = 0;
280exit_2:
281 if (!input)
282 free(in_buf);
283exit_1:
284 if (!output)
285 free(out_buf);
286exit:
287 return ret;
288}
289
290#define decompress unlzo
291