Advertisement
Guest User

Untitled

a guest
Jan 8th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import os
  2. import sys
  3. import binascii
  4. import struct
  5. import re
  6.  
  7. def get_data(filename):
  8. totalbytes = os.path.getsize(filename)
  9. infile = open(filename, 'rb')
  10. totalfiledata = infile.read(totalbytes)
  11. return totalfiledata
  12.  
  13. def extract(filename):
  14. StringOut = ''
  15. StringCount = 0
  16. StringLocList = []
  17. testchar = 0
  18.  
  19. filedata = get_data(filename)
  20.  
  21. for byte in filedata:
  22. testchar = filedata.find('\xFF',testchar,len(filedata))
  23. if testchar != -1:
  24. if filedata[testchar+1:testchar+2] != '\xFF':
  25. StringCount += 1
  26. testchar += 1
  27. StringLocList.append(testchar+2)
  28. else:
  29. testchar += 4
  30.  
  31. if StringCount != 0:
  32. for StringLoc in StringLocList:
  33. StringLen = struct.unpack('>H',filedata[StringLoc-2:StringLoc])[0]
  34. StringOut += str(hex(StringLoc)) + '#' + str(StringLen) + ':' + filedata[StringLoc:StringLoc+StringLen] + '\n'
  35.  
  36. outfile = open(filename + '.strings', 'wb')
  37. outfile.write(StringOut)
  38. outfile.close()
  39.  
  40. def compilea(filename):
  41. origfilename = filename[:len(filename)-8]
  42. OrigFile = get_data(origfilename)
  43. newfiledata = ''
  44. filedata = get_data(filename)
  45. strings = filedata.splitlines()
  46. a = True
  47. b = 0
  48. firstrun = True
  49. currstringsize = 0
  50. splitchars = '\x1b\x01'
  51.  
  52. for string in strings:
  53. StringLoc,junk,string = string.partition('#')
  54. StringLoc = int(StringLoc[2:],16)
  55. OrigLen,junk,string = string.partition(':')
  56. OrigLen = int(OrigLen,10)
  57. for LineCount in range(0,string.count('\x5c\x6e')):
  58. NewlinePos = string.find('\x5c\x6e')
  59. if NewlinePos % 2 != 0:
  60. tempStrFix = list(string)
  61. tempStrFix.insert(NewlinePos,'\x20')
  62. string = ''.join(tempStrFix)
  63. string = string[:string.find('\x5c\x6e')] + b'\x1b\x01' + string[string.find('\x5c\x6e')+2:]
  64.  
  65. if len(string) > 36:
  66. stringlist = ''.join([ s if s not in splitchars else ' ' for s in string]).split()
  67. for word in stringlist:
  68. if (currstringsize + len(word)) >= 36:
  69. if (len(string2) % 2) != 0:
  70. string2 += ' '
  71. string2 = string2 + '\x1b\x01' + word
  72.  
  73. currstringsize = 0 + len(word)
  74. else:
  75. if firstrun == True:
  76. string2 = word
  77. currstringsize = len(word)
  78. firstrun = False
  79. else:
  80. string2 = string2 + ' ' + word
  81. currstringsize += len(word) + 1
  82. string = string2
  83. string = string
  84. if len(string) % 2 != 0:
  85. string += ' '
  86. if a == True:
  87. a = False
  88. newfiledata = OrigFile[0:StringLoc-2] + struct.pack('>H',len(string)) + string
  89. else:
  90. newfiledata += OrigFile[EndStringLoc:StringLoc-2] + struct.pack('>H',len(string)) + string
  91.  
  92. currstringsize = 0
  93. string2 = ''
  94. stringlist = []
  95. firstrun = True
  96.  
  97. EndStringLoc = StringLoc+OrigLen
  98.  
  99. newfiledata += OrigFile[EndStringLoc:] # last string
  100. outfile = open(filename[:len(filename)-8] + '.new', 'wb')
  101. outfile.write(newfiledata)
  102. outfile.close()
  103.  
  104. if __name__ == '__main__':
  105. if sys.argv[2] == '-e':
  106. extract(sys.argv[1])
  107. elif sys.argv[2] == '-c':
  108. compilea(sys.argv[1])
  109. else:
  110. print 'Usage: strings.py <filename> <option>'
  111. print '-c - Compile the .strings file'
  112. print '-e - Extract strings from file'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement