{"version":3,"file":"exe.mjs","names":["align","IDD_BASE_RELOCATION","IMAGE_SCN_CNT_CODE","IMAGE_SCN_CNT_INITIALIZED_DATA","IMAGE_SCN_CNT_UNINITIALIZED_DATA","exeSectionByAddress","exe","address","info","data","getAllSections","virtualAddress","virtualSize","exeCodeSection","s","newHeader","optionalHeader","baseOfCode","Error","exeAssertLastSection","index","name","section","getSectionByEntry","allSections","last","pointerToRawData","exeRemoveReloc","size","optionalHeaderDataDirectory","get","setSectionByEntry","set","exeUpdateSizes","fileAlignment","sizeOfCode","sizeOfInitializedData","sizeOfUninitializedData","characteristics","sizeOfRawData","Math","max"],"sources":["../../../../src/util/internal/windows/exe.ts"],"sourcesContent":["import {NtExecutable} from '@shockpkg/resedit';\n\nimport {align} from '../data.ts';\n\nimport {\n\tIDD_BASE_RELOCATION,\n\tIMAGE_SCN_CNT_CODE,\n\tIMAGE_SCN_CNT_INITIALIZED_DATA,\n\tIMAGE_SCN_CNT_UNINITIALIZED_DATA\n} from './constants.ts';\n\n/**\n * Get the EXE section that includes an address.\n *\n * @param exe NtExecutable instance.\n * @param address The address.\n * @returns The section or null if section not found.\n */\nexport function exeSectionByAddress(exe: NtExecutable, address: number) {\n\tfor (const {info, data} of exe.getAllSections()) {\n\t\tconst {virtualAddress, virtualSize} = info;\n\t\tif (\n\t\t\taddress >= virtualAddress &&\n\t\t\taddress < virtualAddress + virtualSize\n\t\t) {\n\t\t\treturn {\n\t\t\t\tinfo: info as {virtualAddress: number},\n\t\t\t\tdata\n\t\t\t};\n\t\t}\n\t}\n\treturn null;\n}\n\n/**\n * Get the EXE code section.\n *\n * @param exe NtExecutable instance.\n * @returns The section.\n */\nexport function exeCodeSection(exe: NtExecutable) {\n\tconst s = exeSectionByAddress(exe, exe.newHeader.optionalHeader.baseOfCode);\n\tif (!s || !s.data) {\n\t\tthrow new Error(`Invalid PE code section`);\n\t}\n\treturn {\n\t\tinfo: s.info,\n\t\tdata: s.data\n\t};\n}\n\n/**\n * Assert the given section is last section.\n *\n * @param exe NtExecutable instance.\n * @param index ImageDirectory index.\n * @param name Friendly name for messages.\n */\nexport function exeAssertLastSection(\n\texe: NtExecutable,\n\tindex: number,\n\tname: string\n) {\n\tconst section = exe.getSectionByEntry(index);\n\tif (!section) {\n\t\tthrow new Error(`Missing section: ${index}:${name}`);\n\t}\n\tconst allSections = exe.getAllSections();\n\tlet last = allSections[0].info;\n\tfor (const {info} of allSections) {\n\t\tif (info.pointerToRawData > last.pointerToRawData) {\n\t\t\tlast = info;\n\t\t}\n\t}\n\tconst {info} = section;\n\tif (info.pointerToRawData < last.pointerToRawData) {\n\t\tthrow new Error(`Not the last section: ${index}:${name}`);\n\t}\n}\n\n/**\n * Removes the reloc section if exists, fails if not the last section.\n *\n * @param exe NtExecutable instance.\n * @returns Restore function.\n */\nexport function exeRemoveReloc(exe: NtExecutable) {\n\tconst section = exe.getSectionByEntry(IDD_BASE_RELOCATION);\n\tif (!section) {\n\t\treturn () => {};\n\t}\n\tconst {size} =\n\t\texe.newHeader.optionalHeaderDataDirectory.get(IDD_BASE_RELOCATION);\n\texeAssertLastSection(exe, IDD_BASE_RELOCATION, '.reloc');\n\texe.setSectionByEntry(IDD_BASE_RELOCATION, null);\n\treturn () => {\n\t\texe.setSectionByEntry(IDD_BASE_RELOCATION, section);\n\t\tconst {virtualAddress} =\n\t\t\texe.newHeader.optionalHeaderDataDirectory.get(IDD_BASE_RELOCATION);\n\t\texe.newHeader.optionalHeaderDataDirectory.set(IDD_BASE_RELOCATION, {\n\t\t\tvirtualAddress,\n\t\t\tsize\n\t\t});\n\t};\n}\n\n/**\n * Update the sizes in EXE headers.\n *\n * @param exe NtExecutable instance.\n */\nexport function exeUpdateSizes(exe: NtExecutable) {\n\tconst {optionalHeader} = exe.newHeader;\n\tconst {fileAlignment} = optionalHeader;\n\tlet sizeOfCode = 0;\n\tlet sizeOfInitializedData = 0;\n\tlet sizeOfUninitializedData = 0;\n\tfor (const {\n\t\tinfo: {characteristics, sizeOfRawData, virtualSize}\n\t} of exe.getAllSections()) {\n\t\t// eslint-disable-next-line no-bitwise\n\t\tif (characteristics & IMAGE_SCN_CNT_CODE) {\n\t\t\tsizeOfCode += sizeOfRawData;\n\t\t}\n\t\t// eslint-disable-next-line no-bitwise\n\t\tif (characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) {\n\t\t\tsizeOfInitializedData += Math.max(\n\t\t\t\tsizeOfRawData,\n\t\t\t\talign(virtualSize, fileAlignment)\n\t\t\t);\n\t\t}\n\t\t// eslint-disable-next-line no-bitwise\n\t\tif (characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) {\n\t\t\tsizeOfUninitializedData += align(virtualSize, fileAlignment);\n\t\t}\n\t}\n\toptionalHeader.sizeOfCode = sizeOfCode;\n\toptionalHeader.sizeOfInitializedData = sizeOfInitializedData;\n\toptionalHeader.sizeOfUninitializedData = sizeOfUninitializedData;\n}\n"],"mappings":"AAEA,SAAQA,KAAK,QAAO,aAAY;AAEhC,SACCC,mBAAmB,EACnBC,kBAAkB,EAClBC,8BAA8B,EAC9BC,gCAAgC,QAC1B,iBAAgB;;AAEvB;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,mBAAmBA,CAACC,GAAiB,EAAEC,OAAe,EAAE;EACvE,KAAK,MAAM;IAACC,IAAI;IAAEC;EAAI,CAAC,IAAIH,GAAG,CAACI,cAAc,CAAC,CAAC,EAAE;IAChD,MAAM;MAACC,cAAc;MAAEC;IAAW,CAAC,GAAGJ,IAAI;IAC1C,IACCD,OAAO,IAAII,cAAc,IACzBJ,OAAO,GAAGI,cAAc,GAAGC,WAAW,EACrC;MACD,OAAO;QACNJ,IAAI,EAAEA,IAAgC;QACtCC;MACD,CAAC;IACF;EACD;EACA,OAAO,IAAI;AACZ;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASI,cAAcA,CAACP,GAAiB,EAAE;EACjD,MAAMQ,CAAC,GAAGT,mBAAmB,CAACC,GAAG,EAAE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