2 * Copyright 2003-2005 Colin Percival
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
44 #define MIN(x,y) (((x)<(y)) ? (x) : (y))
46 static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h)
48 off_t i,j,k,x,tmp,jj,kk;
51 for(k=start;k<start+len;k+=j) {
53 for(i=1;k+i<start+len;i++) {
59 tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
63 for(i=0;i<j;i++) V[I[k+i]]=k+j-1;
69 x=V[I[start+len/2]+h];
71 for(i=start;i<start+len;i++) {
73 if(V[I[i]+h]==x) kk++;
81 } else if(V[I[i]+h]==x) {
82 tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
85 tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
94 tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
99 if(jj>start) split(I,V,start,jj-start,h);
101 for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1;
102 if(jj==kk-1) I[jj]=-1;
104 if(start+len>kk) split(I,V,kk,start+len-kk,h);
107 static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize)
112 for(i=0;i<256;i++) buckets[i]=0;
113 for(i=0;i<oldsize;i++) buckets[old[i]]++;
114 for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
115 for(i=255;i>0;i--) buckets[i]=buckets[i-1];
118 for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i;
120 for(i=0;i<oldsize;i++) V[i]=buckets[old[i]];
122 for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
125 for(h=1;I[0]!=-(oldsize+1);h+=h) {
127 for(i=0;i<oldsize+1;) {
132 if(len) I[i-len]=-len;
139 if(len) I[i-len]=-len;
142 for(i=0;i<oldsize+1;i++) I[V[i]]=i;
145 static off_t matchlen(u_char *old,off_t oldsize,u_char *new,off_t newsize)
149 for(i=0;(i<oldsize)&&(i<newsize);i++)
150 if(old[i]!=new[i]) break;
155 static off_t search(off_t *I,u_char *old,off_t oldsize,
156 u_char *new,off_t newsize,off_t st,off_t en,off_t *pos)
161 x=matchlen(old+I[st],oldsize-I[st],new,newsize);
162 y=matchlen(old+I[en],oldsize-I[en],new,newsize);
174 if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
175 return search(I,old,oldsize,new,newsize,x,en,pos);
177 return search(I,old,oldsize,new,newsize,st,x,pos);
181 static void offtout(off_t x,u_char *buf)
185 if(x<0) y=-x; else y=x;
187 buf[0]=y%256;y-=buf[0];
188 y=y/256;buf[1]=y%256;y-=buf[1];
189 y=y/256;buf[2]=y%256;y-=buf[2];
190 y=y/256;buf[3]=y%256;y-=buf[3];
191 y=y/256;buf[4]=y%256;y-=buf[4];
192 y=y/256;buf[5]=y%256;y-=buf[5];
193 y=y/256;buf[6]=y%256;y-=buf[6];
194 y=y/256;buf[7]=y%256;
196 if(x<0) buf[7]|=0x80;
203 fprintf(stderr, "usage: bsdiff oldfile newfile patchfile\n");
207 int main(int argc,char *argv[])
211 off_t oldsize,newsize;
214 off_t lastscan,lastpos,lastoffset;
216 off_t s,Sf,lenf,Sb,lenb;
217 off_t overlap,Ss,lens;
230 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
231 that we never try to malloc(0) and get a NULL pointer */
232 if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) ||
233 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
234 ((old=malloc(oldsize+1))==NULL) ||
235 (lseek(fd,0,SEEK_SET)!=0) ||
236 (read(fd,old,oldsize)!=oldsize) ||
237 (close(fd)==-1)) err(1,"%s",argv[1]);
239 if(((I=malloc((oldsize+1)*sizeof(off_t)))==NULL) ||
240 ((V=malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL);
242 qsufsort(I,V,old,oldsize);
246 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
247 that we never try to malloc(0) and get a NULL pointer */
248 if(((fd=open(argv[2],O_RDONLY|O_BINARY,0))<0) ||
249 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
250 ((new=malloc(newsize+1))==NULL) ||
251 (lseek(fd,0,SEEK_SET)!=0) ||
252 (read(fd,new,newsize)!=newsize) ||
253 (close(fd)==-1)) err(1,"%s",argv[2]);
255 if(((db=malloc(newsize+1))==NULL) ||
256 ((eb=malloc(newsize+1))==NULL)) err(1,NULL);
260 /* Create the patch file */
261 if ((pf = fopen(argv[3], "wb")) == NULL)
262 err(1, "%s", argv[3]);
266 8 8 length of bzip2ed ctrl block
267 16 8 length of bzip2ed diff block
268 24 8 length of new file */
271 32 ?? Bzip2ed ctrl block
272 ?? ?? Bzip2ed diff block
273 ?? ?? Bzip2ed extra block */
274 memcpy(header,"BSDIFF40",8);
275 offtout(0, header + 8);
276 offtout(0, header + 16);
277 offtout(newsize, header + 24);
278 if (fwrite(header, 32, 1, pf) != 1)
279 err(1, "fwrite(%s)", argv[3]);
281 /* Compute the differences, writing ctrl as we go */
282 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
283 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
285 lastscan=0;lastpos=0;lastoffset=0;
286 while(scan<newsize) {
289 for(scsc=scan+=len;scan<newsize;scan++) {
290 len=search(I,old,oldsize,new+scan,newsize-scan,
293 for(;scsc<scan+len;scsc++)
294 if((scsc+lastoffset<oldsize) &&
295 (old[scsc+lastoffset] == new[scsc]))
298 if(((len==oldscore) && (len!=0)) ||
299 (len>oldscore+8)) break;
301 if((scan+lastoffset<oldsize) &&
302 (old[scan+lastoffset] == new[scan]))
306 if((len!=oldscore) || (scan==newsize)) {
308 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
309 if(old[lastpos+i]==new[lastscan+i]) s++;
311 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
317 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
318 if(old[pos-i]==new[scan-i]) s++;
319 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
323 if(lastscan+lenf>scan-lenb) {
324 overlap=(lastscan+lenf)-(scan-lenb);
326 for(i=0;i<overlap;i++) {
327 if(new[lastscan+lenf-overlap+i]==
328 old[lastpos+lenf-overlap+i]) s++;
329 if(new[scan-lenb+i]==
330 old[pos-lenb+i]) s--;
331 if(s>Ss) { Ss=s; lens=i+1; };
339 db[dblen+i]=new[lastscan+i]-old[lastpos+i];
340 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
341 eb[eblen+i]=new[lastscan+lenf+i];
344 eblen+=(scan-lenb)-(lastscan+lenf);
347 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
349 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
351 offtout((scan-lenb)-(lastscan+lenf),buf);
352 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
354 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
356 offtout((pos-lenb)-(lastpos+lenf),buf);
357 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
359 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
366 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
368 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
370 /* Compute size of compressed ctrl data */
371 if ((len = ftello(pf)) == -1)
373 offtout(len-32, header + 8);
375 /* Write compressed diff data */
376 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
377 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
378 BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
380 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
381 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
383 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
385 /* Compute size of compressed diff data */
386 if ((newsize = ftello(pf)) == -1)
388 offtout(newsize - len, header + 16);
390 /* Write compressed extra data */
391 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
392 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
393 BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
395 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
396 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
398 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
400 /* Seek to the beginning, write the header, and close the file */
401 if (fseeko(pf, 0, SEEK_SET))
403 if (fwrite(header, 32, 1, pf) != 1)
404 err(1, "fwrite(%s)", argv[3]);
408 /* Free the memory we used */