Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import codecs
- def has_english_letters(string):
- english_alphabets = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
- return any(i in english_alphabets for i in string)
- def contains_japanese(string):
- japanese_unicode_range = [chr(x) for x in range(12352, 12435)] + [chr(x) for x in range(12449, 12538)]
- return any(i in japanese_unicode_range for i in string)
- def get_strings(arr):
- result = []
- current_string = ""
- for i in range(len(arr)):
- if arr[i] == "00":
- if i > 0 and arr[i - 1] == "00":
- current_string += "00"
- else:
- result.append(current_string)
- current_string = "00"
- else:
- current_string += arr[i]
- result.append(current_string)
- return result
- def extract(string):
- output = ""
- i = 0
- while i < len(string) - 1:
- if string[i] == "0" and string[i + 1] == "0":
- output += "00"
- i = i + 2
- else:
- break
- if len(output) % 2 != 0:
- raise AssertionError
- return output
- def try_recognize(i_line, size_pos_start, size_pos_end, zero_count):
- if i_line == "00" and zero_count > 0:
- return f"UE:{i_line}"
- i_sized = is_sized(i_line, size_pos_start, size_pos_end)
- i_d_sized = is_sized(i_line, size_pos_start, size_pos_end + 2)
- if i_d_sized:
- i_unicode_string = codecs.decode(i_line[size_pos_end + 2:], 'hex').decode('utf-8')
- i_out = f"{zero_count}L:{i_unicode_string}"
- elif i_sized:
- i_unicode_string = codecs.decode(i_line[size_pos_end:], 'hex').decode('utf-8')
- i_out = f"{zero_count}L:{i_unicode_string}"
- else:
- i_unicode_string = codecs.decode(i_line[size_pos_start:], 'hex').decode('utf-8')
- i_out = f"{zero_count}S:{i_unicode_string}"
- return i_out
- def is_sized(i_line, size_pos_start, size_pos_end):
- i_sized = False
- try:
- i_text_size = i_line[size_pos_start:size_pos_end]
- i_checked_size = int(i_text_size, base=16)
- i_unicode_string = codecs.decode(i_line[size_pos_end:], 'hex').decode('utf-8')
- if len(bytearray(i_unicode_string, "utf-8")) == i_checked_size:
- i_sized = True
- except UnicodeDecodeError:
- pass
- return i_sized
- with open('text.bin', 'rb') as f:
- hex_data = f.read().hex()
- byte_array = [hex_data[i:i + 2] for i in range(0, len(hex_data), 2)]
- byte_set = {}
- string_array = []
- index = 0
- offset = 0
- lines = get_strings(byte_array)
- result = []
- i = 0
- new_lines = []
- while i < len(lines):
- line = lines[i]
- if line == "0001":
- line = lines[i] + lines[i + 1]
- new_lines.append(line)
- i += 2
- else:
- new_lines.append(line)
- i += 1
- for line in new_lines:
- try:
- if line.startswith("000000"):
- result.append(f"WR:{extract(line)}")
- line = line.replace(extract(line), "")
- out = try_recognize(line, 0, 2, 0)
- result.append(out)
- elif line.startswith("0000"):
- out = try_recognize(line, 4, 6, 2)
- result.append(out)
- elif line.startswith("00"):
- sized = False
- out = try_recognize(line, 2, 4, 1)
- result.append(out)
- else:
- out = f"WR:{line}"
- result.append(out)
- except UnicodeDecodeError as e:
- out = f"UE:{line}"
- result.append(out)
- clean_file = open("clean_unpacked.txt", "w")
- with open("unpacked.txt", "w") as file:
- x = 1
- for i in result:
- if i.__contains__("\r\n"):
- i = i.replace("\r\n", "{rn}")
- elif i.__contains__("\n"):
- i = i.replace("\n", "{n}")
- out = f"{str(x).zfill(5)}:" + i + '\n'
- if not contains_japanese(i) and len(i) > 1 and has_english_letters(i[2:]):
- clean_file.write(out)
- file.write(out)
- x += 1
- clean_file.close()
Advertisement
Add Comment
Please, Sign In to add comment