1 /* bzcat.c - bzip2 decompression
2  *
3  * Copyright 2003, 2007 Rob Landley <rob@landley.net>
4  *
5  * Based on a close reading (but not the actual code) of the original bzip2
6  * decompression code by Julian R Seward (jseward@acm.org), which also
7  * acknowledges contributions by Mike Burrows, David Wheeler, Peter Fenwick,
8  * Alistair Moffat, Radford Neal, Ian H. Witten, Robert Sedgewick, and
9  * Jon L. Bentley.
10  *
11  * No standard.
12 
13 
14 USE_BZCAT(NEWTOY(bzcat, NULL, TOYFLAG_USR|TOYFLAG_BIN))
15 USE_BUNZIP2(NEWTOY(bunzip2, "cftkv", TOYFLAG_USR|TOYFLAG_BIN))
16 
17 config BUNZIP2
18   bool "bunzip2"
19   default y
20   help
21     usage: bunzip2 [-cftkv] [FILE...]
22 
23     Decompress listed files (file.bz becomes file) deleting archive file(s).
24     Read from stdin if no files listed.
25 
26     -c	force output to stdout
27     -f	force decompression. (If FILE doesn't end in .bz, replace original.)
28     -k	keep input files (-c and -t imply this)
29     -t  test integrity
30     -v	verbose
31 
32 config BZCAT
33   bool "bzcat"
34   default y
35   help
36     usage: bzcat [FILE...]
37 
38     Decompress listed files to stdout. Use stdin if no files listed.
39 */
40 
41 #define FOR_bunzip2
42 #include "toys.h"
43 
44 #define THREADS 1
45 
46 // Constants for huffman coding
47 #define MAX_GROUPS               6
48 #define GROUP_SIZE               50     /* 64 would have been more efficient */
49 #define MAX_HUFCODE_BITS         20     /* Longest huffman code allowed */
50 #define MAX_SYMBOLS              258    /* 256 literals + RUNA + RUNB */
51 #define SYMBOL_RUNA              0
52 #define SYMBOL_RUNB              1
53 
54 // Other housekeeping constants
55 #define IOBUF_SIZE               4096
56 
57 // Status return values
58 #define RETVAL_LAST_BLOCK        (-100)
59 #define RETVAL_NOT_BZIP_DATA     (-1)
60 #define RETVAL_DATA_ERROR        (-2)
61 #define RETVAL_OBSOLETE_INPUT    (-3)
62 
63 // This is what we know about each huffman coding group
64 struct group_data {
65   int limit[MAX_HUFCODE_BITS+1], base[MAX_HUFCODE_BITS], permute[MAX_SYMBOLS];
66   char minLen, maxLen;
67 };
68 
69 // Data for burrows wheeler transform
70 
71 struct bwdata {
72   unsigned int origPtr;
73   int byteCount[256];
74   // State saved when interrupting output
75   int writePos, writeRun, writeCount, writeCurrent;
76   unsigned int dataCRC, headerCRC;
77   unsigned int *dbuf;
78 };
79 
80 // Structure holding all the housekeeping data, including IO buffers and
81 // memory that persists between calls to bunzip
82 struct bunzip_data {
83   // Input stream, input buffer, input bit buffer
84   int in_fd, inbufCount, inbufPos;
85   char *inbuf;
86   unsigned int inbufBitCount, inbufBits;
87 
88   // Output buffer
89   char outbuf[IOBUF_SIZE];
90   int outbufPos;
91 
92   unsigned int totalCRC;
93 
94   // First pass decompression data (Huffman and MTF decoding)
95   char selectors[32768];                  // nSelectors=15 bits
96   struct group_data groups[MAX_GROUPS];   // huffman coding tables
97   int symTotal, groupCount, nSelectors;
98   unsigned char symToByte[256], mtfSymbol[256];
99 
100   // The CRC values stored in the block header and calculated from the data
101   unsigned int crc32Table[256];
102 
103   // Second pass decompression data (burrows-wheeler transform)
104   unsigned int dbufSize;
105   struct bwdata bwdata[THREADS];
106 };
107 
108 // Return the next nnn bits of input.  All reads from the compressed input
109 // are done through this function.  All reads are big endian.
get_bits(struct bunzip_data * bd,char bits_wanted)110 static unsigned int get_bits(struct bunzip_data *bd, char bits_wanted)
111 {
112   unsigned int bits = 0;
113 
114   // If we need to get more data from the byte buffer, do so.  (Loop getting
115   // one byte at a time to enforce endianness and avoid unaligned access.)
116   while (bd->inbufBitCount < bits_wanted) {
117 
118     // If we need to read more data from file into byte buffer, do so
119     if (bd->inbufPos == bd->inbufCount) {
120       if (0 >= (bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE)))
121         error_exit("input EOF");
122       bd->inbufPos = 0;
123     }
124 
125     // Avoid 32-bit overflow (dump bit buffer to top of output)
126     if (bd->inbufBitCount>=24) {
127       bits = bd->inbufBits&((1<<bd->inbufBitCount)-1);
128       bits_wanted -= bd->inbufBitCount;
129       bits <<= bits_wanted;
130       bd->inbufBitCount = 0;
131     }
132 
133     // Grab next 8 bits of input from buffer.
134     bd->inbufBits = (bd->inbufBits<<8) | bd->inbuf[bd->inbufPos++];
135     bd->inbufBitCount += 8;
136   }
137 
138   // Calculate result
139   bd->inbufBitCount -= bits_wanted;
140   bits |= (bd->inbufBits>>bd->inbufBitCount) & ((1<<bits_wanted)-1);
141 
142   return bits;
143 }
144 
145 /* Read block header at start of a new compressed data block.  Consists of:
146  *
147  * 48 bits : Block signature, either pi (data block) or e (EOF block).
148  * 32 bits : bw->headerCRC
149  * 1  bit  : obsolete feature flag.
150  * 24 bits : origPtr (Burrows-wheeler unwind index, only 20 bits ever used)
151  * 16 bits : Mapping table index.
152  *[16 bits]: symToByte[symTotal] (Mapping table.  For each bit set in mapping
153  *           table index above, read another 16 bits of mapping table data.
154  *           If correspondig bit is unset, all bits in that mapping table
155  *           section are 0.)
156  *  3 bits : groupCount (how many huffman tables used to encode, anywhere
157  *           from 2 to MAX_GROUPS)
158  * variable: hufGroup[groupCount] (MTF encoded huffman table data.)
159  */
160 
read_block_header(struct bunzip_data * bd,struct bwdata * bw)161 static int read_block_header(struct bunzip_data *bd, struct bwdata *bw)
162 {
163   struct group_data *hufGroup;
164   int hh, ii, jj, kk, symCount, *base, *limit;
165   unsigned char uc;
166 
167   // Read in header signature and CRC (which is stored big endian)
168   ii = get_bits(bd, 24);
169   jj = get_bits(bd, 24);
170   bw->headerCRC = get_bits(bd,32);
171 
172   // Is this the EOF block with CRC for whole file?  (Constant is "e")
173   if (ii==0x177245 && jj==0x385090) return RETVAL_LAST_BLOCK;
174 
175   // Is this a valid data block?  (Constant is "pi".)
176   if (ii!=0x314159 || jj!=0x265359) return RETVAL_NOT_BZIP_DATA;
177 
178   // We can add support for blockRandomised if anybody complains.
179   if (get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT;
180   if ((bw->origPtr = get_bits(bd,24)) > bd->dbufSize) return RETVAL_DATA_ERROR;
181 
182   // mapping table: if some byte values are never used (encoding things
183   // like ascii text), the compression code removes the gaps to have fewer
184   // symbols to deal with, and writes a sparse bitfield indicating which
185   // values were present.  We make a translation table to convert the symbols
186   // back to the corresponding bytes.
187   hh = get_bits(bd, 16);
188   bd->symTotal = 0;
189   for (ii=0; ii<16; ii++) {
190     if (hh & (1 << (15 - ii))) {
191       kk = get_bits(bd, 16);
192       for (jj=0; jj<16; jj++)
193         if (kk & (1 << (15 - jj)))
194           bd->symToByte[bd->symTotal++] = (16 * ii) + jj;
195     }
196   }
197 
198   // How many different huffman coding groups does this block use?
199   bd->groupCount = get_bits(bd,3);
200   if (bd->groupCount<2 || bd->groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR;
201 
202   // nSelectors: Every GROUP_SIZE many symbols we switch huffman coding
203   // tables.  Each group has a selector, which is an index into the huffman
204   // coding table arrays.
205   //
206   // Read in the group selector array, which is stored as MTF encoded
207   // bit runs.  (MTF = Move To Front.  Every time a symbol occurs it's moved
208   // to the front of the table, so it has a shorter encoding next time.)
209   if (!(bd->nSelectors = get_bits(bd, 15))) return RETVAL_DATA_ERROR;
210   for (ii=0; ii<bd->groupCount; ii++) bd->mtfSymbol[ii] = ii;
211   for (ii=0; ii<bd->nSelectors; ii++) {
212 
213     // Get next value
214     for(jj=0;get_bits(bd,1);jj++)
215       if (jj>=bd->groupCount) return RETVAL_DATA_ERROR;
216 
217     // Decode MTF to get the next selector, and move it to the front.
218     uc = bd->mtfSymbol[jj];
219     memmove(bd->mtfSymbol+1, bd->mtfSymbol, jj);
220     bd->mtfSymbol[0] = bd->selectors[ii] = uc;
221   }
222 
223   // Read the huffman coding tables for each group, which code for symTotal
224   // literal symbols, plus two run symbols (RUNA, RUNB)
225   symCount = bd->symTotal+2;
226   for (jj=0; jj<bd->groupCount; jj++) {
227     unsigned char length[MAX_SYMBOLS];
228     unsigned temp[MAX_HUFCODE_BITS+1];
229     int minLen, maxLen, pp;
230 
231     // Read lengths
232     hh = get_bits(bd, 5);
233     for (ii = 0; ii < symCount; ii++) {
234       for(;;) {
235         // !hh || hh > MAX_HUFCODE_BITS in one test.
236         if (MAX_HUFCODE_BITS-1 < (unsigned)hh-1) return RETVAL_DATA_ERROR;
237         // Grab 2 bits instead of 1 (slightly smaller/faster).  Stop if
238         // first bit is 0, otherwise second bit says whether to
239         // increment or decrement.
240         kk = get_bits(bd, 2);
241         if (kk & 2) hh += 1 - ((kk&1)<<1);
242         else {
243           bd->inbufBitCount++;
244           break;
245         }
246       }
247       length[ii] = hh;
248     }
249 
250     // Find largest and smallest lengths in this group
251     minLen = maxLen = length[0];
252     for (ii = 1; ii < symCount; ii++) {
253       if(length[ii] > maxLen) maxLen = length[ii];
254       else if(length[ii] < minLen) minLen = length[ii];
255     }
256 
257     /* Calculate permute[], base[], and limit[] tables from length[].
258      *
259      * permute[] is the lookup table for converting huffman coded symbols
260      * into decoded symbols.  It contains symbol values sorted by length.
261      *
262      * base[] is the amount to subtract from the value of a huffman symbol
263      * of a given length when using permute[].
264      *
265      * limit[] indicates the largest numerical value a symbol with a given
266      * number of bits can have.  It lets us know when to stop reading.
267      *
268      * To use these, keep reading bits until value <= limit[bitcount] or
269      * you've read over 20 bits (error).  Then the decoded symbol
270      * equals permute[hufcode_value - base[hufcode_bitcount]].
271      */
272     hufGroup = bd->groups+jj;
273     hufGroup->minLen = minLen;
274     hufGroup->maxLen = maxLen;
275 
276     // Note that minLen can't be smaller than 1, so we adjust the base
277     // and limit array pointers so we're not always wasting the first
278     // entry.  We do this again when using them (during symbol decoding).
279     base = hufGroup->base-1;
280     limit = hufGroup->limit-1;
281 
282     // zero temp[] and limit[], and calculate permute[]
283     pp = 0;
284     for (ii = minLen; ii <= maxLen; ii++) {
285       temp[ii] = limit[ii] = 0;
286       for (hh = 0; hh < symCount; hh++)
287         if (length[hh] == ii) hufGroup->permute[pp++] = hh;
288     }
289 
290     // Count symbols coded for at each bit length
291     for (ii = 0; ii < symCount; ii++) temp[length[ii]]++;
292 
293     /* Calculate limit[] (the largest symbol-coding value at each bit
294      * length, which is (previous limit<<1)+symbols at this level), and
295      * base[] (number of symbols to ignore at each bit length, which is
296      * limit minus the cumulative count of symbols coded for already). */
297     pp = hh = 0;
298     for (ii = minLen; ii < maxLen; ii++) {
299       pp += temp[ii];
300       limit[ii] = pp-1;
301       pp <<= 1;
302       base[ii+1] = pp-(hh+=temp[ii]);
303     }
304     limit[maxLen] = pp+temp[maxLen]-1;
305     limit[maxLen+1] = INT_MAX;
306     base[minLen] = 0;
307   }
308 
309   return 0;
310 }
311 
312 /* First pass, read block's symbols into dbuf[dbufCount].
313  *
314  * This undoes three types of compression: huffman coding, run length encoding,
315  * and move to front encoding.  We have to undo all those to know when we've
316  * read enough input.
317  */
318 
read_huffman_data(struct bunzip_data * bd,struct bwdata * bw)319 static int read_huffman_data(struct bunzip_data *bd, struct bwdata *bw)
320 {
321   struct group_data *hufGroup;
322   int hh, ii, jj, kk, runPos, dbufCount, symCount, selector, nextSym,
323     *byteCount, *base, *limit;
324   unsigned int *dbuf = bw->dbuf;
325   unsigned char uc;
326 
327   // We've finished reading and digesting the block header.  Now read this
328   // block's huffman coded symbols from the file and undo the huffman coding
329   // and run length encoding, saving the result into dbuf[dbufCount++] = uc
330 
331   // Initialize symbol occurrence counters and symbol mtf table
332   byteCount = bw->byteCount;
333   for(ii=0; ii<256; ii++) {
334     byteCount[ii] = 0;
335     bd->mtfSymbol[ii] = ii;
336   }
337 
338   // Loop through compressed symbols.  This is the first "tight inner loop"
339   // that needs to be micro-optimized for speed.  (This one fills out dbuf[]
340   // linearly, staying in cache more, so isn't as limited by DRAM access.)
341   runPos = dbufCount = symCount = selector = 0;
342   // Some unnecessary initializations to shut gcc up.
343   base = limit = 0;
344   hufGroup = 0;
345   hh = 0;
346 
347   for (;;) {
348     // Have we reached the end of this huffman group?
349     if (!(symCount--)) {
350       // Determine which huffman coding group to use.
351       symCount = GROUP_SIZE-1;
352       if (selector >= bd->nSelectors) return RETVAL_DATA_ERROR;
353       hufGroup = bd->groups + bd->selectors[selector++];
354       base = hufGroup->base-1;
355       limit = hufGroup->limit-1;
356     }
357 
358     // Read next huffman-coded symbol (into jj).
359     ii = hufGroup->minLen;
360     jj = get_bits(bd, ii);
361     while (jj > limit[ii]) {
362       // if (ii > hufGroup->maxLen) return RETVAL_DATA_ERROR;
363       ii++;
364 
365       // Unroll get_bits() to avoid a function call when the data's in
366       // the buffer already.
367       kk = bd->inbufBitCount
368         ? (bd->inbufBits >> --(bd->inbufBitCount)) & 1 : get_bits(bd, 1);
369       jj = (jj << 1) | kk;
370     }
371     // Huffman decode jj into nextSym (with bounds checking)
372     jj-=base[ii];
373 
374     if (ii > hufGroup->maxLen || (unsigned)jj >= MAX_SYMBOLS)
375       return RETVAL_DATA_ERROR;
376     nextSym = hufGroup->permute[jj];
377 
378     // If this is a repeated run, loop collecting data
379     if ((unsigned)nextSym <= SYMBOL_RUNB) {
380       // If this is the start of a new run, zero out counter
381       if(!runPos) {
382         runPos = 1;
383         hh = 0;
384       }
385 
386       /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at
387          each bit position, add 1 or 2 instead. For example,
388          1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2.
389          You can make any bit pattern that way using 1 less symbol than
390          the basic or 0/1 method (except all bits 0, which would use no
391          symbols, but a run of length 0 doesn't mean anything in this
392          context). Thus space is saved. */
393       hh += (runPos << nextSym); // +runPos if RUNA; +2*runPos if RUNB
394       runPos <<= 1;
395       continue;
396     }
397 
398     /* When we hit the first non-run symbol after a run, we now know
399        how many times to repeat the last literal, so append that many
400        copies to our buffer of decoded symbols (dbuf) now. (The last
401        literal used is the one at the head of the mtfSymbol array.) */
402     if (runPos) {
403       runPos = 0;
404       if (dbufCount+hh > bd->dbufSize) return RETVAL_DATA_ERROR;
405 
406       uc = bd->symToByte[bd->mtfSymbol[0]];
407       byteCount[uc] += hh;
408       while (hh--) dbuf[dbufCount++] = uc;
409     }
410 
411     // Is this the terminating symbol?
412     if (nextSym>bd->symTotal) break;
413 
414     /* At this point, the symbol we just decoded indicates a new literal
415        character. Subtract one to get the position in the MTF array
416        at which this literal is currently to be found. (Note that the
417        result can't be -1 or 0, because 0 and 1 are RUNA and RUNB.
418        Another instance of the first symbol in the mtf array, position 0,
419        would have been handled as part of a run.) */
420     if (dbufCount>=bd->dbufSize) return RETVAL_DATA_ERROR;
421     ii = nextSym - 1;
422     uc = bd->mtfSymbol[ii];
423     // On my laptop, unrolling this memmove() into a loop shaves 3.5% off
424     // the total running time.
425     while(ii--) bd->mtfSymbol[ii+1] = bd->mtfSymbol[ii];
426     bd->mtfSymbol[0] = uc;
427     uc = bd->symToByte[uc];
428 
429     // We have our literal byte.  Save it into dbuf.
430     byteCount[uc]++;
431     dbuf[dbufCount++] = (unsigned int)uc;
432   }
433 
434   // Now we know what dbufCount is, do a better sanity check on origPtr.
435   if (bw->origPtr >= (bw->writeCount = dbufCount)) return RETVAL_DATA_ERROR;
436 
437   return 0;
438 }
439 
440 // Flush output buffer to disk
flush_bunzip_outbuf(struct bunzip_data * bd,int out_fd)441 static void flush_bunzip_outbuf(struct bunzip_data *bd, int out_fd)
442 {
443   if (bd->outbufPos) {
444     if (write(out_fd, bd->outbuf, bd->outbufPos) != bd->outbufPos)
445       error_exit("output EOF");
446     bd->outbufPos = 0;
447   }
448 }
449 
burrows_wheeler_prep(struct bunzip_data * bd,struct bwdata * bw)450 static void burrows_wheeler_prep(struct bunzip_data *bd, struct bwdata *bw)
451 {
452   int ii, jj;
453   unsigned int *dbuf = bw->dbuf;
454   int *byteCount = bw->byteCount;
455 
456   // Technically this part is preparation for the burrows-wheeler
457   // transform, but it's quick and convenient to do here.
458 
459   // Turn byteCount into cumulative occurrence counts of 0 to n-1.
460   jj = 0;
461   for (ii=0; ii<256; ii++) {
462     int kk = jj + byteCount[ii];
463     byteCount[ii] = jj;
464     jj = kk;
465   }
466 
467   // Use occurrence counts to quickly figure out what order dbuf would be in
468   // if we sorted it.
469   for (ii=0; ii < bw->writeCount; ii++) {
470     unsigned char uc = dbuf[ii];
471     dbuf[byteCount[uc]] |= (ii << 8);
472     byteCount[uc]++;
473   }
474 
475   // blockRandomised support would go here.
476 
477   // Using ii as position, jj as previous character, hh as current character,
478   // and uc as run count.
479   bw->dataCRC = 0xffffffffL;
480 
481   /* Decode first byte by hand to initialize "previous" byte. Note that it
482      doesn't get output, and if the first three characters are identical
483      it doesn't qualify as a run (hence uc=255, which will either wrap
484      to 1 or get reset). */
485   if (bw->writeCount) {
486     bw->writePos = dbuf[bw->origPtr];
487     bw->writeCurrent = (unsigned char)bw->writePos;
488     bw->writePos >>= 8;
489     bw->writeRun = -1;
490   }
491 }
492 
493 // Decompress a block of text to intermediate buffer
read_bunzip_data(struct bunzip_data * bd)494 static int read_bunzip_data(struct bunzip_data *bd)
495 {
496   int rc = read_block_header(bd, bd->bwdata);
497   if (!rc) rc=read_huffman_data(bd, bd->bwdata);
498 
499   // First thing that can be done by a background thread.
500   burrows_wheeler_prep(bd, bd->bwdata);
501 
502   return rc;
503 }
504 
505 // Undo burrows-wheeler transform on intermediate buffer to produce output.
506 // If !len, write up to len bytes of data to buf.  Otherwise write to out_fd.
507 // Returns len ? bytes written : 0.  Notice all errors are negative #'s.
508 //
509 // Burrows-wheeler transform is described at:
510 // http://dogma.net/markn/articles/bwt/bwt.htm
511 // http://marknelson.us/1996/09/01/bwt/
512 
write_bunzip_data(struct bunzip_data * bd,struct bwdata * bw,int out_fd,char * outbuf,int len)513 static int write_bunzip_data(struct bunzip_data *bd, struct bwdata *bw,
514   int out_fd, char *outbuf, int len)
515 {
516   unsigned int *dbuf = bw->dbuf;
517   int count, pos, current, run, copies, outbyte, previous, gotcount = 0;
518 
519   for (;;) {
520     // If last read was short due to end of file, return last block now
521     if (bw->writeCount < 0) return bw->writeCount;
522 
523     // If we need to refill dbuf, do it.
524     if (!bw->writeCount) {
525       int i = read_bunzip_data(bd);
526       if (i) {
527         if (i == RETVAL_LAST_BLOCK) {
528           bw->writeCount = i;
529           return gotcount;
530         } else return i;
531       }
532     }
533 
534     // loop generating output
535     count = bw->writeCount;
536     pos = bw->writePos;
537     current = bw->writeCurrent;
538     run = bw->writeRun;
539     while (count) {
540 
541       // If somebody (like tar) wants a certain number of bytes of
542       // data from memory instead of written to a file, humor them.
543       if (len && bd->outbufPos >= len) goto dataus_interruptus;
544       count--;
545 
546       // Follow sequence vector to undo Burrows-Wheeler transform.
547       previous = current;
548       pos = dbuf[pos];
549       current = pos&0xff;
550       pos >>= 8;
551 
552       // Whenever we see 3 consecutive copies of the same byte,
553       // the 4th is a repeat count
554       if (run++ == 3) {
555         copies = current;
556         outbyte = previous;
557         current = -1;
558       } else {
559         copies = 1;
560         outbyte = current;
561       }
562 
563       // Output bytes to buffer, flushing to file if necessary
564       while (copies--) {
565         if (bd->outbufPos == IOBUF_SIZE) flush_bunzip_outbuf(bd, out_fd);
566         bd->outbuf[bd->outbufPos++] = outbyte;
567         bw->dataCRC = (bw->dataCRC << 8)
568                 ^ bd->crc32Table[(bw->dataCRC >> 24) ^ outbyte];
569       }
570       if (current != previous) run=0;
571     }
572 
573     // decompression of this block completed successfully
574     bw->dataCRC = ~(bw->dataCRC);
575     bd->totalCRC = ((bd->totalCRC << 1) | (bd->totalCRC >> 31)) ^ bw->dataCRC;
576 
577     // if this block had a crc error, force file level crc error.
578     if (bw->dataCRC != bw->headerCRC) {
579       bd->totalCRC = bw->headerCRC+1;
580 
581       return RETVAL_LAST_BLOCK;
582     }
583 dataus_interruptus:
584     bw->writeCount = count;
585     if (len) {
586       gotcount += bd->outbufPos;
587       memcpy(outbuf, bd->outbuf, len);
588 
589       // If we got enough data, checkpoint loop state and return
590       if ((len -= bd->outbufPos)<1) {
591         bd->outbufPos -= len;
592         if (bd->outbufPos) memmove(bd->outbuf, bd->outbuf+len, bd->outbufPos);
593         bw->writePos = pos;
594         bw->writeCurrent = current;
595         bw->writeRun = run;
596 
597         return gotcount;
598       }
599     }
600   }
601 }
602 
603 // Allocate the structure, read file header. If !len, src_fd contains
604 // filehandle to read from. Else inbuf contains data.
start_bunzip(struct bunzip_data ** bdp,int src_fd,char * inbuf,int len)605 static int start_bunzip(struct bunzip_data **bdp, int src_fd, char *inbuf,
606   int len)
607 {
608   struct bunzip_data *bd;
609   unsigned int i;
610 
611   // Figure out how much data to allocate.
612   i = sizeof(struct bunzip_data);
613   if (!len) i += IOBUF_SIZE;
614 
615   // Allocate bunzip_data. Most fields initialize to zero.
616   bd = *bdp = xzalloc(i);
617   if (len) {
618     bd->inbuf = inbuf;
619     bd->inbufCount = len;
620     bd->in_fd = -1;
621   } else {
622     bd->inbuf = (char *)(bd+1);
623     bd->in_fd = src_fd;
624   }
625 
626   crc_init(bd->crc32Table, 0);
627 
628   // Ensure that file starts with "BZh".
629   for (i=0;i<3;i++) if (get_bits(bd,8)!="BZh"[i]) return RETVAL_NOT_BZIP_DATA;
630 
631   // Next byte ascii '1'-'9', indicates block size in units of 100k of
632   // uncompressed data. Allocate intermediate buffer for block.
633   i = get_bits(bd, 8);
634   if (i<'1' || i>'9') return RETVAL_NOT_BZIP_DATA;
635   bd->dbufSize = 100000*(i-'0')*THREADS;
636   for (i=0; i<THREADS; i++)
637     bd->bwdata[i].dbuf = xmalloc(bd->dbufSize * sizeof(int));
638 
639   return 0;
640 }
641 
642 // Example usage: decompress src_fd to dst_fd. (Stops at end of bzip data,
643 // not end of file.)
bunzipStream(int src_fd,int dst_fd)644 static char *bunzipStream(int src_fd, int dst_fd)
645 {
646   struct bunzip_data *bd;
647   char *bunzip_errors[] = {0, "not bzip", "bad data", "old format"};
648   int i, j;
649 
650   if (!(i = start_bunzip(&bd,src_fd, 0, 0))) {
651     i = write_bunzip_data(bd,bd->bwdata, dst_fd, 0, 0);
652     if (i==RETVAL_LAST_BLOCK && bd->bwdata[0].headerCRC==bd->totalCRC) i = 0;
653   }
654   flush_bunzip_outbuf(bd, dst_fd);
655 
656   for (j=0; j<THREADS; j++) free(bd->bwdata[j].dbuf);
657   free(bd);
658 
659   return bunzip_errors[-i];
660 }
661 
do_bzcat(int fd,char * name)662 static void do_bzcat(int fd, char *name)
663 {
664   char *err = bunzipStream(fd, 1);
665 
666   if (err) error_exit_raw(err);
667 }
668 
bzcat_main(void)669 void bzcat_main(void)
670 {
671   loopfiles(toys.optargs, do_bzcat);
672 }
673 
do_bunzip2(int fd,char * name)674 static void do_bunzip2(int fd, char *name)
675 {
676   int outfd = 1, rename = 0, len = strlen(name);
677   char *tmp, *err, *dotbz = 0;
678 
679   // Trim off .bz or .bz2 extension
680   dotbz = name+len-3;
681   if ((len>3 && !strcmp(dotbz, ".bz")) || (len>4 && !strcmp(--dotbz, ".bz2")))
682     dotbz = 0;
683 
684   // For - no replace
685   if (toys.optflags&FLAG_t) outfd = xopen("/dev/null", O_WRONLY);
686   else if ((fd || strcmp(name, "-")) && !(toys.optflags&FLAG_c)) {
687     if (toys.optflags&FLAG_k) {
688       if (!dotbz || !access(name, X_OK)) {
689         error_msg("%s exists", name);
690 
691         return;
692       }
693     }
694     outfd = copy_tempfile(fd, name, &tmp);
695     rename++;
696   }
697 
698   if (toys.optflags&FLAG_v) printf("%s:", name);
699   err = bunzipStream(fd, outfd);
700   if (toys.optflags&FLAG_v) {
701     printf("%s\n", err ? err : "ok");
702     toys.exitval |= !!err;
703   } else if (err) error_msg_raw(err);
704 
705   // can't test outfd==1 because may have been called with stdin+stdout closed
706   if (rename) {
707     if (toys.optflags&FLAG_k) {
708       free(tmp);
709       tmp = 0;
710     } else {
711       if (dotbz) *dotbz = '.';
712       if (!unlink(name)) perror_msg_raw(name);
713     }
714     (err ? delete_tempfile : replace_tempfile)(-1, outfd, &tmp);
715   }
716 }
717 
bunzip2_main(void)718 void bunzip2_main(void)
719 {
720   loopfiles(toys.optargs, do_bunzip2);
721 }
722