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