]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/usr.bin/bsdiff/bsdiff/bsdiff.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / usr.bin / bsdiff / bsdiff / bsdiff.c
1 /*-
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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31
32 #include <bzlib.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #define MIN(x,y) (((x)<(y)) ? (x) : (y))
41
42 static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h)
43 {
44         off_t i,j,k,x,tmp,jj,kk;
45
46         if(len<16) {
47                 for(k=start;k<start+len;k+=j) {
48                         j=1;x=V[I[k]+h];
49                         for(i=1;k+i<start+len;i++) {
50                                 if(V[I[k+i]+h]<x) {
51                                         x=V[I[k+i]+h];
52                                         j=0;
53                                 };
54                                 if(V[I[k+i]+h]==x) {
55                                         tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
56                                         j++;
57                                 };
58                         };
59                         for(i=0;i<j;i++) V[I[k+i]]=k+j-1;
60                         if(j==1) I[k]=-1;
61                 };
62                 return;
63         };
64
65         x=V[I[start+len/2]+h];
66         jj=0;kk=0;
67         for(i=start;i<start+len;i++) {
68                 if(V[I[i]+h]<x) jj++;
69                 if(V[I[i]+h]==x) kk++;
70         };
71         jj+=start;kk+=jj;
72
73         i=start;j=0;k=0;
74         while(i<jj) {
75                 if(V[I[i]+h]<x) {
76                         i++;
77                 } else if(V[I[i]+h]==x) {
78                         tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
79                         j++;
80                 } else {
81                         tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
82                         k++;
83                 };
84         };
85
86         while(jj+j<kk) {
87                 if(V[I[jj+j]+h]==x) {
88                         j++;
89                 } else {
90                         tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
91                         k++;
92                 };
93         };
94
95         if(jj>start) split(I,V,start,jj-start,h);
96
97         for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1;
98         if(jj==kk-1) I[jj]=-1;
99
100         if(start+len>kk) split(I,V,kk,start+len-kk,h);
101 }
102
103 static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize)
104 {
105         off_t buckets[256];
106         off_t i,h,len;
107
108         for(i=0;i<256;i++) buckets[i]=0;
109         for(i=0;i<oldsize;i++) buckets[old[i]]++;
110         for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
111         for(i=255;i>0;i--) buckets[i]=buckets[i-1];
112         buckets[0]=0;
113
114         for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i;
115         I[0]=oldsize;
116         for(i=0;i<oldsize;i++) V[i]=buckets[old[i]];
117         V[oldsize]=0;
118         for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
119         I[0]=-1;
120
121         for(h=1;I[0]!=-(oldsize+1);h+=h) {
122                 len=0;
123                 for(i=0;i<oldsize+1;) {
124                         if(I[i]<0) {
125                                 len-=I[i];
126                                 i-=I[i];
127                         } else {
128                                 if(len) I[i-len]=-len;
129                                 len=V[I[i]]+1-i;
130                                 split(I,V,i,len,h);
131                                 i+=len;
132                                 len=0;
133                         };
134                 };
135                 if(len) I[i-len]=-len;
136         };
137
138         for(i=0;i<oldsize+1;i++) I[V[i]]=i;
139 }
140
141 static off_t matchlen(u_char *old,off_t oldsize,u_char *new,off_t newsize)
142 {
143         off_t i;
144
145         for(i=0;(i<oldsize)&&(i<newsize);i++)
146                 if(old[i]!=new[i]) break;
147
148         return i;
149 }
150
151 static off_t search(off_t *I,u_char *old,off_t oldsize,
152                 u_char *new,off_t newsize,off_t st,off_t en,off_t *pos)
153 {
154         off_t x,y;
155
156         if(en-st<2) {
157                 x=matchlen(old+I[st],oldsize-I[st],new,newsize);
158                 y=matchlen(old+I[en],oldsize-I[en],new,newsize);
159
160                 if(x>y) {
161                         *pos=I[st];
162                         return x;
163                 } else {
164                         *pos=I[en];
165                         return y;
166                 }
167         };
168
169         x=st+(en-st)/2;
170         if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
171                 return search(I,old,oldsize,new,newsize,x,en,pos);
172         } else {
173                 return search(I,old,oldsize,new,newsize,st,x,pos);
174         };
175 }
176
177 static void offtout(off_t x,u_char *buf)
178 {
179         off_t y;
180
181         if(x<0) y=-x; else y=x;
182
183                 buf[0]=y%256;y-=buf[0];
184         y=y/256;buf[1]=y%256;y-=buf[1];
185         y=y/256;buf[2]=y%256;y-=buf[2];
186         y=y/256;buf[3]=y%256;y-=buf[3];
187         y=y/256;buf[4]=y%256;y-=buf[4];
188         y=y/256;buf[5]=y%256;y-=buf[5];
189         y=y/256;buf[6]=y%256;y-=buf[6];
190         y=y/256;buf[7]=y%256;
191
192         if(x<0) buf[7]|=0x80;
193 }
194
195 int main(int argc,char *argv[])
196 {
197         int fd;
198         u_char *old,*new;
199         off_t oldsize,newsize;
200         off_t *I,*V;
201         off_t scan,pos,len;
202         off_t lastscan,lastpos,lastoffset;
203         off_t oldscore,scsc;
204         off_t s,Sf,lenf,Sb,lenb;
205         off_t overlap,Ss,lens;
206         off_t i;
207         off_t dblen,eblen;
208         u_char *db,*eb;
209         u_char buf[8];
210         u_char header[32];
211         FILE * pf;
212         BZFILE * pfbz2;
213         int bz2err;
214
215         if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
216
217         /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
218                 that we never try to malloc(0) and get a NULL pointer */
219         if(((fd=open(argv[1],O_RDONLY,0))<0) ||
220                 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
221                 ((old=malloc(oldsize+1))==NULL) ||
222                 (lseek(fd,0,SEEK_SET)!=0) ||
223                 (read(fd,old,oldsize)!=oldsize) ||
224                 (close(fd)==-1)) err(1,"%s",argv[1]);
225
226         if(((I=malloc((oldsize+1)*sizeof(off_t)))==NULL) ||
227                 ((V=malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL);
228
229         qsufsort(I,V,old,oldsize);
230
231         free(V);
232
233         /* Allocate newsize+1 bytes instead of newsize bytes to ensure
234                 that we never try to malloc(0) and get a NULL pointer */
235         if(((fd=open(argv[2],O_RDONLY,0))<0) ||
236                 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
237                 ((new=malloc(newsize+1))==NULL) ||
238                 (lseek(fd,0,SEEK_SET)!=0) ||
239                 (read(fd,new,newsize)!=newsize) ||
240                 (close(fd)==-1)) err(1,"%s",argv[2]);
241
242         if(((db=malloc(newsize+1))==NULL) ||
243                 ((eb=malloc(newsize+1))==NULL)) err(1,NULL);
244         dblen=0;
245         eblen=0;
246
247         /* Create the patch file */
248         if ((pf = fopen(argv[3], "w")) == NULL)
249                 err(1, "%s", argv[3]);
250
251         /* Header is
252                 0       8        "BSDIFF40"
253                 8       8       length of bzip2ed ctrl block
254                 16      8       length of bzip2ed diff block
255                 24      8       length of new file */
256         /* File is
257                 0       32      Header
258                 32      ??      Bzip2ed ctrl block
259                 ??      ??      Bzip2ed diff block
260                 ??      ??      Bzip2ed extra block */
261         memcpy(header,"BSDIFF40",8);
262         offtout(0, header + 8);
263         offtout(0, header + 16);
264         offtout(newsize, header + 24);
265         if (fwrite(header, 32, 1, pf) != 1)
266                 err(1, "fwrite(%s)", argv[3]);
267
268         /* Compute the differences, writing ctrl as we go */
269         if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
270                 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
271         scan=0;len=0;
272         lastscan=0;lastpos=0;lastoffset=0;
273         while(scan<newsize) {
274                 oldscore=0;
275
276                 for(scsc=scan+=len;scan<newsize;scan++) {
277                         len=search(I,old,oldsize,new+scan,newsize-scan,
278                                         0,oldsize,&pos);
279
280                         for(;scsc<scan+len;scsc++)
281                         if((scsc+lastoffset<oldsize) &&
282                                 (old[scsc+lastoffset] == new[scsc]))
283                                 oldscore++;
284
285                         if(((len==oldscore) && (len!=0)) || 
286                                 (len>oldscore+8)) break;
287
288                         if((scan+lastoffset<oldsize) &&
289                                 (old[scan+lastoffset] == new[scan]))
290                                 oldscore--;
291                 };
292
293                 if((len!=oldscore) || (scan==newsize)) {
294                         s=0;Sf=0;lenf=0;
295                         for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
296                                 if(old[lastpos+i]==new[lastscan+i]) s++;
297                                 i++;
298                                 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
299                         };
300
301                         lenb=0;
302                         if(scan<newsize) {
303                                 s=0;Sb=0;
304                                 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
305                                         if(old[pos-i]==new[scan-i]) s++;
306                                         if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
307                                 };
308                         };
309
310                         if(lastscan+lenf>scan-lenb) {
311                                 overlap=(lastscan+lenf)-(scan-lenb);
312                                 s=0;Ss=0;lens=0;
313                                 for(i=0;i<overlap;i++) {
314                                         if(new[lastscan+lenf-overlap+i]==
315                                            old[lastpos+lenf-overlap+i]) s++;
316                                         if(new[scan-lenb+i]==
317                                            old[pos-lenb+i]) s--;
318                                         if(s>Ss) { Ss=s; lens=i+1; };
319                                 };
320
321                                 lenf+=lens-overlap;
322                                 lenb-=lens;
323                         };
324
325                         for(i=0;i<lenf;i++)
326                                 db[dblen+i]=new[lastscan+i]-old[lastpos+i];
327                         for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
328                                 eb[eblen+i]=new[lastscan+lenf+i];
329
330                         dblen+=lenf;
331                         eblen+=(scan-lenb)-(lastscan+lenf);
332
333                         offtout(lenf,buf);
334                         BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
335                         if (bz2err != BZ_OK)
336                                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
337
338                         offtout((scan-lenb)-(lastscan+lenf),buf);
339                         BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
340                         if (bz2err != BZ_OK)
341                                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
342
343                         offtout((pos-lenb)-(lastpos+lenf),buf);
344                         BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
345                         if (bz2err != BZ_OK)
346                                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
347
348                         lastscan=scan-lenb;
349                         lastpos=pos-lenb;
350                         lastoffset=pos-scan;
351                 };
352         };
353         BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
354         if (bz2err != BZ_OK)
355                 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
356
357         /* Compute size of compressed ctrl data */
358         if ((len = ftello(pf)) == -1)
359                 err(1, "ftello");
360         offtout(len-32, header + 8);
361
362         /* Write compressed diff data */
363         if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
364                 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
365         BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
366         if (bz2err != BZ_OK)
367                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
368         BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
369         if (bz2err != BZ_OK)
370                 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
371
372         /* Compute size of compressed diff data */
373         if ((newsize = ftello(pf)) == -1)
374                 err(1, "ftello");
375         offtout(newsize - len, header + 16);
376
377         /* Write compressed extra data */
378         if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
379                 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
380         BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
381         if (bz2err != BZ_OK)
382                 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
383         BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
384         if (bz2err != BZ_OK)
385                 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
386
387         /* Seek to the beginning, write the header, and close the file */
388         if (fseeko(pf, 0, SEEK_SET))
389                 err(1, "fseeko");
390         if (fwrite(header, 32, 1, pf) != 1)
391                 err(1, "fwrite(%s)", argv[3]);
392         if (fclose(pf))
393                 err(1, "fclose");
394
395         /* Free the memory we used */
396         free(db);
397         free(eb);
398         free(I);
399         free(old);
400         free(new);
401
402         return 0;
403 }