'use strict'; // A zip archive written and read with Node alone (deflate from node:zlib): enough for a .vsix, // which is a zip, and for checking one. No dependencies. const zlib = require('node:zlib'); const CRC = (() => { const t = new Uint32Array(256); for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; t[n] = c >>> 0; } return t; })(); function crc32(buf) { let c = 0xffffffff; for (let i = 0; i < buf.length; i++) c = CRC[(c ^ buf[i]) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; } function dosTime(date) { const d = date || new Date(Date.UTC(2020, 0, 1)); const time = (d.getUTCHours() << 11) | (d.getUTCMinutes() << 5) | (d.getUTCSeconds() >> 1); const day = ((Math.max(1980, d.getUTCFullYear()) - 1980) << 9) | ((d.getUTCMonth() + 1) << 5) | d.getUTCDate(); return { time, day }; } /** * @param {{ name: string, data: Buffer }[]} files names with forward slashes * @param {{ date?: Date }} [options] one date for every entry, so the same input gives the same archive * @returns {Buffer} */ function zip(files, options) { const { time, day } = dosTime(options && options.date); const parts = []; const central = []; let offset = 0; const seen = new Set(); for (const f of files) { if (!f.name || f.name.startsWith('/') || f.name.includes('\\') || f.name.split('/').includes('..')) throw new Error('bad name in archive: ' + f.name); if (seen.has(f.name)) throw new Error('twice in archive: ' + f.name); seen.add(f.name); const name = Buffer.from(f.name, 'utf8'); const data = Buffer.isBuffer(f.data) ? f.data : Buffer.from(f.data); const packed = zlib.deflateRawSync(data, { level: 9 }); const stored = packed.length >= data.length; const body = stored ? data : packed; const crc = crc32(data); const local = Buffer.alloc(30); local.writeUInt32LE(0x04034b50, 0); local.writeUInt16LE(20, 4); // version needed local.writeUInt16LE(0x0800, 6); // names are UTF-8 local.writeUInt16LE(stored ? 0 : 8, 8); local.writeUInt16LE(time, 10); local.writeUInt16LE(day, 12); local.writeUInt32LE(crc, 14); local.writeUInt32LE(body.length, 18); local.writeUInt32LE(data.length, 22); local.writeUInt16LE(name.length, 26); local.writeUInt16LE(0, 28); parts.push(local, name, body); const entry = Buffer.alloc(46); entry.writeUInt32LE(0x02014b50, 0); entry.writeUInt16LE(20, 4); // made by entry.writeUInt16LE(20, 6); entry.writeUInt16LE(0x0800, 8); entry.writeUInt16LE(stored ? 0 : 8, 10); entry.writeUInt16LE(time, 12); entry.writeUInt16LE(day, 14); entry.writeUInt32LE(crc, 16); entry.writeUInt32LE(body.length, 20); entry.writeUInt32LE(data.length, 24); entry.writeUInt16LE(name.length, 28); entry.writeUInt32LE(offset, 42); central.push(entry, name); offset += local.length + name.length + body.length; } const dir = Buffer.concat(central); const end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50, 0); end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10); end.writeUInt32LE(dir.length, 12); end.writeUInt32LE(offset, 16); return Buffer.concat([...parts, dir, end]); } /** The entries of an archive, each checked against its checksum. */ function unzip(buf) { let end = -1; for (let i = buf.length - 22; i >= Math.max(0, buf.length - 22 - 65535); i--) { if (buf.readUInt32LE(i) === 0x06054b50) { end = i; break; } } if (end < 0) throw new Error('not a zip archive'); const count = buf.readUInt16LE(end + 10); let at = buf.readUInt32LE(end + 16); const out = []; for (let n = 0; n < count; n++) { if (buf.readUInt32LE(at) !== 0x02014b50) throw new Error('damaged archive directory'); const method = buf.readUInt16LE(at + 10); const crc = buf.readUInt32LE(at + 16); const packedSize = buf.readUInt32LE(at + 20); const size = buf.readUInt32LE(at + 24); const nameLength = buf.readUInt16LE(at + 28); const extra = buf.readUInt16LE(at + 30); const comment = buf.readUInt16LE(at + 32); const local = buf.readUInt32LE(at + 42); const name = buf.toString('utf8', at + 46, at + 46 + nameLength); if (buf.readUInt32LE(local) !== 0x04034b50) throw new Error('damaged entry: ' + name); const start = local + 30 + buf.readUInt16LE(local + 26) + buf.readUInt16LE(local + 28); const body = buf.subarray(start, start + packedSize); const data = method === 0 ? Buffer.from(body) : zlib.inflateRawSync(body); if (data.length !== size || crc32(data) !== crc) throw new Error('checksum of ' + name + ' differs'); out.push({ name, data }); at += 46 + nameLength + extra + comment; } return out; } module.exports = { zip, unzip, crc32 };