blob: 669a1e503513c26c82e878a1a3baa12a982c5ff8 [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
Sen Jiang9b93e6a2016-05-03 15:09:15 -070031#include "bsdiff.h"
32
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080033#include <sys/types.h>
34
35#include <bzlib.h>
36#include <err.h>
37#include <fcntl.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
Alex Deymo20891f92015-10-12 17:28:04 -070043#include <algorithm>
44
Thieu Lec2c68a72011-05-17 16:43:07 -070045#if _FILE_OFFSET_BITS == 64
46#include "divsufsort64.h"
47#define saidx_t saidx64_t
48#define divsufsort divsufsort64
49#else
50#include "divsufsort.h"
51#endif
52
Alex Deymo20891f92015-10-12 17:28:04 -070053namespace bsdiff {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080054
Sen Jiang9b93e6a2016-05-03 15:09:15 -070055static off_t matchlen(const u_char* old, off_t oldsize, const u_char* new_buf,
Alex Deymo20891f92015-10-12 17:28:04 -070056 off_t newsize) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080057 off_t i;
58
59 for(i=0;(i<oldsize)&&(i<newsize);i++)
Alex Deymo20891f92015-10-12 17:28:04 -070060 if(old[i]!=new_buf[i]) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080061
62 return i;
63}
64
Sen Jiang01a8c862016-05-06 14:17:25 -070065// This is a binary search of the string |new_buf| of size |newsize| (or a
66// prefix of it) in the |old| string with size |oldsize| using the suffix array
67// |I|. |st| and |en| is the start and end of the search range (inclusive).
68// Returns the length of the longest prefix found and stores the position of the
69// string found in |*pos|.
Sen Jiang9b93e6a2016-05-03 15:09:15 -070070static off_t search(saidx_t* I, const u_char* old, off_t oldsize,
71 const u_char* new_buf, off_t newsize, off_t st, off_t en,
72 off_t* pos) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080073 off_t x,y;
74
75 if(en-st<2) {
Alex Deymo20891f92015-10-12 17:28:04 -070076 x=matchlen(old+I[st],oldsize-I[st],new_buf,newsize);
77 y=matchlen(old+I[en],oldsize-I[en],new_buf,newsize);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080078
79 if(x>y) {
80 *pos=I[st];
81 return x;
82 } else {
83 *pos=I[en];
84 return y;
85 }
86 };
87
88 x=st+(en-st)/2;
Alex Deymo20891f92015-10-12 17:28:04 -070089 if(memcmp(old+I[x],new_buf,std::min(oldsize-I[x],newsize))<=0) {
90 return search(I,old,oldsize,new_buf,newsize,x,en,pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080091 } else {
Alex Deymo20891f92015-10-12 17:28:04 -070092 return search(I,old,oldsize,new_buf,newsize,st,x,pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080093 };
94}
95
96static void offtout(off_t x,u_char *buf)
97{
98 off_t y;
99
100 if(x<0) y=-x; else y=x;
101
102 buf[0]=y%256;y-=buf[0];
103 y=y/256;buf[1]=y%256;y-=buf[1];
104 y=y/256;buf[2]=y%256;y-=buf[2];
105 y=y/256;buf[3]=y%256;y-=buf[3];
106 y=y/256;buf[4]=y%256;y-=buf[4];
107 y=y/256;buf[5]=y%256;y-=buf[5];
108 y=y/256;buf[6]=y%256;y-=buf[6];
109 y=y/256;buf[7]=y%256;
110
111 if(x<0) buf[7]|=0x80;
112}
113
Alex Deymoa5cff222015-04-08 14:10:30 -0700114int bsdiff(const char* old_filename, const char* new_filename,
115 const char* patch_filename) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800116 int fd;
Alex Deymo20891f92015-10-12 17:28:04 -0700117 u_char *old_buf,*new_buf;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800118 off_t oldsize,newsize;
Sen Jiang9b93e6a2016-05-03 15:09:15 -0700119
120 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
121 that we never try to malloc(0) and get a NULL pointer */
122 if(((fd=open(old_filename,O_RDONLY,0))<0) ||
123 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
124 ((old_buf=static_cast<u_char*>(malloc(oldsize+1)))==NULL) ||
125 (lseek(fd,0,SEEK_SET)!=0) ||
126 (read(fd,old_buf,oldsize)!=oldsize) ||
127 (close(fd)==-1)) err(1,"%s",old_filename);
128
129 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
130 that we never try to malloc(0) and get a NULL pointer */
131 if(((fd=open(new_filename,O_RDONLY,0))<0) ||
132 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
133 ((new_buf = static_cast<u_char*>(malloc(newsize+1)))==NULL) ||
134 (lseek(fd,0,SEEK_SET)!=0) ||
135 (read(fd,new_buf,newsize)!=newsize) ||
136 (close(fd)==-1)) err(1,"%s",new_filename);
137
138 int ret = bsdiff(old_buf, oldsize, new_buf, newsize, patch_filename);
139
140 free(old_buf);
141 free(new_buf);
142
143 return ret;
144}
145
146int bsdiff(const u_char* old_buf, off_t oldsize, const u_char* new_buf,
147 off_t newsize, const char* patch_filename) {
Thieu Lec2c68a72011-05-17 16:43:07 -0700148 saidx_t *I;
Gilad Arnold99b53742013-04-30 09:24:14 -0700149 off_t scan,pos=0,len;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800150 off_t lastscan,lastpos,lastoffset;
151 off_t oldscore,scsc;
152 off_t s,Sf,lenf,Sb,lenb;
153 off_t overlap,Ss,lens;
154 off_t i;
155 off_t dblen,eblen;
156 u_char *db,*eb;
157 u_char buf[8];
158 u_char header[32];
159 FILE * pf;
160 BZFILE * pfbz2;
161 int bz2err;
162
Alex Deymo20891f92015-10-12 17:28:04 -0700163 if((I=static_cast<saidx_t*>(malloc((oldsize+1)*sizeof(saidx_t))))==NULL)
164 err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800165
Alex Deymo20891f92015-10-12 17:28:04 -0700166 if(divsufsort(old_buf, I, oldsize)) err(1, "divsufsort");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800167
Alex Deymo20891f92015-10-12 17:28:04 -0700168 if(((db=static_cast<u_char*>(malloc(newsize+1)))==NULL) ||
169 ((eb=static_cast<u_char*>(malloc(newsize+1)))==NULL)) err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800170 dblen=0;
171 eblen=0;
172
173 /* Create the patch file */
Alex Deymoa5cff222015-04-08 14:10:30 -0700174 if ((pf = fopen(patch_filename, "w")) == NULL)
175 err(1, "%s", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800176
177 /* Header is
178 0 8 "BSDIFF40"
179 8 8 length of bzip2ed ctrl block
180 16 8 length of bzip2ed diff block
181 24 8 length of new file */
182 /* File is
183 0 32 Header
184 32 ?? Bzip2ed ctrl block
185 ?? ?? Bzip2ed diff block
186 ?? ?? Bzip2ed extra block */
187 memcpy(header,"BSDIFF40",8);
188 offtout(0, header + 8);
189 offtout(0, header + 16);
190 offtout(newsize, header + 24);
191 if (fwrite(header, 32, 1, pf) != 1)
Alex Deymoa5cff222015-04-08 14:10:30 -0700192 err(1, "fwrite(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800193
194 /* Compute the differences, writing ctrl as we go */
195 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
196 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
197 scan=0;len=0;
198 lastscan=0;lastpos=0;lastoffset=0;
199 while(scan<newsize) {
200 oldscore=0;
201
Thieu Le1f402922011-06-14 15:30:25 -0700202 /* If we come across a large block of data that only differs
203 * by less than 8 bytes, this loop will take a long time to
204 * go past that block of data. We need to track the number of
205 * times we're stuck in the block and break out of it. */
206 int num_less_than_eight = 0;
207 off_t prev_len, prev_oldscore, prev_pos;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800208 for(scsc=scan+=len;scan<newsize;scan++) {
Thieu Le1f402922011-06-14 15:30:25 -0700209 prev_len=len;
210 prev_oldscore=oldscore;
211 prev_pos=pos;
212
Alex Deymo20891f92015-10-12 17:28:04 -0700213 len=search(I,old_buf,oldsize,new_buf+scan,newsize-scan,
Sen Jiang01a8c862016-05-06 14:17:25 -0700214 0,oldsize-1,&pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800215
216 for(;scsc<scan+len;scsc++)
217 if((scsc+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700218 (old_buf[scsc+lastoffset] == new_buf[scsc]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800219 oldscore++;
220
Alex Deymoa5cff222015-04-08 14:10:30 -0700221 if(((len==oldscore) && (len!=0)) ||
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800222 (len>oldscore+8)) break;
223
224 if((scan+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700225 (old_buf[scan+lastoffset] == new_buf[scan]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800226 oldscore--;
Thieu Le1f402922011-06-14 15:30:25 -0700227
Thieu Led1728202012-03-28 17:12:42 -0700228 const off_t fuzz = 8;
229 if (prev_len-fuzz<=len && len<=prev_len &&
230 prev_oldscore-fuzz<=oldscore &&
231 oldscore<=prev_oldscore &&
232 prev_pos<=pos && pos <=prev_pos+fuzz &&
233 oldscore<=len && len<=oldscore+fuzz)
Thieu Le1f402922011-06-14 15:30:25 -0700234 ++num_less_than_eight;
235 else
236 num_less_than_eight=0;
237 if (num_less_than_eight > 100) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800238 };
239
240 if((len!=oldscore) || (scan==newsize)) {
241 s=0;Sf=0;lenf=0;
242 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
Alex Deymo20891f92015-10-12 17:28:04 -0700243 if(old_buf[lastpos+i]==new_buf[lastscan+i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800244 i++;
245 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
246 };
247
248 lenb=0;
249 if(scan<newsize) {
250 s=0;Sb=0;
251 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700252 if(old_buf[pos-i]==new_buf[scan-i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800253 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
254 };
255 };
256
257 if(lastscan+lenf>scan-lenb) {
258 overlap=(lastscan+lenf)-(scan-lenb);
259 s=0;Ss=0;lens=0;
260 for(i=0;i<overlap;i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700261 if(new_buf[lastscan+lenf-overlap+i]==
262 old_buf[lastpos+lenf-overlap+i]) s++;
263 if(new_buf[scan-lenb+i]==
264 old_buf[pos-lenb+i]) s--;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800265 if(s>Ss) { Ss=s; lens=i+1; };
266 };
267
268 lenf+=lens-overlap;
269 lenb-=lens;
270 };
271
272 for(i=0;i<lenf;i++)
Alex Deymo20891f92015-10-12 17:28:04 -0700273 db[dblen+i]=new_buf[lastscan+i]-old_buf[lastpos+i];
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800274 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
Alex Deymo20891f92015-10-12 17:28:04 -0700275 eb[eblen+i]=new_buf[lastscan+lenf+i];
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800276
277 dblen+=lenf;
278 eblen+=(scan-lenb)-(lastscan+lenf);
279
280 offtout(lenf,buf);
281 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
282 if (bz2err != BZ_OK)
283 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
284
285 offtout((scan-lenb)-(lastscan+lenf),buf);
286 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
287 if (bz2err != BZ_OK)
288 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
289
290 offtout((pos-lenb)-(lastpos+lenf),buf);
291 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
292 if (bz2err != BZ_OK)
293 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
294
295 lastscan=scan-lenb;
296 lastpos=pos-lenb;
297 lastoffset=pos-scan;
298 };
299 };
300 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
301 if (bz2err != BZ_OK)
302 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
303
304 /* Compute size of compressed ctrl data */
305 if ((len = ftello(pf)) == -1)
306 err(1, "ftello");
307 offtout(len-32, header + 8);
308
309 /* Write compressed diff data */
310 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
311 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
312 BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
313 if (bz2err != BZ_OK)
314 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
315 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
316 if (bz2err != BZ_OK)
317 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
318
319 /* Compute size of compressed diff data */
320 if ((newsize = ftello(pf)) == -1)
321 err(1, "ftello");
322 offtout(newsize - len, header + 16);
323
324 /* Write compressed extra data */
325 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
326 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
327 BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
328 if (bz2err != BZ_OK)
329 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
330 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
331 if (bz2err != BZ_OK)
332 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
333
334 /* Seek to the beginning, write the header, and close the file */
335 if (fseeko(pf, 0, SEEK_SET))
336 err(1, "fseeko");
337 if (fwrite(header, 32, 1, pf) != 1)
Alex Deymoa5cff222015-04-08 14:10:30 -0700338 err(1, "fwrite(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800339 if (fclose(pf))
340 err(1, "fclose");
341
342 /* Free the memory we used */
343 free(db);
344 free(eb);
345 free(I);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800346
347 return 0;
348}
Alex Deymo20891f92015-10-12 17:28:04 -0700349
350} // namespace bsdiff