// File-format headers only: no decoding, vendor limits, processing or rendition policy. type ImageType = 'image/png' | 'image/gif' | 'image/jpeg' | 'image/webp'; type Dimensions = { width: number; height: number }; type ImageHeader = Dimensions & { jpeg?: { frameEnd: number; nonMarkerBytes: boolean; standaloneMarkers: boolean; fillBytes: boolean; shortSegments: boolean } }; const tag = (bytes: Uint8Array, at: number): string => String.fromCharCode(...bytes.subarray(at, at + 4)); /** Dimensions in a caller-declared image format, or undefined for an incomplete header. */ export function imageDimensions(bytes: Uint8Array, type: ImageType): ImageHeader | undefined { const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); if (type === 'image/png') return bytes.length >= 24 ? { width: view.getUint32(16), height: view.getUint32(20) } : undefined; if (type === 'image/gif') return bytes.length >= 10 ? { width: view.getUint16(6, true), height: view.getUint16(8, true) } : undefined; if (type === 'image/webp') { if (bytes.length < 30) return undefined; const chunk = tag(bytes, 12); if (chunk === 'VP8 ') return { width: view.getUint16(26, true) & 0x3fff, height: view.getUint16(28, true) & 0x3fff }; if (chunk === 'VP8L') { const bits = view.getUint32(21, true); return { width: 1 + (bits & 0x3fff), height: 1 + ((bits >>> 14) & 0x3fff) }; } if (chunk === 'VP8X') { const u24 = (at: number): number => bytes[at]! | (bytes[at + 1]! << 8) | (bytes[at + 2]! << 16); return { width: 1 + u24(24), height: 1 + u24(27) }; } return undefined; } let at = 2; const jpeg = { frameEnd: 0, nonMarkerBytes: false, standaloneMarkers: false, fillBytes: false, shortSegments: false }; while (at + 4 <= bytes.length) { if (bytes[at] !== 0xff) { jpeg.nonMarkerBytes = true; at += 1; continue; } const marker = bytes[at + 1]!; if (marker === 0xff) { jpeg.fillBytes = true; at += 1; continue; } if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) { jpeg.standaloneMarkers = true; at += 2; continue; } const length = view.getUint16(at + 2); if (length < 2) jpeg.shortSegments = true; if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) { return at + 9 <= bytes.length ? { width: view.getUint16(at + 7), height: view.getUint16(at + 5), jpeg: { ...jpeg, frameEnd: at + 9 } } : undefined; } if (marker === 0xda) return undefined; at += 2 + length; } return undefined; } /** An image's signature and dimensions, without deciding whether a vendor accepts that format or size. */ export function readImageHeader(bytes: Uint8Array): (ImageHeader & { type: ImageType }) | undefined { let type: ImageType; if (bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) type = 'image/png'; else if (tag(bytes, 0).startsWith('GIF')) type = 'image/gif'; else if (bytes[0] === 0xff && bytes[1] === 0xd8) type = 'image/jpeg'; else if (tag(bytes, 0) === 'RIFF' && tag(bytes, 8) === 'WEBP') type = 'image/webp'; else return undefined; const dimensions = imageDimensions(bytes, type); return dimensions ? { type, ...dimensions } : undefined; } /** ISO BMFF's bounded top-level boxes and movie headers, with raw duration and 16.16 track dimensions. */ export function readMp4Header(bytes: Uint8Array): { fileType: boolean; hasMediaData: boolean; movies: Array<{ headers: Array<{ timescale: number; duration: number; payloadSize: number }>; tracks: Dimensions[] }>; } { const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); function* boxes(from: number, to: number): Generator<{ type: string; start: number; end: number }> { let at = from; while (at + 8 <= to) { let size = view.getUint32(at); const type = tag(bytes, at + 4); let header = 8; if (size === 1) { if (at + 16 > to) return; size = Number(view.getBigUint64(at + 8)); header = 16; } if (size === 0) size = to - at; if (!Number.isSafeInteger(size) || size < header || size > to - at) return; yield { type, start: at + header, end: at + size }; at += size; } } const result: ReturnType = { fileType: bytes.length >= 8 && tag(bytes, 4) === 'ftyp', hasMediaData: false, movies: [] }; for (const box of boxes(0, bytes.length)) { if (box.type === 'mdat' && box.end > box.start) result.hasMediaData = true; if (box.type !== 'moov') continue; const movie: (typeof result.movies)[number] = { headers: [], tracks: [] }; result.movies.push(movie); for (const child of boxes(box.start, box.end)) { const { type, start, end } = child; if (type === 'mvhd' && start < end) { const version = bytes[start]; const payloadSize = end - start; if (version === 0 && payloadSize >= 20) movie.headers.push({ timescale: view.getUint32(start + 12), duration: view.getUint32(start + 16), payloadSize }); if (version === 1 && payloadSize >= 32) movie.headers.push({ timescale: view.getUint32(start + 20), duration: Number(view.getBigUint64(start + 24)), payloadSize }); } if (type !== 'trak') continue; for (const track of boxes(start, end)) { if (track.type !== 'tkhd' || track.start >= track.end) continue; const version = bytes[track.start]; const at = version === 1 ? track.start + 88 : version === 0 ? track.start + 76 : track.end; if (at + 8 > track.end) continue; const width = view.getUint32(at) / 65536; const height = view.getUint32(at + 4) / 65536; movie.tracks.push({ width, height }); } } } return result; }