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