bsdiff is a PHP extension to build and apply patches to binary files.
This PHP extension is based on the bsdiff and bspatch libraries maintained by Matthew Endsley. The original algorithm and implementation was developed by Colin Percival. The algorithm is detailed in Colin's paper, Naïve Differences of Executable Code. For more information, visit his website at http://www.daemonology.net/bsdiff/.
- PHP 7.2 to PHP 8.x
- BZip2 1.0+
- Linux, macOS, or Windows
pecl install bsdiff
# or, if you want to install it without the prompts (i.e., using default installation option(s)):
yes '' | pecl install bsdiffIn case BZip2 can't be found automatically, use option with-bz2 to specify the installation directory of BZip2. e.g.,
pecl install -D 'with-bz2="/usr/local/opt/bzip2"' bsdiff # If BZip2 is installed via Homebrew on MacOS.phpize
./configure
make
make test
make installOnce done, add the following line to your php.ini file:
extension=bsdiff.soIn case BZip2 can't be found automatically, use option --with-bz2 to specify the installation directory of BZip2. e.g.,
./configure --with-bz2=/usr/local/opt/bzip2 # If BZip2 is installed via Homebrew on MacOS.There are two PHP functions added by the extension:
/**
* @throws \BsdiffException If there is any error happens.
*/
function bsdiff_diff(string $old_file, string $new_file, string $diff_file): void {}
/**
* @throws \BsdiffException If there is any error happens.
*/
function bsdiff_patch(string $old_file, string $new_file, string $diff_file): void {}Here is an example on how to use the extension:
<?php
$old_file = '/path/to/the/old/file';
$new_file = '/path/to/the/new/file';
$diff_file = '/path/to/the/diff/file';
// To create the diff file.
try {
bsdiff_diff($old_file, $new_file, $diff_file);
} catch (BsdiffException $e) {
// Handle the exception.
}
// To apply the diff file.
$patched_file = '/path/to/the/patched/file';
try {
bsdiff_patch($old_file, $patched_file, $diff_file);
} catch (BsdiffException $e) {
// Handle the exception.
}
// File $patched_file will have exactly the same content as file $new_file.