blob: 6a73887f258bf3a3250544026eb88e558a39c5ab [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
31#include <bzlib.h>
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080032#include <err.h>
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080033#include <fcntl.h>
Gilad Arnold99b53742013-04-30 09:24:14 -070034#include <inttypes.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080039#include <sys/types.h> // android
40
Gilad Arnold99b53742013-04-30 09:24:14 -070041#include "extents.h"
Zdenek Behan339e0ee2010-06-14 12:50:53 -070042
Alex Deymo20891f92015-10-12 17:28:04 -070043namespace {
Zdenek Behan339e0ee2010-06-14 12:50:53 -070044
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080045static off_t offtin(u_char *buf)
46{
47 off_t y;
48
49 y=buf[7]&0x7F;
50 y=y*256;y+=buf[6];
51 y=y*256;y+=buf[5];
52 y=y*256;y+=buf[4];
53 y=y*256;y+=buf[3];
54 y=y*256;y+=buf[2];
55 y=y*256;y+=buf[1];
56 y=y*256;y+=buf[0];
57
58 if(buf[7]&0x80) y=-y;
59
60 return y;
61}
62
Alex Deymo870b8022015-09-28 18:15:09 -070063/* TODO(deymo): Re-enable exfile.h once we can build it for the
64target and mac. */
65#if 0
Gilad Arnold99b53742013-04-30 09:24:14 -070066/* Parses an extent string ex_str, returning a pointer to a newly allocated
67 * array of extents. The number of extents is stored in ex_count_p (if
68 * provided). */
69static ex_t *parse_extent_str(const char *ex_str, size_t *ex_count_p)
70{
71 size_t ex_count = (size_t)-1;
72 ex_t *ex_arr = extents_parse(ex_str, NULL, &ex_count);
73 if (!ex_arr)
74 errx(1, (ex_count == (size_t)-1 ?
75 "error parsing extents" :
76 "error allocating extent array"));
77 if (ex_count_p)
78 *ex_count_p = ex_count;
79 return ex_arr;
80}
Alex Deymo870b8022015-09-28 18:15:09 -070081#endif
Gilad Arnold99b53742013-04-30 09:24:14 -070082
Alex Deymo20891f92015-10-12 17:28:04 -070083} // namespace
84
85namespace bsdiff {
86
Alex Deymoa5cff222015-04-08 14:10:30 -070087int bspatch(
88 const char* old_filename, const char* new_filename,
89 const char* patch_filename,
90 const char* old_extents, const char* new_extents) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080091 FILE * f, * cpf, * dpf, * epf;
92 BZFILE * cpfbz2, * dpfbz2, * epfbz2;
93 int cbz2err, dbz2err, ebz2err;
Gilad Arnold99b53742013-04-30 09:24:14 -070094 FILE *old_file = NULL, *new_file = NULL;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080095 ssize_t oldsize,newsize;
96 ssize_t bzctrllen,bzdatalen;
97 u_char header[32],buf[8];
Alex Deymo20891f92015-10-12 17:28:04 -070098 u_char *new_buf;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080099 off_t oldpos,newpos;
100 off_t ctrl[3];
101 off_t lenread;
Gilad Arnold99b53742013-04-30 09:24:14 -0700102 off_t i, j;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800103
Alex Deymoa5cff222015-04-08 14:10:30 -0700104 int using_extents = (old_extents != NULL || new_extents != NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800105
106 /* Open patch file */
Alex Deymoa5cff222015-04-08 14:10:30 -0700107 if ((f = fopen(patch_filename, "r")) == NULL)
108 err(1, "fopen(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800109
110 /*
111 File format:
112 0 8 "BSDIFF40"
113 8 8 X
114 16 8 Y
Alex Deymoa5cff222015-04-08 14:10:30 -0700115 24 8 sizeof(new_filename)
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800116 32 X bzip2(control block)
117 32+X Y bzip2(diff block)
118 32+X+Y ??? bzip2(extra block)
119 with control block a set of triples (x,y,z) meaning "add x bytes
120 from oldfile to x bytes from the diff block; copy y bytes from the
121 extra block; seek forwards in oldfile by z bytes".
122 */
123
124 /* Read header */
125 if (fread(header, 1, 32, f) < 32) {
126 if (feof(f))
127 errx(1, "Corrupt patch\n");
Alex Deymoa5cff222015-04-08 14:10:30 -0700128 err(1, "fread(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800129 }
130
131 /* Check for appropriate magic */
132 if (memcmp(header, "BSDIFF40", 8) != 0)
133 errx(1, "Corrupt patch\n");
134
135 /* Read lengths from header */
136 bzctrllen=offtin(header+8);
137 bzdatalen=offtin(header+16);
138 newsize=offtin(header+24);
139 if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
140 errx(1,"Corrupt patch\n");
141
142 /* Close patch file and re-open it via libbzip2 at the right places */
143 if (fclose(f))
Alex Deymoa5cff222015-04-08 14:10:30 -0700144 err(1, "fclose(%s)", patch_filename);
145 if ((cpf = fopen(patch_filename, "r")) == NULL)
146 err(1, "fopen(%s)", patch_filename);
Alex Deymo870b8022015-09-28 18:15:09 -0700147 if (fseek(cpf, 32, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700148 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800149 (long long)32);
150 if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
151 errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
Alex Deymoa5cff222015-04-08 14:10:30 -0700152 if ((dpf = fopen(patch_filename, "r")) == NULL)
153 err(1, "fopen(%s)", patch_filename);
Alex Deymo870b8022015-09-28 18:15:09 -0700154 if (fseek(dpf, 32 + bzctrllen, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700155 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800156 (long long)(32 + bzctrllen));
157 if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
158 errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
Alex Deymoa5cff222015-04-08 14:10:30 -0700159 if ((epf = fopen(patch_filename, "r")) == NULL)
160 err(1, "fopen(%s)", patch_filename);
Alex Deymo870b8022015-09-28 18:15:09 -0700161 if (fseek(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700162 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800163 (long long)(32 + bzctrllen + bzdatalen));
164 if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
165 errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
166
Gilad Arnold99b53742013-04-30 09:24:14 -0700167 /* Open input file for reading. */
168 if (using_extents) {
Alex Deymo870b8022015-09-28 18:15:09 -0700169 /* TODO(deymo): Re-enable exfile.h once we can build it for the
170 target and mac. */
171 errx(1, "Extent support is disabled.\n");
172#if 0
Gilad Arnold99b53742013-04-30 09:24:14 -0700173 size_t ex_count = 0;
Alex Deymoa5cff222015-04-08 14:10:30 -0700174 ex_t *ex_arr = parse_extent_str(old_extents, &ex_count);
Alex Deymof3767962015-04-24 18:51:34 -0700175 old_file = exfile_fopen(old_filename, "r", ex_arr, ex_count,
Alex Deymoa5cff222015-04-08 14:10:30 -0700176 free);
Alex Deymo870b8022015-09-28 18:15:09 -0700177#endif
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700178 } else {
Alex Deymof3767962015-04-24 18:51:34 -0700179 old_file = fopen(old_filename, "r");
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700180 }
Gilad Arnold99b53742013-04-30 09:24:14 -0700181 if (!old_file ||
182 fseek(old_file, 0, SEEK_END) != 0 ||
183 (oldsize = ftell(old_file)) < 0 ||
184 fseek(old_file, 0, SEEK_SET) != 0)
Alex Deymof3767962015-04-24 18:51:34 -0700185 err(1, "cannot obtain the size of %s", old_filename);
Gilad Arnold99b53742013-04-30 09:24:14 -0700186 off_t old_file_pos = 0;
187
Alex Deymo20891f92015-10-12 17:28:04 -0700188 if((new_buf=static_cast<u_char*>(malloc(newsize+1)))==NULL) err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800189
190 oldpos=0;newpos=0;
191 while(newpos<newsize) {
192 /* Read control data */
193 for(i=0;i<=2;i++) {
194 lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
195 if ((lenread < 8) || ((cbz2err != BZ_OK) &&
196 (cbz2err != BZ_STREAM_END)))
197 errx(1, "Corrupt patch\n");
198 ctrl[i]=offtin(buf);
199 };
200
Doug Zongker4d054792014-05-13 08:37:06 -0700201 // android local change (start)
202 if (ctrl[0]<0||ctrl[1]<0)
203 errx(1,"Corrupt patch\n");
204 // android local change (end)
205
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800206 /* Sanity-check */
207 if(newpos+ctrl[0]>newsize)
208 errx(1,"Corrupt patch\n");
209
210 /* Read diff string */
Alex Deymo20891f92015-10-12 17:28:04 -0700211 lenread = BZ2_bzRead(&dbz2err, dpfbz2, new_buf + newpos, ctrl[0]);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800212 if ((lenread < ctrl[0]) ||
213 ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
214 errx(1, "Corrupt patch\n");
215
Gilad Arnold99b53742013-04-30 09:24:14 -0700216 /* Add old data to diff string. It is enough to fseek once, at
217 * the beginning of the sequence, to avoid unnecessary
218 * overhead. */
219 j = newpos;
220 if ((i = oldpos) < 0) {
221 j -= i;
222 i = 0;
223 }
224 if (i != old_file_pos && fseek(old_file, i, SEEK_SET) < 0)
225 err(1, "error seeking input file to offset %" PRIdMAX,
226 (intmax_t)i);
227 if ((old_file_pos = oldpos + ctrl[0]) > oldsize)
228 old_file_pos = oldsize;
229 while (i++ < old_file_pos) {
230 u_char c;
Alex Deymo870b8022015-09-28 18:15:09 -0700231 if (fread(&c, 1, 1, old_file) != 1)
Gilad Arnold99b53742013-04-30 09:24:14 -0700232 err(1, "error reading from input file");
Alex Deymo20891f92015-10-12 17:28:04 -0700233 new_buf[j++] += c;
Gilad Arnold99b53742013-04-30 09:24:14 -0700234 }
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800235
236 /* Adjust pointers */
237 newpos+=ctrl[0];
238 oldpos+=ctrl[0];
239
240 /* Sanity-check */
241 if(newpos+ctrl[1]>newsize)
242 errx(1,"Corrupt patch\n");
243
244 /* Read extra string */
Alex Deymo20891f92015-10-12 17:28:04 -0700245 lenread = BZ2_bzRead(&ebz2err, epfbz2, new_buf + newpos, ctrl[1]);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800246 if ((lenread < ctrl[1]) ||
247 ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
248 errx(1, "Corrupt patch\n");
249
250 /* Adjust pointers */
251 newpos+=ctrl[1];
252 oldpos+=ctrl[2];
253 };
254
Gilad Arnold99b53742013-04-30 09:24:14 -0700255 /* Close input file. */
256 fclose(old_file);
257
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800258 /* Clean up the bzip2 reads */
259 BZ2_bzReadClose(&cbz2err, cpfbz2);
260 BZ2_bzReadClose(&dbz2err, dpfbz2);
261 BZ2_bzReadClose(&ebz2err, epfbz2);
262 if (fclose(cpf) || fclose(dpf) || fclose(epf))
Alex Deymoa5cff222015-04-08 14:10:30 -0700263 err(1, "fclose(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800264
265 /* Write the new file */
Gilad Arnold99b53742013-04-30 09:24:14 -0700266 if (using_extents) {
Alex Deymo870b8022015-09-28 18:15:09 -0700267 /* TODO(deymo): Re-enable exfile.h once we can build it for the
268 target and mac. */
269#if 0
Gilad Arnold99b53742013-04-30 09:24:14 -0700270 size_t ex_count = 0;
Alex Deymoa5cff222015-04-08 14:10:30 -0700271 ex_t *ex_arr = parse_extent_str(new_extents, &ex_count);
272 new_file = exfile_fopen(new_filename, "w", ex_arr, ex_count,
273 free);
Alex Deymo870b8022015-09-28 18:15:09 -0700274#endif
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700275 } else {
Alex Deymoa5cff222015-04-08 14:10:30 -0700276 new_file = fopen(new_filename, "w");
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700277 }
Gilad Arnold99b53742013-04-30 09:24:14 -0700278 if (!new_file ||
Alex Deymo20891f92015-10-12 17:28:04 -0700279 fwrite(new_buf, 1, newsize, new_file) != (size_t)newsize ||
Gilad Arnold99b53742013-04-30 09:24:14 -0700280 fclose(new_file) == EOF)
Alex Deymoa5cff222015-04-08 14:10:30 -0700281 err(1,"%s",new_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800282
Alex Deymo20891f92015-10-12 17:28:04 -0700283 free(new_buf);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800284
285 return 0;
286}
Alex Deymo20891f92015-10-12 17:28:04 -0700287
288} // namespace bsdiff