blob: 0ab995b308fb10e7cb350ed282986eb93f353744 [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
17// See imgdiff.c in this directory for a description of the patch file
18// format.
19
20#include <stdio.h>
Mark Salyzynf3bb31c2014-03-14 09:39:48 -070021#include <sys/cdefs.h>
Doug Zongker512536a2010-02-17 16:11:44 -080022#include <sys/stat.h>
23#include <errno.h>
24#include <unistd.h>
25#include <string.h>
26
Yabin Cuid483c202016-02-03 17:08:52 -080027#include <vector>
28
Doug Zongker512536a2010-02-17 16:11:44 -080029#include "zlib.h"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080030#include "openssl/sha.h"
Doug Zongker512536a2010-02-17 16:11:44 -080031#include "applypatch.h"
32#include "imgdiff.h"
33#include "utils.h"
34
Sen Jiang0cce9cd2016-01-22 20:49:07 +080035int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
36 const unsigned char* patch_data, ssize_t patch_size,
37 SinkFn sink, void* token) {
38 Value patch = {VAL_BLOB, patch_size,
39 reinterpret_cast<char*>(const_cast<unsigned char*>(patch_data))};
40 return ApplyImagePatch(
41 old_data, old_size, &patch, sink, token, nullptr, nullptr);
42}
43
Doug Zongker512536a2010-02-17 16:11:44 -080044/*
45 * Apply the patch given in 'patch_filename' to the source data given
46 * by (old_data, old_size). Write the patched output to the 'output'
47 * file, and update the SHA context with the output data as well.
48 * Return 0 on success.
49 */
Sen Jiang0cce9cd2016-01-22 20:49:07 +080050int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
Doug Zongkerc4351c72010-02-22 14:46:32 -080051 const Value* patch,
Doug Zongkera3ccba62012-08-20 15:28:02 -070052 SinkFn sink, void* token, SHA_CTX* ctx,
53 const Value* bonus_data) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080054 ssize_t pos = 12;
55 char* header = patch->data;
56 if (patch->size < 12) {
57 printf("patch too short to contain header\n");
58 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -080059 }
60
Doug Zongkerc4351c72010-02-22 14:46:32 -080061 // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
62 // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and
63 // CHUNK_GZIP.)
64 if (memcmp(header, "IMGDIFF2", 8) != 0) {
65 printf("corrupt patch file header (magic number)\n");
Doug Zongker512536a2010-02-17 16:11:44 -080066 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -080067 }
Doug Zongker512536a2010-02-17 16:11:44 -080068
Doug Zongkerc4351c72010-02-22 14:46:32 -080069 int num_chunks = Read4(header+8);
70
71 int i;
72 for (i = 0; i < num_chunks; ++i) {
73 // each chunk's header record starts with 4 bytes.
74 if (pos + 4 > patch->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 char* normal_header = patch->data + pos;
83 pos += 24;
84 if (pos > patch->size) {
85 printf("failed to read chunk %d normal header data\n", i);
86 return -1;
87 }
88
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
Sen Jiang0cce9cd2016-01-22 20:49:07 +080093 if (src_start + src_len > static_cast<size_t>(old_size)) {
94 printf("source data too short\n");
95 return -1;
96 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080097 ApplyBSDiffPatch(old_data + src_start, src_len,
98 patch, patch_offset, sink, token, ctx);
99 } else if (type == CHUNK_RAW) {
100 char* raw_header = patch->data + pos;
101 pos += 4;
102 if (pos > patch->size) {
103 printf("failed to read chunk %d raw header data\n", i);
104 return -1;
105 }
106
107 ssize_t data_len = Read4(raw_header);
108
109 if (pos + data_len > patch->size) {
110 printf("failed to read chunk %d raw data\n", i);
111 return -1;
112 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800113 if (ctx) SHA1_Update(ctx, patch->data + pos, data_len);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800114 if (sink((unsigned char*)patch->data + pos,
115 data_len, token) != data_len) {
116 printf("failed to write chunk %d raw data\n", i);
117 return -1;
118 }
119 pos += data_len;
120 } else if (type == CHUNK_DEFLATE) {
121 // deflate chunks have an additional 60 bytes in their chunk header.
122 char* deflate_header = patch->data + pos;
123 pos += 60;
124 if (pos > patch->size) {
125 printf("failed to read chunk %d deflate header data\n", i);
126 return -1;
127 }
128
129 size_t src_start = Read8(deflate_header);
130 size_t src_len = Read8(deflate_header+8);
131 size_t patch_offset = Read8(deflate_header+16);
132 size_t expanded_len = Read8(deflate_header+24);
Sen Jiang696692a2016-02-02 16:01:23 +0800133 size_t target_len = Read8(deflate_header+32);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800134 int level = Read4(deflate_header+40);
135 int method = Read4(deflate_header+44);
136 int windowBits = Read4(deflate_header+48);
137 int memLevel = Read4(deflate_header+52);
138 int strategy = Read4(deflate_header+56);
139
Sen Jiang0cce9cd2016-01-22 20:49:07 +0800140 if (src_start + src_len > static_cast<size_t>(old_size)) {
141 printf("source data too short\n");
142 return -1;
143 }
144
Doug Zongkerc4351c72010-02-22 14:46:32 -0800145 // Decompress the source data; the chunk header tells us exactly
146 // how big we expect it to be when decompressed.
147
Doug Zongkera3ccba62012-08-20 15:28:02 -0700148 // Note: expanded_len will include the bonus data size if
149 // the patch was constructed with bonus data. The
150 // deflation will come up 'bonus_size' bytes short; these
151 // must be appended from the bonus_data value.
152 size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0;
153
Yabin Cuid483c202016-02-03 17:08:52 -0800154 std::vector<unsigned char> expanded_source(expanded_len);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800155 z_stream strm;
156 strm.zalloc = Z_NULL;
157 strm.zfree = Z_NULL;
158 strm.opaque = Z_NULL;
159 strm.avail_in = src_len;
160 strm.next_in = (unsigned char*)(old_data + src_start);
161 strm.avail_out = expanded_len;
Yabin Cuid483c202016-02-03 17:08:52 -0800162 strm.next_out = expanded_source.data();
Doug Zongkerc4351c72010-02-22 14:46:32 -0800163
164 int ret;
165 ret = inflateInit2(&strm, -15);
166 if (ret != Z_OK) {
167 printf("failed to init source inflation: %d\n", ret);
168 return -1;
169 }
170
171 // Because we've provided enough room to accommodate the output
172 // data, we expect one call to inflate() to suffice.
173 ret = inflate(&strm, Z_SYNC_FLUSH);
174 if (ret != Z_STREAM_END) {
175 printf("source inflation returned %d\n", ret);
176 return -1;
177 }
Doug Zongkera3ccba62012-08-20 15:28:02 -0700178 // We should have filled the output buffer exactly, except
179 // for the bonus_size.
180 if (strm.avail_out != bonus_size) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700181 printf("source inflation short by %zu bytes\n", strm.avail_out-bonus_size);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800182 return -1;
183 }
184 inflateEnd(&strm);
185
Doug Zongkera3ccba62012-08-20 15:28:02 -0700186 if (bonus_size) {
Yabin Cuid483c202016-02-03 17:08:52 -0800187 memcpy(expanded_source.data() + (expanded_len - bonus_size),
Doug Zongkera3ccba62012-08-20 15:28:02 -0700188 bonus_data->data, bonus_size);
189 }
190
Doug Zongkerc4351c72010-02-22 14:46:32 -0800191 // Next, apply the bsdiff patch (in memory) to the uncompressed
192 // data.
Yabin Cuid483c202016-02-03 17:08:52 -0800193 std::vector<unsigned char> uncompressed_target_data;
194 if (ApplyBSDiffPatchMem(expanded_source.data(), expanded_len,
Doug Zongkerc4351c72010-02-22 14:46:32 -0800195 patch, patch_offset,
Yabin Cuid483c202016-02-03 17:08:52 -0800196 &uncompressed_target_data) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800197 return -1;
198 }
Sen Jiang696692a2016-02-02 16:01:23 +0800199 if (uncompressed_target_data.size() != target_len) {
200 printf("expected target len to be %zu, but it's %zu\n",
201 target_len, uncompressed_target_data.size());
202 return -1;
203 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800204
205 // Now compress the target data and append it to the output.
206
207 // we're done with the expanded_source data buffer, so we'll
208 // reuse that memory to receive the output of deflate.
Yabin Cuid483c202016-02-03 17:08:52 -0800209 if (expanded_source.size() < 32768U) {
210 expanded_source.resize(32768U);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800211 }
Yabin Cuid483c202016-02-03 17:08:52 -0800212 std::vector<unsigned char>& temp_data = expanded_source;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800213
214 // now the deflate stream
215 strm.zalloc = Z_NULL;
216 strm.zfree = Z_NULL;
217 strm.opaque = Z_NULL;
Yabin Cuid483c202016-02-03 17:08:52 -0800218 strm.avail_in = uncompressed_target_data.size();
219 strm.next_in = uncompressed_target_data.data();
Doug Zongkerc4351c72010-02-22 14:46:32 -0800220 ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
Yabin Cuid483c202016-02-03 17:08:52 -0800221 if (ret != Z_OK) {
222 printf("failed to init uncompressed data deflation: %d\n", ret);
223 return -1;
224 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800225 do {
Yabin Cuid483c202016-02-03 17:08:52 -0800226 strm.avail_out = temp_data.size();
227 strm.next_out = temp_data.data();
Doug Zongkerc4351c72010-02-22 14:46:32 -0800228 ret = deflate(&strm, Z_FINISH);
Yabin Cuid483c202016-02-03 17:08:52 -0800229 ssize_t have = temp_data.size() - strm.avail_out;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800230
Yabin Cuid483c202016-02-03 17:08:52 -0800231 if (sink(temp_data.data(), have, token) != have) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800232 printf("failed to write %ld compressed bytes to output\n",
233 (long)have);
234 return -1;
235 }
Yabin Cuid483c202016-02-03 17:08:52 -0800236 if (ctx) SHA1_Update(ctx, temp_data.data(), have);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800237 } while (ret != Z_STREAM_END);
238 deflateEnd(&strm);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800239 } else {
240 printf("patch chunk %d is unknown type %d\n", i, type);
241 return -1;
242 }
243 }
244
245 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800246}