Skip to content

Commit

Permalink
Added MachOImage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Jan 20, 2025
1 parent f9d63e2 commit 67f4911
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
31 changes: 31 additions & 0 deletions macho/machoimage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assertEquals, assertStrictEquals } from '@std/assert';
import { MH_MAGIC } from '../const.ts';
import { LoadCommand } from '../mach/loadcommand.ts';
import { MachHeader } from '../mach/machheader.ts';
import { MachOImage } from './machoimage.ts';

Deno.test('constructor', () => {
const buffer = new ArrayBuffer(
MachHeader.BYTE_LENGTH + LoadCommand.BYTE_LENGTH,
);
const header = new MachHeader(buffer);
header.magic = MH_MAGIC;
header.ncmds = 1;
header.sizeofcmds = LoadCommand.BYTE_LENGTH;

{
const macho = new MachOImage(buffer);
const address = macho.address();
assertEquals(address.byteOffset, 0);
assertStrictEquals(address.buffer, buffer);
}

{
const data = new Uint8Array(buffer.byteLength + 2);
data.subarray(1).set(new Uint8Array(buffer));
const macho = new MachOImage(data.subarray(1));
const address = macho.address();
assertEquals(address.byteOffset, 1);
assertStrictEquals(address.buffer, data.buffer);
}
});
42 changes: 42 additions & 0 deletions macho/machoimage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { ArrayBufferReal, BufferPointer } from '@hqtsm/struct';
import { MachOBase } from './machobase.ts';

/**
* A Mach-O binary over a buffer.
*/
export class MachOImage extends MachOBase {
declare public readonly ['constructor']: Omit<typeof MachOImage, 'new'>;

/**
* Construct a Mach-O binary over a buffer.
*
* @param address Buffer pointer.
*/
constructor(address: ArrayBufferReal | BufferPointer) {
super();

this.initHeader(address);

let buffer;
let byteOffset = this.headerSize();
if ('buffer' in address) {
buffer = address.buffer;
byteOffset += address.byteOffset;
} else {
buffer = address;
}
this.initCommands({
buffer,
byteOffset,
});
}

/**
* Get address of Mach-O.
*
* @returns Pionter to Mach-O header.
*/
public address(): BufferPointer {
return this.header()!;
}
}
1 change: 1 addition & 0 deletions macho/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './architecture.ts';
export * from './macho.ts';
export * from './machobase.ts';
export * from './machoimage.ts';
export * from './universal.ts';

0 comments on commit 67f4911

Please sign in to comment.