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> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 35 | #include <fcntl.h> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 36 | #include <inttypes.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <unistd.h> |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 41 | #include <sys/types.h> |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 42 | |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 43 | #include "extents.h" |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 44 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 45 | namespace { |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 46 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 47 | static off_t offtin(u_char *buf) { |
| 48 | off_t y; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 49 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 50 | y = buf[7] & 0x7F; |
| 51 | y = y * 256; |
| 52 | y += buf[6]; |
| 53 | y = y * 256; |
| 54 | y += buf[5]; |
| 55 | y = y * 256; |
| 56 | y += buf[4]; |
| 57 | y = y * 256; |
| 58 | y += buf[3]; |
| 59 | y = y * 256; |
| 60 | y += buf[2]; |
| 61 | y = y * 256; |
| 62 | y += buf[1]; |
| 63 | y = y * 256; |
| 64 | y += buf[0]; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 65 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 66 | if (buf[7] & 0x80) |
| 67 | y = -y; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 68 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 69 | return y; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 72 | // TODO(deymo): Re-enable exfile.h once we can build it for the |
| 73 | // target and mac. |
Alex Deymo | 870b802 | 2015-09-28 18:15:09 -0700 | [diff] [blame] | 74 | #if 0 |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 75 | // Parses an extent string ex_str, returning a pointer to a newly allocated |
| 76 | // array of extents. The number of extents is stored in ex_count_p (if |
| 77 | // provided). |
| 78 | static ex_t *parse_extent_str(const char *ex_str, size_t *ex_count_p) { |
| 79 | size_t ex_count = (size_t)-1; |
| 80 | ex_t* ex_arr = extents_parse(ex_str, NULL, &ex_count); |
| 81 | if (!ex_arr) |
| 82 | errx(1, (ex_count == (size_t)-1 ? "error parsing extents" |
| 83 | : "error allocating extent array")); |
| 84 | if (ex_count_p) |
| 85 | *ex_count_p = ex_count; |
| 86 | return ex_arr; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 87 | } |
Alex Deymo | 870b802 | 2015-09-28 18:15:09 -0700 | [diff] [blame] | 88 | #endif |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 89 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 90 | } // namespace |
| 91 | |
| 92 | namespace bsdiff { |
| 93 | |
Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 94 | int bspatch( |
| 95 | const char* old_filename, const char* new_filename, |
| 96 | const char* patch_filename, |
| 97 | const char* old_extents, const char* new_extents) { |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 98 | FILE* f, *cpf, *dpf, *epf; |
| 99 | BZFILE* cpfbz2, *dpfbz2, *epfbz2; |
| 100 | int cbz2err, dbz2err, ebz2err; |
| 101 | FILE* old_file = NULL, * new_file = NULL; |
| 102 | ssize_t oldsize, newsize; |
| 103 | ssize_t bzctrllen, bzdatalen; |
| 104 | u_char header[32], buf[8]; |
| 105 | u_char* new_buf; |
| 106 | off_t oldpos, newpos; |
| 107 | off_t ctrl[3]; |
| 108 | off_t lenread; |
| 109 | off_t i, j; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 110 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 111 | int using_extents = (old_extents != NULL || new_extents != NULL); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 112 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 113 | // Open patch file. |
| 114 | if ((f = fopen(patch_filename, "r")) == NULL) |
| 115 | err(1, "fopen(%s)", patch_filename); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 116 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 117 | // File format: |
| 118 | // 0 8 "BSDIFF40" |
| 119 | // 8 8 X |
| 120 | // 16 8 Y |
| 121 | // 24 8 sizeof(new_filename) |
| 122 | // 32 X bzip2(control block) |
| 123 | // 32+X Y bzip2(diff block) |
| 124 | // 32+X+Y ??? bzip2(extra block) |
| 125 | // with control block a set of triples (x,y,z) meaning "add x bytes |
| 126 | // from oldfile to x bytes from the diff block; copy y bytes from the |
| 127 | // extra block; seek forwards in oldfile by z bytes". |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 128 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 129 | // Read header. |
| 130 | if (fread(header, 1, 32, f) < 32) { |
| 131 | if (feof(f)) |
| 132 | errx(1, "Corrupt patch\n"); |
| 133 | err(1, "fread(%s)", patch_filename); |
| 134 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 135 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 136 | // Check for appropriate magic. |
| 137 | if (memcmp(header, "BSDIFF40", 8) != 0) |
| 138 | errx(1, "Corrupt patch\n"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 139 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 140 | // Read lengths from header. |
| 141 | bzctrllen = offtin(header + 8); |
| 142 | bzdatalen = offtin(header + 16); |
| 143 | newsize = offtin(header + 24); |
| 144 | if ((bzctrllen < 0) || (bzdatalen < 0) || (newsize < 0)) |
| 145 | errx(1, "Corrupt patch\n"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 146 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 147 | // Close patch file and re-open it via libbzip2 at the right places. |
| 148 | if (fclose(f)) |
| 149 | err(1, "fclose(%s)", patch_filename); |
| 150 | if ((cpf = fopen(patch_filename, "r")) == NULL) |
| 151 | err(1, "fopen(%s)", patch_filename); |
| 152 | if (fseek(cpf, 32, SEEK_SET)) |
| 153 | err(1, "fseeko(%s, %lld)", patch_filename, (long long)32); |
| 154 | if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL) |
| 155 | errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err); |
| 156 | if ((dpf = fopen(patch_filename, "r")) == NULL) |
| 157 | err(1, "fopen(%s)", patch_filename); |
| 158 | if (fseek(dpf, 32 + bzctrllen, SEEK_SET)) |
| 159 | err(1, "fseeko(%s, %lld)", patch_filename, (long long)(32 + bzctrllen)); |
| 160 | if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL) |
| 161 | errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err); |
| 162 | if ((epf = fopen(patch_filename, "r")) == NULL) |
| 163 | err(1, "fopen(%s)", patch_filename); |
| 164 | if (fseek(epf, 32 + bzctrllen + bzdatalen, SEEK_SET)) |
| 165 | err(1, "fseeko(%s, %lld)", patch_filename, |
| 166 | (long long)(32 + bzctrllen + bzdatalen)); |
| 167 | if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL) |
| 168 | errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 169 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 170 | // Open input file for reading. |
| 171 | if (using_extents) { |
| 172 | // TODO(deymo): Re-enable exfile.h once we can build it for the |
| 173 | // target and mac. |
| 174 | errx(1, "Extent support is disabled.\n"); |
Alex Deymo | 870b802 | 2015-09-28 18:15:09 -0700 | [diff] [blame] | 175 | #if 0 |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 176 | size_t ex_count = 0; |
| 177 | ex_t *ex_arr = parse_extent_str(old_extents, &ex_count); |
| 178 | old_file = exfile_fopen(old_filename, "r", ex_arr, ex_count, |
| 179 | free); |
Alex Deymo | 870b802 | 2015-09-28 18:15:09 -0700 | [diff] [blame] | 180 | #endif |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 181 | } else { old_file = fopen(old_filename, "r"); } |
| 182 | if (!old_file || fseek(old_file, 0, SEEK_END) != 0 || |
| 183 | (oldsize = ftell(old_file)) < 0 || fseek(old_file, 0, SEEK_SET) != 0) |
| 184 | err(1, "cannot obtain the size of %s", old_filename); |
| 185 | off_t old_file_pos = 0; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 186 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 187 | if ((new_buf = static_cast<u_char*>(malloc(newsize + 1))) == NULL) |
| 188 | err(1, NULL); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 189 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 190 | oldpos = 0; |
| 191 | newpos = 0; |
| 192 | while (newpos < newsize) { |
| 193 | // Read control data. |
| 194 | for (i = 0; i <= 2; i++) { |
| 195 | lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8); |
| 196 | if ((lenread < 8) || ((cbz2err != BZ_OK) && (cbz2err != BZ_STREAM_END))) |
| 197 | errx(1, "Corrupt patch\n"); |
| 198 | ctrl[i] = offtin(buf); |
| 199 | }; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 200 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 201 | // Sanity-check. |
| 202 | if (ctrl[0] < 0 || ctrl[1] < 0) |
| 203 | errx(1, "Corrupt patch\n"); |
Doug Zongker | 4d05479 | 2014-05-13 08:37:06 -0700 | [diff] [blame] | 204 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 205 | // Sanity-check. |
| 206 | if (newpos + ctrl[0] > newsize) |
| 207 | errx(1, "Corrupt patch\n"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 208 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 209 | // Read diff string. |
| 210 | lenread = BZ2_bzRead(&dbz2err, dpfbz2, new_buf + newpos, ctrl[0]); |
| 211 | if ((lenread < ctrl[0]) || |
| 212 | ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END))) |
| 213 | errx(1, "Corrupt patch\n"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 214 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 215 | // Add old data to diff string. It is enough to fseek once, at |
| 216 | // the beginning of the sequence, to avoid unnecessary overhead. |
| 217 | j = newpos; |
| 218 | if ((i = oldpos) < 0) { |
| 219 | j -= i; |
| 220 | i = 0; |
| 221 | } |
| 222 | if (i != old_file_pos && fseek(old_file, i, SEEK_SET) < 0) |
| 223 | err(1, "error seeking input file to offset %" PRIdMAX, (intmax_t)i); |
| 224 | if ((old_file_pos = oldpos + ctrl[0]) > oldsize) |
| 225 | old_file_pos = oldsize; |
| 226 | while (i++ < old_file_pos) { |
| 227 | u_char c; |
| 228 | if (fread(&c, 1, 1, old_file) != 1) |
| 229 | err(1, "error reading from input file"); |
| 230 | new_buf[j++] += c; |
| 231 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 232 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 233 | // Adjust pointers. |
| 234 | newpos += ctrl[0]; |
| 235 | oldpos += ctrl[0]; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 236 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 237 | // Sanity-check. |
| 238 | if (newpos + ctrl[1] > newsize) |
| 239 | errx(1, "Corrupt patch\n"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 240 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 241 | // Read extra string. |
| 242 | lenread = BZ2_bzRead(&ebz2err, epfbz2, new_buf + newpos, ctrl[1]); |
| 243 | if ((lenread < ctrl[1]) || |
| 244 | ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END))) |
| 245 | errx(1, "Corrupt patch\n"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 246 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 247 | // Adjust pointers. |
| 248 | newpos += ctrl[1]; |
| 249 | oldpos += ctrl[2]; |
| 250 | }; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 251 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 252 | // Close input file. |
| 253 | fclose(old_file); |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 254 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 255 | // Clean up the bzip2 reads. |
| 256 | BZ2_bzReadClose(&cbz2err, cpfbz2); |
| 257 | BZ2_bzReadClose(&dbz2err, dpfbz2); |
| 258 | BZ2_bzReadClose(&ebz2err, epfbz2); |
| 259 | if (fclose(cpf) || fclose(dpf) || fclose(epf)) |
| 260 | err(1, "fclose(%s)", patch_filename); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 261 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 262 | // Write the new file. |
| 263 | if (using_extents) { |
| 264 | // TODO(deymo): Re-enable exfile.h once we can build it for the |
| 265 | // target and mac. |
Alex Deymo | 870b802 | 2015-09-28 18:15:09 -0700 | [diff] [blame] | 266 | #if 0 |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 267 | size_t ex_count = 0; |
| 268 | ex_t *ex_arr = parse_extent_str(new_extents, &ex_count); |
| 269 | new_file = exfile_fopen(new_filename, "w", ex_arr, ex_count, |
| 270 | free); |
Alex Deymo | 870b802 | 2015-09-28 18:15:09 -0700 | [diff] [blame] | 271 | #endif |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 272 | } else { new_file = fopen(new_filename, "w"); } |
| 273 | if (!new_file || fwrite(new_buf, 1, newsize, new_file) != (size_t)newsize || |
| 274 | fclose(new_file) == EOF) |
| 275 | err(1, "%s", new_filename); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 276 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 277 | free(new_buf); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 278 | |
Alex Deymo | b870eb5 | 2015-10-14 21:39:04 -0700 | [diff] [blame^] | 279 | return 0; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 280 | } |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 281 | |
| 282 | } // namespace bsdiff |