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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/queue.h>
22 #include <sys/uio.h>
23 #include <sys/socket.h>
24 #include <sys/wait.h>
25 #include <sys/mman.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <zlib.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <time.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
42 #include "got_path.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_object_qid.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_repository.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #endif
58 const struct got_error *
59 got_object_type_label(const char **label, int obj_type)
60 {
61 const struct got_error *err = NULL;
63 switch (obj_type) {
64 case GOT_OBJ_TYPE_BLOB:
65 *label = GOT_OBJ_LABEL_BLOB;
66 break;
67 case GOT_OBJ_TYPE_TREE:
68 *label = GOT_OBJ_LABEL_TREE;
69 break;
70 case GOT_OBJ_TYPE_COMMIT:
71 *label = GOT_OBJ_LABEL_COMMIT;
72 break;
73 case GOT_OBJ_TYPE_TAG:
74 *label = GOT_OBJ_LABEL_TAG;
75 break;
76 default:
77 *label = NULL;
78 err = got_error(GOT_ERR_OBJ_TYPE);
79 break;
80 }
82 return err;
83 }
85 void
86 got_object_close(struct got_object *obj)
87 {
88 if (obj->refcnt > 0) {
89 obj->refcnt--;
90 if (obj->refcnt > 0)
91 return;
92 }
94 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
95 struct got_delta *delta;
96 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
97 delta = STAILQ_FIRST(&obj->deltas.entries);
98 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
99 free(delta);
102 free(obj);
105 const struct got_error *
106 got_object_raw_close(struct got_raw_object *obj)
108 const struct got_error *err = NULL;
110 if (obj->refcnt > 0) {
111 obj->refcnt--;
112 if (obj->refcnt > 0)
113 return NULL;
116 if (obj->close_cb)
117 obj->close_cb(obj);
119 if (obj->f == NULL) {
120 if (obj->fd != -1) {
121 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
122 err = got_error_from_errno("munmap");
123 if (close(obj->fd) == -1 && err == NULL)
124 err = got_error_from_errno("close");
125 } else
126 free(obj->data);
127 } else {
128 if (fclose(obj->f) == EOF && err == NULL)
129 err = got_error_from_errno("fclose");
131 free(obj);
132 return err;
135 const struct got_error *
136 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
138 const char *obj_labels[] = {
139 GOT_OBJ_LABEL_COMMIT,
140 GOT_OBJ_LABEL_TREE,
141 GOT_OBJ_LABEL_BLOB,
142 GOT_OBJ_LABEL_TAG,
143 };
144 const int obj_types[] = {
145 GOT_OBJ_TYPE_COMMIT,
146 GOT_OBJ_TYPE_TREE,
147 GOT_OBJ_TYPE_BLOB,
148 GOT_OBJ_TYPE_TAG,
149 };
150 int type = 0;
151 size_t size = 0;
152 size_t i;
153 char *end;
155 *obj = NULL;
157 end = memchr(buf, '\0', len);
158 if (end == NULL)
159 return got_error(GOT_ERR_BAD_OBJ_HDR);
161 for (i = 0; i < nitems(obj_labels); i++) {
162 const char *label = obj_labels[i];
163 size_t label_len = strlen(label);
164 const char *errstr;
166 if (len <= label_len || buf + label_len >= end ||
167 strncmp(buf, label, label_len) != 0)
168 continue;
170 type = obj_types[i];
171 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
172 if (errstr != NULL)
173 return got_error(GOT_ERR_BAD_OBJ_HDR);
174 break;
177 if (type == 0)
178 return got_error(GOT_ERR_BAD_OBJ_HDR);
180 *obj = calloc(1, sizeof(**obj));
181 if (*obj == NULL)
182 return got_error_from_errno("calloc");
183 (*obj)->type = type;
184 (*obj)->hdrlen = end - buf + 1;
185 (*obj)->size = size;
186 return NULL;
189 const struct got_error *
190 got_object_read_header(struct got_object **obj, int fd)
192 const struct got_error *err;
193 struct got_inflate_buf zb;
194 uint8_t *buf;
195 const size_t zbsize = 64;
196 size_t outlen, totlen;
197 int nbuf = 1;
199 *obj = NULL;
201 buf = malloc(zbsize);
202 if (buf == NULL)
203 return got_error_from_errno("malloc");
204 buf[0] = '\0';
206 err = got_inflate_init(&zb, buf, zbsize, NULL);
207 if (err)
208 return err;
210 totlen = 0;
211 do {
212 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
213 if (err)
214 goto done;
215 if (outlen == 0)
216 break;
217 totlen += outlen;
218 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
219 uint8_t *newbuf;
220 nbuf++;
221 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
222 if (newbuf == NULL) {
223 err = got_error_from_errno("recallocarray");
224 goto done;
226 buf = newbuf;
227 zb.outbuf = newbuf + totlen;
228 zb.outlen = (nbuf * zbsize) - totlen;
230 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
232 err = got_object_parse_header(obj, buf, totlen);
233 done:
234 free(buf);
235 got_inflate_end(&zb);
236 return err;
239 const struct got_error *
240 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
241 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
242 int infd)
244 const struct got_error *err = NULL;
245 struct got_object *obj;
246 struct got_inflate_checksum csum;
247 struct got_object_id id;
248 struct got_hash ctx;
249 size_t len, consumed;
250 FILE *f = NULL;
252 *outbuf = NULL;
253 *size = 0;
254 *hdrlen = 0;
256 got_hash_init(&ctx, GOT_HASH_SHA1);
257 memset(&csum, 0, sizeof(csum));
258 csum.output_ctx = &ctx;
260 if (lseek(infd, SEEK_SET, 0) == -1)
261 return got_error_from_errno("lseek");
263 err = got_object_read_header(&obj, infd);
264 if (err)
265 return err;
267 if (lseek(infd, SEEK_SET, 0) == -1)
268 return got_error_from_errno("lseek");
270 if (obj->size + obj->hdrlen <= max_in_mem_size) {
271 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
272 obj->size + obj->hdrlen, infd);
273 } else {
274 int fd;
275 /*
276 * XXX This uses an extra file descriptor for no good reason.
277 * We should have got_inflate_fd_to_fd().
278 */
279 fd = dup(infd);
280 if (fd == -1)
281 return got_error_from_errno("dup");
282 f = fdopen(fd, "r");
283 if (f == NULL) {
284 err = got_error_from_errno("fdopen");
285 close(fd);
286 goto done;
288 err = got_inflate_to_fd(&len, f, &csum, outfd);
290 if (err)
291 goto done;
293 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
294 err = got_error(GOT_ERR_BAD_OBJ_HDR);
295 goto done;
298 got_hash_final_object_id(&ctx, &id);
299 if (got_object_id_cmp(expected_id, &id) != 0) {
300 err = got_error_checksum(expected_id);
301 goto done;
304 *size = obj->size;
305 *hdrlen = obj->hdrlen;
306 done:
307 got_object_close(obj);
308 if (f && fclose(f) == EOF && err == NULL)
309 err = got_error_from_errno("fclose");
310 return err;
313 struct got_commit_object *
314 got_object_commit_alloc_partial(void)
316 struct got_commit_object *commit;
318 commit = calloc(1, sizeof(*commit));
319 if (commit == NULL)
320 return NULL;
321 commit->tree_id = malloc(sizeof(*commit->tree_id));
322 if (commit->tree_id == NULL) {
323 free(commit);
324 return NULL;
327 STAILQ_INIT(&commit->parent_ids);
329 return commit;
332 const struct got_error *
333 got_object_commit_add_parent(struct got_commit_object *commit,
334 const char *id_str)
336 const struct got_error *err = NULL;
337 struct got_object_qid *qid;
339 err = got_object_qid_alloc_partial(&qid);
340 if (err)
341 return err;
343 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
344 err = got_error(GOT_ERR_BAD_OBJ_DATA);
345 got_object_qid_free(qid);
346 return err;
349 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
350 commit->nparents++;
352 return NULL;
355 static const struct got_error *
356 parse_gmtoff(time_t *gmtoff, const char *tzstr)
358 int sign = 1;
359 const char *p = tzstr;
360 time_t h, m;
362 *gmtoff = 0;
364 if (*p == '-')
365 sign = -1;
366 else if (*p != '+')
367 return got_error(GOT_ERR_BAD_OBJ_DATA);
368 p++;
369 if (!isdigit((unsigned char)*p) &&
370 !isdigit((unsigned char)*(p + 1)))
371 return got_error(GOT_ERR_BAD_OBJ_DATA);
372 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
374 p += 2;
375 if (!isdigit((unsigned char)*p) &&
376 !isdigit((unsigned char)*(p + 1)))
377 return got_error(GOT_ERR_BAD_OBJ_DATA);
378 m = ((*p - '0') * 10) + (*(p + 1) - '0');
380 *gmtoff = (h * 60 * 60 + m * 60) * sign;
381 return NULL;
384 static const struct got_error *
385 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
387 const struct got_error *err = NULL;
388 const char *errstr;
389 char *space, *tzstr;
391 /* Parse and strip off trailing timezone indicator string. */
392 space = strrchr(committer, ' ');
393 if (space == NULL)
394 return got_error(GOT_ERR_BAD_OBJ_DATA);
395 tzstr = strdup(space + 1);
396 if (tzstr == NULL)
397 return got_error_from_errno("strdup");
398 err = parse_gmtoff(gmtoff, tzstr);
399 free(tzstr);
400 if (err) {
401 if (err->code != GOT_ERR_BAD_OBJ_DATA)
402 return err;
403 /* Old versions of Git omitted the timestamp. */
404 *time = 0;
405 *gmtoff = 0;
406 return NULL;
408 *space = '\0';
410 /* Timestamp is separated from committer name + email by space. */
411 space = strrchr(committer, ' ');
412 if (space == NULL)
413 return got_error(GOT_ERR_BAD_OBJ_DATA);
415 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
416 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
417 if (errstr)
418 return got_error(GOT_ERR_BAD_OBJ_DATA);
420 /* Strip off parsed time information, leaving just author and email. */
421 *space = '\0';
423 return NULL;
426 void
427 got_object_commit_close(struct got_commit_object *commit)
429 if (commit->refcnt > 0) {
430 commit->refcnt--;
431 if (commit->refcnt > 0)
432 return;
435 got_object_id_queue_free(&commit->parent_ids);
436 free(commit->tree_id);
437 free(commit->author);
438 free(commit->committer);
439 free(commit->logmsg);
440 free(commit);
443 struct got_object_id *
444 got_object_commit_get_tree_id(struct got_commit_object *commit)
446 return commit->tree_id;
449 int
450 got_object_commit_get_nparents(struct got_commit_object *commit)
452 return commit->nparents;
455 const struct got_object_id_queue *
456 got_object_commit_get_parent_ids(struct got_commit_object *commit)
458 return &commit->parent_ids;
461 const char *
462 got_object_commit_get_author(struct got_commit_object *commit)
464 return commit->author;
467 time_t
468 got_object_commit_get_author_time(struct got_commit_object *commit)
470 return commit->author_time;
473 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
475 return commit->author_gmtoff;
478 const char *
479 got_object_commit_get_committer(struct got_commit_object *commit)
481 return commit->committer;
484 time_t
485 got_object_commit_get_committer_time(struct got_commit_object *commit)
487 return commit->committer_time;
490 time_t
491 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
493 return commit->committer_gmtoff;
496 const struct got_error *
497 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
499 const struct got_error *err = NULL;
500 const char *src;
501 char *dst;
502 size_t len;
504 len = strlen(commit->logmsg);
505 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
506 if (*logmsg == NULL)
507 return got_error_from_errno("malloc");
509 /*
510 * Strip out unusual headers. Headers are separated from the commit
511 * message body by a single empty line.
512 */
513 src = commit->logmsg;
514 dst = *logmsg;
515 while (*src != '\0' && *src != '\n') {
516 int copy_header = 1, eol = 0;
517 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
518 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
519 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
520 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
521 strncmp(src, GOT_COMMIT_LABEL_PARENT,
522 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
523 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
524 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
525 copy_header = 0;
527 while (*src != '\0' && !eol) {
528 if (copy_header) {
529 *dst = *src;
530 dst++;
532 if (*src == '\n')
533 eol = 1;
534 src++;
537 *dst = '\0';
539 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
540 err = got_error(GOT_ERR_NO_SPACE);
541 goto done;
544 /* Trim redundant trailing whitespace. */
545 len = strlen(*logmsg);
546 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
547 isspace((unsigned char)(*logmsg)[len - 1])) {
548 (*logmsg)[len - 1] = '\0';
549 len--;
552 /* Append a trailing newline if missing. */
553 if (len > 0 && (*logmsg)[len - 1] != '\n') {
554 (*logmsg)[len] = '\n';
555 (*logmsg)[len + 1] = '\0';
557 done:
558 if (err) {
559 free(*logmsg);
560 *logmsg = NULL;
562 return err;
565 const char *
566 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
568 return commit->logmsg;
571 const struct got_error *
572 got_object_parse_commit(struct got_commit_object **commit, char *buf,
573 size_t len)
575 const struct got_error *err = NULL;
576 enum got_hash_algorithm algo = GOT_HASH_SHA1;
577 char *s = buf;
578 size_t label_len;
579 ssize_t remain = (ssize_t)len;
581 if (remain == 0)
582 return got_error(GOT_ERR_BAD_OBJ_DATA);
584 *commit = got_object_commit_alloc_partial();
585 if (*commit == NULL)
586 return got_error_from_errno("got_object_commit_alloc_partial");
588 label_len = strlen(GOT_COMMIT_LABEL_TREE);
589 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
590 remain -= label_len;
591 if (remain < SHA1_DIGEST_STRING_LENGTH) {
592 err = got_error(GOT_ERR_BAD_OBJ_DATA);
593 goto done;
595 s += label_len;
596 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
597 err = got_error(GOT_ERR_BAD_OBJ_DATA);
598 goto done;
600 remain -= SHA1_DIGEST_STRING_LENGTH;
601 s += SHA1_DIGEST_STRING_LENGTH;
602 } else {
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 goto done;
607 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
608 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
609 remain -= label_len;
610 if (remain < SHA1_DIGEST_STRING_LENGTH) {
611 err = got_error(GOT_ERR_BAD_OBJ_DATA);
612 goto done;
614 s += label_len;
615 err = got_object_commit_add_parent(*commit, s);
616 if (err)
617 goto done;
619 remain -= SHA1_DIGEST_STRING_LENGTH;
620 s += SHA1_DIGEST_STRING_LENGTH;
623 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
624 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
625 char *p;
626 size_t slen;
628 remain -= label_len;
629 if (remain <= 0) {
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
631 goto done;
633 s += label_len;
634 p = memchr(s, '\n', remain);
635 if (p == NULL) {
636 err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 goto done;
639 *p = '\0';
640 slen = strlen(s);
641 err = parse_commit_time(&(*commit)->author_time,
642 &(*commit)->author_gmtoff, s);
643 if (err)
644 goto done;
645 (*commit)->author = strdup(s);
646 if ((*commit)->author == NULL) {
647 err = got_error_from_errno("strdup");
648 goto done;
650 s += slen + 1;
651 remain -= slen + 1;
654 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
655 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
656 char *p;
657 size_t slen;
659 remain -= label_len;
660 if (remain <= 0) {
661 err = got_error(GOT_ERR_BAD_OBJ_DATA);
662 goto done;
664 s += label_len;
665 p = memchr(s, '\n', remain);
666 if (p == NULL) {
667 err = got_error(GOT_ERR_BAD_OBJ_DATA);
668 goto done;
670 *p = '\0';
671 slen = strlen(s);
672 err = parse_commit_time(&(*commit)->committer_time,
673 &(*commit)->committer_gmtoff, s);
674 if (err)
675 goto done;
676 (*commit)->committer = strdup(s);
677 if ((*commit)->committer == NULL) {
678 err = got_error_from_errno("strdup");
679 goto done;
681 s += slen + 1;
682 remain -= slen + 1;
685 (*commit)->logmsg = strndup(s, remain);
686 if ((*commit)->logmsg == NULL) {
687 err = got_error_from_errno("strndup");
688 goto done;
690 done:
691 if (err) {
692 got_object_commit_close(*commit);
693 *commit = NULL;
695 return err;
698 const struct got_error *
699 got_object_read_commit(struct got_commit_object **commit, int fd,
700 struct got_object_id *expected_id, size_t expected_size)
702 struct got_object *obj = NULL;
703 const struct got_error *err = NULL;
704 size_t len;
705 uint8_t *p;
706 struct got_inflate_checksum csum;
707 struct got_hash ctx;
708 struct got_object_id id;
710 got_hash_init(&ctx, GOT_HASH_SHA1);
711 memset(&csum, 0, sizeof(csum));
712 csum.output_ctx = &ctx;
714 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
715 if (err)
716 return err;
718 got_hash_final_object_id(&ctx, &id);
719 if (got_object_id_cmp(expected_id, &id) != 0) {
720 err = got_error_checksum(expected_id);
721 goto done;
724 err = got_object_parse_header(&obj, p, len);
725 if (err)
726 goto done;
728 if (len < obj->hdrlen + obj->size) {
729 err = got_error(GOT_ERR_BAD_OBJ_DATA);
730 goto done;
733 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
734 err = got_error(GOT_ERR_OBJ_TYPE);
735 goto done;
738 /* Skip object header. */
739 len -= obj->hdrlen;
740 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
741 done:
742 free(p);
743 if (obj)
744 got_object_close(obj);
745 return err;
748 void
749 got_object_tree_close(struct got_tree_object *tree)
751 if (tree->refcnt > 0) {
752 tree->refcnt--;
753 if (tree->refcnt > 0)
754 return;
757 free(tree->entries);
758 free(tree);
761 const struct got_error *
762 got_object_parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen,
763 char *buf, size_t maxlen)
765 char *p, *space;
767 *elen = 0;
769 *elen = strnlen(buf, maxlen) + 1;
770 if (*elen > maxlen)
771 return got_error(GOT_ERR_BAD_OBJ_DATA);
773 space = memchr(buf, ' ', *elen);
774 if (space == NULL || space <= buf)
775 return got_error(GOT_ERR_BAD_OBJ_DATA);
777 pte->mode = 0;
778 p = buf;
779 while (p < space) {
780 if (*p < '0' || *p > '7')
781 return got_error(GOT_ERR_BAD_OBJ_DATA);
782 pte->mode <<= 3;
783 pte->mode |= *p - '0';
784 p++;
787 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
788 return got_error(GOT_ERR_BAD_OBJ_DATA);
790 pte->name = space + 1;
791 pte->namelen = strlen(pte->name);
792 buf += *elen;
793 pte->id = buf;
794 *elen += SHA1_DIGEST_LENGTH;
795 return NULL;
798 static int
799 pte_cmp(const void *pa, const void *pb)
801 const struct got_parsed_tree_entry *a = pa, *b = pb;
803 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
806 const struct got_error *
807 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
808 size_t *nentries_alloc, uint8_t *buf, size_t len)
810 const struct got_error *err = NULL;
811 size_t remain = len;
812 const size_t nalloc = 16;
813 struct got_parsed_tree_entry *pte;
814 int i;
816 *nentries = 0;
817 if (remain == 0)
818 return NULL; /* tree is empty */
820 while (remain > 0) {
821 size_t elen;
823 if (*nentries >= *nentries_alloc) {
824 pte = recallocarray(*entries, *nentries_alloc,
825 *nentries_alloc + nalloc, sizeof(**entries));
826 if (pte == NULL) {
827 err = got_error_from_errno("recallocarray");
828 goto done;
830 *entries = pte;
831 *nentries_alloc += nalloc;
834 pte = &(*entries)[*nentries];
835 err = got_object_parse_tree_entry(pte, &elen, buf, remain);
836 if (err)
837 goto done;
838 buf += elen;
839 remain -= elen;
840 (*nentries)++;
843 if (remain != 0) {
844 err = got_error(GOT_ERR_BAD_OBJ_DATA);
845 goto done;
848 if (*nentries > 1) {
849 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
851 for (i = 0; i < *nentries - 1; i++) {
852 struct got_parsed_tree_entry *prev = &(*entries)[i];
853 pte = &(*entries)[i + 1];
854 if (got_path_cmp(prev->name, pte->name,
855 prev->namelen, pte->namelen) == 0) {
856 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
857 break;
861 done:
862 if (err)
863 *nentries = 0;
864 return err;
867 const struct got_error *
868 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
869 size_t *nentries_alloc, uint8_t **p, int fd,
870 struct got_object_id *expected_id)
872 const struct got_error *err = NULL;
873 struct got_object *obj = NULL;
874 size_t len;
875 struct got_inflate_checksum csum;
876 struct got_hash ctx;
877 struct got_object_id id;
879 got_hash_init(&ctx, GOT_HASH_SHA1);
880 memset(&csum, 0, sizeof(csum));
881 csum.output_ctx = &ctx;
883 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
884 if (err)
885 return err;
887 got_hash_final_object_id(&ctx, &id);
888 if (got_object_id_cmp(expected_id, &id) != 0) {
889 err = got_error_checksum(expected_id);
890 goto done;
893 err = got_object_parse_header(&obj, *p, len);
894 if (err)
895 goto done;
897 if (len < obj->hdrlen + obj->size) {
898 err = got_error(GOT_ERR_BAD_OBJ_DATA);
899 goto done;
902 /* Skip object header. */
903 len -= obj->hdrlen;
904 err = got_object_parse_tree(entries, nentries, nentries_alloc,
905 *p + obj->hdrlen, len);
906 done:
907 if (obj)
908 got_object_close(obj);
909 return err;
912 void
913 got_object_tag_close(struct got_tag_object *tag)
915 if (tag->refcnt > 0) {
916 tag->refcnt--;
917 if (tag->refcnt > 0)
918 return;
921 free(tag->tag);
922 free(tag->tagger);
923 free(tag->tagmsg);
924 free(tag);
927 const struct got_error *
928 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
930 const struct got_error *err = NULL;
931 enum got_hash_algorithm algo = GOT_HASH_SHA1;
932 size_t remain = len;
933 char *s = buf;
934 size_t label_len;
936 if (remain == 0)
937 return got_error(GOT_ERR_BAD_OBJ_DATA);
939 *tag = calloc(1, sizeof(**tag));
940 if (*tag == NULL)
941 return got_error_from_errno("calloc");
943 label_len = strlen(GOT_TAG_LABEL_OBJECT);
944 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
945 remain -= label_len;
946 if (remain < SHA1_DIGEST_STRING_LENGTH) {
947 err = got_error(GOT_ERR_BAD_OBJ_DATA);
948 goto done;
950 s += label_len;
951 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
952 err = got_error(GOT_ERR_BAD_OBJ_DATA);
953 goto done;
955 remain -= SHA1_DIGEST_STRING_LENGTH;
956 s += SHA1_DIGEST_STRING_LENGTH;
957 } else {
958 err = got_error(GOT_ERR_BAD_OBJ_DATA);
959 goto done;
962 if (remain <= 0) {
963 err = got_error(GOT_ERR_BAD_OBJ_DATA);
964 goto done;
967 label_len = strlen(GOT_TAG_LABEL_TYPE);
968 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
969 remain -= label_len;
970 if (remain <= 0) {
971 err = got_error(GOT_ERR_BAD_OBJ_DATA);
972 goto done;
974 s += label_len;
975 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
976 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
977 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
978 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
979 s += label_len;
980 remain -= label_len;
981 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
982 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
983 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
984 label_len = strlen(GOT_OBJ_LABEL_TREE);
985 s += label_len;
986 remain -= label_len;
987 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
988 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
989 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
990 label_len = strlen(GOT_OBJ_LABEL_BLOB);
991 s += label_len;
992 remain -= label_len;
993 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
994 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
995 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
996 label_len = strlen(GOT_OBJ_LABEL_TAG);
997 s += label_len;
998 remain -= label_len;
999 } else {
1000 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1001 goto done;
1004 if (remain <= 0 || *s != '\n') {
1005 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1006 goto done;
1008 s++;
1009 remain--;
1010 if (remain <= 0) {
1011 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1012 goto done;
1014 } else {
1015 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1016 goto done;
1019 label_len = strlen(GOT_TAG_LABEL_TAG);
1020 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1021 char *p;
1022 size_t slen;
1023 remain -= label_len;
1024 if (remain <= 0) {
1025 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1026 goto done;
1028 s += label_len;
1029 p = memchr(s, '\n', remain);
1030 if (p == NULL) {
1031 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1032 goto done;
1034 *p = '\0';
1035 slen = strlen(s);
1036 (*tag)->tag = strndup(s, slen);
1037 if ((*tag)->tag == NULL) {
1038 err = got_error_from_errno("strndup");
1039 goto done;
1041 s += slen + 1;
1042 remain -= slen + 1;
1043 if (remain <= 0) {
1044 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1045 goto done;
1047 } else {
1048 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1049 goto done;
1052 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1053 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1054 char *p;
1055 size_t slen;
1057 remain -= label_len;
1058 if (remain <= 0) {
1059 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1060 goto done;
1062 s += label_len;
1063 p = memchr(s, '\n', remain);
1064 if (p == NULL) {
1065 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1066 goto done;
1068 *p = '\0';
1069 slen = strlen(s);
1070 err = parse_commit_time(&(*tag)->tagger_time,
1071 &(*tag)->tagger_gmtoff, s);
1072 if (err)
1073 goto done;
1074 (*tag)->tagger = strdup(s);
1075 if ((*tag)->tagger == NULL) {
1076 err = got_error_from_errno("strdup");
1077 goto done;
1079 s += slen + 1;
1080 remain -= slen + 1;
1081 if (remain < 0) {
1082 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1083 goto done;
1085 } else {
1086 /* Some old tags in the Linux git repo have no tagger. */
1087 (*tag)->tagger = strdup("");
1088 if ((*tag)->tagger == NULL) {
1089 err = got_error_from_errno("strdup");
1090 goto done;
1094 (*tag)->tagmsg = strndup(s, remain);
1095 if ((*tag)->tagmsg == NULL) {
1096 err = got_error_from_errno("strndup");
1097 goto done;
1099 done:
1100 if (err) {
1101 got_object_tag_close(*tag);
1102 *tag = NULL;
1104 return err;
1107 const struct got_error *
1108 got_object_read_tag(struct got_tag_object **tag, int fd,
1109 struct got_object_id *expected_id, size_t expected_size)
1111 const struct got_error *err = NULL;
1112 struct got_object *obj = NULL;
1113 size_t len;
1114 uint8_t *p;
1115 struct got_inflate_checksum csum;
1116 struct got_hash ctx;
1117 struct got_object_id id;
1119 got_hash_init(&ctx, GOT_HASH_SHA1);
1120 memset(&csum, 0, sizeof(csum));
1121 csum.output_ctx = &ctx;
1123 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1124 expected_size, fd);
1125 if (err)
1126 return err;
1128 got_hash_final_object_id(&ctx, &id);
1129 if (got_object_id_cmp(expected_id, &id) != 0) {
1130 err = got_error_checksum(expected_id);
1131 goto done;
1134 err = got_object_parse_header(&obj, p, len);
1135 if (err)
1136 goto done;
1138 if (len < obj->hdrlen + obj->size) {
1139 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1140 goto done;
1143 /* Skip object header. */
1144 len -= obj->hdrlen;
1145 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1146 done:
1147 free(p);
1148 if (obj)
1149 got_object_close(obj);
1150 return err;