Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 114 | * |
| 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 122 | */ |
| 123 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 124 | #include "applypatch/imgdiff.h" |
| 125 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 126 | #include <errno.h> |
Tao Bao | d37ce8f | 2016-12-17 17:10:04 -0800 | [diff] [blame] | 127 | #include <fcntl.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 128 | #include <stdio.h> |
| 129 | #include <stdlib.h> |
| 130 | #include <string.h> |
| 131 | #include <sys/stat.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 132 | #include <sys/types.h> |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 133 | #include <unistd.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 134 | |
Tao Bao | d37ce8f | 2016-12-17 17:10:04 -0800 | [diff] [blame] | 135 | #include <android-base/file.h> |
| 136 | #include <android-base/unique_fd.h> |
| 137 | |
Sen Jiang | 2fffcb1 | 2016-05-03 15:49:10 -0700 | [diff] [blame] | 138 | #include <bsdiff.h> |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 139 | #include <zlib.h> |
Sen Jiang | 2fffcb1 | 2016-05-03 15:49:10 -0700 | [diff] [blame] | 140 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 141 | #include "utils.h" |
| 142 | |
| 143 | typedef 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 153 | // --- 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 | |
| 167 | typedef struct { |
| 168 | int data_offset; |
| 169 | int deflate_len; |
| 170 | int uncomp_len; |
| 171 | char* filename; |
| 172 | } ZipFileEntry; |
| 173 | |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 174 | static int fileentry_compare(const void* a, const void* b) { |
| 175 | int ao = ((ZipFileEntry*)a)->data_offset; |
| 176 | int bo = ((ZipFileEntry*)b)->data_offset; |
| 177 | if (ao < bo) { |
| 178 | return -1; |
| 179 | } else if (ao > bo) { |
| 180 | return 1; |
| 181 | } else { |
| 182 | return 0; |
| 183 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 186 | unsigned char* ReadZip(const char* filename, |
| 187 | int* num_chunks, ImageChunk** chunks, |
| 188 | int include_pseudo_chunk) { |
| 189 | struct stat st; |
| 190 | if (stat(filename, &st) != 0) { |
| 191 | printf("failed to stat \"%s\": %s\n", filename, strerror(errno)); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 195 | size_t sz = static_cast<size_t>(st.st_size); |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 196 | unsigned char* img = static_cast<unsigned char*>(malloc(sz)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 197 | FILE* f = fopen(filename, "rb"); |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 198 | if (fread(img, 1, sz, f) != sz) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 199 | printf("failed to read \"%s\" %s\n", filename, strerror(errno)); |
| 200 | fclose(f); |
Rahul Chaudhry | 8b640ff | 2016-12-06 15:10:41 -0800 | [diff] [blame] | 201 | free(img); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 202 | return NULL; |
| 203 | } |
| 204 | fclose(f); |
| 205 | |
| 206 | // look for the end-of-central-directory record. |
| 207 | |
| 208 | int i; |
| 209 | for (i = st.st_size-20; i >= 0 && i > st.st_size - 65600; --i) { |
| 210 | if (img[i] == 0x50 && img[i+1] == 0x4b && |
| 211 | img[i+2] == 0x05 && img[i+3] == 0x06) { |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | // double-check: this archive consists of a single "disk" |
| 216 | if (!(img[i+4] == 0 && img[i+5] == 0 && img[i+6] == 0 && img[i+7] == 0)) { |
| 217 | printf("can't process multi-disk archive\n"); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 221 | int cdcount = Read2(img+i+8); |
| 222 | int cdoffset = Read4(img+i+16); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 223 | |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 224 | ZipFileEntry* temp_entries = static_cast<ZipFileEntry*>(malloc( |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 225 | cdcount * sizeof(ZipFileEntry))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 226 | int entrycount = 0; |
| 227 | |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 228 | unsigned char* cd = img+cdoffset; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 229 | for (i = 0; i < cdcount; ++i) { |
| 230 | if (!(cd[0] == 0x50 && cd[1] == 0x4b && cd[2] == 0x01 && cd[3] == 0x02)) { |
| 231 | printf("bad central directory entry %d\n", i); |
Rahul Chaudhry | 3a5177b | 2016-11-15 16:18:46 -0800 | [diff] [blame] | 232 | free(temp_entries); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | int clen = Read4(cd+20); // compressed len |
| 237 | int ulen = Read4(cd+24); // uncompressed len |
| 238 | int nlen = Read2(cd+28); // filename len |
| 239 | int xlen = Read2(cd+30); // extra field len |
| 240 | int mlen = Read2(cd+32); // file comment len |
| 241 | int hoffset = Read4(cd+42); // local header offset |
| 242 | |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 243 | char* filename = static_cast<char*>(malloc(nlen+1)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 244 | memcpy(filename, cd+46, nlen); |
| 245 | filename[nlen] = '\0'; |
| 246 | |
| 247 | int method = Read2(cd+10); |
| 248 | |
| 249 | cd += 46 + nlen + xlen + mlen; |
| 250 | |
| 251 | if (method != 8) { // 8 == deflate |
| 252 | free(filename); |
| 253 | continue; |
| 254 | } |
| 255 | |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 256 | unsigned char* lh = img + hoffset; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 257 | |
| 258 | if (!(lh[0] == 0x50 && lh[1] == 0x4b && lh[2] == 0x03 && lh[3] == 0x04)) { |
| 259 | printf("bad local file header entry %d\n", i); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | if (Read2(lh+26) != nlen || memcmp(lh+30, filename, nlen) != 0) { |
| 264 | printf("central dir filename doesn't match local header\n"); |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | xlen = Read2(lh+28); // extra field len; might be different from CD entry? |
| 269 | |
| 270 | temp_entries[entrycount].data_offset = hoffset+30+nlen+xlen; |
| 271 | temp_entries[entrycount].deflate_len = clen; |
| 272 | temp_entries[entrycount].uncomp_len = ulen; |
| 273 | temp_entries[entrycount].filename = filename; |
| 274 | ++entrycount; |
| 275 | } |
| 276 | |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 277 | qsort(temp_entries, entrycount, sizeof(ZipFileEntry), fileentry_compare); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 278 | |
| 279 | #if 0 |
| 280 | printf("found %d deflated entries\n", entrycount); |
| 281 | for (i = 0; i < entrycount; ++i) { |
| 282 | printf("off %10d len %10d unlen %10d %p %s\n", |
| 283 | temp_entries[i].data_offset, |
| 284 | temp_entries[i].deflate_len, |
| 285 | temp_entries[i].uncomp_len, |
| 286 | temp_entries[i].filename, |
| 287 | temp_entries[i].filename); |
| 288 | } |
| 289 | #endif |
| 290 | |
| 291 | *num_chunks = 0; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 292 | *chunks = static_cast<ImageChunk*>(malloc((entrycount*2+2) * sizeof(ImageChunk))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 293 | ImageChunk* curr = *chunks; |
| 294 | |
| 295 | if (include_pseudo_chunk) { |
| 296 | curr->type = CHUNK_NORMAL; |
| 297 | curr->start = 0; |
| 298 | curr->len = st.st_size; |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 299 | curr->data = img; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 300 | curr->filename = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 301 | ++curr; |
| 302 | ++*num_chunks; |
| 303 | } |
| 304 | |
| 305 | int pos = 0; |
| 306 | int nextentry = 0; |
| 307 | |
| 308 | while (pos < st.st_size) { |
| 309 | if (nextentry < entrycount && pos == temp_entries[nextentry].data_offset) { |
| 310 | curr->type = CHUNK_DEFLATE; |
| 311 | curr->start = pos; |
| 312 | curr->deflate_len = temp_entries[nextentry].deflate_len; |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 313 | curr->deflate_data = img + pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 314 | curr->filename = temp_entries[nextentry].filename; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 315 | |
| 316 | curr->len = temp_entries[nextentry].uncomp_len; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 317 | curr->data = static_cast<unsigned char*>(malloc(curr->len)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 318 | |
| 319 | z_stream strm; |
| 320 | strm.zalloc = Z_NULL; |
| 321 | strm.zfree = Z_NULL; |
| 322 | strm.opaque = Z_NULL; |
| 323 | strm.avail_in = curr->deflate_len; |
| 324 | strm.next_in = curr->deflate_data; |
| 325 | |
| 326 | // -15 means we are decoding a 'raw' deflate stream; zlib will |
| 327 | // not expect zlib headers. |
| 328 | int ret = inflateInit2(&strm, -15); |
Rahul Chaudhry | a793c58 | 2016-11-29 17:10:14 -0800 | [diff] [blame] | 329 | if (ret < 0) { |
| 330 | printf("failed to initialize inflate: %d\n", ret); |
| 331 | return NULL; |
| 332 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 333 | |
| 334 | strm.avail_out = curr->len; |
| 335 | strm.next_out = curr->data; |
| 336 | ret = inflate(&strm, Z_NO_FLUSH); |
| 337 | if (ret != Z_STREAM_END) { |
| 338 | printf("failed to inflate \"%s\"; %d\n", curr->filename, ret); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | inflateEnd(&strm); |
| 343 | |
| 344 | pos += curr->deflate_len; |
| 345 | ++nextentry; |
| 346 | ++*num_chunks; |
| 347 | ++curr; |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | // use a normal chunk to take all the data up to the start of the |
| 352 | // next deflate section. |
| 353 | |
| 354 | curr->type = CHUNK_NORMAL; |
| 355 | curr->start = pos; |
| 356 | if (nextentry < entrycount) { |
| 357 | curr->len = temp_entries[nextentry].data_offset - pos; |
| 358 | } else { |
| 359 | curr->len = st.st_size - pos; |
| 360 | } |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 361 | curr->data = img + pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 362 | curr->filename = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 363 | pos += curr->len; |
| 364 | |
| 365 | ++*num_chunks; |
| 366 | ++curr; |
| 367 | } |
| 368 | |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 369 | free(temp_entries); |
| 370 | return img; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Read the given file and break it up into chunks, putting the number |
| 375 | * of chunks and their info in *num_chunks and **chunks, |
| 376 | * respectively. Returns a malloc'd block of memory containing the |
| 377 | * contents of the file; various pointers in the output chunk array |
| 378 | * will point into this block of memory. The caller should free the |
| 379 | * return value when done with all the chunks. Returns NULL on |
| 380 | * failure. |
| 381 | */ |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 382 | unsigned char* ReadImage(const char* filename, int* num_chunks, ImageChunk** chunks) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 383 | struct stat st; |
| 384 | if (stat(filename, &st) != 0) { |
| 385 | printf("failed to stat \"%s\": %s\n", filename, strerror(errno)); |
| 386 | return NULL; |
| 387 | } |
| 388 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 389 | size_t sz = static_cast<size_t>(st.st_size); |
Tao Bao | d37ce8f | 2016-12-17 17:10:04 -0800 | [diff] [blame] | 390 | unsigned char* img = static_cast<unsigned char*>(malloc(sz)); |
| 391 | android::base::unique_fd fd(open(filename, O_RDONLY)); |
| 392 | if (!android::base::ReadFully(fd, img, sz)) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 393 | printf("failed to read \"%s\" %s\n", filename, strerror(errno)); |
Tao Bao | d37ce8f | 2016-12-17 17:10:04 -0800 | [diff] [blame] | 394 | return nullptr; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 395 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 396 | |
| 397 | size_t pos = 0; |
| 398 | |
| 399 | *num_chunks = 0; |
| 400 | *chunks = NULL; |
| 401 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 402 | while (pos < sz) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 403 | unsigned char* p = img + pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 404 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 405 | if (sz - pos >= 4 && |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 406 | p[0] == 0x1f && p[1] == 0x8b && |
| 407 | p[2] == 0x08 && // deflate compression |
| 408 | p[3] == 0x00) { // no header flags |
| 409 | // 'pos' is the offset of the start of a gzip chunk. |
Johan Redestig | c68bd34 | 2015-04-14 21:20:06 +0200 | [diff] [blame] | 410 | size_t chunk_offset = pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 411 | |
| 412 | *num_chunks += 3; |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 413 | *chunks = static_cast<ImageChunk*>(realloc(*chunks, *num_chunks * sizeof(ImageChunk))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 414 | ImageChunk* curr = *chunks + (*num_chunks-3); |
| 415 | |
| 416 | // create a normal chunk for the header. |
| 417 | curr->start = pos; |
| 418 | curr->type = CHUNK_NORMAL; |
| 419 | curr->len = GZIP_HEADER_LEN; |
| 420 | curr->data = p; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 421 | |
| 422 | pos += curr->len; |
| 423 | p += curr->len; |
| 424 | ++curr; |
| 425 | |
| 426 | curr->type = CHUNK_DEFLATE; |
| 427 | curr->filename = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 428 | |
| 429 | // We must decompress this chunk in order to discover where it |
| 430 | // ends, and so we can put the uncompressed data and its length |
| 431 | // into curr->data and curr->len. |
| 432 | |
| 433 | size_t allocated = 32768; |
| 434 | curr->len = 0; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 435 | curr->data = static_cast<unsigned char*>(malloc(allocated)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 436 | curr->start = pos; |
| 437 | curr->deflate_data = p; |
| 438 | |
| 439 | z_stream strm; |
| 440 | strm.zalloc = Z_NULL; |
| 441 | strm.zfree = Z_NULL; |
| 442 | strm.opaque = Z_NULL; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 443 | strm.avail_in = sz - pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 444 | strm.next_in = p; |
| 445 | |
| 446 | // -15 means we are decoding a 'raw' deflate stream; zlib will |
| 447 | // not expect zlib headers. |
| 448 | int ret = inflateInit2(&strm, -15); |
Rahul Chaudhry | a793c58 | 2016-11-29 17:10:14 -0800 | [diff] [blame] | 449 | if (ret < 0) { |
| 450 | printf("failed to initialize inflate: %d\n", ret); |
| 451 | return NULL; |
| 452 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 453 | |
| 454 | do { |
| 455 | strm.avail_out = allocated - curr->len; |
| 456 | strm.next_out = curr->data + curr->len; |
| 457 | ret = inflate(&strm, Z_NO_FLUSH); |
Johan Redestig | c68bd34 | 2015-04-14 21:20:06 +0200 | [diff] [blame] | 458 | if (ret < 0) { |
Sen Jiang | fa4f1b7 | 2016-02-11 16:14:23 -0800 | [diff] [blame] | 459 | printf("Warning: inflate failed [%s] at offset [%zu]," |
| 460 | " treating as a normal chunk\n", |
David Riley | 0779fc9 | 2015-12-10 10:18:25 -0800 | [diff] [blame] | 461 | strm.msg, chunk_offset); |
Sen Jiang | fa4f1b7 | 2016-02-11 16:14:23 -0800 | [diff] [blame] | 462 | break; |
Johan Redestig | c68bd34 | 2015-04-14 21:20:06 +0200 | [diff] [blame] | 463 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 464 | curr->len = allocated - strm.avail_out; |
| 465 | if (strm.avail_out == 0) { |
| 466 | allocated *= 2; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 467 | curr->data = static_cast<unsigned char*>(realloc(curr->data, allocated)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 468 | } |
| 469 | } while (ret != Z_STREAM_END); |
| 470 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 471 | curr->deflate_len = sz - strm.avail_in - pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 472 | inflateEnd(&strm); |
Sen Jiang | fa4f1b7 | 2016-02-11 16:14:23 -0800 | [diff] [blame] | 473 | |
| 474 | if (ret < 0) { |
| 475 | free(curr->data); |
| 476 | *num_chunks -= 2; |
| 477 | continue; |
| 478 | } |
| 479 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 480 | pos += curr->deflate_len; |
| 481 | p += curr->deflate_len; |
| 482 | ++curr; |
| 483 | |
| 484 | // create a normal chunk for the footer |
| 485 | |
| 486 | curr->type = CHUNK_NORMAL; |
| 487 | curr->start = pos; |
| 488 | curr->len = GZIP_FOOTER_LEN; |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 489 | curr->data = img+pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 490 | |
| 491 | pos += curr->len; |
| 492 | p += curr->len; |
| 493 | ++curr; |
| 494 | |
| 495 | // The footer (that we just skipped over) contains the size of |
| 496 | // the uncompressed data. Double-check to make sure that it |
| 497 | // matches the size of the data we got when we actually did |
| 498 | // the decompression. |
| 499 | size_t footer_size = Read4(p-4); |
| 500 | if (footer_size != curr[-2].len) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 501 | printf("Error: footer size %zu != decompressed size %zu\n", footer_size, curr[-2].len); |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 502 | free(img); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 503 | return NULL; |
| 504 | } |
| 505 | } else { |
| 506 | // Reallocate the list for every chunk; we expect the number of |
| 507 | // chunks to be small (5 for typical boot and recovery images). |
| 508 | ++*num_chunks; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 509 | *chunks = static_cast<ImageChunk*>(realloc(*chunks, *num_chunks * sizeof(ImageChunk))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 510 | ImageChunk* curr = *chunks + (*num_chunks-1); |
| 511 | curr->start = pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 512 | |
| 513 | // 'pos' is not the offset of the start of a gzip chunk, so scan |
| 514 | // forward until we find a gzip header. |
| 515 | curr->type = CHUNK_NORMAL; |
| 516 | curr->data = p; |
| 517 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 518 | for (curr->len = 0; curr->len < (sz - pos); ++curr->len) { |
Tao Bao | d37ce8f | 2016-12-17 17:10:04 -0800 | [diff] [blame] | 519 | if (sz - pos >= 4 && p[curr->len] == 0x1f && p[curr->len + 1] == 0x8b && |
| 520 | p[curr->len + 2] == 0x08 && p[curr->len + 3] == 0x00) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | } |
| 524 | pos += curr->len; |
| 525 | } |
| 526 | } |
| 527 | |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 528 | return img; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | #define BUFFER_SIZE 32768 |
| 532 | |
| 533 | /* |
| 534 | * Takes the uncompressed data stored in the chunk, compresses it |
| 535 | * using the zlib parameters stored in the chunk, and checks that it |
| 536 | * matches exactly the compressed data we started with (also stored in |
| 537 | * the chunk). Return 0 on success. |
| 538 | */ |
| 539 | int TryReconstruction(ImageChunk* chunk, unsigned char* out) { |
| 540 | size_t p = 0; |
| 541 | |
| 542 | #if 0 |
| 543 | printf("trying %d %d %d %d %d\n", |
| 544 | chunk->level, chunk->method, chunk->windowBits, |
| 545 | chunk->memLevel, chunk->strategy); |
| 546 | #endif |
| 547 | |
| 548 | z_stream strm; |
| 549 | strm.zalloc = Z_NULL; |
| 550 | strm.zfree = Z_NULL; |
| 551 | strm.opaque = Z_NULL; |
| 552 | strm.avail_in = chunk->len; |
| 553 | strm.next_in = chunk->data; |
| 554 | int ret; |
| 555 | ret = deflateInit2(&strm, chunk->level, chunk->method, chunk->windowBits, |
| 556 | chunk->memLevel, chunk->strategy); |
Rahul Chaudhry | a793c58 | 2016-11-29 17:10:14 -0800 | [diff] [blame] | 557 | if (ret < 0) { |
| 558 | printf("failed to initialize deflate: %d\n", ret); |
| 559 | return -1; |
| 560 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 561 | do { |
| 562 | strm.avail_out = BUFFER_SIZE; |
| 563 | strm.next_out = out; |
| 564 | ret = deflate(&strm, Z_FINISH); |
Rahul Chaudhry | a793c58 | 2016-11-29 17:10:14 -0800 | [diff] [blame] | 565 | if (ret < 0) { |
| 566 | printf("failed to deflate: %d\n", ret); |
| 567 | return -1; |
| 568 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 569 | size_t have = BUFFER_SIZE - strm.avail_out; |
| 570 | |
| 571 | if (memcmp(out, chunk->deflate_data+p, have) != 0) { |
| 572 | // mismatch; data isn't the same. |
| 573 | deflateEnd(&strm); |
| 574 | return -1; |
| 575 | } |
| 576 | p += have; |
| 577 | } while (ret != Z_STREAM_END); |
| 578 | deflateEnd(&strm); |
| 579 | if (p != chunk->deflate_len) { |
| 580 | // mismatch; ran out of data before we should have. |
| 581 | return -1; |
| 582 | } |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Verify that we can reproduce exactly the same compressed data that |
| 588 | * we started with. Sets the level, method, windowBits, memLevel, and |
| 589 | * strategy fields in the chunk to the encoding parameters needed to |
| 590 | * produce the right output. Returns 0 on success. |
| 591 | */ |
| 592 | int ReconstructDeflateChunk(ImageChunk* chunk) { |
| 593 | if (chunk->type != CHUNK_DEFLATE) { |
| 594 | printf("attempt to reconstruct non-deflate chunk\n"); |
| 595 | return -1; |
| 596 | } |
| 597 | |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 598 | unsigned char* out = static_cast<unsigned char*>(malloc(BUFFER_SIZE)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 599 | |
| 600 | // We only check two combinations of encoder parameters: level 6 |
| 601 | // (the default) and level 9 (the maximum). |
| 602 | for (chunk->level = 6; chunk->level <= 9; chunk->level += 3) { |
| 603 | chunk->windowBits = -15; // 32kb window; negative to indicate a raw stream. |
| 604 | chunk->memLevel = 8; // the default value. |
| 605 | chunk->method = Z_DEFLATED; |
| 606 | chunk->strategy = Z_DEFAULT_STRATEGY; |
| 607 | |
| 608 | if (TryReconstruction(chunk, out) == 0) { |
| 609 | free(out); |
| 610 | return 0; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | free(out); |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | /* |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 619 | * Given source and target chunks, compute a bsdiff patch between them. |
| 620 | * Return the patch data, placing its length in *size. Return NULL on failure. |
| 621 | * |bsdiff_cache| can be used to cache the suffix array if the same |src| chunk |
| 622 | * is used repeatedly, pass nullptr if not needed. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 623 | */ |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 624 | unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size, saidx_t** bsdiff_cache) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 625 | if (tgt->type == CHUNK_NORMAL) { |
| 626 | if (tgt->len <= 160) { |
| 627 | tgt->type = CHUNK_RAW; |
| 628 | *size = tgt->len; |
| 629 | return tgt->data; |
| 630 | } |
| 631 | } |
| 632 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 633 | #if defined(__ANDROID__) |
| 634 | char ptemp[] = "/data/local/tmp/imgdiff-patch-XXXXXX"; |
| 635 | #else |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 636 | char ptemp[] = "/tmp/imgdiff-patch-XXXXXX"; |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 637 | #endif |
Jeremy Compostella | a91c66d | 2015-09-08 19:15:09 +0200 | [diff] [blame] | 638 | int fd = mkstemp(ptemp); |
| 639 | |
| 640 | if (fd == -1) { |
| 641 | printf("MakePatch failed to create a temporary file: %s\n", |
| 642 | strerror(errno)); |
| 643 | return NULL; |
| 644 | } |
| 645 | close(fd); // temporary file is created and we don't need its file |
| 646 | // descriptor |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 647 | |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 648 | int r = bsdiff::bsdiff(src->data, src->len, tgt->data, tgt->len, ptemp, bsdiff_cache); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 649 | if (r != 0) { |
| 650 | printf("bsdiff() failed: %d\n", r); |
| 651 | return NULL; |
| 652 | } |
| 653 | |
| 654 | struct stat st; |
| 655 | if (stat(ptemp, &st) != 0) { |
| 656 | printf("failed to stat patch file %s: %s\n", |
| 657 | ptemp, strerror(errno)); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 661 | size_t sz = static_cast<size_t>(st.st_size); |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 662 | // TODO: Memory leak on error return. |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 663 | unsigned char* data = static_cast<unsigned char*>(malloc(sz)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 664 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 665 | if (tgt->type == CHUNK_NORMAL && tgt->len <= sz) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 666 | unlink(ptemp); |
| 667 | |
| 668 | tgt->type = CHUNK_RAW; |
| 669 | *size = tgt->len; |
| 670 | return tgt->data; |
| 671 | } |
| 672 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 673 | *size = sz; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 674 | |
| 675 | FILE* f = fopen(ptemp, "rb"); |
| 676 | if (f == NULL) { |
| 677 | printf("failed to open patch %s: %s\n", ptemp, strerror(errno)); |
| 678 | return NULL; |
| 679 | } |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 680 | if (fread(data, 1, sz, f) != sz) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 681 | printf("failed to read patch %s: %s\n", ptemp, strerror(errno)); |
| 682 | return NULL; |
| 683 | } |
| 684 | fclose(f); |
| 685 | |
| 686 | unlink(ptemp); |
| 687 | |
| 688 | tgt->source_start = src->start; |
| 689 | switch (tgt->type) { |
| 690 | case CHUNK_NORMAL: |
| 691 | tgt->source_len = src->len; |
| 692 | break; |
| 693 | case CHUNK_DEFLATE: |
| 694 | tgt->source_len = src->deflate_len; |
| 695 | tgt->source_uncompressed_len = src->len; |
| 696 | break; |
| 697 | } |
| 698 | |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 699 | return data; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Cause a gzip chunk to be treated as a normal chunk (ie, as a blob |
| 704 | * of uninterpreted data). The resulting patch will likely be about |
| 705 | * as big as the target file, but it lets us handle the case of images |
| 706 | * where some gzip chunks are reconstructible but others aren't (by |
| 707 | * treating the ones that aren't as normal chunks). |
| 708 | */ |
| 709 | void ChangeDeflateChunkToNormal(ImageChunk* ch) { |
| 710 | if (ch->type != CHUNK_DEFLATE) return; |
| 711 | ch->type = CHUNK_NORMAL; |
| 712 | free(ch->data); |
| 713 | ch->data = ch->deflate_data; |
| 714 | ch->len = ch->deflate_len; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Return true if the data in the chunk is identical (including the |
| 719 | * compressed representation, for gzip chunks). |
| 720 | */ |
| 721 | int AreChunksEqual(ImageChunk* a, ImageChunk* b) { |
| 722 | if (a->type != b->type) return 0; |
| 723 | |
| 724 | switch (a->type) { |
| 725 | case CHUNK_NORMAL: |
| 726 | return a->len == b->len && memcmp(a->data, b->data, a->len) == 0; |
| 727 | |
| 728 | case CHUNK_DEFLATE: |
| 729 | return a->deflate_len == b->deflate_len && |
| 730 | memcmp(a->deflate_data, b->deflate_data, a->deflate_len) == 0; |
| 731 | |
| 732 | default: |
| 733 | printf("unknown chunk type %d\n", a->type); |
| 734 | return 0; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Look for runs of adjacent normal chunks and compress them down into |
| 740 | * a single chunk. (Such runs can be produced when deflate chunks are |
| 741 | * changed to normal chunks.) |
| 742 | */ |
| 743 | void MergeAdjacentNormalChunks(ImageChunk* chunks, int* num_chunks) { |
| 744 | int out = 0; |
| 745 | int in_start = 0, in_end; |
| 746 | while (in_start < *num_chunks) { |
| 747 | if (chunks[in_start].type != CHUNK_NORMAL) { |
| 748 | in_end = in_start+1; |
| 749 | } else { |
| 750 | // in_start is a normal chunk. Look for a run of normal chunks |
| 751 | // that constitute a solid block of data (ie, each chunk begins |
| 752 | // where the previous one ended). |
| 753 | for (in_end = in_start+1; |
| 754 | in_end < *num_chunks && chunks[in_end].type == CHUNK_NORMAL && |
| 755 | (chunks[in_end].start == |
| 756 | chunks[in_end-1].start + chunks[in_end-1].len && |
| 757 | chunks[in_end].data == |
| 758 | chunks[in_end-1].data + chunks[in_end-1].len); |
| 759 | ++in_end); |
| 760 | } |
| 761 | |
| 762 | if (in_end == in_start+1) { |
| 763 | #if 0 |
| 764 | printf("chunk %d is now %d\n", in_start, out); |
| 765 | #endif |
| 766 | if (out != in_start) { |
| 767 | memcpy(chunks+out, chunks+in_start, sizeof(ImageChunk)); |
| 768 | } |
| 769 | } else { |
| 770 | #if 0 |
| 771 | printf("collapse normal chunks %d-%d into %d\n", in_start, in_end-1, out); |
| 772 | #endif |
| 773 | |
| 774 | // Merge chunks [in_start, in_end-1] into one chunk. Since the |
| 775 | // data member of each chunk is just a pointer into an in-memory |
| 776 | // copy of the file, this can be done without recopying (the |
| 777 | // output chunk has the first chunk's start location and data |
| 778 | // pointer, and length equal to the sum of the input chunk |
| 779 | // lengths). |
| 780 | chunks[out].type = CHUNK_NORMAL; |
| 781 | chunks[out].start = chunks[in_start].start; |
| 782 | chunks[out].data = chunks[in_start].data; |
| 783 | chunks[out].len = chunks[in_end-1].len + |
| 784 | (chunks[in_end-1].start - chunks[in_start].start); |
| 785 | } |
| 786 | |
| 787 | ++out; |
| 788 | in_start = in_end; |
| 789 | } |
| 790 | *num_chunks = out; |
| 791 | } |
| 792 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 793 | ImageChunk* FindChunkByName(const char* name, ImageChunk* chunks, int num_chunks) { |
| 794 | for (int i = 0; i < num_chunks; ++i) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 795 | if (chunks[i].type == CHUNK_DEFLATE && chunks[i].filename && |
| 796 | strcmp(name, chunks[i].filename) == 0) { |
| 797 | return chunks+i; |
| 798 | } |
| 799 | } |
| 800 | return NULL; |
| 801 | } |
| 802 | |
| 803 | void DumpChunks(ImageChunk* chunks, int num_chunks) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 804 | for (int i = 0; i < num_chunks; ++i) { |
| 805 | printf("chunk %d: type %d start %zu len %zu\n", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 806 | i, chunks[i].type, chunks[i].start, chunks[i].len); |
| 807 | } |
| 808 | } |
| 809 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 810 | int imgdiff(int argc, const char** argv) { |
| 811 | bool zip_mode = false; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 812 | |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 813 | if (argc >= 2 && strcmp(argv[1], "-z") == 0) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 814 | zip_mode = true; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 815 | --argc; |
| 816 | ++argv; |
| 817 | } |
| 818 | |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 819 | size_t bonus_size = 0; |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 820 | unsigned char* bonus_data = NULL; |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 821 | if (argc >= 3 && strcmp(argv[1], "-b") == 0) { |
| 822 | struct stat st; |
| 823 | if (stat(argv[2], &st) != 0) { |
| 824 | printf("failed to stat bonus file %s: %s\n", argv[2], strerror(errno)); |
| 825 | return 1; |
| 826 | } |
| 827 | bonus_size = st.st_size; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 828 | bonus_data = static_cast<unsigned char*>(malloc(bonus_size)); |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 829 | FILE* f = fopen(argv[2], "rb"); |
| 830 | if (f == NULL) { |
| 831 | printf("failed to open bonus file %s: %s\n", argv[2], strerror(errno)); |
| 832 | return 1; |
| 833 | } |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 834 | if (fread(bonus_data, 1, bonus_size, f) != bonus_size) { |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 835 | printf("failed to read bonus file %s: %s\n", argv[2], strerror(errno)); |
| 836 | return 1; |
| 837 | } |
| 838 | fclose(f); |
| 839 | |
| 840 | argc -= 2; |
| 841 | argv += 2; |
| 842 | } |
| 843 | |
| 844 | if (argc != 4) { |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 845 | printf("usage: %s [-z] [-b <bonus-file>] <src-img> <tgt-img> <patch-file>\n", |
| 846 | argv[0]); |
| 847 | return 2; |
| 848 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 849 | |
| 850 | int num_src_chunks; |
| 851 | ImageChunk* src_chunks; |
| 852 | int num_tgt_chunks; |
| 853 | ImageChunk* tgt_chunks; |
| 854 | int i; |
| 855 | |
| 856 | if (zip_mode) { |
| 857 | if (ReadZip(argv[1], &num_src_chunks, &src_chunks, 1) == NULL) { |
| 858 | printf("failed to break apart source zip file\n"); |
| 859 | return 1; |
| 860 | } |
| 861 | if (ReadZip(argv[2], &num_tgt_chunks, &tgt_chunks, 0) == NULL) { |
| 862 | printf("failed to break apart target zip file\n"); |
| 863 | return 1; |
| 864 | } |
| 865 | } else { |
| 866 | if (ReadImage(argv[1], &num_src_chunks, &src_chunks) == NULL) { |
| 867 | printf("failed to break apart source image\n"); |
| 868 | return 1; |
| 869 | } |
| 870 | if (ReadImage(argv[2], &num_tgt_chunks, &tgt_chunks) == NULL) { |
| 871 | printf("failed to break apart target image\n"); |
| 872 | return 1; |
| 873 | } |
| 874 | |
| 875 | // Verify that the source and target images have the same chunk |
| 876 | // structure (ie, the same sequence of deflate and normal chunks). |
| 877 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 878 | // Merge the gzip header and footer in with any adjacent |
| 879 | // normal chunks. |
| 880 | MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks); |
| 881 | MergeAdjacentNormalChunks(src_chunks, &num_src_chunks); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 882 | |
| 883 | if (num_src_chunks != num_tgt_chunks) { |
| 884 | printf("source and target don't have same number of chunks!\n"); |
| 885 | printf("source chunks:\n"); |
| 886 | DumpChunks(src_chunks, num_src_chunks); |
| 887 | printf("target chunks:\n"); |
| 888 | DumpChunks(tgt_chunks, num_tgt_chunks); |
| 889 | return 1; |
| 890 | } |
| 891 | for (i = 0; i < num_src_chunks; ++i) { |
| 892 | if (src_chunks[i].type != tgt_chunks[i].type) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 893 | printf("source and target don't have same chunk structure! (chunk %d)\n", i); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 894 | printf("source chunks:\n"); |
| 895 | DumpChunks(src_chunks, num_src_chunks); |
| 896 | printf("target chunks:\n"); |
| 897 | DumpChunks(tgt_chunks, num_tgt_chunks); |
| 898 | return 1; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | for (i = 0; i < num_tgt_chunks; ++i) { |
| 904 | if (tgt_chunks[i].type == CHUNK_DEFLATE) { |
| 905 | // Confirm that given the uncompressed chunk data in the target, we |
| 906 | // can recompress it and get exactly the same bits as are in the |
| 907 | // input target image. If this fails, treat the chunk as a normal |
| 908 | // non-deflated chunk. |
| 909 | if (ReconstructDeflateChunk(tgt_chunks+i) < 0) { |
| 910 | printf("failed to reconstruct target deflate chunk %d [%s]; " |
| 911 | "treating as normal\n", i, tgt_chunks[i].filename); |
| 912 | ChangeDeflateChunkToNormal(tgt_chunks+i); |
| 913 | if (zip_mode) { |
| 914 | ImageChunk* src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks); |
| 915 | if (src) { |
| 916 | ChangeDeflateChunkToNormal(src); |
| 917 | } |
| 918 | } else { |
| 919 | ChangeDeflateChunkToNormal(src_chunks+i); |
| 920 | } |
| 921 | continue; |
| 922 | } |
| 923 | |
| 924 | // If two deflate chunks are identical (eg, the kernel has not |
| 925 | // changed between two builds), treat them as normal chunks. |
| 926 | // This makes applypatch much faster -- it can apply a trivial |
| 927 | // patch to the compressed data, rather than uncompressing and |
| 928 | // recompressing to apply the trivial patch to the uncompressed |
| 929 | // data. |
| 930 | ImageChunk* src; |
| 931 | if (zip_mode) { |
| 932 | src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks); |
| 933 | } else { |
| 934 | src = src_chunks+i; |
| 935 | } |
| 936 | |
| 937 | if (src == NULL || AreChunksEqual(tgt_chunks+i, src)) { |
| 938 | ChangeDeflateChunkToNormal(tgt_chunks+i); |
| 939 | if (src) { |
| 940 | ChangeDeflateChunkToNormal(src); |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | // Merging neighboring normal chunks. |
| 947 | if (zip_mode) { |
| 948 | // For zips, we only need to do this to the target: deflated |
| 949 | // chunks are matched via filename, and normal chunks are patched |
| 950 | // using the entire source file as the source. |
| 951 | MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks); |
| 952 | } else { |
| 953 | // For images, we need to maintain the parallel structure of the |
| 954 | // chunk lists, so do the merging in both the source and target |
| 955 | // lists. |
| 956 | MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks); |
| 957 | MergeAdjacentNormalChunks(src_chunks, &num_src_chunks); |
| 958 | if (num_src_chunks != num_tgt_chunks) { |
| 959 | // This shouldn't happen. |
| 960 | printf("merging normal chunks went awry\n"); |
| 961 | return 1; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // Compute bsdiff patches for each chunk's data (the uncompressed |
| 966 | // data, in the case of deflate chunks). |
| 967 | |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 968 | DumpChunks(src_chunks, num_src_chunks); |
| 969 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 970 | printf("Construct patches for %d chunks...\n", num_tgt_chunks); |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 971 | unsigned char** patch_data = static_cast<unsigned char**>(malloc( |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 972 | num_tgt_chunks * sizeof(unsigned char*))); |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 973 | size_t* patch_size = static_cast<size_t*>(malloc(num_tgt_chunks * sizeof(size_t))); |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 974 | saidx_t* bsdiff_cache = nullptr; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 975 | for (i = 0; i < num_tgt_chunks; ++i) { |
| 976 | if (zip_mode) { |
| 977 | ImageChunk* src; |
| 978 | if (tgt_chunks[i].type == CHUNK_DEFLATE && |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 979 | (src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks))) { |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 980 | patch_data[i] = MakePatch(src, tgt_chunks + i, patch_size + i, nullptr); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 981 | } else { |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 982 | patch_data[i] = MakePatch(src_chunks, tgt_chunks + i, patch_size + i, &bsdiff_cache); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 983 | } |
| 984 | } else { |
Tao Bao | a0c4011 | 2016-06-01 13:15:44 -0700 | [diff] [blame] | 985 | if (i == 1 && bonus_data) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 986 | printf(" using %zu bytes of bonus data for chunk %d\n", bonus_size, i); |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 987 | src_chunks[i].data = |
| 988 | static_cast<unsigned char*>(realloc(src_chunks[i].data, src_chunks[i].len + bonus_size)); |
| 989 | memcpy(src_chunks[i].data + src_chunks[i].len, bonus_data, bonus_size); |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 990 | src_chunks[i].len += bonus_size; |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 991 | } |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 992 | |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 993 | patch_data[i] = MakePatch(src_chunks + i, tgt_chunks + i, patch_size + i, nullptr); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 994 | } |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 995 | printf("patch %3d is %zu bytes (of %zu)\n", i, patch_size[i], tgt_chunks[i].source_len); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 996 | } |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 997 | free(bsdiff_cache); |
| 998 | free(src_chunks); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 999 | |
| 1000 | // Figure out how big the imgdiff file header is going to be, so |
| 1001 | // that we can correctly compute the offset of each bsdiff patch |
| 1002 | // within the file. |
| 1003 | |
| 1004 | size_t total_header_size = 12; |
| 1005 | for (i = 0; i < num_tgt_chunks; ++i) { |
| 1006 | total_header_size += 4; |
| 1007 | switch (tgt_chunks[i].type) { |
| 1008 | case CHUNK_NORMAL: |
| 1009 | total_header_size += 8*3; |
| 1010 | break; |
| 1011 | case CHUNK_DEFLATE: |
| 1012 | total_header_size += 8*5 + 4*5; |
| 1013 | break; |
| 1014 | case CHUNK_RAW: |
| 1015 | total_header_size += 4 + patch_size[i]; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | size_t offset = total_header_size; |
| 1021 | |
| 1022 | FILE* f = fopen(argv[3], "wb"); |
| 1023 | |
| 1024 | // Write out the headers. |
| 1025 | |
| 1026 | fwrite("IMGDIFF2", 1, 8, f); |
| 1027 | Write4(num_tgt_chunks, f); |
| 1028 | for (i = 0; i < num_tgt_chunks; ++i) { |
| 1029 | Write4(tgt_chunks[i].type, f); |
| 1030 | |
| 1031 | switch (tgt_chunks[i].type) { |
| 1032 | case CHUNK_NORMAL: |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1033 | printf("chunk %3d: normal (%10zu, %10zu) %10zu\n", i, |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1034 | tgt_chunks[i].start, tgt_chunks[i].len, patch_size[i]); |
| 1035 | Write8(tgt_chunks[i].source_start, f); |
| 1036 | Write8(tgt_chunks[i].source_len, f); |
| 1037 | Write8(offset, f); |
| 1038 | offset += patch_size[i]; |
| 1039 | break; |
| 1040 | |
| 1041 | case CHUNK_DEFLATE: |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1042 | printf("chunk %3d: deflate (%10zu, %10zu) %10zu %s\n", i, |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1043 | tgt_chunks[i].start, tgt_chunks[i].deflate_len, patch_size[i], |
| 1044 | tgt_chunks[i].filename); |
| 1045 | Write8(tgt_chunks[i].source_start, f); |
| 1046 | Write8(tgt_chunks[i].source_len, f); |
| 1047 | Write8(offset, f); |
| 1048 | Write8(tgt_chunks[i].source_uncompressed_len, f); |
| 1049 | Write8(tgt_chunks[i].len, f); |
| 1050 | Write4(tgt_chunks[i].level, f); |
| 1051 | Write4(tgt_chunks[i].method, f); |
| 1052 | Write4(tgt_chunks[i].windowBits, f); |
| 1053 | Write4(tgt_chunks[i].memLevel, f); |
| 1054 | Write4(tgt_chunks[i].strategy, f); |
| 1055 | offset += patch_size[i]; |
| 1056 | break; |
| 1057 | |
| 1058 | case CHUNK_RAW: |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1059 | printf("chunk %3d: raw (%10zu, %10zu)\n", i, |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1060 | tgt_chunks[i].start, tgt_chunks[i].len); |
| 1061 | Write4(patch_size[i], f); |
| 1062 | fwrite(patch_data[i], 1, patch_size[i], f); |
| 1063 | break; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | // Append each chunk's bsdiff patch, in order. |
| 1068 | |
| 1069 | for (i = 0; i < num_tgt_chunks; ++i) { |
| 1070 | if (tgt_chunks[i].type != CHUNK_RAW) { |
| 1071 | fwrite(patch_data[i], 1, patch_size[i], f); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Sen Jiang | 930edb6 | 2017-01-18 17:26:42 -0800 | [diff] [blame] | 1075 | free(tgt_chunks); |
Rahul Chaudhry | 3a5177b | 2016-11-15 16:18:46 -0800 | [diff] [blame] | 1076 | free(patch_data); |
| 1077 | free(patch_size); |
| 1078 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1079 | fclose(f); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1080 | |
| 1081 | return 0; |
| 1082 | } |