blob: 0fbbe5b401a2696d74b67e049f39efaa68ba5a07 [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 Deymoa28e0192017-09-08 14:21:05 +020044#include "patch_writer.h"
45
Alex Deymo20891f92015-10-12 17:28:04 -070046namespace bsdiff {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080047
Sen Jiang9b93e6a2016-05-03 15:09:15 -070048static off_t matchlen(const u_char* old, off_t oldsize, const u_char* new_buf,
Alex Deymo20891f92015-10-12 17:28:04 -070049 off_t newsize) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080050 off_t i;
51
52 for(i=0;(i<oldsize)&&(i<newsize);i++)
Alex Deymo20891f92015-10-12 17:28:04 -070053 if(old[i]!=new_buf[i]) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080054
55 return i;
56}
57
Sen Jiang01a8c862016-05-06 14:17:25 -070058// This is a binary search of the string |new_buf| of size |newsize| (or a
59// prefix of it) in the |old| string with size |oldsize| using the suffix array
60// |I|. |st| and |en| is the start and end of the search range (inclusive).
61// Returns the length of the longest prefix found and stores the position of the
62// string found in |*pos|.
Sen Jiang9b93e6a2016-05-03 15:09:15 -070063static off_t search(saidx_t* I, const u_char* old, off_t oldsize,
64 const u_char* new_buf, off_t newsize, off_t st, off_t en,
65 off_t* pos) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080066 off_t x,y;
67
68 if(en-st<2) {
Alex Deymo20891f92015-10-12 17:28:04 -070069 x=matchlen(old+I[st],oldsize-I[st],new_buf,newsize);
70 y=matchlen(old+I[en],oldsize-I[en],new_buf,newsize);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080071
72 if(x>y) {
73 *pos=I[st];
74 return x;
75 } else {
76 *pos=I[en];
77 return y;
78 }
79 };
80
81 x=st+(en-st)/2;
Alex Deymo20891f92015-10-12 17:28:04 -070082 if(memcmp(old+I[x],new_buf,std::min(oldsize-I[x],newsize))<=0) {
83 return search(I,old,oldsize,new_buf,newsize,x,en,pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080084 } else {
Alex Deymo20891f92015-10-12 17:28:04 -070085 return search(I,old,oldsize,new_buf,newsize,st,x,pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080086 };
87}
88
Alex Deymoa5cff222015-04-08 14:10:30 -070089int bsdiff(const char* old_filename, const char* new_filename,
90 const char* patch_filename) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080091 int fd;
Alex Deymo20891f92015-10-12 17:28:04 -070092 u_char *old_buf,*new_buf;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080093 off_t oldsize,newsize;
Sen Jiang9b93e6a2016-05-03 15:09:15 -070094
95 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
96 that we never try to malloc(0) and get a NULL pointer */
97 if(((fd=open(old_filename,O_RDONLY,0))<0) ||
98 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
99 ((old_buf=static_cast<u_char*>(malloc(oldsize+1)))==NULL) ||
100 (lseek(fd,0,SEEK_SET)!=0) ||
101 (read(fd,old_buf,oldsize)!=oldsize) ||
102 (close(fd)==-1)) err(1,"%s",old_filename);
103
104 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
105 that we never try to malloc(0) and get a NULL pointer */
106 if(((fd=open(new_filename,O_RDONLY,0))<0) ||
107 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
108 ((new_buf = static_cast<u_char*>(malloc(newsize+1)))==NULL) ||
109 (lseek(fd,0,SEEK_SET)!=0) ||
110 (read(fd,new_buf,newsize)!=newsize) ||
111 (close(fd)==-1)) err(1,"%s",new_filename);
112
Sen Jiang10df2672017-01-18 17:43:51 -0800113 int ret = bsdiff(old_buf, oldsize, new_buf, newsize, patch_filename, nullptr);
Sen Jiang9b93e6a2016-05-03 15:09:15 -0700114
115 free(old_buf);
116 free(new_buf);
117
118 return ret;
119}
120
Sen Jiang10df2672017-01-18 17:43:51 -0800121// Generate bsdiff patch from |old_buf| to |new_buf|, save the patch file to
122// |patch_filename|. Returns 0 on success.
123// |I_cache| can be used to cache the suffix array if the same |old_buf| is used
124// repeatedly, pass nullptr if not needed.
Sen Jiang9b93e6a2016-05-03 15:09:15 -0700125int bsdiff(const u_char* old_buf, off_t oldsize, const u_char* new_buf,
Sen Jiang10df2672017-01-18 17:43:51 -0800126 off_t newsize, const char* patch_filename, saidx_t** I_cache) {
Thieu Lec2c68a72011-05-17 16:43:07 -0700127 saidx_t *I;
Gilad Arnold99b53742013-04-30 09:24:14 -0700128 off_t scan,pos=0,len;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800129 off_t lastscan,lastpos,lastoffset;
130 off_t oldscore,scsc;
131 off_t s,Sf,lenf,Sb,lenb;
132 off_t overlap,Ss,lens;
133 off_t i;
Alex Deymoa28e0192017-09-08 14:21:05 +0200134 BsdiffPatchWriter patch(old_buf, oldsize, new_buf, newsize);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800135
Sen Jiang10df2672017-01-18 17:43:51 -0800136 if (I_cache && *I_cache) {
137 I = *I_cache;
138 } else {
139 if ((I=static_cast<saidx_t*>(malloc((oldsize+1)*sizeof(saidx_t))))==NULL)
140 err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800141
Sen Jiang10df2672017-01-18 17:43:51 -0800142 if (divsufsort(old_buf, I, oldsize)) err(1, "divsufsort");
143 if (I_cache)
144 *I_cache = I;
145 }
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800146
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800147 /* Create the patch file */
Alex Deymoa28e0192017-09-08 14:21:05 +0200148 if (!patch.Open(patch_filename))
149 return 1;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800150
151 /* Compute the differences, writing ctrl as we go */
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800152 scan=0;len=0;
153 lastscan=0;lastpos=0;lastoffset=0;
154 while(scan<newsize) {
155 oldscore=0;
156
Thieu Le1f402922011-06-14 15:30:25 -0700157 /* If we come across a large block of data that only differs
158 * by less than 8 bytes, this loop will take a long time to
159 * go past that block of data. We need to track the number of
160 * times we're stuck in the block and break out of it. */
161 int num_less_than_eight = 0;
162 off_t prev_len, prev_oldscore, prev_pos;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800163 for(scsc=scan+=len;scan<newsize;scan++) {
Thieu Le1f402922011-06-14 15:30:25 -0700164 prev_len=len;
165 prev_oldscore=oldscore;
166 prev_pos=pos;
167
Alex Deymo20891f92015-10-12 17:28:04 -0700168 len=search(I,old_buf,oldsize,new_buf+scan,newsize-scan,
Sen Jiang01a8c862016-05-06 14:17:25 -0700169 0,oldsize-1,&pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800170
171 for(;scsc<scan+len;scsc++)
172 if((scsc+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700173 (old_buf[scsc+lastoffset] == new_buf[scsc]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800174 oldscore++;
175
Alex Deymoa5cff222015-04-08 14:10:30 -0700176 if(((len==oldscore) && (len!=0)) ||
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800177 (len>oldscore+8)) break;
178
179 if((scan+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700180 (old_buf[scan+lastoffset] == new_buf[scan]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800181 oldscore--;
Thieu Le1f402922011-06-14 15:30:25 -0700182
Thieu Led1728202012-03-28 17:12:42 -0700183 const off_t fuzz = 8;
184 if (prev_len-fuzz<=len && len<=prev_len &&
185 prev_oldscore-fuzz<=oldscore &&
186 oldscore<=prev_oldscore &&
187 prev_pos<=pos && pos <=prev_pos+fuzz &&
188 oldscore<=len && len<=oldscore+fuzz)
Thieu Le1f402922011-06-14 15:30:25 -0700189 ++num_less_than_eight;
190 else
191 num_less_than_eight=0;
192 if (num_less_than_eight > 100) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800193 };
194
195 if((len!=oldscore) || (scan==newsize)) {
196 s=0;Sf=0;lenf=0;
197 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
Alex Deymo20891f92015-10-12 17:28:04 -0700198 if(old_buf[lastpos+i]==new_buf[lastscan+i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800199 i++;
200 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
201 };
202
203 lenb=0;
204 if(scan<newsize) {
205 s=0;Sb=0;
206 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700207 if(old_buf[pos-i]==new_buf[scan-i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800208 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
209 };
210 };
211
212 if(lastscan+lenf>scan-lenb) {
213 overlap=(lastscan+lenf)-(scan-lenb);
214 s=0;Ss=0;lens=0;
215 for(i=0;i<overlap;i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700216 if(new_buf[lastscan+lenf-overlap+i]==
217 old_buf[lastpos+lenf-overlap+i]) s++;
218 if(new_buf[scan-lenb+i]==
219 old_buf[pos-lenb+i]) s--;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800220 if(s>Ss) { Ss=s; lens=i+1; };
221 };
222
223 lenf+=lens-overlap;
224 lenb-=lens;
225 };
226
Alex Deymoa28e0192017-09-08 14:21:05 +0200227 if (!patch.AddControlEntry(ControlEntry(lenf,
228 (scan - lenb) - (lastscan + lenf),
229 (pos - lenb) - (lastpos + lenf))))
230 errx(1, "Writing a control entry");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800231
232 lastscan=scan-lenb;
233 lastpos=pos-lenb;
234 lastoffset=pos-scan;
235 };
236 };
Alex Deymoa28e0192017-09-08 14:21:05 +0200237 if (!patch.Close())
238 errx(1, "Closing the patch file");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800239
Sen Jiang10df2672017-01-18 17:43:51 -0800240 if (I_cache == nullptr)
241 free(I);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800242
243 return 0;
244}
Alex Deymo20891f92015-10-12 17:28:04 -0700245
246} // namespace bsdiff