How do I create binary patches?

BinaryPatch

Binary Problem Overview


What's the best way to go about making a patch for a binary file? I want it to be simple for users to apply(a simple patch application would be nice). Running diff on the file just gives Binary files [...] differ

Binary Solutions


Solution 1 - Binary

Check out bsdiff and bspatch (website, manpage, paper, GitHub fork).

To install this tool:

  • Windows: Download and extract this package. You will also need a copy of bzip2.exe in PATH; download that from the "Binaries" link here.
  • macOS: Install Homebrew and use it to install bsdiff.
  • Linux: Use your package manager to install bsdiff.

Solution 2 - Binary

Courgette, by the Google Chrome team, looks like most efficient tool for binary patching executables.

To quote their data:

> Here are the sizes for the recent 190.1 -> 190.4 update on the developer channel: > > - Full update: 10,385,920 bytes > - bsdiff update: 704,512 bytes > - Courgette update: 78,848 bytes

Here are instructions to build it. Here is a Windows binary from 2018 courtesy of Mehrdad.

Solution 3 - Binary

xdelta (website, GitHub) is another option. It seems to be more recent, but otherwise I have no idea how it compares to other tools like bsdiff.

Usage:

  • Creating a patch: xdelta -e -s old_file new_file delta_file
  • Applying a patch: xdelta -d -s old_file delta_file decoded_new_file

Installation:

  • Windows: Download the official binaries.
  • Chocolatey: choco install xdelta3
  • Homebrew: brew install xdelta
  • Linux: Available as xdelta or xdelta3 in your package manager.

Solution 4 - Binary

Modern port: Very useful .NET port for bsdiff/bspatch:

https://github.com/LogosBible/bsdiff.net

My personal choice.

I tested it, and it was the only one of all links. Out of the box I was able to compile it (with Visual Studio, e.g., Visual Studio 2013). (The C++ source elsewhere is a bit outdated and needs at least a bit polishing and is only 32 bit which sets real memory (diff source size) limits. This is a port of this C++ code bsdiff and even tests if the patch results are identical to original code.)

Further idea: With .NET 4.5 you could even get rid of the #Zip library, which is a dependency here.

I haven't measured if it is slightly slower than the C++ code, but it worked fine for me, (bsdiff: 90 MB file in 1-2 minutes), and time-critical for me is only the bspatch, not the bsdiff.

I am not really sure, if the whole memory of a x64 machine is used, but I assume it. The x64 capable build ("Any CPU") works at least. I tried with a 100 MB file.

Besides: The cited Google project 'Courgette' may be the best choice if your main target are executable files. But it is work to build it (for Windows measures, at least), and for binary files it is also using pure bsdiff/bspatch, as far as I have understood the documentation.

Solution 5 - Binary

For small, simple patches, it's easiest just to tell diff to treat the files as text with the -a (or --text) option. As far as I understand, more complicated binary diffs are only useful for reducing the size of patches.

$ man diff | grep -B1 "as text"
       -a, --text
              treat all files as text
$ diff old new
Binary files old and new differ
$ diff -a old new > old.patch
$ patch < old.patch old
patching file old
$ diff old new
$

If the files are the same size and the patch just modifies a few bytes, you can use xxd, which is commonly installed with the OS. The following converts each file to a hex representation with one byte per line, then diffs the files to create a compact patch, then applies the patch.

$ xxd -c1 old > old.hex
$ xxd -c1 new > new.hex
$ diff old.hex new.hex | grep "^+" | grep -v "^++" | sed "s/^+//" > old.hexpatch
$ xxd -c1 -r old.hexpatch old
$ diff old new
$

Solution 6 - Binary

HDiffPatch can run on Windows, macOS, Linux, and Android.

It supports diffs between binary files or directories;

Creating a patch: hdiffz [-m|-s-64] [-c-lzma2] old_path new_path out_delta_file

Applying a patch: hpatchz old_path delta_file out_new_path

Install:

Download from last release, or download the source code & make;

Jojos Binary Diff is another good binary diff algorithm;

Solution 7 - Binary

diff and git-diff can handle binary files by treating them as text with -a.

With git-diff you can also use --binary which produces ASCII encodings of binary files, suitable for pasting into an email for example.

Solution 8 - Binary

https://github.com/reproteq/DiffPatchWpf DiffPatchWpf DiffPatchWpf simple binary patch maker tool.

Compare two binary files and save the differences between them in new file patch.txt

Apply the patch in another binary fast and easy.

Now you can apply the differences in another binary quickly and easily.

example:

1- Load file Aori.bin

2- Load file Amod.bin

3- Compare and save Aori-patch.txt

4- Load file Bori.bin

5- Load patch Aori-patch.txt

6- Apply patch and save file Bori-patched.bin

alt tag

https://youtu.be/EpyuF4t5MWk

Microsoft Visual Studio Community 2019

Versión 16.7.7

.NETFramework,Version=v4.7.2

Tested in windows 10x64bits

Solution 9 - Binary

Assuming you know the structure of the file you could use a C / C++ program to modify it byte by byte:

http://msdn.microsoft.com/en-us/library/c565h7xx(VS.71).aspx

Just read in the old file, and write out a new one modified as you like.

Don't forget to include a file format version number in the file so you know how to read any given version of the file format.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeView Question on Stackoverflow
Solution 1 - BinaryHeinziView Answer on Stackoverflow
Solution 2 - BinaryMaxim KholyavkinView Answer on Stackoverflow
Solution 3 - BinaryJared BeckView Answer on Stackoverflow
Solution 4 - BinaryPhilmView Answer on Stackoverflow
Solution 5 - BinarycjfpView Answer on Stackoverflow
Solution 6 - BinarysisongView Answer on Stackoverflow
Solution 7 - BinaryqwrView Answer on Stackoverflow
Solution 8 - Binaryalejandro gregori tomasView Answer on Stackoverflow
Solution 9 - BinaryJamie ClarkeView Answer on Stackoverflow