| 1 | import { createHash } from "node:crypto"; |
| 2 | import { execFileSync } from "node:child_process"; |
| 3 | import { readFileSync } from "node:fs"; |
| 4 | |
| 5 | const [executable, icon, sevenZip] = process.argv.slice(2); |
| 6 | if (!executable || !icon || !sevenZip) { |
| 7 | throw new Error("Usage: verify-windows-installer-icon.mjs INSTALLER.exe ICON.ico SEVEN_ZIP"); |
| 8 | } |
| 9 | |
| 10 | function frames(bytes) { |
| 11 | if (bytes.length < 6 || bytes.readUInt16LE(0) !== 0 || bytes.readUInt16LE(2) !== 1) { |
| 12 | throw new Error("Invalid Windows icon file."); |
| 13 | } |
| 14 | const count = bytes.readUInt16LE(4); |
| 15 | if (!count || bytes.length < 6 + count * 16) throw new Error("Missing Windows icon images."); |
| 16 | const result = new Map(); |
| 17 | for (let index = 0; index < count; index++) { |
| 18 | const entry = 6 + index * 16; |
| 19 | const size = bytes.readUInt32LE(entry + 8); |
| 20 | const offset = bytes.readUInt32LE(entry + 12); |
| 21 | const key = `${bytes[entry] || 256}x${bytes[entry + 1] || 256}`; |
| 22 | if (offset + size > bytes.length) throw new Error("Truncated Windows icon image."); |
| 23 | result.set(key, createHash("sha256").update(bytes.subarray(offset, offset + size)).digest("hex")); |
| 24 | } |
| 25 | return result; |
| 26 | } |
| 27 | |
| 28 | function resource(...names) { |
| 29 | return execFileSync(sevenZip, ["e", "-tPE", "-so", executable, ...names], { |
| 30 | maxBuffer: 2 * 1024 * 1024, |
| 31 | stdio: ["ignore", "pipe", "pipe"], |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | const expected = frames(readFileSync(icon)); |
| 36 | |
| 37 | const group = resource(".rsrc/GROUP_ICON/103"); |
| 38 | if (group.length !== 6 + expected.size * 14 || group.readUInt32LE(0) !== 0x00010000 || group.readUInt16LE(4) !== expected.size) { |
| 39 | throw new Error("Installer icon sizes differ from the Cage icon."); |
| 40 | } |
| 41 | const seen = new Set(); |
| 42 | for (let index = 0; index < expected.size; index++) { |
| 43 | const entry = 6 + index * 14; |
| 44 | const key = `${group[entry] || 256}x${group[entry + 1] || 256}`; |
| 45 | const id = group.readUInt16LE(entry + 12); |
| 46 | |
| 47 | const bytes = resource(`.rsrc/ICON/${id}`, `.rsrc/ICON/${id}.ico`); |
| 48 | const actual = bytes.length >= 6 && bytes.readUInt32LE(0) === 0x00010000 |
| 49 | ? frames(bytes).get(key) |
| 50 | : createHash("sha256").update(bytes).digest("hex"); |
| 51 | if (seen.has(key) || !expected.has(key) || actual !== expected.get(key)) { |
| 52 | throw new Error(`Installer icon ${key} differs from the Cage logo.`); |
| 53 | } |
| 54 | seen.add(key); |
| 55 | } |
| 56 | console.log(`Verified ${expected.size} Cage installer icon sizes.`); |