2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <sys/resource.h>
23 #include <sys/socket.h>
35 #include "got_error.h"
36 #include "got_object.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_delta_cache.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_parse.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_pack.h"
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 static const struct got_error *
57 verify_fanout_table(uint32_t *fanout_table)
61 for (i = 0; i < 0xff - 1; i++) {
62 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
63 return got_error(GOT_ERR_BAD_PACKIDX);
69 const struct got_error *
70 got_packidx_init_hdr(struct got_packidx *p, int verify, off_t packfile_size)
72 const struct got_error *err = NULL;
73 struct got_packidx_v2_hdr *h;
75 uint8_t sha1[SHA1_DIGEST_LENGTH];
76 size_t nobj, len_fanout, len_ids, offset, remain;
86 if (remain < sizeof(*h->magic)) {
87 err = got_error(GOT_ERR_BAD_PACKIDX);
91 h->magic = (uint32_t *)(p->map + offset);
93 h->magic = malloc(sizeof(*h->magic));
94 if (h->magic == NULL) {
95 err = got_error_from_errno("malloc");
98 n = read(p->fd, h->magic, sizeof(*h->magic));
100 err = got_error_from_errno("read");
102 } else if (n != sizeof(*h->magic)) {
103 err = got_error(GOT_ERR_BAD_PACKIDX);
107 if (*h->magic != htobe32(GOT_PACKIDX_V2_MAGIC)) {
108 err = got_error(GOT_ERR_BAD_PACKIDX);
111 offset += sizeof(*h->magic);
112 remain -= sizeof(*h->magic);
115 SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
117 if (remain < sizeof(*h->version)) {
118 err = got_error(GOT_ERR_BAD_PACKIDX);
122 h->version = (uint32_t *)(p->map + offset);
124 h->version = malloc(sizeof(*h->version));
125 if (h->version == NULL) {
126 err = got_error_from_errno("malloc");
129 n = read(p->fd, h->version, sizeof(*h->version));
131 err = got_error_from_errno("read");
133 } else if (n != sizeof(*h->version)) {
134 err = got_error(GOT_ERR_BAD_PACKIDX);
138 if (*h->version != htobe32(GOT_PACKIDX_VERSION)) {
139 err = got_error(GOT_ERR_BAD_PACKIDX);
142 offset += sizeof(*h->version);
143 remain -= sizeof(*h->version);
146 SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
149 sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
150 if (remain < len_fanout) {
151 err = got_error(GOT_ERR_BAD_PACKIDX);
155 h->fanout_table = (uint32_t *)(p->map + offset);
157 h->fanout_table = malloc(len_fanout);
158 if (h->fanout_table == NULL) {
159 err = got_error_from_errno("malloc");
162 n = read(p->fd, h->fanout_table, len_fanout);
164 err = got_error_from_errno("read");
166 } else if (n != len_fanout) {
167 err = got_error(GOT_ERR_BAD_PACKIDX);
171 err = verify_fanout_table(h->fanout_table);
175 SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
176 offset += len_fanout;
177 remain -= len_fanout;
179 nobj = be32toh(h->fanout_table[0xff]);
180 len_ids = nobj * sizeof(*h->sorted_ids);
181 if (len_ids <= nobj || len_ids > remain) {
182 err = got_error(GOT_ERR_BAD_PACKIDX);
187 (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
189 h->sorted_ids = malloc(len_ids);
190 if (h->sorted_ids == NULL) {
191 err = got_error(GOT_ERR_BAD_PACKIDX);
194 n = read(p->fd, h->sorted_ids, len_ids);
196 err = got_error_from_errno("read");
197 else if (n != len_ids) {
198 err = got_error(GOT_ERR_BAD_PACKIDX);
203 SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
207 if (remain < nobj * sizeof(*h->crc32)) {
208 err = got_error(GOT_ERR_BAD_PACKIDX);
212 h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
214 h->crc32 = malloc(nobj * sizeof(*h->crc32));
215 if (h->crc32 == NULL) {
216 err = got_error_from_errno("malloc");
219 n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
221 err = got_error_from_errno("read");
222 else if (n != nobj * sizeof(*h->crc32)) {
223 err = got_error(GOT_ERR_BAD_PACKIDX);
228 SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
229 remain -= nobj * sizeof(*h->crc32);
230 offset += nobj * sizeof(*h->crc32);
232 if (remain < nobj * sizeof(*h->offsets)) {
233 err = got_error(GOT_ERR_BAD_PACKIDX);
237 h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
239 h->offsets = malloc(nobj * sizeof(*h->offsets));
240 if (h->offsets == NULL) {
241 err = got_error_from_errno("malloc");
244 n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
246 err = got_error_from_errno("read");
247 else if (n != nobj * sizeof(*h->offsets)) {
248 err = got_error(GOT_ERR_BAD_PACKIDX);
253 SHA1Update(&ctx, (uint8_t *)h->offsets,
254 nobj * sizeof(*h->offsets));
255 remain -= nobj * sizeof(*h->offsets);
256 offset += nobj * sizeof(*h->offsets);
258 /* Large file offsets are contained only in files > 2GB. */
259 if (verify || packfile_size > 0x7fffffff) {
260 for (i = 0; i < nobj; i++) {
261 uint32_t o = h->offsets[i];
262 if (o & htobe32(GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX))
266 if (p->nlargeobj == 0)
268 else if (packfile_size <= 0x7fffffff) {
269 err = got_error(GOT_ERR_BAD_PACKIDX);
273 if (remain < p->nlargeobj * sizeof(*h->large_offsets)) {
274 err = got_error(GOT_ERR_BAD_PACKIDX);
278 h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
280 h->large_offsets = malloc(p->nlargeobj *
281 sizeof(*h->large_offsets));
282 if (h->large_offsets == NULL) {
283 err = got_error_from_errno("malloc");
286 n = read(p->fd, h->large_offsets,
287 p->nlargeobj * sizeof(*h->large_offsets));
289 err = got_error_from_errno("read");
290 else if (n != p->nlargeobj * sizeof(*h->large_offsets)) {
291 err = got_error(GOT_ERR_BAD_PACKIDX);
296 SHA1Update(&ctx, (uint8_t*)h->large_offsets,
297 p->nlargeobj * sizeof(*h->large_offsets));
298 remain -= p->nlargeobj * sizeof(*h->large_offsets);
299 offset += p->nlargeobj * sizeof(*h->large_offsets);
302 if (remain < sizeof(*h->trailer)) {
303 err = got_error(GOT_ERR_BAD_PACKIDX);
308 (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
310 h->trailer = malloc(sizeof(*h->trailer));
311 if (h->trailer == NULL) {
312 err = got_error_from_errno("malloc");
315 n = read(p->fd, h->trailer, sizeof(*h->trailer));
317 err = got_error_from_errno("read");
318 else if (n != sizeof(*h->trailer)) {
319 err = got_error(GOT_ERR_BAD_PACKIDX);
324 SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
325 SHA1Final(sha1, &ctx);
326 if (memcmp(h->trailer->packidx_sha1, sha1,
327 SHA1_DIGEST_LENGTH) != 0)
328 err = got_error(GOT_ERR_PACKIDX_CSUM);
334 const struct got_error *
335 got_packidx_open(struct got_packidx **packidx,
336 int dir_fd, const char *relpath, int verify)
338 const struct got_error *err = NULL;
339 struct got_packidx *p = NULL;
341 struct stat idx_sb, pack_sb;
345 err = got_packidx_get_packfile_path(&pack_relpath, relpath);
350 * Ensure that a corresponding pack file exists.
351 * Some Git repositories have this problem. Git seems to ignore
352 * the existence of lonely pack index files but we do not.
354 if (fstatat(dir_fd, pack_relpath, &pack_sb, 0) == -1) {
355 if (errno == ENOENT) {
356 err = got_error_fmt(GOT_ERR_LONELY_PACKIDX,
359 err = got_error_from_errno2("fstatat", pack_relpath);
363 p = calloc(1, sizeof(*p));
365 err = got_error_from_errno("calloc");
369 p->fd = openat(dir_fd, relpath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
371 err = got_error_from_errno2("openat", relpath);
375 if (fstat(p->fd, &idx_sb) != 0) {
376 err = got_error_from_errno2("fstat", relpath);
379 p->len = idx_sb.st_size;
380 if (p->len < sizeof(p->hdr)) {
381 err = got_error(GOT_ERR_BAD_PACKIDX);
385 p->path_packidx = strdup(relpath);
386 if (p->path_packidx == NULL) {
387 err = got_error_from_errno("strdup");
391 #ifndef GOT_PACK_NO_MMAP
392 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
393 if (p->map == MAP_FAILED) {
394 if (errno != ENOMEM) {
395 err = got_error_from_errno("mmap");
398 p->map = NULL; /* fall back to read(2) */
402 err = got_packidx_init_hdr(p, verify, pack_sb.st_size);
406 got_packidx_close(p);
413 const struct got_error *
414 got_packidx_close(struct got_packidx *packidx)
416 const struct got_error *err = NULL;
418 free(packidx->path_packidx);
420 if (munmap(packidx->map, packidx->len) == -1)
421 err = got_error_from_errno("munmap");
423 free(packidx->hdr.magic);
424 free(packidx->hdr.version);
425 free(packidx->hdr.fanout_table);
426 free(packidx->hdr.sorted_ids);
427 free(packidx->hdr.crc32);
428 free(packidx->hdr.offsets);
429 free(packidx->hdr.large_offsets);
430 free(packidx->hdr.trailer);
432 if (close(packidx->fd) == -1 && err == NULL)
433 err = got_error_from_errno("close");
434 free(packidx->sorted_offsets);
435 free(packidx->sorted_large_offsets);
441 const struct got_error *
442 got_packidx_get_packfile_path(char **path_packfile, const char *path_packidx)
446 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
447 size = strlen(path_packidx) + 2;
448 if (size < GOT_PACKFILE_NAMELEN + 1)
449 return got_error_path(path_packidx, GOT_ERR_BAD_PATH);
451 *path_packfile = malloc(size);
452 if (*path_packfile == NULL)
453 return got_error_from_errno("malloc");
455 /* Copy up to and excluding ".idx". */
456 if (strlcpy(*path_packfile, path_packidx,
457 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
458 return got_error(GOT_ERR_NO_SPACE);
460 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
461 return got_error(GOT_ERR_NO_SPACE);
467 got_packidx_get_object_offset(struct got_packidx *packidx, int idx)
469 uint32_t offset = be32toh(packidx->hdr.offsets[idx]);
470 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
472 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
473 if (idx < 0 || idx >= packidx->nlargeobj ||
474 packidx->hdr.large_offsets == NULL)
476 loffset = be64toh(packidx->hdr.large_offsets[idx]);
477 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
479 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
483 got_packidx_get_object_idx(struct got_packidx *packidx,
484 struct got_object_id *id)
486 u_int8_t id0 = id->sha1[0];
487 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
488 int left = 0, right = totobj - 1;
491 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
493 while (left <= right) {
494 struct got_packidx_object_id *oid;
497 i = ((left + right) / 2);
498 oid = &packidx->hdr.sorted_ids[i];
499 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
512 offset_cmp(const void *pa, const void *pb)
514 const struct got_pack_offset_index *a, *b;
516 a = (const struct got_pack_offset_index *)pa;
517 b = (const struct got_pack_offset_index *)pb;
519 if (a->offset < b->offset)
521 else if (a->offset > b->offset)
528 large_offset_cmp(const void *pa, const void *pb)
530 const struct got_pack_large_offset_index *a, *b;
532 a = (const struct got_pack_large_offset_index *)pa;
533 b = (const struct got_pack_large_offset_index *)pb;
535 if (a->offset < b->offset)
537 else if (a->offset > b->offset)
543 static const struct got_error *
544 build_offset_index(struct got_packidx *p)
546 uint32_t nobj = be32toh(p->hdr.fanout_table[0xff]);
547 unsigned int i, j, k;
549 p->sorted_offsets = calloc(nobj - p->nlargeobj,
550 sizeof(p->sorted_offsets[0]));
551 if (p->sorted_offsets == NULL)
552 return got_error_from_errno("calloc");
554 if (p->nlargeobj > 0) {
555 p->sorted_large_offsets = calloc(p->nlargeobj,
556 sizeof(p->sorted_large_offsets[0]));
557 if (p->sorted_large_offsets == NULL)
558 return got_error_from_errno("calloc");
563 for (i = 0; i < nobj; i++) {
564 uint32_t offset = be32toh(p->hdr.offsets[i]);
565 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
568 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
569 if (idx >= p->nlargeobj ||
571 p->hdr.large_offsets == NULL)
572 return got_error(GOT_ERR_BAD_PACKIDX);
573 loffset = be64toh(p->hdr.large_offsets[idx]);
574 p->sorted_large_offsets[j].offset = loffset;
575 p->sorted_large_offsets[j].idx = i;
578 p->sorted_offsets[k].offset = offset;
579 p->sorted_offsets[k].idx = i;
583 if (j != p->nlargeobj || k != nobj - p->nlargeobj)
584 return got_error(GOT_ERR_BAD_PACKIDX);
586 qsort(p->sorted_offsets, nobj - p->nlargeobj,
587 sizeof(p->sorted_offsets[0]), offset_cmp);
589 if (p->sorted_large_offsets)
590 qsort(p->sorted_large_offsets, p->nlargeobj,
591 sizeof(p->sorted_large_offsets[0]), large_offset_cmp);
596 const struct got_error *
597 got_packidx_get_offset_idx(int *idx, struct got_packidx *packidx, off_t offset)
599 const struct got_error *err;
600 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
605 if (packidx->sorted_offsets == NULL) {
606 err = build_offset_index(packidx);
611 if (offset >= 0x7fffffff) {
613 left = 0, right = packidx->nlargeobj - 1;
614 while (left <= right) {
615 i = ((left + right) / 2);
616 lo = packidx->sorted_large_offsets[i].offset;
618 *idx = packidx->sorted_large_offsets[i].idx;
620 } else if (offset > lo)
622 else if (offset < lo)
627 left = 0, right = totobj - packidx->nlargeobj - 1;
628 while (left <= right) {
629 i = ((left + right) / 2);
630 o = packidx->sorted_offsets[i].offset;
632 *idx = packidx->sorted_offsets[i].idx;
634 } else if (offset > o)
644 const struct got_error *
645 got_packidx_get_object_id(struct got_object_id *id,
646 struct got_packidx *packidx, int idx)
648 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
649 struct got_packidx_object_id *oid;
651 if (idx < 0 || idx >= totobj)
652 return got_error(GOT_ERR_NO_OBJ);
654 oid = &packidx->hdr.sorted_ids[idx];
655 memcpy(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
659 const struct got_error *
660 got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
661 struct got_packidx *packidx, const char *id_str_prefix)
663 const struct got_error *err = NULL;
665 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
667 size_t prefix_len = strlen(id_str_prefix);
668 struct got_packidx_object_id *oid;
671 STAILQ_INIT(matched_ids);
674 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
676 hex[0] = id_str_prefix[0];
677 hex[1] = id_str_prefix[1];
679 if (!got_parse_xdigit(&id0, hex))
680 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
683 i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
684 oid = &packidx->hdr.sorted_ids[i];
685 while (i < totobj && oid->sha1[0] == id0) {
686 char id_str[SHA1_DIGEST_STRING_LENGTH];
687 struct got_object_qid *qid;
690 if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
691 return got_error(GOT_ERR_NO_SPACE);
693 cmp = strncmp(id_str, id_str_prefix, prefix_len);
695 oid = &packidx->hdr.sorted_ids[++i];
700 err = got_object_qid_alloc_partial(&qid);
703 memcpy(qid->id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
704 STAILQ_INSERT_TAIL(matched_ids, qid, entry);
706 oid = &packidx->hdr.sorted_ids[++i];
710 got_object_id_queue_free(matched_ids);
715 set_max_datasize(void)
719 if (getrlimit(RLIMIT_DATA, &rl) != 0)
722 rl.rlim_cur = rl.rlim_max;
723 setrlimit(RLIMIT_DATA, &rl);
726 const struct got_error *
727 got_pack_start_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
729 const struct got_error *err = NULL;
732 struct imsgbuf *ibuf;
734 ibuf = calloc(1, sizeof(*ibuf));
736 return got_error_from_errno("calloc");
738 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
739 if (pack->privsep_child == NULL) {
740 err = got_error_from_errno("calloc");
744 pack->child_has_tempfiles = 0;
745 pack->child_has_delta_outfd = 0;
747 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
748 err = got_error_from_errno("socketpair");
754 err = got_error_from_errno("fork");
756 } else if (pid == 0) {
758 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
759 pack->path_packfile);
763 if (close(imsg_fds[1]) == -1)
764 return got_error_from_errno("close");
765 pack->privsep_child->imsg_fd = imsg_fds[0];
766 pack->privsep_child->pid = pid;
767 imsg_init(ibuf, imsg_fds[0]);
768 pack->privsep_child->ibuf = ibuf;
770 err = got_privsep_init_pack_child(ibuf, pack, packidx);
772 const struct got_error *child_err;
773 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
774 child_err = got_privsep_wait_for_child(
775 pack->privsep_child->pid);
776 if (child_err && err == NULL)
782 free(pack->privsep_child);
783 pack->privsep_child = NULL;
788 static const struct got_error *
789 pack_stop_privsep_child(struct got_pack *pack)
791 const struct got_error *err = NULL, *close_err = NULL;
793 if (pack->privsep_child == NULL)
796 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
799 if (close(pack->privsep_child->imsg_fd) == -1)
800 close_err = got_error_from_errno("close");
801 err = got_privsep_wait_for_child(pack->privsep_child->pid);
802 if (close_err && err == NULL)
804 imsg_clear(pack->privsep_child->ibuf);
805 free(pack->privsep_child->ibuf);
806 free(pack->privsep_child);
807 pack->privsep_child = NULL;
811 const struct got_error *
812 got_pack_close(struct got_pack *pack)
814 const struct got_error *err = NULL;
816 err = pack_stop_privsep_child(pack);
817 if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
818 err = got_error_from_errno("munmap");
819 if (pack->fd != -1 && close(pack->fd) == -1 && err == NULL)
820 err = got_error_from_errno("close");
822 free(pack->path_packfile);
823 pack->path_packfile = NULL;
825 if (pack->delta_cache) {
826 got_delta_cache_free(pack->delta_cache);
827 pack->delta_cache = NULL;
831 * Leave accumfd and basefd alone. They are managed by the
832 * repository layer and can be reused.
838 const struct got_error *
839 got_pack_parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
840 struct got_pack *pack, off_t offset)
850 if (offset >= pack->filesize)
851 return got_error(GOT_ERR_PACK_OFFSET);
854 mapoff = (size_t)offset;
856 if (lseek(pack->fd, offset, SEEK_SET) == -1)
857 return got_error_from_errno("lseek");
861 /* We do not support size values which don't fit in 64 bit. */
863 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
864 "packfile offset %lld", (long long)offset);
867 if (mapoff + sizeof(sizeN) >= pack->filesize)
868 return got_error(GOT_ERR_BAD_PACKFILE);
869 sizeN = *(pack->map + mapoff);
870 mapoff += sizeof(sizeN);
872 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
874 return got_error_from_errno("read");
875 if (n != sizeof(sizeN))
876 return got_error(GOT_ERR_BAD_PACKFILE);
878 *len += sizeof(sizeN);
881 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
882 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
883 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
885 size_t shift = 4 + 7 * (i - 1);
886 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
889 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
896 static const struct got_error *
897 open_plain_object(struct got_object **obj, struct got_object_id *id,
898 uint8_t type, off_t offset, size_t size, int idx)
900 *obj = calloc(1, sizeof(**obj));
902 return got_error_from_errno("calloc");
905 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
906 (*obj)->pack_idx = idx;
909 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
910 (*obj)->pack_offset = offset;
915 static const struct got_error *
916 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
927 /* We do not support offset values which don't fit in 64 bit. */
929 return got_error(GOT_ERR_NO_SPACE);
933 mapoff = (size_t)delta_offset + *len;
934 if (mapoff + sizeof(offN) >= pack->filesize)
935 return got_error(GOT_ERR_PACK_OFFSET);
936 offN = *(pack->map + mapoff);
939 n = read(pack->fd, &offN, sizeof(offN));
941 return got_error_from_errno("read");
942 if (n != sizeof(offN))
943 return got_error(GOT_ERR_BAD_PACKFILE);
945 *len += sizeof(offN);
948 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
952 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
955 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
961 const struct got_error *
962 got_pack_parse_offset_delta(off_t *base_offset, size_t *len,
963 struct got_pack *pack, off_t offset, size_t tslen)
965 const struct got_error *err;
971 err = parse_negative_offset(&negoffset, &negofflen, pack,
976 /* Compute the base object's offset (must be in the same pack file). */
977 *base_offset = (offset - negoffset);
978 if (*base_offset <= 0)
979 return got_error(GOT_ERR_BAD_PACKFILE);
985 static const struct got_error *
986 read_delta_data(uint8_t **delta_buf, size_t *delta_len,
987 size_t *delta_compressed_len, size_t delta_data_offset,
988 struct got_pack *pack)
990 const struct got_error *err = NULL;
994 if (delta_data_offset >= pack->filesize)
995 return got_error(GOT_ERR_PACK_OFFSET);
996 err = got_inflate_to_mem_mmap(delta_buf, delta_len,
997 &consumed, NULL, pack->map, delta_data_offset,
998 pack->filesize - delta_data_offset);
1002 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
1003 return got_error_from_errno("lseek");
1004 err = got_inflate_to_mem_fd(delta_buf, delta_len,
1005 &consumed, NULL, 0, pack->fd);
1010 if (delta_compressed_len)
1011 *delta_compressed_len = consumed;
1016 static const struct got_error *
1017 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
1018 int delta_type, size_t delta_size, size_t delta_data_offset)
1020 struct got_delta *delta;
1022 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
1025 return got_error_from_errno("got_delta_open");
1026 /* delta is freed in got_object_close() */
1028 STAILQ_INSERT_HEAD(&deltas->entries, delta, entry);
1032 static const struct got_error *
1033 resolve_offset_delta(struct got_delta_chain *deltas,
1034 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1035 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1038 const struct got_error *err;
1043 off_t delta_data_offset;
1046 err = got_pack_parse_offset_delta(&base_offset, &consumed, pack,
1047 delta_offset, tslen);
1051 delta_data_offset = delta_offset + tslen + consumed;
1052 if (delta_data_offset >= pack->filesize)
1053 return got_error(GOT_ERR_PACK_OFFSET);
1055 if (pack->map == NULL) {
1056 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1057 if (delta_data_offset == -1)
1058 return got_error_from_errno("lseek");
1061 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1066 /* An offset delta must be in the same packfile. */
1067 if (base_offset >= pack->filesize)
1068 return got_error(GOT_ERR_PACK_OFFSET);
1070 err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1071 &base_tslen, pack, base_offset);
1075 return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1076 base_tslen, base_type, base_size, recursion - 1);
1079 const struct got_error *
1080 got_pack_parse_ref_delta(struct got_object_id *id,
1081 struct got_pack *pack, off_t delta_offset, int tslen)
1084 size_t mapoff = delta_offset + tslen;
1085 if (mapoff + sizeof(*id) >= pack->filesize)
1086 return got_error(GOT_ERR_PACK_OFFSET);
1087 memcpy(id, pack->map + mapoff, sizeof(*id));
1090 n = read(pack->fd, id, sizeof(*id));
1092 return got_error_from_errno("read");
1093 if (n != sizeof(*id))
1094 return got_error(GOT_ERR_BAD_PACKFILE);
1100 static const struct got_error *
1101 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
1102 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1103 size_t delta_size, unsigned int recursion)
1105 const struct got_error *err;
1106 struct got_object_id id;
1112 off_t delta_data_offset;
1114 if (delta_offset + tslen >= pack->filesize)
1115 return got_error(GOT_ERR_PACK_OFFSET);
1117 err = got_pack_parse_ref_delta(&id, pack, delta_offset, tslen);
1121 delta_data_offset = delta_offset + tslen + sizeof(id);
1123 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1124 if (delta_data_offset == -1)
1125 return got_error_from_errno("lseek");
1128 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1133 /* Delta base must be in the same pack file. */
1134 idx = got_packidx_get_object_idx(packidx, &id);
1136 return got_error(GOT_ERR_NO_OBJ);
1138 base_offset = got_packidx_get_object_offset(packidx, idx);
1139 if (base_offset == -1)
1140 return got_error(GOT_ERR_BAD_PACKIDX);
1142 if (base_offset >= pack->filesize)
1143 return got_error(GOT_ERR_PACK_OFFSET);
1145 err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1146 &base_tslen, pack, base_offset);
1150 return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1151 base_tslen, base_type, base_size, recursion - 1);
1154 const struct got_error *
1155 got_pack_resolve_delta_chain(struct got_delta_chain *deltas,
1156 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1157 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1159 const struct got_error *err = NULL;
1161 if (--recursion == 0)
1162 return got_error(GOT_ERR_RECURSION);
1164 switch (delta_type) {
1165 case GOT_OBJ_TYPE_COMMIT:
1166 case GOT_OBJ_TYPE_TREE:
1167 case GOT_OBJ_TYPE_BLOB:
1168 case GOT_OBJ_TYPE_TAG:
1169 /* Plain types are the final delta base. Recursion ends. */
1170 err = add_delta(deltas, delta_offset, tslen, delta_type,
1173 case GOT_OBJ_TYPE_OFFSET_DELTA:
1174 err = resolve_offset_delta(deltas, packidx, pack,
1175 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1177 case GOT_OBJ_TYPE_REF_DELTA:
1178 err = resolve_ref_delta(deltas, packidx, pack,
1179 delta_offset, tslen, delta_type, delta_size, recursion - 1);
1182 return got_error(GOT_ERR_OBJ_TYPE);
1188 static const struct got_error *
1189 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1190 struct got_pack *pack, struct got_object_id *id, off_t offset,
1191 size_t tslen, int delta_type, size_t delta_size, int idx)
1193 const struct got_error *err = NULL;
1196 *obj = calloc(1, sizeof(**obj));
1198 return got_error_from_errno("calloc");
1202 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1203 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1204 (*obj)->pack_offset = offset + tslen;
1206 STAILQ_INIT(&(*obj)->deltas.entries);
1207 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1208 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1209 (*obj)->pack_idx = idx;
1211 err = got_pack_resolve_delta_chain(&(*obj)->deltas, packidx, pack,
1212 offset, tslen, delta_type, delta_size,
1213 GOT_DELTA_CHAIN_RECURSION_MAX);
1217 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1220 (*obj)->type = resolved_type;
1223 got_object_close(*obj);
1229 const struct got_error *
1230 got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
1231 struct got_packidx *packidx, int idx, struct got_object_id *id)
1233 const struct got_error *err = NULL;
1241 offset = got_packidx_get_object_offset(packidx, idx);
1243 return got_error(GOT_ERR_BAD_PACKIDX);
1245 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1251 case GOT_OBJ_TYPE_COMMIT:
1252 case GOT_OBJ_TYPE_TREE:
1253 case GOT_OBJ_TYPE_BLOB:
1254 case GOT_OBJ_TYPE_TAG:
1255 err = open_plain_object(obj, id, type, offset + tslen,
1258 case GOT_OBJ_TYPE_OFFSET_DELTA:
1259 case GOT_OBJ_TYPE_REF_DELTA:
1260 err = open_delta_object(obj, packidx, pack, id, offset,
1261 tslen, type, size, idx);
1264 err = got_error(GOT_ERR_OBJ_TYPE);
1271 const struct got_error *
1272 got_pack_get_delta_chain_max_size(uint64_t *max_size,
1273 struct got_delta_chain *deltas, struct got_pack *pack)
1275 struct got_delta *delta;
1276 uint64_t base_size = 0, result_size = 0;
1279 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1280 /* Plain object types are the delta base. */
1281 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1282 delta->type != GOT_OBJ_TYPE_TREE &&
1283 delta->type != GOT_OBJ_TYPE_BLOB &&
1284 delta->type != GOT_OBJ_TYPE_TAG) {
1285 const struct got_error *err;
1286 uint8_t *delta_buf = NULL;
1290 if (pack->delta_cache) {
1291 got_delta_cache_get(&delta_buf, &delta_len,
1292 pack->delta_cache, delta->data_offset);
1294 if (delta_buf == NULL) {
1296 err = read_delta_data(&delta_buf, &delta_len,
1297 NULL, delta->data_offset, pack);
1301 if (pack->delta_cache && !cached) {
1302 err = got_delta_cache_add(pack->delta_cache,
1303 delta->data_offset, delta_buf, delta_len);
1306 else if (err->code != GOT_ERR_NO_SPACE) {
1311 err = got_delta_get_sizes(&base_size, &result_size,
1312 delta_buf, delta_len);
1318 base_size = delta->size;
1319 if (base_size > *max_size)
1320 *max_size = base_size;
1321 if (result_size > *max_size)
1322 *max_size = result_size;
1328 const struct got_error *
1329 got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
1330 struct got_pack *pack)
1332 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1333 return got_error(GOT_ERR_OBJ_TYPE);
1335 return got_pack_get_delta_chain_max_size(size, &obj->deltas, pack);
1338 const struct got_error *
1339 got_pack_dump_delta_chain_to_file(size_t *result_size,
1340 struct got_delta_chain *deltas, struct got_pack *pack, FILE *outfile,
1341 FILE *base_file, FILE *accum_file)
1343 const struct got_error *err = NULL;
1344 struct got_delta *delta;
1345 uint8_t *base_buf = NULL, *accum_buf = NULL;
1346 size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0, delta_len;
1347 /* We process small enough files entirely in memory for speed. */
1348 const size_t max_bufsize = GOT_DELTA_RESULT_SIZE_CACHED_MAX;
1349 uint64_t max_size = 0;
1354 if (STAILQ_EMPTY(&deltas->entries))
1355 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1357 if (fseeko(base_file, 0L, SEEK_SET) == -1)
1358 return got_error_from_errno("fseeko");
1359 if (fseeko(accum_file, 0L, SEEK_SET) == -1)
1360 return got_error_from_errno("fseeko");
1362 /* Deltas are ordered in ascending order. */
1363 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1364 uint8_t *delta_buf = NULL;
1365 uint64_t base_size, result_size = 0;
1369 off_t delta_data_offset;
1371 /* Plain object types are the delta base. */
1372 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1373 delta->type != GOT_OBJ_TYPE_TREE &&
1374 delta->type != GOT_OBJ_TYPE_BLOB &&
1375 delta->type != GOT_OBJ_TYPE_TAG) {
1376 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1380 delta_data_offset = delta->offset + delta->tslen;
1381 if (delta_data_offset >= pack->filesize) {
1382 err = got_error(GOT_ERR_PACK_OFFSET);
1385 if (pack->map == NULL) {
1386 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1388 err = got_error_from_errno("lseek");
1392 if (delta->size > max_size)
1393 max_size = delta->size;
1394 if (max_size > max_bufsize) {
1396 mapoff = (size_t)delta_data_offset;
1397 err = got_inflate_to_file_mmap(
1398 &base_bufsz, NULL, NULL, pack->map,
1399 mapoff, pack->filesize - mapoff,
1402 err = got_inflate_to_file_fd(
1403 &base_bufsz, NULL, NULL, pack->fd,
1406 accum_buf = malloc(max_size);
1407 if (accum_buf == NULL) {
1408 err = got_error_from_errno("malloc");
1411 accum_bufsz = max_size;
1413 mapoff = (size_t)delta_data_offset;
1414 err = got_inflate_to_mem_mmap(&base_buf,
1415 &base_bufsz, NULL, NULL,
1417 pack->filesize - mapoff);
1419 err = got_inflate_to_mem_fd(&base_buf,
1420 &base_bufsz, NULL, NULL, max_size,
1426 if (base_buf == NULL)
1431 if (pack->delta_cache) {
1432 got_delta_cache_get(&delta_buf, &delta_len,
1433 pack->delta_cache, delta->data_offset);
1435 if (delta_buf == NULL) {
1437 err = read_delta_data(&delta_buf, &delta_len, NULL,
1438 delta->data_offset, pack);
1442 if (pack->delta_cache && !cached) {
1443 err = got_delta_cache_add(pack->delta_cache,
1444 delta->data_offset, delta_buf, delta_len);
1447 else if (err->code != GOT_ERR_NO_SPACE) {
1453 err = got_delta_get_sizes(&base_size, &result_size,
1454 delta_buf, delta_len);
1457 if (base_size > max_size)
1458 max_size = base_size;
1459 if (result_size > max_size)
1460 max_size = result_size;
1462 if (base_buf && max_size > max_bufsize) {
1463 /* Switch from buffers to temporary files. */
1464 size_t w = fwrite(base_buf, 1, base_bufsz,
1466 if (w != base_bufsz) {
1467 err = got_ferror(outfile, GOT_ERR_IO);
1476 if (base_buf && max_size > base_bufsz) {
1477 uint8_t *p = realloc(base_buf, max_size);
1479 err = got_error_from_errno("realloc");
1483 base_bufsz = max_size;
1486 if (accum_buf && max_size > accum_bufsz) {
1487 uint8_t *p = realloc(accum_buf, max_size);
1489 err = got_error_from_errno("realloc");
1493 accum_bufsz = max_size;
1497 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1498 delta_buf, delta_len, accum_buf,
1499 &accum_size, max_size);
1502 err = got_delta_apply(base_file, delta_buf,
1504 /* Final delta application writes to output file. */
1505 ++n < deltas->nentries ? accum_file : outfile,
1513 if (n < deltas->nentries) {
1514 /* Accumulated delta becomes the new base. */
1516 uint8_t *tmp = accum_buf;
1517 size_t tmp_size = accum_bufsz;
1518 accum_buf = base_buf;
1519 accum_bufsz = base_bufsz;
1521 base_bufsz = tmp_size;
1523 FILE *tmp = accum_file;
1524 accum_file = base_file;
1535 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1537 if (len != accum_size)
1538 err = got_ferror(outfile, GOT_ERR_IO);
1542 *result_size = accum_size;
1546 const struct got_error *
1547 got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1548 struct got_delta_chain *deltas, struct got_pack *pack)
1550 const struct got_error *err = NULL;
1551 struct got_delta *delta;
1552 uint8_t *base_buf = NULL, *accum_buf = NULL;
1553 size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0, delta_len;
1554 uint64_t max_size = 0;
1560 if (STAILQ_EMPTY(&deltas->entries))
1561 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1563 /* Deltas are ordered in ascending order. */
1564 STAILQ_FOREACH(delta, &deltas->entries, entry) {
1565 uint8_t *delta_buf = NULL;
1566 uint64_t base_size, result_size = 0;
1569 size_t delta_data_offset;
1571 /* Plain object types are the delta base. */
1572 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1573 delta->type != GOT_OBJ_TYPE_TREE &&
1574 delta->type != GOT_OBJ_TYPE_BLOB &&
1575 delta->type != GOT_OBJ_TYPE_TAG) {
1576 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1580 delta_data_offset = delta->offset + delta->tslen;
1581 if (delta_data_offset >= pack->filesize) {
1582 err = got_error(GOT_ERR_PACK_OFFSET);
1586 if (delta->size > max_size)
1587 max_size = delta->size;
1590 size_t mapoff = (size_t)delta_data_offset;
1591 err = got_inflate_to_mem_mmap(&base_buf,
1592 &base_bufsz, NULL, NULL, pack->map,
1593 mapoff, pack->filesize - mapoff);
1595 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1597 err = got_error_from_errno("lseek");
1600 err = got_inflate_to_mem_fd(&base_buf,
1601 &base_bufsz, NULL, NULL, max_size,
1610 if (pack->delta_cache) {
1611 got_delta_cache_get(&delta_buf, &delta_len,
1612 pack->delta_cache, delta->data_offset);
1614 if (delta_buf == NULL) {
1616 err = read_delta_data(&delta_buf, &delta_len, NULL,
1617 delta->data_offset, pack);
1621 if (pack->delta_cache && !cached) {
1622 err = got_delta_cache_add(pack->delta_cache,
1623 delta->data_offset, delta_buf, delta_len);
1626 else if (err->code != GOT_ERR_NO_SPACE) {
1632 err = got_delta_get_sizes(&base_size, &result_size,
1633 delta_buf, delta_len);
1636 if (base_size > max_size)
1637 max_size = base_size;
1638 if (result_size > max_size)
1639 max_size = result_size;
1641 if (max_size > base_bufsz) {
1642 uint8_t *p = realloc(base_buf, max_size);
1644 err = got_error_from_errno("realloc");
1648 base_bufsz = max_size;
1651 if (max_size > accum_bufsz) {
1652 uint8_t *p = realloc(accum_buf, max_size);
1654 err = got_error_from_errno("realloc");
1658 accum_bufsz = max_size;
1661 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1662 delta_buf, delta_len, accum_buf,
1663 &accum_size, max_size);
1670 if (n < deltas->nentries) {
1671 /* Accumulated delta becomes the new base. */
1672 uint8_t *tmp = accum_buf;
1673 size_t tmp_size = accum_bufsz;
1674 accum_buf = base_buf;
1675 accum_bufsz = base_bufsz;
1677 base_bufsz = tmp_size;
1688 *outbuf = accum_buf;
1689 *outlen = accum_size;
1694 const struct got_error *
1695 got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1696 FILE *outfile, FILE *base_file, FILE *accum_file)
1698 const struct got_error *err = NULL;
1700 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1701 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1703 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1704 if (obj->pack_offset >= pack->filesize)
1705 return got_error(GOT_ERR_PACK_OFFSET);
1708 size_t mapoff = (size_t)obj->pack_offset;
1709 err = got_inflate_to_file_mmap(&obj->size, NULL, NULL,
1710 pack->map, mapoff, pack->filesize - mapoff,
1713 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1714 return got_error_from_errno("lseek");
1715 err = got_inflate_to_file_fd(&obj->size, NULL, NULL,
1719 err = got_pack_dump_delta_chain_to_file(&obj->size,
1720 &obj->deltas, pack, outfile, base_file, accum_file);
1725 const struct got_error *
1726 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1727 struct got_object *obj, struct got_pack *pack)
1729 const struct got_error *err = NULL;
1731 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1732 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1734 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1735 if (obj->pack_offset >= pack->filesize)
1736 return got_error(GOT_ERR_PACK_OFFSET);
1738 size_t mapoff = (size_t)obj->pack_offset;
1739 err = got_inflate_to_mem_mmap(buf, len, NULL, NULL,
1740 pack->map, mapoff, pack->filesize - mapoff);
1742 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1743 return got_error_from_errno("lseek");
1744 err = got_inflate_to_mem_fd(buf, len, NULL, NULL,
1745 obj->size, pack->fd);
1748 err = got_pack_dump_delta_chain_to_mem(buf, len, &obj->deltas,
1754 static const struct got_error *
1755 read_raw_delta_data(uint8_t **delta_buf, size_t *delta_len,
1756 size_t *delta_len_compressed, uint64_t *base_size, uint64_t *result_size,
1757 off_t delta_data_offset, struct got_pack *pack, struct got_packidx *packidx)
1759 const struct got_error *err = NULL;
1761 /* Validate decompression and obtain the decompressed size. */
1762 err = read_delta_data(delta_buf, delta_len, delta_len_compressed,
1763 delta_data_offset, pack);
1767 /* Read delta base/result sizes from head of delta stream. */
1768 err = got_delta_get_sizes(base_size, result_size,
1769 *delta_buf, *delta_len);
1773 /* Discard decompressed delta and read it again in compressed form. */
1775 *delta_buf = malloc(*delta_len_compressed);
1776 if (*delta_buf == NULL) {
1777 err = got_error_from_errno("malloc");
1781 if (delta_data_offset >= pack->filesize)
1782 err = got_error(GOT_ERR_PACK_OFFSET);
1783 memcpy(*delta_buf, pack->map + delta_data_offset,
1784 *delta_len_compressed);
1787 if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1788 err = got_error_from_errno("lseek");
1791 n = read(pack->fd, *delta_buf, *delta_len_compressed);
1793 err = got_error_from_errno("read");
1795 } else if (n != *delta_len_compressed) {
1796 err = got_error(GOT_ERR_IO);
1805 *delta_len_compressed = 0;
1812 const struct got_error *
1813 got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
1814 size_t *delta_compressed_size, off_t *delta_offset, off_t *base_offset,
1815 struct got_object_id *base_id, uint64_t *base_size, uint64_t *result_size,
1816 struct got_pack *pack, struct got_packidx *packidx, int idx)
1818 const struct got_error *err = NULL;
1822 size_t tslen, delta_hdrlen;
1823 off_t delta_data_offset;
1827 *delta_compressed_size = 0;
1833 offset = got_packidx_get_object_offset(packidx, idx);
1835 return got_error(GOT_ERR_BAD_PACKIDX);
1837 if (offset >= pack->filesize)
1838 return got_error(GOT_ERR_PACK_OFFSET);
1840 err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1845 if (tslen + size < tslen || offset + size < size ||
1846 tslen + offset < tslen)
1847 return got_error(GOT_ERR_PACK_OFFSET);
1850 case GOT_OBJ_TYPE_OFFSET_DELTA:
1851 err = got_pack_parse_offset_delta(base_offset, &delta_hdrlen,
1852 pack, offset, tslen);
1856 case GOT_OBJ_TYPE_REF_DELTA:
1857 err = got_pack_parse_ref_delta(base_id, pack, offset, tslen);
1860 delta_hdrlen = SHA1_DIGEST_LENGTH;
1863 return got_error_fmt(GOT_ERR_OBJ_TYPE,
1864 "non-delta object type %d found at offset %lld",
1865 type, (long long)offset);
1868 if (tslen + delta_hdrlen < delta_hdrlen ||
1869 offset + delta_hdrlen < delta_hdrlen)
1870 return got_error(GOT_ERR_BAD_DELTA);
1872 delta_data_offset = offset + tslen + delta_hdrlen;
1873 err = read_raw_delta_data(delta_buf, delta_size, delta_compressed_size,
1874 base_size, result_size, delta_data_offset, pack, packidx);
1878 if (*delta_size != size) {
1879 err = got_error(GOT_ERR_BAD_DELTA);
1883 *delta_offset = offset;
1889 *delta_compressed_size = 0;