SASC
DVBSoftCAM

bsdiff.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright 2003-2005 Colin Percival
00003  * All rights reserved
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted providing that the following conditions 
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00016  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00017  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00018  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00019  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00020  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00021  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00022  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00023  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00024  * POSSIBILITY OF SUCH DAMAGE.
00025  */
00026 
00027 #if 0
00028 __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 cperciva Exp $");
00029 #endif
00030 
00031 #include <sys/types.h>
00032 
00033 #include <bzlib.h>
00034 #include <err.h>
00035 #include <fcntl.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 
00041 #define MIN(x,y) (((x)<(y)) ? (x) : (y))
00042 
00043 static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h)
00044 {
00045         off_t i,j,k,x,tmp,jj,kk;
00046 
00047         if(len<16) {
00048                 for(k=start;k<start+len;k+=j) {
00049                         j=1;x=V[I[k]+h];
00050                         for(i=1;k+i<start+len;i++) {
00051                                 if(V[I[k+i]+h]<x) {
00052                                         x=V[I[k+i]+h];
00053                                         j=0;
00054                                 };
00055                                 if(V[I[k+i]+h]==x) {
00056                                         tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
00057                                         j++;
00058                                 };
00059                         };
00060                         for(i=0;i<j;i++) V[I[k+i]]=k+j-1;
00061                         if(j==1) I[k]=-1;
00062                 };
00063                 return;
00064         };
00065 
00066         x=V[I[start+len/2]+h];
00067         jj=0;kk=0;
00068         for(i=start;i<start+len;i++) {
00069                 if(V[I[i]+h]<x) jj++;
00070                 if(V[I[i]+h]==x) kk++;
00071         };
00072         jj+=start;kk+=jj;
00073 
00074         i=start;j=0;k=0;
00075         while(i<jj) {
00076                 if(V[I[i]+h]<x) {
00077                         i++;
00078                 } else if(V[I[i]+h]==x) {
00079                         tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
00080                         j++;
00081                 } else {
00082                         tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
00083                         k++;
00084                 };
00085         };
00086 
00087         while(jj+j<kk) {
00088                 if(V[I[jj+j]+h]==x) {
00089                         j++;
00090                 } else {
00091                         tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
00092                         k++;
00093                 };
00094         };
00095 
00096         if(jj>start) split(I,V,start,jj-start,h);
00097 
00098         for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1;
00099         if(jj==kk-1) I[jj]=-1;
00100 
00101         if(start+len>kk) split(I,V,kk,start+len-kk,h);
00102 }
00103 
00104 static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize)
00105 {
00106         off_t buckets[256];
00107         off_t i,h,len;
00108 
00109         for(i=0;i<256;i++) buckets[i]=0;
00110         for(i=0;i<oldsize;i++) buckets[old[i]]++;
00111         for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
00112         for(i=255;i>0;i--) buckets[i]=buckets[i-1];
00113         buckets[0]=0;
00114 
00115         for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i;
00116         I[0]=oldsize;
00117         for(i=0;i<oldsize;i++) V[i]=buckets[old[i]];
00118         V[oldsize]=0;
00119         for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
00120         I[0]=-1;
00121 
00122         for(h=1;I[0]!=-(oldsize+1);h+=h) {
00123                 len=0;
00124                 for(i=0;i<oldsize+1;) {
00125                         if(I[i]<0) {
00126                                 len-=I[i];
00127                                 i-=I[i];
00128                         } else {
00129                                 if(len) I[i-len]=-len;
00130                                 len=V[I[i]]+1-i;
00131                                 split(I,V,i,len,h);
00132                                 i+=len;
00133                                 len=0;
00134                         };
00135                 };
00136                 if(len) I[i-len]=-len;
00137         };
00138 
00139         for(i=0;i<oldsize+1;i++) I[V[i]]=i;
00140 }
00141 
00142 static off_t matchlen(u_char *old,off_t oldsize,u_char *new,off_t newsize)
00143 {
00144         off_t i;
00145 
00146         for(i=0;(i<oldsize)&&(i<newsize);i++)
00147                 if(old[i]!=new[i]) break;
00148 
00149         return i;
00150 }
00151 
00152 static off_t search(off_t *I,u_char *old,off_t oldsize,
00153                 u_char *new,off_t newsize,off_t st,off_t en,off_t *pos)
00154 {
00155         off_t x,y;
00156 
00157         if(en-st<2) {
00158                 x=matchlen(old+I[st],oldsize-I[st],new,newsize);
00159                 y=matchlen(old+I[en],oldsize-I[en],new,newsize);
00160 
00161                 if(x>y) {
00162                         *pos=I[st];
00163                         return x;
00164                 } else {
00165                         *pos=I[en];
00166                         return y;
00167                 }
00168         };
00169 
00170         x=st+(en-st)/2;
00171         if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
00172                 return search(I,old,oldsize,new,newsize,x,en,pos);
00173         } else {
00174                 return search(I,old,oldsize,new,newsize,st,x,pos);
00175         };
00176 }
00177 
00178 static void offtout(off_t x,u_char *buf)
00179 {
00180         off_t y;
00181 
00182         if(x<0) y=-x; else y=x;
00183 
00184                 buf[0]=y%256;y-=buf[0];
00185         y=y/256;buf[1]=y%256;y-=buf[1];
00186         y=y/256;buf[2]=y%256;y-=buf[2];
00187         y=y/256;buf[3]=y%256;y-=buf[3];
00188         y=y/256;buf[4]=y%256;y-=buf[4];
00189         y=y/256;buf[5]=y%256;y-=buf[5];
00190         y=y/256;buf[6]=y%256;y-=buf[6];
00191         y=y/256;buf[7]=y%256;
00192 
00193         if(x<0) buf[7]|=0x80;
00194 }
00195 
00196 int main(int argc,char *argv[])
00197 {
00198         int fd;
00199         u_char *old,*new;
00200         off_t oldsize,newsize;
00201         off_t *I,*V;
00202         off_t scan,pos,len;
00203         off_t lastscan,lastpos,lastoffset;
00204         off_t oldscore,scsc;
00205         off_t s,Sf,lenf,Sb,lenb;
00206         off_t overlap,Ss,lens;
00207         off_t i;
00208         off_t dblen,eblen;
00209         u_char *db,*eb;
00210         u_char buf[8];
00211         u_char header[32];
00212         FILE * pf;
00213         BZFILE * pfbz2;
00214         int bz2err;
00215 
00216         if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
00217 
00218         /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
00219                 that we never try to malloc(0) and get a NULL pointer */
00220         if(((fd=open(argv[1],O_RDONLY,0))<0) ||
00221                 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
00222                 ((old=malloc(oldsize+1))==NULL) ||
00223                 (lseek(fd,0,SEEK_SET)!=0) ||
00224                 (read(fd,old,oldsize)!=oldsize) ||
00225                 (close(fd)==-1)) err(1,"%s",argv[1]);
00226 
00227         if(((I=malloc((oldsize+1)*sizeof(off_t)))==NULL) ||
00228                 ((V=malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL);
00229 
00230         qsufsort(I,V,old,oldsize);
00231 
00232         free(V);
00233 
00234         /* Allocate newsize+1 bytes instead of newsize bytes to ensure
00235                 that we never try to malloc(0) and get a NULL pointer */
00236         if(((fd=open(argv[2],O_RDONLY,0))<0) ||
00237                 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
00238                 ((new=malloc(newsize+1))==NULL) ||
00239                 (lseek(fd,0,SEEK_SET)!=0) ||
00240                 (read(fd,new,newsize)!=newsize) ||
00241                 (close(fd)==-1)) err(1,"%s",argv[2]);
00242 
00243         if(((db=malloc(newsize+1))==NULL) ||
00244                 ((eb=malloc(newsize+1))==NULL)) err(1,NULL);
00245         dblen=0;
00246         eblen=0;
00247 
00248         /* Create the patch file */
00249         if ((pf = fopen(argv[3], "w")) == NULL)
00250                 err(1, "%s", argv[3]);
00251 
00252         /* Header is
00253                 0       8        "BSDIFF40"
00254                 8       8       length of bzip2ed ctrl block
00255                 16      8       length of bzip2ed diff block
00256                 24      8       length of new file */
00257         /* File is
00258                 0       32      Header
00259                 32      ??      Bzip2ed ctrl block
00260                 ??      ??      Bzip2ed diff block
00261                 ??      ??      Bzip2ed extra block */
00262         memcpy(header,"BSDIFF40",8);
00263         offtout(0, header + 8);
00264         offtout(0, header + 16);
00265         offtout(newsize, header + 24);
00266         if (fwrite(header, 32, 1, pf) != 1)
00267                 err(1, "fwrite(%s)", argv[3]);
00268 
00269         /* Compute the differences, writing ctrl as we go */
00270         if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
00271                 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
00272         scan=0;len=0;
00273         lastscan=0;lastpos=0;lastoffset=0;
00274         while(scan<newsize) {
00275                 oldscore=0;
00276 
00277                 for(scsc=scan+=len;scan<newsize;scan++) {
00278                         len=search(I,old,oldsize,new+scan,newsize-scan,
00279                                         0,oldsize,&pos);
00280 
00281                         for(;scsc<scan+len;scsc++)
00282                         if((scsc+lastoffset<oldsize) &&
00283                                 (old[scsc+lastoffset] == new[scsc]))
00284                                 oldscore++;
00285 
00286                         if(((len==oldscore) && (len!=0)) || 
00287                                 (len>oldscore+8)) break;
00288 
00289                         if((scan+lastoffset<oldsize) &&
00290                                 (old[scan+lastoffset] == new[scan]))
00291                                 oldscore--;
00292                 };
00293 
00294                 if((len!=oldscore) || (scan==newsize)) {
00295                         s=0;Sf=0;lenf=0;
00296                         for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
00297                                 if(old[lastpos+i]==new[lastscan+i]) s++;
00298                                 i++;
00299                                 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
00300                         };
00301 
00302                         lenb=0;
00303                         if(scan<newsize) {
00304                                 s=0;Sb=0;
00305                                 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
00306                                         if(old[pos-i]==new[scan-i]) s++;
00307                                         if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
00308                                 };
00309                         };
00310 
00311                         if(lastscan+lenf>scan-lenb) {
00312                                 overlap=(lastscan+lenf)-(scan-lenb);
00313                                 s=0;Ss=0;lens=0;
00314                                 for(i=0;i<overlap;i++) {
00315                                         if(new[lastscan+lenf-overlap+i]==
00316                                            old[lastpos+lenf-overlap+i]) s++;
00317                                         if(new[scan-lenb+i]==
00318                                            old[pos-lenb+i]) s--;
00319                                         if(s>Ss) { Ss=s; lens=i+1; };
00320                                 };
00321 
00322                                 lenf+=lens-overlap;
00323                                 lenb-=lens;
00324                         };
00325 
00326                         for(i=0;i<lenf;i++)
00327                                 db[dblen+i]=new[lastscan+i]-old[lastpos+i];
00328                         for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
00329                                 eb[eblen+i]=new[lastscan+lenf+i];
00330 
00331                         dblen+=lenf;
00332                         eblen+=(scan-lenb)-(lastscan+lenf);
00333 
00334                         offtout(lenf,buf);
00335                         BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
00336                         if (bz2err != BZ_OK)
00337                                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
00338 
00339                         offtout((scan-lenb)-(lastscan+lenf),buf);
00340                         BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
00341                         if (bz2err != BZ_OK)
00342                                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
00343 
00344                         offtout((pos-lenb)-(lastpos+lenf),buf);
00345                         BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
00346                         if (bz2err != BZ_OK)
00347                                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
00348 
00349                         lastscan=scan-lenb;
00350                         lastpos=pos-lenb;
00351                         lastoffset=pos-scan;
00352                 };
00353         };
00354         BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
00355         if (bz2err != BZ_OK)
00356                 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
00357 
00358         /* Compute size of compressed ctrl data */
00359         if ((len = ftello(pf)) == -1)
00360                 err(1, "ftello");
00361         offtout(len-32, header + 8);
00362 
00363         /* Write compressed diff data */
00364         if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
00365                 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
00366         BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
00367         if (bz2err != BZ_OK)
00368                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
00369         BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
00370         if (bz2err != BZ_OK)
00371                 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
00372 
00373         /* Compute size of compressed diff data */
00374         if ((newsize = ftello(pf)) == -1)
00375                 err(1, "ftello");
00376         offtout(newsize - len, header + 16);
00377 
00378         /* Write compressed extra data */
00379         if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
00380                 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
00381         BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
00382         if (bz2err != BZ_OK)
00383                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
00384         BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
00385         if (bz2err != BZ_OK)
00386                 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
00387 
00388         /* Seek to the beginning, write the header, and close the file */
00389         if (fseeko(pf, 0, SEEK_SET))
00390                 err(1, "fseeko");
00391         if (fwrite(header, 32, 1, pf) != 1)
00392                 err(1, "fwrite(%s)", argv[3]);
00393         if (fclose(pf))
00394                 err(1, "fclose");
00395 
00396         /* Free the memory we used */
00397         free(db);
00398         free(eb);
00399         free(I);
00400         free(old);
00401         free(new);
00402 
00403         return 0;
00404 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines