1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2012 Michihiro NAKAJIMA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "archive_platform.h"
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #ifdef HAVE_LZO_LZOCONF_H
45 #include <lzo/lzoconf.h>
46 #endif
47 #ifdef HAVE_LZO_LZO1X_H
48 #include <lzo/lzo1x.h>
49 #endif
50 #ifdef HAVE_ZLIB_H
51 #include <zlib.h> /* for crc32 and adler32 */
52 #endif
53
54 #include "archive.h"
55 #if !defined(HAVE_ZLIB_H) &&\
56 defined(HAVE_LZO_LZOCONF_H) && defined(HAVE_LZO_LZO1X_H)
57 #include "archive_crc32.h"
58 #endif
59 #include "archive_endian.h"
60 #include "archive_private.h"
61 #include "archive_read_private.h"
62
63 #ifndef HAVE_ZLIB_H
64 #define adler32 lzo_adler32
65 #endif
66
67 #define LZOP_HEADER_MAGIC "\x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a"
68 #define LZOP_HEADER_MAGIC_LEN 9
69
70 #if defined(HAVE_LZO_LZOCONF_H) && defined(HAVE_LZO_LZO1X_H)
71 struct read_lzop {
72 unsigned char *out_block;
73 size_t out_block_size;
74 int64_t total_out;
75 int flags;
76 uint32_t compressed_cksum;
77 uint32_t uncompressed_cksum;
78 size_t compressed_size;
79 size_t uncompressed_size;
80 size_t unconsumed_bytes;
81 char in_stream;
82 char eof; /* True = found end of compressed data. */
83 };
84
85 #define FILTER 0x0800
86 #define CRC32_HEADER 0x1000
87 #define EXTRA_FIELD 0x0040
88 #define ADLER32_UNCOMPRESSED 0x0001
89 #define ADLER32_COMPRESSED 0x0002
90 #define CRC32_UNCOMPRESSED 0x0100
91 #define CRC32_COMPRESSED 0x0200
92 #define MAX_BLOCK_SIZE (64 * 1024 * 1024)
93
94 static ssize_t lzop_filter_read(struct archive_read_filter *, const void **);
95 static int lzop_filter_close(struct archive_read_filter *);
96 #endif
97
98 static int lzop_bidder_bid(struct archive_read_filter_bidder *,
99 struct archive_read_filter *);
100 static int lzop_bidder_init(struct archive_read_filter *);
101
102 static const struct archive_read_filter_bidder_vtable
103 lzop_bidder_vtable = {
104 .bid = lzop_bidder_bid,
105 .init = lzop_bidder_init,
106 };
107
108 int
archive_read_support_filter_lzop(struct archive * _a)109 archive_read_support_filter_lzop(struct archive *_a)
110 {
111 struct archive_read *a = (struct archive_read *)_a;
112
113 if (__archive_read_register_bidder(a, NULL, NULL,
114 &lzop_bidder_vtable) != ARCHIVE_OK)
115 return (ARCHIVE_FATAL);
116
117 /* Signal the extent of lzop support with the return value here. */
118 #if defined(HAVE_LZO_LZOCONF_H) && defined(HAVE_LZO_LZO1X_H)
119 return (ARCHIVE_OK);
120 #else
121 /* Return ARCHIVE_WARN since this always uses an external program. */
122 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
123 "Using external lzop program for lzop decompression");
124 return (ARCHIVE_WARN);
125 #endif
126 }
127
128 /*
129 * Bidder just verifies the header and returns the number of verified bits.
130 */
131 static int
lzop_bidder_bid(struct archive_read_filter_bidder * self,struct archive_read_filter * filter)132 lzop_bidder_bid(struct archive_read_filter_bidder *self,
133 struct archive_read_filter *filter)
134 {
135 const unsigned char *p;
136 ssize_t avail;
137
138 (void)self; /* UNUSED */
139
140 p = __archive_read_filter_ahead(filter, LZOP_HEADER_MAGIC_LEN, &avail);
141 if (p == NULL || avail == 0)
142 return (0);
143
144 if (memcmp(p, LZOP_HEADER_MAGIC, LZOP_HEADER_MAGIC_LEN))
145 return (0);
146
147 return (LZOP_HEADER_MAGIC_LEN * 8);
148 }
149
150 #if !defined(HAVE_LZO_LZOCONF_H) || !defined(HAVE_LZO_LZO1X_H)
151 /*
152 * If we don't have the library on this system, we can't do the
153 * decompression directly. We can, however, try to run "lzop -d"
154 * in case that's available.
155 */
156 static int
lzop_bidder_init(struct archive_read_filter * self)157 lzop_bidder_init(struct archive_read_filter *self)
158 {
159 int r;
160
161 r = __archive_read_program(self, "lzop -d");
162 /* Note: We set the format here even if __archive_read_program()
163 * above fails. We do, after all, know what the format is
164 * even if we weren't able to read it. */
165 self->code = ARCHIVE_FILTER_LZOP;
166 self->name = "lzop";
167 return (r);
168 }
169 #else
170
171 static const struct archive_read_filter_vtable
172 lzop_reader_vtable = {
173 .read = lzop_filter_read,
174 .close = lzop_filter_close
175 };
176
177 /*
178 * Initialize the filter object.
179 */
180 static int
lzop_bidder_init(struct archive_read_filter * self)181 lzop_bidder_init(struct archive_read_filter *self)
182 {
183 struct read_lzop *state;
184
185 self->code = ARCHIVE_FILTER_LZOP;
186 self->name = "lzop";
187
188 state = calloc(1, sizeof(*state));
189 if (state == NULL) {
190 archive_set_error(&self->archive->archive, ENOMEM,
191 "Can't allocate data for lzop decompression");
192 return (ARCHIVE_FATAL);
193 }
194
195 self->data = state;
196 self->vtable = &lzop_reader_vtable;
197
198 return (ARCHIVE_OK);
199 }
200
201 static int
consume_header(struct archive_read_filter * self)202 consume_header(struct archive_read_filter *self)
203 {
204 struct read_lzop *state = (struct read_lzop *)self->data;
205 const unsigned char *p, *_p;
206 unsigned checksum, flags, len, method, version;
207
208 /*
209 * Check LZOP magic code.
210 */
211 p = __archive_read_filter_ahead(self->upstream,
212 LZOP_HEADER_MAGIC_LEN, NULL);
213 if (p == NULL)
214 return (ARCHIVE_EOF);
215
216 if (memcmp(p, LZOP_HEADER_MAGIC, LZOP_HEADER_MAGIC_LEN))
217 return (ARCHIVE_EOF);
218 __archive_read_filter_consume(self->upstream,
219 LZOP_HEADER_MAGIC_LEN);
220
221 p = __archive_read_filter_ahead(self->upstream, 29, NULL);
222 if (p == NULL)
223 goto truncated;
224 _p = p;
225 version = archive_be16dec(p);
226 p += 4;/* version(2 bytes) + library version(2 bytes) */
227
228 if (version >= 0x940) {
229 unsigned reqversion = archive_be16dec(p); p += 2;
230 if (reqversion < 0x900) {
231 archive_set_error(&self->archive->archive,
232 ARCHIVE_ERRNO_MISC, "Invalid required version");
233 return (ARCHIVE_FAILED);
234 }
235 }
236
237 method = *p++;
238 if (method < 1 || method > 3) {
239 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
240 "Unsupported method");
241 return (ARCHIVE_FAILED);
242 }
243
244 if (version >= 0x940) {
245 unsigned level = *p++;
246 #if 0
247 unsigned default_level[] = {0, 3, 1, 9};
248 #endif
249 if (level == 0)
250 /* Method is 1..3 here due to check above. */
251 #if 0 /* Avoid an error Clang Static Analyzer claims
252 "Value stored to 'level' is never read". */
253 level = default_level[method];
254 #else
255 ;/* NOP */
256 #endif
257 else if (level > 9) {
258 archive_set_error(&self->archive->archive,
259 ARCHIVE_ERRNO_MISC, "Invalid level");
260 return (ARCHIVE_FAILED);
261 }
262 }
263
264 flags = archive_be32dec(p); p += 4;
265
266 if (flags & FILTER)
267 p += 4; /* Skip filter */
268 p += 4; /* Skip mode */
269 if (version >= 0x940)
270 p += 8; /* Skip mtime */
271 else
272 p += 4; /* Skip mtime */
273 len = *p++; /* Read filename length */
274 len += p - _p;
275 /* Make sure we have all bytes we need to calculate checksum. */
276 p = __archive_read_filter_ahead(self->upstream, len + 4, NULL);
277 if (p == NULL)
278 goto truncated;
279 if (flags & CRC32_HEADER)
280 checksum = crc32(crc32(0, NULL, 0), p, len);
281 else
282 checksum = adler32(adler32(0, NULL, 0), p, len);
283 #ifndef DONT_FAIL_ON_CRC_ERROR
284 if (archive_be32dec(p + len) != checksum)
285 goto corrupted;
286 #endif
287 __archive_read_filter_consume(self->upstream, len + 4);
288 if (flags & EXTRA_FIELD) {
289 /* Skip extra field */
290 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
291 if (p == NULL)
292 goto truncated;
293 len = archive_be32dec(p);
294 __archive_read_filter_consume(self->upstream,
295 (int64_t)len + 4 + 4);
296 }
297 state->flags = flags;
298 state->in_stream = 1;
299 return (ARCHIVE_OK);
300 truncated:
301 archive_set_error(&self->archive->archive,
302 ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
303 return (ARCHIVE_FAILED);
304 corrupted:
305 archive_set_error(&self->archive->archive,
306 ARCHIVE_ERRNO_FILE_FORMAT, "Corrupted lzop header");
307 return (ARCHIVE_FAILED);
308 }
309
310 static int
consume_block_info(struct archive_read_filter * self)311 consume_block_info(struct archive_read_filter *self)
312 {
313 struct read_lzop *state = (struct read_lzop *)self->data;
314 const unsigned char *p;
315 unsigned flags = state->flags;
316
317 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
318 if (p == NULL)
319 goto truncated;
320 state->uncompressed_size = archive_be32dec(p);
321 __archive_read_filter_consume(self->upstream, 4);
322 if (state->uncompressed_size == 0)
323 return (ARCHIVE_EOF);
324 if (state->uncompressed_size > MAX_BLOCK_SIZE)
325 goto corrupted;
326
327 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
328 if (p == NULL)
329 goto truncated;
330 state->compressed_size = archive_be32dec(p);
331 __archive_read_filter_consume(self->upstream, 4);
332 if (state->compressed_size > state->uncompressed_size)
333 goto corrupted;
334
335 if (flags & (CRC32_UNCOMPRESSED | ADLER32_UNCOMPRESSED)) {
336 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
337 if (p == NULL)
338 goto truncated;
339 state->compressed_cksum = state->uncompressed_cksum =
340 archive_be32dec(p);
341 __archive_read_filter_consume(self->upstream, 4);
342 }
343 if ((flags & (CRC32_COMPRESSED | ADLER32_COMPRESSED)) &&
344 state->compressed_size < state->uncompressed_size) {
345 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
346 if (p == NULL)
347 goto truncated;
348 state->compressed_cksum = archive_be32dec(p);
349 __archive_read_filter_consume(self->upstream, 4);
350 }
351 return (ARCHIVE_OK);
352 truncated:
353 archive_set_error(&self->archive->archive,
354 ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
355 return (ARCHIVE_FAILED);
356 corrupted:
357 archive_set_error(&self->archive->archive,
358 ARCHIVE_ERRNO_FILE_FORMAT, "Corrupted lzop header");
359 return (ARCHIVE_FAILED);
360 }
361
362 static ssize_t
lzop_filter_read(struct archive_read_filter * self,const void ** p)363 lzop_filter_read(struct archive_read_filter *self, const void **p)
364 {
365 struct read_lzop *state = (struct read_lzop *)self->data;
366 const void *b;
367 lzo_uint out_size;
368 uint32_t cksum;
369 int ret, r;
370
371 if (state->unconsumed_bytes) {
372 __archive_read_filter_consume(self->upstream,
373 state->unconsumed_bytes);
374 state->unconsumed_bytes = 0;
375 }
376 if (state->eof)
377 return (0);
378
379 for (;;) {
380 if (!state->in_stream) {
381 ret = consume_header(self);
382 if (ret < ARCHIVE_OK)
383 return (ret);
384 if (ret == ARCHIVE_EOF) {
385 state->eof = 1;
386 return (0);
387 }
388 }
389 ret = consume_block_info(self);
390 if (ret < ARCHIVE_OK)
391 return (ret);
392 if (ret == ARCHIVE_EOF)
393 state->in_stream = 0;
394 else
395 break;
396 }
397
398 if (state->out_block == NULL ||
399 state->out_block_size < state->uncompressed_size) {
400 void *new_block;
401
402 new_block = realloc(state->out_block, state->uncompressed_size);
403 if (new_block == NULL) {
404 archive_set_error(&self->archive->archive, ENOMEM,
405 "Can't allocate data for lzop decompression");
406 return (ARCHIVE_FATAL);
407 }
408 state->out_block = new_block;
409 state->out_block_size = state->uncompressed_size;
410 }
411
412 b = __archive_read_filter_ahead(self->upstream,
413 state->compressed_size, NULL);
414 if (b == NULL) {
415 archive_set_error(&self->archive->archive,
416 ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
417 return (ARCHIVE_FATAL);
418 }
419 if (state->flags & CRC32_COMPRESSED)
420 cksum = crc32(crc32(0, NULL, 0), b, state->compressed_size);
421 else if (state->flags & ADLER32_COMPRESSED)
422 cksum = adler32(adler32(0, NULL, 0), b, state->compressed_size);
423 else
424 cksum = state->compressed_cksum;
425 if (cksum != state->compressed_cksum) {
426 archive_set_error(&self->archive->archive,
427 ARCHIVE_ERRNO_MISC, "Corrupted data");
428 return (ARCHIVE_FATAL);
429 }
430
431 /*
432 * If the both uncompressed size and compressed size are the same,
433 * we do not decompress this block.
434 */
435 if (state->uncompressed_size == state->compressed_size) {
436 *p = b;
437 state->total_out += state->compressed_size;
438 state->unconsumed_bytes = state->compressed_size;
439 return ((ssize_t)state->uncompressed_size);
440 }
441
442 /*
443 * Drive lzo uncompression.
444 */
445 out_size = (lzo_uint)state->uncompressed_size;
446 r = lzo1x_decompress_safe(b, (lzo_uint)state->compressed_size,
447 state->out_block, &out_size, NULL);
448 switch (r) {
449 case LZO_E_OK:
450 if (out_size == state->uncompressed_size)
451 break;
452 archive_set_error(&self->archive->archive,
453 ARCHIVE_ERRNO_MISC, "Corrupted data");
454 return (ARCHIVE_FATAL);
455 case LZO_E_OUT_OF_MEMORY:
456 archive_set_error(&self->archive->archive, ENOMEM,
457 "lzop decompression failed: out of memory");
458 return (ARCHIVE_FATAL);
459 default:
460 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
461 "lzop decompression failed: %d", r);
462 return (ARCHIVE_FATAL);
463 }
464
465 if (state->flags & CRC32_UNCOMPRESSED)
466 cksum = crc32(crc32(0, NULL, 0), state->out_block,
467 state->uncompressed_size);
468 else if (state->flags & ADLER32_UNCOMPRESSED)
469 cksum = adler32(adler32(0, NULL, 0), state->out_block,
470 state->uncompressed_size);
471 else
472 cksum = state->uncompressed_cksum;
473 if (cksum != state->uncompressed_cksum) {
474 archive_set_error(&self->archive->archive,
475 ARCHIVE_ERRNO_MISC, "Corrupted data");
476 return (ARCHIVE_FATAL);
477 }
478
479 __archive_read_filter_consume(self->upstream, state->compressed_size);
480 *p = state->out_block;
481 state->total_out += out_size;
482 return ((ssize_t)out_size);
483 }
484
485 /*
486 * Clean up the decompressor.
487 */
488 static int
lzop_filter_close(struct archive_read_filter * self)489 lzop_filter_close(struct archive_read_filter *self)
490 {
491 struct read_lzop *state = (struct read_lzop *)self->data;
492
493 free(state->out_block);
494 free(state);
495 return (ARCHIVE_OK);
496 }
497
498 #endif
499