Public: anyone can read this; the crew can change it.
GIMP .xcf layers are listed the way a .psd's are #16030
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16014-gimp-xcf-layers-are-listed-the-way-a-psd → main 0 commits fork 2 threads · 2 unresolved
17 files
changed +423
−4
docs/features/file-kinds.md +1
| @@ -27,6 +27,7 @@ The forge draws a file on its page when its kind is below and it is within the s | ||
| 27 | 27 | - **EPUB book**, drawn up to 20 MB: `.epub` |
| 28 | 28 | - **Font**, drawn up to 20 MB: `.ttf`, `.otf`, `.woff`, `.woff2` |
| 29 | 29 | - **Photoshop**, drawn up to 4 GB: `.psd` |
| 30 | - **GIMP**, drawn up to 20 MB: `.xcf` | |
| 30 | 31 | - **CAD or 3D source (header facts)**, drawn up to 4 GB: `.dwg`, `.blend`, `.fbx` |
| 31 | 32 | - **PEM certificate**, drawn up to 1 MB: `.pem`, `.crt`, `.cer` |
| 32 | 33 | - **Bill of materials (CycloneDX, SPDX)**, drawn up to 5 MB: a .json with a bomFormat (CycloneDX) or spdxVersion (SPDX) key |
scripts/kinds-guide.ts +1 −1
| @@ -11,7 +11,7 @@ export const END = "<!-- file-kinds:end -->"; | ||
| 11 | 11 | export const NAMES: Readonly<Record<FileKind, string>> = { |
| 12 | 12 | image: "Image", svg: "SVG", pdf: "PDF", html: "HTML page", audio: "Audio", video: "Video", csv: "Table (CSV, TSV, JSON lines)", |
| 13 | 13 | sqlite: "SQLite database", parquet: "Parquet", pcap: "Packet capture", model3d: "3D model", archive: "Archive", sheet: "Spreadsheet", document: "Word document", |
| 14 | slides: "Slides", layers: "Layered image", font: "Font", psd: "Photoshop", cad: "CAD or 3D source (header facts)", cert: "PEM certificate", sbom: "Bill of materials (CycloneDX, SPDX)", wasm: "WebAssembly module", config: "Config", markdown: "Markdown", | |
| 14 | slides: "Slides", layers: "Layered image", font: "Font", psd: "Photoshop", xcf: "GIMP", cad: "CAD or 3D source (header facts)", cert: "PEM certificate", sbom: "Bill of materials (CycloneDX, SPDX)", wasm: "WebAssembly module", config: "Config", markdown: "Markdown", | |
| 15 | 15 | notebook: "Notebook", patch: "Patch", log: "Log", subtitles: "Subtitles", gpx: "GPS track", har: "HTTP archive", |
| 16 | 16 | geojson: "GeoJSON", kml: "KML", calendar: "Calendar", otio: "OpenTimelineIO", edl: "Edit list", fcpxml: "Final Cut XML", kdenlive: "Kdenlive project", |
| 17 | 17 | contacts: "Contacts", mail: "Email", gcode: "G-code", dxf: "DXF drawing", gerber: "Gerber layer", mermaid: "Mermaid diagram", drawio: "draw.io diagram", |
src/http/mcp/tools/read_file.ts +10
| @@ -5,6 +5,7 @@ import { blobForRender, listTree } from "../../../git/read.ts"; | ||
| 5 | 5 | import { readStl, sayStl, stlSize, TOO_LARGE } from "../../../text/stl.ts"; |
| 6 | 6 | import { checkConfig, configKind, type ConfigKind } from "../../../text/config_check.ts"; |
| 7 | 7 | import { describeMedia } from "../../../text/mp4.ts"; |
| 8 | import { readXcf } from "../../../web/render/xcf-read.ts"; | |
| 8 | 9 | import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; |
| 9 | 10 | |
| 10 | 11 | export const tool: Tool = { |
| @@ -71,6 +72,7 @@ function aboutFile(path: string, body: Uint8Array, text: string | undefined): Re | ||
| 71 | 72 | const { format, triangles } = stl.value; |
| 72 | 73 | return { kind: "stl", format, triangles, size: stlSize(stl.value), says: sayStl(stl.value) }; |
| 73 | 74 | } |
| 75 | if (/\.xcf$/i.test(path)) return aboutXcf(body); | |
| 74 | 76 | const media = describeMedia(path, body); |
| 75 | 77 | if (media) return media; |
| 76 | 78 | if (text === undefined) return undefined; |
| @@ -88,3 +90,11 @@ function aboutConfig(kind: ConfigKind, content: string) { | ||
| 88 | 90 | message: check.message, |
| 89 | 91 | }; |
| 90 | 92 | } |
| 93 | ||
| 94 | /** A GIMP file's canvas and layers, the facts the blob page lists. */ | |
| 95 | function aboutXcf(body: Uint8Array): Record<string, unknown> { | |
| 96 | const x = readXcf(body); | |
| 97 | if (x === undefined) return { kind: "xcf", says: "GIMP, unreadable" }; | |
| 98 | const { width, height, mode, layerCount, layers } = x; | |
| 99 | return { kind: "xcf", width, height, mode, layerCount, layers, says: `GIMP, ${width} × ${height} ${mode}, ${layerCount} ${layerCount === 1 ? "layer" : "layers"}` }; | |
| 100 | } |
src/tui/screens/workspace.ts +3 −1
| @@ -135,6 +135,7 @@ import { renderMarkdown, type Line } from "../ui/markdown.ts"; | ||
| 135 | 135 | import { archiveNote } from "../ui/archive-note.ts"; |
| 136 | 136 | import { holdsExif, readExif } from "../../web/render/exif.ts"; |
| 137 | 137 | import { psdNote } from "../ui/psd-note.ts"; |
| 138 | import { xcfNote } from "../ui/xcf-note.ts"; | |
| 138 | 139 | import { mediaNote } from "../ui/media-note.ts"; |
| 139 | 140 | import { plainReason } from "../../web/render/renderers.ts"; |
| 140 | 141 | import { undrawable, UNDRAWN } from "../../web/render/undrawable.ts"; |
| @@ -1584,7 +1585,8 @@ export function workspaceScreen( | ||
| 1584 | 1585 | else { |
| 1585 | 1586 | const known = |
| 1586 | 1587 | archiveNote(t.path, read.value.body, read.value.size, formatSize) ?? |
| 1587 | (kindByName(t.path) === "psd" ? psdNote(read.value.body, read.value.size, formatSize) : undefined); | |
| 1588 | (kindByName(t.path) === "psd" ? psdNote(read.value.body, read.value.size, formatSize) : undefined) ?? | |
| 1589 | (kindByName(t.path) === "xcf" ? xcfNote(read.value.body, read.value.size, formatSize) : undefined); | |
| 1588 | 1590 | if (known !== undefined) note = known; |
| 1589 | 1591 | else { |
| 1590 | 1592 | // The page's last resort too: size, type and the first bytes as hex (pure ASCII, so |
src/tui/ui/xcf-note.ts added +18
| @@ -0,0 +1,18 @@ | ||
| 1 | /** | |
| 2 | * What the terminal says for a GIMP .xcf: its canvas, mode and layers as | |
| 3 | * lines, no picture. Read through `readXcf`, as the page is, and only from | |
| 4 | * the whole file: a body cut short of its size is not read. | |
| 5 | */ | |
| 6 | ||
| 7 | import { readXcf } from "../../web/render/xcf-read.ts"; | |
| 8 | ||
| 9 | /** The facts and layer rows of a .xcf; `undefined` for a file that is not a readable one. */ | |
| 10 | export function xcfNote(body: Uint8Array, size: number, fmt: (bytes: number) => string): string | undefined { | |
| 11 | const x = body.length >= size ? readXcf(body) : undefined; | |
| 12 | if (x === undefined) return undefined; | |
| 13 | const lines = [`${fmt(size)} · GIMP · ${x.width} × ${x.height} · ${x.mode}`]; | |
| 14 | lines.push(x.layerCount === 0 ? "No layers" : `${x.layerCount} ${x.layerCount === 1 ? "layer" : "layers"}`); | |
| 15 | for (const l of x.layers) lines.push(` ${l.name === "" ? "(unnamed)" : l.name}${l.visible ? "" : " (hidden)"}`); | |
| 16 | if (x.layerCount > x.layers.length) lines.push(` ${x.layerCount - x.layers.length} more not listed`); | |
| 17 | return lines.join("\n"); | |
| 18 | } |
src/web/render/kinds.ts +5
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | import { binaryFacts, FACTS_EXTENSIONS } from "./binary-facts.ts"; |
| 17 | 17 | import { isSbomHead } from "./sbom.ts"; |
| 18 | 18 | import { isWasm } from "./wasm.ts"; |
| 19 | import { isXcf } from "./xcf-read.ts"; | |
| 19 | 20 | |
| 20 | 21 | export type FileKind = |
| 21 | 22 | | "image" |
| @@ -37,6 +38,7 @@ export type FileKind = | ||
| 37 | 38 | | "epub" |
| 38 | 39 | | "font" |
| 39 | 40 | | "psd" |
| 41 | | "xcf" | |
| 40 | 42 | | "cad" |
| 41 | 43 | | "cert" |
| 42 | 44 | | "sbom" |
| @@ -131,6 +133,8 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 131 | 133 | font: 20 * MB, |
| 132 | 134 | // Read whole up to 5 MB; above that the header alone gives the facts. |
| 133 | 135 | psd: 4_000 * MB, |
| 136 | // Layer headers sit anywhere in the body; the page reads it whole. | |
| 137 | xcf: 20 * MB, | |
| 134 | 138 | // Only the first 64 bytes are read, whatever the size. |
| 135 | 139 | cad: 4_000 * MB, |
| 136 | 140 | cert: 1 * MB, |
| @@ -252,6 +256,7 @@ const MEDIA: Readonly<Record<string, Media>> = { | ||
| 252 | 256 | bmp: { kind: "image", name: "BMP", type: "image/bmp", magic: ascii("BM") }, |
| 253 | 257 | ico: { kind: "image", name: "ICO", type: "image/x-icon", magic: bytes(0x00, 0x00, 0x01, 0x00) }, |
| 254 | 258 | psd: { kind: "psd", name: "Photoshop", type: "image/vnd.adobe.photoshop", magic: (h) => at(h, 0, "8BPS") && h[4] === 0 && h[5] === 1 }, |
| 259 | xcf: { kind: "xcf", name: "GIMP", type: "image/x-xcf", magic: (h) => isXcf(h) }, | |
| 255 | 260 | svg: { kind: "svg", name: "SVG", type: "image/svg+xml", magic: svgRoot }, |
| 256 | 261 | html: { kind: "html", name: "HTML", type: "text/html; charset=utf-8", magic: textHead }, |
| 257 | 262 | htm: { kind: "html", name: "HTML", type: "text/html; charset=utf-8", magic: textHead }, |
src/web/render/psd.ts +6 −2
| @@ -12,7 +12,11 @@ import { fileSize } from "../ui.ts"; | ||
| 12 | 12 | import { LAYER_CAP, MAX_PIXELS, modeName, type PsdHeader, READ_CEILING, readPsd, readPsdHeader } from "./psd-read.ts"; |
| 13 | 13 | import type { Rendered, RenderInput } from "./renderers.ts"; |
| 14 | 14 | |
| 15 | const row = (k: string, v: string) => html`<div class="status-row"><dt>${k}</dt><dd>${v}</dd></div>`; | |
| 15 | export const row = (k: string, v: string) => html`<div class="status-row"><dt>${k}</dt><dd>${v}</dd></div>`; | |
| 16 | ||
| 17 | /** A layer table: one row per layer, its name and a line of facts. Shared with the .xcf view. */ | |
| 18 | export const layerTable = (layers: readonly { name: string; says: string }[]) => | |
| 19 | html`<dl class="status-rows" aria-label="Layers">${layers.map((l) => row(l.name === "" ? "(unnamed)" : l.name, l.says))}</dl>`; | |
| 16 | 20 | |
| 17 | 21 | export function psd(input: RenderInput): Rendered { |
| 18 | 22 | // A file over the page's read ceiling arrives as its first bytes only. |
| @@ -55,7 +59,7 @@ export function psd(input: RenderInput): Rendered { | ||
| 55 | 59 | ${when(png === undefined && why !== "", () => html`<p class="muted small">${why}</p>`)} |
| 56 | 60 | ${when( |
| 57 | 61 | layers.length > 0, |
| 58 | () => html`<dl class="status-rows" aria-label="Layers">${layers.map((l) => row(l.name === "" ? "(unnamed)" : l.name, l.visible ? "Visible" : "Hidden"))}</dl>`, | |
| 62 | () => layerTable(layers.map((l) => ({ name: l.name, says: l.visible ? "Visible" : "Hidden" }))), | |
| 59 | 63 | )} |
| 60 | 64 | ${when(more > 0, () => html`<p class="muted small">${String(more)} more not listed (the page shows the first ${String(LAYER_CAP)}).</p>`)} |
| 61 | 65 | </div>`, |
src/web/render/renderers.ts +2
| @@ -59,6 +59,7 @@ import { depsManifest } from "./deps-manifest.ts"; | ||
| 59 | 59 | import { packageJson, readPackage } from "./package-json.ts"; |
| 60 | 60 | import { patch } from "./patch.ts"; |
| 61 | 61 | import { psd } from "./psd.ts"; |
| 62 | import { xcf } from "./xcf.ts"; | |
| 62 | 63 | import { pdf } from "./pdf.ts"; |
| 63 | 64 | import { dockerfile } from "./dockerfile.ts"; |
| 64 | 65 | import { workflow } from "./workflow.ts"; |
| @@ -201,6 +202,7 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = { | ||
| 201 | 202 | epub, |
| 202 | 203 | font, |
| 203 | 204 | psd, |
| 205 | xcf, | |
| 204 | 206 | cad: binaryView, |
| 205 | 207 | cert, |
| 206 | 208 | sbom, |
src/web/render/xcf-read.ts added +167
| @@ -0,0 +1,167 @@ | ||
| 1 | /** | |
| 2 | * Reading a GIMP file (`.xcf`): the canvas size, the colour mode and each | |
| 3 | * layer's name, size, offset and visibility. Pure: bytes in, plain data out, | |
| 4 | * no markup, so the page, the terminal and the MCP file tool share it. | |
| 5 | * | |
| 6 | * The file is untrusted. Every length is checked against what is left before | |
| 7 | * it is read, a string is bounded by `NAME_MAX`, and every loop consumes at | |
| 8 | * least four bytes of a body that is already under the render cap, so none | |
| 9 | * can run longer than the file is long. Only the first `LAYER_CAP` layers are | |
| 10 | * opened; the rest are counted. Pixel data is never touched. | |
| 11 | */ | |
| 12 | ||
| 13 | export const LAYER_CAP = 200; | |
| 14 | /** The longest layer name kept, in bytes. */ | |
| 15 | const NAME_MAX = 256; | |
| 16 | /** Properties walked in one layer before the file is called unreadable. */ | |
| 17 | const PROP_CAP = 512; | |
| 18 | ||
| 19 | export interface XcfLayer { | |
| 20 | readonly name: string; | |
| 21 | readonly width: number; | |
| 22 | readonly height: number; | |
| 23 | readonly x: number; | |
| 24 | readonly y: number; | |
| 25 | readonly visible: boolean; | |
| 26 | } | |
| 27 | ||
| 28 | export interface Xcf { | |
| 29 | /** "file" for the first versions, else "v003" and so on. */ | |
| 30 | readonly version: string; | |
| 31 | readonly width: number; | |
| 32 | readonly height: number; | |
| 33 | /** The colour mode in words. */ | |
| 34 | readonly mode: string; | |
| 35 | readonly layers: readonly XcfLayer[]; | |
| 36 | /** The layers the file holds, which may be more than are listed. */ | |
| 37 | readonly layerCount: number; | |
| 38 | } | |
| 39 | ||
| 40 | const MODES: Readonly<Record<number, string>> = { 0: "RGB", 1: "Grayscale", 2: "Indexed" }; | |
| 41 | const xcfMode = (base: number): string => MODES[base] ?? `mode ${base}`; | |
| 42 | ||
| 43 | const PROP_END = 0; | |
| 44 | const PROP_VISIBLE = 8; | |
| 45 | const PROP_OFFSETS = 15; | |
| 46 | ||
| 47 | class Reader { | |
| 48 | readonly v: DataView; | |
| 49 | constructor(readonly b: Uint8Array, public at = 0) { | |
| 50 | this.v = new DataView(b.buffer, b.byteOffset, b.byteLength); | |
| 51 | } | |
| 52 | need(n: number): void { | |
| 53 | if (!Number.isInteger(n) || n < 0 || n > this.b.length - this.at) throw new RangeError("xcf: length past the end"); | |
| 54 | } | |
| 55 | u32(): number { | |
| 56 | this.need(4); | |
| 57 | const x = this.v.getUint32(this.at); | |
| 58 | this.at += 4; | |
| 59 | return x; | |
| 60 | } | |
| 61 | i32(): number { | |
| 62 | this.need(4); | |
| 63 | const x = this.v.getInt32(this.at); | |
| 64 | this.at += 4; | |
| 65 | return x; | |
| 66 | } | |
| 67 | skip(n: number): void { | |
| 68 | this.need(n); | |
| 69 | this.at += n; | |
| 70 | } | |
| 71 | /** An offset: 32 bits, or 64 from version 11; one beyond the body is refused where it is used. */ | |
| 72 | pointer(wide: boolean): number { | |
| 73 | if (!wide) return this.u32(); | |
| 74 | const hi = this.u32(); | |
| 75 | const lo = this.u32(); | |
| 76 | return hi === 0 ? lo : Number.MAX_SAFE_INTEGER; | |
| 77 | } | |
| 78 | /** A string: its length with the NUL, then the bytes. */ | |
| 79 | string(): string { | |
| 80 | const n = this.u32(); | |
| 81 | this.need(n); | |
| 82 | const keep = Math.min(n, NAME_MAX); | |
| 83 | const raw = this.b.subarray(this.at, this.at + keep); | |
| 84 | this.at += n; | |
| 85 | const nul = raw.indexOf(0); | |
| 86 | return new TextDecoder().decode(nul < 0 ? raw : raw.subarray(0, nul)); | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * The file's version number (0 for "file", else the digits), or undefined when the header is not | |
| 92 | * XCF. | |
| 93 | */ | |
| 94 | function versionOf(b: Uint8Array): { text: string; n: number } | undefined { | |
| 95 | if (b.length < 14) return undefined; | |
| 96 | const head = new TextDecoder("latin1").decode(b.subarray(0, 14)); | |
| 97 | if (!head.startsWith("gimp xcf ") || head[13] !== "\0") return undefined; | |
| 98 | const v = head.slice(9, 13); | |
| 99 | if (v === "file") return { text: v, n: 0 }; | |
| 100 | return /^v\d{3}$/.test(v) ? { text: v, n: Number(v.slice(1)) } : undefined; | |
| 101 | } | |
| 102 | ||
| 103 | /** True when the bytes open as an XCF file. */ | |
| 104 | export const isXcf = (b: Uint8Array): boolean => versionOf(b) !== undefined; | |
| 105 | ||
| 106 | /** | |
| 107 | * Reads the facts; undefined for a file that is not XCF or whose structure lies about its lengths. | |
| 108 | */ | |
| 109 | export function readXcf(body: Uint8Array): Xcf | undefined { | |
| 110 | const ver = versionOf(body); | |
| 111 | if (ver === undefined) return undefined; | |
| 112 | try { | |
| 113 | const r = new Reader(body, 14); | |
| 114 | const width = r.u32(); | |
| 115 | const height = r.u32(); | |
| 116 | const base = r.u32(); | |
| 117 | if (ver.n >= 4) r.u32(); // precision | |
| 118 | const wide = ver.n >= 11; | |
| 119 | // Image properties: skipped by their own sizes, each at least 8 bytes. | |
| 120 | for (;;) { | |
| 121 | const type = r.u32(); | |
| 122 | const size = r.u32(); | |
| 123 | if (type === PROP_END) break; | |
| 124 | r.skip(size); | |
| 125 | } | |
| 126 | const pointers: number[] = []; | |
| 127 | let layerCount = 0; | |
| 128 | for (;;) { | |
| 129 | const p = r.pointer(wide); | |
| 130 | if (p === 0) break; | |
| 131 | layerCount++; | |
| 132 | if (pointers.length < LAYER_CAP) pointers.push(p); | |
| 133 | } | |
| 134 | const layers = pointers.map((p) => readLayer(body, p)); | |
| 135 | return { version: ver.text, width, height, mode: xcfMode(base), layers, layerCount }; | |
| 136 | } catch { | |
| 137 | return undefined; | |
| 138 | } | |
| 139 | } | |
| 140 | ||
| 141 | function readLayer(body: Uint8Array, at: number): XcfLayer { | |
| 142 | const r = new Reader(body, 0); | |
| 143 | r.need(at); | |
| 144 | r.at = at; | |
| 145 | const width = r.u32(); | |
| 146 | const height = r.u32(); | |
| 147 | r.u32(); // type | |
| 148 | const name = r.string(); | |
| 149 | let visible = true; | |
| 150 | let x = 0; | |
| 151 | let y = 0; | |
| 152 | for (let i = 0; ; i++) { | |
| 153 | if (i >= PROP_CAP) throw new RangeError("xcf: too many properties"); | |
| 154 | const type = r.u32(); | |
| 155 | const size = r.u32(); | |
| 156 | if (type === PROP_END) break; | |
| 157 | r.need(size); | |
| 158 | const end = r.at + size; | |
| 159 | if (type === PROP_VISIBLE && size >= 4) visible = r.u32() !== 0; | |
| 160 | else if (type === PROP_OFFSETS && size >= 8) { | |
| 161 | x = r.i32(); | |
| 162 | y = r.i32(); | |
| 163 | } | |
| 164 | r.at = end; | |
| 165 | } | |
| 166 | return { name, width, height, x, y, visible }; | |
| 167 | } |
src/web/render/xcf.ts added +36
| @@ -0,0 +1,36 @@ | ||
| 1 | /** | |
| 2 | * A GIMP file on the blob page: the canvas and colour mode as rows, then the | |
| 3 | * layer table the .psd view draws (name, size, offset, visibility). No | |
| 4 | * pixels are drawn. A file that does not parse falls back to source. Reading | |
| 5 | * is in `xcf-read.ts`. | |
| 6 | */ | |
| 7 | ||
| 8 | import { html, when } from "../html.ts"; | |
| 9 | import { fileSize } from "../ui.ts"; | |
| 10 | import { layerTable, row } from "./psd.ts"; | |
| 11 | import type { Rendered, RenderInput } from "./renderers.ts"; | |
| 12 | import { LAYER_CAP, readXcf, type XcfLayer } from "./xcf-read.ts"; | |
| 13 | ||
Duplicates |
||
| 14 | /** One layer's facts in a line: "200 × 100 at 10, 20 · Visible". */ | |
| 15 | export const layerSays = (l: XcfLayer): string => | |
| 16 | `${l.width} × ${l.height} at ${l.x}, ${l.y} · ${l.visible ? "Visible" : "Hidden"}`; | |
| 17 | ||
| 18 | export function xcf(input: RenderInput): Rendered { | |
| 19 | const x = input.body.length >= input.size ? readXcf(input.body) : undefined; | |
| 20 | if (x === undefined) return { content: html``, lines: 0, fallback: "not a readable GIMP file" }; | |
| 21 | const name = input.path.slice(input.path.lastIndexOf("/") + 1); | |
| 22 | const more = x.layerCount - x.layers.length; | |
| 23 | return { | |
| 24 | lines: 0, | |
| 25 | summary: `${fileSize(input.size)} · GIMP · ${x.width} × ${x.height}`, | |
| 26 | content: html`<div class="psd-view"> | |
| 27 | <dl class="status-rows" aria-label="${`${name} facts`}"> | |
| 28 | ${row("Size", `${x.width} × ${x.height}`)} | |
| 29 | ${row("Mode", x.mode)} | |
| 30 | ${row("Layers", String(x.layerCount))} | |
| 31 | </dl> | |
| 32 | ${when(x.layers.length > 0, () => layerTable(x.layers.map((l) => ({ name: l.name, says: layerSays(l) }))))} | |
| 33 | ${when(more > 0, () => html`<p class="muted small">${String(more)} more not listed (the page shows the first ${String(LAYER_CAP)}).</p>`)} | |
| 34 | </div>`, | |
| 35 | }; | |
| 36 | } |
test/fuzz-renderers.test.ts +14
| @@ -32,6 +32,19 @@ function igesFile(): string { | ||
| 32 | 32 | ].join("\n") + "\n"; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | /** | |
| 36 | * A small valid GIMP file as text, every byte below 0x80 so it survives the fuzzer's round trip. | |
| 37 | */ | |
| 38 | function xcfSample(): string { | |
| 39 | const u32 = (n: number) => String.fromCharCode((n >>> 24) & 255, (n >> 16) & 255, (n >> 8) & 255, n & 255); | |
| 40 | const layer = (name: string) => | |
| 41 | u32(10) + u32(5) + u32(0) + u32(name.length + 1) + name + "\0" + u32(8) + u32(4) + u32(1) + u32(15) + u32(8) + u32(3) + u32(4) + u32(0) + u32(0); | |
| 42 | const head = "gimp xcf v003\0" + u32(64) + u32(48) + u32(0) + u32(0) + u32(0); | |
| 43 | const at = head.length + 16; | |
| 44 | const first = layer("<b>Sky</b>"); | |
| 45 | return head + u32(at) + u32(at + first.length) + u32(0) + u32(0) + first + layer("javascript:alert(1)"); | |
| 46 | } | |
| 47 | ||
| 35 | 48 | /** |
| 36 | 49 | * A valid file of each kind; a case is one of these with hostile text spliced in. The kind is what |
| 37 | 50 | * `fileKind` reads from the path and the first file, and the renderer fuzzed is the one `RENDERERS` |
| @@ -42,6 +55,7 @@ export const SAMPLES: Readonly<Record<string, { path: string; files: readonly st | ||
| 42 | 55 | path: "data.csv", |
| 43 | 56 | files: ['name,url,note\nAda,https://a.test,"quoted, cell"\nBob,javascript:alert(1),x\n'], |
| 44 | 57 | }, |
| 58 | xcf: { path: "a.xcf", files: [xcfSample()] }, | |
| 45 | 59 | tsv: { path: "data.tsv", files: ["a\tb\tc\n1\t2\t3\n"] }, |
| 46 | 60 | jsonl: { |
| 47 | 61 | path: "rows.jsonl", |
test/helpers/xcf.ts added +24
| @@ -0,0 +1,24 @@ | ||
| 1 | /** A GIMP file built in memory: header, layer table and layer headers, no pixels. */ | |
| 2 | ||
| 3 | export const u32 = (n: number): number[] => [(n >>> 24) & 255, (n >> 16) & 255, (n >> 8) & 255, n & 255]; | |
| 4 | export const text = (s: string): number[] => [...new TextEncoder().encode(s)]; | |
| 5 | ||
| 6 | export function layer(name: string, o: { w?: number; h?: number; x?: number; y?: number; hidden?: boolean } = {}): number[] { | |
| 7 | const n = text(name); | |
| 8 | return [ | |
| 9 | ...u32(o.w ?? 10), ...u32(o.h ?? 5), ...u32(0), ...u32(n.length + 1), ...n, 0, | |
| 10 | ...u32(8), ...u32(4), ...u32(o.hidden ? 0 : 1), | |
| 11 | ...u32(15), ...u32(8), ...u32((o.x ?? 0) >>> 0), ...u32((o.y ?? 0) >>> 0), | |
| 12 | ...u32(0), ...u32(0), | |
| 13 | ]; | |
| 14 | } | |
| 15 | ||
| 16 | export function make(layers: [string, Parameters<typeof layer>[1]?][] = [["Background"]], w = 64, h = 48): Uint8Array { | |
| 17 | const head = [...text("gimp xcf v003"), 0, ...u32(w), ...u32(h), ...u32(0), ...u32(0), ...u32(0)]; | |
| 18 | const table = head.length; | |
| 19 | const base = table + 4 * (layers.length + 2); | |
| 20 | const bodies: number[][] = layers.map(([n, o]) => layer(n, o)); | |
| 21 | let at = base; | |
| 22 | const ptrs = bodies.map((b) => { const p = at; at += b.length; return p; }); | |
| 23 | return new Uint8Array([...head, ...ptrs.flatMap(u32), ...u32(0), ...u32(0), ...bodies.flat()]); | |
| 24 | } |
test/mcp-read-file.test.ts +10
| @@ -11,6 +11,7 @@ import { createUser } from "../src/core/users.ts"; | ||
| 11 | 11 | import { serve, type Servers } from "../src/http/server.ts"; |
| 12 | 12 | import { quietLogs } from "./helpers/globals.ts"; |
| 13 | 13 | import { box, concat, ftyp, mvhd0, tkhd } from "./helpers/mp4-boxes.ts"; |
| 14 | import { make as xcfFile } from "./helpers/xcf.ts"; | |
| 14 | 15 | import { asciiBox, binaryBox } from "./helpers/stl.ts"; |
| 15 | 16 | |
| 16 | 17 | quietLogs(); |
| @@ -67,6 +68,7 @@ beforeAll(async () => { | ||
| 67 | 68 | { path: "bad.json", content: '{\n "a": }' }, |
| 68 | 69 | { path: "good.json", content: "{}" }, |
| 69 | 70 | ] })); |
| 71 | ok(await commitToBranch(app, owner, pub, { message: "gimp", files: [{ path: "art.xcf", content: xcfFile([["Sky", { w: 200, h: 100, x: 3, y: 4 }]]) }, { path: "bad.xcf", content: "gimp xcf nope" }] })); | |
| 70 | 72 | const movie = concat(ftyp(), box("moov", mvhd0(600, 600 * 92), box("trak", tkhd(0, 1920, 1080)))); |
| 71 | 73 | ok(await commitToBranch(app, owner, pub, { message: "movie", files: [{ path: "clip.mp4", content: movie }] })); |
| 72 | 74 | ok( |
| @@ -147,3 +149,11 @@ test("a config file says whether it parses; other files carry no about", async ( | ||
| 147 | 149 | expect((await read(t, { repo: "ada/pub", path: "good.json" })).value.about).toEqual({ kind: "json", parses: true }); |
| 148 | 150 | expect((await read(t, { repo: "ada/pub", path: "src/a.txt" })).value.about).toBeUndefined(); |
| 149 | 151 | }); |
| 152 | ||
| 153 | test("a .xcf file carries its canvas and layers as about, or says it is unreadable", async () => { | |
| 154 | const file = (await read(tok["stranger"]!, { repo: "ada/pub", path: "art.xcf" })).value; | |
| 155 | expect(file.about).toMatchObject({ kind: "xcf", width: 64, height: 48, mode: "RGB", layerCount: 1 }); | |
| 156 | expect(file.about.says).toBe("GIMP, 64 × 48 RGB, 1 layer"); | |
| 157 | expect(file.about.layers[0]).toEqual({ name: "Sky", width: 200, height: 100, x: 3, y: 4, visible: true }); | |
| 158 | expect((await read(tok["stranger"]!, { repo: "ada/pub", path: "bad.xcf" })).value.about.says).toBe("GIMP, unreadable"); | |
| 159 | }); |
test/render-kinds.test.ts +1
| @@ -69,6 +69,7 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = { | ||
| 69 | 69 | otf: [enc("OTTO\0\0"), "font", "font/otf"], |
| 70 | 70 | woff: [enc("wOFF\0\0"), "font", "font/woff"], |
| 71 | 71 | psd: [of(0x38, 0x42, 0x50, 0x53, 0, 1), "psd", "image/vnd.adobe.photoshop"], |
| 72 | xcf: [new TextEncoder().encode("gimp xcf v003\0"), "xcf", "image/x-xcf"], | |
| 72 | 73 | woff2: [enc("wOF2\0\0"), "font", "font/woff2"], |
| 73 | 74 | zip: [of(0x50, 0x4b, 3, 4, 0), "archive", "application/octet-stream"], |
| 74 | 75 | tar: [enc("x".repeat(257) + "ustar\0"), "archive", "application/octet-stream"], |
test/render-xcf.test.ts added +73
| @@ -0,0 +1,73 @@ | ||
| 1 | /** A .xcf on the blob page and in the MCP file tool: facts, layers, and no trust in its lengths. */ | |
| 2 | ||
| 3 | import { describe, expect, test } from "bun:test"; | |
| 4 | import { fileKind } from "../src/web/render/kinds.ts"; | |
| 5 | import type { RenderInput } from "../src/web/render/renderers.ts"; | |
| 6 | import { rendererFor } from "../src/web/render/renderers.ts"; | |
| 7 | import { xcf } from "../src/web/render/xcf.ts"; | |
| 8 | import { layer, make, text, u32 } from "./helpers/xcf.ts"; | |
| 9 | import { LAYER_CAP, readXcf } from "../src/web/render/xcf-read.ts"; | |
| 10 | ||
| 11 | const input = (body: Uint8Array, path = "a.xcf"): RenderInput => ({ | |
| 12 | path, | |
| 13 | body, | |
| 14 | size: body.length, | |
| 15 | media: "/m", | |
| 16 | download: "/d", | |
| 17 | all: true, | |
| 18 | }); | |
| 19 | ||
| 20 | describe("xcf", () => { | |
| 21 | test("parse: canvas, mode, and each layer's size, offset and visibility", () => { | |
| 22 | const x = readXcf(make([["Sky", { w: 200, h: 100, x: 10, y: 20 }], ["Ink", { hidden: true }]]))!; | |
| 23 | expect([x.width, x.height, x.mode, x.layerCount]).toEqual([64, 48, "RGB", 2]); | |
| 24 | expect(x.layers[0]).toEqual({ name: "Sky", width: 200, height: 100, x: 10, y: 20, visible: true }); | |
| 25 | expect(x.layers[1]!.visible).toBe(false); | |
| 26 | const out = xcf(input(make([["Sky", { x: -5 >>> 0 }]]))); | |
| 27 | expect(out.fallback).toBeUndefined(); | |
| 28 | expect(out.content.text).toContain("64 × 48"); | |
| 29 | expect(out.content.text).toContain("Sky"); | |
| 30 | expect(out.content.text).toContain("at -5, 0"); | |
| 31 | }); | |
| 32 | ||
| 33 | test("reached: the extension and signature open it through the registry", () => { | |
| 34 | const body = make(); | |
| 35 | expect(fileKind("a.xcf", body)).toBe("xcf"); | |
| 36 | expect(fileKind("a.xcf", new Uint8Array(text("not gimp at all, long enough")))).not.toBe("xcf"); | |
| 37 | expect(rendererFor("a.xcf", { kind: "binary", body, size: body.length })?.render).toBeDefined(); | |
| 38 | }); | |
| 39 | ||
| 40 | test("escape: a hostile layer name never becomes markup", () => { | |
| 41 | const out = xcf(input(make([["<script>alert(1)</script>"]]))); | |
| 42 | expect(out.content.text).not.toContain("<script>"); | |
| 43 | expect(out.content.text).toContain("<script>"); | |
| 44 | }); | |
| 45 | ||
| 46 | test("cap: only the first LAYER_CAP layers are listed, the rest counted", () => { | |
| 47 | const many = Array.from({ length: LAYER_CAP + 5 }, (_, i) => [`L${i}`] as [string]); | |
| 48 | const x = readXcf(make(many))!; | |
| 49 | expect(x.layers).toHaveLength(LAYER_CAP); | |
| 50 | expect(x.layerCount).toBe(LAYER_CAP + 5); | |
| 51 | expect(xcf(input(make(many))).content.text).toContain("5 more not listed"); | |
| 52 | }); | |
| 53 | ||
| 54 | test("malformed: lying lengths and truncation fall back to source with a reason", () => { | |
| 55 | const good = make(); | |
| 56 | expect(xcf(input(good.subarray(0, good.length - 6))).fallback).toBe("not a readable GIMP file"); | |
| 57 | expect(xcf(input(new Uint8Array(text("gimp xcf v003\0")))).fallback).toBeDefined(); | |
| 58 | // A layer pointer far past the end, and a name length of 4 GB. | |
| 59 | const far = make(); | |
| 60 | far.set(u32(0x7fffffff), 34); | |
| 61 | expect(readXcf(far)).toBeUndefined(); | |
| 62 | const lie = make(); | |
| 63 | const nameLen = lie.length - (layer("Background").length) + 12; | |
| 64 | lie.set(u32(0xffffffff), nameLen); | |
| 65 | expect(readXcf(lie)).toBeUndefined(); | |
| 66 | expect(readXcf(new Uint8Array(text("gimp xcf v0x3\0")))).toBeUndefined(); | |
| 67 | }); | |
| 68 | ||
| 69 | test("a body cut short of its size is not read", () => { | |
| 70 | const body = make(); | |
| 71 | expect(xcf({ ...input(body), size: body.length + 10 }).fallback).toBeDefined(); | |
| 72 | }); | |
| 73 | }); |
test/tui-workspace-kinds.test.ts +17
| @@ -17,6 +17,7 @@ import type { TuiIdentity } from "../src/tui/session.ts"; | ||
| 17 | 17 | import { headlessTerminal, type HeadlessTerminal } from "../src/tui/term/terminal.ts"; |
| 18 | 18 | import { settleApp, settleUntil } from "./helpers/tui.ts"; |
| 19 | 19 | import { seedAdmin } from "./helpers/admin.ts"; |
| 20 | import { make as xcfFile } from "./helpers/xcf.ts"; | |
| 20 | 21 | import { theme } from "../src/tui/ui/theme.ts"; |
| 21 | 22 | import { logLevel } from "../src/web/render/log.ts"; |
| 22 | 23 | import { jsonlTable, kindView, logLines, LINE_CAP, notebookLines, ROW_CAP } from "../src/tui/screens/workspace_kinds.ts"; |
| @@ -188,6 +189,7 @@ describe("the workspace opens each kind", () => { | ||
| 188 | 189 | { path: "empty.log", content: "" }, |
| 189 | 190 | { path: "nul.log", content: "a\x00b\nline two\n" }, |
| 190 | 191 | { path: "art.psd", content: psdFile(["Sky", "Ink"]) }, |
| 192 | { path: "art.xcf", content: xcfFile([["Sky"], ["Ink", { hidden: true }]]) }, | |
| 191 | 193 | ], |
| 192 | 194 | }); |
| 193 | 195 | }); |
| @@ -281,4 +283,19 @@ describe("the workspace opens each kind", () => { | ||
| 281 | 283 | h.stop(); |
| 282 | 284 | } |
| 283 | 285 | }); |
| 286 | ||
| 287 | test("a .xcf opens as its facts and layer rows", async () => { | |
| 288 | const h = boot(); | |
| 289 | try { | |
| 290 | await h.settle(); | |
| 291 | await open(h, "kinds"); | |
| 292 | await openFile(h, "art.xcf"); | |
| 293 | await h.until("2 layers"); | |
| 294 | expect(h.text()).toContain("GIMP · 64 × 48"); | |
| 295 | expect(h.text()).toContain("Ink (hidden)"); | |
| 296 | expect(h.text()).not.toContain("Binary file"); | |
| 297 | } finally { | |
| 298 | h.stop(); | |
| 299 | } | |
| 300 | }); | |
| 284 | 301 | }); |
test/tui-xcf-note.test.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { xcfNote } from "../src/tui/ui/xcf-note.ts"; | |
| 3 | import { LAYER_CAP } from "../src/web/render/xcf-read.ts"; | |
| 4 | import { make } from "./helpers/xcf.ts"; | |
| 5 | ||
| 6 | const fmt = (n: number): string => `${n} B`; | |
| 7 | ||
| 8 | describe("xcfNote", () => { | |
| 9 | test("canvas, mode and layers as lines, hidden ones marked", () => { | |
| 10 | const b = make([["Sky"], ["Ink", { hidden: true }]]); | |
| 11 | const out = xcfNote(b, b.length, fmt)!; | |
| 12 | expect(out).toContain("GIMP · 64 × 48"); | |
| 13 | expect(out).toContain("2 layers"); | |
| 14 | expect(out).toContain(" Sky"); | |
| 15 | expect(out).toContain(" Ink (hidden)"); | |
| 16 | }); | |
| 17 | ||
| 18 | test("one layer is not '1 layers'; an unnamed one is named so", () => { | |
| 19 | const b = make([[""]]); | |
| 20 | const out = xcfNote(b, b.length, fmt)!; | |
| 21 | expect(out).toContain("1 layer\n"); | |
| 22 | expect(out).toContain("(unnamed)"); | |
| 23 | }); | |
| 24 | ||
| 25 | test("layers past the cap are counted, not listed", () => { | |
| 26 | const b = make(Array.from({ length: LAYER_CAP + 3 }, (_, i) => [`L${i}`] as [string])); | |
| 27 | expect(xcfNote(b, b.length, fmt)).toContain("3 more not listed"); | |
| 28 | }); | |
| 29 | ||
| 30 | test("a body cut short of its size, or not a GIMP file, is not read", () => { | |
| 31 | const b = make(); | |
| 32 | expect(xcfNote(b, b.length + 1, fmt)).toBeUndefined(); | |
| 33 | expect(xcfNote(new Uint8Array(20), 20, fmt)).toBeUndefined(); | |
| 34 | }); | |
| 35 | }); |
new above
offline
Pluralise:
1 layersreads wrong. Add a test case.