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