blob: 350e5358a6fa62eb3f8a7443161550fc6e9a6a15 [file] [log] [blame]
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +02001/*
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/>
5 *
Denys Vlasenko6948f212010-05-30 04:18:13 +02006 * See README file in unxz/ directory for more information.
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +02007 *
8 * This file is:
9 * Copyright (C) 2010 Denys Vlasenko <vda.linux@googlemail.com>
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2, see file LICENSE in this source tree.
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020011 */
12#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020013#include "bb_archive.h"
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020014
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020015#define XZ_FUNC FAST_FUNC
16#define XZ_EXTERN static
17
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020018#define XZ_DEC_DYNALLOC
19
Denys Vlasenko716f3f62010-06-01 14:41:39 +020020/* Skip check (rather than fail) of unsupported hash functions */
21#define XZ_DEC_ANY_CHECK 1
22
23/* We use our own crc32 function */
24#define XZ_INTERNAL_CRC32 0
Denys Vlasenko716f3f62010-06-01 14:41:39 +020025static uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc)
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020026{
Denys Vlasenko9ce642f2010-10-27 15:26:45 +020027 return ~crc32_block_endian0(~crc, buf, size, global_crc32_table);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020028}
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020029
Denys Vlasenko2c1258c2017-07-15 20:22:25 +020030/* 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:
33 */
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
39#endif
40#define get_le32(p) (*(uint32_t*)(p))
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020041
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020042#include "unxz/xz_dec_bcj.c"
43#include "unxz/xz_dec_lzma2.c"
44#include "unxz/xz_dec_stream.c"
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020045
46IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenkob4c11c12014-12-07 00:44:00 +010047unpack_xz_stream(transformer_state_t *xstate)
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020048{
Lasse Collin380c8a02013-02-27 17:26:40 +010049 enum xz_ret xz_result;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020050 struct xz_buf iobuf;
51 struct xz_dec *state;
52 unsigned char *membuf;
53 IF_DESKTOP(long long) int total = 0;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020054
Denys Vlasenko9ce642f2010-10-27 15:26:45 +020055 if (!global_crc32_table)
56 global_crc32_table = crc32_filltable(NULL, /*endian:*/ 0);
Denys Vlasenko8376bfa2010-06-01 23:26:54 +020057
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020058 memset(&iobuf, 0, sizeof(iobuf));
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010059 membuf = xmalloc(2 * BUFSIZ);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020060 iobuf.in = membuf;
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020061 iobuf.out = membuf + BUFSIZ;
62 iobuf.out_size = BUFSIZ;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020063
Denys Vlasenko984b0a62016-06-20 11:06:42 +020064 if (!xstate || xstate->signature_skipped) {
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010065 /* 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 */
69
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020070 /* Limit memory usage to about 64 MiB. */
71 state = xz_dec_init(XZ_DYNALLOC, 64*1024*1024);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020072
Lasse Collin380c8a02013-02-27 17:26:40 +010073 xz_result = X_OK;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020074 while (1) {
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020075 if (iobuf.in_pos == iobuf.in_size) {
Denys Vlasenkob4c11c12014-12-07 00:44:00 +010076 int rd = safe_read(xstate->src_fd, membuf, BUFSIZ);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020077 if (rd < 0) {
Denys Vlasenkoea8b2522010-06-02 12:57:26 +020078 bb_error_msg(bb_msg_read_error);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020079 total = -1;
80 break;
81 }
Lasse Collin380c8a02013-02-27 17:26:40 +010082 if (rd == 0 && xz_result == XZ_STREAM_END)
83 break;
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020084 iobuf.in_size = rd;
85 iobuf.in_pos = 0;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020086 }
Lasse Collin380c8a02013-02-27 17:26:40 +010087 if (xz_result == XZ_STREAM_END) {
88 /*
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.
97 */
98 do {
99 if (membuf[iobuf.in_pos] != 0) {
100 xz_dec_reset(state);
101 goto do_run;
102 }
103 iobuf.in_pos++;
104 } while (iobuf.in_pos < iobuf.in_size);
105 }
106 do_run:
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200107// 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);
Lasse Collin380c8a02013-02-27 17:26:40 +0100109 xz_result = xz_dec_run(state, &iobuf);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200110// bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
Lasse Collin380c8a02013-02-27 17:26:40 +0100111// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, xz_result);
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200112 if (iobuf.out_pos) {
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100113 xtransformer_write(xstate, iobuf.out, iobuf.out_pos);
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200114 IF_DESKTOP(total += iobuf.out_pos;)
115 iobuf.out_pos = 0;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200116 }
Lasse Collin380c8a02013-02-27 17:26:40 +0100117 if (xz_result == XZ_STREAM_END) {
118 /*
119 * Can just "break;" here, if not for concatenated
120 * .xz streams.
121 * Checking for padding may require buffer
122 * replenishment. Can't do it here.
123 */
124 continue;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200125 }
Lasse Collin380c8a02013-02-27 17:26:40 +0100126 if (xz_result != XZ_OK && xz_result != XZ_UNSUPPORTED_CHECK) {
Denys Vlasenkod2b738a2010-06-21 02:16:51 +0200127 bb_error_msg("corrupted data");
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200128 total = -1;
129 break;
130 }
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200131 }
Lasse Collin380c8a02013-02-27 17:26:40 +0100132
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200133 xz_dec_end(state);
134 free(membuf);
135
136 return total;
137}