commit a43eb860491079d2587b6cda6bb567862cfbaeaa
parent ffec63e2b1bef1c7ffe865f66ffbe374b94f91a0
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date: Thu, 24 Sep 2020 14:51:14 +1000
bsdiff: store filelen without ftello
Diffstat:
M | bsdiff.c | | | 27 | ++++++++++++++------------- |
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/bsdiff.c b/bsdiff.c
@@ -34,6 +34,7 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05
#include "config.h"
#include <sys/stat.h>
+#include <assert.h>
#include <bzlib.h>
#if HAVE_ERR
#include <err.h>
@@ -48,20 +49,23 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05
#include "bsdiff.h"
-static void
+static off_t
bz_write(FILE *f, void *buf, size_t size)
{
int bzerr;
BZFILE *bzf;
+ unsigned int out;
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);
+ BZ2_bzWriteClose(&bzerr, bzf, 0, NULL, &out);
if (bzerr != BZ_OK)
errx(1, "BZ2_bzWriteClose returned %d", bzerr);
+
+ return (off_t)out;
}
int
@@ -70,7 +74,7 @@ main(int argc, char **argv)
uint8_t *old, *new;
off_t oldsize, newsize;
off_t *I, *V;
- off_t scan, pos, len;
+ off_t scan, pos, len, filelen;
off_t lastscan, lastpos, lastoffset;
off_t oldscore, scsc;
off_t s, Sf, lenf, Sb, lenb;
@@ -125,7 +129,7 @@ main(int argc, char **argv)
err(1, "reallocarray");
if ((eb = reallocarray(NULL, oldsize + 1, sizeof(off_t))) == NULL)
err(1, "reallocarray");
- dblen = eblen = 0;
+ dblen = eblen = filelen = 0;
/* Header is
0 8 "BSDIFF40"
@@ -140,6 +144,7 @@ main(int argc, char **argv)
header.new_file_len = newsize;
if (fwrite(&header, 1, sizeof(header), pf) != 32)
err(1, "fwrite(%s, %zu)", argv[3], sizeof(header));
+ filelen += 32;
/* Compute the differences, writing ctrl as we go */
scan = len = lastscan = lastpos = lastoffset = 0;
@@ -224,7 +229,7 @@ main(int argc, char **argv)
offtout(lenf, buf);
offtout((scan - lenb) - (lastscan + lenf), buf + 8);
offtout((pos - lenb) - (lastpos + lenf), buf + 16);
- bz_write(pf, buf, sizeof(buf));
+ filelen += bz_write(pf, buf, sizeof(buf));
lastscan = scan - lenb;
lastpos = pos - lenb;
@@ -232,14 +237,10 @@ main(int argc, char **argv)
}
}
/* 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);
-
- if ((newsize = ftello(pf)) == -1)
- err(1, "ftello");
- header.diff_block_len = newsize - len;
+ header.ctrl_block_len = filelen - 32;
+ filelen += bz_write(pf, db, dblen);
+
+ header.diff_block_len = filelen - (header.ctrl_block_len + 32),
bz_write(pf, eb, eblen);
/* Seek to the beginning, write the header, and close the file */