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