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 | // See imgdiff.c in this directory for a description of the patch file |
| 18 | // format. |
| 19 | |
| 20 | #include <stdio.h> |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 21 | #include <sys/cdefs.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <errno.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 24 | #include <malloc.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #include "zlib.h" |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 29 | #include "openssl/sha.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 30 | #include "applypatch.h" |
| 31 | #include "imgdiff.h" |
| 32 | #include "utils.h" |
| 33 | |
Sen Jiang | 0cce9cd | 2016-01-22 20:49:07 +0800 | [diff] [blame] | 34 | int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, |
| 35 | const unsigned char* patch_data, ssize_t patch_size, |
| 36 | SinkFn sink, void* token) { |
| 37 | Value patch = {VAL_BLOB, patch_size, |
| 38 | reinterpret_cast<char*>(const_cast<unsigned char*>(patch_data))}; |
| 39 | return ApplyImagePatch( |
| 40 | old_data, old_size, &patch, sink, token, nullptr, nullptr); |
| 41 | } |
| 42 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 43 | /* |
| 44 | * Apply the patch given in 'patch_filename' to the source data given |
| 45 | * by (old_data, old_size). Write the patched output to the 'output' |
| 46 | * file, and update the SHA context with the output data as well. |
| 47 | * Return 0 on success. |
| 48 | */ |
Sen Jiang | 0cce9cd | 2016-01-22 20:49:07 +0800 | [diff] [blame] | 49 | int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 50 | const Value* patch, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 51 | SinkFn sink, void* token, SHA_CTX* ctx, |
| 52 | const Value* bonus_data) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 53 | ssize_t pos = 12; |
| 54 | char* header = patch->data; |
| 55 | if (patch->size < 12) { |
| 56 | printf("patch too short to contain header\n"); |
| 57 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 60 | // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW. |
| 61 | // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and |
| 62 | // CHUNK_GZIP.) |
| 63 | if (memcmp(header, "IMGDIFF2", 8) != 0) { |
| 64 | printf("corrupt patch file header (magic number)\n"); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 65 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 66 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 67 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 68 | int num_chunks = Read4(header+8); |
| 69 | |
| 70 | int i; |
| 71 | for (i = 0; i < num_chunks; ++i) { |
| 72 | // each chunk's header record starts with 4 bytes. |
| 73 | if (pos + 4 > patch->size) { |
| 74 | printf("failed to read chunk %d record\n", i); |
| 75 | return -1; |
| 76 | } |
| 77 | int type = Read4(patch->data + pos); |
| 78 | pos += 4; |
| 79 | |
| 80 | if (type == CHUNK_NORMAL) { |
| 81 | char* normal_header = patch->data + pos; |
| 82 | pos += 24; |
| 83 | if (pos > patch->size) { |
| 84 | printf("failed to read chunk %d normal header data\n", i); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | size_t src_start = Read8(normal_header); |
| 89 | size_t src_len = Read8(normal_header+8); |
| 90 | size_t patch_offset = Read8(normal_header+16); |
| 91 | |
Sen Jiang | 0cce9cd | 2016-01-22 20:49:07 +0800 | [diff] [blame] | 92 | if (src_start + src_len > static_cast<size_t>(old_size)) { |
| 93 | printf("source data too short\n"); |
| 94 | return -1; |
| 95 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 96 | ApplyBSDiffPatch(old_data + src_start, src_len, |
| 97 | patch, patch_offset, sink, token, ctx); |
| 98 | } else if (type == CHUNK_RAW) { |
| 99 | char* raw_header = patch->data + pos; |
| 100 | pos += 4; |
| 101 | if (pos > patch->size) { |
| 102 | printf("failed to read chunk %d raw header data\n", i); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | ssize_t data_len = Read4(raw_header); |
| 107 | |
| 108 | if (pos + data_len > patch->size) { |
| 109 | printf("failed to read chunk %d raw data\n", i); |
| 110 | return -1; |
| 111 | } |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 112 | if (ctx) SHA1_Update(ctx, patch->data + pos, data_len); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 113 | if (sink((unsigned char*)patch->data + pos, |
| 114 | data_len, token) != data_len) { |
| 115 | printf("failed to write chunk %d raw data\n", i); |
| 116 | return -1; |
| 117 | } |
| 118 | pos += data_len; |
| 119 | } else if (type == CHUNK_DEFLATE) { |
| 120 | // deflate chunks have an additional 60 bytes in their chunk header. |
| 121 | char* deflate_header = patch->data + pos; |
| 122 | pos += 60; |
| 123 | if (pos > patch->size) { |
| 124 | printf("failed to read chunk %d deflate header data\n", i); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | size_t src_start = Read8(deflate_header); |
| 129 | size_t src_len = Read8(deflate_header+8); |
| 130 | size_t patch_offset = Read8(deflate_header+16); |
| 131 | size_t expanded_len = Read8(deflate_header+24); |
| 132 | size_t target_len = Read8(deflate_header+32); |
| 133 | int level = Read4(deflate_header+40); |
| 134 | int method = Read4(deflate_header+44); |
| 135 | int windowBits = Read4(deflate_header+48); |
| 136 | int memLevel = Read4(deflate_header+52); |
| 137 | int strategy = Read4(deflate_header+56); |
| 138 | |
Sen Jiang | 0cce9cd | 2016-01-22 20:49:07 +0800 | [diff] [blame] | 139 | if (src_start + src_len > static_cast<size_t>(old_size)) { |
| 140 | printf("source data too short\n"); |
| 141 | return -1; |
| 142 | } |
| 143 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 144 | // Decompress the source data; the chunk header tells us exactly |
| 145 | // how big we expect it to be when decompressed. |
| 146 | |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 147 | // Note: expanded_len will include the bonus data size if |
| 148 | // the patch was constructed with bonus data. The |
| 149 | // deflation will come up 'bonus_size' bytes short; these |
| 150 | // must be appended from the bonus_data value. |
| 151 | size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0; |
| 152 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 153 | unsigned char* expanded_source = reinterpret_cast<unsigned char*>(malloc(expanded_len)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 154 | if (expanded_source == NULL) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 155 | printf("failed to allocate %zu bytes for expanded_source\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 156 | expanded_len); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | z_stream strm; |
| 161 | strm.zalloc = Z_NULL; |
| 162 | strm.zfree = Z_NULL; |
| 163 | strm.opaque = Z_NULL; |
| 164 | strm.avail_in = src_len; |
| 165 | strm.next_in = (unsigned char*)(old_data + src_start); |
| 166 | strm.avail_out = expanded_len; |
| 167 | strm.next_out = expanded_source; |
| 168 | |
| 169 | int ret; |
| 170 | ret = inflateInit2(&strm, -15); |
| 171 | if (ret != Z_OK) { |
| 172 | printf("failed to init source inflation: %d\n", ret); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | // Because we've provided enough room to accommodate the output |
| 177 | // data, we expect one call to inflate() to suffice. |
| 178 | ret = inflate(&strm, Z_SYNC_FLUSH); |
| 179 | if (ret != Z_STREAM_END) { |
| 180 | printf("source inflation returned %d\n", ret); |
| 181 | return -1; |
| 182 | } |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 183 | // We should have filled the output buffer exactly, except |
| 184 | // for the bonus_size. |
| 185 | if (strm.avail_out != bonus_size) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 186 | printf("source inflation short by %zu bytes\n", strm.avail_out-bonus_size); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | inflateEnd(&strm); |
| 190 | |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 191 | if (bonus_size) { |
| 192 | memcpy(expanded_source + (expanded_len - bonus_size), |
| 193 | bonus_data->data, bonus_size); |
| 194 | } |
| 195 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 196 | // Next, apply the bsdiff patch (in memory) to the uncompressed |
| 197 | // data. |
| 198 | unsigned char* uncompressed_target_data; |
| 199 | ssize_t uncompressed_target_size; |
| 200 | if (ApplyBSDiffPatchMem(expanded_source, expanded_len, |
| 201 | patch, patch_offset, |
| 202 | &uncompressed_target_data, |
| 203 | &uncompressed_target_size) != 0) { |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | // Now compress the target data and append it to the output. |
| 208 | |
| 209 | // we're done with the expanded_source data buffer, so we'll |
| 210 | // reuse that memory to receive the output of deflate. |
| 211 | unsigned char* temp_data = expanded_source; |
| 212 | ssize_t temp_size = expanded_len; |
| 213 | if (temp_size < 32768) { |
| 214 | // ... unless the buffer is too small, in which case we'll |
| 215 | // allocate a fresh one. |
| 216 | free(temp_data); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 217 | temp_data = reinterpret_cast<unsigned char*>(malloc(32768)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 218 | temp_size = 32768; |
| 219 | } |
| 220 | |
| 221 | // now the deflate stream |
| 222 | strm.zalloc = Z_NULL; |
| 223 | strm.zfree = Z_NULL; |
| 224 | strm.opaque = Z_NULL; |
| 225 | strm.avail_in = uncompressed_target_size; |
| 226 | strm.next_in = uncompressed_target_data; |
| 227 | ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy); |
| 228 | do { |
| 229 | strm.avail_out = temp_size; |
| 230 | strm.next_out = temp_data; |
| 231 | ret = deflate(&strm, Z_FINISH); |
| 232 | ssize_t have = temp_size - strm.avail_out; |
| 233 | |
| 234 | if (sink(temp_data, have, token) != have) { |
| 235 | printf("failed to write %ld compressed bytes to output\n", |
| 236 | (long)have); |
| 237 | return -1; |
| 238 | } |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 239 | if (ctx) SHA1_Update(ctx, temp_data, have); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 240 | } while (ret != Z_STREAM_END); |
| 241 | deflateEnd(&strm); |
| 242 | |
| 243 | free(temp_data); |
| 244 | free(uncompressed_target_data); |
| 245 | } else { |
| 246 | printf("patch chunk %d is unknown type %d\n", i, type); |
| 247 | return -1; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 252 | } |