1047 lines
30 KiB
TypeScript
1047 lines
30 KiB
TypeScript
/**
|
||
* R4.1 freeze O6 (plan 2026-10-17 §B.5.1): what the TypeScript engine does
|
||
* with a PNG. (a) The branding gate `pngIssue` (branding.ts) on every PNG
|
||
* the security suite builds (its constructions reproduced here, that file
|
||
* untouched), on the brand logos and on a variant set (palette, tRNS, grey,
|
||
* grey + alpha, 16-bit, RGB, RGBA, 1-bit, interlaced). (b) pdf-lib's decode
|
||
* exactly as `embedPng` runs it (`PngEmbedder.for` → `PNG.load` → UPNG
|
||
* decode + toRGBA8 + alpha split) on the gate-accepted PNGs, the variants and
|
||
* raster-path variants (bad CRCs, unknown or malformed ancillary chunks,
|
||
* trailing bytes): size, colour space, sha256 of the RGB and alpha planes
|
||
* (null when pdf-lib drops an all-opaque alpha) or the error. A PNG the gate
|
||
* refuses from the security suite is never decoded: UPNG spins or reads past
|
||
* the buffer on some of them. `MAKE_R4_ORACLES=1` writes
|
||
* `crates/core/tests/oracles/r4/png.json` and the PNGs under
|
||
* `crates/core/tests/fixtures/png/r4/`.
|
||
*/
|
||
import { createHash } from 'node:crypto'
|
||
import { readdirSync, readFileSync } from 'node:fs'
|
||
import { basename, relative, resolve } from 'node:path'
|
||
import { crc32, deflateSync } from 'node:zlib'
|
||
import { describe, expect, it } from 'vitest'
|
||
import { PngEmbedder } from 'pdf-lib'
|
||
import { PNG_CAPS, pngIssue } from '../src/branding'
|
||
import { BRAND_ASSETS_DIR, PUBLIC_DIR, REPO_ROOT } from './helpers/draft'
|
||
import { logosBranding, ONE_PX_PNG, TWO_BY_ONE_PNG } from './helpers/goldens'
|
||
import {
|
||
environment,
|
||
freezeJson,
|
||
MAKE_R4,
|
||
writeOracleFile,
|
||
} from './helpers/r4-oracle-io'
|
||
|
||
const PNG_DIR = resolve(REPO_ROOT, 'crates/core/tests/fixtures/png/r4')
|
||
/** The R3-build raster of the v2full obs-1 main view (resvg, 1200 × 943), committed by R4.1b. */
|
||
const REAL_RASTER = resolve(
|
||
REPO_ROOT,
|
||
'apps/app/tests/fixtures/pdf/r3/real/raster-1.png',
|
||
)
|
||
|
||
type Group = 'security' | 'logo' | 'variant' | 'raster'
|
||
|
||
interface PngInput {
|
||
name: string
|
||
group: Group
|
||
origin: string
|
||
bytes: Uint8Array
|
||
/** Repo-relative path: a committed asset, or the file this generator writes. */
|
||
file: string
|
||
}
|
||
|
||
const concat = (...parts: Uint8Array[]): Uint8Array =>
|
||
Uint8Array.from(Buffer.concat(parts.map((p) => Buffer.from(p))))
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// The security suite's constructions (tests/security.test.ts, "PNG budget"), verbatim in behaviour
|
||
|
||
interface IhdrFields {
|
||
depth?: number
|
||
colourType?: number
|
||
compression?: number
|
||
filter?: number
|
||
interlace?: number
|
||
}
|
||
|
||
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
|
||
const CHANNELS: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }
|
||
|
||
function pngHeader(
|
||
width: number,
|
||
height: number,
|
||
fields: IhdrFields = {},
|
||
signature = true,
|
||
): Uint8Array {
|
||
const bytes = new Uint8Array(33)
|
||
bytes.set(PNG_SIGNATURE)
|
||
if (!signature) bytes[3] = 0x48
|
||
const view = new DataView(bytes.buffer)
|
||
view.setUint32(8, 13)
|
||
bytes.set([0x49, 0x48, 0x44, 0x52], 12)
|
||
view.setUint32(16, width)
|
||
view.setUint32(20, height)
|
||
const {
|
||
depth = 8,
|
||
colourType = 0,
|
||
compression = 0,
|
||
filter = 0,
|
||
interlace = 0,
|
||
} = fields
|
||
bytes.set([depth, colourType, compression, filter, interlace], 24)
|
||
return bytes
|
||
}
|
||
|
||
/** One chunk with a zero CRC, as the security suite writes them. */
|
||
function pngChunk(type: string, data: Uint8Array): Uint8Array {
|
||
const bytes = new Uint8Array(12 + data.byteLength)
|
||
new DataView(bytes.buffer).setUint32(0, data.byteLength)
|
||
bytes.set(
|
||
Array.from(type, (c) => c.charCodeAt(0)),
|
||
4,
|
||
)
|
||
bytes.set(data, 8)
|
||
return bytes
|
||
}
|
||
|
||
function rawSize(
|
||
width: number,
|
||
height: number,
|
||
fields: IhdrFields = {},
|
||
): number {
|
||
const { depth = 8, colourType = 0 } = fields
|
||
return (Math.ceil((width * CHANNELS[colourType]! * depth) / 8) + 1) * height
|
||
}
|
||
|
||
const idatOf = (raw: Uint8Array): Uint8Array =>
|
||
pngChunk('IDAT', deflateSync(raw))
|
||
const IEND = pngChunk('IEND', new Uint8Array(0))
|
||
const IDAT = idatOf(new Uint8Array(2))
|
||
|
||
function validPng(
|
||
width: number,
|
||
height: number,
|
||
fields: IhdrFields = {},
|
||
): Uint8Array {
|
||
return concat(
|
||
pngHeader(width, height, fields),
|
||
idatOf(new Uint8Array(rawSize(width, height, fields))),
|
||
IEND,
|
||
)
|
||
}
|
||
|
||
const doubledIhdrPng = (): Uint8Array =>
|
||
concat(pngHeader(1, 1), pngHeader(10_000, 10_000).subarray(8), IEND)
|
||
|
||
function textWithoutNulPng(): Uint8Array {
|
||
const text = pngChunk('tEXt', new TextEncoder().encode('AAAA'))
|
||
text.fill(0xff, text.byteLength - 4)
|
||
return concat(ONE_PX_PNG.subarray(0, ONE_PX_PNG.byteLength - 12), text)
|
||
}
|
||
|
||
function itxtThenFatIendPng(): Uint8Array {
|
||
const itxt = pngChunk('iTXt', new TextEncoder().encode('kw'))
|
||
const fatIend = pngChunk('IEND', new Uint8Array(0x010101).fill(1))
|
||
return concat(pngHeader(1, 1), IDAT, itxt, fatIend)
|
||
}
|
||
|
||
function truncatedIdatPng(): Uint8Array {
|
||
const raw = Uint8Array.from(
|
||
{ length: rawSize(16, 16, { colourType: 6 }) },
|
||
(_, i) => i & 0xff,
|
||
)
|
||
const stream = deflateSync(raw).subarray(0, 20)
|
||
return concat(
|
||
pngHeader(16, 16, { colourType: 6 }),
|
||
pngChunk('IDAT', stream),
|
||
IEND,
|
||
)
|
||
}
|
||
|
||
interface Chunk {
|
||
type: string
|
||
data: Uint8Array
|
||
}
|
||
|
||
function chunksOf(png: Uint8Array): Chunk[] {
|
||
const view = new DataView(png.buffer, png.byteOffset, png.byteLength)
|
||
const chunks: Chunk[] = []
|
||
for (let offset = 8; offset < png.byteLength; ) {
|
||
const length = view.getUint32(offset)
|
||
const type = String.fromCharCode(...png.subarray(offset + 4, offset + 8))
|
||
chunks.push({ type, data: png.subarray(offset + 8, offset + 8 + length) })
|
||
offset += 12 + length
|
||
}
|
||
return chunks
|
||
}
|
||
|
||
function headerLogoBytes(): Uint8Array {
|
||
const asset = logosBranding().headerLogo
|
||
if (asset?.kind !== 'png') throw new Error('png expected')
|
||
return Uint8Array.from(Buffer.from(asset.base64, 'base64'))
|
||
}
|
||
|
||
function cutLogoPng(): Uint8Array {
|
||
const chunks = chunksOf(headerLogoBytes())
|
||
const last = chunks.filter((c) => c.type === 'IDAT').at(-1)!
|
||
last.data = last.data.subarray(0, last.data.byteLength - 5)
|
||
return concat(
|
||
Uint8Array.from(PNG_SIGNATURE),
|
||
...chunks.map((c) => pngChunk(c.type, c.data)),
|
||
)
|
||
}
|
||
|
||
type Made = [name: string, origin: string, bytes: Uint8Array]
|
||
|
||
const headerOnly = (
|
||
width: number,
|
||
height: number,
|
||
fields: IhdrFields = {},
|
||
): Uint8Array => concat(pngHeader(width, height, fields), IEND)
|
||
|
||
function budgetPngs(): Made[] {
|
||
const dims: Array<[string, number, number]> = [
|
||
['8193x1', 8193, 1],
|
||
['1x8193', 1, 8193],
|
||
['4097x4096', 4097, 4096],
|
||
['12000x12000', 12_000, 12_000],
|
||
['0x0', 0, 0],
|
||
['1x0', 1, 0],
|
||
['8192x0', 8192, 0],
|
||
]
|
||
return [
|
||
...dims.map(
|
||
([n, w, h]): Made => [
|
||
`dims-${n}`,
|
||
'dimensions: header + IEND',
|
||
headerOnly(w, h),
|
||
],
|
||
),
|
||
[
|
||
'raw-4096x4096-grey1',
|
||
'raw-size budget: validPng 1-bit',
|
||
validPng(4096, 4096, { depth: 1 }),
|
||
],
|
||
[
|
||
'raw-8192x2048-grey1',
|
||
'raw-size budget: validPng 1-bit',
|
||
validPng(8192, 2048, { depth: 1 }),
|
||
],
|
||
[
|
||
'raw-4095x4096-grey8',
|
||
'raw-size budget: exactly 16 MiB of scanlines',
|
||
validPng(4095, 4096),
|
||
],
|
||
[
|
||
'raw-4096x4096-grey8-header',
|
||
'raw-size budget: one row byte over',
|
||
headerOnly(4096, 4096),
|
||
],
|
||
[
|
||
'raw-4096x4096-rgba-header',
|
||
'raw-size budget: RGBA 4096²',
|
||
headerOnly(4096, 4096, { colourType: 6 }),
|
||
],
|
||
]
|
||
}
|
||
|
||
const IHDR_CASES: Array<[string, IhdrFields]> = [
|
||
['depth255', { depth: 255 }],
|
||
['colour5', { colourType: 5 }],
|
||
['interlace1', { interlace: 1 }],
|
||
['filter1', { filter: 1 }],
|
||
['compression1', { compression: 1 }],
|
||
['rgba-depth4', { depth: 4, colourType: 6 }],
|
||
['palette-depth16', { depth: 16, colourType: 3 }],
|
||
]
|
||
const ALLOWED: Array<[number, number[]]> = [
|
||
[0, [1, 2, 4, 8, 16]],
|
||
[2, [8, 16]],
|
||
[3, [1, 2, 4, 8]],
|
||
[4, [8, 16]],
|
||
[6, [8, 16]],
|
||
]
|
||
|
||
function headerFieldPngs(): Made[] {
|
||
return [
|
||
...IHDR_CASES.flatMap(([n, f]): Made[] => [
|
||
[`ihdr-${n}-4x3`, 'ihdr: validPng(4, 3)', validPng(4, 3, f)],
|
||
[
|
||
`ihdr-${n}-4096`,
|
||
'ihdr: header 4096² + IEND',
|
||
headerOnly(4096, 4096, f),
|
||
],
|
||
]),
|
||
...ALLOWED.flatMap(([colourType, depths]) =>
|
||
depths.map(
|
||
(depth): Made => [
|
||
`allowed-type${colourType}-depth${depth}`,
|
||
'every allowed colour type and depth: validPng(5, 3)',
|
||
validPng(5, 3, { depth, colourType }),
|
||
],
|
||
),
|
||
),
|
||
]
|
||
}
|
||
|
||
function idatPngs(): Made[] {
|
||
const offBy = (delta: number) =>
|
||
concat(pngHeader(4, 3), idatOf(new Uint8Array(rawSize(4, 3) + delta)), IEND)
|
||
const stream = deflateSync(new Uint8Array(rawSize(4, 3)))
|
||
const split = Math.floor(stream.byteLength / 2)
|
||
const halves = [stream.subarray(0, split), stream.subarray(split)].map((s) =>
|
||
pngChunk('IDAT', s),
|
||
)
|
||
return [
|
||
['idat-truncated-poc', 'idat: the 77-byte PoC', truncatedIdatPng()],
|
||
['idat-cut-logo', 'idat: header logo minus 5 IDAT bytes', cutLogoPng()],
|
||
['idat-missing', 'idat: no IDAT', concat(pngHeader(1, 1), IEND)],
|
||
[
|
||
'idat-not-zlib',
|
||
'idat: 16 zero bytes',
|
||
concat(pngHeader(1, 1), pngChunk('IDAT', new Uint8Array(16)), IEND),
|
||
],
|
||
['idat-raw-plus1', 'idat: one raw byte too many', offBy(1)],
|
||
['idat-raw-minus1', 'idat: one raw byte short', offBy(-1)],
|
||
['idat-valid-4x3', 'idat: validPng(4, 3)', validPng(4, 3)],
|
||
[
|
||
'idat-split',
|
||
'the IDAT stream may span several chunks',
|
||
concat(pngHeader(4, 3), ...halves, IEND),
|
||
],
|
||
]
|
||
}
|
||
|
||
function signatureAndChunkPngs(): Made[] {
|
||
const shortIhdr = pngHeader(1, 1)
|
||
new DataView(shortIhdr.buffer).setUint32(8, 12)
|
||
const pastBuffer = pngChunk('IDAT', new Uint8Array(16))
|
||
new DataView(pastBuffer.buffer).setUint32(0, 1_000_000)
|
||
const frame = (type: string, size: number) =>
|
||
concat(pngHeader(1, 1), pngChunk(type, new Uint8Array(size)), IEND)
|
||
return [
|
||
['sig-wrong', 'signature: byte 3 changed', pngHeader(1, 1, {}, false)],
|
||
[
|
||
'sig-truncated-header',
|
||
'signature: 20 bytes',
|
||
pngHeader(1, 1).subarray(0, 20),
|
||
],
|
||
[
|
||
'sig-svg-text',
|
||
'signature: the bytes of <svg/>',
|
||
new TextEncoder().encode('<svg/>'),
|
||
],
|
||
['sig-short-ihdr', 'signature: IHDR length 12', shortIhdr],
|
||
['one-px', 'helpers/goldens ONE_PX_PNG', ONE_PX_PNG],
|
||
['two-by-one', 'helpers/goldens TWO_BY_ONE_PNG', TWO_BY_ONE_PNG],
|
||
['chunks-double-ihdr', 'chunks: second IHDR 10000²', doubledIhdrPng()],
|
||
['chunks-actl', 'chunks: APNG acTL', frame('acTL', 8)],
|
||
['chunks-fctl', 'chunks: APNG fcTL', frame('fcTL', 4)],
|
||
['chunks-fdat', 'chunks: APNG fdAT', frame('fdAT', 4)],
|
||
[
|
||
'chunks-length-past-buffer',
|
||
'chunks: IDAT length 1e6',
|
||
concat(pngHeader(1, 1), pastBuffer),
|
||
],
|
||
[
|
||
'chunks-partial-head',
|
||
'chunks: 5 bytes after IHDR',
|
||
concat(pngHeader(1, 1), new Uint8Array(5)),
|
||
],
|
||
[
|
||
'chunks-valid-1x1',
|
||
'chunks: header, IDAT, IEND',
|
||
concat(pngHeader(1, 1), IDAT, IEND),
|
||
],
|
||
]
|
||
}
|
||
|
||
function listEndPngs(): Made[] {
|
||
const valid = concat(pngHeader(1, 1), IDAT, IEND)
|
||
const ancillary = [
|
||
pngChunk('gAMA', new Uint8Array(4)),
|
||
pngChunk('pHYs', new Uint8Array(9)),
|
||
pngChunk('sRGB', new Uint8Array(1)),
|
||
]
|
||
const between = (type: string) =>
|
||
concat(pngHeader(1, 1), IDAT, pngChunk(type, new Uint8Array(7)), IEND)
|
||
return [
|
||
[
|
||
'chunks-text-without-nul',
|
||
'nextZero PoC: tEXt "AAAA", no NUL, no IEND',
|
||
textWithoutNulPng(),
|
||
],
|
||
[
|
||
'chunks-itxt-fat-iend',
|
||
'nextZero PoC: iTXt then IEND of length 0x010101',
|
||
itxtThenFatIendPng(),
|
||
],
|
||
...['tIME', 'zTXt', 'iCCP', 'abCD'].map(
|
||
(t): Made => [
|
||
`chunks-${t.toLowerCase()}`,
|
||
`chunks: ${t} chunk`,
|
||
between(t),
|
||
],
|
||
),
|
||
[
|
||
'chunks-trailing-byte',
|
||
'list end: one byte after IEND',
|
||
concat(valid, new Uint8Array([0])),
|
||
],
|
||
['chunks-no-iend', 'list end: no IEND', concat(pngHeader(1, 1), IDAT)],
|
||
[
|
||
'chunks-fat-iend',
|
||
'list end: IEND of length 1',
|
||
concat(pngHeader(1, 1), IDAT, pngChunk('IEND', new Uint8Array(1))),
|
||
],
|
||
[
|
||
'chunks-ancillary',
|
||
'list end: gAMA, pHYs, sRGB pass',
|
||
concat(pngHeader(1, 1), ...ancillary, IDAT, IEND),
|
||
],
|
||
[
|
||
'logo-oversized-16384',
|
||
'logo_failed: header 16384² + IEND',
|
||
headerOnly(16_384, 16_384),
|
||
],
|
||
[
|
||
'logo-1x0',
|
||
'logo_failed: 1 × 0 with IDAT',
|
||
concat(pngHeader(1, 0), IDAT, IEND),
|
||
],
|
||
]
|
||
}
|
||
|
||
const generated = (group: Group, [name, origin, bytes]: Made): PngInput => ({
|
||
name,
|
||
group,
|
||
origin,
|
||
bytes,
|
||
file: relative(REPO_ROOT, resolve(PNG_DIR, `${name}.png`)),
|
||
})
|
||
|
||
function securityPngs(): PngInput[] {
|
||
const made = [
|
||
...budgetPngs(),
|
||
...headerFieldPngs(),
|
||
...idatPngs(),
|
||
...signatureAndChunkPngs(),
|
||
...listEndPngs(),
|
||
]
|
||
return made.map((m) => generated('security', m))
|
||
}
|
||
|
||
function committedPngs(): PngInput[] {
|
||
const logos = readdirSync(BRAND_ASSETS_DIR)
|
||
.filter((f) => f.endsWith('.png'))
|
||
.sort()
|
||
const files = [
|
||
...logos.map((f) => resolve(BRAND_ASSETS_DIR, f)),
|
||
resolve(PUBLIC_DIR, 'apple-touch-icon.png'),
|
||
]
|
||
return files.map((path) => ({
|
||
name: basename(path, '.png'),
|
||
group: 'logo',
|
||
origin:
|
||
'committed asset the gate must pass (brand logo or apple-touch-icon)',
|
||
bytes: new Uint8Array(readFileSync(path)),
|
||
file: relative(REPO_ROOT, path),
|
||
}))
|
||
}
|
||
|
||
/** A committed raster-path PNG of realistic size, decoded like the generated ones. */
|
||
function realRasterPng(): PngInput {
|
||
return {
|
||
name: 'raster-real-v2full-obs1-main',
|
||
group: 'raster',
|
||
origin: 'R3-build view raster: v2full obs-1 main view rendered by resvg',
|
||
bytes: new Uint8Array(readFileSync(REAL_RASTER)),
|
||
file: relative(REPO_ROOT, REAL_RASTER),
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// A small PNG encoder with real CRCs: variants and raster-path inputs
|
||
|
||
function crcChunk(type: string, data: Uint8Array): Uint8Array {
|
||
const typed = concat(new TextEncoder().encode(type), data)
|
||
const bytes = new Uint8Array(4 + typed.byteLength + 4)
|
||
const view = new DataView(bytes.buffer)
|
||
view.setUint32(0, data.byteLength)
|
||
bytes.set(typed, 4)
|
||
view.setUint32(bytes.byteLength - 4, crc32(typed))
|
||
return bytes
|
||
}
|
||
|
||
interface ImageSpec {
|
||
width: number
|
||
height: number
|
||
depth: number
|
||
colourType: number
|
||
interlace?: 0 | 1
|
||
/** Sample value of channel `c` at (x, y), within the bit depth. */
|
||
sample: (x: number, y: number, c: number) => number
|
||
palette?: Array<[number, number, number]>
|
||
trns?: number[]
|
||
}
|
||
|
||
/** One row of samples packed at the bit depth, MSB first; 16-bit samples big-endian. */
|
||
function packRow(spec: ImageSpec, y: number, xs: number[]): Uint8Array {
|
||
const channels = CHANNELS[spec.colourType]!
|
||
const bits: number[] = []
|
||
for (const x of xs) {
|
||
for (let c = 0; c < channels; c += 1) bits.push(spec.sample(x, y, c))
|
||
}
|
||
const out = new Uint8Array(Math.ceil((bits.length * spec.depth) / 8))
|
||
bits.forEach((value, i) => {
|
||
if (spec.depth === 16) out.set([value >> 8, value & 0xff], i * 2)
|
||
else if (spec.depth === 8) out[i] = value
|
||
else
|
||
out[(i * spec.depth) >> 3]! |=
|
||
value << (8 - spec.depth - ((i * spec.depth) & 7))
|
||
})
|
||
return out
|
||
}
|
||
|
||
function paeth(a: number, b: number, c: number): number {
|
||
const p = a + b - c
|
||
const [pa, pb, pc] = [Math.abs(p - a), Math.abs(p - b), Math.abs(p - c)]
|
||
if (pa <= pb && pa <= pc) return a
|
||
return pb <= pc ? b : c
|
||
}
|
||
|
||
/** PNG filter `type` applied to `row` against the previous unfiltered row, the filter byte first. */
|
||
function filterRow(
|
||
type: number,
|
||
row: Uint8Array,
|
||
prev: Uint8Array,
|
||
bpp: number,
|
||
): Uint8Array {
|
||
const out = new Uint8Array(row.byteLength + 1)
|
||
out[0] = type
|
||
for (let i = 0; i < row.byteLength; i += 1) {
|
||
const [left, up, upLeft] = [
|
||
i >= bpp ? row[i - bpp]! : 0,
|
||
prev[i]!,
|
||
i >= bpp ? prev[i - bpp]! : 0,
|
||
]
|
||
const predictor = [0, left, up, (left + up) >> 1, paeth(left, up, upLeft)][
|
||
type
|
||
]!
|
||
out[i + 1] = (row[i]! - predictor) & 0xff
|
||
}
|
||
return out
|
||
}
|
||
|
||
const ADAM7: Array<[number, number, number, number]> = [
|
||
[0, 0, 8, 8],
|
||
[4, 0, 8, 8],
|
||
[0, 4, 4, 8],
|
||
[2, 0, 4, 4],
|
||
[0, 2, 2, 4],
|
||
[1, 0, 2, 2],
|
||
[0, 1, 1, 2],
|
||
]
|
||
|
||
/** Filtered scanlines, filter type cycling 0..4 per row; Adam7 passes when interlaced. */
|
||
function scanlines(spec: ImageSpec): Uint8Array {
|
||
const bpp = Math.max(1, (CHANNELS[spec.colourType]! * spec.depth) >> 3)
|
||
const passes = spec.interlace
|
||
? ADAM7
|
||
: [[0, 0, 1, 1] as [number, number, number, number]]
|
||
const rows: Uint8Array[] = []
|
||
for (const [x0, y0, dx, dy] of passes) {
|
||
const xs = Array.from(
|
||
{ length: Math.ceil((spec.width - x0) / dx) },
|
||
(_, i) => x0 + i * dx,
|
||
)
|
||
if (xs.length === 0) continue
|
||
let prev: Uint8Array = new Uint8Array(packRow(spec, y0, xs).byteLength)
|
||
for (let y = y0; y < spec.height; y += dy) {
|
||
const row = packRow(spec, y, xs)
|
||
rows.push(filterRow(rows.length % 5, row, prev, bpp))
|
||
prev = row
|
||
}
|
||
}
|
||
return concat(...rows)
|
||
}
|
||
|
||
function ihdrData(spec: ImageSpec): Uint8Array {
|
||
const data = new Uint8Array(13)
|
||
const view = new DataView(data.buffer)
|
||
view.setUint32(0, spec.width)
|
||
view.setUint32(4, spec.height)
|
||
data.set([spec.depth, spec.colourType, 0, 0, spec.interlace ?? 0], 8)
|
||
return data
|
||
}
|
||
|
||
/** The chunk list of a well-formed PNG: IHDR, PLTE, tRNS, one IDAT, IEND. */
|
||
function encodeChunks(spec: ImageSpec): Chunk[] {
|
||
const chunks: Chunk[] = [{ type: 'IHDR', data: ihdrData(spec) }]
|
||
if (spec.palette)
|
||
chunks.push({ type: 'PLTE', data: Uint8Array.from(spec.palette.flat()) })
|
||
if (spec.trns) chunks.push({ type: 'tRNS', data: Uint8Array.from(spec.trns) })
|
||
chunks.push({ type: 'IDAT', data: deflateSync(scanlines(spec)) })
|
||
chunks.push({ type: 'IEND', data: new Uint8Array(0) })
|
||
return chunks
|
||
}
|
||
|
||
const encodePng = (chunks: Chunk[]): Uint8Array =>
|
||
concat(
|
||
Uint8Array.from(PNG_SIGNATURE),
|
||
...chunks.map((c) => crcChunk(c.type, c.data)),
|
||
)
|
||
|
||
/** A deterministic sample spread over the full range of `depth` bits. */
|
||
function noise(depth: number): (x: number, y: number, c: number) => number {
|
||
return (x, y, c) => {
|
||
const h =
|
||
Math.imul(x + 1, 0x9e3779b1) ^
|
||
Math.imul(y + 1, 0x85ebca6b) ^
|
||
Math.imul(c + 1, 0xc2b2ae35)
|
||
return ((h ^ (h >>> 13)) >>> 0) % 2 ** depth
|
||
}
|
||
}
|
||
|
||
const PALETTE = Array.from({ length: 16 }, (_, i): [number, number, number] => [
|
||
i * 17,
|
||
255 - i * 13,
|
||
(i * 71) & 0xff,
|
||
])
|
||
|
||
/** Alpha 0 on every fourth column, 128 on the next, opaque elsewhere. */
|
||
const stripedAlpha = (x: number, max: number): number =>
|
||
[0, max >> 1, max, max][x % 4]!
|
||
|
||
function withAlpha(depth: number, channels: number, opaque: boolean) {
|
||
const base = noise(depth)
|
||
const max = 2 ** depth - 1
|
||
return (x: number, y: number, c: number) =>
|
||
c < channels ? base(x, y, c) : opaque ? max : stripedAlpha(x, max)
|
||
}
|
||
|
||
/** Every seventh pixel (x + y) takes the tRNS key, so the transparency rule is exercised. */
|
||
function keyed(depth: number, key: number[]) {
|
||
const base = noise(depth)
|
||
return (x: number, y: number, c: number) =>
|
||
(x + y) % 7 === 0 ? key[c]! : base(x, y, c)
|
||
}
|
||
|
||
const VARIANTS: Array<[string, ImageSpec]> = [
|
||
[
|
||
'palette-8',
|
||
{
|
||
width: 16,
|
||
height: 16,
|
||
depth: 8,
|
||
colourType: 3,
|
||
sample: (x, y) => noise(4)(x, y, 0),
|
||
palette: PALETTE,
|
||
},
|
||
],
|
||
[
|
||
'palette-trns',
|
||
{
|
||
width: 16,
|
||
height: 16,
|
||
depth: 8,
|
||
colourType: 3,
|
||
sample: (x, y) => noise(4)(x, y, 0),
|
||
palette: PALETTE,
|
||
trns: [0, 64, 128, 192, 255, 32],
|
||
},
|
||
],
|
||
[
|
||
'palette-4',
|
||
{
|
||
width: 17,
|
||
height: 5,
|
||
depth: 4,
|
||
colourType: 3,
|
||
sample: noise(4),
|
||
palette: PALETTE,
|
||
},
|
||
],
|
||
[
|
||
'grey-8',
|
||
{ width: 16, height: 16, depth: 8, colourType: 0, sample: noise(8) },
|
||
],
|
||
[
|
||
'grey-trns',
|
||
{
|
||
width: 16,
|
||
height: 16,
|
||
depth: 8,
|
||
colourType: 0,
|
||
sample: keyed(8, [0x42]),
|
||
trns: [0x00, 0x42],
|
||
},
|
||
],
|
||
[
|
||
'grey-16',
|
||
{ width: 8, height: 8, depth: 16, colourType: 0, sample: noise(16) },
|
||
],
|
||
[
|
||
'grey-16-trns',
|
||
{
|
||
width: 8,
|
||
height: 8,
|
||
depth: 16,
|
||
colourType: 0,
|
||
sample: keyed(16, [0x1234]),
|
||
trns: [0x12, 0x34],
|
||
},
|
||
],
|
||
[
|
||
'grey-1',
|
||
{ width: 19, height: 7, depth: 1, colourType: 0, sample: noise(1) },
|
||
],
|
||
[
|
||
'grey-alpha-8',
|
||
{
|
||
width: 16,
|
||
height: 16,
|
||
depth: 8,
|
||
colourType: 4,
|
||
sample: withAlpha(8, 1, false),
|
||
},
|
||
],
|
||
[
|
||
'grey-alpha-16',
|
||
{
|
||
width: 8,
|
||
height: 8,
|
||
depth: 16,
|
||
colourType: 4,
|
||
sample: withAlpha(16, 1, false),
|
||
},
|
||
],
|
||
[
|
||
'rgb-8',
|
||
{ width: 16, height: 16, depth: 8, colourType: 2, sample: noise(8) },
|
||
],
|
||
[
|
||
'rgb-trns',
|
||
{
|
||
width: 16,
|
||
height: 16,
|
||
depth: 8,
|
||
colourType: 2,
|
||
sample: keyed(8, [10, 20, 30]),
|
||
trns: [0, 10, 0, 20, 0, 30],
|
||
},
|
||
],
|
||
[
|
||
'rgb-16',
|
||
{ width: 8, height: 8, depth: 16, colourType: 2, sample: noise(16) },
|
||
],
|
||
[
|
||
'rgba-8-opaque',
|
||
{
|
||
width: 16,
|
||
height: 16,
|
||
depth: 8,
|
||
colourType: 6,
|
||
sample: withAlpha(8, 3, true),
|
||
},
|
||
],
|
||
[
|
||
'rgba-8-transparent',
|
||
{
|
||
width: 16,
|
||
height: 16,
|
||
depth: 8,
|
||
colourType: 6,
|
||
sample: withAlpha(8, 3, false),
|
||
},
|
||
],
|
||
[
|
||
'rgba-16',
|
||
{
|
||
width: 8,
|
||
height: 8,
|
||
depth: 16,
|
||
colourType: 6,
|
||
sample: withAlpha(16, 3, false),
|
||
},
|
||
],
|
||
[
|
||
'interlaced-rgba',
|
||
{
|
||
width: 13,
|
||
height: 11,
|
||
depth: 8,
|
||
colourType: 6,
|
||
interlace: 1,
|
||
sample: withAlpha(8, 3, false),
|
||
},
|
||
],
|
||
[
|
||
'interlaced-grey-1',
|
||
{
|
||
width: 13,
|
||
height: 11,
|
||
depth: 1,
|
||
colourType: 0,
|
||
interlace: 1,
|
||
sample: noise(1),
|
||
},
|
||
],
|
||
]
|
||
|
||
function variantPngs(): PngInput[] {
|
||
return VARIANTS.map(([name, spec]) =>
|
||
generated('variant', [
|
||
`variant-${name}`,
|
||
`encoder: ${name}, real CRCs, filters 0-4 by row`,
|
||
encodePng(encodeChunks(spec)),
|
||
]),
|
||
)
|
||
}
|
||
|
||
/** A view-like raster: transparent margin, an opaque disc with a gradient, a half-transparent ring. */
|
||
function rasterSample(x: number, y: number, c: number): number {
|
||
const d = Math.hypot(x - 32, y - 24)
|
||
if (c === 3) return d < 16 ? 255 : d < 20 ? 128 : 0
|
||
return [x * 4, y * 5, (x * y) & 0xff][c]!
|
||
}
|
||
|
||
const RASTER: ImageSpec = {
|
||
width: 64,
|
||
height: 48,
|
||
depth: 8,
|
||
colourType: 6,
|
||
sample: rasterSample,
|
||
}
|
||
|
||
function corruptCrc(png: Uint8Array, type: string): Uint8Array {
|
||
const copy = Uint8Array.from(png)
|
||
const view = new DataView(copy.buffer)
|
||
for (
|
||
let offset = 8;
|
||
offset < copy.byteLength;
|
||
offset += 12 + view.getUint32(offset)
|
||
) {
|
||
if (String.fromCharCode(...copy.subarray(offset + 4, offset + 8)) !== type)
|
||
continue
|
||
const crcAt = offset + 8 + view.getUint32(offset)
|
||
copy[crcAt + 3] = copy[crcAt + 3]! ^ 0xff
|
||
return copy
|
||
}
|
||
throw new Error(`no ${type}`)
|
||
}
|
||
|
||
/** Chunks of the raster with `extra` inserted before the chunk at `index`. */
|
||
function insertedAt(index: number, extra: Chunk): Chunk[] {
|
||
const chunks = encodeChunks(RASTER)
|
||
return [...chunks.slice(0, index), extra, ...chunks.slice(index)]
|
||
}
|
||
|
||
/** A tEXt whose declared length runs past the end of the file, after IDAT: the file was cut there. */
|
||
function cutTextPng(): Uint8Array {
|
||
const chunks = encodeChunks(RASTER).slice(0, 2)
|
||
const data = new TextEncoder().encode('Comment\0cut here')
|
||
const head = new Uint8Array(8)
|
||
new DataView(head.buffer).setUint32(0, data.byteLength + 1000)
|
||
head.set(new TextEncoder().encode('tEXt'), 4)
|
||
return concat(encodePng(chunks), head, data)
|
||
}
|
||
|
||
function badAdlerPng(): Uint8Array {
|
||
const chunks = encodeChunks(RASTER)
|
||
const idat = chunks.find((c) => c.type === 'IDAT')!
|
||
idat.data = Uint8Array.from(idat.data)
|
||
idat.data[idat.data.byteLength - 1] =
|
||
idat.data[idat.data.byteLength - 1]! ^ 0xff
|
||
return encodePng(chunks)
|
||
}
|
||
|
||
/** Row 4 filtered with Paeth but flagged 5, a filter type the PNG spec does not define. */
|
||
function filterFivePng(): Uint8Array {
|
||
const raw = scanlines(RASTER)
|
||
const at = 4 * (RASTER.width * 4 + 1)
|
||
if (raw[at] !== 4) throw new Error('row 4 is expected to use Paeth')
|
||
raw[at] = 5
|
||
const chunks = encodeChunks(RASTER)
|
||
chunks.find((c) => c.type === 'IDAT')!.data = deflateSync(raw)
|
||
return encodePng(chunks)
|
||
}
|
||
|
||
function rasterPngs(): PngInput[] {
|
||
const valid = encodePng(encodeChunks(RASTER))
|
||
const chunks = encodeChunks(RASTER)
|
||
const idat = chunks.find((c) => c.type === 'IDAT')!.data
|
||
const third = Math.floor(idat.byteLength / 3)
|
||
const thirds = [
|
||
idat.subarray(0, third),
|
||
idat.subarray(third, 2 * third),
|
||
idat.subarray(2 * third),
|
||
]
|
||
const made: Made[] = [
|
||
['raster-rgba', 'valid 64 × 48 RGBA view-like raster', valid],
|
||
['raster-bad-crc-idat', 'IDAT CRC wrong', corruptCrc(valid, 'IDAT')],
|
||
['raster-bad-crc-ihdr', 'IHDR CRC wrong', corruptCrc(valid, 'IHDR')],
|
||
[
|
||
'raster-unknown-ancillary',
|
||
'private ancillary faNo chunk after IHDR',
|
||
encodePng(
|
||
insertedAt(1, { type: 'faNo', data: new Uint8Array([1, 2, 3]) }),
|
||
),
|
||
],
|
||
[
|
||
'raster-text-without-separator',
|
||
'tEXt "Comment" with no NUL, before IDAT',
|
||
encodePng(
|
||
insertedAt(1, {
|
||
type: 'tEXt',
|
||
data: new TextEncoder().encode('Comment'),
|
||
}),
|
||
),
|
||
],
|
||
[
|
||
'raster-text-truncated',
|
||
'tEXt after IDAT declared 1000 bytes past the end of the file',
|
||
cutTextPng(),
|
||
],
|
||
[
|
||
'raster-trailing-bytes',
|
||
'32 bytes after IEND',
|
||
concat(
|
||
valid,
|
||
Uint8Array.from({ length: 32 }, (_, i) => i),
|
||
),
|
||
],
|
||
['raster-bad-adler', 'zlib Adler-32 wrong, CRCs right', badAdlerPng()],
|
||
[
|
||
'raster-filter-5',
|
||
'row 4 flagged with filter type 5 (Paeth data)',
|
||
filterFivePng(),
|
||
],
|
||
[
|
||
'raster-idat-split',
|
||
'IDAT stream in three chunks',
|
||
encodePng([
|
||
chunks[0]!,
|
||
...thirds.map((data) => ({ type: 'IDAT', data })),
|
||
chunks.at(-1)!,
|
||
]),
|
||
],
|
||
['raster-no-iend', 'no IEND', encodePng(chunks.slice(0, -1))],
|
||
]
|
||
return made.map((m) => generated('raster', m))
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Verdicts
|
||
|
||
const sha256 = (bytes: Uint8Array): string =>
|
||
createHash('sha256').update(bytes).digest('hex')
|
||
|
||
/** What `PNG.load` leaves on the embedder: the planes and the colour type. */
|
||
interface DecodedPng {
|
||
rgbChannel: Uint8Array
|
||
alphaChannel?: Uint8Array
|
||
type: string
|
||
}
|
||
|
||
/** `embedPng` up to its first await: `PngEmbedder.for(bytes)`, which decodes. */
|
||
async function decodeAsEmbedPng(bytes: Uint8Array) {
|
||
try {
|
||
const embedder = await PngEmbedder.for(bytes)
|
||
const image = (embedder as unknown as { image: DecodedPng }).image
|
||
return {
|
||
width: embedder.width,
|
||
height: embedder.height,
|
||
bitsPerComponent: embedder.bitsPerComponent,
|
||
colorSpace: embedder.colorSpace,
|
||
pngType: image.type,
|
||
rgbBytes: image.rgbChannel.byteLength,
|
||
rgbSha256: sha256(image.rgbChannel),
|
||
alphaSha256: image.alphaChannel ? sha256(image.alphaChannel) : null,
|
||
}
|
||
} catch (error) {
|
||
const kind = error instanceof Error ? error.constructor.name : typeof error
|
||
return {
|
||
error: error instanceof Error ? error.message : String(error),
|
||
errorKind: kind,
|
||
}
|
||
}
|
||
}
|
||
|
||
/** Decoded when the gate passes it, or when it is a variant or a raster-path input (never hostile). */
|
||
async function verdictOf(input: PngInput) {
|
||
const gate = await pngIssue(input.bytes)
|
||
const decodes =
|
||
gate === null || input.group === 'variant' || input.group === 'raster'
|
||
return {
|
||
name: input.name,
|
||
group: input.group,
|
||
origin: input.origin,
|
||
file: input.file,
|
||
bytes: input.bytes.byteLength,
|
||
sha256: sha256(input.bytes),
|
||
gate,
|
||
decode: decodes ? await decodeAsEmbedPng(input.bytes) : null,
|
||
}
|
||
}
|
||
|
||
function allInputs(): PngInput[] {
|
||
const inputs = [
|
||
...securityPngs(),
|
||
...committedPngs(),
|
||
...variantPngs(),
|
||
...rasterPngs(),
|
||
realRasterPng(),
|
||
]
|
||
const names = new Set(inputs.map((i) => i.name))
|
||
expect(names.size, 'unique names').toBe(inputs.length)
|
||
return inputs
|
||
}
|
||
|
||
/** MAKE writes each generated PNG; otherwise each one must equal its committed file byte for byte. */
|
||
function freezeFiles(inputs: PngInput[]): void {
|
||
const generatedHere = (input: PngInput) =>
|
||
resolve(REPO_ROOT, input.file).startsWith(`${PNG_DIR}/`)
|
||
for (const input of inputs.filter(generatedHere)) {
|
||
const path = resolve(REPO_ROOT, input.file)
|
||
if (MAKE_R4) writeOracleFile(path, input.bytes)
|
||
else
|
||
expect(
|
||
Buffer.compare(readFileSync(path), Buffer.from(input.bytes)),
|
||
input.file,
|
||
).toBe(0)
|
||
}
|
||
}
|
||
|
||
async function buildPng(inputs: PngInput[]) {
|
||
const cases = []
|
||
for (const input of inputs) cases.push(await verdictOf(input))
|
||
return {
|
||
comment: [
|
||
'gate: pngIssue(bytes) (packages/pdf/src/branding.ts), null = accepted; it applies to the',
|
||
'base64 branding PNGs only. decode: pdf-lib 1.17.1 PngEmbedder.for(bytes), what embedPng',
|
||
'runs (UPNG decode, toRGBA8, alpha split): rgbSha256 over width*height*3 bytes, alphaSha256',
|
||
'over width*height bytes or null when every alpha is 255 (pdf-lib drops the plane), or',
|
||
'{ error, errorKind } when it throws (UPNG throws strings too). decode null = not run: the',
|
||
'gate refuses it and it comes from the hostile security set (UPNG may never return).',
|
||
'Security PNGs carry zero CRCs as the security suite writes them; variants and rasters have',
|
||
'real CRCs unless their name says otherwise. Files are repo-relative; the logos, the',
|
||
'apple-touch-icon and raster-real-* are committed assets, referenced, not copied.',
|
||
'Checked once against Pillow and a direct computation (not by this test): UPNG matches the',
|
||
'PNG spec on every variant except variant-interlaced-* (a pass whose first row uses filter',
|
||
'3 is unfiltered at absolute offsets in _filterZero) and the alpha of variant-grey-16-trns',
|
||
'(the 16-bit key is compared at off + (x << i) with i undefined); 16-bit samples keep their',
|
||
'high byte. The pinned values are what UPNG gives, deviations included.',
|
||
],
|
||
environment: environment(),
|
||
caps: PNG_CAPS,
|
||
cases,
|
||
}
|
||
}
|
||
|
||
describe('R4 PNG oracles (TypeScript freeze)', () => {
|
||
it('matches the frozen gate verdicts and decodes', async () => {
|
||
const inputs = allInputs()
|
||
freezeFiles(inputs)
|
||
freezeJson('png.json', await buildPng(inputs))
|
||
}, 300_000)
|
||
})
|