Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/mman.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <sha1.h>
31 #include <endian.h>
32 #include <zlib.h>
33 #include <imsg.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_opentemp.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_path.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_pack.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 #endif
51 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 static const struct got_error *
56 verify_fanout_table(uint32_t *fanout_table)
57 {
58 int i;
60 for (i = 0; i < 0xff - 1; i++) {
61 if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
62 return got_error(GOT_ERR_BAD_PACKIDX);
63 }
65 return NULL;
66 }
68 const struct got_error *
69 got_pack_get_packfile_size(size_t *size, const char *path)
70 {
71 struct stat sb;
72 char *dot;
74 *size = 0;
76 dot = strrchr(path, '.');
77 if (dot == NULL)
78 return got_error(GOT_ERR_BAD_PATH);
80 /* Path must point to a pack index or to a pack file. */
81 if (strcmp(dot, GOT_PACKIDX_SUFFIX) == 0) {
82 const struct got_error *err = NULL;
83 char *path_pack;
84 char base_path[PATH_MAX];
86 /* Convert pack index path to pack file path. */
87 if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
88 return got_error(GOT_ERR_NO_SPACE);
89 dot = strrchr(base_path, '.');
90 if (dot == NULL)
91 return got_error(GOT_ERR_BAD_PATH);
92 *dot = '\0';
93 if (asprintf(&path_pack, "%s.pack", base_path) == -1)
94 return got_error_from_errno();
96 if (stat(path_pack, &sb) != 0)
97 err = got_error_from_errno();
98 free(path_pack);
99 if (err)
100 return err;
101 } else if (strcmp(dot, GOT_PACKFILE_SUFFIX) == 0) {
102 if (stat(path, &sb) != 0)
103 return got_error_from_errno();
104 } else
105 return got_error(GOT_ERR_BAD_PATH);
107 *size = sb.st_size;
108 return 0;
111 const struct got_error *
112 got_packidx_init_hdr(struct got_packidx *p, int verify)
114 const struct got_error *err = NULL;
115 struct got_packidx_v2_hdr *h;
116 SHA1_CTX ctx;
117 uint8_t sha1[SHA1_DIGEST_LENGTH];
118 size_t nobj, len_fanout, len_ids, offset, remain;
119 ssize_t n;
121 SHA1Init(&ctx);
123 h = &p->hdr;
124 offset = 0;
125 remain = p->len;
127 if (remain < sizeof(*h->magic)) {
128 err = got_error(GOT_ERR_BAD_PACKIDX);
129 goto done;
131 if (p->map)
132 h->magic = (uint32_t *)(p->map + offset);
133 else {
134 h->magic = malloc(sizeof(*h->magic));
135 if (h->magic == NULL) {
136 err = got_error_from_errno();
137 goto done;
139 n = read(p->fd, h->magic, sizeof(*h->magic));
140 if (n < 0)
141 err = got_error_from_errno();
142 else if (n != sizeof(*h->magic)) {
143 err = got_error(GOT_ERR_BAD_PACKIDX);
144 goto done;
147 if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
148 err = got_error(GOT_ERR_BAD_PACKIDX);
149 goto done;
151 offset += sizeof(*h->magic);
152 remain -= sizeof(*h->magic);
154 if (verify)
155 SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
157 if (remain < sizeof(*h->version)) {
158 err = got_error(GOT_ERR_BAD_PACKIDX);
159 goto done;
161 if (p->map)
162 h->version = (uint32_t *)(p->map + offset);
163 else {
164 h->version = malloc(sizeof(*h->version));
165 if (h->version == NULL) {
166 err = got_error_from_errno();
167 goto done;
169 n = read(p->fd, h->version, sizeof(*h->version));
170 if (n < 0)
171 err = got_error_from_errno();
172 else if (n != sizeof(*h->version)) {
173 err = got_error(GOT_ERR_BAD_PACKIDX);
174 goto done;
177 if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
178 err = got_error(GOT_ERR_BAD_PACKIDX);
179 goto done;
181 offset += sizeof(*h->version);
182 remain -= sizeof(*h->version);
184 if (verify)
185 SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
187 len_fanout =
188 sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
189 if (remain < len_fanout) {
190 err = got_error(GOT_ERR_BAD_PACKIDX);
191 goto done;
193 if (p->map)
194 h->fanout_table = (uint32_t *)(p->map + offset);
195 else {
196 h->fanout_table = malloc(len_fanout);
197 if (h->fanout_table == NULL) {
198 err = got_error_from_errno();
199 goto done;
201 n = read(p->fd, h->fanout_table, len_fanout);
202 if (n < 0)
203 err = got_error_from_errno();
204 else if (n != len_fanout) {
205 err = got_error(GOT_ERR_BAD_PACKIDX);
206 goto done;
209 err = verify_fanout_table(h->fanout_table);
210 if (err)
211 goto done;
212 if (verify)
213 SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
214 offset += len_fanout;
215 remain -= len_fanout;
217 nobj = betoh32(h->fanout_table[0xff]);
218 len_ids = nobj * sizeof(*h->sorted_ids);
219 if (len_ids <= nobj || len_ids > remain) {
220 err = got_error(GOT_ERR_BAD_PACKIDX);
221 goto done;
223 if (p->map)
224 h->sorted_ids =
225 (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
226 else {
227 h->sorted_ids = malloc(len_ids);
228 if (h->sorted_ids == NULL) {
229 err = got_error(GOT_ERR_BAD_PACKIDX);
230 goto done;
232 n = read(p->fd, h->sorted_ids, len_ids);
233 if (n < 0)
234 err = got_error_from_errno();
235 else if (n != len_ids) {
236 err = got_error(GOT_ERR_BAD_PACKIDX);
237 goto done;
240 if (verify)
241 SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
242 offset += len_ids;
243 remain -= len_ids;
245 if (remain < nobj * sizeof(*h->crc32)) {
246 err = got_error(GOT_ERR_BAD_PACKIDX);
247 goto done;
249 if (p->map)
250 h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
251 else {
252 h->crc32 = malloc(nobj * sizeof(*h->crc32));
253 if (h->crc32 == NULL) {
254 err = got_error_from_errno();
255 goto done;
257 n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
258 if (n < 0)
259 err = got_error_from_errno();
260 else if (n != nobj * sizeof(*h->crc32)) {
261 err = got_error(GOT_ERR_BAD_PACKIDX);
262 goto done;
265 if (verify)
266 SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
267 remain -= nobj * sizeof(*h->crc32);
268 offset += nobj * sizeof(*h->crc32);
270 if (remain < nobj * sizeof(*h->offsets)) {
271 err = got_error(GOT_ERR_BAD_PACKIDX);
272 goto done;
274 if (p->map)
275 h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
276 else {
277 h->offsets = malloc(nobj * sizeof(*h->offsets));
278 if (h->offsets == NULL) {
279 err = got_error_from_errno();
280 goto done;
282 n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
283 if (n < 0)
284 err = got_error_from_errno();
285 else if (n != nobj * sizeof(*h->offsets)) {
286 err = got_error(GOT_ERR_BAD_PACKIDX);
287 goto done;
290 if (verify)
291 SHA1Update(&ctx, (uint8_t *)h->offsets,
292 nobj * sizeof(*h->offsets));
293 remain -= nobj * sizeof(*h->offsets);
294 offset += nobj * sizeof(*h->offsets);
296 /* Large file offsets are contained only in files > 2GB. */
297 if (p->len <= 0x80000000)
298 goto checksum;
300 if (remain < nobj * sizeof(*h->large_offsets)) {
301 err = got_error(GOT_ERR_BAD_PACKIDX);
302 goto done;
304 if (p->map)
305 h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
306 else {
307 h->offsets = malloc(nobj * sizeof(*h->large_offsets));
308 if (h->offsets == NULL) {
309 err = got_error_from_errno();
310 goto done;
312 n = read(p->fd, h->large_offsets,
313 nobj * sizeof(*h->large_offsets));
314 if (n < 0)
315 err = got_error_from_errno();
316 else if (n != nobj * sizeof(*h->large_offsets)) {
317 err = got_error(GOT_ERR_BAD_PACKIDX);
318 goto done;
321 if (verify)
322 SHA1Update(&ctx, (uint8_t*)h->large_offsets,
323 nobj * sizeof(*h->large_offsets));
324 remain -= nobj * sizeof(*h->large_offsets);
325 offset += nobj * sizeof(*h->large_offsets);
327 checksum:
328 if (remain < sizeof(*h->trailer)) {
329 err = got_error(GOT_ERR_BAD_PACKIDX);
330 goto done;
332 if (p->map)
333 h->trailer =
334 (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
335 else {
336 h->trailer = malloc(sizeof(*h->trailer));
337 if (h->trailer == NULL) {
338 err = got_error_from_errno();
339 goto done;
341 n = read(p->fd, h->trailer, sizeof(*h->trailer));
342 if (n < 0)
343 err = got_error_from_errno();
344 else if (n != sizeof(*h->trailer)) {
345 err = got_error(GOT_ERR_BAD_PACKIDX);
346 goto done;
349 if (verify) {
350 SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
351 SHA1Final(sha1, &ctx);
352 if (memcmp(h->trailer->packidx_sha1, sha1,
353 SHA1_DIGEST_LENGTH) != 0)
354 err = got_error(GOT_ERR_PACKIDX_CSUM);
356 done:
357 return err;
360 const struct got_error *
361 got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
363 const struct got_error *err = NULL;
364 struct got_packidx *p;
366 *packidx = NULL;
368 p = calloc(1, sizeof(*p));
369 if (p == NULL)
370 return got_error_from_errno();
372 p->fd = open(path, O_RDONLY | O_NOFOLLOW);
373 if (p->fd == -1)
374 return got_error_from_errno();
376 err = got_pack_get_packfile_size(&p->len, path);
377 if (err) {
378 close(p->fd);
379 free(p);
380 return err;
382 if (p->len < sizeof(p->hdr)) {
383 err = got_error(GOT_ERR_BAD_PACKIDX);
384 close(p->fd);
385 free(p);
386 return err;
389 p->path_packidx = strdup(path);
390 if (p->path_packidx == NULL) {
391 err = got_error_from_errno();
392 goto done;
395 #ifndef GOT_PACK_NO_MMAP
396 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
397 if (p->map == MAP_FAILED) {
398 if (errno != ENOMEM) {
399 err = got_error_from_errno();
400 goto done;
402 p->map = NULL; /* fall back to read(2) */
404 #endif
406 err = got_packidx_init_hdr(p, verify);
407 done:
408 if (err)
409 got_packidx_close(p);
410 else
411 *packidx = p;
413 return err;
416 const struct got_error *
417 got_packidx_close(struct got_packidx *packidx)
419 const struct got_error *err = NULL;
421 free(packidx->path_packidx);
422 if (packidx->map) {
423 if (munmap(packidx->map, packidx->len) == -1)
424 err = got_error_from_errno();
425 } else {
426 free(packidx->hdr.magic);
427 free(packidx->hdr.version);
428 free(packidx->hdr.fanout_table);
429 free(packidx->hdr.sorted_ids);
430 free(packidx->hdr.crc32);
431 free(packidx->hdr.offsets);
432 free(packidx->hdr.large_offsets);
433 free(packidx->hdr.trailer);
435 if (close(packidx->fd) != 0 && err == NULL)
436 err = got_error_from_errno();
437 free(packidx);
439 return err;
442 static off_t
443 get_object_offset(struct got_packidx *packidx, int idx)
445 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
446 uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
447 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
448 uint64_t loffset;
449 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
450 if (idx < 0 || idx > totobj ||
451 packidx->hdr.large_offsets == NULL)
452 return -1;
453 loffset = betoh64(packidx->hdr.large_offsets[idx]);
454 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
456 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
459 int
460 got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
462 u_int8_t id0 = id->sha1[0];
463 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
464 int left = 0, right = totobj - 1;
466 if (id0 > 0)
467 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
469 while (left <= right) {
470 struct got_packidx_object_id *oid;
471 int i, cmp;
473 i = ((left + right) / 2);
474 oid = &packidx->hdr.sorted_ids[i];
475 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
476 if (cmp == 0)
477 return i;
478 else if (cmp > 0)
479 left = i + 1;
480 else if (cmp < 0)
481 right = i - 1;
484 return -1;
487 const struct got_error *
488 got_pack_stop_privsep_child(struct got_pack *pack)
490 const struct got_error *err = NULL;
492 if (pack->privsep_child == NULL)
493 return NULL;
495 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
496 if (err)
497 return err;
498 err = got_privsep_wait_for_child(pack->privsep_child->pid);
499 free(pack->privsep_child);
500 pack->privsep_child = NULL;
501 return err;
504 const struct got_error *
505 got_pack_close(struct got_pack *pack)
507 const struct got_error *err = NULL;
509 err = got_pack_stop_privsep_child(pack);
510 if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
511 err = got_error_from_errno();
512 if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
513 err = got_error_from_errno();
514 pack->fd = -1;
515 free(pack->path_packfile);
516 pack->path_packfile = NULL;
517 pack->filesize = 0;
519 return err;
522 static const struct got_error *
523 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
524 struct got_pack *pack, off_t offset)
526 uint8_t t = 0;
527 uint64_t s = 0;
528 uint8_t sizeN;
529 size_t mapoff = 0;
530 int i = 0;
532 *len = 0;
534 if (offset >= pack->filesize)
535 return got_error(GOT_ERR_PACK_OFFSET);
537 if (pack->map) {
538 mapoff = (size_t)offset;
539 } else {
540 if (lseek(pack->fd, offset, SEEK_SET) == -1)
541 return got_error_from_errno();
544 do {
545 /* We do not support size values which don't fit in 64 bit. */
546 if (i > 9)
547 return got_error(GOT_ERR_NO_SPACE);
549 if (pack->map) {
550 sizeN = *(pack->map + mapoff);
551 mapoff += sizeof(sizeN);
552 } else {
553 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
554 if (n < 0)
555 return got_error_from_errno();
556 if (n != sizeof(sizeN))
557 return got_error(GOT_ERR_BAD_PACKFILE);
559 *len += sizeof(sizeN);
561 if (i == 0) {
562 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
563 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
564 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
565 } else {
566 size_t shift = 4 + 7 * (i - 1);
567 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
569 i++;
570 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
572 *type = t;
573 *size = s;
574 return NULL;
577 static const struct got_error *
578 open_plain_object(struct got_object **obj, const char *path_packfile,
579 struct got_object_id *id, uint8_t type, off_t offset, size_t size, int idx)
581 *obj = calloc(1, sizeof(**obj));
582 if (*obj == NULL)
583 return got_error_from_errno();
585 (*obj)->path_packfile = strdup(path_packfile);
586 if ((*obj)->path_packfile == NULL) {
587 const struct got_error *err = got_error_from_errno();
588 free(*obj);
589 *obj = NULL;
590 return err;
593 (*obj)->type = type;
594 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
595 (*obj)->pack_idx = idx;
596 (*obj)->hdrlen = 0;
597 (*obj)->size = size;
598 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
599 (*obj)->pack_offset = offset;
601 return NULL;
604 static const struct got_error *
605 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
606 off_t delta_offset)
608 int64_t o = 0;
609 uint8_t offN;
610 int i = 0;
612 *len = 0;
614 do {
615 /* We do not support offset values which don't fit in 64 bit. */
616 if (i > 8)
617 return got_error(GOT_ERR_NO_SPACE);
619 if (pack->map) {
620 size_t mapoff;
621 if (delta_offset >= pack->filesize)
622 return got_error(GOT_ERR_PACK_OFFSET);
623 mapoff = (size_t)delta_offset + *len;
624 offN = *(pack->map + mapoff);
625 } else {
626 ssize_t n;
627 n = read(pack->fd, &offN, sizeof(offN));
628 if (n < 0)
629 return got_error_from_errno();
630 if (n != sizeof(offN))
631 return got_error(GOT_ERR_BAD_PACKFILE);
633 *len += sizeof(offN);
635 if (i == 0)
636 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
637 else {
638 o++;
639 o <<= 7;
640 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
642 i++;
643 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
645 *offset = o;
646 return NULL;
649 static const struct got_error *
650 parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
651 off_t offset, int tslen)
653 const struct got_error *err;
654 int64_t negoffset;
655 size_t negofflen;
657 *len = 0;
659 err = parse_negative_offset(&negoffset, &negofflen, pack,
660 offset + tslen);
661 if (err)
662 return err;
664 /* Compute the base object's offset (must be in the same pack file). */
665 *base_offset = (offset - negoffset);
666 if (*base_offset <= 0)
667 return got_error(GOT_ERR_BAD_PACKFILE);
669 *len = negofflen;
670 return NULL;
673 static const struct got_error *
674 resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
675 struct got_pack *, off_t, size_t, int, size_t, unsigned int);
677 static const struct got_error *
678 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
679 int delta_type, size_t delta_size, size_t delta_data_offset,
680 uint8_t *delta_buf, size_t delta_len)
682 struct got_delta *delta;
684 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
685 delta_data_offset, delta_buf,
686 delta_len);
687 if (delta == NULL)
688 return got_error_from_errno();
689 /* delta is freed in got_object_close() */
690 deltas->nentries++;
691 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
692 return NULL;
695 static const struct got_error *
696 resolve_offset_delta(struct got_delta_chain *deltas,
697 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
698 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
701 const struct got_error *err;
702 off_t base_offset;
703 uint8_t base_type;
704 uint64_t base_size;
705 size_t base_tslen;
706 off_t delta_data_offset;
707 uint8_t *delta_buf;
708 size_t delta_len, consumed;
710 err = parse_offset_delta(&base_offset, &consumed, pack,
711 delta_offset, tslen);
712 if (err)
713 return err;
715 delta_data_offset = delta_offset + tslen + consumed;
716 if (delta_data_offset >= pack->filesize)
717 return got_error(GOT_ERR_PACK_OFFSET);
719 if (pack->map == NULL) {
720 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
721 if (delta_data_offset == -1)
722 return got_error_from_errno();
725 if (pack->map) {
726 size_t mapoff = (size_t)delta_data_offset;
727 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
728 mapoff, pack->filesize - mapoff);
729 if (err)
730 return err;
731 } else {
733 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
734 if (err)
735 return err;
738 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
739 delta_data_offset, delta_buf, delta_len);
740 if (err)
741 goto done;
743 /* An offset delta must be in the same packfile. */
744 if (base_offset >= pack->filesize) {
745 err = got_error(GOT_ERR_PACK_OFFSET);
746 goto done;
749 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
750 pack, base_offset);
751 if (err)
752 goto done;
754 err = resolve_delta_chain(deltas, packidx, pack, base_offset,
755 base_tslen, base_type, base_size, recursion - 1);
756 done:
757 if (err)
758 free(delta_buf);
759 return err;
762 static const struct got_error *
763 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
764 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
765 size_t delta_size, unsigned int recursion)
767 const struct got_error *err;
768 struct got_object_id id;
769 int idx;
770 off_t base_offset;
771 uint8_t base_type;
772 uint64_t base_size;
773 size_t base_tslen;
774 off_t delta_data_offset;
775 uint8_t *delta_buf = NULL;
776 size_t delta_len;
778 if (delta_offset >= pack->filesize)
779 return got_error(GOT_ERR_PACK_OFFSET);
780 delta_data_offset = delta_offset + tslen;
781 if (delta_data_offset >= pack->filesize)
782 return got_error(GOT_ERR_PACK_OFFSET);
784 if (pack->map == NULL) {
785 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
786 if (delta_data_offset == -1)
787 return got_error_from_errno();
791 if (pack->map) {
792 size_t mapoff = (size_t)delta_data_offset;
793 memcpy(&id, pack->map + mapoff, sizeof(id));
794 mapoff += sizeof(id);
795 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
796 mapoff, pack->filesize - mapoff);
797 if (err)
798 goto done;
799 } else {
800 ssize_t n = read(pack->fd, &id, sizeof(id));
801 if (n < 0)
802 return got_error_from_errno();
803 if (n != sizeof(id))
804 return got_error(GOT_ERR_BAD_PACKFILE);
805 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
806 if (err)
807 goto done;
810 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
811 delta_data_offset, delta_buf, delta_len);
812 if (err)
813 goto done;
815 /* Delta base must be in the same pack file. */
816 idx = got_packidx_get_object_idx(packidx, &id);
817 if (idx == -1) {
818 err = got_error(GOT_ERR_BAD_PACKFILE);
819 goto done;
822 base_offset = get_object_offset(packidx, idx);
823 if (base_offset == (uint64_t)-1) {
824 err = got_error(GOT_ERR_BAD_PACKIDX);
825 goto done;
828 if (base_offset >= pack->filesize) {
829 err = got_error(GOT_ERR_PACK_OFFSET);
830 goto done;
833 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
834 pack, base_offset);
835 if (err)
836 goto done;
838 err = resolve_delta_chain(deltas, packidx, pack, base_offset,
839 base_tslen, base_type, base_size, recursion - 1);
840 done:
841 if (err)
842 free(delta_buf);
843 return err;
846 static const struct got_error *
847 resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
848 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
849 size_t delta_size, unsigned int recursion)
851 const struct got_error *err = NULL;
853 if (--recursion == 0)
854 return got_error(GOT_ERR_RECURSION);
856 switch (delta_type) {
857 case GOT_OBJ_TYPE_COMMIT:
858 case GOT_OBJ_TYPE_TREE:
859 case GOT_OBJ_TYPE_BLOB:
860 case GOT_OBJ_TYPE_TAG:
861 /* Plain types are the final delta base. Recursion ends. */
862 err = add_delta(deltas, delta_offset, tslen, delta_type,
863 delta_size, 0, NULL, 0);
864 break;
865 case GOT_OBJ_TYPE_OFFSET_DELTA:
866 err = resolve_offset_delta(deltas, packidx, pack,
867 delta_offset, tslen, delta_type, delta_size, recursion - 1);
868 break;
869 case GOT_OBJ_TYPE_REF_DELTA:
870 err = resolve_ref_delta(deltas, packidx, pack,
871 delta_offset, tslen, delta_type, delta_size, recursion - 1);
872 break;
873 default:
874 return got_error(GOT_ERR_OBJ_TYPE);
877 return err;
880 static const struct got_error *
881 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
882 struct got_pack *pack, struct got_object_id *id, off_t offset,
883 size_t tslen, int delta_type, size_t delta_size, int idx)
885 const struct got_error *err = NULL;
886 int resolved_type;
888 *obj = calloc(1, sizeof(**obj));
889 if (*obj == NULL)
890 return got_error_from_errno();
892 (*obj)->flags = 0;
893 (*obj)->hdrlen = 0;
894 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
895 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
896 (*obj)->pack_offset = offset + tslen;
898 (*obj)->path_packfile = strdup(pack->path_packfile);
899 if ((*obj)->path_packfile == NULL) {
900 err = got_error_from_errno();
901 goto done;
903 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
904 (*obj)->pack_idx = idx;
906 SIMPLEQ_INIT(&(*obj)->deltas.entries);
907 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
909 err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
910 tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
911 if (err)
912 goto done;
914 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
915 if (err)
916 goto done;
917 (*obj)->type = resolved_type;
918 done:
919 if (err) {
920 got_object_close(*obj);
921 *obj = NULL;
923 return err;
926 const struct got_error *
927 got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
928 struct got_packidx *packidx, int idx, struct got_object_id *id)
930 const struct got_error *err = NULL;
931 off_t offset;
932 uint8_t type;
933 uint64_t size;
934 size_t tslen;
936 *obj = NULL;
938 offset = get_object_offset(packidx, idx);
939 if (offset == (uint64_t)-1)
940 return got_error(GOT_ERR_BAD_PACKIDX);
942 err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
943 if (err)
944 return err;
946 switch (type) {
947 case GOT_OBJ_TYPE_COMMIT:
948 case GOT_OBJ_TYPE_TREE:
949 case GOT_OBJ_TYPE_BLOB:
950 case GOT_OBJ_TYPE_TAG:
951 err = open_plain_object(obj, pack->path_packfile, id, type,
952 offset + tslen, size, idx);
953 break;
954 case GOT_OBJ_TYPE_OFFSET_DELTA:
955 case GOT_OBJ_TYPE_REF_DELTA:
956 err = open_delta_object(obj, packidx, pack, id, offset,
957 tslen, type, size, idx);
958 break;
959 default:
960 err = got_error(GOT_ERR_OBJ_TYPE);
961 break;
964 return err;
967 static const struct got_error *
968 get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
970 struct got_delta *delta;
971 uint64_t base_size = 0, result_size = 0;
973 *max_size = 0;
974 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
975 /* Plain object types are the delta base. */
976 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
977 delta->type != GOT_OBJ_TYPE_TREE &&
978 delta->type != GOT_OBJ_TYPE_BLOB &&
979 delta->type != GOT_OBJ_TYPE_TAG) {
980 const struct got_error *err;
981 err = got_delta_get_sizes(&base_size, &result_size,
982 delta->delta_buf, delta->delta_len);
983 if (err)
984 return err;
985 } else
986 base_size = delta->size;
987 if (base_size > *max_size)
988 *max_size = base_size;
989 if (result_size > *max_size)
990 *max_size = result_size;
993 return NULL;
996 const struct got_error *
997 got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj)
999 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1000 return got_error(GOT_ERR_OBJ_TYPE);
1002 return get_delta_chain_max_size(size, &obj->deltas);
1005 static const struct got_error *
1006 dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1007 struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
1009 const struct got_error *err = NULL;
1010 struct got_delta *delta;
1011 uint8_t *base_buf = NULL, *accum_buf = NULL;
1012 size_t base_bufsz = 0, accum_size = 0;
1013 uint64_t max_size;
1014 int n = 0;
1016 *result_size = 0;
1018 if (SIMPLEQ_EMPTY(&deltas->entries))
1019 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1021 /* We process small enough files entirely in memory for speed. */
1022 err = get_delta_chain_max_size(&max_size, deltas);
1023 if (err)
1024 return err;
1025 if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1026 accum_buf = malloc(max_size);
1027 if (accum_buf == NULL)
1028 return got_error_from_errno();
1029 base_file = NULL;
1030 accum_file = NULL;
1033 /* Deltas are ordered in ascending order. */
1034 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1035 if (n == 0) {
1036 size_t mapoff;
1037 off_t delta_data_offset;
1039 /* Plain object types are the delta base. */
1040 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1041 delta->type != GOT_OBJ_TYPE_TREE &&
1042 delta->type != GOT_OBJ_TYPE_BLOB &&
1043 delta->type != GOT_OBJ_TYPE_TAG) {
1044 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1045 goto done;
1048 delta_data_offset = delta->offset + delta->tslen;
1049 if (delta_data_offset >= pack->filesize) {
1050 err = got_error(GOT_ERR_PACK_OFFSET);
1051 goto done;
1053 if (pack->map == NULL) {
1054 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1055 == -1) {
1056 err = got_error_from_errno();
1057 goto done;
1060 if (base_file) {
1061 if (pack->map) {
1062 mapoff = (size_t)delta_data_offset;
1063 err = got_inflate_to_file_mmap(
1064 &base_bufsz, pack->map, mapoff,
1065 pack->filesize - mapoff, base_file);
1066 } else
1067 err = got_inflate_to_file_fd(
1068 &base_bufsz, pack->fd, base_file);
1069 } else {
1070 if (pack->map) {
1071 mapoff = (size_t)delta_data_offset;
1072 err = got_inflate_to_mem_mmap(&base_buf,
1073 &base_bufsz, pack->map, mapoff,
1074 pack->filesize - mapoff);
1075 } else
1076 err = got_inflate_to_mem_fd(&base_buf,
1077 &base_bufsz, pack->fd);
1079 if (err)
1080 goto done;
1081 n++;
1082 if (base_file)
1083 rewind(base_file);
1084 continue;
1087 if (base_buf) {
1088 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1089 delta->delta_buf, delta->delta_len, accum_buf,
1090 &accum_size, max_size);
1091 n++;
1092 } else {
1093 err = got_delta_apply(base_file, delta->delta_buf,
1094 delta->delta_len,
1095 /* Final delta application writes to output file. */
1096 ++n < deltas->nentries ? accum_file : outfile,
1097 &accum_size);
1099 if (err)
1100 goto done;
1102 if (n < deltas->nentries) {
1103 /* Accumulated delta becomes the new base. */
1104 if (base_buf) {
1105 uint8_t *tmp = accum_buf;
1107 * Base buffer switches roles with accumulation
1108 * buffer. Ensure it can hold the largest
1109 * result in the delta chain. The initial
1110 * allocation might have been smaller.
1112 if (base_bufsz < max_size) {
1113 uint8_t *p;
1114 p = reallocarray(base_buf, 1, max_size);
1115 if (p == NULL) {
1116 err = got_error_from_errno();
1117 goto done;
1119 base_buf = p;
1120 base_bufsz = max_size;
1122 accum_buf = base_buf;
1123 base_buf = tmp;
1124 } else {
1125 FILE *tmp = accum_file;
1126 accum_file = base_file;
1127 base_file = tmp;
1128 rewind(base_file);
1129 rewind(accum_file);
1134 done:
1135 free(base_buf);
1136 if (accum_buf) {
1137 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1138 free(accum_buf);
1139 if (len != accum_size)
1140 err = got_ferror(outfile, GOT_ERR_IO);
1142 rewind(outfile);
1143 if (err == NULL)
1144 *result_size = accum_size;
1145 return err;
1148 static const struct got_error *
1149 dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1150 struct got_delta_chain *deltas, struct got_pack *pack)
1152 const struct got_error *err = NULL;
1153 struct got_delta *delta;
1154 uint8_t *base_buf = NULL, *accum_buf = NULL;
1155 size_t base_bufsz = 0, accum_size = 0;
1156 uint64_t max_size;
1157 int n = 0;
1159 *outbuf = NULL;
1160 *outlen = 0;
1162 if (SIMPLEQ_EMPTY(&deltas->entries))
1163 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1165 err = get_delta_chain_max_size(&max_size, deltas);
1166 if (err)
1167 return err;
1168 accum_buf = malloc(max_size);
1169 if (accum_buf == NULL)
1170 return got_error_from_errno();
1172 /* Deltas are ordered in ascending order. */
1173 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1174 if (n == 0) {
1175 size_t delta_data_offset;
1177 /* Plain object types are the delta base. */
1178 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1179 delta->type != GOT_OBJ_TYPE_TREE &&
1180 delta->type != GOT_OBJ_TYPE_BLOB &&
1181 delta->type != GOT_OBJ_TYPE_TAG) {
1182 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1183 goto done;
1186 delta_data_offset = delta->offset + delta->tslen;
1187 if (delta_data_offset >= pack->filesize) {
1188 err = got_error(GOT_ERR_PACK_OFFSET);
1189 goto done;
1191 if (pack->map) {
1192 size_t mapoff = (size_t)delta_data_offset;
1193 err = got_inflate_to_mem_mmap(&base_buf,
1194 &base_bufsz, pack->map, mapoff,
1195 pack->filesize - mapoff);
1196 } else {
1197 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1198 == -1) {
1199 err = got_error_from_errno();
1200 goto done;
1202 err = got_inflate_to_mem_fd(&base_buf,
1203 &base_bufsz, pack->fd);
1205 if (err)
1206 goto done;
1207 n++;
1208 continue;
1211 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1212 delta->delta_buf, delta->delta_len, accum_buf,
1213 &accum_size, max_size);
1214 n++;
1215 if (err)
1216 goto done;
1218 if (n < deltas->nentries) {
1219 /* Accumulated delta becomes the new base. */
1220 uint8_t *tmp = accum_buf;
1222 * Base buffer switches roles with accumulation buffer.
1223 * Ensure it can hold the largest result in the delta
1224 * chain. Initial allocation might have been smaller.
1226 if (base_bufsz < max_size) {
1227 uint8_t *p;
1228 p = reallocarray(base_buf, 1, max_size);
1229 if (p == NULL) {
1230 err = got_error_from_errno();
1231 goto done;
1233 base_buf = p;
1234 base_bufsz = max_size;
1236 accum_buf = base_buf;
1237 base_buf = tmp;
1241 done:
1242 free(base_buf);
1243 if (err) {
1244 free(accum_buf);
1245 *outbuf = NULL;
1246 *outlen = 0;
1247 } else {
1248 *outbuf = accum_buf;
1249 *outlen = accum_size;
1251 return err;
1254 const struct got_error *
1255 got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1256 FILE *outfile, FILE *base_file, FILE *accum_file)
1258 const struct got_error *err = NULL;
1260 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1261 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1263 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1264 if (obj->pack_offset >= pack->filesize)
1265 return got_error(GOT_ERR_PACK_OFFSET);
1267 if (pack->map) {
1268 size_t mapoff = (size_t)obj->pack_offset;
1269 err = got_inflate_to_file_mmap(&obj->size, pack->map,
1270 mapoff, pack->filesize - mapoff, outfile);
1271 } else {
1272 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1273 return got_error_from_errno();
1274 err = got_inflate_to_file_fd(&obj->size, pack->fd,
1275 outfile);
1277 } else
1278 err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1279 outfile, base_file, accum_file);
1281 return err;
1284 const struct got_error *
1285 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1286 struct got_object *obj, struct got_pack *pack)
1288 const struct got_error *err = NULL;
1290 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1291 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1293 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1294 if (obj->pack_offset >= pack->filesize)
1295 return got_error(GOT_ERR_PACK_OFFSET);
1296 if (pack->map) {
1297 size_t mapoff = (size_t)obj->pack_offset;
1298 err = got_inflate_to_mem_mmap(buf, len, pack->map,
1299 mapoff, pack->filesize - mapoff);
1300 } else {
1301 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1302 return got_error_from_errno();
1303 err = got_inflate_to_mem_fd(buf, len, pack->fd);
1305 } else
1306 err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
1308 return err;