Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import binascii
- import struct
- import re
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- return totalfiledata
- def extract(filename):
- StringOut = ''
- StringCount = 0
- StringLocList = []
- testchar = 0
- filedata = get_data(filename)
- for byte in filedata:
- testchar = filedata.find('\xFF',testchar,len(filedata))
- if testchar != -1:
- if filedata[testchar+1:testchar+2] != '\xFF':
- StringCount += 1
- testchar += 1
- StringLocList.append(testchar+2)
- else:
- testchar += 4
- if StringCount != 0:
- for StringLoc in StringLocList:
- StringLen = struct.unpack('>H',filedata[StringLoc-2:StringLoc])[0]
- StringOut += str(hex(StringLoc)) + '#' + str(StringLen) + ':' + filedata[StringLoc:StringLoc+StringLen] + '\n'
- outfile = open(filename + '.strings', 'wb')
- outfile.write(StringOut)
- outfile.close()
- def compilea(filename):
- origfilename = filename[:len(filename)-8]
- OrigFile = get_data(origfilename)
- newfiledata = ''
- filedata = get_data(filename)
- strings = filedata.splitlines()
- a = True
- b = 0
- firstrun = True
- currstringsize = 0
- splitchars = '\x1b\x01'
- for string in strings:
- StringLoc,junk,string = string.partition('#')
- StringLoc = int(StringLoc[2:],16)
- OrigLen,junk,string = string.partition(':')
- OrigLen = int(OrigLen,10)
- for LineCount in range(0,string.count('\x5c\x6e')):
- NewlinePos = string.find('\x5c\x6e')
- if NewlinePos % 2 != 0:
- tempStrFix = list(string)
- tempStrFix.insert(NewlinePos,'\x20')
- string = ''.join(tempStrFix)
- string = string[:string.find('\x5c\x6e')] + b'\x1b\x01' + string[string.find('\x5c\x6e')+2:]
- if len(string) > 36:
- stringlist = ''.join([ s if s not in splitchars else ' ' for s in string]).split()
- for word in stringlist:
- if (currstringsize + len(word)) >= 36:
- if (len(string2) % 2) != 0:
- string2 += ' '
- string2 = string2 + '\x1b\x01' + word
- currstringsize = 0 + len(word)
- else:
- if firstrun == True:
- string2 = word
- currstringsize = len(word)
- firstrun = False
- else:
- string2 = string2 + ' ' + word
- currstringsize += len(word) + 1
- string = string2
- string = string
- if len(string) % 2 != 0:
- string += ' '
- if a == True:
- a = False
- newfiledata = OrigFile[0:StringLoc-2] + struct.pack('>H',len(string)) + string
- else:
- newfiledata += OrigFile[EndStringLoc:StringLoc-2] + struct.pack('>H',len(string)) + string
- currstringsize = 0
- string2 = ''
- stringlist = []
- firstrun = True
- EndStringLoc = StringLoc+OrigLen
- newfiledata += OrigFile[EndStringLoc:] # last string
- outfile = open(filename[:len(filename)-8] + '.new', 'wb')
- outfile.write(newfiledata)
- outfile.close()
- if __name__ == '__main__':
- if sys.argv[2] == '-e':
- extract(sys.argv[1])
- elif sys.argv[2] == '-c':
- compilea(sys.argv[1])
- else:
- print 'Usage: strings.py <filename> <option>'
- print '-c - Compile the .strings file'
- print '-e - Extract strings from file'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement