Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 1 | /* Lzma decompressor for Linux kernel. Shamelessly snarfed |
| 2 | *from busybox 1.1.1 |
| 3 | * |
| 4 | *Linux kernel adaptation |
| 5 | *Copyright (C) 2006 Alain < alain@knaff.lu > |
| 6 | * |
| 7 | *Based on small lzma deflate implementation/Small range coder |
| 8 | *implementation for lzma. |
| 9 | *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org > |
| 10 | * |
| 11 | *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) |
| 12 | *Copyright (C) 1999-2005 Igor Pavlov |
| 13 | * |
| 14 | *Copyrights of the parts, see headers below. |
| 15 | * |
| 16 | * |
| 17 | *This program is free software; you can redistribute it and/or |
| 18 | *modify it under the terms of the GNU Lesser General Public |
| 19 | *License as published by the Free Software Foundation; either |
| 20 | *version 2.1 of the License, or (at your option) any later version. |
| 21 | * |
| 22 | *This program is distributed in the hope that it will be useful, |
| 23 | *but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | *Lesser General Public License for more details. |
| 26 | * |
| 27 | *You should have received a copy of the GNU Lesser General Public |
| 28 | *License along with this library; if not, write to the Free Software |
| 29 | *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 | */ |
| 31 | |
Phillip Lougher | b1af431 | 2009-08-06 15:09:31 -0700 | [diff] [blame] | 32 | #ifdef STATIC |
| 33 | #define PREBOOT |
| 34 | #else |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 35 | #include <linux/decompress/unlzma.h> |
Albin Tonnerre | 9e5cf0c | 2009-08-06 15:09:32 -0700 | [diff] [blame] | 36 | #include <linux/slab.h> |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 37 | #endif /* STATIC */ |
| 38 | |
| 39 | #include <linux/decompress/mm.h> |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 40 | |
| 41 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 42 | |
| 43 | static long long INIT read_int(unsigned char *ptr, int size) |
| 44 | { |
| 45 | int i; |
| 46 | long long ret = 0; |
| 47 | |
| 48 | for (i = 0; i < size; i++) |
| 49 | ret = (ret << 8) | ptr[size-i-1]; |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | #define ENDIAN_CONVERT(x) \ |
| 54 | x = (typeof(x))read_int((unsigned char *)&x, sizeof(x)) |
| 55 | |
| 56 | |
| 57 | /* Small range coder implementation for lzma. |
| 58 | *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org > |
| 59 | * |
| 60 | *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) |
| 61 | *Copyright (c) 1999-2005 Igor Pavlov |
| 62 | */ |
| 63 | |
| 64 | #include <linux/compiler.h> |
| 65 | |
| 66 | #define LZMA_IOBUF_SIZE 0x10000 |
| 67 | |
| 68 | struct rc { |
| 69 | int (*fill)(void*, unsigned int); |
| 70 | uint8_t *ptr; |
| 71 | uint8_t *buffer; |
| 72 | uint8_t *buffer_end; |
| 73 | int buffer_size; |
| 74 | uint32_t code; |
| 75 | uint32_t range; |
| 76 | uint32_t bound; |
Lasse Collin | 93685ad2 | 2011-01-12 17:01:14 -0800 | [diff] [blame^] | 77 | void (*error)(char *); |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | |
| 81 | #define RC_TOP_BITS 24 |
| 82 | #define RC_MOVE_BITS 5 |
| 83 | #define RC_MODEL_TOTAL_BITS 11 |
| 84 | |
| 85 | |
Lasse Collin | 6b01ed6 | 2011-01-12 17:01:13 -0800 | [diff] [blame] | 86 | static int INIT nofill(void *buffer, unsigned int len) |
Phillip Lougher | 6a88116 | 2009-09-23 15:57:37 -0700 | [diff] [blame] | 87 | { |
| 88 | return -1; |
| 89 | } |
| 90 | |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 91 | /* Called twice: once at startup and once in rc_normalize() */ |
| 92 | static void INIT rc_read(struct rc *rc) |
| 93 | { |
| 94 | rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE); |
| 95 | if (rc->buffer_size <= 0) |
Lasse Collin | 93685ad2 | 2011-01-12 17:01:14 -0800 | [diff] [blame^] | 96 | rc->error("unexpected EOF"); |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 97 | rc->ptr = rc->buffer; |
| 98 | rc->buffer_end = rc->buffer + rc->buffer_size; |
| 99 | } |
| 100 | |
| 101 | /* Called once */ |
| 102 | static inline void INIT rc_init(struct rc *rc, |
| 103 | int (*fill)(void*, unsigned int), |
| 104 | char *buffer, int buffer_size) |
| 105 | { |
Phillip Lougher | 6a88116 | 2009-09-23 15:57:37 -0700 | [diff] [blame] | 106 | if (fill) |
| 107 | rc->fill = fill; |
| 108 | else |
| 109 | rc->fill = nofill; |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 110 | rc->buffer = (uint8_t *)buffer; |
| 111 | rc->buffer_size = buffer_size; |
| 112 | rc->buffer_end = rc->buffer + rc->buffer_size; |
| 113 | rc->ptr = rc->buffer; |
| 114 | |
| 115 | rc->code = 0; |
| 116 | rc->range = 0xFFFFFFFF; |
| 117 | } |
| 118 | |
| 119 | static inline void INIT rc_init_code(struct rc *rc) |
| 120 | { |
| 121 | int i; |
| 122 | |
| 123 | for (i = 0; i < 5; i++) { |
| 124 | if (rc->ptr >= rc->buffer_end) |
| 125 | rc_read(rc); |
| 126 | rc->code = (rc->code << 8) | *rc->ptr++; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* Called once. TODO: bb_maybe_free() */ |
| 132 | static inline void INIT rc_free(struct rc *rc) |
| 133 | { |
| 134 | free(rc->buffer); |
| 135 | } |
| 136 | |
| 137 | /* Called twice, but one callsite is in inline'd rc_is_bit_0_helper() */ |
| 138 | static void INIT rc_do_normalize(struct rc *rc) |
| 139 | { |
| 140 | if (rc->ptr >= rc->buffer_end) |
| 141 | rc_read(rc); |
| 142 | rc->range <<= 8; |
| 143 | rc->code = (rc->code << 8) | *rc->ptr++; |
| 144 | } |
| 145 | static inline void INIT rc_normalize(struct rc *rc) |
| 146 | { |
| 147 | if (rc->range < (1 << RC_TOP_BITS)) |
| 148 | rc_do_normalize(rc); |
| 149 | } |
| 150 | |
| 151 | /* Called 9 times */ |
| 152 | /* Why rc_is_bit_0_helper exists? |
| 153 | *Because we want to always expose (rc->code < rc->bound) to optimizer |
| 154 | */ |
| 155 | static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p) |
| 156 | { |
| 157 | rc_normalize(rc); |
| 158 | rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS); |
| 159 | return rc->bound; |
| 160 | } |
| 161 | static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p) |
| 162 | { |
| 163 | uint32_t t = rc_is_bit_0_helper(rc, p); |
| 164 | return rc->code < t; |
| 165 | } |
| 166 | |
| 167 | /* Called ~10 times, but very small, thus inlined */ |
| 168 | static inline void INIT rc_update_bit_0(struct rc *rc, uint16_t *p) |
| 169 | { |
| 170 | rc->range = rc->bound; |
| 171 | *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS; |
| 172 | } |
Lasse Collin | 6b01ed6 | 2011-01-12 17:01:13 -0800 | [diff] [blame] | 173 | static inline void INIT rc_update_bit_1(struct rc *rc, uint16_t *p) |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 174 | { |
| 175 | rc->range -= rc->bound; |
| 176 | rc->code -= rc->bound; |
| 177 | *p -= *p >> RC_MOVE_BITS; |
| 178 | } |
| 179 | |
| 180 | /* Called 4 times in unlzma loop */ |
| 181 | static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol) |
| 182 | { |
| 183 | if (rc_is_bit_0(rc, p)) { |
| 184 | rc_update_bit_0(rc, p); |
| 185 | *symbol *= 2; |
| 186 | return 0; |
| 187 | } else { |
| 188 | rc_update_bit_1(rc, p); |
| 189 | *symbol = *symbol * 2 + 1; |
| 190 | return 1; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /* Called once */ |
| 195 | static inline int INIT rc_direct_bit(struct rc *rc) |
| 196 | { |
| 197 | rc_normalize(rc); |
| 198 | rc->range >>= 1; |
| 199 | if (rc->code >= rc->range) { |
| 200 | rc->code -= rc->range; |
| 201 | return 1; |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* Called twice */ |
| 207 | static inline void INIT |
| 208 | rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol) |
| 209 | { |
| 210 | int i = num_levels; |
| 211 | |
| 212 | *symbol = 1; |
| 213 | while (i--) |
| 214 | rc_get_bit(rc, p + *symbol, symbol); |
| 215 | *symbol -= 1 << num_levels; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /* |
| 220 | * Small lzma deflate implementation. |
| 221 | * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org > |
| 222 | * |
| 223 | * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) |
| 224 | * Copyright (C) 1999-2005 Igor Pavlov |
| 225 | */ |
| 226 | |
| 227 | |
| 228 | struct lzma_header { |
| 229 | uint8_t pos; |
| 230 | uint32_t dict_size; |
| 231 | uint64_t dst_size; |
| 232 | } __attribute__ ((packed)) ; |
| 233 | |
| 234 | |
| 235 | #define LZMA_BASE_SIZE 1846 |
| 236 | #define LZMA_LIT_SIZE 768 |
| 237 | |
| 238 | #define LZMA_NUM_POS_BITS_MAX 4 |
| 239 | |
| 240 | #define LZMA_LEN_NUM_LOW_BITS 3 |
| 241 | #define LZMA_LEN_NUM_MID_BITS 3 |
| 242 | #define LZMA_LEN_NUM_HIGH_BITS 8 |
| 243 | |
| 244 | #define LZMA_LEN_CHOICE 0 |
| 245 | #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1) |
| 246 | #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1) |
| 247 | #define LZMA_LEN_MID (LZMA_LEN_LOW \ |
| 248 | + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))) |
| 249 | #define LZMA_LEN_HIGH (LZMA_LEN_MID \ |
| 250 | +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))) |
| 251 | #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)) |
| 252 | |
| 253 | #define LZMA_NUM_STATES 12 |
| 254 | #define LZMA_NUM_LIT_STATES 7 |
| 255 | |
| 256 | #define LZMA_START_POS_MODEL_INDEX 4 |
| 257 | #define LZMA_END_POS_MODEL_INDEX 14 |
| 258 | #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1)) |
| 259 | |
| 260 | #define LZMA_NUM_POS_SLOT_BITS 6 |
| 261 | #define LZMA_NUM_LEN_TO_POS_STATES 4 |
| 262 | |
| 263 | #define LZMA_NUM_ALIGN_BITS 4 |
| 264 | |
| 265 | #define LZMA_MATCH_MIN_LEN 2 |
| 266 | |
| 267 | #define LZMA_IS_MATCH 0 |
| 268 | #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)) |
| 269 | #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES) |
| 270 | #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES) |
| 271 | #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES) |
| 272 | #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES) |
| 273 | #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \ |
| 274 | + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)) |
| 275 | #define LZMA_SPEC_POS (LZMA_POS_SLOT \ |
| 276 | +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)) |
| 277 | #define LZMA_ALIGN (LZMA_SPEC_POS \ |
| 278 | + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX) |
| 279 | #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)) |
| 280 | #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS) |
| 281 | #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS) |
| 282 | |
| 283 | |
| 284 | struct writer { |
| 285 | uint8_t *buffer; |
| 286 | uint8_t previous_byte; |
| 287 | size_t buffer_pos; |
| 288 | int bufsize; |
| 289 | size_t global_pos; |
| 290 | int(*flush)(void*, unsigned int); |
| 291 | struct lzma_header *header; |
| 292 | }; |
| 293 | |
| 294 | struct cstate { |
| 295 | int state; |
| 296 | uint32_t rep0, rep1, rep2, rep3; |
| 297 | }; |
| 298 | |
| 299 | static inline size_t INIT get_pos(struct writer *wr) |
| 300 | { |
| 301 | return |
| 302 | wr->global_pos + wr->buffer_pos; |
| 303 | } |
| 304 | |
| 305 | static inline uint8_t INIT peek_old_byte(struct writer *wr, |
| 306 | uint32_t offs) |
| 307 | { |
| 308 | if (!wr->flush) { |
| 309 | int32_t pos; |
| 310 | while (offs > wr->header->dict_size) |
| 311 | offs -= wr->header->dict_size; |
| 312 | pos = wr->buffer_pos - offs; |
| 313 | return wr->buffer[pos]; |
| 314 | } else { |
| 315 | uint32_t pos = wr->buffer_pos - offs; |
| 316 | while (pos >= wr->header->dict_size) |
| 317 | pos += wr->header->dict_size; |
| 318 | return wr->buffer[pos]; |
| 319 | } |
| 320 | |
| 321 | } |
| 322 | |
| 323 | static inline void INIT write_byte(struct writer *wr, uint8_t byte) |
| 324 | { |
| 325 | wr->buffer[wr->buffer_pos++] = wr->previous_byte = byte; |
| 326 | if (wr->flush && wr->buffer_pos == wr->header->dict_size) { |
| 327 | wr->buffer_pos = 0; |
| 328 | wr->global_pos += wr->header->dict_size; |
| 329 | wr->flush((char *)wr->buffer, wr->header->dict_size); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
| 334 | static inline void INIT copy_byte(struct writer *wr, uint32_t offs) |
| 335 | { |
| 336 | write_byte(wr, peek_old_byte(wr, offs)); |
| 337 | } |
| 338 | |
| 339 | static inline void INIT copy_bytes(struct writer *wr, |
| 340 | uint32_t rep0, int len) |
| 341 | { |
| 342 | do { |
| 343 | copy_byte(wr, rep0); |
| 344 | len--; |
| 345 | } while (len != 0 && wr->buffer_pos < wr->header->dst_size); |
| 346 | } |
| 347 | |
| 348 | static inline void INIT process_bit0(struct writer *wr, struct rc *rc, |
| 349 | struct cstate *cst, uint16_t *p, |
| 350 | int pos_state, uint16_t *prob, |
| 351 | int lc, uint32_t literal_pos_mask) { |
| 352 | int mi = 1; |
| 353 | rc_update_bit_0(rc, prob); |
| 354 | prob = (p + LZMA_LITERAL + |
| 355 | (LZMA_LIT_SIZE |
| 356 | * (((get_pos(wr) & literal_pos_mask) << lc) |
| 357 | + (wr->previous_byte >> (8 - lc)))) |
| 358 | ); |
| 359 | |
| 360 | if (cst->state >= LZMA_NUM_LIT_STATES) { |
| 361 | int match_byte = peek_old_byte(wr, cst->rep0); |
| 362 | do { |
| 363 | int bit; |
| 364 | uint16_t *prob_lit; |
| 365 | |
| 366 | match_byte <<= 1; |
| 367 | bit = match_byte & 0x100; |
| 368 | prob_lit = prob + 0x100 + bit + mi; |
| 369 | if (rc_get_bit(rc, prob_lit, &mi)) { |
| 370 | if (!bit) |
| 371 | break; |
| 372 | } else { |
| 373 | if (bit) |
| 374 | break; |
| 375 | } |
| 376 | } while (mi < 0x100); |
| 377 | } |
| 378 | while (mi < 0x100) { |
| 379 | uint16_t *prob_lit = prob + mi; |
| 380 | rc_get_bit(rc, prob_lit, &mi); |
| 381 | } |
| 382 | write_byte(wr, mi); |
| 383 | if (cst->state < 4) |
| 384 | cst->state = 0; |
| 385 | else if (cst->state < 10) |
| 386 | cst->state -= 3; |
| 387 | else |
| 388 | cst->state -= 6; |
| 389 | } |
| 390 | |
| 391 | static inline void INIT process_bit1(struct writer *wr, struct rc *rc, |
| 392 | struct cstate *cst, uint16_t *p, |
| 393 | int pos_state, uint16_t *prob) { |
| 394 | int offset; |
| 395 | uint16_t *prob_len; |
| 396 | int num_bits; |
| 397 | int len; |
| 398 | |
| 399 | rc_update_bit_1(rc, prob); |
| 400 | prob = p + LZMA_IS_REP + cst->state; |
| 401 | if (rc_is_bit_0(rc, prob)) { |
| 402 | rc_update_bit_0(rc, prob); |
| 403 | cst->rep3 = cst->rep2; |
| 404 | cst->rep2 = cst->rep1; |
| 405 | cst->rep1 = cst->rep0; |
| 406 | cst->state = cst->state < LZMA_NUM_LIT_STATES ? 0 : 3; |
| 407 | prob = p + LZMA_LEN_CODER; |
| 408 | } else { |
| 409 | rc_update_bit_1(rc, prob); |
| 410 | prob = p + LZMA_IS_REP_G0 + cst->state; |
| 411 | if (rc_is_bit_0(rc, prob)) { |
| 412 | rc_update_bit_0(rc, prob); |
| 413 | prob = (p + LZMA_IS_REP_0_LONG |
| 414 | + (cst->state << |
| 415 | LZMA_NUM_POS_BITS_MAX) + |
| 416 | pos_state); |
| 417 | if (rc_is_bit_0(rc, prob)) { |
| 418 | rc_update_bit_0(rc, prob); |
| 419 | |
| 420 | cst->state = cst->state < LZMA_NUM_LIT_STATES ? |
| 421 | 9 : 11; |
| 422 | copy_byte(wr, cst->rep0); |
| 423 | return; |
| 424 | } else { |
| 425 | rc_update_bit_1(rc, prob); |
| 426 | } |
| 427 | } else { |
| 428 | uint32_t distance; |
| 429 | |
| 430 | rc_update_bit_1(rc, prob); |
| 431 | prob = p + LZMA_IS_REP_G1 + cst->state; |
| 432 | if (rc_is_bit_0(rc, prob)) { |
| 433 | rc_update_bit_0(rc, prob); |
| 434 | distance = cst->rep1; |
| 435 | } else { |
| 436 | rc_update_bit_1(rc, prob); |
| 437 | prob = p + LZMA_IS_REP_G2 + cst->state; |
| 438 | if (rc_is_bit_0(rc, prob)) { |
| 439 | rc_update_bit_0(rc, prob); |
| 440 | distance = cst->rep2; |
| 441 | } else { |
| 442 | rc_update_bit_1(rc, prob); |
| 443 | distance = cst->rep3; |
| 444 | cst->rep3 = cst->rep2; |
| 445 | } |
| 446 | cst->rep2 = cst->rep1; |
| 447 | } |
| 448 | cst->rep1 = cst->rep0; |
| 449 | cst->rep0 = distance; |
| 450 | } |
| 451 | cst->state = cst->state < LZMA_NUM_LIT_STATES ? 8 : 11; |
| 452 | prob = p + LZMA_REP_LEN_CODER; |
| 453 | } |
| 454 | |
| 455 | prob_len = prob + LZMA_LEN_CHOICE; |
| 456 | if (rc_is_bit_0(rc, prob_len)) { |
| 457 | rc_update_bit_0(rc, prob_len); |
| 458 | prob_len = (prob + LZMA_LEN_LOW |
| 459 | + (pos_state << |
| 460 | LZMA_LEN_NUM_LOW_BITS)); |
| 461 | offset = 0; |
| 462 | num_bits = LZMA_LEN_NUM_LOW_BITS; |
| 463 | } else { |
| 464 | rc_update_bit_1(rc, prob_len); |
| 465 | prob_len = prob + LZMA_LEN_CHOICE_2; |
| 466 | if (rc_is_bit_0(rc, prob_len)) { |
| 467 | rc_update_bit_0(rc, prob_len); |
| 468 | prob_len = (prob + LZMA_LEN_MID |
| 469 | + (pos_state << |
| 470 | LZMA_LEN_NUM_MID_BITS)); |
| 471 | offset = 1 << LZMA_LEN_NUM_LOW_BITS; |
| 472 | num_bits = LZMA_LEN_NUM_MID_BITS; |
| 473 | } else { |
| 474 | rc_update_bit_1(rc, prob_len); |
| 475 | prob_len = prob + LZMA_LEN_HIGH; |
| 476 | offset = ((1 << LZMA_LEN_NUM_LOW_BITS) |
| 477 | + (1 << LZMA_LEN_NUM_MID_BITS)); |
| 478 | num_bits = LZMA_LEN_NUM_HIGH_BITS; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | rc_bit_tree_decode(rc, prob_len, num_bits, &len); |
| 483 | len += offset; |
| 484 | |
| 485 | if (cst->state < 4) { |
| 486 | int pos_slot; |
| 487 | |
| 488 | cst->state += LZMA_NUM_LIT_STATES; |
| 489 | prob = |
| 490 | p + LZMA_POS_SLOT + |
| 491 | ((len < |
| 492 | LZMA_NUM_LEN_TO_POS_STATES ? len : |
| 493 | LZMA_NUM_LEN_TO_POS_STATES - 1) |
| 494 | << LZMA_NUM_POS_SLOT_BITS); |
| 495 | rc_bit_tree_decode(rc, prob, |
| 496 | LZMA_NUM_POS_SLOT_BITS, |
| 497 | &pos_slot); |
| 498 | if (pos_slot >= LZMA_START_POS_MODEL_INDEX) { |
| 499 | int i, mi; |
| 500 | num_bits = (pos_slot >> 1) - 1; |
| 501 | cst->rep0 = 2 | (pos_slot & 1); |
| 502 | if (pos_slot < LZMA_END_POS_MODEL_INDEX) { |
| 503 | cst->rep0 <<= num_bits; |
| 504 | prob = p + LZMA_SPEC_POS + |
| 505 | cst->rep0 - pos_slot - 1; |
| 506 | } else { |
| 507 | num_bits -= LZMA_NUM_ALIGN_BITS; |
| 508 | while (num_bits--) |
| 509 | cst->rep0 = (cst->rep0 << 1) | |
| 510 | rc_direct_bit(rc); |
| 511 | prob = p + LZMA_ALIGN; |
| 512 | cst->rep0 <<= LZMA_NUM_ALIGN_BITS; |
| 513 | num_bits = LZMA_NUM_ALIGN_BITS; |
| 514 | } |
| 515 | i = 1; |
| 516 | mi = 1; |
| 517 | while (num_bits--) { |
| 518 | if (rc_get_bit(rc, prob + mi, &mi)) |
| 519 | cst->rep0 |= i; |
| 520 | i <<= 1; |
| 521 | } |
| 522 | } else |
| 523 | cst->rep0 = pos_slot; |
| 524 | if (++(cst->rep0) == 0) |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | len += LZMA_MATCH_MIN_LEN; |
| 529 | |
| 530 | copy_bytes(wr, cst->rep0, len); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | |
| 535 | STATIC inline int INIT unlzma(unsigned char *buf, int in_len, |
| 536 | int(*fill)(void*, unsigned int), |
| 537 | int(*flush)(void*, unsigned int), |
| 538 | unsigned char *output, |
| 539 | int *posp, |
Lasse Collin | 93685ad2 | 2011-01-12 17:01:14 -0800 | [diff] [blame^] | 540 | void(*error)(char *x) |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 541 | ) |
| 542 | { |
| 543 | struct lzma_header header; |
| 544 | int lc, pb, lp; |
| 545 | uint32_t pos_state_mask; |
| 546 | uint32_t literal_pos_mask; |
| 547 | uint16_t *p; |
| 548 | int num_probs; |
| 549 | struct rc rc; |
| 550 | int i, mi; |
| 551 | struct writer wr; |
| 552 | struct cstate cst; |
| 553 | unsigned char *inbuf; |
| 554 | int ret = -1; |
| 555 | |
Lasse Collin | 93685ad2 | 2011-01-12 17:01:14 -0800 | [diff] [blame^] | 556 | rc.error = error; |
Phillip Lougher | b1af431 | 2009-08-06 15:09:31 -0700 | [diff] [blame] | 557 | |
Alain Knaff | bc22c17 | 2009-01-04 22:46:16 +0100 | [diff] [blame] | 558 | if (buf) |
| 559 | inbuf = buf; |
| 560 | else |
| 561 | inbuf = malloc(LZMA_IOBUF_SIZE); |
| 562 | if (!inbuf) { |
| 563 | error("Could not allocate input bufer"); |
| 564 | goto exit_0; |
| 565 | } |
| 566 | |
| 567 | cst.state = 0; |
| 568 | cst.rep0 = cst.rep1 = cst.rep2 = cst.rep3 = 1; |
| 569 | |
| 570 | wr.header = &header; |
| 571 | wr.flush = flush; |
| 572 | wr.global_pos = 0; |
| 573 | wr.previous_byte = 0; |
| 574 | wr.buffer_pos = 0; |
| 575 | |
| 576 | rc_init(&rc, fill, inbuf, in_len); |
| 577 | |
| 578 | for (i = 0; i < sizeof(header); i++) { |
| 579 | if (rc.ptr >= rc.buffer_end) |
| 580 | rc_read(&rc); |
| 581 | ((unsigned char *)&header)[i] = *rc.ptr++; |
| 582 | } |
| 583 | |
| 584 | if (header.pos >= (9 * 5 * 5)) |
| 585 | error("bad header"); |
| 586 | |
| 587 | mi = 0; |
| 588 | lc = header.pos; |
| 589 | while (lc >= 9) { |
| 590 | mi++; |
| 591 | lc -= 9; |
| 592 | } |
| 593 | pb = 0; |
| 594 | lp = mi; |
| 595 | while (lp >= 5) { |
| 596 | pb++; |
| 597 | lp -= 5; |
| 598 | } |
| 599 | pos_state_mask = (1 << pb) - 1; |
| 600 | literal_pos_mask = (1 << lp) - 1; |
| 601 | |
| 602 | ENDIAN_CONVERT(header.dict_size); |
| 603 | ENDIAN_CONVERT(header.dst_size); |
| 604 | |
| 605 | if (header.dict_size == 0) |
| 606 | header.dict_size = 1; |
| 607 | |
| 608 | if (output) |
| 609 | wr.buffer = output; |
| 610 | else { |
| 611 | wr.bufsize = MIN(header.dst_size, header.dict_size); |
| 612 | wr.buffer = large_malloc(wr.bufsize); |
| 613 | } |
| 614 | if (wr.buffer == NULL) |
| 615 | goto exit_1; |
| 616 | |
| 617 | num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)); |
| 618 | p = (uint16_t *) large_malloc(num_probs * sizeof(*p)); |
| 619 | if (p == 0) |
| 620 | goto exit_2; |
| 621 | num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp)); |
| 622 | for (i = 0; i < num_probs; i++) |
| 623 | p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1; |
| 624 | |
| 625 | rc_init_code(&rc); |
| 626 | |
| 627 | while (get_pos(&wr) < header.dst_size) { |
| 628 | int pos_state = get_pos(&wr) & pos_state_mask; |
| 629 | uint16_t *prob = p + LZMA_IS_MATCH + |
| 630 | (cst.state << LZMA_NUM_POS_BITS_MAX) + pos_state; |
| 631 | if (rc_is_bit_0(&rc, prob)) |
| 632 | process_bit0(&wr, &rc, &cst, p, pos_state, prob, |
| 633 | lc, literal_pos_mask); |
| 634 | else { |
| 635 | process_bit1(&wr, &rc, &cst, p, pos_state, prob); |
| 636 | if (cst.rep0 == 0) |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (posp) |
| 642 | *posp = rc.ptr-rc.buffer; |
| 643 | if (wr.flush) |
| 644 | wr.flush(wr.buffer, wr.buffer_pos); |
| 645 | ret = 0; |
| 646 | large_free(p); |
| 647 | exit_2: |
| 648 | if (!output) |
| 649 | large_free(wr.buffer); |
| 650 | exit_1: |
| 651 | if (!buf) |
| 652 | free(inbuf); |
| 653 | exit_0: |
| 654 | return ret; |
| 655 | } |
| 656 | |
Phillip Lougher | b1af431 | 2009-08-06 15:09:31 -0700 | [diff] [blame] | 657 | #ifdef PREBOOT |
| 658 | STATIC int INIT decompress(unsigned char *buf, int in_len, |
| 659 | int(*fill)(void*, unsigned int), |
| 660 | int(*flush)(void*, unsigned int), |
| 661 | unsigned char *output, |
| 662 | int *posp, |
Lasse Collin | 93685ad2 | 2011-01-12 17:01:14 -0800 | [diff] [blame^] | 663 | void(*error)(char *x) |
Phillip Lougher | b1af431 | 2009-08-06 15:09:31 -0700 | [diff] [blame] | 664 | ) |
| 665 | { |
Lasse Collin | 93685ad2 | 2011-01-12 17:01:14 -0800 | [diff] [blame^] | 666 | return unlzma(buf, in_len - 4, fill, flush, output, posp, error); |
Phillip Lougher | b1af431 | 2009-08-06 15:09:31 -0700 | [diff] [blame] | 667 | } |
| 668 | #endif |