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 __FBSDID("$FreeBSD$");
30 
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #endif
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 #ifdef HAVE_LZO_LZOCONF_H
47 #include <lzo/lzoconf.h>
48 #endif
49 #ifdef HAVE_LZO_LZO1X_H
50 #include <lzo/lzo1x.h>
51 #endif
52 #ifdef HAVE_ZLIB_H
53 #include <zlib.h> /* for crc32 and adler32 */
54 #endif
55 
56 #include "archive.h"
57 #if !defined(HAVE_ZLIB_H) &&\
58      defined(HAVE_LZO_LZOCONF_H) && defined(HAVE_LZO_LZO1X_H)
59 #include "archive_crc32.h"
60 #endif
61 #include "archive_endian.h"
62 #include "archive_private.h"
63 #include "archive_read_private.h"
64 
65 #ifndef HAVE_ZLIB_H
66 #define adler32	lzo_adler32
67 #endif
68 
69 #define LZOP_HEADER_MAGIC "\x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a"
70 #define LZOP_HEADER_MAGIC_LEN 9
71 
72 #if defined(HAVE_LZO_LZOCONF_H) && defined(HAVE_LZO_LZO1X_H)
73 struct read_lzop {
74 	unsigned char	*out_block;
75 	size_t		 out_block_size;
76 	int64_t		 total_out;
77 	int		 flags;
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;
83 	char		 in_stream;
84 	char		 eof; /* True = found end of compressed data. */
85 };
86 
87 #define FILTER			0x0800
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)
95 
96 static ssize_t  lzop_filter_read(struct archive_read_filter *, const void **);
97 static int	lzop_filter_close(struct archive_read_filter *);
98 #endif
99 
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 *);
103 
104 int
105 archive_read_support_filter_lzop(struct archive *_a)
106 {
107 	struct archive_read *a = (struct archive_read *)_a;
108 	struct archive_read_filter_bidder *reader;
109 
110 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
111 	    ARCHIVE_STATE_NEW, "archive_read_support_filter_lzop");
112 
113 	if (__archive_read_get_bidder(a, &reader) != ARCHIVE_OK)
114 		return (ARCHIVE_FATAL);
115 
116 	reader->data = NULL;
117 	reader->bid = lzop_bidder_bid;
118 	reader->init = lzop_bidder_init;
119 	reader->options = NULL;
120 	reader->free = 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)
123 	return (ARCHIVE_OK);
124 #else
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);
129 #endif
130 }
131 
132 /*
133  * Bidder just verifies the header and returns the number of verified bits.
134  */
135 static int
136 lzop_bidder_bid(struct archive_read_filter_bidder *self,
137     struct archive_read_filter *filter)
138 {
139 	const unsigned char *p;
140 	ssize_t avail;
141 
142 	(void)self; /* UNUSED */
143 
144 	p = __archive_read_filter_ahead(filter, LZOP_HEADER_MAGIC_LEN, &avail);
145 	if (p == NULL || avail == 0)
146 		return (0);
147 
148 	if (memcmp(p, LZOP_HEADER_MAGIC, LZOP_HEADER_MAGIC_LEN))
149 		return (0);
150 
151 	return (LZOP_HEADER_MAGIC_LEN * 8);
152 }
153 
154 #if !defined(HAVE_LZO_LZOCONF_H) || !defined(HAVE_LZO_LZO1X_H)
155 /*
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.
159  */
160 static int
161 lzop_bidder_init(struct archive_read_filter *self)
162 {
163 	int r;
164 
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;
170 	self->name = "lzop";
171 	return (r);
172 }
173 #else
174 /*
175  * Initialize the filter object.
176  */
177 static int
178 lzop_bidder_init(struct archive_read_filter *self)
179 {
180 	struct read_lzop *state;
181 
182 	self->code = ARCHIVE_FILTER_LZOP;
183 	self->name = "lzop";
184 
185 	state = (struct read_lzop *)calloc(sizeof(*state), 1);
186 	if (state == NULL) {
187 		archive_set_error(&self->archive->archive, ENOMEM,
188 		    "Can't allocate data for lzop decompression");
189 		return (ARCHIVE_FATAL);
190 	}
191 
192 	self->data = state;
193 	self->read = lzop_filter_read;
194 	self->skip = NULL; /* not supported */
195 	self->close = lzop_filter_close;
196 
197 	return (ARCHIVE_OK);
198 }
199 
200 static int
201 consume_header(struct archive_read_filter *self)
202 {
203 	struct read_lzop *state = (struct read_lzop *)self->data;
204 	const unsigned char *p, *_p;
205 	unsigned checksum, flags, len, method, version;
206 
207 	/*
208 	 * Check LZOP magic code.
209 	 */
210 	p = __archive_read_filter_ahead(self->upstream,
211 		LZOP_HEADER_MAGIC_LEN, NULL);
212 	if (p == NULL)
213 		return (ARCHIVE_EOF);
214 
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);
219 
220 	p = __archive_read_filter_ahead(self->upstream, 29, NULL);
221 	if (p == NULL)
222 		goto truncated;
223 	_p = p;
224 	version = archive_be16dec(p);
225 	p += 4;/* version(2 bytes) + library version(2 bytes) */
226 
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);
233 		}
234 	}
235 
236 	method = *p++;
237 	if (method < 1 || method > 3) {
238 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
239 		    "Unsupported method");
240 		return (ARCHIVE_FAILED);
241 	}
242 
243 	if (version >= 0x940) {
244 		unsigned level = *p++;
245 		if (method == 1 && level == 0) level = 3;
246 		if (method == 2 && level == 0) level = 1;
247 		if (method == 3 && level == 0) level = 9;
248 		if (level < 1 && level > 9) {
249 			archive_set_error(&self->archive->archive,
250 			    ARCHIVE_ERRNO_MISC, "Invalid level");
251 			return (ARCHIVE_FAILED);
252 		}
253 	}
254 
255 	flags = archive_be32dec(p); p += 4;
256 
257 	if (flags & FILTER)
258 		p += 4; /* Skip filter */
259 	p += 4; /* Skip mode */
260 	if (version >= 0x940)
261 		p += 8; /* Skip mtime */
262 	else
263 		p += 4; /* Skip mtime */
264 	len = *p++; /* Read filename length */
265 	len += p - _p;
266 	/* Make sure we have all bytes we need to calculate checksum. */
267 	p = __archive_read_filter_ahead(self->upstream, len + 4, NULL);
268 	if (p == NULL)
269 		goto truncated;
270 	if (flags & CRC32_HEADER)
271 		checksum = crc32(crc32(0, NULL, 0), p, len);
272 	else
273 		checksum = adler32(adler32(0, NULL, 0), p, len);
274 	if (archive_be32dec(p + len) != checksum)
275 		goto corrupted;
276 	__archive_read_filter_consume(self->upstream, len + 4);
277 	if (flags & EXTRA_FIELD) {
278 		/* Skip extra field */
279 		p = __archive_read_filter_ahead(self->upstream, 4, NULL);
280 		if (p == NULL)
281 			goto truncated;
282 		len = archive_be32dec(p);
283 		__archive_read_filter_consume(self->upstream, len + 4 + 4);
284 	}
285 	state->flags = flags;
286 	state->in_stream = 1;
287 	return (ARCHIVE_OK);
288 truncated:
289 	archive_set_error(&self->archive->archive,
290 	    ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
291 	return (ARCHIVE_FAILED);
292 corrupted:
293 	archive_set_error(&self->archive->archive,
294 	    ARCHIVE_ERRNO_FILE_FORMAT, "Corrupted lzop header");
295 	return (ARCHIVE_FAILED);
296 }
297 
298 static int
299 consume_block_info(struct archive_read_filter *self)
300 {
301 	struct read_lzop *state = (struct read_lzop *)self->data;
302 	const unsigned char *p;
303 	unsigned flags = state->flags;
304 
305 	p = __archive_read_filter_ahead(self->upstream, 4, NULL);
306 	if (p == NULL)
307 		goto truncated;
308 	state->uncompressed_size = archive_be32dec(p);
309 	__archive_read_filter_consume(self->upstream, 4);
310 	if (state->uncompressed_size == 0)
311 		return (ARCHIVE_EOF);
312 	if (state->uncompressed_size > MAX_BLOCK_SIZE)
313 		goto corrupted;
314 
315 	p = __archive_read_filter_ahead(self->upstream, 4, NULL);
316 	if (p == NULL)
317 		goto truncated;
318 	state->compressed_size = archive_be32dec(p);
319 	__archive_read_filter_consume(self->upstream, 4);
320 	if (state->compressed_size > state->uncompressed_size)
321 		goto corrupted;
322 
323 	if (flags & (CRC32_UNCOMPRESSED | ADLER32_UNCOMPRESSED)) {
324 		p = __archive_read_filter_ahead(self->upstream, 4, NULL);
325 		if (p == NULL)
326 			goto truncated;
327 		state->compressed_cksum = state->uncompressed_cksum =
328 		    archive_be32dec(p);
329 		__archive_read_filter_consume(self->upstream, 4);
330 	}
331 	if ((flags & (CRC32_COMPRESSED | ADLER32_COMPRESSED)) &&
332 	    state->compressed_size < state->uncompressed_size) {
333 		p = __archive_read_filter_ahead(self->upstream, 4, NULL);
334 		if (p == NULL)
335 			goto truncated;
336 		state->compressed_cksum = archive_be32dec(p);
337 		__archive_read_filter_consume(self->upstream, 4);
338 	}
339 	return (ARCHIVE_OK);
340 truncated:
341 	archive_set_error(&self->archive->archive,
342 	    ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
343 	return (ARCHIVE_FAILED);
344 corrupted:
345 	archive_set_error(&self->archive->archive,
346 	    ARCHIVE_ERRNO_FILE_FORMAT, "Corrupted lzop header");
347 	return (ARCHIVE_FAILED);
348 }
349 
350 static ssize_t
351 lzop_filter_read(struct archive_read_filter *self, const void **p)
352 {
353 	struct read_lzop *state = (struct read_lzop *)self->data;
354 	const void *b;
355 	lzo_uint out_size;
356 	uint32_t cksum;
357 	int ret, r;
358 
359 	if (state->unconsumed_bytes) {
360 		__archive_read_filter_consume(self->upstream,
361 		    state->unconsumed_bytes);
362 		state->unconsumed_bytes = 0;
363 	}
364 	if (state->eof)
365 		return (0);
366 
367 	for (;;) {
368 		if (!state->in_stream) {
369 			ret = consume_header(self);
370 			if (ret < ARCHIVE_OK)
371 				return (ret);
372 			if (ret == ARCHIVE_EOF) {
373 				state->eof = 1;
374 				return (0);
375 			}
376 		}
377 		ret = consume_block_info(self);
378 		if (ret < ARCHIVE_OK)
379 			return (ret);
380 		if (ret == ARCHIVE_EOF)
381 			state->in_stream = 0;
382 		else
383 			break;
384 	}
385 
386 	if (state->out_block == NULL ||
387 	    state->out_block_size < state->uncompressed_size) {
388 		void *new_block;
389 
390 		new_block = realloc(state->out_block, state->uncompressed_size);
391 		if (new_block == NULL) {
392 			archive_set_error(&self->archive->archive, ENOMEM,
393 			    "Can't allocate data for lzop decompression");
394 			return (ARCHIVE_FATAL);
395 		}
396 		state->out_block = new_block;
397 		state->out_block_size = state->uncompressed_size;
398 	}
399 
400 	b = __archive_read_filter_ahead(self->upstream,
401 		state->compressed_size, NULL);
402 	if (b == NULL) {
403 		archive_set_error(&self->archive->archive,
404 		    ARCHIVE_ERRNO_FILE_FORMAT, "Truncated lzop data");
405 		return (ARCHIVE_FATAL);
406 	}
407 	if (state->flags & CRC32_COMPRESSED)
408 		cksum = crc32(crc32(0, NULL, 0), b, state->compressed_size);
409 	else if (state->flags & ADLER32_COMPRESSED)
410 		cksum = adler32(adler32(0, NULL, 0), b, state->compressed_size);
411 	else
412 		cksum = state->compressed_cksum;
413 	if (cksum != state->compressed_cksum) {
414 		archive_set_error(&self->archive->archive,
415 		    ARCHIVE_ERRNO_MISC, "Corrupted data");
416 		return (ARCHIVE_FATAL);
417 	}
418 
419 	/*
420 	 * If the both uncompressed size and compressed size are the same,
421 	 * we do not decompress this block.
422 	 */
423 	if (state->uncompressed_size == state->compressed_size) {
424 		*p = b;
425 		state->total_out += state->compressed_size;
426 		state->unconsumed_bytes = state->compressed_size;
427 		return ((ssize_t)state->uncompressed_size);
428 	}
429 
430 	/*
431 	 * Drive lzo uncompresison.
432 	 */
433 	out_size = (lzo_uint)state->uncompressed_size;
434 	r = lzo1x_decompress_safe(b, (lzo_uint)state->compressed_size,
435 		state->out_block, &out_size, NULL);
436 	switch (r) {
437 	case LZO_E_OK:
438 		if (out_size == state->uncompressed_size)
439 			break;
440 		archive_set_error(&self->archive->archive,
441 		    ARCHIVE_ERRNO_MISC, "Corrupted data");
442 		return (ARCHIVE_FATAL);
443 	case LZO_E_OUT_OF_MEMORY:
444 		archive_set_error(&self->archive->archive, ENOMEM,
445 		    "lzop decompression failed: out of memory");
446 		return (ARCHIVE_FATAL);
447 	default:
448 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
449 		    "lzop decompression failed: %d", r);
450 		return (ARCHIVE_FATAL);
451 	}
452 
453 	if (state->flags & CRC32_UNCOMPRESSED)
454 		cksum = crc32(crc32(0, NULL, 0), state->out_block,
455 		    state->uncompressed_size);
456 	else if (state->flags & ADLER32_UNCOMPRESSED)
457 		cksum = adler32(adler32(0, NULL, 0), state->out_block,
458 		    state->uncompressed_size);
459 	else
460 		cksum = state->uncompressed_cksum;
461 	if (cksum != state->uncompressed_cksum) {
462 		archive_set_error(&self->archive->archive,
463 		    ARCHIVE_ERRNO_MISC, "Corrupted data");
464 		return (ARCHIVE_FATAL);
465 	}
466 
467 	__archive_read_filter_consume(self->upstream, state->compressed_size);
468 	*p = state->out_block;
469 	state->total_out += out_size;
470 	return ((ssize_t)out_size);
471 }
472 
473 /*
474  * Clean up the decompressor.
475  */
476 static int
477 lzop_filter_close(struct archive_read_filter *self)
478 {
479 	struct read_lzop *state = (struct read_lzop *)self->data;
480 
481 	free(state->out_block);
482 	free(state);
483 	return (ARCHIVE_OK);
484 }
485 
486 #endif
487