|
rpm-build |
0fba15 |
/*-
|
|
rpm-build |
0fba15 |
* Copyright 2003-2005 Colin Percival
|
|
rpm-build |
0fba15 |
* Copyright 2012 Matthew Endsley
|
|
rpm-build |
0fba15 |
* All rights reserved
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Redistribution and use in source and binary forms, with or without
|
|
rpm-build |
0fba15 |
* modification, are permitted providing that the following conditions
|
|
rpm-build |
0fba15 |
* are met:
|
|
rpm-build |
0fba15 |
* 1. Redistributions of source code must retain the above copyright
|
|
rpm-build |
0fba15 |
* notice, this list of conditions and the following disclaimer.
|
|
rpm-build |
0fba15 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
rpm-build |
0fba15 |
* notice, this list of conditions and the following disclaimer in the
|
|
rpm-build |
0fba15 |
* documentation and/or other materials provided with the distribution.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
rpm-build |
0fba15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
rpm-build |
0fba15 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
rpm-build |
0fba15 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
rpm-build |
0fba15 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
rpm-build |
0fba15 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
rpm-build |
0fba15 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
rpm-build |
0fba15 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
rpm-build |
0fba15 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
rpm-build |
0fba15 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
rpm-build |
0fba15 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include "bsdiff.h"
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include <limits.h>
|
|
rpm-build |
0fba15 |
#include <string.h>
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static void split(int64_t *I,int64_t *V,int64_t start,int64_t len,int64_t h)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int64_t i,j,k,x,tmp,jj,kk;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(len<16) {
|
|
rpm-build |
0fba15 |
for(k=start;k
|
|
rpm-build |
0fba15 |
j=1;x=V[I[k]+h];
|
|
rpm-build |
0fba15 |
for(i=1;k+i
|
|
rpm-build |
0fba15 |
if(V[I[k+i]+h]
|
|
rpm-build |
0fba15 |
x=V[I[k+i]+h];
|
|
rpm-build |
0fba15 |
j=0;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
if(V[I[k+i]+h]==x) {
|
|
rpm-build |
0fba15 |
tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
|
|
rpm-build |
0fba15 |
j++;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
if(j==1) I[k]=-1;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
return;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
x=V[I[start+len/2]+h];
|
|
rpm-build |
0fba15 |
jj=0;kk=0;
|
|
rpm-build |
0fba15 |
for(i=start;i
|
|
rpm-build |
0fba15 |
if(V[I[i]+h]
|
|
rpm-build |
0fba15 |
if(V[I[i]+h]==x) kk++;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
jj+=start;kk+=jj;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
i=start;j=0;k=0;
|
|
rpm-build |
0fba15 |
while(i
|
|
rpm-build |
0fba15 |
if(V[I[i]+h]
|
|
rpm-build |
0fba15 |
i++;
|
|
rpm-build |
0fba15 |
} else if(V[I[i]+h]==x) {
|
|
rpm-build |
0fba15 |
tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
|
|
rpm-build |
0fba15 |
j++;
|
|
rpm-build |
0fba15 |
} else {
|
|
rpm-build |
0fba15 |
tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
|
|
rpm-build |
0fba15 |
k++;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
while(jj+j
|
|
rpm-build |
0fba15 |
if(V[I[jj+j]+h]==x) {
|
|
rpm-build |
0fba15 |
j++;
|
|
rpm-build |
0fba15 |
} else {
|
|
rpm-build |
0fba15 |
tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
|
|
rpm-build |
0fba15 |
k++;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(jj>start) split(I,V,start,jj-start,h);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
if(jj==kk-1) I[jj]=-1;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(start+len>kk) split(I,V,kk,start+len-kk,h);
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static void qsufsort(int64_t *I,int64_t *V,const uint8_t *old,int64_t oldsize)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int64_t buckets[256];
|
|
rpm-build |
0fba15 |
int64_t i,h,len;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for(i=0;i<256;i++) buckets[i]=0;
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
|
|
rpm-build |
0fba15 |
for(i=255;i>0;i--) buckets[i]=buckets[i-1];
|
|
rpm-build |
0fba15 |
buckets[0]=0;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
I[0]=oldsize;
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
V[oldsize]=0;
|
|
rpm-build |
0fba15 |
for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
|
|
rpm-build |
0fba15 |
I[0]=-1;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for(h=1;I[0]!=-(oldsize+1);h+=h) {
|
|
rpm-build |
0fba15 |
len=0;
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
if(I[i]<0) {
|
|
rpm-build |
0fba15 |
len-=I[i];
|
|
rpm-build |
0fba15 |
i-=I[i];
|
|
rpm-build |
0fba15 |
} else {
|
|
rpm-build |
0fba15 |
if(len) I[i-len]=-len;
|
|
rpm-build |
0fba15 |
len=V[I[i]]+1-i;
|
|
rpm-build |
0fba15 |
split(I,V,i,len,h);
|
|
rpm-build |
0fba15 |
i+=len;
|
|
rpm-build |
0fba15 |
len=0;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
if(len) I[i-len]=-len;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static int64_t matchlen(const uint8_t *old,int64_t oldsize,const uint8_t *new,int64_t newsize)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int64_t i;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for(i=0;(i
|
|
rpm-build |
0fba15 |
if(old[i]!=new[i]) break;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return i;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static int64_t search(const int64_t *I,const uint8_t *old,int64_t oldsize,
|
|
rpm-build |
0fba15 |
const uint8_t *new,int64_t newsize,int64_t st,int64_t en,int64_t *pos)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int64_t x,y;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(en-st<2) {
|
|
rpm-build |
0fba15 |
x=matchlen(old+I[st],oldsize-I[st],new,newsize);
|
|
rpm-build |
0fba15 |
y=matchlen(old+I[en],oldsize-I[en],new,newsize);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(x>y) {
|
|
rpm-build |
0fba15 |
*pos=I[st];
|
|
rpm-build |
0fba15 |
return x;
|
|
rpm-build |
0fba15 |
} else {
|
|
rpm-build |
0fba15 |
*pos=I[en];
|
|
rpm-build |
0fba15 |
return y;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
x=st+(en-st)/2;
|
|
rpm-build |
0fba15 |
if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) {
|
|
rpm-build |
0fba15 |
return search(I,old,oldsize,new,newsize,x,en,pos);
|
|
rpm-build |
0fba15 |
} else {
|
|
rpm-build |
0fba15 |
return search(I,old,oldsize,new,newsize,st,x,pos);
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static void offtout(int64_t x,uint8_t *buf)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int64_t y;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(x<0) y=-x; else y=x;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
buf[0]=y%256;y-=buf[0];
|
|
rpm-build |
0fba15 |
y=y/256;buf[1]=y%256;y-=buf[1];
|
|
rpm-build |
0fba15 |
y=y/256;buf[2]=y%256;y-=buf[2];
|
|
rpm-build |
0fba15 |
y=y/256;buf[3]=y%256;y-=buf[3];
|
|
rpm-build |
0fba15 |
y=y/256;buf[4]=y%256;y-=buf[4];
|
|
rpm-build |
0fba15 |
y=y/256;buf[5]=y%256;y-=buf[5];
|
|
rpm-build |
0fba15 |
y=y/256;buf[6]=y%256;y-=buf[6];
|
|
rpm-build |
0fba15 |
y=y/256;buf[7]=y%256;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(x<0) buf[7]|=0x80;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static int64_t writedata(struct bsdiff_stream* stream, const void* buffer, int64_t length)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int64_t result = 0;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
while (length > 0)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
const int smallsize = (int)MIN(length, INT_MAX);
|
|
rpm-build |
0fba15 |
const int writeresult = stream->write(stream, buffer, smallsize);
|
|
rpm-build |
0fba15 |
if (writeresult == -1)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
return -1;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
result += writeresult;
|
|
rpm-build |
0fba15 |
length -= smallsize;
|
|
rpm-build |
0fba15 |
buffer = (uint8_t*)buffer + smallsize;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return result;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
struct bsdiff_request
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
const uint8_t* old;
|
|
rpm-build |
0fba15 |
int64_t oldsize;
|
|
rpm-build |
0fba15 |
const uint8_t* new;
|
|
rpm-build |
0fba15 |
int64_t newsize;
|
|
rpm-build |
0fba15 |
struct bsdiff_stream* stream;
|
|
rpm-build |
0fba15 |
int64_t *I;
|
|
rpm-build |
0fba15 |
uint8_t *buffer;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static int bsdiff_internal(const struct bsdiff_request req)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int64_t *I,*V;
|
|
rpm-build |
0fba15 |
int64_t scan,pos,len;
|
|
rpm-build |
0fba15 |
int64_t lastscan,lastpos,lastoffset;
|
|
rpm-build |
0fba15 |
int64_t oldscore,scsc;
|
|
rpm-build |
0fba15 |
int64_t s,Sf,lenf,Sb,lenb;
|
|
rpm-build |
0fba15 |
int64_t overlap,Ss,lens;
|
|
rpm-build |
0fba15 |
int64_t i;
|
|
rpm-build |
0fba15 |
uint8_t *buffer;
|
|
rpm-build |
0fba15 |
uint8_t buf[8 * 3];
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if((V=req.stream->malloc((req.oldsize+1)*sizeof(int64_t)))==NULL) return -1;
|
|
rpm-build |
0fba15 |
I = req.I;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
qsufsort(I,V,req.old,req.oldsize);
|
|
rpm-build |
0fba15 |
req.stream->free(V);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
buffer = req.buffer;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Compute the differences, writing ctrl as we go */
|
|
rpm-build |
0fba15 |
scan=0;len=0;pos=0;
|
|
rpm-build |
0fba15 |
lastscan=0;lastpos=0;lastoffset=0;
|
|
rpm-build |
0fba15 |
while(scan
|
|
rpm-build |
0fba15 |
oldscore=0;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for(scsc=scan+=len;scan
|
|
rpm-build |
0fba15 |
len=search(I,req.old,req.oldsize,req.new+scan,req.newsize-scan,
|
|
rpm-build |
0fba15 |
0,req.oldsize,&pos;;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for(;scsc
|
|
rpm-build |
0fba15 |
if((scsc+lastoffset
|
|
rpm-build |
0fba15 |
(req.old[scsc+lastoffset] == req.new[scsc]))
|
|
rpm-build |
0fba15 |
oldscore++;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(((len==oldscore) && (len!=0)) ||
|
|
rpm-build |
0fba15 |
(len>oldscore+8)) break;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if((scan+lastoffset
|
|
rpm-build |
0fba15 |
(req.old[scan+lastoffset] == req.new[scan]))
|
|
rpm-build |
0fba15 |
oldscore--;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if((len!=oldscore) || (scan==req.newsize)) {
|
|
rpm-build |
0fba15 |
s=0;Sf=0;lenf=0;
|
|
rpm-build |
0fba15 |
for(i=0;(lastscan+i
|
|
rpm-build |
0fba15 |
if(req.old[lastpos+i]==req.new[lastscan+i]) s++;
|
|
rpm-build |
0fba15 |
i++;
|
|
rpm-build |
0fba15 |
if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
lenb=0;
|
|
rpm-build |
0fba15 |
if(scan
|
|
rpm-build |
0fba15 |
s=0;Sb=0;
|
|
rpm-build |
0fba15 |
for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
|
|
rpm-build |
0fba15 |
if(req.old[pos-i]==req.new[scan-i]) s++;
|
|
rpm-build |
0fba15 |
if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(lastscan+lenf>scan-lenb) {
|
|
rpm-build |
0fba15 |
overlap=(lastscan+lenf)-(scan-lenb);
|
|
rpm-build |
0fba15 |
s=0;Ss=0;lens=0;
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
if(req.new[lastscan+lenf-overlap+i]==
|
|
rpm-build |
0fba15 |
req.old[lastpos+lenf-overlap+i]) s++;
|
|
rpm-build |
0fba15 |
if(req.new[scan-lenb+i]==
|
|
rpm-build |
0fba15 |
req.old[pos-lenb+i]) s--;
|
|
rpm-build |
0fba15 |
if(s>Ss) { Ss=s; lens=i+1; };
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
lenf+=lens-overlap;
|
|
rpm-build |
0fba15 |
lenb-=lens;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
offtout(lenf,buf);
|
|
rpm-build |
0fba15 |
offtout((scan-lenb)-(lastscan+lenf),buf+8);
|
|
rpm-build |
0fba15 |
offtout((pos-lenb)-(lastpos+lenf),buf+16);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Write control data */
|
|
rpm-build |
0fba15 |
if (writedata(req.stream, buf, sizeof(buf)))
|
|
rpm-build |
0fba15 |
return -1;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Write diff data */
|
|
rpm-build |
0fba15 |
for(i=0;i
|
|
rpm-build |
0fba15 |
buffer[i]=req.new[lastscan+i]-req.old[lastpos+i];
|
|
rpm-build |
0fba15 |
if (writedata(req.stream, buffer, lenf))
|
|
rpm-build |
0fba15 |
return -1;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Write extra data */
|
|
rpm-build |
0fba15 |
for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
|
|
rpm-build |
0fba15 |
buffer[i]=req.new[lastscan+lenf+i];
|
|
rpm-build |
0fba15 |
if (writedata(req.stream, buffer, (scan-lenb)-(lastscan+lenf)))
|
|
rpm-build |
0fba15 |
return -1;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
lastscan=scan-lenb;
|
|
rpm-build |
0fba15 |
lastpos=pos-lenb;
|
|
rpm-build |
0fba15 |
lastoffset=pos-scan;
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return 0;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t newsize, struct bsdiff_stream* stream)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int result;
|
|
rpm-build |
0fba15 |
struct bsdiff_request req;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if((req.I=stream->malloc((oldsize+1)*sizeof(int64_t)))==NULL)
|
|
rpm-build |
0fba15 |
return -1;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if((req.buffer=stream->malloc(newsize+1))==NULL)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
stream->free(req.I);
|
|
rpm-build |
0fba15 |
return -1;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
req.old = old;
|
|
rpm-build |
0fba15 |
req.oldsize = oldsize;
|
|
rpm-build |
0fba15 |
req.new = new;
|
|
rpm-build |
0fba15 |
req.newsize = newsize;
|
|
rpm-build |
0fba15 |
req.stream = stream;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
result = bsdiff_internal(req);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
stream->free(req.buffer);
|
|
rpm-build |
0fba15 |
stream->free(req.I);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return result;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#if defined(BSDIFF_EXECUTABLE)
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include <sys/types.h>
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include <bzlib.h>
|
|
rpm-build |
0fba15 |
#include <err.h>
|
|
rpm-build |
0fba15 |
#include <fcntl.h>
|
|
rpm-build |
0fba15 |
#include <stdio.h>
|
|
rpm-build |
0fba15 |
#include <stdlib.h>
|
|
rpm-build |
0fba15 |
#include <unistd.h>
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
static int bz2_write(struct bsdiff_stream* stream, const void* buffer, int size)
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int bz2err;
|
|
rpm-build |
0fba15 |
BZFILE* bz2;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
bz2 = (BZFILE*)stream->opaque;
|
|
rpm-build |
0fba15 |
BZ2_bzWrite(&bz2err, bz2, (void*)buffer, size);
|
|
rpm-build |
0fba15 |
if (bz2err != BZ_STREAM_END && bz2err != BZ_OK)
|
|
rpm-build |
0fba15 |
return -1;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return 0;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
int main(int argc,char *argv[])
|
|
rpm-build |
0fba15 |
{
|
|
rpm-build |
0fba15 |
int fd;
|
|
rpm-build |
0fba15 |
int bz2err;
|
|
rpm-build |
0fba15 |
uint8_t *old,*new;
|
|
rpm-build |
0fba15 |
off_t oldsize,newsize;
|
|
rpm-build |
0fba15 |
uint8_t buf[8];
|
|
rpm-build |
0fba15 |
FILE * pf;
|
|
rpm-build |
0fba15 |
struct bsdiff_stream stream;
|
|
rpm-build |
0fba15 |
BZFILE* bz2;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
memset(&bz2, 0, sizeof(bz2));
|
|
rpm-build |
0fba15 |
stream.malloc = malloc;
|
|
rpm-build |
0fba15 |
stream.free = free;
|
|
rpm-build |
0fba15 |
stream.write = bz2_write;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
|
|
rpm-build |
0fba15 |
that we never try to malloc(0) and get a NULL pointer */
|
|
rpm-build |
0fba15 |
if(((fd=open(argv[1],O_RDONLY,0))<0) ||
|
|
rpm-build |
0fba15 |
((oldsize=lseek(fd,0,SEEK_END))==-1) ||
|
|
rpm-build |
0fba15 |
((old=malloc(oldsize+1))==NULL) ||
|
|
rpm-build |
0fba15 |
(lseek(fd,0,SEEK_SET)!=0) ||
|
|
rpm-build |
0fba15 |
(read(fd,old,oldsize)!=oldsize) ||
|
|
rpm-build |
0fba15 |
(close(fd)==-1)) err(1,"%s",argv[1]);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Allocate newsize+1 bytes instead of newsize bytes to ensure
|
|
rpm-build |
0fba15 |
that we never try to malloc(0) and get a NULL pointer */
|
|
rpm-build |
0fba15 |
if(((fd=open(argv[2],O_RDONLY,0))<0) ||
|
|
rpm-build |
0fba15 |
((newsize=lseek(fd,0,SEEK_END))==-1) ||
|
|
rpm-build |
0fba15 |
((new=malloc(newsize+1))==NULL) ||
|
|
rpm-build |
0fba15 |
(lseek(fd,0,SEEK_SET)!=0) ||
|
|
rpm-build |
0fba15 |
(read(fd,new,newsize)!=newsize) ||
|
|
rpm-build |
0fba15 |
(close(fd)==-1)) err(1,"%s",argv[2]);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Create the patch file */
|
|
rpm-build |
0fba15 |
if ((pf = fopen(argv[3], "w")) == NULL)
|
|
rpm-build |
0fba15 |
err(1, "%s", argv[3]);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Write header (signature+newsize)*/
|
|
rpm-build |
0fba15 |
offtout(newsize, buf);
|
|
rpm-build |
0fba15 |
if (fwrite("ENDSLEY/BSDIFF43", 16, 1, pf) != 1 ||
|
|
rpm-build |
0fba15 |
fwrite(buf, sizeof(buf), 1, pf) != 1)
|
|
rpm-build |
0fba15 |
err(1, "Failed to write header");
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (NULL == (bz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)))
|
|
rpm-build |
0fba15 |
errx(1, "BZ2_bzWriteOpen, bz2err=%d", bz2err);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
stream.opaque = bz2;
|
|
rpm-build |
0fba15 |
if (bsdiff(old, oldsize, new, newsize, &stream))
|
|
rpm-build |
0fba15 |
err(1, "bsdiff");
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
BZ2_bzWriteClose(&bz2err, bz2, 0, NULL, NULL);
|
|
rpm-build |
0fba15 |
if (bz2err != BZ_OK)
|
|
rpm-build |
0fba15 |
err(1, "BZ2_bzWriteClose, bz2err=%d", bz2err);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (fclose(pf))
|
|
rpm-build |
0fba15 |
err(1, "fclose");
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Free the memory we used */
|
|
rpm-build |
0fba15 |
free(old);
|
|
rpm-build |
0fba15 |
free(new);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
return 0;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#endif
|