Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct,os,sys,re
- opcodes = [
- [0x1,'0x1',['b','b',]],[0x2,'call',['d',]],[0x3,'0x3',['w',]],[0x4,'0x4-return',[]],
- [0x5,'0x5',[]],[0x6,'0x6-condjump',['d']],
- [0x7,'0x7-condjump',['d']],[0x8,'0x8',[]],[0x9,'0x9',[]],
- [0xa,'0xa',['d',]],[0xb,'0xb',['w',]],[0xc,'0xc',['b',]],[0xe,'string',['str']],
- [0xf,'0xf',['w',]],[0x10,'0x10',['b',]],[0x11,'0x11',['w',]],[0x12,'0x12',['b',]],
- [0x14,'0x14',[]],[0x15,'0x15',['w',]],[0x16,'0x16',['b',]],
- [0x17,'0x17',['w',]],[0x18,'0x18',['b']],[0x19,'0x19',[]],
- [0x1a,'0x1a',[]],[0x1b,'0x1b',[]],[0x1c,'0x1c',[]],[0x1d,'0x1d',[]],[0x1e,'0x1e',[]],
- [0x1f,'0x1f',[]],[0x20,'0x20',[]],[0x21,'0x21',[]],
- [0x22,'0x22',[]],[0x23,'0x23',[]],[0x24,'0x24',[]],
- [0x25,'0x25',[]],[0x26,'0x26',[]],[0x27,'0x27',[]],
- ]
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- return totalfiledata
- def extract(filename):
- filedata = bytearray(open(filename,'rb').read())
- newfiledata = '====== Script ======\n\n==== Function 0 ====\n\n'
- endpos = struct.unpack('<I',filedata[0:4])[0]
- pos = 4
- funccount = 1
- opcodecount = 0
- jumplocs = {}
- print 'Extracting...'
- while pos < endpos:
- opcode = struct.unpack('<B',filedata[pos:pos+1])[0]
- c = 0
- while c != len(opcodes):
- if opcodes[c][0] == opcode:
- name = opcodes[c][1]
- params = opcodes[c][2]
- break;
- if c+1 == len(opcodes):
- print 'Unknown opcode %x at loc %x' % (opcode,pos)
- outfile = open('out.txt','wb')
- outfile.write(newfiledata)
- outfile.close()
- sys.exit()
- c += 1
- newfiledata += ('%x :: ' % pos) + '{ ' + name + ' '
- pos += 1
- for a in xrange(0,len(params)):
- if params[a] == 'b':
- var = struct.unpack('<B',filedata[pos:pos+1])[0]
- newfiledata += '%x ' % var
- pos += 1
- elif params[a] == 'w':
- var = struct.unpack('<H',filedata[pos:pos+2])[0]
- newfiledata += '%x ' % var
- pos += 2
- elif params[a] == 'd':
- var = struct.unpack('<I',filedata[pos:pos+4])[0]
- newfiledata += '%x ' % var
- pos += 4
- elif params[a] == 'str':
- strlen = struct.unpack('<B',filedata[pos:pos+1])[0]
- newfiledata += '"' + filedata[pos+1:pos+1+strlen-1] + '" '
- pos += strlen+1
- newfiledata += '}\n'
- if opcode == 0x4 and struct.unpack('<B',filedata[pos:pos+1])[0] != 0x4 and pos < endpos:
- newfiledata += '\n==== Function %d ====\n\n' % funccount
- if pos == struct.unpack('<I',filedata[endpos:endpos+4])[0]:
- newfiledata = newfiledata[:-1] + '### Main Script Start ###\n\n'
- funccount += 1
- opcodecount += 1
- pos += 4
- newfiledata += '\n\n====== Extra ======\n### Do not edit below this point ###\n%x %x %x %x %x %x\n\n==== Strings ====\n\n' % (filedata[pos],filedata[pos+1],filedata[pos+2],filedata[pos+3],filedata[pos+4],filedata[pos+5])
- newfiledata += '%s\n' % (filedata[pos+7:pos+7+struct.unpack('<B',filedata[pos+6:pos+7])[0]-1])
- strcnt = struct.unpack('<B',filedata[pos+0x2b:pos+0x2c])[0]
- pos += 0x2d
- for i in xrange(0,strcnt):
- strlen = struct.unpack('<B',filedata[pos+1:pos+2])[0]
- newfiledata += '%s - %d\n' % (filedata[pos+2:pos+2+strlen-1],struct.unpack('<B',filedata[pos:pos+1])[0])
- pos += 2 + strlen
- outfile = open(filename + '.txt','wb')
- outfile.write(newfiledata)
- outfile.close()
- print 'Done!'
- def comp(filename):
- filedata = get_data(filename)
- newfiledata = ''
- newfiledata += struct.pack('<I',0x0)
- data = re.split('(======.+?======)',filedata)
- script = data[2].splitlines()
- lastpos = -1
- origlocs = {}
- fixlocs = []
- print 'Compiling...'
- for i in xrange(0,len(script)):
- line = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+',script[i])
- if len(line) > 0:
- if line[0] != '' and line[0] != '====':
- if line[0] == '###' and line[1] == 'Main' and line[2] == 'Script':
- scriptstart = len(newfiledata)
- else:
- newloc = False
- if line[1] == '::':
- origlocs[int(line[0],16)] = len(newfiledata)
- line.pop(0)
- line.pop(0)
- else:
- newloc = True
- line.pop(0)
- line.pop(len(line)-1)
- c = 0
- while c < len(opcodes):
- if opcodes[c][1] == line[0]:
- newfiledata += struct.pack('<B',opcodes[c][0])
- params = opcodes[c][2]
- break;
- if c+1 == len(opcodes):
- print 'Unknown opcode %s on line %d' % (line[0],i)
- sys.exit()
- c += 1
- line.pop(0)
- varsmoved = 0
- for a in xrange(0,len(params)):
- if params[a] == 'b':
- newfiledata += struct.pack('<B',int(line[a],16))
- elif params[a] == 'w':
- newfiledata += struct.pack('<H',int(line[a],16))
- elif params[a] == 'd':
- if opcodes[c][0] == 0x2 or opcodes[c][0] == 0x6 or opcodes[c][0] == 0x7 and varsmoved == 0:
- fixlocs.append([len(newfiledata),int(line[a],16)])
- newfiledata += struct.pack('<I',0x0)
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- elif params[a] == 'str':
- line[a] = line[a][1:-1]
- newfiledata += struct.pack('<B',len(line[a])+1)
- newfiledata += line[a] + '\x00'
- else:
- print 'param error'
- sys.exit()
- varsmoved += 1
- for i in range(0,len(fixlocs)):
- if fixlocs[i][1] not in origlocs:
- print 'bad loc fixlocs[i][1]'
- sys.exit()
- loc = origlocs[fixlocs[i][1]]
- newfiledata = newfiledata[:fixlocs[i][0]] + struct.pack('<I',loc) + newfiledata[fixlocs[i][0]+0x4:]
- newfiledata = struct.pack('<I',len(newfiledata)) + newfiledata[0x4:] + struct.pack('<I',scriptstart)
- extralines = data[4].splitlines()
- linesmoved = 0
- strcnt = 0
- for i in xrange(0,len(extralines)):
- if extralines[i].find('===') == -1 and extralines[i].find('###') == -1 and extralines[i] != '':
- if extralines[i].find('-') != -1:
- line = extralines[i].split('-')
- newfiledata += struct.pack('<B',int(line[1])) + struct.pack('<B',len(line[0])) + line[0][:-1] + '\x00'
- strcnt += 1
- else:
- line = extralines[i].split(' ')
- if linesmoved == 0:
- for a in range(0,len(line)):
- newfiledata += struct.pack('<B',int(line[a],16))
- linesmoved += 1
- else:
- newfiledata += struct.pack('<B',len(line[0])+len(line[1])+2) + line[0] + ' ' + line[1] + '\x00'
- fixloc = len(newfiledata)
- newfiledata += struct.pack('<H',0x0)
- newfiledata = newfiledata[:fixloc] + struct.pack('<B',strcnt) + newfiledata[fixloc+1:]
- newfiledata += '\x00\x00'
- print 'Done!'
- outfile = open(filename.rsplit('.',1)[0] + '.new','wb')
- outfile.write(newfiledata)
- outfile.close()
- if __name__ == '__main__':
- if sys.argv[1] == '-e':
- extract(sys.argv[2])
- elif sys.argv[1] == '-c':
- comp(sys.argv[2])
- else:
- print 'usage: script.py <-e/-c> <filename>'
- print '-e - extract'
- print '-c - compile'
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement