2 * Copyright 2003-2005 Colin Percival
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
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.
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.
28 * Modified 18 March 2013 by Jason Stephenson <jason@sigio.com>.
30 * Modifications to make it compatible with the output of Mozilla's mbsdiff:
32 * Switch from 64 bit off_t to 32 bit int32_t.
33 * Don't bzip2 the output blocks.
34 * Use the modified header that Mozilla's bspatch expects.
35 * Add crc32 of the original file to the header.
36 * Use 32-bit ftell and fseek instead of ftello and fseeko.
39 #include <sys/types.h>
45 #include <netinet/in.h>
48 /* Calculate the crc32. */
50 crc32(const unsigned char *buf, uint32_t len);
52 #define MIN(x,y) (((x)<(y)) ? (x) : (y))
54 static void split(int32_t *I,int32_t *V,int32_t start,int32_t len,int32_t h)
56 int32_t i,j,k,x,tmp,jj,kk;
59 for(k=start;k<start+len;k+=j) {
61 for(i=1;k+i<start+len;i++) {
67 tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
71 for(i=0;i<j;i++) V[I[k+i]]=k+j-1;
77 x=V[I[start+len/2]+h];
79 for(i=start;i<start+len;i++) {
81 if(V[I[i]+h]==x) kk++;
89 } else if(V[I[i]+h]==x) {
90 tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
93 tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
102 tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
107 if(jj>start) split(I,V,start,jj-start,h);
109 for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1;
110 if(jj==kk-1) I[jj]=-1;
112 if(start+len>kk) split(I,V,kk,start+len-kk,h);
115 static void qsufsort(int32_t *I,int32_t *V,u_char *old,int32_t oldsize)
117 int32_t buckets[256];
120 for(i=0;i<256;i++) buckets[i]=0;
121 for(i=0;i<oldsize;i++) buckets[old[i]]++;
122 for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
123 for(i=255;i>0;i--) buckets[i]=buckets[i-1];
126 for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i;
128 for(i=0;i<oldsize;i++) V[i]=buckets[old[i]];
130 for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
133 for(h=1;I[0]!=-(oldsize+1);h+=h) {
135 for(i=0;i<oldsize+1;) {
140 if(len) I[i-len]=-len;
147 if(len) I[i-len]=-len;
150 for(i=0;i<oldsize+1;i++) I[V[i]]=i;
153 static int32_t matchlen(u_char *old,int32_t oldsize,u_char *new,int32_t newsize)
157 for(i=0;(i<oldsize)&&(i<newsize);i++)
158 if(old[i]!=new[i]) break;
163 static int32_t search(int32_t *I,u_char *old,int32_t oldsize,
164 u_char *new,int32_t newsize,int32_t st,int32_t en,int32_t *pos)
169 x=matchlen(old+I[st],oldsize-I[st],new,newsize);
170 y=matchlen(old+I[en],oldsize-I[en],new,newsize);
182 if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
183 return search(I,old,oldsize,new,newsize,x,en,pos);
185 return search(I,old,oldsize,new,newsize,st,x,pos);
189 int main(int argc,char *argv[])
193 int32_t oldsize,newsize;
195 int32_t scan,pos,len;
196 int32_t lastscan,lastpos,lastoffset;
197 int32_t oldscore,scsc;
198 int32_t s,Sf,lenf,Sb,lenb;
199 int32_t overlap,Ss,lens;
206 int32_t outVal; /* place holder for result of htonl */
208 if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
210 /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
211 that we never try to malloc(0) and get a NULL pointer */
212 if(((fd=open(argv[1],O_RDONLY,0))<0) ||
213 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
214 ((old=malloc(oldsize+1))==NULL) ||
215 (lseek(fd,0,SEEK_SET)!=0) ||
216 (read(fd,old,oldsize)!=oldsize) ||
217 (close(fd)==-1)) err(1,"%s",argv[1]);
219 if(((I=malloc((oldsize+1)*sizeof(int32_t)))==NULL) ||
220 ((V=malloc((oldsize+1)*sizeof(int32_t)))==NULL)) err(1,NULL);
222 /* Calculate the old file's crc32 value. */
223 crcVal = crc32(old, oldsize);
225 qsufsort(I,V,old,oldsize);
229 /* Allocate newsize+1 bytes instead of newsize bytes to ensure
230 that we never try to malloc(0) and get a NULL pointer */
231 if(((fd=open(argv[2],O_RDONLY,0))<0) ||
232 ((newsize=lseek(fd,0,SEEK_END))==-1) ||
233 ((new=malloc(newsize+1))==NULL) ||
234 (lseek(fd,0,SEEK_SET)!=0) ||
235 (read(fd,new,newsize)!=newsize) ||
236 (close(fd)==-1)) err(1,"%s",argv[2]);
238 if(((db=malloc(newsize+1))==NULL) ||
239 ((eb=malloc(newsize+1))==NULL)) err(1,NULL);
243 /* Create the patch file */
244 if ((pf = fopen(argv[3], "w")) == NULL)
245 err(1, "%s", argv[3]);
247 /* This is the modified header used by Mozilla's hacked bspatch. It
248 * doesn't actually bzip2 anything despite what the documentation on
249 * the wiki implies. */
252 8 4 length of the file to be patched
253 12 4 crc32 of file to be patched
254 16 4 length of the new file
255 20 4 length of ctrl block
256 24 4 length of diff block
257 28 4 length of extra block
263 memcpy(header, "MBDIFF10", 8);
264 outVal = htonl(oldsize);
265 memcpy(header + 8, &outVal, 4);
266 outVal = htonl(crcVal);
267 memcpy(header + 12, &outVal, 4);
268 outVal = htonl(newsize);
269 memcpy(header + 16, &outVal, 4);
270 memcpy(header + 20, 0, 12);
271 if (fwrite(header, 32, 1, pf) != 1)
272 err(1, "fwrite(%s)", argv[3]);
274 /* Compute the differences, writing ctrl as we go */
276 lastscan=0;lastpos=0;lastoffset=0;
277 while(scan<newsize) {
280 for(scsc=scan+=len;scan<newsize;scan++) {
281 len=search(I,old,oldsize,new+scan,newsize-scan,
284 for(;scsc<scan+len;scsc++)
285 if((scsc+lastoffset<oldsize) &&
286 (old[scsc+lastoffset] == new[scsc]))
289 if(((len==oldscore) && (len!=0)) ||
290 (len>oldscore+8)) break;
292 if((scan+lastoffset<oldsize) &&
293 (old[scan+lastoffset] == new[scan]))
297 if((len!=oldscore) || (scan==newsize)) {
299 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
300 if(old[lastpos+i]==new[lastscan+i]) s++;
302 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
308 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
309 if(old[pos-i]==new[scan-i]) s++;
310 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
314 if(lastscan+lenf>scan-lenb) {
315 overlap=(lastscan+lenf)-(scan-lenb);
317 for(i=0;i<overlap;i++) {
318 if(new[lastscan+lenf-overlap+i]==
319 old[lastpos+lenf-overlap+i]) s++;
320 if(new[scan-lenb+i]==
321 old[pos-lenb+i]) s--;
322 if(s>Ss) { Ss=s; lens=i+1; };
330 db[dblen+i]=new[lastscan+i]-old[lastpos+i];
331 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
332 eb[eblen+i]=new[lastscan+lenf+i];
335 eblen+=(scan-lenb)-(lastscan+lenf);
337 outVal = htonl(lenf);
338 if (fwrite(&outVal, 4, 1, pf) != 1)
339 err(1, "fwrite(%s)", argv[3]);
341 outVal = htonl((scan-lenb)-(lastscan+lenf));
342 if (fwrite(&outVal, 4, 1, pf) != 1)
343 err(1, "fwrite(%s)", argv[3]);
345 outVal = htonl((pos-lenb)-(lastpos+lenf));
346 if (fwrite(&outVal, 4, 1, pf) != 1)
347 err(1, "fwrite(%s)", argv[3]);
355 /* Compute size of ctrl data */
356 if ((len = ftell(pf)) == -1)
358 outVal = htonl(len - 32);
359 memcpy(header + 20, &outVal, 4);
361 /* Write the diff data */
362 if (fwrite(db, 1, dblen, pf) != dblen)
363 err(1, "fwrite(%s)", argv[3]);
365 /* Size of diff data */
366 outVal = htonl(dblen);
367 memcpy(header + 24, &outVal, 4);
369 /* Write extra data */
370 if (fwrite(eb, 1, eblen, pf) != eblen)
371 err(1, "fwrite(%s)", argv[3]);
373 /* Size of extra data */
374 outVal = htonl(eblen);
375 memcpy(header + 28, &outVal, 4);
377 /* Seek to the beginning, write the header, and close the file */
378 if (fseek(pf, 0, SEEK_SET))
380 if (fwrite(header, 32, 1, pf) != 1)
381 err(1, "fwrite(%s)", argv[3]);
385 /* Free the memory we used */