| 204 | |
| 205 | //*************************************************************** |
| 206 | int CHuffman::Decompress(const void *pInput, int InputSize, void *pOutput, int OutputSize) const |
| 207 | { |
| 208 | // setup buffer pointers |
| 209 | unsigned char *pDst = (unsigned char *)pOutput; |
| 210 | unsigned char *pSrc = (unsigned char *)pInput; |
| 211 | unsigned char *pDstEnd = pDst + OutputSize; |
| 212 | unsigned char *pSrcEnd = pSrc + InputSize; |
| 213 | |
| 214 | unsigned Bits = 0; |
| 215 | unsigned Bitcount = 0; |
| 216 | |
| 217 | const CNode *pEof = &m_aNodes[HUFFMAN_EOF_SYMBOL]; |
| 218 | |
| 219 | while(true) |
| 220 | { |
| 221 | // {A} try to load a node now, this will reduce dependency at location {D} |
| 222 | const CNode *pNode = nullptr; |
| 223 | if(Bitcount >= HUFFMAN_LUTBITS) |
| 224 | pNode = m_apDecodeLut[Bits & HUFFMAN_LUTMASK]; |
| 225 | |
| 226 | // {B} fill with new bits |
| 227 | while(Bitcount < 24 && pSrc != pSrcEnd) |
| 228 | { |
| 229 | Bits |= (*pSrc++) << Bitcount; |
| 230 | Bitcount += 8; |
| 231 | } |
| 232 | |
| 233 | // {C} load symbol now if we didn't that earlier at location {A} |
| 234 | if(!pNode) |
| 235 | pNode = m_apDecodeLut[Bits & HUFFMAN_LUTMASK]; |
| 236 | |
| 237 | if(!pNode) |
| 238 | return -1; |
| 239 | |
| 240 | // {D} check if we hit a symbol already |
| 241 | if(pNode->m_NumBits) |
| 242 | { |
| 243 | // remove the bits for that symbol |
| 244 | Bits >>= pNode->m_NumBits; |
| 245 | Bitcount -= pNode->m_NumBits; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | // remove the bits that the lut checked up for us |
| 250 | Bits >>= HUFFMAN_LUTBITS; |
| 251 | Bitcount -= HUFFMAN_LUTBITS; |
| 252 | |
| 253 | // walk the tree bit by bit |
| 254 | while(true) |
| 255 | { |
| 256 | // traverse tree |
| 257 | pNode = &m_aNodes[pNode->m_aLeaves[Bits & 1]]; |
| 258 | |
| 259 | // remove bit |
| 260 | Bitcount--; |
| 261 | Bits >>= 1; |
| 262 | |
| 263 | // check if we hit a symbol |