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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <sys/mman.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <zlib.h>
30 #include <ctype.h>
31 #include <limits.h>
32 #include <time.h>
33 #include <unistd.h>
35 #include "got_compat.h"
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
41 #include "got_path.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 struct got_object_id *
58 got_object_id_dup(struct got_object_id *id1)
59 {
60 struct got_object_id *id2;
62 id2 = malloc(sizeof(*id2));
63 if (id2 == NULL)
64 return NULL;
65 memcpy(id2, id1, sizeof(*id2));
66 return id2;
67 }
69 int
70 got_object_id_cmp(const struct got_object_id *id1,
71 const struct got_object_id *id2)
72 {
73 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
74 }
76 const struct got_error *
77 got_object_qid_alloc_partial(struct got_object_qid **qid)
78 {
79 *qid = malloc(sizeof(**qid));
80 if (*qid == NULL)
81 return got_error_from_errno("malloc");
83 (*qid)->data = NULL;
84 return NULL;
85 }
87 const struct got_error *
88 got_object_id_str(char **outbuf, struct got_object_id *id)
89 {
90 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
92 *outbuf = malloc(len);
93 if (*outbuf == NULL)
94 return got_error_from_errno("malloc");
96 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
97 free(*outbuf);
98 *outbuf = NULL;
99 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
102 return NULL;
105 void
106 got_object_close(struct got_object *obj)
108 if (obj->refcnt > 0) {
109 obj->refcnt--;
110 if (obj->refcnt > 0)
111 return;
114 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
115 struct got_delta *delta;
116 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
117 delta = STAILQ_FIRST(&obj->deltas.entries);
118 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
119 free(delta);
122 free(obj);
125 const struct got_error *
126 got_object_raw_close(struct got_raw_object *obj)
128 const struct got_error *err = NULL;
130 if (obj->refcnt > 0) {
131 obj->refcnt--;
132 if (obj->refcnt > 0)
133 return NULL;
136 if (obj->f == NULL) {
137 if (obj->fd != -1) {
138 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
139 err = got_error_from_errno("munmap");
140 if (close(obj->fd) == -1 && err == NULL)
141 err = got_error_from_errno("close");
142 } else
143 free(obj->data);
144 } else {
145 if (fclose(obj->f) == EOF && err == NULL)
146 err = got_error_from_errno("fclose");
148 free(obj);
149 return err;
152 void
153 got_object_qid_free(struct got_object_qid *qid)
155 free(qid);
158 void
159 got_object_id_queue_free(struct got_object_id_queue *ids)
161 struct got_object_qid *qid;
163 while (!STAILQ_EMPTY(ids)) {
164 qid = STAILQ_FIRST(ids);
165 STAILQ_REMOVE_HEAD(ids, entry);
166 got_object_qid_free(qid);
170 const struct got_error *
171 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
173 const char *obj_labels[] = {
174 GOT_OBJ_LABEL_COMMIT,
175 GOT_OBJ_LABEL_TREE,
176 GOT_OBJ_LABEL_BLOB,
177 GOT_OBJ_LABEL_TAG,
178 };
179 const int obj_types[] = {
180 GOT_OBJ_TYPE_COMMIT,
181 GOT_OBJ_TYPE_TREE,
182 GOT_OBJ_TYPE_BLOB,
183 GOT_OBJ_TYPE_TAG,
184 };
185 int type = 0;
186 size_t size = 0;
187 size_t i;
188 char *end;
190 *obj = NULL;
192 end = memchr(buf, '\0', len);
193 if (end == NULL)
194 return got_error(GOT_ERR_BAD_OBJ_HDR);
196 for (i = 0; i < nitems(obj_labels); i++) {
197 const char *label = obj_labels[i];
198 size_t label_len = strlen(label);
199 const char *errstr;
201 if (len <= label_len || buf + label_len >= end ||
202 strncmp(buf, label, label_len) != 0)
203 continue;
205 type = obj_types[i];
206 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
207 if (errstr != NULL)
208 return got_error(GOT_ERR_BAD_OBJ_HDR);
209 break;
212 if (type == 0)
213 return got_error(GOT_ERR_BAD_OBJ_HDR);
215 *obj = calloc(1, sizeof(**obj));
216 if (*obj == NULL)
217 return got_error_from_errno("calloc");
218 (*obj)->type = type;
219 (*obj)->hdrlen = end - buf + 1;
220 (*obj)->size = size;
221 return NULL;
224 const struct got_error *
225 got_object_read_header(struct got_object **obj, int fd)
227 const struct got_error *err;
228 struct got_inflate_buf zb;
229 uint8_t *buf;
230 const size_t zbsize = 64;
231 size_t outlen, totlen;
232 int nbuf = 1;
234 *obj = NULL;
236 buf = malloc(zbsize);
237 if (buf == NULL)
238 return got_error_from_errno("malloc");
239 buf[0] = '\0';
241 err = got_inflate_init(&zb, buf, zbsize, NULL);
242 if (err)
243 return err;
245 totlen = 0;
246 do {
247 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
248 if (err)
249 goto done;
250 if (outlen == 0)
251 break;
252 totlen += outlen;
253 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
254 uint8_t *newbuf;
255 nbuf++;
256 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
257 if (newbuf == NULL) {
258 err = got_error_from_errno("recallocarray");
259 goto done;
261 buf = newbuf;
262 zb.outbuf = newbuf + totlen;
263 zb.outlen = (nbuf * zbsize) - totlen;
265 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
267 err = got_object_parse_header(obj, buf, totlen);
268 done:
269 free(buf);
270 got_inflate_end(&zb);
271 return err;
274 struct got_commit_object *
275 got_object_commit_alloc_partial(void)
277 struct got_commit_object *commit;
279 commit = calloc(1, sizeof(*commit));
280 if (commit == NULL)
281 return NULL;
282 commit->tree_id = malloc(sizeof(*commit->tree_id));
283 if (commit->tree_id == NULL) {
284 free(commit);
285 return NULL;
288 STAILQ_INIT(&commit->parent_ids);
290 return commit;
293 const struct got_error *
294 got_object_commit_add_parent(struct got_commit_object *commit,
295 const char *id_str)
297 const struct got_error *err = NULL;
298 struct got_object_qid *qid;
300 err = got_object_qid_alloc_partial(&qid);
301 if (err)
302 return err;
304 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
305 err = got_error(GOT_ERR_BAD_OBJ_DATA);
306 got_object_qid_free(qid);
307 return err;
310 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
311 commit->nparents++;
313 return NULL;
316 static const struct got_error *
317 parse_gmtoff(time_t *gmtoff, const char *tzstr)
319 int sign = 1;
320 const char *p = tzstr;
321 time_t h, m;
323 *gmtoff = 0;
325 if (*p == '-')
326 sign = -1;
327 else if (*p != '+')
328 return got_error(GOT_ERR_BAD_OBJ_DATA);
329 p++;
330 if (!isdigit(*p) && !isdigit(*(p + 1)))
331 return got_error(GOT_ERR_BAD_OBJ_DATA);
332 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
334 p += 2;
335 if (!isdigit(*p) && !isdigit(*(p + 1)))
336 return got_error(GOT_ERR_BAD_OBJ_DATA);
337 m = ((*p - '0') * 10) + (*(p + 1) - '0');
339 *gmtoff = (h * 60 * 60 + m * 60) * sign;
340 return NULL;
343 static const struct got_error *
344 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
346 const struct got_error *err = NULL;
347 const char *errstr;
348 char *space, *tzstr;
350 /* Parse and strip off trailing timezone indicator string. */
351 space = strrchr(committer, ' ');
352 if (space == NULL)
353 return got_error(GOT_ERR_BAD_OBJ_DATA);
354 tzstr = strdup(space + 1);
355 if (tzstr == NULL)
356 return got_error_from_errno("strdup");
357 err = parse_gmtoff(gmtoff, tzstr);
358 free(tzstr);
359 if (err) {
360 if (err->code != GOT_ERR_BAD_OBJ_DATA)
361 return err;
362 /* Old versions of Git omitted the timestamp. */
363 *time = 0;
364 *gmtoff = 0;
365 return NULL;
367 *space = '\0';
369 /* Timestamp is separated from committer name + email by space. */
370 space = strrchr(committer, ' ');
371 if (space == NULL)
372 return got_error(GOT_ERR_BAD_OBJ_DATA);
374 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
375 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
376 if (errstr)
377 return got_error(GOT_ERR_BAD_OBJ_DATA);
379 /* Strip off parsed time information, leaving just author and email. */
380 *space = '\0';
382 return NULL;
385 void
386 got_object_commit_close(struct got_commit_object *commit)
388 if (commit->refcnt > 0) {
389 commit->refcnt--;
390 if (commit->refcnt > 0)
391 return;
394 got_object_id_queue_free(&commit->parent_ids);
395 free(commit->tree_id);
396 free(commit->author);
397 free(commit->committer);
398 free(commit->logmsg);
399 free(commit);
402 struct got_object_id *
403 got_object_commit_get_tree_id(struct got_commit_object *commit)
405 return commit->tree_id;
408 int
409 got_object_commit_get_nparents(struct got_commit_object *commit)
411 return commit->nparents;
414 const struct got_object_id_queue *
415 got_object_commit_get_parent_ids(struct got_commit_object *commit)
417 return &commit->parent_ids;
420 const char *
421 got_object_commit_get_author(struct got_commit_object *commit)
423 return commit->author;
426 time_t
427 got_object_commit_get_author_time(struct got_commit_object *commit)
429 return commit->author_time;
432 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
434 return commit->author_gmtoff;
437 const char *
438 got_object_commit_get_committer(struct got_commit_object *commit)
440 return commit->committer;
443 time_t
444 got_object_commit_get_committer_time(struct got_commit_object *commit)
446 return commit->committer_time;
449 time_t
450 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
452 return commit->committer_gmtoff;
455 const struct got_error *
456 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
458 const struct got_error *err = NULL;
459 const char *src;
460 char *dst;
461 size_t len;
463 len = strlen(commit->logmsg);
464 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
465 if (*logmsg == NULL)
466 return got_error_from_errno("malloc");
468 /*
469 * Strip out unusual headers. Headers are separated from the commit
470 * message body by a single empty line.
471 */
472 src = commit->logmsg;
473 dst = *logmsg;
474 while (*src != '\0' && *src != '\n') {
475 int copy_header = 1, eol = 0;
476 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
477 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
478 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
479 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
480 strncmp(src, GOT_COMMIT_LABEL_PARENT,
481 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
482 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
483 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
484 copy_header = 0;
486 while (*src != '\0' && !eol) {
487 if (copy_header) {
488 *dst = *src;
489 dst++;
491 if (*src == '\n')
492 eol = 1;
493 src++;
496 *dst = '\0';
498 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
499 err = got_error(GOT_ERR_NO_SPACE);
500 goto done;
503 /* Trim redundant trailing whitespace. */
504 len = strlen(*logmsg);
505 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
506 isspace((unsigned char)(*logmsg)[len - 1])) {
507 (*logmsg)[len - 1] = '\0';
508 len--;
511 /* Append a trailing newline if missing. */
512 if (len > 0 && (*logmsg)[len - 1] != '\n') {
513 (*logmsg)[len] = '\n';
514 (*logmsg)[len + 1] = '\0';
516 done:
517 if (err) {
518 free(*logmsg);
519 *logmsg = NULL;
521 return err;
524 const char *
525 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
527 return commit->logmsg;
530 const struct got_error *
531 got_object_parse_commit(struct got_commit_object **commit, char *buf,
532 size_t len)
534 const struct got_error *err = NULL;
535 char *s = buf;
536 size_t label_len;
537 ssize_t remain = (ssize_t)len;
539 if (remain == 0)
540 return got_error(GOT_ERR_BAD_OBJ_DATA);
542 *commit = got_object_commit_alloc_partial();
543 if (*commit == NULL)
544 return got_error_from_errno("got_object_commit_alloc_partial");
546 label_len = strlen(GOT_COMMIT_LABEL_TREE);
547 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
548 remain -= label_len;
549 if (remain < SHA1_DIGEST_STRING_LENGTH) {
550 err = got_error(GOT_ERR_BAD_OBJ_DATA);
551 goto done;
553 s += label_len;
554 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
555 err = got_error(GOT_ERR_BAD_OBJ_DATA);
556 goto done;
558 remain -= SHA1_DIGEST_STRING_LENGTH;
559 s += SHA1_DIGEST_STRING_LENGTH;
560 } else {
561 err = got_error(GOT_ERR_BAD_OBJ_DATA);
562 goto done;
565 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
566 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
567 remain -= label_len;
568 if (remain < SHA1_DIGEST_STRING_LENGTH) {
569 err = got_error(GOT_ERR_BAD_OBJ_DATA);
570 goto done;
572 s += label_len;
573 err = got_object_commit_add_parent(*commit, s);
574 if (err)
575 goto done;
577 remain -= SHA1_DIGEST_STRING_LENGTH;
578 s += SHA1_DIGEST_STRING_LENGTH;
581 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
582 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
583 char *p;
584 size_t slen;
586 remain -= label_len;
587 if (remain <= 0) {
588 err = got_error(GOT_ERR_BAD_OBJ_DATA);
589 goto done;
591 s += label_len;
592 p = memchr(s, '\n', remain);
593 if (p == NULL) {
594 err = got_error(GOT_ERR_BAD_OBJ_DATA);
595 goto done;
597 *p = '\0';
598 slen = strlen(s);
599 err = parse_commit_time(&(*commit)->author_time,
600 &(*commit)->author_gmtoff, s);
601 if (err)
602 goto done;
603 (*commit)->author = strdup(s);
604 if ((*commit)->author == NULL) {
605 err = got_error_from_errno("strdup");
606 goto done;
608 s += slen + 1;
609 remain -= slen + 1;
612 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
613 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
614 char *p;
615 size_t slen;
617 remain -= label_len;
618 if (remain <= 0) {
619 err = got_error(GOT_ERR_BAD_OBJ_DATA);
620 goto done;
622 s += label_len;
623 p = memchr(s, '\n', remain);
624 if (p == NULL) {
625 err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 goto done;
628 *p = '\0';
629 slen = strlen(s);
630 err = parse_commit_time(&(*commit)->committer_time,
631 &(*commit)->committer_gmtoff, s);
632 if (err)
633 goto done;
634 (*commit)->committer = strdup(s);
635 if ((*commit)->committer == NULL) {
636 err = got_error_from_errno("strdup");
637 goto done;
639 s += slen + 1;
640 remain -= slen + 1;
643 (*commit)->logmsg = strndup(s, remain);
644 if ((*commit)->logmsg == NULL) {
645 err = got_error_from_errno("strndup");
646 goto done;
648 done:
649 if (err) {
650 got_object_commit_close(*commit);
651 *commit = NULL;
653 return err;
656 void
657 got_object_tree_close(struct got_tree_object *tree)
659 if (tree->refcnt > 0) {
660 tree->refcnt--;
661 if (tree->refcnt > 0)
662 return;
665 free(tree->entries);
666 free(tree);
669 static const struct got_error *
670 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
671 size_t maxlen)
673 char *p, *space;
675 *elen = 0;
677 *elen = strnlen(buf, maxlen) + 1;
678 if (*elen > maxlen)
679 return got_error(GOT_ERR_BAD_OBJ_DATA);
681 space = memchr(buf, ' ', *elen);
682 if (space == NULL || space <= buf)
683 return got_error(GOT_ERR_BAD_OBJ_DATA);
685 pte->mode = 0;
686 p = buf;
687 while (p < space) {
688 if (*p < '0' && *p > '7')
689 return got_error(GOT_ERR_BAD_OBJ_DATA);
690 pte->mode <<= 3;
691 pte->mode |= *p - '0';
692 p++;
695 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
696 return got_error(GOT_ERR_BAD_OBJ_DATA);
698 pte->name = space + 1;
699 pte->namelen = strlen(pte->name);
700 buf += *elen;
701 pte->id = buf;
702 *elen += SHA1_DIGEST_LENGTH;
703 return NULL;
706 static int
707 pte_cmp(const void *pa, const void *pb)
709 const struct got_parsed_tree_entry *a = pa, *b = pb;
711 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
714 const struct got_error *
715 got_object_parse_tree(struct got_parsed_tree_entry **entries, int *nentries,
716 uint8_t *buf, size_t len)
718 const struct got_error *err = NULL;
719 size_t remain = len, totalloc;
720 const size_t nalloc = 16;
721 struct got_parsed_tree_entry *pte;
722 int i;
724 *nentries = 0;
725 if (remain == 0)
726 return NULL; /* tree is empty */
728 *entries = calloc(nalloc, sizeof(**entries));
729 if (*entries == NULL)
730 return got_error_from_errno("calloc");
731 totalloc = nalloc;
733 while (remain > 0) {
734 size_t elen;
736 if (*nentries >= totalloc) {
737 pte = recallocarray(*entries, totalloc,
738 totalloc + nalloc, sizeof(**entries));
739 if (pte == NULL) {
740 err = got_error_from_errno("recallocarray");
741 goto done;
743 *entries = pte;
744 totalloc += nalloc;
747 pte = &(*entries)[*nentries];
748 err = parse_tree_entry(pte, &elen, buf, remain);
749 if (err)
750 goto done;
751 buf += elen;
752 remain -= elen;
753 (*nentries)++;
756 if (remain != 0) {
757 err = got_error(GOT_ERR_BAD_OBJ_DATA);
758 goto done;
761 if (*nentries > 1) {
762 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
764 for (i = 0; i < *nentries - 1; i++) {
765 struct got_parsed_tree_entry *prev = &(*entries)[i];
766 pte = &(*entries)[i + 1];
767 if (got_path_cmp(prev->name, pte->name,
768 prev->namelen, pte->namelen) == 0) {
769 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
770 break;
774 done:
775 if (err) {
776 free(*entries);
777 *entries = NULL;
778 *nentries = 0;
780 return err;
783 void
784 got_object_tag_close(struct got_tag_object *tag)
786 if (tag->refcnt > 0) {
787 tag->refcnt--;
788 if (tag->refcnt > 0)
789 return;
792 free(tag->tag);
793 free(tag->tagger);
794 free(tag->tagmsg);
795 free(tag);
798 const struct got_error *
799 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
801 const struct got_error *err = NULL;
802 size_t remain = len;
803 char *s = buf;
804 size_t label_len;
806 if (remain == 0)
807 return got_error(GOT_ERR_BAD_OBJ_DATA);
809 *tag = calloc(1, sizeof(**tag));
810 if (*tag == NULL)
811 return got_error_from_errno("calloc");
813 label_len = strlen(GOT_TAG_LABEL_OBJECT);
814 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
815 remain -= label_len;
816 if (remain < SHA1_DIGEST_STRING_LENGTH) {
817 err = got_error(GOT_ERR_BAD_OBJ_DATA);
818 goto done;
820 s += label_len;
821 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
822 err = got_error(GOT_ERR_BAD_OBJ_DATA);
823 goto done;
825 remain -= SHA1_DIGEST_STRING_LENGTH;
826 s += SHA1_DIGEST_STRING_LENGTH;
827 } else {
828 err = got_error(GOT_ERR_BAD_OBJ_DATA);
829 goto done;
832 if (remain <= 0) {
833 err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 goto done;
837 label_len = strlen(GOT_TAG_LABEL_TYPE);
838 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
839 remain -= label_len;
840 if (remain <= 0) {
841 err = got_error(GOT_ERR_BAD_OBJ_DATA);
842 goto done;
844 s += label_len;
845 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
846 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
847 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
848 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
849 s += label_len;
850 remain -= label_len;
851 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
852 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
853 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
854 label_len = strlen(GOT_OBJ_LABEL_TREE);
855 s += label_len;
856 remain -= label_len;
857 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
858 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
859 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
860 label_len = strlen(GOT_OBJ_LABEL_BLOB);
861 s += label_len;
862 remain -= label_len;
863 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
864 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
865 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
866 label_len = strlen(GOT_OBJ_LABEL_TAG);
867 s += label_len;
868 remain -= label_len;
869 } else {
870 err = got_error(GOT_ERR_BAD_OBJ_DATA);
871 goto done;
874 if (remain <= 0 || *s != '\n') {
875 err = got_error(GOT_ERR_BAD_OBJ_DATA);
876 goto done;
878 s++;
879 remain--;
880 if (remain <= 0) {
881 err = got_error(GOT_ERR_BAD_OBJ_DATA);
882 goto done;
884 } else {
885 err = got_error(GOT_ERR_BAD_OBJ_DATA);
886 goto done;
889 label_len = strlen(GOT_TAG_LABEL_TAG);
890 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
891 char *p;
892 size_t slen;
893 remain -= label_len;
894 if (remain <= 0) {
895 err = got_error(GOT_ERR_BAD_OBJ_DATA);
896 goto done;
898 s += label_len;
899 p = memchr(s, '\n', remain);
900 if (p == NULL) {
901 err = got_error(GOT_ERR_BAD_OBJ_DATA);
902 goto done;
904 *p = '\0';
905 slen = strlen(s);
906 (*tag)->tag = strndup(s, slen);
907 if ((*tag)->tag == NULL) {
908 err = got_error_from_errno("strndup");
909 goto done;
911 s += slen + 1;
912 remain -= slen + 1;
913 if (remain <= 0) {
914 err = got_error(GOT_ERR_BAD_OBJ_DATA);
915 goto done;
917 } else {
918 err = got_error(GOT_ERR_BAD_OBJ_DATA);
919 goto done;
922 label_len = strlen(GOT_TAG_LABEL_TAGGER);
923 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
924 char *p;
925 size_t slen;
927 remain -= label_len;
928 if (remain <= 0) {
929 err = got_error(GOT_ERR_BAD_OBJ_DATA);
930 goto done;
932 s += label_len;
933 p = memchr(s, '\n', remain);
934 if (p == NULL) {
935 err = got_error(GOT_ERR_BAD_OBJ_DATA);
936 goto done;
938 *p = '\0';
939 slen = strlen(s);
940 err = parse_commit_time(&(*tag)->tagger_time,
941 &(*tag)->tagger_gmtoff, s);
942 if (err)
943 goto done;
944 (*tag)->tagger = strdup(s);
945 if ((*tag)->tagger == NULL) {
946 err = got_error_from_errno("strdup");
947 goto done;
949 s += slen + 1;
950 remain -= slen + 1;
951 if (remain < 0) {
952 err = got_error(GOT_ERR_BAD_OBJ_DATA);
953 goto done;
955 } else {
956 /* Some old tags in the Linux git repo have no tagger. */
957 (*tag)->tagger = strdup("");
958 if ((*tag)->tagger == NULL) {
959 err = got_error_from_errno("strdup");
960 goto done;
964 (*tag)->tagmsg = strndup(s, remain);
965 if ((*tag)->tagmsg == NULL) {
966 err = got_error_from_errno("strndup");
967 goto done;
969 done:
970 if (err) {
971 got_object_tag_close(*tag);
972 *tag = NULL;
974 return err;
977 const struct got_error *
978 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
980 const struct got_error *err = NULL;
981 static const size_t blocksize = 512;
982 size_t n, total, remain;
983 uint8_t *buf;
985 *outbuf = NULL;
986 *outlen = 0;
988 buf = malloc(blocksize);
989 if (buf == NULL)
990 return got_error_from_errno("malloc");
992 remain = blocksize;
993 total = 0;
994 for (;;) {
995 if (remain == 0) {
996 uint8_t *newbuf;
997 newbuf = reallocarray(buf, 1, total + blocksize);
998 if (newbuf == NULL) {
999 err = got_error_from_errno("reallocarray");
1000 goto done;
1002 buf = newbuf;
1003 remain += blocksize;
1005 n = fread(buf + total, 1, remain, f);
1006 if (n == 0) {
1007 if (ferror(f)) {
1008 err = got_ferror(f, GOT_ERR_IO);
1009 goto done;
1011 break; /* EOF */
1013 remain -= n;
1014 total += n;
1017 done:
1018 if (err == NULL) {
1019 *outbuf = buf;
1020 *outlen = total;
1021 } else
1022 free(buf);
1023 return err;