Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 1 | /* |
| 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 Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame^] | 6 | * See README file in unxz/ directory for more information. |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 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) |
| 21 | static 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 | |
| 50 | IF_DESKTOP(long long) int FAST_FUNC |
Denys Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame^] | 51 | unpack_xz_stream(int src_fd, int dst_fd) |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 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) { |
Denys Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame^] | 82 | rd = safe_read(src_fd, membuf + insz, rd); |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 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) { |
Denys Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame^] | 97 | xwrite(dst_fd, iobuf.out, outpos); |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 98 | IF_DESKTOP(total += outpos;) |
| 99 | } |
| 100 | if (r == XZ_STREAM_END |
Denys Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame^] | 101 | /* this happens even with well-formed files: */ |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 102 | || (r == XZ_BUF_ERROR && insz == 0 && outpos == 0) |
| 103 | ) { |
| 104 | break; |
| 105 | } |
| 106 | if (r != XZ_OK) { |
| 107 | bb_error_msg("corrupted data"); |
| 108 | total = -1; |
| 109 | break; |
| 110 | } |
| 111 | iobuf.out_pos = 0; |
| 112 | } |
| 113 | xz_dec_end(state); |
| 114 | free(membuf); |
| 115 | |
| 116 | return total; |
| 117 | } |