blob: cca1b32fb080ba015ef96fe7da6cea61b9f8bf90 [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Most of this code comes from bsdiff.c from the bsdiff-4.3
19 * distribution, which is:
20 */
21
22/*-
23 * Copyright 2003-2005 Colin Percival
24 * All rights reserved
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted providing that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
39 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
44 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
46 */
47
48#include <sys/types.h>
49
50#include <bzlib.h>
51#include <err.h>
52#include <fcntl.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57
58#define MIN(x,y) (((x)<(y)) ? (x) : (y))
59
60static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h)
61{
62 off_t i,j,k,x,tmp,jj,kk;
63
64 if(len<16) {
65 for(k=start;k<start+len;k+=j) {
66 j=1;x=V[I[k]+h];
67 for(i=1;k+i<start+len;i++) {
68 if(V[I[k+i]+h]<x) {
69 x=V[I[k+i]+h];
70 j=0;
71 };
72 if(V[I[k+i]+h]==x) {
73 tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
74 j++;
75 };
76 };
77 for(i=0;i<j;i++) V[I[k+i]]=k+j-1;
78 if(j==1) I[k]=-1;
79 };
80 return;
81 };
82
83 x=V[I[start+len/2]+h];
84 jj=0;kk=0;
85 for(i=start;i<start+len;i++) {
86 if(V[I[i]+h]<x) jj++;
87 if(V[I[i]+h]==x) kk++;
88 };
89 jj+=start;kk+=jj;
90
91 i=start;j=0;k=0;
92 while(i<jj) {
93 if(V[I[i]+h]<x) {
94 i++;
95 } else if(V[I[i]+h]==x) {
96 tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
97 j++;
98 } else {
99 tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
100 k++;
101 };
102 };
103
104 while(jj+j<kk) {
105 if(V[I[jj+j]+h]==x) {
106 j++;
107 } else {
108 tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
109 k++;
110 };
111 };
112
113 if(jj>start) split(I,V,start,jj-start,h);
114
115 for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1;
116 if(jj==kk-1) I[jj]=-1;
117
118 if(start+len>kk) split(I,V,kk,start+len-kk,h);
119}
120
121static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize)
122{
123 off_t buckets[256];
124 off_t i,h,len;
125
126 for(i=0;i<256;i++) buckets[i]=0;
127 for(i=0;i<oldsize;i++) buckets[old[i]]++;
128 for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
129 for(i=255;i>0;i--) buckets[i]=buckets[i-1];
130 buckets[0]=0;
131
132 for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i;
133 I[0]=oldsize;
134 for(i=0;i<oldsize;i++) V[i]=buckets[old[i]];
135 V[oldsize]=0;
136 for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
137 I[0]=-1;
138
139 for(h=1;I[0]!=-(oldsize+1);h+=h) {
140 len=0;
141 for(i=0;i<oldsize+1;) {
142 if(I[i]<0) {
143 len-=I[i];
144 i-=I[i];
145 } else {
146 if(len) I[i-len]=-len;
147 len=V[I[i]]+1-i;
148 split(I,V,i,len,h);
149 i+=len;
150 len=0;
151 };
152 };
153 if(len) I[i-len]=-len;
154 };
155
156 for(i=0;i<oldsize+1;i++) I[V[i]]=i;
157}
158
Tao Baoba9a42a2015-06-23 23:23:33 -0700159static off_t matchlen(u_char *olddata,off_t oldsize,u_char *newdata,off_t newsize)
Doug Zongker512536a2010-02-17 16:11:44 -0800160{
161 off_t i;
162
163 for(i=0;(i<oldsize)&&(i<newsize);i++)
Tao Baoba9a42a2015-06-23 23:23:33 -0700164 if(olddata[i]!=newdata[i]) break;
Doug Zongker512536a2010-02-17 16:11:44 -0800165
166 return i;
167}
168
169static off_t search(off_t *I,u_char *old,off_t oldsize,
Tao Baoba9a42a2015-06-23 23:23:33 -0700170 u_char *newdata,off_t newsize,off_t st,off_t en,off_t *pos)
Doug Zongker512536a2010-02-17 16:11:44 -0800171{
172 off_t x,y;
173
174 if(en-st<2) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700175 x=matchlen(old+I[st],oldsize-I[st],newdata,newsize);
176 y=matchlen(old+I[en],oldsize-I[en],newdata,newsize);
Doug Zongker512536a2010-02-17 16:11:44 -0800177
178 if(x>y) {
179 *pos=I[st];
180 return x;
181 } else {
182 *pos=I[en];
183 return y;
184 }
185 };
186
187 x=st+(en-st)/2;
Tao Baoba9a42a2015-06-23 23:23:33 -0700188 if(memcmp(old+I[x],newdata,MIN(oldsize-I[x],newsize))<0) {
189 return search(I,old,oldsize,newdata,newsize,x,en,pos);
Doug Zongker512536a2010-02-17 16:11:44 -0800190 } else {
Tao Baoba9a42a2015-06-23 23:23:33 -0700191 return search(I,old,oldsize,newdata,newsize,st,x,pos);
Doug Zongker512536a2010-02-17 16:11:44 -0800192 };
193}
194
195static void offtout(off_t x,u_char *buf)
196{
197 off_t y;
198
199 if(x<0) y=-x; else y=x;
200
201 buf[0]=y%256;y-=buf[0];
202 y=y/256;buf[1]=y%256;y-=buf[1];
203 y=y/256;buf[2]=y%256;y-=buf[2];
204 y=y/256;buf[3]=y%256;y-=buf[3];
205 y=y/256;buf[4]=y%256;y-=buf[4];
206 y=y/256;buf[5]=y%256;y-=buf[5];
207 y=y/256;buf[6]=y%256;y-=buf[6];
208 y=y/256;buf[7]=y%256;
209
210 if(x<0) buf[7]|=0x80;
211}
212
213// This is main() from bsdiff.c, with the following changes:
214//
Tao Baoba9a42a2015-06-23 23:23:33 -0700215// - old, oldsize, newdata, newsize are arguments; we don't load this
216// data from files. old and newdata are owned by the caller; we
Doug Zongker512536a2010-02-17 16:11:44 -0800217// don't free them at the end.
218//
219// - the "I" block of memory is owned by the caller, who passes a
220// pointer to *I, which can be NULL. This way if we call
221// bsdiff() multiple times with the same 'old' data, we only do
222// the qsufsort() step the first time.
223//
Tao Baoba9a42a2015-06-23 23:23:33 -0700224int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* newdata, off_t newsize,
Doug Zongker512536a2010-02-17 16:11:44 -0800225 const char* patch_filename)
226{
Doug Zongker512536a2010-02-17 16:11:44 -0800227 off_t *I;
228 off_t scan,pos,len;
229 off_t lastscan,lastpos,lastoffset;
230 off_t oldscore,scsc;
231 off_t s,Sf,lenf,Sb,lenb;
232 off_t overlap,Ss,lens;
233 off_t i;
234 off_t dblen,eblen;
235 u_char *db,*eb;
236 u_char buf[8];
237 u_char header[32];
238 FILE * pf;
239 BZFILE * pfbz2;
240 int bz2err;
241
242 if (*IP == NULL) {
243 off_t* V;
Tao Baoba9a42a2015-06-23 23:23:33 -0700244 *IP = reinterpret_cast<off_t*>(malloc((oldsize+1) * sizeof(off_t)));
245 V = reinterpret_cast<off_t*>(malloc((oldsize+1) * sizeof(off_t)));
Doug Zongker512536a2010-02-17 16:11:44 -0800246 qsufsort(*IP, V, old, oldsize);
247 free(V);
248 }
249 I = *IP;
250
Tao Baoba9a42a2015-06-23 23:23:33 -0700251 if(((db=reinterpret_cast<u_char*>(malloc(newsize+1)))==NULL) ||
252 ((eb=reinterpret_cast<u_char*>(malloc(newsize+1)))==NULL)) err(1,NULL);
Doug Zongker512536a2010-02-17 16:11:44 -0800253 dblen=0;
254 eblen=0;
255
256 /* Create the patch file */
257 if ((pf = fopen(patch_filename, "w")) == NULL)
258 err(1, "%s", patch_filename);
259
260 /* Header is
261 0 8 "BSDIFF40"
262 8 8 length of bzip2ed ctrl block
263 16 8 length of bzip2ed diff block
264 24 8 length of new file */
265 /* File is
266 0 32 Header
267 32 ?? Bzip2ed ctrl block
268 ?? ?? Bzip2ed diff block
269 ?? ?? Bzip2ed extra block */
270 memcpy(header,"BSDIFF40",8);
271 offtout(0, header + 8);
272 offtout(0, header + 16);
273 offtout(newsize, header + 24);
274 if (fwrite(header, 32, 1, pf) != 1)
275 err(1, "fwrite(%s)", patch_filename);
276
277 /* Compute the differences, writing ctrl as we go */
278 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
279 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
280 scan=0;len=0;
281 lastscan=0;lastpos=0;lastoffset=0;
282 while(scan<newsize) {
283 oldscore=0;
284
285 for(scsc=scan+=len;scan<newsize;scan++) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700286 len=search(I,old,oldsize,newdata+scan,newsize-scan,
Doug Zongker512536a2010-02-17 16:11:44 -0800287 0,oldsize,&pos);
288
289 for(;scsc<scan+len;scsc++)
290 if((scsc+lastoffset<oldsize) &&
Tao Baoba9a42a2015-06-23 23:23:33 -0700291 (old[scsc+lastoffset] == newdata[scsc]))
Doug Zongker512536a2010-02-17 16:11:44 -0800292 oldscore++;
293
294 if(((len==oldscore) && (len!=0)) ||
295 (len>oldscore+8)) break;
296
297 if((scan+lastoffset<oldsize) &&
Tao Baoba9a42a2015-06-23 23:23:33 -0700298 (old[scan+lastoffset] == newdata[scan]))
Doug Zongker512536a2010-02-17 16:11:44 -0800299 oldscore--;
300 };
301
302 if((len!=oldscore) || (scan==newsize)) {
303 s=0;Sf=0;lenf=0;
304 for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700305 if(old[lastpos+i]==newdata[lastscan+i]) s++;
Doug Zongker512536a2010-02-17 16:11:44 -0800306 i++;
307 if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
308 };
309
310 lenb=0;
311 if(scan<newsize) {
312 s=0;Sb=0;
313 for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700314 if(old[pos-i]==newdata[scan-i]) s++;
Doug Zongker512536a2010-02-17 16:11:44 -0800315 if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
316 };
317 };
318
319 if(lastscan+lenf>scan-lenb) {
320 overlap=(lastscan+lenf)-(scan-lenb);
321 s=0;Ss=0;lens=0;
322 for(i=0;i<overlap;i++) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700323 if(newdata[lastscan+lenf-overlap+i]==
Doug Zongker512536a2010-02-17 16:11:44 -0800324 old[lastpos+lenf-overlap+i]) s++;
Tao Baoba9a42a2015-06-23 23:23:33 -0700325 if(newdata[scan-lenb+i]==
Doug Zongker512536a2010-02-17 16:11:44 -0800326 old[pos-lenb+i]) s--;
327 if(s>Ss) { Ss=s; lens=i+1; };
328 };
329
330 lenf+=lens-overlap;
331 lenb-=lens;
332 };
333
334 for(i=0;i<lenf;i++)
Tao Baoba9a42a2015-06-23 23:23:33 -0700335 db[dblen+i]=newdata[lastscan+i]-old[lastpos+i];
Doug Zongker512536a2010-02-17 16:11:44 -0800336 for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
Tao Baoba9a42a2015-06-23 23:23:33 -0700337 eb[eblen+i]=newdata[lastscan+lenf+i];
Doug Zongker512536a2010-02-17 16:11:44 -0800338
339 dblen+=lenf;
340 eblen+=(scan-lenb)-(lastscan+lenf);
341
342 offtout(lenf,buf);
343 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
344 if (bz2err != BZ_OK)
345 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
346
347 offtout((scan-lenb)-(lastscan+lenf),buf);
348 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
349 if (bz2err != BZ_OK)
350 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
351
352 offtout((pos-lenb)-(lastpos+lenf),buf);
353 BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
354 if (bz2err != BZ_OK)
355 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
356
357 lastscan=scan-lenb;
358 lastpos=pos-lenb;
359 lastoffset=pos-scan;
360 };
361 };
362 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
363 if (bz2err != BZ_OK)
364 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
365
366 /* Compute size of compressed ctrl data */
367 if ((len = ftello(pf)) == -1)
368 err(1, "ftello");
369 offtout(len-32, header + 8);
370
371 /* Write compressed diff data */
372 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
373 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
374 BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
375 if (bz2err != BZ_OK)
376 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
377 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
378 if (bz2err != BZ_OK)
379 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
380
381 /* Compute size of compressed diff data */
382 if ((newsize = ftello(pf)) == -1)
383 err(1, "ftello");
384 offtout(newsize - len, header + 16);
385
386 /* Write compressed extra data */
387 if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
388 errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
389 BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
390 if (bz2err != BZ_OK)
391 errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
392 BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
393 if (bz2err != BZ_OK)
394 errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
395
396 /* Seek to the beginning, write the header, and close the file */
397 if (fseeko(pf, 0, SEEK_SET))
398 err(1, "fseeko");
399 if (fwrite(header, 32, 1, pf) != 1)
400 err(1, "fwrite(%s)", patch_filename);
401 if (fclose(pf))
402 err(1, "fclose");
403
404 /* Free the memory we used */
405 free(db);
406 free(eb);
407
408 return 0;
409}