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 "got_compat.h"
20 #include <sys/types.h>
21 #include <sys/queue.h>
31 #include "got_error.h"
33 #include "got_lib_deltify.h"
34 #include "murmurhash2.h"
37 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
41 * The algorihm used here is FastCDC (Fast Content-Defined Chunking)
42 * https://www.usenix.org/conference/atc16/technical-sessions/presentation/xia
45 static const uint32_t geartab[256] = {
46 0x67ed26b7, 0x32da500c, 0x53d0fee0, 0xce387dc7, 0xcd406d90, 0x2e83a4d4,
47 0x9fc9a38d, 0xb67259dc, 0xca6b1722, 0x6d2ea08c, 0x235cea2e, 0x3149bb5f,
48 0x1beda787, 0x2a6b77d5, 0x2f22d9ac, 0x91fc0544, 0xe413acfa, 0x5a30ff7a,
49 0xad6fdde0, 0x444fd0f5, 0x7ad87864, 0x58c5ff05, 0x8d2ec336, 0x2371f853,
50 0x550f8572, 0x6aa448dd, 0x7c9ddbcf, 0x95221e14, 0x2a82ec33, 0xcbec5a78,
51 0xc6795a0d, 0x243995b7, 0x1c909a2f, 0x4fded51c, 0x635d334b, 0x0e2b9999,
52 0x2702968d, 0x856de1d5, 0x3325d60e, 0xeb6a7502, 0xec2a9844, 0x0905835a,
53 0xa1820375, 0xa4be5cab, 0x96a6c058, 0x2c2ccd70, 0xba40fce3, 0xd794c46b,
54 0x8fbae83e, 0xc3aa7899, 0x3d3ff8ed, 0xa0d42b5b, 0x571c0c97, 0xd2811516,
55 0xf7e7b96c, 0x4fd2fcbd, 0xe2fdec94, 0x282cc436, 0x78e8e95c, 0x80a3b613,
56 0xcfbee20c, 0xd4a32d1c, 0x2a12ff13, 0x6af82936, 0xe5630258, 0x8efa6a98,
57 0x294fb2d1, 0xdeb57086, 0x5f0fddb3, 0xeceda7ce, 0x4c87305f, 0x3a6d3307,
58 0xe22d2942, 0x9d060217, 0x1e42ed02, 0xb6f63b52, 0x4367f39f, 0x055cf262,
59 0x03a461b2, 0x5ef9e382, 0x386bc03a, 0x2a1e79c7, 0xf1a0058b, 0xd4d2dea9,
60 0x56baf37d, 0x5daff6cc, 0xf03a951d, 0xaef7de45, 0xa8f4581e, 0x3960b555,
61 0xffbfff6d, 0xbe702a23, 0x8f5b6d6f, 0x061739fb, 0x98696f47, 0x3fd596d4,
62 0x151eac6b, 0xa9fcc4f5, 0x69181a12, 0x3ac5a107, 0xb5198fe7, 0x96bcb1da,
63 0x1b5ddf8e, 0xc757d650, 0x65865c3a, 0x8fc0a41a, 0x87435536, 0x99eda6f2,
64 0x41874794, 0x29cff4e8, 0xb70efd9a, 0x3103f6e7, 0x84d2453b, 0x15a450bd,
65 0x74f49af1, 0x60f664b1, 0xa1c86935, 0xfdafbce1, 0xe36353e3, 0x5d9ba739,
66 0xbc0559ba, 0x708b0054, 0xd41d808c, 0xb2f31723, 0x9027c41f, 0xf136d165,
67 0xb5374b12, 0x9420a6ac, 0x273958b6, 0xe6c2fad0, 0xebdc1f21, 0xfb33af8b,
68 0xc71c25cd, 0xe9a2d8e5, 0xbeb38a50, 0xbceb7cc2, 0x4e4e73f0, 0xcd6c251d,
69 0xde4c032c, 0x4b04ac30, 0x725b8b21, 0x4eb8c33b, 0x20d07b75, 0x0567aa63,
70 0xb56b2bb7, 0xc1f5fd3a, 0xcafd35ca, 0x470dd4da, 0xfe4f94cd, 0xfb8de424,
71 0xe8dbcf40, 0xfe50a37a, 0x62db5b5d, 0xf32f4ab6, 0x2c4a8a51, 0x18473dc0,
72 0xfe0cbb6e, 0xfe399efd, 0xdf34ecc9, 0x6ccd5055, 0x46097073, 0x139135c2,
73 0x721c76f6, 0x1c6a94b4, 0x6eee014d, 0x8a508e02, 0x3da538f5, 0x280d394f,
74 0x5248a0c4, 0x3ce94c6c, 0x9a71ad3a, 0x8493dd05, 0xe43f0ab6, 0x18e4ed42,
75 0x6c5c0e09, 0x42b06ec9, 0x8d330343, 0xa45b6f59, 0x2a573c0c, 0xd7fd3de6,
76 0xeedeab68, 0x5c84dafc, 0xbbd1b1a8, 0xa3ce1ad1, 0x85b70bed, 0xb6add07f,
77 0xa531309c, 0x8f8ab852, 0x564de332, 0xeac9ed0c, 0x73da402c, 0x3ec52761,
78 0x43af2f4d, 0xd6ff45c8, 0x4c367462, 0xd553bd6a, 0x44724855, 0x3b2aa728,
79 0x56e5eb65, 0xeaf16173, 0x33fa42ff, 0xd714bb5d, 0xfbd0a3b9, 0xaf517134,
80 0x9416c8cd, 0x534cf94f, 0x548947c2, 0x34193569, 0x32f4389a, 0xfe7028bc,
81 0xed73b1ed, 0x9db95770, 0x468e3922, 0x0440c3cd, 0x60059a62, 0x33504562,
82 0x2b229fbd, 0x5174dca5, 0xf7028752, 0xd63c6aa8, 0x31276f38, 0x0646721c,
83 0xb0191da8, 0xe00e6de0, 0x9eac1a6e, 0x9f7628a5, 0xed6c06ea, 0x0bb8af15,
84 0xf119fb12, 0x38693c1c, 0x732bc0fe, 0x84953275, 0xb82ec888, 0x33a4f1b3,
85 0x3099835e, 0x028a8782, 0x5fdd51d7, 0xc6c717b3, 0xb06caf71, 0x17c8c111,
86 0x61bad754, 0x9fd03061, 0xe09df1af, 0x3bc9eb73, 0x85878413, 0x9889aaf2,
87 0x3f5a9e46, 0x42c9f01f, 0x9984a4f4, 0xd5de43cc, 0xd294daed, 0xbecba2d2,
88 0xf1f6e72c, 0x5551128a, 0x83af87e2, 0x6f0342ba,
92 hashblk(const unsigned char *p, off_t n, uint32_t seed)
94 return murmurhash2(p, n, seed);
97 static const struct got_error *
98 addblk(struct got_delta_table *dt, FILE *f, off_t file_offset0, off_t len,
99 off_t offset, uint32_t h)
101 const struct got_error *err = NULL;
103 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
104 uint8_t buf2[GOT_DELTIFY_MAXCHUNK];
111 while (dt->blocks[i].len != 0) {
113 * Avoid adding duplicate blocks.
114 * NB: A matching hash is insufficient for detecting equality.
115 * The hash can only detect inequality.
117 if (len == dt->blocks[i].len && h == dt->blocks[i].hash) {
119 if (fseeko(f, file_offset0 + offset, SEEK_SET) == -1)
120 return got_error_from_errno("fseeko");
121 r = fread(buf, 1, len, f);
125 return got_ferror(f, GOT_ERR_IO);
128 if (fseeko(f, file_offset0 + dt->blocks[i].offset,
130 return got_error_from_errno("fseeko");
131 if (fread(buf2, 1, len, f) != len)
132 return got_ferror(f, GOT_ERR_IO);
133 if (memcmp(buf, buf2, len) == 0)
137 i = (i + 1) % dt->nalloc;
139 assert(dt->blocks[i].len == 0);
140 dt->blocks[i].len = len;
141 dt->blocks[i].offset = offset;
142 dt->blocks[i].hash = h;
144 if (dt->nalloc < dt->nblocks + 64) {
145 struct got_delta_block *db;
146 size_t old_size = dt->nalloc;
148 dt->blocks = calloc(dt->nalloc + 64,
149 sizeof(struct got_delta_block));
150 if (dt->blocks == NULL) {
151 err = got_error_from_errno("calloc");
157 * Recompute all block positions. Hash-based indices of blocks
158 * in the array depend on the allocated length of the array.
161 for (i = 0; i < old_size; i++) {
164 err = addblk(dt, f, file_offset0, db[i].len,
165 db[i].offset, db[i].hash);
175 static const struct got_error *
176 addblk_mem(struct got_delta_table *dt, uint8_t *data, off_t file_offset0,
177 off_t len, off_t offset, uint32_t h)
179 const struct got_error *err = NULL;
188 while (dt->blocks[i].len != 0) {
190 * Avoid adding duplicate blocks.
191 * NB: A matching hash is insufficient for detecting equality.
192 * The hash can only detect inequality.
194 if (len == dt->blocks[i].len && h == dt->blocks[i].hash) {
195 block1 = data + file_offset0 + dt->blocks[i].offset;
196 block2 = data + file_offset0 + offset;
197 if (memcmp(block1, block2, len) == 0)
201 i = (i + 1) % dt->nalloc;
203 assert(dt->blocks[i].len == 0);
204 dt->blocks[i].len = len;
205 dt->blocks[i].offset = offset;
206 dt->blocks[i].hash = h;
208 if (dt->nalloc < dt->nblocks + 64) {
209 struct got_delta_block *db;
210 size_t old_size = dt->nalloc;
212 dt->blocks = calloc(dt->nalloc + 64,
213 sizeof(struct got_delta_block));
214 if (dt->blocks == NULL) {
215 err = got_error_from_errno("calloc");
221 * Recompute all block positions. Hash-based indices of blocks
222 * in the array depend on the allocated length of the array.
225 for (i = 0; i < old_size; i++) {
228 err = addblk_mem(dt, data, file_offset0, db[i].len,
229 db[i].offset, db[i].hash);
239 static const struct got_error *
240 lookupblk(struct got_delta_block **block, struct got_delta_table *dt,
241 unsigned char *p, off_t len, uint32_t seed, FILE *basefile,
242 off_t basefile_offset0)
246 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
251 h = hashblk(p, len, seed);
252 for (i = h % dt->nalloc; dt->blocks[i].len != 0;
253 i = (i + 1) % dt->nalloc) {
254 if (dt->blocks[i].hash != h ||
255 dt->blocks[i].len != len)
257 if (fseeko(basefile, basefile_offset0 + dt->blocks[i].offset,
259 return got_error_from_errno("fseeko");
260 r = fread(buf, 1, len, basefile);
262 return got_ferror(basefile, GOT_ERR_IO);
263 if (memcmp(p, buf, len) == 0) {
264 *block = &dt->blocks[i];
271 static const struct got_error *
272 lookupblk_mem(struct got_delta_block **block, struct got_delta_table *dt,
273 unsigned char *p, off_t len, uint32_t seed, uint8_t *basedata,
274 off_t basefile_offset0)
282 h = hashblk(p, len, seed);
283 for (i = h % dt->nalloc; dt->blocks[i].len != 0;
284 i = (i + 1) % dt->nalloc) {
285 if (dt->blocks[i].hash != h ||
286 dt->blocks[i].len != len)
288 b = basedata + basefile_offset0 + dt->blocks[i].offset;
289 if (memcmp(p, b, len) == 0) {
290 *block = &dt->blocks[i];
297 static const struct got_error *
298 nextblk(uint8_t *buf, off_t *blocklen, FILE *f)
301 const unsigned char *p;
303 off_t pos = ftello(f);
307 r = fread(buf, 1, GOT_DELTIFY_MAXCHUNK, f);
308 if (r == 0 && ferror(f))
309 return got_ferror(f, GOT_ERR_IO);
310 if (r < GOT_DELTIFY_MINCHUNK)
311 return NULL; /* no more delta-worthy blocks left */
313 /* Got a deltifiable block. Find the split-point where it ends. */
314 p = buf + GOT_DELTIFY_MINCHUNK;
316 while (p != buf + r) {
317 gh = (gh << 1) + geartab[*p++];
318 if ((gh & GOT_DELTIFY_SPLITMASK) == 0)
322 *blocklen = (p - buf);
323 if (fseeko(f, pos + *blocklen, SEEK_SET) == -1)
324 return got_error_from_errno("fseeko");
329 static const struct got_error *
330 nextblk_mem(off_t *blocklen, uint8_t *data, off_t fileoffset, off_t filesize)
333 const unsigned char *p;
337 if (fileoffset >= filesize ||
338 filesize - fileoffset < GOT_DELTIFY_MINCHUNK)
339 return NULL; /* no more delta-worthy blocks left */
341 /* Got a deltifiable block. Find the split-point where it ends. */
342 p = data + fileoffset + GOT_DELTIFY_MINCHUNK;
344 while (p != data + MIN(fileoffset + GOT_DELTIFY_MAXCHUNK, filesize)) {
345 gh = (gh << 1) + geartab[*p++];
346 if ((gh & GOT_DELTIFY_SPLITMASK) == 0)
350 *blocklen = (p - (data + fileoffset));
354 const struct got_error *
355 got_deltify_init(struct got_delta_table **dt, FILE *f, off_t fileoffset,
356 off_t filesize, uint32_t seed)
358 const struct got_error *err = NULL;
360 const off_t offset0 = fileoffset;
362 *dt = calloc(1, sizeof(**dt));
364 return got_error_from_errno("calloc");
368 (*dt)->blocks = calloc((*dt)->nalloc, sizeof(struct got_delta_block));
369 if ((*dt)->blocks == NULL) {
370 err = got_error_from_errno("calloc");
374 if (fseeko(f, fileoffset, SEEK_SET) == -1)
375 return got_error_from_errno("fseeko");
377 while (fileoffset < filesize) {
378 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
380 err = nextblk(buf, &blocklen, f);
385 h = hashblk(buf, blocklen, seed);
386 err = addblk(*dt, f, offset0, blocklen,
387 fileoffset - offset0, h);
390 fileoffset += blocklen;
391 if (fseeko(f, fileoffset, SEEK_SET) == -1)
392 return got_error_from_errno("fseeko");
404 const struct got_error *
405 got_deltify_init_mem(struct got_delta_table **dt, uint8_t *data,
406 off_t fileoffset, off_t filesize, uint32_t seed)
408 const struct got_error *err = NULL;
410 const off_t offset0 = fileoffset;
412 *dt = calloc(1, sizeof(**dt));
414 return got_error_from_errno("calloc");
418 (*dt)->blocks = calloc((*dt)->nalloc, sizeof(struct got_delta_block));
419 if ((*dt)->blocks == NULL) {
420 err = got_error_from_errno("calloc");
424 while (fileoffset < filesize) {
426 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
431 h = hashblk(data + fileoffset, blocklen, seed);
432 err = addblk_mem(*dt, data, offset0, blocklen,
433 fileoffset - offset0, h);
436 fileoffset += blocklen;
449 got_deltify_free(struct got_delta_table *dt)
457 static const struct got_error *
458 emitdelta(struct got_delta_instruction **deltas, size_t *nalloc, int *ndeltas,
459 const size_t alloc_chunk_size, int copy, off_t offset, off_t len)
461 struct got_delta_instruction *d, *p;
463 if (*nalloc < *ndeltas + alloc_chunk_size) {
464 p = reallocarray(*deltas, *nalloc + alloc_chunk_size,
465 sizeof(struct got_delta_instruction));
467 return got_error_from_errno("reallocarray");
469 *nalloc += alloc_chunk_size;
472 d = &(*deltas)[*ndeltas - 1];
479 static const struct got_error *
480 stretchblk(FILE *basefile, off_t base_offset0, struct got_delta_block *block,
481 FILE *f, off_t filesize, off_t *blocklen)
483 uint8_t basebuf[GOT_DELTIFY_MAXCHUNK], buf[GOT_DELTIFY_MAXCHUNK];
487 if (fseeko(basefile, base_offset0 + block->offset + *blocklen,
489 return got_error_from_errno("fseeko");
491 while (buf_equal && *blocklen < (1 << 24) - 1) {
492 base_r = fread(basebuf, 1, sizeof(basebuf), basefile);
494 if (ferror(basefile))
495 return got_ferror(basefile, GOT_ERR_IO);
498 r = fread(buf, 1, sizeof(buf), f);
501 return got_ferror(f, GOT_ERR_IO);
504 for (i = 0; i < MIN(base_r, r); i++) {
505 if (buf[i] != basebuf[i]) {
516 static const struct got_error *
517 stretchblk_file_mem(uint8_t *basedata, off_t base_offset0, off_t basefile_size,
518 struct got_delta_block *block, FILE *f, off_t filesize, off_t *blocklen)
520 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
523 off_t base_offset = base_offset0 + block->offset + *blocklen;
525 if (base_offset > basefile_size) {
526 return got_error_fmt(GOT_ERR_RANGE,
527 "read beyond the size of delta base at offset %lld",
528 (long long)base_offset);
531 while (buf_equal && *blocklen < (1 << 24) - 1) {
532 if (base_offset + *blocklen >= basefile_size)
534 r = fread(buf, 1, sizeof(buf), f);
537 return got_ferror(f, GOT_ERR_IO);
540 for (i = 0; i < MIN(basefile_size - base_offset, r); i++) {
541 if (buf[i] != *(basedata + base_offset + i)) {
552 static const struct got_error *
553 stretchblk_mem_file(FILE *basefile, off_t base_offset0,
554 struct got_delta_block *block, uint8_t *data, off_t fileoffset,
555 off_t filesize, off_t *blocklen)
557 uint8_t basebuf[GOT_DELTIFY_MAXCHUNK];
561 if (fileoffset > filesize) {
562 return got_error_fmt(GOT_ERR_RANGE,
563 "read beyond the size of deltify file at offset %lld",
564 (long long)fileoffset);
567 if (fseeko(basefile, base_offset0 + block->offset + *blocklen,
569 return got_error_from_errno("fseeko");
571 while (buf_equal && *blocklen < (1 << 24) - 1) {
572 if (fileoffset + *blocklen >= filesize)
574 base_r = fread(basebuf, 1, sizeof(basebuf), basefile);
576 if (ferror(basefile))
577 return got_ferror(basefile, GOT_ERR_IO);
580 for (i = 0; i < MIN(base_r, filesize - fileoffset); i++) {
581 if (*(data + fileoffset + i) != basebuf[i]) {
592 static const struct got_error *
593 stretchblk_mem_mem(uint8_t *basedata, off_t base_offset0, off_t basefile_size,
594 struct got_delta_block *block, uint8_t *data, off_t fileoffset,
595 off_t filesize, off_t *blocklen)
598 off_t base_offset = base_offset0 + block->offset + *blocklen;
601 if (base_offset > basefile_size) {
602 return got_error_fmt(GOT_ERR_RANGE,
603 "read beyond the size of delta base at offset %lld",
604 (long long)base_offset);
607 if (fileoffset > filesize) {
608 return got_error_fmt(GOT_ERR_RANGE,
609 "read beyond the size of deltify file at offset %lld",
610 (long long)fileoffset);
613 p = data + fileoffset;
614 q = basedata + base_offset;
615 maxlen = MIN(basefile_size - base_offset, filesize - fileoffset);
616 for (i = 0; i < maxlen && *blocklen < (1 << 24) - 1; i++) {
625 const struct got_error *
626 got_deltify(struct got_delta_instruction **deltas, int *ndeltas,
627 FILE *f, off_t fileoffset, off_t filesize, uint32_t seed,
628 struct got_delta_table *dt, FILE *basefile,
629 off_t basefile_offset0, off_t basefile_size)
631 const struct got_error *err = NULL;
632 const off_t offset0 = fileoffset;
634 const size_t alloc_chunk_size = 64;
640 * offset0 indicates where data to be deltified begins.
641 * For example, we want to avoid deltifying a Git object header at
642 * the beginning of the file.
644 if (fseeko(f, offset0, SEEK_SET) == -1)
645 return got_error_from_errno("fseeko");
647 *deltas = reallocarray(NULL, alloc_chunk_size,
648 sizeof(struct got_delta_instruction));
650 return got_error_from_errno("reallocarray");
651 nalloc = alloc_chunk_size;
653 while (fileoffset < filesize) {
654 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
656 struct got_delta_block *block;
657 err = nextblk(buf, &blocklen, f);
661 /* Source remainder from the file itself. */
662 if (fileoffset < filesize) {
663 err = emitdelta(deltas, &nalloc, ndeltas,
664 alloc_chunk_size, 0, fileoffset - offset0,
665 filesize - fileoffset);
669 err = lookupblk(&block, dt, buf, blocklen, seed, basefile,
675 * We have found a matching block in the delta base.
676 * Attempt to stretch the block as far as possible and
677 * generate a copy instruction.
679 err = stretchblk(basefile, basefile_offset0, block,
680 f, filesize, &blocklen);
683 err = emitdelta(deltas, &nalloc, ndeltas,
684 alloc_chunk_size, 1, block->offset, blocklen);
690 * This block needs to be sourced from the file itself.
692 err = emitdelta(deltas, &nalloc, ndeltas,
693 alloc_chunk_size, 0, fileoffset - offset0, blocklen);
697 fileoffset += blocklen;
698 if (fseeko(f, fileoffset, SEEK_SET) == -1) {
699 err = got_error_from_errno("fseeko");
712 const struct got_error *
713 got_deltify_file_mem(struct got_delta_instruction **deltas, int *ndeltas,
714 FILE *f, off_t fileoffset, off_t filesize, uint32_t seed,
715 struct got_delta_table *dt, uint8_t *basedata,
716 off_t basefile_offset0, off_t basefile_size)
718 const struct got_error *err = NULL;
719 const off_t offset0 = fileoffset;
721 const size_t alloc_chunk_size = 64;
727 * offset0 indicates where data to be deltified begins.
728 * For example, we want to avoid deltifying a Git object header at
729 * the beginning of the file.
731 if (fseeko(f, offset0, SEEK_SET) == -1)
732 return got_error_from_errno("fseeko");
734 *deltas = reallocarray(NULL, alloc_chunk_size,
735 sizeof(struct got_delta_instruction));
737 return got_error_from_errno("reallocarray");
738 nalloc = alloc_chunk_size;
740 while (fileoffset < filesize) {
741 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
743 struct got_delta_block *block;
744 err = nextblk(buf, &blocklen, f);
748 /* Source remainder from the file itself. */
749 if (fileoffset < filesize) {
750 err = emitdelta(deltas, &nalloc, ndeltas,
751 alloc_chunk_size, 0, fileoffset - offset0,
752 filesize - fileoffset);
756 err = lookupblk_mem(&block, dt, buf, blocklen, seed, basedata,
762 * We have found a matching block in the delta base.
763 * Attempt to stretch the block as far as possible and
764 * generate a copy instruction.
766 err = stretchblk_file_mem(basedata, basefile_offset0,
767 basefile_size, block, f, filesize, &blocklen);
770 err = emitdelta(deltas, &nalloc, ndeltas,
771 alloc_chunk_size, 1, block->offset, blocklen);
777 * This block needs to be sourced from the file itself.
779 err = emitdelta(deltas, &nalloc, ndeltas,
780 alloc_chunk_size, 0, fileoffset - offset0, blocklen);
784 fileoffset += blocklen;
785 if (fseeko(f, fileoffset, SEEK_SET) == -1) {
786 err = got_error_from_errno("fseeko");
799 const struct got_error *
800 got_deltify_mem_file(struct got_delta_instruction **deltas, int *ndeltas,
801 uint8_t *data, off_t fileoffset, off_t filesize, uint32_t seed,
802 struct got_delta_table *dt, FILE *basefile,
803 off_t basefile_offset0, off_t basefile_size)
805 const struct got_error *err = NULL;
806 const off_t offset0 = fileoffset;
808 const size_t alloc_chunk_size = 64;
813 *deltas = reallocarray(NULL, alloc_chunk_size,
814 sizeof(struct got_delta_instruction));
816 return got_error_from_errno("reallocarray");
817 nalloc = alloc_chunk_size;
819 while (fileoffset < filesize) {
821 struct got_delta_block *block;
822 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
826 /* Source remainder from the file itself. */
827 if (fileoffset < filesize) {
828 err = emitdelta(deltas, &nalloc, ndeltas,
829 alloc_chunk_size, 0, fileoffset - offset0,
830 filesize - fileoffset);
834 err = lookupblk(&block, dt, data + fileoffset, blocklen, seed,
835 basefile, basefile_offset0);
840 * We have found a matching block in the delta base.
841 * Attempt to stretch the block as far as possible and
842 * generate a copy instruction.
844 err = stretchblk_mem_file(basefile, basefile_offset0,
845 block, data, fileoffset + blocklen, filesize,
849 err = emitdelta(deltas, &nalloc, ndeltas,
850 alloc_chunk_size, 1, block->offset, blocklen);
856 * This block needs to be sourced from the file itself.
858 err = emitdelta(deltas, &nalloc, ndeltas,
859 alloc_chunk_size, 0, fileoffset - offset0, blocklen);
863 fileoffset += blocklen;
874 const struct got_error *
875 got_deltify_mem_mem(struct got_delta_instruction **deltas, int *ndeltas,
876 uint8_t *data, off_t fileoffset, off_t filesize, uint32_t seed,
877 struct got_delta_table *dt, uint8_t *basedata,
878 off_t basefile_offset0, off_t basefile_size)
880 const struct got_error *err = NULL;
881 const off_t offset0 = fileoffset;
883 const size_t alloc_chunk_size = 64;
888 *deltas = reallocarray(NULL, alloc_chunk_size,
889 sizeof(struct got_delta_instruction));
891 return got_error_from_errno("reallocarray");
892 nalloc = alloc_chunk_size;
894 while (fileoffset < filesize) {
896 struct got_delta_block *block;
897 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
901 /* Source remainder from the file itself. */
902 if (fileoffset < filesize) {
903 err = emitdelta(deltas, &nalloc, ndeltas,
904 alloc_chunk_size, 0, fileoffset - offset0,
905 filesize - fileoffset);
909 err = lookupblk_mem(&block, dt, data + fileoffset, blocklen,
910 seed, basedata, basefile_offset0);
915 * We have found a matching block in the delta base.
916 * Attempt to stretch the block as far as possible and
917 * generate a copy instruction.
919 err = stretchblk_mem_mem(basedata, basefile_offset0,
920 basefile_size, block, data, fileoffset + blocklen,
921 filesize, &blocklen);
924 err = emitdelta(deltas, &nalloc, ndeltas,
925 alloc_chunk_size, 1, block->offset, blocklen);
931 * This block needs to be sourced from the file itself.
933 err = emitdelta(deltas, &nalloc, ndeltas,
934 alloc_chunk_size, 0, fileoffset - offset0, blocklen);
938 fileoffset += blocklen;