def process(fname): ic = open(fname,'rb') encrypted, d, offset = Mjo.header(ic) #Get entrypoint details ic.seek(0x10) primary = Util.read_int(ic) ic.seek(0x18) record_count = Util.read_int(ic) entrypoints = {} for i in range(record_count): code = Util.read_int32(ic) offs = Util.read_int(ic) entrypoints[offs] = (code, offs == primary) #Get generic channel for bytecode if encrypted: s = Mjo.crypt(ic) bc = strio(s) ic.seek(0) open(fname+'.dec','wb').write(ic.read(offset) + s) else: ic.seek(offset) s = ic.read() bc = strio(s) endPos = len(s) - 1 #Read commands cmds = [] targets = {} # try: if 1: while bc.tell()<endPos: cpos = bc.tell() cmd = read_cmd(bc, cpos + offset) cmds.append((cpos, cmd)) if cmd.__class__ == Cmd: for i in cmd[2]: targets[i] = 0 # except: if 0: print 'warning: this mjo may be truncated.' import pdb pdb.set_trace() #Order labels targets = targets.keys() targets.sort(reverse=True) labels = {} for n in range(len(targets)): labels[targets[n]] = n + 1 #Open output file dir, basename = os.path.split(fname) output_dir = g_output_dir if not output_dir: output_dir = dir noext = os.path.splitext(basename)[0] oc = open(output_dir + '\\' + noext + '.mjs', 'w') res = resstrs(output_dir + '\\' + noext + '.sjs') #Output code def do_lbl(pos): if pos in labels: oc.write('\n @%d\n'%labels[pos]) res.finalise() if pos in targets: targets.remove(pos) def do_pos(pos): if include_offsets == 'Bytecode': oc.write("{-%04x-} "%pos) elif include_offsets == 'File': oc.write("{-%04x-} "%(pos+offset)) stack = [] def pop_all(): if stack: res.finalise() while stack: pos, elt = stack.pop() do_lbl(pos) do_pos(pos) oc.write('push %s\n'%elt) def pop_n(n, clear = True): skip = None l = [] for n in range(n): if skip or not stack: l.append('pop') else: pos, elt = stack.pop() if not skip and pos in labels: skip = pos l.append(elt) if clear or skip: pop_all() if skip: do_lbl(skip) return ', '.join(l) for pos, cmd in cmds: if pos in entrypoints: code, is_entry = entrypoints[pos] del entrypoints[pos] f = "function" if is_entry: f = "entrypoint" oc.write("\n#%s $%08lx\n"%(f, code&0xFFFFFFFF)) res.finalise() if cmd == None: pop_all() do_lbl(pos) elif cmd.__class__ == Stack: s, n, b = cmd res.finalise() if n: s = "%s(%s)"%(s,pop_n(n, False)) elif b: if res.enabled: s=res.add_whole(s) else: s = repr(s) stack.append((pos,s)) elif cmd.__class__ == Cmd: s, n, l = cmd if n: args = " (%s)"%pop_n(n) else: args = "" pop_all() do_lbl(pos) do_pos(pos) res.finalise() oc.write(s) oc.write(args) if len(l) == 1: oc.write(' @%d'%labels[l[0]]) elif l: oc.write(' { %s }'%', '.join(['@%d'%labels[n] for n in l])) oc.write('\n') else:#text pop_all() do_lbl(pos) do_pos(pos) if res.enabled: r = res.add_part(cmd) if r: oc.write("%s\n"%r) else: oc.write(repr(cmd)) pop_all() #Clean up if targets: print "Warning: labels not generated: %s" % ", ".join(['@%d'%labels[n] for n in targets]) if entrypoints: print "Warning: entrypoints not generated: %s"%", ".join(['$%08lx'%entrypoints[n][0]&0xFFFFFFFF for n in entrypoints]) oc.close() res.close()
def process(fname): ic = open(fname, 'rb') encrypted, d, offset = Mjo.header(ic) #Get entrypoint details ic.seek(0x10) primary = Util.read_int(ic) ic.seek(0x18) record_count = Util.read_int(ic) entrypoints = {} for i in range(record_count): code = Util.read_int32(ic) offs = Util.read_int(ic) entrypoints[offs] = (code, offs == primary) #Get generic channel for bytecode if encrypted: s = Mjo.crypt(ic) bc = strio(s) ic.seek(0) open(fname + '.dec', 'wb').write(ic.read(offset) + s) else: ic.seek(offset) s = ic.read() bc = strio(s) endPos = len(s) - 1 #Read commands cmds = [] targets = {} # try: if 1: while bc.tell() < endPos: cpos = bc.tell() cmd = read_cmd(bc, cpos + offset) cmds.append((cpos, cmd)) if cmd.__class__ == Cmd: for i in cmd[2]: targets[i] = 0 # except: if 0: print 'warning: this mjo may be truncated.' import pdb pdb.set_trace() #Order labels targets = targets.keys() targets.sort(reverse=True) labels = {} for n in range(len(targets)): labels[targets[n]] = n + 1 #Open output file dir, basename = os.path.split(fname) output_dir = g_output_dir if not output_dir: output_dir = dir noext = os.path.splitext(basename)[0] oc = open(output_dir + '\\' + noext + '.mjs', 'w') res = resstrs(output_dir + '\\' + noext + '.sjs') #Output code def do_lbl(pos): if pos in labels: oc.write('\n @%d\n' % labels[pos]) res.finalise() if pos in targets: targets.remove(pos) def do_pos(pos): if include_offsets == 'Bytecode': oc.write("{-%04x-} " % pos) elif include_offsets == 'File': oc.write("{-%04x-} " % (pos + offset)) stack = [] def pop_all(): if stack: res.finalise() while stack: pos, elt = stack.pop() do_lbl(pos) do_pos(pos) oc.write('push %s\n' % elt) def pop_n(n, clear=True): skip = None l = [] for n in range(n): if skip or not stack: l.append('pop') else: pos, elt = stack.pop() if not skip and pos in labels: skip = pos l.append(elt) if clear or skip: pop_all() if skip: do_lbl(skip) return ', '.join(l) for pos, cmd in cmds: if pos in entrypoints: code, is_entry = entrypoints[pos] del entrypoints[pos] f = "function" if is_entry: f = "entrypoint" oc.write("\n#%s $%08lx\n" % (f, code & 0xFFFFFFFF)) res.finalise() if cmd == None: pop_all() do_lbl(pos) elif cmd.__class__ == Stack: s, n, b = cmd res.finalise() if n: s = "%s(%s)" % (s, pop_n(n, False)) elif b: if res.enabled: s = res.add_whole(s) else: s = repr(s) stack.append((pos, s)) elif cmd.__class__ == Cmd: s, n, l = cmd if n: args = " (%s)" % pop_n(n) else: args = "" pop_all() do_lbl(pos) do_pos(pos) res.finalise() oc.write(s) oc.write(args) if len(l) == 1: oc.write(' @%d' % labels[l[0]]) elif l: oc.write(' { %s }' % ', '.join(['@%d' % labels[n] for n in l])) oc.write('\n') else: #text pop_all() do_lbl(pos) do_pos(pos) if res.enabled: r = res.add_part(cmd) if r: oc.write("%s\n" % r) else: oc.write(repr(cmd)) pop_all() #Clean up if targets: print "Warning: labels not generated: %s" % ", ".join( ['@%d' % labels[n] for n in targets]) if entrypoints: print "Warning: entrypoints not generated: %s" % ", ".join( ['$%08lx' % entrypoints[n][0] & 0xFFFFFFFF for n in entrypoints]) oc.close() res.close()
0x850: ('switch', 'L'), } for op in (0x100, 0x101, 0x108, 0x109, 0x110, 0x118, 0x119, 0x11a, 0x120, 0x121, 0x130, 0x138, 0x140, 0x142, 0x148, 0x150, 0x158, 0x15a, 0x160, 0x162, 0x170, 0x178, 0x180, 0x188, 0x190, 0x1a0, 0x1a1, 0x83e, 0x83f, 0x841, 0x844, ): opInfo[op] = ('op%03x'%op, []) for op in (0x803, 0x847, ): opInfo[op] = ('op%03x'%op, [4]) for op in (0x1b0, 0x1b1, 0x1b2, 0x1b3, 0x1b5, 0x1b8, 0x1c0, 0x1d0, 0x1d2, 0x1d8, 0x1d9, 0x270, 0x272, 0x290, 0x2c0, 0x802, 0x837): opInfo[op] = ('op%03x'%op, [8]) ic = open(sys.argv[1],'rb') encrypted, l, offset = Mjo.header(ic) ic.seek(16) primary = struct.unpack('I', ic.read(4))[0] ic.seek(0x18) entrypoints = {} for i in range(struct.unpack('I', ic.read(4))[0]): code, offs = struct.unpack('II', ic.read(8)) entrypoints[offs] = (code, offs == primary) if encrypted: s = strio(Mjo.crypt(ic)) else: ic.seek(offset) s = strio(ic.read(l)) dasm = [] labels={}
0x83f, 0x841, 0x844, ): opInfo[op] = ('op%03x' % op, []) for op in ( 0x803, 0x847, ): opInfo[op] = ('op%03x' % op, [4]) for op in (0x1b0, 0x1b1, 0x1b2, 0x1b3, 0x1b5, 0x1b8, 0x1c0, 0x1d0, 0x1d2, 0x1d8, 0x1d9, 0x270, 0x272, 0x290, 0x2c0, 0x802, 0x837): opInfo[op] = ('op%03x' % op, [8]) ic = open(sys.argv[1], 'rb') encrypted, l, offset = Mjo.header(ic) ic.seek(16) primary = struct.unpack('I', ic.read(4))[0] ic.seek(0x18) entrypoints = {} for i in range(struct.unpack('I', ic.read(4))[0]): code, offs = struct.unpack('II', ic.read(8)) entrypoints[offs] = (code, offs == primary) if encrypted: s = strio(Mjo.crypt(ic)) else: ic.seek(offset) s = strio(ic.read(l)) dasm = [] labels = {}