The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 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 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 31 | #include "bsdiff/bspatch.h" |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 32 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 33 | #include <errno.h> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 34 | #include <fcntl.h> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 35 | #include <inttypes.h> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 38 | #include <sys/stat.h> |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 39 | #include <sys/types.h> |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 40 | #include <unistd.h> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 41 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 42 | #include <algorithm> |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 43 | #include <memory> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 44 | #include <vector> |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 45 | |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 46 | #include "bsdiff/buffer_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 47 | #include "bsdiff/control_entry.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 48 | #include "bsdiff/extents.h" |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 49 | #include "bsdiff/extents_file.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 50 | #include "bsdiff/file.h" |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 51 | #include "bsdiff/file_interface.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 52 | #include "bsdiff/memory_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 53 | #include "bsdiff/patch_reader.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 54 | #include "bsdiff/sink_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 55 | #include "bsdiff/utils.h" |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 56 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 57 | namespace { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 58 | // Read the data in |stream| and write |size| decompressed data to |file|; |
| 59 | // using the buffer in |buf| with size |buf_size|. |
| 60 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
| 61 | int ReadStreamAndWriteAll( |
| 62 | const std::unique_ptr<bsdiff::FileInterface>& file, |
| 63 | size_t size, |
| 64 | uint8_t* buf, |
| 65 | size_t buf_size, |
| 66 | const std::function<bool(uint8_t*, size_t)>& read_func) { |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 67 | while (size > 0) { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 68 | size_t bytes_to_output = std::min(size, buf_size); |
| 69 | if (!read_func(buf, bytes_to_output)) { |
| 70 | fprintf(stderr, "Failed to read stream.\n"); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 71 | return 2; |
| 72 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 73 | |
| 74 | if (!WriteAll(file, buf, bytes_to_output)) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 75 | perror("WriteAll() failed"); |
| 76 | return 1; |
| 77 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 78 | size -= bytes_to_output; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 79 | } |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 80 | return 0; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 83 | } // namespace |
| 84 | |
| 85 | namespace bsdiff { |
| 86 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 87 | bool ReadAll(const std::unique_ptr<FileInterface>& file, |
| 88 | uint8_t* data, |
| 89 | size_t size) { |
| 90 | size_t offset = 0, read; |
| 91 | while (offset < size) { |
| 92 | if (!file->Read(data + offset, size - offset, &read) || read == 0) |
| 93 | return false; |
| 94 | offset += read; |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 99 | bool WriteAll(const std::unique_ptr<FileInterface>& file, |
| 100 | const uint8_t* data, |
| 101 | size_t size) { |
| 102 | size_t offset = 0, written; |
| 103 | while (offset < size) { |
Sen Jiang | b14bb55 | 2016-04-11 16:08:03 -0700 | [diff] [blame] | 104 | if (!file->Write(data + offset, size - offset, &written) || written == 0) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 105 | return false; |
| 106 | offset += written; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | bool IsOverlapping(const char* old_filename, |
| 112 | const char* new_filename, |
| 113 | const std::vector<ex_t>& old_extents, |
| 114 | const std::vector<ex_t>& new_extents) { |
| 115 | struct stat old_stat, new_stat; |
| 116 | if (stat(new_filename, &new_stat) == -1) { |
| 117 | if (errno == ENOENT) |
| 118 | return false; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 119 | fprintf(stderr, "Error stat the new file %s: %s\n", new_filename, |
| 120 | strerror(errno)); |
| 121 | return true; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 122 | } |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 123 | if (stat(old_filename, &old_stat) == -1) { |
| 124 | fprintf(stderr, "Error stat the old file %s: %s\n", old_filename, |
| 125 | strerror(errno)); |
| 126 | return true; |
| 127 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 128 | |
| 129 | if (old_stat.st_dev != new_stat.st_dev || old_stat.st_ino != new_stat.st_ino) |
| 130 | return false; |
| 131 | |
| 132 | if (old_extents.empty() && new_extents.empty()) |
| 133 | return true; |
| 134 | |
| 135 | for (ex_t old_ex : old_extents) |
| 136 | for (ex_t new_ex : new_extents) |
| 137 | if (static_cast<uint64_t>(old_ex.off) < new_ex.off + new_ex.len && |
| 138 | static_cast<uint64_t>(new_ex.off) < old_ex.off + old_ex.len) |
| 139 | return true; |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 144 | // Patch |old_filename| with |patch_filename| and save it to |new_filename|. |
| 145 | // |old_extents| and |new_extents| are comma-separated lists of "offset:length" |
| 146 | // extents of |old_filename| and |new_filename|. |
| 147 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
| 148 | int bspatch(const char* old_filename, |
| 149 | const char* new_filename, |
| 150 | const char* patch_filename, |
| 151 | const char* old_extents, |
| 152 | const char* new_extents) { |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 153 | std::unique_ptr<FileInterface> patch_file = |
| 154 | File::FOpen(patch_filename, O_RDONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 155 | if (!patch_file) { |
| 156 | fprintf(stderr, "Error opening the patch file %s: %s\n", patch_filename, |
| 157 | strerror(errno)); |
| 158 | return 1; |
| 159 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 160 | uint64_t patch_size; |
| 161 | patch_file->GetSize(&patch_size); |
| 162 | std::vector<uint8_t> patch(patch_size); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 163 | if (!ReadAll(patch_file, patch.data(), patch_size)) { |
| 164 | fprintf(stderr, "Error reading the patch file %s: %s\n", patch_filename, |
| 165 | strerror(errno)); |
| 166 | return 1; |
| 167 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 168 | patch_file.reset(); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 169 | |
Sen Jiang | 62d5e48 | 2017-01-24 16:25:25 -0800 | [diff] [blame] | 170 | return bspatch(old_filename, new_filename, patch.data(), patch_size, |
| 171 | old_extents, new_extents); |
| 172 | } |
| 173 | |
| 174 | // Patch |old_filename| with |patch_data| and save it to |new_filename|. |
| 175 | // |old_extents| and |new_extents| are comma-separated lists of "offset:length" |
| 176 | // extents of |old_filename| and |new_filename|. |
| 177 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
| 178 | int bspatch(const char* old_filename, |
| 179 | const char* new_filename, |
| 180 | const uint8_t* patch_data, |
| 181 | size_t patch_size, |
| 182 | const char* old_extents, |
| 183 | const char* new_extents) { |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 184 | int using_extents = (old_extents != NULL || new_extents != NULL); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 185 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 186 | // Open input file for reading. |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 187 | std::unique_ptr<FileInterface> old_file = File::FOpen(old_filename, O_RDONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 188 | if (!old_file) { |
| 189 | fprintf(stderr, "Error opening the old file %s: %s\n", old_filename, |
| 190 | strerror(errno)); |
| 191 | return 1; |
| 192 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 193 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 194 | std::vector<ex_t> parsed_old_extents; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 195 | if (using_extents) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 196 | if (!ParseExtentStr(old_extents, &parsed_old_extents)) { |
| 197 | fprintf(stderr, "Error parsing the old extents\n"); |
| 198 | return 2; |
| 199 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 200 | old_file.reset(new ExtentsFile(std::move(old_file), parsed_old_extents)); |
| 201 | } |
| 202 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 203 | // Open output file for writing. |
| 204 | std::unique_ptr<FileInterface> new_file = |
| 205 | File::FOpen(new_filename, O_CREAT | O_WRONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 206 | if (!new_file) { |
| 207 | fprintf(stderr, "Error opening the new file %s: %s\n", new_filename, |
| 208 | strerror(errno)); |
| 209 | return 1; |
| 210 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 211 | |
| 212 | std::vector<ex_t> parsed_new_extents; |
| 213 | if (using_extents) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 214 | if (!ParseExtentStr(new_extents, &parsed_new_extents)) { |
| 215 | fprintf(stderr, "Error parsing the new extents\n"); |
| 216 | return 2; |
| 217 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 218 | new_file.reset(new ExtentsFile(std::move(new_file), parsed_new_extents)); |
| 219 | } |
| 220 | |
| 221 | if (IsOverlapping(old_filename, new_filename, parsed_old_extents, |
| 222 | parsed_new_extents)) { |
| 223 | // New and old file is overlapping, we can not stream output to new file, |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 224 | // cache it in a buffer and write to the file at the end. |
Sen Jiang | 62d5e48 | 2017-01-24 16:25:25 -0800 | [diff] [blame] | 225 | uint64_t newsize = ParseInt64(patch_data + 24); |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 226 | new_file.reset(new BufferFile(std::move(new_file), newsize)); |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 227 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 228 | |
Sen Jiang | 62d5e48 | 2017-01-24 16:25:25 -0800 | [diff] [blame] | 229 | return bspatch(old_file, new_file, patch_data, patch_size); |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 232 | // Patch |old_data| with |patch_data| and save it by calling sink function. |
| 233 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 234 | int bspatch(const uint8_t* old_data, |
| 235 | size_t old_size, |
| 236 | const uint8_t* patch_data, |
| 237 | size_t patch_size, |
| 238 | const sink_func& sink) { |
| 239 | std::unique_ptr<FileInterface> old_file(new MemoryFile(old_data, old_size)); |
| 240 | std::unique_ptr<FileInterface> new_file(new SinkFile(sink)); |
| 241 | |
| 242 | return bspatch(old_file, new_file, patch_data, patch_size); |
| 243 | } |
| 244 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 245 | // Patch |old_file| with |patch_data| and save it to |new_file|. |
| 246 | // Returns 0 on success, 1 on I/O error and 2 on data error. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 247 | int bspatch(const std::unique_ptr<FileInterface>& old_file, |
| 248 | const std::unique_ptr<FileInterface>& new_file, |
| 249 | const uint8_t* patch_data, |
| 250 | size_t patch_size) { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 251 | BsdiffPatchReader patch_reader; |
| 252 | if (!patch_reader.Init(patch_data, patch_size)) { |
| 253 | fprintf(stderr, "Failed to initialize patch reader\n"); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 254 | return 2; |
| 255 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 256 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 257 | uint64_t old_file_size; |
| 258 | if (!old_file->GetSize(&old_file_size)) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 259 | fprintf(stderr, "Cannot obtain the size of old file.\n"); |
| 260 | return 1; |
| 261 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 262 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 263 | // The oldpos can be negative, but the new pos is only incremented linearly. |
| 264 | int64_t oldpos = 0; |
| 265 | uint64_t newpos = 0; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 266 | std::vector<uint8_t> old_buf(1024 * 1024); |
| 267 | std::vector<uint8_t> new_buf(1024 * 1024); |
| 268 | uint64_t old_file_pos = 0; |
| 269 | while (newpos < patch_reader.new_file_size()) { |
| 270 | ControlEntry control_entry(0, 0, 0); |
| 271 | if (!patch_reader.ParseControlEntry(&control_entry)) { |
| 272 | fprintf(stderr, "Failed to read control stream\n"); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 273 | return 2; |
| 274 | } |
Doug Zongker | 4d05479 | 2014-05-13 08:37:06 -0700 | [diff] [blame] | 275 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 276 | // Sanity-check. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 277 | if (newpos + control_entry.diff_size > patch_reader.new_file_size()) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 278 | fprintf(stderr, "Corrupt patch.\n"); |
| 279 | return 2; |
| 280 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 281 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 282 | int ret = 0; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 283 | // Add old data to diff string. It is enough to fseek once, at |
| 284 | // the beginning of the sequence, to avoid unnecessary overhead. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 285 | int64_t seek_offset = oldpos; |
| 286 | if (seek_offset < 0) { |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 287 | // Write diff block directly to new file without adding old data, |
| 288 | // because we will skip part where |oldpos| < 0. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 289 | ret = ReadStreamAndWriteAll( |
| 290 | new_file, oldpos - old_file_size, new_buf.data(), new_buf.size(), |
| 291 | std::bind(&BsdiffPatchReader::ReadDiffStream, &patch_reader, |
| 292 | std::placeholders::_1, std::placeholders::_2)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 293 | if (ret) |
| 294 | return ret; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 295 | seek_offset = 0; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 296 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 297 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 298 | // We just checked that |seek_offset| is not negative. |
| 299 | if (static_cast<uint64_t>(seek_offset) != old_file_pos && |
| 300 | !old_file->Seek(seek_offset)) { |
| 301 | fprintf(stderr, "Error seeking input file to offset %" PRId64 ": %s\n", |
| 302 | seek_offset, strerror(errno)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 303 | return 1; |
| 304 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 305 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 306 | old_file_pos = |
| 307 | std::min<uint64_t>(oldpos + control_entry.diff_size, old_file_size); |
| 308 | size_t chunk_size = old_file_pos - seek_offset; |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 309 | while (chunk_size > 0) { |
| 310 | size_t read_bytes; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 311 | size_t bytes_to_read = std::min(chunk_size, old_buf.size()); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 312 | if (!old_file->Read(old_buf.data(), bytes_to_read, &read_bytes)) { |
| 313 | perror("Error reading from input file"); |
| 314 | return 1; |
| 315 | } |
| 316 | if (!read_bytes) { |
| 317 | fprintf(stderr, "EOF reached while reading from input file.\n"); |
| 318 | return 2; |
| 319 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 320 | // Read same amount of bytes from diff block |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 321 | if (!patch_reader.ReadDiffStream(new_buf.data(), read_bytes)) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 322 | fprintf(stderr, "Failed to read diff stream.\n"); |
| 323 | return 2; |
| 324 | } |
Sen Jiang | d87c835 | 2015-11-20 16:14:36 -0800 | [diff] [blame] | 325 | // new_buf already has data from diff block, adds old data to it. |
| 326 | for (size_t k = 0; k < read_bytes; k++) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 327 | new_buf[k] += old_buf[k]; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 328 | if (!WriteAll(new_file, new_buf.data(), read_bytes)) { |
| 329 | perror("Error writing to new file"); |
| 330 | return 1; |
| 331 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 332 | chunk_size -= read_bytes; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 333 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 334 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 335 | // Adjust pointers. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 336 | newpos += control_entry.diff_size; |
| 337 | oldpos += control_entry.diff_size; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 338 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 339 | if (oldpos > static_cast<int64_t>(old_file_size)) { |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 340 | // Write diff block directly to new file without adding old data, |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 341 | // because we skipped part where |oldpos| > old_file_size. |
| 342 | ret = ReadStreamAndWriteAll( |
| 343 | new_file, oldpos - old_file_size, new_buf.data(), new_buf.size(), |
| 344 | std::bind(&BsdiffPatchReader::ReadDiffStream, &patch_reader, |
| 345 | std::placeholders::_1, std::placeholders::_2)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 346 | if (ret) |
| 347 | return ret; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 350 | // Sanity-check. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 351 | if (newpos + control_entry.extra_size > patch_reader.new_file_size()) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 352 | fprintf(stderr, "Corrupt patch.\n"); |
| 353 | return 2; |
| 354 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 355 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 356 | // Read extra block. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 357 | ret = ReadStreamAndWriteAll( |
| 358 | new_file, control_entry.extra_size, new_buf.data(), new_buf.size(), |
| 359 | std::bind(&BsdiffPatchReader::ReadExtraStream, &patch_reader, |
| 360 | std::placeholders::_1, std::placeholders::_2)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 361 | if (ret) |
| 362 | return ret; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 363 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 364 | // Adjust pointers. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 365 | newpos += control_entry.extra_size; |
| 366 | oldpos += control_entry.offset_increment; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 367 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 368 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 369 | // Close input file. |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 370 | old_file->Close(); |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 371 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame^] | 372 | if (!patch_reader.Finish()) { |
| 373 | fprintf(stderr, "Failed to finish the patch reader\n"); |
| 374 | return 2; |
| 375 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 376 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 377 | if (!new_file->Close()) { |
| 378 | perror("Error closing new file"); |
| 379 | return 1; |
| 380 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 381 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 382 | return 0; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 383 | } |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 384 | |
| 385 | } // namespace bsdiff |