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> |
| 34 | #include <openssl/sha.h> |
| 35 | #include <zlib.h> |
| 36 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 37 | #include "utils.h" |
| 38 | |
Sen Jiang | 0cce9cd | 2016-01-22 20:49:07 +0800 | [diff] [blame] | 39 | int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, |
| 40 | const unsigned char* patch_data, ssize_t patch_size, |
| 41 | SinkFn sink, void* token) { |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 42 | 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] | 43 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 44 | return ApplyImagePatch(old_data, old_size, &patch, sink, token, nullptr, nullptr); |
Sen Jiang | 0cce9cd | 2016-01-22 20:49:07 +0800 | [diff] [blame] | 45 | } |
| 46 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 47 | /* |
| 48 | * Apply the patch given in 'patch_filename' to the source data given |
| 49 | * by (old_data, old_size). Write the patched output to the 'output' |
| 50 | * file, and update the SHA context with the output data as well. |
| 51 | * Return 0 on success. |
| 52 | */ |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 53 | int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, const Value* patch, |
| 54 | SinkFn sink, void* token, SHA_CTX* ctx, const Value* bonus_data) { |
| 55 | if (patch->data.size() < 12) { |
| 56 | printf("patch too short to contain header\n"); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 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 | size_t pos = 12; |
| 64 | const char* header = &patch->data[0]; |
| 65 | if (memcmp(header, "IMGDIFF2", 8) != 0) { |
| 66 | printf("corrupt patch file header (magic number)\n"); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | int num_chunks = Read4(header + 8); |
| 71 | |
| 72 | for (int i = 0; i < num_chunks; ++i) { |
| 73 | // each chunk's header record starts with 4 bytes. |
| 74 | if (pos + 4 > patch->data.size()) { |
| 75 | printf("failed to read chunk %d record\n", i); |
| 76 | return -1; |
| 77 | } |
| 78 | int type = Read4(&patch->data[pos]); |
| 79 | pos += 4; |
| 80 | |
| 81 | if (type == CHUNK_NORMAL) { |
| 82 | const char* normal_header = &patch->data[pos]; |
| 83 | pos += 24; |
| 84 | if (pos > patch->data.size()) { |
| 85 | printf("failed to read chunk %d normal header data\n", i); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 86 | return -1; |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 87 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 88 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 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 | |
| 93 | if (src_start + src_len > static_cast<size_t>(old_size)) { |
| 94 | printf("source data too short\n"); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 95 | return -1; |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 96 | } |
| 97 | ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, token, ctx); |
| 98 | } else if (type == CHUNK_RAW) { |
| 99 | const char* raw_header = &patch->data[pos]; |
| 100 | pos += 4; |
| 101 | if (pos > patch->data.size()) { |
| 102 | printf("failed to read chunk %d raw header data\n", i); |
| 103 | return -1; |
| 104 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 105 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 106 | ssize_t data_len = Read4(raw_header); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 107 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 108 | if (pos + data_len > patch->data.size()) { |
| 109 | printf("failed to read chunk %d raw data\n", i); |
| 110 | return -1; |
| 111 | } |
| 112 | if (ctx) SHA1_Update(ctx, &patch->data[pos], data_len); |
| 113 | if (sink(reinterpret_cast<const unsigned char*>(&patch->data[pos]), data_len, token) != |
| 114 | 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 | const char* deflate_header = &patch->data[pos]; |
| 122 | pos += 60; |
| 123 | if (pos > patch->data.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 | |
| 139 | if (src_start + src_len > static_cast<size_t>(old_size)) { |
| 140 | printf("source data too short\n"); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | // Decompress the source data; the chunk header tells us exactly |
| 145 | // how big we expect it to be when decompressed. |
| 146 | |
| 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->data.size() : 0; |
| 152 | |
| 153 | std::vector<unsigned char> expanded_source(expanded_len); |
| 154 | |
| 155 | // inflate() doesn't like strm.next_out being a nullptr even with |
| 156 | // avail_out being zero (Z_STREAM_ERROR). |
| 157 | if (expanded_len != 0) { |
| 158 | z_stream strm; |
| 159 | strm.zalloc = Z_NULL; |
| 160 | strm.zfree = Z_NULL; |
| 161 | strm.opaque = Z_NULL; |
| 162 | strm.avail_in = src_len; |
| 163 | strm.next_in = const_cast<unsigned char*>(old_data + src_start); |
| 164 | strm.avail_out = expanded_len; |
| 165 | strm.next_out = expanded_source.data(); |
| 166 | |
| 167 | int ret = inflateInit2(&strm, -15); |
| 168 | if (ret != Z_OK) { |
| 169 | printf("failed to init source inflation: %d\n", ret); |
| 170 | return -1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 171 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 172 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 173 | // Because we've provided enough room to accommodate the output |
| 174 | // data, we expect one call to inflate() to suffice. |
| 175 | ret = inflate(&strm, Z_SYNC_FLUSH); |
| 176 | if (ret != Z_STREAM_END) { |
| 177 | printf("source inflation returned %d\n", ret); |
| 178 | return -1; |
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 | // We should have filled the output buffer exactly, except |
| 181 | // for the bonus_size. |
| 182 | if (strm.avail_out != bonus_size) { |
| 183 | printf("source inflation short by %zu bytes\n", strm.avail_out - bonus_size); |
| 184 | return -1; |
| 185 | } |
| 186 | inflateEnd(&strm); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 187 | |
Tao Bao | 97555da | 2016-12-15 10:15:06 -0800 | [diff] [blame] | 188 | if (bonus_size) { |
| 189 | memcpy(expanded_source.data() + (expanded_len - bonus_size), &bonus_data->data[0], |
| 190 | bonus_size); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Next, apply the bsdiff patch (in memory) to the uncompressed data. |
| 195 | std::vector<unsigned char> uncompressed_target_data; |
| 196 | if (ApplyBSDiffPatchMem(expanded_source.data(), expanded_len, patch, patch_offset, |
| 197 | &uncompressed_target_data) != 0) { |
| 198 | return -1; |
| 199 | } |
| 200 | if (uncompressed_target_data.size() != target_len) { |
| 201 | printf("expected target len to be %zu, but it's %zu\n", target_len, |
| 202 | uncompressed_target_data.size()); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | // Now compress the target data and append it to the output. |
| 207 | |
| 208 | // we're done with the expanded_source data buffer, so we'll |
| 209 | // reuse that memory to receive the output of deflate. |
| 210 | if (expanded_source.size() < 32768U) { |
| 211 | expanded_source.resize(32768U); |
| 212 | } |
| 213 | |
| 214 | { |
| 215 | std::vector<unsigned char>& temp_data = expanded_source; |
| 216 | |
| 217 | // now the deflate stream |
| 218 | z_stream strm; |
| 219 | strm.zalloc = Z_NULL; |
| 220 | strm.zfree = Z_NULL; |
| 221 | strm.opaque = Z_NULL; |
| 222 | strm.avail_in = uncompressed_target_data.size(); |
| 223 | strm.next_in = uncompressed_target_data.data(); |
| 224 | int ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy); |
| 225 | if (ret != Z_OK) { |
| 226 | printf("failed to init uncompressed data deflation: %d\n", ret); |
| 227 | return -1; |
| 228 | } |
| 229 | do { |
| 230 | strm.avail_out = temp_data.size(); |
| 231 | strm.next_out = temp_data.data(); |
| 232 | ret = deflate(&strm, Z_FINISH); |
| 233 | ssize_t have = temp_data.size() - strm.avail_out; |
| 234 | |
| 235 | if (sink(temp_data.data(), have, token) != have) { |
| 236 | printf("failed to write %zd compressed bytes to output\n", have); |
| 237 | return -1; |
| 238 | } |
| 239 | if (ctx) SHA1_Update(ctx, temp_data.data(), have); |
| 240 | } while (ret != Z_STREAM_END); |
| 241 | deflateEnd(&strm); |
| 242 | } |
| 243 | } else { |
| 244 | printf("patch chunk %d is unknown type %d\n", i, type); |
| 245 | return -1; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 250 | } |