- /* Initialize symbol occurrence counters and symbol mtf table */
- memset(byteCount,0,256*sizeof(int));
- for(i=0;i<256;i++) mtfSymbol[i]=(unsigned char)i;
- /* Loop through compressed symbols */
- runPos=dbufCount=symCount=selector=0;
- for(;;) {
- /* Determine which huffman coding group to use. */
- if(!(symCount--)) {
- symCount=GROUP_SIZE-1;
- if(selector>=nSelectors) return RETVAL_DATA_ERROR;
- hufGroup=bd->groups+selectors[selector++];
- base=hufGroup->base-1;
- limit=hufGroup->limit-1;
- }
- /* Read next huffman-coded symbol */
- i = hufGroup->minLen;
- j=get_bits(bd, i);
- while (j > limit[i]) { /* The sentinal allows us to avoid testing i. */
- j = (j << 1) | GET_A_BIT(bd);
- ++i;
- }
- /* Huffman decode nextSym (with bounds checking) */
- if ((i > hufGroup->maxLen) || (((unsigned)(j-=base[i])) >= MAX_SYMBOLS)) return RETVAL_DATA_ERROR;
+ /* Initialize symbol occurrence counters and symbol Move To Front table */
+
+ for (i=0;i<256;i++) {
+ byteCount[i] = 0;
+ mtfSymbol[i]=(unsigned char)i;
+ }
+
+ /* Loop through compressed symbols. */
+
+ runPos=dbufCount=selector=0;
+ for (;;) {
+
+ /* fetch next Huffman coding group from list. */
+
+ symCount=GROUP_SIZE-1;
+ if(selector>=nSelectors) return RETVAL_DATA_ERROR;
+ hufGroup=bd->groups+selectors[selector++];
+ base=hufGroup->base-1;
+ limit=hufGroup->limit-1;
+continue_this_group:
+
+ /* Read next Huffman-coded symbol. */
+
+ /* Note: It is far cheaper to read maxLen bits and back up than it is
+ to read minLen bits and then an additional bit at a time, testing
+ as we go. Because there is a trailing last block (with file CRC),
+ there is no danger of the overread causing an unexpected EOF for a
+ valid compressed file. As a further optimization, we do the read
+ inline (falling back to a call to get_bits if the buffer runs
+ dry). The following (up to got_huff_bits:) is equivalent to
+ j=get_bits(bd,hufGroup->maxLen);
+ */
+
+ while (bd->inbufBitCount<hufGroup->maxLen) {
+ if(bd->inbufPos==bd->inbufCount) {
+ j = get_bits(bd,hufGroup->maxLen);
+ goto got_huff_bits;
+ }
+ bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++];
+ bd->inbufBitCount+=8;
+ };
+ bd->inbufBitCount-=hufGroup->maxLen;
+ j = (bd->inbufBits>>bd->inbufBitCount)&((1<<hufGroup->maxLen)-1);
+
+got_huff_bits:
+
+ /* Figure how how many bits are in next symbol and unget extras */
+
+ i=hufGroup->minLen;
+ while (j>limit[i]) ++i;
+ bd->inbufBitCount += (hufGroup->maxLen - i);
+
+ /* Huffman decode value to get nextSym (with bounds checking) */
+
+ if ((i > hufGroup->maxLen)
+ || (((unsigned)(j=(j>>(hufGroup->maxLen-i))-base[i]))
+ >= MAX_SYMBOLS))
+ return RETVAL_DATA_ERROR;