blob: 0ae7891605ba2168854bba00220162fdd81b3b10 [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 *
6 * See README file in unxzbz/ directory for more information.
7 *
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
15//#define XZ_DEBUG_MSG(...) bb_error_msg(__VA_ARGS)
16#define XZ_REALLOC_DICT_BUF(ptr, size) xrealloc(ptr, size)
17#define XZ_FUNC FAST_FUNC
18#define XZ_EXTERN static
19
20#define xz_crc32_init(table) crc32_filltable(table, /*endian:*/ 0)
21static uint32_t xz_crc32(uint32_t *crc32_table,
22 const uint8_t *buf, size_t size, uint32_t crc)
23{
24 crc = ~crc;
25
26 while (size != 0) {
27 crc = crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
28 --size;
29 }
30
31 return ~crc;
32}
33#define xz_crc32 xz_crc32
34
35#define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); })
36#define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); })
37#define put_unaligned_le32(val, buf) move_to_unaligned16(buf, SWAP_LE32(val))
38#define put_unaligned_be32(val, buf) move_to_unaligned16(buf, SWAP_BE32(val))
39
40#include "unxz/xz.h"
41#include "unxz/xz_config.h"
42
43#include "unxz/xz_dec_bcj.c"
44#include "unxz/xz_dec_lzma2.c"
45#include "unxz/xz_dec_stream.c"
46#include "unxz/xz_lzma2.h"
47#include "unxz/xz_private.h"
48#include "unxz/xz_stream.h"
49
50IF_DESKTOP(long long) int FAST_FUNC
51unpack_xz_stream_stdin(void)
52{
53 struct xz_buf iobuf;
54 struct xz_dec *state;
55 unsigned char *membuf;
56 IF_DESKTOP(long long) int total = 0;
57 enum {
58 IN_SIZE = 4 * 1024,
59 OUT_SIZE = 60 * 1024,
60 };
61
62 membuf = xmalloc(IN_SIZE + OUT_SIZE);
63 memset(&iobuf, 0, sizeof(iobuf));
64 iobuf.in = membuf;
65 iobuf.out = membuf + IN_SIZE;
66 iobuf.out_size = OUT_SIZE;
67
68 state = xz_dec_init(64*1024); /* initial dict of 64k */
69 xz_crc32_init(state->crc32_table);
70
71 while (1) {
72 enum xz_ret r;
73 int insz, rd, outpos;
74
75 iobuf.in_size -= iobuf.in_pos;
76 insz = iobuf.in_size;
77 if (insz)
78 memmove(membuf, membuf + iobuf.in_pos, insz);
79 iobuf.in_pos = 0;
80 rd = IN_SIZE - insz;
81 if (rd) {
82 rd = safe_read(STDIN_FILENO, membuf + insz, rd);
83 if (rd < 0) {
84 bb_error_msg("read error");
85 total = -1;
86 break;
87 }
88 iobuf.in_size = insz + rd;
89 }
90// bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
91// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
92 r = xz_dec_run(state, &iobuf);
93// bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
94// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, r);
95 outpos = iobuf.out_pos;
96 if (outpos) {
97 xwrite(STDOUT_FILENO, iobuf.out, outpos);
98 IF_DESKTOP(total += outpos;)
99 }
100 if (r == XZ_STREAM_END
101 || (r == XZ_BUF_ERROR && insz == 0 && outpos == 0)
102 ) {
103 break;
104 }
105 if (r != XZ_OK) {
106 bb_error_msg("corrupted data");
107 total = -1;
108 break;
109 }
110 iobuf.out_pos = 0;
111 }
112 xz_dec_end(state);
113 free(membuf);
114
115 return total;
116}