From 2ff07dee98bc4759aae142daada74ac13ad7ff64 Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Sun, 13 May 2012 17:29:03 -0700 Subject: control/diff/extra streams are passed in the request struct removes bz2 dependencies from bspatch --- bspatch.c | 174 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 86 insertions(+), 88 deletions(-) (limited to 'bspatch.c') diff --git a/bspatch.c b/bspatch.c index 45ba36a..43fc578 100644 --- a/bspatch.c +++ b/bspatch.c @@ -49,9 +49,9 @@ struct bspatch_request int oldsize; uint8_t* new; int newsize; - FILE* cpf; - FILE* dpf; - FILE* epf; + struct bspatch_stream control; + struct bspatch_stream diff; + struct bspatch_stream extra; }; static int64_t offtin(uint8_t *buf) @@ -72,90 +72,19 @@ static int64_t offtin(uint8_t *buf) return y; } -#define BUFFER_SIZE 4096 -struct bspatch_bz2_buffer -{ - FILE* pf; - bz_stream bz2; - char buffer[BUFFER_SIZE]; -}; - -static int readcompress(const struct bspatch_stream* stream, void* buffer, int length) -{ - int n; - int ret; - struct bspatch_bz2_buffer* buf = (struct bspatch_bz2_buffer*)stream->opaque; - bz_stream* bz2 = &buf->bz2; - - bz2->next_out = (char*)buffer; - bz2->avail_out = length; - - for (;;) - { - if (bz2->avail_in == 0 && !feof(buf->pf) && bz2->avail_out > 0) - { - n = fread(buf->buffer, 1, BUFFER_SIZE, buf->pf); - if (ferror(buf->pf)) - return -1; - - bz2->next_in = buf->buffer; - bz2->avail_in = n; - } - - ret = BZ2_bzDecompress(bz2); - if (ret != BZ_OK && ret != BZ_STREAM_END) - return -1; - - if (ret == BZ_OK && feof(buf->pf) && bz2->avail_in == 0 && bz2->avail_out > 0) - return -1; - - if (ret == BZ_STREAM_END) - return length - bz2->avail_out; - if (bz2->avail_out == 0) - return length; - } - - // unreachable - return -1; -} - int bspatch(const struct bspatch_request req) { - struct bspatch_stream cstream; - struct bspatch_stream dstream; - struct bspatch_stream estream; - struct bspatch_bz2_buffer cbz2 = {0}; - struct bspatch_bz2_buffer dbz2 = {0}; - struct bspatch_bz2_buffer ebz2 = {0}; - int cbz2err, dbz2err, ebz2err; uint8_t buf[8]; int64_t oldpos,newpos; int64_t ctrl[3]; int64_t lenread; int64_t i; - cbz2.pf = req.cpf; - cstream.opaque = &cbz2; - cstream.read = readcompress; - dbz2.pf = req.dpf; - dstream.opaque = &dbz2; - dstream.read = readcompress; - ebz2.pf = req.epf; - estream.opaque = &ebz2; - estream.read = readcompress; - - if (BZ_OK != (cbz2err = BZ2_bzDecompressInit(&cbz2.bz2, 0, 0))) - errx(1, "BZ2_bzDecompressInit, bz2err = %d", cbz2err); - if (BZ_OK != (dbz2err = BZ2_bzDecompressInit(&dbz2.bz2, 0, 0))) - errx(1, "BZ2_bzDecompressInit, bz2err = %d", dbz2err); - if (BZ_OK != (ebz2err = BZ2_bzDecompressInit(&ebz2.bz2, 0, 0))) - errx(1, "BZ2_bzDecompressInit, bz2err = %d", ebz2err); - oldpos=0;newpos=0; while(newposopaque; + bz_stream* bz2 = &buf->bz2; + + bz2->next_out = (char*)buffer; + bz2->avail_out = length; + + for (;;) + { + if (bz2->avail_in == 0 && !feof(buf->pf) && bz2->avail_out > 0) + { + n = fread(buf->buffer, 1, BUFFER_SIZE, buf->pf); + if (ferror(buf->pf)) + return -1; + + bz2->next_in = buf->buffer; + bz2->avail_in = n; + } + + ret = BZ2_bzDecompress(bz2); + if (ret != BZ_OK && ret != BZ_STREAM_END) + return -1; + + if (ret == BZ_OK && feof(buf->pf) && bz2->avail_in == 0 && bz2->avail_out > 0) + return -1; + + if (ret == BZ_STREAM_END) + return length - bz2->avail_out; + if (bz2->avail_out == 0) + return length; + } + + // unreachable + return -1; +} + int main(int argc,char * argv[]) { FILE * f; int fd; + int cbz2err, dbz2err, ebz2err; ssize_t bzctrllen,bzdatalen; uint8_t header[32]; uint8_t *old; + FILE *cpf, *dpf, *epf; struct bspatch_request req; + struct bspatch_bz2_buffer cbz2 = {0}; + struct bspatch_bz2_buffer dbz2 = {0}; + struct bspatch_bz2_buffer ebz2 = {0}; if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); @@ -251,19 +227,19 @@ int main(int argc,char * argv[]) /* Close patch file and re-open it via libbzip2 at the right places */ if (fclose(f)) err(1, "fclose(%s)", argv[3]); - if ((req.cpf = fopen(argv[3], "r")) == NULL) + if ((cpf = fopen(argv[3], "r")) == NULL) err(1, "fopen(%s)", argv[3]); - if (fseeko(req.cpf, 32, SEEK_SET)) + if (fseeko(cpf, 32, SEEK_SET)) err(1, "fseeko64(%s, %lld)", argv[3], (long long)32); - if ((req.dpf = fopen(argv[3], "r")) == NULL) + if ((dpf = fopen(argv[3], "r")) == NULL) err(1, "fopen(%s)", argv[3]); - if (fseeko(req.dpf, 32 + bzctrllen, SEEK_SET)) + if (fseeko(dpf, 32 + bzctrllen, SEEK_SET)) err(1, "fseeko64(%s, %lld)", argv[3], (long long)(32 + bzctrllen)); - if ((req.epf = fopen(argv[3], "r")) == NULL) + if ((epf = fopen(argv[3], "r")) == NULL) err(1, "fopen(%s)", argv[3]); - if (fseeko(req.epf, 32 + bzctrllen + bzdatalen, SEEK_SET)) + if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET)) err(1, "fseeko64(%s, %lld)", argv[3], (long long)(32 + bzctrllen + bzdatalen)); @@ -276,9 +252,31 @@ int main(int argc,char * argv[]) req.old = old; if((req.new=malloc(req.newsize+1))==NULL) err(1,NULL); + cbz2.pf = cpf; + req.control.opaque = &cbz2; + req.control.read = readcompress; + dbz2.pf = dpf; + req.diff.opaque = &dbz2; + req.diff.read = readcompress; + ebz2.pf = epf; + req.extra.opaque = &ebz2; + req.extra.read = readcompress; + + if (BZ_OK != (cbz2err = BZ2_bzDecompressInit(&cbz2.bz2, 0, 0))) + errx(1, "BZ2_bzDecompressInit, bz2err = %d", cbz2err); + if (BZ_OK != (dbz2err = BZ2_bzDecompressInit(&dbz2.bz2, 0, 0))) + errx(1, "BZ2_bzDecompressInit, bz2err = %d", dbz2err); + if (BZ_OK != (ebz2err = BZ2_bzDecompressInit(&ebz2.bz2, 0, 0))) + errx(1, "BZ2_bzDecompressInit, bz2err = %d", ebz2err); + if (bspatch(req)) err(1, "bspatch"); + /* Clean up the bzip2 reads */ + BZ2_bzDecompressEnd(&cbz2.bz2); + BZ2_bzDecompressEnd(&dbz2.bz2); + BZ2_bzDecompressEnd(&ebz2.bz2); + /* Write the new file */ if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) || (write(fd,req.new,req.newsize)!=req.newsize) || (close(fd)==-1)) -- cgit v1.2.1