blob: ec9e81b232b7e1e7aa4a39400430487574fb00f3 [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>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
Alex Deymo20891f92015-10-12 17:28:04 -070042#include <algorithm>
43
Alex Deymo68c0e7f2017-10-02 20:38:12 +020044#include "bsdiff/diff_encoder.h"
Alex Deymodcd423b2017-09-13 20:54:24 +020045#include "bsdiff/patch_writer.h"
Alex Deymo48ad5ab2017-09-13 22:17:57 +020046#include "bsdiff/suffix_array_index.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020047
Alex Deymo20891f92015-10-12 17:28:04 -070048namespace bsdiff {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080049
Alex Deymoa5cff222015-04-08 14:10:30 -070050int bsdiff(const char* old_filename, const char* new_filename,
51 const char* patch_filename) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080052 int fd;
Alex Deymo48ad5ab2017-09-13 22:17:57 +020053 uint8_t *old_buf,*new_buf;
54 ssize_t oldsize,newsize;
Sen Jiang9b93e6a2016-05-03 15:09:15 -070055
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 Deymo48ad5ab2017-09-13 22:17:57 +020060 ((old_buf=static_cast<uint8_t*>(malloc(oldsize+1)))==NULL) ||
Sen Jiang9b93e6a2016-05-03 15:09:15 -070061 (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 Deymo48ad5ab2017-09-13 22:17:57 +020069 ((new_buf = static_cast<uint8_t*>(malloc(newsize+1)))==NULL) ||
Sen Jiang9b93e6a2016-05-03 15:09:15 -070070 (lseek(fd,0,SEEK_SET)!=0) ||
71 (read(fd,new_buf,newsize)!=newsize) ||
72 (close(fd)==-1)) err(1,"%s",new_filename);
73
Sen Jiang10df2672017-01-18 17:43:51 -080074 int ret = bsdiff(old_buf, oldsize, new_buf, newsize, patch_filename, nullptr);
Sen Jiang9b93e6a2016-05-03 15:09:15 -070075
76 free(old_buf);
77 free(new_buf);
78
79 return ret;
80}
81
Alex Deymo538a75d2017-09-27 15:34:59 +020082// 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 Deymo48ad5ab2017-09-13 22:17:57 +020085int 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 Deymo538a75d2017-09-27 15:34:59 +020088 BsdiffPatchWriter patch(patch_filename);
Alex Deymo48ad5ab2017-09-13 22:17:57 +020089 return bsdiff(old_buf, oldsize, new_buf, newsize, &patch, sai_cache);
Alex Deymo538a75d2017-09-27 15:34:59 +020090}
91
Alex Deymo48ad5ab2017-09-13 22:17:57 +020092int 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 Projectc285fea2009-03-03 19:29:20 -0800103
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200104 std::unique_ptr<SuffixArrayIndexInterface> local_sai;
105 SuffixArrayIndexInterface* sai;
106
107 if (sai_cache && *sai_cache) {
108 sai = *sai_cache;
Sen Jiang10df2672017-01-18 17:43:51 -0800109 } else {
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200110 local_sai = CreateSuffixArrayIndex(old_buf, oldsize);
111 if (!local_sai)
112 return 1;
113 sai = local_sai.get();
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800114
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200115 // Transfer ownership to the caller.
116 if (sai_cache)
117 *sai_cache = local_sai.release();
Sen Jiang10df2672017-01-18 17:43:51 -0800118 }
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800119
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200120 /* Initialize the patch file encoder */
121 if (!patch->Init())
Alex Deymoa28e0192017-09-08 14:21:05 +0200122 return 1;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200123 DiffEncoder diff_encoder(patch, old_buf, oldsize, new_buf, newsize);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800124
125 /* Compute the differences, writing ctrl as we go */
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800126 scan=0;len=0;
127 lastscan=0;lastpos=0;lastoffset=0;
128 while(scan<newsize) {
129 oldscore=0;
130
Thieu Le1f402922011-06-14 15:30:25 -0700131 /* 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 Deymo48ad5ab2017-09-13 22:17:57 +0200136 size_t prev_len;
137 uint64_t prev_pos, prev_oldscore;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800138 for(scsc=scan+=len;scan<newsize;scan++) {
Thieu Le1f402922011-06-14 15:30:25 -0700139 prev_len=len;
140 prev_oldscore=oldscore;
141 prev_pos=pos;
142
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200143 sai->SearchPrefix(new_buf + scan, newsize - scan, &len, &pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800144
145 for(;scsc<scan+len;scsc++)
146 if((scsc+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700147 (old_buf[scsc+lastoffset] == new_buf[scsc]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800148 oldscore++;
149
Alex Deymoa5cff222015-04-08 14:10:30 -0700150 if(((len==oldscore) && (len!=0)) ||
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800151 (len>oldscore+8)) break;
152
153 if((scan+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700154 (old_buf[scan+lastoffset] == new_buf[scan]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800155 oldscore--;
Thieu Le1f402922011-06-14 15:30:25 -0700156
Alex Deymo48ad5ab2017-09-13 22:17:57 +0200157 const size_t fuzz = 8;
Thieu Led1728202012-03-28 17:12:42 -0700158 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 Le1f402922011-06-14 15:30:25 -0700163 ++num_less_than_eight;
164 else
165 num_less_than_eight=0;
166 if (num_less_than_eight > 100) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800167 };
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 Deymo20891f92015-10-12 17:28:04 -0700172 if(old_buf[lastpos+i]==new_buf[lastscan+i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800173 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 Deymo48ad5ab2017-09-13 22:17:57 +0200180 for(i=1;(scan>=lastscan+i)&&(pos>=static_cast<uint64_t>(i));i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700181 if(old_buf[pos-i]==new_buf[scan-i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800182 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 Deymo20891f92015-10-12 17:28:04 -0700190 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 Projectc285fea2009-03-03 19:29:20 -0800194 if(s>Ss) { Ss=s; lens=i+1; };
195 };
196
197 lenf+=lens-overlap;
198 lenb-=lens;
199 };
200
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200201 if (!diff_encoder.AddControlEntry(
Alex Deymo538a75d2017-09-27 15:34:59 +0200202 ControlEntry(lenf,
203 (scan - lenb) - (lastscan + lenf),
204 (pos - lenb) - (lastpos + lenf))))
Alex Deymoa28e0192017-09-08 14:21:05 +0200205 errx(1, "Writing a control entry");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800206
207 lastscan=scan-lenb;
208 lastpos=pos-lenb;
209 lastoffset=pos-scan;
210 };
211 };
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200212 if (!diff_encoder.Close())
Alex Deymoa28e0192017-09-08 14:21:05 +0200213 errx(1, "Closing the patch file");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800214
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800215 return 0;
216}
Alex Deymo20891f92015-10-12 17:28:04 -0700217
218} // namespace bsdiff