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 close(fd);
288 goto done;
290 err = got_inflate_to_fd(&len, f, &csum, outfd);
292 if (err)
293 goto done;
295 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
296 err = got_error(GOT_ERR_BAD_OBJ_HDR);
297 goto done;
300 got_hash_final_object_id(&ctx, &id);
301 if (got_object_id_cmp(expected_id, &id) != 0) {
302 err = got_error_checksum(expected_id);
303 goto done;
306 *size = obj->size;
307 *hdrlen = obj->hdrlen;
308 done:
309 got_object_close(obj);
310 if (f && fclose(f) == EOF && err == NULL)
311 err = got_error_from_errno("fclose");
312 return err;
315 struct got_commit_object *
316 got_object_commit_alloc_partial(void)
318 struct got_commit_object *commit;
320 commit = calloc(1, sizeof(*commit));
321 if (commit == NULL)
322 return NULL;
323 commit->tree_id = malloc(sizeof(*commit->tree_id));
324 if (commit->tree_id == NULL) {
325 free(commit);
326 return NULL;
329 STAILQ_INIT(&commit->parent_ids);
331 return commit;
334 const struct got_error *
335 got_object_commit_add_parent(struct got_commit_object *commit,
336 const char *id_str)
338 const struct got_error *err = NULL;
339 struct got_object_qid *qid;
341 err = got_object_qid_alloc_partial(&qid);
342 if (err)
343 return err;
345 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 got_object_qid_free(qid);
348 return err;
351 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
352 commit->nparents++;
354 return NULL;
357 static const struct got_error *
358 parse_gmtoff(time_t *gmtoff, const char *tzstr)
360 int sign = 1;
361 const char *p = tzstr;
362 time_t h, m;
364 *gmtoff = 0;
366 if (*p == '-')
367 sign = -1;
368 else if (*p != '+')
369 return got_error(GOT_ERR_BAD_OBJ_DATA);
370 p++;
371 if (!isdigit((unsigned char)*p) &&
372 !isdigit((unsigned char)*(p + 1)))
373 return got_error(GOT_ERR_BAD_OBJ_DATA);
374 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
376 p += 2;
377 if (!isdigit((unsigned char)*p) &&
378 !isdigit((unsigned char)*(p + 1)))
379 return got_error(GOT_ERR_BAD_OBJ_DATA);
380 m = ((*p - '0') * 10) + (*(p + 1) - '0');
382 *gmtoff = (h * 60 * 60 + m * 60) * sign;
383 return NULL;
386 static const struct got_error *
387 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
389 const struct got_error *err = NULL;
390 const char *errstr;
391 char *space, *tzstr;
393 /* Parse and strip off trailing timezone indicator string. */
394 space = strrchr(committer, ' ');
395 if (space == NULL)
396 return got_error(GOT_ERR_BAD_OBJ_DATA);
397 tzstr = strdup(space + 1);
398 if (tzstr == NULL)
399 return got_error_from_errno("strdup");
400 err = parse_gmtoff(gmtoff, tzstr);
401 free(tzstr);
402 if (err) {
403 if (err->code != GOT_ERR_BAD_OBJ_DATA)
404 return err;
405 /* Old versions of Git omitted the timestamp. */
406 *time = 0;
407 *gmtoff = 0;
408 return NULL;
410 *space = '\0';
412 /* Timestamp is separated from committer name + email by space. */
413 space = strrchr(committer, ' ');
414 if (space == NULL)
415 return got_error(GOT_ERR_BAD_OBJ_DATA);
417 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
418 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
419 if (errstr)
420 return got_error(GOT_ERR_BAD_OBJ_DATA);
422 /* Strip off parsed time information, leaving just author and email. */
423 *space = '\0';
425 return NULL;
428 void
429 got_object_commit_close(struct got_commit_object *commit)
431 if (commit->refcnt > 0) {
432 commit->refcnt--;
433 if (commit->refcnt > 0)
434 return;
437 got_object_id_queue_free(&commit->parent_ids);
438 free(commit->tree_id);
439 free(commit->author);
440 free(commit->committer);
441 free(commit->logmsg);
442 free(commit);
445 struct got_object_id *
446 got_object_commit_get_tree_id(struct got_commit_object *commit)
448 return commit->tree_id;
451 int
452 got_object_commit_get_nparents(struct got_commit_object *commit)
454 return commit->nparents;
457 const struct got_object_id_queue *
458 got_object_commit_get_parent_ids(struct got_commit_object *commit)
460 return &commit->parent_ids;
463 const char *
464 got_object_commit_get_author(struct got_commit_object *commit)
466 return commit->author;
469 time_t
470 got_object_commit_get_author_time(struct got_commit_object *commit)
472 return commit->author_time;
475 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
477 return commit->author_gmtoff;
480 const char *
481 got_object_commit_get_committer(struct got_commit_object *commit)
483 return commit->committer;
486 time_t
487 got_object_commit_get_committer_time(struct got_commit_object *commit)
489 return commit->committer_time;
492 time_t
493 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
495 return commit->committer_gmtoff;
498 const struct got_error *
499 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
501 const struct got_error *err = NULL;
502 const char *src;
503 char *dst;
504 size_t len;
506 len = strlen(commit->logmsg);
507 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
508 if (*logmsg == NULL)
509 return got_error_from_errno("malloc");
511 /*
512 * Strip out unusual headers. Headers are separated from the commit
513 * message body by a single empty line.
514 */
515 src = commit->logmsg;
516 dst = *logmsg;
517 while (*src != '\0' && *src != '\n') {
518 int copy_header = 1, eol = 0;
519 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
520 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
521 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
522 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
523 strncmp(src, GOT_COMMIT_LABEL_PARENT,
524 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
525 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
526 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
527 copy_header = 0;
529 while (*src != '\0' && !eol) {
530 if (copy_header) {
531 *dst = *src;
532 dst++;
534 if (*src == '\n')
535 eol = 1;
536 src++;
539 *dst = '\0';
541 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
542 err = got_error(GOT_ERR_NO_SPACE);
543 goto done;
546 /* Trim redundant trailing whitespace. */
547 len = strlen(*logmsg);
548 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
549 isspace((unsigned char)(*logmsg)[len - 1])) {
550 (*logmsg)[len - 1] = '\0';
551 len--;
554 /* Append a trailing newline if missing. */
555 if (len > 0 && (*logmsg)[len - 1] != '\n') {
556 (*logmsg)[len] = '\n';
557 (*logmsg)[len + 1] = '\0';
559 done:
560 if (err) {
561 free(*logmsg);
562 *logmsg = NULL;
564 return err;
567 const char *
568 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
570 return commit->logmsg;
573 const struct got_error *
574 got_object_parse_commit(struct got_commit_object **commit, char *buf,
575 size_t len)
577 const struct got_error *err = NULL;
578 enum got_hash_algorithm algo = GOT_HASH_SHA1;
579 char *s = buf;
580 size_t label_len;
581 ssize_t remain = (ssize_t)len;
583 if (remain == 0)
584 return got_error(GOT_ERR_BAD_OBJ_DATA);
586 *commit = got_object_commit_alloc_partial();
587 if (*commit == NULL)
588 return got_error_from_errno("got_object_commit_alloc_partial");
590 label_len = strlen(GOT_COMMIT_LABEL_TREE);
591 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
592 remain -= label_len;
593 if (remain < SHA1_DIGEST_STRING_LENGTH) {
594 err = got_error(GOT_ERR_BAD_OBJ_DATA);
595 goto done;
597 s += label_len;
598 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
599 err = got_error(GOT_ERR_BAD_OBJ_DATA);
600 goto done;
602 remain -= SHA1_DIGEST_STRING_LENGTH;
603 s += SHA1_DIGEST_STRING_LENGTH;
604 } else {
605 err = got_error(GOT_ERR_BAD_OBJ_DATA);
606 goto done;
609 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
610 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
611 remain -= label_len;
612 if (remain < SHA1_DIGEST_STRING_LENGTH) {
613 err = got_error(GOT_ERR_BAD_OBJ_DATA);
614 goto done;
616 s += label_len;
617 err = got_object_commit_add_parent(*commit, s);
618 if (err)
619 goto done;
621 remain -= SHA1_DIGEST_STRING_LENGTH;
622 s += SHA1_DIGEST_STRING_LENGTH;
625 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
626 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
627 char *p;
628 size_t slen;
630 remain -= label_len;
631 if (remain <= 0) {
632 err = got_error(GOT_ERR_BAD_OBJ_DATA);
633 goto done;
635 s += label_len;
636 p = memchr(s, '\n', remain);
637 if (p == NULL) {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
639 goto done;
641 *p = '\0';
642 slen = strlen(s);
643 err = parse_commit_time(&(*commit)->author_time,
644 &(*commit)->author_gmtoff, s);
645 if (err)
646 goto done;
647 (*commit)->author = strdup(s);
648 if ((*commit)->author == NULL) {
649 err = got_error_from_errno("strdup");
650 goto done;
652 s += slen + 1;
653 remain -= slen + 1;
656 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
657 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
658 char *p;
659 size_t slen;
661 remain -= label_len;
662 if (remain <= 0) {
663 err = got_error(GOT_ERR_BAD_OBJ_DATA);
664 goto done;
666 s += label_len;
667 p = memchr(s, '\n', remain);
668 if (p == NULL) {
669 err = got_error(GOT_ERR_BAD_OBJ_DATA);
670 goto done;
672 *p = '\0';
673 slen = strlen(s);
674 err = parse_commit_time(&(*commit)->committer_time,
675 &(*commit)->committer_gmtoff, s);
676 if (err)
677 goto done;
678 (*commit)->committer = strdup(s);
679 if ((*commit)->committer == NULL) {
680 err = got_error_from_errno("strdup");
681 goto done;
683 s += slen + 1;
684 remain -= slen + 1;
687 (*commit)->logmsg = strndup(s, remain);
688 if ((*commit)->logmsg == NULL) {
689 err = got_error_from_errno("strndup");
690 goto done;
692 done:
693 if (err) {
694 got_object_commit_close(*commit);
695 *commit = NULL;
697 return err;
700 const struct got_error *
701 got_object_read_commit(struct got_commit_object **commit, int fd,
702 struct got_object_id *expected_id, size_t expected_size)
704 struct got_object *obj = NULL;
705 const struct got_error *err = NULL;
706 size_t len;
707 uint8_t *p;
708 struct got_inflate_checksum csum;
709 struct got_hash ctx;
710 struct got_object_id id;
712 got_hash_init(&ctx, GOT_HASH_SHA1);
713 memset(&csum, 0, sizeof(csum));
714 csum.output_ctx = &ctx;
716 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
717 if (err)
718 return err;
720 got_hash_final_object_id(&ctx, &id);
721 if (got_object_id_cmp(expected_id, &id) != 0) {
722 err = got_error_checksum(expected_id);
723 goto done;
726 err = got_object_parse_header(&obj, p, len);
727 if (err)
728 goto done;
730 if (len < obj->hdrlen + obj->size) {
731 err = got_error(GOT_ERR_BAD_OBJ_DATA);
732 goto done;
735 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
736 err = got_error(GOT_ERR_OBJ_TYPE);
737 goto done;
740 /* Skip object header. */
741 len -= obj->hdrlen;
742 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
743 done:
744 free(p);
745 if (obj)
746 got_object_close(obj);
747 return err;
750 void
751 got_object_tree_close(struct got_tree_object *tree)
753 if (tree->refcnt > 0) {
754 tree->refcnt--;
755 if (tree->refcnt > 0)
756 return;
759 free(tree->entries);
760 free(tree);
763 const struct got_error *
764 got_object_parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen,
765 char *buf, size_t maxlen)
767 char *p, *space;
769 *elen = 0;
771 *elen = strnlen(buf, maxlen) + 1;
772 if (*elen > maxlen)
773 return got_error(GOT_ERR_BAD_OBJ_DATA);
775 space = memchr(buf, ' ', *elen);
776 if (space == NULL || space <= buf)
777 return got_error(GOT_ERR_BAD_OBJ_DATA);
779 pte->mode = 0;
780 p = buf;
781 while (p < space) {
782 if (*p < '0' || *p > '7')
783 return got_error(GOT_ERR_BAD_OBJ_DATA);
784 pte->mode <<= 3;
785 pte->mode |= *p - '0';
786 p++;
789 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
790 return got_error(GOT_ERR_BAD_OBJ_DATA);
792 pte->name = space + 1;
793 pte->namelen = strlen(pte->name);
794 buf += *elen;
795 pte->id = buf;
796 *elen += SHA1_DIGEST_LENGTH;
797 return NULL;
800 static int
801 pte_cmp(const void *pa, const void *pb)
803 const struct got_parsed_tree_entry *a = pa, *b = pb;
805 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
808 const struct got_error *
809 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
810 size_t *nentries_alloc, uint8_t *buf, size_t len)
812 const struct got_error *err = NULL;
813 size_t remain = len;
814 const size_t nalloc = 16;
815 struct got_parsed_tree_entry *pte;
816 int i;
818 *nentries = 0;
819 if (remain == 0)
820 return NULL; /* tree is empty */
822 while (remain > 0) {
823 size_t elen;
825 if (*nentries >= *nentries_alloc) {
826 pte = recallocarray(*entries, *nentries_alloc,
827 *nentries_alloc + nalloc, sizeof(**entries));
828 if (pte == NULL) {
829 err = got_error_from_errno("recallocarray");
830 goto done;
832 *entries = pte;
833 *nentries_alloc += nalloc;
836 pte = &(*entries)[*nentries];
837 err = got_object_parse_tree_entry(pte, &elen, buf, remain);
838 if (err)
839 goto done;
840 buf += elen;
841 remain -= elen;
842 (*nentries)++;
845 if (remain != 0) {
846 err = got_error(GOT_ERR_BAD_OBJ_DATA);
847 goto done;
850 if (*nentries > 1) {
851 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
853 for (i = 0; i < *nentries - 1; i++) {
854 struct got_parsed_tree_entry *prev = &(*entries)[i];
855 pte = &(*entries)[i + 1];
856 if (got_path_cmp(prev->name, pte->name,
857 prev->namelen, pte->namelen) == 0) {
858 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
859 break;
863 done:
864 if (err)
865 *nentries = 0;
866 return err;
869 const struct got_error *
870 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
871 size_t *nentries_alloc, uint8_t **p, int fd,
872 struct got_object_id *expected_id)
874 const struct got_error *err = NULL;
875 struct got_object *obj = NULL;
876 size_t len;
877 struct got_inflate_checksum csum;
878 struct got_hash ctx;
879 struct got_object_id id;
881 got_hash_init(&ctx, GOT_HASH_SHA1);
882 memset(&csum, 0, sizeof(csum));
883 csum.output_ctx = &ctx;
885 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
886 if (err)
887 return err;
889 got_hash_final_object_id(&ctx, &id);
890 if (got_object_id_cmp(expected_id, &id) != 0) {
891 err = got_error_checksum(expected_id);
892 goto done;
895 err = got_object_parse_header(&obj, *p, len);
896 if (err)
897 goto done;
899 if (len < obj->hdrlen + obj->size) {
900 err = got_error(GOT_ERR_BAD_OBJ_DATA);
901 goto done;
904 /* Skip object header. */
905 len -= obj->hdrlen;
906 err = got_object_parse_tree(entries, nentries, nentries_alloc,
907 *p + obj->hdrlen, len);
908 done:
909 if (obj)
910 got_object_close(obj);
911 return err;
914 void
915 got_object_tag_close(struct got_tag_object *tag)
917 if (tag->refcnt > 0) {
918 tag->refcnt--;
919 if (tag->refcnt > 0)
920 return;
923 free(tag->tag);
924 free(tag->tagger);
925 free(tag->tagmsg);
926 free(tag);
929 const struct got_error *
930 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
932 const struct got_error *err = NULL;
933 enum got_hash_algorithm algo = GOT_HASH_SHA1;
934 size_t remain = len;
935 char *s = buf;
936 size_t label_len;
938 if (remain == 0)
939 return got_error(GOT_ERR_BAD_OBJ_DATA);
941 *tag = calloc(1, sizeof(**tag));
942 if (*tag == NULL)
943 return got_error_from_errno("calloc");
945 label_len = strlen(GOT_TAG_LABEL_OBJECT);
946 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
947 remain -= label_len;
948 if (remain < SHA1_DIGEST_STRING_LENGTH) {
949 err = got_error(GOT_ERR_BAD_OBJ_DATA);
950 goto done;
952 s += label_len;
953 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
954 err = got_error(GOT_ERR_BAD_OBJ_DATA);
955 goto done;
957 remain -= SHA1_DIGEST_STRING_LENGTH;
958 s += SHA1_DIGEST_STRING_LENGTH;
959 } else {
960 err = got_error(GOT_ERR_BAD_OBJ_DATA);
961 goto done;
964 if (remain <= 0) {
965 err = got_error(GOT_ERR_BAD_OBJ_DATA);
966 goto done;
969 label_len = strlen(GOT_TAG_LABEL_TYPE);
970 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
971 remain -= label_len;
972 if (remain <= 0) {
973 err = got_error(GOT_ERR_BAD_OBJ_DATA);
974 goto done;
976 s += label_len;
977 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
978 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
979 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
980 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
981 s += label_len;
982 remain -= label_len;
983 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
984 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
985 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
986 label_len = strlen(GOT_OBJ_LABEL_TREE);
987 s += label_len;
988 remain -= label_len;
989 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
990 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
991 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
992 label_len = strlen(GOT_OBJ_LABEL_BLOB);
993 s += label_len;
994 remain -= label_len;
995 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
996 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
997 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
998 label_len = strlen(GOT_OBJ_LABEL_TAG);
999 s += label_len;
1000 remain -= label_len;
1001 } else {
1002 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1003 goto done;
1006 if (remain <= 0 || *s != '\n') {
1007 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1008 goto done;
1010 s++;
1011 remain--;
1012 if (remain <= 0) {
1013 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1014 goto done;
1016 } else {
1017 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1018 goto done;
1021 label_len = strlen(GOT_TAG_LABEL_TAG);
1022 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1023 char *p;
1024 size_t slen;
1025 remain -= label_len;
1026 if (remain <= 0) {
1027 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1028 goto done;
1030 s += label_len;
1031 p = memchr(s, '\n', remain);
1032 if (p == NULL) {
1033 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1034 goto done;
1036 *p = '\0';
1037 slen = strlen(s);
1038 (*tag)->tag = strndup(s, slen);
1039 if ((*tag)->tag == NULL) {
1040 err = got_error_from_errno("strndup");
1041 goto done;
1043 s += slen + 1;
1044 remain -= slen + 1;
1045 if (remain <= 0) {
1046 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1047 goto done;
1049 } else {
1050 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 goto done;
1054 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1055 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1056 char *p;
1057 size_t slen;
1059 remain -= label_len;
1060 if (remain <= 0) {
1061 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 goto done;
1064 s += label_len;
1065 p = memchr(s, '\n', remain);
1066 if (p == NULL) {
1067 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1068 goto done;
1070 *p = '\0';
1071 slen = strlen(s);
1072 err = parse_commit_time(&(*tag)->tagger_time,
1073 &(*tag)->tagger_gmtoff, s);
1074 if (err)
1075 goto done;
1076 (*tag)->tagger = strdup(s);
1077 if ((*tag)->tagger == NULL) {
1078 err = got_error_from_errno("strdup");
1079 goto done;
1081 s += slen + 1;
1082 remain -= slen + 1;
1083 if (remain < 0) {
1084 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1085 goto done;
1087 } else {
1088 /* Some old tags in the Linux git repo have no tagger. */
1089 (*tag)->tagger = strdup("");
1090 if ((*tag)->tagger == NULL) {
1091 err = got_error_from_errno("strdup");
1092 goto done;
1096 (*tag)->tagmsg = strndup(s, remain);
1097 if ((*tag)->tagmsg == NULL) {
1098 err = got_error_from_errno("strndup");
1099 goto done;
1101 done:
1102 if (err) {
1103 got_object_tag_close(*tag);
1104 *tag = NULL;
1106 return err;
1109 const struct got_error *
1110 got_object_read_tag(struct got_tag_object **tag, int fd,
1111 struct got_object_id *expected_id, size_t expected_size)
1113 const struct got_error *err = NULL;
1114 struct got_object *obj = NULL;
1115 size_t len;
1116 uint8_t *p;
1117 struct got_inflate_checksum csum;
1118 struct got_hash ctx;
1119 struct got_object_id id;
1121 got_hash_init(&ctx, GOT_HASH_SHA1);
1122 memset(&csum, 0, sizeof(csum));
1123 csum.output_ctx = &ctx;
1125 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1126 expected_size, fd);
1127 if (err)
1128 return err;
1130 got_hash_final_object_id(&ctx, &id);
1131 if (got_object_id_cmp(expected_id, &id) != 0) {
1132 err = got_error_checksum(expected_id);
1133 goto done;
1136 err = got_object_parse_header(&obj, p, len);
1137 if (err)
1138 goto done;
1140 if (len < obj->hdrlen + obj->size) {
1141 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1142 goto done;
1145 /* Skip object header. */
1146 len -= obj->hdrlen;
1147 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1148 done:
1149 free(p);
1150 if (obj)
1151 got_object_close(obj);
1152 return err;