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/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/mman.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <zlib.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <time.h>
34 #include <unistd.h>
36 #include "got_compat.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_sha1.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_cache.h"
50 #include "got_lib_pack.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, size_t *nentries,
716 size_t *nentries_alloc, uint8_t *buf, size_t len)
718 const struct got_error *err = NULL;
719 size_t remain = len;
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 while (remain > 0) {
729 size_t elen;
731 if (*nentries >= *nentries_alloc) {
732 pte = recallocarray(*entries, *nentries_alloc,
733 *nentries_alloc + nalloc, sizeof(**entries));
734 if (pte == NULL) {
735 err = got_error_from_errno("recallocarray");
736 goto done;
738 *entries = pte;
739 *nentries_alloc += nalloc;
742 pte = &(*entries)[*nentries];
743 err = parse_tree_entry(pte, &elen, buf, remain);
744 if (err)
745 goto done;
746 buf += elen;
747 remain -= elen;
748 (*nentries)++;
751 if (remain != 0) {
752 err = got_error(GOT_ERR_BAD_OBJ_DATA);
753 goto done;
756 if (*nentries > 1) {
757 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
759 for (i = 0; i < *nentries - 1; i++) {
760 struct got_parsed_tree_entry *prev = &(*entries)[i];
761 pte = &(*entries)[i + 1];
762 if (got_path_cmp(prev->name, pte->name,
763 prev->namelen, pte->namelen) == 0) {
764 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
765 break;
769 done:
770 if (err)
771 *nentries = 0;
772 return err;
775 void
776 got_object_tag_close(struct got_tag_object *tag)
778 if (tag->refcnt > 0) {
779 tag->refcnt--;
780 if (tag->refcnt > 0)
781 return;
784 free(tag->tag);
785 free(tag->tagger);
786 free(tag->tagmsg);
787 free(tag);
790 const struct got_error *
791 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
793 const struct got_error *err = NULL;
794 size_t remain = len;
795 char *s = buf;
796 size_t label_len;
798 if (remain == 0)
799 return got_error(GOT_ERR_BAD_OBJ_DATA);
801 *tag = calloc(1, sizeof(**tag));
802 if (*tag == NULL)
803 return got_error_from_errno("calloc");
805 label_len = strlen(GOT_TAG_LABEL_OBJECT);
806 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
807 remain -= label_len;
808 if (remain < SHA1_DIGEST_STRING_LENGTH) {
809 err = got_error(GOT_ERR_BAD_OBJ_DATA);
810 goto done;
812 s += label_len;
813 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
814 err = got_error(GOT_ERR_BAD_OBJ_DATA);
815 goto done;
817 remain -= SHA1_DIGEST_STRING_LENGTH;
818 s += SHA1_DIGEST_STRING_LENGTH;
819 } else {
820 err = got_error(GOT_ERR_BAD_OBJ_DATA);
821 goto done;
824 if (remain <= 0) {
825 err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 goto done;
829 label_len = strlen(GOT_TAG_LABEL_TYPE);
830 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
831 remain -= label_len;
832 if (remain <= 0) {
833 err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 goto done;
836 s += label_len;
837 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
838 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
839 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
840 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
841 s += label_len;
842 remain -= label_len;
843 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
844 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
845 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
846 label_len = strlen(GOT_OBJ_LABEL_TREE);
847 s += label_len;
848 remain -= label_len;
849 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
850 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
851 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
852 label_len = strlen(GOT_OBJ_LABEL_BLOB);
853 s += label_len;
854 remain -= label_len;
855 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
856 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
857 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
858 label_len = strlen(GOT_OBJ_LABEL_TAG);
859 s += label_len;
860 remain -= label_len;
861 } else {
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
863 goto done;
866 if (remain <= 0 || *s != '\n') {
867 err = got_error(GOT_ERR_BAD_OBJ_DATA);
868 goto done;
870 s++;
871 remain--;
872 if (remain <= 0) {
873 err = got_error(GOT_ERR_BAD_OBJ_DATA);
874 goto done;
876 } else {
877 err = got_error(GOT_ERR_BAD_OBJ_DATA);
878 goto done;
881 label_len = strlen(GOT_TAG_LABEL_TAG);
882 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
883 char *p;
884 size_t slen;
885 remain -= label_len;
886 if (remain <= 0) {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
888 goto done;
890 s += label_len;
891 p = memchr(s, '\n', remain);
892 if (p == NULL) {
893 err = got_error(GOT_ERR_BAD_OBJ_DATA);
894 goto done;
896 *p = '\0';
897 slen = strlen(s);
898 (*tag)->tag = strndup(s, slen);
899 if ((*tag)->tag == NULL) {
900 err = got_error_from_errno("strndup");
901 goto done;
903 s += slen + 1;
904 remain -= slen + 1;
905 if (remain <= 0) {
906 err = got_error(GOT_ERR_BAD_OBJ_DATA);
907 goto done;
909 } else {
910 err = got_error(GOT_ERR_BAD_OBJ_DATA);
911 goto done;
914 label_len = strlen(GOT_TAG_LABEL_TAGGER);
915 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
916 char *p;
917 size_t slen;
919 remain -= label_len;
920 if (remain <= 0) {
921 err = got_error(GOT_ERR_BAD_OBJ_DATA);
922 goto done;
924 s += label_len;
925 p = memchr(s, '\n', remain);
926 if (p == NULL) {
927 err = got_error(GOT_ERR_BAD_OBJ_DATA);
928 goto done;
930 *p = '\0';
931 slen = strlen(s);
932 err = parse_commit_time(&(*tag)->tagger_time,
933 &(*tag)->tagger_gmtoff, s);
934 if (err)
935 goto done;
936 (*tag)->tagger = strdup(s);
937 if ((*tag)->tagger == NULL) {
938 err = got_error_from_errno("strdup");
939 goto done;
941 s += slen + 1;
942 remain -= slen + 1;
943 if (remain < 0) {
944 err = got_error(GOT_ERR_BAD_OBJ_DATA);
945 goto done;
947 } else {
948 /* Some old tags in the Linux git repo have no tagger. */
949 (*tag)->tagger = strdup("");
950 if ((*tag)->tagger == NULL) {
951 err = got_error_from_errno("strdup");
952 goto done;
956 (*tag)->tagmsg = strndup(s, remain);
957 if ((*tag)->tagmsg == NULL) {
958 err = got_error_from_errno("strndup");
959 goto done;
961 done:
962 if (err) {
963 got_object_tag_close(*tag);
964 *tag = NULL;
966 return err;
969 const struct got_error *
970 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
972 const struct got_error *err = NULL;
973 static const size_t blocksize = 512;
974 size_t n, total, remain;
975 uint8_t *buf;
977 *outbuf = NULL;
978 *outlen = 0;
980 buf = malloc(blocksize);
981 if (buf == NULL)
982 return got_error_from_errno("malloc");
984 remain = blocksize;
985 total = 0;
986 for (;;) {
987 if (remain == 0) {
988 uint8_t *newbuf;
989 newbuf = reallocarray(buf, 1, total + blocksize);
990 if (newbuf == NULL) {
991 err = got_error_from_errno("reallocarray");
992 goto done;
994 buf = newbuf;
995 remain += blocksize;
997 n = fread(buf + total, 1, remain, f);
998 if (n == 0) {
999 if (ferror(f)) {
1000 err = got_ferror(f, GOT_ERR_IO);
1001 goto done;
1003 break; /* EOF */
1005 remain -= n;
1006 total += n;
1009 done:
1010 if (err == NULL) {
1011 *outbuf = buf;
1012 *outlen = total;
1013 } else
1014 free(buf);
1015 return err;