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