commit 394a89f23ca1ede2bd098e2f76279c42a9cd2f8a
parent a8fc680d49c967d1409bf3d39c8240727ea4c294
Author: Matthew Endsley <mendsley@gmail.com>
Date: Thu, 24 Sep 2020 15:28:16 +1000
bsdiff: use a single buffer for diff/extra blocks
Diffstat:
M | bsdiff.c | | | 29 | +++++++++++++---------------- |
1 file changed, 13 insertions(+), 16 deletions(-)
diff --git a/bsdiff.c b/bsdiff.c
@@ -80,8 +80,8 @@ main(int argc, char **argv)
off_t s, Sf, lenf, Sb, lenb;
off_t overlap, Ss, lens;
off_t i;
- off_t dblen, eblen;
- uint8_t *db, *eb;
+ off_t dblen;
+ uint8_t *dbuf;
uint8_t ctrlbuf[24];
struct bsdiff_header header = {"BSDIFF40", 0, 0, 0};
FILE *pf;
@@ -125,11 +125,9 @@ main(int argc, char **argv)
err(1, "pledge");
#endif
- if ((db = reallocarray(NULL, oldsize + 1, sizeof(off_t))) == NULL)
+ if ((dbuf = reallocarray(NULL, newsize + 1, sizeof(off_t))) == NULL)
err(1, "reallocarray");
- if ((eb = reallocarray(NULL, oldsize + 1, sizeof(off_t))) == NULL)
- err(1, "reallocarray");
- dblen = eblen = filelen = 0;
+ filelen = 0;
/* Header is
0 8 "BSDIFF40"
@@ -225,16 +223,16 @@ main(int argc, char **argv)
filelen += bz_write(pf, ctrlbuf, sizeof(ctrlbuf));
header.ctrl_block_len = filelen - 32;
- for (i = 0; i < lenf; i++)
- db[dblen + i] = new[lastscan + i] - old[lastpos + i];
- dblen += lenf;
- filelen += bz_write(pf, db, dblen);
+ dblen = lenf;
+ for (i = 0; i < dblen; i++)
+ dbuf[i] = new[lastscan + i] - old[lastpos + i];
+ filelen += bz_write(pf, dbuf, dblen);
header.diff_block_len = filelen - (header.ctrl_block_len + 32);
- for (i = 0; i < (scan - lenb) - (lastscan + lenf); i++)
- eb[eblen + i] = new[lastscan + lenf + i];
- eblen += (scan - lenb) - (lastscan + lenf);
- bz_write(pf, eb, eblen);
+ dblen = (scan - lenb) - (lastscan + lenf);
+ for (i = 0; i < dblen; i++)
+ dbuf[i] = new[lastscan + lenf + i];
+ bz_write(pf, dbuf, dblen);
/* Seek to the beginning, write the header, and close the file */
if (fseeko(pf, 0, SEEK_SET))
@@ -245,8 +243,7 @@ main(int argc, char **argv)
err(1, "fclose");
/* Free the memory we used */
- free(db);
- free(eb);
+ free(dbuf);
free(I);
free(old);
free(new);