Overview
INFLATE is the decompression algorithm that reverses the DEFLATE compression process. It reads compressed GZIP data and reconstructs the original uncompressed data. The INFLATE algorithm is specified in RFC 1951.INFLATE Function
Fromour_zlib.h:69:
bytes- Pointer to compressed data (after GZIP header, before footer)comp_len- Length of compressed data in bytes
- A malloc’d buffer containing decompressed data
NULLon error (NULL pointers, malformed data, etc.)
The decompressed size is stored in the GZIP footer (
full_size field). The caller should read the footer to know how much data to expect.INFLATE Algorithm
The INFLATE decompression process:Decompress based on block type
- Type 00: Copy uncompressed data
- Type 01: Decode with fixed Huffman codes
- Type 10: Decode with dynamic Huffman codes
- Type 11: Error (reserved)
INFLATE Flowchart
From README lines 648-726:Block Processing
Reading Block Header
From README lines 447-456:Block Type 00: Uncompressed
From README lines 458-459:Block Type 01: Fixed Huffman
From README lines 460-477: Use predefined Huffman codes:- Literals 0-143: 8 bits, codes
00110000-10111111 - Literals 144-255: 9 bits, codes
110010000-111111111 - Length codes 256-279: 7 bits, codes
0000000-0010111 - Length codes 280-287: 8 bits, codes
11000000-11000111 - Distance codes 0-29: 5 bits, codes
00000-11101
Block Type 10: Dynamic Huffman
From README lines 479-506: Read Huffman trees from the block:Decoding Code Lengths
From README lines 729-869: Code length symbols have special meanings:Decoding Huffman Symbols
The main decoding loop:Decoding Lengths and Distances
Use the tables from README lines 404-430:Complete Implementation Outline
Error Handling
ReturnNULL if:
bytesis NULLcomp_lenis 0- Block type is 3 (reserved)
- Invalid Huffman codes
- Distance codes 30 or 31
- LEN/NLEN mismatch in uncompressed block
- Invalid code length sequences
- Premature end of data
- Memory allocation fails
- Distance exceeds available data
Testing INFLATE
Test Cases
Uncompressed Block
Uncompressed Block
Create a file with BTYPE=00 and verify decompression
Fixed Huffman Block
Fixed Huffman Block
Compress data with static Huffman and decompress
Dynamic Huffman Block
Dynamic Huffman Block
Compress data with dynamic Huffman and decompress
Multi-Block File
Multi-Block File
Create file with 3 blocks, verify all are decompressed
Cross-Block References
Cross-Block References
Test LZ77 matches that reference previous blocks
Wrap-Around Case
Wrap-Around Case
Test distance < length (run-length encoding)
Validation
Implementation Reference
Seemain.c:141-187 for how inflate() is called:
Memory Management
Summary
Key INFLATE implementation points:- Process blocks until BFINAL = 1
- Handle three block types: uncompressed, fixed Huffman, dynamic Huffman
- Reserved block type (11) is an error
- Read dynamic Huffman trees from block header
- Decode code lengths with special symbols (16, 17, 18)
- Perform LZ77 decompression during Huffman decoding
- Reject distance codes 30 and 31
- Handle cross-block references with history buffer
- Handle wrap-around (distance < length)
- Return malloc’d buffer
- Handle all error cases
Next Steps
- DEFLATE - Compression algorithm
- Huffman Implementation - Huffman decoding details
- LZ77 Implementation - LZ77 decompression details
- GZIP Format - File format specification