1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#ifdef STATIC
49#define PREBOOT
50#else
51#include <linux/decompress/bunzip2.h>
52#include <linux/slab.h>
53#endif
54
55#include <linux/decompress/mm.h>
56
57#ifndef INT_MAX
58#define INT_MAX 0x7fffffff
59#endif
60
61
62#define MAX_GROUPS 6
63#define GROUP_SIZE 50
64#define MAX_HUFCODE_BITS 20
65#define MAX_SYMBOLS 258
66#define SYMBOL_RUNA 0
67#define SYMBOL_RUNB 1
68
69
70#define RETVAL_OK 0
71#define RETVAL_LAST_BLOCK (-1)
72#define RETVAL_NOT_BZIP_DATA (-2)
73#define RETVAL_UNEXPECTED_INPUT_EOF (-3)
74#define RETVAL_UNEXPECTED_OUTPUT_EOF (-4)
75#define RETVAL_DATA_ERROR (-5)
76#define RETVAL_OUT_OF_MEMORY (-6)
77#define RETVAL_OBSOLETE_INPUT (-7)
78
79
80#define BZIP2_IOBUF_SIZE 4096
81
82
83struct group_data {
84
85 int limit[MAX_HUFCODE_BITS+1];
86 int base[MAX_HUFCODE_BITS];
87 int permute[MAX_SYMBOLS];
88 int minLen, maxLen;
89};
90
91
92
93struct bunzip_data {
94
95 int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
96
97 int (*fill)(void*, unsigned int);
98 int inbufCount, inbufPos ;
99 unsigned char *inbuf ;
100 unsigned int inbufBitCount, inbufBits;
101
102
103 unsigned int crc32Table[256], headerCRC, totalCRC, writeCRC;
104
105 unsigned int *dbuf, dbufSize;
106
107 unsigned char selectors[32768];
108 struct group_data groups[MAX_GROUPS];
109 int io_error;
110};
111
112
113
114
115static unsigned int INIT get_bits(struct bunzip_data *bd, char bits_wanted)
116{
117 unsigned int bits = 0;
118
119
120
121
122 while (bd->inbufBitCount < bits_wanted) {
123
124
125 if (bd->inbufPos == bd->inbufCount) {
126 if (bd->io_error)
127 return 0;
128 bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE);
129 if (bd->inbufCount <= 0) {
130 bd->io_error = RETVAL_UNEXPECTED_INPUT_EOF;
131 return 0;
132 }
133 bd->inbufPos = 0;
134 }
135
136 if (bd->inbufBitCount >= 24) {
137 bits = bd->inbufBits&((1 << bd->inbufBitCount)-1);
138 bits_wanted -= bd->inbufBitCount;
139 bits <<= bits_wanted;
140 bd->inbufBitCount = 0;
141 }
142
143 bd->inbufBits = (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
144 bd->inbufBitCount += 8;
145 }
146
147 bd->inbufBitCount -= bits_wanted;
148 bits |= (bd->inbufBits >> bd->inbufBitCount)&((1 << bits_wanted)-1);
149
150 return bits;
151}
152
153
154
155static int INIT get_next_block(struct bunzip_data *bd)
156{
157 struct group_data *hufGroup = NULL;
158 int *base = NULL;
159 int *limit = NULL;
160 int dbufCount, nextSym, dbufSize, groupCount, selector,
161 i, j, k, t, runPos, symCount, symTotal, nSelectors,
162 byteCount[256];
163 unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
164 unsigned int *dbuf, origPtr;
165
166 dbuf = bd->dbuf;
167 dbufSize = bd->dbufSize;
168 selectors = bd->selectors;
169
170
171
172 i = get_bits(bd, 24);
173 j = get_bits(bd, 24);
174 bd->headerCRC = get_bits(bd, 32);
175 if ((i == 0x177245) && (j == 0x385090))
176 return RETVAL_LAST_BLOCK;
177 if ((i != 0x314159) || (j != 0x265359))
178 return RETVAL_NOT_BZIP_DATA;
179
180
181
182 if (get_bits(bd, 1))
183 return RETVAL_OBSOLETE_INPUT;
184 origPtr = get_bits(bd, 24);
185 if (origPtr > dbufSize)
186 return RETVAL_DATA_ERROR;
187
188
189
190
191
192 t = get_bits(bd, 16);
193 symTotal = 0;
194 for (i = 0; i < 16; i++) {
195 if (t&(1 << (15-i))) {
196 k = get_bits(bd, 16);
197 for (j = 0; j < 16; j++)
198 if (k&(1 << (15-j)))
199 symToByte[symTotal++] = (16*i)+j;
200 }
201 }
202
203 groupCount = get_bits(bd, 3);
204 if (groupCount < 2 || groupCount > MAX_GROUPS)
205 return RETVAL_DATA_ERROR;
206
207
208
209
210
211 nSelectors = get_bits(bd, 15);
212 if (!nSelectors)
213 return RETVAL_DATA_ERROR;
214 for (i = 0; i < groupCount; i++)
215 mtfSymbol[i] = i;
216 for (i = 0; i < nSelectors; i++) {
217
218 for (j = 0; get_bits(bd, 1); j++)
219 if (j >= groupCount)
220 return RETVAL_DATA_ERROR;
221
222 uc = mtfSymbol[j];
223 for (; j; j--)
224 mtfSymbol[j] = mtfSymbol[j-1];
225 mtfSymbol[0] = selectors[i] = uc;
226 }
227
228
229
230 symCount = symTotal+2;
231 for (j = 0; j < groupCount; j++) {
232 unsigned char length[MAX_SYMBOLS], temp[MAX_HUFCODE_BITS+1];
233 int minLen, maxLen, pp;
234
235
236
237
238
239
240
241
242
243 t = get_bits(bd, 5)-1;
244 for (i = 0; i < symCount; i++) {
245 for (;;) {
246 if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
247 return RETVAL_DATA_ERROR;
248
249
250
251
252
253
254
255 k = get_bits(bd, 2);
256 if (k < 2) {
257 bd->inbufBitCount++;
258 break;
259 }
260
261
262 t += (((k+1)&2)-1);
263 }
264
265
266 length[i] = t+1;
267 }
268
269 minLen = maxLen = length[0];
270
271 for (i = 1; i < symCount; i++) {
272 if (length[i] > maxLen)
273 maxLen = length[i];
274 else if (length[i] < minLen)
275 minLen = length[i];
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 hufGroup = bd->groups+j;
294 hufGroup->minLen = minLen;
295 hufGroup->maxLen = maxLen;
296
297
298
299
300 base = hufGroup->base-1;
301 limit = hufGroup->limit-1;
302
303
304 pp = 0;
305 for (i = minLen; i <= maxLen; i++) {
306 temp[i] = limit[i] = 0;
307 for (t = 0; t < symCount; t++)
308 if (length[t] == i)
309 hufGroup->permute[pp++] = t;
310 }
311
312 for (i = 0; i < symCount; i++)
313 temp[length[i]]++;
314
315
316
317
318
319
320 pp = t = 0;
321 for (i = minLen; i < maxLen; i++) {
322 pp += temp[i];
323
324
325
326
327
328
329
330
331
332
333 limit[i] = (pp << (maxLen - i)) - 1;
334 pp <<= 1;
335 base[i+1] = pp-(t += temp[i]);
336 }
337 limit[maxLen+1] = INT_MAX;
338
339 limit[maxLen] = pp+temp[maxLen]-1;
340 base[minLen] = 0;
341 }
342
343
344
345
346
347
348
349 for (i = 0; i < 256; i++) {
350 byteCount[i] = 0;
351 mtfSymbol[i] = (unsigned char)i;
352 }
353
354 runPos = dbufCount = symCount = selector = 0;
355 for (;;) {
356
357 if (!(symCount--)) {
358 symCount = GROUP_SIZE-1;
359 if (selector >= nSelectors)
360 return RETVAL_DATA_ERROR;
361 hufGroup = bd->groups+selectors[selector++];
362 base = hufGroup->base-1;
363 limit = hufGroup->limit-1;
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377 while (bd->inbufBitCount < hufGroup->maxLen) {
378 if (bd->inbufPos == bd->inbufCount) {
379 j = get_bits(bd, hufGroup->maxLen);
380 goto got_huff_bits;
381 }
382 bd->inbufBits =
383 (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
384 bd->inbufBitCount += 8;
385 };
386 bd->inbufBitCount -= hufGroup->maxLen;
387 j = (bd->inbufBits >> bd->inbufBitCount)&
388 ((1 << hufGroup->maxLen)-1);
389got_huff_bits:
390
391
392 i = hufGroup->minLen;
393 while (j > limit[i])
394 ++i;
395 bd->inbufBitCount += (hufGroup->maxLen - i);
396
397 if ((i > hufGroup->maxLen)
398 || (((unsigned)(j = (j>>(hufGroup->maxLen-i))-base[i]))
399 >= MAX_SYMBOLS))
400 return RETVAL_DATA_ERROR;
401 nextSym = hufGroup->permute[j];
402
403
404
405
406
407 if (((unsigned)nextSym) <= SYMBOL_RUNB) {
408
409
410 if (!runPos) {
411 runPos = 1;
412 t = 0;
413 }
414
415
416
417
418
419
420
421
422
423
424 t += (runPos << nextSym);
425
426
427 runPos <<= 1;
428 continue;
429 }
430
431
432
433
434
435
436 if (runPos) {
437 runPos = 0;
438 if (dbufCount+t >= dbufSize)
439 return RETVAL_DATA_ERROR;
440
441 uc = symToByte[mtfSymbol[0]];
442 byteCount[uc] += t;
443 while (t--)
444 dbuf[dbufCount++] = uc;
445 }
446
447 if (nextSym > symTotal)
448 break;
449
450
451
452
453
454
455
456
457
458 if (dbufCount >= dbufSize)
459 return RETVAL_DATA_ERROR;
460 i = nextSym - 1;
461 uc = mtfSymbol[i];
462
463
464
465
466
467 do {
468 mtfSymbol[i] = mtfSymbol[i-1];
469 } while (--i);
470 mtfSymbol[0] = uc;
471 uc = symToByte[uc];
472
473 byteCount[uc]++;
474 dbuf[dbufCount++] = (unsigned int)uc;
475 }
476
477
478
479
480
481
482
483
484 j = 0;
485 for (i = 0; i < 256; i++) {
486 k = j+byteCount[i];
487 byteCount[i] = j;
488 j = k;
489 }
490
491 for (i = 0; i < dbufCount; i++) {
492 uc = (unsigned char)(dbuf[i] & 0xff);
493 dbuf[byteCount[uc]] |= (i << 8);
494 byteCount[uc]++;
495 }
496
497
498
499
500 if (dbufCount) {
501 if (origPtr >= dbufCount)
502 return RETVAL_DATA_ERROR;
503 bd->writePos = dbuf[origPtr];
504 bd->writeCurrent = (unsigned char)(bd->writePos&0xff);
505 bd->writePos >>= 8;
506 bd->writeRunCountdown = 5;
507 }
508 bd->writeCount = dbufCount;
509
510 return RETVAL_OK;
511}
512
513
514
515
516
517
518
519
520static int INIT read_bunzip(struct bunzip_data *bd, char *outbuf, int len)
521{
522 const unsigned int *dbuf;
523 int pos, xcurrent, previous, gotcount;
524
525
526 if (bd->writeCount < 0)
527 return bd->writeCount;
528
529 gotcount = 0;
530 dbuf = bd->dbuf;
531 pos = bd->writePos;
532 xcurrent = bd->writeCurrent;
533
534
535
536
537
538 if (bd->writeCopies) {
539
540 --bd->writeCopies;
541
542 for (;;) {
543
544
545 if (gotcount >= len) {
546 bd->writePos = pos;
547 bd->writeCurrent = xcurrent;
548 bd->writeCopies++;
549 return len;
550 }
551
552 outbuf[gotcount++] = xcurrent;
553 bd->writeCRC = (((bd->writeCRC) << 8)
554 ^bd->crc32Table[((bd->writeCRC) >> 24)
555 ^xcurrent]);
556
557
558 if (bd->writeCopies) {
559 --bd->writeCopies;
560 continue;
561 }
562decode_next_byte:
563 if (!bd->writeCount--)
564 break;
565
566
567 previous = xcurrent;
568 pos = dbuf[pos];
569 xcurrent = pos&0xff;
570 pos >>= 8;
571
572
573
574
575 if (--bd->writeRunCountdown) {
576 if (xcurrent != previous)
577 bd->writeRunCountdown = 4;
578 } else {
579
580
581 bd->writeCopies = xcurrent;
582 xcurrent = previous;
583 bd->writeRunCountdown = 5;
584
585
586 if (!bd->writeCopies)
587 goto decode_next_byte;
588
589
590 --bd->writeCopies;
591 }
592 }
593
594 bd->writeCRC = ~bd->writeCRC;
595 bd->totalCRC = ((bd->totalCRC << 1) |
596 (bd->totalCRC >> 31)) ^ bd->writeCRC;
597
598 if (bd->writeCRC != bd->headerCRC) {
599 bd->totalCRC = bd->headerCRC+1;
600 return RETVAL_LAST_BLOCK;
601 }
602 }
603
604
605
606
607 previous = get_next_block(bd);
608 if (previous) {
609 bd->writeCount = previous;
610 return (previous != RETVAL_LAST_BLOCK) ? previous : gotcount;
611 }
612 bd->writeCRC = 0xffffffffUL;
613 pos = bd->writePos;
614 xcurrent = bd->writeCurrent;
615 goto decode_next_byte;
616}
617
618static int INIT nofill(void *buf, unsigned int len)
619{
620 return -1;
621}
622
623
624
625
626static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
627 int (*fill)(void*, unsigned int))
628{
629 struct bunzip_data *bd;
630 unsigned int i, j, c;
631 const unsigned int BZh0 =
632 (((unsigned int)'B') << 24)+(((unsigned int)'Z') << 16)
633 +(((unsigned int)'h') << 8)+(unsigned int)'0';
634
635
636 i = sizeof(struct bunzip_data);
637
638
639 bd = *bdp = malloc(i);
640 if (!bd)
641 return RETVAL_OUT_OF_MEMORY;
642 memset(bd, 0, sizeof(struct bunzip_data));
643
644 bd->inbuf = inbuf;
645 bd->inbufCount = len;
646 if (fill != NULL)
647 bd->fill = fill;
648 else
649 bd->fill = nofill;
650
651
652 for (i = 0; i < 256; i++) {
653 c = i << 24;
654 for (j = 8; j; j--)
655 c = c&0x80000000 ? (c << 1)^0x04c11db7 : (c << 1);
656 bd->crc32Table[i] = c;
657 }
658
659
660 i = get_bits(bd, 32);
661 if (((unsigned int)(i-BZh0-1)) >= 9)
662 return RETVAL_NOT_BZIP_DATA;
663
664
665
666 bd->dbufSize = 100000*(i-BZh0);
667
668 bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
669 if (!bd->dbuf)
670 return RETVAL_OUT_OF_MEMORY;
671 return RETVAL_OK;
672}
673
674
675
676STATIC int INIT bunzip2(unsigned char *buf, int len,
677 int(*fill)(void*, unsigned int),
678 int(*flush)(void*, unsigned int),
679 unsigned char *outbuf,
680 int *pos,
681 void(*error_fn)(char *x))
682{
683 struct bunzip_data *bd;
684 int i = -1;
685 unsigned char *inbuf;
686
687 set_error_fn(error_fn);
688 if (flush)
689 outbuf = malloc(BZIP2_IOBUF_SIZE);
690
691 if (!outbuf) {
692 error("Could not allocate output bufer");
693 return RETVAL_OUT_OF_MEMORY;
694 }
695 if (buf)
696 inbuf = buf;
697 else
698 inbuf = malloc(BZIP2_IOBUF_SIZE);
699 if (!inbuf) {
700 error("Could not allocate input bufer");
701 i = RETVAL_OUT_OF_MEMORY;
702 goto exit_0;
703 }
704 i = start_bunzip(&bd, inbuf, len, fill);
705 if (!i) {
706 for (;;) {
707 i = read_bunzip(bd, outbuf, BZIP2_IOBUF_SIZE);
708 if (i <= 0)
709 break;
710 if (!flush)
711 outbuf += i;
712 else
713 if (i != flush(outbuf, i)) {
714 i = RETVAL_UNEXPECTED_OUTPUT_EOF;
715 break;
716 }
717 }
718 }
719
720 if (i == RETVAL_LAST_BLOCK) {
721 if (bd->headerCRC != bd->totalCRC)
722 error("Data integrity error when decompressing.");
723 else
724 i = RETVAL_OK;
725 } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
726 error("Compressed file ends unexpectedly");
727 }
728 if (!bd)
729 goto exit_1;
730 if (bd->dbuf)
731 large_free(bd->dbuf);
732 if (pos)
733 *pos = bd->inbufPos;
734 free(bd);
735exit_1:
736 if (!buf)
737 free(inbuf);
738exit_0:
739 if (flush)
740 free(outbuf);
741 return i;
742}
743
744#ifdef PREBOOT
745STATIC int INIT decompress(unsigned char *buf, int len,
746 int(*fill)(void*, unsigned int),
747 int(*flush)(void*, unsigned int),
748 unsigned char *outbuf,
749 int *pos,
750 void(*error_fn)(char *x))
751{
752 return bunzip2(buf, len - 4, fill, flush, outbuf, pos, error_fn);
753}
754#endif
755