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 | a897f95 | 2017-05-17 22:41:55 -0700 | [diff] [blame] | 29 | #include <memory> |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 30 | #include <string> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Tianjie Xu | a897f95 | 2017-05-17 22:41:55 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
| 34 | #include <android-base/memory.h> |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 35 | #include <applypatch/applypatch.h> |
| 36 | #include <applypatch/imgdiff.h> |
| 37 | #include <openssl/sha.h> |
| 38 | #include <zlib.h> |
| 39 | |
Tao Bao | 38d78d1 | 2017-10-09 11:03:38 -0700 | [diff] [blame] | 40 | #include "edify/expr.h" |
| 41 | |
Tianjie Xu | 12b9055 | 2017-03-07 14:44:14 -0800 | [diff] [blame] | 42 | static inline int64_t Read8(const void *address) { |
| 43 | return android::base::get_unaligned<int64_t>(address); |
| 44 | } |
| 45 | |
| 46 | static inline int32_t Read4(const void *address) { |
| 47 | return android::base::get_unaligned<int32_t>(address); |
| 48 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 49 | |
Tianjie Xu | a897f95 | 2017-05-17 22:41:55 -0700 | [diff] [blame] | 50 | // This function is a wrapper of ApplyBSDiffPatch(). It has a custom sink function to deflate the |
| 51 | // patched data and stream the deflated data to output. |
| 52 | static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_len, |
| 53 | const Value* patch, size_t patch_offset, |
| 54 | const char* deflate_header, SinkFn sink, SHA_CTX* ctx) { |
| 55 | size_t expected_target_length = static_cast<size_t>(Read8(deflate_header + 32)); |
| 56 | int level = Read4(deflate_header + 40); |
| 57 | int method = Read4(deflate_header + 44); |
| 58 | int window_bits = Read4(deflate_header + 48); |
| 59 | int mem_level = Read4(deflate_header + 52); |
| 60 | int strategy = Read4(deflate_header + 56); |
| 61 | |
| 62 | std::unique_ptr<z_stream, decltype(&deflateEnd)> strm(new z_stream(), deflateEnd); |
| 63 | strm->zalloc = Z_NULL; |
| 64 | strm->zfree = Z_NULL; |
| 65 | strm->opaque = Z_NULL; |
| 66 | strm->avail_in = 0; |
| 67 | strm->next_in = nullptr; |
| 68 | int ret = deflateInit2(strm.get(), level, method, window_bits, mem_level, strategy); |
| 69 | if (ret != Z_OK) { |
| 70 | LOG(ERROR) << "Failed to init uncompressed data deflation: " << ret; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | // Define a custom sink wrapper that feeds to bspatch. It deflates the available patch data on |
| 75 | // the fly and outputs the compressed data to the given sink. |
| 76 | size_t actual_target_length = 0; |
| 77 | size_t total_written = 0; |
| 78 | static constexpr size_t buffer_size = 32768; |
| 79 | auto compression_sink = [&](const uint8_t* data, size_t len) -> size_t { |
| 80 | // The input patch length for an update never exceeds INT_MAX. |
| 81 | strm->avail_in = len; |
| 82 | strm->next_in = data; |
| 83 | do { |
| 84 | std::vector<uint8_t> buffer(buffer_size); |
| 85 | strm->avail_out = buffer_size; |
| 86 | strm->next_out = buffer.data(); |
| 87 | if (actual_target_length + len < expected_target_length) { |
| 88 | ret = deflate(strm.get(), Z_NO_FLUSH); |
| 89 | } else { |
| 90 | ret = deflate(strm.get(), Z_FINISH); |
| 91 | } |
| 92 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 93 | LOG(ERROR) << "Failed to deflate stream: " << ret; |
| 94 | // zero length indicates an error in the sink function of bspatch(). |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | size_t have = buffer_size - strm->avail_out; |
| 99 | total_written += have; |
| 100 | if (sink(buffer.data(), have) != have) { |
| 101 | LOG(ERROR) << "Failed to write " << have << " compressed bytes to output."; |
| 102 | return 0; |
| 103 | } |
| 104 | if (ctx) SHA1_Update(ctx, buffer.data(), have); |
| 105 | } while ((strm->avail_in != 0 || strm->avail_out == 0) && ret != Z_STREAM_END); |
| 106 | |
| 107 | actual_target_length += len; |
| 108 | return len; |
| 109 | }; |
| 110 | |
| 111 | if (ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink, nullptr) != 0) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if (ret != Z_STREAM_END) { |
| 116 | LOG(ERROR) << "ret is expected to be Z_STREAM_END, but it's " << ret; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (expected_target_length != actual_target_length) { |
| 121 | LOG(ERROR) << "target length is expected to be " << expected_target_length << ", but it's " |
| 122 | << actual_target_length; |
| 123 | return false; |
| 124 | } |
| 125 | LOG(DEBUG) << "bspatch writes " << total_written << " bytes in total to streaming output."; |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 130 | int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data, |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 131 | size_t patch_size, SinkFn sink) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 132 | 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] | 133 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 134 | return ApplyImagePatch(old_data, old_size, &patch, sink, nullptr, nullptr); |
Sen Jiang | 0cce9cd | 2016-01-22 20:49:07 +0800 | [diff] [blame] | 135 | } |
| 136 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 137 | /* |
| 138 | * Apply the patch given in 'patch_filename' to the source data given |
| 139 | * by (old_data, old_size). Write the patched output to the 'output' |
| 140 | * file, and update the SHA context with the output data as well. |
| 141 | * Return 0 on success. |
| 142 | */ |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 143 | int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* patch, SinkFn sink, |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 144 | SHA_CTX* ctx, const Value* bonus_data) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 145 | if (patch->data.size() < 12) { |
| 146 | printf("patch too short to contain header\n"); |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW. |
| 151 | // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and |
| 152 | // CHUNK_GZIP.) |
| 153 | size_t pos = 12; |
| 154 | const char* header = &patch->data[0]; |
| 155 | if (memcmp(header, "IMGDIFF2", 8) != 0) { |
| 156 | printf("corrupt patch file header (magic number)\n"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | int num_chunks = Read4(header + 8); |
| 161 | |
| 162 | for (int i = 0; i < num_chunks; ++i) { |
| 163 | // each chunk's header record starts with 4 bytes. |
| 164 | if (pos + 4 > patch->data.size()) { |
| 165 | printf("failed to read chunk %d record\n", i); |
| 166 | return -1; |
| 167 | } |
| 168 | int type = Read4(&patch->data[pos]); |
| 169 | pos += 4; |
| 170 | |
| 171 | if (type == CHUNK_NORMAL) { |
| 172 | const char* normal_header = &patch->data[pos]; |
| 173 | pos += 24; |
| 174 | if (pos > patch->data.size()) { |
| 175 | printf("failed to read chunk %d normal header data\n", i); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 176 | return -1; |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 177 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 178 | |
Tianjie Xu | 12b9055 | 2017-03-07 14:44:14 -0800 | [diff] [blame] | 179 | size_t src_start = static_cast<size_t>(Read8(normal_header)); |
| 180 | size_t src_len = static_cast<size_t>(Read8(normal_header + 8)); |
| 181 | size_t patch_offset = static_cast<size_t>(Read8(normal_header + 16)); |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 182 | |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 183 | if (src_start + src_len > old_size) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 184 | printf("source data too short\n"); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 185 | return -1; |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 186 | } |
Jinguang Dong | 391bb7b | 2017-04-26 10:40:45 +0800 | [diff] [blame] | 187 | if (ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, ctx) != 0) { |
| 188 | printf("Failed to apply bsdiff patch.\n"); |
| 189 | return -1; |
| 190 | } |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 191 | } else if (type == CHUNK_RAW) { |
| 192 | const char* raw_header = &patch->data[pos]; |
| 193 | pos += 4; |
| 194 | if (pos > patch->data.size()) { |
| 195 | printf("failed to read chunk %d raw header data\n", i); |
| 196 | return -1; |
| 197 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 198 | |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 199 | size_t data_len = static_cast<size_t>(Read4(raw_header)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 200 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 201 | if (pos + data_len > patch->data.size()) { |
| 202 | printf("failed to read chunk %d raw data\n", i); |
| 203 | return -1; |
| 204 | } |
| 205 | if (ctx) SHA1_Update(ctx, &patch->data[pos], data_len); |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 206 | if (sink(reinterpret_cast<const unsigned char*>(&patch->data[pos]), data_len) != data_len) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 207 | printf("failed to write chunk %d raw data\n", i); |
| 208 | return -1; |
| 209 | } |
| 210 | pos += data_len; |
| 211 | } else if (type == CHUNK_DEFLATE) { |
| 212 | // deflate chunks have an additional 60 bytes in their chunk header. |
| 213 | const char* deflate_header = &patch->data[pos]; |
| 214 | pos += 60; |
| 215 | if (pos > patch->data.size()) { |
| 216 | printf("failed to read chunk %d deflate header data\n", i); |
| 217 | return -1; |
| 218 | } |
| 219 | |
Tianjie Xu | 12b9055 | 2017-03-07 14:44:14 -0800 | [diff] [blame] | 220 | size_t src_start = static_cast<size_t>(Read8(deflate_header)); |
| 221 | size_t src_len = static_cast<size_t>(Read8(deflate_header + 8)); |
| 222 | size_t patch_offset = static_cast<size_t>(Read8(deflate_header + 16)); |
| 223 | size_t expanded_len = static_cast<size_t>(Read8(deflate_header + 24)); |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 224 | |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 225 | if (src_start + src_len > old_size) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 226 | printf("source data too short\n"); |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | // Decompress the source data; the chunk header tells us exactly |
| 231 | // how big we expect it to be when decompressed. |
| 232 | |
| 233 | // Note: expanded_len will include the bonus data size if |
| 234 | // the patch was constructed with bonus data. The |
| 235 | // deflation will come up 'bonus_size' bytes short; these |
| 236 | // must be appended from the bonus_data value. |
| 237 | size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->data.size() : 0; |
| 238 | |
| 239 | std::vector<unsigned char> expanded_source(expanded_len); |
| 240 | |
| 241 | // inflate() doesn't like strm.next_out being a nullptr even with |
| 242 | // avail_out being zero (Z_STREAM_ERROR). |
| 243 | if (expanded_len != 0) { |
| 244 | z_stream strm; |
| 245 | strm.zalloc = Z_NULL; |
| 246 | strm.zfree = Z_NULL; |
| 247 | strm.opaque = Z_NULL; |
| 248 | strm.avail_in = src_len; |
Tao Bao | 087bc0c | 2017-01-19 10:46:39 -0800 | [diff] [blame] | 249 | strm.next_in = old_data + src_start; |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 250 | strm.avail_out = expanded_len; |
| 251 | strm.next_out = expanded_source.data(); |
| 252 | |
| 253 | int ret = inflateInit2(&strm, -15); |
| 254 | if (ret != Z_OK) { |
| 255 | printf("failed to init source inflation: %d\n", ret); |
| 256 | return -1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 257 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 258 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 259 | // Because we've provided enough room to accommodate the output |
| 260 | // data, we expect one call to inflate() to suffice. |
| 261 | ret = inflate(&strm, Z_SYNC_FLUSH); |
| 262 | if (ret != Z_STREAM_END) { |
| 263 | printf("source inflation returned %d\n", ret); |
| 264 | return -1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 265 | } |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 266 | // We should have filled the output buffer exactly, except |
| 267 | // for the bonus_size. |
| 268 | if (strm.avail_out != bonus_size) { |
| 269 | printf("source inflation short by %zu bytes\n", strm.avail_out - bonus_size); |
| 270 | return -1; |
| 271 | } |
| 272 | inflateEnd(&strm); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 273 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 274 | if (bonus_size) { |
| 275 | memcpy(expanded_source.data() + (expanded_len - bonus_size), &bonus_data->data[0], |
| 276 | bonus_size); |
| 277 | } |
| 278 | } |
| 279 | |
Tianjie Xu | a897f95 | 2017-05-17 22:41:55 -0700 | [diff] [blame] | 280 | if (!ApplyBSDiffPatchAndStreamOutput(expanded_source.data(), expanded_len, patch, |
| 281 | patch_offset, deflate_header, sink, ctx)) { |
| 282 | LOG(ERROR) << "Fail to apply streaming bspatch."; |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 283 | return -1; |
| 284 | } |
| 285 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 286 | } else { |
| 287 | printf("patch chunk %d is unknown type %d\n", i, type); |
| 288 | return -1; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 293 | } |