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