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/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 cperciva Exp $"); |
| 29 | #endif |
| 30 | |
| 31 | #include <sys/types.h> |
| 32 | |
| 33 | #include <bzlib.h> |
| 34 | #include <err.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | |
Thieu Le | c2c68a7 | 2011-05-17 16:43:07 -0700 | [diff] [blame] | 41 | #if _FILE_OFFSET_BITS == 64 |
| 42 | #include "divsufsort64.h" |
| 43 | #define saidx_t saidx64_t |
| 44 | #define divsufsort divsufsort64 |
| 45 | #else |
| 46 | #include "divsufsort.h" |
| 47 | #endif |
| 48 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 49 | #define MIN(x,y) (((x)<(y)) ? (x) : (y)) |
| 50 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 51 | static off_t matchlen(u_char *old,off_t oldsize,u_char *new,off_t newsize) |
| 52 | { |
| 53 | off_t i; |
| 54 | |
| 55 | for(i=0;(i<oldsize)&&(i<newsize);i++) |
| 56 | if(old[i]!=new[i]) break; |
| 57 | |
| 58 | return i; |
| 59 | } |
| 60 | |
| 61 | static off_t search(off_t *I,u_char *old,off_t oldsize, |
| 62 | u_char *new,off_t newsize,off_t st,off_t en,off_t *pos) |
| 63 | { |
| 64 | off_t x,y; |
| 65 | |
| 66 | if(en-st<2) { |
| 67 | x=matchlen(old+I[st],oldsize-I[st],new,newsize); |
| 68 | y=matchlen(old+I[en],oldsize-I[en],new,newsize); |
| 69 | |
| 70 | if(x>y) { |
| 71 | *pos=I[st]; |
| 72 | return x; |
| 73 | } else { |
| 74 | *pos=I[en]; |
| 75 | return y; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | x=st+(en-st)/2; |
| 80 | if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) { |
| 81 | return search(I,old,oldsize,new,newsize,x,en,pos); |
| 82 | } else { |
| 83 | return search(I,old,oldsize,new,newsize,st,x,pos); |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | static void offtout(off_t x,u_char *buf) |
| 88 | { |
| 89 | off_t y; |
| 90 | |
| 91 | if(x<0) y=-x; else y=x; |
| 92 | |
| 93 | buf[0]=y%256;y-=buf[0]; |
| 94 | y=y/256;buf[1]=y%256;y-=buf[1]; |
| 95 | y=y/256;buf[2]=y%256;y-=buf[2]; |
| 96 | y=y/256;buf[3]=y%256;y-=buf[3]; |
| 97 | y=y/256;buf[4]=y%256;y-=buf[4]; |
| 98 | y=y/256;buf[5]=y%256;y-=buf[5]; |
| 99 | y=y/256;buf[6]=y%256;y-=buf[6]; |
| 100 | y=y/256;buf[7]=y%256; |
| 101 | |
| 102 | if(x<0) buf[7]|=0x80; |
| 103 | } |
| 104 | |
| 105 | int main(int argc,char *argv[]) |
| 106 | { |
| 107 | int fd; |
| 108 | u_char *old,*new; |
| 109 | off_t oldsize,newsize; |
Thieu Le | c2c68a7 | 2011-05-17 16:43:07 -0700 | [diff] [blame] | 110 | saidx_t *I; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 111 | off_t scan,pos,len; |
| 112 | off_t lastscan,lastpos,lastoffset; |
| 113 | off_t oldscore,scsc; |
| 114 | off_t s,Sf,lenf,Sb,lenb; |
| 115 | off_t overlap,Ss,lens; |
| 116 | off_t i; |
| 117 | off_t dblen,eblen; |
| 118 | u_char *db,*eb; |
| 119 | u_char buf[8]; |
| 120 | u_char header[32]; |
| 121 | FILE * pf; |
| 122 | BZFILE * pfbz2; |
| 123 | int bz2err; |
| 124 | |
| 125 | if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); |
| 126 | |
| 127 | /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure |
| 128 | that we never try to malloc(0) and get a NULL pointer */ |
| 129 | if(((fd=open(argv[1],O_RDONLY,0))<0) || |
| 130 | ((oldsize=lseek(fd,0,SEEK_END))==-1) || |
| 131 | ((old=malloc(oldsize+1))==NULL) || |
| 132 | (lseek(fd,0,SEEK_SET)!=0) || |
| 133 | (read(fd,old,oldsize)!=oldsize) || |
| 134 | (close(fd)==-1)) err(1,"%s",argv[1]); |
| 135 | |
Thieu Le | c2c68a7 | 2011-05-17 16:43:07 -0700 | [diff] [blame] | 136 | if(((I=malloc((oldsize+1)*sizeof(saidx_t)))==NULL)) err(1,NULL); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 137 | |
Thieu Le | c2c68a7 | 2011-05-17 16:43:07 -0700 | [diff] [blame] | 138 | if(divsufsort(old, I, oldsize)) err(1, "divsufsort"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 139 | |
| 140 | /* Allocate newsize+1 bytes instead of newsize bytes to ensure |
| 141 | that we never try to malloc(0) and get a NULL pointer */ |
| 142 | if(((fd=open(argv[2],O_RDONLY,0))<0) || |
| 143 | ((newsize=lseek(fd,0,SEEK_END))==-1) || |
| 144 | ((new=malloc(newsize+1))==NULL) || |
| 145 | (lseek(fd,0,SEEK_SET)!=0) || |
| 146 | (read(fd,new,newsize)!=newsize) || |
| 147 | (close(fd)==-1)) err(1,"%s",argv[2]); |
| 148 | |
| 149 | if(((db=malloc(newsize+1))==NULL) || |
| 150 | ((eb=malloc(newsize+1))==NULL)) err(1,NULL); |
| 151 | dblen=0; |
| 152 | eblen=0; |
| 153 | |
| 154 | /* Create the patch file */ |
| 155 | if ((pf = fopen(argv[3], "w")) == NULL) |
| 156 | err(1, "%s", argv[3]); |
| 157 | |
| 158 | /* Header is |
| 159 | 0 8 "BSDIFF40" |
| 160 | 8 8 length of bzip2ed ctrl block |
| 161 | 16 8 length of bzip2ed diff block |
| 162 | 24 8 length of new file */ |
| 163 | /* File is |
| 164 | 0 32 Header |
| 165 | 32 ?? Bzip2ed ctrl block |
| 166 | ?? ?? Bzip2ed diff block |
| 167 | ?? ?? Bzip2ed extra block */ |
| 168 | memcpy(header,"BSDIFF40",8); |
| 169 | offtout(0, header + 8); |
| 170 | offtout(0, header + 16); |
| 171 | offtout(newsize, header + 24); |
| 172 | if (fwrite(header, 32, 1, pf) != 1) |
| 173 | err(1, "fwrite(%s)", argv[3]); |
| 174 | |
| 175 | /* Compute the differences, writing ctrl as we go */ |
| 176 | if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) |
| 177 | errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); |
| 178 | scan=0;len=0; |
| 179 | lastscan=0;lastpos=0;lastoffset=0; |
| 180 | while(scan<newsize) { |
| 181 | oldscore=0; |
| 182 | |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame^] | 183 | /* If we come across a large block of data that only differs |
| 184 | * by less than 8 bytes, this loop will take a long time to |
| 185 | * go past that block of data. We need to track the number of |
| 186 | * times we're stuck in the block and break out of it. */ |
| 187 | int num_less_than_eight = 0; |
| 188 | off_t prev_len, prev_oldscore, prev_pos; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 189 | for(scsc=scan+=len;scan<newsize;scan++) { |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame^] | 190 | prev_len=len; |
| 191 | prev_oldscore=oldscore; |
| 192 | prev_pos=pos; |
| 193 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 194 | len=search(I,old,oldsize,new+scan,newsize-scan, |
| 195 | 0,oldsize,&pos); |
| 196 | |
| 197 | for(;scsc<scan+len;scsc++) |
| 198 | if((scsc+lastoffset<oldsize) && |
| 199 | (old[scsc+lastoffset] == new[scsc])) |
| 200 | oldscore++; |
| 201 | |
| 202 | if(((len==oldscore) && (len!=0)) || |
| 203 | (len>oldscore+8)) break; |
| 204 | |
| 205 | if((scan+lastoffset<oldsize) && |
| 206 | (old[scan+lastoffset] == new[scan])) |
| 207 | oldscore--; |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame^] | 208 | |
| 209 | if (len==prev_len-1 && |
| 210 | oldscore==prev_oldscore-1 && |
| 211 | pos==prev_pos+1 && |
| 212 | len>oldscore && |
| 213 | len<=oldscore+8) |
| 214 | ++num_less_than_eight; |
| 215 | else |
| 216 | num_less_than_eight=0; |
| 217 | if (num_less_than_eight > 100) break; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | if((len!=oldscore) || (scan==newsize)) { |
| 221 | s=0;Sf=0;lenf=0; |
| 222 | for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) { |
| 223 | if(old[lastpos+i]==new[lastscan+i]) s++; |
| 224 | i++; |
| 225 | if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; }; |
| 226 | }; |
| 227 | |
| 228 | lenb=0; |
| 229 | if(scan<newsize) { |
| 230 | s=0;Sb=0; |
| 231 | for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) { |
| 232 | if(old[pos-i]==new[scan-i]) s++; |
| 233 | if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; |
| 234 | }; |
| 235 | }; |
| 236 | |
| 237 | if(lastscan+lenf>scan-lenb) { |
| 238 | overlap=(lastscan+lenf)-(scan-lenb); |
| 239 | s=0;Ss=0;lens=0; |
| 240 | for(i=0;i<overlap;i++) { |
| 241 | if(new[lastscan+lenf-overlap+i]== |
| 242 | old[lastpos+lenf-overlap+i]) s++; |
| 243 | if(new[scan-lenb+i]== |
| 244 | old[pos-lenb+i]) s--; |
| 245 | if(s>Ss) { Ss=s; lens=i+1; }; |
| 246 | }; |
| 247 | |
| 248 | lenf+=lens-overlap; |
| 249 | lenb-=lens; |
| 250 | }; |
| 251 | |
| 252 | for(i=0;i<lenf;i++) |
| 253 | db[dblen+i]=new[lastscan+i]-old[lastpos+i]; |
| 254 | for(i=0;i<(scan-lenb)-(lastscan+lenf);i++) |
| 255 | eb[eblen+i]=new[lastscan+lenf+i]; |
| 256 | |
| 257 | dblen+=lenf; |
| 258 | eblen+=(scan-lenb)-(lastscan+lenf); |
| 259 | |
| 260 | offtout(lenf,buf); |
| 261 | BZ2_bzWrite(&bz2err, pfbz2, buf, 8); |
| 262 | if (bz2err != BZ_OK) |
| 263 | errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); |
| 264 | |
| 265 | offtout((scan-lenb)-(lastscan+lenf),buf); |
| 266 | BZ2_bzWrite(&bz2err, pfbz2, buf, 8); |
| 267 | if (bz2err != BZ_OK) |
| 268 | errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); |
| 269 | |
| 270 | offtout((pos-lenb)-(lastpos+lenf),buf); |
| 271 | BZ2_bzWrite(&bz2err, pfbz2, buf, 8); |
| 272 | if (bz2err != BZ_OK) |
| 273 | errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); |
| 274 | |
| 275 | lastscan=scan-lenb; |
| 276 | lastpos=pos-lenb; |
| 277 | lastoffset=pos-scan; |
| 278 | }; |
| 279 | }; |
| 280 | BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); |
| 281 | if (bz2err != BZ_OK) |
| 282 | errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); |
| 283 | |
| 284 | /* Compute size of compressed ctrl data */ |
| 285 | if ((len = ftello(pf)) == -1) |
| 286 | err(1, "ftello"); |
| 287 | offtout(len-32, header + 8); |
| 288 | |
| 289 | /* Write compressed diff data */ |
| 290 | if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) |
| 291 | errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); |
| 292 | BZ2_bzWrite(&bz2err, pfbz2, db, dblen); |
| 293 | if (bz2err != BZ_OK) |
| 294 | errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); |
| 295 | BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); |
| 296 | if (bz2err != BZ_OK) |
| 297 | errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); |
| 298 | |
| 299 | /* Compute size of compressed diff data */ |
| 300 | if ((newsize = ftello(pf)) == -1) |
| 301 | err(1, "ftello"); |
| 302 | offtout(newsize - len, header + 16); |
| 303 | |
| 304 | /* Write compressed extra data */ |
| 305 | if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) |
| 306 | errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); |
| 307 | BZ2_bzWrite(&bz2err, pfbz2, eb, eblen); |
| 308 | if (bz2err != BZ_OK) |
| 309 | errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); |
| 310 | BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); |
| 311 | if (bz2err != BZ_OK) |
| 312 | errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); |
| 313 | |
| 314 | /* Seek to the beginning, write the header, and close the file */ |
| 315 | if (fseeko(pf, 0, SEEK_SET)) |
| 316 | err(1, "fseeko"); |
| 317 | if (fwrite(header, 32, 1, pf) != 1) |
| 318 | err(1, "fwrite(%s)", argv[3]); |
| 319 | if (fclose(pf)) |
| 320 | err(1, "fclose"); |
| 321 | |
| 322 | /* Free the memory we used */ |
| 323 | free(db); |
| 324 | free(eb); |
| 325 | free(I); |
| 326 | free(old); |
| 327 | free(new); |
| 328 | |
| 329 | return 0; |
| 330 | } |