2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
46 #ifdef HAVE_LZO_LZOCONF_H
47 #include <lzo/lzoconf.h>
49 #ifdef HAVE_LZO_LZO1X_H
50 #include <lzo/lzo1x.h>
53 #include <zlib.h> /* for crc32 and adler32 */
57 #if !defined(HAVE_ZLIB_H) &&\
58 defined(HAVE_LZO_LZOCONF_H) && defined(HAVE_LZO_LZO1X_H)
59 #include "archive_crc32.h"
61 #include "archive_endian.h"
62 #include "archive_private.h"
63 #include "archive_read_private.h"
66 #define adler32 lzo_adler32
69 #define LZOP_HEADER_MAGIC "\x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a"
70 #define LZOP_HEADER_MAGIC_LEN 9
72 #if defined(HAVE_LZO_LZOCONF_H) && defined(HAVE_LZO_LZO1X_H)
74 unsigned char *out_block;
75 size_t out_block_size;
78 uint32_t compressed_cksum;
79 uint32_t uncompressed_cksum;
80 size_t compressed_size;
81 size_t uncompressed_size;
82 size_t unconsumed_bytes;
84 char eof; /* True = found end of compressed data. */
88 #define CRC32_HEADER 0x1000
89 #define EXTRA_FIELD 0x0040
90 #define ADLER32_UNCOMPRESSED 0x0001
91 #define ADLER32_COMPRESSED 0x0002
92 #define CRC32_UNCOMPRESSED 0x0100
93 #define CRC32_COMPRESSED 0x0200
94 #define MAX_BLOCK_SIZE (64 * 1024 * 1024)
96 static ssize_t lzop_filter_read(struct archive_read_filter *, const void **);
97 static int lzop_filter_close(struct archive_read_filter *);
100 static int lzop_bidder_bid(struct archive_read_filter_bidder *,
101 struct archive_read_filter *);
102 static int lzop_bidder_init(struct archive_read_filter *);
105 archive_read_support_filter_lzop(struct archive *_a)
107 struct archive_read *a = (struct archive_read *)_a;
108 struct archive_read_filter_bidder *reader;
110 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
111 ARCHIVE_STATE_NEW, "archive_read_support_filter_lzop");
113 if (__archive_read_get_bidder(a, &reader) != ARCHIVE_OK)
114 return (ARCHIVE_FATAL);
117 reader->bid = lzop_bidder_bid;
118 reader->init = lzop_bidder_init;
119 reader->options = NULL;
121 /* Signal the extent of lzop support with the return value here. */
122 #if defined(HAVE_LZO_LZOCONF_H) && defined(HAVE_LZO_LZO1X_H)
125 /* Return ARCHIVE_WARN since this always uses an external program. */
126 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
127 "Using external lzop program for lzop decompression");
128 return (ARCHIVE_WARN);
133 * Bidder just verifies the header and returns the number of verified bits.
136 lzop_bidder_bid(struct archive_read_filter_bidder *self,
137 struct archive_read_filter *filter)
139 const unsigned char *p;
142 (void)self; /* UNUSED */
144 p = __archive_read_filter_ahead(filter, LZOP_HEADER_MAGIC_LEN, &avail);
145 if (p == NULL || avail == 0)
148 if (memcmp(p, LZOP_HEADER_MAGIC, LZOP_HEADER_MAGIC_LEN))
151 return (LZOP_HEADER_MAGIC_LEN * 8);
154 #if !defined(HAVE_LZO_LZOCONF_H) || !defined(HAVE_LZO_LZO1X_H)
156 * If we don't have the library on this system, we can't do the
157 * decompression directly. We can, however, try to run "lzop -d"
158 * in case that's available.
161 lzop_bidder_init(struct archive_read_filter *self)
165 r = __archive_read_program(self, "lzop -d");
166 /* Note: We set the format here even if __archive_read_program()
167 * above fails. We do, after all, know what the format is
168 * even if we weren't able to read it. */
169 self->code = ARCHIVE_FILTER_LZOP;
175 * Initialize the filter object.
178 lzop_bidder_init(struct archive_read_filter *self)
180 struct read_lzop *state;
182 self->code = ARCHIVE_FILTER_LZOP;
185 state = (struct read_lzop *)calloc(sizeof(*state), 1);
187 archive_set_error(&self->archive->archive, ENOMEM,
188 "Can't allocate data for lzop decompression");
189 return (ARCHIVE_FATAL);
193 self->read = lzop_filter_read;
194 self->skip = NULL; /* not supported */
195 self->close = lzop_filter_close;
201 consume_header(struct archive_read_filter *self)
203 struct read_lzop *state = (struct read_lzop *)self->data;
204 const unsigned char *p, *_p;
205 unsigned checksum, flags, len, method, version;
208 * Check LZOP magic code.
210 p = __archive_read_filter_ahead(self->upstream,
211 LZOP_HEADER_MAGIC_LEN, NULL);
213 return (ARCHIVE_EOF);
215 if (memcmp(p, LZOP_HEADER_MAGIC, LZOP_HEADER_MAGIC_LEN))
216 return (ARCHIVE_EOF);
217 __archive_read_filter_consume(self->upstream,
218 LZOP_HEADER_MAGIC_LEN);
220 p = __archive_read_filter_ahead(self->upstream, 29, NULL);
224 version = archive_be16dec(p);
225 p += 4;/* version(2 bytes) + library version(2 bytes) */
227 if (version >= 0x940) {
228 unsigned reqversion = archive_be16dec(p); p += 2;
229 if (reqversion < 0x900) {
230 archive_set_error(&self->archive->archive,
231 ARCHIVE_ERRNO_MISC, "Invalid required version");
232 return (ARCHIVE_FAILED);
237 if (method < 1 || method > 3) {
238 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
239 "Unsupported method");
240 return (ARCHIVE_FAILED);
243 if (version >= 0x940) {
244 unsigned level = *p++;
246 unsigned default_level[] = {0, 3, 1, 9};
249 /* Method is 1..3 here due to check above. */
250 #if 0 /* Avoid an error Clang Static Analyzer claims
251 "Value stored to 'level' is never read". */
252 level = default_level[method];
256 else if (level > 9) {
257 archive_set_error(&self->archive->archive,
258 ARCHIVE_ERRNO_MISC, "Invalid level");
259 return (ARCHIVE_FAILED);
263 flags = archive_be32dec(p); p += 4;
266 p += 4; /* Skip filter */
267 p += 4; /* Skip mode */
268 if (version >= 0x940)
269 p += 8; /* Skip mtime */
271 p += 4; /* Skip mtime */
272 len = *p++; /* Read filename length */
274 /* Make sure we have all bytes we need to calculate checksum. */
275 p = __archive_read_filter_ahead(self->upstream, len + 4, NULL);
278 if (flags & CRC32_HEADER)
279 checksum = crc32(crc32(0, NULL, 0), p, len);
281 checksum = adler32(adler32(0, NULL, 0), p, len);
282 if (archive_be32dec(p + len) != checksum)
284 __archive_read_filter_consume(self->upstream, len + 4);
285 if (flags & EXTRA_FIELD) {
286 /* Skip extra field */
287 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
290 len = archive_be32dec(p);
291 __archive_read_filter_consume(self->upstream, len + 4 + 4);
293 state->flags = flags;
294 state->in_stream = 1;
297 archive_set_error(&self->archive->archive,
298 ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
299 return (ARCHIVE_FAILED);
301 archive_set_error(&self->archive->archive,
302 ARCHIVE_ERRNO_FILE_FORMAT, "Corrupted lzop header");
303 return (ARCHIVE_FAILED);
307 consume_block_info(struct archive_read_filter *self)
309 struct read_lzop *state = (struct read_lzop *)self->data;
310 const unsigned char *p;
311 unsigned flags = state->flags;
313 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
316 state->uncompressed_size = archive_be32dec(p);
317 __archive_read_filter_consume(self->upstream, 4);
318 if (state->uncompressed_size == 0)
319 return (ARCHIVE_EOF);
320 if (state->uncompressed_size > MAX_BLOCK_SIZE)
323 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
326 state->compressed_size = archive_be32dec(p);
327 __archive_read_filter_consume(self->upstream, 4);
328 if (state->compressed_size > state->uncompressed_size)
331 if (flags & (CRC32_UNCOMPRESSED | ADLER32_UNCOMPRESSED)) {
332 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
335 state->compressed_cksum = state->uncompressed_cksum =
337 __archive_read_filter_consume(self->upstream, 4);
339 if ((flags & (CRC32_COMPRESSED | ADLER32_COMPRESSED)) &&
340 state->compressed_size < state->uncompressed_size) {
341 p = __archive_read_filter_ahead(self->upstream, 4, NULL);
344 state->compressed_cksum = archive_be32dec(p);
345 __archive_read_filter_consume(self->upstream, 4);
349 archive_set_error(&self->archive->archive,
350 ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
351 return (ARCHIVE_FAILED);
353 archive_set_error(&self->archive->archive,
354 ARCHIVE_ERRNO_FILE_FORMAT, "Corrupted lzop header");
355 return (ARCHIVE_FAILED);
359 lzop_filter_read(struct archive_read_filter *self, const void **p)
361 struct read_lzop *state = (struct read_lzop *)self->data;
367 if (state->unconsumed_bytes) {
368 __archive_read_filter_consume(self->upstream,
369 state->unconsumed_bytes);
370 state->unconsumed_bytes = 0;
376 if (!state->in_stream) {
377 ret = consume_header(self);
378 if (ret < ARCHIVE_OK)
380 if (ret == ARCHIVE_EOF) {
385 ret = consume_block_info(self);
386 if (ret < ARCHIVE_OK)
388 if (ret == ARCHIVE_EOF)
389 state->in_stream = 0;
394 if (state->out_block == NULL ||
395 state->out_block_size < state->uncompressed_size) {
398 new_block = realloc(state->out_block, state->uncompressed_size);
399 if (new_block == NULL) {
400 archive_set_error(&self->archive->archive, ENOMEM,
401 "Can't allocate data for lzop decompression");
402 return (ARCHIVE_FATAL);
404 state->out_block = new_block;
405 state->out_block_size = state->uncompressed_size;
408 b = __archive_read_filter_ahead(self->upstream,
409 state->compressed_size, NULL);
411 archive_set_error(&self->archive->archive,
412 ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
413 return (ARCHIVE_FATAL);
415 if (state->flags & CRC32_COMPRESSED)
416 cksum = crc32(crc32(0, NULL, 0), b, state->compressed_size);
417 else if (state->flags & ADLER32_COMPRESSED)
418 cksum = adler32(adler32(0, NULL, 0), b, state->compressed_size);
420 cksum = state->compressed_cksum;
421 if (cksum != state->compressed_cksum) {
422 archive_set_error(&self->archive->archive,
423 ARCHIVE_ERRNO_MISC, "Corrupted data");
424 return (ARCHIVE_FATAL);
428 * If the both uncompressed size and compressed size are the same,
429 * we do not decompress this block.
431 if (state->uncompressed_size == state->compressed_size) {
433 state->total_out += state->compressed_size;
434 state->unconsumed_bytes = state->compressed_size;
435 return ((ssize_t)state->uncompressed_size);
439 * Drive lzo uncompression.
441 out_size = (lzo_uint)state->uncompressed_size;
442 r = lzo1x_decompress_safe(b, (lzo_uint)state->compressed_size,
443 state->out_block, &out_size, NULL);
446 if (out_size == state->uncompressed_size)
448 archive_set_error(&self->archive->archive,
449 ARCHIVE_ERRNO_MISC, "Corrupted data");
450 return (ARCHIVE_FATAL);
451 case LZO_E_OUT_OF_MEMORY:
452 archive_set_error(&self->archive->archive, ENOMEM,
453 "lzop decompression failed: out of memory");
454 return (ARCHIVE_FATAL);
456 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
457 "lzop decompression failed: %d", r);
458 return (ARCHIVE_FATAL);
461 if (state->flags & CRC32_UNCOMPRESSED)
462 cksum = crc32(crc32(0, NULL, 0), state->out_block,
463 state->uncompressed_size);
464 else if (state->flags & ADLER32_UNCOMPRESSED)
465 cksum = adler32(adler32(0, NULL, 0), state->out_block,
466 state->uncompressed_size);
468 cksum = state->uncompressed_cksum;
469 if (cksum != state->uncompressed_cksum) {
470 archive_set_error(&self->archive->archive,
471 ARCHIVE_ERRNO_MISC, "Corrupted data");
472 return (ARCHIVE_FATAL);
475 __archive_read_filter_consume(self->upstream, state->compressed_size);
476 *p = state->out_block;
477 state->total_out += out_size;
478 return ((ssize_t)out_size);
482 * Clean up the decompressor.
485 lzop_filter_close(struct archive_read_filter *self)
487 struct read_lzop *state = (struct read_lzop *)self->data;
489 free(state->out_block);