qazmlpok

First Disaster extract

Jul 5th, 2012
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. import os
  2. import sys
  3.  
  4. #Constants taken from insani tools.
  5. LITTLE_ENDIAN = 1
  6. BIG_ENDIAN = -1
  7.  
  8. BYTE_LENGTH = 1
  9. SHORT_LENGTH = 2
  10. INT_LENGTH = 4
  11. LONG_LENGTH = 8
  12.  
  13. ERROR_NONE = 0
  14. ERROR_WARNING = 1
  15. ERROR_ABORT = 2
  16.  
  17. def readbytes(infile, bytecount, key=-1):
  18.     ret = infile.read(bytecount)
  19.  
  20.     return ret
  21.  
  22. def converttoint(bytes, size=INT_LENGTH, endian=LITTLE_ENDIAN):
  23.     result = 0
  24.     for i in xrange(size):
  25.         temp=long(ord(bytes[i]))
  26.         if endian == LITTLE_ENDIAN :
  27.             result |= (temp << (8*i))
  28.         elif endian == BIG_ENDIAN :
  29.             result = (result << 8) | temp
  30.         else :
  31.                 raise Exception('converttoint: Unknown endian specification')
  32.  
  33.         return result
  34.  
  35. def convertfromint(value, size=INT_LENGTH, endian=LITTLE_ENDIAN):
  36.     result = ""
  37.     for i in xrange(size):
  38.         if endian == LITTLE_ENDIAN :
  39.             #result |= (temp << (8*i))
  40.             temp = (value >> (8*i)) & 0x00FF
  41.         elif endian == BIG_ENDIAN :
  42.             #result = (result << 8) | temp
  43.             temp = (value >> (8*(size-i-1))) & 0x00FF
  44.         else :
  45.                 raise Exception('convertfromint: Unknown endian specification')
  46.             result += chr(temp)
  47.  
  48.         return result
  49.  
  50. #program
  51.  
  52. files = sys.argv[1:]
  53. for filename in files:
  54.     infile = open(filename, 'rb')
  55.  
  56.     print "Reading file", filename
  57.  
  58.     #Header:
  59.     #4 bytes for the total size of the index, counting this header.
  60.  
  61.     indexsize = converttoint(readbytes(infile, 4, 0))
  62.     #print "Expecting", filecount, "files."
  63.  
  64.     #Each file entry is
  65.     #4 bytes    n bytes     4 bytes     4 bytes
  66.     #Filename len   Filename    Filesize    Filestart
  67.  
  68.     entries = []
  69.  
  70.     while (infile.tell() < indexsize):
  71.         entry = {}
  72.         namelen = converttoint(readbytes(infile, 4))
  73.         nameraw = readbytes(infile, namelen)
  74.  
  75.         entry['filesize'] = converttoint(readbytes(infile, 4))
  76.         entry['filestart'] = converttoint(readbytes(infile, 4))
  77.  
  78.         entry['name'] = nameraw[0:nameraw.find("\x00")]     #Truncate to the first \0.
  79.  
  80.         entries.append(entry)
  81.  
  82.     print "Found", len(entries),"files."
  83.  
  84.  
  85.     #print "Current position is now %d / %X" % (infile.tell(), infile.tell())
  86.  
  87.     #for entry in entries:
  88.     #   for i in entry:
  89.     #       print i + ":", entry[i]
  90.     #   print ""
  91.  
  92.     #sys.exit(0)
  93.  
  94.     dirname = filename[0:filename.find(".")]
  95.     if not os.path.isdir(dirname):
  96.         os.mkdir(dirname)
  97.  
  98.     i = 0
  99.     for entry in entries:
  100.         filename = entry['name'].strip(" \n\t\x00")
  101.  
  102.         dirsegment, filesegment = os.path.split(filename)
  103.         #filename = os.path.join(dirsegment, ("%03d_" % i) + filesegment)       #Can be done to enforce correct order on repacking
  104.         if (not os.path.isdir(os.path.join(dirname, dirsegment))):
  105.             os.makedirs(os.path.join(dirname, dirsegment))
  106.  
  107.         infile.seek(entry['filestart'])
  108.         data = readbytes(infile, entry['filesize'])
  109.         outfile = open(os.path.join(dirname, filename), 'wb')
  110.         outfile.write(data)
  111.         outfile.close()
  112.  
  113.         print "Created file %s of size %08X from position %08X" % (filename, entry['filesize'], entry['filestart'])
  114.         #print "Unk is %08X " % entry['unk']    #It's mostly 0012F79C, also a few 0012FAC4. No idea what it would be used for.
  115.  
  116.         i += 1
  117.  
  118.     infile.close()
Advertisement
Add Comment
Please, Sign In to add comment