bsdiff-portable

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

commit ffec63e2b1bef1c7ffe865f66ffbe374b94f91a0
parent f7aada98359c9fd7b575e326187fc35e02a9da76
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Thu, 24 Sep 2020 13:38:42 +1000

bsdiff: consolidate bzip writing into one function

Diffstat:
Mbsdiff.c | 53++++++++++++++++++++---------------------------------
1 file changed, 20 insertions(+), 33 deletions(-)

diff --git a/bsdiff.c b/bsdiff.c @@ -48,6 +48,22 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 #include "bsdiff.h" +static void +bz_write(FILE *f, void *buf, size_t size) +{ + int bzerr; + BZFILE *bzf; + + if ((bzf = BZ2_bzWriteOpen(&bzerr, f, 9, 0, 0)) == NULL) + errx(1, "BZ2_bzWriteOpen returned %d", bzerr); + BZ2_bzWrite(&bzerr, bzf, buf, size); + if (bzerr != BZ_OK) + errx(1, "BZ2_bzWrite returned %d", bzerr); + BZ2_bzWriteClose(&bzerr, bzf, 0, NULL, NULL); + if (bzerr != BZ_OK) + errx(1, "BZ2_bzWriteClose returned %d", bzerr); +} + int main(int argc, char **argv) { @@ -65,8 +81,6 @@ main(int argc, char **argv) uint8_t buf[24]; struct bsdiff_header header = {"BSDIFF40", 0, 0, 0}; FILE *pf; - BZFILE *pfbz2; - int bz2err; if (argc != 4) usage(); @@ -128,8 +142,6 @@ main(int argc, char **argv) err(1, "fwrite(%s, %zu)", argv[3], sizeof(header)); /* 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 = len = lastscan = lastpos = lastoffset = 0; while (scan < newsize) { oldscore = 0; @@ -212,48 +224,23 @@ main(int argc, char **argv) offtout(lenf, buf); offtout((scan - lenb) - (lastscan + lenf), buf + 8); offtout((pos - lenb) - (lastpos + lenf), buf + 16); - BZ2_bzWrite(&bz2err, pfbz2, buf, sizeof(buf)); - if (bz2err != BZ_OK) - errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); + bz_write(pf, buf, sizeof(buf)); lastscan = scan - lenb; lastpos = pos - lenb; lastoffset = pos - scan; } } - BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); - if (bz2err != BZ_OK) - errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); - - /* Compute size of compressed ctrl data */ + /* Compute the size of the blocks and write them */ if ((len = ftello(pf)) == -1) err(1, "ftello"); header.ctrl_block_len = len - 32; + bz_write(pf, db, dblen); - /* Write compressed diff data */ - if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) - errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); - BZ2_bzWrite(&bz2err, pfbz2, db, dblen); - if (bz2err != BZ_OK) - errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); - BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); - if (bz2err != BZ_OK) - errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); - - /* Compute size of compressed diff data */ if ((newsize = ftello(pf)) == -1) err(1, "ftello"); header.diff_block_len = newsize - len; - - /* Write compressed extra data */ - if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) - errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); - BZ2_bzWrite(&bz2err, pfbz2, eb, eblen); - if (bz2err != BZ_OK) - errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); - BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); - if (bz2err != BZ_OK) - errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); + bz_write(pf, eb, eblen); /* Seek to the beginning, write the header, and close the file */ if (fseeko(pf, 0, SEEK_SET))