bitcut
is a simple CLI tool for creating and applying binary patches.
When two binary files are similar, rewriting or transmitting the entire new version can be inefficient. This tool reduces the cost by generating a compact binary patch that represents only the difference. The new file can then be reconstructed by applying the patch to the original.
A patch is a sequence of opcodes:
Copy (0x00, offset, len)
: Copy len bytes from offset in the original file.Add (0x01, len, bytes)
: Add len new bytes directly.
The original file is indexed using a sliding window (default: 8 bytes).
The new file is scanned with the same window.
If a match is found in the original file, the longest matching sequence is emitted as a Copy.
Otherwise, unmatched bytes are grouped into an Add.
To apply a patch:
- Start with an empty output buffer.
- Iterate over the patch:
- Copy: append bytes from the original file.
- Add: append new bytes from the patch.
This reconstructs the new version of the file.
This tool works best when the two binary files are mostly similar — for example, when only small regions have changed.
Creates a binary patch from two files and writes the patch to stdout.
bitcut diff old_file.bin new_file.bin > patch.bin
Applies a binary patch to a file and writes the result to stdout.
bitcut patch old_file.bin patch.bin > new_file.bin
cargo install bitcut
cargo build --release
./target/release/bitcut diff a.bin b.bin > patch
./target/release/bitcut patch a.bin patch > b.bin