blob: 8bb21b7a7b94ebbcff80b8b2ed8cd68ba7adefd9 [file] [log] [blame]
The Android Open Source Projectc285fea2009-03-03 19:29:20 -08001/*-
2 * Copyright 2003-2005 Colin Percival
3 * All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
Alex Deymoa5cff222015-04-08 14:10:30 -07006 * modification, are permitted providing that the following conditions
The Android Open Source Projectc285fea2009-03-03 19:29:20 -08007 * 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 Deymoddf9db52017-03-02 16:10:41 -080031#include "bsdiff/bsdiff.h"
Sen Jiang9b93e6a2016-05-03 15:09:15 -070032
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080033#include <sys/types.h>
34
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080035#include <err.h>
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080036#include <string.h>
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080037
Alex Deymo20891f92015-10-12 17:28:04 -070038#include <algorithm>
39
Alex Deymo68c0e7f2017-10-02 20:38:12 +020040#include "bsdiff/diff_encoder.h"
Alex Deymodcd423b2017-09-13 20:54:24 +020041#include "bsdiff/patch_writer.h"
Alex Deymo48ad5ab2017-09-13 22:17:57 +020042#include "bsdiff/suffix_array_index.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020043
Alex Deymo20891f92015-10-12 17:28:04 -070044namespace bsdiff {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080045
Alex Deymo538a75d2017-09-27 15:34:59 +020046// 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 Deymo48ad5ab2017-09-13 22:17:57 +020049int 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) {
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080052 BsdiffPatchWriter patch(patch_filename);
Alex Deymo383f6772018-02-08 15:50:11 +010053 return bsdiff(old_buf, oldsize, new_buf, newsize, 0, &patch,
54 sai_cache);
Alex Deymo538a75d2017-09-27 15:34:59 +020055}
56
Alex Deymo48ad5ab2017-09-13 22:17:57 +020057int bsdiff(const uint8_t* old_buf, size_t oldsize, const uint8_t* new_buf,
58 size_t newsize, PatchWriterInterface* patch,
59 SuffixArrayIndexInterface** sai_cache) {
Alex Deymo383f6772018-02-08 15:50:11 +010060 return bsdiff(old_buf, oldsize, new_buf, newsize, 0, patch,
61 sai_cache);
62}
63
64int bsdiff(const uint8_t* old_buf, size_t oldsize, const uint8_t* new_buf,
65 size_t newsize, size_t min_length, PatchWriterInterface* patch,
66 SuffixArrayIndexInterface** sai_cache) {
Alex Deymo48ad5ab2017-09-13 22:17:57 +020067 size_t scsc, scan;
68 uint64_t pos=0;
69 size_t len;
70 size_t lastscan,lastpos,lastoffset;
71 uint64_t oldscore;
72 ssize_t s,Sf,lenf,Sb,lenb;
73 ssize_t overlap,Ss,lens;
74 ssize_t i;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080075
Alex Deymo48ad5ab2017-09-13 22:17:57 +020076 std::unique_ptr<SuffixArrayIndexInterface> local_sai;
77 SuffixArrayIndexInterface* sai;
78
79 if (sai_cache && *sai_cache) {
80 sai = *sai_cache;
Sen Jiang10df2672017-01-18 17:43:51 -080081 } else {
Alex Deymo48ad5ab2017-09-13 22:17:57 +020082 local_sai = CreateSuffixArrayIndex(old_buf, oldsize);
83 if (!local_sai)
84 return 1;
85 sai = local_sai.get();
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080086
Alex Deymo48ad5ab2017-09-13 22:17:57 +020087 // Transfer ownership to the caller.
88 if (sai_cache)
89 *sai_cache = local_sai.release();
Sen Jiang10df2672017-01-18 17:43:51 -080090 }
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080091
Alex Deymo68c0e7f2017-10-02 20:38:12 +020092 /* Initialize the patch file encoder */
Alex Deymo68c0e7f2017-10-02 20:38:12 +020093 DiffEncoder diff_encoder(patch, old_buf, oldsize, new_buf, newsize);
Alex Deymo4dadd8b2017-10-26 16:19:33 +020094 if (!diff_encoder.Init())
95 return 1;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080096
97 /* Compute the differences, writing ctrl as we go */
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080098 scan=0;len=0;
99 lastscan=0;lastpos=0;lastoffset=0;
100 while(scan<newsize) {
101 oldscore=0;
102
Thieu Le1f402922011-06-14 15:30:25 -0700103 /* If we come across a large block of data that only differs
104 * by less than 8 bytes, this loop will take a long time to
105 * go past that block of data. We need to track the number of
106 * times we're stuck in the block and break out of it. */
107 int num_less_than_eight = 0;
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200108 size_t prev_len;
109 uint64_t prev_pos, prev_oldscore;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800110 for(scsc=scan+=len;scan<newsize;scan++) {
Thieu Le1f402922011-06-14 15:30:25 -0700111 prev_len=len;
112 prev_oldscore=oldscore;
113 prev_pos=pos;
114
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200115 sai->SearchPrefix(new_buf + scan, newsize - scan, &len, &pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800116
Sen Jiang3a4725a2018-04-06 15:33:06 -0700117 for(;scsc<scan+len && scsc+lastoffset<oldsize;scsc++)
118 if(old_buf[scsc+lastoffset] == new_buf[scsc])
119 oldscore++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800120
Alex Deymoa5cff222015-04-08 14:10:30 -0700121 if(((len==oldscore) && (len!=0)) ||
Alex Deymo383f6772018-02-08 15:50:11 +0100122 (len>oldscore+8 && len>=min_length)) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800123
124 if((scan+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700125 (old_buf[scan+lastoffset] == new_buf[scan]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800126 oldscore--;
Thieu Le1f402922011-06-14 15:30:25 -0700127
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200128 const size_t fuzz = 8;
Thieu Led1728202012-03-28 17:12:42 -0700129 if (prev_len-fuzz<=len && len<=prev_len &&
130 prev_oldscore-fuzz<=oldscore &&
131 oldscore<=prev_oldscore &&
132 prev_pos<=pos && pos <=prev_pos+fuzz &&
133 oldscore<=len && len<=oldscore+fuzz)
Thieu Le1f402922011-06-14 15:30:25 -0700134 ++num_less_than_eight;
135 else
136 num_less_than_eight=0;
137 if (num_less_than_eight > 100) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800138 };
139
140 if((len!=oldscore) || (scan==newsize)) {
141 s=0;Sf=0;lenf=0;
142 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
Alex Deymo20891f92015-10-12 17:28:04 -0700143 if(old_buf[lastpos+i]==new_buf[lastscan+i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800144 i++;
145 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
146 };
147
148 lenb=0;
149 if(scan<newsize) {
150 s=0;Sb=0;
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200151 for(i=1;(scan>=lastscan+i)&&(pos>=static_cast<uint64_t>(i));i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700152 if(old_buf[pos-i]==new_buf[scan-i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800153 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
154 };
155 };
156
157 if(lastscan+lenf>scan-lenb) {
158 overlap=(lastscan+lenf)-(scan-lenb);
159 s=0;Ss=0;lens=0;
160 for(i=0;i<overlap;i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700161 if(new_buf[lastscan+lenf-overlap+i]==
162 old_buf[lastpos+lenf-overlap+i]) s++;
163 if(new_buf[scan-lenb+i]==
164 old_buf[pos-lenb+i]) s--;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800165 if(s>Ss) { Ss=s; lens=i+1; };
166 };
167
168 lenf+=lens-overlap;
169 lenb-=lens;
170 };
171
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200172 if (!diff_encoder.AddControlEntry(
Alex Deymo538a75d2017-09-27 15:34:59 +0200173 ControlEntry(lenf,
174 (scan - lenb) - (lastscan + lenf),
175 (pos - lenb) - (lastpos + lenf))))
Alex Deymoa28e0192017-09-08 14:21:05 +0200176 errx(1, "Writing a control entry");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800177
178 lastscan=scan-lenb;
179 lastpos=pos-lenb;
180 lastoffset=pos-scan;
181 };
182 };
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200183 if (!diff_encoder.Close())
Alex Deymoa28e0192017-09-08 14:21:05 +0200184 errx(1, "Closing the patch file");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800185
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800186 return 0;
187}
Alex Deymo20891f92015-10-12 17:28:04 -0700188
189} // namespace bsdiff