1 /*-
2 * Copyright 2003-2005 Colin Percival
3 * All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #if 0
28 __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $");
29 #endif
30
31 #include "bsdiff/bspatch.h"
32
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43
44 #include <algorithm>
45 #include <memory>
46 #include <vector>
47
48 #include "bsdiff/buffer_file.h"
49 #include "bsdiff/control_entry.h"
50 #include "bsdiff/extents.h"
51 #include "bsdiff/extents_file.h"
52 #include "bsdiff/file.h"
53 #include "bsdiff/file_interface.h"
54 #include "bsdiff/logging.h"
55 #include "bsdiff/memory_file.h"
56 #include "bsdiff/patch_reader.h"
57 #include "bsdiff/sink_file.h"
58 #include "bsdiff/utils.h"
59
60 namespace {
61 // Read the data in |stream| and write |size| decompressed data to |file|;
62 // using the buffer in |buf| with size |buf_size|.
63 // Returns 0 on success, 1 on I/O error and 2 on data error.
ReadStreamAndWriteAll(const std::unique_ptr<bsdiff::FileInterface> & file,size_t size,uint8_t * buf,size_t buf_size,const std::function<bool (uint8_t *,size_t)> & read_func)64 int ReadStreamAndWriteAll(
65 const std::unique_ptr<bsdiff::FileInterface>& file,
66 size_t size,
67 uint8_t* buf,
68 size_t buf_size,
69 const std::function<bool(uint8_t*, size_t)>& read_func) {
70 while (size > 0) {
71 size_t bytes_to_output = std::min(size, buf_size);
72 if (!read_func(buf, bytes_to_output)) {
73 LOG(ERROR) << "Failed to read stream.";
74 return 2;
75 }
76
77 if (!WriteAll(file, buf, bytes_to_output)) {
78 PLOG(ERROR) << "WriteAll() failed.";
79 return 1;
80 }
81 size -= bytes_to_output;
82 }
83 return 0;
84 }
85
86 } // namespace
87
88 namespace bsdiff {
89
ReadAll(const std::unique_ptr<FileInterface> & file,uint8_t * data,size_t size)90 bool ReadAll(const std::unique_ptr<FileInterface>& file,
91 uint8_t* data,
92 size_t size) {
93 size_t offset = 0, read;
94 while (offset < size) {
95 if (!file->Read(data + offset, size - offset, &read) || read == 0)
96 return false;
97 offset += read;
98 }
99 return true;
100 }
101
WriteAll(const std::unique_ptr<FileInterface> & file,const uint8_t * data,size_t size)102 bool WriteAll(const std::unique_ptr<FileInterface>& file,
103 const uint8_t* data,
104 size_t size) {
105 size_t offset = 0, written;
106 while (offset < size) {
107 if (!file->Write(data + offset, size - offset, &written) || written == 0)
108 return false;
109 offset += written;
110 }
111 return true;
112 }
113
IsOverlapping(const char * old_filename,const char * new_filename,const std::vector<ex_t> & old_extents,const std::vector<ex_t> & new_extents)114 bool IsOverlapping(const char* old_filename,
115 const char* new_filename,
116 const std::vector<ex_t>& old_extents,
117 const std::vector<ex_t>& new_extents) {
118 struct stat old_stat, new_stat;
119 if (stat(new_filename, &new_stat) == -1) {
120 if (errno == ENOENT)
121 return false;
122 PLOG(ERROR) << "Error stat the new file: " << new_filename;
123 return true;
124 }
125 if (stat(old_filename, &old_stat) == -1) {
126 PLOG(ERROR) << "Error stat the old file: " << old_filename;
127 return true;
128 }
129
130 if (old_stat.st_dev != new_stat.st_dev || old_stat.st_ino != new_stat.st_ino)
131 return false;
132
133 if (old_extents.empty() && new_extents.empty())
134 return true;
135
136 for (ex_t old_ex : old_extents)
137 for (ex_t new_ex : new_extents)
138 if (static_cast<uint64_t>(old_ex.off) < new_ex.off + new_ex.len &&
139 static_cast<uint64_t>(new_ex.off) < old_ex.off + old_ex.len)
140 return true;
141
142 return false;
143 }
144
145 // Patch |old_filename| with |patch_filename| and save it to |new_filename|.
146 // |old_extents| and |new_extents| are comma-separated lists of "offset:length"
147 // extents of |old_filename| and |new_filename|.
148 // Returns 0 on success, 1 on I/O error and 2 on data error.
bspatch(const char * old_filename,const char * new_filename,const char * patch_filename,const char * old_extents,const char * new_extents)149 int bspatch(const char* old_filename,
150 const char* new_filename,
151 const char* patch_filename,
152 const char* old_extents,
153 const char* new_extents) {
154 std::unique_ptr<FileInterface> patch_file =
155 File::FOpen(patch_filename, O_RDONLY);
156 if (!patch_file) {
157 PLOG(ERROR) << "Error opening the patch file: " << patch_filename;
158 return 1;
159 }
160 uint64_t patch_size;
161 patch_file->GetSize(&patch_size);
162 std::vector<uint8_t> patch(patch_size);
163 if (!ReadAll(patch_file, patch.data(), patch_size)) {
164 PLOG(ERROR) << "Error reading the patch file: " << patch_filename;
165 return 1;
166 }
167 patch_file.reset();
168
169 return bspatch(old_filename, new_filename, patch.data(), patch_size,
170 old_extents, new_extents);
171 }
172
173 // Patch |old_filename| with |patch_data| and save it to |new_filename|.
174 // |old_extents| and |new_extents| are comma-separated lists of "offset:length"
175 // extents of |old_filename| and |new_filename|.
176 // Returns 0 on success, 1 on I/O error and 2 on data error.
bspatch(const char * old_filename,const char * new_filename,const uint8_t * patch_data,size_t patch_size,const char * old_extents,const char * new_extents)177 int bspatch(const char* old_filename,
178 const char* new_filename,
179 const uint8_t* patch_data,
180 size_t patch_size,
181 const char* old_extents,
182 const char* new_extents) {
183 int using_extents = (old_extents != NULL || new_extents != NULL);
184
185 // Open input file for reading.
186 std::unique_ptr<FileInterface> old_file = File::FOpen(old_filename, O_RDONLY);
187 if (!old_file) {
188 PLOG(ERROR) << "Error opening the old file: " << old_filename;
189 return 1;
190 }
191
192 std::vector<ex_t> parsed_old_extents;
193 if (using_extents) {
194 if (!ParseExtentStr(old_extents, &parsed_old_extents)) {
195 LOG(ERROR) << "Error parsing the old extents.";
196 return 2;
197 }
198 old_file.reset(new ExtentsFile(std::move(old_file), parsed_old_extents));
199 }
200
201 // Open output file for writing.
202 std::unique_ptr<FileInterface> new_file =
203 File::FOpen(new_filename, O_CREAT | O_WRONLY);
204 if (!new_file) {
205 PLOG(ERROR) << "Error opening the new file: " << new_filename;
206 return 1;
207 }
208
209 std::vector<ex_t> parsed_new_extents;
210 if (using_extents) {
211 if (!ParseExtentStr(new_extents, &parsed_new_extents)) {
212 LOG(ERROR) << "Error parsing the new extents.";
213 return 2;
214 }
215 new_file.reset(new ExtentsFile(std::move(new_file), parsed_new_extents));
216 }
217
218 if (IsOverlapping(old_filename, new_filename, parsed_old_extents,
219 parsed_new_extents)) {
220 // New and old file is overlapping, we can not stream output to new file,
221 // cache it in a buffer and write to the file at the end.
222 uint64_t newsize = ParseInt64(patch_data + 24);
223 new_file.reset(new BufferFile(std::move(new_file), newsize));
224 }
225
226 return bspatch(old_file, new_file, patch_data, patch_size);
227 }
228
229 // Patch |old_data| with |patch_data| and save it by calling sink function.
230 // Returns 0 on success, 1 on I/O error and 2 on data error.
bspatch(const uint8_t * old_data,size_t old_size,const uint8_t * patch_data,size_t patch_size,const sink_func & sink)231 int bspatch(const uint8_t* old_data,
232 size_t old_size,
233 const uint8_t* patch_data,
234 size_t patch_size,
235 const sink_func& sink) {
236 std::unique_ptr<FileInterface> old_file(new MemoryFile(old_data, old_size));
237 std::unique_ptr<FileInterface> new_file(new SinkFile(sink));
238
239 return bspatch(old_file, new_file, patch_data, patch_size);
240 }
241
242 // Patch |old_file| with |patch_data| and save it to |new_file|.
243 // Returns 0 on success, 1 on I/O error and 2 on data error.
bspatch(const std::unique_ptr<FileInterface> & old_file,const std::unique_ptr<FileInterface> & new_file,const uint8_t * patch_data,size_t patch_size)244 int bspatch(const std::unique_ptr<FileInterface>& old_file,
245 const std::unique_ptr<FileInterface>& new_file,
246 const uint8_t* patch_data,
247 size_t patch_size) {
248 BsdiffPatchReader patch_reader;
249 if (!patch_reader.Init(patch_data, patch_size)) {
250 LOG(ERROR) << "Failed to initialize patch reader.";
251 return 2;
252 }
253
254 uint64_t old_file_size;
255 if (!old_file->GetSize(&old_file_size)) {
256 LOG(ERROR) << "Cannot obtain the size of old file.";
257 return 1;
258 }
259
260 // The oldpos can be negative, but the new pos is only incremented linearly.
261 int64_t oldpos = 0;
262 uint64_t newpos = 0;
263 std::vector<uint8_t> old_buf(1024 * 1024);
264 std::vector<uint8_t> new_buf(1024 * 1024);
265 uint64_t old_file_pos = 0;
266 while (newpos < patch_reader.new_file_size()) {
267 ControlEntry control_entry(0, 0, 0);
268 if (!patch_reader.ParseControlEntry(&control_entry)) {
269 LOG(ERROR) << "Failed to read control stream.";
270 return 2;
271 }
272
273 // Sanity-check.
274 if (newpos + control_entry.diff_size > patch_reader.new_file_size()) {
275 LOG(ERROR) << "Corrupt patch.";
276 return 2;
277 }
278
279 int ret = 0;
280 // Add old data to diff string. It is enough to fseek once, at
281 // the beginning of the sequence, to avoid unnecessary overhead.
282 int64_t seek_offset = oldpos;
283 if (seek_offset < 0) {
284 // Write diff block directly to new file without adding old data,
285 // because we will skip part where |oldpos| < 0.
286 ret = ReadStreamAndWriteAll(
287 new_file, oldpos - old_file_size, new_buf.data(), new_buf.size(),
288 std::bind(&BsdiffPatchReader::ReadDiffStream, &patch_reader,
289 std::placeholders::_1, std::placeholders::_2));
290 if (ret)
291 return ret;
292 seek_offset = 0;
293 }
294
295 // We just checked that |seek_offset| is not negative.
296 if (static_cast<uint64_t>(seek_offset) != old_file_pos &&
297 !old_file->Seek(seek_offset)) {
298 PLOG(ERROR) << "Error seeking input file to offset: " << seek_offset;
299 return 1;
300 }
301
302 old_file_pos =
303 std::min<uint64_t>(oldpos + control_entry.diff_size, old_file_size);
304 size_t chunk_size = old_file_pos - seek_offset;
305 while (chunk_size > 0) {
306 size_t read_bytes;
307 size_t bytes_to_read = std::min(chunk_size, old_buf.size());
308 if (!old_file->Read(old_buf.data(), bytes_to_read, &read_bytes)) {
309 PLOG(ERROR) << "Error reading from input file.";
310 return 1;
311 }
312 if (!read_bytes) {
313 LOG(ERROR) << "EOF reached while reading from input file.";
314 return 2;
315 }
316 // Read same amount of bytes from diff block
317 if (!patch_reader.ReadDiffStream(new_buf.data(), read_bytes)) {
318 LOG(ERROR) << "Failed to read diff stream.";
319 return 2;
320 }
321 // new_buf already has data from diff block, adds old data to it.
322 for (size_t k = 0; k < read_bytes; k++)
323 new_buf[k] += old_buf[k];
324 if (!WriteAll(new_file, new_buf.data(), read_bytes)) {
325 PLOG(ERROR) << "Error writing to new file.";
326 return 1;
327 }
328 chunk_size -= read_bytes;
329 }
330
331 // Adjust pointers.
332 newpos += control_entry.diff_size;
333 if (oldpos > INT64_MAX - static_cast<int64_t>(control_entry.diff_size))
334 return 2;
335 oldpos += control_entry.diff_size;
336
337 if (oldpos > static_cast<int64_t>(old_file_size)) {
338 // Write diff block directly to new file without adding old data,
339 // because we skipped part where |oldpos| > old_file_size.
340 ret = ReadStreamAndWriteAll(
341 new_file, oldpos - old_file_size, new_buf.data(), new_buf.size(),
342 std::bind(&BsdiffPatchReader::ReadDiffStream, &patch_reader,
343 std::placeholders::_1, std::placeholders::_2));
344 if (ret)
345 return ret;
346 }
347
348 // Sanity-check.
349 if (newpos + control_entry.extra_size > patch_reader.new_file_size()) {
350 LOG(ERROR) << "Corrupt patch.";
351 return 2;
352 }
353
354 // Read extra block.
355 ret = ReadStreamAndWriteAll(
356 new_file, control_entry.extra_size, new_buf.data(), new_buf.size(),
357 std::bind(&BsdiffPatchReader::ReadExtraStream, &patch_reader,
358 std::placeholders::_1, std::placeholders::_2));
359 if (ret)
360 return ret;
361
362 // Adjust pointers.
363 newpos += control_entry.extra_size;
364 if (control_entry.offset_increment > 0 &&
365 oldpos > INT64_MAX - control_entry.offset_increment)
366 return 2;
367 oldpos += control_entry.offset_increment;
368 }
369
370 // Close input file.
371 old_file->Close();
372
373 if (!patch_reader.Finish()) {
374 LOG(ERROR) << "Failed to finish the patch reader.";
375 return 2;
376 }
377
378 if (!new_file->Close()) {
379 PLOG(ERROR) << "Error closing new file.";
380 return 1;
381 }
382
383 return 0;
384 }
385
386 } // namespace bsdiff
387