Guest User

Untitled

a guest
Jan 20th, 2023
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. import codecs
  2.  
  3.  
  4. def has_english_letters(string):
  5. english_alphabets = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  6. return any(i in english_alphabets for i in string)
  7.  
  8.  
  9. def contains_japanese(string):
  10. japanese_unicode_range = [chr(x) for x in range(12352, 12435)] + [chr(x) for x in range(12449, 12538)]
  11. return any(i in japanese_unicode_range for i in string)
  12.  
  13.  
  14. def get_strings(arr):
  15. result = []
  16. current_string = ""
  17. for i in range(len(arr)):
  18. if arr[i] == "00":
  19. if i > 0 and arr[i - 1] == "00":
  20. current_string += "00"
  21. else:
  22. result.append(current_string)
  23. current_string = "00"
  24. else:
  25. current_string += arr[i]
  26. result.append(current_string)
  27. return result
  28.  
  29.  
  30. def extract(string):
  31. output = ""
  32.  
  33. i = 0
  34. while i < len(string) - 1:
  35. if string[i] == "0" and string[i + 1] == "0":
  36. output += "00"
  37. i = i + 2
  38. else:
  39. break
  40. if len(output) % 2 != 0:
  41. raise AssertionError
  42.  
  43. return output
  44.  
  45.  
  46. def try_recognize(i_line, size_pos_start, size_pos_end, zero_count):
  47. if i_line == "00" and zero_count > 0:
  48. return f"UE:{i_line}"
  49.  
  50. i_sized = is_sized(i_line, size_pos_start, size_pos_end)
  51. i_d_sized = is_sized(i_line, size_pos_start, size_pos_end + 2)
  52.  
  53. if i_d_sized:
  54. i_unicode_string = codecs.decode(i_line[size_pos_end + 2:], 'hex').decode('utf-8')
  55. i_out = f"{zero_count}L:{i_unicode_string}"
  56. elif i_sized:
  57. i_unicode_string = codecs.decode(i_line[size_pos_end:], 'hex').decode('utf-8')
  58. i_out = f"{zero_count}L:{i_unicode_string}"
  59. else:
  60. i_unicode_string = codecs.decode(i_line[size_pos_start:], 'hex').decode('utf-8')
  61. i_out = f"{zero_count}S:{i_unicode_string}"
  62. return i_out
  63.  
  64.  
  65. def is_sized(i_line, size_pos_start, size_pos_end):
  66. i_sized = False
  67. try:
  68. i_text_size = i_line[size_pos_start:size_pos_end]
  69. i_checked_size = int(i_text_size, base=16)
  70. i_unicode_string = codecs.decode(i_line[size_pos_end:], 'hex').decode('utf-8')
  71. if len(bytearray(i_unicode_string, "utf-8")) == i_checked_size:
  72. i_sized = True
  73. except UnicodeDecodeError:
  74. pass
  75. return i_sized
  76.  
  77.  
  78. with open('text.bin', 'rb') as f:
  79. hex_data = f.read().hex()
  80. byte_array = [hex_data[i:i + 2] for i in range(0, len(hex_data), 2)]
  81. byte_set = {}
  82. string_array = []
  83. index = 0
  84. offset = 0
  85. lines = get_strings(byte_array)
  86. result = []
  87. i = 0
  88. new_lines = []
  89. while i < len(lines):
  90. line = lines[i]
  91. if line == "0001":
  92. line = lines[i] + lines[i + 1]
  93. new_lines.append(line)
  94. i += 2
  95. else:
  96. new_lines.append(line)
  97. i += 1
  98.  
  99. for line in new_lines:
  100. try:
  101. if line.startswith("000000"):
  102. result.append(f"WR:{extract(line)}")
  103. line = line.replace(extract(line), "")
  104. out = try_recognize(line, 0, 2, 0)
  105. result.append(out)
  106. elif line.startswith("0000"):
  107. out = try_recognize(line, 4, 6, 2)
  108. result.append(out)
  109. elif line.startswith("00"):
  110. sized = False
  111. out = try_recognize(line, 2, 4, 1)
  112. result.append(out)
  113. else:
  114. out = f"WR:{line}"
  115. result.append(out)
  116. except UnicodeDecodeError as e:
  117. out = f"UE:{line}"
  118. result.append(out)
  119.  
  120. clean_file = open("clean_unpacked.txt", "w")
  121. with open("unpacked.txt", "w") as file:
  122. x = 1
  123. for i in result:
  124. if i.__contains__("\r\n"):
  125. i = i.replace("\r\n", "{rn}")
  126. elif i.__contains__("\n"):
  127. i = i.replace("\n", "{n}")
  128. out = f"{str(x).zfill(5)}:" + i + '\n'
  129. if not contains_japanese(i) and len(i) > 1 and has_english_letters(i[2:]):
  130. clean_file.write(out)
  131. file.write(out)
  132. x += 1
  133. clean_file.close()
  134.  
Advertisement
Add Comment
Please, Sign In to add comment