Mercurial > hg > toybox
annotate lib/bunzip.c @ 214:98820d1eaa79
Upgrade patch to understand creating and deleting files.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 20 Dec 2007 06:30:19 -0600 |
parents | 1e8f4b05cb65 |
children | 34e36dffd47a |
rev | line source |
---|---|
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4: */ |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 /* micro-bunzip, a small, simple bzip2 decompression implementation. |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 Copyright 2003, 2006 by Rob Landley (rob@landley.net). |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 |
54
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
6 Based on a close reading (but not the actual code) of the original bzip2 |
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
7 decompression code by Julian R Seward (jseward@acm.org), which also |
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
8 acknowledges contributions by Mike Burrows, David Wheeler, Peter Fenwick, |
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
9 Alistair Moffat, Radford Neal, Ian H. Witten, Robert Sedgewick, and |
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
10 Jon L. Bentley. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 */ |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 #include "toys.h" |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
15 // Constants for huffman coding |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 #define MAX_GROUPS 6 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 #define GROUP_SIZE 50 /* 64 would have been more efficient */ |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 #define MAX_HUFCODE_BITS 20 /* Longest huffman code allowed */ |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 #define MAX_SYMBOLS 258 /* 256 literals + RUNA + RUNB */ |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 #define SYMBOL_RUNA 0 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 #define SYMBOL_RUNB 1 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
23 // Other housekeeping constants |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
24 #define IOBUF_SIZE 4096 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
25 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
26 // Status return values |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 #define RETVAL_OK 0 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 #define RETVAL_LAST_BLOCK (-1) |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 #define RETVAL_NOT_BZIP_DATA (-2) |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
30 #define RETVAL_DATA_ERROR (-3) |
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
31 #define RETVAL_OBSOLETE_INPUT (-4) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
33 char *bunzip_errors[]={ |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
34 NULL, |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
35 "Not bzip data", |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
36 "Data error", |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
37 "Out of memory", |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
38 "Obsolete (pre 0.9.5) bzip format not supported." |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
39 }; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
41 // This is what we know about each huffman coding group |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 struct group_data { |
62
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
43 int limit[MAX_HUFCODE_BITS+1], base[MAX_HUFCODE_BITS], permute[MAX_SYMBOLS]; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 char minLen, maxLen; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 }; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
47 // Structure holding all the housekeeping data, including IO buffers and |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
48 // memory that persists between calls to bunzip |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 typedef struct { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
50 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
51 // Input stream, input buffer, input bit buffer |
46 | 52 int in_fd, inbufCount, inbufPos; |
53 char *inbuf; | |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 unsigned int inbufBitCount, inbufBits; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
55 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
56 // Output buffer |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 char outbuf[IOBUF_SIZE]; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 int outbufPos; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
59 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
60 // The CRC values stored in the block header and calculated from the data |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
61 unsigned int crc32Table[256], headerCRC, dataCRC, totalCRC; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
62 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
63 // Intermediate buffer and its size (in bytes) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 unsigned int *dbuf, dbufSize; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
65 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
66 // State for interrupting output loop |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
67 int writePos, writeRun, writeCount, writeCurrent; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
69 // These things are a bit too big to go on the stack |
46 | 70 char selectors[32768]; // nSelectors=15 bits |
71 struct group_data groups[MAX_GROUPS]; // huffman coding tables | |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 } bunzip_data; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
74 // Return the next nnn bits of input. All reads from the compressed input |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
75 // are done through this function. All reads are big endian. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 static unsigned int get_bits(bunzip_data *bd, char bits_wanted) |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
78 unsigned int bits = 0; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
79 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
80 // If we need to get more data from the byte buffer, do so. (Loop getting |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
81 // one byte at a time to enforce endianness and avoid unaligned access.) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
82 while (bd->inbufBitCount < bits_wanted) { |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
84 // If we need to read more data from file into byte buffer, do so |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
85 if (bd->inbufPos == bd->inbufCount) { |
40
d046f8d1401e
Trawling through the archives: a broken test, an unnecessary memset, and a
Rob Landley <rob@landley.net>
parents:
39
diff
changeset
|
86 if (0 >= (bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE))) |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
87 error_exit("Unexpected input EOF"); |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
88 bd->inbufPos = 0; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
90 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
91 // Avoid 32-bit overflow (dump bit buffer to top of output) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
92 if (bd->inbufBitCount>=24) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
93 bits = bd->inbufBits&((1<<bd->inbufBitCount)-1); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
94 bits_wanted -= bd->inbufBitCount; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
95 bits <<= bits_wanted; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
96 bd->inbufBitCount = 0; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
98 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
99 // Grab next 8 bits of input from buffer. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
100 bd->inbufBits = (bd->inbufBits<<8) | bd->inbuf[bd->inbufPos++]; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
101 bd->inbufBitCount += 8; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
103 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
104 // Calculate result |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
105 bd->inbufBitCount -= bits_wanted; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
106 bits |= (bd->inbufBits>>bd->inbufBitCount) & ((1<<bits_wanted)-1); |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
107 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 return bits; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
111 // Decompress a block of text to intermediate buffer |
54
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
112 int read_bunzip_data(bunzip_data *bd) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 { |
94
884c03c29f21
Teach build to build only the toys/*.c selected in .config, and teach
Rob Landley <rob@landley.net>
parents:
69
diff
changeset
|
114 struct group_data *hufGroup GCC_BUG; |
61 | 115 unsigned origPtr; |
94
884c03c29f21
Teach build to build only the toys/*.c selected in .config, and teach
Rob Landley <rob@landley.net>
parents:
69
diff
changeset
|
116 int dbufCount, nextSym, dbufSize, groupCount, *base GCC_BUG, *limit GCC_BUG, |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
117 selector, i, j, k, t, runPos, symCount, symTotal, nSelectors, |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
118 byteCount[256]; |
46 | 119 char uc, mtfSymbol[256], symToByte[256], *selectors; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 unsigned int *dbuf; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
122 // Read in header signature and CRC (which is stored big endian) |
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
123 i = get_bits(bd, 24); |
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
124 j = get_bits(bd, 24); |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
125 bd->headerCRC = get_bits(bd,32); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
126 |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
127 // Is this the EOF block with CRC for whole file? |
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
128 if (i==0x177245 && j==0x385090) return RETVAL_LAST_BLOCK; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
129 |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
130 // Is this a valid data block? |
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
131 if (i!=0x314159 || j!=0x265359) return RETVAL_NOT_BZIP_DATA; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
133 dbuf = bd->dbuf; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
134 dbufSize = bd->dbufSize; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
135 selectors = bd->selectors; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
136 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
137 // We can add support for blockRandomised if anybody complains. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
138 if (get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT; |
61 | 139 if ((origPtr = get_bits(bd,24)) > dbufSize) return RETVAL_DATA_ERROR; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
140 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
141 // mapping table: if some byte values are never used (encoding things |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
142 // like ascii text), the compression code removes the gaps to have fewer |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
143 // symbols to deal with, and writes a sparse bitfield indicating which |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
144 // values were present. We make a translation table to convert the symbols |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
145 // back to the corresponding bytes. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
146 t = get_bits(bd, 16); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
147 symTotal = 0; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
148 for (i=0; i<16; i++) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
149 if (t&(1<<(15-i))) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
150 k = get_bits(bd,16); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
151 for (j=0;j<16;j++) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
152 if (k&(1<<(15-j))) symToByte[symTotal++] = (16*i)+j; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
155 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
156 // How many different huffman coding groups does this block use? |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
157 groupCount = get_bits(bd,3); |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 if (groupCount<2 || groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
159 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
160 // nSelectors: Every GROUP_SIZE many symbols we select a new huffman coding |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
161 // group. Read in the group selector list, which is stored as MTF encoded |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
162 // bit runs. (MTF = Move To Front. Every time a symbol occurs it's moved |
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
163 // to the front of the table, so it has a shorter encoding next time.) |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
164 if (!(nSelectors = get_bits(bd, 15))) return RETVAL_DATA_ERROR; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
165 for (i=0; i<groupCount; i++) mtfSymbol[i] = i; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
166 for (i=0; i<nSelectors; i++) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
167 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
168 // Get next value |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
169 for(j=0;get_bits(bd,1);j++) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
170 if (j>=groupCount) return RETVAL_DATA_ERROR; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
171 |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
172 // Decode MTF to get the next selector, and move it to the front. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
173 uc = mtfSymbol[j]; |
55
0bb7c679499a
Memmove is 11 bytes shorter than the unrolled loop, and Manuel Nova pointed
Rob Landley <rob@landley.net>
parents:
54
diff
changeset
|
174 memmove(mtfSymbol+1, mtfSymbol, j); |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
175 mtfSymbol[0] = selectors[i] = uc; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
176 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
177 // Read the huffman coding tables for each group, which code for symTotal |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
178 // literal symbols, plus two run symbols (RUNA, RUNB) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
179 symCount = symTotal+2; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
180 for (j=0; j<groupCount; j++) { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
181 unsigned char length[MAX_SYMBOLS], temp[MAX_HUFCODE_BITS+1]; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
182 int minLen, maxLen, pp; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
183 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
184 // Read lengths |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
185 t = get_bits(bd, 5); |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
186 for (i = 0; i < symCount; i++) { |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
187 for(;;) { |
57
034de958ee65
Fix off by one error in a test.
Rob Landley <rob@landley.net>
parents:
56
diff
changeset
|
188 // !t || t > MAX_HUFCODE_BITS in one test. |
034de958ee65
Fix off by one error in a test.
Rob Landley <rob@landley.net>
parents:
56
diff
changeset
|
189 if (MAX_HUFCODE_BITS-1 < (unsigned)t-1) |
034de958ee65
Fix off by one error in a test.
Rob Landley <rob@landley.net>
parents:
56
diff
changeset
|
190 return RETVAL_DATA_ERROR; |
67
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
191 // Grab 2 bits instead of 1 (slightly smaller/faster). Stop if |
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
192 // first bit is 0, otherwise second bit says whether to |
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
193 // increment or decrement. |
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
194 k = get_bits(bd, 2); |
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
195 if (k & 2) t += 1 - ((k&1)<<1); |
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
196 else { |
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
197 bd->inbufBitCount++; |
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
198 break; |
b6646155e924
Another suggestion from Manuel: Grab 2 bits instead of 1 inside a loop. Saves
Rob Landley <rob@landley.net>
parents:
64
diff
changeset
|
199 } |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 length[i] = t; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
203 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
204 // Find largest and smallest lengths in this group |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
205 minLen = maxLen = length[0]; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
206 for (i = 1; i < symCount; i++) { |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
207 if(length[i] > maxLen) maxLen = length[i]; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
208 else if(length[i] < minLen) minLen = length[i]; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
209 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
210 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
211 /* Calculate permute[], base[], and limit[] tables from length[]. |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
212 * |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
213 * permute[] is the lookup table for converting huffman coded symbols |
68
7b89650905c0
Merge two similar loops and expand a comment.
Rob Landley <rob@landley.net>
parents:
67
diff
changeset
|
214 * into decoded symbols. It contains symbol values sorted by length. |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
94
diff
changeset
|
215 * |
68
7b89650905c0
Merge two similar loops and expand a comment.
Rob Landley <rob@landley.net>
parents:
67
diff
changeset
|
216 * base[] is the amount to subtract from the value of a huffman symbol |
7b89650905c0
Merge two similar loops and expand a comment.
Rob Landley <rob@landley.net>
parents:
67
diff
changeset
|
217 * of a given length when using permute[]. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
218 * |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 * limit[] indicates the largest numerical value a symbol with a given |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
220 * number of bits can have. It lets us know when to stop reading. |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
221 * |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
222 * To use these, keep reading bits until value <= limit[bitcount] or |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
223 * you've read over 20 bits (error). Then the decoded symbol |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
224 * equals permute[hufcode_value - base[hufcode_bitcount]]. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
225 */ |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
226 hufGroup = bd->groups+j; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
227 hufGroup->minLen = minLen; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
228 hufGroup->maxLen = maxLen; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
229 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
230 // Note that minLen can't be smaller than 1, so we adjust the base |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
231 // and limit array pointers so we're not always wasting the first |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
232 // entry. We do this again when using them (during symbol decoding). |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
233 base = hufGroup->base-1; |
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
234 limit = hufGroup->limit-1; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
235 |
68
7b89650905c0
Merge two similar loops and expand a comment.
Rob Landley <rob@landley.net>
parents:
67
diff
changeset
|
236 // Calculate permute[], and zero temp[] and limit[]. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
237 pp = 0; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
94
diff
changeset
|
238 for (i = minLen; i <= maxLen; i++) |
68
7b89650905c0
Merge two similar loops and expand a comment.
Rob Landley <rob@landley.net>
parents:
67
diff
changeset
|
239 temp[i] = limit[i] = 0; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
94
diff
changeset
|
240 for (t = 0; t < symCount; t++) |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
241 if (length[t] == i) hufGroup->permute[pp++] = t; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
242 |
68
7b89650905c0
Merge two similar loops and expand a comment.
Rob Landley <rob@landley.net>
parents:
67
diff
changeset
|
243 // Count symbols coded for at each bit length |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
244 for (i = 0; i < symCount; i++) temp[length[i]]++; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
245 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
246 /* Calculate limit[] (the largest symbol-coding value at each bit |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
247 * length, which is (previous limit<<1)+symbols at this level), and |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
248 * base[] (number of symbols to ignore at each bit length, which is |
68
7b89650905c0
Merge two similar loops and expand a comment.
Rob Landley <rob@landley.net>
parents:
67
diff
changeset
|
249 * limit minus the cumulative count of symbols coded for already). */ |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
250 pp = t = 0; |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
251 for (i = minLen; i < maxLen; i++) { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
252 pp += temp[i]; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
253 limit[i] = pp-1; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
254 pp <<= 1; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
255 base[i+1] = pp-(t+=temp[i]); |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
256 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
257 limit[maxLen] = pp+temp[maxLen]-1; |
62
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
258 limit[maxLen+1] = INT_MAX; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
259 base[minLen] = 0; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
260 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
261 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
262 // We've finished reading and digesting the block header. Now read this |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
263 // block's huffman coded symbols from the file and undo the huffman coding |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
264 // and run length encoding, saving the result into dbuf[dbufCount++] = uc |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
265 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
266 // Initialize symbol occurrence counters and symbol mtf table |
69
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
267 for(i=0; i<256; i++) { |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
268 byteCount[i] = 0; |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
269 mtfSymbol[i] = i; |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
270 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
271 |
56
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
272 // Loop through compressed symbols. This is the first "tight inner loop" |
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
273 // that needs to be micro-optimized for speed. (This one fills out dbuf[] |
7794562e2e64
Comment and whitespace changes.
Rob Landley <rob@landley.net>
parents:
55
diff
changeset
|
274 // linearly, staying in cache more, so isn't as limited by DRAM access.) |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
275 runPos = dbufCount = symCount = selector = 0; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
276 for (;;) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
277 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
278 // Determine which huffman coding group to use. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
279 if (!(symCount--)) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
280 symCount = GROUP_SIZE-1; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
281 if (selector >= nSelectors) return RETVAL_DATA_ERROR; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
282 hufGroup = bd->groups+selectors[selector++]; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
283 base = hufGroup->base-1; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
284 limit = hufGroup->limit-1; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
285 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
286 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
287 // Read next huffman-coded symbol |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
288 i = hufGroup->minLen; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
289 j = get_bits(bd, i); |
62
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
290 while (j > limit[i]) { |
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
291 // if (i > hufGroup->maxLen) return RETVAL_DATA_ERROR; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
292 i++; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
293 |
62
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
294 // Unroll get_bits() to avoid a function call when the data's in |
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
295 // the buffer already. |
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
296 k = bd->inbufBitCount |
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
297 ? (bd->inbufBits >> --(bd->inbufBitCount)) & 1 |
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
298 : get_bits(bd, 1); |
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
299 j = (j << 1) | k; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
300 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
301 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
302 // Huffman decode nextSym (with bounds checking) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
303 j-=base[i]; |
62
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
304 if (i > hufGroup->maxLen || (unsigned)j >= MAX_SYMBOLS) |
f41f997c1e73
More optimizations originally suggested by Manuel Nova: Use a sentinel value
Rob Landley <rob@landley.net>
parents:
61
diff
changeset
|
305 return RETVAL_DATA_ERROR; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
306 nextSym = hufGroup->permute[j]; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
307 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
308 // If this is a repeated run, loop collecting data |
58
b060142aee01
Minor optimization the compiler already does for us (according to bloatcheck),
Rob Landley <rob@landley.net>
parents:
57
diff
changeset
|
309 if ((unsigned)nextSym <= SYMBOL_RUNB) { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
310 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
311 // If this is the start of a new run, zero out counter |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
312 if(!runPos) { |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
313 runPos = 1; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
314 t = 0; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
315 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
316 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
317 /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
318 each bit position, add 1 or 2 instead. For example, |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
319 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2. |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
320 You can make any bit pattern that way using 1 less symbol than |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
321 the basic or 0/1 method (except all bits 0, which would use no |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
322 symbols, but a run of length 0 doesn't mean anything in this |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
323 context). Thus space is saved. */ |
59
e82ae73acbd7
Another application of Manuel Nova's "math instead of ifs" trick. Saves 17b.
Rob Landley <rob@landley.net>
parents:
58
diff
changeset
|
324 t += (runPos << nextSym); // +runPos if RUNA; +2*runPos if RUNB |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
325 runPos <<= 1; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
326 continue; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
327 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
328 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
329 /* When we hit the first non-run symbol after a run, we now know |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
330 how many times to repeat the last literal, so append that many |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
331 copies to our buffer of decoded symbols (dbuf) now. (The last |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
332 literal used is the one at the head of the mtfSymbol array.) */ |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
333 if (runPos) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
334 runPos = 0; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
335 if (dbufCount+t >= dbufSize) return RETVAL_DATA_ERROR; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
336 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
337 uc = symToByte[mtfSymbol[0]]; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
338 byteCount[uc] += t; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
339 while (t--) dbuf[dbufCount++] = uc; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
340 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
341 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
342 // Is this the terminating symbol? |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
343 if (nextSym>symTotal) break; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
344 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
345 /* At this point, the symbol we just decoded indicates a new literal |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
346 character. Subtract one to get the position in the MTF array |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
347 at which this literal is currently to be found. (Note that the |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
348 result can't be -1 or 0, because 0 and 1 are RUNA and RUNB. |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
349 Another instance of the first symbol in the mtf array, position 0, |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
350 would have been handled as part of a run.) */ |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
351 if (dbufCount>=dbufSize) return RETVAL_DATA_ERROR; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
352 i = nextSym - 1; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
353 uc = mtfSymbol[i]; |
61 | 354 // On my laptop, unrolling this memmove() into a loop shaves 3.5% off |
355 // the total running time. | |
60
23aac9d42234
Turn a memmove into a while(), reducing running time by 3.5% in my tests.
Rob Landley <rob@landley.net>
parents:
59
diff
changeset
|
356 while(i--) mtfSymbol[i+1] = mtfSymbol[i]; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
357 mtfSymbol[0] = uc; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
358 uc = symToByte[uc]; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
359 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
360 // We have our literal byte. Save it into dbuf. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
361 byteCount[uc]++; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
362 dbuf[dbufCount++] = (unsigned int)uc; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
363 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
364 |
69
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
365 /* At this point, we've finished reading all of this block's huffman-coded |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
366 * symbols (and repeated runs) from the input stream, and have written |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
367 * dbufCount many of them into dbuf[], the intermediate buffer. |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
368 * |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
369 * Now undo the Burrows-Wheeler transform on dbuf, described here: |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
370 * http://dogma.net/markn/articles/bwt/bwt.htm |
530168fc253f
Merge a memset with an existing loop, tweak comments.
Rob Landley <rob@landley.net>
parents:
68
diff
changeset
|
371 * http://marknelson.us/1996/09/01/bwt/ |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
372 */ |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
373 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
374 // Now we know what dbufCount is, do a better sanity check on origPtr. |
61 | 375 if (origPtr>=dbufCount) return RETVAL_DATA_ERROR; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
376 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
377 // Turn byteCount into cumulative occurrence counts of 0 to n-1. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
378 j = 0; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
379 for (i=0;i<256;i++) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
380 k = j+byteCount[i]; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
381 byteCount[i] = j; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
382 j = k; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
383 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
384 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
385 // Figure out what order dbuf would be in if we sorted it. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
386 for (i=0; i<dbufCount; i++) { |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
387 uc = (unsigned char)(dbuf[i] & 0xff); |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
388 dbuf[byteCount[uc]] |= (i << 8); |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
389 byteCount[uc]++; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
390 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
391 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
392 // blockRandomised support would go here. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
393 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
394 // Using i as position, j as previous character, t as current character, |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
395 // and uc as run count. |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
396 bd->dataCRC = 0xffffffffL; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
397 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
398 /* Decode first byte by hand to initialize "previous" byte. Note that it |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
399 doesn't get output, and if the first three characters are identical |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
400 it doesn't qualify as a run (hence uc=255, which will either wrap |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
401 to 1 or get reset). */ |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
402 if (dbufCount) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
403 bd->writePos = dbuf[origPtr]; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
404 bd->writeCurrent = (unsigned char)(bd->writePos&0xff); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
405 bd->writePos >>= 8; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
406 bd->writeRun = -1; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
407 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
408 bd->writeCount = dbufCount; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
409 |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
410 return RETVAL_OK; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
411 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
412 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
413 // Flush output buffer to disk |
54
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
414 void flush_bunzip_outbuf(bunzip_data *bd, int out_fd) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
415 { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
416 if (bd->outbufPos) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
417 if (write(out_fd, bd->outbuf, bd->outbufPos) != bd->outbufPos) |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
418 error_exit("Unexpected output EOF"); |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
419 bd->outbufPos = 0; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
420 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
421 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
422 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
423 // Undo burrows-wheeler transform on intermediate buffer to produce output. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
424 // If !len, write up to len bytes of data to buf. Otherwise write to out_fd. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
425 // Returns len ? bytes written : RETVAL_OK. Notice all errors negative #'s. |
54
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
426 int write_bunzip_data(bunzip_data *bd, int out_fd, char *outbuf, int len) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
427 { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
428 unsigned int *dbuf = bd->dbuf; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
429 int count, pos, current, run, copies, outbyte, previous, gotcount = 0; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
430 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
431 for (;;) { |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
432 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
433 // If last read was short due to end of file, return last block now |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
434 if (bd->writeCount < 0) return bd->writeCount; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
435 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
436 // If we need to refill dbuf, do it. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
437 if (!bd->writeCount) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
438 int i = read_bunzip_data(bd); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
439 if (i) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
440 if (i == RETVAL_LAST_BLOCK) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
441 bd->writeCount = i; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
442 return gotcount; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
443 } else return i; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
444 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
445 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
446 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
447 // Loop generating output |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
448 count = bd->writeCount; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
449 pos = bd->writePos; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
450 current = bd->writeCurrent; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
451 run = bd->writeRun; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
452 while (count) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
453 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
454 // If somebody (like tar) wants a certain number of bytes of |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
455 // data from memory instead of written to a file, humor them. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
456 if (len && bd->outbufPos>=len) goto dataus_interruptus; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
457 count--; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
458 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
459 // Follow sequence vector to undo Burrows-Wheeler transform. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
460 previous = current; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
461 pos = dbuf[pos]; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
462 current = pos&0xff; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
463 pos >>= 8; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
464 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
465 // Whenever we see 3 consecutive copies of the same byte, |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
466 // the 4th is a repeat count |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
467 if (run++ == 3) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
468 copies = current; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
469 outbyte = previous; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
470 current = -1; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
471 } else { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
472 copies = 1; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
473 outbyte = current; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
474 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
475 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
476 // Output bytes to buffer, flushing to file if necessary |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
477 while (copies--) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
478 if (bd->outbufPos == IOBUF_SIZE) flush_bunzip_outbuf(bd,out_fd); |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
479 bd->outbuf[bd->outbufPos++] = outbyte; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
480 bd->dataCRC = (bd->dataCRC << 8) |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
481 ^ bd->crc32Table[(bd->dataCRC >> 24) ^ outbyte]; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
482 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
483 if (current!=previous) run=0; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
484 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
485 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
486 // Decompression of this block completed successfully |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
487 bd->dataCRC = ~(bd->dataCRC); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
488 bd->totalCRC = ((bd->totalCRC << 1) | (bd->totalCRC >> 31)) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
489 ^ bd->dataCRC; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
490 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
491 // If this block had a CRC error, force file level CRC error. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
492 if (bd->dataCRC != bd->headerCRC) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
493 bd->totalCRC = bd->headerCRC+1; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
494 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
495 return RETVAL_LAST_BLOCK; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
496 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
497 dataus_interruptus: |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
498 bd->writeCount = count; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
499 if (len) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
500 gotcount += bd->outbufPos; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
501 memcpy(outbuf, bd->outbuf, len); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
502 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
503 // If we got enough data, checkpoint loop state and return |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
504 if ((len-=bd->outbufPos)<1) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
505 bd->outbufPos -= len; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
506 if (bd->outbufPos) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
507 memmove(bd->outbuf, bd->outbuf+len, bd->outbufPos); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
508 bd->writePos = pos; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
509 bd->writeCurrent = current; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
510 bd->writeRun = run; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
511 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
512 return gotcount; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
513 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
514 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
515 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
516 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
517 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
518 // Allocate the structure, read file header. If !len, src_fd contains |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
519 // filehandle to read from. Else inbuf contains data. |
54
60a7fb8ddeb4
Add a quick and dirt bzcat (stdin to stdout only for the moment) to test
Rob Landley <rob@landley.net>
parents:
46
diff
changeset
|
520 int start_bunzip(bunzip_data **bdp, int src_fd, char *inbuf, int len) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
521 { |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
522 bunzip_data *bd; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
523 unsigned int i, j, c; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
524 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
525 // Figure out how much data to allocate. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
526 i = sizeof(bunzip_data); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
527 if (!len) i += IOBUF_SIZE; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
528 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
529 // Allocate bunzip_data. Most fields initialize to zero. |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
530 bd = *bdp = xzalloc(i); |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
531 if (len) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
532 bd->inbuf = inbuf; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
533 bd->inbufCount = len; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
534 bd->in_fd = -1; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
535 } else { |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
536 bd->inbuf = (char *)(bd+1); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
537 bd->in_fd = src_fd; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
538 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
539 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
540 // Init the CRC32 table (big endian) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
541 for (i=0; i<256; i++) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
542 c = i<<24; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
543 for (j=8; j; j--) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
544 c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1); |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
545 bd->crc32Table[i] = c; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
546 } |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
547 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
548 // Ensure that file starts with "BZh". |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
549 for (i=0;i<3;i++) |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
550 if (get_bits(bd,8)!="BZh"[i]) return RETVAL_NOT_BZIP_DATA; |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
551 |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
552 // Next byte ascii '1'-'9', indicates block size in units of 100k of |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
553 // uncompressed data. Allocate intermediate buffer for block. |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
554 i = get_bits(bd, 8); |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
555 if (i<'1' || i>'9') return RETVAL_NOT_BZIP_DATA; |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
556 bd->dbufSize = 100000*(i-'0'); |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
557 bd->dbuf = xmalloc(bd->dbufSize * sizeof(int)); |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
558 |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
559 return RETVAL_OK; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
560 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
561 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
562 // Example usage: decompress src_fd to dst_fd. (Stops at end of bzip data, |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
563 // not end of file.) |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
564 void bunzipStream(int src_fd, int dst_fd) |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
565 { |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
566 bunzip_data *bd; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
567 int i; |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
568 |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
569 if (!(i = start_bunzip(&bd,src_fd,0,0))) { |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
570 i = write_bunzip_data(bd,dst_fd,0,0); |
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
571 if (i==RETVAL_LAST_BLOCK && bd->headerCRC==bd->totalCRC) i = RETVAL_OK; |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
572 } |
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
573 flush_bunzip_outbuf(bd,dst_fd); |
39
00e73c4dea66
More whitespace/cowbell. (And change comment style to //.)
Rob Landley <rob@landley.net>
parents:
37
diff
changeset
|
574 free(bd->dbuf); |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
575 free(bd); |
64
67ee3a0b76e1
In bunzip replace setjmp/longjmp handling with error_exit(), replace string
Rob Landley <rob@landley.net>
parents:
62
diff
changeset
|
576 if (i) error_exit(bunzip_errors[-i]); |
37
14d27eab3acf
Add my old micro-bunzip library. Needs some cleanup...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
577 } |