Coverage Report

Created: 2026-10-13 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/decompress_bunzip2.c
Line
Count
Source
1
/* vi: set sw = 4 ts = 4: */
2
/*  Small bzip2 deflate implementation, by Rob Landley (rob@landley.net).
3
4
  Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
5
  which also acknowledges contributions by Mike Burrows, David Wheeler,
6
  Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
7
  Robert Sedgewick, and Jon L. Bentley.
8
9
  This code is licensed under the LGPLv2:
10
    LGPL (http://www.gnu.org/copyleft/lgpl.html
11
*/
12
13
/*
14
  Size and speed optimizations by Manuel Novoa III  (mjn3@codepoet.org).
15
16
  More efficient reading of Huffman codes, a streamlined read_bunzip()
17
  function, and various other tweaks.  In (limited) tests, approximately
18
  20% faster than bzcat on x86 and about 10% faster on ARM.
19
20
  Note that about 2/3 of the time is spent in read_unzip() reversing
21
  the Burrows-Wheeler transformation.  Much of that time is delay
22
  resulting from cache misses.
23
24
  I would ask that anyone benefiting from this work, especially those
25
  using it in commercial products, consider making a donation to my local
26
  non-profit hospice organization in the name of the woman I loved, who
27
  passed away Feb. 12, 2003.
28
29
    In memory of Toni W. Hagan
30
31
    Hospice of Acadiana, Inc.
32
    2600 Johnston St., Suite 200
33
    Lafayette, LA 70503-3240
34
35
    Phone (337) 232-1234 or 1-800-738-2226
36
    Fax   (337) 232-1297
37
38
    http://www.hospiceacadiana.com/
39
40
  Manuel
41
 */
42
43
/*
44
  Made it fit for running in Linux Kernel by Alain Knaff (alain@knaff.lu)
45
*/
46
47
#include <common.h>
48
#include <malloc.h>
49
50
#ifdef STATIC
51
#define PREBOOT
52
#else
53
#include <bunzip2.h>
54
#endif /* STATIC */
55
56
/* Constants for Huffman coding */
57
356
#define MAX_GROUPS    6
58
761
#define GROUP_SIZE      50  /* 64 would have been more efficient */
59
10.2k
#define MAX_HUFCODE_BITS  20  /* Longest Huffman code allowed */
60
28.5k
#define MAX_SYMBOLS     258  /* 256 literals + RUNA + RUNB */
61
#define SYMBOL_RUNA   0
62
28.5k
#define SYMBOL_RUNB   1
63
64
/* Status return values */
65
630
#define RETVAL_OK     0
66
970
#define RETVAL_LAST_BLOCK   (-1)
67
121
#define RETVAL_NOT_BZIP_DATA    (-2)
68
248
#define RETVAL_UNEXPECTED_INPUT_EOF (-3)
69
368
#define RETVAL_UNEXPECTED_OUTPUT_EOF  (-4)
70
246
#define RETVAL_DATA_ERROR   (-5)
71
0
#define RETVAL_OUT_OF_MEMORY    (-6)
72
1
#define RETVAL_OBSOLETE_INPUT   (-7)
73
74
/* Other housekeeping constants */
75
18.5k
#define BZIP2_IOBUF_SIZE    4096
76
77
/* This is what we know about each Huffman coding group */
78
struct group_data {
79
  /*
80
   * Both arrays are indexed by code length, which starts at 1, and
81
   * limit[] has an extra slot at the end for a sentinel value.
82
   * Entry 0 is unused rather than biasing the pointers by -1, which
83
   * would be undefined behaviour.
84
   */
85
  int limit[MAX_HUFCODE_BITS+2];
86
  int base[MAX_HUFCODE_BITS+1];
87
  int permute[MAX_SYMBOLS];
88
  int minLen, maxLen;
89
};
90
91
/* Structure holding all the housekeeping data, including IO buffers and
92
   memory that persists between calls to bunzip */
93
struct bunzip_data {
94
  /* State for interrupting output loop */
95
  int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
96
  /* I/O tracking data (file handles, buffers, positions, etc.) */
97
  long (*fill)(void*, unsigned long);
98
  int inbufCount, inbufPos /*, outbufPos*/;
99
  unsigned char *inbuf /*,*outbuf*/;
100
  unsigned int inbufBitCount, inbufBits;
101
  /* The CRC values stored in the block header and calculated from the
102
  data */
103
  unsigned int crc32Table[256], headerCRC, totalCRC, writeCRC;
104
  /* Intermediate buffer and its size (in bytes) */
105
  unsigned int *dbuf, dbufSize;
106
  /* These things are a bit too big to go on the stack */
107
  unsigned char selectors[32768];   /* nSelectors = 15 bits */
108
  struct group_data groups[MAX_GROUPS]; /* Huffman coding tables */
109
  int io_error;     /* non-zero if we have IO error */
110
  int byteCount[256];
111
  unsigned char symToByte[256], mtfSymbol[256];
112
};
113
114
115
/* Return the next nnn bits of input.  All reads from the compressed input
116
   are done through this function.  All reads are big endian */
117
static unsigned int get_bits(struct bunzip_data *bd, char bits_wanted)
118
611k
{
119
611k
  unsigned int bits = 0;
120
121
  /* If we need to get more data from the byte buffer, do so.
122
     (Loop getting one byte at a time to enforce endianness and avoid
123
     unaligned access.) */
124
628k
  while (bd->inbufBitCount < bits_wanted) {
125
588k
    if (bd->io_error)
126
571k
      return 0;
127
    /* If we need to read more data from file into byte buffer, do
128
       so */
129
16.6k
    if (bd->inbufPos == bd->inbufCount) {
130
248
      bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE);
131
248
      if (bd->inbufCount <= 0) {
132
248
        bd->io_error = RETVAL_UNEXPECTED_INPUT_EOF;
133
248
        return 0;
134
248
      }
135
0
      bd->inbufPos = 0;
136
0
    }
137
    /* Avoid 32-bit overflow (dump bit buffer to top of output) */
138
16.3k
    if (bd->inbufBitCount >= 24) {
139
974
      bits = bd->inbufBits&((1 << bd->inbufBitCount)-1);
140
974
      bits_wanted -= bd->inbufBitCount;
141
974
      bits <<= bits_wanted;
142
974
      bd->inbufBitCount = 0;
143
974
    }
144
    /* Grab next 8 bits of input from buffer. */
145
16.3k
    bd->inbufBits = (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
146
16.3k
    bd->inbufBitCount += 8;
147
16.3k
  }
148
  /* Calculate result */
149
40.1k
  bd->inbufBitCount -= bits_wanted;
150
40.1k
  bits |= (bd->inbufBits >> bd->inbufBitCount)&((1 << bits_wanted)-1);
151
152
40.1k
  return bits;
153
611k
}
154
155
/* Unpacks the next block and sets up for the inverse burrows-wheeler step. */
156
157
static int get_next_block(struct bunzip_data *bd)
158
518
{
159
518
  struct group_data *hufGroup = NULL;
160
518
  int *base = NULL;
161
518
  int *limit = NULL;
162
518
  int dbufCount, nextSym, dbufSize, groupCount, selector,
163
518
    i, j, k, t, runPos, symCount, symTotal, nSelectors, *byteCount;
164
518
  unsigned char uc, *symToByte, *mtfSymbol, *selectors;
165
518
  unsigned int *dbuf, origPtr;
166
167
518
  dbuf = bd->dbuf;
168
518
  dbufSize = bd->dbufSize;
169
518
  selectors = bd->selectors;
170
518
  byteCount = bd->byteCount;
171
518
  symToByte = bd->symToByte;
172
518
  mtfSymbol = bd->mtfSymbol;
173
174
  /* Read in header signature and CRC, then validate signature.
175
     (last block signature means CRC is for whole file, return now) */
176
518
  i = get_bits(bd, 24);
177
518
  j = get_bits(bd, 24);
178
518
  bd->headerCRC = get_bits(bd, 32);
179
518
  if ((i == 0x177245) && (j == 0x385090))
180
4
    return RETVAL_LAST_BLOCK;
181
514
  if ((i != 0x314159) || (j != 0x265359))
182
121
    return RETVAL_NOT_BZIP_DATA;
183
  /* We can add support for blockRandomised if anybody complains.
184
     There was some code for this in busybox 1.0.0-pre3, but nobody ever
185
     noticed that it didn't actually work. */
186
393
  if (get_bits(bd, 1))
187
1
    return RETVAL_OBSOLETE_INPUT;
188
392
  origPtr = get_bits(bd, 24);
189
392
  if (origPtr > dbufSize)
190
28
    return RETVAL_DATA_ERROR;
191
  /* mapping table: if some byte values are never used (encoding things
192
     like ascii text), the compression code removes the gaps to have fewer
193
     symbols to deal with, and writes a sparse bitfield indicating which
194
     values were present.  We make a translation table to convert the
195
     symbols back to the corresponding bytes. */
196
364
  t = get_bits(bd, 16);
197
364
  symTotal = 0;
198
6.18k
  for (i = 0; i < 16; i++) {
199
5.82k
    if (t&(1 << (15-i))) {
200
933
      k = get_bits(bd, 16);
201
15.8k
      for (j = 0; j < 16; j++)
202
14.9k
        if (k&(1 << (15-j)))
203
4.11k
          symToByte[symTotal++] = (16*i)+j;
204
933
    }
205
5.82k
  }
206
  /* How many different Huffman coding groups does this block use? */
207
364
  groupCount = get_bits(bd, 3);
208
364
  if (groupCount < 2 || groupCount > MAX_GROUPS)
209
17
    return RETVAL_DATA_ERROR;
210
  /* nSelectors: Every GROUP_SIZE many symbols we select a new
211
     Huffman coding group.  Read in the group selector list,
212
     which is stored as MTF encoded bit runs.  (MTF = Move To
213
     Front, as each value is used it's moved to the start of the
214
     list.) */
215
347
  nSelectors = get_bits(bd, 15);
216
347
  if (!nSelectors)
217
16
    return RETVAL_DATA_ERROR;
218
1.16k
  for (i = 0; i < groupCount; i++)
219
829
    mtfSymbol[i] = i;
220
570k
  for (i = 0; i < nSelectors; i++) {
221
    /* Get next value */
222
580k
    for (j = 0; get_bits(bd, 1); j++)
223
10.1k
      if (j >= groupCount)
224
5
        return RETVAL_DATA_ERROR;
225
    /* Decode MTF to get the next selector */
226
570k
    uc = mtfSymbol[j];
227
580k
    for (; j; j--)
228
10.1k
      mtfSymbol[j] = mtfSymbol[j-1];
229
570k
    mtfSymbol[0] = selectors[i] = uc;
230
570k
  }
231
  /* Read the Huffman coding tables for each group, which code
232
     for symTotal literal symbols, plus two run symbols (RUNA,
233
     RUNB) */
234
326
  symCount = symTotal+2;
235
877
  for (j = 0; j < groupCount; j++) {
236
630
    unsigned char length[MAX_SYMBOLS], temp[MAX_HUFCODE_BITS+1];
237
630
    int minLen, maxLen, pp;
238
    /* Read Huffman code lengths for each symbol.  They're
239
       stored in a way similar to mtf; record a starting
240
       value for the first symbol, and an offset from the
241
       previous value for everys symbol after that.
242
       (Subtracting 1 before the loop and then adding it
243
       back at the end is an optimization that makes the
244
       test inside the loop simpler: symbol length 0
245
       becomes negative, so an unsigned inequality catches
246
       it.) */
247
630
    t = get_bits(bd, 5)-1;
248
7.88k
    for (i = 0; i < symCount; i++) {
249
10.2k
      for (;;) {
250
10.2k
        if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
251
79
          return RETVAL_DATA_ERROR;
252
253
        /* If first bit is 0, stop.  Else
254
           second bit indicates whether to
255
           increment or decrement the value.
256
           Optimization: grab 2 bits and unget
257
           the second if the first was 0. */
258
259
10.2k
        k = get_bits(bd, 2);
260
10.2k
        if (k < 2) {
261
7.25k
          bd->inbufBitCount++;
262
7.25k
          break;
263
7.25k
        }
264
        /* Add one if second bit 1, else
265
         * subtract 1.  Avoids if/else */
266
2.95k
        t += (((k+1)&2)-1);
267
2.95k
      }
268
      /* Correct for the initial -1, to get the
269
       * final symbol length */
270
7.25k
      length[i] = t+1;
271
7.25k
    }
272
    /* Find largest and smallest lengths in this group */
273
551
    minLen = maxLen = length[0];
274
275
7.25k
    for (i = 1; i < symCount; i++) {
276
6.70k
      if (length[i] > maxLen)
277
400
        maxLen = length[i];
278
6.30k
      else if (length[i] < minLen)
279
593
        minLen = length[i];
280
6.70k
    }
281
282
    /* Calculate permute[], base[], and limit[] tables from
283
     * length[].
284
     *
285
     * permute[] is the lookup table for converting
286
     * Huffman coded symbols into decoded symbols.  base[]
287
     * is the amount to subtract from the value of a
288
     * Huffman symbol of a given length when using
289
     * permute[].
290
     *
291
     * limit[] indicates the largest numerical value a
292
     * symbol with a given number of bits can have.  This
293
     * is how the Huffman codes can vary in length: each
294
     * code with a value > limit[length] needs another
295
     * bit.
296
     */
297
551
    hufGroup = bd->groups+j;
298
551
    hufGroup->minLen = minLen;
299
551
    hufGroup->maxLen = maxLen;
300
    /* Note that minLen can't be smaller than 1, so entry 0
301
       of both arrays stays unused. */
302
551
    base = hufGroup->base;
303
551
    limit = hufGroup->limit;
304
    /* Calculate permute[].  Concurrently, initialize
305
     * temp[] and limit[]. */
306
551
    pp = 0;
307
2.23k
    for (i = minLen; i <= maxLen; i++) {
308
1.68k
      temp[i] = limit[i] = 0;
309
27.9k
      for (t = 0; t < symCount; t++)
310
26.2k
        if (length[t] == i)
311
7.25k
          hufGroup->permute[pp++] = t;
312
1.68k
    }
313
    /* Count symbols coded for at each bit length */
314
7.80k
    for (i = 0; i < symCount; i++)
315
7.25k
      temp[length[i]]++;
316
    /* Calculate limit[] (the largest symbol-coding value
317
     *at each bit length, which is (previous limit <<
318
     *1)+symbols at this level), and base[] (number of
319
     *symbols to ignore at each bit length, which is limit
320
     *minus the cumulative count of symbols coded for
321
     *already). */
322
551
    pp = t = 0;
323
1.68k
    for (i = minLen; i < maxLen; i++) {
324
1.13k
      pp += temp[i];
325
      /* We read the largest possible symbol size
326
         and then unget bits after determining how
327
         many we need, and those extra bits could be
328
         set to anything.  (They're noise from
329
         future symbols.)  At each level we're
330
         really only interested in the first few
331
         bits, so here we set all the trailing
332
         to-be-ignored bits to 1 so they don't
333
         affect the value > limit[length]
334
         comparison. */
335
1.13k
      limit[i] = (pp << (maxLen - i)) - 1;
336
1.13k
      pp <<= 1;
337
1.13k
      base[i+1] = pp-(t += temp[i]);
338
1.13k
    }
339
551
    limit[maxLen+1] = INT_MAX; /* Sentinal value for
340
              * reading next sym. */
341
551
    limit[maxLen] = pp+temp[maxLen]-1;
342
551
    base[minLen] = 0;
343
551
  }
344
  /* We've finished reading and digesting the block header.  Now
345
     read this block's Huffman coded symbols from the file and
346
     undo the Huffman coding and run length encoding, saving the
347
     result into dbuf[dbufCount++] = uc */
348
349
  /* Initialize symbol occurrence counters and symbol Move To
350
   * Front table */
351
63.4k
  for (i = 0; i < 256; i++) {
352
63.2k
    byteCount[i] = 0;
353
63.2k
    mtfSymbol[i] = (unsigned char)i;
354
63.2k
  }
355
  /* Loop through compressed symbols. */
356
247
  runPos = dbufCount = symCount = selector = 0;
357
28.6k
  for (;;) {
358
    /* Determine which Huffman coding group to use. */
359
28.6k
    if (!(symCount--)) {
360
761
      symCount = GROUP_SIZE-1;
361
761
      if (selector >= nSelectors)
362
20
        return RETVAL_DATA_ERROR;
363
741
      hufGroup = bd->groups+selectors[selector++];
364
741
      base = hufGroup->base;
365
741
      limit = hufGroup->limit;
366
741
    }
367
    /* Read next Huffman-coded symbol. */
368
    /* Note: It is far cheaper to read maxLen bits and
369
       back up than it is to read minLen bits and then an
370
       additional bit at a time, testing as we go.
371
       Because there is a trailing last block (with file
372
       CRC), there is no danger of the overread causing an
373
       unexpected EOF for a valid compressed file.  As a
374
       further optimization, we do the read inline
375
       (falling back to a call to get_bits if the buffer
376
       runs dry).  The following (up to got_huff_bits:) is
377
       equivalent to j = get_bits(bd, hufGroup->maxLen);
378
     */
379
30.5k
    while (bd->inbufBitCount < hufGroup->maxLen) {
380
17.1k
      if (bd->io_error ||
381
15.2k
          bd->inbufPos == bd->inbufCount) {
382
15.2k
        j = get_bits(bd, hufGroup->maxLen);
383
15.2k
        goto got_huff_bits;
384
15.2k
      }
385
1.90k
      bd->inbufBits =
386
1.90k
        (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
387
1.90k
      bd->inbufBitCount += 8;
388
13.3k
    };
389
13.3k
    bd->inbufBitCount -= hufGroup->maxLen;
390
13.3k
    j = (bd->inbufBits >> bd->inbufBitCount)&
391
13.3k
      ((1 << hufGroup->maxLen)-1);
392
28.6k
got_huff_bits:
393
    /* Figure how how many bits are in next symbol and
394
     * unget extras */
395
28.6k
    i = hufGroup->minLen;
396
31.6k
    while (j > limit[i])
397
3.08k
      ++i;
398
28.6k
    bd->inbufBitCount += (hufGroup->maxLen - i);
399
    /* Huffman decode value to get nextSym (with bounds checking) */
400
28.6k
    if ((i > hufGroup->maxLen)
401
28.5k
      || (((unsigned)(j = (j>>(hufGroup->maxLen-i))-base[i]))
402
28.5k
        >= MAX_SYMBOLS))
403
33
      return RETVAL_DATA_ERROR;
404
28.5k
    nextSym = hufGroup->permute[j];
405
    /* We have now decoded the symbol, which indicates
406
       either a new literal byte, or a repeated run of the
407
       most recent literal byte.  First, check if nextSym
408
       indicates a repeated run, and if so loop collecting
409
       how many times to repeat the last literal. */
410
28.5k
    if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */
411
      /* If this is the start of a new run, zero out
412
       * counter */
413
14.9k
      if (!runPos) {
414
4.17k
        runPos = 1;
415
4.17k
        t = 0;
416
4.17k
      }
417
      /* Neat trick that saves 1 symbol: instead of
418
         or-ing 0 or 1 at each bit position, add 1
419
         or 2 instead.  For example, 1011 is 1 << 0
420
         + 1 << 1 + 2 << 2.  1010 is 2 << 0 + 2 << 1
421
         + 1 << 2.  You can make any bit pattern
422
         that way using 1 less symbol than the basic
423
         or 0/1 method (except all bits 0, which
424
         would use no symbols, but a run of length 0
425
         doesn't mean anything in this context).
426
         Thus space is saved. */
427
428
      /* runPos doubles for every run symbol, so a
429
         long enough sequence of them shifts it out
430
         of range and makes t overflow into negative
431
         values, which pass the dbufSize check
432
         below.  A run cannot be longer than the
433
         output buffer, so refuse it before the
434
         shift. */
435
14.9k
      if (runPos > dbufSize || t > dbufSize)
436
21
        return RETVAL_DATA_ERROR;
437
438
14.9k
      t += (runPos << nextSym);
439
      /* +runPos if RUNA; +2*runPos if RUNB */
440
441
14.9k
      runPos <<= 1;
442
14.9k
      continue;
443
14.9k
    }
444
    /* When we hit the first non-run symbol after a run,
445
       we now know how many times to repeat the last
446
       literal, so append that many copies to our buffer
447
       of decoded symbols (dbuf) now.  (The last literal
448
       used is the one at the head of the mtfSymbol
449
       array.) */
450
13.5k
    if (runPos) {
451
4.13k
      runPos = 0;
452
4.13k
      if (dbufCount+t >= dbufSize)
453
20
        return RETVAL_DATA_ERROR;
454
455
4.11k
      uc = symToByte[mtfSymbol[0]];
456
4.11k
      byteCount[uc] += t;
457
7.00M
      while (t--)
458
7.00M
        dbuf[dbufCount++] = uc;
459
4.11k
    }
460
    /* Is this the terminating symbol? */
461
13.5k
    if (nextSym > symTotal)
462
152
      break;
463
    /* At this point, nextSym indicates a new literal
464
       character.  Subtract one to get the position in the
465
       MTF array at which this literal is currently to be
466
       found.  (Note that the result can't be -1 or 0,
467
       because 0 and 1 are RUNA and RUNB.  But another
468
       instance of the first symbol in the mtf array,
469
       position 0, would have been handled as part of a
470
       run above.  Therefore 1 unused mtf position minus 2
471
       non-literal nextSym values equals -1.) */
472
13.4k
    if (dbufCount >= dbufSize)
473
1
      return RETVAL_DATA_ERROR;
474
13.4k
    i = nextSym - 1;
475
13.4k
    uc = mtfSymbol[i];
476
    /* Adjust the MTF array.  Since we typically expect to
477
     *move only a small number of symbols, and are bound
478
     *by 256 in any case, using memmove here would
479
     *typically be bigger and slower due to function call
480
     *overhead and other assorted setup costs. */
481
73.2k
    do {
482
73.2k
      mtfSymbol[i] = mtfSymbol[i-1];
483
73.2k
    } while (--i);
484
13.4k
    mtfSymbol[0] = uc;
485
13.4k
    uc = symToByte[uc];
486
    /* We have our literal byte.  Save it into dbuf. */
487
13.4k
    byteCount[uc]++;
488
13.4k
    dbuf[dbufCount++] = (unsigned int)uc;
489
13.4k
  }
490
  /* At this point, we've read all the Huffman-coded symbols
491
     (and repeated runs) for this block from the input stream,
492
     and decoded them into the intermediate buffer.  There are
493
     dbufCount many decoded bytes in dbuf[].  Now undo the
494
     Burrows-Wheeler transform on dbuf.  See
495
     http://dogma.net/markn/articles/bwt/bwt.htm
496
   */
497
  /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
498
152
  j = 0;
499
39.0k
  for (i = 0; i < 256; i++) {
500
38.9k
    k = j+byteCount[i];
501
38.9k
    byteCount[i] = j;
502
38.9k
    j = k;
503
38.9k
  }
504
  /* Figure out what order dbuf would be in if we sorted it. */
505
3.01M
  for (i = 0; i < dbufCount; i++) {
506
3.01M
    uc = (unsigned char)(dbuf[i] & 0xff);
507
3.01M
    dbuf[byteCount[uc]] |= (i << 8);
508
3.01M
    byteCount[uc]++;
509
3.01M
  }
510
  /* Decode first byte by hand to initialize "previous" byte.
511
     Note that it doesn't get output, and if the first three
512
     characters are identical it doesn't qualify as a run (hence
513
     writeRunCountdown = 5). */
514
152
  if (dbufCount) {
515
110
    if (origPtr >= dbufCount)
516
6
      return RETVAL_DATA_ERROR;
517
104
    bd->writePos = dbuf[origPtr];
518
104
    bd->writeCurrent = (unsigned char)(bd->writePos&0xff);
519
104
    bd->writePos >>= 8;
520
104
    bd->writeRunCountdown = 5;
521
104
  }
522
146
  bd->writeCount = dbufCount;
523
524
146
  return RETVAL_OK;
525
152
}
526
527
/* Undo burrows-wheeler transform on intermediate buffer to produce output.
528
   If start_bunzip was initialized with out_fd =-1, then up to len bytes of
529
   data are written to outbuf.  Return value is number of bytes written or
530
   error (all errors are negative numbers).  If out_fd!=-1, outbuf and len
531
   are ignored, data is written to out_fd and return is RETVAL_OK or error.
532
*/
533
534
static int read_bunzip(struct bunzip_data *bd, char *outbuf, int len)
535
17.8k
{
536
17.8k
  const unsigned int *dbuf;
537
17.8k
  int pos, xcurrent, previous, gotcount;
538
539
  /* If last read was short due to end of file, return last block now */
540
17.8k
  if (bd->writeCount < 0)
541
4
    return bd->writeCount;
542
543
17.8k
  gotcount = 0;
544
17.8k
  dbuf = bd->dbuf;
545
17.8k
  pos = bd->writePos;
546
17.8k
  xcurrent = bd->writeCurrent;
547
548
  /* We will always have pending decoded data to write into the output
549
     buffer unless this is the very first call (in which case we haven't
550
     Huffman-decoded a block into the intermediate buffer yet). */
551
552
17.8k
  if (bd->writeCopies) {
553
    /* Inside the loop, writeCopies means extra copies (beyond 1) */
554
17.3k
    --bd->writeCopies;
555
    /* Loop outputting bytes */
556
71.2M
    for (;;) {
557
      /* If the output buffer is full, snapshot
558
       * state and return */
559
71.2M
      if (gotcount >= len) {
560
17.3k
        bd->writePos = pos;
561
17.3k
        bd->writeCurrent = xcurrent;
562
17.3k
        bd->writeCopies++;
563
17.3k
        return len;
564
17.3k
      }
565
      /* Write next byte into output buffer, updating CRC */
566
71.1M
      outbuf[gotcount++] = xcurrent;
567
71.1M
      bd->writeCRC = (((bd->writeCRC) << 8)
568
71.1M
        ^bd->crc32Table[((bd->writeCRC) >> 24)
569
71.1M
        ^xcurrent]);
570
      /* Loop now if we're outputting multiple
571
       * copies of this byte */
572
71.1M
      if (bd->writeCopies) {
573
68.1M
        --bd->writeCopies;
574
68.1M
        continue;
575
68.1M
      }
576
3.01M
decode_next_byte:
577
3.01M
      if (!bd->writeCount--)
578
146
        break;
579
      /* Follow sequence vector to undo
580
       * Burrows-Wheeler transform */
581
3.01M
      previous = xcurrent;
582
3.01M
      pos = dbuf[pos];
583
3.01M
      xcurrent = pos&0xff;
584
3.01M
      pos >>= 8;
585
      /* After 3 consecutive copies of the same
586
         byte, the 4th is a repeat count.  We count
587
         down from 4 instead *of counting up because
588
         testing for non-zero is faster */
589
3.01M
      if (--bd->writeRunCountdown) {
590
2.41M
        if (xcurrent != previous)
591
10.0k
          bd->writeRunCountdown = 4;
592
2.41M
      } else {
593
        /* We have a repeated run, this byte
594
         * indicates the count */
595
601k
        bd->writeCopies = xcurrent;
596
601k
        xcurrent = previous;
597
601k
        bd->writeRunCountdown = 5;
598
        /* Sometimes there are just 3 bytes
599
         * (run length 0) */
600
601k
        if (!bd->writeCopies)
601
504
          goto decode_next_byte;
602
        /* Subtract the 1 copy we'd output
603
         * anyway to get extras */
604
600k
        --bd->writeCopies;
605
600k
      }
606
3.01M
    }
607
    /* Decompression of this block completed successfully */
608
146
    bd->writeCRC = ~bd->writeCRC;
609
146
    bd->totalCRC = ((bd->totalCRC << 1) |
610
146
        (bd->totalCRC >> 31)) ^ bd->writeCRC;
611
    /* If this block had a CRC error, force file level CRC error. */
612
146
    if (bd->writeCRC != bd->headerCRC) {
613
111
      bd->totalCRC = bd->headerCRC+1;
614
111
      return RETVAL_LAST_BLOCK;
615
111
    }
616
146
  }
617
618
  /* Refill the intermediate buffer by Huffman-decoding next
619
   * block of input */
620
  /* (previous is just a convenient unused temp variable here) */
621
518
  previous = get_next_block(bd);
622
518
  if (previous) {
623
372
    bd->writeCount = previous;
624
372
    return (previous != RETVAL_LAST_BLOCK) ? previous : gotcount;
625
372
  }
626
146
  bd->writeCRC = 0xffffffffUL;
627
146
  pos = bd->writePos;
628
146
  xcurrent = bd->writeCurrent;
629
146
  goto decode_next_byte;
630
518
}
631
632
static long nofill(void *buf, unsigned long len)
633
248
{
634
248
  return -1;
635
248
}
636
637
/* Allocate the structure, read file header.  If in_fd ==-1, inbuf must contain
638
   a complete bunzip file (len bytes long).  If in_fd!=-1, inbuf and len are
639
   ignored, and data is read from file handle into temporary buffer. */
640
static int start_bunzip(struct bunzip_data **bdp, void *inbuf, long len,
641
           long (*fill)(void*, unsigned long))
642
483
{
643
483
  struct bunzip_data *bd;
644
483
  unsigned int i, j, c;
645
483
  const unsigned int BZh0 =
646
483
    (((unsigned int)'B') << 24)+(((unsigned int)'Z') << 16)
647
483
    +(((unsigned int)'h') << 8)+(unsigned int)'0';
648
649
  /* Figure out how much data to allocate */
650
483
  i = sizeof(struct bunzip_data);
651
652
  /* Allocate bunzip_data.  Most fields initialize to zero. */
653
483
  bd = *bdp = malloc(i);
654
483
  if (!bd)
655
0
    return RETVAL_OUT_OF_MEMORY;
656
483
  memset(bd, 0, sizeof(struct bunzip_data));
657
  /* Setup input buffer */
658
483
  bd->inbuf = inbuf;
659
483
  bd->inbufCount = len;
660
483
  if (fill != NULL)
661
0
    bd->fill = fill;
662
483
  else
663
483
    bd->fill = nofill;
664
665
  /* Init the CRC32 table (big endian) */
666
124k
  for (i = 0; i < 256; i++) {
667
123k
    c = i << 24;
668
1.11M
    for (j = 8; j; j--)
669
989k
      c = c&0x80000000 ? (c << 1)^0x04c11db7 : (c << 1);
670
123k
    bd->crc32Table[i] = c;
671
123k
  }
672
673
  /* Ensure that file starts with "BZh['1'-'9']." */
674
483
  i = get_bits(bd, 32);
675
483
  if (((unsigned int)(i-BZh0-1)) >= 9)
676
0
    return RETVAL_NOT_BZIP_DATA;
677
678
  /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
679
     uncompressed data.  Allocate intermediate buffer for block. */
680
483
  bd->dbufSize = 100000*(i-BZh0);
681
682
483
  bd->dbuf = malloc(bd->dbufSize * sizeof(int));
683
483
  if (!bd->dbuf)
684
0
    return RETVAL_OUT_OF_MEMORY;
685
483
  return RETVAL_OK;
686
483
}
687
688
/* Example usage: decompress src_fd to dst_fd.  (Stops at end of bzip2 data,
689
   not end of file.) */
690
int bunzip2(unsigned char *buf, long len,
691
      long(*fill)(void*, unsigned long),
692
      long(*flush)(void*, unsigned long),
693
      unsigned char *outbuf,
694
      long *pos,
695
      void(*error)(char *x))
696
483
{
697
483
  struct bunzip_data *bd;
698
483
  int i = -1;
699
483
  unsigned char *inbuf;
700
701
483
  if (flush)
702
483
    outbuf = malloc(BZIP2_IOBUF_SIZE);
703
704
483
  if (!outbuf) {
705
0
    error("Could not allocate output bufer");
706
0
    return RETVAL_OUT_OF_MEMORY;
707
0
  }
708
483
  if (buf)
709
483
    inbuf = buf;
710
0
  else
711
0
    inbuf = malloc(BZIP2_IOBUF_SIZE);
712
483
  if (!inbuf) {
713
0
    error("Could not allocate input bufer");
714
0
    i = RETVAL_OUT_OF_MEMORY;
715
0
    goto exit_0;
716
0
  }
717
483
  i = start_bunzip(&bd, inbuf, len, fill);
718
483
  if (!i) {
719
17.8k
    for (;;) {
720
17.8k
      i = read_bunzip(bd, outbuf, BZIP2_IOBUF_SIZE);
721
17.8k
      if (i <= 0)
722
483
        break;
723
17.3k
      if (!flush)
724
0
        outbuf += i;
725
17.3k
      else
726
17.3k
        if (i != flush(outbuf, i)) {
727
0
          i = RETVAL_UNEXPECTED_OUTPUT_EOF;
728
0
          break;
729
0
        }
730
17.3k
    }
731
483
  }
732
  /* Check CRC and release memory */
733
483
  if (i == RETVAL_LAST_BLOCK) {
734
115
    if (bd->headerCRC != bd->totalCRC)
735
114
      error("Data integrity error when decompressing.");
736
1
    else
737
1
      i = RETVAL_OK;
738
368
  } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
739
0
    error("Compressed file ends unexpectedly");
740
0
  }
741
483
  if (!bd)
742
0
    goto exit_1;
743
483
  if (bd->dbuf)
744
483
    free(bd->dbuf);
745
483
  if (pos)
746
0
    *pos = bd->inbufPos;
747
483
  free(bd);
748
483
exit_1:
749
483
  if (!buf)
750
0
    free(inbuf);
751
483
exit_0:
752
483
  if (flush)
753
483
    free(outbuf);
754
483
  return i;
755
483
}
756
757
#ifdef PREBOOT
758
STATIC int INIT decompress(unsigned char *buf, int len,
759
      int(*fill)(void*, unsigned int),
760
      int(*flush)(void*, unsigned int),
761
      unsigned char *outbuf,
762
      int *pos,
763
      void(*error)(char *x))
764
{
765
  return bunzip2(buf, len - 4, fill, flush, outbuf, pos, error);
766
}
767
#endif