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 bd45371d268cb6da6692ab0b4efb76c8ee7bef40
parent f4eed7a708e3bc2151707b9f5006e6afbbfb055b
Author: Matthew Endsley <mendsley@gmail.com>
Date:   Thu, 24 Sep 2020 00:12:28 +1000

Replace header buf with struct

Diffstat:
Mbsdiff.c | 17+++++++----------
Mbsdiff.h | 7+++++++
Mbspatch.c | 2+-
3 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/bsdiff.c b/bsdiff.c @@ -63,7 +63,7 @@ main(int argc, char **argv) off_t dblen, eblen; uint8_t *db, *eb; uint8_t buf[8]; - uint8_t header[32]; + struct bsdiff_header header = {"BSDIFF40", 0, 0, 0}; FILE *pf; BZFILE *pfbz2; int bz2err; @@ -123,12 +123,9 @@ main(int argc, char **argv) 32 ?? Bzip2ed ctrl block ?? ?? Bzip2ed diff block ?? ?? Bzip2ed extra block */ - memcpy(header, "BSDIFF40", 8); - offtout(0, header + 8); - offtout(0, header + 16); - offtout(newsize, header + 24); - if (fwrite(header, 32, 1, pf) == 0) - err(1, "fwrite(%s, %lld)", argv[3], (long long)32); + header.new_file_len = newsize; + if (fwrite(&header, 1, sizeof(header), pf) != 32) + 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) @@ -240,7 +237,7 @@ main(int argc, char **argv) /* Compute size of compressed ctrl data */ if ((len = ftello(pf)) == -1) err(1, "ftello"); - offtout(len - 32, header + 8); + header.ctrl_block_len = len - 32; /* Write compressed diff data */ if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) @@ -255,7 +252,7 @@ main(int argc, char **argv) /* Compute size of compressed diff data */ if ((newsize = ftello(pf)) == -1) err(1, "ftello"); - offtout(newsize - len, header + 16); + header.diff_block_len = newsize - len; /* Write compressed extra data */ if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) @@ -270,7 +267,7 @@ main(int argc, char **argv) /* Seek to the beginning, write the header, and close the file */ if (fseeko(pf, 0, SEEK_SET)) err(1, "fseeko"); - if (fwrite(header, 32, 1, pf) != 1) + if (fwrite(&header, 1, sizeof(header), pf) != 32) err(1, "fwrite(%s)", argv[3]); if (fclose(pf)) err(1, "fclose"); diff --git a/bsdiff.h b/bsdiff.h @@ -35,6 +35,13 @@ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) +struct bsdiff_header { + uint8_t magic[8]; + off_t ctrl_block_len; + off_t diff_block_len; + off_t new_file_len; +}; + void split(off_t *, off_t *, off_t, off_t, off_t); void qsufsort(off_t *, off_t *, uint8_t *, off_t); off_t matchlen(uint8_t *, off_t, uint8_t *, off_t); diff --git a/bspatch.c b/bspatch.c @@ -80,7 +80,7 @@ main(int argc, char **argv) #endif /* Open patch file */ if ((f = fopen(argv[3], "r")) == NULL) - err(1, "%s: fopen", argv[3]); + err(1, "fopen(%s)", argv[3]); /* Open the new file */ if ((fd = open(argv[2], O_CREAT|O_TRUNC|O_WRONLY, 0666)) == -1) err(1, "open(%s)", argv[2]);