| 371 | } |
| 372 | |
| 373 | void Inflator::DecodeHeader() |
| 374 | { |
| 375 | if (!m_reader.FillBuffer(3)) |
| 376 | throw UnexpectedEndErr(); |
| 377 | m_eof = m_reader.GetBits(1) != 0; |
| 378 | m_blockType = (byte)m_reader.GetBits(2); |
| 379 | switch (m_blockType) |
| 380 | { |
| 381 | case 0: // stored |
| 382 | { |
| 383 | m_reader.SkipBits(m_reader.BitsBuffered() % 8); |
| 384 | if (!m_reader.FillBuffer(32)) |
| 385 | throw UnexpectedEndErr(); |
| 386 | m_storedLen = (word16)m_reader.GetBits(16); |
| 387 | word16 nlen = (word16)m_reader.GetBits(16); |
| 388 | if (nlen != (word16)~m_storedLen) |
| 389 | throw BadBlockErr(); |
| 390 | break; |
| 391 | } |
| 392 | case 1: // fixed codes |
| 393 | m_nextDecode = LITERAL; |
| 394 | break; |
| 395 | case 2: // dynamic codes |
| 396 | { |
| 397 | if (!m_reader.FillBuffer(5+5+4)) |
| 398 | throw UnexpectedEndErr(); |
| 399 | unsigned int hlit = m_reader.GetBits(5); |
| 400 | unsigned int hdist = m_reader.GetBits(5); |
| 401 | unsigned int hclen = m_reader.GetBits(4); |
| 402 | |
| 403 | FixedSizeSecBlock<unsigned int, 286+32> codeLengths; |
| 404 | unsigned int i; |
| 405 | static const unsigned int border[] = { // Order of the bit length code lengths |
| 406 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
| 407 | std::fill(codeLengths.begin(), codeLengths+19, 0); |
| 408 | for (i=0; i<hclen+4; i++) |
| 409 | codeLengths[border[i]] = m_reader.GetBits(3); |
| 410 | |
| 411 | try |
| 412 | { |
| 413 | HuffmanDecoder codeLengthDecoder(codeLengths, 19); |
| 414 | for (i = 0; i < hlit+257+hdist+1; ) |
| 415 | { |
| 416 | unsigned int k = 0, count = 0, repeater = 0; |
| 417 | bool result = codeLengthDecoder.Decode(m_reader, k); |
| 418 | if (!result) |
| 419 | throw UnexpectedEndErr(); |
| 420 | if (k <= 15) |
| 421 | { |
| 422 | count = 1; |
| 423 | repeater = k; |
| 424 | } |
| 425 | else switch (k) |
| 426 | { |
| 427 | case 16: |
| 428 | if (!m_reader.FillBuffer(2)) |
| 429 | throw UnexpectedEndErr(); |
| 430 | count = 3 + m_reader.GetBits(2); |
nothing calls this directly
no test coverage detected