Jump to content

  • Log In with Google      Sign In   
  • Create Account





Python and binary files

Posted by alfith, 08 March 2012 · 1,338 views

Hello.
That was a long silence. I would like to say I worked a lot on the game but I can't. The only thing I coded is the subject of this entry: a python script to "patch" binary files. I am posting it here as it could be useful to someone.
I tested it with Python 2.6 and 3.2. If you want to use the "crc" command, the "crcmod" package is needed.

The python script is used to apply modifications to one or more binary files using a really simple scripting "language". You can:
- read/write/swap values
- do simple calculations
- insert bytes (from other files or using a specified value)
- import other files inside the one being processed
- export/delete parts of the file
- use loops, if/else, macros
- send parameters to the script using the command line
- calculate crc, xor, ...

A small sample script to add 1 to each even byte of a binary file (even length) can be:
cursor 0 //set cursor to the first byte (it is done by default, this line is not needed)
loop SIZE2 #1 // SIZE2 is a constant representing the size of the file divided by 2, #1 is the variable 1 used to store the current loop index
	read @0 #0 // read the byte at cursor position and store its value in variable 0
	sum #0 1 #0 // add 1 to the variable 0 and store the result in variable 0
	write @0 #0 // write at the cursor position the value of the variable 0
	cursor @2 // move the cursor 2 bytes
end_loop

Commands and usage are described in the python file header.

Archive content:
- patch_bin.py: the python script
- demo.patch: demo script
- demo.bin: binary file to update with the demo patch
- import.bin: small binary file imported in the demo.bin by the demo patch

Attached File  patch_bin.tar.gz (8.5KB)
downloads: 74




December 2014 »

S M T W T F S
 123456
78910111213
1415161718 19 20
21222324252627
28293031   

Recent Entries

Recent Comments

Recent Entries

Recent Comments

PARTNERS