The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { PreviewError, previewFailure } from "./previewFailure"; |
| 2 | |
| 3 | const MAX_PIXELS = 32_000_000; |
| 4 | |
| 5 | function validateTiffMetadata(bytes: ArrayBuffer) { |
| 6 | const view = new DataView(bytes); |
| 7 | if (bytes.byteLength < 8) throw new PreviewError({ code: "tiff-metadata-invalid" }); |
| 8 | const order = view.getUint16(0); |
| 9 | const littleEndian = order === 0x4949; |
| 10 | if (![0x4949, 0x4d4d].includes(order) || view.getUint16(2, littleEndian) !== 42) |
| 11 | throw new PreviewError({ code: "tiff-metadata-invalid" }); |
| 12 | const visited = new Set<number>(); |
| 13 | let entries = 0; |
| 14 | let allocation = 0; |
| 15 | const inspect = (offset: number, depth: number) => { |
| 16 | if (depth > 8 || visited.has(offset) || offset < 8 || offset + 2 > bytes.byteLength) |
| 17 | throw new PreviewError({ code: "tiff-metadata-invalid" }); |
| 18 | visited.add(offset); |
| 19 | const count = view.getUint16(offset, littleEndian); |
| 20 | entries += count; |
| 21 | if (entries > 2048 || offset + 2 + count * 12 + 4 > bytes.byteLength) |
| 22 | throw new PreviewError({ code: "tiff-metadata-truncated" }); |
| 23 | for (let index = 0; index < count; index++) { |
| 24 | const entry = offset + 2 + index * 12; |
| 25 | const tag = view.getUint16(entry, littleEndian); |
| 26 | const type = view.getUint16(entry + 2, littleEndian); |
| 27 | const values = view.getUint32(entry + 4, littleEndian); |
| 28 | const width = [0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8][type]; |
| 29 | if (!width) throw new PreviewError({ code: "tiff-metadata-type" }); |
| 30 | const length = values * width; |
| 31 | const start = length > 4 ? view.getUint32(entry + 8, littleEndian) : entry + 8; |
| 32 | // The decoder retains a separate array or string for every tag, including aliases. |
| 33 | allocation += values * Math.max(16, width); |
| 34 | if (start + length > bytes.byteLength || allocation > 8 * 1024 * 1024) |
| 35 | throw new PreviewError({ code: "tiff-metadata-truncated" }); |
| 36 | if (tag === 0x8769 || tag === 0x8825) { |
| 37 | if (type !== 4 || values !== 1) throw new PreviewError({ code: "tiff-metadata-pointer" }); |
| 38 | inspect(view.getUint32(entry + 8, littleEndian), depth + 1); |
| 39 | } |
| 40 | } |
| 41 | }; |
| 42 | inspect(view.getUint32(4, littleEndian), 0); |
| 43 | } |
| 44 | |
| 45 | function dimensions(width: number, height: number) { |
| 46 | if (!(width > 0 && height > 0) || width > 16384 || height > 16384 || width > MAX_PIXELS / height) |
| 47 | throw new PreviewError({ code: "image-too-large" }); |
| 48 | } |
| 49 | |
| 50 | function validateHeif(bytes: ArrayBuffer) { |
| 51 | const view = new DataView(bytes); |
| 52 | let found = false; |
| 53 | let count = 0; |
| 54 | const boxes = (start: number, end: number, depth: number) => { |
| 55 | if (depth > 4) return; |
| 56 | let offset = start; |
| 57 | while (offset + 8 <= end) { |
| 58 | if (++count > 2048) throw new PreviewError({ code: "image-metadata-too-large" }); |
| 59 | let size = view.getUint32(offset); |
| 60 | let header = 8; |
| 61 | if (size === 1) { |
| 62 | if (offset + 16 > end) throw new PreviewError({ code: "heif-metadata-invalid" }); |
| 63 | const large = view.getBigUint64(offset + 8); |
| 64 | if (large > BigInt(bytes.byteLength)) |
| 65 | throw new PreviewError({ code: "heif-metadata-invalid" }); |
| 66 | size = Number(large); |
| 67 | header = 16; |
| 68 | } else if (size === 0) size = end - offset; |
| 69 | if (size < header || offset + size > end) |
| 70 | throw new PreviewError({ code: "heif-metadata-invalid" }); |
| 71 | const type = view.getUint32(offset + 4); |
| 72 | if (type === 0x69737065) { |
| 73 | if (size < header + 12) throw new PreviewError({ code: "heif-dimensions-invalid" }); |
| 74 | dimensions(view.getUint32(offset + header + 4), view.getUint32(offset + header + 8)); |
| 75 | found = true; |
| 76 | } else if (type === 0x6d657461 || type === 0x69707270 || type === 0x6970636f) |
| 77 | boxes(offset + header + (type === 0x6d657461 ? 4 : 0), offset + size, depth + 1); |
| 78 | offset += size; |
| 79 | } |
| 80 | }; |
| 81 | boxes(0, bytes.byteLength, 0); |
| 82 | if (!found) throw new PreviewError({ code: "heif-no-dimensions" }); |
| 83 | } |
| 84 | |
| 85 | // Count LZW output without building strings before the decoder allocates its strip buffer. |
| 86 | function validateLzw(bytes: Uint8Array, limit: number) { |
| 87 | const lengths = new Uint16Array(4096).fill(1, 0, 256); |
| 88 | let bits = 9; |
| 89 | let bit = 0; |
| 90 | let next = 258; |
| 91 | let previous = 0; |
| 92 | let total = 0; |
| 93 | while (bit + bits <= bytes.length * 8) { |
| 94 | let code = 0; |
| 95 | for (let index = 0; index < bits; index++, bit++) |
| 96 | code = (code << 1) | ((bytes[bit >> 3]! >> (7 - (bit & 7))) & 1); |
| 97 | if (code === 257) return; |
| 98 | if (code === 256) { |
| 99 | bits = 9; |
| 100 | next = 258; |
| 101 | previous = 0; |
| 102 | continue; |
| 103 | } |
| 104 | const length = code < next ? lengths[code]! : code === next && previous ? previous + 1 : 0; |
| 105 | if (!length || (total += length) > limit) |
| 106 | throw new PreviewError({ code: "tiff-strip-invalid-or-oversized" }); |
| 107 | if (previous) { |
| 108 | if (next >= 4096) throw new PreviewError({ code: "tiff-lzw-invalid" }); |
| 109 | lengths[next++] = previous + 1; |
| 110 | if (next === (1 << bits) - 1 && bits < 12) bits++; |
| 111 | } |
| 112 | previous = length; |
| 113 | } |
| 114 | throw new PreviewError({ code: "tiff-strip-truncated" }); |
| 115 | } |
| 116 | |
| 117 | async function validateTiffStrips(bytes: ArrayBuffer, image: import("tiff").TiffIfd) { |
| 118 | const offsets = image.tiled ? image.tileOffsets : image.stripOffsets; |
| 119 | const counts = image.tiled ? image.tileByteCounts : image.stripByteCounts; |
| 120 | const width = image.tiled ? image.tileWidth! : image.width; |
| 121 | const height = image.tiled ? image.tileHeight! : Math.min(image.rowsPerStrip, image.height); |
| 122 | dimensions(width, height); |
| 123 | const limit = width * height * image.components * (image.bitsPerSample / 8); |
| 124 | if (!offsets?.length || offsets.length > 2048 || counts?.length !== offsets.length) |
| 125 | throw new PreviewError({ code: "tiff-strip-layout" }); |
| 126 | let expanded = 0; |
| 127 | for (let index = 0; index < offsets.length; index++) { |
| 128 | const offset = offsets[index]!; |
| 129 | const size = counts[index]!; |
| 130 | if (offset < 0 || size < 1 || offset + size > bytes.byteLength) |
| 131 | throw new PreviewError({ code: "tiff-strip-invalid" }); |
| 132 | const strip = new Uint8Array(bytes, offset, size); |
| 133 | if (image.compression === 5) validateLzw(strip, limit); |
| 134 | else if (image.compression === 8 || image.compression === 32946) { |
| 135 | const reader = new Blob([strip]) |
| 136 | .stream() |
| 137 | .pipeThrough(new DecompressionStream("deflate")) |
| 138 | .getReader(); |
| 139 | let length = 0; |
| 140 | try { |
| 141 | while (true) { |
| 142 | const { value, done } = await reader.read(); |
| 143 | if (done) break; |
| 144 | if ((length += value.length) > limit) |
| 145 | throw new PreviewError({ code: "tiff-strip-oversized" }); |
| 146 | } |
| 147 | } finally { |
| 148 | await reader.cancel(); |
| 149 | } |
| 150 | } else if (image.compression !== 1) throw new PreviewError({ code: "tiff-compression" }); |
| 151 | // Bound repeated strips and padding as well as the visible image dimensions. |
| 152 | expanded += limit; |
| 153 | if (expanded > 256 * 1024 * 1024) throw new PreviewError({ code: "tiff-data-too-large" }); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | async function decode(bytes: ArrayBuffer, heif: boolean) { |
| 158 | if (bytes.byteLength > 50 * 1024 * 1024) throw new PreviewError({ code: "file-too-large" }); |
| 159 | if (heif) { |
| 160 | validateHeif(bytes); |
| 161 | // Desktop CSP forbids the JavaScript evaluation used by heic-to/next. |
| 162 | const { heicTo } = await import("heic-to/csp"); |
| 163 | const bitmap = await heicTo({ blob: new Blob([bytes]), type: "bitmap" }); |
| 164 | try { |
| 165 | const canvas = new OffscreenCanvas(bitmap.width, bitmap.height); |
| 166 | const context = canvas.getContext("2d"); |
| 167 | if (!context) throw new PreviewError({ code: "image-decoding-unavailable" }); |
| 168 | context.drawImage(bitmap, 0, 0); |
| 169 | return await canvas.convertToBlob({ type: "image/png" }); |
| 170 | } finally { |
| 171 | bitmap.close(); |
| 172 | } |
| 173 | } |
| 174 | const { decode: decodeTiff } = await import("tiff"); |
| 175 | validateTiffMetadata(bytes); |
| 176 | const metadata = decodeTiff(bytes, { ignoreImageData: true, pages: [0] })[0]; |
| 177 | if (!metadata) throw new PreviewError({ code: "tiff-no-image" }); |
| 178 | dimensions(metadata.width, metadata.height); |
| 179 | if (metadata.orientation && metadata.orientation !== 1) |
| 180 | throw new PreviewError({ code: "tiff-orientation" }); |
| 181 | if ( |
| 182 | (metadata.tiled && metadata.components > 1) || |
| 183 | ![1, 2, 3, 4].includes(metadata.components) || |
| 184 | ![8, 16].includes(metadata.bitsPerSample) || |
| 185 | ![0, 1, 2, 3].includes(metadata.type) |
| 186 | ) |
| 187 | throw new PreviewError({ code: "tiff-color-format" }); |
| 188 | await validateTiffStrips(bytes, metadata); |
| 189 | const image = decodeTiff(bytes, { pages: [0] })[0]!; |
| 190 | const rgba = new Uint8ClampedArray(image.width * image.height * 4); |
| 191 | const scale = image.bitsPerSample === 16 ? 257 : 1; |
| 192 | const palette = image.palette; |
| 193 | for (let pixel = 0; pixel < image.width * image.height; pixel++) { |
| 194 | const index = pixel * image.components; |
| 195 | if (palette) { |
| 196 | const color = palette[image.data[index]!] ?? [0, 0, 0]; |
| 197 | for (let channel = 0; channel < 3; channel++) |
| 198 | rgba[pixel * 4 + channel] = color[channel]! / 257; |
| 199 | } else if (image.type === 2) { |
| 200 | for (let channel = 0; channel < 3; channel++) |
| 201 | rgba[pixel * 4 + channel] = image.data[index + channel]! / scale; |
| 202 | } else { |
| 203 | const gray = image.data[index]! / scale; |
| 204 | rgba.fill(gray, pixel * 4, pixel * 4 + 3); |
| 205 | } |
| 206 | rgba[pixel * 4 + 3] = image.alpha ? image.data[index + image.components - 1]! / scale : 255; |
| 207 | } |
| 208 | const canvas = new OffscreenCanvas(image.width, image.height); |
| 209 | const context = canvas.getContext("2d"); |
| 210 | if (!context) throw new PreviewError({ code: "image-decoding-unavailable" }); |
| 211 | context.putImageData(new ImageData(rgba, image.width, image.height), 0, 0); |
| 212 | return await canvas.convertToBlob({ type: "image/png" }); |
| 213 | } |
| 214 | |
| 215 | self.addEventListener("message", (event: MessageEvent<{ bytes: ArrayBuffer; heif: boolean }>) => { |
| 216 | void decode(event.data.bytes, event.data.heif).then( |
| 217 | (blob) => self.postMessage({ blob }, { transfer: [] }), |
| 218 | (cause: unknown) => |
| 219 | self.postMessage({ failure: previewFailure(cause, "image-decode-failed") }, { transfer: [] }), |
| 220 | ); |
| 221 | }); |