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 | |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 15 | #define XZ_REALLOC_DICT_BUF(ptr, size) xrealloc(ptr, size) |
| 16 | #define XZ_FUNC FAST_FUNC |
| 17 | #define XZ_EXTERN static |
| 18 | |
Denys Vlasenko | 716f3f6 | 2010-06-01 14:41:39 +0200 | [diff] [blame^] | 19 | /* 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 |
| 24 | static uint32_t *crc32_table; |
| 25 | static uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc) |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 26 | { |
| 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 Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 36 | |
Denys Vlasenko | 716f3f6 | 2010-06-01 14:41:39 +0200 | [diff] [blame^] | 37 | /* We use arch-optimized unaligned accessors */ |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 38 | #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 | |
| 53 | IF_DESKTOP(long long) int FAST_FUNC |
Denys Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame] | 54 | unpack_xz_stream(int src_fd, int dst_fd) |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 55 | { |
| 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 | |
| 65 | membuf = xmalloc(IN_SIZE + OUT_SIZE); |
| 66 | memset(&iobuf, 0, sizeof(iobuf)); |
| 67 | iobuf.in = membuf; |
| 68 | iobuf.out = membuf + IN_SIZE; |
| 69 | iobuf.out_size = OUT_SIZE; |
| 70 | |
Denys Vlasenko | 716f3f6 | 2010-06-01 14:41:39 +0200 | [diff] [blame^] | 71 | if (!crc32_table) |
| 72 | crc32_table = crc32_filltable(NULL, /*endian:*/ 0); |
| 73 | |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 74 | state = xz_dec_init(64*1024); /* initial dict of 64k */ |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 75 | |
| 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 Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame] | 87 | rd = safe_read(src_fd, membuf + insz, rd); |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 88 | 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 Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame] | 102 | xwrite(dst_fd, iobuf.out, outpos); |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 103 | IF_DESKTOP(total += outpos;) |
| 104 | } |
| 105 | if (r == XZ_STREAM_END |
Denys Vlasenko | 6948f21 | 2010-05-30 04:18:13 +0200 | [diff] [blame] | 106 | /* this happens even with well-formed files: */ |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 107 | || (r == XZ_BUF_ERROR && insz == 0 && outpos == 0) |
| 108 | ) { |
| 109 | break; |
| 110 | } |
Denys Vlasenko | 716f3f6 | 2010-06-01 14:41:39 +0200 | [diff] [blame^] | 111 | if (r != XZ_OK && r != XZ_UNSUPPORTED_CHECK) { |
Denys Vlasenko | fb6c76c | 2010-05-30 03:47:40 +0200 | [diff] [blame] | 112 | 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 | } |