Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. import os,sys,struct
  2.  
  3. def get_data(filename):
  4.     totalbytes = os.path.getsize(filename)
  5.     infile = open(filename, 'rb')
  6.     totalfiledata = infile.read(totalbytes)
  7.     infile.close()
  8.     return totalfiledata
  9.  
  10. charname = {
  11.             0x0:"",0x1:"Yuuji",0x2:"Saki",0x3:"Misaki",0x4:"Kozue",
  12.             0x5:"Rei",0x6:"Misato",0x7:"Sumiyoshi",0x8:"Takeaki",0x9:"Kataoka",
  13.             0xa:"Keijiro",0xb:"Vice Principal",0xc:"Sumiyoshi's Mother",0xd:"Kuraki",
  14.             0xe:"Saki & Sumiyoshi",0xf:"Kozue & Saki",0x10:"Misaki & Misato",0x11:"Rei's Father",
  15.             0x12:"Rei's Mother",0x13:"Student",0x14:"Man",0x15:"Man A",0x16:"Man B",0x17:"Boy A",
  16.             0x18:"Boy B",0x19:"Woman",0x1a:"Woman A",0x1b:"Woman B",0x1c:"Girl",0x1d:"Girl A",
  17.             0x1e:"Girl B",0x1f:"Girl C",0x20:"Customer",0x21:"Customer A",0x22:"Customer B",
  18.             0x23:"Customer C",0x24:"Toshizou",0x25:"Oyuki",0x26:"Man in Black",0x27:"Master",
  19.             0x28:"Butler",0x29:"Passenger",0x2a:"Optometrist",0x2b:"Sakura",0x2c:"Snow Sakura",
  20.             0x2d:"Dog",0x2e:"Voice",0x2f:"TV",0x30:"Movie",0x31:"Broadcast",0x32:"Everyone",
  21.             0x33:"???",0x34:"Crab",0x35:"Yuuji & Sumiyoshi",0x36:"Yuuji & Saki",
  22.             }
  23.  
  24. if __name__ == '__main__':
  25.     filedata = get_data(sys.argv[1])
  26.     filecnt = struct.unpack('<I',filedata[0x8:0xc])[0]
  27.     datastart = struct.unpack('<I',filedata[0xc:0x10])[0]
  28.  
  29.     for i in range(0x20,0x20+(filecnt*0x14),0x14):
  30.         filename = ''
  31.         a = i
  32.         while (ord(filedata[a]) != 0x0) and (len(filename) <= 12):
  33.             filename += filedata[a:a+1]
  34.             a += 1
  35.            
  36.         filepos = struct.unpack('<I',filedata[i+0xc:i+0x10])[0]
  37.         filelen = struct.unpack('<I',filedata[i+0x10:i+0x14])[0]
  38.         print 'Extracting %s - pos %x len %x' % (filename,filepos,filelen)
  39.  
  40.         filed = bytearray(filedata[filepos:filepos+filelen])
  41.         if struct.unpack('>H',filed[0x4:0x6])[0] == 0x9597:            
  42.             #xor
  43.             for a in range(0x8,len(filed)):
  44.                 filed[a] = ((filed[a] << 0x6) | (filed[a] >> 0x2)) & 0xFF
  45.  
  46.         #try read the strings...
  47.         newfiledata = ''
  48.         strpos = struct.unpack('<I',filed[:4])[0]
  49.         stringcount = 0
  50.        
  51.         for a in range(0x8,strpos,0x4):
  52.             currstrpos = struct.unpack('<I',filed[a:a+4])[0] + strpos
  53.             if a+0x4 == strpos:
  54.                 nextstrpos = len(filed)
  55.             else:
  56.                 nextstrpos = struct.unpack('<I',filed[a+4:a+8])[0] + strpos
  57.             #print 'start of string: %x end of string %x' % (currstrpos,nextstrpos)
  58.  
  59.             if filed.find('\x2b',currstrpos,nextstrpos) != -1:
  60.                 stringpos = filed.find('\xff\x7f',currstrpos,nextstrpos)
  61.                 if stringpos != -1:
  62.                     #assume we have a string
  63.                     #get the char name
  64.                     charnamepos = filed.find('\x03\x11',currstrpos,nextstrpos)
  65.                     #print 'name: %x' % filed[charnamepos+2]
  66.                     if not charname.has_key(filed[charnamepos+2]):
  67.                         print 'Character %x not found at pos %x' % (filed[charnamepos+2],charnamepos+2)
  68.                         sys.exit()
  69.  
  70.                     #get the string
  71.                     stringpos += 2
  72.                     string = ''
  73.                     while filed[stringpos] != 0x0 and stringpos <= nextstrpos:
  74.                         if filed[stringpos] != 0x7f:
  75.                             string += chr(filed[stringpos])
  76.                         stringpos += 1
  77.                     if filed[charnamepos+2] > 0:
  78.                         newfiledata += charname[filed[charnamepos+2]] + ': ' + string + '\n'
  79.                     else:
  80.                         newfiledata += string + '\n'
  81.                     stringcount += 1
  82.                     #print 'string was: %s' % string
  83.        
  84.         outfile = open('Out\\' + filename,'wb')
  85.         outfile.write(filed)
  86.         outfile.close()
  87.        
  88.         outfile = open('Out\\' + filename.rsplit('.',1)[0] + '_strings.txt','wb')
  89.         outfile.write(newfiledata)
  90.         outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement