blob: 924a52513a9ba7f9b5960271e5b6376a60f9ea50 [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
19#define xz_crc32_init(table) crc32_filltable(table, /*endian:*/ 0)
20static uint32_t xz_crc32(uint32_t *crc32_table,
21 const uint8_t *buf, size_t size, uint32_t crc)
22{
23 crc = ~crc;
24
25 while (size != 0) {
26 crc = crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
27 --size;
28 }
29
30 return ~crc;
31}
32#define xz_crc32 xz_crc32
33
34#define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); })
35#define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); })
36#define put_unaligned_le32(val, buf) move_to_unaligned16(buf, SWAP_LE32(val))
37#define put_unaligned_be32(val, buf) move_to_unaligned16(buf, SWAP_BE32(val))
38
39#include "unxz/xz.h"
40#include "unxz/xz_config.h"
41
42#include "unxz/xz_dec_bcj.c"
43#include "unxz/xz_dec_lzma2.c"
44#include "unxz/xz_dec_stream.c"
45#include "unxz/xz_lzma2.h"
46#include "unxz/xz_private.h"
47#include "unxz/xz_stream.h"
48
49IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenko6948f212010-05-30 04:18:13 +020050unpack_xz_stream(int src_fd, int dst_fd)
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020051{
52 struct xz_buf iobuf;
53 struct xz_dec *state;
54 unsigned char *membuf;
55 IF_DESKTOP(long long) int total = 0;
56 enum {
57 IN_SIZE = 4 * 1024,
58 OUT_SIZE = 60 * 1024,
59 };
60
61 membuf = xmalloc(IN_SIZE + OUT_SIZE);
62 memset(&iobuf, 0, sizeof(iobuf));
63 iobuf.in = membuf;
64 iobuf.out = membuf + IN_SIZE;
65 iobuf.out_size = OUT_SIZE;
66
67 state = xz_dec_init(64*1024); /* initial dict of 64k */
68 xz_crc32_init(state->crc32_table);
69
70 while (1) {
71 enum xz_ret r;
72 int insz, rd, outpos;
73
74 iobuf.in_size -= iobuf.in_pos;
75 insz = iobuf.in_size;
76 if (insz)
77 memmove(membuf, membuf + iobuf.in_pos, insz);
78 iobuf.in_pos = 0;
79 rd = IN_SIZE - insz;
80 if (rd) {
Denys Vlasenko6948f212010-05-30 04:18:13 +020081 rd = safe_read(src_fd, membuf + insz, rd);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020082 if (rd < 0) {
83 bb_error_msg("read error");
84 total = -1;
85 break;
86 }
87 iobuf.in_size = insz + rd;
88 }
89// bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
90// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
91 r = xz_dec_run(state, &iobuf);
92// bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
93// iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, r);
94 outpos = iobuf.out_pos;
95 if (outpos) {
Denys Vlasenko6948f212010-05-30 04:18:13 +020096 xwrite(dst_fd, iobuf.out, outpos);
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +020097 IF_DESKTOP(total += outpos;)
98 }
99 if (r == XZ_STREAM_END
Denys Vlasenko6948f212010-05-30 04:18:13 +0200100 /* this happens even with well-formed files: */
Denys Vlasenkofb6c76c2010-05-30 03:47:40 +0200101 || (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}