blob: 3bd3f0e56a148204be9447c422ca86e268ab4c97 [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
Zdenek Behan339e0ee2010-06-14 12:50:53 -070043
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080044static off_t offtin(u_char *buf)
45{
46 off_t y;
47
48 y=buf[7]&0x7F;
49 y=y*256;y+=buf[6];
50 y=y*256;y+=buf[5];
51 y=y*256;y+=buf[4];
52 y=y*256;y+=buf[3];
53 y=y*256;y+=buf[2];
54 y=y*256;y+=buf[1];
55 y=y*256;y+=buf[0];
56
57 if(buf[7]&0x80) y=-y;
58
59 return y;
60}
61
Alex Deymo870b8022015-09-28 18:15:09 -070062/* TODO(deymo): Re-enable exfile.h once we can build it for the
63target and mac. */
64#if 0
Gilad Arnold99b53742013-04-30 09:24:14 -070065/* Parses an extent string ex_str, returning a pointer to a newly allocated
66 * array of extents. The number of extents is stored in ex_count_p (if
67 * provided). */
68static ex_t *parse_extent_str(const char *ex_str, size_t *ex_count_p)
69{
70 size_t ex_count = (size_t)-1;
71 ex_t *ex_arr = extents_parse(ex_str, NULL, &ex_count);
72 if (!ex_arr)
73 errx(1, (ex_count == (size_t)-1 ?
74 "error parsing extents" :
75 "error allocating extent array"));
76 if (ex_count_p)
77 *ex_count_p = ex_count;
78 return ex_arr;
79}
Alex Deymo870b8022015-09-28 18:15:09 -070080#endif
Gilad Arnold99b53742013-04-30 09:24:14 -070081
Alex Deymoa5cff222015-04-08 14:10:30 -070082int bspatch(
83 const char* old_filename, const char* new_filename,
84 const char* patch_filename,
85 const char* old_extents, const char* new_extents) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080086 FILE * f, * cpf, * dpf, * epf;
87 BZFILE * cpfbz2, * dpfbz2, * epfbz2;
88 int cbz2err, dbz2err, ebz2err;
Gilad Arnold99b53742013-04-30 09:24:14 -070089 FILE *old_file = NULL, *new_file = NULL;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080090 ssize_t oldsize,newsize;
91 ssize_t bzctrllen,bzdatalen;
92 u_char header[32],buf[8];
Gilad Arnold99b53742013-04-30 09:24:14 -070093 u_char *new;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080094 off_t oldpos,newpos;
95 off_t ctrl[3];
96 off_t lenread;
Gilad Arnold99b53742013-04-30 09:24:14 -070097 off_t i, j;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080098
Alex Deymoa5cff222015-04-08 14:10:30 -070099 int using_extents = (old_extents != NULL || new_extents != NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800100
101 /* Open patch file */
Alex Deymoa5cff222015-04-08 14:10:30 -0700102 if ((f = fopen(patch_filename, "r")) == NULL)
103 err(1, "fopen(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800104
105 /*
106 File format:
107 0 8 "BSDIFF40"
108 8 8 X
109 16 8 Y
Alex Deymoa5cff222015-04-08 14:10:30 -0700110 24 8 sizeof(new_filename)
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800111 32 X bzip2(control block)
112 32+X Y bzip2(diff block)
113 32+X+Y ??? bzip2(extra block)
114 with control block a set of triples (x,y,z) meaning "add x bytes
115 from oldfile to x bytes from the diff block; copy y bytes from the
116 extra block; seek forwards in oldfile by z bytes".
117 */
118
119 /* Read header */
120 if (fread(header, 1, 32, f) < 32) {
121 if (feof(f))
122 errx(1, "Corrupt patch\n");
Alex Deymoa5cff222015-04-08 14:10:30 -0700123 err(1, "fread(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800124 }
125
126 /* Check for appropriate magic */
127 if (memcmp(header, "BSDIFF40", 8) != 0)
128 errx(1, "Corrupt patch\n");
129
130 /* Read lengths from header */
131 bzctrllen=offtin(header+8);
132 bzdatalen=offtin(header+16);
133 newsize=offtin(header+24);
134 if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
135 errx(1,"Corrupt patch\n");
136
137 /* Close patch file and re-open it via libbzip2 at the right places */
138 if (fclose(f))
Alex Deymoa5cff222015-04-08 14:10:30 -0700139 err(1, "fclose(%s)", patch_filename);
140 if ((cpf = fopen(patch_filename, "r")) == NULL)
141 err(1, "fopen(%s)", patch_filename);
Alex Deymo870b8022015-09-28 18:15:09 -0700142 if (fseek(cpf, 32, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700143 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800144 (long long)32);
145 if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
146 errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
Alex Deymoa5cff222015-04-08 14:10:30 -0700147 if ((dpf = fopen(patch_filename, "r")) == NULL)
148 err(1, "fopen(%s)", patch_filename);
Alex Deymo870b8022015-09-28 18:15:09 -0700149 if (fseek(dpf, 32 + bzctrllen, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700150 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800151 (long long)(32 + bzctrllen));
152 if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
153 errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
Alex Deymoa5cff222015-04-08 14:10:30 -0700154 if ((epf = fopen(patch_filename, "r")) == NULL)
155 err(1, "fopen(%s)", patch_filename);
Alex Deymo870b8022015-09-28 18:15:09 -0700156 if (fseek(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700157 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800158 (long long)(32 + bzctrllen + bzdatalen));
159 if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
160 errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
161
Gilad Arnold99b53742013-04-30 09:24:14 -0700162 /* Open input file for reading. */
163 if (using_extents) {
Alex Deymo870b8022015-09-28 18:15:09 -0700164 /* TODO(deymo): Re-enable exfile.h once we can build it for the
165 target and mac. */
166 errx(1, "Extent support is disabled.\n");
167#if 0
Gilad Arnold99b53742013-04-30 09:24:14 -0700168 size_t ex_count = 0;
Alex Deymoa5cff222015-04-08 14:10:30 -0700169 ex_t *ex_arr = parse_extent_str(old_extents, &ex_count);
Alex Deymof3767962015-04-24 18:51:34 -0700170 old_file = exfile_fopen(old_filename, "r", ex_arr, ex_count,
Alex Deymoa5cff222015-04-08 14:10:30 -0700171 free);
Alex Deymo870b8022015-09-28 18:15:09 -0700172#endif
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700173 } else {
Alex Deymof3767962015-04-24 18:51:34 -0700174 old_file = fopen(old_filename, "r");
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700175 }
Gilad Arnold99b53742013-04-30 09:24:14 -0700176 if (!old_file ||
177 fseek(old_file, 0, SEEK_END) != 0 ||
178 (oldsize = ftell(old_file)) < 0 ||
179 fseek(old_file, 0, SEEK_SET) != 0)
Alex Deymof3767962015-04-24 18:51:34 -0700180 err(1, "cannot obtain the size of %s", old_filename);
Gilad Arnold99b53742013-04-30 09:24:14 -0700181 off_t old_file_pos = 0;
182
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800183 if((new=malloc(newsize+1))==NULL) err(1,NULL);
184
185 oldpos=0;newpos=0;
186 while(newpos<newsize) {
187 /* Read control data */
188 for(i=0;i<=2;i++) {
189 lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
190 if ((lenread < 8) || ((cbz2err != BZ_OK) &&
191 (cbz2err != BZ_STREAM_END)))
192 errx(1, "Corrupt patch\n");
193 ctrl[i]=offtin(buf);
194 };
195
Doug Zongker4d054792014-05-13 08:37:06 -0700196 // android local change (start)
197 if (ctrl[0]<0||ctrl[1]<0)
198 errx(1,"Corrupt patch\n");
199 // android local change (end)
200
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800201 /* Sanity-check */
202 if(newpos+ctrl[0]>newsize)
203 errx(1,"Corrupt patch\n");
204
205 /* Read diff string */
206 lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
207 if ((lenread < ctrl[0]) ||
208 ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
209 errx(1, "Corrupt patch\n");
210
Gilad Arnold99b53742013-04-30 09:24:14 -0700211 /* Add old data to diff string. It is enough to fseek once, at
212 * the beginning of the sequence, to avoid unnecessary
213 * overhead. */
214 j = newpos;
215 if ((i = oldpos) < 0) {
216 j -= i;
217 i = 0;
218 }
219 if (i != old_file_pos && fseek(old_file, i, SEEK_SET) < 0)
220 err(1, "error seeking input file to offset %" PRIdMAX,
221 (intmax_t)i);
222 if ((old_file_pos = oldpos + ctrl[0]) > oldsize)
223 old_file_pos = oldsize;
224 while (i++ < old_file_pos) {
225 u_char c;
Alex Deymo870b8022015-09-28 18:15:09 -0700226 if (fread(&c, 1, 1, old_file) != 1)
Gilad Arnold99b53742013-04-30 09:24:14 -0700227 err(1, "error reading from input file");
228 new[j++] += c;
229 }
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800230
231 /* Adjust pointers */
232 newpos+=ctrl[0];
233 oldpos+=ctrl[0];
234
235 /* Sanity-check */
236 if(newpos+ctrl[1]>newsize)
237 errx(1,"Corrupt patch\n");
238
239 /* Read extra string */
240 lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
241 if ((lenread < ctrl[1]) ||
242 ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
243 errx(1, "Corrupt patch\n");
244
245 /* Adjust pointers */
246 newpos+=ctrl[1];
247 oldpos+=ctrl[2];
248 };
249
Gilad Arnold99b53742013-04-30 09:24:14 -0700250 /* Close input file. */
251 fclose(old_file);
252
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800253 /* Clean up the bzip2 reads */
254 BZ2_bzReadClose(&cbz2err, cpfbz2);
255 BZ2_bzReadClose(&dbz2err, dpfbz2);
256 BZ2_bzReadClose(&ebz2err, epfbz2);
257 if (fclose(cpf) || fclose(dpf) || fclose(epf))
Alex Deymoa5cff222015-04-08 14:10:30 -0700258 err(1, "fclose(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800259
260 /* Write the new file */
Gilad Arnold99b53742013-04-30 09:24:14 -0700261 if (using_extents) {
Alex Deymo870b8022015-09-28 18:15:09 -0700262 /* TODO(deymo): Re-enable exfile.h once we can build it for the
263 target and mac. */
264#if 0
Gilad Arnold99b53742013-04-30 09:24:14 -0700265 size_t ex_count = 0;
Alex Deymoa5cff222015-04-08 14:10:30 -0700266 ex_t *ex_arr = parse_extent_str(new_extents, &ex_count);
267 new_file = exfile_fopen(new_filename, "w", ex_arr, ex_count,
268 free);
Alex Deymo870b8022015-09-28 18:15:09 -0700269#endif
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700270 } else {
Alex Deymoa5cff222015-04-08 14:10:30 -0700271 new_file = fopen(new_filename, "w");
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700272 }
Gilad Arnold99b53742013-04-30 09:24:14 -0700273 if (!new_file ||
Alex Deymo870b8022015-09-28 18:15:09 -0700274 fwrite(new, 1, newsize, new_file) != (size_t)newsize ||
Gilad Arnold99b53742013-04-30 09:24:14 -0700275 fclose(new_file) == EOF)
Alex Deymoa5cff222015-04-08 14:10:30 -0700276 err(1,"%s",new_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800277
278 free(new);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800279
280 return 0;
281}