bsdiff is a library for building and applying patches to binary files.
The original algorithm and implementation was developed by Colin Percival. The algorithm is described in his (unpublished) paper. For more information visit his website at http://www.daemonology.net/bsdiff/.
I maintain this project separately from Colin's work, with the following goals:
- Ability to easily embed the routines as a library instead of an external binary.
- Compatible with the original patch format and can easily adapt to new patch format.
- Support memory-based input/output stream.
- Self-contained 3rd-party libraries, build on Windows/Linux/OSX.
/** * @brief * Generate a patch between two binary files. * @param ctx * The context. * @param oldfile * The stream of the old file. * @param newfile * The stream of the new file. * @param packer * The packer. * @return * BSDIFF_SUCCESS if no error. */BSDIFF_APIintbsdiff(
structbsdiff_ctx*ctx,
structbsdiff_stream*oldfile, structbsdiff_stream*newfile, structbsdiff_patch_packer*packer);
/** * @brief * Apply the patch to the old file, re-create the new file. * @param ctx * The context. * @param oldfile * The stream of the old file. * @param newfile * The stream of the new file. * @param packer * The packer. * @return * BSDIFF_SUCCESS if no error. */BSDIFF_APIintbspatch(
structbsdiff_ctx*ctx,
structbsdiff_stream*oldfile, structbsdiff_stream*newfile,
structbsdiff_patch_packer*packer);#include<stdio.h>#include"bsdiff.h"staticvoidlog_error(void*opaque, constchar*errmsg)
{
(void)opaque;
fprintf(stderr, "%s", errmsg);
}
intgenerate_patch(constchar*oldname, constchar*newname, constchar*patchname)
{
intret=1;
structbsdiff_streamoldfile= { 0 }, newfile= { 0 }, patchfile= { 0 };
structbsdiff_ctxctx= { 0 };
structbsdiff_patch_packerpacker= { 0 };
ret=bsdiff_open_file_stream(BSDIFF_MODE_READ, oldname, &oldfile);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "can't open oldfile: %s\n", oldname);
goto cleanup;
}
ret=bsdiff_open_file_stream(BSDIFF_MODE_READ, newname, &newfile);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "can't open newfile: %s\n", newname);
goto cleanup;
}
ret=bsdiff_open_file_stream(BSDIFF_MODE_WRITE, patchname, &patchfile);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "can't open patchfile: %s\n", patchname);
goto cleanup;
}
ret=bsdiff_open_bz2_patch_packer(BSDIFF_MODE_WRITE, &patchfile, &packer);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "can't create BZ2 patch packer\n");
goto cleanup;
}
ctx.log_error=log_error;
ret=bsdiff(&ctx, &oldfile, &newfile, &packer);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "bsdiff failed: %d\n", ret);
goto cleanup;
}
cleanup:
bsdiff_close_patch_packer(&packer);
bsdiff_close_stream(&patchfile);
bsdiff_close_stream(&newfile);
bsdiff_close_stream(&oldfile);
returnret;
}
intapply_patch(constchar*oldname, constchar*newname, constchar*patchname)
{
intret=1;
structbsdiff_streamoldfile= { 0 }, newfile= { 0 }, patchfile= { 0 };
structbsdiff_ctxctx= { 0 };
structbsdiff_patch_packerpacker= { 0 };
ret=bsdiff_open_file_stream(BSDIFF_MODE_READ, oldname, &oldfile);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "can't open oldfile: %s\n", oldname);
goto cleanup;
}
ret=bsdiff_open_file_stream(BSDIFF_MODE_WRITE, newname, &newfile);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "can't open newfile: %s\n", newname);
goto cleanup;
}
ret=bsdiff_open_file_stream(BSDIFF_MODE_READ, patchname, &patchfile);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "can't open patchfile: %s\n", patchname);
goto cleanup;
}
ret=bsdiff_open_bz2_patch_packer(BSDIFF_MODE_READ, &patchfile, &packer);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "can't create BZ2 patch packer\n");
goto cleanup;
}
ctx.log_error=log_error;
ret=bspatch(&ctx, &oldfile, &newfile, &packer);
if (ret!=BSDIFF_SUCCESS) {
fprintf(stderr, "bspatch failed: %d\n", ret);
goto cleanup;
}
cleanup:
bsdiff_close_patch_packer(&packer);
bsdiff_close_stream(&patchfile);
bsdiff_close_stream(&newfile);
bsdiff_close_stream(&oldfile);
returnret;
}