blob: 884c642f41a1fbc176c967b289c5e21a0f236bad [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 "exfile.h"
42#include "extents.h"
Zdenek Behan339e0ee2010-06-14 12:50:53 -070043
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
Gilad Arnold99b53742013-04-30 09:24:14 -070063/* Parses an extent string ex_str, returning a pointer to a newly allocated
64 * array of extents. The number of extents is stored in ex_count_p (if
65 * provided). */
66static ex_t *parse_extent_str(const char *ex_str, size_t *ex_count_p)
67{
68 size_t ex_count = (size_t)-1;
69 ex_t *ex_arr = extents_parse(ex_str, NULL, &ex_count);
70 if (!ex_arr)
71 errx(1, (ex_count == (size_t)-1 ?
72 "error parsing extents" :
73 "error allocating extent array"));
74 if (ex_count_p)
75 *ex_count_p = ex_count;
76 return ex_arr;
77}
78
Alex Deymoa5cff222015-04-08 14:10:30 -070079int bspatch(
80 const char* old_filename, const char* new_filename,
81 const char* patch_filename,
82 const char* old_extents, const char* new_extents) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080083 FILE * f, * cpf, * dpf, * epf;
84 BZFILE * cpfbz2, * dpfbz2, * epfbz2;
85 int cbz2err, dbz2err, ebz2err;
Gilad Arnold99b53742013-04-30 09:24:14 -070086 FILE *old_file = NULL, *new_file = NULL;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080087 ssize_t oldsize,newsize;
88 ssize_t bzctrllen,bzdatalen;
89 u_char header[32],buf[8];
Gilad Arnold99b53742013-04-30 09:24:14 -070090 u_char *new;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080091 off_t oldpos,newpos;
92 off_t ctrl[3];
93 off_t lenread;
Gilad Arnold99b53742013-04-30 09:24:14 -070094 off_t i, j;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080095
Alex Deymoa5cff222015-04-08 14:10:30 -070096 int using_extents = (old_extents != NULL || new_extents != NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080097
98 /* Open patch file */
Alex Deymoa5cff222015-04-08 14:10:30 -070099 if ((f = fopen(patch_filename, "r")) == NULL)
100 err(1, "fopen(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800101
102 /*
103 File format:
104 0 8 "BSDIFF40"
105 8 8 X
106 16 8 Y
Alex Deymoa5cff222015-04-08 14:10:30 -0700107 24 8 sizeof(new_filename)
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800108 32 X bzip2(control block)
109 32+X Y bzip2(diff block)
110 32+X+Y ??? bzip2(extra block)
111 with control block a set of triples (x,y,z) meaning "add x bytes
112 from oldfile to x bytes from the diff block; copy y bytes from the
113 extra block; seek forwards in oldfile by z bytes".
114 */
115
116 /* Read header */
117 if (fread(header, 1, 32, f) < 32) {
118 if (feof(f))
119 errx(1, "Corrupt patch\n");
Alex Deymoa5cff222015-04-08 14:10:30 -0700120 err(1, "fread(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800121 }
122
123 /* Check for appropriate magic */
124 if (memcmp(header, "BSDIFF40", 8) != 0)
125 errx(1, "Corrupt patch\n");
126
127 /* Read lengths from header */
128 bzctrllen=offtin(header+8);
129 bzdatalen=offtin(header+16);
130 newsize=offtin(header+24);
131 if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
132 errx(1,"Corrupt patch\n");
133
134 /* Close patch file and re-open it via libbzip2 at the right places */
135 if (fclose(f))
Alex Deymoa5cff222015-04-08 14:10:30 -0700136 err(1, "fclose(%s)", patch_filename);
137 if ((cpf = fopen(patch_filename, "r")) == NULL)
138 err(1, "fopen(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800139 if (fseeko(cpf, 32, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700140 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800141 (long long)32);
142 if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
143 errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
Alex Deymoa5cff222015-04-08 14:10:30 -0700144 if ((dpf = fopen(patch_filename, "r")) == NULL)
145 err(1, "fopen(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800146 if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700147 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800148 (long long)(32 + bzctrllen));
149 if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
150 errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
Alex Deymoa5cff222015-04-08 14:10:30 -0700151 if ((epf = fopen(patch_filename, "r")) == NULL)
152 err(1, "fopen(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800153 if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
Alex Deymoa5cff222015-04-08 14:10:30 -0700154 err(1, "fseeko(%s, %lld)", patch_filename,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800155 (long long)(32 + bzctrllen + bzdatalen));
156 if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
157 errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
158
Gilad Arnold99b53742013-04-30 09:24:14 -0700159 /* Open input file for reading. */
160 if (using_extents) {
161 size_t ex_count = 0;
Alex Deymoa5cff222015-04-08 14:10:30 -0700162 ex_t *ex_arr = parse_extent_str(old_extents, &ex_count);
163 old_file = exfile_fopen(new_filename, "r", ex_arr, ex_count,
164 free);
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700165 } else {
Alex Deymoa5cff222015-04-08 14:10:30 -0700166 old_file = fopen(new_filename, "r");
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700167 }
Gilad Arnold99b53742013-04-30 09:24:14 -0700168 if (!old_file ||
169 fseek(old_file, 0, SEEK_END) != 0 ||
170 (oldsize = ftell(old_file)) < 0 ||
171 fseek(old_file, 0, SEEK_SET) != 0)
Alex Deymoa5cff222015-04-08 14:10:30 -0700172 err(1, "cannot obtain the size of %s", new_filename);
Gilad Arnold99b53742013-04-30 09:24:14 -0700173 off_t old_file_pos = 0;
174
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800175 if((new=malloc(newsize+1))==NULL) err(1,NULL);
176
177 oldpos=0;newpos=0;
178 while(newpos<newsize) {
179 /* Read control data */
180 for(i=0;i<=2;i++) {
181 lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
182 if ((lenread < 8) || ((cbz2err != BZ_OK) &&
183 (cbz2err != BZ_STREAM_END)))
184 errx(1, "Corrupt patch\n");
185 ctrl[i]=offtin(buf);
186 };
187
Doug Zongker4d054792014-05-13 08:37:06 -0700188 // android local change (start)
189 if (ctrl[0]<0||ctrl[1]<0)
190 errx(1,"Corrupt patch\n");
191 // android local change (end)
192
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800193 /* Sanity-check */
194 if(newpos+ctrl[0]>newsize)
195 errx(1,"Corrupt patch\n");
196
197 /* Read diff string */
198 lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
199 if ((lenread < ctrl[0]) ||
200 ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
201 errx(1, "Corrupt patch\n");
202
Gilad Arnold99b53742013-04-30 09:24:14 -0700203 /* Add old data to diff string. It is enough to fseek once, at
204 * the beginning of the sequence, to avoid unnecessary
205 * overhead. */
206 j = newpos;
207 if ((i = oldpos) < 0) {
208 j -= i;
209 i = 0;
210 }
211 if (i != old_file_pos && fseek(old_file, i, SEEK_SET) < 0)
212 err(1, "error seeking input file to offset %" PRIdMAX,
213 (intmax_t)i);
214 if ((old_file_pos = oldpos + ctrl[0]) > oldsize)
215 old_file_pos = oldsize;
216 while (i++ < old_file_pos) {
217 u_char c;
218 if (fread_unlocked(&c, 1, 1, old_file) != 1)
219 err(1, "error reading from input file");
220 new[j++] += c;
221 }
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800222
223 /* Adjust pointers */
224 newpos+=ctrl[0];
225 oldpos+=ctrl[0];
226
227 /* Sanity-check */
228 if(newpos+ctrl[1]>newsize)
229 errx(1,"Corrupt patch\n");
230
231 /* Read extra string */
232 lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
233 if ((lenread < ctrl[1]) ||
234 ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
235 errx(1, "Corrupt patch\n");
236
237 /* Adjust pointers */
238 newpos+=ctrl[1];
239 oldpos+=ctrl[2];
240 };
241
Gilad Arnold99b53742013-04-30 09:24:14 -0700242 /* Close input file. */
243 fclose(old_file);
244
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800245 /* Clean up the bzip2 reads */
246 BZ2_bzReadClose(&cbz2err, cpfbz2);
247 BZ2_bzReadClose(&dbz2err, dpfbz2);
248 BZ2_bzReadClose(&ebz2err, epfbz2);
249 if (fclose(cpf) || fclose(dpf) || fclose(epf))
Alex Deymoa5cff222015-04-08 14:10:30 -0700250 err(1, "fclose(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800251
252 /* Write the new file */
Gilad Arnold99b53742013-04-30 09:24:14 -0700253 if (using_extents) {
254 size_t ex_count = 0;
Alex Deymoa5cff222015-04-08 14:10:30 -0700255 ex_t *ex_arr = parse_extent_str(new_extents, &ex_count);
256 new_file = exfile_fopen(new_filename, "w", ex_arr, ex_count,
257 free);
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700258 } else {
Alex Deymoa5cff222015-04-08 14:10:30 -0700259 new_file = fopen(new_filename, "w");
Zdenek Behan339e0ee2010-06-14 12:50:53 -0700260 }
Gilad Arnold99b53742013-04-30 09:24:14 -0700261 if (!new_file ||
262 fwrite_unlocked(new, 1, newsize, new_file) != newsize ||
263 fclose(new_file) == EOF)
Alex Deymoa5cff222015-04-08 14:10:30 -0700264 err(1,"%s",new_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800265
266 free(new);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800267
268 return 0;
269}