1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // This file is a nearly line-for-line copy of bspatch.c from the
18 // bsdiff-4.3 distribution; the primary differences being how the
19 // input and output data are read and the error handling. Running
20 // applypatch with the -l option will display the bsdiff license
21 // notice.
22
23 #include <stdio.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 #include <bzlib.h>
30
31 #include "mincrypt/sha.h"
32 #include "applypatch.h"
33
ShowBSDiffLicense()34 void ShowBSDiffLicense() {
35 puts("The bsdiff library used herein is:\n"
36 "\n"
37 "Copyright 2003-2005 Colin Percival\n"
38 "All rights reserved\n"
39 "\n"
40 "Redistribution and use in source and binary forms, with or without\n"
41 "modification, are permitted providing that the following conditions\n"
42 "are met:\n"
43 "1. Redistributions of source code must retain the above copyright\n"
44 " notice, this list of conditions and the following disclaimer.\n"
45 "2. Redistributions in binary form must reproduce the above copyright\n"
46 " notice, this list of conditions and the following disclaimer in the\n"
47 " documentation and/or other materials provided with the distribution.\n"
48 "\n"
49 "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
50 "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
51 "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
52 "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\n"
53 "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
54 "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n"
55 "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
56 "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"
57 "STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"
58 "IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n"
59 "POSSIBILITY OF SUCH DAMAGE.\n"
60 "\n------------------\n\n"
61 "This program uses Julian R Seward's \"libbzip2\" library, available\n"
62 "from http://www.bzip.org/.\n"
63 );
64 }
65
offtin(u_char * buf)66 static off_t offtin(u_char *buf)
67 {
68 off_t y;
69
70 y=buf[7]&0x7F;
71 y=y*256;y+=buf[6];
72 y=y*256;y+=buf[5];
73 y=y*256;y+=buf[4];
74 y=y*256;y+=buf[3];
75 y=y*256;y+=buf[2];
76 y=y*256;y+=buf[1];
77 y=y*256;y+=buf[0];
78
79 if(buf[7]&0x80) y=-y;
80
81 return y;
82 }
83
84
ApplyBSDiffPatch(const unsigned char * old_data,ssize_t old_size,const char * patch_filename,ssize_t patch_offset,SinkFn sink,void * token,SHA_CTX * ctx)85 int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
86 const char* patch_filename, ssize_t patch_offset,
87 SinkFn sink, void* token, SHA_CTX* ctx) {
88
89 unsigned char* new_data;
90 ssize_t new_size;
91 if (ApplyBSDiffPatchMem(old_data, old_size, patch_filename, patch_offset,
92 &new_data, &new_size) != 0) {
93 return -1;
94 }
95
96 if (sink(new_data, new_size, token) < new_size) {
97 fprintf(stderr, "short write of output: %d (%s)\n", errno, strerror(errno));
98 return 1;
99 }
100 if (ctx) {
101 SHA_update(ctx, new_data, new_size);
102 }
103 free(new_data);
104
105 return 0;
106 }
107
ApplyBSDiffPatchMem(const unsigned char * old_data,ssize_t old_size,const char * patch_filename,ssize_t patch_offset,unsigned char ** new_data,ssize_t * new_size)108 int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
109 const char* patch_filename, ssize_t patch_offset,
110 unsigned char** new_data, ssize_t* new_size) {
111
112 FILE* f;
113 if ((f = fopen(patch_filename, "rb")) == NULL) {
114 fprintf(stderr, "failed to open patch file\n");
115 return 1;
116 }
117
118 // File format:
119 // 0 8 "BSDIFF40"
120 // 8 8 X
121 // 16 8 Y
122 // 24 8 sizeof(newfile)
123 // 32 X bzip2(control block)
124 // 32+X Y bzip2(diff block)
125 // 32+X+Y ??? bzip2(extra block)
126 // with control block a set of triples (x,y,z) meaning "add x bytes
127 // from oldfile to x bytes from the diff block; copy y bytes from the
128 // extra block; seek forwards in oldfile by z bytes".
129
130 fseek(f, patch_offset, SEEK_SET);
131
132 unsigned char header[32];
133 if (fread(header, 1, 32, f) < 32) {
134 fprintf(stderr, "failed to read patch file header\n");
135 return 1;
136 }
137
138 if (memcmp(header, "BSDIFF40", 8) != 0) {
139 fprintf(stderr, "corrupt bsdiff patch file header (magic number)\n");
140 return 1;
141 }
142
143 ssize_t ctrl_len, data_len;
144 ctrl_len = offtin(header+8);
145 data_len = offtin(header+16);
146 *new_size = offtin(header+24);
147
148 if (ctrl_len < 0 || data_len < 0 || *new_size < 0) {
149 fprintf(stderr, "corrupt patch file header (data lengths)\n");
150 return 1;
151 }
152
153 fclose(f);
154
155 int bzerr;
156
157 #define OPEN_AT(f, bzf, offset) \
158 FILE* f; \
159 BZFILE* bzf; \
160 if ((f = fopen(patch_filename, "rb")) == NULL) { \
161 fprintf(stderr, "failed to open patch file\n"); \
162 return 1; \
163 } \
164 if (fseeko(f, offset+patch_offset, SEEK_SET)) { \
165 fprintf(stderr, "failed to seek in patch file\n"); \
166 return 1; \
167 } \
168 if ((bzf = BZ2_bzReadOpen(&bzerr, f, 0, 0, NULL, 0)) == NULL) { \
169 fprintf(stderr, "failed to bzReadOpen in patch file (%d)\n", bzerr); \
170 return 1; \
171 }
172
173 OPEN_AT(cpf, cpfbz2, 32);
174 OPEN_AT(dpf, dpfbz2, 32+ctrl_len);
175 OPEN_AT(epf, epfbz2, 32+ctrl_len+data_len);
176
177 #undef OPEN_AT
178
179 *new_data = malloc(*new_size);
180 if (*new_data == NULL) {
181 fprintf(stderr, "failed to allocate %d bytes of memory for output file\n",
182 (int)*new_size);
183 return 1;
184 }
185
186 off_t oldpos = 0, newpos = 0;
187 off_t ctrl[3];
188 off_t len_read;
189 int i;
190 unsigned char buf[8];
191 while (newpos < *new_size) {
192 // Read control data
193 for (i = 0; i < 3; ++i) {
194 len_read = BZ2_bzRead(&bzerr, cpfbz2, buf, 8);
195 if (len_read < 8 || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
196 fprintf(stderr, "corrupt patch (read control)\n");
197 return 1;
198 }
199 ctrl[i] = offtin(buf);
200 }
201
202 // Sanity check
203 if (newpos + ctrl[0] > *new_size) {
204 fprintf(stderr, "corrupt patch (new file overrun)\n");
205 return 1;
206 }
207
208 // Read diff string
209 len_read = BZ2_bzRead(&bzerr, dpfbz2, *new_data + newpos, ctrl[0]);
210 if (len_read < ctrl[0] || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
211 fprintf(stderr, "corrupt patch (read diff)\n");
212 return 1;
213 }
214
215 // Add old data to diff string
216 for (i = 0; i < ctrl[0]; ++i) {
217 if ((oldpos+i >= 0) && (oldpos+i < old_size)) {
218 (*new_data)[newpos+i] += old_data[oldpos+i];
219 }
220 }
221
222 // Adjust pointers
223 newpos += ctrl[0];
224 oldpos += ctrl[0];
225
226 // Sanity check
227 if (newpos + ctrl[1] > *new_size) {
228 fprintf(stderr, "corrupt patch (new file overrun)\n");
229 return 1;
230 }
231
232 // Read extra string
233 len_read = BZ2_bzRead(&bzerr, epfbz2, *new_data + newpos, ctrl[1]);
234 if (len_read < ctrl[1] || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
235 fprintf(stderr, "corrupt patch (read extra)\n");
236 return 1;
237 }
238
239 // Adjust pointers
240 newpos += ctrl[1];
241 oldpos += ctrl[2];
242 }
243
244 BZ2_bzReadClose(&bzerr, cpfbz2);
245 BZ2_bzReadClose(&bzerr, dpfbz2);
246 BZ2_bzReadClose(&bzerr, epfbz2);
247 fclose(cpf);
248 fclose(dpf);
249 fclose(epf);
250
251 return 0;
252 }
253