GithubHelp home page GithubHelp logo

zhuyie / bsdiff Goto Github PK

View Code? Open in Web Editor NEW
33.0 2.0 13.0 11.06 MB

bsdiff is a library for building and applying patches to binary files.

License: Other

CMake 3.64% C 96.36%
bsdiff binary-diffing software-update delta-encoding suffix-array

bsdiff's Introduction

bsdiff

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.

API

/**
 * @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_API
int bsdiff(
	struct bsdiff_ctx *ctx,
	struct bsdiff_stream *oldfile, 
	struct bsdiff_stream *newfile, 
	struct bsdiff_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_API
int bspatch(
	struct bsdiff_ctx *ctx,
	struct bsdiff_stream *oldfile, 
	struct bsdiff_stream *newfile,
	struct bsdiff_patch_packer *packer);

Demo Usage

#include <stdio.h>
#include "bsdiff.h"

static void log_error(void *opaque, const char *errmsg)
{
	(void)opaque;
	fprintf(stderr, "%s", errmsg);
}

int generate_patch(const char *oldname, const char *newname, const char *patchname)
{
	int ret = 1;
	struct bsdiff_stream oldfile = { 0 }, newfile = { 0 }, patchfile = { 0 };
	struct bsdiff_ctx ctx = { 0 };
	struct bsdiff_patch_packer packer = { 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);

	return ret;
}

int apply_patch(const char *oldname, const char *newname, const char *patchname)
{
	int ret = 1;
	struct bsdiff_stream oldfile = { 0 }, newfile = { 0 }, patchfile = { 0 };
	struct bsdiff_ctx ctx = { 0 };
	struct bsdiff_patch_packer packer = { 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);

	return ret;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.