Guest User

Nayuta .tbb Fake Compressor v1

a guest
Mar 11th, 2015
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import struct
  3.  
  4. def get_data(filename):
  5.     totalbytes = os.path.getsize(filename)
  6.     infile = open(filename, 'rb')
  7.     totalfiledata = infile.read(totalbytes)
  8.     infile.close()
  9.     return totalfiledata
  10.  
  11. def replacestr(origstr,replacestr,startpos,replacelen):
  12. #Returns a string with a replaced sub-string
  13. #origstr - the original string, replacestr = the string to replace
  14. #startpos - where the replacement string should go
  15. #replacelen - how many characters of the original string to replace
  16.     return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
  17.  
  18. def myprogram(filename,messageoffset):
  19.     filedata = get_data(filename + '.orig')
  20.     unknown_data1 = filedata[:messageoffset]
  21.     unpacked_size = struct.unpack('<I',filedata[messageoffset+4:messageoffset+8])[0]
  22.     number_of_chunks = struct.unpack('<I',filedata[messageoffset+8:messageoffset+12])[0]
  23.     unknown_data2 = filedata[-8:]
  24.     filedata = get_data(filename  + '.unpack3')
  25.     pos = 0
  26.     output = []
  27.     print '{:8} {}'.format(filename, number_of_chunks)
  28.  
  29.     for chunk in range(number_of_chunks):
  30.         if chunk == number_of_chunks - 1:
  31.             output.append(unknown_data2)
  32.             break
  33.         output2 = ['\x00']
  34.         bits = 8
  35.         flag = False
  36.         if len(filedata) - pos >= 0x7FF0:
  37.             chunk_end = 0x7FF0 * (chunk + 1)
  38.         else:
  39.             chunk_end = len(filedata)
  40.         startpos = pos
  41.         while pos < chunk_end:
  42.             if bits == 0:
  43.                 if chunk_end - pos < 16:
  44.                     flag = True
  45.                     s = '0'*(14 - (chunk_end - pos)) + '11' + '0'*(chunk_end - pos)
  46.                     print '{:8} {} {}'.format(filename, chunk, s)
  47.                     output2.append(struct.pack('>H',int(s,2)))
  48.                 else:
  49.                     output2.append('\x00'*2)
  50.                 bits = 16
  51.             output2.append(filedata[pos])
  52.             bits -= 1
  53.             pos += 1
  54.         if flag:
  55.             output2.append('\x00')
  56.         else:
  57.             s = '0'*14 + '11'
  58.             output2.append(struct.pack('>H',int(s,2)))
  59.             output2.append('\x00')
  60.         if chunk != 0:
  61.             output.append('\x01')
  62.         output.append(struct.pack('<H',len(''.join(output2)) + 3)) #Chunk size, 3 = size plus 00 byte
  63.         output.append('\x00')
  64.         output += output2
  65.  
  66.     newlen = len("".join(output)) + 8 #8 = 4 bytes for unpacked size, 4 bytes for # of chunks
  67.     outfile = open(filename + '.tbb','wb')
  68.     outfile.write("".join([unknown_data1, #Data before the message
  69.                            struct.pack('<I',newlen), #Message size
  70.                            struct.pack('<I',unpacked_size),
  71.                            struct.pack('<I',number_of_chunks)]))
  72.     outfile.write(''.join(output))
  73.     outfile.close()
  74.  
  75.     filedata = get_data('data.lst')
  76.     basepos = 0x14150
  77.     newsize = os.path.getsize(filename + '.tbb')
  78.     pos = filedata.find(filename + '\x00'*(8-len(filename)),basepos)
  79.     if pos == -1:
  80.         print 'Error'
  81.         quit()
  82.     filedata = replacestr(filedata,struct.pack('<I',newsize),pos+8,4)
  83.     outfile = open('data.lst','wb')
  84.     outfile.write(filedata)
  85.     outfile.close()
  86.  
  87. myprogram('monslib',0x1F)
  88. myprogram('questlib',0x1F)
  89. myprogram('fldlist0',0x1F)
  90. myprogram('fldlist1',0x1F)
  91. myprogram('fldlist2',0x1F)
  92. myprogram('fldlist3',0x1F)
  93. myprogram('helplib',0x1F)
  94. myprogram('foodarea',0x1F)
RAW Paste Data