Modifying binary file with Python

2024/11/16 12:30:56

i am trying to patch a hex file. i have two patch files (hex) named "patch 1" and "patch 2"

the file to be patched is a 16 MB file named "file.bin".

i have tried many different way for the past 6 or 7 hours to figure out how to do it. I can write a string to a file all day long, but i am trying to do the following:

open patch1.bin with read bytes open patch2.bin with read bytes open file.bin with write bytes

i want to seek to positions 0xc0010, and 0x7c0010, and apply patch1.bin then i want to seek to 0x040000 and apply patch2.bin

so all in all i will have 3 patches applied, then close the "file.bin"

if someone cold give me an example i would very much appreciate it :)

i tried this first:

patch1 = open("patch1", "r");
patch2 = open("patch2", "r");
main = open("file.bin", "w");main.seek(0xC0010);
main.write(patch1);
main.seek(0x7C0010);
main.write(patch1);
main.seek(0x40000);
main.write(patch2);
main.close();

but was informed i was was trying to write a string to a file, when indeed its not what i wanted, lol then i tried this:

infile1 = open("patch1.bin", "rb") 
new_pos1 = int("0x00", 16)
infile1.seek(new_pos1, 0)
infile2 = open('file.bin', 'wb')
new_pos2 = int('0xc0010', 16)
infile2.seek(new_pos2, 0xc0010)
chunk1 = int("6FFFE0", 16)         #this is how long patch1 file is
data1 = infile1.read(chunk1)
with open("file.bin", "a") as outfile:outfile.write(data1)

but it did not work either, as no matter what i tried, i could not get it to write the data at he correct offset.

I did manage a few times to write the patch1 to file.bin, but it did not patch at the right offset, as a matter of fact it deleted the file.bin and just copied patch1 in its place. which ofcourse is wrong.

i must remind you i am new to python and programming, but i am really trying to dig my feet into it and learn, so any good examples will be examined and hopefully will be a good learning lesson for me :)

thanks guys and gals for helping me figure out what i was doing wrong :)

Answer

You only need to use seek and write. Use seek to jump to the position and write to overrwite the existing data.

with file('patch1.bin', 'rb') as fh:patch1 = fh.read()with file('patch2.bin', 'rb') as fh:patch2 = fh.read()with file('file.bin', 'r+b') as fh:# apply patch1fh.seek(0xc0010)fh.write(patch1)fh.seek(0x7c0010)fh.write(patch1)# apply patch2fh.seek(0x040000)fh.write(patch2)
https://en.xdnf.cn/q/71344.html

Related Q&A

python error : module object has no attribute AF_UNIX

this is my python code :if __name__ == __main__: import socket sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect((0.0.0.0, 4000)) import time time.sleep(2) #sock.send(1)print …

How to speed up pandas string function?

I am using the pandas vectorized str.split() method to extract the first element returned from a split on "~". I also have also tried using df.apply() with a lambda and str.split() to produc…

sqlalchemy autoloaded orm persistence

We are using sqlalchemys autoload feature to do column mapping to prevent hardcoding in our code.class users(Base):__tablename__ = users__table_args__ = {autoload: True,mysql_engine: InnoDB,mysql_chars…

Data Normalization with tensorflow tf-transform

Im doing a neural network prediction with my own datasets using Tensorflow. The first I did was a model that works with a small dataset in my computer. After this, I changed the code a little bit in or…

Relationship of metaclasss __call__ and instances __init__?

Say Ive got a metaclass and a class using it:class Meta(type):def __call__(cls, *args):print "Meta: __call__ with", argsclass ProductClass(object):__metaclass__ = Metadef __init__(self, *args…

How to present numpy array into pygame surface?

Im writing a code that part of it is reading an image source and displaying it on the screen for the user to interact with. I also need the sharpened image data. I use the following to read the data an…

Following backreferences of unknown kinds in NDB

Im in the process of writing my first RESTful web service atop GAE and the Python 2.7 runtime; Ive started out using Guidos shiny new ndb API.However, Im unsure how to solve a particular case without t…

How to enable math in sphinx?

I am using sphinx with the pngmath extension to document my code that has a lot of mathematical expressions. Doing that in a *.rst file is working just fine.a \times b becomes: However, if I try the sa…

How to set the xticklabels for date in matplotlib

I am trying to plot values from two list. The x axis values are date. Tried these things so faryear = [20070102,20070806,20091208,20111109,20120816,20140117,20140813] yvalues = [-0.5,-0.5,-0.75,-0.75,…

PyParsing: Is this correct use of setParseAction()?

I have strings like this:"MSE 2110, 3030, 4102"I would like to output:[("MSE", 2110), ("MSE", 3030), ("MSE", 4102)]This is my way of going about it, although I h…