Import cstr and segment functions into macho

This commit is contained in:
Sam Carlton 2021-02-02 20:22:58 -06:00
parent b6d2316313
commit 362e5bcc6a
3 changed files with 87 additions and 80 deletions

View file

@ -1,10 +1,12 @@
var Cstr = function Cstr(buf) { var Cstr = function Cstr(buf) {
this.toString = function() { this.toString = function() {
return JSON.stringify(this); return JSON.stringify(this);
}; };
var cstr = ''; var cstr = '';
for(var i = 0; i < buf.length; i++) { for(var i = 0; i < buf.length; i++) {
cstr += String.fromCharCode(buf[i]); cstr += String.fromCharCode(buf[i]);
} }
return cstr; return cstr;
}; };
export default Cstr

View file

@ -11,6 +11,8 @@ import { MachoHeader64, MachoHeader } from './macho.header.js'
import { LOAD_COMMAND_TYPE, LoadCommand } from './macho.loadcommand.js' import { LOAD_COMMAND_TYPE, LoadCommand } from './macho.loadcommand.js'
import { CPU_TYPE, CPU_SUB_TYPE } from './macho.cpu.js' import { CPU_TYPE, CPU_SUB_TYPE } from './macho.cpu.js'
import { FILE_FLAGS, FILE_TYPE } from './macho.file.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) { function default_callback(buffer) {
console.log('Received ' + buffer.byteLength / (1024 * 1024) + ' MB'); console.log('Received ' + buffer.byteLength / (1024 * 1024) + ' MB');

View file

@ -1,70 +1,73 @@
//Global Constants //Global Constants
var SG_FLAGS = []; var SG_FLAGS = [];
let SG_FLAG = { let SG_FLAG = {
SG_HIGHVM: 0x1, SG_HIGHVM: 0x1,
SG_FVMLIB: 0x2, SG_FVMLIB: 0x2,
SG_NORELOC: 0x4, SG_NORELOC: 0x4,
DESCRIPTION: function(search) { DESCRIPTION: function(search) {
let result = SG_FLAGS[search]; let result = SG_FLAGS[search];
return (result != undefined) ? result : search; return (result != undefined) ? result : search;
}, },
toString: function() { toString: function() {
return JSON.stringify(this); return JSON.stringify(this);
} }
}; };
SG_FLAGS[SG_FLAG.SG_HIGHVM] = 'SG_HIGHVM'; SG_FLAGS[SG_FLAG.SG_HIGHVM] = 'SG_HIGHVM';
SG_FLAGS[SG_FLAG.SG_FVMLIB] = 'SG_FVMLIB'; SG_FLAGS[SG_FLAG.SG_FVMLIB] = 'SG_FVMLIB';
SG_FLAGS[SG_FLAG.SG_NORELOC] = 'SG_NORELOC'; SG_FLAGS[SG_FLAG.SG_NORELOC] = 'SG_NORELOC';
let SEGMENT = { let SEGMENT = {
SEG_PAGEZERO: "__PAGEZERO", SEG_PAGEZERO: "__PAGEZERO",
SEG_TEXT: "__TEXT", SEG_TEXT: "__TEXT",
SEG_ICON: "__ICON", SEG_ICON: "__ICON",
SEG_OBJC: "__OBJC", SEG_OBJC: "__OBJC",
SEG_LINKEDIT: "__LINKEDIT", SEG_LINKEDIT: "__LINKEDIT",
SEG_UNIXSTACK: "__UNIXSTACK", SEG_UNIXSTACK: "__UNIXSTACK",
SEG_DATA: "__DATA", SEG_DATA: "__DATA",
toString: function() { toString: function() {
return JSON.stringify(this); return JSON.stringify(this);
} }
}; };
//32-bits segment command //32-bits segment command
var SegmentCommand = function SegmentCommand(type, size, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) { var SegmentCommand = function SegmentCommand(type, size, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) {
this.cmd = type || 0x00000000; this.cmd = type || 0x00000000;
this.cmdsize = size || 0x00000000; this.cmdsize = size || 0x00000000;
this.segname = new TextEncoder("utf-8").encode(segname) || new Uint8Array(16); this.segname = new TextEncoder("utf-8").encode(segname) || new Uint8Array(16);
this.vmaddr = vmaddr || 0x00000000; this.vmaddr = vmaddr || 0x00000000;
this.vmsize = vmsize || 0x00000000; this.vmsize = vmsize || 0x00000000;
this.fileoff = fileoff || 0x00000000; this.fileoff = fileoff || 0x00000000;
this.filesize = filesize || 0x00000000; this.filesize = filesize || 0x00000000;
this.maxprot = maxprot || 0x00000000; this.maxprot = maxprot || 0x00000000;
this.initprot = initprot || 0x00000000; this.initprot = initprot || 0x00000000;
this.nsects = nsects || 0x00000000; this.nsects = nsects || 0x00000000;
this.flags = flags || 0x00000000; this.flags = flags || 0x00000000;
this.toString = function() { this.toString = function() {
return JSON.stringify(this); return JSON.stringify(this);
}; };
}; };
//64-bits segment command //64-bits segment command
var SegmentCommand64 = function SegmentCommand64(cmd, cmdsize, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) { var SegmentCommand64 = function SegmentCommand64(cmd, cmdsize, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) {
this.cmd = cmd || 0x00000000; this.cmd = cmd || 0x00000000;
this.cmdsize = cmdsize || 0x00000000; this.cmdsize = cmdsize || 0x00000000;
this.segname = new TextEncoder("utf-8").encode(segname) || new Uint8Array(16); this.segname = new TextEncoder("utf-8").encode(segname) || new Uint8Array(16);
this.vmaddr = vmaddr || 0x0000000000000000; this.vmaddr = vmaddr || 0x0000000000000000;
this.vmsize = vmsize || 0x0000000000000000; this.vmsize = vmsize || 0x0000000000000000;
this.fileoff = fileoff || 0x0000000000000000; this.fileoff = fileoff || 0x0000000000000000;
this.filesize = filesize || 0x0000000000000000; this.filesize = filesize || 0x0000000000000000;
this.maxprot = maxprot || 0x00000000; this.maxprot = maxprot || 0x00000000;
this.initprot = initprot || 0x00000000; this.initprot = initprot || 0x00000000;
this.nsects = nsects || 0x00000000; this.nsects = nsects || 0x00000000;
this.flags = flags || 0x00000000; this.flags = flags || 0x00000000;
this.toString = function() { this.toString = function() {
return JSON.stringify(this); return JSON.stringify(this);
}; };
}; };
export { SG_FLAGS, SG_FLAG, SEGMENT, SegmentCommand, SegmentCommand64 }