blob: 53228174fbcd208b90c038d3527c0e100bc30ebf [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2009 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// See imgdiff.c in this directory for a description of the patch file
18// format.
19
20#include <stdio.h>
21#include <sys/stat.h>
22#include <errno.h>
23#include <unistd.h>
24#include <string.h>
25
26#include "zlib.h"
27#include "mincrypt/sha.h"
28#include "applypatch.h"
29#include "imgdiff.h"
30#include "utils.h"
31
32/*
33 * Apply the patch given in 'patch_filename' to the source data given
34 * by (old_data, old_size). Write the patched output to the 'output'
35 * file, and update the SHA context with the output data as well.
36 * Return 0 on success.
37 */
38int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
39 const char* patch_filename,
40 SinkFn sink, void* token, SHA_CTX* ctx) {
41 FILE* f;
42 if ((f = fopen(patch_filename, "rb")) == NULL) {
43 printf("failed to open patch file\n");
44 return -1;
45 }
46
47 unsigned char header[12];
48 if (fread(header, 1, 12, f) != 12) {
49 printf("failed to read patch file header\n");
50 return -1;
51 }
52
53 // IMGDIFF1 uses CHUNK_NORMAL and CHUNK_GZIP.
54 // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
55 if (memcmp(header, "IMGDIFF", 7) != 0 ||
56 (header[7] != '1' && header[7] != '2')) {
57 printf("corrupt patch file header (magic number)\n");
58 return -1;
59 }
60
61 int num_chunks = Read4(header+8);
62
63 int i;
64 for (i = 0; i < num_chunks; ++i) {
65 // each chunk's header record starts with 4 bytes.
66 unsigned char chunk[4];
67 if (fread(chunk, 1, 4, f) != 4) {
68 printf("failed to read chunk %d record\n", i);
69 return -1;
70 }
71
72 int type = Read4(chunk);
73
74 if (type == CHUNK_NORMAL) {
75 unsigned char normal_header[24];
76 if (fread(normal_header, 1, 24, f) != 24) {
77 printf("failed to read chunk %d normal header data\n", i);
78 return -1;
79 }
80
81 size_t src_start = Read8(normal_header);
82 size_t src_len = Read8(normal_header+8);
83 size_t patch_offset = Read8(normal_header+16);
84
85 printf("CHUNK %d: normal patch offset %d\n", i, patch_offset);
86
87 ApplyBSDiffPatch(old_data + src_start, src_len,
88 patch_filename, patch_offset,
89 sink, token, ctx);
90 } else if (type == CHUNK_GZIP) {
91 // This branch is basically a duplicate of the CHUNK_DEFLATE
92 // branch, with a bit of extra processing for the gzip header
93 // and footer. I've avoided factoring the common code out since
94 // this branch will just be deleted when we drop support for
95 // IMGDIFF1.
96
97 // gzip chunks have an additional 64 + gzip_header_len + 8 bytes
98 // in their chunk header.
99 unsigned char* gzip = malloc(64);
100 if (fread(gzip, 1, 64, f) != 64) {
101 printf("failed to read chunk %d initial gzip header data\n",
102 i);
103 return -1;
104 }
105 size_t gzip_header_len = Read4(gzip+60);
106 gzip = realloc(gzip, 64 + gzip_header_len + 8);
107 if (fread(gzip+64, 1, gzip_header_len+8, f) != gzip_header_len+8) {
108 printf("failed to read chunk %d remaining gzip header data\n",
109 i);
110 return -1;
111 }
112
113 size_t src_start = Read8(gzip);
114 size_t src_len = Read8(gzip+8);
115 size_t patch_offset = Read8(gzip+16);
116
117 size_t expanded_len = Read8(gzip+24);
118 size_t target_len = Read8(gzip+32);
119 int gz_level = Read4(gzip+40);
120 int gz_method = Read4(gzip+44);
121 int gz_windowBits = Read4(gzip+48);
122 int gz_memLevel = Read4(gzip+52);
123 int gz_strategy = Read4(gzip+56);
124
125 printf("CHUNK %d: gzip patch offset %d\n", i, patch_offset);
126
127 // Decompress the source data; the chunk header tells us exactly
128 // how big we expect it to be when decompressed.
129
130 unsigned char* expanded_source = malloc(expanded_len);
131 if (expanded_source == NULL) {
132 printf("failed to allocate %d bytes for expanded_source\n",
133 expanded_len);
134 return -1;
135 }
136
137 z_stream strm;
138 strm.zalloc = Z_NULL;
139 strm.zfree = Z_NULL;
140 strm.opaque = Z_NULL;
141 strm.avail_in = src_len - (gzip_header_len + 8);
142 strm.next_in = (unsigned char*)(old_data + src_start + gzip_header_len);
143 strm.avail_out = expanded_len;
144 strm.next_out = expanded_source;
145
146 int ret;
147 ret = inflateInit2(&strm, -15);
148 if (ret != Z_OK) {
149 printf("failed to init source inflation: %d\n", ret);
150 return -1;
151 }
152
153 // Because we've provided enough room to accommodate the output
154 // data, we expect one call to inflate() to suffice.
155 ret = inflate(&strm, Z_SYNC_FLUSH);
156 if (ret != Z_STREAM_END) {
157 printf("source inflation returned %d\n", ret);
158 return -1;
159 }
160 // We should have filled the output buffer exactly.
161 if (strm.avail_out != 0) {
162 printf("source inflation short by %d bytes\n", strm.avail_out);
163 return -1;
164 }
165 inflateEnd(&strm);
166
167 // Next, apply the bsdiff patch (in memory) to the uncompressed
168 // data.
169 unsigned char* uncompressed_target_data;
170 ssize_t uncompressed_target_size;
171 if (ApplyBSDiffPatchMem(expanded_source, expanded_len,
172 patch_filename, patch_offset,
173 &uncompressed_target_data,
174 &uncompressed_target_size) != 0) {
175 return -1;
176 }
177
178 // Now compress the target data and append it to the output.
179
180 // start with the gzip header.
181 sink(gzip+64, gzip_header_len, token);
182 SHA_update(ctx, gzip+64, gzip_header_len);
183
184 // we're done with the expanded_source data buffer, so we'll
185 // reuse that memory to receive the output of deflate.
186 unsigned char* temp_data = expanded_source;
187 ssize_t temp_size = expanded_len;
188 if (temp_size < 32768) {
189 // ... unless the buffer is too small, in which case we'll
190 // allocate a fresh one.
191 free(temp_data);
192 temp_data = malloc(32768);
193 temp_size = 32768;
194 }
195
196 // now the deflate stream
197 strm.zalloc = Z_NULL;
198 strm.zfree = Z_NULL;
199 strm.opaque = Z_NULL;
200 strm.avail_in = uncompressed_target_size;
201 strm.next_in = uncompressed_target_data;
202 ret = deflateInit2(&strm, gz_level, gz_method, gz_windowBits,
203 gz_memLevel, gz_strategy);
204 do {
205 strm.avail_out = temp_size;
206 strm.next_out = temp_data;
207 ret = deflate(&strm, Z_FINISH);
208 size_t have = temp_size - strm.avail_out;
209
210 if (sink(temp_data, have, token) != have) {
211 printf("failed to write %d compressed bytes to output\n",
212 have);
213 return -1;
214 }
215 SHA_update(ctx, temp_data, have);
216 } while (ret != Z_STREAM_END);
217 deflateEnd(&strm);
218
219 // lastly, the gzip footer.
220 sink(gzip+64+gzip_header_len, 8, token);
221 SHA_update(ctx, gzip+64+gzip_header_len, 8);
222
223 free(temp_data);
224 free(uncompressed_target_data);
225 free(gzip);
226 } else if (type == CHUNK_RAW) {
227 unsigned char raw_header[4];
228 if (fread(raw_header, 1, 4, f) != 4) {
229 printf("failed to read chunk %d raw header data\n", i);
230 return -1;
231 }
232
233 size_t data_len = Read4(raw_header);
234
235 printf("CHUNK %d: raw data %d\n", i, data_len);
236
237 unsigned char* temp = malloc(data_len);
238 if (fread(temp, 1, data_len, f) != data_len) {
239 printf("failed to read chunk %d raw data\n", i);
240 return -1;
241 }
242 SHA_update(ctx, temp, data_len);
243 if (sink(temp, data_len, token) != data_len) {
244 printf("failed to write chunk %d raw data\n", i);
245 return -1;
246 }
247 } else if (type == CHUNK_DEFLATE) {
248 // deflate chunks have an additional 60 bytes in their chunk header.
249 unsigned char deflate_header[60];
250 if (fread(deflate_header, 1, 60, f) != 60) {
251 printf("failed to read chunk %d deflate header data\n", i);
252 return -1;
253 }
254
255 size_t src_start = Read8(deflate_header);
256 size_t src_len = Read8(deflate_header+8);
257 size_t patch_offset = Read8(deflate_header+16);
258 size_t expanded_len = Read8(deflate_header+24);
259 size_t target_len = Read8(deflate_header+32);
260 int level = Read4(deflate_header+40);
261 int method = Read4(deflate_header+44);
262 int windowBits = Read4(deflate_header+48);
263 int memLevel = Read4(deflate_header+52);
264 int strategy = Read4(deflate_header+56);
265
266 printf("CHUNK %d: deflate patch offset %d\n", i, patch_offset);
267
268 // Decompress the source data; the chunk header tells us exactly
269 // how big we expect it to be when decompressed.
270
271 unsigned char* expanded_source = malloc(expanded_len);
272 if (expanded_source == NULL) {
273 printf("failed to allocate %d bytes for expanded_source\n",
274 expanded_len);
275 return -1;
276 }
277
278 z_stream strm;
279 strm.zalloc = Z_NULL;
280 strm.zfree = Z_NULL;
281 strm.opaque = Z_NULL;
282 strm.avail_in = src_len;
283 strm.next_in = (unsigned char*)(old_data + src_start);
284 strm.avail_out = expanded_len;
285 strm.next_out = expanded_source;
286
287 int ret;
288 ret = inflateInit2(&strm, -15);
289 if (ret != Z_OK) {
290 printf("failed to init source inflation: %d\n", ret);
291 return -1;
292 }
293
294 // Because we've provided enough room to accommodate the output
295 // data, we expect one call to inflate() to suffice.
296 ret = inflate(&strm, Z_SYNC_FLUSH);
297 if (ret != Z_STREAM_END) {
298 printf("source inflation returned %d\n", ret);
299 return -1;
300 }
301 // We should have filled the output buffer exactly.
302 if (strm.avail_out != 0) {
303 printf("source inflation short by %d bytes\n", strm.avail_out);
304 return -1;
305 }
306 inflateEnd(&strm);
307
308 // Next, apply the bsdiff patch (in memory) to the uncompressed
309 // data.
310 unsigned char* uncompressed_target_data;
311 ssize_t uncompressed_target_size;
312 if (ApplyBSDiffPatchMem(expanded_source, expanded_len,
313 patch_filename, patch_offset,
314 &uncompressed_target_data,
315 &uncompressed_target_size) != 0) {
316 return -1;
317 }
318
319 // Now compress the target data and append it to the output.
320
321 // we're done with the expanded_source data buffer, so we'll
322 // reuse that memory to receive the output of deflate.
323 unsigned char* temp_data = expanded_source;
324 ssize_t temp_size = expanded_len;
325 if (temp_size < 32768) {
326 // ... unless the buffer is too small, in which case we'll
327 // allocate a fresh one.
328 free(temp_data);
329 temp_data = malloc(32768);
330 temp_size = 32768;
331 }
332
333 // now the deflate stream
334 strm.zalloc = Z_NULL;
335 strm.zfree = Z_NULL;
336 strm.opaque = Z_NULL;
337 strm.avail_in = uncompressed_target_size;
338 strm.next_in = uncompressed_target_data;
339 ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
340 do {
341 strm.avail_out = temp_size;
342 strm.next_out = temp_data;
343 ret = deflate(&strm, Z_FINISH);
344 size_t have = temp_size - strm.avail_out;
345
346 if (sink(temp_data, have, token) != have) {
347 printf("failed to write %d compressed bytes to output\n",
348 have);
349 return -1;
350 }
351 SHA_update(ctx, temp_data, have);
352 } while (ret != Z_STREAM_END);
353 deflateEnd(&strm);
354
355 free(temp_data);
356 free(uncompressed_target_data);
357 } else {
358 printf("patch chunk %d is unknown type %d\n", i, type);
359 return -1;
360 }
361 }
362
363 return 0;
364}