blob: c59edeb6b750a0621b22b1dce84bae5fc077d16b [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/*
18 * This program constructs binary patches for images -- such as boot.img
19 * and recovery.img -- that consist primarily of large chunks of gzipped
20 * data interspersed with uncompressed data. Doing a naive bsdiff of
21 * these files is not useful because small changes in the data lead to
22 * large changes in the compressed bitstream; bsdiff patches of gzipped
23 * data are typically as large as the data itself.
24 *
25 * To patch these usefully, we break the source and target images up into
26 * chunks of two types: "normal" and "gzip". Normal chunks are simply
27 * patched using a plain bsdiff. Gzip chunks are first expanded, then a
28 * bsdiff is applied to the uncompressed data, then the patched data is
29 * gzipped using the same encoder parameters. Patched chunks are
30 * concatenated together to create the output file; the output image
31 * should be *exactly* the same series of bytes as the target image used
32 * originally to generate the patch.
33 *
34 * To work well with this tool, the gzipped sections of the target
35 * image must have been generated using the same deflate encoder that
36 * is available in applypatch, namely, the one in the zlib library.
37 * In practice this means that images should be compressed using the
38 * "minigzip" tool included in the zlib distribution, not the GNU gzip
39 * program.
40 *
41 * An "imgdiff" patch consists of a header describing the chunk structure
42 * of the file and any encoding parameters needed for the gzipped
43 * chunks, followed by N bsdiff patches, one per chunk.
44 *
45 * For a diff to be generated, the source and target images must have the
46 * same "chunk" structure: that is, the same number of gzipped and normal
47 * chunks in the same order. Android boot and recovery images currently
48 * consist of five chunks: a small normal header, a gzipped kernel, a
49 * small normal section, a gzipped ramdisk, and finally a small normal
50 * footer.
51 *
52 * Caveats: we locate gzipped sections within the source and target
53 * images by searching for the byte sequence 1f8b0800: 1f8b is the gzip
54 * magic number; 08 specifies the "deflate" encoding [the only encoding
55 * supported by the gzip standard]; and 00 is the flags byte. We do not
56 * currently support any extra header fields (which would be indicated by
57 * a nonzero flags byte). We also don't handle the case when that byte
58 * sequence appears spuriously in the file. (Note that it would have to
59 * occur spuriously within a normal chunk to be a problem.)
60 *
61 *
62 * The imgdiff patch header looks like this:
63 *
64 * "IMGDIFF1" (8) [magic number and version]
65 * chunk count (4)
66 * for each chunk:
67 * chunk type (4) [CHUNK_{NORMAL, GZIP, DEFLATE, RAW}]
68 * if chunk type == CHUNK_NORMAL:
69 * source start (8)
70 * source len (8)
71 * bsdiff patch offset (8) [from start of patch file]
72 * if chunk type == CHUNK_GZIP: (version 1 only)
73 * source start (8)
74 * source len (8)
75 * bsdiff patch offset (8) [from start of patch file]
76 * source expanded len (8) [size of uncompressed source]
77 * target expected len (8) [size of uncompressed target]
78 * gzip level (4)
79 * method (4)
80 * windowBits (4)
81 * memLevel (4)
82 * strategy (4)
83 * gzip header len (4)
84 * gzip header (gzip header len)
85 * gzip footer (8)
86 * if chunk type == CHUNK_DEFLATE: (version 2 only)
87 * source start (8)
88 * source len (8)
89 * bsdiff patch offset (8) [from start of patch file]
90 * source expanded len (8) [size of uncompressed source]
91 * target expected len (8) [size of uncompressed target]
92 * gzip level (4)
93 * method (4)
94 * windowBits (4)
95 * memLevel (4)
96 * strategy (4)
97 * if chunk type == RAW: (version 2 only)
98 * target len (4)
99 * data (target len)
100 *
101 * All integers are little-endian. "source start" and "source len"
102 * specify the section of the input image that comprises this chunk,
103 * including the gzip header and footer for gzip chunks. "source
104 * expanded len" is the size of the uncompressed source data. "target
105 * expected len" is the size of the uncompressed data after applying
106 * the bsdiff patch. The next five parameters specify the zlib
107 * parameters to be used when compressing the patched data, and the
108 * next three specify the header and footer to be wrapped around the
109 * compressed data to create the output chunk (so that header contents
110 * like the timestamp are recreated exactly).
111 *
112 * After the header there are 'chunk count' bsdiff patches; the offset
113 * of each from the beginning of the file is specified in the header.
Doug Zongkera3ccba62012-08-20 15:28:02 -0700114 *
115 * This tool can take an optional file of "bonus data". This is an
116 * extra file of data that is appended to chunk #1 after it is
117 * compressed (it must be a CHUNK_DEFLATE chunk). The same file must
118 * be available (and passed to applypatch with -b) when applying the
119 * patch. This is used to reduce the size of recovery-from-boot
120 * patches by combining the boot image with recovery ramdisk
121 * information that is stored on the system partition.
Doug Zongker512536a2010-02-17 16:11:44 -0800122 */
123
124#include <errno.h>
Tao Baoba9a42a2015-06-23 23:23:33 -0700125#include <inttypes.h>
Doug Zongker512536a2010-02-17 16:11:44 -0800126#include <stdio.h>
127#include <stdlib.h>
128#include <string.h>
129#include <sys/stat.h>
130#include <unistd.h>
131#include <sys/types.h>
132
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700133#include <algorithm>
134#include <memory>
135#include <vector>
136
Sen Jiang2fffcb12016-05-03 15:49:10 -0700137#include <bsdiff.h>
138
Doug Zongker512536a2010-02-17 16:11:44 -0800139#include "zlib.h"
140#include "imgdiff.h"
141#include "utils.h"
142
143typedef struct {
144 int type; // CHUNK_NORMAL, CHUNK_DEFLATE
145 size_t start; // offset of chunk in original image file
146
147 size_t len;
148 unsigned char* data; // data to be patched (uncompressed, for deflate chunks)
149
150 size_t source_start;
151 size_t source_len;
152
Doug Zongker512536a2010-02-17 16:11:44 -0800153 // --- for CHUNK_DEFLATE chunks only: ---
154
155 // original (compressed) deflate data
156 size_t deflate_len;
157 unsigned char* deflate_data;
158
159 char* filename; // used for zip entries
160
161 // deflate encoder parameters
162 int level, method, windowBits, memLevel, strategy;
163
164 size_t source_uncompressed_len;
165} ImageChunk;
166
167typedef struct {
168 int data_offset;
169 int deflate_len;
170 int uncomp_len;
171 char* filename;
172} ZipFileEntry;
173
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700174static bool fileentry_compare(const ZipFileEntry& a, const ZipFileEntry& b) {
175 return a.data_offset < b.data_offset;
Doug Zongker512536a2010-02-17 16:11:44 -0800176}
177
Doug Zongker512536a2010-02-17 16:11:44 -0800178unsigned char* ReadZip(const char* filename,
179 int* num_chunks, ImageChunk** chunks,
180 int include_pseudo_chunk) {
181 struct stat st;
182 if (stat(filename, &st) != 0) {
183 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
184 return NULL;
185 }
186
Tao Baoba9a42a2015-06-23 23:23:33 -0700187 size_t sz = static_cast<size_t>(st.st_size);
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700188 std::unique_ptr<unsigned char[]> img(new unsigned char[sz]);
Doug Zongker512536a2010-02-17 16:11:44 -0800189 FILE* f = fopen(filename, "rb");
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700190 if (fread(img.get(), 1, sz, f) != sz) {
Doug Zongker512536a2010-02-17 16:11:44 -0800191 printf("failed to read \"%s\" %s\n", filename, strerror(errno));
192 fclose(f);
193 return NULL;
194 }
195 fclose(f);
196
197 // look for the end-of-central-directory record.
198
199 int i;
200 for (i = st.st_size-20; i >= 0 && i > st.st_size - 65600; --i) {
201 if (img[i] == 0x50 && img[i+1] == 0x4b &&
202 img[i+2] == 0x05 && img[i+3] == 0x06) {
203 break;
204 }
205 }
206 // double-check: this archive consists of a single "disk"
207 if (!(img[i+4] == 0 && img[i+5] == 0 && img[i+6] == 0 && img[i+7] == 0)) {
208 printf("can't process multi-disk archive\n");
209 return NULL;
210 }
211
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700212 int cdcount = Read2(&img[i+8]);
213 int cdoffset = Read4(&img[i+16]);
Doug Zongker512536a2010-02-17 16:11:44 -0800214
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700215 std::vector<ZipFileEntry> temp_entries(cdcount);
Doug Zongker512536a2010-02-17 16:11:44 -0800216 int entrycount = 0;
217
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700218 unsigned char* cd = &img[cdoffset];
Doug Zongker512536a2010-02-17 16:11:44 -0800219 for (i = 0; i < cdcount; ++i) {
220 if (!(cd[0] == 0x50 && cd[1] == 0x4b && cd[2] == 0x01 && cd[3] == 0x02)) {
221 printf("bad central directory entry %d\n", i);
222 return NULL;
223 }
224
225 int clen = Read4(cd+20); // compressed len
226 int ulen = Read4(cd+24); // uncompressed len
227 int nlen = Read2(cd+28); // filename len
228 int xlen = Read2(cd+30); // extra field len
229 int mlen = Read2(cd+32); // file comment len
230 int hoffset = Read4(cd+42); // local header offset
231
Tao Baoba9a42a2015-06-23 23:23:33 -0700232 char* filename = reinterpret_cast<char*>(malloc(nlen+1));
Doug Zongker512536a2010-02-17 16:11:44 -0800233 memcpy(filename, cd+46, nlen);
234 filename[nlen] = '\0';
235
236 int method = Read2(cd+10);
237
238 cd += 46 + nlen + xlen + mlen;
239
240 if (method != 8) { // 8 == deflate
241 free(filename);
242 continue;
243 }
244
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700245 unsigned char* lh = &img[hoffset];
Doug Zongker512536a2010-02-17 16:11:44 -0800246
247 if (!(lh[0] == 0x50 && lh[1] == 0x4b && lh[2] == 0x03 && lh[3] == 0x04)) {
248 printf("bad local file header entry %d\n", i);
249 return NULL;
250 }
251
252 if (Read2(lh+26) != nlen || memcmp(lh+30, filename, nlen) != 0) {
253 printf("central dir filename doesn't match local header\n");
254 return NULL;
255 }
256
257 xlen = Read2(lh+28); // extra field len; might be different from CD entry?
258
259 temp_entries[entrycount].data_offset = hoffset+30+nlen+xlen;
260 temp_entries[entrycount].deflate_len = clen;
261 temp_entries[entrycount].uncomp_len = ulen;
262 temp_entries[entrycount].filename = filename;
263 ++entrycount;
264 }
265
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700266 std::sort(temp_entries.begin(), temp_entries.end(), fileentry_compare);
Doug Zongker512536a2010-02-17 16:11:44 -0800267
268#if 0
269 printf("found %d deflated entries\n", entrycount);
270 for (i = 0; i < entrycount; ++i) {
271 printf("off %10d len %10d unlen %10d %p %s\n",
272 temp_entries[i].data_offset,
273 temp_entries[i].deflate_len,
274 temp_entries[i].uncomp_len,
275 temp_entries[i].filename,
276 temp_entries[i].filename);
277 }
278#endif
279
280 *num_chunks = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700281 *chunks = reinterpret_cast<ImageChunk*>(malloc((entrycount*2+2) * sizeof(ImageChunk)));
Doug Zongker512536a2010-02-17 16:11:44 -0800282 ImageChunk* curr = *chunks;
283
284 if (include_pseudo_chunk) {
285 curr->type = CHUNK_NORMAL;
286 curr->start = 0;
287 curr->len = st.st_size;
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700288 curr->data = img.get();
Doug Zongker512536a2010-02-17 16:11:44 -0800289 curr->filename = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800290 ++curr;
291 ++*num_chunks;
292 }
293
294 int pos = 0;
295 int nextentry = 0;
296
297 while (pos < st.st_size) {
298 if (nextentry < entrycount && pos == temp_entries[nextentry].data_offset) {
299 curr->type = CHUNK_DEFLATE;
300 curr->start = pos;
301 curr->deflate_len = temp_entries[nextentry].deflate_len;
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700302 curr->deflate_data = &img[pos];
Doug Zongker512536a2010-02-17 16:11:44 -0800303 curr->filename = temp_entries[nextentry].filename;
Doug Zongker512536a2010-02-17 16:11:44 -0800304
305 curr->len = temp_entries[nextentry].uncomp_len;
Tao Baoba9a42a2015-06-23 23:23:33 -0700306 curr->data = reinterpret_cast<unsigned char*>(malloc(curr->len));
Doug Zongker512536a2010-02-17 16:11:44 -0800307
308 z_stream strm;
309 strm.zalloc = Z_NULL;
310 strm.zfree = Z_NULL;
311 strm.opaque = Z_NULL;
312 strm.avail_in = curr->deflate_len;
313 strm.next_in = curr->deflate_data;
314
315 // -15 means we are decoding a 'raw' deflate stream; zlib will
316 // not expect zlib headers.
317 int ret = inflateInit2(&strm, -15);
318
319 strm.avail_out = curr->len;
320 strm.next_out = curr->data;
321 ret = inflate(&strm, Z_NO_FLUSH);
322 if (ret != Z_STREAM_END) {
323 printf("failed to inflate \"%s\"; %d\n", curr->filename, ret);
324 return NULL;
325 }
326
327 inflateEnd(&strm);
328
329 pos += curr->deflate_len;
330 ++nextentry;
331 ++*num_chunks;
332 ++curr;
333 continue;
334 }
335
336 // use a normal chunk to take all the data up to the start of the
337 // next deflate section.
338
339 curr->type = CHUNK_NORMAL;
340 curr->start = pos;
341 if (nextentry < entrycount) {
342 curr->len = temp_entries[nextentry].data_offset - pos;
343 } else {
344 curr->len = st.st_size - pos;
345 }
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700346 curr->data = &img[pos];
Doug Zongker512536a2010-02-17 16:11:44 -0800347 curr->filename = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800348 pos += curr->len;
349
350 ++*num_chunks;
351 ++curr;
352 }
353
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700354 return img.release();
Doug Zongker512536a2010-02-17 16:11:44 -0800355}
356
357/*
358 * Read the given file and break it up into chunks, putting the number
359 * of chunks and their info in *num_chunks and **chunks,
360 * respectively. Returns a malloc'd block of memory containing the
361 * contents of the file; various pointers in the output chunk array
362 * will point into this block of memory. The caller should free the
363 * return value when done with all the chunks. Returns NULL on
364 * failure.
365 */
366unsigned char* ReadImage(const char* filename,
367 int* num_chunks, ImageChunk** chunks) {
368 struct stat st;
369 if (stat(filename, &st) != 0) {
370 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
371 return NULL;
372 }
373
Tao Baoba9a42a2015-06-23 23:23:33 -0700374 size_t sz = static_cast<size_t>(st.st_size);
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700375 std::unique_ptr<unsigned char[]> img(new unsigned char[sz+4]);
Doug Zongker512536a2010-02-17 16:11:44 -0800376 FILE* f = fopen(filename, "rb");
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700377 if (fread(img.get(), 1, sz, f) != sz) {
Doug Zongker512536a2010-02-17 16:11:44 -0800378 printf("failed to read \"%s\" %s\n", filename, strerror(errno));
379 fclose(f);
380 return NULL;
381 }
382 fclose(f);
383
384 // append 4 zero bytes to the data so we can always search for the
385 // four-byte string 1f8b0800 starting at any point in the actual
386 // file data, without special-casing the end of the data.
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700387 memset(&img[sz], 0, 4);
Doug Zongker512536a2010-02-17 16:11:44 -0800388
389 size_t pos = 0;
390
391 *num_chunks = 0;
392 *chunks = NULL;
393
Tao Baoba9a42a2015-06-23 23:23:33 -0700394 while (pos < sz) {
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700395 unsigned char* p = &img[pos];
Doug Zongker512536a2010-02-17 16:11:44 -0800396
Tao Baoba9a42a2015-06-23 23:23:33 -0700397 if (sz - pos >= 4 &&
Doug Zongker512536a2010-02-17 16:11:44 -0800398 p[0] == 0x1f && p[1] == 0x8b &&
399 p[2] == 0x08 && // deflate compression
400 p[3] == 0x00) { // no header flags
401 // 'pos' is the offset of the start of a gzip chunk.
Johan Redestigc68bd342015-04-14 21:20:06 +0200402 size_t chunk_offset = pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800403
404 *num_chunks += 3;
Tao Baoba9a42a2015-06-23 23:23:33 -0700405 *chunks = reinterpret_cast<ImageChunk*>(realloc(*chunks,
406 *num_chunks * sizeof(ImageChunk)));
Doug Zongker512536a2010-02-17 16:11:44 -0800407 ImageChunk* curr = *chunks + (*num_chunks-3);
408
409 // create a normal chunk for the header.
410 curr->start = pos;
411 curr->type = CHUNK_NORMAL;
412 curr->len = GZIP_HEADER_LEN;
413 curr->data = p;
Doug Zongker512536a2010-02-17 16:11:44 -0800414
415 pos += curr->len;
416 p += curr->len;
417 ++curr;
418
419 curr->type = CHUNK_DEFLATE;
420 curr->filename = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800421
422 // We must decompress this chunk in order to discover where it
423 // ends, and so we can put the uncompressed data and its length
424 // into curr->data and curr->len.
425
426 size_t allocated = 32768;
427 curr->len = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700428 curr->data = reinterpret_cast<unsigned char*>(malloc(allocated));
Doug Zongker512536a2010-02-17 16:11:44 -0800429 curr->start = pos;
430 curr->deflate_data = p;
431
432 z_stream strm;
433 strm.zalloc = Z_NULL;
434 strm.zfree = Z_NULL;
435 strm.opaque = Z_NULL;
Tao Baoba9a42a2015-06-23 23:23:33 -0700436 strm.avail_in = sz - pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800437 strm.next_in = p;
438
439 // -15 means we are decoding a 'raw' deflate stream; zlib will
440 // not expect zlib headers.
441 int ret = inflateInit2(&strm, -15);
442
443 do {
444 strm.avail_out = allocated - curr->len;
445 strm.next_out = curr->data + curr->len;
446 ret = inflate(&strm, Z_NO_FLUSH);
Johan Redestigc68bd342015-04-14 21:20:06 +0200447 if (ret < 0) {
Sen Jiangfa4f1b72016-02-11 16:14:23 -0800448 printf("Warning: inflate failed [%s] at offset [%zu],"
449 " treating as a normal chunk\n",
David Riley0779fc92015-12-10 10:18:25 -0800450 strm.msg, chunk_offset);
Sen Jiangfa4f1b72016-02-11 16:14:23 -0800451 break;
Johan Redestigc68bd342015-04-14 21:20:06 +0200452 }
Doug Zongker512536a2010-02-17 16:11:44 -0800453 curr->len = allocated - strm.avail_out;
454 if (strm.avail_out == 0) {
455 allocated *= 2;
Tao Baoba9a42a2015-06-23 23:23:33 -0700456 curr->data = reinterpret_cast<unsigned char*>(realloc(curr->data, allocated));
Doug Zongker512536a2010-02-17 16:11:44 -0800457 }
458 } while (ret != Z_STREAM_END);
459
Tao Baoba9a42a2015-06-23 23:23:33 -0700460 curr->deflate_len = sz - strm.avail_in - pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800461 inflateEnd(&strm);
Sen Jiangfa4f1b72016-02-11 16:14:23 -0800462
463 if (ret < 0) {
464 free(curr->data);
465 *num_chunks -= 2;
466 continue;
467 }
468
Doug Zongker512536a2010-02-17 16:11:44 -0800469 pos += curr->deflate_len;
470 p += curr->deflate_len;
471 ++curr;
472
473 // create a normal chunk for the footer
474
475 curr->type = CHUNK_NORMAL;
476 curr->start = pos;
477 curr->len = GZIP_FOOTER_LEN;
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700478 curr->data = &img[pos];
Doug Zongker512536a2010-02-17 16:11:44 -0800479
480 pos += curr->len;
481 p += curr->len;
482 ++curr;
483
484 // The footer (that we just skipped over) contains the size of
485 // the uncompressed data. Double-check to make sure that it
486 // matches the size of the data we got when we actually did
487 // the decompression.
488 size_t footer_size = Read4(p-4);
489 if (footer_size != curr[-2].len) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700490 printf("Error: footer size %zu != decompressed size %zu\n",
491 footer_size, curr[-2].len);
Doug Zongker512536a2010-02-17 16:11:44 -0800492 return NULL;
493 }
494 } else {
495 // Reallocate the list for every chunk; we expect the number of
496 // chunks to be small (5 for typical boot and recovery images).
497 ++*num_chunks;
Tao Baoba9a42a2015-06-23 23:23:33 -0700498 *chunks = reinterpret_cast<ImageChunk*>(realloc(*chunks, *num_chunks * sizeof(ImageChunk)));
Doug Zongker512536a2010-02-17 16:11:44 -0800499 ImageChunk* curr = *chunks + (*num_chunks-1);
500 curr->start = pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800501
502 // 'pos' is not the offset of the start of a gzip chunk, so scan
503 // forward until we find a gzip header.
504 curr->type = CHUNK_NORMAL;
505 curr->data = p;
506
Tao Baoba9a42a2015-06-23 23:23:33 -0700507 for (curr->len = 0; curr->len < (sz - pos); ++curr->len) {
Doug Zongker512536a2010-02-17 16:11:44 -0800508 if (p[curr->len] == 0x1f &&
509 p[curr->len+1] == 0x8b &&
510 p[curr->len+2] == 0x08 &&
511 p[curr->len+3] == 0x00) {
512 break;
513 }
514 }
515 pos += curr->len;
516 }
517 }
518
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700519 return img.release();
Doug Zongker512536a2010-02-17 16:11:44 -0800520}
521
522#define BUFFER_SIZE 32768
523
524/*
525 * Takes the uncompressed data stored in the chunk, compresses it
526 * using the zlib parameters stored in the chunk, and checks that it
527 * matches exactly the compressed data we started with (also stored in
528 * the chunk). Return 0 on success.
529 */
530int TryReconstruction(ImageChunk* chunk, unsigned char* out) {
531 size_t p = 0;
532
533#if 0
534 printf("trying %d %d %d %d %d\n",
535 chunk->level, chunk->method, chunk->windowBits,
536 chunk->memLevel, chunk->strategy);
537#endif
538
539 z_stream strm;
540 strm.zalloc = Z_NULL;
541 strm.zfree = Z_NULL;
542 strm.opaque = Z_NULL;
543 strm.avail_in = chunk->len;
544 strm.next_in = chunk->data;
545 int ret;
546 ret = deflateInit2(&strm, chunk->level, chunk->method, chunk->windowBits,
547 chunk->memLevel, chunk->strategy);
548 do {
549 strm.avail_out = BUFFER_SIZE;
550 strm.next_out = out;
551 ret = deflate(&strm, Z_FINISH);
552 size_t have = BUFFER_SIZE - strm.avail_out;
553
554 if (memcmp(out, chunk->deflate_data+p, have) != 0) {
555 // mismatch; data isn't the same.
556 deflateEnd(&strm);
557 return -1;
558 }
559 p += have;
560 } while (ret != Z_STREAM_END);
561 deflateEnd(&strm);
562 if (p != chunk->deflate_len) {
563 // mismatch; ran out of data before we should have.
564 return -1;
565 }
566 return 0;
567}
568
569/*
570 * Verify that we can reproduce exactly the same compressed data that
571 * we started with. Sets the level, method, windowBits, memLevel, and
572 * strategy fields in the chunk to the encoding parameters needed to
573 * produce the right output. Returns 0 on success.
574 */
575int ReconstructDeflateChunk(ImageChunk* chunk) {
576 if (chunk->type != CHUNK_DEFLATE) {
577 printf("attempt to reconstruct non-deflate chunk\n");
578 return -1;
579 }
580
Tao Baoba9a42a2015-06-23 23:23:33 -0700581 unsigned char* out = reinterpret_cast<unsigned char*>(malloc(BUFFER_SIZE));
Doug Zongker512536a2010-02-17 16:11:44 -0800582
583 // We only check two combinations of encoder parameters: level 6
584 // (the default) and level 9 (the maximum).
585 for (chunk->level = 6; chunk->level <= 9; chunk->level += 3) {
586 chunk->windowBits = -15; // 32kb window; negative to indicate a raw stream.
587 chunk->memLevel = 8; // the default value.
588 chunk->method = Z_DEFLATED;
589 chunk->strategy = Z_DEFAULT_STRATEGY;
590
591 if (TryReconstruction(chunk, out) == 0) {
592 free(out);
593 return 0;
594 }
595 }
596
597 free(out);
598 return -1;
599}
600
601/*
602 * Given source and target chunks, compute a bsdiff patch between them
603 * by running bsdiff in a subprocess. Return the patch data, placing
604 * its length in *size. Return NULL on failure. We expect the bsdiff
605 * program to be in the path.
606 */
607unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) {
608 if (tgt->type == CHUNK_NORMAL) {
609 if (tgt->len <= 160) {
610 tgt->type = CHUNK_RAW;
611 *size = tgt->len;
612 return tgt->data;
613 }
614 }
615
616 char ptemp[] = "/tmp/imgdiff-patch-XXXXXX";
Jeremy Compostellaa91c66d2015-09-08 19:15:09 +0200617 int fd = mkstemp(ptemp);
618
619 if (fd == -1) {
620 printf("MakePatch failed to create a temporary file: %s\n",
621 strerror(errno));
622 return NULL;
623 }
624 close(fd); // temporary file is created and we don't need its file
625 // descriptor
Doug Zongker512536a2010-02-17 16:11:44 -0800626
Sen Jiang2fffcb12016-05-03 15:49:10 -0700627 int r = bsdiff::bsdiff(src->data, src->len, tgt->data, tgt->len, ptemp);
Doug Zongker512536a2010-02-17 16:11:44 -0800628 if (r != 0) {
629 printf("bsdiff() failed: %d\n", r);
630 return NULL;
631 }
632
633 struct stat st;
634 if (stat(ptemp, &st) != 0) {
635 printf("failed to stat patch file %s: %s\n",
636 ptemp, strerror(errno));
637 return NULL;
638 }
639
Tao Baoba9a42a2015-06-23 23:23:33 -0700640 size_t sz = static_cast<size_t>(st.st_size);
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700641 std::unique_ptr<unsigned char[]> data(new unsigned char[sz]);
Doug Zongker512536a2010-02-17 16:11:44 -0800642
Tao Baoba9a42a2015-06-23 23:23:33 -0700643 if (tgt->type == CHUNK_NORMAL && tgt->len <= sz) {
Doug Zongker512536a2010-02-17 16:11:44 -0800644 unlink(ptemp);
645
646 tgt->type = CHUNK_RAW;
647 *size = tgt->len;
648 return tgt->data;
649 }
650
Tao Baoba9a42a2015-06-23 23:23:33 -0700651 *size = sz;
Doug Zongker512536a2010-02-17 16:11:44 -0800652
653 FILE* f = fopen(ptemp, "rb");
654 if (f == NULL) {
655 printf("failed to open patch %s: %s\n", ptemp, strerror(errno));
656 return NULL;
657 }
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700658 if (fread(data.get(), 1, sz, f) != sz) {
Doug Zongker512536a2010-02-17 16:11:44 -0800659 printf("failed to read patch %s: %s\n", ptemp, strerror(errno));
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700660 fclose(f);
Doug Zongker512536a2010-02-17 16:11:44 -0800661 return NULL;
662 }
663 fclose(f);
664
665 unlink(ptemp);
666
667 tgt->source_start = src->start;
668 switch (tgt->type) {
669 case CHUNK_NORMAL:
670 tgt->source_len = src->len;
671 break;
672 case CHUNK_DEFLATE:
673 tgt->source_len = src->deflate_len;
674 tgt->source_uncompressed_len = src->len;
675 break;
676 }
677
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700678 return data.release();
Doug Zongker512536a2010-02-17 16:11:44 -0800679}
680
681/*
682 * Cause a gzip chunk to be treated as a normal chunk (ie, as a blob
683 * of uninterpreted data). The resulting patch will likely be about
684 * as big as the target file, but it lets us handle the case of images
685 * where some gzip chunks are reconstructible but others aren't (by
686 * treating the ones that aren't as normal chunks).
687 */
688void ChangeDeflateChunkToNormal(ImageChunk* ch) {
689 if (ch->type != CHUNK_DEFLATE) return;
690 ch->type = CHUNK_NORMAL;
691 free(ch->data);
692 ch->data = ch->deflate_data;
693 ch->len = ch->deflate_len;
694}
695
696/*
697 * Return true if the data in the chunk is identical (including the
698 * compressed representation, for gzip chunks).
699 */
700int AreChunksEqual(ImageChunk* a, ImageChunk* b) {
701 if (a->type != b->type) return 0;
702
703 switch (a->type) {
704 case CHUNK_NORMAL:
705 return a->len == b->len && memcmp(a->data, b->data, a->len) == 0;
706
707 case CHUNK_DEFLATE:
708 return a->deflate_len == b->deflate_len &&
709 memcmp(a->deflate_data, b->deflate_data, a->deflate_len) == 0;
710
711 default:
712 printf("unknown chunk type %d\n", a->type);
713 return 0;
714 }
715}
716
717/*
718 * Look for runs of adjacent normal chunks and compress them down into
719 * a single chunk. (Such runs can be produced when deflate chunks are
720 * changed to normal chunks.)
721 */
722void MergeAdjacentNormalChunks(ImageChunk* chunks, int* num_chunks) {
723 int out = 0;
724 int in_start = 0, in_end;
725 while (in_start < *num_chunks) {
726 if (chunks[in_start].type != CHUNK_NORMAL) {
727 in_end = in_start+1;
728 } else {
729 // in_start is a normal chunk. Look for a run of normal chunks
730 // that constitute a solid block of data (ie, each chunk begins
731 // where the previous one ended).
732 for (in_end = in_start+1;
733 in_end < *num_chunks && chunks[in_end].type == CHUNK_NORMAL &&
734 (chunks[in_end].start ==
735 chunks[in_end-1].start + chunks[in_end-1].len &&
736 chunks[in_end].data ==
737 chunks[in_end-1].data + chunks[in_end-1].len);
738 ++in_end);
739 }
740
741 if (in_end == in_start+1) {
742#if 0
743 printf("chunk %d is now %d\n", in_start, out);
744#endif
745 if (out != in_start) {
746 memcpy(chunks+out, chunks+in_start, sizeof(ImageChunk));
747 }
748 } else {
749#if 0
750 printf("collapse normal chunks %d-%d into %d\n", in_start, in_end-1, out);
751#endif
752
753 // Merge chunks [in_start, in_end-1] into one chunk. Since the
754 // data member of each chunk is just a pointer into an in-memory
755 // copy of the file, this can be done without recopying (the
756 // output chunk has the first chunk's start location and data
757 // pointer, and length equal to the sum of the input chunk
758 // lengths).
759 chunks[out].type = CHUNK_NORMAL;
760 chunks[out].start = chunks[in_start].start;
761 chunks[out].data = chunks[in_start].data;
762 chunks[out].len = chunks[in_end-1].len +
763 (chunks[in_end-1].start - chunks[in_start].start);
764 }
765
766 ++out;
767 in_start = in_end;
768 }
769 *num_chunks = out;
770}
771
772ImageChunk* FindChunkByName(const char* name,
773 ImageChunk* chunks, int num_chunks) {
774 int i;
775 for (i = 0; i < num_chunks; ++i) {
776 if (chunks[i].type == CHUNK_DEFLATE && chunks[i].filename &&
777 strcmp(name, chunks[i].filename) == 0) {
778 return chunks+i;
779 }
780 }
781 return NULL;
782}
783
784void DumpChunks(ImageChunk* chunks, int num_chunks) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700785 for (int i = 0; i < num_chunks; ++i) {
786 printf("chunk %d: type %d start %zu len %zu\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800787 i, chunks[i].type, chunks[i].start, chunks[i].len);
788 }
789}
790
791int main(int argc, char** argv) {
Doug Zongker512536a2010-02-17 16:11:44 -0800792 int zip_mode = 0;
793
Doug Zongkera3ccba62012-08-20 15:28:02 -0700794 if (argc >= 2 && strcmp(argv[1], "-z") == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -0800795 zip_mode = 1;
796 --argc;
797 ++argv;
798 }
799
Doug Zongkera3ccba62012-08-20 15:28:02 -0700800 size_t bonus_size = 0;
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700801 std::vector<unsigned char> bonus_data;
Doug Zongkera3ccba62012-08-20 15:28:02 -0700802 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
803 struct stat st;
804 if (stat(argv[2], &st) != 0) {
805 printf("failed to stat bonus file %s: %s\n", argv[2], strerror(errno));
806 return 1;
807 }
808 bonus_size = st.st_size;
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700809 bonus_data.resize(bonus_size);
Doug Zongkera3ccba62012-08-20 15:28:02 -0700810 FILE* f = fopen(argv[2], "rb");
811 if (f == NULL) {
812 printf("failed to open bonus file %s: %s\n", argv[2], strerror(errno));
813 return 1;
814 }
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700815 if (fread(bonus_data.data(), 1, bonus_size, f) != bonus_size) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700816 printf("failed to read bonus file %s: %s\n", argv[2], strerror(errno));
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700817 fclose(f);
Doug Zongkera3ccba62012-08-20 15:28:02 -0700818 return 1;
819 }
820 fclose(f);
821
822 argc -= 2;
823 argv += 2;
824 }
825
826 if (argc != 4) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700827 printf("usage: %s [-z] [-b <bonus-file>] <src-img> <tgt-img> <patch-file>\n",
828 argv[0]);
829 return 2;
830 }
Doug Zongker512536a2010-02-17 16:11:44 -0800831
832 int num_src_chunks;
833 ImageChunk* src_chunks;
834 int num_tgt_chunks;
835 ImageChunk* tgt_chunks;
836 int i;
837
838 if (zip_mode) {
839 if (ReadZip(argv[1], &num_src_chunks, &src_chunks, 1) == NULL) {
840 printf("failed to break apart source zip file\n");
841 return 1;
842 }
843 if (ReadZip(argv[2], &num_tgt_chunks, &tgt_chunks, 0) == NULL) {
844 printf("failed to break apart target zip file\n");
845 return 1;
846 }
847 } else {
848 if (ReadImage(argv[1], &num_src_chunks, &src_chunks) == NULL) {
849 printf("failed to break apart source image\n");
850 return 1;
851 }
852 if (ReadImage(argv[2], &num_tgt_chunks, &tgt_chunks) == NULL) {
853 printf("failed to break apart target image\n");
854 return 1;
855 }
856
857 // Verify that the source and target images have the same chunk
858 // structure (ie, the same sequence of deflate and normal chunks).
859
860 if (!zip_mode) {
861 // Merge the gzip header and footer in with any adjacent
862 // normal chunks.
863 MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
864 MergeAdjacentNormalChunks(src_chunks, &num_src_chunks);
865 }
866
867 if (num_src_chunks != num_tgt_chunks) {
868 printf("source and target don't have same number of chunks!\n");
869 printf("source chunks:\n");
870 DumpChunks(src_chunks, num_src_chunks);
871 printf("target chunks:\n");
872 DumpChunks(tgt_chunks, num_tgt_chunks);
873 return 1;
874 }
875 for (i = 0; i < num_src_chunks; ++i) {
876 if (src_chunks[i].type != tgt_chunks[i].type) {
877 printf("source and target don't have same chunk "
878 "structure! (chunk %d)\n", i);
879 printf("source chunks:\n");
880 DumpChunks(src_chunks, num_src_chunks);
881 printf("target chunks:\n");
882 DumpChunks(tgt_chunks, num_tgt_chunks);
883 return 1;
884 }
885 }
886 }
887
888 for (i = 0; i < num_tgt_chunks; ++i) {
889 if (tgt_chunks[i].type == CHUNK_DEFLATE) {
890 // Confirm that given the uncompressed chunk data in the target, we
891 // can recompress it and get exactly the same bits as are in the
892 // input target image. If this fails, treat the chunk as a normal
893 // non-deflated chunk.
894 if (ReconstructDeflateChunk(tgt_chunks+i) < 0) {
895 printf("failed to reconstruct target deflate chunk %d [%s]; "
896 "treating as normal\n", i, tgt_chunks[i].filename);
897 ChangeDeflateChunkToNormal(tgt_chunks+i);
898 if (zip_mode) {
899 ImageChunk* src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks);
900 if (src) {
901 ChangeDeflateChunkToNormal(src);
902 }
903 } else {
904 ChangeDeflateChunkToNormal(src_chunks+i);
905 }
906 continue;
907 }
908
909 // If two deflate chunks are identical (eg, the kernel has not
910 // changed between two builds), treat them as normal chunks.
911 // This makes applypatch much faster -- it can apply a trivial
912 // patch to the compressed data, rather than uncompressing and
913 // recompressing to apply the trivial patch to the uncompressed
914 // data.
915 ImageChunk* src;
916 if (zip_mode) {
917 src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks);
918 } else {
919 src = src_chunks+i;
920 }
921
922 if (src == NULL || AreChunksEqual(tgt_chunks+i, src)) {
923 ChangeDeflateChunkToNormal(tgt_chunks+i);
924 if (src) {
925 ChangeDeflateChunkToNormal(src);
926 }
927 }
928 }
929 }
930
931 // Merging neighboring normal chunks.
932 if (zip_mode) {
933 // For zips, we only need to do this to the target: deflated
934 // chunks are matched via filename, and normal chunks are patched
935 // using the entire source file as the source.
936 MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
937 } else {
938 // For images, we need to maintain the parallel structure of the
939 // chunk lists, so do the merging in both the source and target
940 // lists.
941 MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
942 MergeAdjacentNormalChunks(src_chunks, &num_src_chunks);
943 if (num_src_chunks != num_tgt_chunks) {
944 // This shouldn't happen.
945 printf("merging normal chunks went awry\n");
946 return 1;
947 }
948 }
949
950 // Compute bsdiff patches for each chunk's data (the uncompressed
951 // data, in the case of deflate chunks).
952
Doug Zongkera3ccba62012-08-20 15:28:02 -0700953 DumpChunks(src_chunks, num_src_chunks);
954
Doug Zongker512536a2010-02-17 16:11:44 -0800955 printf("Construct patches for %d chunks...\n", num_tgt_chunks);
Tao Baoba9a42a2015-06-23 23:23:33 -0700956 unsigned char** patch_data = reinterpret_cast<unsigned char**>(malloc(
957 num_tgt_chunks * sizeof(unsigned char*)));
958 size_t* patch_size = reinterpret_cast<size_t*>(malloc(num_tgt_chunks * sizeof(size_t)));
Doug Zongker512536a2010-02-17 16:11:44 -0800959 for (i = 0; i < num_tgt_chunks; ++i) {
960 if (zip_mode) {
961 ImageChunk* src;
962 if (tgt_chunks[i].type == CHUNK_DEFLATE &&
963 (src = FindChunkByName(tgt_chunks[i].filename, src_chunks,
964 num_src_chunks))) {
965 patch_data[i] = MakePatch(src, tgt_chunks+i, patch_size+i);
966 } else {
967 patch_data[i] = MakePatch(src_chunks, tgt_chunks+i, patch_size+i);
968 }
969 } else {
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700970 if (i == 1 && !bonus_data.empty()) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700971 printf(" using %zu bytes of bonus data for chunk %d\n", bonus_size, i);
972 src_chunks[i].data = reinterpret_cast<unsigned char*>(realloc(src_chunks[i].data,
973 src_chunks[i].len + bonus_size));
Adam Buchbinder51dc9442016-05-20 16:45:37 -0700974 memcpy(src_chunks[i].data+src_chunks[i].len, bonus_data.data(), bonus_size);
Doug Zongkera3ccba62012-08-20 15:28:02 -0700975 src_chunks[i].len += bonus_size;
976 }
977
Doug Zongker512536a2010-02-17 16:11:44 -0800978 patch_data[i] = MakePatch(src_chunks+i, tgt_chunks+i, patch_size+i);
979 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700980 printf("patch %3d is %zu bytes (of %zu)\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800981 i, patch_size[i], tgt_chunks[i].source_len);
982 }
983
984 // Figure out how big the imgdiff file header is going to be, so
985 // that we can correctly compute the offset of each bsdiff patch
986 // within the file.
987
988 size_t total_header_size = 12;
989 for (i = 0; i < num_tgt_chunks; ++i) {
990 total_header_size += 4;
991 switch (tgt_chunks[i].type) {
992 case CHUNK_NORMAL:
993 total_header_size += 8*3;
994 break;
995 case CHUNK_DEFLATE:
996 total_header_size += 8*5 + 4*5;
997 break;
998 case CHUNK_RAW:
999 total_header_size += 4 + patch_size[i];
1000 break;
1001 }
1002 }
1003
1004 size_t offset = total_header_size;
1005
1006 FILE* f = fopen(argv[3], "wb");
1007
1008 // Write out the headers.
1009
1010 fwrite("IMGDIFF2", 1, 8, f);
1011 Write4(num_tgt_chunks, f);
1012 for (i = 0; i < num_tgt_chunks; ++i) {
1013 Write4(tgt_chunks[i].type, f);
1014
1015 switch (tgt_chunks[i].type) {
1016 case CHUNK_NORMAL:
Tao Baoba9a42a2015-06-23 23:23:33 -07001017 printf("chunk %3d: normal (%10zu, %10zu) %10zu\n", i,
Doug Zongker512536a2010-02-17 16:11:44 -08001018 tgt_chunks[i].start, tgt_chunks[i].len, patch_size[i]);
1019 Write8(tgt_chunks[i].source_start, f);
1020 Write8(tgt_chunks[i].source_len, f);
1021 Write8(offset, f);
1022 offset += patch_size[i];
1023 break;
1024
1025 case CHUNK_DEFLATE:
Tao Baoba9a42a2015-06-23 23:23:33 -07001026 printf("chunk %3d: deflate (%10zu, %10zu) %10zu %s\n", i,
Doug Zongker512536a2010-02-17 16:11:44 -08001027 tgt_chunks[i].start, tgt_chunks[i].deflate_len, patch_size[i],
1028 tgt_chunks[i].filename);
1029 Write8(tgt_chunks[i].source_start, f);
1030 Write8(tgt_chunks[i].source_len, f);
1031 Write8(offset, f);
1032 Write8(tgt_chunks[i].source_uncompressed_len, f);
1033 Write8(tgt_chunks[i].len, f);
1034 Write4(tgt_chunks[i].level, f);
1035 Write4(tgt_chunks[i].method, f);
1036 Write4(tgt_chunks[i].windowBits, f);
1037 Write4(tgt_chunks[i].memLevel, f);
1038 Write4(tgt_chunks[i].strategy, f);
1039 offset += patch_size[i];
1040 break;
1041
1042 case CHUNK_RAW:
Tao Baoba9a42a2015-06-23 23:23:33 -07001043 printf("chunk %3d: raw (%10zu, %10zu)\n", i,
Doug Zongker512536a2010-02-17 16:11:44 -08001044 tgt_chunks[i].start, tgt_chunks[i].len);
1045 Write4(patch_size[i], f);
1046 fwrite(patch_data[i], 1, patch_size[i], f);
1047 break;
1048 }
1049 }
1050
1051 // Append each chunk's bsdiff patch, in order.
1052
1053 for (i = 0; i < num_tgt_chunks; ++i) {
1054 if (tgt_chunks[i].type != CHUNK_RAW) {
1055 fwrite(patch_data[i], 1, patch_size[i], f);
1056 }
1057 }
1058
1059 fclose(f);
Adam Buchbinder51dc9442016-05-20 16:45:37 -07001060 for (i = 0; i < num_tgt_chunks; ++i) {
1061 free(patch_data[i]);
1062 }
1063 free(patch_data);
Doug Zongker512536a2010-02-17 16:11:44 -08001064
1065 return 0;
1066}