Skip to content

Commit

Permalink
Operate on a buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 15, 2023
1 parent 9635606 commit 34ea453
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/projector/otto/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,20 @@ export class ProjectorOttoWindows extends ProjectorOtto {
* @inheritdoc
*/
protected async _modifySkeleton() {
const {path} = this;
const iconData = await this.getIconData();
const {versionStrings} = this;
if (!(iconData || versionStrings)) {
return;
}

await peResourceReplace(this.path, {
iconData,
versionStrings
});
await writeFile(
path,
peResourceReplace(await readFile(path), {
iconData,
versionStrings
})
);

await this._patch3dDisplayDriversSize();
}
Expand Down
18 changes: 8 additions & 10 deletions src/util/windows.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {readFile, writeFile} from 'node:fs/promises';

import {signatureGet, signatureSet} from 'portable-executable-signature';
import {
NtExecutable,
Expand Down Expand Up @@ -221,19 +219,19 @@ function rsrcPatchVersion(
/**
* Replace resources in Windows PE file.
*
* @param path File path.
* @param data File data.
* @param options Replacement options.
* @returns Modified data.
*/
export async function peResourceReplace(
path: string,
export function peResourceReplace(
data: Readonly<Uint8Array>,
options: Readonly<IPeResourceReplace>
) {
const {iconData, versionStrings, removeSignature} = options;

// Read EXE file and remove signature if present.
const exeOriginal = await readFile(path);
const signedData = removeSignature ? null : signatureGet(exeOriginal);
let exeData = signatureSet(exeOriginal, null, true, true);
const signedData = removeSignature ? null : signatureGet(data);
let exeData = signatureSet(data, null, true, true);

// Parse EXE.
const exe = NtExecutable.from(exeData);
Expand Down Expand Up @@ -271,8 +269,8 @@ export async function peResourceReplace(
exeData = signatureSet(exeData, signedData, true, true);
}

// Write updated EXE file.
await writeFile(path, new Uint8Array(exeData));
// Return updated EXE file.
return new Uint8Array(exeData);
}

/**
Expand Down

0 comments on commit 34ea453

Please sign in to comment.