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 "exfile.h" |
| 42 | #include "extents.h" |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 43 | |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 45 | static off_t offtin(u_char *buf) |
| 46 | { |
| 47 | off_t y; |
| 48 | |
| 49 | y=buf[7]&0x7F; |
| 50 | y=y*256;y+=buf[6]; |
| 51 | y=y*256;y+=buf[5]; |
| 52 | y=y*256;y+=buf[4]; |
| 53 | y=y*256;y+=buf[3]; |
| 54 | y=y*256;y+=buf[2]; |
| 55 | y=y*256;y+=buf[1]; |
| 56 | y=y*256;y+=buf[0]; |
| 57 | |
| 58 | if(buf[7]&0x80) y=-y; |
| 59 | |
| 60 | return y; |
| 61 | } |
| 62 | |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 63 | /* Parses an extent string ex_str, returning a pointer to a newly allocated |
| 64 | * array of extents. The number of extents is stored in ex_count_p (if |
| 65 | * provided). */ |
| 66 | static ex_t *parse_extent_str(const char *ex_str, size_t *ex_count_p) |
| 67 | { |
| 68 | size_t ex_count = (size_t)-1; |
| 69 | ex_t *ex_arr = extents_parse(ex_str, NULL, &ex_count); |
| 70 | if (!ex_arr) |
| 71 | errx(1, (ex_count == (size_t)-1 ? |
| 72 | "error parsing extents" : |
| 73 | "error allocating extent array")); |
| 74 | if (ex_count_p) |
| 75 | *ex_count_p = ex_count; |
| 76 | return ex_arr; |
| 77 | } |
| 78 | |
| 79 | #define USAGE_TEMPLATE_STR \ |
| 80 | "usage: %s oldfile newfile patchfile [old-extents new-extents]\n" \ |
| 81 | "with extents taking the form \"off_1:len_1,...,off_n:len_n\"\n" |
| 82 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 83 | int main(int argc,char * argv[]) |
| 84 | { |
| 85 | FILE * f, * cpf, * dpf, * epf; |
| 86 | BZFILE * cpfbz2, * dpfbz2, * epfbz2; |
| 87 | int cbz2err, dbz2err, ebz2err; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 88 | FILE *old_file = NULL, *new_file = NULL; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 89 | ssize_t oldsize,newsize; |
| 90 | ssize_t bzctrllen,bzdatalen; |
| 91 | u_char header[32],buf[8]; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 92 | u_char *new; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 93 | off_t oldpos,newpos; |
| 94 | off_t ctrl[3]; |
| 95 | off_t lenread; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 96 | off_t i, j; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 97 | |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 98 | if ((argc != 6) && (argc != 4)) errx(1, USAGE_TEMPLATE_STR, argv[0]); |
| 99 | int using_extents = (argc == 6); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 100 | |
| 101 | /* Open patch file */ |
| 102 | if ((f = fopen(argv[3], "r")) == NULL) |
| 103 | err(1, "fopen(%s)", argv[3]); |
| 104 | |
| 105 | /* |
| 106 | File format: |
| 107 | 0 8 "BSDIFF40" |
| 108 | 8 8 X |
| 109 | 16 8 Y |
| 110 | 24 8 sizeof(newfile) |
| 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"); |
| 123 | err(1, "fread(%s)", argv[3]); |
| 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)) |
| 139 | err(1, "fclose(%s)", argv[3]); |
| 140 | if ((cpf = fopen(argv[3], "r")) == NULL) |
| 141 | err(1, "fopen(%s)", argv[3]); |
| 142 | if (fseeko(cpf, 32, SEEK_SET)) |
| 143 | err(1, "fseeko(%s, %lld)", argv[3], |
| 144 | (long long)32); |
| 145 | if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL) |
| 146 | errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err); |
| 147 | if ((dpf = fopen(argv[3], "r")) == NULL) |
| 148 | err(1, "fopen(%s)", argv[3]); |
| 149 | if (fseeko(dpf, 32 + bzctrllen, SEEK_SET)) |
| 150 | err(1, "fseeko(%s, %lld)", argv[3], |
| 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); |
| 154 | if ((epf = fopen(argv[3], "r")) == NULL) |
| 155 | err(1, "fopen(%s)", argv[3]); |
| 156 | if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET)) |
| 157 | err(1, "fseeko(%s, %lld)", argv[3], |
| 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) { |
| 164 | size_t ex_count = 0; |
| 165 | ex_t *ex_arr = parse_extent_str(argv[4], &ex_count); |
| 166 | old_file = exfile_fopen(argv[1], "r", ex_arr, ex_count, free); |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 167 | } else { |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 168 | old_file = fopen(argv[1], "r"); |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 169 | } |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 170 | if (!old_file || |
| 171 | fseek(old_file, 0, SEEK_END) != 0 || |
| 172 | (oldsize = ftell(old_file)) < 0 || |
| 173 | fseek(old_file, 0, SEEK_SET) != 0) |
| 174 | err(1, "cannot obtain the size of %s", argv[1]); |
| 175 | off_t old_file_pos = 0; |
| 176 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 177 | if((new=malloc(newsize+1))==NULL) err(1,NULL); |
| 178 | |
| 179 | oldpos=0;newpos=0; |
| 180 | while(newpos<newsize) { |
| 181 | /* Read control data */ |
| 182 | for(i=0;i<=2;i++) { |
| 183 | lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8); |
| 184 | if ((lenread < 8) || ((cbz2err != BZ_OK) && |
| 185 | (cbz2err != BZ_STREAM_END))) |
| 186 | errx(1, "Corrupt patch\n"); |
| 187 | ctrl[i]=offtin(buf); |
| 188 | }; |
| 189 | |
Doug Zongker | 4d05479 | 2014-05-13 08:37:06 -0700 | [diff] [blame] | 190 | // android local change (start) |
| 191 | if (ctrl[0]<0||ctrl[1]<0) |
| 192 | errx(1,"Corrupt patch\n"); |
| 193 | // android local change (end) |
| 194 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 195 | /* Sanity-check */ |
| 196 | if(newpos+ctrl[0]>newsize) |
| 197 | errx(1,"Corrupt patch\n"); |
| 198 | |
| 199 | /* Read diff string */ |
| 200 | lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]); |
| 201 | if ((lenread < ctrl[0]) || |
| 202 | ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END))) |
| 203 | errx(1, "Corrupt patch\n"); |
| 204 | |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 205 | /* Add old data to diff string. It is enough to fseek once, at |
| 206 | * the beginning of the sequence, to avoid unnecessary |
| 207 | * overhead. */ |
| 208 | j = newpos; |
| 209 | if ((i = oldpos) < 0) { |
| 210 | j -= i; |
| 211 | i = 0; |
| 212 | } |
| 213 | if (i != old_file_pos && fseek(old_file, i, SEEK_SET) < 0) |
| 214 | err(1, "error seeking input file to offset %" PRIdMAX, |
| 215 | (intmax_t)i); |
| 216 | if ((old_file_pos = oldpos + ctrl[0]) > oldsize) |
| 217 | old_file_pos = oldsize; |
| 218 | while (i++ < old_file_pos) { |
| 219 | u_char c; |
| 220 | if (fread_unlocked(&c, 1, 1, old_file) != 1) |
| 221 | err(1, "error reading from input file"); |
| 222 | new[j++] += c; |
| 223 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 224 | |
| 225 | /* Adjust pointers */ |
| 226 | newpos+=ctrl[0]; |
| 227 | oldpos+=ctrl[0]; |
| 228 | |
| 229 | /* Sanity-check */ |
| 230 | if(newpos+ctrl[1]>newsize) |
| 231 | errx(1,"Corrupt patch\n"); |
| 232 | |
| 233 | /* Read extra string */ |
| 234 | lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]); |
| 235 | if ((lenread < ctrl[1]) || |
| 236 | ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END))) |
| 237 | errx(1, "Corrupt patch\n"); |
| 238 | |
| 239 | /* Adjust pointers */ |
| 240 | newpos+=ctrl[1]; |
| 241 | oldpos+=ctrl[2]; |
| 242 | }; |
| 243 | |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 244 | /* Close input file. */ |
| 245 | fclose(old_file); |
| 246 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 247 | /* Clean up the bzip2 reads */ |
| 248 | BZ2_bzReadClose(&cbz2err, cpfbz2); |
| 249 | BZ2_bzReadClose(&dbz2err, dpfbz2); |
| 250 | BZ2_bzReadClose(&ebz2err, epfbz2); |
| 251 | if (fclose(cpf) || fclose(dpf) || fclose(epf)) |
| 252 | err(1, "fclose(%s)", argv[3]); |
| 253 | |
| 254 | /* Write the new file */ |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 255 | if (using_extents) { |
| 256 | size_t ex_count = 0; |
| 257 | ex_t *ex_arr = parse_extent_str(argv[5], &ex_count); |
| 258 | new_file = exfile_fopen(argv[2], "w", ex_arr, ex_count, free); |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 259 | } else { |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 260 | new_file = fopen(argv[2], "w"); |
Zdenek Behan | 339e0ee | 2010-06-14 12:50:53 -0700 | [diff] [blame] | 261 | } |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame^] | 262 | if (!new_file || |
| 263 | fwrite_unlocked(new, 1, newsize, new_file) != newsize || |
| 264 | fclose(new_file) == EOF) |
| 265 | err(1,"%s",argv[2]); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 266 | |
| 267 | free(new); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |