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