Извлечение ресурсов из двоичных файлов: некоторые ресурсы повреждены
Я работаю над проектом, в котором мне нужно извлечь ресурсы (изображения, аудио, GIF-файлы) и т. д.) из двоичных файлов. Я написал сценарий для извлечения PNG, но некоторые изображения извлекаются успешно, другие повреждены. Вот мой код:
main.py:
Код: Выделить всё
import os
from src.bin2png import bin2png
for root, dirs, files in os.walk('.\\Bin'):
for file in files:
if not file.endswith('.bin'):
continue
print(f'Found file: {file}')
bin2png(os.path.join(root, file), './png')
print('DONE')
Код: Выделить всё
import os
import re
import binascii
def bin2png(file_path: str, save_path: str):
counter = 0
png_pattern = re.compile(r'89504E47.*?49454E44AE426082', re.IGNORECASE)
file_path = file_path.replace('\\', '/')
file_name = file_path.split('/')[-1][:-4]
save_path = os.path.join(save_path, file_name)
if not os.path.exists(save_path):
os.mkdir(save_path)
with open(file_path, 'rb') as file:
for match in png_pattern.findall(str(binascii.hexlify(file.read()))):
with open('{}/{}.png'.format(save_path, counter), 'wb+') as image:
image.write(binascii.unhexlify(match))
counter += 1
if counter != 0:
print('Successfully extracted {} PNGs from file {}'.format(counter, file_name))
else:
print('Failed to find PNGs in file {}'.format(file_name))
Код: Выделить всё
A9 70 6E 67 2D 2A 3A 2A 20 20 20 2D 69 68 64 72 20 20 20 5F 20 20 CD 5F 26 75 39 D4 8C EF B0 3E 34 AC C3 23 A9 A3 4D 1D A0 FA 5D F8 32 EF 60 76 75 18 E2 88 D4 24
hex_modifier.py:
Код: Выделить всё
def modify_hex_string(hex_string):
hex_values = hex_string.split()
modified_values = []
for hex_value in hex_values:
int_value = int(hex_value, 16)
modified_value = abs(0x20 - int_value)
modified_value = max(0, min(modified_value, 0xFF))
modified_values.append(format(modified_value, '02X'))
result_string = ' '.join(modified_values)
return result_string
hex_string = "A9 70 6E 67 2D 2A 3A 2A 20 20 20 2D 69 68 64 72 20 20 20 5F 20 20 CD 5F 26 75 39 D4 8C EF B0 3E 34 AC C3 23 A9 A3 4D 1D A0 FA 5D F8 32 EF 60 76 75 18 E2 88 D4 24"
result = modify_hex_string(hex_string)
print(f"Result: [{result}]")
Код: Выделить всё
Result: [89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 00 00 00 3F 00 00 AD 3F 06 55 19 B4 6C CF 90 1E 14 8C A3 03 89 83 2D 03 80 DA 3D D8 12 CF 40 56 55 08 C2 68 B4 04]
Подробнее здесь: https://stackoverflow.com/questions/788 ... inary-file