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 | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 46 | #include "bsdiff/suffix_array_index.h" |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 47 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 48 | namespace bsdiff { |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 49 | |
Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 50 | int bsdiff(const char* old_filename, const char* new_filename, |
| 51 | const char* patch_filename) { |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 52 | int fd; |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 53 | uint8_t *old_buf,*new_buf; |
| 54 | ssize_t oldsize,newsize; |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 55 | |
| 56 | /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure |
| 57 | that we never try to malloc(0) and get a NULL pointer */ |
| 58 | if(((fd=open(old_filename,O_RDONLY,0))<0) || |
| 59 | ((oldsize=lseek(fd,0,SEEK_END))==-1) || |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 60 | ((old_buf=static_cast<uint8_t*>(malloc(oldsize+1)))==NULL) || |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 61 | (lseek(fd,0,SEEK_SET)!=0) || |
| 62 | (read(fd,old_buf,oldsize)!=oldsize) || |
| 63 | (close(fd)==-1)) err(1,"%s",old_filename); |
| 64 | |
| 65 | /* Allocate newsize+1 bytes instead of newsize bytes to ensure |
| 66 | that we never try to malloc(0) and get a NULL pointer */ |
| 67 | if(((fd=open(new_filename,O_RDONLY,0))<0) || |
| 68 | ((newsize=lseek(fd,0,SEEK_END))==-1) || |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 69 | ((new_buf = static_cast<uint8_t*>(malloc(newsize+1)))==NULL) || |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 70 | (lseek(fd,0,SEEK_SET)!=0) || |
| 71 | (read(fd,new_buf,newsize)!=newsize) || |
| 72 | (close(fd)==-1)) err(1,"%s",new_filename); |
| 73 | |
Sen Jiang | 10df267 | 2017-01-18 17:43:51 -0800 | [diff] [blame] | 74 | int ret = bsdiff(old_buf, oldsize, new_buf, newsize, patch_filename, nullptr); |
Sen Jiang | 9b93e6a | 2016-05-03 15:09:15 -0700 | [diff] [blame] | 75 | |
| 76 | free(old_buf); |
| 77 | free(new_buf); |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 82 | // TODO(deymo): Deprecate this version of the interface and move all callers |
| 83 | // to the underlying version using PatchWriterInterface instead. This allows |
| 84 | // more flexible options including different encodings. |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 85 | int bsdiff(const uint8_t* old_buf, size_t oldsize, const uint8_t* new_buf, |
| 86 | size_t newsize, const char* patch_filename, |
| 87 | SuffixArrayIndexInterface** sai_cache) { |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 88 | BsdiffPatchWriter patch(patch_filename); |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 89 | return bsdiff(old_buf, oldsize, new_buf, newsize, &patch, sai_cache); |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 92 | int bsdiff(const uint8_t* old_buf, size_t oldsize, const uint8_t* new_buf, |
| 93 | size_t newsize, PatchWriterInterface* patch, |
| 94 | SuffixArrayIndexInterface** sai_cache) { |
| 95 | size_t scsc, scan; |
| 96 | uint64_t pos=0; |
| 97 | size_t len; |
| 98 | size_t lastscan,lastpos,lastoffset; |
| 99 | uint64_t oldscore; |
| 100 | ssize_t s,Sf,lenf,Sb,lenb; |
| 101 | ssize_t overlap,Ss,lens; |
| 102 | ssize_t i; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 103 | |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 104 | std::unique_ptr<SuffixArrayIndexInterface> local_sai; |
| 105 | SuffixArrayIndexInterface* sai; |
| 106 | |
| 107 | if (sai_cache && *sai_cache) { |
| 108 | sai = *sai_cache; |
Sen Jiang | 10df267 | 2017-01-18 17:43:51 -0800 | [diff] [blame] | 109 | } else { |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 110 | local_sai = CreateSuffixArrayIndex(old_buf, oldsize); |
| 111 | if (!local_sai) |
| 112 | return 1; |
| 113 | sai = local_sai.get(); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 114 | |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 115 | // Transfer ownership to the caller. |
| 116 | if (sai_cache) |
| 117 | *sai_cache = local_sai.release(); |
Sen Jiang | 10df267 | 2017-01-18 17:43:51 -0800 | [diff] [blame] | 118 | } |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 119 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 120 | /* Initialize the patch file encoder */ |
| 121 | if (!patch->Init()) |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 122 | return 1; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 123 | 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] | 124 | |
| 125 | /* Compute the differences, writing ctrl as we go */ |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 126 | scan=0;len=0; |
| 127 | lastscan=0;lastpos=0;lastoffset=0; |
| 128 | while(scan<newsize) { |
| 129 | oldscore=0; |
| 130 | |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame] | 131 | /* If we come across a large block of data that only differs |
| 132 | * by less than 8 bytes, this loop will take a long time to |
| 133 | * go past that block of data. We need to track the number of |
| 134 | * times we're stuck in the block and break out of it. */ |
| 135 | int num_less_than_eight = 0; |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 136 | size_t prev_len; |
| 137 | uint64_t prev_pos, prev_oldscore; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 138 | for(scsc=scan+=len;scan<newsize;scan++) { |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame] | 139 | prev_len=len; |
| 140 | prev_oldscore=oldscore; |
| 141 | prev_pos=pos; |
| 142 | |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 143 | sai->SearchPrefix(new_buf + scan, newsize - scan, &len, &pos); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 144 | |
| 145 | for(;scsc<scan+len;scsc++) |
| 146 | if((scsc+lastoffset<oldsize) && |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 147 | (old_buf[scsc+lastoffset] == new_buf[scsc])) |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 148 | oldscore++; |
| 149 | |
Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 150 | if(((len==oldscore) && (len!=0)) || |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 151 | (len>oldscore+8)) break; |
| 152 | |
| 153 | if((scan+lastoffset<oldsize) && |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 154 | (old_buf[scan+lastoffset] == new_buf[scan])) |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 155 | oldscore--; |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame] | 156 | |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 157 | const size_t fuzz = 8; |
Thieu Le | d172820 | 2012-03-28 17:12:42 -0700 | [diff] [blame] | 158 | if (prev_len-fuzz<=len && len<=prev_len && |
| 159 | prev_oldscore-fuzz<=oldscore && |
| 160 | oldscore<=prev_oldscore && |
| 161 | prev_pos<=pos && pos <=prev_pos+fuzz && |
| 162 | oldscore<=len && len<=oldscore+fuzz) |
Thieu Le | 1f40292 | 2011-06-14 15:30:25 -0700 | [diff] [blame] | 163 | ++num_less_than_eight; |
| 164 | else |
| 165 | num_less_than_eight=0; |
| 166 | if (num_less_than_eight > 100) break; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | if((len!=oldscore) || (scan==newsize)) { |
| 170 | s=0;Sf=0;lenf=0; |
| 171 | for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) { |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 172 | 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] | 173 | i++; |
| 174 | if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; }; |
| 175 | }; |
| 176 | |
| 177 | lenb=0; |
| 178 | if(scan<newsize) { |
| 179 | s=0;Sb=0; |
Alex Deymo | 48ad5ab | 2017-09-13 22:17:57 +0200 | [diff] [blame^] | 180 | for(i=1;(scan>=lastscan+i)&&(pos>=static_cast<uint64_t>(i));i++) { |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 181 | 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] | 182 | if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; |
| 183 | }; |
| 184 | }; |
| 185 | |
| 186 | if(lastscan+lenf>scan-lenb) { |
| 187 | overlap=(lastscan+lenf)-(scan-lenb); |
| 188 | s=0;Ss=0;lens=0; |
| 189 | for(i=0;i<overlap;i++) { |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 190 | if(new_buf[lastscan+lenf-overlap+i]== |
| 191 | old_buf[lastpos+lenf-overlap+i]) s++; |
| 192 | if(new_buf[scan-lenb+i]== |
| 193 | old_buf[pos-lenb+i]) s--; |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 194 | if(s>Ss) { Ss=s; lens=i+1; }; |
| 195 | }; |
| 196 | |
| 197 | lenf+=lens-overlap; |
| 198 | lenb-=lens; |
| 199 | }; |
| 200 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 201 | if (!diff_encoder.AddControlEntry( |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 202 | ControlEntry(lenf, |
| 203 | (scan - lenb) - (lastscan + lenf), |
| 204 | (pos - lenb) - (lastpos + lenf)))) |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 205 | errx(1, "Writing a control entry"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 206 | |
| 207 | lastscan=scan-lenb; |
| 208 | lastpos=pos-lenb; |
| 209 | lastoffset=pos-scan; |
| 210 | }; |
| 211 | }; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 212 | if (!diff_encoder.Close()) |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 213 | errx(1, "Closing the patch file"); |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 214 | |
The Android Open Source Project | c285fea | 2009-03-03 19:29:20 -0800 | [diff] [blame] | 215 | return 0; |
| 216 | } |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 217 | |
| 218 | } // namespace bsdiff |