2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2003-2005 Colin Percival
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted providing that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
49 #include "divsufsort64.h"
50 #define saidx_t saidx64_t
51 #define divsufsort divsufsort64
53 #define MIN(x,y) (((x)<(y)) ? (x) : (y))
55 static off_t matchlen(u_char *old,off_t oldsize,u_char *new,off_t newsize)
59 for(i=0;(i<oldsize)&&(i<newsize);i++)
60 if(old[i]!=new[i]) break;
65 static off_t search(off_t *I,u_char *old,off_t oldsize,
66 u_char *new,off_t newsize,off_t st,off_t en,off_t *pos)
71 x=matchlen(old+I[st],oldsize-I[st],new,newsize);
72 y=matchlen(old+I[en],oldsize-I[en],new,newsize);
84 if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
85 return search(I,old,oldsize,new,newsize,x,en,pos);
87 return search(I,old,oldsize,new,newsize,st,x,pos);
91 static void offtout(off_t x,u_char *buf)
95 if(x<0) y=-x; else y=x;
97 buf[0]=y%256;y-=buf[0];
98 y=y/256;buf[1]=y%256;y-=buf[1];
99 y=y/256;buf[2]=y%256;y-=buf[2];
100 y=y/256;buf[3]=y%256;y-=buf[3];
101 y=y/256;buf[4]=y%256;y-=buf[4];
102 y=y/256;buf[5]=y%256;y-=buf[5];
103 y=y/256;buf[6]=y%256;y-=buf[6];
104 y=y/256;buf[7]=y%256;
106 if(x<0) buf[7]|=0x80;
113 fprintf(stderr, "usage: bsdiff oldfile newfile patchfile\n");
117 int main(int argc,char *argv[])
121 off_t oldsize,newsize;
124 off_t lastscan,lastpos,lastoffset;
126 off_t s,Sf,lenf,Sb,lenb;
127 off_t overlap,Ss,lens;
140 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
141 that we never try to malloc(0) and get a NULL pointer */
142 if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) ||
143 ((oldsize=lseek(fd,0,SEEK_END))==-1))
144 err(1, "%s", argv[1]);
146 if (oldsize > SSIZE_MAX ||
147 (uintmax_t)oldsize >= SIZE_T_MAX / sizeof(off_t) ||
148 oldsize == OFF_MAX) {
150 err(1, "%s", argv[1]);
153 if (((old=malloc(oldsize+1))==NULL) ||
154 (lseek(fd,0,SEEK_SET)!=0) ||
155 (read(fd,old,oldsize)!=oldsize) ||
156 (close(fd)==-1)) err(1,"%s",argv[1]);
158 if(((I=malloc((oldsize+1)*sizeof(saidx_t)))==NULL)) err(1,NULL);
160 if(divsufsort(old, I, oldsize)) err(1, "divsufsort");
162 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
163 that we never try to malloc(0) and get a NULL pointer */
164 if(((fd=open(argv[2],O_RDONLY|O_BINARY,0))<0) ||
165 ((newsize=lseek(fd,0,SEEK_END))==-1))
166 err(1, "%s", argv[2]);
168 if (newsize > SSIZE_MAX || (uintmax_t)newsize >= SIZE_T_MAX ||
169 newsize == OFF_MAX) {
171 err(1, "%s", argv[2]);
174 if (((new=malloc(newsize+1))==NULL) ||
175 (lseek(fd,0,SEEK_SET)!=0) ||
176 (read(fd,new,newsize)!=newsize) ||
177 (close(fd)==-1)) err(1,"%s",argv[2]);
179 if(((db=malloc(newsize+1))==NULL) ||
180 ((eb=malloc(newsize+1))==NULL)) err(1,NULL);
184 /* Create the patch file */
185 if ((pf = fopen(argv[3], "wb")) == NULL)
186 err(1, "%s", argv[3]);
190 8 8 length of bzip2ed ctrl block
191 16 8 length of bzip2ed diff block
192 24 8 length of new file */
195 32 ?? Bzip2ed ctrl block
196 ?? ?? Bzip2ed diff block
197 ?? ?? Bzip2ed extra block */
198 memcpy(header,"BSDIFF40",8);
199 offtout(0, header + 8);
200 offtout(0, header + 16);
201 offtout(newsize, header + 24);
202 if (fwrite(header, 32, 1, pf) != 1)
203 err(1, "fwrite(%s)", argv[3]);
205 /* Compute the differences, writing ctrl as we go */
206 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
207 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
209 lastscan=0;lastpos=0;lastoffset=0;
210 while(scan<newsize) {
213 for(scsc=scan+=len;scan<newsize;scan++) {
214 len=search(I,old,oldsize,new+scan,newsize-scan,
217 for(;scsc<scan+len;scsc++)
218 if((scsc+lastoffset<oldsize) &&
219 (old[scsc+lastoffset] == new[scsc]))
222 if(((len==oldscore) && (len!=0)) ||
223 (len>oldscore+8)) break;
225 if((scan+lastoffset<oldsize) &&
226 (old[scan+lastoffset] == new[scan]))
230 if((len!=oldscore) || (scan==newsize)) {
232 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
233 if(old[lastpos+i]==new[lastscan+i]) s++;
235 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; }
241 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
242 if(old[pos-i]==new[scan-i]) s++;
243 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }
247 if(lastscan+lenf>scan-lenb) {
248 overlap=(lastscan+lenf)-(scan-lenb);
250 for(i=0;i<overlap;i++) {
251 if(new[lastscan+lenf-overlap+i]==
252 old[lastpos+lenf-overlap+i]) s++;
253 if(new[scan-lenb+i]==
254 old[pos-lenb+i]) s--;
255 if(s>Ss) { Ss=s; lens=i+1; }
263 db[dblen+i]=new[lastscan+i]-old[lastpos+i];
264 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
265 eb[eblen+i]=new[lastscan+lenf+i];
268 eblen+=(scan-lenb)-(lastscan+lenf);
271 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
273 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
275 offtout((scan-lenb)-(lastscan+lenf),buf);
276 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
278 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
280 offtout((pos-lenb)-(lastpos+lenf),buf);
281 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
283 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
290 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
292 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
294 /* Compute size of compressed ctrl data */
295 if ((len = ftello(pf)) == -1)
297 offtout(len-32, header + 8);
299 /* Write compressed diff data */
300 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
301 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
302 BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
304 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
305 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
307 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
309 /* Compute size of compressed diff data */
310 if ((newsize = ftello(pf)) == -1)
312 offtout(newsize - len, header + 16);
314 /* Write compressed extra data */
315 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
316 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
317 BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
319 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
320 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
322 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
324 /* Seek to the beginning, write the header, and close the file */
325 if (fseeko(pf, 0, SEEK_SET))
327 if (fwrite(header, 32, 1, pf) != 1)
328 err(1, "fwrite(%s)", argv[3]);
332 /* Free the memory we used */