Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- import os
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- return totalfiledata
- def replacestr(origstr,replacestr,startpos,replacelen):
- #Returns a string with a replaced sub-string
- #origstr - the original string, replacestr = the string to replace
- #startpos - where the replacement string should go
- #replacelen - how many characters of the original string to replace
- return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
- infile = 't_ittxt.data'
- infile1 = 't_ittxt.orig'
- origfiledata = get_data(infile1)
- outfiledata = get_data(infile1)
- tblindex = 0x16
- offset = 0
- inputindex = 0
- lasttblpointer = 0x0
- inputdata = []
- #Read the input file
- with open(infile) as f:
- for line in f:
- inputdata.append(line.split('\t'))
- while True:
- #Break out of the loop if at end of pointers
- if outfiledata[tblindex:tblindex+2] == '\x00\x00':
- break
- if inputindex == len(inputdata):
- break
- origpointer = struct.unpack('<H',origfiledata[tblindex:tblindex+2])[0]+8
- if origpointer != lasttblpointer:
- lasttblpointer = origpointer
- #Unpack pointer, add offset
- tblpointer = struct.unpack('<H',outfiledata[tblindex:tblindex+2])[0] + offset
- #Rewrite the pointer
- outfiledata = outfiledata[:tblindex] + struct.pack('<H',tblpointer) + outfiledata[tblindex+2:]
- itemnamepointer = tblpointer + 8 #The first pointer.
- itemdescriptionpointer = itemnamepointer + len(inputdata[inputindex][1])
- code = struct.pack('<H',itemnamepointer) + struct.pack('<H',itemdescriptionpointer)
- origend = origfiledata.find('\x00',origpointer)
- origend = origfiledata.find('\x00',origend+1)
- origlength = origend-origpointer
- if inputdata[inputindex+1][0] != "": #A 1-length record (one line long)
- code += inputdata[inputindex][1][:-1] + '\x00' + " "
- inputindex += 1
- elif inputdata[inputindex+2][0] != "": #2-length record
- code += inputdata[inputindex][1][:-1] + '\x00' + inputdata[inputindex+1][1][:-1]
- inputindex += 2
- else: #3-length record
- code += inputdata[inputindex][1][:-1] + '\x00' + inputdata[inputindex+1][1][:-1] + inputdata[inputindex+2][1][:-1]
- inputindex += 3
- newlength = len(code) - 4
- oldoffset = offset
- offset += newlength - origlength
- outfiledata = replacestr(outfiledata,code,tblpointer + 4,origlength + 4)
- else:
- #Unpack pointer, add offset
- tblpointer = struct.unpack('<H',outfiledata[tblindex:tblindex+2])[0] + oldoffset
- #Rewrite the pointer
- outfiledata = outfiledata[:tblindex] + struct.pack('<H',tblpointer) + outfiledata[tblindex+2:]
- tblindex += 2
- outfile = open('t_ittxt._dt','wb')
- outfile.write(outfiledata) #write the output
- outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement