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