Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/mman.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <imsg.h>
37 #include <time.h>
38 #include <unistd.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_hash.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_qid.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
56 #ifndef nitems
57 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 #endif
60 const struct got_error *
61 got_object_type_label(const char **label, int obj_type)
62 {
63 const struct got_error *err = NULL;
65 switch (obj_type) {
66 case GOT_OBJ_TYPE_BLOB:
67 *label = GOT_OBJ_LABEL_BLOB;
68 break;
69 case GOT_OBJ_TYPE_TREE:
70 *label = GOT_OBJ_LABEL_TREE;
71 break;
72 case GOT_OBJ_TYPE_COMMIT:
73 *label = GOT_OBJ_LABEL_COMMIT;
74 break;
75 case GOT_OBJ_TYPE_TAG:
76 *label = GOT_OBJ_LABEL_TAG;
77 break;
78 default:
79 *label = NULL;
80 err = got_error(GOT_ERR_OBJ_TYPE);
81 break;
82 }
84 return err;
85 }
87 void
88 got_object_close(struct got_object *obj)
89 {
90 if (obj->refcnt > 0) {
91 obj->refcnt--;
92 if (obj->refcnt > 0)
93 return;
94 }
96 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
97 struct got_delta *delta;
98 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
99 delta = STAILQ_FIRST(&obj->deltas.entries);
100 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
101 free(delta);
104 free(obj);
107 const struct got_error *
108 got_object_raw_close(struct got_raw_object *obj)
110 const struct got_error *err = NULL;
112 if (obj->refcnt > 0) {
113 obj->refcnt--;
114 if (obj->refcnt > 0)
115 return NULL;
118 if (obj->close_cb)
119 obj->close_cb(obj);
121 if (obj->f == NULL) {
122 if (obj->fd != -1) {
123 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
124 err = got_error_from_errno("munmap");
125 if (close(obj->fd) == -1 && err == NULL)
126 err = got_error_from_errno("close");
127 } else
128 free(obj->data);
129 } else {
130 if (fclose(obj->f) == EOF && err == NULL)
131 err = got_error_from_errno("fclose");
133 free(obj);
134 return err;
137 const struct got_error *
138 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
140 const char *obj_labels[] = {
141 GOT_OBJ_LABEL_COMMIT,
142 GOT_OBJ_LABEL_TREE,
143 GOT_OBJ_LABEL_BLOB,
144 GOT_OBJ_LABEL_TAG,
145 };
146 const int obj_types[] = {
147 GOT_OBJ_TYPE_COMMIT,
148 GOT_OBJ_TYPE_TREE,
149 GOT_OBJ_TYPE_BLOB,
150 GOT_OBJ_TYPE_TAG,
151 };
152 int type = 0;
153 size_t size = 0;
154 size_t i;
155 char *end;
157 *obj = NULL;
159 end = memchr(buf, '\0', len);
160 if (end == NULL)
161 return got_error(GOT_ERR_BAD_OBJ_HDR);
163 for (i = 0; i < nitems(obj_labels); i++) {
164 const char *label = obj_labels[i];
165 size_t label_len = strlen(label);
166 const char *errstr;
168 if (len <= label_len || buf + label_len >= end ||
169 strncmp(buf, label, label_len) != 0)
170 continue;
172 type = obj_types[i];
173 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
174 if (errstr != NULL)
175 return got_error(GOT_ERR_BAD_OBJ_HDR);
176 break;
179 if (type == 0)
180 return got_error(GOT_ERR_BAD_OBJ_HDR);
182 *obj = calloc(1, sizeof(**obj));
183 if (*obj == NULL)
184 return got_error_from_errno("calloc");
185 (*obj)->type = type;
186 (*obj)->hdrlen = end - buf + 1;
187 (*obj)->size = size;
188 return NULL;
191 const struct got_error *
192 got_object_read_header(struct got_object **obj, int fd)
194 const struct got_error *err;
195 struct got_inflate_buf zb;
196 uint8_t *buf;
197 const size_t zbsize = 64;
198 size_t outlen, totlen;
199 int nbuf = 1;
201 *obj = NULL;
203 buf = malloc(zbsize);
204 if (buf == NULL)
205 return got_error_from_errno("malloc");
206 buf[0] = '\0';
208 err = got_inflate_init(&zb, buf, zbsize, NULL);
209 if (err)
210 return err;
212 totlen = 0;
213 do {
214 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
215 if (err)
216 goto done;
217 if (outlen == 0)
218 break;
219 totlen += outlen;
220 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
221 uint8_t *newbuf;
222 nbuf++;
223 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
224 if (newbuf == NULL) {
225 err = got_error_from_errno("recallocarray");
226 goto done;
228 buf = newbuf;
229 zb.outbuf = newbuf + totlen;
230 zb.outlen = (nbuf * zbsize) - totlen;
232 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
234 err = got_object_parse_header(obj, buf, totlen);
235 done:
236 free(buf);
237 got_inflate_end(&zb);
238 return err;
241 const struct got_error *
242 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
243 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
244 int infd)
246 const struct got_error *err = NULL;
247 struct got_object *obj;
248 struct got_inflate_checksum csum;
249 struct got_object_id id;
250 struct got_hash ctx;
251 size_t len, consumed;
252 FILE *f = NULL;
254 *outbuf = NULL;
255 *size = 0;
256 *hdrlen = 0;
258 got_hash_init(&ctx, GOT_HASH_SHA1);
259 memset(&csum, 0, sizeof(csum));
260 csum.output_ctx = &ctx;
262 if (lseek(infd, SEEK_SET, 0) == -1)
263 return got_error_from_errno("lseek");
265 err = got_object_read_header(&obj, infd);
266 if (err)
267 return err;
269 if (lseek(infd, SEEK_SET, 0) == -1)
270 return got_error_from_errno("lseek");
272 if (obj->size + obj->hdrlen <= max_in_mem_size) {
273 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
274 obj->size + obj->hdrlen, infd);
275 } else {
276 int fd;
277 /*
278 * XXX This uses an extra file descriptor for no good reason.
279 * We should have got_inflate_fd_to_fd().
280 */
281 fd = dup(infd);
282 if (fd == -1)
283 return got_error_from_errno("dup");
284 f = fdopen(fd, "r");
285 if (f == NULL) {
286 err = got_error_from_errno("fdopen");
287 abort();
288 close(fd);
289 goto done;
291 err = got_inflate_to_fd(&len, f, &csum, outfd);
293 if (err)
294 goto done;
296 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
297 err = got_error(GOT_ERR_BAD_OBJ_HDR);
298 goto done;
301 got_hash_final_object_id(&ctx, &id);
302 if (got_object_id_cmp(expected_id, &id) != 0) {
303 err = got_error_checksum(expected_id);
304 goto done;
307 *size = obj->size;
308 *hdrlen = obj->hdrlen;
309 done:
310 got_object_close(obj);
311 if (f && fclose(f) == EOF && err == NULL)
312 err = got_error_from_errno("fclose");
313 return err;
316 struct got_commit_object *
317 got_object_commit_alloc_partial(void)
319 struct got_commit_object *commit;
321 commit = calloc(1, sizeof(*commit));
322 if (commit == NULL)
323 return NULL;
324 commit->tree_id = malloc(sizeof(*commit->tree_id));
325 if (commit->tree_id == NULL) {
326 free(commit);
327 return NULL;
330 STAILQ_INIT(&commit->parent_ids);
332 return commit;
335 const struct got_error *
336 got_object_commit_add_parent(struct got_commit_object *commit,
337 const char *id_str)
339 const struct got_error *err = NULL;
340 struct got_object_qid *qid;
342 err = got_object_qid_alloc_partial(&qid);
343 if (err)
344 return err;
346 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
347 err = got_error(GOT_ERR_BAD_OBJ_DATA);
348 got_object_qid_free(qid);
349 return err;
352 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
353 commit->nparents++;
355 return NULL;
358 static const struct got_error *
359 parse_gmtoff(time_t *gmtoff, const char *tzstr)
361 int sign = 1;
362 const char *p = tzstr;
363 time_t h, m;
365 *gmtoff = 0;
367 if (*p == '-')
368 sign = -1;
369 else if (*p != '+')
370 return got_error(GOT_ERR_BAD_OBJ_DATA);
371 p++;
372 if (!isdigit((unsigned char)*p) &&
373 !isdigit((unsigned char)*(p + 1)))
374 return got_error(GOT_ERR_BAD_OBJ_DATA);
375 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
377 p += 2;
378 if (!isdigit((unsigned char)*p) &&
379 !isdigit((unsigned char)*(p + 1)))
380 return got_error(GOT_ERR_BAD_OBJ_DATA);
381 m = ((*p - '0') * 10) + (*(p + 1) - '0');
383 *gmtoff = (h * 60 * 60 + m * 60) * sign;
384 return NULL;
387 static const struct got_error *
388 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
390 const struct got_error *err = NULL;
391 const char *errstr;
392 char *space, *tzstr;
394 /* Parse and strip off trailing timezone indicator string. */
395 space = strrchr(committer, ' ');
396 if (space == NULL)
397 return got_error(GOT_ERR_BAD_OBJ_DATA);
398 tzstr = strdup(space + 1);
399 if (tzstr == NULL)
400 return got_error_from_errno("strdup");
401 err = parse_gmtoff(gmtoff, tzstr);
402 free(tzstr);
403 if (err) {
404 if (err->code != GOT_ERR_BAD_OBJ_DATA)
405 return err;
406 /* Old versions of Git omitted the timestamp. */
407 *time = 0;
408 *gmtoff = 0;
409 return NULL;
411 *space = '\0';
413 /* Timestamp is separated from committer name + email by space. */
414 space = strrchr(committer, ' ');
415 if (space == NULL)
416 return got_error(GOT_ERR_BAD_OBJ_DATA);
418 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
419 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
420 if (errstr)
421 return got_error(GOT_ERR_BAD_OBJ_DATA);
423 /* Strip off parsed time information, leaving just author and email. */
424 *space = '\0';
426 return NULL;
429 void
430 got_object_commit_close(struct got_commit_object *commit)
432 if (commit->refcnt > 0) {
433 commit->refcnt--;
434 if (commit->refcnt > 0)
435 return;
438 got_object_id_queue_free(&commit->parent_ids);
439 free(commit->tree_id);
440 free(commit->author);
441 free(commit->committer);
442 free(commit->logmsg);
443 free(commit);
446 struct got_object_id *
447 got_object_commit_get_tree_id(struct got_commit_object *commit)
449 return commit->tree_id;
452 int
453 got_object_commit_get_nparents(struct got_commit_object *commit)
455 return commit->nparents;
458 const struct got_object_id_queue *
459 got_object_commit_get_parent_ids(struct got_commit_object *commit)
461 return &commit->parent_ids;
464 const char *
465 got_object_commit_get_author(struct got_commit_object *commit)
467 return commit->author;
470 time_t
471 got_object_commit_get_author_time(struct got_commit_object *commit)
473 return commit->author_time;
476 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
478 return commit->author_gmtoff;
481 const char *
482 got_object_commit_get_committer(struct got_commit_object *commit)
484 return commit->committer;
487 time_t
488 got_object_commit_get_committer_time(struct got_commit_object *commit)
490 return commit->committer_time;
493 time_t
494 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
496 return commit->committer_gmtoff;
499 const struct got_error *
500 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
502 const struct got_error *err = NULL;
503 const char *src;
504 char *dst;
505 size_t len;
507 len = strlen(commit->logmsg);
508 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
509 if (*logmsg == NULL)
510 return got_error_from_errno("malloc");
512 /*
513 * Strip out unusual headers. Headers are separated from the commit
514 * message body by a single empty line.
515 */
516 src = commit->logmsg;
517 dst = *logmsg;
518 while (*src != '\0' && *src != '\n') {
519 int copy_header = 1, eol = 0;
520 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
521 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
522 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
523 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
524 strncmp(src, GOT_COMMIT_LABEL_PARENT,
525 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
526 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
527 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
528 copy_header = 0;
530 while (*src != '\0' && !eol) {
531 if (copy_header) {
532 *dst = *src;
533 dst++;
535 if (*src == '\n')
536 eol = 1;
537 src++;
540 *dst = '\0';
542 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
543 err = got_error(GOT_ERR_NO_SPACE);
544 goto done;
547 /* Trim redundant trailing whitespace. */
548 len = strlen(*logmsg);
549 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
550 isspace((unsigned char)(*logmsg)[len - 1])) {
551 (*logmsg)[len - 1] = '\0';
552 len--;
555 /* Append a trailing newline if missing. */
556 if (len > 0 && (*logmsg)[len - 1] != '\n') {
557 (*logmsg)[len] = '\n';
558 (*logmsg)[len + 1] = '\0';
560 done:
561 if (err) {
562 free(*logmsg);
563 *logmsg = NULL;
565 return err;
568 const char *
569 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
571 return commit->logmsg;
574 const struct got_error *
575 got_object_parse_commit(struct got_commit_object **commit, char *buf,
576 size_t len)
578 const struct got_error *err = NULL;
579 enum got_hash_algorithm algo = GOT_HASH_SHA1;
580 char *s = buf;
581 size_t label_len;
582 ssize_t remain = (ssize_t)len;
584 if (remain == 0)
585 return got_error(GOT_ERR_BAD_OBJ_DATA);
587 *commit = got_object_commit_alloc_partial();
588 if (*commit == NULL)
589 return got_error_from_errno("got_object_commit_alloc_partial");
591 label_len = strlen(GOT_COMMIT_LABEL_TREE);
592 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
593 remain -= label_len;
594 if (remain < SHA1_DIGEST_STRING_LENGTH) {
595 err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 goto done;
598 s += label_len;
599 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
600 err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 goto done;
603 remain -= SHA1_DIGEST_STRING_LENGTH;
604 s += SHA1_DIGEST_STRING_LENGTH;
605 } else {
606 err = got_error(GOT_ERR_BAD_OBJ_DATA);
607 goto done;
610 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
611 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
612 remain -= label_len;
613 if (remain < SHA1_DIGEST_STRING_LENGTH) {
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 goto done;
617 s += label_len;
618 err = got_object_commit_add_parent(*commit, s);
619 if (err)
620 goto done;
622 remain -= SHA1_DIGEST_STRING_LENGTH;
623 s += SHA1_DIGEST_STRING_LENGTH;
626 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
627 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
628 char *p;
629 size_t slen;
631 remain -= label_len;
632 if (remain <= 0) {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 goto done;
636 s += label_len;
637 p = memchr(s, '\n', remain);
638 if (p == NULL) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 goto done;
642 *p = '\0';
643 slen = strlen(s);
644 err = parse_commit_time(&(*commit)->author_time,
645 &(*commit)->author_gmtoff, s);
646 if (err)
647 goto done;
648 (*commit)->author = strdup(s);
649 if ((*commit)->author == NULL) {
650 err = got_error_from_errno("strdup");
651 goto done;
653 s += slen + 1;
654 remain -= slen + 1;
657 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
658 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
659 char *p;
660 size_t slen;
662 remain -= label_len;
663 if (remain <= 0) {
664 err = got_error(GOT_ERR_BAD_OBJ_DATA);
665 goto done;
667 s += label_len;
668 p = memchr(s, '\n', remain);
669 if (p == NULL) {
670 err = got_error(GOT_ERR_BAD_OBJ_DATA);
671 goto done;
673 *p = '\0';
674 slen = strlen(s);
675 err = parse_commit_time(&(*commit)->committer_time,
676 &(*commit)->committer_gmtoff, s);
677 if (err)
678 goto done;
679 (*commit)->committer = strdup(s);
680 if ((*commit)->committer == NULL) {
681 err = got_error_from_errno("strdup");
682 goto done;
684 s += slen + 1;
685 remain -= slen + 1;
688 (*commit)->logmsg = strndup(s, remain);
689 if ((*commit)->logmsg == NULL) {
690 err = got_error_from_errno("strndup");
691 goto done;
693 done:
694 if (err) {
695 got_object_commit_close(*commit);
696 *commit = NULL;
698 return err;
701 const struct got_error *
702 got_object_read_commit(struct got_commit_object **commit, int fd,
703 struct got_object_id *expected_id, size_t expected_size)
705 struct got_object *obj = NULL;
706 const struct got_error *err = NULL;
707 size_t len;
708 uint8_t *p;
709 struct got_inflate_checksum csum;
710 struct got_hash ctx;
711 struct got_object_id id;
713 got_hash_init(&ctx, GOT_HASH_SHA1);
714 memset(&csum, 0, sizeof(csum));
715 csum.output_ctx = &ctx;
717 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
718 if (err)
719 return err;
721 got_hash_final_object_id(&ctx, &id);
722 if (got_object_id_cmp(expected_id, &id) != 0) {
723 err = got_error_checksum(expected_id);
724 goto done;
727 err = got_object_parse_header(&obj, p, len);
728 if (err)
729 goto done;
731 if (len < obj->hdrlen + obj->size) {
732 err = got_error(GOT_ERR_BAD_OBJ_DATA);
733 goto done;
736 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
737 err = got_error(GOT_ERR_OBJ_TYPE);
738 goto done;
741 /* Skip object header. */
742 len -= obj->hdrlen;
743 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
744 done:
745 free(p);
746 if (obj)
747 got_object_close(obj);
748 return err;
751 void
752 got_object_tree_close(struct got_tree_object *tree)
754 if (tree->refcnt > 0) {
755 tree->refcnt--;
756 if (tree->refcnt > 0)
757 return;
760 free(tree->entries);
761 free(tree);
764 const struct got_error *
765 got_object_parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen,
766 char *buf, size_t maxlen)
768 char *p, *space;
770 *elen = 0;
772 *elen = strnlen(buf, maxlen) + 1;
773 if (*elen > maxlen)
774 return got_error(GOT_ERR_BAD_OBJ_DATA);
776 space = memchr(buf, ' ', *elen);
777 if (space == NULL || space <= buf)
778 return got_error(GOT_ERR_BAD_OBJ_DATA);
780 pte->mode = 0;
781 p = buf;
782 while (p < space) {
783 if (*p < '0' || *p > '7')
784 return got_error(GOT_ERR_BAD_OBJ_DATA);
785 pte->mode <<= 3;
786 pte->mode |= *p - '0';
787 p++;
790 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
791 return got_error(GOT_ERR_BAD_OBJ_DATA);
793 pte->name = space + 1;
794 pte->namelen = strlen(pte->name);
795 buf += *elen;
796 pte->id = buf;
797 *elen += SHA1_DIGEST_LENGTH;
798 return NULL;
801 static int
802 pte_cmp(const void *pa, const void *pb)
804 const struct got_parsed_tree_entry *a = pa, *b = pb;
806 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
809 const struct got_error *
810 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
811 size_t *nentries_alloc, uint8_t *buf, size_t len)
813 const struct got_error *err = NULL;
814 size_t remain = len;
815 const size_t nalloc = 16;
816 struct got_parsed_tree_entry *pte;
817 int i;
819 *nentries = 0;
820 if (remain == 0)
821 return NULL; /* tree is empty */
823 while (remain > 0) {
824 size_t elen;
826 if (*nentries >= *nentries_alloc) {
827 pte = recallocarray(*entries, *nentries_alloc,
828 *nentries_alloc + nalloc, sizeof(**entries));
829 if (pte == NULL) {
830 err = got_error_from_errno("recallocarray");
831 goto done;
833 *entries = pte;
834 *nentries_alloc += nalloc;
837 pte = &(*entries)[*nentries];
838 err = got_object_parse_tree_entry(pte, &elen, buf, remain);
839 if (err)
840 goto done;
841 buf += elen;
842 remain -= elen;
843 (*nentries)++;
846 if (remain != 0) {
847 err = got_error(GOT_ERR_BAD_OBJ_DATA);
848 goto done;
851 if (*nentries > 1) {
852 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
854 for (i = 0; i < *nentries - 1; i++) {
855 struct got_parsed_tree_entry *prev = &(*entries)[i];
856 pte = &(*entries)[i + 1];
857 if (got_path_cmp(prev->name, pte->name,
858 prev->namelen, pte->namelen) == 0) {
859 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
860 break;
864 done:
865 if (err)
866 *nentries = 0;
867 return err;
870 const struct got_error *
871 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
872 size_t *nentries_alloc, uint8_t **p, int fd,
873 struct got_object_id *expected_id)
875 const struct got_error *err = NULL;
876 struct got_object *obj = NULL;
877 size_t len;
878 struct got_inflate_checksum csum;
879 struct got_hash ctx;
880 struct got_object_id id;
882 got_hash_init(&ctx, GOT_HASH_SHA1);
883 memset(&csum, 0, sizeof(csum));
884 csum.output_ctx = &ctx;
886 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
887 if (err)
888 return err;
890 got_hash_final_object_id(&ctx, &id);
891 if (got_object_id_cmp(expected_id, &id) != 0) {
892 err = got_error_checksum(expected_id);
893 goto done;
896 err = got_object_parse_header(&obj, *p, len);
897 if (err)
898 goto done;
900 if (len < obj->hdrlen + obj->size) {
901 err = got_error(GOT_ERR_BAD_OBJ_DATA);
902 goto done;
905 /* Skip object header. */
906 len -= obj->hdrlen;
907 err = got_object_parse_tree(entries, nentries, nentries_alloc,
908 *p + obj->hdrlen, len);
909 done:
910 if (obj)
911 got_object_close(obj);
912 return err;
915 void
916 got_object_tag_close(struct got_tag_object *tag)
918 if (tag->refcnt > 0) {
919 tag->refcnt--;
920 if (tag->refcnt > 0)
921 return;
924 free(tag->tag);
925 free(tag->tagger);
926 free(tag->tagmsg);
927 free(tag);
930 const struct got_error *
931 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
933 const struct got_error *err = NULL;
934 enum got_hash_algorithm algo = GOT_HASH_SHA1;
935 size_t remain = len;
936 char *s = buf;
937 size_t label_len;
939 if (remain == 0)
940 return got_error(GOT_ERR_BAD_OBJ_DATA);
942 *tag = calloc(1, sizeof(**tag));
943 if (*tag == NULL)
944 return got_error_from_errno("calloc");
946 label_len = strlen(GOT_TAG_LABEL_OBJECT);
947 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
948 remain -= label_len;
949 if (remain < SHA1_DIGEST_STRING_LENGTH) {
950 err = got_error(GOT_ERR_BAD_OBJ_DATA);
951 goto done;
953 s += label_len;
954 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
955 err = got_error(GOT_ERR_BAD_OBJ_DATA);
956 goto done;
958 remain -= SHA1_DIGEST_STRING_LENGTH;
959 s += SHA1_DIGEST_STRING_LENGTH;
960 } else {
961 err = got_error(GOT_ERR_BAD_OBJ_DATA);
962 goto done;
965 if (remain <= 0) {
966 err = got_error(GOT_ERR_BAD_OBJ_DATA);
967 goto done;
970 label_len = strlen(GOT_TAG_LABEL_TYPE);
971 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
972 remain -= label_len;
973 if (remain <= 0) {
974 err = got_error(GOT_ERR_BAD_OBJ_DATA);
975 goto done;
977 s += label_len;
978 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
979 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
980 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
981 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
982 s += label_len;
983 remain -= label_len;
984 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
985 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
986 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
987 label_len = strlen(GOT_OBJ_LABEL_TREE);
988 s += label_len;
989 remain -= label_len;
990 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
991 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
992 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
993 label_len = strlen(GOT_OBJ_LABEL_BLOB);
994 s += label_len;
995 remain -= label_len;
996 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
997 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
998 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
999 label_len = strlen(GOT_OBJ_LABEL_TAG);
1000 s += label_len;
1001 remain -= label_len;
1002 } else {
1003 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1004 goto done;
1007 if (remain <= 0 || *s != '\n') {
1008 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1009 goto done;
1011 s++;
1012 remain--;
1013 if (remain <= 0) {
1014 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1015 goto done;
1017 } else {
1018 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1019 goto done;
1022 label_len = strlen(GOT_TAG_LABEL_TAG);
1023 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1024 char *p;
1025 size_t slen;
1026 remain -= label_len;
1027 if (remain <= 0) {
1028 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1029 goto done;
1031 s += label_len;
1032 p = memchr(s, '\n', remain);
1033 if (p == NULL) {
1034 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1035 goto done;
1037 *p = '\0';
1038 slen = strlen(s);
1039 (*tag)->tag = strndup(s, slen);
1040 if ((*tag)->tag == NULL) {
1041 err = got_error_from_errno("strndup");
1042 goto done;
1044 s += slen + 1;
1045 remain -= slen + 1;
1046 if (remain <= 0) {
1047 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1048 goto done;
1050 } else {
1051 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1052 goto done;
1055 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1056 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1057 char *p;
1058 size_t slen;
1060 remain -= label_len;
1061 if (remain <= 0) {
1062 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1063 goto done;
1065 s += label_len;
1066 p = memchr(s, '\n', remain);
1067 if (p == NULL) {
1068 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1069 goto done;
1071 *p = '\0';
1072 slen = strlen(s);
1073 err = parse_commit_time(&(*tag)->tagger_time,
1074 &(*tag)->tagger_gmtoff, s);
1075 if (err)
1076 goto done;
1077 (*tag)->tagger = strdup(s);
1078 if ((*tag)->tagger == NULL) {
1079 err = got_error_from_errno("strdup");
1080 goto done;
1082 s += slen + 1;
1083 remain -= slen + 1;
1084 if (remain < 0) {
1085 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1086 goto done;
1088 } else {
1089 /* Some old tags in the Linux git repo have no tagger. */
1090 (*tag)->tagger = strdup("");
1091 if ((*tag)->tagger == NULL) {
1092 err = got_error_from_errno("strdup");
1093 goto done;
1097 (*tag)->tagmsg = strndup(s, remain);
1098 if ((*tag)->tagmsg == NULL) {
1099 err = got_error_from_errno("strndup");
1100 goto done;
1102 done:
1103 if (err) {
1104 got_object_tag_close(*tag);
1105 *tag = NULL;
1107 return err;
1110 const struct got_error *
1111 got_object_read_tag(struct got_tag_object **tag, int fd,
1112 struct got_object_id *expected_id, size_t expected_size)
1114 const struct got_error *err = NULL;
1115 struct got_object *obj = NULL;
1116 size_t len;
1117 uint8_t *p;
1118 struct got_inflate_checksum csum;
1119 struct got_hash ctx;
1120 struct got_object_id id;
1122 got_hash_init(&ctx, GOT_HASH_SHA1);
1123 memset(&csum, 0, sizeof(csum));
1124 csum.output_ctx = &ctx;
1126 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1127 expected_size, fd);
1128 if (err)
1129 return err;
1131 got_hash_final_object_id(&ctx, &id);
1132 if (got_object_id_cmp(expected_id, &id) != 0) {
1133 err = got_error_checksum(expected_id);
1134 goto done;
1137 err = got_object_parse_header(&obj, p, len);
1138 if (err)
1139 goto done;
1141 if (len < obj->hdrlen + obj->size) {
1142 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1143 goto done;
1146 /* Skip object header. */
1147 len -= obj->hdrlen;
1148 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1149 done:
1150 free(p);
1151 if (obj)
1152 got_object_close(obj);
1153 return err;