aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-04-10 17:16:33 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-04-10 17:16:33 +0000
commitb38cf3ff8a31b2b35d6bcfb1513d568d1977bcba (patch)
tree16da2cd8c469815f4506b59c73b3668b694ea316
parenta9d7d24e1f524bb7528aa881265fe8f826023751 (diff)
downloadbusybox-w32-b38cf3ff8a31b2b35d6bcfb1513d568d1977bcba.tar.gz
busybox-w32-b38cf3ff8a31b2b35d6bcfb1513d568d1977bcba.tar.bz2
busybox-w32-b38cf3ff8a31b2b35d6bcfb1513d568d1977bcba.zip
bunzip2: big style cleanup. No code changes apart from one s/write/safe_write/
(verified with objdump).
-rw-r--r--archival/libunarchive/decompress_bunzip2.c398
1 files changed, 208 insertions, 190 deletions
diff --git a/archival/libunarchive/decompress_bunzip2.c b/archival/libunarchive/decompress_bunzip2.c
index dd2e1ddcb..75d225bee 100644
--- a/archival/libunarchive/decompress_bunzip2.c
+++ b/archival/libunarchive/decompress_bunzip2.c
@@ -55,7 +55,7 @@
55/* This is what we know about each Huffman coding group */ 55/* This is what we know about each Huffman coding group */
56struct group_data { 56struct group_data {
57 /* We have an extra slot at the end of limit[] for a sentinal value. */ 57 /* We have an extra slot at the end of limit[] for a sentinal value. */
58 int limit[MAX_HUFCODE_BITS+1],base[MAX_HUFCODE_BITS],permute[MAX_SYMBOLS]; 58 int limit[MAX_HUFCODE_BITS+1], base[MAX_HUFCODE_BITS], permute[MAX_SYMBOLS];
59 int minLen, maxLen; 59 int minLen, maxLen;
60}; 60};
61 61
@@ -65,13 +65,13 @@ struct group_data {
65typedef struct { 65typedef struct {
66 /* State for interrupting output loop */ 66 /* State for interrupting output loop */
67 67
68 int writeCopies,writePos,writeRunCountdown,writeCount,writeCurrent; 68 int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
69 69
70 /* I/O tracking data (file handles, buffers, positions, etc.) */ 70 /* I/O tracking data (file handles, buffers, positions, etc.) */
71 71
72 int in_fd,out_fd,inbufCount,inbufPos /*,outbufPos*/; 72 int in_fd, out_fd, inbufCount, inbufPos /*, outbufPos*/;
73 unsigned char *inbuf /*,*outbuf*/; 73 unsigned char *inbuf /*,*outbuf*/;
74 unsigned int inbufBitCount, inbufBits; 74 unsigned inbufBitCount, inbufBits;
75 75
76 /* The CRC values stored in the block header and calculated from the data */ 76 /* The CRC values stored in the block header and calculated from the data */
77 77
@@ -79,7 +79,7 @@ typedef struct {
79 uint32_t *crc32Table; 79 uint32_t *crc32Table;
80 /* Intermediate buffer and its size (in bytes) */ 80 /* Intermediate buffer and its size (in bytes) */
81 81
82 unsigned int *dbuf, dbufSize; 82 unsigned *dbuf, dbufSize;
83 83
84 /* These things are a bit too big to go on the stack */ 84 /* These things are a bit too big to go on the stack */
85 85
@@ -94,42 +94,43 @@ typedef struct {
94/* Return the next nnn bits of input. All reads from the compressed input 94/* Return the next nnn bits of input. All reads from the compressed input
95 are done through this function. All reads are big endian */ 95 are done through this function. All reads are big endian */
96 96
97static unsigned int get_bits(bunzip_data *bd, char bits_wanted) 97static unsigned get_bits(bunzip_data *bd, char bits_wanted)
98{ 98{
99 unsigned int bits=0; 99 unsigned bits = 0;
100 100
101 /* If we need to get more data from the byte buffer, do so. (Loop getting 101 /* If we need to get more data from the byte buffer, do so. (Loop getting
102 one byte at a time to enforce endianness and avoid unaligned access.) */ 102 one byte at a time to enforce endianness and avoid unaligned access.) */
103 103
104 while (bd->inbufBitCount<bits_wanted) { 104 while (bd->inbufBitCount < bits_wanted) {
105 105
106 /* If we need to read more data from file into byte buffer, do so */ 106 /* If we need to read more data from file into byte buffer, do so */
107 107
108 if(bd->inbufPos==bd->inbufCount) { 108 if (bd->inbufPos == bd->inbufCount) {
109 if((bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE)) <= 0) 109 bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE);
110 longjmp(bd->jmpbuf,RETVAL_UNEXPECTED_INPUT_EOF); 110 if (bd->inbufCount <= 0)
111 bd->inbufPos=0; 111 longjmp(bd->jmpbuf, RETVAL_UNEXPECTED_INPUT_EOF);
112 bd->inbufPos = 0;
112 } 113 }
113 114
114 /* Avoid 32-bit overflow (dump bit buffer to top of output) */ 115 /* Avoid 32-bit overflow (dump bit buffer to top of output) */
115 116
116 if(bd->inbufBitCount>=24) { 117 if (bd->inbufBitCount >= 24) {
117 bits=bd->inbufBits&((1<<bd->inbufBitCount)-1); 118 bits = bd->inbufBits & ((1 << bd->inbufBitCount) - 1);
118 bits_wanted-=bd->inbufBitCount; 119 bits_wanted -= bd->inbufBitCount;
119 bits<<=bits_wanted; 120 bits <<= bits_wanted;
120 bd->inbufBitCount=0; 121 bd->inbufBitCount = 0;
121 } 122 }
122 123
123 /* Grab next 8 bits of input from buffer. */ 124 /* Grab next 8 bits of input from buffer. */
124 125
125 bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++]; 126 bd->inbufBits = (bd->inbufBits<<8) | bd->inbuf[bd->inbufPos++];
126 bd->inbufBitCount+=8; 127 bd->inbufBitCount += 8;
127 } 128 }
128 129
129 /* Calculate result */ 130 /* Calculate result */
130 131
131 bd->inbufBitCount-=bits_wanted; 132 bd->inbufBitCount -= bits_wanted;
132 bits|=(bd->inbufBits>>bd->inbufBitCount)&((1<<bits_wanted)-1); 133 bits |= (bd->inbufBits >> bd->inbufBitCount) & ((1 << bits_wanted) - 1);
133 134
134 return bits; 135 return bits;
135} 136}
@@ -139,26 +140,26 @@ static unsigned int get_bits(bunzip_data *bd, char bits_wanted)
139static int get_next_block(bunzip_data *bd) 140static int get_next_block(bunzip_data *bd)
140{ 141{
141 struct group_data *hufGroup; 142 struct group_data *hufGroup;
142 int dbufCount,nextSym,dbufSize,groupCount,*base,*limit,selector, 143 int dbufCount, nextSym, dbufSize, groupCount, *base, *limit, selector,
143 i,j,k,t,runPos,symCount,symTotal,nSelectors,byteCount[256]; 144 i, j, k, t, runPos, symCount, symTotal, nSelectors, byteCount[256];
144 unsigned char uc, symToByte[256], mtfSymbol[256], *selectors; 145 unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
145 unsigned int *dbuf,origPtr; 146 unsigned *dbuf, origPtr;
146 147
147 dbuf=bd->dbuf; 148 dbuf = bd->dbuf;
148 dbufSize=bd->dbufSize; 149 dbufSize = bd->dbufSize;
149 selectors=bd->selectors; 150 selectors = bd->selectors;
150 151
151 /* Reset longjmp I/O error handling */ 152 /* Reset longjmp I/O error handling */
152 153
153 i=setjmp(bd->jmpbuf); 154 i = setjmp(bd->jmpbuf);
154 if (i) return i; 155 if (i) return i;
155 156
156 /* Read in header signature and CRC, then validate signature. 157 /* Read in header signature and CRC, then validate signature.
157 (last block signature means CRC is for whole file, return now) */ 158 (last block signature means CRC is for whole file, return now) */
158 159
159 i = get_bits(bd,24); 160 i = get_bits(bd, 24);
160 j = get_bits(bd,24); 161 j = get_bits(bd, 24);
161 bd->headerCRC=get_bits(bd,32); 162 bd->headerCRC = get_bits(bd, 32);
162 if ((i == 0x177245) && (j == 0x385090)) return RETVAL_LAST_BLOCK; 163 if ((i == 0x177245) && (j == 0x385090)) return RETVAL_LAST_BLOCK;
163 if ((i != 0x314159) || (j != 0x265359)) return RETVAL_NOT_BZIP_DATA; 164 if ((i != 0x314159) || (j != 0x265359)) return RETVAL_NOT_BZIP_DATA;
164 165
@@ -166,8 +167,9 @@ static int get_next_block(bunzip_data *bd)
166 some code for this in busybox 1.0.0-pre3, but nobody ever noticed that 167 some code for this in busybox 1.0.0-pre3, but nobody ever noticed that
167 it didn't actually work. */ 168 it didn't actually work. */
168 169
169 if (get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT; 170 if (get_bits(bd, 1)) return RETVAL_OBSOLETE_INPUT;
170 if ((origPtr=get_bits(bd,24)) > dbufSize) return RETVAL_DATA_ERROR; 171 origPtr = get_bits(bd, 24);
172 if (origPtr > dbufSize) return RETVAL_DATA_ERROR;
171 173
172 /* mapping table: if some byte values are never used (encoding things 174 /* mapping table: if some byte values are never used (encoding things
173 like ascii text), the compression code removes the gaps to have fewer 175 like ascii text), the compression code removes the gaps to have fewer
@@ -175,48 +177,52 @@ static int get_next_block(bunzip_data *bd)
175 values were present. We make a translation table to convert the symbols 177 values were present. We make a translation table to convert the symbols
176 back to the corresponding bytes. */ 178 back to the corresponding bytes. */
177 179
178 t=get_bits(bd, 16); 180 t = get_bits(bd, 16);
179 symTotal=0; 181 symTotal = 0;
180 for (i=0;i<16;i++) { 182 for (i = 0; i < 16; i++) {
181 if(t&(1<<(15-i))) { 183 if (t & (1 << (15-i))) {
182 k=get_bits(bd,16); 184 k = get_bits(bd, 16);
183 for (j=0;j<16;j++) 185 for (j = 0; j < 16; j++)
184 if(k&(1<<(15-j))) symToByte[symTotal++]=(16*i)+j; 186 if (k & (1 << (15-j)))
187 symToByte[symTotal++] = (16*i) + j;
185 } 188 }
186 } 189 }
187 190
188 /* How many different Huffman coding groups does this block use? */ 191 /* How many different Huffman coding groups does this block use? */
189 192
190 groupCount=get_bits(bd,3); 193 groupCount = get_bits(bd, 3);
191 if (groupCount<2 || groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR; 194 if (groupCount < 2 || groupCount > MAX_GROUPS)
195 return RETVAL_DATA_ERROR;
192 196
193 /* nSelectors: Every GROUP_SIZE many symbols we select a new Huffman coding 197 /* nSelectors: Every GROUP_SIZE many symbols we select a new Huffman coding
194 group. Read in the group selector list, which is stored as MTF encoded 198 group. Read in the group selector list, which is stored as MTF encoded
195 bit runs. (MTF=Move To Front, as each value is used it's moved to the 199 bit runs. (MTF=Move To Front, as each value is used it's moved to the
196 start of the list.) */ 200 start of the list.) */
197 201
198 if(!(nSelectors=get_bits(bd, 15))) return RETVAL_DATA_ERROR; 202 nSelectors = get_bits(bd, 15);
199 for (i=0; i<groupCount; i++) mtfSymbol[i] = i; 203 if (!nSelectors) return RETVAL_DATA_ERROR;
200 for (i=0; i<nSelectors; i++) { 204 for (i = 0; i < groupCount; i++) mtfSymbol[i] = i;
205 for (i = 0; i < nSelectors; i++) {
201 206
202 /* Get next value */ 207 /* Get next value */
203 208
204 for (j=0;get_bits(bd,1);j++) if (j>=groupCount) return RETVAL_DATA_ERROR; 209 for (j = 0; get_bits(bd, 1); j++)
210 if (j>=groupCount) return RETVAL_DATA_ERROR;
205 211
206 /* Decode MTF to get the next selector */ 212 /* Decode MTF to get the next selector */
207 213
208 uc = mtfSymbol[j]; 214 uc = mtfSymbol[j];
209 for (;j;j--) mtfSymbol[j] = mtfSymbol[j-1]; 215 for (;j;j--) mtfSymbol[j] = mtfSymbol[j-1];
210 mtfSymbol[0]=selectors[i]=uc; 216 mtfSymbol[0] = selectors[i] = uc;
211 } 217 }
212 218
213 /* Read the Huffman coding tables for each group, which code for symTotal 219 /* Read the Huffman coding tables for each group, which code for symTotal
214 literal symbols, plus two run symbols (RUNA, RUNB) */ 220 literal symbols, plus two run symbols (RUNA, RUNB) */
215 221
216 symCount=symTotal+2; 222 symCount = symTotal + 2;
217 for (j=0; j<groupCount; j++) { 223 for (j = 0; j < groupCount; j++) {
218 unsigned char length[MAX_SYMBOLS],temp[MAX_HUFCODE_BITS+1]; 224 unsigned char length[MAX_SYMBOLS], temp[MAX_HUFCODE_BITS+1];
219 int minLen, maxLen, pp; 225 int minLen, maxLen, pp;
220 226
221 /* Read Huffman code lengths for each symbol. They're stored in 227 /* Read Huffman code lengths for each symbol. They're stored in
222 a way similar to mtf; record a starting value for the first symbol, 228 a way similar to mtf; record a starting value for the first symbol,
@@ -225,17 +231,17 @@ static int get_next_block(bunzip_data *bd)
225 an optimization that makes the test inside the loop simpler: symbol 231 an optimization that makes the test inside the loop simpler: symbol
226 length 0 becomes negative, so an unsigned inequality catches it.) */ 232 length 0 becomes negative, so an unsigned inequality catches it.) */
227 233
228 t=get_bits(bd, 5)-1; 234 t = get_bits(bd, 5) - 1;
229 for (i = 0; i < symCount; i++) { 235 for (i = 0; i < symCount; i++) {
230 for (;;) { 236 for (;;) {
231 if (((unsigned)t) > (MAX_HUFCODE_BITS-1)) 237 if ((unsigned)t > (MAX_HUFCODE_BITS-1))
232 return RETVAL_DATA_ERROR; 238 return RETVAL_DATA_ERROR;
233 239
234 /* If first bit is 0, stop. Else second bit indicates whether 240 /* If first bit is 0, stop. Else second bit indicates whether
235 to increment or decrement the value. Optimization: grab 2 241 to increment or decrement the value. Optimization: grab 2
236 bits and unget the second if the first was 0. */ 242 bits and unget the second if the first was 0. */
237 243
238 k = get_bits(bd,2); 244 k = get_bits(bd, 2);
239 if (k < 2) { 245 if (k < 2) {
240 bd->inbufBitCount++; 246 bd->inbufBitCount++;
241 break; 247 break;
@@ -243,20 +249,20 @@ static int get_next_block(bunzip_data *bd)
243 249
244 /* Add one if second bit 1, else subtract 1. Avoids if/else */ 250 /* Add one if second bit 1, else subtract 1. Avoids if/else */
245 251
246 t+=(((k+1)&2)-1); 252 t += (((k+1) & 2) - 1);
247 } 253 }
248 254
249 /* Correct for the initial -1, to get the final symbol length */ 255 /* Correct for the initial -1, to get the final symbol length */
250 256
251 length[i]=t+1; 257 length[i] = t + 1;
252 } 258 }
253 259
254 /* Find largest and smallest lengths in this group */ 260 /* Find largest and smallest lengths in this group */
255 261
256 minLen=maxLen=length[0]; 262 minLen = maxLen = length[0];
257 for (i = 1; i < symCount; i++) { 263 for (i = 1; i < symCount; i++) {
258 if(length[i] > maxLen) maxLen = length[i]; 264 if (length[i] > maxLen) maxLen = length[i];
259 else if(length[i] < minLen) minLen = length[i]; 265 else if (length[i] < minLen) minLen = length[i];
260 } 266 }
261 267
262 /* Calculate permute[], base[], and limit[] tables from length[]. 268 /* Calculate permute[], base[], and limit[] tables from length[].
@@ -270,7 +276,7 @@ static int get_next_block(bunzip_data *bd)
270 * length: each code with a value>limit[length] needs another bit. 276 * length: each code with a value>limit[length] needs another bit.
271 */ 277 */
272 278
273 hufGroup=bd->groups+j; 279 hufGroup = bd->groups + j;
274 hufGroup->minLen = minLen; 280 hufGroup->minLen = minLen;
275 hufGroup->maxLen = maxLen; 281 hufGroup->maxLen = maxLen;
276 282
@@ -278,30 +284,31 @@ static int get_next_block(bunzip_data *bd)
278 and limit array pointers so we're not always wasting the first 284 and limit array pointers so we're not always wasting the first
279 entry. We do this again when using them (during symbol decoding).*/ 285 entry. We do this again when using them (during symbol decoding).*/
280 286
281 base=hufGroup->base-1; 287 base = hufGroup->base - 1;
282 limit=hufGroup->limit-1; 288 limit = hufGroup->limit - 1;
283 289
284 /* Calculate permute[]. Concurently, initialize temp[] and limit[]. */ 290 /* Calculate permute[]. Concurently, initialize temp[] and limit[]. */
285 291
286 pp=0; 292 pp = 0;
287 for (i=minLen;i<=maxLen;i++) { 293 for (i = minLen; i <= maxLen; i++) {
288 temp[i]=limit[i]=0; 294 temp[i] = limit[i] = 0;
289 for (t=0;t<symCount;t++) 295 for (t = 0; t < symCount; t++)
290 if(length[t]==i) hufGroup->permute[pp++] = t; 296 if (length[t] == i)
297 hufGroup->permute[pp++] = t;
291 } 298 }
292 299
293 /* Count symbols coded for at each bit length */ 300 /* Count symbols coded for at each bit length */
294 301
295 for (i=0;i<symCount;i++) temp[length[i]]++; 302 for (i = 0; i < symCount; i++) temp[length[i]]++;
296 303
297 /* Calculate limit[] (the largest symbol-coding value at each bit 304 /* Calculate limit[] (the largest symbol-coding value at each bit
298 * length, which is (previous limit<<1)+symbols at this level), and 305 * length, which is (previous limit<<1)+symbols at this level), and
299 * base[] (number of symbols to ignore at each bit length, which is 306 * base[] (number of symbols to ignore at each bit length, which is
300 * limit minus the cumulative count of symbols coded for already). */ 307 * limit minus the cumulative count of symbols coded for already). */
301 308
302 pp=t=0; 309 pp = t = 0;
303 for (i=minLen; i<maxLen; i++) { 310 for (i = minLen; i < maxLen; i++) {
304 pp+=temp[i]; 311 pp += temp[i];
305 312
306 /* We read the largest possible symbol size and then unget bits 313 /* We read the largest possible symbol size and then unget bits
307 after determining how many we need, and those extra bits could 314 after determining how many we need, and those extra bits could
@@ -310,13 +317,14 @@ static int get_next_block(bunzip_data *bd)
310 so here we set all the trailing to-be-ignored bits to 1 so they 317 so here we set all the trailing to-be-ignored bits to 1 so they
311 don't affect the value>limit[length] comparison. */ 318 don't affect the value>limit[length] comparison. */
312 319
313 limit[i]= (pp << (maxLen - i)) - 1; 320 limit[i] = (pp << (maxLen - i)) - 1;
314 pp<<=1; 321 pp <<= 1;
315 base[i+1]=pp-(t+=temp[i]); 322 t += temp[i];
323 base[i+1] = pp - t;
316 } 324 }
317 limit[maxLen+1] = INT_MAX; /* Sentinal value for reading next sym. */ 325 limit[maxLen+1] = INT_MAX; /* Sentinal value for reading next sym. */
318 limit[maxLen]=pp+temp[maxLen]-1; 326 limit[maxLen] = pp + temp[maxLen] - 1;
319 base[minLen]=0; 327 base[minLen] = 0;
320 } 328 }
321 329
322 /* We've finished reading and digesting the block header. Now read this 330 /* We've finished reading and digesting the block header. Now read this
@@ -325,24 +333,24 @@ static int get_next_block(bunzip_data *bd)
325 333
326 /* Initialize symbol occurrence counters and symbol Move To Front table */ 334 /* Initialize symbol occurrence counters and symbol Move To Front table */
327 335
328 for (i=0;i<256;i++) { 336 for (i = 0; i < 256; i++) {
329 byteCount[i] = 0; 337 byteCount[i] = 0;
330 mtfSymbol[i]=(unsigned char)i; 338 mtfSymbol[i] = (unsigned char)i;
331 } 339 }
332 340
333 /* Loop through compressed symbols. */ 341 /* Loop through compressed symbols. */
334 342
335 runPos=dbufCount=selector=0; 343 runPos = dbufCount = selector = 0;
336 for (;;) { 344 for (;;) {
337 345
338 /* fetch next Huffman coding group from list. */ 346 /* fetch next Huffman coding group from list. */
339 347
340 symCount=GROUP_SIZE-1; 348 symCount = GROUP_SIZE - 1;
341 if(selector>=nSelectors) return RETVAL_DATA_ERROR; 349 if (selector >= nSelectors) return RETVAL_DATA_ERROR;
342 hufGroup=bd->groups+selectors[selector++]; 350 hufGroup = bd->groups + selectors[selector++];
343 base=hufGroup->base-1; 351 base = hufGroup->base - 1;
344 limit=hufGroup->limit-1; 352 limit = hufGroup->limit - 1;
345continue_this_group: 353 continue_this_group:
346 354
347 /* Read next Huffman-coded symbol. */ 355 /* Read next Huffman-coded symbol. */
348 356
@@ -353,33 +361,34 @@ continue_this_group:
353 valid compressed file. As a further optimization, we do the read 361 valid compressed file. As a further optimization, we do the read
354 inline (falling back to a call to get_bits if the buffer runs 362 inline (falling back to a call to get_bits if the buffer runs
355 dry). The following (up to got_huff_bits:) is equivalent to 363 dry). The following (up to got_huff_bits:) is equivalent to
356 j=get_bits(bd,hufGroup->maxLen); 364 j = get_bits(bd, hufGroup->maxLen);
357 */ 365 */
358 366
359 while (bd->inbufBitCount<hufGroup->maxLen) { 367 while (bd->inbufBitCount < hufGroup->maxLen) {
360 if(bd->inbufPos==bd->inbufCount) { 368 if (bd->inbufPos == bd->inbufCount) {
361 j = get_bits(bd,hufGroup->maxLen); 369 j = get_bits(bd, hufGroup->maxLen);
362 goto got_huff_bits; 370 goto got_huff_bits;
363 } 371 }
364 bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++]; 372 bd->inbufBits = (bd->inbufBits << 8) | bd->inbuf[bd->inbufPos++];
365 bd->inbufBitCount+=8; 373 bd->inbufBitCount += 8;
366 }; 374 };
367 bd->inbufBitCount-=hufGroup->maxLen; 375 bd->inbufBitCount -= hufGroup->maxLen;
368 j = (bd->inbufBits>>bd->inbufBitCount)&((1<<hufGroup->maxLen)-1); 376 j = (bd->inbufBits >> bd->inbufBitCount) & ((1 << hufGroup->maxLen) - 1);
369 377
370got_huff_bits: 378 got_huff_bits:
371 379
372 /* Figure how how many bits are in next symbol and unget extras */ 380 /* Figure how how many bits are in next symbol and unget extras */
373 381
374 i=hufGroup->minLen; 382 i = hufGroup->minLen;
375 while (j>limit[i]) ++i; 383 while (j > limit[i]) ++i;
376 bd->inbufBitCount += (hufGroup->maxLen - i); 384 bd->inbufBitCount += (hufGroup->maxLen - i);
377 385
378 /* Huffman decode value to get nextSym (with bounds checking) */ 386 /* Huffman decode value to get nextSym (with bounds checking) */
379 387
380 if ((i > hufGroup->maxLen) 388 if (i > hufGroup->maxLen)
381 || (((unsigned)(j=(j>>(hufGroup->maxLen-i))-base[i])) 389 return RETVAL_DATA_ERROR;
382 >= MAX_SYMBOLS)) 390 j = (j >> (hufGroup->maxLen - i)) - base[i];
391 if ((unsigned)j >= MAX_SYMBOLS)
383 return RETVAL_DATA_ERROR; 392 return RETVAL_DATA_ERROR;
384 nextSym = hufGroup->permute[j]; 393 nextSym = hufGroup->permute[j];
385 394
@@ -388,11 +397,11 @@ got_huff_bits:
388 check if nextSym indicates a repeated run, and if so loop collecting 397 check if nextSym indicates a repeated run, and if so loop collecting
389 how many times to repeat the last literal. */ 398 how many times to repeat the last literal. */
390 399
391 if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */ 400 if ((unsigned)nextSym <= SYMBOL_RUNB) { /* RUNA or RUNB */
392 401
393 /* If this is the start of a new run, zero out counter */ 402 /* If this is the start of a new run, zero out counter */
394 403
395 if(!runPos) { 404 if (!runPos) {
396 runPos = 1; 405 runPos = 1;
397 t = 0; 406 t = 0;
398 } 407 }
@@ -406,7 +415,7 @@ got_huff_bits:
406 context). Thus space is saved. */ 415 context). Thus space is saved. */
407 416
408 t += (runPos << nextSym); /* +runPos if RUNA; +2*runPos if RUNB */ 417 t += (runPos << nextSym); /* +runPos if RUNA; +2*runPos if RUNB */
409 if(runPos < dbufSize) runPos <<= 1; 418 if (runPos < dbufSize) runPos <<= 1;
410 goto end_of_huffman_loop; 419 goto end_of_huffman_loop;
411 } 420 }
412 421
@@ -415,18 +424,18 @@ got_huff_bits:
415 copies to our buffer of decoded symbols (dbuf) now. (The last 424 copies to our buffer of decoded symbols (dbuf) now. (The last
416 literal used is the one at the head of the mtfSymbol array.) */ 425 literal used is the one at the head of the mtfSymbol array.) */
417 426
418 if(runPos) { 427 if (runPos) {
419 runPos=0; 428 runPos = 0;
420 if(dbufCount+t>=dbufSize) return RETVAL_DATA_ERROR; 429 if (dbufCount + t >= dbufSize) return RETVAL_DATA_ERROR;
421 430
422 uc = symToByte[mtfSymbol[0]]; 431 uc = symToByte[mtfSymbol[0]];
423 byteCount[uc] += t; 432 byteCount[uc] += t;
424 while (t--) dbuf[dbufCount++]=uc; 433 while (t--) dbuf[dbufCount++] = uc;
425 } 434 }
426 435
427 /* Is this the terminating symbol? */ 436 /* Is this the terminating symbol? */
428 437
429 if(nextSym>symTotal) break; 438 if (nextSym > symTotal) break;
430 439
431 /* At this point, nextSym indicates a new literal character. Subtract 440 /* At this point, nextSym indicates a new literal character. Subtract
432 one to get the position in the MTF array at which this literal is 441 one to get the position in the MTF array at which this literal is
@@ -436,7 +445,7 @@ got_huff_bits:
436 as part of a run above. Therefore 1 unused mtf position minus 445 as part of a run above. Therefore 1 unused mtf position minus
437 2 non-literal nextSym values equals -1.) */ 446 2 non-literal nextSym values equals -1.) */
438 447
439 if(dbufCount>=dbufSize) return RETVAL_DATA_ERROR; 448 if (dbufCount >= dbufSize) return RETVAL_DATA_ERROR;
440 i = nextSym - 1; 449 i = nextSym - 1;
441 uc = mtfSymbol[i]; 450 uc = mtfSymbol[i];
442 451
@@ -449,18 +458,18 @@ got_huff_bits:
449 mtfSymbol[i] = mtfSymbol[i-1]; 458 mtfSymbol[i] = mtfSymbol[i-1];
450 } while (--i); 459 } while (--i);
451 mtfSymbol[0] = uc; 460 mtfSymbol[0] = uc;
452 uc=symToByte[uc]; 461 uc = symToByte[uc];
453 462
454 /* We have our literal byte. Save it into dbuf. */ 463 /* We have our literal byte. Save it into dbuf. */
455 464
456 byteCount[uc]++; 465 byteCount[uc]++;
457 dbuf[dbufCount++] = (unsigned int)uc; 466 dbuf[dbufCount++] = (unsigned)uc;
458 467
459 /* Skip group initialization if we're not done with this group. Done 468 /* Skip group initialization if we're not done with this group. Done
460 * this way to avoid compiler warning. */ 469 * this way to avoid compiler warning. */
461 470
462end_of_huffman_loop: 471 end_of_huffman_loop:
463 if(symCount--) goto continue_this_group; 472 if (symCount--) goto continue_this_group;
464 } 473 }
465 474
466 /* At this point, we've read all the Huffman-coded symbols (and repeated 475 /* At this point, we've read all the Huffman-coded symbols (and repeated
@@ -472,17 +481,17 @@ end_of_huffman_loop:
472 481
473 /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */ 482 /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
474 483
475 j=0; 484 j = 0;
476 for (i=0;i<256;i++) { 485 for (i = 0; i < 256; i++) {
477 k=j+byteCount[i]; 486 k = j + byteCount[i];
478 byteCount[i] = j; 487 byteCount[i] = j;
479 j=k; 488 j = k;
480 } 489 }
481 490
482 /* Figure out what order dbuf would be in if we sorted it. */ 491 /* Figure out what order dbuf would be in if we sorted it. */
483 492
484 for (i=0;i<dbufCount;i++) { 493 for (i = 0; i < dbufCount; i++) {
485 uc=(unsigned char)(dbuf[i] & 0xff); 494 uc = (unsigned char)(dbuf[i] & 0xff);
486 dbuf[byteCount[uc]] |= (i << 8); 495 dbuf[byteCount[uc]] |= (i << 8);
487 byteCount[uc]++; 496 byteCount[uc]++;
488 } 497 }
@@ -491,14 +500,14 @@ end_of_huffman_loop:
491 doesn't get output, and if the first three characters are identical 500 doesn't get output, and if the first three characters are identical
492 it doesn't qualify as a run (hence writeRunCountdown=5). */ 501 it doesn't qualify as a run (hence writeRunCountdown=5). */
493 502
494 if(dbufCount) { 503 if (dbufCount) {
495 if(origPtr>=dbufCount) return RETVAL_DATA_ERROR; 504 if (origPtr >= dbufCount) return RETVAL_DATA_ERROR;
496 bd->writePos=dbuf[origPtr]; 505 bd->writePos = dbuf[origPtr];
497 bd->writeCurrent=(unsigned char)(bd->writePos&0xff); 506 bd->writeCurrent = (unsigned char)(bd->writePos & 0xff);
498 bd->writePos>>=8; 507 bd->writePos >>= 8;
499 bd->writeRunCountdown=5; 508 bd->writeRunCountdown = 5;
500 } 509 }
501 bd->writeCount=dbufCount; 510 bd->writeCount = dbufCount;
502 511
503 return RETVAL_OK; 512 return RETVAL_OK;
504} 513}
@@ -512,16 +521,16 @@ end_of_huffman_loop:
512 521
513static int read_bunzip(bunzip_data *bd, char *outbuf, int len) 522static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
514{ 523{
515 const unsigned int *dbuf; 524 const unsigned *dbuf;
516 int pos,current,previous,gotcount; 525 int pos, current, previous, gotcount;
517 526
518 /* If last read was short due to end of file, return last block now */ 527 /* If last read was short due to end of file, return last block now */
519 if(bd->writeCount<0) return bd->writeCount; 528 if (bd->writeCount < 0) return bd->writeCount;
520 529
521 gotcount = 0; 530 gotcount = 0;
522 dbuf=bd->dbuf; 531 dbuf = bd->dbuf;
523 pos=bd->writePos; 532 pos = bd->writePos;
524 current=bd->writeCurrent; 533 current = bd->writeCurrent;
525 534
526 /* We will always have pending decoded data to write into the output 535 /* We will always have pending decoded data to write into the output
527 buffer unless this is the very first call (in which case we haven't 536 buffer unless this is the very first call (in which case we haven't
@@ -539,9 +548,9 @@ static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
539 548
540 /* If the output buffer is full, snapshot state and return */ 549 /* If the output buffer is full, snapshot state and return */
541 550
542 if(gotcount >= len) { 551 if (gotcount >= len) {
543 bd->writePos=pos; 552 bd->writePos =pos;
544 bd->writeCurrent=current; 553 bd->writeCurrent = current;
545 bd->writeCopies++; 554 bd->writeCopies++;
546 return len; 555 return len;
547 } 556 }
@@ -549,8 +558,8 @@ static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
549 /* Write next byte into output buffer, updating CRC */ 558 /* Write next byte into output buffer, updating CRC */
550 559
551 outbuf[gotcount++] = current; 560 outbuf[gotcount++] = current;
552 bd->writeCRC=(((bd->writeCRC)<<8) 561 bd->writeCRC = (bd->writeCRC << 8)
553 ^bd->crc32Table[((bd->writeCRC)>>24)^current]); 562 ^ bd->crc32Table[(bd->writeCRC >> 24) ^ current];
554 563
555 /* Loop now if we're outputting multiple copies of this byte */ 564 /* Loop now if we're outputting multiple copies of this byte */
556 565
@@ -558,31 +567,32 @@ static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
558 --bd->writeCopies; 567 --bd->writeCopies;
559 continue; 568 continue;
560 } 569 }
561decode_next_byte: 570 decode_next_byte:
562 if (!bd->writeCount--) break; 571 if (!bd->writeCount--) break;
563 /* Follow sequence vector to undo Burrows-Wheeler transform */ 572 /* Follow sequence vector to undo Burrows-Wheeler transform */
564 previous=current; 573 previous = current;
565 pos=dbuf[pos]; 574 pos = dbuf[pos];
566 current=pos&0xff; 575 current = pos & 0xff;
567 pos>>=8; 576 pos >>= 8;
568 577
569 /* After 3 consecutive copies of the same byte, the 4th is a repeat 578 /* After 3 consecutive copies of the same byte, the 4th is a repeat
570 count. We count down from 4 instead 579 count. We count down from 4 instead
571 * of counting up because testing for non-zero is faster */ 580 * of counting up because testing for non-zero is faster */
572 581
573 if(--bd->writeRunCountdown) { 582 if (--bd->writeRunCountdown) {
574 if(current!=previous) bd->writeRunCountdown=4; 583 if (current != previous)
584 bd->writeRunCountdown = 4;
575 } else { 585 } else {
576 586
577 /* We have a repeated run, this byte indicates the count */ 587 /* We have a repeated run, this byte indicates the count */
578 588
579 bd->writeCopies=current; 589 bd->writeCopies = current;
580 current=previous; 590 current = previous;
581 bd->writeRunCountdown=5; 591 bd->writeRunCountdown = 5;
582 592
583 /* Sometimes there are just 3 bytes (run length 0) */ 593 /* Sometimes there are just 3 bytes (run length 0) */
584 594
585 if(!bd->writeCopies) goto decode_next_byte; 595 if (!bd->writeCopies) goto decode_next_byte;
586 596
587 /* Subtract the 1 copy we'd output anyway to get extras */ 597 /* Subtract the 1 copy we'd output anyway to get extras */
588 598
@@ -592,13 +602,13 @@ decode_next_byte:
592 602
593 /* Decompression of this block completed successfully */ 603 /* Decompression of this block completed successfully */
594 604
595 bd->writeCRC=~bd->writeCRC; 605 bd->writeCRC = ~bd->writeCRC;
596 bd->totalCRC=((bd->totalCRC<<1) | (bd->totalCRC>>31)) ^ bd->writeCRC; 606 bd->totalCRC = ((bd->totalCRC << 1) | (bd->totalCRC >> 31)) ^ bd->writeCRC;
597 607
598 /* If this block had a CRC error, force file level CRC error. */ 608 /* If this block had a CRC error, force file level CRC error. */
599 609
600 if(bd->writeCRC!=bd->headerCRC) { 610 if (bd->writeCRC != bd->headerCRC) {
601 bd->totalCRC=bd->headerCRC+1; 611 bd->totalCRC = bd->headerCRC+1;
602 return RETVAL_LAST_BLOCK; 612 return RETVAL_LAST_BLOCK;
603 } 613 }
604 } 614 }
@@ -606,14 +616,14 @@ decode_next_byte:
606 /* Refill the intermediate buffer by Huffman-decoding next block of input */ 616 /* Refill the intermediate buffer by Huffman-decoding next block of input */
607 /* (previous is just a convenient unused temp variable here) */ 617 /* (previous is just a convenient unused temp variable here) */
608 618
609 previous=get_next_block(bd); 619 previous = get_next_block(bd);
610 if(previous) { 620 if (previous) {
611 bd->writeCount=previous; 621 bd->writeCount = previous;
612 return (previous!=RETVAL_LAST_BLOCK) ? previous : gotcount; 622 return (previous != RETVAL_LAST_BLOCK) ? previous : gotcount;
613 } 623 }
614 bd->writeCRC=~0; 624 bd->writeCRC = ~0;
615 pos=bd->writePos; 625 pos = bd->writePos;
616 current=bd->writeCurrent; 626 current = bd->writeCurrent;
617 goto decode_next_byte; 627 goto decode_next_byte;
618} 628}
619 629
@@ -625,25 +635,28 @@ static int start_bunzip(bunzip_data **bdp, int in_fd, unsigned char *inbuf,
625 int len) 635 int len)
626{ 636{
627 bunzip_data *bd; 637 bunzip_data *bd;
628 unsigned int i; 638 unsigned i;
629 const unsigned int BZh0=(((unsigned int)'B')<<24)+(((unsigned int)'Z')<<16) 639 enum {
630 +(((unsigned int)'h')<<8)+(unsigned int)'0'; 640 BZh0 = ('B' << 24) + ('Z' << 16) + ('h' << 8) + '0'
641 };
631 642
632 /* Figure out how much data to allocate */ 643 /* Figure out how much data to allocate */
633 644
634 i=sizeof(bunzip_data); 645 i = sizeof(bunzip_data);
635 if(in_fd!=-1) i+=IOBUF_SIZE; 646 if (in_fd != -1) i += IOBUF_SIZE;
636 647
637 /* Allocate bunzip_data. Most fields initialize to zero. */ 648 /* Allocate bunzip_data. Most fields initialize to zero. */
638 649
639 bd=*bdp=xzalloc(i); 650 bd = *bdp = xzalloc(i);
640 651
641 /* Setup input buffer */ 652 /* Setup input buffer */
642 653
643 if(-1==(bd->in_fd=in_fd)) { 654 bd->in_fd = in_fd;
644 bd->inbuf=inbuf; 655 if (-1 == in_fd) {
645 bd->inbufCount=len; 656 bd->inbuf = inbuf;
646 } else bd->inbuf=(unsigned char *)(bd+1); 657 bd->inbufCount = len;
658 } else
659 bd->inbuf = (unsigned char *)(bd + 1);
647 660
648 /* Init the CRC32 table (big endian) */ 661 /* Init the CRC32 table (big endian) */
649 662
@@ -651,20 +664,20 @@ static int start_bunzip(bunzip_data **bdp, int in_fd, unsigned char *inbuf,
651 664
652 /* Setup for I/O error handling via longjmp */ 665 /* Setup for I/O error handling via longjmp */
653 666
654 i=setjmp(bd->jmpbuf); 667 i = setjmp(bd->jmpbuf);
655 if(i) return i; 668 if (i) return i;
656 669
657 /* Ensure that file starts with "BZh['1'-'9']." */ 670 /* Ensure that file starts with "BZh['1'-'9']." */
658 671
659 i = get_bits(bd,32); 672 i = get_bits(bd, 32);
660 if (((unsigned int)(i-BZh0-1)) >= 9) return RETVAL_NOT_BZIP_DATA; 673 if (((unsigned)(i - BZh0 - 1)) >= 9) return RETVAL_NOT_BZIP_DATA;
661 674
662 /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of 675 /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
663 uncompressed data. Allocate intermediate buffer for block. */ 676 uncompressed data. Allocate intermediate buffer for block. */
664 677
665 bd->dbufSize=100000*(i-BZh0); 678 bd->dbufSize = 100000 * (i - BZh0);
666 679
667 bd->dbuf=xmalloc(bd->dbufSize * sizeof(int)); 680 bd->dbuf = xmalloc(bd->dbufSize * sizeof(int));
668 return RETVAL_OK; 681 return RETVAL_OK;
669} 682}
670 683
@@ -679,13 +692,14 @@ uncompressStream(int src_fd, int dst_fd)
679 bunzip_data *bd; 692 bunzip_data *bd;
680 int i; 693 int i;
681 694
682 outbuf=xmalloc(IOBUF_SIZE); 695 outbuf = xmalloc(IOBUF_SIZE);
683 i=start_bunzip(&bd,src_fd,0,0); 696 i = start_bunzip(&bd, src_fd, 0, 0);
684 if(!i) { 697 if (!i) {
685 for (;;) { 698 for (;;) {
686 if((i=read_bunzip(bd,outbuf,IOBUF_SIZE)) <= 0) break; 699 i = read_bunzip(bd, outbuf, IOBUF_SIZE);
687 if(i!=write(dst_fd,outbuf,i)) { 700 if (i <= 0) break;
688 i=RETVAL_UNEXPECTED_OUTPUT_EOF; 701 if (i != safe_write(dst_fd, outbuf, i)) {
702 i = RETVAL_UNEXPECTED_OUTPUT_EOF;
689 break; 703 break;
690 } 704 }
691 USE_DESKTOP(total_written += i;) 705 USE_DESKTOP(total_written += i;)
@@ -694,13 +708,13 @@ uncompressStream(int src_fd, int dst_fd)
694 708
695 /* Check CRC and release memory */ 709 /* Check CRC and release memory */
696 710
697 if(i==RETVAL_LAST_BLOCK) { 711 if (i == RETVAL_LAST_BLOCK) {
698 if (bd->headerCRC!=bd->totalCRC) { 712 if (bd->headerCRC != bd->totalCRC) {
699 bb_error_msg("data integrity error when decompressing"); 713 bb_error_msg("data integrity error when decompressing");
700 } else { 714 } else {
701 i=RETVAL_OK; 715 i = RETVAL_OK;
702 } 716 }
703 } else if (i==RETVAL_UNEXPECTED_OUTPUT_EOF) { 717 } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
704 bb_error_msg("compressed file ends unexpectedly"); 718 bb_error_msg("compressed file ends unexpectedly");
705 } else { 719 } else {
706 bb_error_msg("decompression failed"); 720 bb_error_msg("decompression failed");
@@ -714,18 +728,22 @@ uncompressStream(int src_fd, int dst_fd)
714 728
715#ifdef TESTING 729#ifdef TESTING
716 730
717static char * const bunzip_errors[]={NULL,"Bad file checksum","Not bzip data", 731static char *const bunzip_errors[] = {
718 "Unexpected input EOF","Unexpected output EOF","Data error", 732 NULL, "Bad file checksum", "Not bzip data",
719 "Out of memory","Obsolete (pre 0.9.5) bzip format not supported."}; 733 "Unexpected input EOF", "Unexpected output EOF", "Data error",
734 "Out of memory", "Obsolete (pre 0.9.5) bzip format not supported"
735};
720 736
721/* Dumb little test thing, decompress stdin to stdout */ 737/* Dumb little test thing, decompress stdin to stdout */
722int main(int argc, char **argv) 738int main(int argc, char **argv)
723{ 739{
724 int i=uncompressStream(0,1); 740 int i = uncompressStream(0, 1);
725 char c; 741 char c;
726 742
727 if(i<0) fprintf(stderr,"%s\n", bunzip_errors[-i]); 743 if (i < 0)
728 else if(read(0,&c,1)) fprintf(stderr,"Trailing garbage ignored\n"); 744 fprintf(stderr,"%s\n", bunzip_errors[-i]);
745 else if (read(0, &c, 1))
746 fprintf(stderr,"Trailing garbage ignored\n");
729 return -i; 747 return -i;
730} 748}
731#endif 749#endif