Jump to content
  • Advertisement
  • entries
  • comments
  • views

Python and binary files

Sign in to follow this  


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

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

Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!