返回 DeepSeek-Reasonix
verify-windows-gui-subsystem.mjs
根目录 / scripts / verify-windows-gui-subsystem.mjs
1 #!/usr/bin/env node
2
3 import fs from "node:fs";
4 import path from "node:path";
5 import { fileURLToPath } from "node:url";
6
7 export const WINDOWS_GUI_SUBSYSTEM = 2;
8
9 export function readPESubsystem(bytes) {
10 const image = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
11 if (image.length < 0x40 || image.toString("ascii", 0, 2) !== "MZ") {
12 throw new Error("not a valid DOS/PE image");
13 }
14
15 const peOffset = image.readUInt32LE(0x3c);
16 const optionalHeaderOffset = peOffset + 24;
17 const subsystemOffset = optionalHeaderOffset + 68;
18 if (
19 peOffset > image.length - 24 ||
20 image.toString("binary", peOffset, peOffset + 4) !== "PE\0\0" ||
21 subsystemOffset > image.length - 2
22 ) {
23 throw new Error("not a valid PE image");
24 }
25
26 const optionalHeaderSize = image.readUInt16LE(peOffset + 20);
27 if (optionalHeaderSize < 70 || optionalHeaderOffset + optionalHeaderSize > image.length) {
28 throw new Error("invalid PE optional header");
29 }
30 const magic = image.readUInt16LE(optionalHeaderOffset);
31 if (magic !== 0x10b && magic !== 0x20b) {
32 throw new Error(`unsupported PE optional header magic 0x${magic.toString(16)}`);
33 }
34 return image.readUInt16LE(subsystemOffset);
35 }
36
37 export function verifyWindowsGUISubsystem(file) {
38 const subsystem = readPESubsystem(fs.readFileSync(file));
39 if (subsystem !== WINDOWS_GUI_SUBSYSTEM) {
40 throw new Error(
41 `${file}: expected Windows GUI subsystem ${WINDOWS_GUI_SUBSYSTEM}, got ${subsystem}`,
42 );
43 }
44 }
45
46 function main(files) {
47 if (files.length === 0) {
48 throw new Error("usage: verify-windows-gui-subsystem.mjs <exe> [exe ...]");
49 }
50 for (const file of files) {
51 verifyWindowsGUISubsystem(file);
52 console.log(`${file}: Windows GUI subsystem verified`);
53 }
54 }
55
56 if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
57 try {
58 main(process.argv.slice(2));
59 } catch (error) {
60 console.error(error instanceof Error ? error.message : error);
61 process.exitCode = 1;
62 }
63 }
64
64 lines Plain Text