blob: 79b48a152e77e440060f53fd379945f7741adf0e [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 Vlasenko716f3f62010-06-01 14:41:39 +020030/* We use arch-optimized unaligned accessors */
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020031#define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); })
32#define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); })
33#define put_unaligned_le32(val, buf) move_to_unaligned16(buf, SWAP_LE32(val))
34#define put_unaligned_be32(val, buf) move_to_unaligned16(buf, SWAP_BE32(val))
35
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020036#include "unxz/xz_dec_bcj.c"
37#include "unxz/xz_dec_lzma2.c"
38#include "unxz/xz_dec_stream.c"
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020039
40IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010041unpack_xz_stream(transformer_aux_data_t *aux, int src_fd, int dst_fd)
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020042{
43 struct xz_buf iobuf;
44 struct xz_dec *state;
45 unsigned char *membuf;
46 IF_DESKTOP(long long) int total = 0;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020047
Denys Vlasenko9ce642f2010-10-27 15:26:45 +020048 if (!global_crc32_table)
49 global_crc32_table = crc32_filltable(NULL, /*endian:*/ 0);
Denys Vlasenko8376bfa2010-06-01 23:26:54 +020050
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020051 memset(&iobuf, 0, sizeof(iobuf));
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010052 membuf = xmalloc(2 * BUFSIZ);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020053 iobuf.in = membuf;
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020054 iobuf.out = membuf + BUFSIZ;
55 iobuf.out_size = BUFSIZ;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020056
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010057 if (!aux || aux->check_signature == 0) {
58 /* Preload XZ file signature */
59 strcpy((char*)membuf, HEADER_MAGIC);
60 iobuf.in_size = HEADER_MAGIC_SIZE;
61 } /* else: let xz code read & check it */
62
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020063 /* Limit memory usage to about 64 MiB. */
64 state = xz_dec_init(XZ_DYNALLOC, 64*1024*1024);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020065
66 while (1) {
67 enum xz_ret r;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020068
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020069 if (iobuf.in_pos == iobuf.in_size) {
70 int rd = safe_read(src_fd, membuf, BUFSIZ);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020071 if (rd < 0) {
Denys Vlasenkoea8b2522010-06-02 12:57:26 +020072 bb_error_msg(bb_msg_read_error);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020073 total = -1;
74 break;
75 }
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020076 iobuf.in_size = rd;
77 iobuf.in_pos = 0;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020078 }
79// bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
80// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
81 r = xz_dec_run(state, &iobuf);
82// bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
83// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, r);
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020084 if (iobuf.out_pos) {
85 xwrite(dst_fd, iobuf.out, iobuf.out_pos);
86 IF_DESKTOP(total += iobuf.out_pos;)
87 iobuf.out_pos = 0;
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020088 }
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020089 if (r == XZ_STREAM_END) {
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020090 break;
91 }
Denys Vlasenko716f3f62010-06-01 14:41:39 +020092 if (r != XZ_OK && r != XZ_UNSUPPORTED_CHECK) {
Denys Vlasenkod2b738a2010-06-21 02:16:51 +020093 bb_error_msg("corrupted data");
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020094 total = -1;
95 break;
96 }
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020097 }
98 xz_dec_end(state);
99 free(membuf);
100
101 return total;
102}