blob: 25ba0a182e1ef809614608845b0f53abe8b17126 [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
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 Bao97555da2016-12-15 10:15:06 -080017// See imgdiff.cpp in this directory for a description of the patch file
Doug Zongker512536a2010-02-17 16:11:44 -080018// format.
19
Tao Bao97555da2016-12-15 10:15:06 -080020#include <applypatch/imgpatch.h>
21
22#include <errno.h>
Doug Zongker512536a2010-02-17 16:11:44 -080023#include <stdio.h>
Tao Bao97555da2016-12-15 10:15:06 -080024#include <string.h>
Mark Salyzynf3bb31c2014-03-14 09:39:48 -070025#include <sys/cdefs.h>
Doug Zongker512536a2010-02-17 16:11:44 -080026#include <sys/stat.h>
Doug Zongker512536a2010-02-17 16:11:44 -080027#include <unistd.h>
Doug Zongker512536a2010-02-17 16:11:44 -080028
Tianjie Xua897f952017-05-17 22:41:55 -070029#include <memory>
Tianjie Xuaced5d92016-10-12 10:55:04 -070030#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080031#include <vector>
32
Tianjie Xua897f952017-05-17 22:41:55 -070033#include <android-base/logging.h>
34#include <android-base/memory.h>
Tao Bao97555da2016-12-15 10:15:06 -080035#include <applypatch/applypatch.h>
36#include <applypatch/imgdiff.h>
37#include <openssl/sha.h>
38#include <zlib.h>
39
Tao Bao38d78d12017-10-09 11:03:38 -070040#include "edify/expr.h"
41
Tianjie Xu12b90552017-03-07 14:44:14 -080042static inline int64_t Read8(const void *address) {
43 return android::base::get_unaligned<int64_t>(address);
44}
45
46static inline int32_t Read4(const void *address) {
47 return android::base::get_unaligned<int32_t>(address);
48}
Doug Zongker512536a2010-02-17 16:11:44 -080049
Tianjie Xua897f952017-05-17 22:41:55 -070050// 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.
52static 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
Tao Baofdec1032017-10-24 12:25:41 -070062 z_stream strm;
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, level, method, window_bits, mem_level, strategy);
Tianjie Xua897f952017-05-17 22:41:55 -070069 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;
Tao Baofdec1032017-10-24 12:25:41 -070079 auto compression_sink = [&strm, &actual_target_length, &expected_target_length, &total_written,
80 &ret, &ctx, &sink](const uint8_t* data, size_t len) -> size_t {
Tianjie Xua897f952017-05-17 22:41:55 -070081 // The input patch length for an update never exceeds INT_MAX.
Tao Baofdec1032017-10-24 12:25:41 -070082 strm.avail_in = len;
83 strm.next_in = data;
Tianjie Xua897f952017-05-17 22:41:55 -070084 do {
85 std::vector<uint8_t> buffer(buffer_size);
Tao Baofdec1032017-10-24 12:25:41 -070086 strm.avail_out = buffer_size;
87 strm.next_out = buffer.data();
Tianjie Xua897f952017-05-17 22:41:55 -070088 if (actual_target_length + len < expected_target_length) {
Tao Baofdec1032017-10-24 12:25:41 -070089 ret = deflate(&strm, Z_NO_FLUSH);
Tianjie Xua897f952017-05-17 22:41:55 -070090 } else {
Tao Baofdec1032017-10-24 12:25:41 -070091 ret = deflate(&strm, Z_FINISH);
Tianjie Xua897f952017-05-17 22:41:55 -070092 }
93 if (ret != Z_OK && ret != Z_STREAM_END) {
94 LOG(ERROR) << "Failed to deflate stream: " << ret;
95 // zero length indicates an error in the sink function of bspatch().
96 return 0;
97 }
98
Tao Baofdec1032017-10-24 12:25:41 -070099 size_t have = buffer_size - strm.avail_out;
Tianjie Xua897f952017-05-17 22:41:55 -0700100 total_written += have;
101 if (sink(buffer.data(), have) != have) {
102 LOG(ERROR) << "Failed to write " << have << " compressed bytes to output.";
103 return 0;
104 }
105 if (ctx) SHA1_Update(ctx, buffer.data(), have);
Tao Baofdec1032017-10-24 12:25:41 -0700106 } while ((strm.avail_in != 0 || strm.avail_out == 0) && ret != Z_STREAM_END);
Tianjie Xua897f952017-05-17 22:41:55 -0700107
108 actual_target_length += len;
109 return len;
110 };
111
Tao Baofdec1032017-10-24 12:25:41 -0700112 int bspatch_result =
113 ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink, nullptr);
114 deflateEnd(&strm);
115
116 if (bspatch_result != 0) {
Tianjie Xua897f952017-05-17 22:41:55 -0700117 return false;
118 }
119
120 if (ret != Z_STREAM_END) {
121 LOG(ERROR) << "ret is expected to be Z_STREAM_END, but it's " << ret;
122 return false;
123 }
124
125 if (expected_target_length != actual_target_length) {
126 LOG(ERROR) << "target length is expected to be " << expected_target_length << ", but it's "
127 << actual_target_length;
128 return false;
129 }
130 LOG(DEBUG) << "bspatch writes " << total_written << " bytes in total to streaming output.";
131
132 return true;
133}
134
Tao Baof7eb7602017-03-27 15:12:48 -0700135int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data,
Tao Baoc0e1c462017-02-01 10:20:10 -0800136 size_t patch_size, SinkFn sink) {
Tao Bao97555da2016-12-15 10:15:06 -0800137 Value patch(VAL_BLOB, std::string(reinterpret_cast<const char*>(patch_data), patch_size));
Tianjie Xuaced5d92016-10-12 10:55:04 -0700138
Tao Baoc0e1c462017-02-01 10:20:10 -0800139 return ApplyImagePatch(old_data, old_size, &patch, sink, nullptr, nullptr);
Sen Jiang0cce9cd2016-01-22 20:49:07 +0800140}
141
Doug Zongker512536a2010-02-17 16:11:44 -0800142/*
143 * Apply the patch given in 'patch_filename' to the source data given
144 * by (old_data, old_size). Write the patched output to the 'output'
145 * file, and update the SHA context with the output data as well.
146 * Return 0 on success.
147 */
Tao Baof7eb7602017-03-27 15:12:48 -0700148int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* patch, SinkFn sink,
Tao Baoc0e1c462017-02-01 10:20:10 -0800149 SHA_CTX* ctx, const Value* bonus_data) {
Tao Bao97555da2016-12-15 10:15:06 -0800150 if (patch->data.size() < 12) {
151 printf("patch too short to contain header\n");
152 return -1;
153 }
154
155 // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
156 // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and
157 // CHUNK_GZIP.)
158 size_t pos = 12;
159 const char* header = &patch->data[0];
160 if (memcmp(header, "IMGDIFF2", 8) != 0) {
161 printf("corrupt patch file header (magic number)\n");
162 return -1;
163 }
164
165 int num_chunks = Read4(header + 8);
166
167 for (int i = 0; i < num_chunks; ++i) {
168 // each chunk's header record starts with 4 bytes.
169 if (pos + 4 > patch->data.size()) {
170 printf("failed to read chunk %d record\n", i);
171 return -1;
172 }
173 int type = Read4(&patch->data[pos]);
174 pos += 4;
175
176 if (type == CHUNK_NORMAL) {
177 const char* normal_header = &patch->data[pos];
178 pos += 24;
179 if (pos > patch->data.size()) {
180 printf("failed to read chunk %d normal header data\n", i);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800181 return -1;
Tao Bao97555da2016-12-15 10:15:06 -0800182 }
Doug Zongker512536a2010-02-17 16:11:44 -0800183
Tianjie Xu12b90552017-03-07 14:44:14 -0800184 size_t src_start = static_cast<size_t>(Read8(normal_header));
185 size_t src_len = static_cast<size_t>(Read8(normal_header + 8));
186 size_t patch_offset = static_cast<size_t>(Read8(normal_header + 16));
Tao Bao97555da2016-12-15 10:15:06 -0800187
Tao Baof7eb7602017-03-27 15:12:48 -0700188 if (src_start + src_len > old_size) {
Tao Bao97555da2016-12-15 10:15:06 -0800189 printf("source data too short\n");
Doug Zongker512536a2010-02-17 16:11:44 -0800190 return -1;
Tao Bao97555da2016-12-15 10:15:06 -0800191 }
Jinguang Dong391bb7b2017-04-26 10:40:45 +0800192 if (ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, ctx) != 0) {
193 printf("Failed to apply bsdiff patch.\n");
194 return -1;
195 }
Tao Bao97555da2016-12-15 10:15:06 -0800196 } else if (type == CHUNK_RAW) {
197 const char* raw_header = &patch->data[pos];
198 pos += 4;
199 if (pos > patch->data.size()) {
200 printf("failed to read chunk %d raw header data\n", i);
201 return -1;
202 }
Doug Zongker512536a2010-02-17 16:11:44 -0800203
Tao Baof7eb7602017-03-27 15:12:48 -0700204 size_t data_len = static_cast<size_t>(Read4(raw_header));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800205
Tao Bao97555da2016-12-15 10:15:06 -0800206 if (pos + data_len > patch->data.size()) {
207 printf("failed to read chunk %d raw data\n", i);
208 return -1;
209 }
210 if (ctx) SHA1_Update(ctx, &patch->data[pos], data_len);
Tao Baoc0e1c462017-02-01 10:20:10 -0800211 if (sink(reinterpret_cast<const unsigned char*>(&patch->data[pos]), data_len) != data_len) {
Tao Bao97555da2016-12-15 10:15:06 -0800212 printf("failed to write chunk %d raw data\n", i);
213 return -1;
214 }
215 pos += data_len;
216 } else if (type == CHUNK_DEFLATE) {
217 // deflate chunks have an additional 60 bytes in their chunk header.
218 const char* deflate_header = &patch->data[pos];
219 pos += 60;
220 if (pos > patch->data.size()) {
221 printf("failed to read chunk %d deflate header data\n", i);
222 return -1;
223 }
224
Tianjie Xu12b90552017-03-07 14:44:14 -0800225 size_t src_start = static_cast<size_t>(Read8(deflate_header));
226 size_t src_len = static_cast<size_t>(Read8(deflate_header + 8));
227 size_t patch_offset = static_cast<size_t>(Read8(deflate_header + 16));
228 size_t expanded_len = static_cast<size_t>(Read8(deflate_header + 24));
Tao Bao97555da2016-12-15 10:15:06 -0800229
Tao Baof7eb7602017-03-27 15:12:48 -0700230 if (src_start + src_len > old_size) {
Tao Bao97555da2016-12-15 10:15:06 -0800231 printf("source data too short\n");
232 return -1;
233 }
234
235 // Decompress the source data; the chunk header tells us exactly
236 // how big we expect it to be when decompressed.
237
238 // Note: expanded_len will include the bonus data size if
239 // the patch was constructed with bonus data. The
240 // deflation will come up 'bonus_size' bytes short; these
241 // must be appended from the bonus_data value.
242 size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->data.size() : 0;
243
244 std::vector<unsigned char> expanded_source(expanded_len);
245
246 // inflate() doesn't like strm.next_out being a nullptr even with
247 // avail_out being zero (Z_STREAM_ERROR).
248 if (expanded_len != 0) {
249 z_stream strm;
250 strm.zalloc = Z_NULL;
251 strm.zfree = Z_NULL;
252 strm.opaque = Z_NULL;
253 strm.avail_in = src_len;
Tao Bao087bc0c2017-01-19 10:46:39 -0800254 strm.next_in = old_data + src_start;
Tao Bao97555da2016-12-15 10:15:06 -0800255 strm.avail_out = expanded_len;
256 strm.next_out = expanded_source.data();
257
258 int ret = inflateInit2(&strm, -15);
259 if (ret != Z_OK) {
260 printf("failed to init source inflation: %d\n", ret);
261 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800262 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800263
Tao Bao97555da2016-12-15 10:15:06 -0800264 // Because we've provided enough room to accommodate the output
265 // data, we expect one call to inflate() to suffice.
266 ret = inflate(&strm, Z_SYNC_FLUSH);
267 if (ret != Z_STREAM_END) {
268 printf("source inflation returned %d\n", ret);
269 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800270 }
Tao Bao97555da2016-12-15 10:15:06 -0800271 // We should have filled the output buffer exactly, except
272 // for the bonus_size.
273 if (strm.avail_out != bonus_size) {
274 printf("source inflation short by %zu bytes\n", strm.avail_out - bonus_size);
275 return -1;
276 }
277 inflateEnd(&strm);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800278
Tao Bao97555da2016-12-15 10:15:06 -0800279 if (bonus_size) {
280 memcpy(expanded_source.data() + (expanded_len - bonus_size), &bonus_data->data[0],
281 bonus_size);
282 }
283 }
284
Tianjie Xua897f952017-05-17 22:41:55 -0700285 if (!ApplyBSDiffPatchAndStreamOutput(expanded_source.data(), expanded_len, patch,
286 patch_offset, deflate_header, sink, ctx)) {
287 LOG(ERROR) << "Fail to apply streaming bspatch.";
Tao Bao97555da2016-12-15 10:15:06 -0800288 return -1;
289 }
290
Tao Bao97555da2016-12-15 10:15:06 -0800291 } else {
292 printf("patch chunk %d is unknown type %d\n", i, type);
293 return -1;
294 }
295 }
296
297 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800298}