blob: 3f939298415e8dd4f094e1b3f3ede0248d1838b9 [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>
10 * Licensed under GPLv2, see file LICENSE in this tarball for details.
11 */
12#include "libbb.h"
13#include "unarchive.h"
14
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020015#define XZ_REALLOC_DICT_BUF(ptr, size) xrealloc(ptr, size)
16#define XZ_FUNC FAST_FUNC
17#define XZ_EXTERN static
18
Denys Vlasenko716f3f62010-06-01 14:41:39 +020019/* Skip check (rather than fail) of unsupported hash functions */
20#define XZ_DEC_ANY_CHECK 1
21
22/* We use our own crc32 function */
23#define XZ_INTERNAL_CRC32 0
24static uint32_t *crc32_table;
25static uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc)
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020026{
27 crc = ~crc;
28
29 while (size != 0) {
30 crc = crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
31 --size;
32 }
33
34 return ~crc;
35}
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020036
Denys Vlasenko716f3f62010-06-01 14:41:39 +020037/* We use arch-optimized unaligned accessors */
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020038#define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); })
39#define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); })
40#define put_unaligned_le32(val, buf) move_to_unaligned16(buf, SWAP_LE32(val))
41#define put_unaligned_be32(val, buf) move_to_unaligned16(buf, SWAP_BE32(val))
42
43#include "unxz/xz.h"
44#include "unxz/xz_config.h"
45
46#include "unxz/xz_dec_bcj.c"
47#include "unxz/xz_dec_lzma2.c"
48#include "unxz/xz_dec_stream.c"
49#include "unxz/xz_lzma2.h"
50#include "unxz/xz_private.h"
51#include "unxz/xz_stream.h"
52
53IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenko6948f212010-05-30 04:18:13 +020054unpack_xz_stream(int src_fd, int dst_fd)
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020055{
56 struct xz_buf iobuf;
57 struct xz_dec *state;
58 unsigned char *membuf;
59 IF_DESKTOP(long long) int total = 0;
60 enum {
61 IN_SIZE = 4 * 1024,
62 OUT_SIZE = 60 * 1024,
63 };
64
Denys Vlasenko8376bfa2010-06-01 23:26:54 +020065 if (!crc32_table)
66 crc32_table = crc32_filltable(NULL, /*endian:*/ 0);
67
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020068 membuf = xmalloc(IN_SIZE + OUT_SIZE);
69 memset(&iobuf, 0, sizeof(iobuf));
70 iobuf.in = membuf;
71 iobuf.out = membuf + IN_SIZE;
72 iobuf.out_size = OUT_SIZE;
73
74 state = xz_dec_init(64*1024); /* initial dict of 64k */
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020075
76 while (1) {
77 enum xz_ret r;
78 int insz, rd, outpos;
79
80 iobuf.in_size -= iobuf.in_pos;
81 insz = iobuf.in_size;
82 if (insz)
83 memmove(membuf, membuf + iobuf.in_pos, insz);
84 iobuf.in_pos = 0;
85 rd = IN_SIZE - insz;
86 if (rd) {
Denys Vlasenko6948f212010-05-30 04:18:13 +020087 rd = safe_read(src_fd, membuf + insz, rd);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020088 if (rd < 0) {
89 bb_error_msg("read error");
90 total = -1;
91 break;
92 }
93 iobuf.in_size = insz + rd;
94 }
95// bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
96// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
97 r = xz_dec_run(state, &iobuf);
98// bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
99// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, r);
100 outpos = iobuf.out_pos;
101 if (outpos) {
Denys Vlasenko6948f212010-05-30 04:18:13 +0200102 xwrite(dst_fd, iobuf.out, outpos);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200103 IF_DESKTOP(total += outpos;)
104 }
105 if (r == XZ_STREAM_END
Denys Vlasenko6948f212010-05-30 04:18:13 +0200106 /* this happens even with well-formed files: */
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200107 || (r == XZ_BUF_ERROR && insz == 0 && outpos == 0)
108 ) {
109 break;
110 }
Denys Vlasenko716f3f62010-06-01 14:41:39 +0200111 if (r != XZ_OK && r != XZ_UNSUPPORTED_CHECK) {
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200112 bb_error_msg("corrupted data");
113 total = -1;
114 break;
115 }
116 iobuf.out_pos = 0;
117 }
118 xz_dec_end(state);
119 free(membuf);
120
121 return total;
122}