blob: 33c4487620c3eb10236ed61fe2d209bf9678232b [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
27#include "zlib.h"
28#include "mincrypt/sha.h"
29#include "applypatch.h"
30#include "imgdiff.h"
31#include "utils.h"
32
33/*
34 * Apply the patch given in 'patch_filename' to the source data given
35 * by (old_data, old_size). Write the patched output to the 'output'
36 * file, and update the SHA context with the output data as well.
37 * Return 0 on success.
38 */
Mark Salyzynf3bb31c2014-03-14 09:39:48 -070039int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size __unused,
Doug Zongkerc4351c72010-02-22 14:46:32 -080040 const Value* patch,
Doug Zongkera3ccba62012-08-20 15:28:02 -070041 SinkFn sink, void* token, SHA_CTX* ctx,
42 const Value* bonus_data) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080043 ssize_t pos = 12;
44 char* header = patch->data;
45 if (patch->size < 12) {
46 printf("patch too short to contain header\n");
47 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -080048 }
49
Doug Zongkerc4351c72010-02-22 14:46:32 -080050 // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
51 // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and
52 // CHUNK_GZIP.)
53 if (memcmp(header, "IMGDIFF2", 8) != 0) {
54 printf("corrupt patch file header (magic number)\n");
Doug Zongker512536a2010-02-17 16:11:44 -080055 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -080056 }
Doug Zongker512536a2010-02-17 16:11:44 -080057
Doug Zongkerc4351c72010-02-22 14:46:32 -080058 int num_chunks = Read4(header+8);
59
60 int i;
61 for (i = 0; i < num_chunks; ++i) {
62 // each chunk's header record starts with 4 bytes.
63 if (pos + 4 > patch->size) {
64 printf("failed to read chunk %d record\n", i);
65 return -1;
66 }
67 int type = Read4(patch->data + pos);
68 pos += 4;
69
70 if (type == CHUNK_NORMAL) {
71 char* normal_header = patch->data + pos;
72 pos += 24;
73 if (pos > patch->size) {
74 printf("failed to read chunk %d normal header data\n", i);
75 return -1;
76 }
77
78 size_t src_start = Read8(normal_header);
79 size_t src_len = Read8(normal_header+8);
80 size_t patch_offset = Read8(normal_header+16);
81
82 ApplyBSDiffPatch(old_data + src_start, src_len,
83 patch, patch_offset, sink, token, ctx);
84 } else if (type == CHUNK_RAW) {
85 char* raw_header = patch->data + pos;
86 pos += 4;
87 if (pos > patch->size) {
88 printf("failed to read chunk %d raw header data\n", i);
89 return -1;
90 }
91
92 ssize_t data_len = Read4(raw_header);
93
94 if (pos + data_len > patch->size) {
95 printf("failed to read chunk %d raw data\n", i);
96 return -1;
97 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070098 if (ctx) SHA_update(ctx, patch->data + pos, data_len);
Doug Zongkerc4351c72010-02-22 14:46:32 -080099 if (sink((unsigned char*)patch->data + pos,
100 data_len, token) != data_len) {
101 printf("failed to write chunk %d raw data\n", i);
102 return -1;
103 }
104 pos += data_len;
105 } else if (type == CHUNK_DEFLATE) {
106 // deflate chunks have an additional 60 bytes in their chunk header.
107 char* deflate_header = patch->data + pos;
108 pos += 60;
109 if (pos > patch->size) {
110 printf("failed to read chunk %d deflate header data\n", i);
111 return -1;
112 }
113
114 size_t src_start = Read8(deflate_header);
115 size_t src_len = Read8(deflate_header+8);
116 size_t patch_offset = Read8(deflate_header+16);
117 size_t expanded_len = Read8(deflate_header+24);
118 size_t target_len = Read8(deflate_header+32);
119 int level = Read4(deflate_header+40);
120 int method = Read4(deflate_header+44);
121 int windowBits = Read4(deflate_header+48);
122 int memLevel = Read4(deflate_header+52);
123 int strategy = Read4(deflate_header+56);
124
125 // Decompress the source data; the chunk header tells us exactly
126 // how big we expect it to be when decompressed.
127
Doug Zongkera3ccba62012-08-20 15:28:02 -0700128 // Note: expanded_len will include the bonus data size if
129 // the patch was constructed with bonus data. The
130 // deflation will come up 'bonus_size' bytes short; these
131 // must be appended from the bonus_data value.
132 size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0;
133
Doug Zongkerc4351c72010-02-22 14:46:32 -0800134 unsigned char* expanded_source = malloc(expanded_len);
135 if (expanded_source == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700136 printf("failed to allocate %zu bytes for expanded_source\n",
Doug Zongkerc4351c72010-02-22 14:46:32 -0800137 expanded_len);
138 return -1;
139 }
140
141 z_stream strm;
142 strm.zalloc = Z_NULL;
143 strm.zfree = Z_NULL;
144 strm.opaque = Z_NULL;
145 strm.avail_in = src_len;
146 strm.next_in = (unsigned char*)(old_data + src_start);
147 strm.avail_out = expanded_len;
148 strm.next_out = expanded_source;
149
150 int ret;
151 ret = inflateInit2(&strm, -15);
152 if (ret != Z_OK) {
153 printf("failed to init source inflation: %d\n", ret);
154 return -1;
155 }
156
157 // Because we've provided enough room to accommodate the output
158 // data, we expect one call to inflate() to suffice.
159 ret = inflate(&strm, Z_SYNC_FLUSH);
160 if (ret != Z_STREAM_END) {
161 printf("source inflation returned %d\n", ret);
162 return -1;
163 }
Doug Zongkera3ccba62012-08-20 15:28:02 -0700164 // We should have filled the output buffer exactly, except
165 // for the bonus_size.
166 if (strm.avail_out != bonus_size) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700167 printf("source inflation short by %zu bytes\n", strm.avail_out-bonus_size);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800168 return -1;
169 }
170 inflateEnd(&strm);
171
Doug Zongkera3ccba62012-08-20 15:28:02 -0700172 if (bonus_size) {
173 memcpy(expanded_source + (expanded_len - bonus_size),
174 bonus_data->data, bonus_size);
175 }
176
Doug Zongkerc4351c72010-02-22 14:46:32 -0800177 // Next, apply the bsdiff patch (in memory) to the uncompressed
178 // data.
179 unsigned char* uncompressed_target_data;
180 ssize_t uncompressed_target_size;
181 if (ApplyBSDiffPatchMem(expanded_source, expanded_len,
182 patch, patch_offset,
183 &uncompressed_target_data,
184 &uncompressed_target_size) != 0) {
185 return -1;
186 }
187
188 // Now compress the target data and append it to the output.
189
190 // we're done with the expanded_source data buffer, so we'll
191 // reuse that memory to receive the output of deflate.
192 unsigned char* temp_data = expanded_source;
193 ssize_t temp_size = expanded_len;
194 if (temp_size < 32768) {
195 // ... unless the buffer is too small, in which case we'll
196 // allocate a fresh one.
197 free(temp_data);
198 temp_data = malloc(32768);
199 temp_size = 32768;
200 }
201
202 // now the deflate stream
203 strm.zalloc = Z_NULL;
204 strm.zfree = Z_NULL;
205 strm.opaque = Z_NULL;
206 strm.avail_in = uncompressed_target_size;
207 strm.next_in = uncompressed_target_data;
208 ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
209 do {
210 strm.avail_out = temp_size;
211 strm.next_out = temp_data;
212 ret = deflate(&strm, Z_FINISH);
213 ssize_t have = temp_size - strm.avail_out;
214
215 if (sink(temp_data, have, token) != have) {
216 printf("failed to write %ld compressed bytes to output\n",
217 (long)have);
218 return -1;
219 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700220 if (ctx) SHA_update(ctx, temp_data, have);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800221 } while (ret != Z_STREAM_END);
222 deflateEnd(&strm);
223
224 free(temp_data);
225 free(uncompressed_target_data);
226 } else {
227 printf("patch chunk %d is unknown type %d\n", i, type);
228 return -1;
229 }
230 }
231
232 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800233}