"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Small lzma deflate implementation. |
| 4 | * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
| 5 | * |
| 6 | * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) |
| 7 | * Copyright (C) 1999-2005 Igor Pavlov |
| 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 10 | */ |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 12 | #include "bb_archive.h" |
Bernhard Reutner-Fischer | cfb53df | 2006-04-02 21:50:01 +0000 | [diff] [blame] | 13 | |
Denys Vlasenko | a1870f4 | 2018-04-08 20:45:16 +0200 | [diff] [blame] | 14 | #if 0 |
| 15 | # define dbg(...) bb_error_msg(__VA_ARGS__) |
| 16 | #else |
| 17 | # define dbg(...) ((void)0) |
| 18 | #endif |
| 19 | |
| 20 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 21 | #if ENABLE_FEATURE_LZMA_FAST |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 22 | # define speed_inline ALWAYS_INLINE |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 23 | # define size_inline |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 24 | #else |
| 25 | # define speed_inline |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 26 | # define size_inline ALWAYS_INLINE |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | |
| 30 | typedef struct { |
| 31 | int fd; |
| 32 | uint8_t *ptr; |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 33 | |
| 34 | /* Was keeping rc on stack in unlzma and separately allocating buffer, |
| 35 | * but with "buffer 'attached to' allocated rc" code is smaller: */ |
| 36 | /* uint8_t *buffer; */ |
| 37 | #define RC_BUFFER ((uint8_t*)(rc+1)) |
| 38 | |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 39 | uint8_t *buffer_end; |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 40 | |
| 41 | /* Had provisions for variable buffer, but we don't need it here */ |
| 42 | /* int buffer_size; */ |
| 43 | #define RC_BUFFER_SIZE 0x10000 |
| 44 | |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 45 | uint32_t code; |
| 46 | uint32_t range; |
| 47 | uint32_t bound; |
| 48 | } rc_t; |
| 49 | |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 50 | #define RC_TOP_BITS 24 |
| 51 | #define RC_MOVE_BITS 5 |
| 52 | #define RC_MODEL_TOTAL_BITS 11 |
| 53 | |
| 54 | |
Denys Vlasenko | c09fd27 | 2013-03-01 14:37:58 +0100 | [diff] [blame] | 55 | /* Called once in rc_do_normalize() */ |
| 56 | static void rc_read(rc_t *rc) |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 57 | { |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 58 | int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE); |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 59 | //TODO: return -1 instead |
| 60 | //This will make unlzma delete broken unpacked file on unpack errors |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 61 | if (buffer_size <= 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 62 | bb_simple_error_msg_and_die("unexpected EOF"); |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 63 | rc->buffer_end = RC_BUFFER + buffer_size; |
Denys Vlasenko | c09fd27 | 2013-03-01 14:37:58 +0100 | [diff] [blame] | 64 | rc->ptr = RC_BUFFER; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 67 | /* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */ |
| 68 | static void rc_do_normalize(rc_t *rc) |
| 69 | { |
| 70 | if (rc->ptr >= rc->buffer_end) |
| 71 | rc_read(rc); |
| 72 | rc->range <<= 8; |
| 73 | rc->code = (rc->code << 8) | *rc->ptr++; |
| 74 | } |
Denys Vlasenko | 507f6ea | 2013-03-01 14:48:10 +0100 | [diff] [blame] | 75 | static ALWAYS_INLINE void rc_normalize(rc_t *rc) |
| 76 | { |
| 77 | if (rc->range < (1 << RC_TOP_BITS)) { |
| 78 | rc_do_normalize(rc); |
| 79 | } |
| 80 | } |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 81 | |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 82 | /* Called once */ |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 83 | static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */ |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 84 | { |
| 85 | int i; |
Denis Vlasenko | 2bbdda0 | 2008-06-27 15:48:45 +0000 | [diff] [blame] | 86 | rc_t *rc; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 87 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 88 | rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE); |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 89 | |
| 90 | rc->fd = fd; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 91 | /* rc->ptr = rc->buffer_end; */ |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 92 | |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 93 | for (i = 0; i < 5; i++) { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 94 | rc_do_normalize(rc); |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 95 | } |
Denys Vlasenko | c09fd27 | 2013-03-01 14:37:58 +0100 | [diff] [blame] | 96 | rc->range = 0xffffffff; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 97 | return rc; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 100 | /* Called once */ |
Denis Vlasenko | 2bbdda0 | 2008-06-27 15:48:45 +0000 | [diff] [blame] | 101 | static ALWAYS_INLINE void rc_free(rc_t *rc) |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 102 | { |
Denis Vlasenko | 2bbdda0 | 2008-06-27 15:48:45 +0000 | [diff] [blame] | 103 | free(rc); |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 106 | /* rc_is_bit_1 is called 9 times */ |
| 107 | static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p) |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 108 | { |
| 109 | rc_normalize(rc); |
| 110 | rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS); |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 111 | if (rc->code < rc->bound) { |
| 112 | rc->range = rc->bound; |
| 113 | *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS; |
| 114 | return 0; |
| 115 | } |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 116 | rc->range -= rc->bound; |
| 117 | rc->code -= rc->bound; |
| 118 | *p -= *p >> RC_MOVE_BITS; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 119 | return 1; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Called 4 times in unlzma loop */ |
Denys Vlasenko | a2d04e0 | 2013-03-01 14:43:07 +0100 | [diff] [blame] | 123 | static ALWAYS_INLINE int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol) |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 124 | { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 125 | int ret = rc_is_bit_1(rc, p); |
| 126 | *symbol = *symbol * 2 + ret; |
| 127 | return ret; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Called once */ |
Denis Vlasenko | 2bbdda0 | 2008-06-27 15:48:45 +0000 | [diff] [blame] | 131 | static ALWAYS_INLINE int rc_direct_bit(rc_t *rc) |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 132 | { |
| 133 | rc_normalize(rc); |
| 134 | rc->range >>= 1; |
| 135 | if (rc->code >= rc->range) { |
| 136 | rc->code -= rc->range; |
| 137 | return 1; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* Called twice */ |
| 143 | static speed_inline void |
Denis Vlasenko | 2bbdda0 | 2008-06-27 15:48:45 +0000 | [diff] [blame] | 144 | rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol) |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 145 | { |
| 146 | int i = num_levels; |
| 147 | |
| 148 | *symbol = 1; |
| 149 | while (i--) |
| 150 | rc_get_bit(rc, p + *symbol, symbol); |
| 151 | *symbol -= 1 << num_levels; |
| 152 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | typedef struct { |
| 156 | uint8_t pos; |
| 157 | uint32_t dict_size; |
| 158 | uint64_t dst_size; |
Denys Vlasenko | 8dc0e19 | 2009-09-16 00:58:11 +0200 | [diff] [blame] | 159 | } PACKED lzma_header_t; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 160 | |
| 161 | |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 162 | /* #defines will force compiler to compute/optimize each one with each usage. |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 163 | * Have heart and use enum instead. */ |
| 164 | enum { |
| 165 | LZMA_BASE_SIZE = 1846, |
| 166 | LZMA_LIT_SIZE = 768, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 168 | LZMA_NUM_POS_BITS_MAX = 4, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 169 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 170 | LZMA_LEN_NUM_LOW_BITS = 3, |
| 171 | LZMA_LEN_NUM_MID_BITS = 3, |
| 172 | LZMA_LEN_NUM_HIGH_BITS = 8, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 173 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 174 | LZMA_LEN_CHOICE = 0, |
| 175 | LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1), |
| 176 | LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1), |
| 177 | LZMA_LEN_MID = (LZMA_LEN_LOW \ |
| 178 | + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))), |
| 179 | LZMA_LEN_HIGH = (LZMA_LEN_MID \ |
| 180 | + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))), |
| 181 | LZMA_NUM_LEN_PROBS = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)), |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 182 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 183 | LZMA_NUM_STATES = 12, |
| 184 | LZMA_NUM_LIT_STATES = 7, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 186 | LZMA_START_POS_MODEL_INDEX = 4, |
| 187 | LZMA_END_POS_MODEL_INDEX = 14, |
| 188 | LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)), |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 189 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 190 | LZMA_NUM_POS_SLOT_BITS = 6, |
| 191 | LZMA_NUM_LEN_TO_POS_STATES = 4, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 193 | LZMA_NUM_ALIGN_BITS = 4, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 194 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 195 | LZMA_MATCH_MIN_LEN = 2, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 197 | LZMA_IS_MATCH = 0, |
| 198 | LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)), |
| 199 | LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES), |
| 200 | LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES), |
| 201 | LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES), |
| 202 | LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES), |
| 203 | LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \ |
| 204 | + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)), |
| 205 | LZMA_SPEC_POS = (LZMA_POS_SLOT \ |
| 206 | + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)), |
| 207 | LZMA_ALIGN = (LZMA_SPEC_POS \ |
| 208 | + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX), |
| 209 | LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)), |
| 210 | LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS), |
| 211 | LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS), |
| 212 | }; |
| 213 | |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 214 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 215 | IF_DESKTOP(long long) int FAST_FUNC |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 216 | unpack_lzma_stream(transformer_state_t *xstate) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 217 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 218 | IF_DESKTOP(long long total_written = 0;) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 219 | lzma_header_t header; |
| 220 | int lc, pb, lp; |
| 221 | uint32_t pos_state_mask; |
| 222 | uint32_t literal_pos_mask; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 223 | uint16_t *p; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 224 | rc_t *rc; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 225 | int i; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 226 | uint8_t *buffer; |
Denys Vlasenko | e09c426 | 2018-04-19 19:29:49 +0200 | [diff] [blame] | 227 | uint32_t buffer_size; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 228 | uint8_t previous_byte = 0; |
| 229 | size_t buffer_pos = 0, global_pos = 0; |
| 230 | int len = 0; |
| 231 | int state = 0; |
| 232 | uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1; |
| 233 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 234 | if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header) |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 235 | || header.pos >= (9 * 5 * 5) |
| 236 | ) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 237 | bb_simple_error_msg("bad lzma header"); |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 238 | return -1; |
| 239 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 240 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 241 | i = header.pos / 9; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 242 | lc = header.pos % 9; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 243 | pb = i / 5; |
| 244 | lp = i % 5; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 245 | pos_state_mask = (1 << pb) - 1; |
| 246 | literal_pos_mask = (1 << lp) - 1; |
| 247 | |
Denys Vlasenko | 81071e6 | 2014-02-28 15:42:10 +0100 | [diff] [blame] | 248 | /* Example values from linux-3.3.4.tar.lzma: |
| 249 | * dict_size: 64M, dst_size: 2^64-1 |
| 250 | */ |
Rob Landley | bba7f08 | 2006-05-29 05:51:12 +0000 | [diff] [blame] | 251 | header.dict_size = SWAP_LE32(header.dict_size); |
| 252 | header.dst_size = SWAP_LE64(header.dst_size); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 253 | |
| 254 | if (header.dict_size == 0) |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 255 | header.dict_size++; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 256 | |
Denys Vlasenko | e09c426 | 2018-04-19 19:29:49 +0200 | [diff] [blame] | 257 | buffer_size = MIN(header.dst_size, header.dict_size); |
| 258 | buffer = xmalloc(buffer_size); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 259 | |
Denys Vlasenko | 2108a6f | 2014-02-28 15:05:43 +0100 | [diff] [blame] | 260 | { |
| 261 | int num_probs; |
| 262 | |
| 263 | num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)); |
| 264 | p = xmalloc(num_probs * sizeof(*p)); |
| 265 | num_probs += LZMA_LITERAL - LZMA_BASE_SIZE; |
| 266 | for (i = 0; i < num_probs; i++) |
| 267 | p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1; |
| 268 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 269 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 270 | rc = rc_init(xstate->src_fd); /*, RC_BUFFER_SIZE); */ |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 271 | |
| 272 | while (global_pos + buffer_pos < header.dst_size) { |
| 273 | int pos_state = (buffer_pos + global_pos) & pos_state_mask; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 274 | uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 275 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 276 | if (!rc_is_bit_1(rc, prob)) { |
| 277 | static const char next_state[LZMA_NUM_STATES] = |
| 278 | { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 }; |
| 279 | int mi = 1; |
| 280 | |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 281 | prob = (p + LZMA_LITERAL |
| 282 | + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc) |
| 283 | + (previous_byte >> (8 - lc)) |
| 284 | ) |
| 285 | ) |
| 286 | ); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 287 | |
| 288 | if (state >= LZMA_NUM_LIT_STATES) { |
| 289 | int match_byte; |
Denys Vlasenko | 3989e5a | 2017-01-09 13:55:11 +0100 | [diff] [blame] | 290 | uint32_t pos; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 291 | |
Denys Vlasenko | 3989e5a | 2017-01-09 13:55:11 +0100 | [diff] [blame] | 292 | pos = buffer_pos - rep0; |
Denys Vlasenko | 04f052c | 2021-06-15 15:07:57 +0200 | [diff] [blame] | 293 | if ((int32_t)pos < 0) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 294 | pos += header.dict_size; |
Denys Vlasenko | 04f052c | 2021-06-15 15:07:57 +0200 | [diff] [blame] | 295 | if ((int32_t)pos < 0) |
| 296 | goto bad; |
| 297 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 298 | match_byte = buffer[pos]; |
| 299 | do { |
| 300 | int bit; |
| 301 | |
| 302 | match_byte <<= 1; |
| 303 | bit = match_byte & 0x100; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 304 | bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */ |
Denis Vlasenko | 444639c | 2008-03-02 18:56:23 +0000 | [diff] [blame] | 305 | if (bit) |
| 306 | break; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 307 | } while (mi < 0x100); |
| 308 | } |
| 309 | while (mi < 0x100) { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 310 | rc_get_bit(rc, prob + mi, &mi); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 311 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 312 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 313 | state = next_state[state]; |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 314 | |
| 315 | previous_byte = (uint8_t) mi; |
| 316 | #if ENABLE_FEATURE_LZMA_FAST |
Denis Vlasenko | c0183e6 | 2008-03-18 03:13:25 +0000 | [diff] [blame] | 317 | one_byte1: |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 318 | buffer[buffer_pos++] = previous_byte; |
| 319 | if (buffer_pos == header.dict_size) { |
| 320 | buffer_pos = 0; |
| 321 | global_pos += header.dict_size; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 322 | if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size) |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 323 | goto bad; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 324 | IF_DESKTOP(total_written += header.dict_size;) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 325 | } |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 326 | #else |
| 327 | len = 1; |
Denis Vlasenko | c0183e6 | 2008-03-18 03:13:25 +0000 | [diff] [blame] | 328 | goto one_byte2; |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 329 | #endif |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 330 | } else { |
Denys Vlasenko | 2108a6f | 2014-02-28 15:05:43 +0100 | [diff] [blame] | 331 | int num_bits; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 332 | int offset; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 333 | uint16_t *prob2; |
| 334 | #define prob_len prob2 |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 335 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 336 | prob2 = p + LZMA_IS_REP + state; |
| 337 | if (!rc_is_bit_1(rc, prob2)) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 338 | rep3 = rep2; |
| 339 | rep2 = rep1; |
| 340 | rep1 = rep0; |
| 341 | state = state < LZMA_NUM_LIT_STATES ? 0 : 3; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 342 | prob2 = p + LZMA_LEN_CODER; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 343 | } else { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 344 | prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP; |
| 345 | if (!rc_is_bit_1(rc, prob2)) { |
| 346 | prob2 = (p + LZMA_IS_REP_0_LONG |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 347 | + (state << LZMA_NUM_POS_BITS_MAX) |
| 348 | + pos_state |
| 349 | ); |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 350 | if (!rc_is_bit_1(rc, prob2)) { |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 351 | #if ENABLE_FEATURE_LZMA_FAST |
Denys Vlasenko | 3989e5a | 2017-01-09 13:55:11 +0100 | [diff] [blame] | 352 | uint32_t pos; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 353 | state = state < LZMA_NUM_LIT_STATES ? 9 : 11; |
Denys Vlasenko | 3989e5a | 2017-01-09 13:55:11 +0100 | [diff] [blame] | 354 | |
| 355 | pos = buffer_pos - rep0; |
Denys Vlasenko | a36986b | 2018-05-25 17:03:46 +0200 | [diff] [blame] | 356 | if ((int32_t)pos < 0) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 357 | pos += header.dict_size; |
Denys Vlasenko | a36986b | 2018-05-25 17:03:46 +0200 | [diff] [blame] | 358 | /* see unzip_bad_lzma_2.zip: */ |
Denys Vlasenko | e80d04b | 2018-11-27 11:52:14 +0100 | [diff] [blame] | 359 | if (pos >= buffer_size) { |
| 360 | dbg("%d pos:%d buffer_size:%d", __LINE__, pos, buffer_size); |
Denys Vlasenko | a36986b | 2018-05-25 17:03:46 +0200 | [diff] [blame] | 361 | goto bad; |
Denys Vlasenko | e80d04b | 2018-11-27 11:52:14 +0100 | [diff] [blame] | 362 | } |
Denys Vlasenko | a36986b | 2018-05-25 17:03:46 +0200 | [diff] [blame] | 363 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 364 | previous_byte = buffer[pos]; |
Denis Vlasenko | c0183e6 | 2008-03-18 03:13:25 +0000 | [diff] [blame] | 365 | goto one_byte1; |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 366 | #else |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 367 | state = state < LZMA_NUM_LIT_STATES ? 9 : 11; |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 368 | len = 1; |
| 369 | goto string; |
| 370 | #endif |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 371 | } |
| 372 | } else { |
| 373 | uint32_t distance; |
| 374 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 375 | prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0; |
| 376 | distance = rep1; |
| 377 | if (rc_is_bit_1(rc, prob2)) { |
| 378 | prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1; |
| 379 | distance = rep2; |
| 380 | if (rc_is_bit_1(rc, prob2)) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 381 | distance = rep3; |
| 382 | rep3 = rep2; |
| 383 | } |
| 384 | rep2 = rep1; |
| 385 | } |
| 386 | rep1 = rep0; |
| 387 | rep0 = distance; |
| 388 | } |
| 389 | state = state < LZMA_NUM_LIT_STATES ? 8 : 11; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 390 | prob2 = p + LZMA_REP_LEN_CODER; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 393 | prob_len = prob2 + LZMA_LEN_CHOICE; |
| 394 | num_bits = LZMA_LEN_NUM_LOW_BITS; |
| 395 | if (!rc_is_bit_1(rc, prob_len)) { |
| 396 | prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE |
| 397 | + (pos_state << LZMA_LEN_NUM_LOW_BITS); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 398 | offset = 0; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 399 | } else { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 400 | prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE; |
| 401 | if (!rc_is_bit_1(rc, prob_len)) { |
| 402 | prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2 |
| 403 | + (pos_state << LZMA_LEN_NUM_MID_BITS); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 404 | offset = 1 << LZMA_LEN_NUM_LOW_BITS; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 405 | num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 406 | } else { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 407 | prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 408 | offset = ((1 << LZMA_LEN_NUM_LOW_BITS) |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 409 | + (1 << LZMA_LEN_NUM_MID_BITS)); |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 410 | num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 413 | rc_bit_tree_decode(rc, prob_len, num_bits, &len); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 414 | len += offset; |
| 415 | |
| 416 | if (state < 4) { |
| 417 | int pos_slot; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 418 | uint16_t *prob3; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 419 | |
| 420 | state += LZMA_NUM_LIT_STATES; |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 421 | prob3 = p + LZMA_POS_SLOT + |
Denis Vlasenko | c3fff87 | 2008-03-02 18:55:49 +0000 | [diff] [blame] | 422 | ((len < LZMA_NUM_LEN_TO_POS_STATES ? len : |
| 423 | LZMA_NUM_LEN_TO_POS_STATES - 1) |
| 424 | << LZMA_NUM_POS_SLOT_BITS); |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 425 | rc_bit_tree_decode(rc, prob3, |
| 426 | LZMA_NUM_POS_SLOT_BITS, &pos_slot); |
| 427 | rep0 = pos_slot; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 428 | if (pos_slot >= LZMA_START_POS_MODEL_INDEX) { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 429 | int i2, mi2, num_bits2 = (pos_slot >> 1) - 1; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 430 | rep0 = 2 | (pos_slot & 1); |
| 431 | if (pos_slot < LZMA_END_POS_MODEL_INDEX) { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 432 | rep0 <<= num_bits2; |
| 433 | prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 434 | } else { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 435 | for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--) |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame] | 436 | rep0 = (rep0 << 1) | rc_direct_bit(rc); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 437 | rep0 <<= LZMA_NUM_ALIGN_BITS; |
Denys Vlasenko | e80d04b | 2018-11-27 11:52:14 +0100 | [diff] [blame] | 438 | // Note: (int32_t)rep0 may be < 0 here |
| 439 | // (I have linux-3.3.4.tar.lzma which has it). |
| 440 | // I moved the check after "++rep0 == 0" check below. |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 441 | prob3 = p + LZMA_ALIGN; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 442 | } |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 443 | i2 = 1; |
| 444 | mi2 = 1; |
| 445 | while (num_bits2--) { |
| 446 | if (rc_get_bit(rc, prob3 + mi2, &mi2)) |
| 447 | rep0 |= i2; |
| 448 | i2 <<= 1; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 449 | } |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 450 | } |
Denys Vlasenko | e80d04b | 2018-11-27 11:52:14 +0100 | [diff] [blame] | 451 | rep0++; |
| 452 | if ((int32_t)rep0 <= 0) { |
| 453 | if (rep0 == 0) |
| 454 | break; |
| 455 | dbg("%d rep0:%d", __LINE__, rep0); |
| 456 | goto bad; |
| 457 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | len += LZMA_MATCH_MIN_LEN; |
Denys Vlasenko | 590402b | 2017-01-09 14:28:25 +0100 | [diff] [blame] | 461 | /* |
| 462 | * LZMA SDK has this optimized: |
| 463 | * it precalculates size and copies many bytes |
| 464 | * in a loop with simpler checks, a-la: |
| 465 | * do |
| 466 | * *(dest) = *(dest + ofs); |
| 467 | * while (++dest != lim); |
| 468 | * and |
| 469 | * do { |
| 470 | * buffer[buffer_pos++] = buffer[pos]; |
| 471 | * if (++pos == header.dict_size) |
| 472 | * pos = 0; |
| 473 | * } while (--cur_len != 0); |
| 474 | * Our code is slower (more checks per byte copy): |
| 475 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 476 | IF_NOT_FEATURE_LZMA_FAST(string:) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 477 | do { |
Denys Vlasenko | f2c184b | 2009-09-15 23:40:08 +0200 | [diff] [blame] | 478 | uint32_t pos = buffer_pos - rep0; |
Denys Vlasenko | 9ac42c5 | 2017-10-27 15:37:03 +0200 | [diff] [blame] | 479 | if ((int32_t)pos < 0) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 480 | pos += header.dict_size; |
Denys Vlasenko | 9ac42c5 | 2017-10-27 15:37:03 +0200 | [diff] [blame] | 481 | /* bug 10436 has an example file where this triggers: */ |
Denys Vlasenko | e09c426 | 2018-04-19 19:29:49 +0200 | [diff] [blame] | 482 | //if ((int32_t)pos < 0) |
| 483 | // goto bad; |
| 484 | /* more stringent test (see unzip_bad_lzma_1.zip): */ |
| 485 | if (pos >= buffer_size) |
Denys Vlasenko | 9ac42c5 | 2017-10-27 15:37:03 +0200 | [diff] [blame] | 486 | goto bad; |
| 487 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 488 | previous_byte = buffer[pos]; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 489 | IF_NOT_FEATURE_LZMA_FAST(one_byte2:) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 490 | buffer[buffer_pos++] = previous_byte; |
| 491 | if (buffer_pos == header.dict_size) { |
| 492 | buffer_pos = 0; |
| 493 | global_pos += header.dict_size; |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 494 | if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size) |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 495 | goto bad; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 496 | IF_DESKTOP(total_written += header.dict_size;) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 497 | } |
| 498 | len--; |
| 499 | } while (len != 0 && buffer_pos < header.dst_size); |
Denys Vlasenko | 81071e6 | 2014-02-28 15:42:10 +0100 | [diff] [blame] | 500 | /* FIXME: ...........^^^^^ |
| 501 | * shouldn't it be "global_pos + buffer_pos < header.dst_size"? |
Denys Vlasenko | 590402b | 2017-01-09 14:28:25 +0100 | [diff] [blame] | 502 | * It probably should, but it is a "do we accidentally |
| 503 | * unpack more bytes than expected?" check - which |
| 504 | * never happens for well-formed compression data... |
Denys Vlasenko | 81071e6 | 2014-02-28 15:42:10 +0100 | [diff] [blame] | 505 | */ |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Denis Vlasenko | 2bbdda0 | 2008-06-27 15:48:45 +0000 | [diff] [blame] | 509 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 510 | IF_NOT_DESKTOP(int total_written = 0; /* success */) |
| 511 | IF_DESKTOP(total_written += buffer_pos;) |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 512 | if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) { |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 513 | bad: |
Denys Vlasenko | 77bf05d | 2018-04-24 13:49:12 +0200 | [diff] [blame] | 514 | /* One of our users, bbunpack(), expects _us_ to emit |
| 515 | * the error message (since it's the best place to give |
| 516 | * potentially more detailed information). |
| 517 | * Do not fail silently. |
| 518 | */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 519 | bb_simple_error_msg("corrupted data"); |
Denis Vlasenko | 2bbdda0 | 2008-06-27 15:48:45 +0000 | [diff] [blame] | 520 | total_written = -1; /* failure */ |
| 521 | } |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 522 | rc_free(rc); |
Denis Vlasenko | 2bbdda0 | 2008-06-27 15:48:45 +0000 | [diff] [blame] | 523 | free(p); |
| 524 | free(buffer); |
| 525 | return total_written; |
Denis Vlasenko | 3376298 | 2007-01-05 14:04:47 +0000 | [diff] [blame] | 526 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 527 | } |