| 80 | |
| 81 | |
| 82 | _forceinline uint Unpack::DecodeNumber(BitInput &Inp,DecodeTable *Dec) |
| 83 | { |
| 84 | // Left aligned 15 bit length raw bit field. |
| 85 | uint BitField=Inp.getbits() & 0xfffe; |
| 86 | |
| 87 | if (BitField<Dec->DecodeLen[Dec->QuickBits]) |
| 88 | { |
| 89 | uint Code=BitField>>(16-Dec->QuickBits); |
| 90 | Inp.addbits(Dec->QuickLen[Code]); |
| 91 | return Dec->QuickNum[Code]; |
| 92 | } |
| 93 | |
| 94 | // Detect the real bit length for current code. |
| 95 | uint Bits=15; |
| 96 | for (uint I=Dec->QuickBits+1;I<15;I++) |
| 97 | if (BitField<Dec->DecodeLen[I]) |
| 98 | { |
| 99 | Bits=I; |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | Inp.addbits(Bits); |
| 104 | |
| 105 | // Calculate the distance from the start code for current bit length. |
| 106 | uint Dist=BitField-Dec->DecodeLen[Bits-1]; |
| 107 | |
| 108 | // Start codes are left aligned, but we need the normal right aligned |
| 109 | // number. So we shift the distance to the right. |
| 110 | Dist>>=(16-Bits); |
| 111 | |
| 112 | // Now we can calculate the position in the code list. It is the sum |
| 113 | // of first position for current bit length and right aligned distance |
| 114 | // between our bit field and start code for current bit length. |
| 115 | uint Pos=Dec->DecodePos[Bits]+Dist; |
| 116 | |
| 117 | // Out of bounds safety check required for damaged archives. |
| 118 | if (Pos>=Dec->MaxNum) |
| 119 | Pos=0; |
| 120 | |
| 121 | // Convert the position in the code list to position in alphabet |
| 122 | // and return it. |
| 123 | return Dec->DecodeNum[Pos]; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | _forceinline uint Unpack::SlotToLength(BitInput &Inp,uint Slot) |