blob: 319a42cfbddf12cc7a94fb81a6ec90fffb02e641 [file] [log] [blame]
The Android Open Source Projectc285fea2009-03-03 19:29:20 -08001/*-
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
Alex Deymob870eb52015-10-14 21:39:04 -070031#include "bspatch.h"
32
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080033#include <bzlib.h>
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080034#include <err.h>
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080035#include <fcntl.h>
Gilad Arnold99b53742013-04-30 09:24:14 -070036#include <inttypes.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
Alex Deymob870eb52015-10-14 21:39:04 -070041#include <sys/types.h>
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080042
Gilad Arnold99b53742013-04-30 09:24:14 -070043#include "extents.h"
Zdenek Behan339e0ee2010-06-14 12:50:53 -070044
Alex Deymo20891f92015-10-12 17:28:04 -070045namespace {
Zdenek Behan339e0ee2010-06-14 12:50:53 -070046
Alex Deymob870eb52015-10-14 21:39:04 -070047static off_t offtin(u_char *buf) {
48 off_t y;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080049
Alex Deymob870eb52015-10-14 21:39:04 -070050 y = buf[7] & 0x7F;
51 y = y * 256;
52 y += buf[6];
53 y = y * 256;
54 y += buf[5];
55 y = y * 256;
56 y += buf[4];
57 y = y * 256;
58 y += buf[3];
59 y = y * 256;
60 y += buf[2];
61 y = y * 256;
62 y += buf[1];
63 y = y * 256;
64 y += buf[0];
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080065
Alex Deymob870eb52015-10-14 21:39:04 -070066 if (buf[7] & 0x80)
67 y = -y;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080068
Alex Deymob870eb52015-10-14 21:39:04 -070069 return y;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080070}
71
Alex Deymob870eb52015-10-14 21:39:04 -070072// TODO(deymo): Re-enable exfile.h once we can build it for the
73// target and mac.
Alex Deymo870b8022015-09-28 18:15:09 -070074#if 0
Alex Deymob870eb52015-10-14 21:39:04 -070075// Parses an extent string ex_str, returning a pointer to a newly allocated
76// array of extents. The number of extents is stored in ex_count_p (if
77// provided).
78static ex_t *parse_extent_str(const char *ex_str, size_t *ex_count_p) {
79 size_t ex_count = (size_t)-1;
80 ex_t* ex_arr = extents_parse(ex_str, NULL, &ex_count);
81 if (!ex_arr)
82 errx(1, (ex_count == (size_t)-1 ? "error parsing extents"
83 : "error allocating extent array"));
84 if (ex_count_p)
85 *ex_count_p = ex_count;
86 return ex_arr;
Gilad Arnold99b53742013-04-30 09:24:14 -070087}
Alex Deymo870b8022015-09-28 18:15:09 -070088#endif
Gilad Arnold99b53742013-04-30 09:24:14 -070089
Alex Deymo20891f92015-10-12 17:28:04 -070090} // namespace
91
92namespace bsdiff {
93
Alex Deymoa5cff222015-04-08 14:10:30 -070094int bspatch(
95 const char* old_filename, const char* new_filename,
96 const char* patch_filename,
97 const char* old_extents, const char* new_extents) {
Alex Deymob870eb52015-10-14 21:39:04 -070098 FILE* f, *cpf, *dpf, *epf;
99 BZFILE* cpfbz2, *dpfbz2, *epfbz2;
100 int cbz2err, dbz2err, ebz2err;
101 FILE* old_file = NULL, * new_file = NULL;
102 ssize_t oldsize, newsize;
103 ssize_t bzctrllen, bzdatalen;
104 u_char header[32], buf[8];
105 u_char* new_buf;
106 off_t oldpos, newpos;
107 off_t ctrl[3];
108 off_t lenread;
109 off_t i, j;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800110
Alex Deymob870eb52015-10-14 21:39:04 -0700111 int using_extents = (old_extents != NULL || new_extents != NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800112
Alex Deymob870eb52015-10-14 21:39:04 -0700113 // Open patch file.
114 if ((f = fopen(patch_filename, "r")) == NULL)
115 err(1, "fopen(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800116
Alex Deymob870eb52015-10-14 21:39:04 -0700117 // File format:
118 // 0 8 "BSDIFF40"
119 // 8 8 X
120 // 16 8 Y
121 // 24 8 sizeof(new_filename)
122 // 32 X bzip2(control block)
123 // 32+X Y bzip2(diff block)
124 // 32+X+Y ??? bzip2(extra block)
125 // with control block a set of triples (x,y,z) meaning "add x bytes
126 // from oldfile to x bytes from the diff block; copy y bytes from the
127 // extra block; seek forwards in oldfile by z bytes".
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800128
Alex Deymob870eb52015-10-14 21:39:04 -0700129 // Read header.
130 if (fread(header, 1, 32, f) < 32) {
131 if (feof(f))
132 errx(1, "Corrupt patch\n");
133 err(1, "fread(%s)", patch_filename);
134 }
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800135
Alex Deymob870eb52015-10-14 21:39:04 -0700136 // Check for appropriate magic.
137 if (memcmp(header, "BSDIFF40", 8) != 0)
138 errx(1, "Corrupt patch\n");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800139
Alex Deymob870eb52015-10-14 21:39:04 -0700140 // Read lengths from header.
141 bzctrllen = offtin(header + 8);
142 bzdatalen = offtin(header + 16);
143 newsize = offtin(header + 24);
144 if ((bzctrllen < 0) || (bzdatalen < 0) || (newsize < 0))
145 errx(1, "Corrupt patch\n");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800146
Alex Deymob870eb52015-10-14 21:39:04 -0700147 // Close patch file and re-open it via libbzip2 at the right places.
148 if (fclose(f))
149 err(1, "fclose(%s)", patch_filename);
150 if ((cpf = fopen(patch_filename, "r")) == NULL)
151 err(1, "fopen(%s)", patch_filename);
152 if (fseek(cpf, 32, SEEK_SET))
153 err(1, "fseeko(%s, %lld)", patch_filename, (long long)32);
154 if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
155 errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
156 if ((dpf = fopen(patch_filename, "r")) == NULL)
157 err(1, "fopen(%s)", patch_filename);
158 if (fseek(dpf, 32 + bzctrllen, SEEK_SET))
159 err(1, "fseeko(%s, %lld)", patch_filename, (long long)(32 + bzctrllen));
160 if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
161 errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
162 if ((epf = fopen(patch_filename, "r")) == NULL)
163 err(1, "fopen(%s)", patch_filename);
164 if (fseek(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
165 err(1, "fseeko(%s, %lld)", patch_filename,
166 (long long)(32 + bzctrllen + bzdatalen));
167 if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
168 errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800169
Alex Deymob870eb52015-10-14 21:39:04 -0700170 // Open input file for reading.
171 if (using_extents) {
172 // TODO(deymo): Re-enable exfile.h once we can build it for the
173 // target and mac.
174 errx(1, "Extent support is disabled.\n");
Alex Deymo870b8022015-09-28 18:15:09 -0700175#if 0
Alex Deymob870eb52015-10-14 21:39:04 -0700176 size_t ex_count = 0;
177 ex_t *ex_arr = parse_extent_str(old_extents, &ex_count);
178 old_file = exfile_fopen(old_filename, "r", ex_arr, ex_count,
179 free);
Alex Deymo870b8022015-09-28 18:15:09 -0700180#endif
Alex Deymob870eb52015-10-14 21:39:04 -0700181 } else { old_file = fopen(old_filename, "r"); }
182 if (!old_file || fseek(old_file, 0, SEEK_END) != 0 ||
183 (oldsize = ftell(old_file)) < 0 || fseek(old_file, 0, SEEK_SET) != 0)
184 err(1, "cannot obtain the size of %s", old_filename);
185 off_t old_file_pos = 0;
Gilad Arnold99b53742013-04-30 09:24:14 -0700186
Alex Deymob870eb52015-10-14 21:39:04 -0700187 if ((new_buf = static_cast<u_char*>(malloc(newsize + 1))) == NULL)
188 err(1, NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800189
Alex Deymob870eb52015-10-14 21:39:04 -0700190 oldpos = 0;
191 newpos = 0;
192 while (newpos < newsize) {
193 // Read control data.
194 for (i = 0; i <= 2; i++) {
195 lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
196 if ((lenread < 8) || ((cbz2err != BZ_OK) && (cbz2err != BZ_STREAM_END)))
197 errx(1, "Corrupt patch\n");
198 ctrl[i] = offtin(buf);
199 };
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800200
Alex Deymob870eb52015-10-14 21:39:04 -0700201 // Sanity-check.
202 if (ctrl[0] < 0 || ctrl[1] < 0)
203 errx(1, "Corrupt patch\n");
Doug Zongker4d054792014-05-13 08:37:06 -0700204
Alex Deymob870eb52015-10-14 21:39:04 -0700205 // Sanity-check.
206 if (newpos + ctrl[0] > newsize)
207 errx(1, "Corrupt patch\n");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800208
Alex Deymob870eb52015-10-14 21:39:04 -0700209 // Read diff string.
210 lenread = BZ2_bzRead(&dbz2err, dpfbz2, new_buf + newpos, ctrl[0]);
211 if ((lenread < ctrl[0]) ||
212 ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
213 errx(1, "Corrupt patch\n");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800214
Alex Deymob870eb52015-10-14 21:39:04 -0700215 // Add old data to diff string. It is enough to fseek once, at
216 // the beginning of the sequence, to avoid unnecessary overhead.
217 j = newpos;
218 if ((i = oldpos) < 0) {
219 j -= i;
220 i = 0;
221 }
222 if (i != old_file_pos && fseek(old_file, i, SEEK_SET) < 0)
223 err(1, "error seeking input file to offset %" PRIdMAX, (intmax_t)i);
224 if ((old_file_pos = oldpos + ctrl[0]) > oldsize)
225 old_file_pos = oldsize;
226 while (i++ < old_file_pos) {
227 u_char c;
228 if (fread(&c, 1, 1, old_file) != 1)
229 err(1, "error reading from input file");
230 new_buf[j++] += c;
231 }
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800232
Alex Deymob870eb52015-10-14 21:39:04 -0700233 // Adjust pointers.
234 newpos += ctrl[0];
235 oldpos += ctrl[0];
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800236
Alex Deymob870eb52015-10-14 21:39:04 -0700237 // Sanity-check.
238 if (newpos + ctrl[1] > newsize)
239 errx(1, "Corrupt patch\n");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800240
Alex Deymob870eb52015-10-14 21:39:04 -0700241 // Read extra string.
242 lenread = BZ2_bzRead(&ebz2err, epfbz2, new_buf + newpos, ctrl[1]);
243 if ((lenread < ctrl[1]) ||
244 ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
245 errx(1, "Corrupt patch\n");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800246
Alex Deymob870eb52015-10-14 21:39:04 -0700247 // Adjust pointers.
248 newpos += ctrl[1];
249 oldpos += ctrl[2];
250 };
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800251
Alex Deymob870eb52015-10-14 21:39:04 -0700252 // Close input file.
253 fclose(old_file);
Gilad Arnold99b53742013-04-30 09:24:14 -0700254
Alex Deymob870eb52015-10-14 21:39:04 -0700255 // Clean up the bzip2 reads.
256 BZ2_bzReadClose(&cbz2err, cpfbz2);
257 BZ2_bzReadClose(&dbz2err, dpfbz2);
258 BZ2_bzReadClose(&ebz2err, epfbz2);
259 if (fclose(cpf) || fclose(dpf) || fclose(epf))
260 err(1, "fclose(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800261
Alex Deymob870eb52015-10-14 21:39:04 -0700262 // Write the new file.
263 if (using_extents) {
264// TODO(deymo): Re-enable exfile.h once we can build it for the
265// target and mac.
Alex Deymo870b8022015-09-28 18:15:09 -0700266#if 0
Alex Deymob870eb52015-10-14 21:39:04 -0700267 size_t ex_count = 0;
268 ex_t *ex_arr = parse_extent_str(new_extents, &ex_count);
269 new_file = exfile_fopen(new_filename, "w", ex_arr, ex_count,
270 free);
Alex Deymo870b8022015-09-28 18:15:09 -0700271#endif
Alex Deymob870eb52015-10-14 21:39:04 -0700272 } else { new_file = fopen(new_filename, "w"); }
273 if (!new_file || fwrite(new_buf, 1, newsize, new_file) != (size_t)newsize ||
274 fclose(new_file) == EOF)
275 err(1, "%s", new_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800276
Alex Deymob870eb52015-10-14 21:39:04 -0700277 free(new_buf);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800278
Alex Deymob870eb52015-10-14 21:39:04 -0700279 return 0;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800280}
Alex Deymo20891f92015-10-12 17:28:04 -0700281
282} // namespace bsdiff