import { readFileSync } from 'fs'; import { join } from 'path'; import sharp from 'sharp'; import { deflateSync } from 'zlib'; const CRC_TABLE = Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) { c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; } return c >>> 0; }); function crc32(data: Buffer): number { let c = 0xffffffff; for (const byte of data) { c = CRC_TABLE[(c ^ byte) & 255] ^ (c >>> 8); } return (c ^ 0xffffffff) >>> 0; } export function pngChunk(type: string, data: Buffer): Buffer { const name = Buffer.from(type, 'latin1'); const length = Buffer.alloc(4); length.writeUInt32BE(data.length); const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(Buffer.concat([name, data]))); return Buffer.concat([length, name, data, crc]); } const SIGNATURE = Buffer.from('89504e470d0a1a0a', 'hex'); const END = pngChunk('IEND', Buffer.alloc(0)); function header(width: number, height: number, colorType = 2): Buffer { const data = Buffer.alloc(13); data.writeUInt32BE(width, 0); data.writeUInt32BE(height, 4); data[8] = 8; data[9] = colorType; return pngChunk('IHDR', data); } function rows(width: number, height: number, filter = 0): Buffer { const stride = width * 3 + 1; const raw = Buffer.alloc(stride * height, 7); for (let y = 0; y < height; y++) { raw[y * stride] = filter; } return raw; } export function png(width: number, height: number, extra: Buffer[] = []): Buffer { return Buffer.concat([ SIGNATURE, header(width, height), ...extra, pngChunk('IDAT', deflateSync(rows(width, height))), END, ]); } export function pngOfExactSize(size: number): Buffer { const base = png(4, 4); const padding = size - base.length - 12; return png(4, 4, [pngChunk('abCd', Buffer.alloc(padding))]); } export function pngHeaderOnly(width: number, height: number): Buffer { return Buffer.concat([ SIGNATURE, header(width, height), pngChunk('IDAT', deflateSync(Buffer.alloc(16))), END, ]); } export const SAMPLE_PNG = readFileSync( join(__dirname, '..', 'tests', 'fixtures', 'sample.png'), ); export const JPEG_WITHOUT_DATA = Buffer.from( 'ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9', 'hex', ); export async function jpeg(): Promise { const noise = Buffer.alloc(320 * 200 * 3); for (let i = 0; i < noise.length; i++) { noise[i] = (i * 7919) & 255; } return sharp(noise, { raw: { width: 320, height: 200, channels: 3 } }) .jpeg() .toBuffer(); } export function corruptedPngs(): Record { const lastRowFilter = rows(64, 64); lastRowFilter[63 * (64 * 3 + 1)] = 5; const flipped = Buffer.from(SAMPLE_PNG); flipped[100] ^= 255; return { 'первые 33 байта sample.png': SAMPLE_PNG.subarray(0, 33), 'половина файла': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length >> 1), 'без последних 20 байт': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length - 20), 'без IEND': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length - 12), 'неверный CRC': flipped, 'фильтр строки 5': pngFromRaw(1, 1, rows(1, 1, 5)), 'фильтр строки 255': pngFromRaw(1, 1, rows(1, 1, 255)), 'неверный фильтр в последней строке': pngFromRaw(64, 64, lastRowFilter), 'мусор в IDAT с верным CRC': Buffer.concat([ SIGNATURE, header(64, 64), pngChunk('IDAT', Buffer.alloc(40, 0xab)), END, ]), 'данных меньше, чем строк': pngFromRaw(64, 64, rows(64, 10)), 'палитра на 257 записей': Buffer.concat([ SIGNATURE, header(1, 1, 3), pngChunk('PLTE', Buffer.alloc(771)), pngChunk('IDAT', deflateSync(Buffer.from([0, 0]))), END, ]), }; } export function corruptedJpegs(valid: Buffer): Record { const garbled = Buffer.from(valid); for (let i = 800; i < garbled.length - 2; i += 3) { garbled[i] = 0x55; } return { '27 байт без таблиц и данных': JPEG_WITHOUT_DATA, 'половина файла': valid.subarray(0, valid.length >> 1), 'без последних 20 байт': valid.subarray(0, valid.length - 20), 'испорченные данные скана': garbled, }; } function pngFromRaw(width: number, height: number, raw: Buffer): Buffer { return Buffer.concat([ SIGNATURE, header(width, height), pngChunk('IDAT', deflateSync(raw)), END, ]); }