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"
38 #include "got_path.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.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"
48 #ifndef nitems
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #endif
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 static const struct got_error *
57 verify_fanout_table(uint32_t *fanout_table)
58 {
59 int i;
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);
64 }
66 return NULL;
67 }
69 const struct got_error *
70 got_packidx_init_hdr(struct got_packidx *p, int verify)
71 {
72 const struct got_error *err = NULL;
73 struct got_packidx_v2_hdr *h;
74 SHA1_CTX ctx;
75 uint8_t sha1[SHA1_DIGEST_LENGTH];
76 size_t nobj, len_fanout, len_ids, offset, remain;
77 ssize_t n;
79 SHA1Init(&ctx);
81 h = &p->hdr;
82 offset = 0;
83 remain = p->len;
85 if (remain < sizeof(*h->magic)) {
86 err = got_error(GOT_ERR_BAD_PACKIDX);
87 goto done;
88 }
89 if (p->map)
90 h->magic = (uint32_t *)(p->map + offset);
91 else {
92 h->magic = malloc(sizeof(*h->magic));
93 if (h->magic == NULL) {
94 err = got_error_from_errno("malloc");
95 goto done;
96 }
97 n = read(p->fd, h->magic, sizeof(*h->magic));
98 if (n < 0)
99 err = got_error_from_errno("read");
100 else if (n != sizeof(*h->magic)) {
101 err = got_error(GOT_ERR_BAD_PACKIDX);
102 goto done;
105 if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
106 err = got_error(GOT_ERR_BAD_PACKIDX);
107 goto done;
109 offset += sizeof(*h->magic);
110 remain -= sizeof(*h->magic);
112 if (verify)
113 SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
115 if (remain < sizeof(*h->version)) {
116 err = got_error(GOT_ERR_BAD_PACKIDX);
117 goto done;
119 if (p->map)
120 h->version = (uint32_t *)(p->map + offset);
121 else {
122 h->version = malloc(sizeof(*h->version));
123 if (h->version == NULL) {
124 err = got_error_from_errno("malloc");
125 goto done;
127 n = read(p->fd, h->version, sizeof(*h->version));
128 if (n < 0)
129 err = got_error_from_errno("read");
130 else if (n != sizeof(*h->version)) {
131 err = got_error(GOT_ERR_BAD_PACKIDX);
132 goto done;
135 if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
136 err = got_error(GOT_ERR_BAD_PACKIDX);
137 goto done;
139 offset += sizeof(*h->version);
140 remain -= sizeof(*h->version);
142 if (verify)
143 SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
145 len_fanout =
146 sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
147 if (remain < len_fanout) {
148 err = got_error(GOT_ERR_BAD_PACKIDX);
149 goto done;
151 if (p->map)
152 h->fanout_table = (uint32_t *)(p->map + offset);
153 else {
154 h->fanout_table = malloc(len_fanout);
155 if (h->fanout_table == NULL) {
156 err = got_error_from_errno("malloc");
157 goto done;
159 n = read(p->fd, h->fanout_table, len_fanout);
160 if (n < 0)
161 err = got_error_from_errno("read");
162 else if (n != len_fanout) {
163 err = got_error(GOT_ERR_BAD_PACKIDX);
164 goto done;
167 err = verify_fanout_table(h->fanout_table);
168 if (err)
169 goto done;
170 if (verify)
171 SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
172 offset += len_fanout;
173 remain -= len_fanout;
175 nobj = betoh32(h->fanout_table[0xff]);
176 len_ids = nobj * sizeof(*h->sorted_ids);
177 if (len_ids <= nobj || len_ids > remain) {
178 err = got_error(GOT_ERR_BAD_PACKIDX);
179 goto done;
181 if (p->map)
182 h->sorted_ids =
183 (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
184 else {
185 h->sorted_ids = malloc(len_ids);
186 if (h->sorted_ids == NULL) {
187 err = got_error(GOT_ERR_BAD_PACKIDX);
188 goto done;
190 n = read(p->fd, h->sorted_ids, len_ids);
191 if (n < 0)
192 err = got_error_from_errno("read");
193 else if (n != len_ids) {
194 err = got_error(GOT_ERR_BAD_PACKIDX);
195 goto done;
198 if (verify)
199 SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
200 offset += len_ids;
201 remain -= len_ids;
203 if (remain < nobj * sizeof(*h->crc32)) {
204 err = got_error(GOT_ERR_BAD_PACKIDX);
205 goto done;
207 if (p->map)
208 h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
209 else {
210 h->crc32 = malloc(nobj * sizeof(*h->crc32));
211 if (h->crc32 == NULL) {
212 err = got_error_from_errno("malloc");
213 goto done;
215 n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
216 if (n < 0)
217 err = got_error_from_errno("read");
218 else if (n != nobj * sizeof(*h->crc32)) {
219 err = got_error(GOT_ERR_BAD_PACKIDX);
220 goto done;
223 if (verify)
224 SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
225 remain -= nobj * sizeof(*h->crc32);
226 offset += nobj * sizeof(*h->crc32);
228 if (remain < nobj * sizeof(*h->offsets)) {
229 err = got_error(GOT_ERR_BAD_PACKIDX);
230 goto done;
232 if (p->map)
233 h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
234 else {
235 h->offsets = malloc(nobj * sizeof(*h->offsets));
236 if (h->offsets == NULL) {
237 err = got_error_from_errno("malloc");
238 goto done;
240 n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
241 if (n < 0)
242 err = got_error_from_errno("read");
243 else if (n != nobj * sizeof(*h->offsets)) {
244 err = got_error(GOT_ERR_BAD_PACKIDX);
245 goto done;
248 if (verify)
249 SHA1Update(&ctx, (uint8_t *)h->offsets,
250 nobj * sizeof(*h->offsets));
251 remain -= nobj * sizeof(*h->offsets);
252 offset += nobj * sizeof(*h->offsets);
254 /* Large file offsets are contained only in files > 2GB. */
255 if (p->len <= 0x80000000)
256 goto checksum;
258 if (remain < nobj * sizeof(*h->large_offsets)) {
259 err = got_error(GOT_ERR_BAD_PACKIDX);
260 goto done;
262 if (p->map)
263 h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
264 else {
265 h->offsets = malloc(nobj * sizeof(*h->large_offsets));
266 if (h->offsets == NULL) {
267 err = got_error_from_errno("malloc");
268 goto done;
270 n = read(p->fd, h->large_offsets,
271 nobj * sizeof(*h->large_offsets));
272 if (n < 0)
273 err = got_error_from_errno("read");
274 else if (n != nobj * sizeof(*h->large_offsets)) {
275 err = got_error(GOT_ERR_BAD_PACKIDX);
276 goto done;
279 if (verify)
280 SHA1Update(&ctx, (uint8_t*)h->large_offsets,
281 nobj * sizeof(*h->large_offsets));
282 remain -= nobj * sizeof(*h->large_offsets);
283 offset += nobj * sizeof(*h->large_offsets);
285 checksum:
286 if (remain < sizeof(*h->trailer)) {
287 err = got_error(GOT_ERR_BAD_PACKIDX);
288 goto done;
290 if (p->map)
291 h->trailer =
292 (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
293 else {
294 h->trailer = malloc(sizeof(*h->trailer));
295 if (h->trailer == NULL) {
296 err = got_error_from_errno("malloc");
297 goto done;
299 n = read(p->fd, h->trailer, sizeof(*h->trailer));
300 if (n < 0)
301 err = got_error_from_errno("read");
302 else if (n != sizeof(*h->trailer)) {
303 err = got_error(GOT_ERR_BAD_PACKIDX);
304 goto done;
307 if (verify) {
308 SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
309 SHA1Final(sha1, &ctx);
310 if (memcmp(h->trailer->packidx_sha1, sha1,
311 SHA1_DIGEST_LENGTH) != 0)
312 err = got_error(GOT_ERR_PACKIDX_CSUM);
314 done:
315 return err;
318 const struct got_error *
319 got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
321 const struct got_error *err = NULL;
322 struct got_packidx *p;
323 struct stat sb;
325 *packidx = NULL;
327 p = calloc(1, sizeof(*p));
328 if (p == NULL)
329 return got_error_from_errno("calloc");
331 p->fd = open(path, O_RDONLY | O_NOFOLLOW);
332 if (p->fd == -1)
333 return got_error_from_errno2("open", path);
335 if (fstat(p->fd, &sb) != 0) {
336 err = got_error_from_errno2("fstat", path);
337 close(p->fd);
338 free(p);
339 return err;
341 p->len = sb.st_size;
342 if (p->len < sizeof(p->hdr)) {
343 err = got_error(GOT_ERR_BAD_PACKIDX);
344 close(p->fd);
345 free(p);
346 return err;
349 p->path_packidx = strdup(path);
350 if (p->path_packidx == NULL) {
351 err = got_error_from_errno("strdup");
352 goto done;
355 #ifndef GOT_PACK_NO_MMAP
356 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
357 if (p->map == MAP_FAILED) {
358 if (errno != ENOMEM) {
359 err = got_error_from_errno("mmap");
360 goto done;
362 p->map = NULL; /* fall back to read(2) */
364 #endif
366 err = got_packidx_init_hdr(p, verify);
367 done:
368 if (err)
369 got_packidx_close(p);
370 else
371 *packidx = p;
373 return err;
376 const struct got_error *
377 got_packidx_close(struct got_packidx *packidx)
379 const struct got_error *err = NULL;
381 free(packidx->path_packidx);
382 if (packidx->map) {
383 if (munmap(packidx->map, packidx->len) == -1)
384 err = got_error_from_errno("munmap");
385 } else {
386 free(packidx->hdr.magic);
387 free(packidx->hdr.version);
388 free(packidx->hdr.fanout_table);
389 free(packidx->hdr.sorted_ids);
390 free(packidx->hdr.crc32);
391 free(packidx->hdr.offsets);
392 free(packidx->hdr.large_offsets);
393 free(packidx->hdr.trailer);
395 if (close(packidx->fd) != 0 && err == NULL)
396 err = got_error_from_errno("close");
397 free(packidx);
399 return err;
402 static off_t
403 get_object_offset(struct got_packidx *packidx, int idx)
405 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
406 uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
407 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
408 uint64_t loffset;
409 idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
410 if (idx < 0 || idx > totobj ||
411 packidx->hdr.large_offsets == NULL)
412 return -1;
413 loffset = betoh64(packidx->hdr.large_offsets[idx]);
414 return (loffset > INT64_MAX ? -1 : (off_t)loffset);
416 return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
419 int
420 got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
422 u_int8_t id0 = id->sha1[0];
423 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
424 int left = 0, right = totobj - 1;
426 if (id0 > 0)
427 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
429 while (left <= right) {
430 struct got_packidx_object_id *oid;
431 int i, cmp;
433 i = ((left + right) / 2);
434 oid = &packidx->hdr.sorted_ids[i];
435 cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
436 if (cmp == 0)
437 return i;
438 else if (cmp > 0)
439 left = i + 1;
440 else if (cmp < 0)
441 right = i - 1;
444 return -1;
447 const struct got_error *
448 got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
449 struct got_packidx *packidx, const char *id_str_prefix)
451 const struct got_error *err = NULL;
452 u_int8_t id0;
453 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
454 char hex[3];
455 size_t prefix_len = strlen(id_str_prefix);
456 struct got_packidx_object_id *oid;
457 int i;
459 SIMPLEQ_INIT(matched_ids);
461 if (prefix_len < 2)
462 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
464 hex[0] = id_str_prefix[0];
465 hex[1] = id_str_prefix[1];
466 hex[2] = '\0';
467 if (!got_parse_xdigit(&id0, hex))
468 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
470 i = betoh32(packidx->hdr.fanout_table[id0 - 1]);
471 if (i == 0)
472 return NULL;
474 oid = &packidx->hdr.sorted_ids[i];
475 while (i < totobj && oid->sha1[0] == id0) {
476 char id_str[SHA1_DIGEST_STRING_LENGTH];
477 struct got_object_qid *qid;
478 int cmp;
480 if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
481 return got_error(GOT_ERR_NO_SPACE);
483 cmp = strncmp(id_str, id_str_prefix, prefix_len);
484 if (cmp < 0) {
485 oid = &packidx->hdr.sorted_ids[++i];
486 continue;
487 } else if (cmp > 0)
488 break;
490 err = got_object_qid_alloc_partial(&qid);
491 if (err)
492 break;
493 memcpy(qid->id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
494 SIMPLEQ_INSERT_TAIL(matched_ids, qid, entry);
496 oid = &packidx->hdr.sorted_ids[++i];
499 if (err)
500 got_object_id_queue_free(matched_ids);
501 return err;
504 const struct got_error *
505 got_pack_stop_privsep_child(struct got_pack *pack)
507 const struct got_error *err = NULL;
509 if (pack->privsep_child == NULL)
510 return NULL;
512 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
513 if (err)
514 return err;
515 err = got_privsep_wait_for_child(pack->privsep_child->pid);
516 if (close(pack->privsep_child->imsg_fd) != 0 && err == NULL)
517 err = got_error_from_errno("close");
518 free(pack->privsep_child);
519 pack->privsep_child = NULL;
520 return err;
523 const struct got_error *
524 got_pack_close(struct got_pack *pack)
526 const struct got_error *err = NULL;
528 err = got_pack_stop_privsep_child(pack);
529 if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
530 err = got_error_from_errno("munmap");
531 if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
532 err = got_error_from_errno("close");
533 pack->fd = -1;
534 free(pack->path_packfile);
535 pack->path_packfile = NULL;
536 pack->filesize = 0;
538 return err;
541 static const struct got_error *
542 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
543 struct got_pack *pack, off_t offset)
545 uint8_t t = 0;
546 uint64_t s = 0;
547 uint8_t sizeN;
548 size_t mapoff = 0;
549 int i = 0;
551 *len = 0;
553 if (offset >= pack->filesize)
554 return got_error(GOT_ERR_PACK_OFFSET);
556 if (pack->map) {
557 mapoff = (size_t)offset;
558 } else {
559 if (lseek(pack->fd, offset, SEEK_SET) == -1)
560 return got_error_from_errno("lseek");
563 do {
564 /* We do not support size values which don't fit in 64 bit. */
565 if (i > 9)
566 return got_error(GOT_ERR_NO_SPACE);
568 if (pack->map) {
569 sizeN = *(pack->map + mapoff);
570 mapoff += sizeof(sizeN);
571 } else {
572 ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
573 if (n < 0)
574 return got_error_from_errno("read");
575 if (n != sizeof(sizeN))
576 return got_error(GOT_ERR_BAD_PACKFILE);
578 *len += sizeof(sizeN);
580 if (i == 0) {
581 t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
582 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
583 s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
584 } else {
585 size_t shift = 4 + 7 * (i - 1);
586 s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
588 i++;
589 } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
591 *type = t;
592 *size = s;
593 return NULL;
596 static const struct got_error *
597 open_plain_object(struct got_object **obj, const char *path_packfile,
598 struct got_object_id *id, uint8_t type, off_t offset, size_t size, int idx)
600 *obj = calloc(1, sizeof(**obj));
601 if (*obj == NULL)
602 return got_error_from_errno("calloc");
604 (*obj)->path_packfile = strdup(path_packfile);
605 if ((*obj)->path_packfile == NULL) {
606 const struct got_error *err = got_error_from_errno("strdup");
607 free(*obj);
608 *obj = NULL;
609 return err;
612 (*obj)->type = type;
613 (*obj)->flags = GOT_OBJ_FLAG_PACKED;
614 (*obj)->pack_idx = idx;
615 (*obj)->hdrlen = 0;
616 (*obj)->size = size;
617 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
618 (*obj)->pack_offset = offset;
620 return NULL;
623 static const struct got_error *
624 parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
625 off_t delta_offset)
627 int64_t o = 0;
628 uint8_t offN;
629 int i = 0;
631 *offset = 0;
632 *len = 0;
634 do {
635 /* We do not support offset values which don't fit in 64 bit. */
636 if (i > 8)
637 return got_error(GOT_ERR_NO_SPACE);
639 if (pack->map) {
640 size_t mapoff;
641 if (delta_offset >= pack->filesize)
642 return got_error(GOT_ERR_PACK_OFFSET);
643 mapoff = (size_t)delta_offset + *len;
644 offN = *(pack->map + mapoff);
645 } else {
646 ssize_t n;
647 n = read(pack->fd, &offN, sizeof(offN));
648 if (n < 0)
649 return got_error_from_errno("read");
650 if (n != sizeof(offN))
651 return got_error(GOT_ERR_BAD_PACKFILE);
653 *len += sizeof(offN);
655 if (i == 0)
656 o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
657 else {
658 o++;
659 o <<= 7;
660 o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
662 i++;
663 } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
665 *offset = o;
666 return NULL;
669 static const struct got_error *
670 parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
671 off_t offset, int tslen)
673 const struct got_error *err;
674 int64_t negoffset;
675 size_t negofflen;
677 *len = 0;
679 err = parse_negative_offset(&negoffset, &negofflen, pack,
680 offset + tslen);
681 if (err)
682 return err;
684 /* Compute the base object's offset (must be in the same pack file). */
685 *base_offset = (offset - negoffset);
686 if (*base_offset <= 0)
687 return got_error(GOT_ERR_BAD_PACKFILE);
689 *len = negofflen;
690 return NULL;
693 static const struct got_error *
694 resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
695 struct got_pack *, off_t, size_t, int, size_t, unsigned int);
697 static const struct got_error *
698 add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
699 int delta_type, size_t delta_size, size_t delta_data_offset,
700 uint8_t *delta_buf, size_t delta_len)
702 struct got_delta *delta;
704 delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
705 delta_data_offset, delta_buf,
706 delta_len);
707 if (delta == NULL)
708 return got_error_from_errno("got_delta_open");
709 /* delta is freed in got_object_close() */
710 deltas->nentries++;
711 SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
712 return NULL;
715 static const struct got_error *
716 resolve_offset_delta(struct got_delta_chain *deltas,
717 struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
718 size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
721 const struct got_error *err;
722 off_t base_offset;
723 uint8_t base_type;
724 uint64_t base_size;
725 size_t base_tslen;
726 off_t delta_data_offset;
727 uint8_t *delta_buf;
728 size_t delta_len, consumed;
730 err = parse_offset_delta(&base_offset, &consumed, pack,
731 delta_offset, tslen);
732 if (err)
733 return err;
735 delta_data_offset = delta_offset + tslen + consumed;
736 if (delta_data_offset >= pack->filesize)
737 return got_error(GOT_ERR_PACK_OFFSET);
739 if (pack->map == NULL) {
740 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
741 if (delta_data_offset == -1)
742 return got_error_from_errno("lseek");
745 if (pack->map) {
746 size_t mapoff = (size_t)delta_data_offset;
747 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
748 mapoff, pack->filesize - mapoff);
749 if (err)
750 return err;
751 } else {
753 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
754 if (err)
755 return err;
758 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
759 delta_data_offset, delta_buf, delta_len);
760 if (err)
761 return err;
763 /* An offset delta must be in the same packfile. */
764 if (base_offset >= pack->filesize)
765 return got_error(GOT_ERR_PACK_OFFSET);
767 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
768 pack, base_offset);
769 if (err)
770 return err;
772 return resolve_delta_chain(deltas, packidx, pack, base_offset,
773 base_tslen, base_type, base_size, recursion - 1);
776 static const struct got_error *
777 resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
778 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
779 size_t delta_size, unsigned int recursion)
781 const struct got_error *err;
782 struct got_object_id id;
783 int idx;
784 off_t base_offset;
785 uint8_t base_type;
786 uint64_t base_size;
787 size_t base_tslen;
788 off_t delta_data_offset;
789 uint8_t *delta_buf;
790 size_t delta_len;
792 if (delta_offset >= pack->filesize)
793 return got_error(GOT_ERR_PACK_OFFSET);
794 delta_data_offset = delta_offset + tslen;
795 if (delta_data_offset >= pack->filesize)
796 return got_error(GOT_ERR_PACK_OFFSET);
798 if (pack->map == NULL) {
799 delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
800 if (delta_data_offset == -1)
801 return got_error_from_errno("lseek");
804 if (pack->map) {
805 size_t mapoff = (size_t)delta_data_offset;
806 memcpy(&id, pack->map + mapoff, sizeof(id));
807 mapoff += sizeof(id);
808 err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
809 mapoff, pack->filesize - mapoff);
810 if (err)
811 return err;
812 } else {
813 ssize_t n = read(pack->fd, &id, sizeof(id));
814 if (n < 0)
815 return got_error_from_errno("read");
816 if (n != sizeof(id))
817 return got_error(GOT_ERR_BAD_PACKFILE);
818 err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
819 if (err)
820 return err;
823 err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
824 delta_data_offset, delta_buf, delta_len);
825 if (err)
826 return err;
828 /* Delta base must be in the same pack file. */
829 idx = got_packidx_get_object_idx(packidx, &id);
830 if (idx == -1)
831 return got_error(GOT_ERR_BAD_PACKFILE);
833 base_offset = get_object_offset(packidx, idx);
834 if (base_offset == (uint64_t)-1)
835 return got_error(GOT_ERR_BAD_PACKIDX);
837 if (base_offset >= pack->filesize)
838 return got_error(GOT_ERR_PACK_OFFSET);
840 err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
841 pack, base_offset);
842 if (err)
843 return err;
845 return resolve_delta_chain(deltas, packidx, pack, base_offset,
846 base_tslen, base_type, base_size, recursion - 1);
849 static const struct got_error *
850 resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
851 struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
852 size_t delta_size, unsigned int recursion)
854 const struct got_error *err = NULL;
856 if (--recursion == 0)
857 return got_error(GOT_ERR_RECURSION);
859 switch (delta_type) {
860 case GOT_OBJ_TYPE_COMMIT:
861 case GOT_OBJ_TYPE_TREE:
862 case GOT_OBJ_TYPE_BLOB:
863 case GOT_OBJ_TYPE_TAG:
864 /* Plain types are the final delta base. Recursion ends. */
865 err = add_delta(deltas, delta_offset, tslen, delta_type,
866 delta_size, 0, NULL, 0);
867 break;
868 case GOT_OBJ_TYPE_OFFSET_DELTA:
869 err = resolve_offset_delta(deltas, packidx, pack,
870 delta_offset, tslen, delta_type, delta_size, recursion - 1);
871 break;
872 case GOT_OBJ_TYPE_REF_DELTA:
873 err = resolve_ref_delta(deltas, packidx, pack,
874 delta_offset, tslen, delta_type, delta_size, recursion - 1);
875 break;
876 default:
877 return got_error(GOT_ERR_OBJ_TYPE);
880 return err;
883 static const struct got_error *
884 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
885 struct got_pack *pack, struct got_object_id *id, off_t offset,
886 size_t tslen, int delta_type, size_t delta_size, int idx)
888 const struct got_error *err = NULL;
889 int resolved_type;
891 *obj = calloc(1, sizeof(**obj));
892 if (*obj == NULL)
893 return got_error_from_errno("calloc");
895 (*obj)->flags = 0;
896 (*obj)->hdrlen = 0;
897 (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
898 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
899 (*obj)->pack_offset = offset + tslen;
901 SIMPLEQ_INIT(&(*obj)->deltas.entries);
902 (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
904 (*obj)->path_packfile = strdup(pack->path_packfile);
905 if ((*obj)->path_packfile == NULL) {
906 err = got_error_from_errno("strdup");
907 goto done;
909 (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
910 (*obj)->pack_idx = idx;
912 err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
913 tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
914 if (err)
915 goto done;
917 err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
918 if (err)
919 goto done;
920 (*obj)->type = resolved_type;
921 done:
922 if (err) {
923 got_object_close(*obj);
924 *obj = NULL;
926 return err;
929 const struct got_error *
930 got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
931 struct got_packidx *packidx, int idx, struct got_object_id *id)
933 const struct got_error *err = NULL;
934 off_t offset;
935 uint8_t type;
936 uint64_t size;
937 size_t tslen;
939 *obj = NULL;
941 offset = get_object_offset(packidx, idx);
942 if (offset == (uint64_t)-1)
943 return got_error(GOT_ERR_BAD_PACKIDX);
945 err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
946 if (err)
947 return err;
949 switch (type) {
950 case GOT_OBJ_TYPE_COMMIT:
951 case GOT_OBJ_TYPE_TREE:
952 case GOT_OBJ_TYPE_BLOB:
953 case GOT_OBJ_TYPE_TAG:
954 err = open_plain_object(obj, pack->path_packfile, id, type,
955 offset + tslen, size, idx);
956 break;
957 case GOT_OBJ_TYPE_OFFSET_DELTA:
958 case GOT_OBJ_TYPE_REF_DELTA:
959 err = open_delta_object(obj, packidx, pack, id, offset,
960 tslen, type, size, idx);
961 break;
962 default:
963 err = got_error(GOT_ERR_OBJ_TYPE);
964 break;
967 return err;
970 static const struct got_error *
971 get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
973 struct got_delta *delta;
974 uint64_t base_size = 0, result_size = 0;
976 *max_size = 0;
977 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
978 /* Plain object types are the delta base. */
979 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
980 delta->type != GOT_OBJ_TYPE_TREE &&
981 delta->type != GOT_OBJ_TYPE_BLOB &&
982 delta->type != GOT_OBJ_TYPE_TAG) {
983 const struct got_error *err;
984 err = got_delta_get_sizes(&base_size, &result_size,
985 delta->delta_buf, delta->delta_len);
986 if (err)
987 return err;
988 } else
989 base_size = delta->size;
990 if (base_size > *max_size)
991 *max_size = base_size;
992 if (result_size > *max_size)
993 *max_size = result_size;
996 return NULL;
999 const struct got_error *
1000 got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj)
1002 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1003 return got_error(GOT_ERR_OBJ_TYPE);
1005 return get_delta_chain_max_size(size, &obj->deltas);
1008 static const struct got_error *
1009 dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1010 struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
1012 const struct got_error *err = NULL;
1013 struct got_delta *delta;
1014 uint8_t *base_buf = NULL, *accum_buf = NULL;
1015 size_t base_bufsz = 0, accum_size = 0;
1016 uint64_t max_size;
1017 int n = 0;
1019 *result_size = 0;
1021 if (SIMPLEQ_EMPTY(&deltas->entries))
1022 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1024 /* We process small enough files entirely in memory for speed. */
1025 err = get_delta_chain_max_size(&max_size, deltas);
1026 if (err)
1027 return err;
1028 if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1029 accum_buf = malloc(max_size);
1030 if (accum_buf == NULL)
1031 return got_error_from_errno("malloc");
1032 base_file = NULL;
1033 accum_file = NULL;
1036 /* Deltas are ordered in ascending order. */
1037 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1038 if (n == 0) {
1039 size_t mapoff;
1040 off_t delta_data_offset;
1042 /* Plain object types are the delta base. */
1043 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1044 delta->type != GOT_OBJ_TYPE_TREE &&
1045 delta->type != GOT_OBJ_TYPE_BLOB &&
1046 delta->type != GOT_OBJ_TYPE_TAG) {
1047 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1048 goto done;
1051 delta_data_offset = delta->offset + delta->tslen;
1052 if (delta_data_offset >= pack->filesize) {
1053 err = got_error(GOT_ERR_PACK_OFFSET);
1054 goto done;
1056 if (pack->map == NULL) {
1057 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1058 == -1) {
1059 err = got_error_from_errno("lseek");
1060 goto done;
1063 if (base_file) {
1064 if (pack->map) {
1065 mapoff = (size_t)delta_data_offset;
1066 err = got_inflate_to_file_mmap(
1067 &base_bufsz, pack->map, mapoff,
1068 pack->filesize - mapoff, base_file);
1069 } else
1070 err = got_inflate_to_file_fd(
1071 &base_bufsz, pack->fd, base_file);
1072 } else {
1073 if (pack->map) {
1074 mapoff = (size_t)delta_data_offset;
1075 err = got_inflate_to_mem_mmap(&base_buf,
1076 &base_bufsz, pack->map, mapoff,
1077 pack->filesize - mapoff);
1078 } else
1079 err = got_inflate_to_mem_fd(&base_buf,
1080 &base_bufsz, pack->fd);
1082 if (err)
1083 goto done;
1084 n++;
1085 if (base_file)
1086 rewind(base_file);
1087 continue;
1090 if (base_buf) {
1091 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1092 delta->delta_buf, delta->delta_len, accum_buf,
1093 &accum_size, max_size);
1094 n++;
1095 } else {
1096 err = got_delta_apply(base_file, delta->delta_buf,
1097 delta->delta_len,
1098 /* Final delta application writes to output file. */
1099 ++n < deltas->nentries ? accum_file : outfile,
1100 &accum_size);
1102 if (err)
1103 goto done;
1105 if (n < deltas->nentries) {
1106 /* Accumulated delta becomes the new base. */
1107 if (base_buf) {
1108 uint8_t *tmp = accum_buf;
1110 * Base buffer switches roles with accumulation
1111 * buffer. Ensure it can hold the largest
1112 * result in the delta chain. The initial
1113 * allocation might have been smaller.
1115 if (base_bufsz < max_size) {
1116 uint8_t *p;
1117 p = reallocarray(base_buf, 1, max_size);
1118 if (p == NULL) {
1119 err = got_error_from_errno(
1120 "reallocarray");
1121 goto done;
1123 base_buf = p;
1124 base_bufsz = max_size;
1126 accum_buf = base_buf;
1127 base_buf = tmp;
1128 } else {
1129 FILE *tmp = accum_file;
1130 accum_file = base_file;
1131 base_file = tmp;
1132 rewind(base_file);
1133 rewind(accum_file);
1138 done:
1139 free(base_buf);
1140 if (accum_buf) {
1141 size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1142 free(accum_buf);
1143 if (len != accum_size)
1144 err = got_ferror(outfile, GOT_ERR_IO);
1146 rewind(outfile);
1147 if (err == NULL)
1148 *result_size = accum_size;
1149 return err;
1152 static const struct got_error *
1153 dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1154 struct got_delta_chain *deltas, struct got_pack *pack)
1156 const struct got_error *err = NULL;
1157 struct got_delta *delta;
1158 uint8_t *base_buf = NULL, *accum_buf = NULL;
1159 size_t base_bufsz = 0, accum_size = 0;
1160 uint64_t max_size;
1161 int n = 0;
1163 *outbuf = NULL;
1164 *outlen = 0;
1166 if (SIMPLEQ_EMPTY(&deltas->entries))
1167 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1169 err = get_delta_chain_max_size(&max_size, deltas);
1170 if (err)
1171 return err;
1172 accum_buf = malloc(max_size);
1173 if (accum_buf == NULL)
1174 return got_error_from_errno("malloc");
1176 /* Deltas are ordered in ascending order. */
1177 SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1178 if (n == 0) {
1179 size_t delta_data_offset;
1181 /* Plain object types are the delta base. */
1182 if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1183 delta->type != GOT_OBJ_TYPE_TREE &&
1184 delta->type != GOT_OBJ_TYPE_BLOB &&
1185 delta->type != GOT_OBJ_TYPE_TAG) {
1186 err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1187 goto done;
1190 delta_data_offset = delta->offset + delta->tslen;
1191 if (delta_data_offset >= pack->filesize) {
1192 err = got_error(GOT_ERR_PACK_OFFSET);
1193 goto done;
1195 if (pack->map) {
1196 size_t mapoff = (size_t)delta_data_offset;
1197 err = got_inflate_to_mem_mmap(&base_buf,
1198 &base_bufsz, pack->map, mapoff,
1199 pack->filesize - mapoff);
1200 } else {
1201 if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1202 == -1) {
1203 err = got_error_from_errno("lseek");
1204 goto done;
1206 err = got_inflate_to_mem_fd(&base_buf,
1207 &base_bufsz, pack->fd);
1209 if (err)
1210 goto done;
1211 n++;
1212 continue;
1215 err = got_delta_apply_in_mem(base_buf, base_bufsz,
1216 delta->delta_buf, delta->delta_len, accum_buf,
1217 &accum_size, max_size);
1218 n++;
1219 if (err)
1220 goto done;
1222 if (n < deltas->nentries) {
1223 /* Accumulated delta becomes the new base. */
1224 uint8_t *tmp = accum_buf;
1226 * Base buffer switches roles with accumulation buffer.
1227 * Ensure it can hold the largest result in the delta
1228 * chain. Initial allocation might have been smaller.
1230 if (base_bufsz < max_size) {
1231 uint8_t *p;
1232 p = reallocarray(base_buf, 1, max_size);
1233 if (p == NULL) {
1234 err = got_error_from_errno(
1235 "reallocarray");
1236 goto done;
1238 base_buf = p;
1239 base_bufsz = max_size;
1241 accum_buf = base_buf;
1242 base_buf = tmp;
1246 done:
1247 free(base_buf);
1248 if (err) {
1249 free(accum_buf);
1250 *outbuf = NULL;
1251 *outlen = 0;
1252 } else {
1253 *outbuf = accum_buf;
1254 *outlen = accum_size;
1256 return err;
1259 const struct got_error *
1260 got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1261 FILE *outfile, FILE *base_file, FILE *accum_file)
1263 const struct got_error *err = NULL;
1265 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1266 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1268 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1269 if (obj->pack_offset >= pack->filesize)
1270 return got_error(GOT_ERR_PACK_OFFSET);
1272 if (pack->map) {
1273 size_t mapoff = (size_t)obj->pack_offset;
1274 err = got_inflate_to_file_mmap(&obj->size, pack->map,
1275 mapoff, pack->filesize - mapoff, outfile);
1276 } else {
1277 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1278 return got_error_from_errno("lseek");
1279 err = got_inflate_to_file_fd(&obj->size, pack->fd,
1280 outfile);
1282 } else
1283 err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1284 outfile, base_file, accum_file);
1286 return err;
1289 const struct got_error *
1290 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1291 struct got_object *obj, struct got_pack *pack)
1293 const struct got_error *err = NULL;
1295 if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1296 return got_error(GOT_ERR_OBJ_NOT_PACKED);
1298 if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1299 if (obj->pack_offset >= pack->filesize)
1300 return got_error(GOT_ERR_PACK_OFFSET);
1301 if (pack->map) {
1302 size_t mapoff = (size_t)obj->pack_offset;
1303 err = got_inflate_to_mem_mmap(buf, len, pack->map,
1304 mapoff, pack->filesize - mapoff);
1305 } else {
1306 if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1307 return got_error_from_errno("lseek");
1308 err = got_inflate_to_mem_fd(buf, len, pack->fd);
1310 } else
1311 err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
1313 return err;