0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 #ifdef STATIC
0048 #define PREBOOT
0049 #else
0050 #include <linux/decompress/bunzip2.h>
0051 #endif
0052
0053 #include <linux/decompress/mm.h>
0054 #include <linux/crc32poly.h>
0055
0056 #ifndef INT_MAX
0057 #define INT_MAX 0x7fffffff
0058 #endif
0059
0060
0061 #define MAX_GROUPS 6
0062 #define GROUP_SIZE 50
0063 #define MAX_HUFCODE_BITS 20
0064 #define MAX_SYMBOLS 258
0065 #define SYMBOL_RUNA 0
0066 #define SYMBOL_RUNB 1
0067
0068
0069 #define RETVAL_OK 0
0070 #define RETVAL_LAST_BLOCK (-1)
0071 #define RETVAL_NOT_BZIP_DATA (-2)
0072 #define RETVAL_UNEXPECTED_INPUT_EOF (-3)
0073 #define RETVAL_UNEXPECTED_OUTPUT_EOF (-4)
0074 #define RETVAL_DATA_ERROR (-5)
0075 #define RETVAL_OUT_OF_MEMORY (-6)
0076 #define RETVAL_OBSOLETE_INPUT (-7)
0077
0078
0079 #define BZIP2_IOBUF_SIZE 4096
0080
0081
0082 struct group_data {
0083
0084 int limit[MAX_HUFCODE_BITS+1];
0085 int base[MAX_HUFCODE_BITS];
0086 int permute[MAX_SYMBOLS];
0087 int minLen, maxLen;
0088 };
0089
0090
0091
0092 struct bunzip_data {
0093
0094 int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
0095
0096 long (*fill)(void*, unsigned long);
0097 long inbufCount, inbufPos ;
0098 unsigned char *inbuf ;
0099 unsigned int inbufBitCount, inbufBits;
0100
0101
0102 unsigned int crc32Table[256], headerCRC, totalCRC, writeCRC;
0103
0104 unsigned int *dbuf, dbufSize;
0105
0106 unsigned char selectors[32768];
0107 struct group_data groups[MAX_GROUPS];
0108 int io_error;
0109 int byteCount[256];
0110 unsigned char symToByte[256], mtfSymbol[256];
0111 };
0112
0113
0114
0115
0116 static unsigned int INIT get_bits(struct bunzip_data *bd, char bits_wanted)
0117 {
0118 unsigned int bits = 0;
0119
0120
0121
0122
0123 while (bd->inbufBitCount < bits_wanted) {
0124
0125
0126 if (bd->inbufPos == bd->inbufCount) {
0127 if (bd->io_error)
0128 return 0;
0129 bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE);
0130 if (bd->inbufCount <= 0) {
0131 bd->io_error = RETVAL_UNEXPECTED_INPUT_EOF;
0132 return 0;
0133 }
0134 bd->inbufPos = 0;
0135 }
0136
0137 if (bd->inbufBitCount >= 24) {
0138 bits = bd->inbufBits&((1 << bd->inbufBitCount)-1);
0139 bits_wanted -= bd->inbufBitCount;
0140 bits <<= bits_wanted;
0141 bd->inbufBitCount = 0;
0142 }
0143
0144 bd->inbufBits = (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
0145 bd->inbufBitCount += 8;
0146 }
0147
0148 bd->inbufBitCount -= bits_wanted;
0149 bits |= (bd->inbufBits >> bd->inbufBitCount)&((1 << bits_wanted)-1);
0150
0151 return bits;
0152 }
0153
0154
0155
0156 static int INIT get_next_block(struct bunzip_data *bd)
0157 {
0158 struct group_data *hufGroup = NULL;
0159 int *base = NULL;
0160 int *limit = NULL;
0161 int dbufCount, nextSym, dbufSize, groupCount, selector,
0162 i, j, k, t, runPos, symCount, symTotal, nSelectors, *byteCount;
0163 unsigned char uc, *symToByte, *mtfSymbol, *selectors;
0164 unsigned int *dbuf, origPtr;
0165
0166 dbuf = bd->dbuf;
0167 dbufSize = bd->dbufSize;
0168 selectors = bd->selectors;
0169 byteCount = bd->byteCount;
0170 symToByte = bd->symToByte;
0171 mtfSymbol = bd->mtfSymbol;
0172
0173
0174
0175 i = get_bits(bd, 24);
0176 j = get_bits(bd, 24);
0177 bd->headerCRC = get_bits(bd, 32);
0178 if ((i == 0x177245) && (j == 0x385090))
0179 return RETVAL_LAST_BLOCK;
0180 if ((i != 0x314159) || (j != 0x265359))
0181 return RETVAL_NOT_BZIP_DATA;
0182
0183
0184
0185 if (get_bits(bd, 1))
0186 return RETVAL_OBSOLETE_INPUT;
0187 origPtr = get_bits(bd, 24);
0188 if (origPtr >= dbufSize)
0189 return RETVAL_DATA_ERROR;
0190
0191
0192
0193
0194
0195 t = get_bits(bd, 16);
0196 symTotal = 0;
0197 for (i = 0; i < 16; i++) {
0198 if (t&(1 << (15-i))) {
0199 k = get_bits(bd, 16);
0200 for (j = 0; j < 16; j++)
0201 if (k&(1 << (15-j)))
0202 symToByte[symTotal++] = (16*i)+j;
0203 }
0204 }
0205
0206 groupCount = get_bits(bd, 3);
0207 if (groupCount < 2 || groupCount > MAX_GROUPS)
0208 return RETVAL_DATA_ERROR;
0209
0210
0211
0212
0213
0214 nSelectors = get_bits(bd, 15);
0215 if (!nSelectors)
0216 return RETVAL_DATA_ERROR;
0217 for (i = 0; i < groupCount; i++)
0218 mtfSymbol[i] = i;
0219 for (i = 0; i < nSelectors; i++) {
0220
0221 for (j = 0; get_bits(bd, 1); j++)
0222 if (j >= groupCount)
0223 return RETVAL_DATA_ERROR;
0224
0225 uc = mtfSymbol[j];
0226 for (; j; j--)
0227 mtfSymbol[j] = mtfSymbol[j-1];
0228 mtfSymbol[0] = selectors[i] = uc;
0229 }
0230
0231
0232
0233 symCount = symTotal+2;
0234 for (j = 0; j < groupCount; j++) {
0235 unsigned char length[MAX_SYMBOLS], temp[MAX_HUFCODE_BITS+1];
0236 int minLen, maxLen, pp;
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246 t = get_bits(bd, 5)-1;
0247 for (i = 0; i < symCount; i++) {
0248 for (;;) {
0249 if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
0250 return RETVAL_DATA_ERROR;
0251
0252
0253
0254
0255
0256
0257
0258 k = get_bits(bd, 2);
0259 if (k < 2) {
0260 bd->inbufBitCount++;
0261 break;
0262 }
0263
0264
0265 t += (((k+1)&2)-1);
0266 }
0267
0268
0269 length[i] = t+1;
0270 }
0271
0272 minLen = maxLen = length[0];
0273
0274 for (i = 1; i < symCount; i++) {
0275 if (length[i] > maxLen)
0276 maxLen = length[i];
0277 else if (length[i] < minLen)
0278 minLen = length[i];
0279 }
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 hufGroup = bd->groups+j;
0297 hufGroup->minLen = minLen;
0298 hufGroup->maxLen = maxLen;
0299
0300
0301
0302
0303 base = hufGroup->base-1;
0304 limit = hufGroup->limit-1;
0305
0306
0307 pp = 0;
0308 for (i = minLen; i <= maxLen; i++) {
0309 temp[i] = limit[i] = 0;
0310 for (t = 0; t < symCount; t++)
0311 if (length[t] == i)
0312 hufGroup->permute[pp++] = t;
0313 }
0314
0315 for (i = 0; i < symCount; i++)
0316 temp[length[i]]++;
0317
0318
0319
0320
0321
0322
0323 pp = t = 0;
0324 for (i = minLen; i < maxLen; i++) {
0325 pp += temp[i];
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336 limit[i] = (pp << (maxLen - i)) - 1;
0337 pp <<= 1;
0338 base[i+1] = pp-(t += temp[i]);
0339 }
0340 limit[maxLen+1] = INT_MAX;
0341
0342 limit[maxLen] = pp+temp[maxLen]-1;
0343 base[minLen] = 0;
0344 }
0345
0346
0347
0348
0349
0350
0351
0352 for (i = 0; i < 256; i++) {
0353 byteCount[i] = 0;
0354 mtfSymbol[i] = (unsigned char)i;
0355 }
0356
0357 runPos = dbufCount = symCount = selector = 0;
0358 for (;;) {
0359
0360 if (!(symCount--)) {
0361 symCount = GROUP_SIZE-1;
0362 if (selector >= nSelectors)
0363 return RETVAL_DATA_ERROR;
0364 hufGroup = bd->groups+selectors[selector++];
0365 base = hufGroup->base-1;
0366 limit = hufGroup->limit-1;
0367 }
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380 while (bd->inbufBitCount < hufGroup->maxLen) {
0381 if (bd->inbufPos == bd->inbufCount) {
0382 j = get_bits(bd, hufGroup->maxLen);
0383 goto got_huff_bits;
0384 }
0385 bd->inbufBits =
0386 (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
0387 bd->inbufBitCount += 8;
0388 }
0389 bd->inbufBitCount -= hufGroup->maxLen;
0390 j = (bd->inbufBits >> bd->inbufBitCount)&
0391 ((1 << hufGroup->maxLen)-1);
0392 got_huff_bits:
0393
0394
0395 i = hufGroup->minLen;
0396 while (j > limit[i])
0397 ++i;
0398 bd->inbufBitCount += (hufGroup->maxLen - i);
0399
0400 if ((i > hufGroup->maxLen)
0401 || (((unsigned)(j = (j>>(hufGroup->maxLen-i))-base[i]))
0402 >= MAX_SYMBOLS))
0403 return RETVAL_DATA_ERROR;
0404 nextSym = hufGroup->permute[j];
0405
0406
0407
0408
0409
0410 if (((unsigned)nextSym) <= SYMBOL_RUNB) {
0411
0412
0413 if (!runPos) {
0414 runPos = 1;
0415 t = 0;
0416 }
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427 t += (runPos << nextSym);
0428
0429
0430 runPos <<= 1;
0431 continue;
0432 }
0433
0434
0435
0436
0437
0438
0439 if (runPos) {
0440 runPos = 0;
0441 if (dbufCount+t >= dbufSize)
0442 return RETVAL_DATA_ERROR;
0443
0444 uc = symToByte[mtfSymbol[0]];
0445 byteCount[uc] += t;
0446 while (t--)
0447 dbuf[dbufCount++] = uc;
0448 }
0449
0450 if (nextSym > symTotal)
0451 break;
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461 if (dbufCount >= dbufSize)
0462 return RETVAL_DATA_ERROR;
0463 i = nextSym - 1;
0464 uc = mtfSymbol[i];
0465
0466
0467
0468
0469
0470 do {
0471 mtfSymbol[i] = mtfSymbol[i-1];
0472 } while (--i);
0473 mtfSymbol[0] = uc;
0474 uc = symToByte[uc];
0475
0476 byteCount[uc]++;
0477 dbuf[dbufCount++] = (unsigned int)uc;
0478 }
0479
0480
0481
0482
0483
0484
0485
0486
0487 j = 0;
0488 for (i = 0; i < 256; i++) {
0489 k = j+byteCount[i];
0490 byteCount[i] = j;
0491 j = k;
0492 }
0493
0494 for (i = 0; i < dbufCount; i++) {
0495 uc = (unsigned char)(dbuf[i] & 0xff);
0496 dbuf[byteCount[uc]] |= (i << 8);
0497 byteCount[uc]++;
0498 }
0499
0500
0501
0502
0503 if (dbufCount) {
0504 if (origPtr >= dbufCount)
0505 return RETVAL_DATA_ERROR;
0506 bd->writePos = dbuf[origPtr];
0507 bd->writeCurrent = (unsigned char)(bd->writePos&0xff);
0508 bd->writePos >>= 8;
0509 bd->writeRunCountdown = 5;
0510 }
0511 bd->writeCount = dbufCount;
0512
0513 return RETVAL_OK;
0514 }
0515
0516
0517
0518
0519
0520
0521
0522
0523 static int INIT read_bunzip(struct bunzip_data *bd, char *outbuf, int len)
0524 {
0525 const unsigned int *dbuf;
0526 int pos, xcurrent, previous, gotcount;
0527
0528
0529 if (bd->writeCount < 0)
0530 return bd->writeCount;
0531
0532 gotcount = 0;
0533 dbuf = bd->dbuf;
0534 pos = bd->writePos;
0535 xcurrent = bd->writeCurrent;
0536
0537
0538
0539
0540
0541 if (bd->writeCopies) {
0542
0543 --bd->writeCopies;
0544
0545 for (;;) {
0546
0547
0548 if (gotcount >= len) {
0549 bd->writePos = pos;
0550 bd->writeCurrent = xcurrent;
0551 bd->writeCopies++;
0552 return len;
0553 }
0554
0555 outbuf[gotcount++] = xcurrent;
0556 bd->writeCRC = (((bd->writeCRC) << 8)
0557 ^bd->crc32Table[((bd->writeCRC) >> 24)
0558 ^xcurrent]);
0559
0560
0561 if (bd->writeCopies) {
0562 --bd->writeCopies;
0563 continue;
0564 }
0565 decode_next_byte:
0566 if (!bd->writeCount--)
0567 break;
0568
0569
0570 previous = xcurrent;
0571 pos = dbuf[pos];
0572 xcurrent = pos&0xff;
0573 pos >>= 8;
0574
0575
0576
0577
0578 if (--bd->writeRunCountdown) {
0579 if (xcurrent != previous)
0580 bd->writeRunCountdown = 4;
0581 } else {
0582
0583
0584 bd->writeCopies = xcurrent;
0585 xcurrent = previous;
0586 bd->writeRunCountdown = 5;
0587
0588
0589 if (!bd->writeCopies)
0590 goto decode_next_byte;
0591
0592
0593 --bd->writeCopies;
0594 }
0595 }
0596
0597 bd->writeCRC = ~bd->writeCRC;
0598 bd->totalCRC = ((bd->totalCRC << 1) |
0599 (bd->totalCRC >> 31)) ^ bd->writeCRC;
0600
0601 if (bd->writeCRC != bd->headerCRC) {
0602 bd->totalCRC = bd->headerCRC+1;
0603 return RETVAL_LAST_BLOCK;
0604 }
0605 }
0606
0607
0608
0609
0610 previous = get_next_block(bd);
0611 if (previous) {
0612 bd->writeCount = previous;
0613 return (previous != RETVAL_LAST_BLOCK) ? previous : gotcount;
0614 }
0615 bd->writeCRC = 0xffffffffUL;
0616 pos = bd->writePos;
0617 xcurrent = bd->writeCurrent;
0618 goto decode_next_byte;
0619 }
0620
0621 static long INIT nofill(void *buf, unsigned long len)
0622 {
0623 return -1;
0624 }
0625
0626
0627
0628
0629 static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, long len,
0630 long (*fill)(void*, unsigned long))
0631 {
0632 struct bunzip_data *bd;
0633 unsigned int i, j, c;
0634 const unsigned int BZh0 =
0635 (((unsigned int)'B') << 24)+(((unsigned int)'Z') << 16)
0636 +(((unsigned int)'h') << 8)+(unsigned int)'0';
0637
0638
0639 i = sizeof(struct bunzip_data);
0640
0641
0642 bd = *bdp = malloc(i);
0643 if (!bd)
0644 return RETVAL_OUT_OF_MEMORY;
0645 memset(bd, 0, sizeof(struct bunzip_data));
0646
0647 bd->inbuf = inbuf;
0648 bd->inbufCount = len;
0649 if (fill != NULL)
0650 bd->fill = fill;
0651 else
0652 bd->fill = nofill;
0653
0654
0655 for (i = 0; i < 256; i++) {
0656 c = i << 24;
0657 for (j = 8; j; j--)
0658 c = c&0x80000000 ? (c << 1)^(CRC32_POLY_BE) : (c << 1);
0659 bd->crc32Table[i] = c;
0660 }
0661
0662
0663 i = get_bits(bd, 32);
0664 if (((unsigned int)(i-BZh0-1)) >= 9)
0665 return RETVAL_NOT_BZIP_DATA;
0666
0667
0668
0669 bd->dbufSize = 100000*(i-BZh0);
0670
0671 bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
0672 if (!bd->dbuf)
0673 return RETVAL_OUT_OF_MEMORY;
0674 return RETVAL_OK;
0675 }
0676
0677
0678
0679 STATIC int INIT bunzip2(unsigned char *buf, long len,
0680 long (*fill)(void*, unsigned long),
0681 long (*flush)(void*, unsigned long),
0682 unsigned char *outbuf,
0683 long *pos,
0684 void(*error)(char *x))
0685 {
0686 struct bunzip_data *bd;
0687 int i = -1;
0688 unsigned char *inbuf;
0689
0690 if (flush)
0691 outbuf = malloc(BZIP2_IOBUF_SIZE);
0692
0693 if (!outbuf) {
0694 error("Could not allocate output buffer");
0695 return RETVAL_OUT_OF_MEMORY;
0696 }
0697 if (buf)
0698 inbuf = buf;
0699 else
0700 inbuf = malloc(BZIP2_IOBUF_SIZE);
0701 if (!inbuf) {
0702 error("Could not allocate input buffer");
0703 i = RETVAL_OUT_OF_MEMORY;
0704 goto exit_0;
0705 }
0706 i = start_bunzip(&bd, inbuf, len, fill);
0707 if (!i) {
0708 for (;;) {
0709 i = read_bunzip(bd, outbuf, BZIP2_IOBUF_SIZE);
0710 if (i <= 0)
0711 break;
0712 if (!flush)
0713 outbuf += i;
0714 else
0715 if (i != flush(outbuf, i)) {
0716 i = RETVAL_UNEXPECTED_OUTPUT_EOF;
0717 break;
0718 }
0719 }
0720 }
0721
0722 if (i == RETVAL_LAST_BLOCK) {
0723 if (bd->headerCRC != bd->totalCRC)
0724 error("Data integrity error when decompressing.");
0725 else
0726 i = RETVAL_OK;
0727 } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
0728 error("Compressed file ends unexpectedly");
0729 }
0730 if (!bd)
0731 goto exit_1;
0732 if (bd->dbuf)
0733 large_free(bd->dbuf);
0734 if (pos)
0735 *pos = bd->inbufPos;
0736 free(bd);
0737 exit_1:
0738 if (!buf)
0739 free(inbuf);
0740 exit_0:
0741 if (flush)
0742 free(outbuf);
0743 return i;
0744 }
0745
0746 #ifdef PREBOOT
0747 STATIC int INIT __decompress(unsigned char *buf, long len,
0748 long (*fill)(void*, unsigned long),
0749 long (*flush)(void*, unsigned long),
0750 unsigned char *outbuf, long olen,
0751 long *pos,
0752 void (*error)(char *x))
0753 {
0754 return bunzip2(buf, len - 4, fill, flush, outbuf, pos, error);
0755 }
0756 #endif