1 /*-
2  * Copyright (c) 2014 Michihiro NAKAJIMA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "archive_platform.h"
27 
28 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #include <stdio.h>
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #ifdef HAVE_LZ4_H
42 #include <lz4.h>
43 #endif
44 
45 #include "archive.h"
46 #include "archive_endian.h"
47 #include "archive_private.h"
48 #include "archive_read_private.h"
49 #include "archive_xxhash.h"
50 
51 #define LZ4_MAGICNUMBER		0x184d2204
52 #define LZ4_SKIPPABLED		0x184d2a50
53 #define LZ4_LEGACY		0x184c2102
54 
55 #if defined(HAVE_LIBLZ4)
56 struct private_data {
57 	enum {  SELECT_STREAM,
58 		READ_DEFAULT_STREAM,
59 		READ_DEFAULT_BLOCK,
60 		READ_LEGACY_STREAM,
61 		READ_LEGACY_BLOCK,
62 	}		stage;
63 	struct {
64 		unsigned block_independence:1;
65 		unsigned block_checksum:3;
66 		unsigned stream_size:1;
67 		unsigned stream_checksum:1;
68 		unsigned preset_dictionary:1;
69 		int	 block_maximum_size;
70 	} flags;
71 	int64_t		 stream_size;
72 	uint32_t	 dict_id;
73 	char		*out_block;
74 	size_t		 out_block_size;
75 
76 	/* Bytes read but not yet consumed via __archive_read_consume() */
77 	size_t		 unconsumed;
78 	size_t		 decoded_size;
79 	void		*xxh32_state;
80 
81 	char		 valid; /* True = decompressor is initialized */
82 	char		 eof; /* True = found end of compressed data. */
83 };
84 
85 #define LEGACY_BLOCK_SIZE	(8 * 1024 * 1024)
86 
87 /* Lz4 filter */
88 static ssize_t	lz4_filter_read(struct archive_read_filter *, const void **);
89 static int	lz4_filter_close(struct archive_read_filter *);
90 #endif
91 
92 /*
93  * Note that we can detect lz4 archives even if we can't decompress
94  * them.  (In fact, we like detecting them because we can give better
95  * error messages.)  So the bid framework here gets compiled even
96  * if liblz4 is unavailable.
97  */
98 static int	lz4_reader_bid(struct archive_read_filter_bidder *, struct archive_read_filter *);
99 static int	lz4_reader_init(struct archive_read_filter *);
100 #if defined(HAVE_LIBLZ4)
101 static ssize_t  lz4_filter_read_default_stream(struct archive_read_filter *,
102 		    const void **);
103 static ssize_t  lz4_filter_read_legacy_stream(struct archive_read_filter *,
104 		    const void **);
105 #endif
106 
107 static const struct archive_read_filter_bidder_vtable
108 lz4_bidder_vtable = {
109 	.bid = lz4_reader_bid,
110 	.init = lz4_reader_init,
111 };
112 
113 int
114 archive_read_support_filter_lz4(struct archive *_a)
115 {
116 	struct archive_read *a = (struct archive_read *)_a;
117 
118 	if (__archive_read_register_bidder(a, NULL, "lz4",
119 				&lz4_bidder_vtable) != ARCHIVE_OK)
120 		return (ARCHIVE_FATAL);
121 
122 #if defined(HAVE_LIBLZ4)
123 	return (ARCHIVE_OK);
124 #else
125 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
126 	    "Using external lz4 program");
127 	return (ARCHIVE_WARN);
128 #endif
129 }
130 
131 /*
132  * Test whether we can handle this data.
133  *
134  * This logic returns zero if any part of the signature fails.  It
135  * also tries to Do The Right Thing if a very short buffer prevents us
136  * from verifying as much as we would like.
137  */
138 static int
139 lz4_reader_bid(struct archive_read_filter_bidder *self,
140     struct archive_read_filter *filter)
141 {
142 	const unsigned char *buffer;
143 	ssize_t avail;
144 	int bits_checked;
145 	uint32_t number;
146 
147 	(void)self; /* UNUSED */
148 
149 	/* Minimal lz4 archive is 11 bytes. */
150 	buffer = __archive_read_filter_ahead(filter, 11, &avail);
151 	if (buffer == NULL)
152 		return (0);
153 
154 	/* First four bytes must be LZ4 magic numbers. */
155 	bits_checked = 0;
156 	if ((number = archive_le32dec(buffer)) == LZ4_MAGICNUMBER) {
157 		unsigned char flag, BD;
158 
159 		bits_checked += 32;
160 		/* Next follows a stream descriptor. */
161 		/* Descriptor Flags. */
162 		flag = buffer[4];
163 		/* A version number must be "01". */
164 		if (((flag & 0xc0) >> 6) != 1)
165 			return (0);
166 		/* A reserved bit must be "0". */
167 		if (flag & 2)
168 			return (0);
169 		bits_checked += 8;
170 		BD = buffer[5];
171 		/* A block maximum size should be more than 3. */
172 		if (((BD & 0x70) >> 4) < 4)
173 			return (0);
174 		/* Reserved bits must be "0". */
175 		if (BD & ~0x70)
176 			return (0);
177 		bits_checked += 8;
178 	} else if (number == LZ4_LEGACY) {
179 		bits_checked += 32;
180 	}
181 
182 	return (bits_checked);
183 }
184 
185 #if !defined(HAVE_LIBLZ4)
186 
187 /*
188  * If we don't have the library on this system, we can't actually do the
189  * decompression.  We can, however, still detect compressed archives
190  * and emit a useful message.
191  */
192 static int
193 lz4_reader_init(struct archive_read_filter *self)
194 {
195 	int r;
196 
197 	r = __archive_read_program(self, "lz4 -d -q");
198 	/* Note: We set the format here even if __archive_read_program()
199 	 * above fails.  We do, after all, know what the format is
200 	 * even if we weren't able to read it. */
201 	self->code = ARCHIVE_FILTER_LZ4;
202 	self->name = "lz4";
203 	return (r);
204 }
205 
206 
207 #else
208 
209 static const struct archive_read_filter_vtable
210 lz4_reader_vtable = {
211 	.read = lz4_filter_read,
212 	.close = lz4_filter_close,
213 };
214 
215 /*
216  * Setup the callbacks.
217  */
218 static int
219 lz4_reader_init(struct archive_read_filter *self)
220 {
221 	struct private_data *state;
222 
223 	self->code = ARCHIVE_FILTER_LZ4;
224 	self->name = "lz4";
225 
226 	state = (struct private_data *)calloc(1, sizeof(*state));
227 	if (state == NULL) {
228 		archive_set_error(&self->archive->archive, ENOMEM,
229 		    "Can't allocate data for lz4 decompression");
230 		return (ARCHIVE_FATAL);
231 	}
232 
233 	self->data = state;
234 	state->stage = SELECT_STREAM;
235 	self->vtable = &lz4_reader_vtable;
236 
237 	return (ARCHIVE_OK);
238 }
239 
240 static int
241 lz4_allocate_out_block(struct archive_read_filter *self)
242 {
243 	struct private_data *state = (struct private_data *)self->data;
244 	size_t out_block_size = state->flags.block_maximum_size;
245 	void *out_block;
246 
247 	if (!state->flags.block_independence)
248 		out_block_size += 64 * 1024;
249 	if (state->out_block_size < out_block_size) {
250 		free(state->out_block);
251 		out_block = (unsigned char *)malloc(out_block_size);
252 		state->out_block_size = out_block_size;
253 		if (out_block == NULL) {
254 			archive_set_error(&self->archive->archive, ENOMEM,
255 			    "Can't allocate data for lz4 decompression");
256 			return (ARCHIVE_FATAL);
257 		}
258 		state->out_block = out_block;
259 	}
260 	if (!state->flags.block_independence)
261 		memset(state->out_block, 0, 64 * 1024);
262 	return (ARCHIVE_OK);
263 }
264 
265 static int
266 lz4_allocate_out_block_for_legacy(struct archive_read_filter *self)
267 {
268 	struct private_data *state = (struct private_data *)self->data;
269 	size_t out_block_size = LEGACY_BLOCK_SIZE;
270 	void *out_block;
271 
272 	if (state->out_block_size < out_block_size) {
273 		free(state->out_block);
274 		out_block = (unsigned char *)malloc(out_block_size);
275 		state->out_block_size = out_block_size;
276 		if (out_block == NULL) {
277 			archive_set_error(&self->archive->archive, ENOMEM,
278 			    "Can't allocate data for lz4 decompression");
279 			return (ARCHIVE_FATAL);
280 		}
281 		state->out_block = out_block;
282 	}
283 	return (ARCHIVE_OK);
284 }
285 
286 /*
287  * Return the next block of decompressed data.
288  */
289 static ssize_t
290 lz4_filter_read(struct archive_read_filter *self, const void **p)
291 {
292 	struct private_data *state = (struct private_data *)self->data;
293 	ssize_t ret;
294 
295 	if (state->eof) {
296 		*p = NULL;
297 		return (0);
298 	}
299 
300 	__archive_read_filter_consume(self->upstream, state->unconsumed);
301 	state->unconsumed = 0;
302 
303 	switch (state->stage) {
304 	case SELECT_STREAM:
305 		break;
306 	case READ_DEFAULT_STREAM:
307 	case READ_LEGACY_STREAM:
308 		/* Reading a lz4 stream already failed. */
309 		archive_set_error(&self->archive->archive,
310 		    ARCHIVE_ERRNO_MISC, "Invalid sequence.");
311 		return (ARCHIVE_FATAL);
312 	case READ_DEFAULT_BLOCK:
313 		ret = lz4_filter_read_default_stream(self, p);
314 		if (ret != 0 || state->stage != SELECT_STREAM)
315 			return ret;
316 		break;
317 	case READ_LEGACY_BLOCK:
318 		ret = lz4_filter_read_legacy_stream(self, p);
319 		if (ret != 0 || state->stage != SELECT_STREAM)
320 			return ret;
321 		break;
322 	default:
323 		archive_set_error(&self->archive->archive,
324 		    ARCHIVE_ERRNO_MISC, "Program error.");
325 		return (ARCHIVE_FATAL);
326 		break;
327 	}
328 
329 	while (state->stage == SELECT_STREAM) {
330 		const char *read_buf;
331 
332 		/* Read a magic number. */
333 		read_buf = __archive_read_filter_ahead(self->upstream, 4,
334 				NULL);
335 		if (read_buf == NULL) {
336 			state->eof = 1;
337 			*p = NULL;
338 			return (0);
339 		}
340 		uint32_t number = archive_le32dec(read_buf);
341 		__archive_read_filter_consume(self->upstream, 4);
342 		if (number == LZ4_MAGICNUMBER)
343 			return lz4_filter_read_default_stream(self, p);
344 		else if (number == LZ4_LEGACY)
345 			return lz4_filter_read_legacy_stream(self, p);
346 		else if ((number & ~0xF) == LZ4_SKIPPABLED) {
347 			read_buf = __archive_read_filter_ahead(
348 				self->upstream, 4, NULL);
349 			if (read_buf == NULL) {
350 				archive_set_error(
351 				    &self->archive->archive,
352 		    		    ARCHIVE_ERRNO_MISC,
353 				    "Malformed lz4 data");
354 				return (ARCHIVE_FATAL);
355 			}
356 			uint32_t skip_bytes = archive_le32dec(read_buf);
357 			__archive_read_filter_consume(self->upstream,
358 				4 + skip_bytes);
359 		} else {
360 			/* Ignore following unrecognized data. */
361 			state->eof = 1;
362 			*p = NULL;
363 			return (0);
364 		}
365 	}
366 	state->eof = 1;
367 	*p = NULL;
368 	return (0);
369 }
370 
371 static int
372 lz4_filter_read_descriptor(struct archive_read_filter *self)
373 {
374 	struct private_data *state = (struct private_data *)self->data;
375 	const char *read_buf;
376 	ssize_t bytes_remaining;
377 	ssize_t descriptor_bytes;
378 	unsigned char flag, bd;
379 	unsigned int chsum, chsum_verifier;
380 
381 	/* Make sure we have 2 bytes for flags. */
382 	read_buf = __archive_read_filter_ahead(self->upstream, 2,
383 	    &bytes_remaining);
384 	if (read_buf == NULL) {
385 		archive_set_error(&self->archive->archive,
386 		    ARCHIVE_ERRNO_MISC,
387 		    "truncated lz4 input");
388 		return (ARCHIVE_FATAL);
389 	}
390 
391 	/*
392 	   Parse flags.
393 	 */
394 	flag = (unsigned char)read_buf[0];
395 	/* Verify version number. */
396 	if ((flag & 0xc0) != 1<<6)
397 		goto malformed_error;
398 	/* A reserved bit must be zero. */
399 	if (flag & 0x02)
400 		goto malformed_error;
401 	state->flags.block_independence = (flag & 0x20) != 0;
402 	state->flags.block_checksum = (flag & 0x10)?4:0;
403 	state->flags.stream_size = (flag & 0x08) != 0;
404 	state->flags.stream_checksum = (flag & 0x04) != 0;
405 	state->flags.preset_dictionary = (flag & 0x01) != 0;
406 
407 	/* BD */
408 	bd = (unsigned char)read_buf[1];
409 	/* Reserved bits must be zero. */
410 	if (bd & 0x8f)
411 		goto malformed_error;
412 	/* Get a maximum block size. */
413 	switch (read_buf[1] >> 4) {
414 	case 4: /* 64 KB */
415 		state->flags.block_maximum_size = 64 * 1024;
416 		break;
417 	case 5: /* 256 KB */
418 		state->flags.block_maximum_size = 256 * 1024;
419 		break;
420 	case 6: /* 1 MB */
421 		state->flags.block_maximum_size = 1024 * 1024;
422 		break;
423 	case 7: /* 4 MB */
424 		state->flags.block_maximum_size = 4 * 1024 * 1024;
425 		break;
426 	default:
427 		goto malformed_error;
428 	}
429 
430 	/* Read the whole descriptor in a stream block. */
431 	descriptor_bytes = 3;
432 	if (state->flags.stream_size)
433 		descriptor_bytes += 8;
434 	if (state->flags.preset_dictionary)
435 		descriptor_bytes += 4;
436 	if (bytes_remaining < descriptor_bytes) {
437 		read_buf = __archive_read_filter_ahead(self->upstream,
438 		    descriptor_bytes, &bytes_remaining);
439 		if (read_buf == NULL) {
440 			archive_set_error(&self->archive->archive,
441 			    ARCHIVE_ERRNO_MISC,
442 			    "truncated lz4 input");
443 			return (ARCHIVE_FATAL);
444 		}
445 	}
446 	/* Check if a descriptor is corrupted */
447 	chsum = __archive_xxhash.XXH32(read_buf, (int)descriptor_bytes -1, 0);
448 	chsum = (chsum >> 8) & 0xff;
449 	chsum_verifier = read_buf[descriptor_bytes-1] & 0xff;
450 #ifndef DONT_FAIL_ON_CRC_ERROR
451 	if (chsum != chsum_verifier)
452 		goto malformed_error;
453 #endif
454 
455 	__archive_read_filter_consume(self->upstream, descriptor_bytes);
456 
457 	/* Make sure we have a large enough buffer for uncompressed data. */
458 	if (lz4_allocate_out_block(self) != ARCHIVE_OK)
459 		return (ARCHIVE_FATAL);
460 	if (state->flags.stream_checksum)
461 		state->xxh32_state = __archive_xxhash.XXH32_init(0);
462 
463 	state->decoded_size = 0;
464 	/* Success */
465 	return (ARCHIVE_OK);
466 malformed_error:
467 	archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
468 	    "malformed lz4 data");
469 	return (ARCHIVE_FATAL);
470 }
471 
472 static ssize_t
473 lz4_filter_read_data_block(struct archive_read_filter *self, const void **p)
474 {
475 	struct private_data *state = (struct private_data *)self->data;
476 	ssize_t compressed_size;
477 	const char *read_buf;
478 	ssize_t bytes_remaining;
479 	int checksum_size;
480 	ssize_t uncompressed_size;
481 	size_t prefix64k;
482 
483 	*p = NULL;
484 
485 	/* Make sure we have 4 bytes for a block size. */
486 	read_buf = __archive_read_filter_ahead(self->upstream, 4,
487 	    &bytes_remaining);
488 	if (read_buf == NULL)
489 		goto truncated_error;
490 	compressed_size = archive_le32dec(read_buf);
491 	if ((compressed_size & 0x7fffffff) > state->flags.block_maximum_size)
492 		goto malformed_error;
493 	/* A compressed size == 0 means the end of stream blocks. */
494 	if (compressed_size == 0) {
495 		__archive_read_filter_consume(self->upstream, 4);
496 		return 0;
497 	}
498 
499 	checksum_size = state->flags.block_checksum;
500 	/* Check if the block is uncompressed. */
501 	if (compressed_size & 0x80000000U) {
502 		compressed_size &= 0x7fffffff;
503 		uncompressed_size = compressed_size;
504 	} else
505 		uncompressed_size = 0;/* Unknown yet. */
506 
507 	/*
508 	  Unfortunately, lz4 decompression API requires a whole block
509 	  for its decompression speed, so we read a whole block and allocate
510 	  a huge buffer used for decoded data.
511 	*/
512 	read_buf = __archive_read_filter_ahead(self->upstream,
513 	    4 + compressed_size + checksum_size, &bytes_remaining);
514 	if (read_buf == NULL)
515 		goto truncated_error;
516 
517 	/* Optional processing, checking a block sum. */
518 	if (checksum_size) {
519 		unsigned int chsum = __archive_xxhash.XXH32(
520 			read_buf + 4, (int)compressed_size, 0);
521 		unsigned int chsum_block =
522 		    archive_le32dec(read_buf + 4 + compressed_size);
523 #ifndef DONT_FAIL_ON_CRC_ERROR
524 		if (chsum != chsum_block)
525 			goto malformed_error;
526 #endif
527 	}
528 
529 
530 	/* If the block is uncompressed, there is nothing to do. */
531 	if (uncompressed_size) {
532 		/* Prepare a prefix 64k block for next block. */
533 		if (!state->flags.block_independence) {
534 			prefix64k = 64 * 1024;
535 			if (uncompressed_size < (ssize_t)prefix64k) {
536 				memcpy(state->out_block
537 					+ prefix64k - uncompressed_size,
538 				    read_buf + 4,
539 				    uncompressed_size);
540 				memset(state->out_block, 0,
541 				    prefix64k - uncompressed_size);
542 			} else {
543 				memcpy(state->out_block,
544 				    read_buf + 4
545 					+ uncompressed_size - prefix64k,
546 				    prefix64k);
547 			}
548 			state->decoded_size = 0;
549 		}
550 		state->unconsumed = 4 + uncompressed_size + checksum_size;
551 		*p = read_buf + 4;
552 		return uncompressed_size;
553 	}
554 
555 	/*
556 	   Decompress a block data.
557 	 */
558 	if (state->flags.block_independence) {
559 		prefix64k = 0;
560 		uncompressed_size = LZ4_decompress_safe(read_buf + 4,
561 		    state->out_block, (int)compressed_size,
562 		    state->flags.block_maximum_size);
563 	} else {
564 		prefix64k = 64 * 1024;
565 		if (state->decoded_size) {
566 			if (state->decoded_size < prefix64k) {
567 				memmove(state->out_block
568 					+ prefix64k - state->decoded_size,
569 				    state->out_block + prefix64k,
570 				    state->decoded_size);
571 				memset(state->out_block, 0,
572 				    prefix64k - state->decoded_size);
573 			} else {
574 				memmove(state->out_block,
575 				    state->out_block + state->decoded_size,
576 				    prefix64k);
577 			}
578 		}
579 #if LZ4_VERSION_MAJOR >= 1 && LZ4_VERSION_MINOR >= 7
580 		uncompressed_size = LZ4_decompress_safe_usingDict(
581 		    read_buf + 4,
582 		    state->out_block + prefix64k, (int)compressed_size,
583 		    state->flags.block_maximum_size,
584 		    state->out_block,
585 		    (int)prefix64k);
586 #else
587 		uncompressed_size = LZ4_decompress_safe_withPrefix64k(
588 		    read_buf + 4,
589 		    state->out_block + prefix64k, (int)compressed_size,
590 		    state->flags.block_maximum_size);
591 #endif
592 	}
593 
594 	/* Check if an error occurred in the decompression process. */
595 	if (uncompressed_size < 0) {
596 		archive_set_error(&(self->archive->archive),
597 		    ARCHIVE_ERRNO_MISC, "lz4 decompression failed");
598 		return (ARCHIVE_FATAL);
599 	}
600 
601 	state->unconsumed = 4 + compressed_size + checksum_size;
602 	*p = state->out_block + prefix64k;
603 	state->decoded_size = uncompressed_size;
604 	return uncompressed_size;
605 
606 malformed_error:
607 	archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
608 	    "malformed lz4 data");
609 	return (ARCHIVE_FATAL);
610 truncated_error:
611 	archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
612 	    "truncated lz4 input");
613 	return (ARCHIVE_FATAL);
614 }
615 
616 static ssize_t
617 lz4_filter_read_default_stream(struct archive_read_filter *self, const void **p)
618 {
619 	struct private_data *state = (struct private_data *)self->data;
620 	const char *read_buf;
621 	ssize_t bytes_remaining;
622 	ssize_t ret;
623 
624 	if (state->stage == SELECT_STREAM) {
625 		state->stage = READ_DEFAULT_STREAM;
626 		/* First, read a descriptor. */
627 		if((ret = lz4_filter_read_descriptor(self)) != ARCHIVE_OK)
628 			return (ret);
629 		state->stage = READ_DEFAULT_BLOCK;
630 	}
631 	/* Decompress a block. */
632 	ret = lz4_filter_read_data_block(self, p);
633 
634 	/* If the end of block is detected, change the filter status
635 	   to read next stream. */
636 	if (ret == 0 && *p == NULL)
637 		state->stage = SELECT_STREAM;
638 
639 	/* Optional processing, checking a stream sum. */
640 	if (state->flags.stream_checksum) {
641 		if (state->stage == SELECT_STREAM) {
642 			unsigned int checksum;
643 			unsigned int checksum_stream;
644 			read_buf = __archive_read_filter_ahead(self->upstream,
645 			    4, &bytes_remaining);
646 			if (read_buf == NULL) {
647 				archive_set_error(&self->archive->archive,
648 				    ARCHIVE_ERRNO_MISC, "truncated lz4 input");
649 				return (ARCHIVE_FATAL);
650 			}
651 			checksum = archive_le32dec(read_buf);
652 			__archive_read_filter_consume(self->upstream, 4);
653 			checksum_stream = __archive_xxhash.XXH32_digest(
654 			    state->xxh32_state);
655 			state->xxh32_state = NULL;
656 			if (checksum != checksum_stream) {
657 #ifndef DONT_FAIL_ON_CRC_ERROR
658 				archive_set_error(&self->archive->archive,
659 				    ARCHIVE_ERRNO_MISC,
660 				    "lz4 stream checksum error");
661 				return (ARCHIVE_FATAL);
662 #endif
663 			}
664 		} else if (ret > 0)
665 			__archive_xxhash.XXH32_update(state->xxh32_state,
666 			    *p, (int)ret);
667 	}
668 	return (ret);
669 }
670 
671 static ssize_t
672 lz4_filter_read_legacy_stream(struct archive_read_filter *self, const void **p)
673 {
674 	struct private_data *state = (struct private_data *)self->data;
675 	uint32_t compressed;
676 	const char *read_buf;
677 	ssize_t ret;
678 
679 	*p = NULL;
680 	ret = lz4_allocate_out_block_for_legacy(self);
681 	if (ret != ARCHIVE_OK)
682 		return ret;
683 
684 	/* Make sure we have 4 bytes for a block size. */
685 	read_buf = __archive_read_filter_ahead(self->upstream, 4, NULL);
686 	if (read_buf == NULL) {
687 		if (state->stage == SELECT_STREAM) {
688 			state->stage = READ_LEGACY_STREAM;
689 			archive_set_error(&self->archive->archive,
690 			    ARCHIVE_ERRNO_MISC,
691 			    "truncated lz4 input");
692 			return (ARCHIVE_FATAL);
693 		}
694 		state->stage = SELECT_STREAM;
695 		return 0;
696 	}
697 	state->stage = READ_LEGACY_BLOCK;
698 	compressed = archive_le32dec(read_buf);
699 	if (compressed > LZ4_COMPRESSBOUND(LEGACY_BLOCK_SIZE)) {
700 		state->stage = SELECT_STREAM;
701 		return 0;
702 	}
703 
704 	/* Make sure we have a whole block. */
705 	read_buf = __archive_read_filter_ahead(self->upstream,
706 	    4 + compressed, NULL);
707 	if (read_buf == NULL) {
708 		archive_set_error(&(self->archive->archive),
709 		    ARCHIVE_ERRNO_MISC, "truncated lz4 input");
710 		return (ARCHIVE_FATAL);
711 	}
712 	ret = LZ4_decompress_safe(read_buf + 4, state->out_block,
713 	    compressed, (int)state->out_block_size);
714 	if (ret < 0) {
715 		archive_set_error(&(self->archive->archive),
716 		    ARCHIVE_ERRNO_MISC, "lz4 decompression failed");
717 		return (ARCHIVE_FATAL);
718 	}
719 	*p = state->out_block;
720 	state->unconsumed = 4 + compressed;
721 	return ret;
722 }
723 
724 /*
725  * Clean up the decompressor.
726  */
727 static int
728 lz4_filter_close(struct archive_read_filter *self)
729 {
730 	struct private_data *state;
731 	int ret = ARCHIVE_OK;
732 
733 	state = (struct private_data *)self->data;
734 	free(state->xxh32_state);
735 	free(state->out_block);
736 	free(state);
737 	return (ret);
738 }
739 
740 #endif /* HAVE_LIBLZ4 */
741