Overview
Thepng_crc.h module provides functions for computing CRC-32 checksums used in PNG chunk validation. PNG uses CRC-32 with the polynomial 0xEDB88320 to ensure data integrity. The CRC is computed over the chunk type name (4 bytes) concatenated with the chunk data.
Functions
png_crc
Pointer to the buffer containing the data to compute CRC over
Number of bytes in the buffer
The 32-bit CRC value
Example Usage
Validating a PNG Chunk
Computing CRC for a New Chunk
Algorithm Details
The CRC-32 algorithm works as follows:1. Initialize Lookup Table
Create a 256-entry table where each entry is computed by applying the CRC polynomial0xEDB88320 to each possible byte value (0-255):
2. Process Each Byte
Initialize CRC register to0xFFFFFFFF and process each byte:
3. Finalize
XOR the final CRC value with0xFFFFFFFF to get the result.
PNG Specification
From the PNG specification:
The CRC checksum is computed over the chunk type and chunk data, but not the chunk length or the CRC itself. The CRC is calculated using the polynomial 0xEDB88320.
Notes
- The CRC is computed over both the chunk type (4 bytes) and chunk data (variable length)
- The chunk length and CRC fields themselves are not included in the CRC calculation
- The lookup table is initialized once and reused for all subsequent calls (static storage)
- All multi-byte CRC values in PNG files are stored in big-endian format
- Sample implementation is provided in the PNG specification Appendix
Error Detection
CRC-32 provides strong error detection capabilities:- Detects all single-bit errors
- Detects all double-bit errors
- Detects any odd number of bit errors
- Detects any burst error of 32 bits or less
- Detects most longer burst errors with high probability
Performance
The lookup table approach provides efficient CRC computation:- Table generation: One-time cost of 256 iterations
- Per-byte cost: One table lookup, two XOR operations, one shift
- Typical performance: Several hundred MB/s on modern CPUs