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