portable: release 0.93
[got-portable.git] / lib / deltify.c
blob1e15dde9d8968aefdeb7e7e8b17ed022ff243ff8
1 /*
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>
23 #include <assert.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "got_error.h"
33 #include "got_lib_deltify.h"
34 #include "murmurhash2.h"
36 #ifndef MIN
37 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 #endif
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,
91 static uint32_t
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;
102 int i;
103 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
104 uint8_t buf2[GOT_DELTIFY_MAXCHUNK];
105 size_t r = 0;
107 if (len == 0)
108 return NULL;
110 i = h % dt->nalloc;
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) {
118 if (r == 0) {
119 if (fseeko(f, file_offset0 + offset, SEEK_SET) == -1)
120 return got_error_from_errno("fseeko");
121 r = fread(buf, 1, len, f);
122 if (r != len) {
123 if (!ferror(f))
124 return NULL;
125 return got_ferror(f, GOT_ERR_IO);
128 if (fseeko(f, file_offset0 + dt->blocks[i].offset,
129 SEEK_SET) == -1)
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)
134 return NULL;
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;
143 dt->nblocks++;
144 if (dt->nalloc < dt->nblocks + 256) {
145 struct got_delta_block *db;
146 size_t old_size = dt->nalloc;
147 db = dt->blocks;
148 dt->blocks = calloc(dt->nalloc + 256,
149 sizeof(struct got_delta_block));
150 if (dt->blocks == NULL) {
151 err = got_error_from_errno("calloc");
152 dt->blocks = db;
153 return err;
155 dt->nalloc += 256;
157 * Recompute all block positions. Hash-based indices of blocks
158 * in the array depend on the allocated length of the array.
160 dt->nblocks = 0;
161 for (i = 0; i < old_size; i++) {
162 if (db[i].len == 0)
163 continue;
164 err = addblk(dt, f, file_offset0, db[i].len,
165 db[i].offset, db[i].hash);
166 if (err)
167 break;
169 free(db);
172 return err;
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;
180 int i;
181 uint8_t *block1;
182 uint8_t *block2;
184 if (len == 0)
185 return NULL;
187 i = h % dt->nalloc;
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)
198 return NULL;
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;
207 dt->nblocks++;
208 if (dt->nalloc < dt->nblocks + 256) {
209 struct got_delta_block *db;
210 size_t old_size = dt->nalloc;
211 db = dt->blocks;
212 dt->blocks = calloc(dt->nalloc + 256,
213 sizeof(struct got_delta_block));
214 if (dt->blocks == NULL) {
215 err = got_error_from_errno("calloc");
216 dt->blocks = db;
217 return err;
219 dt->nalloc += 256;
221 * Recompute all block positions. Hash-based indices of blocks
222 * in the array depend on the allocated length of the array.
224 dt->nblocks = 0;
225 for (i = 0; i < old_size; i++) {
226 if (db[i].len == 0)
227 continue;
228 err = addblk_mem(dt, data, file_offset0, db[i].len,
229 db[i].offset, db[i].hash);
230 if (err)
231 break;
233 free(db);
236 return err;
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)
244 int i;
245 uint32_t h;
246 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
247 size_t r;
249 *block = NULL;
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)
256 continue;
257 if (fseeko(basefile, basefile_offset0 + dt->blocks[i].offset,
258 SEEK_SET) == -1)
259 return got_error_from_errno("fseeko");
260 r = fread(buf, 1, len, basefile);
261 if (r != len)
262 return got_ferror(basefile, GOT_ERR_IO);
263 if (memcmp(p, buf, len) == 0) {
264 *block = &dt->blocks[i];
265 break;
268 return NULL;
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)
276 int i;
277 uint32_t h;
278 uint8_t *b;
280 *block = NULL;
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)
287 continue;
288 b = basedata + basefile_offset0 + dt->blocks[i].offset;
289 if (memcmp(p, b, len) == 0) {
290 *block = &dt->blocks[i];
291 break;
294 return NULL;
297 static const struct got_error *
298 nextblk(uint8_t *buf, off_t *blocklen, FILE *f)
300 uint32_t gh;
301 const unsigned char *p;
302 size_t r;
303 off_t pos = ftello(f);
305 *blocklen = 0;
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;
315 gh = 0;
316 while (p != buf + r) {
317 gh = (gh << 1) + geartab[*p++];
318 if ((gh & GOT_DELTIFY_SPLITMASK) == 0)
319 break;
322 *blocklen = (p - buf);
323 if (fseeko(f, pos + *blocklen, SEEK_SET) == -1)
324 return got_error_from_errno("fseeko");
326 return NULL;
329 static const struct got_error *
330 nextblk_mem(off_t *blocklen, uint8_t *data, off_t fileoffset, off_t filesize)
332 uint32_t gh;
333 const unsigned char *p;
335 *blocklen = 0;
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;
343 gh = 0;
344 while (p != data + MIN(fileoffset + GOT_DELTIFY_MAXCHUNK, filesize)) {
345 gh = (gh << 1) + geartab[*p++];
346 if ((gh & GOT_DELTIFY_SPLITMASK) == 0)
347 break;
350 *blocklen = (p - (data + fileoffset));
351 return NULL;
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;
359 uint32_t h;
360 const off_t offset0 = fileoffset;
362 *dt = calloc(1, sizeof(**dt));
363 if (*dt == NULL)
364 return got_error_from_errno("calloc");
366 (*dt)->nblocks = 0;
367 (*dt)->nalloc = 128;
368 (*dt)->blocks = calloc((*dt)->nalloc, sizeof(struct got_delta_block));
369 if ((*dt)->blocks == NULL) {
370 err = got_error_from_errno("calloc");
371 goto done;
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];
379 off_t blocklen;
380 err = nextblk(buf, &blocklen, f);
381 if (err)
382 goto done;
383 if (blocklen == 0)
384 break;
385 h = hashblk(buf, blocklen, seed);
386 err = addblk(*dt, f, offset0, blocklen,
387 fileoffset - offset0, h);
388 if (err)
389 goto done;
390 fileoffset += blocklen;
391 if (fseeko(f, fileoffset, SEEK_SET) == -1)
392 return got_error_from_errno("fseeko");
394 done:
395 if (err) {
396 free((*dt)->blocks);
397 free(*dt);
398 *dt = NULL;
401 return err;
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;
409 uint32_t h;
410 const off_t offset0 = fileoffset;
412 *dt = calloc(1, sizeof(**dt));
413 if (*dt == NULL)
414 return got_error_from_errno("calloc");
416 (*dt)->nblocks = 0;
417 (*dt)->nalloc = 128;
418 (*dt)->blocks = calloc((*dt)->nalloc, sizeof(struct got_delta_block));
419 if ((*dt)->blocks == NULL) {
420 err = got_error_from_errno("calloc");
421 goto done;
424 while (fileoffset < filesize) {
425 off_t blocklen;
426 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
427 if (err)
428 goto done;
429 if (blocklen == 0)
430 break;
431 h = hashblk(data + fileoffset, blocklen, seed);
432 err = addblk_mem(*dt, data, offset0, blocklen,
433 fileoffset - offset0, h);
434 if (err)
435 goto done;
436 fileoffset += blocklen;
438 done:
439 if (err) {
440 free((*dt)->blocks);
441 free(*dt);
442 *dt = NULL;
445 return err;
448 void
449 got_deltify_free(struct got_delta_table *dt)
451 if (dt == NULL)
452 return;
453 free(dt->blocks);
454 free(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));
466 if (p == NULL)
467 return got_error_from_errno("reallocarray");
468 *deltas = p;
469 *nalloc += alloc_chunk_size;
471 *ndeltas += 1;
472 d = &(*deltas)[*ndeltas - 1];
473 d->copy = copy;
474 d->offset = offset;
475 d->len = len;
476 return NULL;
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];
484 size_t base_r, r, i;
485 int buf_equal = 1;
487 if (fseeko(basefile, base_offset0 + block->offset + *blocklen,
488 SEEK_SET) == -1)
489 return got_error_from_errno("fseeko");
491 while (buf_equal && *blocklen < (1 << 24) - 1) {
492 base_r = fread(basebuf, 1, sizeof(basebuf), basefile);
493 if (base_r == 0) {
494 if (ferror(basefile))
495 return got_ferror(basefile, GOT_ERR_IO);
496 break;
498 r = fread(buf, 1, sizeof(buf), f);
499 if (r == 0) {
500 if (ferror(f))
501 return got_ferror(f, GOT_ERR_IO);
502 break;
504 for (i = 0; i < MIN(base_r, r); i++) {
505 if (buf[i] != basebuf[i]) {
506 buf_equal = 0;
507 break;
509 (*blocklen)++;
513 return NULL;
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];
521 size_t r, i;
522 int buf_equal = 1;
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)
533 break;
534 r = fread(buf, 1, sizeof(buf), f);
535 if (r == 0) {
536 if (ferror(f))
537 return got_ferror(f, GOT_ERR_IO);
538 break;
540 for (i = 0; i < MIN(basefile_size - base_offset, r); i++) {
541 if (buf[i] != *(basedata + base_offset + i)) {
542 buf_equal = 0;
543 break;
545 (*blocklen)++;
549 return NULL;
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];
558 size_t base_r, i;
559 int buf_equal = 1;
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,
568 SEEK_SET) == -1)
569 return got_error_from_errno("fseeko");
571 while (buf_equal && *blocklen < (1 << 24) - 1) {
572 if (fileoffset + *blocklen >= filesize)
573 break;
574 base_r = fread(basebuf, 1, sizeof(basebuf), basefile);
575 if (base_r == 0) {
576 if (ferror(basefile))
577 return got_ferror(basefile, GOT_ERR_IO);
578 break;
580 for (i = 0; i < MIN(base_r, filesize - fileoffset); i++) {
581 if (*(data + fileoffset + i) != basebuf[i]) {
582 buf_equal = 0;
583 break;
585 (*blocklen)++;
589 return NULL;
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)
597 off_t i, maxlen;
598 off_t base_offset = base_offset0 + block->offset + *blocklen;
599 uint8_t *p, *q;
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++) {
617 if (p[i] != q[i])
618 break;
619 (*blocklen)++;
622 return NULL;
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;
633 size_t nalloc = 0;
634 const size_t alloc_chunk_size = 64;
636 *deltas = NULL;
637 *ndeltas = 0;
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));
649 if (*deltas == NULL)
650 return got_error_from_errno("reallocarray");
651 nalloc = alloc_chunk_size;
653 while (fileoffset < filesize) {
654 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
655 off_t blocklen;
656 struct got_delta_block *block;
657 err = nextblk(buf, &blocklen, f);
658 if (err)
659 break;
660 if (blocklen == 0) {
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);
667 break;
669 err = lookupblk(&block, dt, buf, blocklen, seed, basefile,
670 basefile_offset0);
671 if (err)
672 break;
673 if (block != NULL) {
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);
681 if (err)
682 break;
683 err = emitdelta(deltas, &nalloc, ndeltas,
684 alloc_chunk_size, 1, block->offset, blocklen);
685 if (err)
686 break;
687 } else {
689 * No match.
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);
694 if (err)
695 break;
697 fileoffset += blocklen;
698 if (fseeko(f, fileoffset, SEEK_SET) == -1) {
699 err = got_error_from_errno("fseeko");
700 break;
704 if (err) {
705 free(*deltas);
706 *deltas = NULL;
707 *ndeltas = 0;
709 return err;
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;
720 size_t nalloc = 0;
721 const size_t alloc_chunk_size = 64;
723 *deltas = NULL;
724 *ndeltas = 0;
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));
736 if (*deltas == NULL)
737 return got_error_from_errno("reallocarray");
738 nalloc = alloc_chunk_size;
740 while (fileoffset < filesize) {
741 uint8_t buf[GOT_DELTIFY_MAXCHUNK];
742 off_t blocklen;
743 struct got_delta_block *block;
744 err = nextblk(buf, &blocklen, f);
745 if (err)
746 break;
747 if (blocklen == 0) {
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);
754 break;
756 err = lookupblk_mem(&block, dt, buf, blocklen, seed, basedata,
757 basefile_offset0);
758 if (err)
759 break;
760 if (block != NULL) {
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);
768 if (err)
769 break;
770 err = emitdelta(deltas, &nalloc, ndeltas,
771 alloc_chunk_size, 1, block->offset, blocklen);
772 if (err)
773 break;
774 } else {
776 * No match.
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);
781 if (err)
782 break;
784 fileoffset += blocklen;
785 if (fseeko(f, fileoffset, SEEK_SET) == -1) {
786 err = got_error_from_errno("fseeko");
787 break;
791 if (err) {
792 free(*deltas);
793 *deltas = NULL;
794 *ndeltas = 0;
796 return err;
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;
807 size_t nalloc = 0;
808 const size_t alloc_chunk_size = 64;
810 *deltas = NULL;
811 *ndeltas = 0;
813 *deltas = reallocarray(NULL, alloc_chunk_size,
814 sizeof(struct got_delta_instruction));
815 if (*deltas == NULL)
816 return got_error_from_errno("reallocarray");
817 nalloc = alloc_chunk_size;
819 while (fileoffset < filesize) {
820 off_t blocklen;
821 struct got_delta_block *block;
822 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
823 if (err)
824 break;
825 if (blocklen == 0) {
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);
832 break;
834 err = lookupblk(&block, dt, data + fileoffset, blocklen, seed,
835 basefile, basefile_offset0);
836 if (err)
837 break;
838 if (block != NULL) {
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,
846 &blocklen);
847 if (err)
848 break;
849 err = emitdelta(deltas, &nalloc, ndeltas,
850 alloc_chunk_size, 1, block->offset, blocklen);
851 if (err)
852 break;
853 } else {
855 * No match.
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);
860 if (err)
861 break;
863 fileoffset += blocklen;
866 if (err) {
867 free(*deltas);
868 *deltas = NULL;
869 *ndeltas = 0;
871 return err;
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;
882 size_t nalloc = 0;
883 const size_t alloc_chunk_size = 64;
885 *deltas = NULL;
886 *ndeltas = 0;
888 *deltas = reallocarray(NULL, alloc_chunk_size,
889 sizeof(struct got_delta_instruction));
890 if (*deltas == NULL)
891 return got_error_from_errno("reallocarray");
892 nalloc = alloc_chunk_size;
894 while (fileoffset < filesize) {
895 off_t blocklen;
896 struct got_delta_block *block;
897 err = nextblk_mem(&blocklen, data, fileoffset, filesize);
898 if (err)
899 break;
900 if (blocklen == 0) {
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);
907 break;
909 err = lookupblk_mem(&block, dt, data + fileoffset, blocklen,
910 seed, basedata, basefile_offset0);
911 if (err)
912 break;
913 if (block != NULL) {
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);
922 if (err)
923 break;
924 err = emitdelta(deltas, &nalloc, ndeltas,
925 alloc_chunk_size, 1, block->offset, blocklen);
926 if (err)
927 break;
928 } else {
930 * No match.
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);
935 if (err)
936 break;
938 fileoffset += blocklen;
941 if (err) {
942 free(*deltas);
943 *deltas = NULL;
944 *ndeltas = 0;
946 return err;