head     1.1;
branch   1.1.1;
access   ;
symbols  binpatch-base:1.1.1.1 CROOKS:1.1.1;
locks    ; strict;
comment  @# @;


1.1
date     2013.01.11.06.27.00;  author agc;  state Exp;
branches 1.1.1.1;
next     ;

1.1.1.1
date     2013.01.11.06.27.00;  author agc;  state Exp;
branches ;
next     ;


desc
@@



1.1
log
@Initial revision
@
text
@#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include <err.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// bufgap

typedef struct f_t {
	char	*name;
	char	*mapped;
	FILE	*fp;
	size_t	 size;
} f_t;

static int
finit(f_t *file, const char *name)
{
	struct stat	st;

	memset(file, 0x0, sizeof(*file));
	if ((file->fp = fopen(name, "r")) == NULL) {
		return 0;
	}
	fstat(fileno(file->fp), &st);
	file->size = st.st_size;
	file->mapped = mmap(NULL, file->size, PROT_READ, MAP_SHARED, fileno(file->fp), 0);
	// xxx
	file->name = strdup(name);
	return 1;
}

static void
fend(f_t *f)
{
	free(f->name);
	munmap(f->mapped, f->size);
	fclose(f->fp);
	memset(f, 0x0, sizeof(*f));
}

static int
diff(const char *filename1, const char *filename2)
{
	FILE	*pp;
	char	 buf[2048]; // XXX
	char	 cmd[2048]; // XXX
	f_t	 f[2];

	finit(&f[0], filename1);
	finit(&f[1], filename2);
	snprintf(cmd, sizeof(cmd), "diff %s %s", filename1, filename2);
	if ((pp = popen(cmd, "r")) == NULL) {
		return 0;
	}
	while (fgets(buf, sizeof(buf), pp) != NULL) {
		printf("%s", buf);
	}
	pclose(pp);
	fend(&f[0]);
	fend(&f[1]);
	return 1;
}

int
main(int argc, char **argv)
{
	diff(argv[optind], argv[optind + 1]);
	exit(EXIT_SUCCESS);
}
@


1.1.1.1
log
@Import a binary patch library, libbinpatch(3), and program,
binpatch(1), into the othersrc repository.  This can be used to encode
a binary patch file from an original file, and a new file.  To do
this, 3 separate "instructions" are used, similar to those outlined in
RFC 3284 -

	+ add - addition of new data
	+ copy - copying of data from original file
	+ run - a run of characters, similar to memset(3)

The binary patches generated here are a much simplified form of those
described in the RFC, but have 64bit offsets and 32bit sizes in the
instructions.  The generated binary patch files are also much smaller
than the original files, and even standard diffs, and are only just
larger then the equivalent vcdiff binary patch file.

	% diff f1 f2 | wc
	      58     338    1969
	% diff -u f1 f2 | wc 
	      85     408    2530
	% binpatch -e f1 f2 | wc
	      44     269    1801
	% vcdiff encode -dictionary f1 -target f2 | wc
	      40     257    1663

(The last is the Google vcdiff program found in pkgsrc/devel/open-vcdiff)

The binpatch(1) program can be used to generate binary patch files,
and to reconstruct the target files, in the following way

	% binpatch -e -o patch f1 f2
	% binpatch -f f1 -o recon patch
	% diff f2 recon
	% rm -f recon
	% binpatch -v -f f1 -o recon patch
	Add  1337 bytes
	Copy 187 bytes from 0
	Copy 386 bytes from 198
	Add  50 bytes
	Copy 173 bytes from 592
	Add  55 bytes
	Copy 373 bytes from 816
	Add  74 bytes
	Copy 34 bytes from 1189
	Add  182 bytes
	Copy 23 bytes from 1262
	diff f2 recon
	% diff f2 recon
	% rm -f recon

binpatch(1) uses libbinpatch(3) and libnetdiff(3) to encode binary
patch files, and libbinpatch(3) to decode binary patch files.
@
text
@@
