Skip to content

deminy/php-bsdiff

Repository files navigation

php-bsdiff

Build Status PHP version

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/.


Requirements

  • PHP 7.2 to PHP 8.x
  • BZip2 1.0+
  • Linux, macOS, or Windows

Installation

Install via PECL

pecl install bsdiff

# or, if you want to install it without the prompts (i.e., using default installation option(s)):
yes '' | pecl install bsdiff

In 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.

Manual Installation

phpize
./configure
make
make test
make install

Once done, add the following line to your php.ini file:

extension=bsdiff.so

In 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.

Usage

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.

License

The PHP license.