blob: 11d955c0712e32962e7719901155e96942332d0b [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
6 * modification, are permitted providing that the following conditions
7 * 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
61static off_t search(off_t *I,u_char *old,off_t oldsize,
62 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
105int main(int argc,char *argv[])
106{
107 int fd;
108 u_char *old,*new;
109 off_t oldsize,newsize;
Thieu Lec2c68a72011-05-17 16:43:07 -0700110 saidx_t *I;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800111 off_t scan,pos,len;
112 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
125 if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
126
127 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
128 that we never try to malloc(0) and get a NULL pointer */
129 if(((fd=open(argv[1],O_RDONLY,0))<0) ||
130 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
131 ((old=malloc(oldsize+1))==NULL) ||
132 (lseek(fd,0,SEEK_SET)!=0) ||
133 (read(fd,old,oldsize)!=oldsize) ||
134 (close(fd)==-1)) err(1,"%s",argv[1]);
135
Thieu Lec2c68a72011-05-17 16:43:07 -0700136 if(((I=malloc((oldsize+1)*sizeof(saidx_t)))==NULL)) err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800137
Thieu Lec2c68a72011-05-17 16:43:07 -0700138 if(divsufsort(old, I, oldsize)) err(1, "divsufsort");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800139
140 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
141 that we never try to malloc(0) and get a NULL pointer */
142 if(((fd=open(argv[2],O_RDONLY,0))<0) ||
143 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
144 ((new=malloc(newsize+1))==NULL) ||
145 (lseek(fd,0,SEEK_SET)!=0) ||
146 (read(fd,new,newsize)!=newsize) ||
147 (close(fd)==-1)) err(1,"%s",argv[2]);
148
149 if(((db=malloc(newsize+1))==NULL) ||
150 ((eb=malloc(newsize+1))==NULL)) err(1,NULL);
151 dblen=0;
152 eblen=0;
153
154 /* Create the patch file */
155 if ((pf = fopen(argv[3], "w")) == NULL)
156 err(1, "%s", argv[3]);
157
158 /* Header is
159 0 8 "BSDIFF40"
160 8 8 length of bzip2ed ctrl block
161 16 8 length of bzip2ed diff block
162 24 8 length of new file */
163 /* File is
164 0 32 Header
165 32 ?? Bzip2ed ctrl block
166 ?? ?? Bzip2ed diff block
167 ?? ?? Bzip2ed extra block */
168 memcpy(header,"BSDIFF40",8);
169 offtout(0, header + 8);
170 offtout(0, header + 16);
171 offtout(newsize, header + 24);
172 if (fwrite(header, 32, 1, pf) != 1)
173 err(1, "fwrite(%s)", argv[3]);
174
175 /* Compute the differences, writing ctrl as we go */
176 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
177 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
178 scan=0;len=0;
179 lastscan=0;lastpos=0;lastoffset=0;
180 while(scan<newsize) {
181 oldscore=0;
182
Thieu Le1f402922011-06-14 15:30:25 -0700183 /* If we come across a large block of data that only differs
184 * by less than 8 bytes, this loop will take a long time to
185 * go past that block of data. We need to track the number of
186 * times we're stuck in the block and break out of it. */
187 int num_less_than_eight = 0;
188 off_t prev_len, prev_oldscore, prev_pos;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800189 for(scsc=scan+=len;scan<newsize;scan++) {
Thieu Le1f402922011-06-14 15:30:25 -0700190 prev_len=len;
191 prev_oldscore=oldscore;
192 prev_pos=pos;
193
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800194 len=search(I,old,oldsize,new+scan,newsize-scan,
195 0,oldsize,&pos);
196
197 for(;scsc<scan+len;scsc++)
198 if((scsc+lastoffset<oldsize) &&
199 (old[scsc+lastoffset] == new[scsc]))
200 oldscore++;
201
202 if(((len==oldscore) && (len!=0)) ||
203 (len>oldscore+8)) break;
204
205 if((scan+lastoffset<oldsize) &&
206 (old[scan+lastoffset] == new[scan]))
207 oldscore--;
Thieu Le1f402922011-06-14 15:30:25 -0700208
Thieu Led1728202012-03-28 17:12:42 -0700209 const off_t fuzz = 8;
210 if (prev_len-fuzz<=len && len<=prev_len &&
211 prev_oldscore-fuzz<=oldscore &&
212 oldscore<=prev_oldscore &&
213 prev_pos<=pos && pos <=prev_pos+fuzz &&
214 oldscore<=len && len<=oldscore+fuzz)
Thieu Le1f402922011-06-14 15:30:25 -0700215 ++num_less_than_eight;
216 else
217 num_less_than_eight=0;
218 if (num_less_than_eight > 100) break;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800219 };
220
221 if((len!=oldscore) || (scan==newsize)) {
222 s=0;Sf=0;lenf=0;
223 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
224 if(old[lastpos+i]==new[lastscan+i]) s++;
225 i++;
226 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
227 };
228
229 lenb=0;
230 if(scan<newsize) {
231 s=0;Sb=0;
232 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
233 if(old[pos-i]==new[scan-i]) s++;
234 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
235 };
236 };
237
238 if(lastscan+lenf>scan-lenb) {
239 overlap=(lastscan+lenf)-(scan-lenb);
240 s=0;Ss=0;lens=0;
241 for(i=0;i<overlap;i++) {
242 if(new[lastscan+lenf-overlap+i]==
243 old[lastpos+lenf-overlap+i]) s++;
244 if(new[scan-lenb+i]==
245 old[pos-lenb+i]) s--;
246 if(s>Ss) { Ss=s; lens=i+1; };
247 };
248
249 lenf+=lens-overlap;
250 lenb-=lens;
251 };
252
253 for(i=0;i<lenf;i++)
254 db[dblen+i]=new[lastscan+i]-old[lastpos+i];
255 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
256 eb[eblen+i]=new[lastscan+lenf+i];
257
258 dblen+=lenf;
259 eblen+=(scan-lenb)-(lastscan+lenf);
260
261 offtout(lenf,buf);
262 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
263 if (bz2err != BZ_OK)
264 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
265
266 offtout((scan-lenb)-(lastscan+lenf),buf);
267 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
268 if (bz2err != BZ_OK)
269 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
270
271 offtout((pos-lenb)-(lastpos+lenf),buf);
272 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
273 if (bz2err != BZ_OK)
274 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
275
276 lastscan=scan-lenb;
277 lastpos=pos-lenb;
278 lastoffset=pos-scan;
279 };
280 };
281 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
282 if (bz2err != BZ_OK)
283 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
284
285 /* Compute size of compressed ctrl data */
286 if ((len = ftello(pf)) == -1)
287 err(1, "ftello");
288 offtout(len-32, header + 8);
289
290 /* Write compressed diff data */
291 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
292 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
293 BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
294 if (bz2err != BZ_OK)
295 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
296 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
297 if (bz2err != BZ_OK)
298 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
299
300 /* Compute size of compressed diff data */
301 if ((newsize = ftello(pf)) == -1)
302 err(1, "ftello");
303 offtout(newsize - len, header + 16);
304
305 /* Write compressed extra data */
306 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
307 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
308 BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
309 if (bz2err != BZ_OK)
310 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
311 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
312 if (bz2err != BZ_OK)
313 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
314
315 /* Seek to the beginning, write the header, and close the file */
316 if (fseeko(pf, 0, SEEK_SET))
317 err(1, "fseeko");
318 if (fwrite(header, 32, 1, pf) != 1)
319 err(1, "fwrite(%s)", argv[3]);
320 if (fclose(pf))
321 err(1, "fclose");
322
323 /* Free the memory we used */
324 free(db);
325 free(eb);
326 free(I);
327 free(old);
328 free(new);
329
330 return 0;
331}