blob: 815437681c98d68dd88f39bd939dceb49ed812a9 [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
31#include <sys/types.h>
32
33#include <bzlib.h>
34#include <err.h>
35#include <fcntl.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
Thieu Lec2c68a72011-05-17 16:43:07 -070041#if _FILE_OFFSET_BITS == 64
42#include "divsufsort64.h"
43#define saidx_t saidx64_t
44#define divsufsort divsufsort64
45#else
46#include "divsufsort.h"
47#endif
48
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080049#define MIN(x,y) (((x)<(y)) ? (x) : (y))
50
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080051static off_t matchlen(u_char *old,off_t oldsize,u_char *new,off_t newsize)
52{
53 off_t i;
54
55 for(i=0;(i<oldsize)&&(i<newsize);i++)
56 if(old[i]!=new[i]) break;
57
58 return i;
59}
60
Gilad Arnold99b53742013-04-30 09:24:14 -070061static off_t search(saidx_t *I,u_char *old,off_t oldsize,
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080062 u_char *new,off_t newsize,off_t st,off_t en,off_t *pos)
63{
64 off_t x,y;
65
66 if(en-st<2) {
67 x=matchlen(old+I[st],oldsize-I[st],new,newsize);
68 y=matchlen(old+I[en],oldsize-I[en],new,newsize);
69
70 if(x>y) {
71 *pos=I[st];
72 return x;
73 } else {
74 *pos=I[en];
75 return y;
76 }
77 };
78
79 x=st+(en-st)/2;
Thieu Lee3fd60e2011-09-22 17:59:31 -070080 if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<=0) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080081 return search(I,old,oldsize,new,newsize,x,en,pos);
82 } else {
83 return search(I,old,oldsize,new,newsize,st,x,pos);
84 };
85}
86
87static void offtout(off_t x,u_char *buf)
88{
89 off_t y;
90
91 if(x<0) y=-x; else y=x;
92
93 buf[0]=y%256;y-=buf[0];
94 y=y/256;buf[1]=y%256;y-=buf[1];
95 y=y/256;buf[2]=y%256;y-=buf[2];
96 y=y/256;buf[3]=y%256;y-=buf[3];
97 y=y/256;buf[4]=y%256;y-=buf[4];
98 y=y/256;buf[5]=y%256;y-=buf[5];
99 y=y/256;buf[6]=y%256;y-=buf[6];
100 y=y/256;buf[7]=y%256;
101
102 if(x<0) buf[7]|=0x80;
103}
104
Alex Deymoa5cff222015-04-08 14:10:30 -0700105int bsdiff(const char* old_filename, const char* new_filename,
106 const char* patch_filename) {
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800107 int fd;
108 u_char *old,*new;
109 off_t oldsize,newsize;
Thieu Lec2c68a72011-05-17 16:43:07 -0700110 saidx_t *I;
Gilad Arnold99b53742013-04-30 09:24:14 -0700111 off_t scan,pos=0,len;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800112 off_t lastscan,lastpos,lastoffset;
113 off_t oldscore,scsc;
114 off_t s,Sf,lenf,Sb,lenb;
115 off_t overlap,Ss,lens;
116 off_t i;
117 off_t dblen,eblen;
118 u_char *db,*eb;
119 u_char buf[8];
120 u_char header[32];
121 FILE * pf;
122 BZFILE * pfbz2;
123 int bz2err;
124
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800125 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
126 that we never try to malloc(0) and get a NULL pointer */
Alex Deymoa5cff222015-04-08 14:10:30 -0700127 if(((fd=open(old_filename,O_RDONLY,0))<0) ||
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800128 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
129 ((old=malloc(oldsize+1))==NULL) ||
130 (lseek(fd,0,SEEK_SET)!=0) ||
131 (read(fd,old,oldsize)!=oldsize) ||
Alex Deymoa5cff222015-04-08 14:10:30 -0700132 (close(fd)==-1)) err(1,"%s",old_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800133
Thieu Lec2c68a72011-05-17 16:43:07 -0700134 if(((I=malloc((oldsize+1)*sizeof(saidx_t)))==NULL)) err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800135
Thieu Lec2c68a72011-05-17 16:43:07 -0700136 if(divsufsort(old, I, oldsize)) err(1, "divsufsort");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800137
138 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
139 that we never try to malloc(0) and get a NULL pointer */
Alex Deymoa5cff222015-04-08 14:10:30 -0700140 if(((fd=open(new_filename,O_RDONLY,0))<0) ||
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800141 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
142 ((new=malloc(newsize+1))==NULL) ||
143 (lseek(fd,0,SEEK_SET)!=0) ||
144 (read(fd,new,newsize)!=newsize) ||
Alex Deymoa5cff222015-04-08 14:10:30 -0700145 (close(fd)==-1)) err(1,"%s",new_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800146
147 if(((db=malloc(newsize+1))==NULL) ||
148 ((eb=malloc(newsize+1))==NULL)) err(1,NULL);
149 dblen=0;
150 eblen=0;
151
152 /* Create the patch file */
Alex Deymoa5cff222015-04-08 14:10:30 -0700153 if ((pf = fopen(patch_filename, "w")) == NULL)
154 err(1, "%s", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800155
156 /* Header is
157 0 8 "BSDIFF40"
158 8 8 length of bzip2ed ctrl block
159 16 8 length of bzip2ed diff block
160 24 8 length of new file */
161 /* File is
162 0 32 Header
163 32 ?? Bzip2ed ctrl block
164 ?? ?? Bzip2ed diff block
165 ?? ?? Bzip2ed extra block */
166 memcpy(header,"BSDIFF40",8);
167 offtout(0, header + 8);
168 offtout(0, header + 16);
169 offtout(newsize, header + 24);
170 if (fwrite(header, 32, 1, pf) != 1)
Alex Deymoa5cff222015-04-08 14:10:30 -0700171 err(1, "fwrite(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800172
173 /* Compute the differences, writing ctrl as we go */
174 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
175 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
176 scan=0;len=0;
177 lastscan=0;lastpos=0;lastoffset=0;
178 while(scan<newsize) {
179 oldscore=0;
180
Thieu Le1f402922011-06-14 15:30:25 -0700181 /* If we come across a large block of data that only differs
182 * by less than 8 bytes, this loop will take a long time to
183 * go past that block of data. We need to track the number of
184 * times we're stuck in the block and break out of it. */
185 int num_less_than_eight = 0;
186 off_t prev_len, prev_oldscore, prev_pos;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800187 for(scsc=scan+=len;scan<newsize;scan++) {
Thieu Le1f402922011-06-14 15:30:25 -0700188 prev_len=len;
189 prev_oldscore=oldscore;
190 prev_pos=pos;
191
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800192 len=search(I,old,oldsize,new+scan,newsize-scan,
193 0,oldsize,&pos);
194
195 for(;scsc<scan+len;scsc++)
196 if((scsc+lastoffset<oldsize) &&
197 (old[scsc+lastoffset] == new[scsc]))
198 oldscore++;
199
Alex Deymoa5cff222015-04-08 14:10:30 -0700200 if(((len==oldscore) && (len!=0)) ||
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800201 (len>oldscore+8)) break;
202
203 if((scan+lastoffset<oldsize) &&
204 (old[scan+lastoffset] == new[scan]))
205 oldscore--;
Thieu Le1f402922011-06-14 15:30:25 -0700206
Thieu Led1728202012-03-28 17:12:42 -0700207 const off_t fuzz = 8;
208 if (prev_len-fuzz<=len && len<=prev_len &&
209 prev_oldscore-fuzz<=oldscore &&
210 oldscore<=prev_oldscore &&
211 prev_pos<=pos && pos <=prev_pos+fuzz &&
212 oldscore<=len && len<=oldscore+fuzz)
Thieu Le1f402922011-06-14 15:30:25 -0700213 ++num_less_than_eight;
214 else
215 num_less_than_eight=0;
216 if (num_less_than_eight > 100) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800217 };
218
219 if((len!=oldscore) || (scan==newsize)) {
220 s=0;Sf=0;lenf=0;
221 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
222 if(old[lastpos+i]==new[lastscan+i]) s++;
223 i++;
224 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
225 };
226
227 lenb=0;
228 if(scan<newsize) {
229 s=0;Sb=0;
230 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
231 if(old[pos-i]==new[scan-i]) s++;
232 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
233 };
234 };
235
236 if(lastscan+lenf>scan-lenb) {
237 overlap=(lastscan+lenf)-(scan-lenb);
238 s=0;Ss=0;lens=0;
239 for(i=0;i<overlap;i++) {
240 if(new[lastscan+lenf-overlap+i]==
241 old[lastpos+lenf-overlap+i]) s++;
242 if(new[scan-lenb+i]==
243 old[pos-lenb+i]) s--;
244 if(s>Ss) { Ss=s; lens=i+1; };
245 };
246
247 lenf+=lens-overlap;
248 lenb-=lens;
249 };
250
251 for(i=0;i<lenf;i++)
252 db[dblen+i]=new[lastscan+i]-old[lastpos+i];
253 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
254 eb[eblen+i]=new[lastscan+lenf+i];
255
256 dblen+=lenf;
257 eblen+=(scan-lenb)-(lastscan+lenf);
258
259 offtout(lenf,buf);
260 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
261 if (bz2err != BZ_OK)
262 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
263
264 offtout((scan-lenb)-(lastscan+lenf),buf);
265 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
266 if (bz2err != BZ_OK)
267 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
268
269 offtout((pos-lenb)-(lastpos+lenf),buf);
270 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
271 if (bz2err != BZ_OK)
272 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
273
274 lastscan=scan-lenb;
275 lastpos=pos-lenb;
276 lastoffset=pos-scan;
277 };
278 };
279 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
280 if (bz2err != BZ_OK)
281 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
282
283 /* Compute size of compressed ctrl data */
284 if ((len = ftello(pf)) == -1)
285 err(1, "ftello");
286 offtout(len-32, header + 8);
287
288 /* Write compressed diff data */
289 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
290 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
291 BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
292 if (bz2err != BZ_OK)
293 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
294 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
295 if (bz2err != BZ_OK)
296 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
297
298 /* Compute size of compressed diff data */
299 if ((newsize = ftello(pf)) == -1)
300 err(1, "ftello");
301 offtout(newsize - len, header + 16);
302
303 /* Write compressed extra data */
304 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
305 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
306 BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
307 if (bz2err != BZ_OK)
308 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
309 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
310 if (bz2err != BZ_OK)
311 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
312
313 /* Seek to the beginning, write the header, and close the file */
314 if (fseeko(pf, 0, SEEK_SET))
315 err(1, "fseeko");
316 if (fwrite(header, 32, 1, pf) != 1)
Alex Deymoa5cff222015-04-08 14:10:30 -0700317 err(1, "fwrite(%s)", patch_filename);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800318 if (fclose(pf))
319 err(1, "fclose");
320
321 /* Free the memory we used */
322 free(db);
323 free(eb);
324 free(I);
325 free(old);
326 free(new);
327
328 return 0;
329}