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> |
Alex Deymo | ea72d9f | 2017-10-26 13:15:13 +0200 | [diff] [blame] | 36 | #include <stdio.h> |
Amin Hassani | 6e40d93 | 2019-05-13 11:47:57 -0700 | [diff] [blame] | 37 | #include <stdint.h> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 40 | #include <sys/stat.h> |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 41 | #include <sys/types.h> |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 42 | #include <unistd.h> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 43 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 44 | #include <algorithm> |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 45 | #include <memory> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 46 | #include <vector> |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 47 | |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 48 | #include "bsdiff/buffer_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 49 | #include "bsdiff/control_entry.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 50 | #include "bsdiff/extents.h" |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 51 | #include "bsdiff/extents_file.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 52 | #include "bsdiff/file.h" |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 53 | #include "bsdiff/file_interface.h" |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 54 | #include "bsdiff/logging.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 55 | #include "bsdiff/memory_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 56 | #include "bsdiff/patch_reader.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 57 | #include "bsdiff/sink_file.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 58 | #include "bsdiff/utils.h" |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 59 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 60 | namespace { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 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. |
| 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) { |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 70 | while (size > 0) { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 71 | size_t bytes_to_output = std::min(size, buf_size); |
| 72 | if (!read_func(buf, bytes_to_output)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 73 | LOG(ERROR) << "Failed to read stream."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 74 | return 2; |
| 75 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 76 | |
| 77 | if (!WriteAll(file, buf, bytes_to_output)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 78 | PLOG(ERROR) << "WriteAll() failed."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 79 | return 1; |
| 80 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 81 | size -= bytes_to_output; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 82 | } |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 83 | return 0; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 86 | } // namespace |
| 87 | |
| 88 | namespace bsdiff { |
| 89 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 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 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 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) { |
Sen Jiang | b14bb55 | 2016-04-11 16:08:03 -0700 | [diff] [blame] | 107 | if (!file->Write(data + offset, size - offset, &written) || written == 0) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 108 | return false; |
| 109 | offset += written; |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
| 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; |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 122 | PLOG(ERROR) << "Error stat the new file: " << new_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 123 | return true; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 124 | } |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 125 | if (stat(old_filename, &old_stat) == -1) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 126 | PLOG(ERROR) << "Error stat the old file: " << old_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 127 | return true; |
| 128 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 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 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 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. |
| 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) { |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 154 | std::unique_ptr<FileInterface> patch_file = |
| 155 | File::FOpen(patch_filename, O_RDONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 156 | if (!patch_file) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 157 | PLOG(ERROR) << "Error opening the patch file: " << patch_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 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)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 164 | PLOG(ERROR) << "Error reading the patch file: " << patch_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 165 | return 1; |
| 166 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 167 | patch_file.reset(); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 168 | |
Sen Jiang | 62d5e48 | 2017-01-24 16:25:25 -0800 | [diff] [blame] | 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. |
| 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) { |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 183 | int using_extents = (old_extents != NULL || new_extents != NULL); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 184 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 185 | // Open input file for reading. |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 186 | std::unique_ptr<FileInterface> old_file = File::FOpen(old_filename, O_RDONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 187 | if (!old_file) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 188 | PLOG(ERROR) << "Error opening the old file: " << old_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 189 | return 1; |
| 190 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 191 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 192 | std::vector<ex_t> parsed_old_extents; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 193 | if (using_extents) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 194 | if (!ParseExtentStr(old_extents, &parsed_old_extents)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 195 | LOG(ERROR) << "Error parsing the old extents."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 196 | return 2; |
| 197 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 198 | old_file.reset(new ExtentsFile(std::move(old_file), parsed_old_extents)); |
| 199 | } |
| 200 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 201 | // Open output file for writing. |
| 202 | std::unique_ptr<FileInterface> new_file = |
| 203 | File::FOpen(new_filename, O_CREAT | O_WRONLY); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 204 | if (!new_file) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 205 | PLOG(ERROR) << "Error opening the new file: " << new_filename; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 206 | return 1; |
| 207 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 208 | |
| 209 | std::vector<ex_t> parsed_new_extents; |
| 210 | if (using_extents) { |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 211 | if (!ParseExtentStr(new_extents, &parsed_new_extents)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 212 | LOG(ERROR) << "Error parsing the new extents."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 213 | return 2; |
| 214 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 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, |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 221 | // 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] | 222 | uint64_t newsize = ParseInt64(patch_data + 24); |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 223 | new_file.reset(new BufferFile(std::move(new_file), newsize)); |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 224 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 225 | |
Sen Jiang | 62d5e48 | 2017-01-24 16:25:25 -0800 | [diff] [blame] | 226 | return bspatch(old_file, new_file, patch_data, patch_size); |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 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. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 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 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 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. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 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) { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 248 | BsdiffPatchReader patch_reader; |
| 249 | if (!patch_reader.Init(patch_data, patch_size)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 250 | LOG(ERROR) << "Failed to initialize patch reader."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 251 | return 2; |
| 252 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 253 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 254 | uint64_t old_file_size; |
| 255 | if (!old_file->GetSize(&old_file_size)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 256 | LOG(ERROR) << "Cannot obtain the size of old file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 257 | return 1; |
| 258 | } |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 259 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 260 | // The oldpos can be negative, but the new pos is only incremented linearly. |
| 261 | int64_t oldpos = 0; |
| 262 | uint64_t newpos = 0; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 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)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 269 | LOG(ERROR) << "Failed to read control stream."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 270 | return 2; |
| 271 | } |
Doug Zongker | 4d05479 | 2014-05-13 08:37:06 -0700 | [diff] [blame] | 272 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 273 | // Sanity-check. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 274 | if (newpos + control_entry.diff_size > patch_reader.new_file_size()) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 275 | LOG(ERROR) << "Corrupt patch."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 276 | return 2; |
| 277 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 278 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 279 | int ret = 0; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 280 | // Add old data to diff string. It is enough to fseek once, at |
| 281 | // the beginning of the sequence, to avoid unnecessary overhead. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 282 | int64_t seek_offset = oldpos; |
| 283 | if (seek_offset < 0) { |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 284 | // Write diff block directly to new file without adding old data, |
| 285 | // because we will skip part where |oldpos| < 0. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 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)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 290 | if (ret) |
| 291 | return ret; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 292 | seek_offset = 0; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 293 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 294 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 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)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 298 | PLOG(ERROR) << "Error seeking input file to offset: " << seek_offset; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 299 | return 1; |
| 300 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 301 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 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; |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 305 | while (chunk_size > 0) { |
| 306 | size_t read_bytes; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 307 | size_t bytes_to_read = std::min(chunk_size, old_buf.size()); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 308 | if (!old_file->Read(old_buf.data(), bytes_to_read, &read_bytes)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 309 | PLOG(ERROR) << "Error reading from input file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 310 | return 1; |
| 311 | } |
| 312 | if (!read_bytes) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 313 | LOG(ERROR) << "EOF reached while reading from input file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 314 | return 2; |
| 315 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 316 | // Read same amount of bytes from diff block |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 317 | if (!patch_reader.ReadDiffStream(new_buf.data(), read_bytes)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 318 | LOG(ERROR) << "Failed to read diff stream."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 319 | return 2; |
| 320 | } |
Sen Jiang | d87c835 | 2015-11-20 16:14:36 -0800 | [diff] [blame] | 321 | // new_buf already has data from diff block, adds old data to it. |
| 322 | for (size_t k = 0; k < read_bytes; k++) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 323 | new_buf[k] += old_buf[k]; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 324 | if (!WriteAll(new_file, new_buf.data(), read_bytes)) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 325 | PLOG(ERROR) << "Error writing to new file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 326 | return 1; |
| 327 | } |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 328 | chunk_size -= read_bytes; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 329 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 330 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 331 | // Adjust pointers. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 332 | newpos += control_entry.diff_size; |
Amin Hassani | 6e40d93 | 2019-05-13 11:47:57 -0700 | [diff] [blame] | 333 | if (oldpos > INT64_MAX - static_cast<int64_t>(control_entry.diff_size)) |
| 334 | return 2; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 335 | oldpos += control_entry.diff_size; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 336 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 337 | if (oldpos > static_cast<int64_t>(old_file_size)) { |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 338 | // Write diff block directly to new file without adding old data, |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 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)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 344 | if (ret) |
| 345 | return ret; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 348 | // Sanity-check. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 349 | if (newpos + control_entry.extra_size > patch_reader.new_file_size()) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 350 | LOG(ERROR) << "Corrupt patch."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 351 | return 2; |
| 352 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 353 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 354 | // Read extra block. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 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)); |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 359 | if (ret) |
| 360 | return ret; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 361 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 362 | // Adjust pointers. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 363 | newpos += control_entry.extra_size; |
Amin Hassani | 6e40d93 | 2019-05-13 11:47:57 -0700 | [diff] [blame] | 364 | if (control_entry.offset_increment > 0 && |
| 365 | oldpos > INT64_MAX - control_entry.offset_increment) |
| 366 | return 2; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 367 | oldpos += control_entry.offset_increment; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 368 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 369 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 370 | // Close input file. |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 371 | old_file->Close(); |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 372 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 373 | if (!patch_reader.Finish()) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 374 | LOG(ERROR) << "Failed to finish the patch reader."; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 375 | return 2; |
| 376 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 377 | |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 378 | if (!new_file->Close()) { |
Amin Hassani | 1106bf7 | 2017-11-15 17:26:03 -0800 | [diff] [blame] | 379 | PLOG(ERROR) << "Error closing new file."; |
Sen Jiang | b552c79 | 2017-01-20 14:07:49 -0800 | [diff] [blame] | 380 | return 1; |
| 381 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 382 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 383 | return 0; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 384 | } |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 385 | |
| 386 | } // namespace bsdiff |