blob: abfb6d19909e6eb81af4a3cfc3b0fd3fb0033b2d [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 Jiang9b93e6a2016-05-03 15:09:15 -070065static off_t search(saidx_t* I, const u_char* old, off_t oldsize,
66 const u_char* new_buf, off_t newsize, off_t st, off_t en,
67 off_t* pos) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080068 off_t x,y;
69
70 if(en-st<2) {
Alex Deymo20891f92015-10-12 17:28:04 -070071 x=matchlen(old+I[st],oldsize-I[st],new_buf,newsize);
72 y=matchlen(old+I[en],oldsize-I[en],new_buf,newsize);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080073
74 if(x>y) {
75 *pos=I[st];
76 return x;
77 } else {
78 *pos=I[en];
79 return y;
80 }
81 };
82
83 x=st+(en-st)/2;
Alex Deymo20891f92015-10-12 17:28:04 -070084 if(memcmp(old+I[x],new_buf,std::min(oldsize-I[x],newsize))<=0) {
85 return search(I,old,oldsize,new_buf,newsize,x,en,pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080086 } else {
Alex Deymo20891f92015-10-12 17:28:04 -070087 return search(I,old,oldsize,new_buf,newsize,st,x,pos);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080088 };
89}
90
91static void offtout(off_t x,u_char *buf)
92{
93 off_t y;
94
95 if(x<0) y=-x; else y=x;
96
97 buf[0]=y%256;y-=buf[0];
98 y=y/256;buf[1]=y%256;y-=buf[1];
99 y=y/256;buf[2]=y%256;y-=buf[2];
100 y=y/256;buf[3]=y%256;y-=buf[3];
101 y=y/256;buf[4]=y%256;y-=buf[4];
102 y=y/256;buf[5]=y%256;y-=buf[5];
103 y=y/256;buf[6]=y%256;y-=buf[6];
104 y=y/256;buf[7]=y%256;
105
106 if(x<0) buf[7]|=0x80;
107}
108
Alex Deymoa5cff222015-04-08 14:10:30 -0700109int bsdiff(const char* old_filename, const char* new_filename,
110 const char* patch_filename) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800111 int fd;
Alex Deymo20891f92015-10-12 17:28:04 -0700112 u_char *old_buf,*new_buf;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800113 off_t oldsize,newsize;
Sen Jiang9b93e6a2016-05-03 15:09:15 -0700114
115 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
116 that we never try to malloc(0) and get a NULL pointer */
117 if(((fd=open(old_filename,O_RDONLY,0))<0) ||
118 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
119 ((old_buf=static_cast<u_char*>(malloc(oldsize+1)))==NULL) ||
120 (lseek(fd,0,SEEK_SET)!=0) ||
121 (read(fd,old_buf,oldsize)!=oldsize) ||
122 (close(fd)==-1)) err(1,"%s",old_filename);
123
124 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
125 that we never try to malloc(0) and get a NULL pointer */
126 if(((fd=open(new_filename,O_RDONLY,0))<0) ||
127 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
128 ((new_buf = static_cast<u_char*>(malloc(newsize+1)))==NULL) ||
129 (lseek(fd,0,SEEK_SET)!=0) ||
130 (read(fd,new_buf,newsize)!=newsize) ||
131 (close(fd)==-1)) err(1,"%s",new_filename);
132
133 int ret = bsdiff(old_buf, oldsize, new_buf, newsize, patch_filename);
134
135 free(old_buf);
136 free(new_buf);
137
138 return ret;
139}
140
141int bsdiff(const u_char* old_buf, off_t oldsize, const u_char* new_buf,
142 off_t newsize, const char* patch_filename) {
Thieu Lec2c68a72011-05-17 16:43:07 -0700143 saidx_t *I;
Gilad Arnold99b53742013-04-30 09:24:14 -0700144 off_t scan,pos=0,len;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800145 off_t lastscan,lastpos,lastoffset;
146 off_t oldscore,scsc;
147 off_t s,Sf,lenf,Sb,lenb;
148 off_t overlap,Ss,lens;
149 off_t i;
150 off_t dblen,eblen;
151 u_char *db,*eb;
152 u_char buf[8];
153 u_char header[32];
154 FILE * pf;
155 BZFILE * pfbz2;
156 int bz2err;
157
Alex Deymo20891f92015-10-12 17:28:04 -0700158 if((I=static_cast<saidx_t*>(malloc((oldsize+1)*sizeof(saidx_t))))==NULL)
159 err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800160
Alex Deymo20891f92015-10-12 17:28:04 -0700161 if(divsufsort(old_buf, I, oldsize)) err(1, "divsufsort");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800162
Alex Deymo20891f92015-10-12 17:28:04 -0700163 if(((db=static_cast<u_char*>(malloc(newsize+1)))==NULL) ||
164 ((eb=static_cast<u_char*>(malloc(newsize+1)))==NULL)) err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800165 dblen=0;
166 eblen=0;
167
168 /* Create the patch file */
Alex Deymoa5cff222015-04-08 14:10:30 -0700169 if ((pf = fopen(patch_filename, "w")) == NULL)
170 err(1, "%s", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800171
172 /* Header is
173 0 8 "BSDIFF40"
174 8 8 length of bzip2ed ctrl block
175 16 8 length of bzip2ed diff block
176 24 8 length of new file */
177 /* File is
178 0 32 Header
179 32 ?? Bzip2ed ctrl block
180 ?? ?? Bzip2ed diff block
181 ?? ?? Bzip2ed extra block */
182 memcpy(header,"BSDIFF40",8);
183 offtout(0, header + 8);
184 offtout(0, header + 16);
185 offtout(newsize, header + 24);
186 if (fwrite(header, 32, 1, pf) != 1)
Alex Deymoa5cff222015-04-08 14:10:30 -0700187 err(1, "fwrite(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800188
189 /* Compute the differences, writing ctrl as we go */
190 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
191 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
192 scan=0;len=0;
193 lastscan=0;lastpos=0;lastoffset=0;
194 while(scan<newsize) {
195 oldscore=0;
196
Thieu Le1f402922011-06-14 15:30:25 -0700197 /* If we come across a large block of data that only differs
198 * by less than 8 bytes, this loop will take a long time to
199 * go past that block of data. We need to track the number of
200 * times we're stuck in the block and break out of it. */
201 int num_less_than_eight = 0;
202 off_t prev_len, prev_oldscore, prev_pos;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800203 for(scsc=scan+=len;scan<newsize;scan++) {
Thieu Le1f402922011-06-14 15:30:25 -0700204 prev_len=len;
205 prev_oldscore=oldscore;
206 prev_pos=pos;
207
Alex Deymo20891f92015-10-12 17:28:04 -0700208 len=search(I,old_buf,oldsize,new_buf+scan,newsize-scan,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800209 0,oldsize,&pos);
210
211 for(;scsc<scan+len;scsc++)
212 if((scsc+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700213 (old_buf[scsc+lastoffset] == new_buf[scsc]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800214 oldscore++;
215
Alex Deymoa5cff222015-04-08 14:10:30 -0700216 if(((len==oldscore) && (len!=0)) ||
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800217 (len>oldscore+8)) break;
218
219 if((scan+lastoffset<oldsize) &&
Alex Deymo20891f92015-10-12 17:28:04 -0700220 (old_buf[scan+lastoffset] == new_buf[scan]))
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800221 oldscore--;
Thieu Le1f402922011-06-14 15:30:25 -0700222
Thieu Led1728202012-03-28 17:12:42 -0700223 const off_t fuzz = 8;
224 if (prev_len-fuzz<=len && len<=prev_len &&
225 prev_oldscore-fuzz<=oldscore &&
226 oldscore<=prev_oldscore &&
227 prev_pos<=pos && pos <=prev_pos+fuzz &&
228 oldscore<=len && len<=oldscore+fuzz)
Thieu Le1f402922011-06-14 15:30:25 -0700229 ++num_less_than_eight;
230 else
231 num_less_than_eight=0;
232 if (num_less_than_eight > 100) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800233 };
234
235 if((len!=oldscore) || (scan==newsize)) {
236 s=0;Sf=0;lenf=0;
237 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
Alex Deymo20891f92015-10-12 17:28:04 -0700238 if(old_buf[lastpos+i]==new_buf[lastscan+i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800239 i++;
240 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
241 };
242
243 lenb=0;
244 if(scan<newsize) {
245 s=0;Sb=0;
246 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700247 if(old_buf[pos-i]==new_buf[scan-i]) s++;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800248 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
249 };
250 };
251
252 if(lastscan+lenf>scan-lenb) {
253 overlap=(lastscan+lenf)-(scan-lenb);
254 s=0;Ss=0;lens=0;
255 for(i=0;i<overlap;i++) {
Alex Deymo20891f92015-10-12 17:28:04 -0700256 if(new_buf[lastscan+lenf-overlap+i]==
257 old_buf[lastpos+lenf-overlap+i]) s++;
258 if(new_buf[scan-lenb+i]==
259 old_buf[pos-lenb+i]) s--;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800260 if(s>Ss) { Ss=s; lens=i+1; };
261 };
262
263 lenf+=lens-overlap;
264 lenb-=lens;
265 };
266
267 for(i=0;i<lenf;i++)
Alex Deymo20891f92015-10-12 17:28:04 -0700268 db[dblen+i]=new_buf[lastscan+i]-old_buf[lastpos+i];
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800269 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
Alex Deymo20891f92015-10-12 17:28:04 -0700270 eb[eblen+i]=new_buf[lastscan+lenf+i];
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800271
272 dblen+=lenf;
273 eblen+=(scan-lenb)-(lastscan+lenf);
274
275 offtout(lenf,buf);
276 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
277 if (bz2err != BZ_OK)
278 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
279
280 offtout((scan-lenb)-(lastscan+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((pos-lenb)-(lastpos+lenf),buf);
286 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
287 if (bz2err != BZ_OK)
288 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
289
290 lastscan=scan-lenb;
291 lastpos=pos-lenb;
292 lastoffset=pos-scan;
293 };
294 };
295 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
296 if (bz2err != BZ_OK)
297 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
298
299 /* Compute size of compressed ctrl data */
300 if ((len = ftello(pf)) == -1)
301 err(1, "ftello");
302 offtout(len-32, header + 8);
303
304 /* Write compressed diff data */
305 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
306 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
307 BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
308 if (bz2err != BZ_OK)
309 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
310 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
311 if (bz2err != BZ_OK)
312 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
313
314 /* Compute size of compressed diff data */
315 if ((newsize = ftello(pf)) == -1)
316 err(1, "ftello");
317 offtout(newsize - len, header + 16);
318
319 /* Write compressed extra data */
320 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
321 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
322 BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
323 if (bz2err != BZ_OK)
324 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
325 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
326 if (bz2err != BZ_OK)
327 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
328
329 /* Seek to the beginning, write the header, and close the file */
330 if (fseeko(pf, 0, SEEK_SET))
331 err(1, "fseeko");
332 if (fwrite(header, 32, 1, pf) != 1)
Alex Deymoa5cff222015-04-08 14:10:30 -0700333 err(1, "fwrite(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800334 if (fclose(pf))
335 err(1, "fclose");
336
337 /* Free the memory we used */
338 free(db);
339 free(eb);
340 free(I);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800341
342 return 0;
343}
Alex Deymo20891f92015-10-12 17:28:04 -0700344
345} // namespace bsdiff