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