Blame bsdiff/bspatch.c

rpm-build 0fba15
/*-
rpm-build 0fba15
 * Copyright 2003-2005 Colin Percival
rpm-build 0fba15
 * Copyright 2012 Matthew Endsley
rpm-build 0fba15
 * All rights reserved
rpm-build 0fba15
 *
rpm-build 0fba15
 * Redistribution and use in source and binary forms, with or without
rpm-build 0fba15
 * modification, are permitted providing that the following conditions 
rpm-build 0fba15
 * are met:
rpm-build 0fba15
 * 1. Redistributions of source code must retain the above copyright
rpm-build 0fba15
 *    notice, this list of conditions and the following disclaimer.
rpm-build 0fba15
 * 2. Redistributions in binary form must reproduce the above copyright
rpm-build 0fba15
 *    notice, this list of conditions and the following disclaimer in the
rpm-build 0fba15
 *    documentation and/or other materials provided with the distribution.
rpm-build 0fba15
 *
rpm-build 0fba15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
rpm-build 0fba15
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
rpm-build 0fba15
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
rpm-build 0fba15
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
rpm-build 0fba15
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
rpm-build 0fba15
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
rpm-build 0fba15
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
rpm-build 0fba15
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
rpm-build 0fba15
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
rpm-build 0fba15
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
rpm-build 0fba15
 * POSSIBILITY OF SUCH DAMAGE.
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
#include "bspatch.h"
rpm-build 0fba15
rpm-build 0fba15
static int64_t offtin(uint8_t *buf)
rpm-build 0fba15
{
rpm-build 0fba15
	int64_t y;
rpm-build 0fba15
rpm-build 0fba15
	y=buf[7]&0x7F;
rpm-build 0fba15
	y=y*256;y+=buf[6];
rpm-build 0fba15
	y=y*256;y+=buf[5];
rpm-build 0fba15
	y=y*256;y+=buf[4];
rpm-build 0fba15
	y=y*256;y+=buf[3];
rpm-build 0fba15
	y=y*256;y+=buf[2];
rpm-build 0fba15
	y=y*256;y+=buf[1];
rpm-build 0fba15
	y=y*256;y+=buf[0];
rpm-build 0fba15
rpm-build 0fba15
	if(buf[7]&0x80) y=-y;
rpm-build 0fba15
rpm-build 0fba15
	return y;
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
int bspatch(const uint8_t* old, int64_t oldsize, uint8_t* new, int64_t newsize, struct bspatch_stream* stream)
rpm-build 0fba15
{
rpm-build 0fba15
	uint8_t buf[8];
rpm-build 0fba15
	int64_t oldpos,newpos;
rpm-build 0fba15
	int64_t ctrl[3];
rpm-build 0fba15
	int64_t i;
rpm-build 0fba15
rpm-build 0fba15
	oldpos=0;newpos=0;
rpm-build 0fba15
	while(newpos
rpm-build 0fba15
		/* Read control data */
rpm-build 0fba15
		for(i=0;i<=2;i++) {
rpm-build 0fba15
			if (stream->read(stream, buf, 8))
rpm-build 0fba15
				return -1;
rpm-build 0fba15
			ctrl[i]=offtin(buf);
rpm-build 0fba15
		};
rpm-build 0fba15
rpm-build 0fba15
		/* Sanity-check */
rpm-build 0fba15
		if(newpos+ctrl[0]>newsize)
rpm-build 0fba15
			return -1;
rpm-build 0fba15
rpm-build 0fba15
		/* Read diff string */
rpm-build 0fba15
		if (stream->read(stream, new + newpos, ctrl[0]))
rpm-build 0fba15
			return -1;
rpm-build 0fba15
rpm-build 0fba15
		/* Add old data to diff string */
rpm-build 0fba15
		for(i=0;i
rpm-build 0fba15
			if((oldpos+i>=0) && (oldpos+i
rpm-build 0fba15
				new[newpos+i]+=old[oldpos+i];
rpm-build 0fba15
rpm-build 0fba15
		/* Adjust pointers */
rpm-build 0fba15
		newpos+=ctrl[0];
rpm-build 0fba15
		oldpos+=ctrl[0];
rpm-build 0fba15
rpm-build 0fba15
		/* Sanity-check */
rpm-build 0fba15
		if(newpos+ctrl[1]>newsize)
rpm-build 0fba15
			return -1;
rpm-build 0fba15
rpm-build 0fba15
		/* Read extra string */
rpm-build 0fba15
		if (stream->read(stream, new + newpos, ctrl[1]))
rpm-build 0fba15
			return -1;
rpm-build 0fba15
rpm-build 0fba15
		/* Adjust pointers */
rpm-build 0fba15
		newpos+=ctrl[1];
rpm-build 0fba15
		oldpos+=ctrl[2];
rpm-build 0fba15
	};
rpm-build 0fba15
rpm-build 0fba15
	return 0;
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
#if defined(BSPATCH_EXECUTABLE)
rpm-build 0fba15
rpm-build 0fba15
#include <bzlib.h>
rpm-build 0fba15
#include <stdlib.h>
rpm-build 0fba15
#include <stdint.h>
rpm-build 0fba15
#include <stdio.h>
rpm-build 0fba15
#include <string.h>
rpm-build 0fba15
#include <err.h>
rpm-build 0fba15
#include <unistd.h>
rpm-build 0fba15
#include <fcntl.h>
rpm-build 0fba15
rpm-build 0fba15
static int bz2_read(const struct bspatch_stream* stream, void* buffer, int length)
rpm-build 0fba15
{
rpm-build 0fba15
	int n;
rpm-build 0fba15
	int bz2err;
rpm-build 0fba15
	BZFILE* bz2;
rpm-build 0fba15
rpm-build 0fba15
	bz2 = (BZFILE*)stream->opaque;
rpm-build 0fba15
	n = BZ2_bzRead(&bz2err, bz2, buffer, length);
rpm-build 0fba15
	if (n != length)
rpm-build 0fba15
		return -1;
rpm-build 0fba15
rpm-build 0fba15
	return 0;
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
int main(int argc,char * argv[])
rpm-build 0fba15
{
rpm-build 0fba15
	FILE * f;
rpm-build 0fba15
	int fd;
rpm-build 0fba15
	int bz2err;
rpm-build 0fba15
	uint8_t header[24];
rpm-build 0fba15
	uint8_t *old, *new;
rpm-build 0fba15
	int64_t oldsize, newsize;
rpm-build 0fba15
	BZFILE* bz2;
rpm-build 0fba15
	struct bspatch_stream stream;
rpm-build 0fba15
rpm-build 0fba15
	if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
rpm-build 0fba15
rpm-build 0fba15
	/* Open patch file */
rpm-build 0fba15
	if ((f = fopen(argv[3], "r")) == NULL)
rpm-build 0fba15
		err(1, "fopen(%s)", argv[3]);
rpm-build 0fba15
rpm-build 0fba15
	/* Read header */
rpm-build 0fba15
	if (fread(header, 1, 24, f) != 24) {
rpm-build 0fba15
		if (feof(f))
rpm-build 0fba15
			errx(1, "Corrupt patch\n");
rpm-build 0fba15
		err(1, "fread(%s)", argv[3]);
rpm-build 0fba15
	}
rpm-build 0fba15
rpm-build 0fba15
	/* Check for appropriate magic */
rpm-build 0fba15
	if (memcmp(header, "ENDSLEY/BSDIFF43", 16) != 0)
rpm-build 0fba15
		errx(1, "Corrupt patch\n");
rpm-build 0fba15
rpm-build 0fba15
	/* Read lengths from header */
rpm-build 0fba15
	newsize=offtin(header+16);
rpm-build 0fba15
	if(newsize<0)
rpm-build 0fba15
		errx(1,"Corrupt patch\n");
rpm-build 0fba15
rpm-build 0fba15
	/* Close patch file and re-open it via libbzip2 at the right places */
rpm-build 0fba15
	if(((fd=open(argv[1],O_RDONLY,0))<0) ||
rpm-build 0fba15
		((oldsize=lseek(fd,0,SEEK_END))==-1) ||
rpm-build 0fba15
		((old=malloc(oldsize+1))==NULL) ||
rpm-build 0fba15
		(lseek(fd,0,SEEK_SET)!=0) ||
rpm-build 0fba15
		(read(fd,old,oldsize)!=oldsize) ||
rpm-build 0fba15
		(close(fd)==-1)) err(1,"%s",argv[1]);
rpm-build 0fba15
	if((new=malloc(newsize+1))==NULL) err(1,NULL);
rpm-build 0fba15
rpm-build 0fba15
	if (NULL == (bz2 = BZ2_bzReadOpen(&bz2err, f, 0, 0, NULL, 0)))
rpm-build 0fba15
		errx(1, "BZ2_bzReadOpen, bz2err=%d", bz2err);
rpm-build 0fba15
rpm-build 0fba15
	stream.read = bz2_read;
rpm-build 0fba15
	stream.opaque = bz2;
rpm-build 0fba15
	if (bspatch(old, oldsize, new, newsize, &stream))
rpm-build 0fba15
		errx(1, "bspatch");
rpm-build 0fba15
rpm-build 0fba15
	/* Clean up the bzip2 reads */
rpm-build 0fba15
	BZ2_bzReadClose(&bz2err, bz2);
rpm-build 0fba15
	fclose(f);
rpm-build 0fba15
rpm-build 0fba15
	/* Write the new file */
rpm-build 0fba15
	if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) ||
rpm-build 0fba15
		(write(fd,new,newsize)!=newsize) || (close(fd)==-1))
rpm-build 0fba15
		err(1,"%s",argv[2]);
rpm-build 0fba15
rpm-build 0fba15
	free(new);
rpm-build 0fba15
	free(old);
rpm-build 0fba15
rpm-build 0fba15
	return 0;
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
#endif