Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.14 KB | None | 0 0
  1. import os,sys,struct
  2.  
  3. reload(sys)
  4. sys.setdefaultencoding('sjis')
  5.  
  6. op_codes = {
  7.             0x0:0x0,0x1:0x1,0x2:0x2,0x3:0x2,
  8.             0x4:0x3,0x5:0x3,0x6:0x3,0x7:0x3,
  9.             0x8:0x3,0x9:0x4,0xa:0x4,0xb:0x4,
  10.             0xc:0x4,0xd:0x5,0xe:0x5,0xf:0x5,
  11.             0x10:0x5,0x11:0x6,0x12:0x6,0x13:0x6,
  12.             0x14:0x6,0x15:0x7,0x16:0x7,0x17:0x7,
  13.             0x18:0x7,0x19:0x8,0x1a:0x8,0x1b:0x8,
  14.             0x1c:0x8,0x1d:0x9,0x1e:0x9,0x1f:0xa,
  15.             0x20:0xb,0x21:0xc,
  16.            }
  17.  
  18. def get_data(filename):
  19.     totalbytes = os.path.getsize(filename)
  20.     infile = open(filename, 'rb')
  21.     totalfiledata = infile.read(totalbytes)
  22.     infile.close()
  23.     return totalfiledata
  24.  
  25. def get_dataval(filedata,i,data_type,minus):
  26.     data_type -= minus
  27.     if (data_type < 0 or data_type > 3):
  28.         print 'data_type FUCKED UP WITH A VAL OF 0x%x' % data_type
  29.         sys.exit()
  30.     elif data_type == 0:
  31.         data = struct.unpack('<B',filedata[i:i+1])[0]
  32.         i += 1
  33.     elif data_type == 1:
  34.         data = struct.unpack('<H',filedata[i:i+2])[0]
  35.         i += 2
  36.     elif data_type == 2:
  37.         data = struct.unpack('<B',filedata[i+2:i+3])[0]
  38.         data << 16
  39.         data |= struct.unpack('<H',filedata[i:i+2])[0]
  40.         i += 3
  41.     elif data_type == 3:
  42.         data = struct.unpack('<I',filedata[i:i+4])[0]
  43.         i += 4
  44.     else:
  45.         print 'UNREACHABLE!!!'
  46.         sys.exit()
  47.  
  48.     return data,i
  49.  
  50. if __name__ == '__main__':
  51.     filedata = get_data(sys.argv[1])
  52.     header_len = struct.unpack('<I',filedata[0x8:0xc])[0]
  53.     sec1_len = (struct.unpack('<H',filedata[header_len+1:header_len+3])[0] * 2) + 4
  54.     sec2_len = (struct.unpack('<H',filedata[header_len+sec1_len+1:header_len+sec1_len+3])[0] * 2) + 4
  55.     sec3_len = (struct.unpack('<B',filedata[header_len+sec1_len+sec2_len+1:header_len+sec1_len+sec2_len+2])[0] * 2) + 3
  56.     script_start = header_len+sec1_len+sec2_len+sec3_len
  57.  
  58.     string_script_pos = struct.unpack('<I',filedata[0x10:0x14])[0]
  59.     string_pos = struct.unpack('<I',filedata[0x14:0x18])[0]
  60.     strings = ''
  61.  
  62.     i = script_start
  63.     while i < string_script_pos:
  64.         op_start = i
  65.         if struct.unpack('<B',filedata[i:i+1])[0] not in op_codes:
  66.             print 'Unknown op code %x at %x' % (struct.unpack('<B',filedata[i:i+1])[0],i)
  67.             sys.exit()
  68.         real_opcode = struct.unpack('<B',filedata[i:i+1])[0]
  69.         op_code = op_codes[real_opcode]
  70.         i += 1
  71.  
  72.         if op_code == 0x1: # opcodes 0x1
  73.             continue;
  74.         elif op_code == 0x3: # opcodes 0x4,0x5,0x6,0x7,0x8
  75.             data_type = real_opcode - 4
  76.             if data_type < 0 or data_type > 4:
  77.                 print '%x - data type fucked!' % i
  78.                 sys.exit()
  79.             elif data_type == 0:
  80.                 data = 0
  81.             elif data_type == 1:
  82.                 data = struct.unpack('<B',filedata[i:i+1])[0]
  83.                 i += 1
  84.             elif data_type == 2:
  85.                 data = struct.unpack('<H',filedata[i:i+2])[0]
  86.                 i += 2
  87.             elif data_type == 3:
  88.                 data = struct.unpack('<B',filedata[i+2:i+3])[0]
  89.                 data <<= 16
  90.                 data |= struct.unpack('<H',filedata[i:i+2])[0]
  91.                 i += 3
  92.             elif data_type == 4:
  93.                 data = struct.unpack('<I',filedata[i:i+4])[0]
  94.                 i += 4
  95.  
  96.         elif op_code == 0x4: # opcodes 0x9,0xa,0xb,0xc
  97.             data_type = real_opcode - 0x9
  98.             if data_type < 0 or data_type > 3:
  99.                 print 'data_type fucked at %x' % i
  100.                 sys.exit()
  101.             elif data_type == 0: #5 bytes
  102.                 i += 5
  103.             elif data_type == 1: #6 bytes
  104.                 i += 6
  105.             elif data_type == 2:
  106.                 i += 7
  107.             elif data_type == 3:
  108.                 i += 8
  109.         elif op_code == 0x7: # opcodes 0x15,0x16,0x17,18, strings
  110.             data,i = get_dataval(filedata,i,real_opcode,0x15)
  111.             script_type = struct.unpack('<B',filedata[string_script_pos:string_script_pos+1])[0]
  112.             data2,throwaway = get_dataval(filedata,string_script_pos+1,script_type,0xD)
  113.             script_type -= 0xB
  114.            
  115.             string_format_step = struct.unpack('<B',filedata[string_script_pos+3:string_script_pos+4])[0] - 0xC
  116.             total_string_len = data2 * string_format_step
  117.             unknown1 = string_format_step + total_string_len + 1 #psbfile+AD1B
  118.  
  119.             script_type = string_format_step - 1
  120.  
  121.             data *= string_format_step
  122.             data += string_script_pos + (struct.unpack('<B',filedata[string_script_pos:string_script_pos+1])[0] - 0xC) + 2
  123.             if script_type < 0 or script_type > 3:
  124.                 print 'script_type fucked'
  125.                 sys.exit()
  126.             elif script_type == 0:
  127.                 curr_string_pos = struct.unpack('<B',filedata[data:data+1])[0]
  128.             elif script_type == 1:
  129.                 curr_string_pos = struct.unpack('<H',filedata[data:data+2])[0]
  130.             elif script_type == 2:
  131.                 curr_string_pos = struct.unpack('<B',filedata[data+2:data+3])[0]
  132.                 curr_string_pos <<= 16
  133.                 curr_string_pos |= struct.unpack('<H',filedata[data:data+2])[0]
  134.             elif script_type == 3:
  135.                 curr_string_pos = struct.unpack('<I',filedata[data:data+4])[0]
  136.             curr_string_pos += string_pos
  137.  
  138.             string = filedata[curr_string_pos:filedata.find('\x00',curr_string_pos)]
  139.             if strings.find(string) == -1:
  140.                 strings += string + '\n'
  141.  
  142.         elif op_code == 0x9: #opcodes 0x1d,0x1e
  143.             data_type = real_opcode - 0x1D
  144.             if data_type < 0 or data_type > 1:
  145.                 print 'opcode 0x9 fucked at %x!' % i
  146.                 sys.exit()
  147.             if data_type == 1:
  148.                 data = struct.unpack('<I',filedata[i:i+4])[0]
  149.                 i += 4
  150.  
  151.         elif op_code == 0xa: #opcodes 0x1f
  152.             val1 = struct.unpack('<I',filedata[i:i+4])[0]
  153.             val2 = struct.unpack('<I',filedata[i+4:i+8])[0]
  154.             i += 8
  155.            
  156.         elif op_code == 0xb: # op_code 0x20, jump
  157.             data_type = struct.unpack('<B',filedata[i:i+1])[0]
  158.             i += 1
  159.             data,i = get_dataval(filedata,i,data_type,0xD)
  160.             data_type -= 0xB
  161.             data_type2 = struct.unpack('<B',filedata[i:i+1])[0] - 0xC
  162.             i += 1
  163.             data_len = data * data_type2
  164.             i += data_len
  165.            
  166.         elif op_code == 0xc: #opcodes 0x21
  167.             for x in range(0,2):
  168.                 #print 'i %x' % i
  169.                 byte2 = struct.unpack('<B',filedata[i:i+1])[0]
  170.                 i += 1
  171.                 byte3,i = get_dataval(filedata,i,byte2,0xD)
  172.                 byte2 -= 0xB
  173.                 byte4 = struct.unpack('<B',filedata[i:i+1])[0] - 0xC
  174.                 data_len = byte3 * byte4
  175.                 i += 1
  176.                 i += data_len
  177.         else:
  178.             print 'Unfinished op code %x at %x! Real opcode %x' % (op_code, i-1, real_opcode)
  179.             sys.exit()
  180.    
  181.     outfile = open('strings.txt','wb')
  182.     outfile.write(strings)
  183.     outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement