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 |
Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 6 | * modification, are permitted providing that the following conditions |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 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 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 31 | #include "bsdiff/bsdiff.h" |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 32 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 33 | #include <sys/types.h> |
| 34 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 35 | #include <err.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <unistd.h> |
| 41 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 42 | #include <algorithm> |
| 43 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 44 | #include "bsdiff/diff_encoder.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 45 | #include "bsdiff/patch_writer.h" |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 46 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 47 | namespace bsdiff { |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 48 | |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 49 | static off_t matchlen(const u_char* old, off_t oldsize, const u_char* new_buf, |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 50 | off_t newsize) { |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 51 | off_t i; |
| 52 | |
| 53 | for(i=0;(i<oldsize)&&(i<newsize);i++) |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 54 | if(old[i]!=new_buf[i]) break; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 55 | |
| 56 | return i; |
| 57 | } |
| 58 | |
Sen Jiang | 01a8c86 | 2016-05-06 14:17:25 -0700 | [diff] [blame] | 59 | // This is a binary search of the string |new_buf| of size |newsize| (or a |
| 60 | // prefix of it) in the |old| string with size |oldsize| using the suffix array |
| 61 | // |I|. |st| and |en| is the start and end of the search range (inclusive). |
| 62 | // Returns the length of the longest prefix found and stores the position of the |
| 63 | // string found in |*pos|. |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 64 | static off_t search(saidx_t* I, const u_char* old, off_t oldsize, |
| 65 | const u_char* new_buf, off_t newsize, off_t st, off_t en, |
| 66 | off_t* pos) { |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 67 | off_t x,y; |
| 68 | |
| 69 | if(en-st<2) { |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 70 | x=matchlen(old+I[st],oldsize-I[st],new_buf,newsize); |
| 71 | y=matchlen(old+I[en],oldsize-I[en],new_buf,newsize); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 72 | |
| 73 | if(x>y) { |
| 74 | *pos=I[st]; |
| 75 | return x; |
| 76 | } else { |
| 77 | *pos=I[en]; |
| 78 | return y; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | x=st+(en-st)/2; |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 83 | if(memcmp(old+I[x],new_buf,std::min(oldsize-I[x],newsize))<=0) { |
| 84 | return search(I,old,oldsize,new_buf,newsize,x,en,pos); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 85 | } else { |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 86 | return search(I,old,oldsize,new_buf,newsize,st,x,pos); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 87 | }; |
| 88 | } |
| 89 | |
Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 90 | int bsdiff(const char* old_filename, const char* new_filename, |
| 91 | const char* patch_filename) { |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 92 | int fd; |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 93 | u_char *old_buf,*new_buf; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 94 | off_t oldsize,newsize; |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 95 | |
| 96 | /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure |
| 97 | that we never try to malloc(0) and get a NULL pointer */ |
| 98 | if(((fd=open(old_filename,O_RDONLY,0))<0) || |
| 99 | ((oldsize=lseek(fd,0,SEEK_END))==-1) || |
| 100 | ((old_buf=static_cast<u_char*>(malloc(oldsize+1)))==NULL) || |
| 101 | (lseek(fd,0,SEEK_SET)!=0) || |
| 102 | (read(fd,old_buf,oldsize)!=oldsize) || |
| 103 | (close(fd)==-1)) err(1,"%s",old_filename); |
| 104 | |
| 105 | /* Allocate newsize+1 bytes instead of newsize bytes to ensure |
| 106 | that we never try to malloc(0) and get a NULL pointer */ |
| 107 | if(((fd=open(new_filename,O_RDONLY,0))<0) || |
| 108 | ((newsize=lseek(fd,0,SEEK_END))==-1) || |
| 109 | ((new_buf = static_cast<u_char*>(malloc(newsize+1)))==NULL) || |
| 110 | (lseek(fd,0,SEEK_SET)!=0) || |
| 111 | (read(fd,new_buf,newsize)!=newsize) || |
| 112 | (close(fd)==-1)) err(1,"%s",new_filename); |
| 113 | |
Sen Jiang | 10df267 | 2017-01-18 17:43:51 -0800 | [diff] [blame] | 114 | int ret = bsdiff(old_buf, oldsize, new_buf, newsize, patch_filename, nullptr); |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 115 | |
| 116 | free(old_buf); |
| 117 | free(new_buf); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 122 | // TODO(deymo): Deprecate this version of the interface and move all callers |
| 123 | // to the underlying version using PatchWriterInterface instead. This allows |
| 124 | // more flexible options including different encodings. |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 125 | int bsdiff(const u_char* old_buf, off_t oldsize, const u_char* new_buf, |
Sen Jiang | 10df267 | 2017-01-18 17:43:51 -0800 | [diff] [blame] | 126 | off_t newsize, const char* patch_filename, saidx_t** I_cache) { |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 127 | BsdiffPatchWriter patch(patch_filename); |
| 128 | return bsdiff(old_buf, oldsize, new_buf, newsize, &patch, I_cache); |
| 129 | } |
| 130 | |
| 131 | int bsdiff(const u_char* old_buf, off_t oldsize, const u_char* new_buf, |
| 132 | off_t newsize, PatchWriterInterface* patch, saidx_t** I_cache) { |
Thieu Le | c2c68a7 | 2011-05-17 16:43:07 -0700 | [diff] [blame] | 133 | saidx_t *I; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 134 | off_t scan,pos=0,len; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 135 | off_t lastscan,lastpos,lastoffset; |
| 136 | off_t oldscore,scsc; |
| 137 | off_t s,Sf,lenf,Sb,lenb; |
| 138 | off_t overlap,Ss,lens; |
| 139 | off_t i; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 140 | |
Sen Jiang | 10df267 | 2017-01-18 17:43:51 -0800 | [diff] [blame] | 141 | if (I_cache && *I_cache) { |
| 142 | I = *I_cache; |
| 143 | } else { |
| 144 | if ((I=static_cast<saidx_t*>(malloc((oldsize+1)*sizeof(saidx_t))))==NULL) |
| 145 | err(1,NULL); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 146 | |
Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 147 | // Note: divsufsort() fails when the passed size is 0 and old_buf is NULL. |
| 148 | if (oldsize > 0 && divsufsort(old_buf, I, oldsize)) err(1, "divsufsort"); |
Sen Jiang | 10df267 | 2017-01-18 17:43:51 -0800 | [diff] [blame] | 149 | if (I_cache) |
| 150 | *I_cache = I; |
| 151 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 152 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 153 | /* Initialize the patch file encoder */ |
| 154 | if (!patch->Init()) |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 155 | return 1; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 156 | DiffEncoder diff_encoder(patch, old_buf, oldsize, new_buf, newsize); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 157 | |
| 158 | /* Compute the differences, writing ctrl as we go */ |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 159 | scan=0;len=0; |
| 160 | lastscan=0;lastpos=0;lastoffset=0; |
| 161 | while(scan<newsize) { |
| 162 | oldscore=0; |
| 163 | |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame] | 164 | /* If we come across a large block of data that only differs |
| 165 | * by less than 8 bytes, this loop will take a long time to |
| 166 | * go past that block of data. We need to track the number of |
| 167 | * times we're stuck in the block and break out of it. */ |
| 168 | int num_less_than_eight = 0; |
| 169 | off_t prev_len, prev_oldscore, prev_pos; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 170 | for(scsc=scan+=len;scan<newsize;scan++) { |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame] | 171 | prev_len=len; |
| 172 | prev_oldscore=oldscore; |
| 173 | prev_pos=pos; |
| 174 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 175 | len=search(I,old_buf,oldsize,new_buf+scan,newsize-scan, |
Sen Jiang | 01a8c86 | 2016-05-06 14:17:25 -0700 | [diff] [blame] | 176 | 0,oldsize-1,&pos); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 177 | |
| 178 | for(;scsc<scan+len;scsc++) |
| 179 | if((scsc+lastoffset<oldsize) && |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 180 | (old_buf[scsc+lastoffset] == new_buf[scsc])) |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 181 | oldscore++; |
| 182 | |
Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 183 | if(((len==oldscore) && (len!=0)) || |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 184 | (len>oldscore+8)) break; |
| 185 | |
| 186 | if((scan+lastoffset<oldsize) && |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 187 | (old_buf[scan+lastoffset] == new_buf[scan])) |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 188 | oldscore--; |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame] | 189 | |
Thieu Le | d172820 | 2012-03-28 17:12:42 -0700 | [diff] [blame] | 190 | const off_t fuzz = 8; |
| 191 | if (prev_len-fuzz<=len && len<=prev_len && |
| 192 | prev_oldscore-fuzz<=oldscore && |
| 193 | oldscore<=prev_oldscore && |
| 194 | prev_pos<=pos && pos <=prev_pos+fuzz && |
| 195 | oldscore<=len && len<=oldscore+fuzz) |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame] | 196 | ++num_less_than_eight; |
| 197 | else |
| 198 | num_less_than_eight=0; |
| 199 | if (num_less_than_eight > 100) break; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | if((len!=oldscore) || (scan==newsize)) { |
| 203 | s=0;Sf=0;lenf=0; |
| 204 | for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) { |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 205 | if(old_buf[lastpos+i]==new_buf[lastscan+i]) s++; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 206 | i++; |
| 207 | if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; }; |
| 208 | }; |
| 209 | |
| 210 | lenb=0; |
| 211 | if(scan<newsize) { |
| 212 | s=0;Sb=0; |
| 213 | for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) { |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 214 | if(old_buf[pos-i]==new_buf[scan-i]) s++; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 215 | if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; |
| 216 | }; |
| 217 | }; |
| 218 | |
| 219 | if(lastscan+lenf>scan-lenb) { |
| 220 | overlap=(lastscan+lenf)-(scan-lenb); |
| 221 | s=0;Ss=0;lens=0; |
| 222 | for(i=0;i<overlap;i++) { |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 223 | if(new_buf[lastscan+lenf-overlap+i]== |
| 224 | old_buf[lastpos+lenf-overlap+i]) s++; |
| 225 | if(new_buf[scan-lenb+i]== |
| 226 | old_buf[pos-lenb+i]) s--; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 227 | if(s>Ss) { Ss=s; lens=i+1; }; |
| 228 | }; |
| 229 | |
| 230 | lenf+=lens-overlap; |
| 231 | lenb-=lens; |
| 232 | }; |
| 233 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 234 | if (!diff_encoder.AddControlEntry( |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 235 | ControlEntry(lenf, |
| 236 | (scan - lenb) - (lastscan + lenf), |
| 237 | (pos - lenb) - (lastpos + lenf)))) |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 238 | errx(1, "Writing a control entry"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 239 | |
| 240 | lastscan=scan-lenb; |
| 241 | lastpos=pos-lenb; |
| 242 | lastoffset=pos-scan; |
| 243 | }; |
| 244 | }; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 245 | if (!diff_encoder.Close()) |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 246 | errx(1, "Closing the patch file"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 247 | |
Sen Jiang | 10df267 | 2017-01-18 17:43:51 -0800 | [diff] [blame] | 248 | if (I_cache == nullptr) |
| 249 | free(I); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 250 | |
| 251 | return 0; |
| 252 | } |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 253 | |
| 254 | } // namespace bsdiff |