diff options
author | Rob Landley <rob@landley.net> | 2006-02-17 05:12:03 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-02-17 05:12:03 +0000 |
commit | 2c98c40ec881dcaac93b069525314bc078359175 (patch) | |
tree | 9e28a807c14d2b39b13718d44b9bdd5b67c32afb | |
parent | f856eabcde32f015691c0108d7ad62584e9fafdb (diff) | |
download | busybox-w32-2c98c40ec881dcaac93b069525314bc078359175.tar.gz busybox-w32-2c98c40ec881dcaac93b069525314bc078359175.tar.bz2 busybox-w32-2c98c40ec881dcaac93b069525314bc078359175.zip |
The tendency of vi to auto-indent can be really annoying at times.
-rw-r--r-- | archival/libunarchive/decompress_bunzip2.c | 150 |
1 files changed, 75 insertions, 75 deletions
diff --git a/archival/libunarchive/decompress_bunzip2.c b/archival/libunarchive/decompress_bunzip2.c index 8694a32cd..34afd6f99 100644 --- a/archival/libunarchive/decompress_bunzip2.c +++ b/archival/libunarchive/decompress_bunzip2.c | |||
@@ -71,7 +71,7 @@ struct group_data { | |||
71 | 71 | ||
72 | typedef struct { | 72 | typedef struct { |
73 | /* State for interrupting output loop */ | 73 | /* State for interrupting output loop */ |
74 | 74 | ||
75 | int writeCopies,writePos,writeRunCountdown,writeCount,writeCurrent; | 75 | int writeCopies,writePos,writeRunCountdown,writeCount,writeCurrent; |
76 | 76 | ||
77 | /* I/O tracking data (file handles, buffers, positions, etc.) */ | 77 | /* I/O tracking data (file handles, buffers, positions, etc.) */ |
@@ -154,34 +154,34 @@ static int get_next_block(bunzip_data *bd) | |||
154 | dbuf=bd->dbuf; | 154 | dbuf=bd->dbuf; |
155 | dbufSize=bd->dbufSize; | 155 | dbufSize=bd->dbufSize; |
156 | selectors=bd->selectors; | 156 | selectors=bd->selectors; |
157 | 157 | ||
158 | /* Reset longjmp I/O error handling */ | 158 | /* Reset longjmp I/O error handling */ |
159 | 159 | ||
160 | i=setjmp(bd->jmpbuf); | 160 | i=setjmp(bd->jmpbuf); |
161 | if(i) return i; | 161 | if(i) return i; |
162 | 162 | ||
163 | /* Read in header signature and CRC, then validate signature. | 163 | /* Read in header signature and CRC, then validate signature. |
164 | (last block signature means CRC is for whole file, return now) */ | 164 | (last block signature means CRC is for whole file, return now) */ |
165 | 165 | ||
166 | i = get_bits(bd,24); | 166 | i = get_bits(bd,24); |
167 | j = get_bits(bd,24); | 167 | j = get_bits(bd,24); |
168 | bd->headerCRC=get_bits(bd,32); | 168 | bd->headerCRC=get_bits(bd,32); |
169 | if ((i == 0x177245) && (j == 0x385090)) return RETVAL_LAST_BLOCK; | 169 | if ((i == 0x177245) && (j == 0x385090)) return RETVAL_LAST_BLOCK; |
170 | if ((i != 0x314159) || (j != 0x265359)) return RETVAL_NOT_BZIP_DATA; | 170 | if ((i != 0x314159) || (j != 0x265359)) return RETVAL_NOT_BZIP_DATA; |
171 | 171 | ||
172 | /* We can add support for blockRandomised if anybody complains. There was | 172 | /* We can add support for blockRandomised if anybody complains. There was |
173 | some code for this in busybox 1.0.0-pre3, but nobody ever noticed that | 173 | some code for this in busybox 1.0.0-pre3, but nobody ever noticed that |
174 | it didn't actually work. */ | 174 | it didn't actually work. */ |
175 | 175 | ||
176 | if(get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT; | 176 | if(get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT; |
177 | if((origPtr=get_bits(bd,24)) > dbufSize) return RETVAL_DATA_ERROR; | 177 | if((origPtr=get_bits(bd,24)) > dbufSize) return RETVAL_DATA_ERROR; |
178 | 178 | ||
179 | /* mapping table: if some byte values are never used (encoding things | 179 | /* mapping table: if some byte values are never used (encoding things |
180 | like ascii text), the compression code removes the gaps to have fewer | 180 | like ascii text), the compression code removes the gaps to have fewer |
181 | symbols to deal with, and writes a sparse bitfield indicating which | 181 | symbols to deal with, and writes a sparse bitfield indicating which |
182 | values were present. We make a translation table to convert the symbols | 182 | values were present. We make a translation table to convert the symbols |
183 | back to the corresponding bytes. */ | 183 | back to the corresponding bytes. */ |
184 | 184 | ||
185 | t=get_bits(bd, 16); | 185 | t=get_bits(bd, 16); |
186 | symTotal=0; | 186 | symTotal=0; |
187 | for (i=0;i<16;i++) { | 187 | for (i=0;i<16;i++) { |
@@ -191,81 +191,81 @@ static int get_next_block(bunzip_data *bd) | |||
191 | if(k&(1<<(15-j))) symToByte[symTotal++]=(16*i)+j; | 191 | if(k&(1<<(15-j))) symToByte[symTotal++]=(16*i)+j; |
192 | } | 192 | } |
193 | } | 193 | } |
194 | 194 | ||
195 | /* How many different Huffman coding groups does this block use? */ | 195 | /* How many different Huffman coding groups does this block use? */ |
196 | 196 | ||
197 | groupCount=get_bits(bd,3); | 197 | groupCount=get_bits(bd,3); |
198 | if (groupCount<2 || groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR; | 198 | if (groupCount<2 || groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR; |
199 | 199 | ||
200 | /* nSelectors: Every GROUP_SIZE many symbols we select a new Huffman coding | 200 | /* nSelectors: Every GROUP_SIZE many symbols we select a new Huffman coding |
201 | group. Read in the group selector list, which is stored as MTF encoded | 201 | group. Read in the group selector list, which is stored as MTF encoded |
202 | bit runs. (MTF=Move To Front, as each value is used it's moved to the | 202 | bit runs. (MTF=Move To Front, as each value is used it's moved to the |
203 | start of the list.) */ | 203 | start of the list.) */ |
204 | 204 | ||
205 | if(!(nSelectors=get_bits(bd, 15))) return RETVAL_DATA_ERROR; | 205 | if(!(nSelectors=get_bits(bd, 15))) return RETVAL_DATA_ERROR; |
206 | for(i=0; i<groupCount; i++) mtfSymbol[i] = i; | 206 | for(i=0; i<groupCount; i++) mtfSymbol[i] = i; |
207 | for(i=0; i<nSelectors; i++) { | 207 | for(i=0; i<nSelectors; i++) { |
208 | 208 | ||
209 | /* Get next value */ | 209 | /* Get next value */ |
210 | 210 | ||
211 | for(j=0;get_bits(bd,1);j++) if (j>=groupCount) return RETVAL_DATA_ERROR; | 211 | for(j=0;get_bits(bd,1);j++) if (j>=groupCount) return RETVAL_DATA_ERROR; |
212 | 212 | ||
213 | /* Decode MTF to get the next selector */ | 213 | /* Decode MTF to get the next selector */ |
214 | 214 | ||
215 | uc = mtfSymbol[j]; | 215 | uc = mtfSymbol[j]; |
216 | for(;j;j--) mtfSymbol[j] = mtfSymbol[j-1]; | 216 | for(;j;j--) mtfSymbol[j] = mtfSymbol[j-1]; |
217 | mtfSymbol[0]=selectors[i]=uc; | 217 | mtfSymbol[0]=selectors[i]=uc; |
218 | } | 218 | } |
219 | 219 | ||
220 | /* Read the Huffman coding tables for each group, which code for symTotal | 220 | /* Read the Huffman coding tables for each group, which code for symTotal |
221 | literal symbols, plus two run symbols (RUNA, RUNB) */ | 221 | literal symbols, plus two run symbols (RUNA, RUNB) */ |
222 | 222 | ||
223 | symCount=symTotal+2; | 223 | symCount=symTotal+2; |
224 | for (j=0; j<groupCount; j++) { | 224 | for (j=0; j<groupCount; j++) { |
225 | unsigned char length[MAX_SYMBOLS],temp[MAX_HUFCODE_BITS+1]; | 225 | unsigned char length[MAX_SYMBOLS],temp[MAX_HUFCODE_BITS+1]; |
226 | int minLen, maxLen, pp; | 226 | int minLen, maxLen, pp; |
227 | 227 | ||
228 | /* Read Huffman code lengths for each symbol. They're stored in | 228 | /* Read Huffman code lengths for each symbol. They're stored in |
229 | a way similar to mtf; record a starting value for the first symbol, | 229 | a way similar to mtf; record a starting value for the first symbol, |
230 | and an offset from the previous value for everys symbol after that. | 230 | and an offset from the previous value for everys symbol after that. |
231 | (Subtracting 1 before the loop and then adding it back at the end is | 231 | (Subtracting 1 before the loop and then adding it back at the end is |
232 | an optimization that makes the test inside the loop simpler: symbol | 232 | an optimization that makes the test inside the loop simpler: symbol |
233 | length 0 becomes negative, so an unsigned inequality catches it.) */ | 233 | length 0 becomes negative, so an unsigned inequality catches it.) */ |
234 | 234 | ||
235 | t=get_bits(bd, 5)-1; | 235 | t=get_bits(bd, 5)-1; |
236 | for (i = 0; i < symCount; i++) { | 236 | for (i = 0; i < symCount; i++) { |
237 | for(;;) { | 237 | for(;;) { |
238 | if (((unsigned)t) > (MAX_HUFCODE_BITS-1)) | 238 | if (((unsigned)t) > (MAX_HUFCODE_BITS-1)) |
239 | return RETVAL_DATA_ERROR; | 239 | return RETVAL_DATA_ERROR; |
240 | 240 | ||
241 | /* If first bit is 0, stop. Else second bit indicates whether | 241 | /* If first bit is 0, stop. Else second bit indicates whether |
242 | to increment or decrement the value. Optimization: grab 2 | 242 | to increment or decrement the value. Optimization: grab 2 |
243 | bits and unget the second if the first was 0. */ | 243 | bits and unget the second if the first was 0. */ |
244 | 244 | ||
245 | k = get_bits(bd,2); | 245 | k = get_bits(bd,2); |
246 | if (k < 2) { | 246 | if (k < 2) { |
247 | bd->inbufBitCount++; | 247 | bd->inbufBitCount++; |
248 | break; | 248 | break; |
249 | } | 249 | } |
250 | 250 | ||
251 | /* Add one if second bit 1, else subtract 1. Avoids if/else */ | 251 | /* Add one if second bit 1, else subtract 1. Avoids if/else */ |
252 | 252 | ||
253 | t+=(((k+1)&2)-1); | 253 | t+=(((k+1)&2)-1); |
254 | } | 254 | } |
255 | 255 | ||
256 | /* Correct for the initial -1, to get the final symbol length */ | 256 | /* Correct for the initial -1, to get the final symbol length */ |
257 | 257 | ||
258 | length[i]=t+1; | 258 | length[i]=t+1; |
259 | } | 259 | } |
260 | 260 | ||
261 | /* Find largest and smallest lengths in this group */ | 261 | /* Find largest and smallest lengths in this group */ |
262 | 262 | ||
263 | minLen=maxLen=length[0]; | 263 | minLen=maxLen=length[0]; |
264 | for(i = 1; i < symCount; i++) { | 264 | for(i = 1; i < symCount; i++) { |
265 | if(length[i] > maxLen) maxLen = length[i]; | 265 | if(length[i] > maxLen) maxLen = length[i]; |
266 | else if(length[i] < minLen) minLen = length[i]; | 266 | else if(length[i] < minLen) minLen = length[i]; |
267 | } | 267 | } |
268 | 268 | ||
269 | /* Calculate permute[], base[], and limit[] tables from length[]. | 269 | /* Calculate permute[], base[], and limit[] tables from length[]. |
270 | * | 270 | * |
271 | * permute[] is the lookup table for converting Huffman coded symbols | 271 | * permute[] is the lookup table for converting Huffman coded symbols |
@@ -276,47 +276,47 @@ static int get_next_block(bunzip_data *bd) | |||
276 | * number of bits can have. This is how the Huffman codes can vary in | 276 | * number of bits can have. This is how the Huffman codes can vary in |
277 | * length: each code with a value>limit[length] needs another bit. | 277 | * length: each code with a value>limit[length] needs another bit. |
278 | */ | 278 | */ |
279 | 279 | ||
280 | hufGroup=bd->groups+j; | 280 | hufGroup=bd->groups+j; |
281 | hufGroup->minLen = minLen; | 281 | hufGroup->minLen = minLen; |
282 | hufGroup->maxLen = maxLen; | 282 | hufGroup->maxLen = maxLen; |
283 | 283 | ||
284 | /* Note that minLen can't be smaller than 1, so we adjust the base | 284 | /* Note that minLen can't be smaller than 1, so we adjust the base |
285 | and limit array pointers so we're not always wasting the first | 285 | and limit array pointers so we're not always wasting the first |
286 | entry. We do this again when using them (during symbol decoding).*/ | 286 | entry. We do this again when using them (during symbol decoding).*/ |
287 | 287 | ||
288 | base=hufGroup->base-1; | 288 | base=hufGroup->base-1; |
289 | limit=hufGroup->limit-1; | 289 | limit=hufGroup->limit-1; |
290 | 290 | ||
291 | /* Calculate permute[]. Concurently, initialize temp[] and limit[]. */ | 291 | /* Calculate permute[]. Concurently, initialize temp[] and limit[]. */ |
292 | 292 | ||
293 | pp=0; | 293 | pp=0; |
294 | for(i=minLen;i<=maxLen;i++) { | 294 | for(i=minLen;i<=maxLen;i++) { |
295 | temp[i]=limit[i]=0; | 295 | temp[i]=limit[i]=0; |
296 | for(t=0;t<symCount;t++) | 296 | for(t=0;t<symCount;t++) |
297 | if(length[t]==i) hufGroup->permute[pp++] = t; | 297 | if(length[t]==i) hufGroup->permute[pp++] = t; |
298 | } | 298 | } |
299 | 299 | ||
300 | /* Count symbols coded for at each bit length */ | 300 | /* Count symbols coded for at each bit length */ |
301 | 301 | ||
302 | for (i=0;i<symCount;i++) temp[length[i]]++; | 302 | for (i=0;i<symCount;i++) temp[length[i]]++; |
303 | 303 | ||
304 | /* Calculate limit[] (the largest symbol-coding value at each bit | 304 | /* Calculate limit[] (the largest symbol-coding value at each bit |
305 | * length, which is (previous limit<<1)+symbols at this level), and | 305 | * length, which is (previous limit<<1)+symbols at this level), and |
306 | * 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 |
307 | * limit minus the cumulative count of symbols coded for already). */ | 307 | * limit minus the cumulative count of symbols coded for already). */ |
308 | 308 | ||
309 | pp=t=0; | 309 | pp=t=0; |
310 | for (i=minLen; i<maxLen; i++) { | 310 | for (i=minLen; i<maxLen; i++) { |
311 | pp+=temp[i]; | 311 | pp+=temp[i]; |
312 | 312 | ||
313 | /* We read the largest possible symbol size and then unget bits | 313 | /* We read the largest possible symbol size and then unget bits |
314 | after determining how many we need, and those extra bits could | 314 | after determining how many we need, and those extra bits could |
315 | be set to anything. (They're noise from future symbols.) At | 315 | be set to anything. (They're noise from future symbols.) At |
316 | each level we're really only interested in the first few bits, | 316 | each level we're really only interested in the first few bits, |
317 | 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 |
318 | don't affect the value>limit[length] comparison. */ | 318 | don't affect the value>limit[length] comparison. */ |
319 | 319 | ||
320 | limit[i]= (pp << (maxLen - i)) - 1; | 320 | limit[i]= (pp << (maxLen - i)) - 1; |
321 | pp<<=1; | 321 | pp<<=1; |
322 | base[i+1]=pp-(t+=temp[i]); | 322 | base[i+1]=pp-(t+=temp[i]); |
@@ -325,34 +325,34 @@ static int get_next_block(bunzip_data *bd) | |||
325 | limit[maxLen]=pp+temp[maxLen]-1; | 325 | limit[maxLen]=pp+temp[maxLen]-1; |
326 | base[minLen]=0; | 326 | base[minLen]=0; |
327 | } | 327 | } |
328 | 328 | ||
329 | /* We've finished reading and digesting the block header. Now read this | 329 | /* We've finished reading and digesting the block header. Now read this |
330 | block's Huffman coded symbols from the file and undo the Huffman coding | 330 | block's Huffman coded symbols from the file and undo the Huffman coding |
331 | and run length encoding, saving the result into dbuf[dbufCount++]=uc */ | 331 | and run length encoding, saving the result into dbuf[dbufCount++]=uc */ |
332 | 332 | ||
333 | /* Initialize symbol occurrence counters and symbol Move To Front table */ | 333 | /* Initialize symbol occurrence counters and symbol Move To Front table */ |
334 | 334 | ||
335 | for(i=0;i<256;i++) { | 335 | for(i=0;i<256;i++) { |
336 | byteCount[i] = 0; | 336 | byteCount[i] = 0; |
337 | mtfSymbol[i]=(unsigned char)i; | 337 | mtfSymbol[i]=(unsigned char)i; |
338 | } | 338 | } |
339 | 339 | ||
340 | /* Loop through compressed symbols. */ | 340 | /* Loop through compressed symbols. */ |
341 | 341 | ||
342 | runPos=dbufCount=selector=0; | 342 | runPos=dbufCount=selector=0; |
343 | for(;;) { | 343 | for(;;) { |
344 | 344 | ||
345 | /* fetch next Huffman coding group from list. */ | 345 | /* fetch next Huffman coding group from list. */ |
346 | 346 | ||
347 | symCount=GROUP_SIZE-1; | 347 | symCount=GROUP_SIZE-1; |
348 | if(selector>=nSelectors) return RETVAL_DATA_ERROR; | 348 | if(selector>=nSelectors) return RETVAL_DATA_ERROR; |
349 | hufGroup=bd->groups+selectors[selector++]; | 349 | hufGroup=bd->groups+selectors[selector++]; |
350 | base=hufGroup->base-1; | 350 | base=hufGroup->base-1; |
351 | limit=hufGroup->limit-1; | 351 | limit=hufGroup->limit-1; |
352 | continue_this_group: | 352 | continue_this_group: |
353 | 353 | ||
354 | /* Read next Huffman-coded symbol. */ | 354 | /* Read next Huffman-coded symbol. */ |
355 | 355 | ||
356 | /* Note: It is far cheaper to read maxLen bits and back up than it is | 356 | /* Note: It is far cheaper to read maxLen bits and back up than it is |
357 | to read minLen bits and then an additional bit at a time, testing | 357 | to read minLen bits and then an additional bit at a time, testing |
358 | as we go. Because there is a trailing last block (with file CRC), | 358 | as we go. Because there is a trailing last block (with file CRC), |
@@ -362,7 +362,7 @@ continue_this_group: | |||
362 | dry). The following (up to got_huff_bits:) is equivalent to | 362 | dry). The following (up to got_huff_bits:) is equivalent to |
363 | j=get_bits(bd,hufGroup->maxLen); | 363 | j=get_bits(bd,hufGroup->maxLen); |
364 | */ | 364 | */ |
365 | 365 | ||
366 | while (bd->inbufBitCount<hufGroup->maxLen) { | 366 | while (bd->inbufBitCount<hufGroup->maxLen) { |
367 | if(bd->inbufPos==bd->inbufCount) { | 367 | if(bd->inbufPos==bd->inbufCount) { |
368 | j = get_bits(bd,hufGroup->maxLen); | 368 | j = get_bits(bd,hufGroup->maxLen); |
@@ -373,37 +373,37 @@ continue_this_group: | |||
373 | }; | 373 | }; |
374 | bd->inbufBitCount-=hufGroup->maxLen; | 374 | bd->inbufBitCount-=hufGroup->maxLen; |
375 | j = (bd->inbufBits>>bd->inbufBitCount)&((1<<hufGroup->maxLen)-1); | 375 | j = (bd->inbufBits>>bd->inbufBitCount)&((1<<hufGroup->maxLen)-1); |
376 | 376 | ||
377 | got_huff_bits: | 377 | got_huff_bits: |
378 | 378 | ||
379 | /* Figure how how many bits are in next symbol and unget extras */ | 379 | /* Figure how how many bits are in next symbol and unget extras */ |
380 | 380 | ||
381 | i=hufGroup->minLen; | 381 | i=hufGroup->minLen; |
382 | while(j>limit[i]) ++i; | 382 | while(j>limit[i]) ++i; |
383 | bd->inbufBitCount += (hufGroup->maxLen - i); | 383 | bd->inbufBitCount += (hufGroup->maxLen - i); |
384 | 384 | ||
385 | /* Huffman decode value to get nextSym (with bounds checking) */ | 385 | /* Huffman decode value to get nextSym (with bounds checking) */ |
386 | 386 | ||
387 | if ((i > hufGroup->maxLen) | 387 | if ((i > hufGroup->maxLen) |
388 | || (((unsigned)(j=(j>>(hufGroup->maxLen-i))-base[i])) | 388 | || (((unsigned)(j=(j>>(hufGroup->maxLen-i))-base[i])) |
389 | >= MAX_SYMBOLS)) | 389 | >= MAX_SYMBOLS)) |
390 | return RETVAL_DATA_ERROR; | 390 | return RETVAL_DATA_ERROR; |
391 | nextSym = hufGroup->permute[j]; | 391 | nextSym = hufGroup->permute[j]; |
392 | 392 | ||
393 | /* We have now decoded the symbol, which indicates either a new literal | 393 | /* We have now decoded the symbol, which indicates either a new literal |
394 | byte, or a repeated run of the most recent literal byte. First, | 394 | byte, or a repeated run of the most recent literal byte. First, |
395 | check if nextSym indicates a repeated run, and if so loop collecting | 395 | check if nextSym indicates a repeated run, and if so loop collecting |
396 | how many times to repeat the last literal. */ | 396 | how many times to repeat the last literal. */ |
397 | 397 | ||
398 | if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */ | 398 | if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */ |
399 | 399 | ||
400 | /* If this is the start of a new run, zero out counter */ | 400 | /* If this is the start of a new run, zero out counter */ |
401 | 401 | ||
402 | if(!runPos) { | 402 | if(!runPos) { |
403 | runPos = 1; | 403 | runPos = 1; |
404 | t = 0; | 404 | t = 0; |
405 | } | 405 | } |
406 | 406 | ||
407 | /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at | 407 | /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at |
408 | each bit position, add 1 or 2 instead. For example, | 408 | each bit position, add 1 or 2 instead. For example, |
409 | 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2. | 409 | 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2. |
@@ -411,17 +411,17 @@ got_huff_bits: | |||
411 | the basic or 0/1 method (except all bits 0, which would use no | 411 | the basic or 0/1 method (except all bits 0, which would use no |
412 | symbols, but a run of length 0 doesn't mean anything in this | 412 | symbols, but a run of length 0 doesn't mean anything in this |
413 | context). Thus space is saved. */ | 413 | context). Thus space is saved. */ |
414 | 414 | ||
415 | t += (runPos << nextSym); /* +runPos if RUNA; +2*runPos if RUNB */ | 415 | t += (runPos << nextSym); /* +runPos if RUNA; +2*runPos if RUNB */ |
416 | runPos <<= 1; | 416 | runPos <<= 1; |
417 | goto end_of_huffman_loop; | 417 | goto end_of_huffman_loop; |
418 | } | 418 | } |
419 | 419 | ||
420 | /* When we hit the first non-run symbol after a run, we now know | 420 | /* When we hit the first non-run symbol after a run, we now know |
421 | how many times to repeat the last literal, so append that many | 421 | how many times to repeat the last literal, so append that many |
422 | copies to our buffer of decoded symbols (dbuf) now. (The last | 422 | copies to our buffer of decoded symbols (dbuf) now. (The last |
423 | literal used is the one at the head of the mtfSymbol array.) */ | 423 | literal used is the one at the head of the mtfSymbol array.) */ |
424 | 424 | ||
425 | if(runPos) { | 425 | if(runPos) { |
426 | runPos=0; | 426 | runPos=0; |
427 | if(dbufCount+t>=dbufSize) return RETVAL_DATA_ERROR; | 427 | if(dbufCount+t>=dbufSize) return RETVAL_DATA_ERROR; |
@@ -430,11 +430,11 @@ got_huff_bits: | |||
430 | byteCount[uc] += t; | 430 | byteCount[uc] += t; |
431 | while(t--) dbuf[dbufCount++]=uc; | 431 | while(t--) dbuf[dbufCount++]=uc; |
432 | } | 432 | } |
433 | 433 | ||
434 | /* Is this the terminating symbol? */ | 434 | /* Is this the terminating symbol? */ |
435 | 435 | ||
436 | if(nextSym>symTotal) break; | 436 | if(nextSym>symTotal) break; |
437 | 437 | ||
438 | /* At this point, nextSym indicates a new literal character. Subtract | 438 | /* At this point, nextSym indicates a new literal character. Subtract |
439 | one to get the position in the MTF array at which this literal is | 439 | one to get the position in the MTF array at which this literal is |
440 | currently to be found. (Note that the result can't be -1 or 0, | 440 | currently to be found. (Note that the result can't be -1 or 0, |
@@ -442,30 +442,30 @@ got_huff_bits: | |||
442 | first symbol in the mtf array, position 0, would have been handled | 442 | first symbol in the mtf array, position 0, would have been handled |
443 | as part of a run above. Therefore 1 unused mtf position minus | 443 | as part of a run above. Therefore 1 unused mtf position minus |
444 | 2 non-literal nextSym values equals -1.) */ | 444 | 2 non-literal nextSym values equals -1.) */ |
445 | 445 | ||
446 | if(dbufCount>=dbufSize) return RETVAL_DATA_ERROR; | 446 | if(dbufCount>=dbufSize) return RETVAL_DATA_ERROR; |
447 | i = nextSym - 1; | 447 | i = nextSym - 1; |
448 | uc = mtfSymbol[i]; | 448 | uc = mtfSymbol[i]; |
449 | 449 | ||
450 | /* Adjust the MTF array. Since we typically expect to move only a | 450 | /* Adjust the MTF array. Since we typically expect to move only a |
451 | * small number of symbols, and are bound by 256 in any case, using | 451 | * small number of symbols, and are bound by 256 in any case, using |
452 | * memmove here would typically be bigger and slower due to function | 452 | * memmove here would typically be bigger and slower due to function |
453 | * call overhead and other assorted setup costs. */ | 453 | * call overhead and other assorted setup costs. */ |
454 | 454 | ||
455 | do { | 455 | do { |
456 | mtfSymbol[i] = mtfSymbol[i-1]; | 456 | mtfSymbol[i] = mtfSymbol[i-1]; |
457 | } while (--i); | 457 | } while (--i); |
458 | mtfSymbol[0] = uc; | 458 | mtfSymbol[0] = uc; |
459 | uc=symToByte[uc]; | 459 | uc=symToByte[uc]; |
460 | 460 | ||
461 | /* We have our literal byte. Save it into dbuf. */ | 461 | /* We have our literal byte. Save it into dbuf. */ |
462 | 462 | ||
463 | byteCount[uc]++; | 463 | byteCount[uc]++; |
464 | dbuf[dbufCount++] = (unsigned int)uc; | 464 | dbuf[dbufCount++] = (unsigned int)uc; |
465 | 465 | ||
466 | /* Skip group initialization if we're not done with this group. Done | 466 | /* Skip group initialization if we're not done with this group. Done |
467 | * this way to avoid compiler warning. */ | 467 | * this way to avoid compiler warning. */ |
468 | 468 | ||
469 | end_of_huffman_loop: | 469 | end_of_huffman_loop: |
470 | if(symCount--) goto continue_this_group; | 470 | if(symCount--) goto continue_this_group; |
471 | } | 471 | } |
@@ -476,7 +476,7 @@ end_of_huffman_loop: | |||
476 | Now undo the Burrows-Wheeler transform on dbuf. | 476 | Now undo the Burrows-Wheeler transform on dbuf. |
477 | See http://dogma.net/markn/articles/bwt/bwt.htm | 477 | See http://dogma.net/markn/articles/bwt/bwt.htm |
478 | */ | 478 | */ |
479 | 479 | ||
480 | /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */ | 480 | /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */ |
481 | 481 | ||
482 | j=0; | 482 | j=0; |
@@ -497,7 +497,7 @@ end_of_huffman_loop: | |||
497 | /* Decode first byte by hand to initialize "previous" byte. Note that it | 497 | /* Decode first byte by hand to initialize "previous" byte. Note that it |
498 | doesn't get output, and if the first three characters are identical | 498 | doesn't get output, and if the first three characters are identical |
499 | it doesn't qualify as a run (hence writeRunCountdown=5). */ | 499 | it doesn't qualify as a run (hence writeRunCountdown=5). */ |
500 | 500 | ||
501 | if(dbufCount) { | 501 | if(dbufCount) { |
502 | if(origPtr>=dbufCount) return RETVAL_DATA_ERROR; | 502 | if(origPtr>=dbufCount) return RETVAL_DATA_ERROR; |
503 | bd->writePos=dbuf[origPtr]; | 503 | bd->writePos=dbuf[origPtr]; |