"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 | * |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Bernhard Reutner-Fischer | cfb53df | 2006-04-02 21:50:01 +0000 | [diff] [blame] | 13 | #include "unarchive.h" |
| 14 | |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 15 | #ifdef CONFIG_FEATURE_LZMA_FAST |
| 16 | # define speed_inline ATTRIBUTE_ALWAYS_INLINE |
| 17 | #else |
| 18 | # define speed_inline |
| 19 | #endif |
| 20 | |
| 21 | |
| 22 | typedef struct { |
| 23 | int fd; |
| 24 | uint8_t *ptr; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 25 | // uint8_t *buffer; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 26 | uint8_t *buffer_end; |
| 27 | int buffer_size; |
| 28 | uint32_t code; |
| 29 | uint32_t range; |
| 30 | uint32_t bound; |
| 31 | } rc_t; |
| 32 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 33 | //#define RC_BUFFER ((uint8_t*)(void*)(rc+1)) |
| 34 | #define RC_BUFFER ((uint8_t*)(rc+1)) |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 35 | |
| 36 | #define RC_TOP_BITS 24 |
| 37 | #define RC_MOVE_BITS 5 |
| 38 | #define RC_MODEL_TOTAL_BITS 11 |
| 39 | |
| 40 | |
| 41 | /* Called twice: once at startup and once in rc_normalize() */ |
| 42 | static void rc_read(rc_t * rc) |
| 43 | { |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 44 | rc->buffer_size = read(rc->fd, RC_BUFFER, rc->buffer_size); |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 45 | if (rc->buffer_size <= 0) |
| 46 | bb_error_msg_and_die("unexpected EOF"); |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 47 | rc->ptr = RC_BUFFER; |
| 48 | rc->buffer_end = RC_BUFFER + rc->buffer_size; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* Called once */ |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 52 | static rc_t* rc_init(int fd, int buffer_size) |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 53 | { |
| 54 | int i; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 55 | rc_t* rc; |
| 56 | |
| 57 | rc = xmalloc(sizeof(rc_t) + buffer_size); |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 58 | |
| 59 | rc->fd = fd; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 60 | rc->buffer_size = buffer_size; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 61 | rc->buffer_end = RC_BUFFER + rc->buffer_size; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 62 | rc->ptr = rc->buffer_end; |
| 63 | |
| 64 | rc->code = 0; |
| 65 | rc->range = 0xFFFFFFFF; |
| 66 | for (i = 0; i < 5; i++) { |
| 67 | if (rc->ptr >= rc->buffer_end) |
| 68 | rc_read(rc); |
| 69 | rc->code = (rc->code << 8) | *rc->ptr++; |
| 70 | } |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 71 | return rc; |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 74 | /* Called once */ |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 75 | static ATTRIBUTE_ALWAYS_INLINE void rc_free(rc_t * rc) |
| 76 | { |
| 77 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 78 | free(rc); |
Rob Landley | b13fee4 | 2006-06-20 22:38:00 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */ |
| 82 | static void rc_do_normalize(rc_t * rc) |
| 83 | { |
| 84 | if (rc->ptr >= rc->buffer_end) |
| 85 | rc_read(rc); |
| 86 | rc->range <<= 8; |
| 87 | rc->code = (rc->code << 8) | *rc->ptr++; |
| 88 | } |
| 89 | static ATTRIBUTE_ALWAYS_INLINE void rc_normalize(rc_t * rc) |
| 90 | { |
| 91 | if (rc->range < (1 << RC_TOP_BITS)) { |
| 92 | rc_do_normalize(rc); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* Called 9 times */ |
| 97 | /* Why rc_is_bit_0_helper exists? |
| 98 | * Because we want to always expose (rc->code < rc->bound) to optimizer |
| 99 | */ |
| 100 | static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p) |
| 101 | { |
| 102 | rc_normalize(rc); |
| 103 | rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS); |
| 104 | return rc->bound; |
| 105 | } |
| 106 | static ATTRIBUTE_ALWAYS_INLINE int rc_is_bit_0(rc_t * rc, uint16_t * p) |
| 107 | { |
| 108 | uint32_t t = rc_is_bit_0_helper(rc, p); |
| 109 | return rc->code < t; |
| 110 | } |
| 111 | |
| 112 | /* Called ~10 times, but very small, thus inlined */ |
| 113 | static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p) |
| 114 | { |
| 115 | rc->range = rc->bound; |
| 116 | *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS; |
| 117 | } |
| 118 | static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p) |
| 119 | { |
| 120 | rc->range -= rc->bound; |
| 121 | rc->code -= rc->bound; |
| 122 | *p -= *p >> RC_MOVE_BITS; |
| 123 | } |
| 124 | |
| 125 | /* Called 4 times in unlzma loop */ |
| 126 | static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol) |
| 127 | { |
| 128 | if (rc_is_bit_0(rc, p)) { |
| 129 | rc_update_bit_0(rc, p); |
| 130 | *symbol *= 2; |
| 131 | return 0; |
| 132 | } else { |
| 133 | rc_update_bit_1(rc, p); |
| 134 | *symbol = *symbol * 2 + 1; |
| 135 | return 1; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* Called once */ |
| 140 | static ATTRIBUTE_ALWAYS_INLINE int rc_direct_bit(rc_t * rc) |
| 141 | { |
| 142 | rc_normalize(rc); |
| 143 | rc->range >>= 1; |
| 144 | if (rc->code >= rc->range) { |
| 145 | rc->code -= rc->range; |
| 146 | return 1; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* Called twice */ |
| 152 | static speed_inline void |
| 153 | rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol) |
| 154 | { |
| 155 | int i = num_levels; |
| 156 | |
| 157 | *symbol = 1; |
| 158 | while (i--) |
| 159 | rc_get_bit(rc, p + *symbol, symbol); |
| 160 | *symbol -= 1 << num_levels; |
| 161 | } |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | typedef struct { |
| 165 | uint8_t pos; |
| 166 | uint32_t dict_size; |
| 167 | uint64_t dst_size; |
| 168 | } __attribute__ ((packed)) lzma_header_t; |
| 169 | |
| 170 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 171 | /* #defines will make compiler to compute/optimize each one with each usage. |
| 172 | * Have heart and use enum instead. */ |
| 173 | enum { |
| 174 | LZMA_BASE_SIZE = 1846, |
| 175 | LZMA_LIT_SIZE = 768, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 177 | LZMA_NUM_POS_BITS_MAX = 4, |
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_LEN_NUM_LOW_BITS = 3, |
| 180 | LZMA_LEN_NUM_MID_BITS = 3, |
| 181 | LZMA_LEN_NUM_HIGH_BITS = 8, |
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_LEN_CHOICE = 0, |
| 184 | LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1), |
| 185 | LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1), |
| 186 | LZMA_LEN_MID = (LZMA_LEN_LOW \ |
| 187 | + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))), |
| 188 | LZMA_LEN_HIGH = (LZMA_LEN_MID \ |
| 189 | + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))), |
| 190 | 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] | 191 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 192 | LZMA_NUM_STATES = 12, |
| 193 | LZMA_NUM_LIT_STATES = 7, |
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_START_POS_MODEL_INDEX = 4, |
| 196 | LZMA_END_POS_MODEL_INDEX = 14, |
| 197 | LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)), |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 199 | LZMA_NUM_POS_SLOT_BITS = 6, |
| 200 | LZMA_NUM_LEN_TO_POS_STATES = 4, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 202 | LZMA_NUM_ALIGN_BITS = 4, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 203 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 204 | LZMA_MATCH_MIN_LEN = 2, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 205 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 206 | LZMA_IS_MATCH = 0, |
| 207 | LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)), |
| 208 | LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES), |
| 209 | LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES), |
| 210 | LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES), |
| 211 | LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES), |
| 212 | LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \ |
| 213 | + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)), |
| 214 | LZMA_SPEC_POS = (LZMA_POS_SLOT \ |
| 215 | + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)), |
| 216 | LZMA_ALIGN = (LZMA_SPEC_POS \ |
| 217 | + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX), |
| 218 | LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)), |
| 219 | LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS), |
| 220 | LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS), |
| 221 | }; |
| 222 | |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 223 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 224 | USE_DESKTOP(long long) int |
| 225 | unlzma(int src_fd, int dst_fd) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 226 | { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 227 | USE_DESKTOP(long long total_written = 0;) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 228 | lzma_header_t header; |
| 229 | int lc, pb, lp; |
| 230 | uint32_t pos_state_mask; |
| 231 | uint32_t literal_pos_mask; |
| 232 | uint32_t pos; |
| 233 | uint16_t *p; |
| 234 | uint16_t *prob; |
| 235 | uint16_t *prob_lit; |
| 236 | int num_bits; |
| 237 | int num_probs; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 238 | rc_t *rc; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 239 | int i, mi; |
| 240 | uint8_t *buffer; |
| 241 | uint8_t previous_byte = 0; |
| 242 | size_t buffer_pos = 0, global_pos = 0; |
| 243 | int len = 0; |
| 244 | int state = 0; |
| 245 | uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1; |
| 246 | |
| 247 | if (read(src_fd, &header, sizeof(header)) != sizeof(header)) |
| 248 | bb_error_msg_and_die("can't read header"); |
| 249 | |
| 250 | if (header.pos >= (9 * 5 * 5)) |
| 251 | bb_error_msg_and_die("bad header"); |
| 252 | mi = header.pos / 9; |
| 253 | lc = header.pos % 9; |
| 254 | pb = mi / 5; |
| 255 | lp = mi % 5; |
| 256 | pos_state_mask = (1 << pb) - 1; |
| 257 | literal_pos_mask = (1 << lp) - 1; |
| 258 | |
Rob Landley | bba7f08 | 2006-05-29 05:51:12 +0000 | [diff] [blame] | 259 | header.dict_size = SWAP_LE32(header.dict_size); |
| 260 | header.dst_size = SWAP_LE64(header.dst_size); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 261 | |
| 262 | if (header.dict_size == 0) |
| 263 | header.dict_size = 1; |
| 264 | |
| 265 | buffer = xmalloc(MIN(header.dst_size, header.dict_size)); |
| 266 | |
| 267 | num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)); |
| 268 | p = xmalloc(num_probs * sizeof(*p)); |
| 269 | num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp)); |
| 270 | for (i = 0; i < num_probs; i++) |
| 271 | p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1; |
| 272 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 273 | rc = rc_init(src_fd, 0x10000); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 274 | |
| 275 | while (global_pos + buffer_pos < header.dst_size) { |
| 276 | int pos_state = (buffer_pos + global_pos) & pos_state_mask; |
| 277 | |
| 278 | prob = |
| 279 | p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 280 | if (rc_is_bit_0(rc, prob)) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 281 | mi = 1; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 282 | rc_update_bit_0(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 283 | prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE |
| 284 | * ((((buffer_pos + global_pos) & literal_pos_mask) << lc) |
| 285 | + (previous_byte >> (8 - lc))))); |
| 286 | |
| 287 | if (state >= LZMA_NUM_LIT_STATES) { |
| 288 | int match_byte; |
| 289 | |
| 290 | pos = buffer_pos - rep0; |
| 291 | while (pos >= header.dict_size) |
| 292 | pos += header.dict_size; |
| 293 | match_byte = buffer[pos]; |
| 294 | do { |
| 295 | int bit; |
| 296 | |
| 297 | match_byte <<= 1; |
| 298 | bit = match_byte & 0x100; |
| 299 | prob_lit = prob + 0x100 + bit + mi; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 300 | if (rc_get_bit(rc, prob_lit, &mi)) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 301 | if (!bit) |
| 302 | break; |
| 303 | } else { |
| 304 | if (bit) |
| 305 | break; |
| 306 | } |
| 307 | } while (mi < 0x100); |
| 308 | } |
| 309 | while (mi < 0x100) { |
| 310 | prob_lit = prob + mi; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 311 | rc_get_bit(rc, prob_lit, &mi); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 312 | } |
| 313 | previous_byte = (uint8_t) mi; |
| 314 | |
| 315 | buffer[buffer_pos++] = previous_byte; |
| 316 | if (buffer_pos == header.dict_size) { |
| 317 | buffer_pos = 0; |
| 318 | global_pos += header.dict_size; |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 319 | // FIXME: error check |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 320 | write(dst_fd, buffer, header.dict_size); |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 321 | USE_DESKTOP(total_written += header.dict_size;) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 322 | } |
| 323 | if (state < 4) |
| 324 | state = 0; |
| 325 | else if (state < 10) |
| 326 | state -= 3; |
| 327 | else |
| 328 | state -= 6; |
| 329 | } else { |
| 330 | int offset; |
| 331 | uint16_t *prob_len; |
| 332 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 333 | rc_update_bit_1(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 334 | prob = p + LZMA_IS_REP + state; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 335 | if (rc_is_bit_0(rc, prob)) { |
| 336 | rc_update_bit_0(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 337 | rep3 = rep2; |
| 338 | rep2 = rep1; |
| 339 | rep1 = rep0; |
| 340 | state = state < LZMA_NUM_LIT_STATES ? 0 : 3; |
| 341 | prob = p + LZMA_LEN_CODER; |
| 342 | } else { |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 343 | rc_update_bit_1(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 344 | prob = p + LZMA_IS_REP_G0 + state; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 345 | if (rc_is_bit_0(rc, prob)) { |
| 346 | rc_update_bit_0(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 347 | prob = (p + LZMA_IS_REP_0_LONG |
| 348 | + (state << LZMA_NUM_POS_BITS_MAX) + pos_state); |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 349 | if (rc_is_bit_0(rc, prob)) { |
| 350 | rc_update_bit_0(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 351 | |
| 352 | state = state < LZMA_NUM_LIT_STATES ? 9 : 11; |
| 353 | pos = buffer_pos - rep0; |
| 354 | while (pos >= header.dict_size) |
| 355 | pos += header.dict_size; |
| 356 | previous_byte = buffer[pos]; |
| 357 | buffer[buffer_pos++] = previous_byte; |
| 358 | if (buffer_pos == header.dict_size) { |
| 359 | buffer_pos = 0; |
| 360 | global_pos += header.dict_size; |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 361 | // FIXME: error check |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 362 | write(dst_fd, buffer, header.dict_size); |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 363 | USE_DESKTOP(total_written += header.dict_size;) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 364 | } |
| 365 | continue; |
| 366 | } else { |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 367 | rc_update_bit_1(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 368 | } |
| 369 | } else { |
| 370 | uint32_t distance; |
| 371 | |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 372 | rc_update_bit_1(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 373 | prob = p + LZMA_IS_REP_G1 + state; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 374 | if (rc_is_bit_0(rc, prob)) { |
| 375 | rc_update_bit_0(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 376 | distance = rep1; |
| 377 | } else { |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 378 | rc_update_bit_1(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 379 | prob = p + LZMA_IS_REP_G2 + state; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 380 | if (rc_is_bit_0(rc, prob)) { |
| 381 | rc_update_bit_0(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 382 | distance = rep2; |
| 383 | } else { |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 384 | rc_update_bit_1(rc, prob); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 385 | distance = rep3; |
| 386 | rep3 = rep2; |
| 387 | } |
| 388 | rep2 = rep1; |
| 389 | } |
| 390 | rep1 = rep0; |
| 391 | rep0 = distance; |
| 392 | } |
| 393 | state = state < LZMA_NUM_LIT_STATES ? 8 : 11; |
| 394 | prob = p + LZMA_REP_LEN_CODER; |
| 395 | } |
| 396 | |
| 397 | prob_len = prob + LZMA_LEN_CHOICE; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 398 | if (rc_is_bit_0(rc, prob_len)) { |
| 399 | rc_update_bit_0(rc, prob_len); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 400 | prob_len = (prob + LZMA_LEN_LOW |
| 401 | + (pos_state << LZMA_LEN_NUM_LOW_BITS)); |
| 402 | offset = 0; |
| 403 | num_bits = LZMA_LEN_NUM_LOW_BITS; |
| 404 | } else { |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 405 | rc_update_bit_1(rc, prob_len); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 406 | prob_len = prob + LZMA_LEN_CHOICE_2; |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 407 | if (rc_is_bit_0(rc, prob_len)) { |
| 408 | rc_update_bit_0(rc, prob_len); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 409 | prob_len = (prob + LZMA_LEN_MID |
| 410 | + (pos_state << LZMA_LEN_NUM_MID_BITS)); |
| 411 | offset = 1 << LZMA_LEN_NUM_LOW_BITS; |
| 412 | num_bits = LZMA_LEN_NUM_MID_BITS; |
| 413 | } else { |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 414 | rc_update_bit_1(rc, prob_len); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 415 | prob_len = prob + LZMA_LEN_HIGH; |
| 416 | offset = ((1 << LZMA_LEN_NUM_LOW_BITS) |
| 417 | + (1 << LZMA_LEN_NUM_MID_BITS)); |
| 418 | num_bits = LZMA_LEN_NUM_HIGH_BITS; |
| 419 | } |
| 420 | } |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 421 | rc_bit_tree_decode(rc, prob_len, num_bits, &len); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 422 | len += offset; |
| 423 | |
| 424 | if (state < 4) { |
| 425 | int pos_slot; |
| 426 | |
| 427 | state += LZMA_NUM_LIT_STATES; |
| 428 | prob = |
| 429 | p + LZMA_POS_SLOT + |
| 430 | ((len < |
| 431 | LZMA_NUM_LEN_TO_POS_STATES ? len : |
| 432 | LZMA_NUM_LEN_TO_POS_STATES - 1) |
| 433 | << LZMA_NUM_POS_SLOT_BITS); |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 434 | rc_bit_tree_decode(rc, prob, LZMA_NUM_POS_SLOT_BITS, |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 435 | &pos_slot); |
| 436 | if (pos_slot >= LZMA_START_POS_MODEL_INDEX) { |
| 437 | num_bits = (pos_slot >> 1) - 1; |
| 438 | rep0 = 2 | (pos_slot & 1); |
| 439 | if (pos_slot < LZMA_END_POS_MODEL_INDEX) { |
| 440 | rep0 <<= num_bits; |
| 441 | prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1; |
| 442 | } else { |
| 443 | num_bits -= LZMA_NUM_ALIGN_BITS; |
| 444 | while (num_bits--) |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 445 | rep0 = (rep0 << 1) | rc_direct_bit(rc); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 446 | prob = p + LZMA_ALIGN; |
| 447 | rep0 <<= LZMA_NUM_ALIGN_BITS; |
| 448 | num_bits = LZMA_NUM_ALIGN_BITS; |
| 449 | } |
| 450 | i = 1; |
| 451 | mi = 1; |
| 452 | while (num_bits--) { |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 453 | if (rc_get_bit(rc, prob + mi, &mi)) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 454 | rep0 |= i; |
| 455 | i <<= 1; |
| 456 | } |
| 457 | } else |
| 458 | rep0 = pos_slot; |
| 459 | if (++rep0 == 0) |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | len += LZMA_MATCH_MIN_LEN; |
| 464 | |
| 465 | do { |
| 466 | pos = buffer_pos - rep0; |
| 467 | while (pos >= header.dict_size) |
| 468 | pos += header.dict_size; |
| 469 | previous_byte = buffer[pos]; |
| 470 | buffer[buffer_pos++] = previous_byte; |
| 471 | if (buffer_pos == header.dict_size) { |
| 472 | buffer_pos = 0; |
| 473 | global_pos += header.dict_size; |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 474 | // FIXME: error check |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 475 | write(dst_fd, buffer, header.dict_size); |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 476 | USE_DESKTOP(total_written += header.dict_size;) |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 477 | } |
| 478 | len--; |
| 479 | } while (len != 0 && buffer_pos < header.dst_size); |
| 480 | } |
| 481 | } |
| 482 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 483 | // FIXME: error check |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 484 | write(dst_fd, buffer, buffer_pos); |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 485 | USE_DESKTOP(total_written += buffer_pos;) |
Denis Vlasenko | 98b8e94 | 2007-01-05 13:59:05 +0000 | [diff] [blame^] | 486 | rc_free(rc); |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 487 | return USE_DESKTOP(total_written) + 0; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 488 | } |