bspatch.c revision 303301
1/*-
2 * Copyright 2003-2005 Colin Percival
3 * All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/usr.bin/bsdiff/bspatch/bspatch.c 303301 2016-07-25 14:53:04Z delphij $");
29
30#include <bzlib.h>
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
34#include <err.h>
35#include <unistd.h>
36#include <fcntl.h>
37
38#ifndef O_BINARY
39#define O_BINARY 0
40#endif
41
42static off_t offtin(u_char *buf)
43{
44	off_t y;
45
46	y=buf[7]&0x7F;
47	y=y*256;y+=buf[6];
48	y=y*256;y+=buf[5];
49	y=y*256;y+=buf[4];
50	y=y*256;y+=buf[3];
51	y=y*256;y+=buf[2];
52	y=y*256;y+=buf[1];
53	y=y*256;y+=buf[0];
54
55	if(buf[7]&0x80) y=-y;
56
57	return y;
58}
59
60int main(int argc,char * argv[])
61{
62	FILE * f, * cpf, * dpf, * epf;
63	BZFILE * cpfbz2, * dpfbz2, * epfbz2;
64	int cbz2err, dbz2err, ebz2err;
65	int fd;
66	ssize_t oldsize,newsize;
67	ssize_t bzctrllen,bzdatalen;
68	u_char header[32],buf[8];
69	u_char *old, *new;
70	off_t oldpos,newpos;
71	off_t ctrl[3];
72	off_t lenread;
73	off_t i;
74
75	if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
76
77	/* Open patch file */
78	if ((f = fopen(argv[3], "rb")) == NULL)
79		err(1, "fopen(%s)", argv[3]);
80
81	/*
82	File format:
83		0	8	"BSDIFF40"
84		8	8	X
85		16	8	Y
86		24	8	sizeof(newfile)
87		32	X	bzip2(control block)
88		32+X	Y	bzip2(diff block)
89		32+X+Y	???	bzip2(extra block)
90	with control block a set of triples (x,y,z) meaning "add x bytes
91	from oldfile to x bytes from the diff block; copy y bytes from the
92	extra block; seek forwards in oldfile by z bytes".
93	*/
94
95	/* Read header */
96	if (fread(header, 1, 32, f) < 32) {
97		if (feof(f))
98			errx(1, "Corrupt patch\n");
99		err(1, "fread(%s)", argv[3]);
100	}
101
102	/* Check for appropriate magic */
103	if (memcmp(header, "BSDIFF40", 8) != 0)
104		errx(1, "Corrupt patch\n");
105
106	/* Read lengths from header */
107	bzctrllen=offtin(header+8);
108	bzdatalen=offtin(header+16);
109	newsize=offtin(header+24);
110	if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
111		errx(1,"Corrupt patch\n");
112
113	/* Close patch file and re-open it via libbzip2 at the right places */
114	if (fclose(f))
115		err(1, "fclose(%s)", argv[3]);
116	if ((cpf = fopen(argv[3], "rb")) == NULL)
117		err(1, "fopen(%s)", argv[3]);
118	if (fseeko(cpf, 32, SEEK_SET))
119		err(1, "fseeko(%s, %lld)", argv[3],
120		    (long long)32);
121	if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
122		errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
123	if ((dpf = fopen(argv[3], "rb")) == NULL)
124		err(1, "fopen(%s)", argv[3]);
125	if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
126		err(1, "fseeko(%s, %lld)", argv[3],
127		    (long long)(32 + bzctrllen));
128	if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
129		errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
130	if ((epf = fopen(argv[3], "rb")) == NULL)
131		err(1, "fopen(%s)", argv[3]);
132	if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
133		err(1, "fseeko(%s, %lld)", argv[3],
134		    (long long)(32 + bzctrllen + bzdatalen));
135	if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
136		errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
137
138	if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) ||
139		((oldsize=lseek(fd,0,SEEK_END))==-1) ||
140		((old=malloc(oldsize+1))==NULL) ||
141		(lseek(fd,0,SEEK_SET)!=0) ||
142		(read(fd,old,oldsize)!=oldsize) ||
143		(close(fd)==-1)) err(1,"%s",argv[1]);
144	if((new=malloc(newsize+1))==NULL) err(1,NULL);
145
146	oldpos=0;newpos=0;
147	while(newpos<newsize) {
148		/* Read control data */
149		for(i=0;i<=2;i++) {
150			lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
151			if ((lenread < 8) || ((cbz2err != BZ_OK) &&
152			    (cbz2err != BZ_STREAM_END)))
153				errx(1, "Corrupt patch\n");
154			ctrl[i]=offtin(buf);
155		};
156
157		/* Sanity-check */
158		if ((ctrl[0] < 0) || (ctrl[1] < 0))
159			errx(1,"Corrupt patch\n");
160
161		/* Sanity-check */
162		if(newpos+ctrl[0]>newsize)
163			errx(1,"Corrupt patch\n");
164
165		/* Read diff string */
166		lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
167		if ((lenread < ctrl[0]) ||
168		    ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
169			errx(1, "Corrupt patch\n");
170
171		/* Add old data to diff string */
172		for(i=0;i<ctrl[0];i++)
173			if((oldpos+i>=0) && (oldpos+i<oldsize))
174				new[newpos+i]+=old[oldpos+i];
175
176		/* Adjust pointers */
177		newpos+=ctrl[0];
178		oldpos+=ctrl[0];
179
180		/* Sanity-check */
181		if(newpos+ctrl[1]>newsize)
182			errx(1,"Corrupt patch\n");
183
184		/* Read extra string */
185		lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
186		if ((lenread < ctrl[1]) ||
187		    ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
188			errx(1, "Corrupt patch\n");
189
190		/* Adjust pointers */
191		newpos+=ctrl[1];
192		oldpos+=ctrl[2];
193	};
194
195	/* Clean up the bzip2 reads */
196	BZ2_bzReadClose(&cbz2err, cpfbz2);
197	BZ2_bzReadClose(&dbz2err, dpfbz2);
198	BZ2_bzReadClose(&ebz2err, epfbz2);
199	if (fclose(cpf) || fclose(dpf) || fclose(epf))
200		err(1, "fclose(%s)", argv[3]);
201
202	/* Write the new file */
203	if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))<0) ||
204		(write(fd,new,newsize)!=newsize) || (close(fd)==-1))
205		err(1,"%s",argv[2]);
206
207	free(new);
208	free(old);
209
210	return 0;
211}
212