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#if 0
28__FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $");
29#endif
30
31#include <bzlib.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include <err.h>
36#include <unistd.h>
37#include <fcntl.h>
38#include <sys/types.h>    // android
39
40static off_t offtin(u_char *buf)
41{
42	off_t y;
43
44	y=buf[7]&0x7F;
45	y=y*256;y+=buf[6];
46	y=y*256;y+=buf[5];
47	y=y*256;y+=buf[4];
48	y=y*256;y+=buf[3];
49	y=y*256;y+=buf[2];
50	y=y*256;y+=buf[1];
51	y=y*256;y+=buf[0];
52
53	if(buf[7]&0x80) y=-y;
54
55	return y;
56}
57
58int main(int argc,char * argv[])
59{
60	FILE * f, * cpf, * dpf, * epf;
61	BZFILE * cpfbz2, * dpfbz2, * epfbz2;
62	int cbz2err, dbz2err, ebz2err;
63	int fd;
64	ssize_t oldsize,newsize;
65	ssize_t bzctrllen,bzdatalen;
66	u_char header[32],buf[8];
67	u_char *old, *new;
68	off_t oldpos,newpos;
69	off_t ctrl[3];
70	off_t lenread;
71	off_t i;
72
73	if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
74
75	/* Open patch file */
76	if ((f = fopen(argv[3], "r")) == NULL)
77		err(1, "fopen(%s)", argv[3]);
78
79	/*
80	File format:
81		0	8	"BSDIFF40"
82		8	8	X
83		16	8	Y
84		24	8	sizeof(newfile)
85		32	X	bzip2(control block)
86		32+X	Y	bzip2(diff block)
87		32+X+Y	???	bzip2(extra block)
88	with control block a set of triples (x,y,z) meaning "add x bytes
89	from oldfile to x bytes from the diff block; copy y bytes from the
90	extra block; seek forwards in oldfile by z bytes".
91	*/
92
93	/* Read header */
94	if (fread(header, 1, 32, f) < 32) {
95		if (feof(f))
96			errx(1, "Corrupt patch\n");
97		err(1, "fread(%s)", argv[3]);
98	}
99
100	/* Check for appropriate magic */
101	if (memcmp(header, "BSDIFF40", 8) != 0)
102		errx(1, "Corrupt patch\n");
103
104	/* Read lengths from header */
105	bzctrllen=offtin(header+8);
106	bzdatalen=offtin(header+16);
107	newsize=offtin(header+24);
108	if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
109		errx(1,"Corrupt patch\n");
110
111	/* Close patch file and re-open it via libbzip2 at the right places */
112	if (fclose(f))
113		err(1, "fclose(%s)", argv[3]);
114	if ((cpf = fopen(argv[3], "r")) == NULL)
115		err(1, "fopen(%s)", argv[3]);
116	if (fseeko(cpf, 32, SEEK_SET))
117		err(1, "fseeko(%s, %lld)", argv[3],
118		    (long long)32);
119	if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
120		errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
121	if ((dpf = fopen(argv[3], "r")) == NULL)
122		err(1, "fopen(%s)", argv[3]);
123	if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
124		err(1, "fseeko(%s, %lld)", argv[3],
125		    (long long)(32 + bzctrllen));
126	if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
127		errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
128	if ((epf = fopen(argv[3], "r")) == NULL)
129		err(1, "fopen(%s)", argv[3]);
130	if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
131		err(1, "fseeko(%s, %lld)", argv[3],
132		    (long long)(32 + bzctrllen + bzdatalen));
133	if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
134		errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
135
136	if(((fd=open(argv[1],O_RDONLY,0))<0) ||
137		((oldsize=lseek(fd,0,SEEK_END))==-1) ||
138		((old=malloc(oldsize+1))==NULL) ||
139		(lseek(fd,0,SEEK_SET)!=0) ||
140		(read(fd,old,oldsize)!=oldsize) ||
141		(close(fd)==-1)) err(1,"%s",argv[1]);
142	if((new=malloc(newsize+1))==NULL) err(1,NULL);
143
144	oldpos=0;newpos=0;
145	while(newpos<newsize) {
146		/* Read control data */
147		for(i=0;i<=2;i++) {
148			lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
149			if ((lenread < 8) || ((cbz2err != BZ_OK) &&
150			    (cbz2err != BZ_STREAM_END)))
151				errx(1, "Corrupt patch\n");
152			ctrl[i]=offtin(buf);
153		};
154
155		// android local change (start)
156		if (ctrl[0]<0||ctrl[1]<0)
157			errx(1,"Corrupt patch\n");
158		// android local change (end)
159
160		/* Sanity-check */
161		if(newpos+ctrl[0]>newsize)
162			errx(1,"Corrupt patch\n");
163
164		/* Read diff string */
165		lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
166		if ((lenread < ctrl[0]) ||
167		    ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
168			errx(1, "Corrupt patch\n");
169
170		/* Add old data to diff string */
171		for(i=0;i<ctrl[0];i++)
172			if((oldpos+i>=0) && (oldpos+i<oldsize))
173				new[newpos+i]+=old[oldpos+i];
174
175		/* Adjust pointers */
176		newpos+=ctrl[0];
177		oldpos+=ctrl[0];
178
179		/* Sanity-check */
180		if(newpos+ctrl[1]>newsize)
181			errx(1,"Corrupt patch\n");
182
183		/* Read extra string */
184		lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
185		if ((lenread < ctrl[1]) ||
186		    ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
187			errx(1, "Corrupt patch\n");
188
189		/* Adjust pointers */
190		newpos+=ctrl[1];
191		oldpos+=ctrl[2];
192	};
193
194	/* Clean up the bzip2 reads */
195	BZ2_bzReadClose(&cbz2err, cpfbz2);
196	BZ2_bzReadClose(&dbz2err, dpfbz2);
197	BZ2_bzReadClose(&ebz2err, epfbz2);
198	if (fclose(cpf) || fclose(dpf) || fclose(epf))
199		err(1, "fclose(%s)", argv[3]);
200
201	/* Write the new file */
202	if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) ||
203		(write(fd,new,newsize)!=newsize) || (close(fd)==-1))
204		err(1,"%s",argv[2]);
205
206	free(new);
207	free(old);
208
209	return 0;
210}
211