Skip to content

Commit

Permalink
Avoid reading entire file
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 10, 2023
1 parent b2c77a2 commit 4987d79
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
Binary file added spec/fixtures/macho-head/fat-ppc-i386.bin
Binary file not shown.
Binary file added spec/fixtures/macho-head/thin-i386.bin
Binary file not shown.
Binary file added spec/fixtures/macho-head/thin-ppc.bin
Binary file not shown.
30 changes: 12 additions & 18 deletions src/util/mac.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import {describe, it} from 'node:test';
import {deepStrictEqual, strictEqual} from 'node:assert';
import {createHash} from 'node:crypto';

import {machoAppLauncher, machoTypesData} from './mac';
import {fixtureFile} from '../util.spec';

const unhex = (hex: string) => Buffer.from(hex.replace(/\s/g, ''), 'hex');
import {machoAppLauncher, machoTypesData, machoTypesFile} from './mac';

function sha256(data: Buffer) {
return createHash('sha256').update(data).digest('hex');
}

const machoTypes = [
{
name: 'slim: ppc',
data: unhex('FE ED FA CE 00 00 00 12 00 00 00 0A'),
name: 'thin-ppc',
format: {
cpuType: 0x00000012,
cpuSubtype: 10
Expand All @@ -22,8 +21,7 @@ const machoTypes = [
'17414c123fe82ac74a89fad9c80e36d8b612ded5a520e35f3c33eabe75a023a7'
},
{
name: 'slim: i386',
data: unhex('CE FA ED FE 07 00 00 00 03 00 00 00'),
name: 'thin-i386',
format: {
cpuType: 0x00000007,
cpuSubtype: 3
Expand All @@ -32,14 +30,7 @@ const machoTypes = [
'e52e19fce336130824dcfd4731bf85db7e8e96628ef8c6a49769dc5247ef6ed0'
},
{
name: 'fat: ppc, i386',
data: unhex(
[
'CA FE BA BE 00 00 00 02',
'00 00 00 12 00 00 00 0A 00 00 00 00 00 00 00 00 00 00 00 00',
'00 00 00 07 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 00'
].join('')
),
name: 'fat-ppc-i386',
format: [
{
cpuType: 0x00000012,
Expand All @@ -56,10 +47,13 @@ const machoTypes = [
];

void describe('util/mac', () => {
void describe('machoTypesData', () => {
for (const {name, data, format} of machoTypes) {
void it(name, () => {
deepStrictEqual(machoTypesData(data), format);
void describe('machoTypesFile', () => {
for (const {name, format} of machoTypes) {
void it(name, async () => {
deepStrictEqual(
await machoTypesFile(fixtureFile(`macho-head/${name}.bin`)),
format
);
});
}
});
Expand Down
24 changes: 22 additions & 2 deletions src/util/mac.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {readFile} from 'node:fs/promises';
import {open} from 'node:fs/promises';

import {launcher} from '../util';

Expand Down Expand Up @@ -88,7 +88,27 @@ export function machoTypesData(data: Readonly<Buffer>) {
* @returns Mach-O types.
*/
export async function machoTypesFile(path: string) {
return machoTypesData(await readFile(path));
let data;
const f = await open(path, 'r');
try {
const h = 8;
const head = Buffer.alloc(h);
const {bytesRead} = await f.read(head, 0, h, 0);
if (bytesRead < h) {
data = head.subarray(0, bytesRead);
} else {
const n =
head.readUInt32BE(0) === FAT_MAGIC
? head.readUInt32BE(4) * 20
: 4;
const d = Buffer.alloc(n);
const {bytesRead} = await f.read(d, 0, n, h);
data = Buffer.concat([head, d.subarray(0, bytesRead)]);
}
} finally {
await f.close();
}
return machoTypesData(data);
}

/**
Expand Down

0 comments on commit 4987d79

Please sign in to comment.