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