libspng (C)
A single-file C99 PNG reader and writer that replaces libpng's callback-and-setjmp API with one opaque context, a pull-model byte source and integer error codes; its memory limits require caller configuration, and its decoder is progressive by rows but not suspendable.
| Field | Value |
|---|---|
| Language | C99 (meson.build), one translation unit spng/spng.c (6,980 lines) and one header spng/spng.h (537 lines) |
| License | BSD-2-Clause; the SIMD filter code derived from libpng is under the PNG Reference Library License version 2 (spng.c, README.md) |
| Repository | randy408/libspng |
| Revision read | 46c81dc2b3747fa43aac33704c890a468f195d6e (master merge, August 31, 2026; header and Meson version 0.7.4, the release of May 8, 2023) |
| Documentation | libspng.org/docs · in-tree docs/ (decode.md, context.md, chunk.md) |
| Category | Formats: PNG 1.2 codec with a context API and zlib or miniz as the inflate backend |
| Tier | A |
| Last reviewed | November 15, 2026 |
NOTE
The clone is shallow, so the history before the pinned merge was read through the GitHub API, as were the release notes and the commits that landed upstream after the pin (November 10 to 13, 2026), which this page cites where they fix something the pinned code gets wrong. Claims marked verified come from two C probes compiled with GCC 14.3.0 against the pinned spng.c and zlib 1.3.2 under AddressSanitizer and UndefinedBehaviorSanitizer, in a scratch directory under /tmp. The verifier reran both probes on November 15, 2026 with GCC 16.2.0, zlib 1.3.2 and the same sanitizers, against freshly extracted pinned source in /tmp/libspng-verify-epypAA; the reported results matched. An additional probe checked the discarded text chunk and a sub-8-KiB chunk limit. A second verifier wrote an independent probe (same compiler, zlib and sanitizers) that reproduced every verified result and also confirmed the critical-CRC skip described in section 5. These are scratch probes, not CI examples. No performance number in this page was measured; the project's own benchmark chart carries no methodology, so none is repeated here.
Overview
What it solves
libpng is the reference PNG implementation, and its API is old: errors arrive through a setjmp/longjmp handler, transformations are switched on one call at a time (png_set_expand, png_set_strip_16, and so on), and the image header lives in a separate info structure. libspng covers every PNG 1.2 colour type and bit depth, Adam7 and the standard PNG 1.2 ancillary chunks, adds eXIf and an encoder, and offers a context, a target pixel format and return codes. The README states the scope in its first lines (README.md):
"libspng (simple png) is a C library for reading and writing Portable Network Graphics (PNG) format files with a focus on security and ease of use."
and the motivation under its Motivation heading (README.md):
"The goal is to provide a fast PNG library with a simpler API than libpng, it outperforms the reference implementation in common use cases."
The common case is a four-call decode: create a context, set the input, ask for the output size in a chosen format, decode into a caller buffer. Every PNG colour type and depth converts to SPNG_FMT_RGBA8, SPNG_FMT_RGBA16 or SPNG_FMT_RGB8; SPNG_FMT_PNG and SPNG_FMT_RAW return the samples untouched (host-endian and big-endian respectively) (decode.md).
Design philosophy
Three commitments recur in the source and the documentation.
- One opaque context holds everything.
spng_ctxis a single heap object: the input cursor, every parsed chunk, the zlib stream, the scanline buffers and the decode state machine (spng.c). The migration guide says so directly: "The context handlespng_ctxis an opaque datatype that holds all information, there is no separate info struct." (migrate-libpng.md). - Errors are values, and a critical one poisons the context. Status-returning functions return
0or aspng_errnovalue (the enum has 88 enumerators,spng.h). A critical error sets the state toSPNG_STATE_INVALIDthroughdecode_err(spng.c), and "most functions check for a valid context state and returnSPNG_EBADSTATEfor subsequent calls to prevent undefined behavior" (decode.md). The row entry points omit that state check at the pin, as the probes below demonstrate. - Lenient by default, for compatibility with files in the wild. "The default behavior is meant to emulate libpng for compatibility reasons with existing images in the wild, most non-critical errors are ignored. Configuring decoder strictness is currently limited to checksums." (
decode.md). Many malformed ancillary chunks are discarded; limit errors remain fatal. A bad CRC on an ancillary chunk is discarded by default since 0.6.2 (spng.h).
The README's security claims are process claims: the code follows the CERT C coding standard, "All integer arithmetic is checked for overflow and all error conditions are handled gracefully", and it is fuzzed on OSS-Fuzz and scanned by Clang Static Analyzer, Coverity and PVS-Studio (README.md). Where this page tests those claims, it says so.
How it works
The context and its state machine
A decoder context moves through a linear state machine (spng.c):
// spng/spng.c (abridged)
enum spng_state
{
SPNG_STATE_INVALID = 0,
SPNG_STATE_INIT = 1, /* No PNG buffer/stream is set */
SPNG_STATE_INPUT, /* Decoder input PNG was set */
SPNG_STATE_IHDR, /* IHDR was read/written */
SPNG_STATE_FIRST_IDAT, /* Encoded up to / reached first IDAT */
SPNG_STATE_DECODE_INIT, /* Decoder is ready for progressive reads */
SPNG_STATE_EOI, /* Reached the last scanline/row */
SPNG_STATE_LAST_IDAT, /* Reached last IDAT, set at end of decode_image() */
SPNG_STATE_AFTER_IDAT, /* */
SPNG_STATE_IEND, /* Reached IEND */
};Chunk getters run read_chunks first, usually through the SPNG_GET_CHUNK_BOILERPLATE macro (spng.c), so metadata is parsed lazily: calling spng_get_text before decoding reads every chunk up to the first IDAT, and calling it after decoding reads on to IEND. spng_get_ihdr is the exception: it reads only the header (chunk.md). spng_ctx_new2 takes a struct spng_alloc of four function pointers (malloc_fn, realloc_fn, calloc_fn, free_fn), and the same allocator is passed to zlib through spng__zalloc (spng.c, context.md).
Input: buffer, stream or file
Three setters choose the source, once per context (spng.c):
spng_set_png_bufferkeeps a pointer to the caller's bytes. Its read function,buffer_read_fn, copies nothing: it advancesctx->dataand decrementsbytes_left, so every chunk is parsed in place (spng.c).spng_set_png_streamtakes aspng_rw_fnand a user pointer and allocates an 8 KiBstream_buf(SPNG_READ_SIZE, marked "Not build options, edit at your own risk!",spng.c). The callback contract is blocking: "A read callback function should copylengthbytes todestand return 0 orSPNG_IO_EOF/SPNG_IO_ERRORon error" (context.md).spng_set_png_filewrapsfreadandfwritein the same callback type.
The API is therefore pull: the decoder asks for exactly length bytes and the source either supplies them or fails. There is no "need more input" return, so a source that does not have the bytes yet must block inside the callback. The decoder cannot be suspended and resumed when data arrives (see suspendable decoding).
Chunk parsing and the undo log
read_header reads the previous chunk's CRC and the next chunk's length and type in one step (spng.c); read_chunk_bytes feeds every byte it reads to crc32 (spng.c). Because the CRC trails the data, a chunk is parsed and stored before its CRC is known. read_non_idat_chunks handles this with an undo log: each arbitrary-length chunk handler sets ctx->undo (text_undo, splt_undo, chunk_undo), and when the next read_header sets ctx->discard after a bad ancillary CRC, the loop calls the undo function and restores the stored bitfield from prev_stored (spng.c).
Chunks with a fixed small payload (PLTE, cHRM, gAMA, sBIT, sRGB, bKGD, tRNS, hIST, pHYs, tIME, oFFs) are recognised by is_small_chunk and read with at most 768 bytes, the size of a 256-entry PLTE (spng.c); they are stored in fixed fields of the context and allocate nothing. eXIf, iCCP, tEXt, zTXt, iTXt, sPLT and unknown chunks allocate and are charged to the chunk cache.
When a chunk fails validation, read_chunks consults a list of 43 recoverable error codes: if the chunk is ancillary and the context is not strict, the rest of the chunk is skipped, the undo function runs, and parsing continues (spng.c):
// spng/spng.c, read_chunks()
case SPNG_EZLIB:
{
if(!ctx->strict && !is_critical_chunk(&ctx->current_chunk))
{
ret = discard_chunk_bytes(ctx, ctx->cur_chunk_bytes_left);
if(ret) return decode_err(ctx, ret);
if(ctx->undo) ctx->undo(ctx);ctx->strict has no setter at the pinned revision: a search of spng.c finds it read four times and never assigned, so strict mode is unreachable through the public API.
Inflate
Metadata streams (iCCP, zTXt, compressed iTXt) go through spng__inflate_stream, which starts with an 8 KiB buffer and doubles it until the stream ends, refusing to grow past the smaller of the chunk size limit and the remaining cache budget. The initial buffer and the final size are not checked against that bound, so limits below 8 KiB can be bypassed (spng.c, verified below):
// spng/spng.c, spng__inflate_stream()
if(!stream->avail_out) /* Resize buffer */
{
/* overflow or reached chunk/cache limit */
if( (2 > SIZE_MAX / size) || (size > max / 2) )
{
ret = SPNG_ECHUNK_LIMITS;
goto err;
}Image data is inflated directly into the scanline buffer by read_scanline_bytes, which asks read_idat_bytes for more input whenever zlib returns Z_BUF_ERROR, stepping over IDAT boundaries (spng.c). With buffer input, read_idat_bytes consumes a whole IDAT chunk per call; with stream input, at most 8 KiB. Before inflating, spng_decode_image peeks at the zlib header's FLEVEL bits and exposes them as an estimate through SPNG_IMG_COMPRESSION_LEVEL (spng.c).
Scanlines, filters and the one-byte offset
Each row is read together with the next row's filter byte, so that the pixel data starts at offset zero of the buffer (spng.c):
// spng/spng.c, spng_decode_image()
/* Read the first filter byte, offsetting all reads by 1 byte.
The scanlines will be aligned with the start of the array with
the next scanline's filter byte at the end,
the last scanline will end up being 1 byte "shorter". */
ret = read_scanline_bytes(ctx, &ri->filter, 1);read_scanline then validates that filter byte (greater than 4 is SPNG_EFILTER), byte-swaps 16-bit samples to host order before defiltering, and calls defilter_scanline (spng.c). Swapping first is sound because every filter works bytewise at a distance of bytes_per_pixel, which is even for 16-bit images, so the byte permutation commutes with the filter arithmetic. Only two rows are live (scanline, prev_scanline, swapped after each row), plus a third, row, for interlaced images (decode.md).
spng_decode_scanline converts one defiltered scanline to the output format using branches on colour type and depth, with whole-row fast paths for some combinations (spng.c), then applies trns_row, scale_row (for sBIT) and gamma_correct_row as whole-row passes (spng.c). spng_decode_row adds Adam7 scatter: for passes 0 to 5 it decodes into ctx->row and copies each pixel to adam7_x_start[pass] + k * adam7_x_delta[pass] in the caller's row (spng.c).
Progressive decoding
With SPNG_DECODE_PROGRESSIVE, spng_decode_image initialises the decoder and returns; the caller then pulls rows. For interlaced images rows arrive out of order, so the documented loop asks for the destination first (decode.md):
// docs/decode.md
do
{
error = spng_get_row_info(ctx, &row_info);
if(error) break;
void *row = image + image_width * row_info.row_num;
error = spng_decode_row(ctx, row, len);
}
while(!error)
if(error == SPNG_EOI) /* success */spng_row_info carries scanline_idx, row_num (the deinterlaced row), pass and filter (spng.h). This is progressive in the row sense only: the caller controls when each row is produced, but each call still blocks in the read callback until its bytes exist.
Analysis
1. Pixel and memory model
There is no image type. The output is a caller-owned byte buffer whose size spng_decoded_image_size computes as row bytes times height, with overflow checks at each multiplication; SPNG_FMT_PNG and SPNG_FMT_RAW retain packed sub-byte samples (spng.c). Rows are tightly packed, with no stride parameter and no alignment option. Progressive decoding hands out one row at a time, so a caller can place rows anywhere, which is the only way to get a strided or tiled destination. The pixel vocabulary is the eight spng_format values; "Channels are always in byte-order" and 16-bit outputs are host-endian except SPNG_FMT_RAW (spng.h). Alpha is "always straight alpha, premultiplied alpha is not supported" (decode.md) (see straight and premultiplied alpha). SPNG_FMT_G8, GA8 and GA16 are partial: check_decode_fmt accepts G8/GA8 only for 1-, 2-, 4- or 8-bit greyscale input, and GA16 only for 16-bit greyscale input; greyscale-alpha input is rejected (spng.c).
2. Genericity mechanism
None in the language sense. Format conversion is a run-time switch inside one loop (spng_decode_scanline), with flags precomputed once per image into struct decode_flags (same_layout, unpack, do_scaling, apply_trns, apply_gamma, indexed, interlaced) (spng.c). A zerocopy flag that would let a same-layout, non-interlaced decode skip the copy exists but its assignment is commented out (spng.c). The choice of output format is a value, not a type. The conversion loop contains branches on image-invariant values, as well as whole-row fast paths (spng.c). Whether the compiler hoists or specialises the remaining branches was not checked. A D implementation could express format specialisation with compile-time parameters; its benefit needs measurement.
3. Colour and metadata
libspng parses colour chunks and exposes them; it applies almost none of them.
- Gamma.
SPNG_DECODE_GAMMAbuilds a lookup table fromgAMAwith a fixed screen gamma of 2.2 (float screen_gamma = 2.2f) andpowonfloatoperands (upstream switched topowfafter the pin); the table is 256 entries for 8-bit outputs and a 128 KiB allocation for 16-bit (spng.c,decode.md). It is not implemented forSPNG_FMT_PNG,G8,GA8orGA16(README.md), and it ignoressRGB,cHRMandiCCPwhen deciding whether to apply. - ICC.
iCCPis inflated and returned as bytes; "ICC profiles are not validated" (chunk.md). See ICC profile. - CICP, HDR and gain maps. Absent. The
spng_chunk_bitfieldhas 17 fields (spng.c) and none iscICP,mDCVorcLLI, which the third edition of the PNG specification added (W3C PNG third edition). AcICPchunk is treated as unknown: discarded, or kept as raw bytes withSPNG_KEEP_UNKNOWN_CHUNKS. No CICP or gain-map support exists. - EXIF and XMP.
eXIfis supported and checked only for aII*\0orMM\0*byte-order mark (check_exif,spng.c); the TIFF structure is not parsed. XMP travels in aniTXtchunk with the keywordXML:com.adobe.xmpand is returned as ordinary text. Text is validated as Latin-1 only in strict mode, which is unreachable. sBIT. Honoured through the undocumentedSPNG_DECODE_USE_SBITflag, which rescales samples by their significant bits (spng.h).
Chunk-order rules are enforced per chunk (cHRM after PLTE is SPNG_ECHUNK_POS, a second gAMA is SPNG_EDUP_GAMA), but both errors are in the recoverable list, so the default outcome is that the offending chunk is dropped (spng.c, recovery list). See image metadata.
4. I/O and codec architecture
The answers to the codec questions, in one place:
| Question | Answer at the pinned revision |
|---|---|
| Push or pull | Pull: spng_rw_fn must supply exactly length bytes or fail (context.md) |
| Incremental | Only through a blocking callback; no partial-input return |
| Progressive | Row by row (spng_decode_row, spng_decode_scanline), Adam7 rows out of order with spng_get_row_info |
| Suspendable | No. A callback that cannot supply bytes ends the decode with SPNG_IO_EOF or SPNG_IO_ERROR, which invalidates the context |
| Row or tile callbacks | No callbacks; the caller pulls rows |
| Region decode | No. Rows can be discarded after decoding; columns cannot be skipped, and inflate is sequential |
| Downscaled decode | No. Adam7 passes are decoded but there is no API to stop after pass N and get a low-resolution image |
| Zero-copy input | Yes for buffers (buffer_read_fn advances a pointer); streams copy through an 8 KiB buffer |
| Inflate backend | zlib or miniz via SPNG_USE_MINIZ; disabling Adler-32 checks requires a suitable zlib (inflateValidate, documented as 1.2.11 or later) and is unavailable with miniz (spng.c, decode.md) |
| Encoder | Same context with SPNG_CTX_ENCODER; adaptive filter choice per row (get_best_filter), output to a stream or an internal buffer |
Truncation behaves differently by input mode (verified). A 256 by 256 random-content greyscale image cut at 60% of its bytes and fed through spng_set_png_stream delivered 127 of 256 rows before spng_decode_row returned SPNG_IO_EOF. The same cut fed through spng_set_png_buffer delivered none: buffer mode reads a whole IDAT chunk at once, so the progressive initialisation itself failed with SPNG_IO_EOF. The documentation warns of this: "getting a partial image is possible with progressive decoding but is not guaranteed to work in all cases. The decoder issues read callbacks that can span multiple rows or even the whole image, partial reads are not processed" (decode.md).
5. Hostile input and safety
Limits and their defaults. Dimension and chunk-size defaults are the format's maxima, and the cache defaults to SIZE_MAX; the allocating-chunk count has a finite default of 1000 (spng.c, context.md, decode.md):
| Limit | Setter | Default | Enforced on |
|---|---|---|---|
| Width, height | spng_set_image_limits | 2^31 − 1 each (spng_u32max) | IHDR, in check_ihdr |
| Decoded image bytes | none; the caller checks spng_decoded_image_size | unbounded | caller |
| Chunk size | spng_set_chunk_limits(chunk_size, …) | 2^31 − 1 | growth of inflated iCCP, zTXt, iTXt; sub-8-KiB caveat below |
| Chunk cache (allocated metadata) | spng_set_chunk_limits(…, cache_limit) | SIZE_MAX | eXIf, text, sPLT, unknown chunks, inflated metadata; sub-8-KiB caveat below |
| Stored chunk count | SPNG_CHUNK_COUNT_LIMIT option | 1000 (SPNG_MAX_CHUNK_COUNT) | eXIf, text, sPLT and unknown chunks; iCCP is not counted |
| Total input, work, time | none | none | none |
The documentation is explicit that untrusted input needs all three caller actions: set image limits, check spng_decoded_image_size "against a constant limit", and "Set a chunk size and chunk cache limit with spng_set_chunk_limits() to control memory usage and avoid DoS from decompression bombs" (decode.md). What the defaults allow (verified):
- A 100,000 by 100,000 RGBA 16-bit
IHDRpasses default limits;spng_decoded_image_sizereports 80,000,000,000 bytes. Withspng_set_image_limits(ctx, 16384, 16384)the same file fails withSPNG_EUSER_WIDTH. - An
eXIfchunk whose header declares 0x7FFFFFF0 bytes, in a 45-byte file ending after four payload bytes, made the context request one allocation of 2,147,483,632 bytes before the payload was read; the call then failed withSPNG_IO_EOF. With 1 MiB chunk limits, it failed withSPNG_ECHUNK_LIMITSand allocated nothing. The handler chargeschunk.lengthto the cache and thenmallocs it in full (spng.c); the declared length is trusted up to the cache limit. - A
zTXtpayload of 65,248 bytes expanding to 64 MiB of text madespng_get_textallocate a 67,108,865-byte buffer with default limits. With a 1 MiB chunk limit,spng_get_textfailed withSPNG_ECHUNK_LIMITS, and the followingspng_decode_imagereturnedSPNG_EBADSTATE:SPNG_ECHUNK_LIMITSis not in the recoverable list, so an oversized ancillary chunk kills the pixel decode as well. - With
spng_set_chunk_limits(ctx, 1024, 8192), azTXtexpanding to 4,096 text bytes was accepted (verifier probe). The inflater starts with 8 KiB regardless of the bound, checks the bound only when doubling, and ignores the final cache-accounting error (spng.c). Small configured limits are therefore not hard bounds. spng_set_option(ctx, SPNG_CHUNK_COUNT_LIMIT, 100)on a fresh context returns1(failure), as does5000; only0succeeds. The setter rejects any value greater than the number of chunks already counted, which is zero before decoding (spng.c):if(value > (int)ctx->chunk_count_total) return 1;. The comparison looks inverted; as written, the documented "configurable" count limit can only be lowered to zero on a fresh context.
Checksums. spng_set_crc_action(ctx, critical, ancillary) takes SPNG_CRC_ERROR, SPNG_CRC_DISCARD (ancillary only) or SPNG_CRC_USE; the defaults are error for critical and discard for ancillary chunks (spng.c, spng.h). SPNG_CRC_USE also turns off Adler-32 checking for the corresponding chunk kind via inflateValidate (spng.c). At the pin, read_header also skips critical CRCs when only the ancillary action is SPNG_CRC_USE, because its second branch lacks an ancillary-kind check (spng.c) (verified: a PLTE with a corrupted CRC failed with SPNG_ECHUNK_CRC under the defaults and decoded with return 0 after spng_set_crc_action(ctx, SPNG_CRC_ERROR, SPNG_CRC_USE)). Because a CRC is checked when the next header is read, the last IDAT's CRC is not checked by spng_decode_image (verified): a file whose only IDAT has a corrupted CRC decoded with return 0, and the error, SPNG_ECHUNK_CRC, appeared only when spng_decode_chunks was called afterwards. A corrupted Adler-32 was reported by spng_decode_image itself (SPNG_EIDAT_STREAM). A corrupted tEXt CRC was discarded silently and the decode succeeded; the verifier's follow-up spng_get_text returned SPNG_ECHUNKAVAIL.
Lenience findings (verified). A file with two PLTE chunks decoded successfully and used the second palette; issue 275 reports libpng rejecting its duplicate-palette test file, and upstream added the check on November 13, 2026, after the pin, in response to issue 275 (commit 272c9f29e5, issue 275). Palette indices past the palette's end decode as opaque black, as documented (decode.md); that is bounds-safe by construction, because decode_plte is a 256-entry table in the context; palette RGB entries start zeroed and all 256 alpha entries are initialised to 255 unless tRNS applies (spng.c). Calling spng_decode_row after a failed progressive initialisation returned SPNG_EINTERNAL rather than SPNG_EBADSTATE; upstream added state checks to spng_decode_scanline on November 10, 2026 (commit 8288236fa2).
Arithmetic. The overflow checks the README promises are present in the size calculations (calculate_scanline_width, calculate_image_width, increase_cache_usage). One encoder-side check is dead: require_bytes guards its doubling loop with if(new_size / SIZE_MAX > 2), which is never true, so the intended overflow test does not run (spng.c). Reaching it needs attempted buffer growth past SIZE_MAX / 2. Reachability was not established on either 32- or 64-bit targets; this was read, not run.
Vulnerability history. The author's web search found no libspng CVE, but that does not establish that none exists. The NVD API returned HTTP 503 on November 14 and again on November 15, 2026. OSV lists 11 OSS-Fuzz issues, all published in June and July 2020 against development commits (OSV query). Each record gives only the crash type, the crash stack and the introducing and fixing commits; the root causes below are not stated in the records and were not reconstructed. Three cover the classic memory-safety classes of a hand-written C parser:
- OSV-2020-227, a heap double free in
spng__free, with the stackspng__inflate_streamthenread_non_idat_chunks. The stack locates the crash on the metadata-inflate path; it does not establish the ownership mistake that caused it. - OSV-2020-200, a heap-buffer-overflow write of 2 bytes in
spng_decode_scanline, reached fromspng_decode_rowandspng_decode_image. The record does not identify the particular buffer or conversion responsible. - OSV-2020-360, a heap use-after-free read in
read_chunks_after_idat(a function absent from the pinned revision, whereread_non_idat_chunkshandles chunks on both sides of theIDATsequence).
The release notes of 0.7.2 add a fourth, reported in a public issue from the reporter's own fuzzing output: "Fixed encoder heap overflow with specially crafted images", a memset past the end of a buffer in encode_row for an interlaced 4-bit image (issue 196, releases). The README's claim of "No known security bugs" rests on the policy that "vulnerabilities are fixed before they become public" (README.md). See hostile input and the hostile-input papers thread.
6. Allocation and ownership
All allocation goes through the context's spng_alloc, including zlib's (spng.c). The image buffer is always the caller's. The context owns parsed metadata: strings returned by spng_get_text, spng_get_iccp and spng_get_exif point into it and are freed by spng_ctx_free (chunk.md). In the other direction, data handed to spng_set_splt or spng_set_unknown_chunks "should be a valid reference for the lifetime of the context", and the user bitfield records which chunks the caller owns so that spng_ctx_free does not free them (chunk.md, spng.c). Decode scratch is two scanline buffers sized to the widest Adam7 pass plus 32 bytes, an interlace row buffer, and the optional 16-bit gamma table (spng.c). The encoder's internal output buffer grows by doubling and becomes the caller's after spng_get_png_buffer (spng.c).
7. CPU parallelism and SIMD
No threading: pthread.h is included under SPNG_MULTITHREADING and then unused (spng.c). SIMD covers defiltering and, on ARM, palette expansion:
- x86. SSE2 implementations of Sub, Avg and Paeth for 3- and 4-byte pixels, derived from libpng's code (
spng.c). The ISA level is a compile-time macro,SPNG_SSE(1 to 4); level 3 uses_mm_abs_epi16, level 4_mm_blendv_epi8(spng.c, intrinsics). "Currently only SSE2 optimizations are tested" (build.md). - AArch64. NEON versions of the same filters plus
expand_palette_rgba8_neonandexpand_palette_rgb8_neon, which gather four or eight palette entries per iteration with lane loads (spng.c). "NOTE: only arm64 builds are tested!" (spng.c). - Runtime dispatch. One function only:
defilter_upcarries__attribute__((target_clones("default,avx2"))), enabled when the build probetests/target_clones.clinks (GCC and glibc on x86-64) (spng.c,target_clones.c). Every other SIMD path is chosen by the preprocessor, and an unknown architecture falls back to scalar code with a#pragma message. - Up filter is scalar on purpose:
defilter_scanlinejumps to the plain loop forSPNG_FILTER_UP, leaving it to the compiler's auto-vectoriser (and the AVX2 clone).
Sub, Avg and Paeth for 1-, 2-, 6- and 8-byte pixels, and all filtering in the encoder, use scalar source loops. Up uses the auto-vectorisable loop at every pixel size. Inflate speed is whatever the linked zlib provides.
8. GPU offload and interop
No GPU offload. libspng produces host bytes and has no planar output. The caller can supply CPU-writable mapped or externally allocated memory to spng_decode_image or the progressive row API, which permits direct decoding into a suitably laid-out staging buffer (decode.md).
9. Temporal, animation and containers
APNG is not supported. The README lists "Animated PNG" as "Planned" (README.md); acTL, fcTL and fdAT are unknown chunks to the parser, and an fdAT after the IDAT sequence is either discarded or kept as raw bytes. PNG is the only container. APNG is now part of the PNG specification's third edition (W3C PNG third edition), so this is a gap against the current standard, not only against libpng's patched builds.
10. Testing and conformance
The test suite needs a developer build (meson configure -Ddev_build=true) and libpng 1.6.0 or later; the fallback Meson wrap pins 1.6.37 (tests/README.md, tests/meson.build, libpng.wrap). It is a differential test against libpng over PngSuite (175 images in tests/images, the README's count, among them exif2c08.png with an eXIf chunk): for each image and each output format and flag combination, both libraries decode and "the output images have to be bit-identical, for gamma-corrected images each color / grayscale sample has to be within 2%" (tests/README.md, testsuite.c). Chunk contents are compared too (compare_chunks). tests/crashers holds 27 regression files, many copied from libpng: one per oversized ancillary chunk type (huge_iCCP_chunk.png, huge_eXIf_chunk.png and so on), bad CRC, bad Adler-32, zero width, zero gAMA, missing PLTE (tree).
Fuzzing runs on OSS-Fuzz with three targets built by ossfuzz.sh: spng_read_fuzzer, the same target with the libpng png_mutator.h structure-aware mutator (from Google's fuzzer-test-suite, included with -include and PNG_MUTATOR_DEFINE_LIBFUZZER_CUSTOM_MUTATOR), and spng_write_fuzzer. The read target derives the output format, flags, input mode (buffer, stream, FILE), progressive mode and the ancillary CRC action from the last four input bytes. It creates the context with SPNG_CTX_IGNORE_ADLER32 and sets SPNG_CRC_USE for critical chunks, so mutations are not rejected by checksum and the default checksum configuration is never fuzzed. It caps dimensions at 200,000, decoded output at 80,000,000 bytes, and metadata at 4 MB per chunk and 8 MB in total, calls the getters before decoding and checks text string invariants (spng_read_fuzzer.c, getter checks). Seeds are the repository's PNGs plus a dictionary (spng.dict). Pull requests run CIFuzz for 900 seconds (ci-fuzz.yml), and the OSS-Fuzz corpora can be replayed as Meson tests with the oss_fuzz option (tests/meson.build). Benchmarks live outside the repository, in the spngt and benchmark_images wraps, both pinned to head (spngt.wrap).
Maintenance. Tags run from 0.2.0 to 0.7.4 (May 8, 2023), and the README calls releases from 0.4.0 on stable (README.md); the release notes of 0.6.3 and the 0.7.0 series credit NLNet funding (releases). Between the 0.7.4 release and the pinned merge the repository received three non-merge commits (a README badge change in May 2023, a CMake fix in December 2023 and a copyright-year update in August 2026), plus the merge itself. After the pin, eight commits landed on November 10 to 13, 2026, fixing the duplicate PLTE acceptance, adding state checks, preventing a second spng_decode_image call and switching gamma to powf (commit list). Security reports go to one contact address, and only the most recent release is supported (SECURITY.md).
Lessons for Sparkles
- Q4 — A context state machine with "invalid" as a terminal state is the right failure model, and D can expose it explicitly. libspng's
decode_errsets one field, but the row entry points omit the check;SPNG_EINTERNALafter a failed init demonstrates the gap, fixed upstream later. A D decoder can returnExpected!(Row, PngError)and reject further work after a fatal error.Expectedalone does not prevent callers from invoking the decoder again; that still needs an enforced state or ownership contract. The draftsparkles:imagealready returnsPngResultwith an offset (existing code). - Q4 — Pull with a blocking callback is not suspendable, and libspng shows the cost. Its progressive API requires synchronous reads; in the single-
IDATtruncation probe, buffer input yielded no rows while stream input yielded 127. The result depends on chunk boundaries and callback request sizes. A Sparkles decoder meant to sit onsparkles:event-horizonreads needs a push or resumable design (Wuffs' coroutines, the survey's counterpoint) that returns "short read" with its state intact (concepts, event-horizon baseline). Both existing Sparkles PNG decoders are whole-file and single-shot (G7 in the baseline); libspng's row API would be a step, not the target. - Q4, Q8 — Lazy metadata parsing behind getters is convenient and hides cost. A call named
spng_get_textcan allocate 64 MiB (verified). A D design that parses metadata on demand should make the budget an argument or a property of the decoder value, not an ambient context setting, and should decide whether an over-budget ancillary chunk is skipped or fatal. libspng makes it fatal, which lets a hostilezTXtdeny the image. - Q4, Q8 — Limits must default closed. libspng's dimension and allocation defaults allow a caller that forgets
spng_set_chunk_limitsto get a 2 GiB allocation request from a 45-byte file (verified). Sparkles' existingmaxPngPixels = 1UL << 26default is the opposite stance and matches the hostile-input thread's recommendations (existing code, papers). The list of limits a D decoder needs is visible here: dimensions, decoded bytes, per-chunk raw length, per-chunk inflated length, total metadata, chunk count, and none of libspng's for total input or work. - Q8 — Test the limit setters, not only the limits. The
SPNG_CHUNK_COUNT_LIMITsetter is unable to accept a positive limit on a fresh context at the pin; the read fuzzer never calls it. The sub-8-KiB inflate bypass also needs boundary tests. A D limits struct still needs tests that check each field's effect on actual decoding, including small and exact-boundary budgets. - Q4 — Trailing-CRC formats need an explicit "verified" point. libspng parses a chunk before its CRC arrives and undoes it later; for the last
IDATthe CRC is checked only if the caller reads on. A D decoder over a range of chunks can make "pixels delivered" and "file verified" two distinct states, so a caller that needs integrity (golden tests, theDBG1byte-compared screenshots) can ask for the second. - Q3, Q7 — Defiltering is a SIMD candidate, and dispatch should be at run time. libspng picks SSE2 or NEON at compile time and uses
target_clonesfor a single function. The repository's host-SIMD precedent already does run-time dispatch by hand (shaders and SIMD baseline, G5). Its Paeth kernels for 3 and 4 bytes per pixel are candidates to profile and port; this review did not measure their share of decode time. - Q2 — Explore a compile-time output format parameter. libspng's conversion source branches over
fmtand colour type, with whole-row fast paths for some combinations. D can instantiate the conversion loop per (source colour type, depth, target format) and keep a thin run-time switch at the top. A benchmark and generated-code inspection must establish whether that improves on the C compiler's existing optimisation. - Q5 — Parse every colour chunk, apply none by default. libspng returns
gAMA,cHRM,sRGBandiCCPraw and offers only a fixed-2.2 gamma transform. That is a reasonable floor for a codec; the missing piece iscICP,mDCVandcLLIfrom PNG's third edition, which a Sparkles PNG decoder written now should carry as typed metadata from the start (concepts). - Q8 — The differential harness is the transferable asset. libspng's tests decode every PngSuite file in every supported output format with both libraries and require bit identity, with a 2% tolerance for gamma. That is the shape Sparkles'
png_oracle.dlacks (byte-exact, single-format, stb_image only, G11 in existing code). libspng is a candidate second oracle: one C file needing only zlib, which suits ImportC (not tried here), and a lineage independent of stb_image. - Q8 — Fuzz targets should encode configuration in the input. The read fuzzer's trailing four bytes select format, flags, input mode and progressive mode, so one corpus covers the API surface. A D fuzz target over a
sparkles:imagedecoder can do the same with the decoder's options struct. - Q9 — One file, one context, codec plus encoder. libspng's single-translation-unit layout and absence of a pixel type are what make it easy to vendor, and what keep it a codec rather than an image library. That is evidence that a PNG codec does not need an image-core package beneath it, only a row sink.
Strengths
- A small, coherent API: one context type, four calls for the common decode, integer errors, no
setjmp. - Covers the whole PNG 1.2 feature set including Adam7, 16-bit,
sBIT,tRNS, every standard ancillary chunk andeXIf, plus an encoder with adaptive filtering. - Explicit, documented limits for dimensions, chunk size, metadata cache and chunk count, and a documented recipe for untrusted input.
- Zero-copy parsing from caller buffers; two scanlines of decode scratch.
- A real differential test suite against libpng over PngSuite, a regression corpus of crashers, and continuous OSS-Fuzz plus CIFuzz coverage, with a structure-aware mutator.
- Custom allocator threaded through to zlib.
Weaknesses
- Not suspendable: a blocking pull callback, and buffer mode that needs whole
IDATchunks. - Dimension and memory limits default open despite the finite chunk-count default; a hostile header triggers a 2 GiB allocation and a small
zTXta 64 MiB one (verified). Inflated metadata can exceed configured sub-8-KiB limits (verifier probe). - The chunk-count limit setter rejects every positive value on a fresh context (verified), and strict mode has no setter.
- Lenient by default: duplicate
PLTEwas accepted until a post-pin fix; the finalIDATCRC is verified only if the caller keeps reading (verified). - No
cICP,mDCV,cLLIor APNG, all now in the PNG specification. - SIMD is compile-time apart from one function; only SSE2 and arm64 are tested.
- Sparse maintenance: no release since May 2023 and three non-merge commits plus the merge between that release and the pin; fixes resumed in November 2026, after the pinned revision.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
| One opaque context for decoder, encoder, metadata and zlib state | Simple API; no separate info struct; one free call | Cannot share parsed metadata across decodes; the context is large and mutable |
| Pull I/O with a callback that must fill the request | Mirrors libpng; easy to write against FILE and memory | Not suspendable; a non-blocking source cannot be used without threads |
| Output format chosen at run time from eight values | Shared conversion code plus whole-row fast paths | Branches in the source; compiled cost unmeasured; partial G8/GA8/GA16; no premultiplied output |
| Lenient defaults: discard bad ancillary chunks and CRCs | Matches libpng on files in the wild | Corruption goes unreported; strict mode exists internally but cannot be enabled |
| Parse first, undo on bad CRC | CRC trails the data; streaming parse without buffering whole chunks | The last IDAT CRC is unchecked unless the caller reads on |
| Large default dimension and memory limits; count defaults to 1000 | Avoid restrictive size defaults | Caller must budget memory; small inflated limits have gaps; over-limit metadata is fatal |
| Lazy metadata parse behind getters | Parsing begins when a getter or decode requires it | Getters can block and allocate; requesting one type also parses other preceding metadata |
Compile-time SIMD from libpng's intrinsics; target_clones once | Reuses tested code; dispatch limited to one function | Default x86 intrinsics use SSE2; AVX2 clone only for Up; other compiler vectorisation unexamined |
| Single C file, zlib or miniz backend | Small vendoring surface; ImportC compatibility untested | Inflate speed and Adler-32 control depend on the linked zlib; miniz drops SPNG_CTX_IGNORE_ADLER32 |
Sources
- libspng source at
46c81dc2b:spng/spng.c,spng/spng.h, thedocs/directory,README.md,SECURITY.md, thetests/tree andtests/ossfuzz.sh. - Upstream history read through the GitHub API on November 14 and rechecked on November 15, 2026: commits, releases, issue 196, issue 275.
- OSV records of the OSS-Fuzz issues: OSV-2020-227, OSV-2020-200, OSV-2020-360, and the OSV query.
- The PNG specification, third edition: W3C PNG third edition.
- Sparkles context: existing code baseline, shaders and SIMD baseline, event-horizon baseline, hostile-input papers, concepts; sibling deep-dives libpng, Wuffs, stb_image, image-rs, zune-image (which benchmarks against libspng) and Skia (whose PNG codecs lack CICP in libpng mode).
- Probes: two C programs compiled against the pinned
spng.cwith zlib 1.3.2, GCC 14.3.0,-fsanitize=address,undefined, in/tmp/spng-probe; not committed. The verifier rebuilt them with GCC 16.2.0 and the same zlib and sanitizer flags in/tmp/libspng-verify-epypAAand ran./probe,./probe2and a follow-up./extra, usingnix develop -c bash -c '…'from the repository root for compilation. A second verifier's independent probe,v.candv2.cin/tmp/spng-claude-verify-qh3OpF, was built the same way. These results have no committed CI fixture.