Advertisement
qazmlpok

恋々幻想郷

May 15th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. from insani import *
  2. import os
  3. import sys
  4.  
  5. def readbytes(infile, bytecount, xorkey, offset=0):
  6.     ret = ""
  7.     while (offset < bytecount):
  8.         byte = long(ord(infile.read(1)))
  9.  
  10.         ret += chr(byte)
  11.  
  12.         offset += 1
  13.  
  14.     return ret
  15.  
  16. def converttoint(bytes, size=INT_LENGTH, endian=LITTLE_ENDIAN):
  17.     result = 0
  18.     for i in xrange(size):
  19.         temp=long(ord(bytes[i]))
  20.         if endian == LITTLE_ENDIAN :
  21.             result |= (temp << (8*i))
  22.         elif endian == BIG_ENDIAN :
  23.             result = (result << 8) | temp
  24.         else :
  25.                 raise 'converttoint', 'Unknown endian specification'
  26.  
  27.         return result
  28.  
  29. def convertfromint(value, size=INT_LENGTH, endian=LITTLE_ENDIAN):
  30.     result = ""
  31.     for i in xrange(size):
  32.         if endian == LITTLE_ENDIAN :
  33.             #result |= (temp << (8*i))
  34.             temp = (value >> (8*i)) & 0x00FF
  35.         elif endian == BIG_ENDIAN :
  36.             #result = (result << 8) | temp
  37.             temp = (value >> (8*(size-i-1))) & 0x00FF
  38.         else :
  39.                 raise 'convertfromint', 'Unknown endian specification'
  40.             result += chr(temp)
  41.  
  42.         return result
  43.  
  44. #program
  45.  
  46. files = sys.argv[1:]
  47. for filename in files:
  48.     infile = open(filename, 'rb')
  49.  
  50.     xorkey = 0
  51.  
  52.     header_unk = converttoint(readbytes(infile, 4, xorkey))
  53.     print "The unknown crap is %8X" % header_unk
  54.  
  55.     filecount = converttoint(readbytes(infile, 4, xorkey))
  56.     print "There are %d files" % filecount
  57.  
  58.     entries = []
  59.  
  60.     for i in xrange(filecount):
  61.         entry = {}
  62.         filedata = readbytes(infile, 0x54, xorkey)
  63.         entry['name'] = filedata[0:0x40].strip(" \t\n\0")
  64.         print "Name is %s" % (entry['name'])
  65.  
  66.         #block1 = converttoint(filedata[0x100:0x104])
  67.         entry['filesize'] = converttoint(filedata[0x4C:0x50])
  68.         entry['filestart'] = converttoint(filedata[0x48:0x4C])
  69.  
  70.         #print "And the blocks are: %08X - %08X" % (filesize, filestart)
  71.  
  72.         entries.append(entry)
  73.  
  74.  
  75.     #print "Current position is now %d / %X" % (infile.tell(), infile.tell())
  76.  
  77.     dirname = filename[0:filename.find(".")]
  78.     if not os.path.isdir(dirname):
  79.         os.mkdir(dirname)
  80.  
  81.     i = 0
  82.     for entry in entries:
  83.         filename = entry['name']
  84.         splitpath = filename.split('\\');
  85.  
  86.         dirpath = dirname + '\\'
  87.         for k in (xrange(len(splitpath)-1)):
  88.             dirpath += splitpath[k] + '\\'
  89.             if (not os.path.isdir(dirpath)):
  90.                 os.mkdir(dirpath)
  91.  
  92.         infile.seek(entry['filestart'])
  93.         data = readbytes(infile, entry['filesize'], xorkey)
  94.         outfile = open(dirname + '\\' + filename, 'wb')
  95.         outfile.write(data)
  96.         outfile.close()
  97.  
  98.         print "Created file", filename, "of size", entry['filesize'], "from position", entry['filestart']
  99.  
  100.         i += 1
  101.  
  102.     infile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement