diff --git a/crates/core/src/pdf/write/png.rs b/crates/core/src/pdf/write/png.rs index 58ca7346..2d431e84 100644 --- a/crates/core/src/pdf/write/png.rs +++ b/crates/core/src/pdf/write/png.rs @@ -1 +1,1181 @@ -//! R4.5a: the branding PNG gate and the UPNG-rule decoder. Written by the R4.5a PNG track. +//! The two PNG behaviours of the TypeScript export (R4.5, plan §B.1.3), pinned by the O6 +//! oracle (`tests/oracles/r4/png.json`). +//! +//! [`png_issue`] is the branding gate, `pngIssue` of `packages/pdf/src/branding.ts`: it applies +//! to the base64 branding PNGs only (the serialiser calls it) and refuses before any large +//! allocation. [`decode_png`] is what pdf-lib 1.17.1 `embedPng` runs on any PNG, branding or +//! raster: `PngEmbedder.for` → `PNG.load` → `UPNG.decode`, `UPNG.toRGBA8`, then the RGB / +//! alpha split that drops an alpha plane where every alpha is 255 (@pdf-lib/upng 1.0.1). +//! +//! The decoder follows UPNG's acceptance (no CRC check, unknown and ancillary chunks ignored, +//! the last IHDR wins, the zlib header and the Adler-32 never read, bytes after IEND ignored) +//! and its arithmetic on JavaScript typed arrays (`JsBytes`: a read outside the array is +//! `undefined`, a write outside is dropped, a NaN is stored as 0), deviations included: the +//! Average first row of a later Adam7 pass unfiltered at absolute offsets (`first_row_fixup`), +//! the 16-bit grey key compared at `off + x` (`grey_sample`), 16-bit samples reduced to their +//! high byte, a grey key of 0 ignored (`grey_key`). Where UPNG has no bound it gets one: the +//! gate's caps before the scanline buffer or the bitmap exist, the IDAT stream inflated into +//! that buffer and never past it, every loop bounded by the buffer or the declared sizes. What +//! UPNG would hang or throw on, or what no export can use, is an error ([`PngDecodeError`]). +#![cfg(feature = "pdf-read")] + +use std::ops::Range; + +use miniz_oxide::inflate::TINFLStatus; +use miniz_oxide::inflate::core::{DecompressorOxide, decompress, inflate_flags}; + +use crate::pdf::read::MAX_ATTACHMENT_BYTES; +use crate::pdf::read::inflate::inflate_flate; + +/// `PNG_CAPS.side`: a logo is at most 8192 px a side (STEP6 §15). +pub const PNG_MAX_SIDE: u64 = 8192; +/// `PNG_CAPS.pixels`: at most 4096² pixels (64 MiB of RGBA). +pub const PNG_MAX_PIXELS: u64 = 4096 * 4096; +/// `PNG_CAPS.rawBytes`: the raw (filtered, uncompressed) scanlines fit the attachment cap. +pub const PNG_MAX_RAW_BYTES: u64 = MAX_ATTACHMENT_BYTES as u64; + +const SIGNATURE: [u8; 8] = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]; +/// A chunk is length (4), type (4), data (length), CRC (4). +const CHUNK_HEAD: u64 = 8; +const CHUNK_OVERHEAD: u64 = 12; +const IHDR_LENGTH: u32 = 13; +const IHDR_END: u64 = SIGNATURE.len() as u64 + CHUNK_OVERHEAD + IHDR_LENGTH as u64; + +/// The chunk types the gate lets through (`ALLOWED_CHUNKS`): UPNG reads them by fixed offsets +/// or by declared length. A second IHDR, APNG chunks and every text or profile chunk (read +/// with `_bin.nextZero`, which never stops without a NUL) stay out. +const GATE_CHUNKS: [&[u8; 4]; 10] = [ + b"PLTE", b"tRNS", b"IDAT", b"IEND", b"gAMA", b"cHRM", b"sRGB", b"sBIT", b"pHYs", b"bKGD", +]; + +/// Why the gate refuses a PNG (`PngIssue` of `branding.ts`). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PngIssue { + Signature, + Dimensions, + Ihdr, + Chunks, + Idat, +} + +impl PngIssue { + /// The TypeScript spelling. + pub fn as_str(self) -> &'static str { + match self { + PngIssue::Signature => "signature", + PngIssue::Dimensions => "dimensions", + PngIssue::Ihdr => "ihdr", + PngIssue::Chunks => "chunks", + PngIssue::Idat => "idat", + } + } +} + +/// What `embedPng` holds: the RGB plane (`width * height * 3`) and the alpha plane +/// (`width * height`), `None` when every alpha is 255. Always 8 bits, DeviceRGB. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct DecodedPng { + pub width: u32, + pub height: u32, + pub rgb: Vec, + pub alpha: Option>, +} + +/// Why [`decode_png`] gives no image. pdf-lib throws at `Signature`, `ColourType` and `Palette` +/// as well; the other kinds are where UPNG has no bound (it hangs, reads past its buffers or +/// allocates whatever IHDR claims) or throws on crafted input, refused here instead. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PngDecodeError { + /// The 8-byte signature differs: UPNG throws "The input is not a PNG file!". + Signature, + /// No IHDR, or one cut by the end of the buffer (UPNG reads `undefined` fields and sizes + /// its buffers with NaN). + Header, + /// A colour type outside 0, 2, 3, 4, 6: pdf-lib's `getImageType` throws. + ColourType, + /// A side of 0 (UPNG returns an empty image the page would scale by Infinity) or sizes + /// over the gate's caps (UPNG has none): refused before any allocation. + Dimensions, + /// An `acTL` chunk. pdf-lib refuses two frames or more ("Animated PNGs are not + /// supported") and none (`frames[0]` undefined); a single frame, which UPNG draws over a + /// transparent canvas with its blend op, is refused too: the gate keeps APNG chunks out + /// of the branding PNGs and no rasteriser writes one. + Animated, + /// A chunk UPNG never returns from or throws on: `tEXt` / `iTXt` whose NUL search runs + /// off the buffer (`_bin.nextZero` spins), `iTXt` text past the buffer (`readUTF8` calls + /// `toString` on `undefined`), `hIST` before any `PLTE`, `fdAT` data before any `acTL`. + Chunk, + /// The IDAT stream fails before it fills the scanline buffer (corrupt, truncated, a + /// distance before the output start, a stored block whose NLEN is wrong): UPNG's + /// `inflateRaw` checks none of it and loops, throws or writes zeros. + Inflate, + /// Colour type 3 without PLTE: `toRGBA8` reads `p[cj]` on `undefined` (TypeError). + Palette, +} + +impl PngDecodeError { + pub fn as_str(self) -> &'static str { + match self { + PngDecodeError::Signature => "signature", + PngDecodeError::Header => "header", + PngDecodeError::ColourType => "colour_type", + PngDecodeError::Dimensions => "dimensions", + PngDecodeError::Animated => "animated", + PngDecodeError::Chunk => "chunk", + PngDecodeError::Inflate => "inflate", + PngDecodeError::Palette => "palette", + } + } +} + +/// The 13 IHDR fields, read as announced. +#[derive(Debug, Clone, Copy)] +struct Header { + width: u32, + height: u32, + depth: u8, + colour_type: u8, + compression: u8, + filter: u8, + interlace: u8, +} + +impl Header { + /// The fields at `at` (`UPNG.decode._IHDR` reads 13 bytes whatever the declared length); + /// `None` when the buffer ends before them. + fn read(bytes: &[u8], at: u64) -> Option
{ + let start = usize::try_from(at).ok()?; + let fields = bytes.get(start..start.checked_add(13)?)?; + let &[ + w0, + w1, + w2, + w3, + h0, + h1, + h2, + h3, + depth, + colour_type, + compression, + filter, + interlace, + ] = fields + else { + return None; + }; + Some(Header { + width: u32::from_be_bytes([w0, w1, w2, w3]), + height: u32::from_be_bytes([h0, h1, h2, h3]), + depth, + colour_type, + compression, + filter, + interlace, + }) + } +} + +// --------------------------------------------------------------------------------------------- +// The gate (`pngIssue`) +// --------------------------------------------------------------------------------------------- + +/// Why a branding PNG is refused before it is decoded, or `None` when it may be. In order: +/// the signature and a first 13-byte IHDR (`signature`); the announced size under the caps +/// (`dimensions`); IHDR fields the spec allows, non-interlaced (`ihdr`); the raw scanline size +/// under the cap (`dimensions`); an allowlisted chunk list closed by an empty IEND that ends +/// the buffer (`chunks`); an IDAT stream the bounded inflater accepts and that yields exactly +/// the raw scanline size (`idat`). +pub fn png_issue(bytes: &[u8]) -> Option { + if (bytes.len() as u64) < IHDR_END || !bytes.starts_with(&SIGNATURE) { + return Some(PngIssue::Signature); + } + if be_u32(bytes, 8) != Some(IHDR_LENGTH) || bytes.get(12..16) != Some(&b"IHDR"[..]) { + return Some(PngIssue::Signature); + } + let Some(header) = Header::read(bytes, 16) else { + return Some(PngIssue::Signature); + }; + if let Some(issue) = header_issue(&header) { + return Some(issue); + } + let raw_size = gate_raw_size(&header); + if raw_size > PNG_MAX_RAW_BYTES { + return Some(PngIssue::Dimensions); + } + chunk_list_issue(bytes).or_else(|| idat_issue(bytes, raw_size)) +} + +/// `headerIssue`: the size budget, then the IHDR fields (an unknown colour type or depth has +/// no raw size to check against; an interlaced image is refused outright). +fn header_issue(header: &Header) -> Option { + let (width, height) = (u64::from(header.width), u64::from(header.height)); + if width < 1 || height < 1 || width > PNG_MAX_SIDE || height > PNG_MAX_SIDE { + return Some(PngIssue::Dimensions); + } + if width * height > PNG_MAX_PIXELS { + return Some(PngIssue::Dimensions); + } + if !spec_depths(header.colour_type).contains(&header.depth) { + return Some(PngIssue::Ihdr); + } + if header.compression != 0 || header.filter != 0 || header.interlace != 0 { + return Some(PngIssue::Ihdr); + } + None +} + +/// `DEPTHS`: the bit depths the PNG spec allows, by colour type. +fn spec_depths(colour_type: u8) -> &'static [u8] { + match colour_type { + 0 => &[1, 2, 4, 8, 16], + 2 | 4 | 6 => &[8, 16], + 3 => &[1, 2, 4, 8], + _ => &[], + } +} + +/// `expectedRawSize`: each scanline is a filter byte plus its packed samples (the header is +/// known sound: a side is at most 8192, a pixel at most 64 bits). +fn gate_raw_size(header: &Header) -> u64 { + let channels = upng_channels(header.colour_type).unwrap_or(0); + let bits_per_pixel = channels * u64::from(header.depth); + let row_bytes = (u64::from(header.width) * bits_per_pixel).div_ceil(8) + 1; + row_bytes * u64::from(header.height) +} + +/// `chunkListIssue`: walks the chunks after the first IHDR to an IEND of length 0 that ends +/// the buffer exactly; a type off the list, a length past the buffer, a truncated chunk head, +/// a missing IEND or bytes after it are refused. +fn chunk_list_issue(bytes: &[u8]) -> Option { + let total = bytes.len() as u64; + let mut offset = IHDR_END; + while offset + CHUNK_HEAD <= total { + let Some(length) = be_u32(bytes, offset) else { + return Some(PngIssue::Chunks); + }; + let kind = quad(bytes, offset + 4); + let next = offset + CHUNK_OVERHEAD + u64::from(length); + if next > total || !GATE_CHUNKS.contains(&&kind) { + return Some(PngIssue::Chunks); + } + if &kind == b"IEND" { + return (length != 0 || next != total).then_some(PngIssue::Chunks); + } + offset = next; + } + Some(PngIssue::Chunks) +} + +/// `idatIssue`: the IDAT stream inflated by the native, bounded inflater (zlib framing, +/// Adler-32 checked) must give exactly the raw scanline size. +fn idat_issue(bytes: &[u8], raw_size: u64) -> Option { + let Ok(limit) = usize::try_from(raw_size) else { + return Some(PngIssue::Idat); + }; + match inflate_flate(&gate_idat_payload(bytes), limit) { + Some(raw) if raw.len() == limit => None, + _ => Some(PngIssue::Idat), + } +} + +/// `idatPayload`: the IDAT payloads in file order, one zlib stream (the chunk list is known +/// sound, so the payload is at most the file). +fn gate_idat_payload(bytes: &[u8]) -> Vec { + let total = bytes.len() as u64; + let mut payload = Vec::new(); + let mut offset = IHDR_END; + while offset + CHUNK_HEAD <= total { + let length = u64::from(be_u32(bytes, offset).unwrap_or(0)); + if &quad(bytes, offset + 4) == b"IDAT" { + let data = clamp_range(offset + CHUNK_HEAD, length, bytes.len()); + payload.extend_from_slice(bytes.get(data).unwrap_or(&[])); + } + offset += CHUNK_OVERHEAD + length; + } + payload +} + +// --------------------------------------------------------------------------------------------- +// Byte reading +// --------------------------------------------------------------------------------------------- + +/// The big-endian uint32 at `at`, `None` past the buffer. +fn be_u32(bytes: &[u8], at: u64) -> Option { + let start = usize::try_from(at).ok()?; + let word = bytes.get(start..start.checked_add(4)?)?; + Some(u32::from_be_bytes(word.try_into().ok()?)) +} + +/// The file byte at `at`, 0 past the buffer: what `undefined` becomes in UPNG's bit +/// operations (`readUint`, `readUshort`) and in `String.fromCharCode` (`readASCII`). +fn file_byte(bytes: &[u8], at: u64) -> u8 { + usize::try_from(at) + .ok() + .and_then(|i| bytes.get(i)) + .copied() + .unwrap_or(0) +} + +/// Four bytes at `at` (a chunk type, or the bytes of `readUint`), missing ones as 0. +fn quad(bytes: &[u8], at: u64) -> [u8; 4] { + [0, 1, 2, 3].map(|k| file_byte(bytes, at + k)) +} + +/// `_bin.readUshort(data, at)`: `(data[at] << 8) | data[at + 1]`, missing bytes as 0. +fn js_ushort(bytes: &[u8], at: u64) -> i64 { + (i64::from(file_byte(bytes, at)) << 8) | i64::from(file_byte(bytes, at + 1)) +} + +/// The part of `[start, start + length)` inside `[0, len)`. +fn clamp_range(start: u64, length: u64, len: usize) -> Range { + let end_of = |at: u64| usize::try_from(at).map_or(len, |at| at.min(len)); + end_of(start)..end_of(start.saturating_add(length)) +} + +/// `_bin.nextZero(data, from)`: the first NUL at or after `from`, `None` where UPNG's +/// `while (data[p] != 0) p++` never ends. +fn next_zero(bytes: &[u8], from: u64) -> Option { + let rest = bytes.get(usize::try_from(from).ok()?..)?; + let found = rest.iter().position(|&byte| byte == 0)?; + Some(from + found as u64) +} + +// --------------------------------------------------------------------------------------------- +// The decoder: `UPNG.decode` +// --------------------------------------------------------------------------------------------- + +/// The RGB and alpha planes pdf-lib 1.17.1 embeds for `bytes`: `UPNG.decode`, `toRGBA8`, +/// `splitAlphaChannel`, the alpha plane dropped when no alpha is below 255. +pub fn decode_png(bytes: &[u8]) -> Result { + if !bytes.starts_with(&SIGNATURE) { + return Err(PngDecodeError::Signature); + } + let walk = walk_chunks(bytes)?; + let header = walk.header.ok_or(PngDecodeError::Header)?; + let geometry = Geometry::checked(&header)?; + if walk.animated { + return Err(PngDecodeError::Animated); + } + let raw = upng_inflate(&walk.idat, geometry.buffer_bytes)?; + let pixels = unfilter(JsBytes(raw), &geometry); + let planes = decode_image(&pixels, &geometry, &walk.tables)?; + Ok(planes.finish(&header)) +} + +/// The palette and the transparency key, as `out.tabs` holds them. +#[derive(Debug, Default)] +struct Tables { + /// `PLTE`: `readBytes(data, offset, len)`, the part inside the buffer (entries past it + /// are `undefined`, drawn as 0 like the entries past the array). + palette: Option>, + transparency: Option, +} + +/// `tRNS` as UPNG stores it, by the colour type of the IHDR read before it. +#[derive(Debug, Clone)] +enum Transparency { + /// Colour type 3: `readBytes`, an array of the declared length whose entries past the + /// buffer are `undefined`; `bytes` is the part inside the buffer. + Palette { bytes: Vec, declared: u32 }, + /// Colour type 0: `readUshort`. + Grey(i64), + /// Colour type 2: three `readUshort`. + Rgb([i64; 3]), +} + +/// A chunk's data position and declared length. +#[derive(Debug, Clone, Copy)] +struct ChunkAt { + at: u64, + length: u32, +} + +/// What the chunk loop of `UPNG.decode` leaves: the last IHDR, `dd` (the IDAT payloads in +/// file order in a buffer of the file's length, the rest zero), the tables, `acTL` seen. +#[derive(Debug)] +struct Walk { + header: Option
, + idat: Vec, + /// `doff`: where the next IDAT payload goes (may pass the buffer: writes are dropped). + idat_end: u64, + tables: Tables, + animated: bool, +} + +/// The chunk loop of `UPNG.decode`: `while (offset < data.length)`, a length and a type read +/// with missing bytes as 0, the CRC skipped unread, stopped by IEND. Each turn advances by at +/// least 12 bytes, so the loop is bounded by the buffer. +fn walk_chunks(bytes: &[u8]) -> Result { + let total = bytes.len() as u64; + let mut walk = Walk { + header: None, + idat: vec![0; bytes.len()], + idat_end: 0, + tables: Tables::default(), + animated: false, + }; + let mut offset = SIGNATURE.len() as u64; + while offset < total { + let length = u32::from_be_bytes(quad(bytes, offset)); + let chunk = ChunkAt { + at: offset + CHUNK_HEAD, + length, + }; + let kind = quad(bytes, offset + 4); + if &kind == b"IEND" { + break; + } + walk.read_chunk(bytes, &kind, chunk)?; + offset = chunk.at + u64::from(length) + 4; + } + Ok(walk) +} + +impl Walk { + /// One chunk of the loop, the types UPNG reads; the others (`pHYs`, `cHRM`, `gAMA`, + /// `sRGB`, `bKGD`, `fcTL` without frame data, unknown types) change nothing it decodes. + fn read_chunk( + &mut self, + bytes: &[u8], + kind: &[u8; 4], + chunk: ChunkAt, + ) -> Result<(), PngDecodeError> { + match kind { + b"IHDR" => { + let header = Header::read(bytes, chunk.at).ok_or(PngDecodeError::Header)?; + self.header = Some(header); + } + b"IDAT" => self.append_idat(bytes, chunk), + b"acTL" => self.animated = true, + // `fd[foff + i]` on a frame buffer `acTL` never allocated (TypeError), or a + // negative `foff` that fails at the end; a length of 4 copies nothing. + b"fdAT" if chunk.length != 4 && !self.animated => { + return Err(PngDecodeError::Chunk); + } + b"tEXt" => { + next_zero(bytes, chunk.at).ok_or(PngDecodeError::Chunk)?; + } + b"iTXt" => international_text_returns(bytes, chunk)?, + b"PLTE" => self.tables.palette = Some(in_buffer(bytes, chunk).to_vec()), + b"hIST" if self.tables.palette.is_none() => return Err(PngDecodeError::Chunk), + b"tRNS" => self.read_transparency(bytes, chunk), + _ => {} + } + Ok(()) + } + + /// `for (i < len) dd[doff + i] = data[offset + i]; doff += len`: bytes past the file are + /// `undefined`, stored as 0 (`dd` starts zeroed), positions past `dd` are dropped. + fn append_idat(&mut self, bytes: &[u8], chunk: ChunkAt) { + let length = u64::from(chunk.length); + let target = clamp_range(self.idat_end, length, self.idat.len()); + let source = clamp_range(chunk.at, length, bytes.len()); + let count = target.len().min(source.len()); + let to = self.idat.get_mut(target.start..target.start + count); + if let (Some(to), Some(from)) = (to, bytes.get(source.start..source.start + count)) { + to.copy_from_slice(from); + } + self.idat_end = self.idat_end.saturating_add(length); + } + + /// `tRNS` by `out.ctype` at the time it is read: nothing before an IHDR or for the colour + /// types 4 and 6 (an earlier table is kept). + fn read_transparency(&mut self, bytes: &[u8], chunk: ChunkAt) { + let table = match self.header.map(|header| header.colour_type) { + Some(3) => Transparency::Palette { + bytes: in_buffer(bytes, chunk).to_vec(), + declared: chunk.length, + }, + Some(0) => Transparency::Grey(js_ushort(bytes, chunk.at)), + Some(2) => Transparency::Rgb([0, 2, 4].map(|k| js_ushort(bytes, chunk.at + k))), + _ => return, + }; + self.tables.transparency = Some(table); + } +} + +/// The part of a chunk's declared data inside the buffer. +fn in_buffer(bytes: &[u8], chunk: ChunkAt) -> &[u8] { + let range = clamp_range(chunk.at, u64::from(chunk.length), bytes.len()); + bytes.get(range).unwrap_or(&[]) +} + +/// Whether the `iTXt` branch of `UPNG.decode` returns: three NUL searches (keyword, then +/// past the two flag bytes the language tag, then the translated keyword), then `readUTF8` +/// over the rest of the declared length, which throws on a byte past the buffer. +fn international_text_returns(bytes: &[u8], chunk: ChunkAt) -> Result<(), PngDecodeError> { + let spins = PngDecodeError::Chunk; + let keyword_end = next_zero(bytes, chunk.at).ok_or(spins)?; + let language_end = next_zero(bytes, keyword_end + 3).ok_or(spins)?; + let text_at = next_zero(bytes, language_end + 1).ok_or(spins)? + 1; + let text_end = chunk.at + u64::from(chunk.length); + if text_end > text_at && text_end > bytes.len() as u64 { + return Err(PngDecodeError::Chunk); + } + Ok(()) +} + +// --------------------------------------------------------------------------------------------- +// Sizes and inflation: `UPNG.decode._decompress` +// --------------------------------------------------------------------------------------------- + +/// `UPNG.decode._getBPP`: `[1, null, 3, 1, 2, null, 4][ctype]` samples per pixel; the colour +/// types 1 and 5 (0 bits) and 7 and above (NaN) end in pdf-lib's `getImageType` throw. +fn upng_channels(colour_type: u8) -> Option { + match colour_type { + 0 | 3 => Some(1), + 2 => Some(3), + 4 => Some(2), + 6 => Some(4), + _ => None, + } +} + +/// The sizes UPNG derives from the last IHDR, as JavaScript numbers (every value is bounded +/// by the caps, far inside `i64`). +#[derive(Debug, Clone, Copy)] +struct Geometry { + width: i64, + height: i64, + depth: i64, + colour_type: u8, + interlace: u8, + bits_per_pixel: i64, + /// `bpl = Math.ceil(w * bpp / 8)`. + row_bytes: i64, + area: usize, + /// `(bpl + 1 + out.interlace) * h`, the buffer `_decompress` inflates into. + buffer_bytes: usize, +} + +impl Geometry { + /// The sizes, refused over the gate's caps before the scanline buffer or the bitmap is + /// allocated (`side`, `pixels`, and `rawBytes` on UPNG's own buffer size). + fn checked(header: &Header) -> Result { + let too_large = PngDecodeError::Dimensions; + let channels = upng_channels(header.colour_type).ok_or(PngDecodeError::ColourType)?; + let (width, height) = (u64::from(header.width), u64::from(header.height)); + if width == 0 || height == 0 || width > PNG_MAX_SIDE || height > PNG_MAX_SIDE { + return Err(too_large); + } + let bits_per_pixel = channels * u64::from(header.depth); + let row_bytes = (width * bits_per_pixel).div_ceil(8); + let buffer = (row_bytes + 1 + u64::from(header.interlace)) * height; + if width * height > PNG_MAX_PIXELS || buffer > PNG_MAX_RAW_BYTES { + return Err(too_large); + } + Ok(Geometry { + width: i64::from(header.width), + height: i64::from(header.height), + depth: i64::from(header.depth), + colour_type: header.colour_type, + interlace: header.interlace, + bits_per_pixel: i64::try_from(bits_per_pixel).map_err(|_| too_large)?, + row_bytes: i64::try_from(row_bytes).map_err(|_| too_large)?, + area: usize::try_from(width * height).map_err(|_| too_large)?, + buffer_bytes: usize::try_from(buffer).map_err(|_| too_large)?, + }) + } +} + +/// `UPNG.decode._inflate`: `inflateRaw(new Uint8Array(dd.buffer, 2, dd.length - 6), buff)`. +/// The two zlib header bytes are skipped unread, the raw deflate stream runs into the zero +/// tail of `dd` and stops at its final block (the Adler-32 and anything after are ignored). +/// The output buffer is UPNG's: never grown, the output past it dropped, a shorter output +/// returned short (`W.slice(0, w)`); `03 00` (an empty fixed block) returns the whole zeroed +/// buffer, UPNG's shortcut. Where UPNG's unbounded inflater would loop, throw or invent zeros +/// on a broken stream, this one fails ([`PngDecodeError::Inflate`]); a stored block running +/// past the buffer, a `RangeError` in UPNG, is kept truncated like a Huffman block. +fn upng_inflate(idat: &[u8], capacity: usize) -> Result, PngDecodeError> { + let end = idat.len().saturating_sub(4); + let input = idat.get(2..end).ok_or(PngDecodeError::Inflate)?; + let mut out = vec![0; capacity]; + if input.starts_with(&[3, 0]) { + return Ok(out); + } + let mut decompressor = Box::::default(); + let flags = inflate_flags::TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; + let (status, _, produced) = decompress(&mut decompressor, input, &mut out, 0, flags); + match status { + TINFLStatus::Done | TINFLStatus::HasMoreOutput => { + out.truncate(produced); + Ok(out) + } + _ => Err(PngDecodeError::Inflate), + } +} + +// --------------------------------------------------------------------------------------------- +// JavaScript typed-array semantics +// --------------------------------------------------------------------------------------------- + +/// A UPNG `Uint8Array` seen through JavaScript: a read outside is `undefined` (`None`), a +/// write outside is dropped, a number is stored modulo 256 and a NaN (`None`) as 0. +#[derive(Debug)] +struct JsBytes(Vec); + +impl JsBytes { + fn get(&self, index: i64) -> Option { + let at = usize::try_from(index).ok()?; + self.0.get(at).map(|&byte| i64::from(byte)) + } + + /// The byte as a bit operation sees it: `undefined` is 0. + fn bits(&self, index: i64) -> i64 { + self.get(index).unwrap_or(0) + } + + /// The byte as a store into a `Uint8Array` leaves it: `undefined` is 0. + fn byte(&self, index: i64) -> u8 { + self.get(index).map_or(0, |value| value as u8) + } + + /// `_bin.readUshort`: `(b[p] << 8) | b[p + 1]`, never NaN. + fn ushort(&self, index: i64) -> i64 { + (self.bits(index) << 8) | self.bits(index + 1) + } + + fn set(&mut self, index: i64, value: Option) { + let Ok(at) = usize::try_from(index) else { + return; + }; + if let Some(slot) = self.0.get_mut(at) { + *slot = value.map_or(0, |value| (value & 0xff) as u8); + } + } +} + +/// JavaScript `a + b` on bytes that may be `undefined` (NaN). +fn js_add(a: Option, b: Option) -> Option { + Some(a? + b?) +} + +/// JavaScript `v >>> 1`: NaN becomes 0 (operands here are never negative). +fn js_half(value: Option) -> i64 { + value.map_or(0, |value| value >> 1) +} + +/// `UPNG.decode._paeth` on JavaScript numbers: with a NaN operand every comparison is false +/// and `c` is returned, whatever it is. +fn paeth(a: Option, b: Option, c: Option) -> Option { + let (Some(av), Some(bv), Some(cv)) = (a, b, c) else { + return c; + }; + let p = av + bv - cv; + let (pa, pb, pc) = ((p - av).pow(2), (p - bv).pow(2), (p - cv).pow(2)); + if pa <= pb && pa <= pc { + a + } else if pb <= pc { + b + } else { + c + } +} + +// --------------------------------------------------------------------------------------------- +// Unfiltering: `_filterZero`, `_readInterlace` +// --------------------------------------------------------------------------------------------- + +/// `_decompress` after the inflation: `_filterZero` for interlace 0, `_readInterlace` for 1, +/// and for any other interlace byte the inflated bytes as they are (filter bytes included). +fn unfilter(mut data: JsBytes, geometry: &Geometry) -> JsBytes { + let whole = Pass { + offset: 0, + width: geometry.width, + height: geometry.height, + }; + match geometry.interlace { + 0 => filter_zero(&mut data, geometry.bits_per_pixel, whole), + 1 => return JsBytes(read_interlace(&mut data, geometry)), + _ => {} + } + data +} + +/// The rows `_filterZero` unfilters: the image, or one Adam7 pass at `offset`. +#[derive(Debug, Clone, Copy)] +struct Pass { + offset: i64, + width: i64, + height: i64, +} + +/// One row: unfiltered bytes written at `at`, filtered bytes read from `from` (`at + y + 1`: +/// the rows are compacted in place over their filter bytes). +#[derive(Debug, Clone, Copy)] +struct Row { + at: i64, + from: i64, + row_bytes: i64, + /// `Math.ceil(bpp / 8)`: the left neighbour distance, 1 below 8 bits. + step: i64, +} + +/// `UPNG.decode._filterZero(data, out, off, w, h)`, in place. It runs for every Adam7 pass, +/// empty ones included (their first-row fixup touches the next pass's filter byte). +fn filter_zero(data: &mut JsBytes, bits_per_pixel: i64, pass: Pass) { + let row_bytes = (pass.width * bits_per_pixel + 7).div_euclid(8); + let step = (bits_per_pixel + 7).div_euclid(8); + first_row_fixup(data, pass.offset, step, row_bytes); + for y in 0..pass.height { + let at = pass.offset + y * row_bytes; + let row = Row { + at, + from: at + y + 1, + row_bytes, + step, + }; + match data.get(row.from - 1) { + Some(0) => row_none(data, row), + Some(1) => row_sub(data, row), + Some(2) => row_up(data, row), + Some(3) => row_average(data, row), + _ => row_paeth(data, row), + } + } +} + +/// The first row of `_filterZero`: a filter type above 1 is rewritten in place, +/// `[0, 0, 1][type - 2]` (Up and Average become None, Paeth becomes Sub, 5 and above +/// `undefined`, stored as 0), and an Average row is first summed with its left neighbours +/// (`data[x + 1] += data[x + 1 - bpp] >>> 1`) at absolute offsets `x + 1`. That is right for +/// the image and the first Adam7 pass (offset 0); for a later pass it rewrites bytes of the +/// first pass and leaves the pass's own row as None: UPNG's deviation, kept. +fn first_row_fixup(data: &mut JsBytes, offset: i64, step: i64, row_bytes: i64) { + let Some(kind) = data.get(offset).filter(|&kind| kind > 1) else { + return; + }; + let rewritten = match kind { + 2 | 3 => Some(0), + 4 => Some(1), + _ => None, + }; + data.set(offset, rewritten); + if kind == 3 { + for x in step..row_bytes { + let sum = js_add(data.get(x + 1), Some(js_half(data.get(x + 1 - step)))); + data.set(x + 1, Some(sum.map_or(0, |value| value & 255))); + } + } +} + +fn row_none(data: &mut JsBytes, row: Row) { + for x in 0..row.row_bytes { + data.set(row.at + x, data.get(row.from + x)); + } +} + +/// Sub. The first loop runs to `bpp` even past `bpl` (an empty pass row), as in UPNG. +fn row_sub(data: &mut JsBytes, row: Row) { + for x in 0..row.step { + data.set(row.at + x, data.get(row.from + x)); + } + for x in row.step..row.row_bytes { + let left = data.get(row.at + x - row.step); + data.set(row.at + x, js_add(data.get(row.from + x), left)); + } +} + +fn row_up(data: &mut JsBytes, row: Row) { + for x in 0..row.row_bytes { + let up = data.get(row.at + x - row.row_bytes); + data.set(row.at + x, js_add(data.get(row.from + x), up)); + } +} + +fn row_average(data: &mut JsBytes, row: Row) { + for x in 0..row.step { + let up = js_half(data.get(row.at + x - row.row_bytes)); + data.set(row.at + x, js_add(data.get(row.from + x), Some(up))); + } + for x in row.step..row.row_bytes { + let left = data.get(row.at + x - row.step); + let mean = js_half(js_add(data.get(row.at + x - row.row_bytes), left)); + data.set(row.at + x, js_add(data.get(row.from + x), Some(mean))); + } +} + +/// Paeth, and every type UPNG does not name (5 to 255, `undefined` past the buffer). +fn row_paeth(data: &mut JsBytes, row: Row) { + for x in 0..row.step { + let up = data.get(row.at + x - row.row_bytes); + let predictor = paeth(Some(0), up, Some(0)); + data.set(row.at + x, js_add(data.get(row.from + x), predictor)); + } + for x in row.step..row.row_bytes { + let at = row.at + x; + let predictor = paeth( + data.get(at - row.step), + data.get(at - row.row_bytes), + data.get(at - row.step - row.row_bytes), + ); + data.set(at, js_add(data.get(row.from + x), predictor)); + } +} + +/// The Adam7 passes of `_readInterlace`: starting row, starting column, row and column steps. +const ADAM7: [(i64, i64, usize, usize); 7] = [ + (0, 0, 8, 8), + (0, 4, 8, 8), + (4, 0, 8, 4), + (0, 2, 4, 4), + (2, 0, 4, 2), + (0, 1, 2, 2), + (1, 0, 2, 1), +]; + +/// `UPNG.decode._readInterlace`: each pass unfiltered in place at its offset, then its pixels +/// copied into a non-interlaced image of `h * bpl` bytes. The offset advances only past a +/// non-empty pass. +fn read_interlace(data: &mut JsBytes, geometry: &Geometry) -> Vec { + let size = usize::try_from(geometry.height * geometry.row_bytes).unwrap_or(0); + let mut image = vec![0; size]; + let mut offset = 0; + for (row0, col0, row_step, col_step) in ADAM7 { + let width = pass_count(geometry.width, col0, col_step); + let height = pass_count(geometry.height, row0, row_step); + let pass_row_bytes = (width * geometry.bits_per_pixel + 7).div_euclid(8); + filter_zero( + data, + geometry.bits_per_pixel, + Pass { + offset, + width, + height, + }, + ); + let rows = (row0..geometry.height).step_by(row_step).enumerate(); + for (y, row) in rows { + let bit = (offset + y as i64 * pass_row_bytes) << 3; + let target = Target { + row, + col0, + col_step, + bit, + }; + copy_pass_row(&mut image, data, geometry, target); + } + if width * height != 0 { + offset += height * (1 + pass_row_bytes); + } + } + image +} + +/// `while (cc < w) { cc += ci; sw++ }`: the pixels of a pass along one side. +fn pass_count(size: i64, start: i64, step: usize) -> i64 { + let step = step as i64; + if size > start { + (size - start + step - 1) / step + } else { + 0 + } +} + +/// Where one pass row lands: the image row, its columns, the pass's first bit (`cdi`). +#[derive(Debug, Clone, Copy)] +struct Target { + row: i64, + col0: i64, + col_step: usize, + bit: i64, +} + +/// The inner loop of `_readInterlace`: one pass row placed pixel by pixel. Packed depths are +/// OR-ed in by their bit position (`img[...] |= val << shift`), 8 bits and more copy `bpp >> 3` +/// bytes; any other pixel size (3, 5, 6, 7 bits) is not copied. +fn copy_pass_row(image: &mut [u8], data: &JsBytes, geometry: &Geometry, target: Target) { + let bpp = geometry.bits_per_pixel; + let line = target.row * geometry.row_bytes; + let columns = (target.col0..geometry.width) + .step_by(target.col_step) + .enumerate(); + for (k, col) in columns { + let bit = target.bit + k as i64 * bpp; + if matches!(bpp, 1 | 2 | 4) { + let value = packed_at(data, bit, bpp); + let per_byte = 8 / bpp; + let shift = 8 - bpp - (col % per_byte) * bpp; + if let Some(slot) = js_index(image, line + col / per_byte) { + *slot |= (value << shift) as u8; + } + } else if bpp >= 8 { + for j in 0..bpp >> 3 { + if let Some(slot) = js_index(image, line + col * (bpp >> 3) + j) { + *slot = data.byte((bit >> 3) + j); + } + } + } + } +} + +/// A slot of a typed array, `None` outside (the write is dropped). +fn js_index(bytes: &mut [u8], index: i64) -> Option<&mut u8> { + bytes.get_mut(usize::try_from(index).ok()?) +} + +/// `(data[bit >> 3] >> (8 - depth - (bit & 7))) & mask`: the packed sample at `bit` +/// (`undefined` is 0). +fn packed_at(data: &JsBytes, bit: i64, depth: i64) -> i64 { + let shift = 8 - depth - (bit & 7); + (data.bits(bit >> 3) >> shift.max(0)) & ((1 << depth) - 1) +} + +// --------------------------------------------------------------------------------------------- +// Pixels: `UPNG.toRGBA8.decodeImage`, then pdf-lib's split +// --------------------------------------------------------------------------------------------- + +/// The RGB and alpha planes `splitAlphaChannel` cuts from `toRGBA8`'s RGBA bitmap, written +/// directly. They start at zero: a depth `decodeImage` has no branch for leaves the bitmap +/// zero (black, alpha 0), as in UPNG. +struct Planes { + rgb: Vec, + alpha: Vec, +} + +impl Planes { + fn new(area: usize) -> Planes { + Planes { + rgb: vec![0; area.saturating_mul(3)], + alpha: vec![0; area], + } + } + + fn put(&mut self, pixel: usize, [red, green, blue, alpha]: [u8; 4]) { + let at = pixel.saturating_mul(3); + if let Some(rgb) = self.rgb.get_mut(at..at.saturating_add(3)) { + rgb.copy_from_slice(&[red, green, blue]); + } + if let Some(slot) = self.alpha.get_mut(pixel) { + *slot = alpha; + } + } + + /// pdf-lib keeps the alpha plane only when some alpha is below 255. + fn finish(self, header: &Header) -> DecodedPng { + let translucent = self.alpha.iter().any(|&alpha| alpha < 255); + DecodedPng { + width: header.width, + height: header.height, + rgb: self.rgb, + alpha: translucent.then_some(self.alpha), + } + } +} + +/// `UPNG.toRGBA8.decodeImage(out.data, w, h, out)` (the static path: `acTL` is refused). +fn decode_image( + data: &JsBytes, + geometry: &Geometry, + tables: &Tables, +) -> Result { + let mut planes = Planes::new(geometry.area); + let transparency = tables.transparency.as_ref(); + match geometry.colour_type { + 6 => truecolour_alpha(data, geometry, &mut planes), + 2 => truecolour(data, geometry, rgb_key(transparency), &mut planes), + 3 => indexed(data, geometry, tables, &mut planes)?, + 4 => grey_alpha(data, geometry, &mut planes), + _ => grey(data, geometry, grey_key(transparency), &mut planes), + } + Ok(planes) +} + +/// Colour type 6: `bf[i] = data[i]` (8 bits) or `data[i << 1]` (16 bits, the high byte). +fn truecolour_alpha(data: &JsBytes, geometry: &Geometry, planes: &mut Planes) { + let stride = match geometry.depth { + 8 => 1, + 16 => 2, + _ => return, + }; + for pixel in 0..geometry.area { + let at = pixel as i64 * 4 * stride; + planes.put(pixel, [0, 1, 2, 3].map(|k| data.byte(at + k * stride))); + } +} + +/// Colour type 2: opaque, or alpha 0 where the samples equal the `tRNS` key (`==` on the +/// bytes at 8 bits, an `undefined` byte never equal; `readUshort`, never NaN, at 16 bits). +fn truecolour(data: &JsBytes, geometry: &Geometry, key: Option<[i64; 3]>, planes: &mut Planes) { + let stride = match geometry.depth { + 8 => 1, + 16 => 2, + _ => return, + }; + for pixel in 0..geometry.area { + let at = pixel as i64 * 3 * stride; + let [red, green, blue] = [0, 1, 2].map(|k| data.byte(at + k * stride)); + let keyed = key.is_some_and(|key| truecolour_keyed(data, at, stride, key)); + planes.put(pixel, [red, green, blue, if keyed { 0 } else { 255 }]); + } +} + +/// `data[ti] == tr && data[ti + 1] == tg && data[ti + 2] == tb` (8 bits), or the same on +/// `readUshort(data, ti + 2k)` (16 bits). +fn truecolour_keyed(data: &JsBytes, at: i64, stride: i64, key: [i64; 3]) -> bool { + let sample = |k: i64| match stride { + 1 => data.get(at + k), + _ => Some(data.ushort(at + k * 2)), + }; + (0..3).zip(key).all(|(k, wanted)| sample(k) == Some(wanted)) +} + +/// The colour type 2 key `tr, tg, tb = ts[0], ts[1], ts[2]`: an RGB table, the first three +/// entries of a palette table read under an earlier IHDR, never a number (`ts[0]` undefined). +fn rgb_key(transparency: Option<&Transparency>) -> Option<[i64; 3]> { + match transparency? { + Transparency::Rgb(key) => Some(*key), + Transparency::Palette { bytes, declared } if *declared >= 3 => { + let [red, green, blue, ..] = bytes.as_slice() else { + return None; + }; + Some([red, green, blue].map(|&value| i64::from(value))) + } + _ => None, + } +} + +/// Colour type 3 at 1, 2, 4 or 8 bits (any other depth draws nothing); no PLTE is the +/// TypeError of `p[cj]` on `undefined`. +fn indexed( + data: &JsBytes, + geometry: &Geometry, + tables: &Tables, + planes: &mut Planes, +) -> Result<(), PngDecodeError> { + if !matches!(geometry.depth, 1 | 2 | 4 | 8) { + return Ok(()); + } + let palette = tables.palette.as_deref().ok_or(PngDecodeError::Palette)?; + let transparency = tables.transparency.as_ref(); + for pixel in 0..geometry.area { + let index = match geometry.depth { + 8 => data.get(pixel as i64), + depth => Some(row_sample(data, geometry, pixel, depth)), + }; + planes.put(pixel, palette_colour(palette, transparency, index)); + } + Ok(()) +} + +/// The packed sample of `pixel` on its row (`s0 = y * bpl`). +fn row_sample(data: &JsBytes, geometry: &Geometry, pixel: usize, depth: i64) -> i64 { + let (y, x) = (pixel as i64 / geometry.width, pixel as i64 % geometry.width); + packed_at(data, y * geometry.row_bytes * 8 + x * depth, depth) +} + +/// `bf = p[3j], p[3j + 1], p[3j + 2], (j < tl) ? ap[j] : 255`. An `undefined` index (8 bits +/// past the buffer) reads `p[NaN]` (0) and is never below `tl` (255). +fn palette_colour( + palette: &[u8], + transparency: Option<&Transparency>, + index: Option, +) -> [u8; 4] { + let Some(index) = index else { + return [0, 0, 0, 255]; + }; + let entry = |k: i64| { + let at = usize::try_from(index * 3 + k).ok(); + at.and_then(|at| palette.get(at)).copied().unwrap_or(0) + }; + [ + entry(0), + entry(1), + entry(2), + palette_alpha(transparency, index), + ] +} + +/// `tl = ap ? ap.length : 0`: a palette table's declared length (entries past the buffer +/// `undefined`, stored as 0), 3 for an RGB table (values stored modulo 256), none for a number. +fn palette_alpha(transparency: Option<&Transparency>, index: i64) -> u8 { + let slot = usize::try_from(index).unwrap_or(usize::MAX); + match transparency { + Some(Transparency::Palette { bytes, declared }) if index < i64::from(*declared) => { + bytes.get(slot).copied().unwrap_or(0) + } + Some(Transparency::Rgb(key)) if index < 3 => key.get(slot).map_or(0, |&v| v as u8), + _ => 255, + } +} + +/// Colour type 4: grey from `data[i << 1]` and alpha from `data[(i << 1) + 1]` (8 bits), or +/// the high bytes `data[i << 2]` and `data[(i << 2) + 2]` (16 bits). +fn grey_alpha(data: &JsBytes, geometry: &Geometry, planes: &mut Planes) { + let (stride, alpha_at) = match geometry.depth { + 8 => (2, 1), + 16 => (4, 2), + _ => return, + }; + for pixel in 0..geometry.area { + let at = pixel as i64 * stride; + let grey = data.byte(at); + planes.put(pixel, [grey, grey, grey, data.byte(at + alpha_at)]); + } +} + +/// Colour type 0, row by row (`off = y * bpl`); a depth with no branch draws nothing. +fn grey(data: &JsBytes, geometry: &Geometry, key: Option, planes: &mut Planes) { + if !matches!(geometry.depth, 1 | 2 | 4 | 8 | 16) { + return; + } + for pixel in 0..geometry.area { + let (y, x) = (pixel as i64 / geometry.width, pixel as i64 % geometry.width); + let at = SampleAt { + row: y * geometry.row_bytes, + x, + }; + let (value, keyed) = grey_sample(data, geometry.depth, at, key); + planes.put(pixel, [value, value, value, if keyed { 0 } else { 255 }]); + } +} + +/// A grey pixel: its row offset and column. +#[derive(Debug, Clone, Copy)] +struct SampleAt { + row: i64, + x: i64, +} + +/// One grey pixel and whether it equals the key. Packed depths scale the sample (255, 85, +/// 17) and compare it with `tr * scale`; 8 bits compares the byte (`undefined` never equal); +/// 16 bits keeps the high byte `data[off + (x << 1)]` but compares the key with +/// `readUshort(data, off + (x << i))`, `i` undefined in that branch, so at `off + x`: the +/// overlapping pair of bytes, UPNG's deviation, kept. +fn grey_sample(data: &JsBytes, depth: i64, at: SampleAt, key: Option) -> (u8, bool) { + match depth { + 8 => { + let value = data.get(at.row + at.x); + ( + value.map_or(0, |v| v as u8), + value.is_some() && value == key, + ) + } + 16 => { + let value = data.byte(at.row + (at.x << 1)); + (value, key == Some(data.ushort(at.row + at.x))) + } + _ => { + let scale = 255 / ((1 << depth) - 1); + let value = scale * packed_at(data, at.row * 8 + at.x * depth, depth); + (value as u8, key.is_some_and(|key| value == key * scale)) + } + } +} + +/// The colour type 0 key, `tr = tabs.tRNS ? tabs.tRNS : -1` compared with `==`: a key of 0 +/// is falsy, so no pixel is transparent (UPNG's deviation, kept); a table read under an +/// earlier IHDR compares through its joined text (`[]` and `[undefined]` as 0, `[v]` as v, +/// longer arrays as NaN). +fn grey_key(transparency: Option<&Transparency>) -> Option { + match transparency? { + Transparency::Grey(0) | Transparency::Rgb(_) => None, + Transparency::Grey(key) => Some(*key), + Transparency::Palette { bytes, declared } => match declared { + 0 => Some(0), + 1 => Some(bytes.first().map_or(0, |&value| i64::from(value))), + _ => None, + }, + } +} diff --git a/crates/core/tests/pdf_png.rs b/crates/core/tests/pdf_png.rs index df7b0b70..8af4f85d 100644 --- a/crates/core/tests/pdf_png.rs +++ b/crates/core/tests/pdf_png.rs @@ -1 +1,258 @@ -//! R4.5 placeholder, written by its track. +//! O6: the branding gate `png_issue` against `pngIssue` and the decoder `decode_png` against +//! pdf-lib 1.17.1 `embedPng` (UPNG decode, `toRGBA8`, alpha split), frozen in +//! `tests/oracles/r4/png.json`. +#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] + +mod common; + +use std::collections::BTreeMap; +use std::time::{Duration, Instant}; + +use common::r4::{r4_oracle, repo_root, sha256_hex}; +use faunosteo_core::pdf::write::png::{ + PNG_MAX_PIXELS, PNG_MAX_RAW_BYTES, PNG_MAX_SIDE, PngDecodeError, decode_png, png_issue, +}; +use serde_json::Value; + +/// The 108 rows of O6. +fn cases() -> Vec { + let oracle = r4_oracle("png.json"); + let cases = oracle["cases"].as_array().unwrap().clone(); + assert_eq!(cases.len(), 108, "O6 row count"); + cases +} + +/// The bytes of a row, checked against the length and digest the oracle recorded. +fn case_bytes(case: &Value) -> Vec { + let file = case["file"].as_str().unwrap(); + let bytes = std::fs::read(repo_root().join(file)).unwrap_or_else(|e| panic!("{file}: {e}")); + assert_eq!( + bytes.len() as u64, + case["bytes"].as_u64().unwrap(), + "{file}: length" + ); + assert_eq!( + sha256_hex(&bytes), + case["sha256"].as_str().unwrap(), + "{file}: digest" + ); + bytes +} + +fn name(case: &Value) -> &str { + case["name"].as_str().unwrap() +} + +/// The pdf-lib `PngType` of the colour type in the first IHDR. +fn png_type(bytes: &[u8]) -> &'static str { + match bytes[25] { + 0 => "Greyscale", + 2 => "Truecolour", + 3 => "IndexedColour", + 4 => "GreyscaleWithAlpha", + 6 => "TruecolourWithAlpha", + _ => "unknown", + } +} + +#[test] +fn the_caps_are_the_oracle_caps() { + let oracle = r4_oracle("png.json"); + assert_eq!(oracle["caps"]["side"].as_u64(), Some(PNG_MAX_SIDE)); + assert_eq!(oracle["caps"]["pixels"].as_u64(), Some(PNG_MAX_PIXELS)); + assert_eq!(oracle["caps"]["rawBytes"].as_u64(), Some(PNG_MAX_RAW_BYTES)); +} + +#[test] +fn every_o6_gate_verdict_matches_png_issue() { + let mut failures = Vec::new(); + for case in cases() { + let got = png_issue(&case_bytes(&case)).map(|issue| issue.as_str()); + if got != case["gate"].as_str() { + failures.push(format!( + "{}: node {}, rust {got:?}", + name(&case), + case["gate"] + )); + } + } + assert!( + failures.is_empty(), + "O6 gate rows differ:\n{}", + failures.join("\n") + ); +} + +/// One decoded row against the oracle: sizes, plane lengths and digests, alpha kept or not. +fn decode_mismatch(case: &Value, bytes: &[u8]) -> Option { + let expected = &case["decode"]; + let got = match decode_png(bytes) { + Ok(got) => got, + Err(error) => return Some(format!("{}: rust error {error:?}", name(case))), + }; + assert_eq!(expected["bitsPerComponent"], 8, "{}", name(case)); + assert_eq!(expected["colorSpace"], "DeviceRGB", "{}", name(case)); + assert_eq!(expected["pngType"], png_type(bytes), "{}", name(case)); + let alpha = got.alpha.as_deref().map(sha256_hex); + let facts = [ + (u64::from(got.width), expected["width"].as_u64().unwrap()), + (u64::from(got.height), expected["height"].as_u64().unwrap()), + (got.rgb.len() as u64, expected["rgbBytes"].as_u64().unwrap()), + ]; + let sizes_equal = facts.iter().all(|(rust, node)| rust == node); + let rgb_equal = sha256_hex(&got.rgb) == expected["rgbSha256"].as_str().unwrap(); + let alpha_equal = alpha.as_deref() == expected["alphaSha256"].as_str(); + let pixels = u64::from(got.width) * u64::from(got.height); + let alpha_length = got + .alpha + .as_ref() + .is_none_or(|plane| plane.len() as u64 == pixels); + (!(sizes_equal && rgb_equal && alpha_equal && alpha_length)).then(|| { + format!( + "{}: node {expected}, rust {}x{} rgb {} alpha {alpha:?}", + name(case), + got.width, + got.height, + sha256_hex(&got.rgb) + ) + }) +} + +#[derive(Default, Debug)] +struct GroupCount { + rows: u32, + decoded: u32, + errors: u32, + not_run: u32, +} + +#[test] +fn every_o6_decode_matches_pdf_lib() { + let mut failures = Vec::new(); + let mut groups: BTreeMap = BTreeMap::new(); + for case in cases() { + let bytes = case_bytes(&case); + let count = groups + .entry(case["group"].as_str().unwrap().to_owned()) + .or_default(); + count.rows += 1; + let expected = &case["decode"]; + if expected.is_null() { + count.not_run += 1; + } else if expected.get("error").is_some() { + count.errors += 1; + if let Ok(got) = decode_png(&bytes) { + failures.push(format!( + "{}: node {expected}, rust {}x{}", + name(&case), + got.width, + got.height + )); + } + } else { + count.decoded += 1; + failures.extend(decode_mismatch(&case, &bytes)); + } + } + for (group, count) in &groups { + println!("O6 {group}: {count:?}"); + } + assert!( + failures.is_empty(), + "O6 decode rows differ:\n{}", + failures.join("\n") + ); +} + +#[test] +fn the_error_rows_fail_in_rust_with_their_reason() { + for case in cases() + .iter() + .filter(|case| case["decode"].get("error").is_some()) + { + let outcome = decode_png(&case_bytes(case)); + println!( + "O6 error {}: node {}, rust {outcome:?}", + name(case), + case["decode"]["error"] + ); + assert_eq!( + outcome.err(), + Some(PngDecodeError::Palette), + "{}", + name(case) + ); + } +} + +/// The hostile rows UPNG was never run on (the gate refuses them; UPNG may never return): +/// the Rust decoder returns, Ok or Err, within a second and never panics. +#[test] +fn the_rows_upng_never_ran_return_within_a_second() { + let mut slow = Vec::new(); + for case in cases().iter().filter(|case| case["decode"].is_null()) { + let bytes = case_bytes(case); + let started = Instant::now(); + let outcome = decode_png(&bytes).map(|got| (got.width, got.height)); + let elapsed = started.elapsed(); + println!("O6 not run {}: rust {outcome:?} in {elapsed:?}", name(case)); + if elapsed > Duration::from_secs(1) { + slow.push(format!("{}: {elapsed:?}", name(case))); + } + } + assert!( + slow.is_empty(), + "slow hostile decodes:\n{}", + slow.join("\n") + ); +} + +/// A PNG of one IHDR (zero CRCs, as the security suite writes them), an IDAT holding an +/// empty stored zlib stream, and IEND. +fn header_only_png(width: u32, height: u32, depth: u8, colour_type: u8) -> Vec { + let mut bytes = vec![0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]; + let chunk = |bytes: &mut Vec, kind: &[u8; 4], data: &[u8]| { + bytes.extend_from_slice(&(data.len() as u32).to_be_bytes()); + bytes.extend_from_slice(kind); + bytes.extend_from_slice(data); + bytes.extend_from_slice(&[0; 4]); + }; + let mut ihdr = Vec::new(); + ihdr.extend_from_slice(&width.to_be_bytes()); + ihdr.extend_from_slice(&height.to_be_bytes()); + ihdr.extend_from_slice(&[depth, colour_type, 0, 0, 0]); + chunk(&mut bytes, b"IHDR", &ihdr); + chunk( + &mut bytes, + b"IDAT", + &[0x78, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0, 0, 0, 1], + ); + chunk(&mut bytes, b"IEND", &[]); + bytes +} + +#[test] +fn the_decoder_refuses_over_the_caps_before_allocating() { + let refused = [ + (4097, 4096, 8, 0), + (8193, 1, 8, 0), + (1, 8193, 8, 0), + (4096, 4096, 8, 6), + (4096, 4096, 8, 0), + (0, 1, 8, 0), + ]; + for (width, height, depth, colour_type) in refused { + let started = Instant::now(); + let outcome = decode_png(&header_only_png(width, height, depth, colour_type)); + assert_eq!(outcome, Err(PngDecodeError::Dimensions), "{width}x{height}"); + assert!( + started.elapsed() < Duration::from_millis(50), + "{width}x{height}" + ); + } + // At the side cap the size check passes; the empty stream leaves UPNG's buffer short, and + // every sample read past it is `undefined`: an opaque black row. + let at_the_cap = decode_png(&header_only_png(8192, 1, 8, 0)).unwrap(); + assert_eq!((at_the_cap.width, at_the_cap.alpha), (8192, None)); + assert!(at_the_cap.rgb.iter().all(|&byte| byte == 0)); +}