2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
29 #include "got_error.h"
31 #include "got_lib_deltify.h"
32 #include "murmurhash2.h"
35 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
39 * The algorihm used here is FastCDC (Fast Content-Defined Chunking)
40 * https://www.usenix.org/conference/atc16/technical-sessions/presentation/xia
43 static const uint32_t geartab[256] = {
44 0x67ed26b7, 0x32da500c, 0x53d0fee0, 0xce387dc7, 0xcd406d90, 0x2e83a4d4,
45 0x9fc9a38d, 0xb67259dc, 0xca6b1722, 0x6d2ea08c, 0x235cea2e, 0x3149bb5f,
46 0x1beda787, 0x2a6b77d5, 0x2f22d9ac, 0x91fc0544, 0xe413acfa, 0x5a30ff7a,
47 0xad6fdde0, 0x444fd0f5, 0x7ad87864, 0x58c5ff05, 0x8d2ec336, 0x2371f853,
48 0x550f8572, 0x6aa448dd, 0x7c9ddbcf, 0x95221e14, 0x2a82ec33, 0xcbec5a78,
49 0xc6795a0d, 0x243995b7, 0x1c909a2f, 0x4fded51c, 0x635d334b, 0x0e2b9999,
50 0x2702968d, 0x856de1d5, 0x3325d60e, 0xeb6a7502, 0xec2a9844, 0x0905835a,
51 0xa1820375, 0xa4be5cab, 0x96a6c058, 0x2c2ccd70, 0xba40fce3, 0xd794c46b,
52 0x8fbae83e, 0xc3aa7899, 0x3d3ff8ed, 0xa0d42b5b, 0x571c0c97, 0xd2811516,
53 0xf7e7b96c, 0x4fd2fcbd, 0xe2fdec94, 0x282cc436, 0x78e8e95c, 0x80a3b613,
54 0xcfbee20c, 0xd4a32d1c, 0x2a12ff13, 0x6af82936, 0xe5630258, 0x8efa6a98,
55 0x294fb2d1, 0xdeb57086, 0x5f0fddb3, 0xeceda7ce, 0x4c87305f, 0x3a6d3307,
56 0xe22d2942, 0x9d060217, 0x1e42ed02, 0xb6f63b52, 0x4367f39f, 0x055cf262,
57 0x03a461b2, 0x5ef9e382, 0x386bc03a, 0x2a1e79c7, 0xf1a0058b, 0xd4d2dea9,
58 0x56baf37d, 0x5daff6cc, 0xf03a951d, 0xaef7de45, 0xa8f4581e, 0x3960b555,
59 0xffbfff6d, 0xbe702a23, 0x8f5b6d6f, 0x061739fb, 0x98696f47, 0x3fd596d4,
60 0x151eac6b, 0xa9fcc4f5, 0x69181a12, 0x3ac5a107, 0xb5198fe7, 0x96bcb1da,
61 0x1b5ddf8e, 0xc757d650, 0x65865c3a, 0x8fc0a41a, 0x87435536, 0x99eda6f2,
62 0x41874794, 0x29cff4e8, 0xb70efd9a, 0x3103f6e7, 0x84d2453b, 0x15a450bd,
63 0x74f49af1, 0x60f664b1, 0xa1c86935, 0xfdafbce1, 0xe36353e3, 0x5d9ba739,
64 0xbc0559ba, 0x708b0054, 0xd41d808c, 0xb2f31723, 0x9027c41f, 0xf136d165,
65 0xb5374b12, 0x9420a6ac, 0x273958b6, 0xe6c2fad0, 0xebdc1f21, 0xfb33af8b,
66 0xc71c25cd, 0xe9a2d8e5, 0xbeb38a50, 0xbceb7cc2, 0x4e4e73f0, 0xcd6c251d,
67 0xde4c032c, 0x4b04ac30, 0x725b8b21, 0x4eb8c33b, 0x20d07b75, 0x0567aa63,
68 0xb56b2bb7, 0xc1f5fd3a, 0xcafd35ca, 0x470dd4da, 0xfe4f94cd, 0xfb8de424,
69 0xe8dbcf40, 0xfe50a37a, 0x62db5b5d, 0xf32f4ab6, 0x2c4a8a51, 0x18473dc0,
70 0xfe0cbb6e, 0xfe399efd, 0xdf34ecc9, 0x6ccd5055, 0x46097073, 0x139135c2,
71 0x721c76f6, 0x1c6a94b4, 0x6eee014d, 0x8a508e02, 0x3da538f5, 0x280d394f,
72 0x5248a0c4, 0x3ce94c6c, 0x9a71ad3a, 0x8493dd05, 0xe43f0ab6, 0x18e4ed42,
73 0x6c5c0e09, 0x42b06ec9, 0x8d330343, 0xa45b6f59, 0x2a573c0c, 0xd7fd3de6,
74 0xeedeab68, 0x5c84dafc, 0xbbd1b1a8, 0xa3ce1ad1, 0x85b70bed, 0xb6add07f,
75 0xa531309c, 0x8f8ab852, 0x564de332, 0xeac9ed0c, 0x73da402c, 0x3ec52761,
76 0x43af2f4d, 0xd6ff45c8, 0x4c367462, 0xd553bd6a, 0x44724855, 0x3b2aa728,
77 0x56e5eb65, 0xeaf16173, 0x33fa42ff, 0xd714bb5d, 0xfbd0a3b9, 0xaf517134,
78 0x9416c8cd, 0x534cf94f, 0x548947c2, 0x34193569, 0x32f4389a, 0xfe7028bc,
79 0xed73b1ed, 0x9db95770, 0x468e3922, 0x0440c3cd, 0x60059a62, 0x33504562,
80 0x2b229fbd, 0x5174dca5, 0xf7028752, 0xd63c6aa8, 0x31276f38, 0x0646721c,
81 0xb0191da8, 0xe00e6de0, 0x9eac1a6e, 0x9f7628a5, 0xed6c06ea, 0x0bb8af15,
82 0xf119fb12, 0x38693c1c, 0x732bc0fe, 0x84953275, 0xb82ec888, 0x33a4f1b3,
83 0x3099835e, 0x028a8782, 0x5fdd51d7, 0xc6c717b3, 0xb06caf71, 0x17c8c111,
84 0x61bad754, 0x9fd03061, 0xe09df1af, 0x3bc9eb73, 0x85878413, 0x9889aaf2,
85 0x3f5a9e46, 0x42c9f01f, 0x9984a4f4, 0xd5de43cc, 0xd294daed, 0xbecba2d2,
86 0xf1f6e72c, 0x5551128a, 0x83af87e2, 0x6f0342ba,
90 hashblk(const unsigned char *p, off_t n, uint32_t seed)
92 return murmurhash2(p, n, seed);
95 static const struct got_error *
96 addblk(struct got_delta_table *dt, FILE *f, off_t file_offset0, off_t len,
97 off_t offset, uint32_t h)
99 const struct got_error *err = NULL;
101 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
102 uint8_t buf2[GOT_DELTIFY_MAXCHUNK];
109 while (dt->blocks[i].len != 0) {
111 * Avoid adding duplicate blocks.
112 * NB: A matching hash is insufficient for detecting equality.
113 * The hash can only detect inequality.
115 if (len == dt->blocks[i].len && h == dt->blocks[i].hash) {
117 if (fseeko(f, file_offset0 + offset, SEEK_SET) == -1)
118 return got_error_from_errno("fseeko");
119 r = fread(buf, 1, len, f);
123 return got_ferror(f, GOT_ERR_IO);
126 if (fseeko(f, file_offset0 + dt->blocks[i].offset,
128 return got_error_from_errno("fseeko");
129 if (fread(buf2, 1, len, f) != len)
130 return got_ferror(f, GOT_ERR_IO);
131 if (memcmp(buf, buf2, len) == 0)
135 i = (i + 1) % dt->nalloc;
137 assert(dt->blocks[i].len == 0);
138 dt->blocks[i].len = len;
139 dt->blocks[i].offset = offset;
140 dt->blocks[i].hash = h;
142 if (dt->nalloc < dt->nblocks + 64) {
143 struct got_delta_block *db;
144 size_t old_size = dt->nalloc;
146 dt->blocks = calloc(dt->nalloc + 64,
147 sizeof(struct got_delta_block));
148 if (dt->blocks == NULL) {
149 err = got_error_from_errno("calloc");
155 * Recompute all block positions. Hash-based indices of blocks
156 * in the array depend on the allocated length of the array.
159 for (i = 0; i < old_size; i++) {
162 err = addblk(dt, f, file_offset0, db[i].len,
163 db[i].offset, db[i].hash);
173 static const struct got_error *
174 addblk_mem(struct got_delta_table *dt, uint8_t *data, off_t file_offset0,
175 off_t len, off_t offset, uint32_t h)
177 const struct got_error *err = NULL;
186 while (dt->blocks[i].len != 0) {
188 * Avoid adding duplicate blocks.
189 * NB: A matching hash is insufficient for detecting equality.
190 * The hash can only detect inequality.
192 if (len == dt->blocks[i].len && h == dt->blocks[i].hash) {
193 block1 = data + file_offset0 + dt->blocks[i].offset;
194 block2 = data + file_offset0 + offset;
195 if (memcmp(block1, block2, len) == 0)
199 i = (i + 1) % dt->nalloc;
201 assert(dt->blocks[i].len == 0);
202 dt->blocks[i].len = len;
203 dt->blocks[i].offset = offset;
204 dt->blocks[i].hash = h;
206 if (dt->nalloc < dt->nblocks + 64) {
207 struct got_delta_block *db;
208 size_t old_size = dt->nalloc;
210 dt->blocks = calloc(dt->nalloc + 64,
211 sizeof(struct got_delta_block));
212 if (dt->blocks == NULL) {
213 err = got_error_from_errno("calloc");
219 * Recompute all block positions. Hash-based indices of blocks
220 * in the array depend on the allocated length of the array.
223 for (i = 0; i < old_size; i++) {
226 err = addblk_mem(dt, data, file_offset0, db[i].len,
227 db[i].offset, db[i].hash);
237 static const struct got_error *
238 lookupblk(struct got_delta_block **block, struct got_delta_table *dt,
239 unsigned char *p, off_t len, uint32_t seed, FILE *basefile,
240 off_t basefile_offset0)
244 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
249 h = hashblk(p, len, seed);
250 for (i = h % dt->nalloc; dt->blocks[i].len != 0;
251 i = (i + 1) % dt->nalloc) {
252 if (dt->blocks[i].hash != h ||
253 dt->blocks[i].len != len)
255 if (fseeko(basefile, basefile_offset0 + dt->blocks[i].offset,
257 return got_error_from_errno("fseeko");
258 r = fread(buf, 1, len, basefile);
260 return got_ferror(basefile, GOT_ERR_IO);
261 if (memcmp(p, buf, len) == 0) {
262 *block = &dt->blocks[i];
269 static const struct got_error *
270 lookupblk_mem(struct got_delta_block **block, struct got_delta_table *dt,
271 unsigned char *p, off_t len, uint32_t seed, uint8_t *basedata,
272 off_t basefile_offset0)
280 h = hashblk(p, len, seed);
281 for (i = h % dt->nalloc; dt->blocks[i].len != 0;
282 i = (i + 1) % dt->nalloc) {
283 if (dt->blocks[i].hash != h ||
284 dt->blocks[i].len != len)
286 b = basedata + basefile_offset0 + dt->blocks[i].offset;
287 if (memcmp(p, b, len) == 0) {
288 *block = &dt->blocks[i];
295 static const struct got_error *
296 nextblk(uint8_t *buf, off_t *blocklen, FILE *f)
299 const unsigned char *p;
301 off_t pos = ftello(f);
305 r = fread(buf, 1, GOT_DELTIFY_MAXCHUNK, f);
306 if (r == 0 && ferror(f))
307 return got_ferror(f, GOT_ERR_IO);
308 if (r < GOT_DELTIFY_MINCHUNK)
309 return NULL; /* no more delta-worthy blocks left */
311 /* Got a deltifiable block. Find the split-point where it ends. */
312 p = buf + GOT_DELTIFY_MINCHUNK;
314 while (p != buf + r) {
315 gh = (gh << 1) + geartab[*p++];
316 if ((gh & GOT_DELTIFY_SPLITMASK) == 0)
320 *blocklen = (p - buf);
321 if (fseeko(f, pos + *blocklen, SEEK_SET) == -1)
322 return got_error_from_errno("fseeko");
327 static const struct got_error *
328 nextblk_mem(off_t *blocklen, uint8_t *data, off_t fileoffset, off_t filesize)
331 const unsigned char *p;
335 if (fileoffset >= filesize ||
336 filesize - fileoffset < GOT_DELTIFY_MINCHUNK)
337 return NULL; /* no more delta-worthy blocks left */
339 /* Got a deltifiable block. Find the split-point where it ends. */
340 p = data + fileoffset + GOT_DELTIFY_MINCHUNK;
342 while (p != data + MIN(fileoffset + GOT_DELTIFY_MAXCHUNK, filesize)) {
343 gh = (gh << 1) + geartab[*p++];
344 if ((gh & GOT_DELTIFY_SPLITMASK) == 0)
348 *blocklen = (p - (data + fileoffset));
352 const struct got_error *
353 got_deltify_init(struct got_delta_table **dt, FILE *f, off_t fileoffset,
354 off_t filesize, uint32_t seed)
356 const struct got_error *err = NULL;
358 const off_t offset0 = fileoffset;
360 *dt = calloc(1, sizeof(**dt));
362 return got_error_from_errno("calloc");
366 (*dt)->blocks = calloc((*dt)->nalloc, sizeof(struct got_delta_block));
367 if ((*dt)->blocks == NULL) {
368 err = got_error_from_errno("calloc");
372 if (fseeko(f, fileoffset, SEEK_SET) == -1)
373 return got_error_from_errno("fseeko");
375 while (fileoffset < filesize) {
376 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
378 err = nextblk(buf, &blocklen, f);
383 h = hashblk(buf, blocklen, seed);
384 err = addblk(*dt, f, offset0, blocklen,
385 fileoffset - offset0, h);
388 fileoffset += blocklen;
389 if (fseeko(f, fileoffset, SEEK_SET) == -1)
390 return got_error_from_errno("fseeko");
402 const struct got_error *
403 got_deltify_init_mem(struct got_delta_table **dt, uint8_t *data,
404 off_t fileoffset, off_t filesize, uint32_t seed)
406 const struct got_error *err = NULL;
408 const off_t offset0 = fileoffset;
410 *dt = calloc(1, sizeof(**dt));
412 return got_error_from_errno("calloc");
416 (*dt)->blocks = calloc((*dt)->nalloc, sizeof(struct got_delta_block));
417 if ((*dt)->blocks == NULL) {
418 err = got_error_from_errno("calloc");
422 while (fileoffset < filesize) {
424 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
429 h = hashblk(data + fileoffset, blocklen, seed);
430 err = addblk_mem(*dt, data, offset0, blocklen,
431 fileoffset - offset0, h);
434 fileoffset += blocklen;
447 got_deltify_free(struct got_delta_table *dt)
455 static const struct got_error *
456 emitdelta(struct got_delta_instruction **deltas, size_t *nalloc, int *ndeltas,
457 const size_t alloc_chunk_size, int copy, off_t offset, off_t len)
459 struct got_delta_instruction *d, *p;
461 if (*nalloc < *ndeltas + alloc_chunk_size) {
462 p = reallocarray(*deltas, *nalloc + alloc_chunk_size,
463 sizeof(struct got_delta_instruction));
465 return got_error_from_errno("reallocarray");
467 *nalloc += alloc_chunk_size;
470 d = &(*deltas)[*ndeltas - 1];
477 static const struct got_error *
478 stretchblk(FILE *basefile, off_t base_offset0, struct got_delta_block *block,
479 FILE *f, off_t filesize, off_t *blocklen)
481 uint8_t basebuf[GOT_DELTIFY_MAXCHUNK], buf[GOT_DELTIFY_MAXCHUNK];
485 if (fseeko(basefile, base_offset0 + block->offset + *blocklen,
487 return got_error_from_errno("fseeko");
489 while (buf_equal && *blocklen < (1 << 24) - 1) {
490 base_r = fread(basebuf, 1, sizeof(basebuf), basefile);
492 if (ferror(basefile))
493 return got_ferror(basefile, GOT_ERR_IO);
496 r = fread(buf, 1, sizeof(buf), f);
499 return got_ferror(f, GOT_ERR_IO);
502 for (i = 0; i < MIN(base_r, r); i++) {
503 if (buf[i] != basebuf[i]) {
514 static const struct got_error *
515 stretchblk_file_mem(uint8_t *basedata, off_t base_offset0, off_t basefile_size,
516 struct got_delta_block *block, FILE *f, off_t filesize, off_t *blocklen)
518 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
521 off_t base_offset = base_offset0 + block->offset + *blocklen;
523 if (base_offset > basefile_size) {
524 return got_error_fmt(GOT_ERR_RANGE,
525 "read beyond the size of delta base at offset %lld",
526 (long long)base_offset);
529 while (buf_equal && *blocklen < (1 << 24) - 1) {
530 if (base_offset + *blocklen >= basefile_size)
532 r = fread(buf, 1, sizeof(buf), f);
535 return got_ferror(f, GOT_ERR_IO);
538 for (i = 0; i < MIN(basefile_size - base_offset, r); i++) {
539 if (buf[i] != *(basedata + base_offset + i)) {
550 static const struct got_error *
551 stretchblk_mem_file(FILE *basefile, off_t base_offset0,
552 struct got_delta_block *block, uint8_t *data, off_t fileoffset,
553 off_t filesize, off_t *blocklen)
555 uint8_t basebuf[GOT_DELTIFY_MAXCHUNK];
559 if (fileoffset > filesize) {
560 return got_error_fmt(GOT_ERR_RANGE,
561 "read beyond the size of deltify file at offset %lld",
562 (long long)fileoffset);
565 if (fseeko(basefile, base_offset0 + block->offset + *blocklen,
567 return got_error_from_errno("fseeko");
569 while (buf_equal && *blocklen < (1 << 24) - 1) {
570 if (fileoffset + *blocklen >= filesize)
572 base_r = fread(basebuf, 1, sizeof(basebuf), basefile);
574 if (ferror(basefile))
575 return got_ferror(basefile, GOT_ERR_IO);
578 for (i = 0; i < MIN(base_r, filesize - fileoffset); i++) {
579 if (*(data + fileoffset + i) != basebuf[i]) {
590 static const struct got_error *
591 stretchblk_mem_mem(uint8_t *basedata, off_t base_offset0, off_t basefile_size,
592 struct got_delta_block *block, uint8_t *data, off_t fileoffset,
593 off_t filesize, off_t *blocklen)
596 off_t base_offset = base_offset0 + block->offset + *blocklen;
599 if (base_offset > basefile_size) {
600 return got_error_fmt(GOT_ERR_RANGE,
601 "read beyond the size of delta base at offset %lld",
602 (long long)base_offset);
605 if (fileoffset > filesize) {
606 return got_error_fmt(GOT_ERR_RANGE,
607 "read beyond the size of deltify file at offset %lld",
608 (long long)fileoffset);
611 p = data + fileoffset;
612 q = basedata + base_offset;
613 maxlen = MIN(basefile_size - base_offset, filesize - fileoffset);
614 for (i = 0; i < maxlen && *blocklen < (1 << 24) - 1; i++) {
623 const struct got_error *
624 got_deltify(struct got_delta_instruction **deltas, int *ndeltas,
625 FILE *f, off_t fileoffset, off_t filesize, uint32_t seed,
626 struct got_delta_table *dt, FILE *basefile,
627 off_t basefile_offset0, off_t basefile_size)
629 const struct got_error *err = NULL;
630 const off_t offset0 = fileoffset;
632 const size_t alloc_chunk_size = 64;
638 * offset0 indicates where data to be deltified begins.
639 * For example, we want to avoid deltifying a Git object header at
640 * the beginning of the file.
642 if (fseeko(f, offset0, SEEK_SET) == -1)
643 return got_error_from_errno("fseeko");
645 *deltas = reallocarray(NULL, alloc_chunk_size,
646 sizeof(struct got_delta_instruction));
648 return got_error_from_errno("reallocarray");
649 nalloc = alloc_chunk_size;
651 while (fileoffset < filesize) {
652 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
654 struct got_delta_block *block;
655 err = nextblk(buf, &blocklen, f);
659 /* Source remainder from the file itself. */
660 if (fileoffset < filesize) {
661 err = emitdelta(deltas, &nalloc, ndeltas,
662 alloc_chunk_size, 0, fileoffset - offset0,
663 filesize - fileoffset);
667 err = lookupblk(&block, dt, buf, blocklen, seed, basefile,
673 * We have found a matching block in the delta base.
674 * Attempt to stretch the block as far as possible and
675 * generate a copy instruction.
677 err = stretchblk(basefile, basefile_offset0, block,
678 f, filesize, &blocklen);
681 err = emitdelta(deltas, &nalloc, ndeltas,
682 alloc_chunk_size, 1, block->offset, blocklen);
688 * This block needs to be sourced from the file itself.
690 err = emitdelta(deltas, &nalloc, ndeltas,
691 alloc_chunk_size, 0, fileoffset - offset0, blocklen);
695 fileoffset += blocklen;
696 if (fseeko(f, fileoffset, SEEK_SET) == -1) {
697 err = got_error_from_errno("fseeko");
710 const struct got_error *
711 got_deltify_file_mem(struct got_delta_instruction **deltas, int *ndeltas,
712 FILE *f, off_t fileoffset, off_t filesize, uint32_t seed,
713 struct got_delta_table *dt, uint8_t *basedata,
714 off_t basefile_offset0, off_t basefile_size)
716 const struct got_error *err = NULL;
717 const off_t offset0 = fileoffset;
719 const size_t alloc_chunk_size = 64;
725 * offset0 indicates where data to be deltified begins.
726 * For example, we want to avoid deltifying a Git object header at
727 * the beginning of the file.
729 if (fseeko(f, offset0, SEEK_SET) == -1)
730 return got_error_from_errno("fseeko");
732 *deltas = reallocarray(NULL, alloc_chunk_size,
733 sizeof(struct got_delta_instruction));
735 return got_error_from_errno("reallocarray");
736 nalloc = alloc_chunk_size;
738 while (fileoffset < filesize) {
739 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
741 struct got_delta_block *block;
742 err = nextblk(buf, &blocklen, f);
746 /* Source remainder from the file itself. */
747 if (fileoffset < filesize) {
748 err = emitdelta(deltas, &nalloc, ndeltas,
749 alloc_chunk_size, 0, fileoffset - offset0,
750 filesize - fileoffset);
754 err = lookupblk_mem(&block, dt, buf, blocklen, seed, basedata,
760 * We have found a matching block in the delta base.
761 * Attempt to stretch the block as far as possible and
762 * generate a copy instruction.
764 err = stretchblk_file_mem(basedata, basefile_offset0,
765 basefile_size, block, f, filesize, &blocklen);
768 err = emitdelta(deltas, &nalloc, ndeltas,
769 alloc_chunk_size, 1, block->offset, blocklen);
775 * This block needs to be sourced from the file itself.
777 err = emitdelta(deltas, &nalloc, ndeltas,
778 alloc_chunk_size, 0, fileoffset - offset0, blocklen);
782 fileoffset += blocklen;
783 if (fseeko(f, fileoffset, SEEK_SET) == -1) {
784 err = got_error_from_errno("fseeko");
797 const struct got_error *
798 got_deltify_mem_file(struct got_delta_instruction **deltas, int *ndeltas,
799 uint8_t *data, off_t fileoffset, off_t filesize, uint32_t seed,
800 struct got_delta_table *dt, FILE *basefile,
801 off_t basefile_offset0, off_t basefile_size)
803 const struct got_error *err = NULL;
804 const off_t offset0 = fileoffset;
806 const size_t alloc_chunk_size = 64;
811 *deltas = reallocarray(NULL, alloc_chunk_size,
812 sizeof(struct got_delta_instruction));
814 return got_error_from_errno("reallocarray");
815 nalloc = alloc_chunk_size;
817 while (fileoffset < filesize) {
819 struct got_delta_block *block;
820 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
824 /* Source remainder from the file itself. */
825 if (fileoffset < filesize) {
826 err = emitdelta(deltas, &nalloc, ndeltas,
827 alloc_chunk_size, 0, fileoffset - offset0,
828 filesize - fileoffset);
832 err = lookupblk(&block, dt, data + fileoffset, blocklen, seed,
833 basefile, basefile_offset0);
838 * We have found a matching block in the delta base.
839 * Attempt to stretch the block as far as possible and
840 * generate a copy instruction.
842 err = stretchblk_mem_file(basefile, basefile_offset0,
843 block, data, fileoffset + blocklen, filesize,
847 err = emitdelta(deltas, &nalloc, ndeltas,
848 alloc_chunk_size, 1, block->offset, blocklen);
854 * This block needs to be sourced from the file itself.
856 err = emitdelta(deltas, &nalloc, ndeltas,
857 alloc_chunk_size, 0, fileoffset - offset0, blocklen);
861 fileoffset += blocklen;
872 const struct got_error *
873 got_deltify_mem_mem(struct got_delta_instruction **deltas, int *ndeltas,
874 uint8_t *data, off_t fileoffset, off_t filesize, uint32_t seed,
875 struct got_delta_table *dt, uint8_t *basedata,
876 off_t basefile_offset0, off_t basefile_size)
878 const struct got_error *err = NULL;
879 const off_t offset0 = fileoffset;
881 const size_t alloc_chunk_size = 64;
886 *deltas = reallocarray(NULL, alloc_chunk_size,
887 sizeof(struct got_delta_instruction));
889 return got_error_from_errno("reallocarray");
890 nalloc = alloc_chunk_size;
892 while (fileoffset < filesize) {
894 struct got_delta_block *block;
895 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
899 /* Source remainder from the file itself. */
900 if (fileoffset < filesize) {
901 err = emitdelta(deltas, &nalloc, ndeltas,
902 alloc_chunk_size, 0, fileoffset - offset0,
903 filesize - fileoffset);
907 err = lookupblk_mem(&block, dt, data + fileoffset, blocklen,
908 seed, basedata, basefile_offset0);
913 * We have found a matching block in the delta base.
914 * Attempt to stretch the block as far as possible and
915 * generate a copy instruction.
917 err = stretchblk_mem_mem(basedata, basefile_offset0,
918 basefile_size, block, data, fileoffset + blocklen,
919 filesize, &blocklen);
922 err = emitdelta(deltas, &nalloc, ndeltas,
923 alloc_chunk_size, 1, block->offset, blocklen);
929 * This block needs to be sourced from the file itself.
931 err = emitdelta(deltas, &nalloc, ndeltas,
932 alloc_chunk_size, 0, fileoffset - offset0, blocklen);
936 fileoffset += blocklen;