blob: 86ce025bbce01e20ad12866e4df7ff58705c68f5 [file] [log] [blame]
The Android Open Source Projectc285fea2009-03-03 19:29:20 -08001/*-
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#if 0
28__FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 cperciva Exp $");
29#endif
30
31#include <sys/types.h>
32
33#include <bzlib.h>
34#include <err.h>
35#include <fcntl.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
Thieu Lec2c68a72011-05-17 16:43:07 -070041#if _FILE_OFFSET_BITS == 64
42#include "divsufsort64.h"
43#define saidx_t saidx64_t
44#define divsufsort divsufsort64
45#else
46#include "divsufsort.h"
47#endif
48
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080049#define MIN(x,y) (((x)<(y)) ? (x) : (y))
50
The Android Open Source Projectc285fea2009-03-03 19:29:20 -080051static off_t matchlen(u_char *old,off_t oldsize,u_char *new,off_t newsize)
52{
53 off_t i;
54
55 for(i=0;(i<oldsize)&&(i<newsize);i++)
56 if(old[i]!=new[i]) break;
57
58 return i;
59}
60
61static off_t search(off_t *I,u_char *old,off_t oldsize,
62 u_char *new,off_t newsize,off_t st,off_t en,off_t *pos)
63{
64 off_t x,y;
65
66 if(en-st<2) {
67 x=matchlen(old+I[st],oldsize-I[st],new,newsize);
68 y=matchlen(old+I[en],oldsize-I[en],new,newsize);
69
70 if(x>y) {
71 *pos=I[st];
72 return x;
73 } else {
74 *pos=I[en];
75 return y;
76 }
77 };
78
79 x=st+(en-st)/2;
80 if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
81 return search(I,old,oldsize,new,newsize,x,en,pos);
82 } else {
83 return search(I,old,oldsize,new,newsize,st,x,pos);
84 };
85}
86
87static void offtout(off_t x,u_char *buf)
88{
89 off_t y;
90
91 if(x<0) y=-x; else y=x;
92
93 buf[0]=y%256;y-=buf[0];
94 y=y/256;buf[1]=y%256;y-=buf[1];
95 y=y/256;buf[2]=y%256;y-=buf[2];
96 y=y/256;buf[3]=y%256;y-=buf[3];
97 y=y/256;buf[4]=y%256;y-=buf[4];
98 y=y/256;buf[5]=y%256;y-=buf[5];
99 y=y/256;buf[6]=y%256;y-=buf[6];
100 y=y/256;buf[7]=y%256;
101
102 if(x<0) buf[7]|=0x80;
103}
104
105int main(int argc,char *argv[])
106{
107 int fd;
108 u_char *old,*new;
109 off_t oldsize,newsize;
Thieu Lec2c68a72011-05-17 16:43:07 -0700110 saidx_t *I;
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800111 off_t scan,pos,len;
112 off_t lastscan,lastpos,lastoffset;
113 off_t oldscore,scsc;
114 off_t s,Sf,lenf,Sb,lenb;
115 off_t overlap,Ss,lens;
116 off_t i;
117 off_t dblen,eblen;
118 u_char *db,*eb;
119 u_char buf[8];
120 u_char header[32];
121 FILE * pf;
122 BZFILE * pfbz2;
123 int bz2err;
124
125 if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
126
127 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
128 that we never try to malloc(0) and get a NULL pointer */
129 if(((fd=open(argv[1],O_RDONLY,0))<0) ||
130 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
131 ((old=malloc(oldsize+1))==NULL) ||
132 (lseek(fd,0,SEEK_SET)!=0) ||
133 (read(fd,old,oldsize)!=oldsize) ||
134 (close(fd)==-1)) err(1,"%s",argv[1]);
135
Thieu Lec2c68a72011-05-17 16:43:07 -0700136 if(((I=malloc((oldsize+1)*sizeof(saidx_t)))==NULL)) err(1,NULL);
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800137
Thieu Lec2c68a72011-05-17 16:43:07 -0700138 if(divsufsort(old, I, oldsize)) err(1, "divsufsort");
The Android Open Source Projectc285fea2009-03-03 19:29:20 -0800139
140 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
141 that we never try to malloc(0) and get a NULL pointer */
142 if(((fd=open(argv[2],O_RDONLY,0))<0) ||
143 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
144 ((new=malloc(newsize+1))==NULL) ||
145 (lseek(fd,0,SEEK_SET)!=0) ||
146 (read(fd,new,newsize)!=newsize) ||
147 (close(fd)==-1)) err(1,"%s",argv[2]);
148
149 if(((db=malloc(newsize+1))==NULL) ||
150 ((eb=malloc(newsize+1))==NULL)) err(1,NULL);
151 dblen=0;
152 eblen=0;
153
154 /* Create the patch file */
155 if ((pf = fopen(argv[3], "w")) == NULL)
156 err(1, "%s", argv[3]);
157
158 /* Header is
159 0 8 "BSDIFF40"
160 8 8 length of bzip2ed ctrl block
161 16 8 length of bzip2ed diff block
162 24 8 length of new file */
163 /* File is
164 0 32 Header
165 32 ?? Bzip2ed ctrl block
166 ?? ?? Bzip2ed diff block
167 ?? ?? Bzip2ed extra block */
168 memcpy(header,"BSDIFF40",8);
169 offtout(0, header + 8);
170 offtout(0, header + 16);
171 offtout(newsize, header + 24);
172 if (fwrite(header, 32, 1, pf) != 1)
173 err(1, "fwrite(%s)", argv[3]);
174
175 /* Compute the differences, writing ctrl as we go */
176 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
177 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
178 scan=0;len=0;
179 lastscan=0;lastpos=0;lastoffset=0;
180 while(scan<newsize) {
181 oldscore=0;
182
183 for(scsc=scan+=len;scan<newsize;scan++) {
184 len=search(I,old,oldsize,new+scan,newsize-scan,
185 0,oldsize,&pos);
186
187 for(;scsc<scan+len;scsc++)
188 if((scsc+lastoffset<oldsize) &&
189 (old[scsc+lastoffset] == new[scsc]))
190 oldscore++;
191
192 if(((len==oldscore) && (len!=0)) ||
193 (len>oldscore+8)) break;
194
195 if((scan+lastoffset<oldsize) &&
196 (old[scan+lastoffset] == new[scan]))
197 oldscore--;
198 };
199
200 if((len!=oldscore) || (scan==newsize)) {
201 s=0;Sf=0;lenf=0;
202 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
203 if(old[lastpos+i]==new[lastscan+i]) s++;
204 i++;
205 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
206 };
207
208 lenb=0;
209 if(scan<newsize) {
210 s=0;Sb=0;
211 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
212 if(old[pos-i]==new[scan-i]) s++;
213 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
214 };
215 };
216
217 if(lastscan+lenf>scan-lenb) {
218 overlap=(lastscan+lenf)-(scan-lenb);
219 s=0;Ss=0;lens=0;
220 for(i=0;i<overlap;i++) {
221 if(new[lastscan+lenf-overlap+i]==
222 old[lastpos+lenf-overlap+i]) s++;
223 if(new[scan-lenb+i]==
224 old[pos-lenb+i]) s--;
225 if(s>Ss) { Ss=s; lens=i+1; };
226 };
227
228 lenf+=lens-overlap;
229 lenb-=lens;
230 };
231
232 for(i=0;i<lenf;i++)
233 db[dblen+i]=new[lastscan+i]-old[lastpos+i];
234 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
235 eb[eblen+i]=new[lastscan+lenf+i];
236
237 dblen+=lenf;
238 eblen+=(scan-lenb)-(lastscan+lenf);
239
240 offtout(lenf,buf);
241 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
242 if (bz2err != BZ_OK)
243 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
244
245 offtout((scan-lenb)-(lastscan+lenf),buf);
246 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
247 if (bz2err != BZ_OK)
248 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
249
250 offtout((pos-lenb)-(lastpos+lenf),buf);
251 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
252 if (bz2err != BZ_OK)
253 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
254
255 lastscan=scan-lenb;
256 lastpos=pos-lenb;
257 lastoffset=pos-scan;
258 };
259 };
260 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
261 if (bz2err != BZ_OK)
262 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
263
264 /* Compute size of compressed ctrl data */
265 if ((len = ftello(pf)) == -1)
266 err(1, "ftello");
267 offtout(len-32, header + 8);
268
269 /* Write compressed diff data */
270 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
271 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
272 BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
273 if (bz2err != BZ_OK)
274 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
275 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
276 if (bz2err != BZ_OK)
277 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
278
279 /* Compute size of compressed diff data */
280 if ((newsize = ftello(pf)) == -1)
281 err(1, "ftello");
282 offtout(newsize - len, header + 16);
283
284 /* Write compressed extra data */
285 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
286 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
287 BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
288 if (bz2err != BZ_OK)
289 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
290 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
291 if (bz2err != BZ_OK)
292 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
293
294 /* Seek to the beginning, write the header, and close the file */
295 if (fseeko(pf, 0, SEEK_SET))
296 err(1, "fseeko");
297 if (fwrite(header, 32, 1, pf) != 1)
298 err(1, "fwrite(%s)", argv[3]);
299 if (fclose(pf))
300 err(1, "fclose");
301
302 /* Free the memory we used */
303 free(db);
304 free(eb);
305 free(I);
306 free(old);
307 free(new);
308
309 return 0;
310}