qazmlpok

NITORI BOX extraction

Jun 2nd, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 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 "pack"
  60.     #4 bytes number of files.
  61.     #4 bytes of zeroes.
  62.  
  63.     pack = readbytes(infile, 4)
  64.     assert(pack == "pack")
  65.  
  66.     filecount = converttoint(readbytes(infile, 4, 0))
  67.     print "Expecting", filecount, "files."
  68.  
  69.     readbytes(infile, 4)
  70.  
  71.     #Each file entry is
  72.     #4 bytes        4 bytes         4 bytes         n bytes
  73.     #Filename length    File size       File start      Filename
  74.  
  75.     #Index size is variable.
  76.  
  77.     entries = []
  78.  
  79.     for i in xrange(filecount):
  80.         entry = {}
  81.  
  82.         namelen = converttoint(readbytes(infile, 4))    #Length of the filename. There is no \0 at the end or any other padding.
  83.  
  84.         entry['filesize'] = converttoint(readbytes(infile, 4))
  85.         entry['filestart'] = converttoint(readbytes(infile, 4))
  86.         entry['name'] = readbytes(infile, namelen)
  87.  
  88.         entries.append(entry)
  89.  
  90.     #print "Current position is now %d / %X" % (infile.tell(), infile.tell())
  91.  
  92.     #for entry in entries:
  93.     #   for i in entry:
  94.     #       print i + ":", entry[i]
  95.     #   print ""
  96.  
  97.     #sys.exit(0)
  98.  
  99.     dirname = filename[0:filename.find(".")]
  100.     if not os.path.isdir(dirname):
  101.         os.mkdir(dirname)
  102.  
  103.     i = 0
  104.     for entry in entries:
  105.         filename = entry['name'].strip(" \n\t\x00")
  106.  
  107.         dirsegment, filesegment = os.path.split(filename)
  108.         #filename = os.path.join(dirsegment, ("%03d_" % i) + filesegment)       #Can be done to enforce correct order on repacking
  109.         if (not os.path.isdir(os.path.join(dirname, dirsegment))):
  110.             os.makedirs(os.path.join(dirname, dirsegment))
  111.  
  112.         infile.seek(entry['filestart'])
  113.         data = readbytes(infile, entry['filesize'])
  114.         outfile = open(os.path.join(dirname, filename), 'wb')
  115.         outfile.write(data)
  116.         outfile.close()
  117.  
  118.         print "Created file %s of size %08X from position %08X" % (filename, entry['filesize'], entry['filestart'])
  119.  
  120.         i += 1
  121.  
  122.     infile.close()
Advertisement
Add Comment
Please, Sign In to add comment