From 4f07ebc67e74629724c441352a58b12143383129 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 29 Dec 2004 22:08:48 +0000 Subject: [PATCH] * Integrated bsdiff/bspatch 4.2 (from http://www.daemonology.net/bsdiff/bsdiff-4.2.tar.gz) into the source tree. The license is a bit peculiar, but it does allow verbatim copying, which is what we do here (i.e., so don't make any changes to the sources). --- configure.ac | 1 + src/Makefile.am | 2 +- src/bsdiff-4.2/LICENSE | 121 ++++++++++++ src/bsdiff-4.2/Makefile.am | 7 + src/bsdiff-4.2/Makefile.old | 18 ++ src/bsdiff-4.2/bsdiff.1 | 36 ++++ src/bsdiff-4.2/bsdiff.c | 385 ++++++++++++++++++++++++++++++++++++ src/bsdiff-4.2/bspatch.1 | 32 +++ src/bsdiff-4.2/bspatch.c | 216 ++++++++++++++++++++ 9 files changed, 817 insertions(+), 1 deletion(-) create mode 100644 src/bsdiff-4.2/LICENSE create mode 100644 src/bsdiff-4.2/Makefile.am create mode 100644 src/bsdiff-4.2/Makefile.old create mode 100644 src/bsdiff-4.2/bsdiff.1 create mode 100644 src/bsdiff-4.2/bsdiff.c create mode 100644 src/bsdiff-4.2/bspatch.1 create mode 100644 src/bsdiff-4.2/bspatch.c diff --git a/configure.ac b/configure.ac index d7574bba..67e95493 100644 --- a/configure.ac +++ b/configure.ac @@ -189,6 +189,7 @@ AC_CONFIG_FILES([Makefile src/nix-instantiate/Makefile src/nix-env/Makefile src/log2xml/Makefile + src/bsdiff-4.2/Makefile scripts/Makefile corepkgs/Makefile corepkgs/fetchurl/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 089f5322..7753a77d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ SUBDIRS = bin2c boost libutil libstore libmain nix-store nix-hash \ - libexpr nix-instantiate nix-env log2xml + libexpr nix-instantiate nix-env log2xml bsdiff-4.2 EXTRA_DIST = aterm-helper.pl diff --git a/src/bsdiff-4.2/LICENSE b/src/bsdiff-4.2/LICENSE new file mode 100644 index 00000000..c146b5b3 --- /dev/null +++ b/src/bsdiff-4.2/LICENSE @@ -0,0 +1,121 @@ +BSD Protection License +February 2002 + +Preamble +-------- + +The Berkeley Software Distribution ("BSD") license has proven very effective +over the years at allowing for a wide spread of work throughout both +commercial and non-commercial products. For programmers whose primary +intention is to improve the general quality of available software, it is +arguable that there is no better license than the BSD license, as it +permits improvements to be used wherever they will help, without idealogical +or metallic constraint. + +This is of particular value to those who produce reference implementations +of proposed standards: The case of TCP/IP clearly illustrates that freely +and universally available implementations leads the rapid acceptance of +standards -- often even being used instead of a de jure standard (eg, OSI +network models). + +With the rapid proliferation of software licensed under the GNU General +Public License, however, the continued success of this role is called into +question. Given that the inclusion of a few lines of "GPL-tainted" work +into a larger body of work will result in restricted distribution -- and +given that further work will likely build upon the "tainted" portions, +making them difficult to remove at a future date -- there are inevitable +circumstances where authors would, in order to protect their goal of +providing for the widespread usage of their work, wish to guard against +such "GPL-taint". + +In addition, one can imagine that companies which operate by producing and +selling (possibly closed-source) code would wish to protect themselves +against the rise of a GPL-licensed competitor. While under existing +licenses this would mean not releasing their code under any form of open +license, if a license existed under which they could incorporate any +improvements back into their own (commercial) products then they might be +far more willing to provide for non-closed distribution. + +For the above reasons, we put forth this "BSD Protection License": A +license designed to retain the freedom granted by the BSD license to use +licensed works in a wide variety of settings, both non-commercial and +commercial, while protecting the work from having future contributors +restrict that freedom. + +The precise terms and conditions for copying, distribution, and +modification follow. + +BSD PROTECTION LICENSE +TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION, AND MODIFICATION +---------------------------------------------------------------- + +0. Definitions. + a) "Program", below, refers to any program or work distributed under + the terms of this license. + b) A "work based on the Program", below, refers to either the Program + or any derivative work under copyright law. + c) "Modification", below, refers to the act of creating derivative works. + d) "You", below, refers to each licensee. + +1. Scope. + This license governs the copying, distribution, and modification of the + Program. Other activities are outside the scope of this license; The + act of running the Program is not restricted, and the output from the + Program is covered only if its contents constitute a work based on the + Program. + +2. Verbatim copies. + You may copy and distribute verbatim copies of the Program as you + receive it, in any medium, provided that you conspicuously and + appropriately publish on each copy an appropriate copyright notice; keep + intact all the notices that refer to this License and to the absence of + any warranty; and give any other recipients of the Program a copy of this + License along with the Program. + +3. Modification and redistribution under closed license. + You may modify your copy or copies of the Program, and distribute + the resulting derivative works, provided that you meet the + following conditions: + a) The copyright notice and disclaimer on the Program must be reproduced + and included in the source code, documentation, and/or other materials + provided in a manner in which such notices are normally distributed. + b) The derivative work must be clearly identified as such, in order that + it may not be confused with the original work. + c) The license under which the derivative work is distributed must + expressly prohibit the distribution of further derivative works. + +4. Modification and redistribution under open license. + You may modify your copy or copies of the Program, and distribute + the resulting derivative works, provided that you meet the + following conditions: + a) The copyright notice and disclaimer on the Program must be reproduced + and included in the source code, documentation, and/or other materials + provided in a manner in which such notices are normally distributed. + b) You must clearly indicate the nature and date of any changes made + to the Program. The full details need not necessarily be included in + the individual modified files, provided that each modified file is + clearly marked as such and instructions are included on where the + full details of the modifications may be found. + c) You must cause any work that you distribute or publish, that in whole + or in part contains or is derived from the Program or any part + thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + +5. Implied acceptance. + You may not copy or distribute the Program or any derivative works except + as expressly provided under this license. Consequently, any such action + will be taken as implied acceptance of the terms of this license. + +6. NO WARRANTY. + THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR + REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + TORT, EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE + POSSIBILITY OF SUCH DAMAGES. diff --git a/src/bsdiff-4.2/Makefile.am b/src/bsdiff-4.2/Makefile.am new file mode 100644 index 00000000..70f2ead7 --- /dev/null +++ b/src/bsdiff-4.2/Makefile.am @@ -0,0 +1,7 @@ +libexec_PROGRAMS = bsdiff bspatch + +bsdiff_SOURCES = bsdiff.c + +bspatch_SOURCES = bspatch.c + +AM_CFLAGS = -O3 -DBZIP2=\"$(bzip2)\" diff --git a/src/bsdiff-4.2/Makefile.old b/src/bsdiff-4.2/Makefile.old new file mode 100644 index 00000000..ce5dd344 --- /dev/null +++ b/src/bsdiff-4.2/Makefile.old @@ -0,0 +1,18 @@ +CFLAGS += -O3 +.ifdef BZIP2 +CFLAGS += -DBZIP2=\"${BZIP2}\" +.endif + +PREFIX ?= /usr/local +INSTALL_PROGRAM ?= ${INSTALL} -c -s -m 555 +INSTALL_MAN ?= ${INSTALL} -c -m 444 + +all: bsdiff bspatch +bsdiff: bsdiff.c +bspatch: bspatch.c + +install: + ${INSTALL_PROGRAM} bsdiff bspatch ${PREFIX}/bin +.ifndef WITHOUT_MAN + ${INSTALL_MAN} bsdiff.1 bspatch.1 ${PREFIX}/man/man1 +.endif diff --git a/src/bsdiff-4.2/bsdiff.1 b/src/bsdiff-4.2/bsdiff.1 new file mode 100644 index 00000000..8d3926d3 --- /dev/null +++ b/src/bsdiff-4.2/bsdiff.1 @@ -0,0 +1,36 @@ +.Dd May 18, 2003 +.Dt BSDIFF 1 +.Os FreeBSD +.Sh NAME +.Nm bsdiff +.Nd generate a patch between two binary files +.Sh SYNOPSIS +.Nm +.Ao Ar oldfile Ac Ao Ar newfile Ac Ao Ar patchfile Ac +.Sh DESCRIPTION +.Nm +compares +.Ao Ar oldfile Ac +to +.Ao Ar newfile Ac +and writes to +.Ao Ar patchfile Ac +a binary patch suitable for use by bspatch(1). +When +.Ao Ar oldfile Ac +and +.Ao Ar newfile Ac +are two versions of an executable program, the +patches produced are on average a factor of five smaller +than those produced by any other binary patch tool known +to the author. +.Pp +.Nm +uses memory equal to 17 times the size of +.Ao Ar oldfile Ac , +and requires +an absolute minimum working set size of 8 times the size of oldfile. +.Sh SEE ALSO +.Xr bspatch 1 +.Sh AUTHORS +.An Colin Percival Aq cperciva@daemonology.net diff --git a/src/bsdiff-4.2/bsdiff.c b/src/bsdiff-4.2/bsdiff.c new file mode 100644 index 00000000..f47226c1 --- /dev/null +++ b/src/bsdiff-4.2/bsdiff.c @@ -0,0 +1,385 @@ +/* + bsdiff.c -- Binary patch generator. + + Copyright 2003 Colin Percival + + For the terms under which this work may be distributed, please see + the adjoining file "LICENSE". +*/ + +#ifndef BZIP2 +#define BZIP2 "/usr/bin/bzip2" +#endif + +#include +#include +#include +#include +#include +#include +#include + +#define MIN(x,y) (((x)<(y)) ? (x) : (y)) + +void split(off_t *I,off_t *V,off_t start,off_t len,off_t h) +{ + off_t i,j,k,x,tmp,jj,kk; + + if(len<16) { + for(k=start;kstart) split(I,V,start,jj-start,h); + + for(i=0;ikk) split(I,V,kk,start+len-kk,h); +} + +void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize) +{ + off_t buckets[256]; + off_t i,h,len; + + for(i=0;i<256;i++) buckets[i]=0; + for(i=0;i0;i--) buckets[i]=buckets[i-1]; + buckets[0]=0; + + for(i=0;iy) { + *pos=I[st]; + return x; + } else { + *pos=I[en]; + return y; + } + }; + + x=st+(en-st)/2; + if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) { + return search(I,old,oldsize,new,newsize,x,en,pos); + } else { + return search(I,old,oldsize,new,newsize,st,x,pos); + }; +} + +void offtout(off_t x,u_char *buf) +{ + off_t y; + + if(x<0) y=-x; else y=x; + + buf[0]=y%256;y-=buf[0]; + y=y/256;buf[1]=y%256;y-=buf[1]; + y=y/256;buf[2]=y%256;y-=buf[2]; + y=y/256;buf[3]=y%256;y-=buf[3]; + y=y/256;buf[4]=y%256;y-=buf[4]; + y=y/256;buf[5]=y%256;y-=buf[5]; + y=y/256;buf[6]=y%256;y-=buf[6]; + y=y/256;buf[7]=y%256; + + if(x<0) buf[7]|=0x80; +} + +int main(int argc,char *argv[]) +{ + int fd,p[2]; + pid_t pid; + u_char *old,*new; + off_t oldsize,newsize; + off_t *I,*V; + + off_t scan,pos,len; + off_t lastscan,lastpos,lastoffset; + off_t oldscore,scsc; + + off_t s,Sf,lenf,Sb,lenb; + off_t overlap,Ss,lens; + off_t i; + + off_t dblen,eblen; + u_char *db,*eb; + + u_char buf[8]; + u_char header[32]; + + if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); + + /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure + that we never try to malloc(0) and get a NULL pointer */ + if(((fd=open(argv[1],O_RDONLY,0))<0) || + ((oldsize=lseek(fd,0,SEEK_END))==-1) || + ((old=malloc(oldsize+1))==NULL) || + (lseek(fd,0,SEEK_SET)!=0) || + (read(fd,old,oldsize)!=oldsize) || + (close(fd)==-1)) err(1,"%s",argv[1]); + + if(((I=malloc((oldsize+1)*sizeof(off_t)))==NULL) || + ((V=malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL); + + qsufsort(I,V,old,oldsize); + + free(V); + + /* Allocate newsize+1 bytes instead of newsize bytes to ensure + that we never try to malloc(0) and get a NULL pointer */ + if(((fd=open(argv[2],O_RDONLY,0))<0) || + ((newsize=lseek(fd,0,SEEK_END))==-1) || + ((new=malloc(newsize+1))==NULL) || + (lseek(fd,0,SEEK_SET)!=0) || + (read(fd,new,newsize)!=newsize) || + (close(fd)==-1)) err(1,"%s",argv[2]); + + if(((db=malloc(newsize+1))==NULL) || + ((eb=malloc(newsize+1))==NULL)) err(1,NULL); + dblen=0; + eblen=0; + + if((fd=open(argv[3],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) + err(1,"%s",argv[3]); + + /* Header is + 0 8 "BSDIFF40" + 8 8 length of bzip2ed ctrl block + 16 8 length of bzip2ed diff block + 24 8 length of new file */ + /* File is + 0 32 Header + 32 ?? Bzip2ed ctrl block + ?? ?? Bzip2ed diff block + ?? ?? Bzip2ed extra block */ + memcpy(header,"BSDIFF40",8); + memset(header+8,0,24); + if(write(fd,header,32)!=32) err(1,"%s",argv[3]); + + if((pipe(p)==-1) || ((pid=fork())==-1)) err(1,NULL); + if(pid==0) { + if((close(0)==-1) || (close(1)==-1) || (dup2(fd,1)==-1) || + (dup2(p[0],0)==-1) || (close(fd)==-1) || + (close(p[0])==-1) || (close(p[1])==-1)) + err(1,NULL); + execl(BZIP2,BZIP2,"-zc",NULL); + err(1,"%s",BZIP2); + }; + if(close(p[0])==-1) err(1,NULL); + + scan=0;len=0; + lastscan=0;lastpos=0;lastoffset=0; + while(scanoldscore+8)) break; + + if((scan+lastoffsetSf*2-lenf) { Sf=s; lenf=i; }; + }; + + lenb=0; + if(scan=lastscan+i)&&(pos>=i);i++) { + if(old[pos-i]==new[scan-i]) s++; + if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; + }; + }; + + if(lastscan+lenf>scan-lenb) { + overlap=(lastscan+lenf)-(scan-lenb); + s=0;Ss=0;lens=0; + for(i=0;iSs) { Ss=s; lens=i+1; }; + }; + + lenf+=lens-overlap; + lenb-=lens; + }; + + for(i=0;i +#include +#include +#include +#include +#include + +ssize_t loopread(int d,void *buf,size_t nbytes) +{ + ssize_t ptr,lenread; + + for(ptr=0;ptrnewsize) errx(1,"Corrupt patch\n"); + if((lenread=loopread(diffpipe,new+newpos,ctrl[0]))<0) + err(1,NULL); + if(lenread!=ctrl[0]) errx(1,"Corrupt patch\n"); + for(i=0;i=0) && (oldpos+inewsize) errx(1,"Corrupt patch\n"); + if((lenread=loopread(extrapipe,new+newpos,ctrl[1]))<0) + err(1,NULL); + if(lenread!=ctrl[1]) errx(1,"Corrupt patch\n"); + + newpos+=ctrl[1]; + oldpos+=ctrl[2]; + }; + }; + + if(loopread(ctrlpipe,buf,1)!=0) errx(1,"Corrupt patch\n"); + if(loopread(diffpipe,buf,1)!=0) errx(1,"Corrupt patch\n"); + if(version==2) + if(loopread(extrapipe,buf,1)!=0) errx(1,"Corrupt patch\n"); + + if(close(ctrlpipe) || close(diffpipe) || + ((version==2) && close(extrapipe))) + err(1,NULL); + for(i=0;i<(version+1)*2;i++) waitpid(pids[i],NULL,0); + + if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) || + (write(fd,new,newsize)!=newsize) || (close(fd)==-1)) + err(1,"%s",argv[2]); + + free(new); + free(old); + + return 0; +}