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>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <zlib.h>
29 #include <ctype.h>
30 #include <limits.h>
31 #include <time.h>
32 #include <unistd.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
39 #include "got_opentemp.h"
40 #include "got_path.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_repository.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #endif
56 struct got_object_id *
57 got_object_id_dup(struct got_object_id *id1)
58 {
59 struct got_object_id *id2;
61 id2 = malloc(sizeof(*id2));
62 if (id2 == NULL)
63 return NULL;
64 memcpy(id2, id1, sizeof(*id2));
65 return id2;
66 }
68 int
69 got_object_id_cmp(const struct got_object_id *id1,
70 const struct got_object_id *id2)
71 {
72 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
73 }
75 const struct got_error *
76 got_object_qid_alloc_partial(struct got_object_qid **qid)
77 {
78 const struct got_error *err = NULL;
80 *qid = malloc(sizeof(**qid));
81 if (*qid == NULL)
82 return got_error_from_errno("malloc");
84 (*qid)->id = malloc(sizeof(*((*qid)->id)));
85 if ((*qid)->id == NULL) {
86 err = got_error_from_errno("malloc");
87 got_object_qid_free(*qid);
88 *qid = NULL;
89 return err;
90 }
91 (*qid)->data = NULL;
93 return NULL;
94 }
96 const struct got_error *
97 got_object_id_str(char **outbuf, struct got_object_id *id)
98 {
99 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
101 *outbuf = malloc(len);
102 if (*outbuf == NULL)
103 return got_error_from_errno("malloc");
105 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
106 free(*outbuf);
107 *outbuf = NULL;
108 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
111 return NULL;
114 void
115 got_object_close(struct got_object *obj)
117 if (obj->refcnt > 0) {
118 obj->refcnt--;
119 if (obj->refcnt > 0)
120 return;
123 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
124 struct got_delta *delta;
125 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
126 delta = STAILQ_FIRST(&obj->deltas.entries);
127 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
128 free(delta);
131 free(obj);
134 void
135 got_object_qid_free(struct got_object_qid *qid)
137 free(qid->id);
138 free(qid);
141 void
142 got_object_id_queue_free(struct got_object_id_queue *ids)
144 struct got_object_qid *qid;
146 while (!STAILQ_EMPTY(ids)) {
147 qid = STAILQ_FIRST(ids);
148 STAILQ_REMOVE_HEAD(ids, entry);
149 got_object_qid_free(qid);
153 const struct got_error *
154 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
156 const char *obj_labels[] = {
157 GOT_OBJ_LABEL_COMMIT,
158 GOT_OBJ_LABEL_TREE,
159 GOT_OBJ_LABEL_BLOB,
160 GOT_OBJ_LABEL_TAG,
161 };
162 const int obj_types[] = {
163 GOT_OBJ_TYPE_COMMIT,
164 GOT_OBJ_TYPE_TREE,
165 GOT_OBJ_TYPE_BLOB,
166 GOT_OBJ_TYPE_TAG,
167 };
168 int type = 0;
169 size_t size = 0, hdrlen = 0;
170 size_t i;
172 *obj = NULL;
174 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
175 if (hdrlen > len)
176 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 for (i = 0; i < nitems(obj_labels); i++) {
179 const char *label = obj_labels[i];
180 size_t label_len = strlen(label);
181 const char *errstr;
183 if (strncmp(buf, label, label_len) != 0)
184 continue;
186 type = obj_types[i];
187 if (len <= label_len)
188 return got_error(GOT_ERR_BAD_OBJ_HDR);
189 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
190 if (errstr != NULL)
191 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 break;
195 if (type == 0)
196 return got_error(GOT_ERR_BAD_OBJ_HDR);
198 *obj = calloc(1, sizeof(**obj));
199 if (*obj == NULL)
200 return got_error_from_errno("calloc");
201 (*obj)->type = type;
202 (*obj)->hdrlen = hdrlen;
203 (*obj)->size = size;
204 return NULL;
207 const struct got_error *
208 got_object_read_header(struct got_object **obj, int fd)
210 const struct got_error *err;
211 struct got_inflate_buf zb;
212 char *buf;
213 const size_t zbsize = 64;
214 size_t outlen, totlen;
215 int nbuf = 1;
217 *obj = NULL;
219 buf = malloc(zbsize);
220 if (buf == NULL)
221 return got_error_from_errno("malloc");
223 err = got_inflate_init(&zb, buf, zbsize, NULL);
224 if (err)
225 return err;
227 totlen = 0;
228 do {
229 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
230 if (err)
231 goto done;
232 if (outlen == 0)
233 break;
234 totlen += outlen;
235 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
236 char *newbuf;
237 nbuf++;
238 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
239 if (newbuf == NULL) {
240 err = got_error_from_errno("recallocarray");
241 goto done;
243 buf = newbuf;
244 zb.outbuf = newbuf + totlen;
245 zb.outlen = (nbuf * zbsize) - totlen;
247 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
249 err = got_object_parse_header(obj, buf, totlen);
250 done:
251 free(buf);
252 got_inflate_end(&zb);
253 return err;
256 struct got_commit_object *
257 got_object_commit_alloc_partial(void)
259 struct got_commit_object *commit;
261 commit = calloc(1, sizeof(*commit));
262 if (commit == NULL)
263 return NULL;
264 commit->tree_id = malloc(sizeof(*commit->tree_id));
265 if (commit->tree_id == NULL) {
266 free(commit);
267 return NULL;
270 STAILQ_INIT(&commit->parent_ids);
272 return commit;
275 const struct got_error *
276 got_object_commit_add_parent(struct got_commit_object *commit,
277 const char *id_str)
279 const struct got_error *err = NULL;
280 struct got_object_qid *qid;
282 err = got_object_qid_alloc_partial(&qid);
283 if (err)
284 return err;
286 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
287 err = got_error(GOT_ERR_BAD_OBJ_DATA);
288 got_object_qid_free(qid);
289 return err;
292 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
293 commit->nparents++;
295 return NULL;
298 static const struct got_error *
299 parse_gmtoff(time_t *gmtoff, const char *tzstr)
301 int sign = 1;
302 const char *p = tzstr;
303 time_t h, m;
305 *gmtoff = 0;
307 if (*p == '-')
308 sign = -1;
309 else if (*p != '+')
310 return got_error(GOT_ERR_BAD_OBJ_DATA);
311 p++;
312 if (!isdigit(*p) && !isdigit(*(p + 1)))
313 return got_error(GOT_ERR_BAD_OBJ_DATA);
314 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
316 p += 2;
317 if (!isdigit(*p) && !isdigit(*(p + 1)))
318 return got_error(GOT_ERR_BAD_OBJ_DATA);
319 m = ((*p - '0') * 10) + (*(p + 1) - '0');
321 *gmtoff = (h * 60 * 60 + m * 60) * sign;
322 return NULL;
325 static const struct got_error *
326 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
328 const struct got_error *err = NULL;
329 const char *errstr;
330 char *space, *tzstr;
332 /* Parse and strip off trailing timezone indicator string. */
333 space = strrchr(committer, ' ');
334 if (space == NULL)
335 return got_error(GOT_ERR_BAD_OBJ_DATA);
336 tzstr = strdup(space + 1);
337 if (tzstr == NULL)
338 return got_error_from_errno("strdup");
339 err = parse_gmtoff(gmtoff, tzstr);
340 free(tzstr);
341 if (err) {
342 if (err->code != GOT_ERR_BAD_OBJ_DATA)
343 return err;
344 /* Old versions of Git omitted the timestamp. */
345 *time = 0;
346 *gmtoff = 0;
347 return NULL;
349 *space = '\0';
351 /* Timestamp is separated from committer name + email by space. */
352 space = strrchr(committer, ' ');
353 if (space == NULL)
354 return got_error(GOT_ERR_BAD_OBJ_DATA);
356 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
357 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
358 if (errstr)
359 return got_error(GOT_ERR_BAD_OBJ_DATA);
361 /* Strip off parsed time information, leaving just author and email. */
362 *space = '\0';
364 return NULL;
367 void
368 got_object_commit_close(struct got_commit_object *commit)
370 if (commit->refcnt > 0) {
371 commit->refcnt--;
372 if (commit->refcnt > 0)
373 return;
376 got_object_id_queue_free(&commit->parent_ids);
377 free(commit->tree_id);
378 free(commit->author);
379 free(commit->committer);
380 free(commit->logmsg);
381 free(commit);
384 struct got_object_id *
385 got_object_commit_get_tree_id(struct got_commit_object *commit)
387 return commit->tree_id;
390 int
391 got_object_commit_get_nparents(struct got_commit_object *commit)
393 return commit->nparents;
396 const struct got_object_id_queue *
397 got_object_commit_get_parent_ids(struct got_commit_object *commit)
399 return &commit->parent_ids;
402 const char *
403 got_object_commit_get_author(struct got_commit_object *commit)
405 return commit->author;
408 time_t
409 got_object_commit_get_author_time(struct got_commit_object *commit)
411 return commit->author_time;
414 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
416 return commit->author_gmtoff;
419 const char *
420 got_object_commit_get_committer(struct got_commit_object *commit)
422 return commit->committer;
425 time_t
426 got_object_commit_get_committer_time(struct got_commit_object *commit)
428 return commit->committer_time;
431 time_t
432 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
434 return commit->committer_gmtoff;
437 const struct got_error *
438 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
440 const struct got_error *err = NULL;
441 const char *src;
442 char *dst;
443 size_t len;
445 len = strlen(commit->logmsg);
446 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
447 if (*logmsg == NULL)
448 return got_error_from_errno("malloc");
450 /*
451 * Strip out unusual headers. Headers are separated from the commit
452 * message body by a single empty line.
453 */
454 src = commit->logmsg;
455 dst = *logmsg;
456 while (*src != '\0' && *src != '\n') {
457 int copy_header = 1, eol = 0;
458 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
459 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
460 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
461 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
462 strncmp(src, GOT_COMMIT_LABEL_PARENT,
463 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
464 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
465 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
466 copy_header = 0;
468 while (*src != '\0' && !eol) {
469 if (copy_header) {
470 *dst = *src;
471 dst++;
473 if (*src == '\n')
474 eol = 1;
475 src++;
478 *dst = '\0';
480 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
481 err = got_error(GOT_ERR_NO_SPACE);
482 goto done;
485 /* Trim redundant trailing whitespace. */
486 len = strlen(*logmsg);
487 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
488 isspace((unsigned char)(*logmsg)[len - 1])) {
489 (*logmsg)[len - 1] = '\0';
490 len--;
493 /* Append a trailing newline if missing. */
494 if (len > 0 && (*logmsg)[len - 1] != '\n') {
495 (*logmsg)[len] = '\n';
496 (*logmsg)[len + 1] = '\0';
498 done:
499 if (err) {
500 free(*logmsg);
501 *logmsg = NULL;
503 return err;
506 const char *
507 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
509 return commit->logmsg;
512 const struct got_error *
513 got_object_parse_commit(struct got_commit_object **commit, char *buf,
514 size_t len)
516 const struct got_error *err = NULL;
517 char *s = buf;
518 size_t label_len;
519 ssize_t remain = (ssize_t)len;
521 if (remain == 0)
522 return got_error(GOT_ERR_BAD_OBJ_DATA);
524 *commit = got_object_commit_alloc_partial();
525 if (*commit == NULL)
526 return got_error_from_errno("got_object_commit_alloc_partial");
528 label_len = strlen(GOT_COMMIT_LABEL_TREE);
529 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
530 remain -= label_len;
531 if (remain < SHA1_DIGEST_STRING_LENGTH) {
532 err = got_error(GOT_ERR_BAD_OBJ_DATA);
533 goto done;
535 s += label_len;
536 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
537 err = got_error(GOT_ERR_BAD_OBJ_DATA);
538 goto done;
540 remain -= SHA1_DIGEST_STRING_LENGTH;
541 s += SHA1_DIGEST_STRING_LENGTH;
542 } else {
543 err = got_error(GOT_ERR_BAD_OBJ_DATA);
544 goto done;
547 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
548 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
549 remain -= label_len;
550 if (remain < SHA1_DIGEST_STRING_LENGTH) {
551 err = got_error(GOT_ERR_BAD_OBJ_DATA);
552 goto done;
554 s += label_len;
555 err = got_object_commit_add_parent(*commit, s);
556 if (err)
557 goto done;
559 remain -= SHA1_DIGEST_STRING_LENGTH;
560 s += SHA1_DIGEST_STRING_LENGTH;
563 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
564 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
565 char *p;
566 size_t slen;
568 remain -= label_len;
569 if (remain <= 0) {
570 err = got_error(GOT_ERR_BAD_OBJ_DATA);
571 goto done;
573 s += label_len;
574 p = memchr(s, '\n', remain);
575 if (p == NULL) {
576 err = got_error(GOT_ERR_BAD_OBJ_DATA);
577 goto done;
579 *p = '\0';
580 slen = strlen(s);
581 err = parse_commit_time(&(*commit)->author_time,
582 &(*commit)->author_gmtoff, s);
583 if (err)
584 goto done;
585 (*commit)->author = strdup(s);
586 if ((*commit)->author == NULL) {
587 err = got_error_from_errno("strdup");
588 goto done;
590 s += slen + 1;
591 remain -= slen + 1;
594 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
595 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
596 char *p;
597 size_t slen;
599 remain -= label_len;
600 if (remain <= 0) {
601 err = got_error(GOT_ERR_BAD_OBJ_DATA);
602 goto done;
604 s += label_len;
605 p = memchr(s, '\n', remain);
606 if (p == NULL) {
607 err = got_error(GOT_ERR_BAD_OBJ_DATA);
608 goto done;
610 *p = '\0';
611 slen = strlen(s);
612 err = parse_commit_time(&(*commit)->committer_time,
613 &(*commit)->committer_gmtoff, s);
614 if (err)
615 goto done;
616 (*commit)->committer = strdup(s);
617 if ((*commit)->committer == NULL) {
618 err = got_error_from_errno("strdup");
619 goto done;
621 s += slen + 1;
622 remain -= slen + 1;
625 (*commit)->logmsg = strndup(s, remain);
626 if ((*commit)->logmsg == NULL) {
627 err = got_error_from_errno("strndup");
628 goto done;
630 done:
631 if (err) {
632 got_object_commit_close(*commit);
633 *commit = NULL;
635 return err;
638 void
639 got_object_tree_close(struct got_tree_object *tree)
641 if (tree->refcnt > 0) {
642 tree->refcnt--;
643 if (tree->refcnt > 0)
644 return;
647 free(tree->entries);
648 free(tree);
651 static const struct got_error *
652 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
653 size_t *elen, char *buf,
654 size_t maxlen)
656 char *p, *space;
657 const struct got_error *err = NULL;
659 *name = NULL;
660 *elen = 0;
662 *pte = malloc(sizeof(**pte));
663 if (*pte == NULL)
664 return got_error_from_errno("malloc");
666 *elen = strnlen(buf, maxlen) + 1;
667 if (*elen > maxlen) {
668 free(*pte);
669 *pte = NULL;
670 return got_error(GOT_ERR_BAD_OBJ_DATA);
673 space = memchr(buf, ' ', *elen);
674 if (space == NULL || space <= buf) {
675 err = got_error(GOT_ERR_BAD_OBJ_DATA);
676 free(*pte);
677 *pte = NULL;
678 return err;
680 (*pte)->mode = 0;
681 p = buf;
682 while (p < space) {
683 if (*p < '0' && *p > '7') {
684 err = got_error(GOT_ERR_BAD_OBJ_DATA);
685 goto done;
687 (*pte)->mode <<= 3;
688 (*pte)->mode |= *p - '0';
689 p++;
692 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
693 err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 goto done;
696 *name = space + 1;
697 buf += *elen;
698 (*pte)->id = buf;
699 *elen += SHA1_DIGEST_LENGTH;
700 done:
701 if (err) {
702 free(*pte);
703 *pte = NULL;
705 return err;
708 const struct got_error *
709 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
710 uint8_t *buf, size_t len)
712 const struct got_error *err = NULL;
713 size_t remain = len;
715 *nentries = 0;
716 if (remain == 0)
717 return NULL; /* tree is empty */
719 while (remain > 0) {
720 struct got_parsed_tree_entry *pte;
721 struct got_pathlist_entry *new = NULL;
722 const char *name;
723 size_t elen;
725 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
726 if (err)
727 goto done;
728 err = got_pathlist_insert(&new, entries, name, pte);
729 if (err)
730 goto done;
731 if (new == NULL) {
732 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
733 goto done;
735 buf += elen;
736 remain -= elen;
737 (*nentries)++;
740 if (remain != 0) {
741 err = got_error(GOT_ERR_BAD_OBJ_DATA);
742 goto done;
744 done:
745 if (err) {
746 got_object_parsed_tree_entries_free(entries);
747 *nentries = 0;
749 return err;
752 void
753 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
755 struct got_pathlist_entry *pe;
757 TAILQ_FOREACH(pe, entries, entry) {
758 struct got_parsed_tree_entry *pte = pe->data;
759 free(pte);
761 got_pathlist_free(entries);
764 void
765 got_object_tag_close(struct got_tag_object *tag)
767 if (tag->refcnt > 0) {
768 tag->refcnt--;
769 if (tag->refcnt > 0)
770 return;
773 free(tag->tag);
774 free(tag->tagger);
775 free(tag->tagmsg);
776 free(tag);
779 const struct got_error *
780 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
782 const struct got_error *err = NULL;
783 size_t remain = len;
784 char *s = buf;
785 size_t label_len;
787 if (remain == 0)
788 return got_error(GOT_ERR_BAD_OBJ_DATA);
790 *tag = calloc(1, sizeof(**tag));
791 if (*tag == NULL)
792 return got_error_from_errno("calloc");
794 label_len = strlen(GOT_TAG_LABEL_OBJECT);
795 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
796 remain -= label_len;
797 if (remain < SHA1_DIGEST_STRING_LENGTH) {
798 err = got_error(GOT_ERR_BAD_OBJ_DATA);
799 goto done;
801 s += label_len;
802 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
803 err = got_error(GOT_ERR_BAD_OBJ_DATA);
804 goto done;
806 remain -= SHA1_DIGEST_STRING_LENGTH;
807 s += SHA1_DIGEST_STRING_LENGTH;
808 } else {
809 err = got_error(GOT_ERR_BAD_OBJ_DATA);
810 goto done;
813 if (remain <= 0) {
814 err = got_error(GOT_ERR_BAD_OBJ_DATA);
815 goto done;
818 label_len = strlen(GOT_TAG_LABEL_TYPE);
819 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
820 remain -= label_len;
821 if (remain <= 0) {
822 err = got_error(GOT_ERR_BAD_OBJ_DATA);
823 goto done;
825 s += label_len;
826 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
827 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
828 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
829 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
830 s += label_len;
831 remain -= label_len;
832 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
833 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
834 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
835 label_len = strlen(GOT_OBJ_LABEL_TREE);
836 s += label_len;
837 remain -= label_len;
838 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
839 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
840 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
841 label_len = strlen(GOT_OBJ_LABEL_BLOB);
842 s += label_len;
843 remain -= label_len;
844 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
845 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
846 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
847 label_len = strlen(GOT_OBJ_LABEL_TAG);
848 s += label_len;
849 remain -= label_len;
850 } else {
851 err = got_error(GOT_ERR_BAD_OBJ_DATA);
852 goto done;
855 if (remain <= 0 || *s != '\n') {
856 err = got_error(GOT_ERR_BAD_OBJ_DATA);
857 goto done;
859 s++;
860 remain--;
861 if (remain <= 0) {
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
863 goto done;
865 } else {
866 err = got_error(GOT_ERR_BAD_OBJ_DATA);
867 goto done;
870 label_len = strlen(GOT_TAG_LABEL_TAG);
871 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
872 char *p;
873 size_t slen;
874 remain -= label_len;
875 if (remain <= 0) {
876 err = got_error(GOT_ERR_BAD_OBJ_DATA);
877 goto done;
879 s += label_len;
880 p = memchr(s, '\n', remain);
881 if (p == NULL) {
882 err = got_error(GOT_ERR_BAD_OBJ_DATA);
883 goto done;
885 *p = '\0';
886 slen = strlen(s);
887 (*tag)->tag = strndup(s, slen);
888 if ((*tag)->tag == NULL) {
889 err = got_error_from_errno("strndup");
890 goto done;
892 s += slen + 1;
893 remain -= slen + 1;
894 if (remain <= 0) {
895 err = got_error(GOT_ERR_BAD_OBJ_DATA);
896 goto done;
898 } else {
899 err = got_error(GOT_ERR_BAD_OBJ_DATA);
900 goto done;
903 label_len = strlen(GOT_TAG_LABEL_TAGGER);
904 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
905 char *p;
906 size_t slen;
908 remain -= label_len;
909 if (remain <= 0) {
910 err = got_error(GOT_ERR_BAD_OBJ_DATA);
911 goto done;
913 s += label_len;
914 p = memchr(s, '\n', remain);
915 if (p == NULL) {
916 err = got_error(GOT_ERR_BAD_OBJ_DATA);
917 goto done;
919 *p = '\0';
920 slen = strlen(s);
921 err = parse_commit_time(&(*tag)->tagger_time,
922 &(*tag)->tagger_gmtoff, s);
923 if (err)
924 goto done;
925 (*tag)->tagger = strdup(s);
926 if ((*tag)->tagger == NULL) {
927 err = got_error_from_errno("strdup");
928 goto done;
930 s += slen + 1;
931 remain -= slen + 1;
932 if (remain < 0) {
933 err = got_error(GOT_ERR_BAD_OBJ_DATA);
934 goto done;
936 } else {
937 /* Some old tags in the Linux git repo have no tagger. */
938 (*tag)->tagger = strdup("");
939 if ((*tag)->tagger == NULL) {
940 err = got_error_from_errno("strdup");
941 goto done;
945 (*tag)->tagmsg = strndup(s, remain);
946 if ((*tag)->tagmsg == NULL) {
947 err = got_error_from_errno("strndup");
948 goto done;
950 done:
951 if (err) {
952 got_object_tag_close(*tag);
953 *tag = NULL;
955 return err;
958 const struct got_error *
959 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
961 const struct got_error *err = NULL;
962 static const size_t blocksize = 512;
963 size_t n, total, remain;
964 uint8_t *buf;
966 *outbuf = NULL;
967 *outlen = 0;
969 buf = malloc(blocksize);
970 if (buf == NULL)
971 return got_error_from_errno("malloc");
973 remain = blocksize;
974 total = 0;
975 for (;;) {
976 if (remain == 0) {
977 uint8_t *newbuf;
978 newbuf = reallocarray(buf, 1, total + blocksize);
979 if (newbuf == NULL) {
980 err = got_error_from_errno("reallocarray");
981 goto done;
983 buf = newbuf;
984 remain += blocksize;
986 n = fread(buf + total, 1, remain, f);
987 if (n == 0) {
988 if (ferror(f)) {
989 err = got_ferror(f, GOT_ERR_IO);
990 goto done;
992 break; /* EOF */
994 remain -= n;
995 total += n;
996 };
998 done:
999 if (err == NULL) {
1000 *outbuf = buf;
1001 *outlen = total;
1002 } else
1003 free(buf);
1004 return err;