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 | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 31 | #include "bspatch.h" |
| 32 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 33 | #include <bzlib.h> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 34 | #include <err.h> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 35 | #include <errno.h> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 36 | #include <fcntl.h> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 37 | #include <inttypes.h> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <unistd.h> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 41 | #include <sys/stat.h> |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 42 | #include <sys/types.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> |
| 45 | #include <memory> |
| 46 | #include <limits> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 47 | #include <vector> |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 48 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 49 | #include "buffer_file.h" |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 50 | #include "extents.h" |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 51 | #include "extents_file.h" |
| 52 | #include "file.h" |
| 53 | #include "file_interface.h" |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 54 | #include "memory_file.h" |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 55 | #include "sink_file.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 { |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 58 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 59 | int64_t ParseInt64(const u_char* buf) { |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 60 | int64_t y; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 61 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 62 | y = buf[7] & 0x7F; |
| 63 | y = y * 256; |
| 64 | y += buf[6]; |
| 65 | y = y * 256; |
| 66 | y += buf[5]; |
| 67 | y = y * 256; |
| 68 | y += buf[4]; |
| 69 | y = y * 256; |
| 70 | y += buf[3]; |
| 71 | y = y * 256; |
| 72 | y += buf[2]; |
| 73 | y = y * 256; |
| 74 | y += buf[1]; |
| 75 | y = y * 256; |
| 76 | y += buf[0]; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 77 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 78 | if (buf[7] & 0x80) |
| 79 | y = -y; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 80 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 81 | return y; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 84 | bool ReadBZ2(bz_stream* stream, uint8_t* data, size_t size) { |
| 85 | stream->next_out = (char*)data; |
| 86 | while (size > 0) { |
| 87 | unsigned int read_size = std::min( |
| 88 | static_cast<size_t>(std::numeric_limits<unsigned int>::max()), size); |
| 89 | stream->avail_out = read_size; |
| 90 | int bz2err = BZ2_bzDecompress(stream); |
| 91 | if (bz2err != BZ_OK && bz2err != BZ_STREAM_END) |
| 92 | return false; |
| 93 | size -= read_size - stream->avail_out; |
| 94 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool ReadBZ2AndWriteAll(const std::unique_ptr<bsdiff::FileInterface>& file, |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 99 | bz_stream* stream, |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 100 | size_t size, |
| 101 | uint8_t* buf, |
| 102 | size_t buf_size) { |
| 103 | while (size > 0) { |
| 104 | size_t bytes_to_read = std::min(size, buf_size); |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 105 | if (!ReadBZ2(stream, buf, bytes_to_read)) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 106 | return false; |
| 107 | if (!WriteAll(file, buf, bytes_to_read)) |
| 108 | return false; |
| 109 | size -= bytes_to_read; |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 114 | } // namespace |
| 115 | |
| 116 | namespace bsdiff { |
| 117 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 118 | bool ReadAll(const std::unique_ptr<FileInterface>& file, |
| 119 | uint8_t* data, |
| 120 | size_t size) { |
| 121 | size_t offset = 0, read; |
| 122 | while (offset < size) { |
| 123 | if (!file->Read(data + offset, size - offset, &read) || read == 0) |
| 124 | return false; |
| 125 | offset += read; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 130 | bool WriteAll(const std::unique_ptr<FileInterface>& file, |
| 131 | const uint8_t* data, |
| 132 | size_t size) { |
| 133 | size_t offset = 0, written; |
| 134 | while (offset < size) { |
Sen Jiang | b14bb55 | 2016-04-11 16:08:03 -0700 | [diff] [blame] | 135 | if (!file->Write(data + offset, size - offset, &written) || written == 0) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 136 | return false; |
| 137 | offset += written; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool IsOverlapping(const char* old_filename, |
| 143 | const char* new_filename, |
| 144 | const std::vector<ex_t>& old_extents, |
| 145 | const std::vector<ex_t>& new_extents) { |
| 146 | struct stat old_stat, new_stat; |
| 147 | if (stat(new_filename, &new_stat) == -1) { |
| 148 | if (errno == ENOENT) |
| 149 | return false; |
| 150 | err(1, "Error stat the new filename %s", new_filename); |
| 151 | } |
| 152 | if (stat(old_filename, &old_stat) == -1) |
| 153 | err(1, "Error stat the old filename %s", old_filename); |
| 154 | |
| 155 | if (old_stat.st_dev != new_stat.st_dev || old_stat.st_ino != new_stat.st_ino) |
| 156 | return false; |
| 157 | |
| 158 | if (old_extents.empty() && new_extents.empty()) |
| 159 | return true; |
| 160 | |
| 161 | for (ex_t old_ex : old_extents) |
| 162 | for (ex_t new_ex : new_extents) |
| 163 | if (static_cast<uint64_t>(old_ex.off) < new_ex.off + new_ex.len && |
| 164 | static_cast<uint64_t>(new_ex.off) < old_ex.off + old_ex.len) |
| 165 | return true; |
| 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 170 | int bspatch( |
| 171 | const char* old_filename, const char* new_filename, |
| 172 | const char* patch_filename, |
| 173 | const char* old_extents, const char* new_extents) { |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 174 | std::unique_ptr<FileInterface> patch_file = |
| 175 | File::FOpen(patch_filename, O_RDONLY); |
| 176 | if (!patch_file) |
| 177 | err(1, "Error opening the patch filename %s", patch_filename); |
| 178 | uint64_t patch_size; |
| 179 | patch_file->GetSize(&patch_size); |
| 180 | std::vector<uint8_t> patch(patch_size); |
| 181 | if (!ReadAll(patch_file, patch.data(), patch_size)) |
| 182 | errx(1, "Corrupt patch\n"); |
| 183 | patch_file.reset(); |
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 | int using_extents = (old_extents != NULL || new_extents != NULL); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 186 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 187 | // Open input file for reading. |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 188 | std::unique_ptr<FileInterface> old_file = File::FOpen(old_filename, O_RDONLY); |
| 189 | if (!old_file) |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 190 | err(1, "Error opening the old filename %s", old_filename); |
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) { |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 194 | if (!ParseExtentStr(old_extents, &parsed_old_extents)) |
| 195 | errx(1, "Error parsing the old extents"); |
| 196 | old_file.reset(new ExtentsFile(std::move(old_file), parsed_old_extents)); |
| 197 | } |
| 198 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 199 | // Open output file for writing. |
| 200 | std::unique_ptr<FileInterface> new_file = |
| 201 | File::FOpen(new_filename, O_CREAT | O_WRONLY); |
| 202 | if (!new_file) |
| 203 | err(1, "Error opening the new filename %s", new_filename); |
| 204 | |
| 205 | std::vector<ex_t> parsed_new_extents; |
| 206 | if (using_extents) { |
| 207 | if (!ParseExtentStr(new_extents, &parsed_new_extents)) |
| 208 | errx(1, "Error parsing the new extents"); |
| 209 | new_file.reset(new ExtentsFile(std::move(new_file), parsed_new_extents)); |
| 210 | } |
| 211 | |
| 212 | if (IsOverlapping(old_filename, new_filename, parsed_old_extents, |
| 213 | parsed_new_extents)) { |
| 214 | // 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^] | 215 | // cache it in a buffer and write to the file at the end. |
| 216 | uint64_t newsize = ParseInt64(patch.data() + 24); |
| 217 | new_file.reset(new BufferFile(std::move(new_file), newsize)); |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 218 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 219 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 220 | return bspatch(old_file, new_file, patch.data(), patch_size); |
| 221 | } |
| 222 | |
| 223 | int bspatch(const uint8_t* old_data, |
| 224 | size_t old_size, |
| 225 | const uint8_t* patch_data, |
| 226 | size_t patch_size, |
| 227 | const sink_func& sink) { |
| 228 | std::unique_ptr<FileInterface> old_file(new MemoryFile(old_data, old_size)); |
| 229 | std::unique_ptr<FileInterface> new_file(new SinkFile(sink)); |
| 230 | |
| 231 | return bspatch(old_file, new_file, patch_data, patch_size); |
| 232 | } |
| 233 | |
| 234 | int bspatch(const std::unique_ptr<FileInterface>& old_file, |
| 235 | const std::unique_ptr<FileInterface>& new_file, |
| 236 | const uint8_t* patch_data, |
| 237 | size_t patch_size) { |
| 238 | int bz2err; |
| 239 | u_char buf[8]; |
| 240 | off_t ctrl[3]; |
| 241 | |
| 242 | // File format: |
| 243 | // 0 8 "BSDIFF40" |
| 244 | // 8 8 X |
| 245 | // 16 8 Y |
| 246 | // 24 8 sizeof(new_filename) |
| 247 | // 32 X bzip2(control block) |
| 248 | // 32+X Y bzip2(diff block) |
| 249 | // 32+X+Y ??? bzip2(extra block) |
| 250 | // with control block a set of triples (x,y,z) meaning "add x bytes |
| 251 | // from oldfile to x bytes from the diff block; copy y bytes from the |
| 252 | // extra block; seek forwards in oldfile by z bytes". |
| 253 | |
| 254 | // Check for appropriate magic. |
| 255 | if (memcmp(patch_data, "BSDIFF40", 8) != 0) |
| 256 | errx(1, "Corrupt patch\n"); |
| 257 | |
| 258 | // Read lengths from header. |
| 259 | uint64_t oldsize, newsize; |
| 260 | int64_t ctrl_len = ParseInt64(patch_data + 8); |
| 261 | int64_t data_len = ParseInt64(patch_data + 16); |
| 262 | int64_t signed_newsize = ParseInt64(patch_data + 24); |
| 263 | newsize = signed_newsize; |
| 264 | if ((ctrl_len < 0) || (data_len < 0) || (signed_newsize < 0) || |
| 265 | (32 + ctrl_len + data_len > static_cast<int64_t>(patch_size))) |
| 266 | errx(1, "Corrupt patch\n"); |
| 267 | |
| 268 | bz_stream cstream; |
| 269 | cstream.next_in = (char*)patch_data + 32; |
| 270 | cstream.avail_in = ctrl_len; |
| 271 | cstream.bzalloc = nullptr; |
| 272 | cstream.bzfree = nullptr; |
| 273 | cstream.opaque = nullptr; |
| 274 | if ((bz2err = BZ2_bzDecompressInit(&cstream, 0, 0)) != BZ_OK) |
| 275 | errx(1, "failed to bzinit control stream (%d)\n", bz2err); |
| 276 | |
| 277 | bz_stream dstream; |
| 278 | dstream.next_in = (char*)patch_data + 32 + ctrl_len; |
| 279 | dstream.avail_in = data_len; |
| 280 | dstream.bzalloc = nullptr; |
| 281 | dstream.bzfree = nullptr; |
| 282 | dstream.opaque = nullptr; |
| 283 | if ((bz2err = BZ2_bzDecompressInit(&dstream, 0, 0)) != BZ_OK) |
| 284 | errx(1, "failed to bzinit diff stream (%d)\n", bz2err); |
| 285 | |
| 286 | bz_stream estream; |
| 287 | estream.next_in = (char*)patch_data + 32 + ctrl_len + data_len; |
| 288 | estream.avail_in = patch_size - (32 + ctrl_len + data_len); |
| 289 | estream.bzalloc = nullptr; |
| 290 | estream.bzfree = nullptr; |
| 291 | estream.opaque = nullptr; |
| 292 | if ((bz2err = BZ2_bzDecompressInit(&estream, 0, 0)) != BZ_OK) |
| 293 | errx(1, "failed to bzinit extra stream (%d)\n", bz2err); |
| 294 | |
| 295 | uint64_t old_file_pos = 0; |
| 296 | |
| 297 | if (!old_file->GetSize(&oldsize)) |
| 298 | err(1, "cannot obtain the size of old file"); |
| 299 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 300 | // The oldpos can be negative, but the new pos is only incremented linearly. |
| 301 | int64_t oldpos = 0; |
| 302 | uint64_t newpos = 0; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 303 | std::vector<uint8_t> old_buf(1024 * 1024), new_buf(1024 * 1024); |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 304 | while (newpos < newsize) { |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 305 | int64_t i; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 306 | // Read control data. |
| 307 | for (i = 0; i <= 2; i++) { |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 308 | if (!ReadBZ2(&cstream, buf, 8)) |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 309 | errx(1, "Corrupt patch\n"); |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 310 | ctrl[i] = ParseInt64(buf); |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 311 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 312 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 313 | // Sanity-check. |
| 314 | if (ctrl[0] < 0 || ctrl[1] < 0) |
| 315 | errx(1, "Corrupt patch\n"); |
Doug Zongker | 4d05479 | 2014-05-13 08:37:06 -0700 | [diff] [blame] | 316 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 317 | // Sanity-check. |
| 318 | if (newpos + ctrl[0] > newsize) |
| 319 | errx(1, "Corrupt patch\n"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 320 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 321 | // Add old data to diff string. It is enough to fseek once, at |
| 322 | // the beginning of the sequence, to avoid unnecessary overhead. |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 323 | if ((i = oldpos) < 0) { |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 324 | // Write diff block directly to new file without adding old data, |
| 325 | // because we will skip part where |oldpos| < 0. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 326 | if (!ReadBZ2AndWriteAll(new_file, &dstream, -i, new_buf.data(), |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 327 | new_buf.size())) |
| 328 | errx(1, "Error during ReadBZ2AndWriteAll()"); |
| 329 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 330 | i = 0; |
| 331 | } |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 332 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 333 | // We just checked that |i| is not negative. |
| 334 | if (static_cast<uint64_t>(i) != old_file_pos && !old_file->Seek(i)) |
| 335 | err(1, "error seeking input file to offset %" PRId64, i); |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 336 | if ((old_file_pos = oldpos + ctrl[0]) > oldsize) |
| 337 | old_file_pos = oldsize; |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 338 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 339 | size_t chunk_size = old_file_pos - i; |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 340 | while (chunk_size > 0) { |
| 341 | size_t read_bytes; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 342 | size_t bytes_to_read = std::min(chunk_size, old_buf.size()); |
Sen Jiang | d87c835 | 2015-11-20 16:14:36 -0800 | [diff] [blame] | 343 | if (!old_file->Read(old_buf.data(), bytes_to_read, &read_bytes)) |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 344 | err(1, "error reading from input file"); |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 345 | if (!read_bytes) |
| 346 | errx(1, "EOF reached while reading from input file"); |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 347 | // Read same amount of bytes from diff block |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 348 | if (!ReadBZ2(&dstream, new_buf.data(), read_bytes)) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 349 | errx(1, "Corrupt patch\n"); |
Sen Jiang | d87c835 | 2015-11-20 16:14:36 -0800 | [diff] [blame] | 350 | // new_buf already has data from diff block, adds old data to it. |
| 351 | for (size_t k = 0; k < read_bytes; k++) |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 352 | new_buf[k] += old_buf[k]; |
| 353 | if (!WriteAll(new_file, new_buf.data(), read_bytes)) |
| 354 | err(1, "Error writing new file."); |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 355 | chunk_size -= read_bytes; |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 356 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 357 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 358 | // Adjust pointers. |
| 359 | newpos += ctrl[0]; |
| 360 | oldpos += ctrl[0]; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 361 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 362 | if (oldpos > static_cast<int64_t>(oldsize)) { |
| 363 | // Write diff block directly to new file without adding old data, |
| 364 | // because we skipped part where |oldpos| > oldsize. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 365 | if (!ReadBZ2AndWriteAll(new_file, &dstream, oldpos - oldsize, |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 366 | new_buf.data(), new_buf.size())) |
| 367 | errx(1, "Error during ReadBZ2AndWriteAll()"); |
| 368 | } |
| 369 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 370 | // Sanity-check. |
| 371 | if (newpos + ctrl[1] > newsize) |
| 372 | errx(1, "Corrupt patch\n"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 373 | |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 374 | // Read extra block. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 375 | if (!ReadBZ2AndWriteAll(new_file, &estream, ctrl[1], new_buf.data(), |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 376 | new_buf.size())) |
| 377 | errx(1, "Error during ReadBZ2AndWriteAll()"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 378 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 379 | // Adjust pointers. |
| 380 | newpos += ctrl[1]; |
| 381 | oldpos += ctrl[2]; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 382 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 383 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 384 | // Close input file. |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 385 | old_file->Close(); |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 386 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 387 | // Clean up the bzip2 reads. |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 388 | BZ2_bzDecompressEnd(&cstream); |
| 389 | BZ2_bzDecompressEnd(&dstream); |
| 390 | BZ2_bzDecompressEnd(&estream); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 391 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 392 | if (!new_file->Close()) |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame^] | 393 | err(1, "Error closing new file"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 394 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame] | 395 | return 0; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 396 | } |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 397 | |
| 398 | } // namespace bsdiff |