blob: 7d8b7361c1bc8e2a8b9335838ce94fb4c7c0fb0d [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 Xuaced5d92016-10-12 10:55:04 -070029#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080030#include <vector>
31
Tao Bao97555da2016-12-15 10:15:06 -080032#include <applypatch/applypatch.h>
33#include <applypatch/imgdiff.h>
Tianjie Xu12b90552017-03-07 14:44:14 -080034#include <android-base/memory.h>
Tao Bao97555da2016-12-15 10:15:06 -080035#include <openssl/sha.h>
36#include <zlib.h>
37
Tianjie Xu12b90552017-03-07 14:44:14 -080038static inline int64_t Read8(const void *address) {
39 return android::base::get_unaligned<int64_t>(address);
40}
41
42static inline int32_t Read4(const void *address) {
43 return android::base::get_unaligned<int32_t>(address);
44}
Doug Zongker512536a2010-02-17 16:11:44 -080045
Tao Baof7eb7602017-03-27 15:12:48 -070046int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data,
Tao Baoc0e1c462017-02-01 10:20:10 -080047 size_t patch_size, SinkFn sink) {
Tao Bao97555da2016-12-15 10:15:06 -080048 Value patch(VAL_BLOB, std::string(reinterpret_cast<const char*>(patch_data), patch_size));
Tianjie Xuaced5d92016-10-12 10:55:04 -070049
Tao Baoc0e1c462017-02-01 10:20:10 -080050 return ApplyImagePatch(old_data, old_size, &patch, sink, nullptr, nullptr);
Sen Jiang0cce9cd2016-01-22 20:49:07 +080051}
52
Doug Zongker512536a2010-02-17 16:11:44 -080053/*
54 * Apply the patch given in 'patch_filename' to the source data given
55 * by (old_data, old_size). Write the patched output to the 'output'
56 * file, and update the SHA context with the output data as well.
57 * Return 0 on success.
58 */
Tao Baof7eb7602017-03-27 15:12:48 -070059int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* patch, SinkFn sink,
Tao Baoc0e1c462017-02-01 10:20:10 -080060 SHA_CTX* ctx, const Value* bonus_data) {
Tao Bao97555da2016-12-15 10:15:06 -080061 if (patch->data.size() < 12) {
62 printf("patch too short to contain header\n");
63 return -1;
64 }
65
66 // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
67 // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and
68 // CHUNK_GZIP.)
69 size_t pos = 12;
70 const char* header = &patch->data[0];
71 if (memcmp(header, "IMGDIFF2", 8) != 0) {
72 printf("corrupt patch file header (magic number)\n");
73 return -1;
74 }
75
76 int num_chunks = Read4(header + 8);
77
78 for (int i = 0; i < num_chunks; ++i) {
79 // each chunk's header record starts with 4 bytes.
80 if (pos + 4 > patch->data.size()) {
81 printf("failed to read chunk %d record\n", i);
82 return -1;
83 }
84 int type = Read4(&patch->data[pos]);
85 pos += 4;
86
87 if (type == CHUNK_NORMAL) {
88 const char* normal_header = &patch->data[pos];
89 pos += 24;
90 if (pos > patch->data.size()) {
91 printf("failed to read chunk %d normal header data\n", i);
Doug Zongkerc4351c72010-02-22 14:46:32 -080092 return -1;
Tao Bao97555da2016-12-15 10:15:06 -080093 }
Doug Zongker512536a2010-02-17 16:11:44 -080094
Tianjie Xu12b90552017-03-07 14:44:14 -080095 size_t src_start = static_cast<size_t>(Read8(normal_header));
96 size_t src_len = static_cast<size_t>(Read8(normal_header + 8));
97 size_t patch_offset = static_cast<size_t>(Read8(normal_header + 16));
Tao Bao97555da2016-12-15 10:15:06 -080098
Tao Baof7eb7602017-03-27 15:12:48 -070099 if (src_start + src_len > old_size) {
Tao Bao97555da2016-12-15 10:15:06 -0800100 printf("source data too short\n");
Doug Zongker512536a2010-02-17 16:11:44 -0800101 return -1;
Tao Bao97555da2016-12-15 10:15:06 -0800102 }
Tao Baoc0e1c462017-02-01 10:20:10 -0800103 ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, ctx);
Tao Bao97555da2016-12-15 10:15:06 -0800104 } else if (type == CHUNK_RAW) {
105 const char* raw_header = &patch->data[pos];
106 pos += 4;
107 if (pos > patch->data.size()) {
108 printf("failed to read chunk %d raw header data\n", i);
109 return -1;
110 }
Doug Zongker512536a2010-02-17 16:11:44 -0800111
Tao Baof7eb7602017-03-27 15:12:48 -0700112 size_t data_len = static_cast<size_t>(Read4(raw_header));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800113
Tao Bao97555da2016-12-15 10:15:06 -0800114 if (pos + data_len > patch->data.size()) {
115 printf("failed to read chunk %d raw data\n", i);
116 return -1;
117 }
118 if (ctx) SHA1_Update(ctx, &patch->data[pos], data_len);
Tao Baoc0e1c462017-02-01 10:20:10 -0800119 if (sink(reinterpret_cast<const unsigned char*>(&patch->data[pos]), data_len) != data_len) {
Tao Bao97555da2016-12-15 10:15:06 -0800120 printf("failed to write chunk %d raw data\n", i);
121 return -1;
122 }
123 pos += data_len;
124 } else if (type == CHUNK_DEFLATE) {
125 // deflate chunks have an additional 60 bytes in their chunk header.
126 const char* deflate_header = &patch->data[pos];
127 pos += 60;
128 if (pos > patch->data.size()) {
129 printf("failed to read chunk %d deflate header data\n", i);
130 return -1;
131 }
132
Tianjie Xu12b90552017-03-07 14:44:14 -0800133 size_t src_start = static_cast<size_t>(Read8(deflate_header));
134 size_t src_len = static_cast<size_t>(Read8(deflate_header + 8));
135 size_t patch_offset = static_cast<size_t>(Read8(deflate_header + 16));
136 size_t expanded_len = static_cast<size_t>(Read8(deflate_header + 24));
137 size_t target_len = static_cast<size_t>(Read8(deflate_header + 32));
Tao Bao97555da2016-12-15 10:15:06 -0800138 int level = Read4(deflate_header + 40);
139 int method = Read4(deflate_header + 44);
140 int windowBits = Read4(deflate_header + 48);
141 int memLevel = Read4(deflate_header + 52);
142 int strategy = Read4(deflate_header + 56);
143
Tao Baof7eb7602017-03-27 15:12:48 -0700144 if (src_start + src_len > old_size) {
Tao Bao97555da2016-12-15 10:15:06 -0800145 printf("source data too short\n");
146 return -1;
147 }
148
149 // Decompress the source data; the chunk header tells us exactly
150 // how big we expect it to be when decompressed.
151
152 // Note: expanded_len will include the bonus data size if
153 // the patch was constructed with bonus data. The
154 // deflation will come up 'bonus_size' bytes short; these
155 // must be appended from the bonus_data value.
156 size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->data.size() : 0;
157
158 std::vector<unsigned char> expanded_source(expanded_len);
159
160 // inflate() doesn't like strm.next_out being a nullptr even with
161 // avail_out being zero (Z_STREAM_ERROR).
162 if (expanded_len != 0) {
163 z_stream strm;
164 strm.zalloc = Z_NULL;
165 strm.zfree = Z_NULL;
166 strm.opaque = Z_NULL;
167 strm.avail_in = src_len;
Tao Bao087bc0c2017-01-19 10:46:39 -0800168 strm.next_in = old_data + src_start;
Tao Bao97555da2016-12-15 10:15:06 -0800169 strm.avail_out = expanded_len;
170 strm.next_out = expanded_source.data();
171
172 int ret = inflateInit2(&strm, -15);
173 if (ret != Z_OK) {
174 printf("failed to init source inflation: %d\n", ret);
175 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800176 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800177
Tao Bao97555da2016-12-15 10:15:06 -0800178 // Because we've provided enough room to accommodate the output
179 // data, we expect one call to inflate() to suffice.
180 ret = inflate(&strm, Z_SYNC_FLUSH);
181 if (ret != Z_STREAM_END) {
182 printf("source inflation returned %d\n", ret);
183 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800184 }
Tao Bao97555da2016-12-15 10:15:06 -0800185 // We should have filled the output buffer exactly, except
186 // for the bonus_size.
187 if (strm.avail_out != bonus_size) {
188 printf("source inflation short by %zu bytes\n", strm.avail_out - bonus_size);
189 return -1;
190 }
191 inflateEnd(&strm);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800192
Tao Bao97555da2016-12-15 10:15:06 -0800193 if (bonus_size) {
194 memcpy(expanded_source.data() + (expanded_len - bonus_size), &bonus_data->data[0],
195 bonus_size);
196 }
197 }
198
199 // Next, apply the bsdiff patch (in memory) to the uncompressed data.
200 std::vector<unsigned char> uncompressed_target_data;
Sen Jiang25c56972016-05-10 15:23:25 -0700201 // TODO(senj): Remove the only usage of ApplyBSDiffPatchMem here,
202 // replace it with ApplyBSDiffPatch with a custom sink function that
203 // wraps the given sink function to stream output to save memory.
Tao Bao97555da2016-12-15 10:15:06 -0800204 if (ApplyBSDiffPatchMem(expanded_source.data(), expanded_len, patch, patch_offset,
205 &uncompressed_target_data) != 0) {
206 return -1;
207 }
208 if (uncompressed_target_data.size() != target_len) {
209 printf("expected target len to be %zu, but it's %zu\n", target_len,
210 uncompressed_target_data.size());
211 return -1;
212 }
213
214 // Now compress the target data and append it to the output.
215
216 // we're done with the expanded_source data buffer, so we'll
217 // reuse that memory to receive the output of deflate.
218 if (expanded_source.size() < 32768U) {
219 expanded_source.resize(32768U);
220 }
221
222 {
223 std::vector<unsigned char>& temp_data = expanded_source;
224
225 // now the deflate stream
226 z_stream strm;
227 strm.zalloc = Z_NULL;
228 strm.zfree = Z_NULL;
229 strm.opaque = Z_NULL;
230 strm.avail_in = uncompressed_target_data.size();
231 strm.next_in = uncompressed_target_data.data();
232 int ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
233 if (ret != Z_OK) {
234 printf("failed to init uncompressed data deflation: %d\n", ret);
235 return -1;
236 }
237 do {
238 strm.avail_out = temp_data.size();
239 strm.next_out = temp_data.data();
240 ret = deflate(&strm, Z_FINISH);
Tao Baof7eb7602017-03-27 15:12:48 -0700241 size_t have = temp_data.size() - strm.avail_out;
Tao Bao97555da2016-12-15 10:15:06 -0800242
Tao Baoc0e1c462017-02-01 10:20:10 -0800243 if (sink(temp_data.data(), have) != have) {
Tao Bao97555da2016-12-15 10:15:06 -0800244 printf("failed to write %zd compressed bytes to output\n", have);
245 return -1;
246 }
247 if (ctx) SHA1_Update(ctx, temp_data.data(), have);
248 } while (ret != Z_STREAM_END);
249 deflateEnd(&strm);
250 }
251 } else {
252 printf("patch chunk %d is unknown type %d\n", i, type);
253 return -1;
254 }
255 }
256
257 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800258}