blob: a2851f95293eec5ce5b4467fd1e9e47937cd343c [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
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
33void ShowBSDiffLicense() {
34 puts("The bsdiff library used herein is:\n"
35 "\n"
36 "Copyright 2003-2005 Colin Percival\n"
37 "All rights reserved\n"
38 "\n"
39 "Redistribution and use in source and binary forms, with or without\n"
40 "modification, are permitted providing that the following conditions\n"
41 "are met:\n"
42 "1. Redistributions of source code must retain the above copyright\n"
43 " notice, this list of conditions and the following disclaimer.\n"
44 "2. Redistributions in binary form must reproduce the above copyright\n"
45 " notice, this list of conditions and the following disclaimer in the\n"
46 " documentation and/or other materials provided with the distribution.\n"
47 "\n"
48 "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
49 "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
50 "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
51 "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\n"
52 "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
53 "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n"
54 "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
55 "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"
56 "STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"
57 "IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n"
58 "POSSIBILITY OF SUCH DAMAGE.\n"
59 "\n------------------\n\n"
60 "This program uses Julian R Seward's \"libbzip2\" library, available\n"
61 "from http://www.bzip.org/.\n"
62 );
63}
64
65static off_t offtin(u_char *buf)
66{
67 off_t y;
68
69 y=buf[7]&0x7F;
70 y=y*256;y+=buf[6];
71 y=y*256;y+=buf[5];
72 y=y*256;y+=buf[4];
73 y=y*256;y+=buf[3];
74 y=y*256;y+=buf[2];
75 y=y*256;y+=buf[1];
76 y=y*256;y+=buf[0];
77
78 if(buf[7]&0x80) y=-y;
79
80 return y;
81}
82
83int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
84 const char* patch_filename,
85 FILE* output, SHA_CTX* ctx) {
86
87 FILE* f;
88 if ((f = fopen(patch_filename, "rb")) == NULL) {
89 fprintf(stderr, "failed to open patch file\n");
90 return 1;
91 }
92
93 // File format:
94 // 0 8 "BSDIFF40"
95 // 8 8 X
96 // 16 8 Y
97 // 24 8 sizeof(newfile)
98 // 32 X bzip2(control block)
99 // 32+X Y bzip2(diff block)
100 // 32+X+Y ??? bzip2(extra block)
101 // with control block a set of triples (x,y,z) meaning "add x bytes
102 // from oldfile to x bytes from the diff block; copy y bytes from the
103 // extra block; seek forwards in oldfile by z bytes".
104
105 unsigned char header[32];
106 if (fread(header, 1, 32, f) < 32) {
107 fprintf(stderr, "failed to read patch file header\n");
108 return 1;
109 }
110
111 if (memcmp(header, "BSDIFF40", 8) != 0) {
112 fprintf(stderr, "corrupt patch file header (magic number)\n");
113 return 1;
114 }
115
116 ssize_t ctrl_len, data_len;
117 ssize_t new_size;
118 ctrl_len = offtin(header+8);
119 data_len = offtin(header+16);
120 new_size = offtin(header+24);
121
122 if (ctrl_len < 0 || data_len < 0 || new_size < 0) {
123 fprintf(stderr, "corrupt patch file header (data lengths)\n");
124 return 1;
125 }
126
127 fclose(f);
128
129 int bzerr;
130
131#define OPEN_AT(f, bzf, offset) \
132 FILE* f; \
133 BZFILE* bzf; \
134 if ((f = fopen(patch_filename, "rb")) == NULL) { \
135 fprintf(stderr, "failed to open patch file\n"); \
136 return 1; \
137 } \
138 if (fseeko(f, offset, SEEK_SET)) { \
139 fprintf(stderr, "failed to seek in patch file\n"); \
140 return 1; \
141 } \
142 if ((bzf = BZ2_bzReadOpen(&bzerr, f, 0, 0, NULL, 0)) == NULL) { \
143 fprintf(stderr, "failed to bzReadOpen in patch file (%d)\n", bzerr); \
144 return 1; \
145 }
146
147 OPEN_AT(cpf, cpfbz2, 32);
148 OPEN_AT(dpf, dpfbz2, 32+ctrl_len);
149 OPEN_AT(epf, epfbz2, 32+ctrl_len+data_len);
150
151#undef OPEN_AT
152
153 unsigned char* new_data = malloc(new_size);
154 if (new_data == NULL) {
155 fprintf(stderr, "failed to allocate memory for output file\n");
156 return 1;
157 }
158
159 off_t oldpos = 0, newpos = 0;
160 off_t ctrl[3];
161 off_t len_read;
162 int i;
163 unsigned char buf[8];
164 while (newpos < new_size) {
165 // Read control data
166 for (i = 0; i < 3; ++i) {
167 len_read = BZ2_bzRead(&bzerr, cpfbz2, buf, 8);
168 if (len_read < 8 || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
169 fprintf(stderr, "corrupt patch (read control)\n");
170 return 1;
171 }
172 ctrl[i] = offtin(buf);
173 }
174
175 // Sanity check
176 if (newpos + ctrl[0] > new_size) {
177 fprintf(stderr, "corrupt patch (new file overrun)\n");
178 return 1;
179 }
180
181 // Read diff string
182 len_read = BZ2_bzRead(&bzerr, dpfbz2, new_data + newpos, ctrl[0]);
183 if (len_read < ctrl[0] || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
184 fprintf(stderr, "corrupt patch (read diff)\n");
185 return 1;
186 }
187
188 // Add old data to diff string
189 for (i = 0; i < ctrl[0]; ++i) {
190 if ((oldpos+i >= 0) && (oldpos+i < old_size)) {
191 new_data[newpos+i] += old_data[oldpos+i];
192 }
193 }
194
195 // Adjust pointers
196 newpos += ctrl[0];
197 oldpos += ctrl[0];
198
199 // Sanity check
200 if (newpos + ctrl[1] > new_size) {
201 fprintf(stderr, "corrupt patch (new file overrun)\n");
202 return 1;
203 }
204
205 // Read extra string
206 len_read = BZ2_bzRead(&bzerr, epfbz2, new_data + newpos, ctrl[1]);
207 if (len_read < ctrl[1] || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
208 fprintf(stderr, "corrupt patch (read extra)\n");
209 return 1;
210 }
211
212 // Adjust pointers
213 newpos += ctrl[1];
214 oldpos += ctrl[2];
215 }
216
217 BZ2_bzReadClose(&bzerr, cpfbz2);
218 BZ2_bzReadClose(&bzerr, dpfbz2);
219 BZ2_bzReadClose(&bzerr, epfbz2);
220 fclose(cpf);
221 fclose(dpf);
222 fclose(epf);
223
224 if (fwrite(new_data, 1, new_size, output) < new_size) {
225 fprintf(stderr, "short write of output: %d (%s)\n", errno, strerror(errno));
226 return 1;
227 }
228 SHA_update(ctx, new_data, new_size);
229 free(new_data);
230
231 return 0;
232}