bsdiff-portable

A more portable version of Colin Percival's bsdiff
git clone git://git.sgregoratto.me/bsdiff-portable
Log | Files | Refs | LICENSE

commit fe637e413d68c4ccdb0dc0f11a52e7a6d90e19d0
parent 7438b720cf491848fb724fdfaaea63fd8f54ecf9
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Wed, 23 Sep 2020 16:47:18 +1000

KNF sources

Diffstat:
A.clang-format | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mbsdiff.c | 455+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mbspatch.c | 132+++++++++++++++++++++++++++++++++++++++++++------------------------------------
3 files changed, 401 insertions(+), 244 deletions(-)

diff --git a/.clang-format b/.clang-format @@ -0,0 +1,58 @@ +AlignTrailingComments: true +AlwaysBreakAfterReturnType: All +AlignConsecutiveMacros: true +#AlignConsecutiveLists: true +#BitFieldDeclarationsOnePerLine: true +BreakBeforeBraces: Mozilla +ColumnLimit: 80 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: false +FixNamespaceComments: true +IndentCaseLabels: false +IndentWidth: 8 +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^<sys/param\.h>' + Priority: 1 + SortPriority: 0 + - Regex: '^<sys/types\.h>' + Priority: 1 + SortPriority: 1 + - Regex: '^<sys.*/' + Priority: 1 + SortPriority: 2 + - Regex: '^<uvm/' + Priority: 2 + SortPriority: 3 + - Regex: '^<machine/' + Priority: 3 + SortPriority: 4 + - Regex: '^<dev/' + Priority: 4 + SortPriority: 5 + - Regex: '^<net.*/' + Priority: 5 + SortPriority: 6 + - Regex: '^<protocols/' + Priority: 5 + SortPriority: 7 + - Regex: '^<(fs|miscfs|msdosfs|nfs|ufs)/' + Priority: 6 + SortPriority: 8 + - Regex: '^<(x86|amd64|i386|xen)/' + Priority: 7 + SortPriority: 8 + - Regex: '^<path' + Priority: 9 + SortPriority: 11 + - Regex: '^<[^/].*\.h' + Priority: 8 + SortPriority: 10 + - Regex: '^\".*\.h\"' + Priority: 10 + SortPriority: 12 +SortIncludes: true +#SpacesBeforeCpp11BracedList: true +SpacesBeforeTrailingComments: 4 +TabWidth: 8 +UseTab: Always diff --git a/bsdiff.c b/bsdiff.c @@ -3,7 +3,7 @@ * All rights reserved * * Redistribution and use in source and binary forms, with or without - * modification, are permitted providing that the following conditions + * modification, are permitted providing that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. @@ -41,212 +41,272 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 #include <string.h> #include <unistd.h> -#define MIN(x,y) (((x)<(y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) -static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h) +static void +split(off_t *I, off_t *V, off_t start, off_t len, off_t h) { - off_t i,j,k,x,tmp,jj,kk; - - if(len<16) { - for(k=start;k<start+len;k+=j) { - j=1;x=V[I[k]+h]; - for(i=1;k+i<start+len;i++) { - if(V[I[k+i]+h]<x) { - x=V[I[k+i]+h]; - j=0; + off_t i, j, k, x, tmp, jj, kk; + + if (len < 16) { + for (k = start; k < start + len; k += j) { + j = 1; + x = V[I[k] + h]; + for (i = 1; k + i < start + len; i++) { + if (V[I[k + i] + h] < x) { + x = V[I[k + i] + h]; + j = 0; }; - if(V[I[k+i]+h]==x) { - tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp; + if (V[I[k + i] + h] == x) { + tmp = I[k + j]; + I[k + j] = I[k + i]; + I[k + i] = tmp; j++; }; }; - for(i=0;i<j;i++) V[I[k+i]]=k+j-1; - if(j==1) I[k]=-1; + for (i = 0; i < j; i++) + V[I[k + i]] = k + j - 1; + if (j == 1) + I[k] = -1; }; return; }; - x=V[I[start+len/2]+h]; - jj=0;kk=0; - for(i=start;i<start+len;i++) { - if(V[I[i]+h]<x) jj++; - if(V[I[i]+h]==x) kk++; + x = V[I[start + len / 2] + h]; + jj = 0; + kk = 0; + for (i = start; i < start + len; i++) { + if (V[I[i] + h] < x) + jj++; + if (V[I[i] + h] == x) + kk++; }; - jj+=start;kk+=jj; - - i=start;j=0;k=0; - while(i<jj) { - if(V[I[i]+h]<x) { + jj += start; + kk += jj; + + i = start; + j = 0; + k = 0; + while (i < jj) { + if (V[I[i] + h] < x) { i++; - } else if(V[I[i]+h]==x) { - tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp; + } else if (V[I[i] + h] == x) { + tmp = I[i]; + I[i] = I[jj + j]; + I[jj + j] = tmp; j++; } else { - tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp; + tmp = I[i]; + I[i] = I[kk + k]; + I[kk + k] = tmp; k++; }; }; - while(jj+j<kk) { - if(V[I[jj+j]+h]==x) { + while (jj + j < kk) { + if (V[I[jj + j] + h] == x) { j++; } else { - tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp; + tmp = I[jj + j]; + I[jj + j] = I[kk + k]; + I[kk + k] = tmp; k++; }; }; - if(jj>start) split(I,V,start,jj-start,h); + if (jj > start) + split(I, V, start, jj - start, h); - for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1; - if(jj==kk-1) I[jj]=-1; + for (i = 0; i < kk - jj; i++) + V[I[jj + i]] = kk - 1; + if (jj == kk - 1) + I[jj] = -1; - if(start+len>kk) split(I,V,kk,start+len-kk,h); + if (start + len > kk) + split(I, V, kk, start + len - kk, h); } -static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize) +static void +qsufsort(off_t *I, off_t *V, u_char *old, off_t oldsize) { off_t buckets[256]; - off_t i,h,len; - - for(i=0;i<256;i++) buckets[i]=0; - for(i=0;i<oldsize;i++) buckets[old[i]]++; - for(i=1;i<256;i++) buckets[i]+=buckets[i-1]; - for(i=255;i>0;i--) buckets[i]=buckets[i-1]; - buckets[0]=0; - - for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i; - I[0]=oldsize; - for(i=0;i<oldsize;i++) V[i]=buckets[old[i]]; - V[oldsize]=0; - for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1; - I[0]=-1; - - for(h=1;I[0]!=-(oldsize+1);h+=h) { - len=0; - for(i=0;i<oldsize+1;) { - if(I[i]<0) { - len-=I[i]; - i-=I[i]; + off_t i, h, len; + + for (i = 0; i < 256; i++) + buckets[i] = 0; + for (i = 0; i < oldsize; i++) + buckets[old[i]]++; + for (i = 1; i < 256; i++) + buckets[i] += buckets[i - 1]; + for (i = 255; i > 0; i--) + buckets[i] = buckets[i - 1]; + buckets[0] = 0; + + for (i = 0; i < oldsize; i++) + I[++buckets[old[i]]] = i; + I[0] = oldsize; + for (i = 0; i < oldsize; i++) + V[i] = buckets[old[i]]; + V[oldsize] = 0; + for (i = 1; i < 256; i++) + if (buckets[i] == buckets[i - 1] + 1) + I[buckets[i]] = -1; + I[0] = -1; + + for (h = 1; I[0] != -(oldsize + 1); h += h) { + len = 0; + for (i = 0; i < oldsize + 1;) { + if (I[i] < 0) { + len -= I[i]; + i -= I[i]; } else { - if(len) I[i-len]=-len; - len=V[I[i]]+1-i; - split(I,V,i,len,h); - i+=len; - len=0; + if (len) + I[i - len] = -len; + len = V[I[i]] + 1 - i; + split(I, V, i, len, h); + i += len; + len = 0; }; }; - if(len) I[i-len]=-len; + if (len) + I[i - len] = -len; }; - for(i=0;i<oldsize+1;i++) I[V[i]]=i; + for (i = 0; i < oldsize + 1; i++) + I[V[i]] = i; } -static off_t matchlen(u_char *old,off_t oldsize,u_char *new,off_t newsize) +static off_t +matchlen(u_char *old, off_t oldsize, u_char *new, off_t newsize) { off_t i; - for(i=0;(i<oldsize)&&(i<newsize);i++) - if(old[i]!=new[i]) break; + for (i = 0; (i < oldsize) && (i < newsize); i++) + if (old[i] != new[i]) + break; return i; } -static off_t search(off_t *I,u_char *old,off_t oldsize, - u_char *new,off_t newsize,off_t st,off_t en,off_t *pos) +static off_t +search(off_t *I, u_char *old, off_t oldsize, u_char *new, off_t newsize, + off_t st, off_t en, off_t *pos) { - off_t x,y; + off_t x, y; - if(en-st<2) { - x=matchlen(old+I[st],oldsize-I[st],new,newsize); - y=matchlen(old+I[en],oldsize-I[en],new,newsize); + if (en - st < 2) { + x = matchlen(old + I[st], oldsize - I[st], new, newsize); + y = matchlen(old + I[en], oldsize - I[en], new, newsize); - if(x>y) { - *pos=I[st]; + if (x > y) { + *pos = I[st]; return x; } else { - *pos=I[en]; + *pos = I[en]; return y; } }; - x=st+(en-st)/2; - if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) { - return search(I,old,oldsize,new,newsize,x,en,pos); + x = st + (en - st) / 2; + if (memcmp(old + I[x], new, MIN(oldsize - I[x], newsize)) < 0) { + return search(I, old, oldsize, new, newsize, x, en, pos); } else { - return search(I,old,oldsize,new,newsize,st,x,pos); + return search(I, old, oldsize, new, newsize, st, x, pos); }; } -static void offtout(off_t x,u_char *buf) +static void +offtout(off_t x, u_char *buf) { off_t y; - if(x<0) y=-x; else y=x; - - buf[0]=y%256;y-=buf[0]; - y=y/256;buf[1]=y%256;y-=buf[1]; - y=y/256;buf[2]=y%256;y-=buf[2]; - y=y/256;buf[3]=y%256;y-=buf[3]; - y=y/256;buf[4]=y%256;y-=buf[4]; - y=y/256;buf[5]=y%256;y-=buf[5]; - y=y/256;buf[6]=y%256;y-=buf[6]; - y=y/256;buf[7]=y%256; - - if(x<0) buf[7]|=0x80; + if (x < 0) + y = -x; + else + y = x; + + buf[0] = y % 256; + y -= buf[0]; + y = y / 256; + buf[1] = y % 256; + y -= buf[1]; + y = y / 256; + buf[2] = y % 256; + y -= buf[2]; + y = y / 256; + buf[3] = y % 256; + y -= buf[3]; + y = y / 256; + buf[4] = y % 256; + y -= buf[4]; + y = y / 256; + buf[5] = y % 256; + y -= buf[5]; + y = y / 256; + buf[6] = y % 256; + y -= buf[6]; + y = y / 256; + buf[7] = y % 256; + + if (x < 0) + buf[7] |= 0x80; } -int main(int argc,char *argv[]) +int +main(int argc, char *argv[]) { int fd; - u_char *old,*new; - off_t oldsize,newsize; - off_t *I,*V; - off_t scan,pos,len; - off_t lastscan,lastpos,lastoffset; - off_t oldscore,scsc; - off_t s,Sf,lenf,Sb,lenb; - off_t overlap,Ss,lens; + u_char *old, *new; + off_t oldsize, newsize; + off_t *I, *V; + off_t scan, pos, len; + off_t lastscan, lastpos, lastoffset; + off_t oldscore, scsc; + off_t s, Sf, lenf, Sb, lenb; + off_t overlap, Ss, lens; off_t i; - off_t dblen,eblen; - u_char *db,*eb; + off_t dblen, eblen; + u_char *db, *eb; u_char buf[8]; u_char header[32]; - FILE * pf; - BZFILE * pfbz2; + FILE *pf; + BZFILE *pfbz2; int bz2err; - if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); + if (argc != 4) + errx(1, "usage: %s oldfile newfile patchfile\n", argv[0]); /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ - if(((fd=open(argv[1],O_RDONLY,0))<0) || - ((oldsize=lseek(fd,0,SEEK_END))==-1) || - ((old=malloc(oldsize+1))==NULL) || - (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,old,oldsize)!=oldsize) || - (close(fd)==-1)) err(1,"%s",argv[1]); + if (((fd = open(argv[1], O_RDONLY, 0)) < 0) || + ((oldsize = lseek(fd, 0, SEEK_END)) == -1) || + ((old = malloc(oldsize + 1)) == NULL) || + (lseek(fd, 0, SEEK_SET) != 0) || + (read(fd, old, oldsize) != oldsize) || (close(fd) == -1)) + err(1, "%s", argv[1]); - if(((I=malloc((oldsize+1)*sizeof(off_t)))==NULL) || - ((V=malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL); + if (((I = malloc((oldsize + 1) * sizeof(off_t))) == NULL) || + ((V = malloc((oldsize + 1) * sizeof(off_t))) == NULL)) + err(1, NULL); - qsufsort(I,V,old,oldsize); + qsufsort(I, V, old, oldsize); free(V); /* Allocate newsize+1 bytes instead of newsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ - if(((fd=open(argv[2],O_RDONLY,0))<0) || - ((newsize=lseek(fd,0,SEEK_END))==-1) || - ((new=malloc(newsize+1))==NULL) || - (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,new,newsize)!=newsize) || - (close(fd)==-1)) err(1,"%s",argv[2]); - - if(((db=malloc(newsize+1))==NULL) || - ((eb=malloc(newsize+1))==NULL)) err(1,NULL); - dblen=0; - eblen=0; + if (((fd = open(argv[2], O_RDONLY, 0)) < 0) || + ((newsize = lseek(fd, 0, SEEK_END)) == -1) || + ((new = malloc(newsize + 1)) == NULL) || + (lseek(fd, 0, SEEK_SET) != 0) || + (read(fd, new, newsize) != newsize) || (close(fd) == -1)) + err(1, "%s", argv[2]); + + if (((db = malloc(newsize + 1)) == NULL) || + ((eb = malloc(newsize + 1)) == NULL)) + err(1, NULL); + dblen = 0; + eblen = 0; /* Create the patch file */ if ((pf = fopen(argv[3], "w")) == NULL) @@ -262,7 +322,7 @@ int main(int argc,char *argv[]) 32 ?? Bzip2ed ctrl block ?? ?? Bzip2ed diff block ?? ?? Bzip2ed extra block */ - memcpy(header,"BSDIFF40",8); + memcpy(header, "BSDIFF40", 8); offtout(0, header + 8); offtout(0, header + 16); offtout(newsize, header + 24); @@ -272,86 +332,113 @@ int main(int argc,char *argv[]) /* Compute the differences, writing ctrl as we go */ if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); - scan=0;len=0; - lastscan=0;lastpos=0;lastoffset=0; - while(scan<newsize) { - oldscore=0; - - for(scsc=scan+=len;scan<newsize;scan++) { - len=search(I,old,oldsize,new+scan,newsize-scan, - 0,oldsize,&pos); - - for(;scsc<scan+len;scsc++) - if((scsc+lastoffset<oldsize) && - (old[scsc+lastoffset] == new[scsc])) - oldscore++; - - if(((len==oldscore) && (len!=0)) || - (len>oldscore+8)) break; - - if((scan+lastoffset<oldsize) && - (old[scan+lastoffset] == new[scan])) + scan = 0; + len = 0; + lastscan = 0; + lastpos = 0; + lastoffset = 0; + while (scan < newsize) { + oldscore = 0; + + for (scsc = scan += len; scan < newsize; scan++) { + len = search(I, old, oldsize, new + scan, + newsize - scan, 0, oldsize, &pos); + + for (; scsc < scan + len; scsc++) + if ((scsc + lastoffset < oldsize) && + (old[scsc + lastoffset] == new[scsc])) + oldscore++; + + if (((len == oldscore) && (len != 0)) || + (len > oldscore + 8)) + break; + + if ((scan + lastoffset < oldsize) && + (old[scan + lastoffset] == new[scan])) oldscore--; - }; + } - if((len!=oldscore) || (scan==newsize)) { - s=0;Sf=0;lenf=0; - for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) { - if(old[lastpos+i]==new[lastscan+i]) s++; + if ((len != oldscore) || (scan == newsize)) { + s = 0; + Sf = 0; + lenf = 0; + for (i = 0; (lastscan + i < scan) && + (lastpos + i < oldsize);) { + if (old[lastpos + i] == new[lastscan + i]) + s++; i++; - if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; }; + if (s * 2 - i > Sf * 2 - lenf) { + Sf = s; + lenf = i; + }; }; - lenb=0; - if(scan<newsize) { - s=0;Sb=0; - for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) { - if(old[pos-i]==new[scan-i]) s++; - if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; + lenb = 0; + if (scan < newsize) { + s = 0; + Sb = 0; + for (i = 1; + (scan >= lastscan + i) && (pos >= i); + i++) { + if (old[pos - i] == new[scan - i]) + s++; + if (s * 2 - i > Sb * 2 - lenb) { + Sb = s; + lenb = i; + }; }; }; - if(lastscan+lenf>scan-lenb) { - overlap=(lastscan+lenf)-(scan-lenb); - s=0;Ss=0;lens=0; - for(i=0;i<overlap;i++) { - if(new[lastscan+lenf-overlap+i]== - old[lastpos+lenf-overlap+i]) s++; - if(new[scan-lenb+i]== - old[pos-lenb+i]) s--; - if(s>Ss) { Ss=s; lens=i+1; }; + if (lastscan + lenf > scan - lenb) { + overlap = (lastscan + lenf) - (scan - lenb); + s = 0; + Ss = 0; + lens = 0; + for (i = 0; i < overlap; i++) { + if (new[lastscan + lenf - overlap + + i] == + old[lastpos + lenf - overlap + i]) + s++; + if (new[scan - lenb + i] == + old[pos - lenb + i]) + s--; + if (s > Ss) { + Ss = s; + lens = i + 1; + }; }; - lenf+=lens-overlap; - lenb-=lens; + lenf += lens - overlap; + lenb -= lens; }; - for(i=0;i<lenf;i++) - db[dblen+i]=new[lastscan+i]-old[lastpos+i]; - for(i=0;i<(scan-lenb)-(lastscan+lenf);i++) - eb[eblen+i]=new[lastscan+lenf+i]; + for (i = 0; i < lenf; i++) + db[dblen + i] = + new[lastscan + i] - old[lastpos + i]; + for (i = 0; i < (scan - lenb) - (lastscan + lenf); i++) + eb[eblen + i] = new[lastscan + lenf + i]; - dblen+=lenf; - eblen+=(scan-lenb)-(lastscan+lenf); + dblen += lenf; + eblen += (scan - lenb) - (lastscan + lenf); - offtout(lenf,buf); + offtout(lenf, buf); BZ2_bzWrite(&bz2err, pfbz2, buf, 8); if (bz2err != BZ_OK) errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); - offtout((scan-lenb)-(lastscan+lenf),buf); + offtout((scan - lenb) - (lastscan + lenf), buf); BZ2_bzWrite(&bz2err, pfbz2, buf, 8); if (bz2err != BZ_OK) errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); - offtout((pos-lenb)-(lastpos+lenf),buf); + offtout((pos - lenb) - (lastpos + lenf), buf); BZ2_bzWrite(&bz2err, pfbz2, buf, 8); if (bz2err != BZ_OK) errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); - lastscan=scan-lenb; - lastpos=pos-lenb; - lastoffset=pos-scan; + lastscan = scan - lenb; + lastpos = pos - lenb; + lastoffset = pos - scan; }; }; BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); @@ -361,7 +448,7 @@ int main(int argc,char *argv[]) /* Compute size of compressed ctrl data */ if ((len = ftello(pf)) == -1) err(1, "ftello"); - offtout(len-32, header + 8); + offtout(len - 32, header + 8); /* Write compressed diff data */ if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) diff --git a/bspatch.c b/bspatch.c @@ -3,7 +3,7 @@ * All rights reserved * * Redistribution and use in source and binary forms, with or without - * modification, are permitted providing that the following conditions + * modification, are permitted providing that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. @@ -30,49 +30,60 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59: #include "config.h" #include <bzlib.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> #if HAVE_ERR #include <err.h> #endif -#include <unistd.h> #include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> -static off_t offtin(u_char *buf) +static off_t +offtin(u_char *buf) { off_t y; - y=buf[7]&0x7F; - y=y*256;y+=buf[6]; - y=y*256;y+=buf[5]; - y=y*256;y+=buf[4]; - y=y*256;y+=buf[3]; - y=y*256;y+=buf[2]; - y=y*256;y+=buf[1]; - y=y*256;y+=buf[0]; - - if(buf[7]&0x80) y=-y; + y = buf[7] & 0x7F; + y = y * 256; + y += buf[6]; + y = y * 256; + y += buf[5]; + y = y * 256; + y += buf[4]; + y = y * 256; + y += buf[3]; + y = y * 256; + y += buf[2]; + y = y * 256; + y += buf[1]; + y = y * 256; + y += buf[0]; + + if (buf[7] & 0x80) + y = -y; return y; } -int main(int argc,char * argv[]) +int +main(int argc, char *argv[]) { - FILE * f, * cpf, * dpf, * epf; - BZFILE * cpfbz2, * dpfbz2, * epfbz2; + FILE *f, *cpf, *dpf, *epf; + BZFILE *cpfbz2, *dpfbz2, *epfbz2; int cbz2err, dbz2err, ebz2err; int fd; - ssize_t oldsize,newsize; - ssize_t bzctrllen,bzdatalen; - u_char header[32],buf[8]; + ssize_t oldsize, newsize; + ssize_t bzctrllen, bzdatalen; + u_char header[32], buf[8]; u_char *old, *new; - off_t oldpos,newpos; + off_t oldpos, newpos; off_t ctrl[3]; off_t lenread; off_t i; - if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); + if (argc != 4) + errx(1, "usage: %s oldfile newfile patchfile\n", argv[0]); /* Open patch file */ if ((f = fopen(argv[3], "r")) == NULL) @@ -104,11 +115,11 @@ int main(int argc,char * argv[]) errx(1, "Corrupt patch\n"); /* Read lengths from header */ - bzctrllen=offtin(header+8); - bzdatalen=offtin(header+16); - newsize=offtin(header+24); - if((bzctrllen<0) || (bzdatalen<0) || (newsize<0)) - errx(1,"Corrupt patch\n"); + bzctrllen = offtin(header + 8); + bzdatalen = offtin(header + 16); + newsize = offtin(header + 24); + if ((bzctrllen < 0) || (bzdatalen < 0) || (newsize < 0)) + errx(1, "Corrupt patch\n"); /* Close patch file and re-open it via libbzip2 at the right places */ if (fclose(f)) @@ -116,8 +127,7 @@ int main(int argc,char * argv[]) if ((cpf = fopen(argv[3], "r")) == NULL) err(1, "fopen(%s)", argv[3]); if (fseeko(cpf, 32, SEEK_SET)) - err(1, "fseeko(%s, %lld)", argv[3], - (long long)32); + err(1, "fseeko(%s, %lld)", argv[3], (long long)32); if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err); if ((dpf = fopen(argv[3], "r")) == NULL) @@ -135,32 +145,34 @@ int main(int argc,char * argv[]) if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err); - if(((fd=open(argv[1],O_RDONLY,0))<0) || - ((oldsize=lseek(fd,0,SEEK_END))==-1) || - ((old=malloc(oldsize+1))==NULL) || - (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,old,oldsize)!=oldsize) || - (close(fd)==-1)) err(1,"%s",argv[1]); - if((new=malloc(newsize+1))==NULL) err(1,NULL); - - oldpos=0;newpos=0; - while(newpos<newsize) { + if (((fd = open(argv[1], O_RDONLY, 0)) < 0) || + ((oldsize = lseek(fd, 0, SEEK_END)) == -1) || + ((old = malloc(oldsize + 1)) == NULL) || + (lseek(fd, 0, SEEK_SET) != 0) || + (read(fd, old, oldsize) != oldsize) || (close(fd) == -1)) + err(1, "%s", argv[1]); + if ((new = malloc(newsize + 1)) == NULL) + err(1, NULL); + + oldpos = 0; + newpos = 0; + while (newpos < newsize) { /* Read control data */ - for(i=0;i<=2;i++) { + for (i = 0; i <= 2; i++) { lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8); - if ((lenread < 8) || ((cbz2err != BZ_OK) && - (cbz2err != BZ_STREAM_END))) + if ((lenread < 8) || + ((cbz2err != BZ_OK) && (cbz2err != BZ_STREAM_END))) errx(1, "Corrupt patch\n"); - ctrl[i]=offtin(buf); + ctrl[i] = offtin(buf); }; /* Sanity-check */ if ((ctrl[0] < 0) || (ctrl[1] < 0)) - errx(1,"Corrupt patch\n"); + errx(1, "Corrupt patch\n"); /* Sanity-check */ - if(newpos+ctrl[0]>newsize) - errx(1,"Corrupt patch\n"); + if (newpos + ctrl[0] > newsize) + errx(1, "Corrupt patch\n"); /* Read diff string */ lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]); @@ -169,17 +181,17 @@ int main(int argc,char * argv[]) errx(1, "Corrupt patch\n"); /* Add old data to diff string */ - for(i=0;i<ctrl[0];i++) - if((oldpos+i>=0) && (oldpos+i<oldsize)) - new[newpos+i]+=old[oldpos+i]; + for (i = 0; i < ctrl[0]; i++) + if ((oldpos + i >= 0) && (oldpos + i < oldsize)) + new[newpos + i] += old[oldpos + i]; /* Adjust pointers */ - newpos+=ctrl[0]; - oldpos+=ctrl[0]; + newpos += ctrl[0]; + oldpos += ctrl[0]; /* Sanity-check */ - if(newpos+ctrl[1]>newsize) - errx(1,"Corrupt patch\n"); + if (newpos + ctrl[1] > newsize) + errx(1, "Corrupt patch\n"); /* Read extra string */ lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]); @@ -188,8 +200,8 @@ int main(int argc,char * argv[]) errx(1, "Corrupt patch\n"); /* Adjust pointers */ - newpos+=ctrl[1]; - oldpos+=ctrl[2]; + newpos += ctrl[1]; + oldpos += ctrl[2]; }; /* Clean up the bzip2 reads */ @@ -200,9 +212,9 @@ int main(int argc,char * argv[]) err(1, "fclose(%s)", argv[3]); /* Write the new file */ - if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) || - (write(fd,new,newsize)!=newsize) || (close(fd)==-1)) - err(1,"%s",argv[2]); + if (((fd = open(argv[2], O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0) || + (write(fd, new, newsize) != newsize) || (close(fd) == -1)) + err(1, "%s", argv[2]); free(new); free(old);