{"version":3,"file":"patch.mjs","names":["findFuzzy","PATCH_I386","EM_386","Patch","constructor","elf","_elf","_getShdrForAddress","addr","shdr","sectionHeaders","shAddr","shSize","_theShdrForAddress","Error","toString","_findFuzzyCode","find","elfHeader","eEntry","d","Uint8Array","data","i","_findEbx","bits","eMachine","v","DataView","before","getUint32","_readCstr","s","Number","e","length","String","fromCharCode","subarray"],"sources":["../../../../src/util/internal/linux/patch.ts"],"sourcesContent":["import {findFuzzy} from '../patch.ts';\n\nimport {PATCH_I386} from './asm.ts';\nimport {Elf32, Elf32Shdr, Elf64, EM_386} from './elf.ts';\n\ntype Unpacked<T> = T extends (infer U)[] ? U : T;\n\n/**\n * Patch object.\n */\nexport abstract class Patch<T extends Elf32 | Elf64> {\n\t/**\n\t * ELF object.\n\t */\n\tprotected _elf: T;\n\n\t/**\n\t * Patch constructor.\n\t *\n\t * @param elf ELF object.\n\t */\n\tconstructor(elf: T) {\n\t\tthis._elf = elf;\n\t}\n\n\t/**\n\t * Get shdr for address.\n\t *\n\t * @param addr The address.\n\t * @returns The shdr or null.\n\t */\n\tprotected _getShdrForAddress(addr: number | bigint) {\n\t\tfor (const shdr of this._elf.sectionHeaders) {\n\t\t\tconst {shAddr} = shdr;\n\t\t\tif (\n\t\t\t\taddr >= shAddr &&\n\t\t\t\taddr < (shAddr as number) + (shdr.shSize as number)\n\t\t\t) {\n\t\t\t\treturn shdr as Unpacked<T['sectionHeaders']>;\n\t\t\t}\n\t\t}\n\t\treturn null;\n\t}\n\n\t/**\n\t * The shdr for address or throw.\n\t *\n\t * @param addr The address.\n\t * @returns The shdr.\n\t */\n\tprotected _theShdrForAddress(addr: number | bigint) {\n\t\tconst shdr = this._getShdrForAddress(addr);\n\t\tif (!shdr) {\n\t\t\tthrow new Error(`No section at address: ${addr.toString()}`);\n\t\t}\n\t\treturn shdr;\n\t}\n\n\t/**\n\t * Fuzzy find in code.\n\t *\n\t * @param find Fuzzy find.\n\t * @yields The shdr and index in shdr.\n\t */\n\tprotected *_findFuzzyCode(find: number[]) {\n\t\tconst shdr = this._getShdrForAddress(this._elf.elfHeader.eEntry);\n\t\tif (!shdr) {\n\t\t\treturn;\n\t\t}\n\t\tconst d = new Uint8Array(shdr.data);\n\t\tfor (const i of findFuzzy(d, find)) {\n\t\t\tyield [shdr, i, d] as [\n\t\t\t\tUnpacked<T['sectionHeaders']>,\n\t\t\t\tnumber,\n\t\t\t\tUint8Array\n\t\t\t];\n\t\t}\n\t}\n\n\t/**\n\t * Get ebx for code at address.\n\t *\n\t * @param addr The address.\n\t * @returns The value of ebd or null if value not found.\n\t */\n\tprotected _findEbx(addr: number) {\n\t\tif (this._elf.bits !== 32 || this._elf.elfHeader.eMachine !== EM_386) {\n\t\t\tthrow new Error('Unsupported architecture');\n\t\t}\n\t\tconst shdr = this._theShdrForAddress(addr) as Elf32Shdr;\n\t\tconst d = new Uint8Array(shdr.data);\n\t\tconst v = new DataView(shdr.data);\n\t\tconst before = addr - shdr.shAddr;\n\t\tfor (const i of findFuzzy(d, PATCH_I386['ebx'], 0, before, true)) {\n\t\t\treturn shdr.shAddr + i + 5 + v.getUint32(i + 7, true);\n\t\t}\n\t\treturn null;\n\t}\n\n\t/**\n\t * Read C-String from address.\n\t *\n\t * @param addr String address.\n\t * @returns The C-String or null if invalid.\n\t */\n\tprotected _readCstr(addr: number | bigint) {\n\t\tconst shdr = this._getShdrForAddress(addr);\n\t\tif (!shdr) {\n\t\t\treturn null;\n\t\t}\n\t\tconst d = new Uint8Array(shdr.data);\n\t\tconst s = Number(addr) - Number(shdr.shAddr);\n\t\tconst e = d.length;\n\t\tfor (let i = s; i < e; i++) {\n\t\t\tif (!d[i]) {\n\t\t\t\t// eslint-disable-next-line unicorn/prefer-code-point\n\t\t\t\treturn String.fromCharCode(...d.subarray(s, i));\n\t\t\t}\n\t\t}\n\t\treturn null;\n\t}\n\n\t/**\n\t * Check patch.\n\t *\n\t * @returns True if valid patch, else false.\n\t */\n\tpublic abstract check(): boolean;\n\n\t/**\n\t * Apply patch.\n\t */\n\tpublic abstract patch(): void;\n}\n"],"mappings":"AAAA,SAAQA,SAAS,QAAO,cAAa;AAErC,SAAQC,UAAU,QAAO,WAAU;AACnC,SAAiCC,MAAM,QAAO,WAAU;AAIxD;AACA;AACA;AACA,OAAO,MAAeC,KAAK,CAA0B;EACpD;AACD;AACA;;EAGC;AACD;AACA;AACA;AACA;EACCC,WAAWA,CAACC,GAAM,EAAE;IACnB,IAAI,CAACC,IAAI,GAAGD,GAAG;EAChB;;EAEA;AACD;AACA;AACA;AACA;AACA;EACWE,kBAAkBA,CAACC,IAAqB,EAAE;IACnD,KAAK,MAAMC,IAAI,IAAI,IAAI,CAACH,IAAI,CAACI,cAAc,EAAE;MAC5C,MAAM;QAACC;MAAM,CAAC,GAAGF,IAAI;MACrB,IACCD,IAAI,IAAIG,MAAM,IACdH,IAAI,GAAIG,MAAM,GAAeF,IAAI,CAACG,MAAiB,EAClD;QACD,OAAOH,IAAI;MACZ;IACD;IACA,OAAO,IAAI;EACZ;;EAEA;AACD;AACA;AACA;AACA;AACA;EACWI,kBAAkBA,CAACL,IAAqB,EAAE;IACnD,MAAMC,IAAI,GAAG,IAAI,CAACF,kBAAkB,CAACC,IAAI,CAAC;IAC1C,IAAI,CAACC,IAAI,EAAE;MACV,MAAM,IAAIK,KAAK,CAAC,0BAA0BN,IAAI,CAACO,QAAQ,CAAC,CAAC,EAAE,CAAC;IAC7D;IACA,OAAON,IAAI;EACZ;;EAEA;AACD;AACA;AACA;AACA;AACA;EACC,CAAWO,cAAcA,CAACC,IAAc,EAAE;IACzC,MAAMR,IAAI,GAAG,IAAI,CAACF,kBAAkB,CAAC,IAAI,CAACD,IAAI,CAACY,SAAS,CAACC,MAAM,CAAC;IAChE,IAAI,CAACV,IAAI,EAAE;MACV;IACD;IACA,MAAMW,CAAC,GAAG,IAAIC,UAAU,CAACZ,IAAI,CAACa,IAAI,CAAC;IACnC,KAAK,MAAMC,CAAC,IAAIvB,SAAS,CAACoB,CA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