|
| 1 | +bsdiff/bspatch |
| 2 | +============== |
| 3 | +bsdiff and bspatch are libraries for building and applying patches to binary |
| 4 | +files. |
| 5 | + |
| 6 | +The original algorithm and implementation was developed by Colin Percival. The |
| 7 | +algorithm is detailed in his paper, [Naïve Differences of Executable Code](http://www.daemonology.net/papers/bsdiff.pdf). For more information, visit his |
| 8 | +website at <http://www.daemonology.net/bsdiff/>. |
| 9 | + |
| 10 | +I maintain this project separately from Colin's work, with the goal of making |
| 11 | +the core functionality easily embeddable in existing projects. |
| 12 | + |
| 13 | +Contact |
| 14 | +------- |
| 15 | +[@MatthewEndsley](https://twitter.com/#!/MatthewEndsley) |
| 16 | +<https://github.com/mendsley/bsdiff> |
| 17 | + |
| 18 | +License |
| 19 | +------- |
| 20 | +Copyright 2003-2005 Colin Percival |
| 21 | +Copyright 2012 Matthew Endsley |
| 22 | + |
| 23 | +This project is governed by the BSD 2-clause license. For details see the file |
| 24 | +titled LICENSE in the project root folder. |
| 25 | + |
| 26 | +Overview |
| 27 | +-------- |
| 28 | +There are two separate libraries in the project, bsdiff and bspatch. Each are |
| 29 | +self contained in bsdiff.c and bspatch.c The easiest way to integrate is to |
| 30 | +simply copy the c file to your source folder and build it. |
| 31 | + |
| 32 | +The overarching goal was to modify the original bsdiff/bspatch code from Colin |
| 33 | +and eliminate external dependencies and provide a simple interface to the core |
| 34 | +functionality. |
| 35 | + |
| 36 | +I've exposed relevant functions via the `_stream` classes. The only external |
| 37 | +dependency not exposed is `memcmp` in `bsdiff`. |
| 38 | + |
| 39 | +This library generates patches that are not compatible with the original bsdiff |
| 40 | +tool. The incompatibilities were motivated by the patching needs for the game |
| 41 | +AirMech <https://www.carbongames.com> and the following requirements: |
| 42 | + |
| 43 | +* Eliminate/minimize any seek operations when applying patches |
| 44 | +* Eliminate any required disk I/O and support embedded streams |
| 45 | +* Ability to easily embed the routines as a library instead of an external binary |
| 46 | +* Compile+run on all platforms we use to build the game (Windows, Linux, NaCl, OSX) |
| 47 | + |
| 48 | +Compiling |
| 49 | +--------- |
| 50 | +The libraries should compile warning free in any moderately recent version of |
| 51 | +gcc. The project uses `<stdint.h>` which is technically a C99 file and not |
| 52 | +available in Microsoft Visual Studio. The easiest solution here is to use the |
| 53 | +msinttypes version of stdint.h from <https://code.google.com/p/msinttypes/>. |
| 54 | +The direct link for the lazy people is: |
| 55 | +<https://msinttypes.googlecode.com/svn/trunk/stdint.h>. |
| 56 | + |
| 57 | +If your compiler does not provide an implementation of `<stdint.h>` you can |
| 58 | +remove the header from the bsdiff/bspatch files and provide your own typedefs |
| 59 | +for the following symbols: `uint8_t`, `uint64_t` and `int64_t`. |
| 60 | + |
| 61 | +Examples |
| 62 | +-------- |
| 63 | +Each project has an optional main function that serves as an example for using |
| 64 | +the library. Simply defined `BSDIFF_EXECUTABLE` or `BSPATCH_EXECUTABLE` to |
| 65 | +enable building the standalone tools. |
| 66 | + |
| 67 | +Reference |
| 68 | +--------- |
| 69 | +### bsdiff |
| 70 | + |
| 71 | + struct bsdiff_stream |
| 72 | + { |
| 73 | + void* opaque; |
| 74 | + void* (*malloc)(size_t size); |
| 75 | + void (*free)(void* ptr); |
| 76 | + int (*write)(struct bsdiff_stream* stream, |
| 77 | + const void* buffer, int size); |
| 78 | + }; |
| 79 | + |
| 80 | + int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, |
| 81 | + int64_t newsize, struct bsdiff_stream* stream); |
| 82 | + |
| 83 | + |
| 84 | +In order to use `bsdiff`, you need to define functions for allocating memory and |
| 85 | +writing binary data. This behavior is controlled by the `stream` parameter |
| 86 | +passed to to `bsdiff(...)`. |
| 87 | + |
| 88 | +The `opaque` field is never read or modified from within the `bsdiff` function. |
| 89 | +The caller can use this field to store custom state data needed for the callback |
| 90 | +functions. |
| 91 | + |
| 92 | +The `malloc` and `free` members should point to functions that behave like the |
| 93 | +standard `malloc` and `free` C functions. |
| 94 | + |
| 95 | +The `write` function is called by bsdiff to write a block of binary data to the |
| 96 | +stream. The return value for `write` should be `0` on success and non-zero if |
| 97 | +the callback failed to write all data. In the default example, bzip2 is used to |
| 98 | +compress output data. |
| 99 | + |
| 100 | +`bsdiff` returns `0` on success and `-1` on failure. |
| 101 | + |
| 102 | +### bspatch |
| 103 | + |
| 104 | + struct bspatch_stream |
| 105 | + { |
| 106 | + void* opaque; |
| 107 | + int (*read)(const struct bspatch_stream* stream, |
| 108 | + void* buffer, int length); |
| 109 | + }; |
| 110 | + |
| 111 | + int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, |
| 112 | + int64_t newsize, struct bspatch_stream* stream); |
| 113 | + |
| 114 | +The `bspatch` function transforms the data for a file using data generated from |
| 115 | +`bsdiff`. The caller takes care of loading the old file and allocating space for |
| 116 | +new file data. The `stream` parameter controls the process for reading binary |
| 117 | +patch data. |
| 118 | + |
| 119 | +The `opaque` field is never read or modified from within the bspatch function. |
| 120 | +The caller can use this field to store custom state data needed for the read |
| 121 | +function. |
| 122 | + |
| 123 | +The `read` function is called by `bspatch` to read a block of binary data from |
| 124 | +the stream. The return value for `read` should be `0` on success and non-zero |
| 125 | +if the callback failed to read the requested amount of data. In the default |
| 126 | +example, bzip2 is used to decompress input data. |
| 127 | + |
| 128 | +`bspatch` returns `0` on success and `-1` on failure. On success, `new` contains |
| 129 | +the data for the patched file. |
0 commit comments