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