blob: 7d6ebab6e40f4ffc5f178a7287efbebfcb520995 [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
Sen Jiang2fffcb12016-05-03 15:49:10 -0700133#include <bsdiff.h>
134
Doug Zongker512536a2010-02-17 16:11:44 -0800135#include "zlib.h"
136#include "imgdiff.h"
137#include "utils.h"
138
139typedef struct {
140 int type; // CHUNK_NORMAL, CHUNK_DEFLATE
141 size_t start; // offset of chunk in original image file
142
143 size_t len;
144 unsigned char* data; // data to be patched (uncompressed, for deflate chunks)
145
146 size_t source_start;
147 size_t source_len;
148
Doug Zongker512536a2010-02-17 16:11:44 -0800149 // --- for CHUNK_DEFLATE chunks only: ---
150
151 // original (compressed) deflate data
152 size_t deflate_len;
153 unsigned char* deflate_data;
154
155 char* filename; // used for zip entries
156
157 // deflate encoder parameters
158 int level, method, windowBits, memLevel, strategy;
159
160 size_t source_uncompressed_len;
161} ImageChunk;
162
163typedef struct {
164 int data_offset;
165 int deflate_len;
166 int uncomp_len;
167 char* filename;
168} ZipFileEntry;
169
Tao Baoa0c40112016-06-01 13:15:44 -0700170static int fileentry_compare(const void* a, const void* b) {
171 int ao = ((ZipFileEntry*)a)->data_offset;
172 int bo = ((ZipFileEntry*)b)->data_offset;
173 if (ao < bo) {
174 return -1;
175 } else if (ao > bo) {
176 return 1;
177 } else {
178 return 0;
179 }
Doug Zongker512536a2010-02-17 16:11:44 -0800180}
181
Doug Zongker512536a2010-02-17 16:11:44 -0800182unsigned char* ReadZip(const char* filename,
183 int* num_chunks, ImageChunk** chunks,
184 int include_pseudo_chunk) {
185 struct stat st;
186 if (stat(filename, &st) != 0) {
187 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
188 return NULL;
189 }
190
Tao Baoba9a42a2015-06-23 23:23:33 -0700191 size_t sz = static_cast<size_t>(st.st_size);
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800192 unsigned char* img = static_cast<unsigned char*>(malloc(sz));
Doug Zongker512536a2010-02-17 16:11:44 -0800193 FILE* f = fopen(filename, "rb");
Tao Baoa0c40112016-06-01 13:15:44 -0700194 if (fread(img, 1, sz, f) != sz) {
Doug Zongker512536a2010-02-17 16:11:44 -0800195 printf("failed to read \"%s\" %s\n", filename, strerror(errno));
196 fclose(f);
Rahul Chaudhry8b640ff2016-12-06 15:10:41 -0800197 free(img);
Doug Zongker512536a2010-02-17 16:11:44 -0800198 return NULL;
199 }
200 fclose(f);
201
202 // look for the end-of-central-directory record.
203
204 int i;
205 for (i = st.st_size-20; i >= 0 && i > st.st_size - 65600; --i) {
206 if (img[i] == 0x50 && img[i+1] == 0x4b &&
207 img[i+2] == 0x05 && img[i+3] == 0x06) {
208 break;
209 }
210 }
211 // double-check: this archive consists of a single "disk"
212 if (!(img[i+4] == 0 && img[i+5] == 0 && img[i+6] == 0 && img[i+7] == 0)) {
213 printf("can't process multi-disk archive\n");
214 return NULL;
215 }
216
Tao Baoa0c40112016-06-01 13:15:44 -0700217 int cdcount = Read2(img+i+8);
218 int cdoffset = Read4(img+i+16);
Doug Zongker512536a2010-02-17 16:11:44 -0800219
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800220 ZipFileEntry* temp_entries = static_cast<ZipFileEntry*>(malloc(
Tao Baoa0c40112016-06-01 13:15:44 -0700221 cdcount * sizeof(ZipFileEntry)));
Doug Zongker512536a2010-02-17 16:11:44 -0800222 int entrycount = 0;
223
Tao Baoa0c40112016-06-01 13:15:44 -0700224 unsigned char* cd = img+cdoffset;
Doug Zongker512536a2010-02-17 16:11:44 -0800225 for (i = 0; i < cdcount; ++i) {
226 if (!(cd[0] == 0x50 && cd[1] == 0x4b && cd[2] == 0x01 && cd[3] == 0x02)) {
227 printf("bad central directory entry %d\n", i);
Rahul Chaudhry3a5177b2016-11-15 16:18:46 -0800228 free(temp_entries);
Doug Zongker512536a2010-02-17 16:11:44 -0800229 return NULL;
230 }
231
232 int clen = Read4(cd+20); // compressed len
233 int ulen = Read4(cd+24); // uncompressed len
234 int nlen = Read2(cd+28); // filename len
235 int xlen = Read2(cd+30); // extra field len
236 int mlen = Read2(cd+32); // file comment len
237 int hoffset = Read4(cd+42); // local header offset
238
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800239 char* filename = static_cast<char*>(malloc(nlen+1));
Doug Zongker512536a2010-02-17 16:11:44 -0800240 memcpy(filename, cd+46, nlen);
241 filename[nlen] = '\0';
242
243 int method = Read2(cd+10);
244
245 cd += 46 + nlen + xlen + mlen;
246
247 if (method != 8) { // 8 == deflate
248 free(filename);
249 continue;
250 }
251
Tao Baoa0c40112016-06-01 13:15:44 -0700252 unsigned char* lh = img + hoffset;
Doug Zongker512536a2010-02-17 16:11:44 -0800253
254 if (!(lh[0] == 0x50 && lh[1] == 0x4b && lh[2] == 0x03 && lh[3] == 0x04)) {
255 printf("bad local file header entry %d\n", i);
256 return NULL;
257 }
258
259 if (Read2(lh+26) != nlen || memcmp(lh+30, filename, nlen) != 0) {
260 printf("central dir filename doesn't match local header\n");
261 return NULL;
262 }
263
264 xlen = Read2(lh+28); // extra field len; might be different from CD entry?
265
266 temp_entries[entrycount].data_offset = hoffset+30+nlen+xlen;
267 temp_entries[entrycount].deflate_len = clen;
268 temp_entries[entrycount].uncomp_len = ulen;
269 temp_entries[entrycount].filename = filename;
270 ++entrycount;
271 }
272
Tao Baoa0c40112016-06-01 13:15:44 -0700273 qsort(temp_entries, entrycount, sizeof(ZipFileEntry), fileentry_compare);
Doug Zongker512536a2010-02-17 16:11:44 -0800274
275#if 0
276 printf("found %d deflated entries\n", entrycount);
277 for (i = 0; i < entrycount; ++i) {
278 printf("off %10d len %10d unlen %10d %p %s\n",
279 temp_entries[i].data_offset,
280 temp_entries[i].deflate_len,
281 temp_entries[i].uncomp_len,
282 temp_entries[i].filename,
283 temp_entries[i].filename);
284 }
285#endif
286
287 *num_chunks = 0;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800288 *chunks = static_cast<ImageChunk*>(malloc((entrycount*2+2) * sizeof(ImageChunk)));
Doug Zongker512536a2010-02-17 16:11:44 -0800289 ImageChunk* curr = *chunks;
290
291 if (include_pseudo_chunk) {
292 curr->type = CHUNK_NORMAL;
293 curr->start = 0;
294 curr->len = st.st_size;
Tao Baoa0c40112016-06-01 13:15:44 -0700295 curr->data = img;
Doug Zongker512536a2010-02-17 16:11:44 -0800296 curr->filename = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800297 ++curr;
298 ++*num_chunks;
299 }
300
301 int pos = 0;
302 int nextentry = 0;
303
304 while (pos < st.st_size) {
305 if (nextentry < entrycount && pos == temp_entries[nextentry].data_offset) {
306 curr->type = CHUNK_DEFLATE;
307 curr->start = pos;
308 curr->deflate_len = temp_entries[nextentry].deflate_len;
Tao Baoa0c40112016-06-01 13:15:44 -0700309 curr->deflate_data = img + pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800310 curr->filename = temp_entries[nextentry].filename;
Doug Zongker512536a2010-02-17 16:11:44 -0800311
312 curr->len = temp_entries[nextentry].uncomp_len;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800313 curr->data = static_cast<unsigned char*>(malloc(curr->len));
Doug Zongker512536a2010-02-17 16:11:44 -0800314
315 z_stream strm;
316 strm.zalloc = Z_NULL;
317 strm.zfree = Z_NULL;
318 strm.opaque = Z_NULL;
319 strm.avail_in = curr->deflate_len;
320 strm.next_in = curr->deflate_data;
321
322 // -15 means we are decoding a 'raw' deflate stream; zlib will
323 // not expect zlib headers.
324 int ret = inflateInit2(&strm, -15);
Rahul Chaudhrya793c582016-11-29 17:10:14 -0800325 if (ret < 0) {
326 printf("failed to initialize inflate: %d\n", ret);
327 return NULL;
328 }
Doug Zongker512536a2010-02-17 16:11:44 -0800329
330 strm.avail_out = curr->len;
331 strm.next_out = curr->data;
332 ret = inflate(&strm, Z_NO_FLUSH);
333 if (ret != Z_STREAM_END) {
334 printf("failed to inflate \"%s\"; %d\n", curr->filename, ret);
335 return NULL;
336 }
337
338 inflateEnd(&strm);
339
340 pos += curr->deflate_len;
341 ++nextentry;
342 ++*num_chunks;
343 ++curr;
344 continue;
345 }
346
347 // use a normal chunk to take all the data up to the start of the
348 // next deflate section.
349
350 curr->type = CHUNK_NORMAL;
351 curr->start = pos;
352 if (nextentry < entrycount) {
353 curr->len = temp_entries[nextentry].data_offset - pos;
354 } else {
355 curr->len = st.st_size - pos;
356 }
Tao Baoa0c40112016-06-01 13:15:44 -0700357 curr->data = img + pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800358 curr->filename = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800359 pos += curr->len;
360
361 ++*num_chunks;
362 ++curr;
363 }
364
Tao Baoa0c40112016-06-01 13:15:44 -0700365 free(temp_entries);
366 return img;
Doug Zongker512536a2010-02-17 16:11:44 -0800367}
368
369/*
370 * Read the given file and break it up into chunks, putting the number
371 * of chunks and their info in *num_chunks and **chunks,
372 * respectively. Returns a malloc'd block of memory containing the
373 * contents of the file; various pointers in the output chunk array
374 * will point into this block of memory. The caller should free the
375 * return value when done with all the chunks. Returns NULL on
376 * failure.
377 */
378unsigned char* ReadImage(const char* filename,
379 int* num_chunks, ImageChunk** chunks) {
380 struct stat st;
381 if (stat(filename, &st) != 0) {
382 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
383 return NULL;
384 }
385
Tao Baoba9a42a2015-06-23 23:23:33 -0700386 size_t sz = static_cast<size_t>(st.st_size);
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800387 unsigned char* img = static_cast<unsigned char*>(malloc(sz + 4));
Doug Zongker512536a2010-02-17 16:11:44 -0800388 FILE* f = fopen(filename, "rb");
Tao Baoa0c40112016-06-01 13:15:44 -0700389 if (fread(img, 1, sz, f) != sz) {
Doug Zongker512536a2010-02-17 16:11:44 -0800390 printf("failed to read \"%s\" %s\n", filename, strerror(errno));
391 fclose(f);
392 return NULL;
393 }
394 fclose(f);
395
396 // append 4 zero bytes to the data so we can always search for the
397 // four-byte string 1f8b0800 starting at any point in the actual
398 // file data, without special-casing the end of the data.
Tao Baoa0c40112016-06-01 13:15:44 -0700399 memset(img+sz, 0, 4);
Doug Zongker512536a2010-02-17 16:11:44 -0800400
401 size_t pos = 0;
402
403 *num_chunks = 0;
404 *chunks = NULL;
405
Tao Baoba9a42a2015-06-23 23:23:33 -0700406 while (pos < sz) {
Tao Baoa0c40112016-06-01 13:15:44 -0700407 unsigned char* p = img+pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800408
Tao Baoba9a42a2015-06-23 23:23:33 -0700409 if (sz - pos >= 4 &&
Doug Zongker512536a2010-02-17 16:11:44 -0800410 p[0] == 0x1f && p[1] == 0x8b &&
411 p[2] == 0x08 && // deflate compression
412 p[3] == 0x00) { // no header flags
413 // 'pos' is the offset of the start of a gzip chunk.
Johan Redestigc68bd342015-04-14 21:20:06 +0200414 size_t chunk_offset = pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800415
416 *num_chunks += 3;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800417 *chunks = static_cast<ImageChunk*>(realloc(*chunks,
Tao Baoba9a42a2015-06-23 23:23:33 -0700418 *num_chunks * sizeof(ImageChunk)));
Doug Zongker512536a2010-02-17 16:11:44 -0800419 ImageChunk* curr = *chunks + (*num_chunks-3);
420
421 // create a normal chunk for the header.
422 curr->start = pos;
423 curr->type = CHUNK_NORMAL;
424 curr->len = GZIP_HEADER_LEN;
425 curr->data = p;
Doug Zongker512536a2010-02-17 16:11:44 -0800426
427 pos += curr->len;
428 p += curr->len;
429 ++curr;
430
431 curr->type = CHUNK_DEFLATE;
432 curr->filename = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800433
434 // We must decompress this chunk in order to discover where it
435 // ends, and so we can put the uncompressed data and its length
436 // into curr->data and curr->len.
437
438 size_t allocated = 32768;
439 curr->len = 0;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800440 curr->data = static_cast<unsigned char*>(malloc(allocated));
Doug Zongker512536a2010-02-17 16:11:44 -0800441 curr->start = pos;
442 curr->deflate_data = p;
443
444 z_stream strm;
445 strm.zalloc = Z_NULL;
446 strm.zfree = Z_NULL;
447 strm.opaque = Z_NULL;
Tao Baoba9a42a2015-06-23 23:23:33 -0700448 strm.avail_in = sz - pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800449 strm.next_in = p;
450
451 // -15 means we are decoding a 'raw' deflate stream; zlib will
452 // not expect zlib headers.
453 int ret = inflateInit2(&strm, -15);
Rahul Chaudhrya793c582016-11-29 17:10:14 -0800454 if (ret < 0) {
455 printf("failed to initialize inflate: %d\n", ret);
456 return NULL;
457 }
Doug Zongker512536a2010-02-17 16:11:44 -0800458
459 do {
460 strm.avail_out = allocated - curr->len;
461 strm.next_out = curr->data + curr->len;
462 ret = inflate(&strm, Z_NO_FLUSH);
Johan Redestigc68bd342015-04-14 21:20:06 +0200463 if (ret < 0) {
Sen Jiangfa4f1b72016-02-11 16:14:23 -0800464 printf("Warning: inflate failed [%s] at offset [%zu],"
465 " treating as a normal chunk\n",
David Riley0779fc92015-12-10 10:18:25 -0800466 strm.msg, chunk_offset);
Sen Jiangfa4f1b72016-02-11 16:14:23 -0800467 break;
Johan Redestigc68bd342015-04-14 21:20:06 +0200468 }
Doug Zongker512536a2010-02-17 16:11:44 -0800469 curr->len = allocated - strm.avail_out;
470 if (strm.avail_out == 0) {
471 allocated *= 2;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800472 curr->data = static_cast<unsigned char*>(realloc(curr->data, allocated));
Doug Zongker512536a2010-02-17 16:11:44 -0800473 }
474 } while (ret != Z_STREAM_END);
475
Tao Baoba9a42a2015-06-23 23:23:33 -0700476 curr->deflate_len = sz - strm.avail_in - pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800477 inflateEnd(&strm);
Sen Jiangfa4f1b72016-02-11 16:14:23 -0800478
479 if (ret < 0) {
480 free(curr->data);
481 *num_chunks -= 2;
482 continue;
483 }
484
Doug Zongker512536a2010-02-17 16:11:44 -0800485 pos += curr->deflate_len;
486 p += curr->deflate_len;
487 ++curr;
488
489 // create a normal chunk for the footer
490
491 curr->type = CHUNK_NORMAL;
492 curr->start = pos;
493 curr->len = GZIP_FOOTER_LEN;
Tao Baoa0c40112016-06-01 13:15:44 -0700494 curr->data = img+pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800495
496 pos += curr->len;
497 p += curr->len;
498 ++curr;
499
500 // The footer (that we just skipped over) contains the size of
501 // the uncompressed data. Double-check to make sure that it
502 // matches the size of the data we got when we actually did
503 // the decompression.
504 size_t footer_size = Read4(p-4);
505 if (footer_size != curr[-2].len) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700506 printf("Error: footer size %zu != decompressed size %zu\n",
507 footer_size, curr[-2].len);
Tao Baoa0c40112016-06-01 13:15:44 -0700508 free(img);
Doug Zongker512536a2010-02-17 16:11:44 -0800509 return NULL;
510 }
511 } else {
512 // Reallocate the list for every chunk; we expect the number of
513 // chunks to be small (5 for typical boot and recovery images).
514 ++*num_chunks;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800515 *chunks = static_cast<ImageChunk*>(realloc(*chunks, *num_chunks * sizeof(ImageChunk)));
Doug Zongker512536a2010-02-17 16:11:44 -0800516 ImageChunk* curr = *chunks + (*num_chunks-1);
517 curr->start = pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800518
519 // 'pos' is not the offset of the start of a gzip chunk, so scan
520 // forward until we find a gzip header.
521 curr->type = CHUNK_NORMAL;
522 curr->data = p;
523
Tao Baoba9a42a2015-06-23 23:23:33 -0700524 for (curr->len = 0; curr->len < (sz - pos); ++curr->len) {
Doug Zongker512536a2010-02-17 16:11:44 -0800525 if (p[curr->len] == 0x1f &&
526 p[curr->len+1] == 0x8b &&
527 p[curr->len+2] == 0x08 &&
528 p[curr->len+3] == 0x00) {
529 break;
530 }
531 }
532 pos += curr->len;
533 }
534 }
535
Tao Baoa0c40112016-06-01 13:15:44 -0700536 return img;
Doug Zongker512536a2010-02-17 16:11:44 -0800537}
538
539#define BUFFER_SIZE 32768
540
541/*
542 * Takes the uncompressed data stored in the chunk, compresses it
543 * using the zlib parameters stored in the chunk, and checks that it
544 * matches exactly the compressed data we started with (also stored in
545 * the chunk). Return 0 on success.
546 */
547int TryReconstruction(ImageChunk* chunk, unsigned char* out) {
548 size_t p = 0;
549
550#if 0
551 printf("trying %d %d %d %d %d\n",
552 chunk->level, chunk->method, chunk->windowBits,
553 chunk->memLevel, chunk->strategy);
554#endif
555
556 z_stream strm;
557 strm.zalloc = Z_NULL;
558 strm.zfree = Z_NULL;
559 strm.opaque = Z_NULL;
560 strm.avail_in = chunk->len;
561 strm.next_in = chunk->data;
562 int ret;
563 ret = deflateInit2(&strm, chunk->level, chunk->method, chunk->windowBits,
564 chunk->memLevel, chunk->strategy);
Rahul Chaudhrya793c582016-11-29 17:10:14 -0800565 if (ret < 0) {
566 printf("failed to initialize deflate: %d\n", ret);
567 return -1;
568 }
Doug Zongker512536a2010-02-17 16:11:44 -0800569 do {
570 strm.avail_out = BUFFER_SIZE;
571 strm.next_out = out;
572 ret = deflate(&strm, Z_FINISH);
Rahul Chaudhrya793c582016-11-29 17:10:14 -0800573 if (ret < 0) {
574 printf("failed to deflate: %d\n", ret);
575 return -1;
576 }
Doug Zongker512536a2010-02-17 16:11:44 -0800577 size_t have = BUFFER_SIZE - strm.avail_out;
578
579 if (memcmp(out, chunk->deflate_data+p, have) != 0) {
580 // mismatch; data isn't the same.
581 deflateEnd(&strm);
582 return -1;
583 }
584 p += have;
585 } while (ret != Z_STREAM_END);
586 deflateEnd(&strm);
587 if (p != chunk->deflate_len) {
588 // mismatch; ran out of data before we should have.
589 return -1;
590 }
591 return 0;
592}
593
594/*
595 * Verify that we can reproduce exactly the same compressed data that
596 * we started with. Sets the level, method, windowBits, memLevel, and
597 * strategy fields in the chunk to the encoding parameters needed to
598 * produce the right output. Returns 0 on success.
599 */
600int ReconstructDeflateChunk(ImageChunk* chunk) {
601 if (chunk->type != CHUNK_DEFLATE) {
602 printf("attempt to reconstruct non-deflate chunk\n");
603 return -1;
604 }
605
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800606 unsigned char* out = static_cast<unsigned char*>(malloc(BUFFER_SIZE));
Doug Zongker512536a2010-02-17 16:11:44 -0800607
608 // We only check two combinations of encoder parameters: level 6
609 // (the default) and level 9 (the maximum).
610 for (chunk->level = 6; chunk->level <= 9; chunk->level += 3) {
611 chunk->windowBits = -15; // 32kb window; negative to indicate a raw stream.
612 chunk->memLevel = 8; // the default value.
613 chunk->method = Z_DEFLATED;
614 chunk->strategy = Z_DEFAULT_STRATEGY;
615
616 if (TryReconstruction(chunk, out) == 0) {
617 free(out);
618 return 0;
619 }
620 }
621
622 free(out);
623 return -1;
624}
625
626/*
627 * Given source and target chunks, compute a bsdiff patch between them
628 * by running bsdiff in a subprocess. Return the patch data, placing
629 * its length in *size. Return NULL on failure. We expect the bsdiff
630 * program to be in the path.
631 */
632unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) {
633 if (tgt->type == CHUNK_NORMAL) {
634 if (tgt->len <= 160) {
635 tgt->type = CHUNK_RAW;
636 *size = tgt->len;
637 return tgt->data;
638 }
639 }
640
641 char ptemp[] = "/tmp/imgdiff-patch-XXXXXX";
Jeremy Compostellaa91c66d2015-09-08 19:15:09 +0200642 int fd = mkstemp(ptemp);
643
644 if (fd == -1) {
645 printf("MakePatch failed to create a temporary file: %s\n",
646 strerror(errno));
647 return NULL;
648 }
649 close(fd); // temporary file is created and we don't need its file
650 // descriptor
Doug Zongker512536a2010-02-17 16:11:44 -0800651
Sen Jiang2fffcb12016-05-03 15:49:10 -0700652 int r = bsdiff::bsdiff(src->data, src->len, tgt->data, tgt->len, ptemp);
Doug Zongker512536a2010-02-17 16:11:44 -0800653 if (r != 0) {
654 printf("bsdiff() failed: %d\n", r);
655 return NULL;
656 }
657
658 struct stat st;
659 if (stat(ptemp, &st) != 0) {
660 printf("failed to stat patch file %s: %s\n",
661 ptemp, strerror(errno));
662 return NULL;
663 }
664
Tao Baoba9a42a2015-06-23 23:23:33 -0700665 size_t sz = static_cast<size_t>(st.st_size);
Tao Baoa0c40112016-06-01 13:15:44 -0700666 // TODO: Memory leak on error return.
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800667 unsigned char* data = static_cast<unsigned char*>(malloc(sz));
Doug Zongker512536a2010-02-17 16:11:44 -0800668
Tao Baoba9a42a2015-06-23 23:23:33 -0700669 if (tgt->type == CHUNK_NORMAL && tgt->len <= sz) {
Doug Zongker512536a2010-02-17 16:11:44 -0800670 unlink(ptemp);
671
672 tgt->type = CHUNK_RAW;
673 *size = tgt->len;
674 return tgt->data;
675 }
676
Tao Baoba9a42a2015-06-23 23:23:33 -0700677 *size = sz;
Doug Zongker512536a2010-02-17 16:11:44 -0800678
679 FILE* f = fopen(ptemp, "rb");
680 if (f == NULL) {
681 printf("failed to open patch %s: %s\n", ptemp, strerror(errno));
682 return NULL;
683 }
Tao Baoa0c40112016-06-01 13:15:44 -0700684 if (fread(data, 1, sz, f) != sz) {
Doug Zongker512536a2010-02-17 16:11:44 -0800685 printf("failed to read patch %s: %s\n", ptemp, strerror(errno));
686 return NULL;
687 }
688 fclose(f);
689
690 unlink(ptemp);
691
692 tgt->source_start = src->start;
693 switch (tgt->type) {
694 case CHUNK_NORMAL:
695 tgt->source_len = src->len;
696 break;
697 case CHUNK_DEFLATE:
698 tgt->source_len = src->deflate_len;
699 tgt->source_uncompressed_len = src->len;
700 break;
701 }
702
Tao Baoa0c40112016-06-01 13:15:44 -0700703 return data;
Doug Zongker512536a2010-02-17 16:11:44 -0800704}
705
706/*
707 * Cause a gzip chunk to be treated as a normal chunk (ie, as a blob
708 * of uninterpreted data). The resulting patch will likely be about
709 * as big as the target file, but it lets us handle the case of images
710 * where some gzip chunks are reconstructible but others aren't (by
711 * treating the ones that aren't as normal chunks).
712 */
713void ChangeDeflateChunkToNormal(ImageChunk* ch) {
714 if (ch->type != CHUNK_DEFLATE) return;
715 ch->type = CHUNK_NORMAL;
716 free(ch->data);
717 ch->data = ch->deflate_data;
718 ch->len = ch->deflate_len;
719}
720
721/*
722 * Return true if the data in the chunk is identical (including the
723 * compressed representation, for gzip chunks).
724 */
725int AreChunksEqual(ImageChunk* a, ImageChunk* b) {
726 if (a->type != b->type) return 0;
727
728 switch (a->type) {
729 case CHUNK_NORMAL:
730 return a->len == b->len && memcmp(a->data, b->data, a->len) == 0;
731
732 case CHUNK_DEFLATE:
733 return a->deflate_len == b->deflate_len &&
734 memcmp(a->deflate_data, b->deflate_data, a->deflate_len) == 0;
735
736 default:
737 printf("unknown chunk type %d\n", a->type);
738 return 0;
739 }
740}
741
742/*
743 * Look for runs of adjacent normal chunks and compress them down into
744 * a single chunk. (Such runs can be produced when deflate chunks are
745 * changed to normal chunks.)
746 */
747void MergeAdjacentNormalChunks(ImageChunk* chunks, int* num_chunks) {
748 int out = 0;
749 int in_start = 0, in_end;
750 while (in_start < *num_chunks) {
751 if (chunks[in_start].type != CHUNK_NORMAL) {
752 in_end = in_start+1;
753 } else {
754 // in_start is a normal chunk. Look for a run of normal chunks
755 // that constitute a solid block of data (ie, each chunk begins
756 // where the previous one ended).
757 for (in_end = in_start+1;
758 in_end < *num_chunks && chunks[in_end].type == CHUNK_NORMAL &&
759 (chunks[in_end].start ==
760 chunks[in_end-1].start + chunks[in_end-1].len &&
761 chunks[in_end].data ==
762 chunks[in_end-1].data + chunks[in_end-1].len);
763 ++in_end);
764 }
765
766 if (in_end == in_start+1) {
767#if 0
768 printf("chunk %d is now %d\n", in_start, out);
769#endif
770 if (out != in_start) {
771 memcpy(chunks+out, chunks+in_start, sizeof(ImageChunk));
772 }
773 } else {
774#if 0
775 printf("collapse normal chunks %d-%d into %d\n", in_start, in_end-1, out);
776#endif
777
778 // Merge chunks [in_start, in_end-1] into one chunk. Since the
779 // data member of each chunk is just a pointer into an in-memory
780 // copy of the file, this can be done without recopying (the
781 // output chunk has the first chunk's start location and data
782 // pointer, and length equal to the sum of the input chunk
783 // lengths).
784 chunks[out].type = CHUNK_NORMAL;
785 chunks[out].start = chunks[in_start].start;
786 chunks[out].data = chunks[in_start].data;
787 chunks[out].len = chunks[in_end-1].len +
788 (chunks[in_end-1].start - chunks[in_start].start);
789 }
790
791 ++out;
792 in_start = in_end;
793 }
794 *num_chunks = out;
795}
796
797ImageChunk* FindChunkByName(const char* name,
798 ImageChunk* chunks, int num_chunks) {
799 int i;
800 for (i = 0; i < num_chunks; ++i) {
801 if (chunks[i].type == CHUNK_DEFLATE && chunks[i].filename &&
802 strcmp(name, chunks[i].filename) == 0) {
803 return chunks+i;
804 }
805 }
806 return NULL;
807}
808
809void DumpChunks(ImageChunk* chunks, int num_chunks) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700810 for (int i = 0; i < num_chunks; ++i) {
811 printf("chunk %d: type %d start %zu len %zu\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800812 i, chunks[i].type, chunks[i].start, chunks[i].len);
813 }
814}
815
816int main(int argc, char** argv) {
Doug Zongker512536a2010-02-17 16:11:44 -0800817 int zip_mode = 0;
818
Doug Zongkera3ccba62012-08-20 15:28:02 -0700819 if (argc >= 2 && strcmp(argv[1], "-z") == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -0800820 zip_mode = 1;
821 --argc;
822 ++argv;
823 }
824
Doug Zongkera3ccba62012-08-20 15:28:02 -0700825 size_t bonus_size = 0;
Tao Baoa0c40112016-06-01 13:15:44 -0700826 unsigned char* bonus_data = NULL;
Doug Zongkera3ccba62012-08-20 15:28:02 -0700827 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
828 struct stat st;
829 if (stat(argv[2], &st) != 0) {
830 printf("failed to stat bonus file %s: %s\n", argv[2], strerror(errno));
831 return 1;
832 }
833 bonus_size = st.st_size;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800834 bonus_data = static_cast<unsigned char*>(malloc(bonus_size));
Doug Zongkera3ccba62012-08-20 15:28:02 -0700835 FILE* f = fopen(argv[2], "rb");
836 if (f == NULL) {
837 printf("failed to open bonus file %s: %s\n", argv[2], strerror(errno));
838 return 1;
839 }
Tao Baoa0c40112016-06-01 13:15:44 -0700840 if (fread(bonus_data, 1, bonus_size, f) != bonus_size) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700841 printf("failed to read bonus file %s: %s\n", argv[2], strerror(errno));
842 return 1;
843 }
844 fclose(f);
845
846 argc -= 2;
847 argv += 2;
848 }
849
850 if (argc != 4) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700851 printf("usage: %s [-z] [-b <bonus-file>] <src-img> <tgt-img> <patch-file>\n",
852 argv[0]);
853 return 2;
854 }
Doug Zongker512536a2010-02-17 16:11:44 -0800855
856 int num_src_chunks;
857 ImageChunk* src_chunks;
858 int num_tgt_chunks;
859 ImageChunk* tgt_chunks;
860 int i;
861
862 if (zip_mode) {
863 if (ReadZip(argv[1], &num_src_chunks, &src_chunks, 1) == NULL) {
864 printf("failed to break apart source zip file\n");
865 return 1;
866 }
867 if (ReadZip(argv[2], &num_tgt_chunks, &tgt_chunks, 0) == NULL) {
868 printf("failed to break apart target zip file\n");
869 return 1;
870 }
871 } else {
872 if (ReadImage(argv[1], &num_src_chunks, &src_chunks) == NULL) {
873 printf("failed to break apart source image\n");
874 return 1;
875 }
876 if (ReadImage(argv[2], &num_tgt_chunks, &tgt_chunks) == NULL) {
877 printf("failed to break apart target image\n");
878 return 1;
879 }
880
881 // Verify that the source and target images have the same chunk
882 // structure (ie, the same sequence of deflate and normal chunks).
883
884 if (!zip_mode) {
885 // Merge the gzip header and footer in with any adjacent
886 // normal chunks.
887 MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
888 MergeAdjacentNormalChunks(src_chunks, &num_src_chunks);
889 }
890
891 if (num_src_chunks != num_tgt_chunks) {
892 printf("source and target don't have same number of chunks!\n");
893 printf("source chunks:\n");
894 DumpChunks(src_chunks, num_src_chunks);
895 printf("target chunks:\n");
896 DumpChunks(tgt_chunks, num_tgt_chunks);
897 return 1;
898 }
899 for (i = 0; i < num_src_chunks; ++i) {
900 if (src_chunks[i].type != tgt_chunks[i].type) {
901 printf("source and target don't have same chunk "
902 "structure! (chunk %d)\n", i);
903 printf("source chunks:\n");
904 DumpChunks(src_chunks, num_src_chunks);
905 printf("target chunks:\n");
906 DumpChunks(tgt_chunks, num_tgt_chunks);
907 return 1;
908 }
909 }
910 }
911
912 for (i = 0; i < num_tgt_chunks; ++i) {
913 if (tgt_chunks[i].type == CHUNK_DEFLATE) {
914 // Confirm that given the uncompressed chunk data in the target, we
915 // can recompress it and get exactly the same bits as are in the
916 // input target image. If this fails, treat the chunk as a normal
917 // non-deflated chunk.
918 if (ReconstructDeflateChunk(tgt_chunks+i) < 0) {
919 printf("failed to reconstruct target deflate chunk %d [%s]; "
920 "treating as normal\n", i, tgt_chunks[i].filename);
921 ChangeDeflateChunkToNormal(tgt_chunks+i);
922 if (zip_mode) {
923 ImageChunk* src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks);
924 if (src) {
925 ChangeDeflateChunkToNormal(src);
926 }
927 } else {
928 ChangeDeflateChunkToNormal(src_chunks+i);
929 }
930 continue;
931 }
932
933 // If two deflate chunks are identical (eg, the kernel has not
934 // changed between two builds), treat them as normal chunks.
935 // This makes applypatch much faster -- it can apply a trivial
936 // patch to the compressed data, rather than uncompressing and
937 // recompressing to apply the trivial patch to the uncompressed
938 // data.
939 ImageChunk* src;
940 if (zip_mode) {
941 src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks);
942 } else {
943 src = src_chunks+i;
944 }
945
946 if (src == NULL || AreChunksEqual(tgt_chunks+i, src)) {
947 ChangeDeflateChunkToNormal(tgt_chunks+i);
948 if (src) {
949 ChangeDeflateChunkToNormal(src);
950 }
951 }
952 }
953 }
954
955 // Merging neighboring normal chunks.
956 if (zip_mode) {
957 // For zips, we only need to do this to the target: deflated
958 // chunks are matched via filename, and normal chunks are patched
959 // using the entire source file as the source.
960 MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
961 } else {
962 // For images, we need to maintain the parallel structure of the
963 // chunk lists, so do the merging in both the source and target
964 // lists.
965 MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
966 MergeAdjacentNormalChunks(src_chunks, &num_src_chunks);
967 if (num_src_chunks != num_tgt_chunks) {
968 // This shouldn't happen.
969 printf("merging normal chunks went awry\n");
970 return 1;
971 }
972 }
973
974 // Compute bsdiff patches for each chunk's data (the uncompressed
975 // data, in the case of deflate chunks).
976
Doug Zongkera3ccba62012-08-20 15:28:02 -0700977 DumpChunks(src_chunks, num_src_chunks);
978
Doug Zongker512536a2010-02-17 16:11:44 -0800979 printf("Construct patches for %d chunks...\n", num_tgt_chunks);
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800980 unsigned char** patch_data = static_cast<unsigned char**>(malloc(
Tao Baoba9a42a2015-06-23 23:23:33 -0700981 num_tgt_chunks * sizeof(unsigned char*)));
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800982 size_t* patch_size = static_cast<size_t*>(malloc(num_tgt_chunks * sizeof(size_t)));
Doug Zongker512536a2010-02-17 16:11:44 -0800983 for (i = 0; i < num_tgt_chunks; ++i) {
984 if (zip_mode) {
985 ImageChunk* src;
986 if (tgt_chunks[i].type == CHUNK_DEFLATE &&
987 (src = FindChunkByName(tgt_chunks[i].filename, src_chunks,
988 num_src_chunks))) {
989 patch_data[i] = MakePatch(src, tgt_chunks+i, patch_size+i);
990 } else {
991 patch_data[i] = MakePatch(src_chunks, tgt_chunks+i, patch_size+i);
992 }
993 } else {
Tao Baoa0c40112016-06-01 13:15:44 -0700994 if (i == 1 && bonus_data) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700995 printf(" using %zu bytes of bonus data for chunk %d\n", bonus_size, i);
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800996 src_chunks[i].data = static_cast<unsigned char*>(realloc(src_chunks[i].data,
Tao Baoba9a42a2015-06-23 23:23:33 -0700997 src_chunks[i].len + bonus_size));
Tao Baoa0c40112016-06-01 13:15:44 -0700998 memcpy(src_chunks[i].data+src_chunks[i].len, bonus_data, bonus_size);
Doug Zongkera3ccba62012-08-20 15:28:02 -0700999 src_chunks[i].len += bonus_size;
1000 }
1001
Doug Zongker512536a2010-02-17 16:11:44 -08001002 patch_data[i] = MakePatch(src_chunks+i, tgt_chunks+i, patch_size+i);
1003 }
Tao Baoba9a42a2015-06-23 23:23:33 -07001004 printf("patch %3d is %zu bytes (of %zu)\n",
Doug Zongker512536a2010-02-17 16:11:44 -08001005 i, patch_size[i], tgt_chunks[i].source_len);
1006 }
1007
1008 // Figure out how big the imgdiff file header is going to be, so
1009 // that we can correctly compute the offset of each bsdiff patch
1010 // within the file.
1011
1012 size_t total_header_size = 12;
1013 for (i = 0; i < num_tgt_chunks; ++i) {
1014 total_header_size += 4;
1015 switch (tgt_chunks[i].type) {
1016 case CHUNK_NORMAL:
1017 total_header_size += 8*3;
1018 break;
1019 case CHUNK_DEFLATE:
1020 total_header_size += 8*5 + 4*5;
1021 break;
1022 case CHUNK_RAW:
1023 total_header_size += 4 + patch_size[i];
1024 break;
1025 }
1026 }
1027
1028 size_t offset = total_header_size;
1029
1030 FILE* f = fopen(argv[3], "wb");
1031
1032 // Write out the headers.
1033
1034 fwrite("IMGDIFF2", 1, 8, f);
1035 Write4(num_tgt_chunks, f);
1036 for (i = 0; i < num_tgt_chunks; ++i) {
1037 Write4(tgt_chunks[i].type, f);
1038
1039 switch (tgt_chunks[i].type) {
1040 case CHUNK_NORMAL:
Tao Baoba9a42a2015-06-23 23:23:33 -07001041 printf("chunk %3d: normal (%10zu, %10zu) %10zu\n", i,
Doug Zongker512536a2010-02-17 16:11:44 -08001042 tgt_chunks[i].start, tgt_chunks[i].len, patch_size[i]);
1043 Write8(tgt_chunks[i].source_start, f);
1044 Write8(tgt_chunks[i].source_len, f);
1045 Write8(offset, f);
1046 offset += patch_size[i];
1047 break;
1048
1049 case CHUNK_DEFLATE:
Tao Baoba9a42a2015-06-23 23:23:33 -07001050 printf("chunk %3d: deflate (%10zu, %10zu) %10zu %s\n", i,
Doug Zongker512536a2010-02-17 16:11:44 -08001051 tgt_chunks[i].start, tgt_chunks[i].deflate_len, patch_size[i],
1052 tgt_chunks[i].filename);
1053 Write8(tgt_chunks[i].source_start, f);
1054 Write8(tgt_chunks[i].source_len, f);
1055 Write8(offset, f);
1056 Write8(tgt_chunks[i].source_uncompressed_len, f);
1057 Write8(tgt_chunks[i].len, f);
1058 Write4(tgt_chunks[i].level, f);
1059 Write4(tgt_chunks[i].method, f);
1060 Write4(tgt_chunks[i].windowBits, f);
1061 Write4(tgt_chunks[i].memLevel, f);
1062 Write4(tgt_chunks[i].strategy, f);
1063 offset += patch_size[i];
1064 break;
1065
1066 case CHUNK_RAW:
Tao Baoba9a42a2015-06-23 23:23:33 -07001067 printf("chunk %3d: raw (%10zu, %10zu)\n", i,
Doug Zongker512536a2010-02-17 16:11:44 -08001068 tgt_chunks[i].start, tgt_chunks[i].len);
1069 Write4(patch_size[i], f);
1070 fwrite(patch_data[i], 1, patch_size[i], f);
1071 break;
1072 }
1073 }
1074
1075 // Append each chunk's bsdiff patch, in order.
1076
1077 for (i = 0; i < num_tgt_chunks; ++i) {
1078 if (tgt_chunks[i].type != CHUNK_RAW) {
1079 fwrite(patch_data[i], 1, patch_size[i], f);
1080 }
1081 }
1082
Rahul Chaudhry3a5177b2016-11-15 16:18:46 -08001083 free(patch_data);
1084 free(patch_size);
1085
Doug Zongker512536a2010-02-17 16:11:44 -08001086 fclose(f);
Doug Zongker512536a2010-02-17 16:11:44 -08001087
1088 return 0;
1089}