New to the forum, I hope this is in the correct place. Its purpose is to search a binary file, pages (4096) that are full of EE and write them to FF like they should be(see comments), so far this code never opens my file and runs**. I know it has an unused import I have changed this script alot and tried some different tools within it.**
import mmap
import os
import sys
filename = “2101.bin”
def replace_ee_pages_with_ff(filename, page_size=4096):
“”"
Detects pages filled with 0xEE bytes in a binary file and replaces them
with pages of 0xFF bytes.
Args:
filename (str): The path to the binary file.
page_size (int): The size of a memory page in bytes (default is 4096).
"""
# Define the byte patterns
pattern_EE = b'\xEE' * page_size
pattern_FF = b'\xFF' * page_size
try:
# Open the file in binary read/write mode
with open(filename, 'r+b') as f:
# Memory map the file, allowing in-place modification
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE) as mm:
file_size = len(mm)
print(f"File size: {file_size} bytes. Page size: {page_size} bytes.")
# Iterate through the file in page-sized chunks
for offset in range(0, file_size, page_size):
# Check if the current chunk is a full page
if offset + page_size <= file_size:
# Read the page content
current_page = mm[offset:offset + page_size]
# Check if the page is entirely 0xEE
if current_page == pattern_EE:
# If it is, replace it with 0xFF bytes
mm[offset:offset + page_size] = pattern_FF
print(f"Replaced page at offset {offset:#x}")
# Force the changes to be written to disk
mm.flush()
print("Replacement complete. Changes flushed to disk.")
except FileNotFoundError:
print(f"Error: The file '{filename}' was not found.")
sys.exit(1)
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)
any help would be great thank you!
