From 362e5bcc6af5d9ce046b64805f4ff23dec915798 Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Tue, 2 Feb 2021 20:22:58 -0600 Subject: [PATCH] Import cstr and segment functions into macho --- helpers/macho/macho.cstr.js | 22 ++--- helpers/macho/macho.js | 2 + helpers/macho/macho.segment.js | 143 +++++++++++++++++---------------- 3 files changed, 87 insertions(+), 80 deletions(-) diff --git a/helpers/macho/macho.cstr.js b/helpers/macho/macho.cstr.js index fb21969..4c94b5c 100644 --- a/helpers/macho/macho.cstr.js +++ b/helpers/macho/macho.cstr.js @@ -1,10 +1,12 @@ -var Cstr = function Cstr(buf) { - this.toString = function() { - return JSON.stringify(this); - }; - var cstr = ''; - for(var i = 0; i < buf.length; i++) { - cstr += String.fromCharCode(buf[i]); - } - return cstr; -}; \ No newline at end of file +var Cstr = function Cstr(buf) { + this.toString = function() { + return JSON.stringify(this); + }; + var cstr = ''; + for(var i = 0; i < buf.length; i++) { + cstr += String.fromCharCode(buf[i]); + } + return cstr; +}; + +export default Cstr diff --git a/helpers/macho/macho.js b/helpers/macho/macho.js index b4593ad..32d7e7d 100644 --- a/helpers/macho/macho.js +++ b/helpers/macho/macho.js @@ -11,6 +11,8 @@ import { MachoHeader64, MachoHeader } from './macho.header.js' import { LOAD_COMMAND_TYPE, LoadCommand } from './macho.loadcommand.js' import { CPU_TYPE, CPU_SUB_TYPE } from './macho.cpu.js' import { FILE_FLAGS, FILE_TYPE } from './macho.file.js' +import Cstr from './macho.cstr.js' +import { SegmentCommand } from './macho.segment.js' function default_callback(buffer) { console.log('Received ' + buffer.byteLength / (1024 * 1024) + ' MB'); diff --git a/helpers/macho/macho.segment.js b/helpers/macho/macho.segment.js index 379c215..7ae5530 100644 --- a/helpers/macho/macho.segment.js +++ b/helpers/macho/macho.segment.js @@ -1,70 +1,73 @@ -//Global Constants -var SG_FLAGS = []; - -let SG_FLAG = { - SG_HIGHVM: 0x1, - SG_FVMLIB: 0x2, - SG_NORELOC: 0x4, - - DESCRIPTION: function(search) { - let result = SG_FLAGS[search]; - return (result != undefined) ? result : search; - }, - - toString: function() { - return JSON.stringify(this); - } -}; - -SG_FLAGS[SG_FLAG.SG_HIGHVM] = 'SG_HIGHVM'; -SG_FLAGS[SG_FLAG.SG_FVMLIB] = 'SG_FVMLIB'; -SG_FLAGS[SG_FLAG.SG_NORELOC] = 'SG_NORELOC'; - -let SEGMENT = { - SEG_PAGEZERO: "__PAGEZERO", - SEG_TEXT: "__TEXT", - SEG_ICON: "__ICON", - SEG_OBJC: "__OBJC", - SEG_LINKEDIT: "__LINKEDIT", - SEG_UNIXSTACK: "__UNIXSTACK", - SEG_DATA: "__DATA", - toString: function() { - return JSON.stringify(this); - } -}; - -//32-bits segment command -var SegmentCommand = function SegmentCommand(type, size, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) { - this.cmd = type || 0x00000000; - this.cmdsize = size || 0x00000000; - this.segname = new TextEncoder("utf-8").encode(segname) || new Uint8Array(16); - this.vmaddr = vmaddr || 0x00000000; - this.vmsize = vmsize || 0x00000000; - this.fileoff = fileoff || 0x00000000; - this.filesize = filesize || 0x00000000; - this.maxprot = maxprot || 0x00000000; - this.initprot = initprot || 0x00000000; - this.nsects = nsects || 0x00000000; - this.flags = flags || 0x00000000; - this.toString = function() { - return JSON.stringify(this); - }; -}; - -//64-bits segment command -var SegmentCommand64 = function SegmentCommand64(cmd, cmdsize, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) { - this.cmd = cmd || 0x00000000; - this.cmdsize = cmdsize || 0x00000000; - this.segname = new TextEncoder("utf-8").encode(segname) || new Uint8Array(16); - this.vmaddr = vmaddr || 0x0000000000000000; - this.vmsize = vmsize || 0x0000000000000000; - this.fileoff = fileoff || 0x0000000000000000; - this.filesize = filesize || 0x0000000000000000; - this.maxprot = maxprot || 0x00000000; - this.initprot = initprot || 0x00000000; - this.nsects = nsects || 0x00000000; - this.flags = flags || 0x00000000; - this.toString = function() { - return JSON.stringify(this); - }; -}; \ No newline at end of file +//Global Constants +var SG_FLAGS = []; + +let SG_FLAG = { + SG_HIGHVM: 0x1, + SG_FVMLIB: 0x2, + SG_NORELOC: 0x4, + + DESCRIPTION: function(search) { + let result = SG_FLAGS[search]; + return (result != undefined) ? result : search; + }, + + toString: function() { + return JSON.stringify(this); + } +}; + +SG_FLAGS[SG_FLAG.SG_HIGHVM] = 'SG_HIGHVM'; +SG_FLAGS[SG_FLAG.SG_FVMLIB] = 'SG_FVMLIB'; +SG_FLAGS[SG_FLAG.SG_NORELOC] = 'SG_NORELOC'; + +let SEGMENT = { + SEG_PAGEZERO: "__PAGEZERO", + SEG_TEXT: "__TEXT", + SEG_ICON: "__ICON", + SEG_OBJC: "__OBJC", + SEG_LINKEDIT: "__LINKEDIT", + SEG_UNIXSTACK: "__UNIXSTACK", + SEG_DATA: "__DATA", + toString: function() { + return JSON.stringify(this); + } +}; + +//32-bits segment command +var SegmentCommand = function SegmentCommand(type, size, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) { + this.cmd = type || 0x00000000; + this.cmdsize = size || 0x00000000; + this.segname = new TextEncoder("utf-8").encode(segname) || new Uint8Array(16); + this.vmaddr = vmaddr || 0x00000000; + this.vmsize = vmsize || 0x00000000; + this.fileoff = fileoff || 0x00000000; + this.filesize = filesize || 0x00000000; + this.maxprot = maxprot || 0x00000000; + this.initprot = initprot || 0x00000000; + this.nsects = nsects || 0x00000000; + this.flags = flags || 0x00000000; + this.toString = function() { + return JSON.stringify(this); + }; +}; + +//64-bits segment command +var SegmentCommand64 = function SegmentCommand64(cmd, cmdsize, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) { + this.cmd = cmd || 0x00000000; + this.cmdsize = cmdsize || 0x00000000; + this.segname = new TextEncoder("utf-8").encode(segname) || new Uint8Array(16); + this.vmaddr = vmaddr || 0x0000000000000000; + this.vmsize = vmsize || 0x0000000000000000; + this.fileoff = fileoff || 0x0000000000000000; + this.filesize = filesize || 0x0000000000000000; + this.maxprot = maxprot || 0x00000000; + this.initprot = initprot || 0x00000000; + this.nsects = nsects || 0x00000000; + this.flags = flags || 0x00000000; + this.toString = function() { + return JSON.stringify(this); + }; +}; + + +export { SG_FLAGS, SG_FLAG, SEGMENT, SegmentCommand, SegmentCommand64 }