Fix indentation with bspatch.c

This commit is contained in:
Zorg committed 2016-05-16 22:30:48 -04:00
1 parent e485baf8b8
commit 1316707136
1 file changed
+122 -122
+122 -122
View File
@@ -46,48 +46,48 @@ typedef void* stream_t;
typedef struct
{
stream_t (*open)(FILE*);
void (*close)(stream_t);
off_t (*read)(stream_t, void*, off_t);
stream_t (*open)(FILE*);
void (*close)(stream_t);
off_t (*read)(stream_t, void*, off_t);
} io_funcs_t;
static stream_t BSDIFF40_open(FILE *f)
{
int bzerr = 0;
BZFILE *s = NULL;
int bzerr = 0;
BZFILE *s = NULL;
if ((s = BZ2_bzReadOpen(&bzerr, f, 0, 0, NULL, 0)) == NULL) {
warnx("BZ2_bzReadOpen, bz2err = %d", bzerr);
warnx("BZ2_bzReadOpen, bz2err = %d", bzerr);
}
return s;
return s;
}
static void BSDIFF40_close(stream_t s)
{
int bzerr;
BZ2_bzReadClose(&bzerr, (BZFILE*)s);
int bzerr;
BZ2_bzReadClose(&bzerr, (BZFILE*)s);
}
static off_t BSDIFF40_read(stream_t s, void *buf, off_t len)
{
int bzerr = 0, lenread = 0;
lenread = BZ2_bzRead(&bzerr, (BZFILE*)s, buf, (int)len);
int bzerr = 0, lenread = 0;
lenread = BZ2_bzRead(&bzerr, (BZFILE*)s, buf, (int)len);
if (bzerr != BZ_OK && bzerr != BZ_STREAM_END) {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
lenread = -1;
}
return lenread;
return lenread;
}
static io_funcs_t BSDIFF40_funcs = {
BSDIFF40_open,
BSDIFF40_close,
BSDIFF40_read
BSDIFF40_open,
BSDIFF40_close,
BSDIFF40_read
};
static stream_t BSDIFN40_open(FILE *f)
{
return f;
return f;
}
static void BSDIFN40_close(stream_t __unused s)
@@ -96,13 +96,13 @@ static void BSDIFN40_close(stream_t __unused s)
static off_t BSDIFN40_read(stream_t s, void *buf, off_t len)
{
return (off_t)fread(buf, 1, (size_t)len, (FILE*)s);
return (off_t)fread(buf, 1, (size_t)len, (FILE*)s);
}
static io_funcs_t BSDIFN40_funcs = {
BSDIFN40_open,
BSDIFN40_close,
BSDIFN40_read
BSDIFN40_open,
BSDIFN40_close,
BSDIFN40_read
};
@@ -112,35 +112,35 @@ typedef unsigned char u_char;
static off_t offtin(u_char *buf)
{
off_t y;
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];
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;
if(buf[7]&0x80) y=-y;
return y;
return y;
}
int bspatch(int argc,const char * const argv[])
{
FILE * f = NULL, * cpf = NULL, * dpf = NULL, * epf = NULL;
stream_t cstream = NULL, dstream = NULL, estream = NULL;
ssize_t oldsize = 0,newsize = 0;
ssize_t bzctrllen = 0,bzdatalen = 0;
FILE * f = NULL, * cpf = NULL, * dpf = NULL, * epf = NULL;
stream_t cstream = NULL, dstream = NULL, estream = NULL;
ssize_t oldsize = 0,newsize = 0;
ssize_t bzctrllen = 0,bzdatalen = 0;
u_char header[32] = {0},buf[8] = {0};
u_char *old = NULL, *new = NULL;
off_t oldpos = 0,newpos = 0;
u_char *old = NULL, *new = NULL;
off_t oldpos = 0,newpos = 0;
off_t ctrl[3] = {0};
off_t lenread = 0;
off_t i = 0;
io_funcs_t * io = NULL;
off_t lenread = 0;
off_t i = 0;
io_funcs_t * io = NULL;
int exitstatus = -1;
if(argc!=4) {
@@ -148,101 +148,101 @@ int bspatch(int argc,const char * const argv[])
goto cleanup;
}
/* Open patch file */
/* Open patch file */
if ((f = fopen(argv[3], "r")) == NULL) {
warn("fopen(%s)", argv[3]);
warn("fopen(%s)", argv[3]);
goto cleanup;
}
/*
File format:
0 8 "BSDIFF40" (bzip2) or "BSDIFN40" (raw)
8 8 X
16 8 Y
24 8 sizeof(newfile)
32 X bzip2(control block)
32+X Y bzip2(diff block)
32+X+Y ??? bzip2(extra block)
with control block a set of triples (x,y,z) meaning "add x bytes
from oldfile to x bytes from the diff block; copy y bytes from the
extra block; seek forwards in oldfile by z bytes".
*/
/*
File format:
0 8 "BSDIFF40" (bzip2) or "BSDIFN40" (raw)
8 8 X
16 8 Y
24 8 sizeof(newfile)
32 X bzip2(control block)
32+X Y bzip2(diff block)
32+X+Y ??? bzip2(extra block)
with control block a set of triples (x,y,z) meaning "add x bytes
from oldfile to x bytes from the diff block; copy y bytes from the
extra block; seek forwards in oldfile by z bytes".
*/
/* Read header */
if (fread(header, 1, 32, f) < 32) {
/* Read header */
if (fread(header, 1, 32, f) < 32) {
if (feof(f)) {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
} else {
warn("fread(%s)", argv[3]);
}
goto cleanup;
}
}
/* Check for appropriate magic */
if (memcmp(header, "BSDIFF40", 8) == 0)
io = &BSDIFF40_funcs;
else if (memcmp(header, "BSDIFN40", 8) == 0)
io = &BSDIFN40_funcs;
/* Check for appropriate magic */
if (memcmp(header, "BSDIFF40", 8) == 0)
io = &BSDIFF40_funcs;
else if (memcmp(header, "BSDIFN40", 8) == 0)
io = &BSDIFN40_funcs;
else {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
goto cleanup;
}
/* Read lengths from header */
bzctrllen=offtin(header+8);
bzdatalen=offtin(header+16);
newsize=offtin(header+24);
/* Read lengths from header */
bzctrllen=offtin(header+8);
bzdatalen=offtin(header+16);
newsize=offtin(header+24);
if((bzctrllen<0) || (bzdatalen<0) || (newsize<0)) {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
goto cleanup;
}
/* Close patch file and re-open it via libbzip2 at the right places */
/* Close patch file and re-open it via libbzip2 at the right places */
if (fclose(f)) {
warn("fclose(%s)", argv[3]);
warn("fclose(%s)", argv[3]);
f = NULL;
goto cleanup;
}
f = NULL;
if ((cpf = fopen(argv[3], "r")) == NULL) {
warn("fopen(%s)", argv[3]);
warn("fopen(%s)", argv[3]);
goto cleanup;
}
if (fseeko(cpf, 32, SEEK_SET)) {
warn("fseeko(%s, %lld)", argv[3],
(long long)32);
warn("fseeko(%s, %lld)", argv[3],
(long long)32);
goto cleanup;
}
cstream = io->open(cpf);
cstream = io->open(cpf);
if (cstream == NULL) {
warn("cstream open");
goto cleanup;
}
if ((dpf = fopen(argv[3], "r")) == NULL) {
warn("fopen(%s)", argv[3]);
warn("fopen(%s)", argv[3]);
goto cleanup;
}
if (fseeko(dpf, 32 + bzctrllen, SEEK_SET)) {
warn("fseeko(%s, %lld)", argv[3],
(long long)(32 + bzctrllen));
warn("fseeko(%s, %lld)", argv[3],
(long long)(32 + bzctrllen));
goto cleanup;
}
dstream = io->open(dpf);
dstream = io->open(dpf);
if (dstream == NULL) {
warn("dstream open");
goto cleanup;
}
if ((epf = fopen(argv[3], "r")) == NULL) {
warn("fopen(%s)", argv[3]);
warn("fopen(%s)", argv[3]);
goto cleanup;
}
if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET)) {
warn("fseeko(%s, %lld)", argv[3],
(long long)(32 + bzctrllen + bzdatalen));
warn("fseeko(%s, %lld)", argv[3],
(long long)(32 + bzctrllen + bzdatalen));
goto cleanup;
}
estream = io->open(epf);
estream = io->open(epf);
if (estream == NULL) {
warn("estream open");
goto cleanup;
@@ -261,64 +261,64 @@ int bspatch(int argc,const char * const argv[])
goto cleanup;
}
oldpos=0;newpos=0;
while(newpos<newsize) {
/* Read control data */
for(i=0;i<=2;i++) {
lenread = io->read(cstream, buf, 8);
oldpos=0;newpos=0;
while(newpos<newsize) {
/* Read control data */
for(i=0;i<=2;i++) {
lenread = io->read(cstream, buf, 8);
if (lenread < 8) {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
goto cleanup;
}
ctrl[i]=offtin(buf);
};
ctrl[i]=offtin(buf);
};
/* Sanity-check */
/* Sanity-check */
if(newpos+ctrl[0]>newsize) {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
goto cleanup;
}
/* Read diff string */
lenread = io->read(dstream, new + newpos, ctrl[0]);
/* Read diff string */
lenread = io->read(dstream, new + newpos, ctrl[0]);
if (lenread < 0 || lenread < ctrl[0]) {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
goto cleanup;
}
/* 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];
/* 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];
/* Adjust pointers */
newpos+=ctrl[0];
oldpos+=ctrl[0];
/* Adjust pointers */
newpos+=ctrl[0];
oldpos+=ctrl[0];
/* Sanity-check */
/* Sanity-check */
if(newpos+ctrl[1]>newsize) {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
goto cleanup;
}
/* Read extra string */
lenread = io->read(estream, new + newpos, ctrl[1]);
/* Read extra string */
lenread = io->read(estream, new + newpos, ctrl[1]);
if (lenread < 0 || lenread < ctrl[1]) {
warnx("Corrupt patch\n");
warnx("Corrupt patch\n");
goto cleanup;
}
/* Adjust pointers */
newpos+=ctrl[1];
oldpos+=ctrl[2];
};
/* Adjust pointers */
newpos+=ctrl[1];
oldpos+=ctrl[2];
};
/* Clean up the bzip2 reads */
io->close(cstream);
/* Clean up the bzip2 reads */
io->close(cstream);
cstream = NULL;
io->close(dstream);
io->close(dstream);
dstream = NULL;
io->close(estream);
io->close(estream);
estream = NULL;
if (fclose(cpf) != 0) {
@@ -342,7 +342,7 @@ int bspatch(int argc,const char * const argv[])
}
epf = NULL;
/* Write the new file */
/* Write the new file */
f = fopen(argv[2], "w");
if (f == NULL) {
warn("failed to write new file: %s", argv[2]);
@@ -363,8 +363,8 @@ int bspatch(int argc,const char * const argv[])
exitstatus = 0;
cleanup:
free(new);
free(old);
free(new);
free(old);
if (f != NULL) {
fclose(f);
@@ -394,5 +394,5 @@ cleanup:
fclose(cpf);
}
return exitstatus;
return exitstatus;
}