2 * This file uses XZ Embedded library code which is written
3 * by Lasse Collin <lasse.collin@tukaani.org>
4 * and Igor Pavlov <http://7-zip.org/>
6 * See README file in unxz/ directory for more information.
9 * Copyright (C) 2010 Denys Vlasenko <vda.linux@googlemail.com>
10 * Licensed under GPLv2, see file LICENSE in this source tree.
13 #include "bb_archive.h"
15 #define XZ_FUNC FAST_FUNC
16 #define XZ_EXTERN static
18 #define XZ_DEC_DYNALLOC
20 /* Skip check (rather than fail) of unsupported hash functions */
21 #define XZ_DEC_ANY_CHECK 1
23 /* We use our own crc32 function */
24 #define XZ_INTERNAL_CRC32 0
25 static uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc)
27 return ~crc32_block_endian0(~crc, buf, size, global_crc32_table);
30 /* We use arch-optimized unaligned fixed-endian accessors.
31 * They have been moved to libbb (proved to be useful elsewhere as well),
32 * just check that we have them defined:
34 #if !defined(get_unaligned_le32) \
35 || !defined(get_unaligned_be32) \
36 || !defined(put_unaligned_le32) \
37 || !defined(put_unaligned_be32)
38 # error get_unaligned_le32 accessors are not defined
40 #define get_le32(p) (*(uint32_t*)(p))
42 #include "unxz/xz_dec_bcj.c"
43 #include "unxz/xz_dec_lzma2.c"
44 #include "unxz/xz_dec_stream.c"
46 IF_DESKTOP(long long) int FAST_FUNC
47 unpack_xz_stream(transformer_state_t *xstate)
49 enum xz_ret xz_result;
52 unsigned char *membuf;
53 IF_DESKTOP(long long) int total = 0;
55 if (!global_crc32_table)
56 global_crc32_table = crc32_filltable(NULL, /*endian:*/ 0);
58 memset(&iobuf, 0, sizeof(iobuf));
59 membuf = xmalloc(2 * BUFSIZ);
61 iobuf.out = membuf + BUFSIZ;
62 iobuf.out_size = BUFSIZ;
64 if (!xstate || xstate->signature_skipped) {
65 /* Preload XZ file signature */
66 strcpy((char*)membuf, HEADER_MAGIC);
67 iobuf.in_size = HEADER_MAGIC_SIZE;
68 } /* else: let xz code read & check it */
70 /* Limit memory usage to about 64 MiB. */
71 state = xz_dec_init(XZ_DYNALLOC, 64*1024*1024);
75 if (iobuf.in_pos == iobuf.in_size) {
76 int rd = safe_read(xstate->src_fd, membuf, BUFSIZ);
78 bb_error_msg(bb_msg_read_error);
82 if (rd == 0 && xz_result == XZ_STREAM_END)
87 if (xz_result == XZ_STREAM_END) {
89 * Try to start decoding next concatenated stream.
90 * Stream padding must always be a multiple of four
91 * bytes to preserve four-byte alignment. To keep the
92 * code slightly smaller, we aren't as strict here as
93 * the .xz spec requires. We just skip all zero-bytes
94 * without checking the alignment and thus can accept
95 * files that aren't valid, e.g. the XZ utils test
96 * files bad-0pad-empty.xz and bad-0catpad-empty.xz.
99 if (membuf[iobuf.in_pos] != 0) {
104 } while (iobuf.in_pos < iobuf.in_size);
107 // bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
108 // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
109 xz_result = xz_dec_run(state, &iobuf);
110 // bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
111 // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, xz_result);
113 xtransformer_write(xstate, iobuf.out, iobuf.out_pos);
114 IF_DESKTOP(total += iobuf.out_pos;)
117 if (xz_result == XZ_STREAM_END) {
119 * Can just "break;" here, if not for concatenated
121 * Checking for padding may require buffer
122 * replenishment. Can't do it here.
126 if (xz_result != XZ_OK && xz_result != XZ_UNSUPPORTED_CHECK) {
127 bb_error_msg("corrupted data");