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>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <sha1.h>
30 #include <zlib.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <imsg.h>
34 #include <time.h>
35 #include <unistd.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 const struct got_error *err = NULL;
81 *qid = malloc(sizeof(**qid));
82 if (*qid == NULL)
83 return got_error_from_errno("malloc");
85 (*qid)->id = malloc(sizeof(*((*qid)->id)));
86 if ((*qid)->id == NULL) {
87 err = got_error_from_errno("malloc");
88 got_object_qid_free(*qid);
89 *qid = NULL;
90 return err;
91 }
92 (*qid)->data = NULL;
94 return NULL;
95 }
97 const struct got_error *
98 got_object_id_str(char **outbuf, struct got_object_id *id)
99 {
100 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
102 *outbuf = malloc(len);
103 if (*outbuf == NULL)
104 return got_error_from_errno("malloc");
106 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
107 free(*outbuf);
108 *outbuf = NULL;
109 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
112 return NULL;
115 void
116 got_object_close(struct got_object *obj)
118 if (obj->refcnt > 0) {
119 obj->refcnt--;
120 if (obj->refcnt > 0)
121 return;
124 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
125 struct got_delta *delta;
126 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
127 delta = STAILQ_FIRST(&obj->deltas.entries);
128 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
129 free(delta);
132 free(obj);
135 void
136 got_object_qid_free(struct got_object_qid *qid)
138 free(qid->id);
139 free(qid);
142 void
143 got_object_id_queue_free(struct got_object_id_queue *ids)
145 struct got_object_qid *qid;
147 while (!STAILQ_EMPTY(ids)) {
148 qid = STAILQ_FIRST(ids);
149 STAILQ_REMOVE_HEAD(ids, entry);
150 got_object_qid_free(qid);
154 const struct got_error *
155 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
157 const char *obj_labels[] = {
158 GOT_OBJ_LABEL_COMMIT,
159 GOT_OBJ_LABEL_TREE,
160 GOT_OBJ_LABEL_BLOB,
161 GOT_OBJ_LABEL_TAG,
162 };
163 const int obj_types[] = {
164 GOT_OBJ_TYPE_COMMIT,
165 GOT_OBJ_TYPE_TREE,
166 GOT_OBJ_TYPE_BLOB,
167 GOT_OBJ_TYPE_TAG,
168 };
169 int type = 0;
170 size_t size = 0, hdrlen = 0;
171 size_t i;
173 *obj = NULL;
175 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
176 if (hdrlen > len)
177 return got_error(GOT_ERR_BAD_OBJ_HDR);
179 for (i = 0; i < nitems(obj_labels); i++) {
180 const char *label = obj_labels[i];
181 size_t label_len = strlen(label);
182 const char *errstr;
184 if (strncmp(buf, label, label_len) != 0)
185 continue;
187 type = obj_types[i];
188 if (len <= label_len)
189 return got_error(GOT_ERR_BAD_OBJ_HDR);
190 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
191 if (errstr != NULL)
192 return got_error(GOT_ERR_BAD_OBJ_HDR);
193 break;
196 if (type == 0)
197 return got_error(GOT_ERR_BAD_OBJ_HDR);
199 *obj = calloc(1, sizeof(**obj));
200 if (*obj == NULL)
201 return got_error_from_errno("calloc");
202 (*obj)->type = type;
203 (*obj)->hdrlen = hdrlen;
204 (*obj)->size = size;
205 return NULL;
208 const struct got_error *
209 got_object_read_header(struct got_object **obj, int fd)
211 const struct got_error *err;
212 struct got_inflate_buf zb;
213 char *buf;
214 const size_t zbsize = 64;
215 size_t outlen, totlen;
216 int nbuf = 1;
218 *obj = NULL;
220 buf = malloc(zbsize);
221 if (buf == NULL)
222 return got_error_from_errno("malloc");
224 err = got_inflate_init(&zb, buf, zbsize, NULL);
225 if (err)
226 return err;
228 totlen = 0;
229 do {
230 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
231 if (err)
232 goto done;
233 if (outlen == 0)
234 break;
235 totlen += outlen;
236 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
237 char *newbuf;
238 nbuf++;
239 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
240 if (newbuf == NULL) {
241 err = got_error_from_errno("recallocarray");
242 goto done;
244 buf = newbuf;
245 zb.outbuf = newbuf + totlen;
246 zb.outlen = (nbuf * zbsize) - totlen;
248 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
250 err = got_object_parse_header(obj, buf, totlen);
251 done:
252 free(buf);
253 got_inflate_end(&zb);
254 return err;
257 struct got_commit_object *
258 got_object_commit_alloc_partial(void)
260 struct got_commit_object *commit;
262 commit = calloc(1, sizeof(*commit));
263 if (commit == NULL)
264 return NULL;
265 commit->tree_id = malloc(sizeof(*commit->tree_id));
266 if (commit->tree_id == NULL) {
267 free(commit);
268 return NULL;
271 STAILQ_INIT(&commit->parent_ids);
273 return commit;
276 const struct got_error *
277 got_object_commit_add_parent(struct got_commit_object *commit,
278 const char *id_str)
280 const struct got_error *err = NULL;
281 struct got_object_qid *qid;
283 err = got_object_qid_alloc_partial(&qid);
284 if (err)
285 return err;
287 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
288 err = got_error(GOT_ERR_BAD_OBJ_DATA);
289 got_object_qid_free(qid);
290 return err;
293 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
294 commit->nparents++;
296 return NULL;
299 static const struct got_error *
300 parse_gmtoff(time_t *gmtoff, const char *tzstr)
302 int sign = 1;
303 const char *p = tzstr;
304 time_t h, m;
306 *gmtoff = 0;
308 if (*p == '-')
309 sign = -1;
310 else if (*p != '+')
311 return got_error(GOT_ERR_BAD_OBJ_DATA);
312 p++;
313 if (!isdigit(*p) && !isdigit(*(p + 1)))
314 return got_error(GOT_ERR_BAD_OBJ_DATA);
315 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
317 p += 2;
318 if (!isdigit(*p) && !isdigit(*(p + 1)))
319 return got_error(GOT_ERR_BAD_OBJ_DATA);
320 m = ((*p - '0') * 10) + (*(p + 1) - '0');
322 *gmtoff = (h * 60 * 60 + m * 60) * sign;
323 return NULL;
326 static const struct got_error *
327 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
329 const struct got_error *err = NULL;
330 const char *errstr;
331 char *space, *tzstr;
333 /* Parse and strip off trailing timezone indicator string. */
334 space = strrchr(committer, ' ');
335 if (space == NULL)
336 return got_error(GOT_ERR_BAD_OBJ_DATA);
337 tzstr = strdup(space + 1);
338 if (tzstr == NULL)
339 return got_error_from_errno("strdup");
340 err = parse_gmtoff(gmtoff, tzstr);
341 free(tzstr);
342 if (err) {
343 if (err->code != GOT_ERR_BAD_OBJ_DATA)
344 return err;
345 /* Old versions of Git omitted the timestamp. */
346 *time = 0;
347 *gmtoff = 0;
348 return NULL;
350 *space = '\0';
352 /* Timestamp is separated from committer name + email by space. */
353 space = strrchr(committer, ' ');
354 if (space == NULL)
355 return got_error(GOT_ERR_BAD_OBJ_DATA);
357 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
358 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
359 if (errstr)
360 return got_error(GOT_ERR_BAD_OBJ_DATA);
362 /* Strip off parsed time information, leaving just author and email. */
363 *space = '\0';
365 return NULL;
368 void
369 got_object_commit_close(struct got_commit_object *commit)
371 if (commit->refcnt > 0) {
372 commit->refcnt--;
373 if (commit->refcnt > 0)
374 return;
377 got_object_id_queue_free(&commit->parent_ids);
378 free(commit->tree_id);
379 free(commit->author);
380 free(commit->committer);
381 free(commit->logmsg);
382 free(commit);
385 struct got_object_id *
386 got_object_commit_get_tree_id(struct got_commit_object *commit)
388 return commit->tree_id;
391 int
392 got_object_commit_get_nparents(struct got_commit_object *commit)
394 return commit->nparents;
397 const struct got_object_id_queue *
398 got_object_commit_get_parent_ids(struct got_commit_object *commit)
400 return &commit->parent_ids;
403 const char *
404 got_object_commit_get_author(struct got_commit_object *commit)
406 return commit->author;
409 time_t
410 got_object_commit_get_author_time(struct got_commit_object *commit)
412 return commit->author_time;
415 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
417 return commit->author_gmtoff;
420 const char *
421 got_object_commit_get_committer(struct got_commit_object *commit)
423 return commit->committer;
426 time_t
427 got_object_commit_get_committer_time(struct got_commit_object *commit)
429 return commit->committer_time;
432 time_t
433 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
435 return commit->committer_gmtoff;
438 const struct got_error *
439 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
441 const struct got_error *err = NULL;
442 const char *src;
443 char *dst;
444 size_t len;
446 len = strlen(commit->logmsg);
447 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
448 if (*logmsg == NULL)
449 return got_error_from_errno("malloc");
451 /*
452 * Strip out unusual headers. Headers are separated from the commit
453 * message body by a single empty line.
454 */
455 src = commit->logmsg;
456 dst = *logmsg;
457 while (*src != '\0' && *src != '\n') {
458 int copy_header = 1, eol = 0;
459 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
460 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
461 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
462 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
463 strncmp(src, GOT_COMMIT_LABEL_PARENT,
464 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
465 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
466 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
467 copy_header = 0;
469 while (*src != '\0' && !eol) {
470 if (copy_header) {
471 *dst = *src;
472 dst++;
474 if (*src == '\n')
475 eol = 1;
476 src++;
479 *dst = '\0';
481 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
482 err = got_error(GOT_ERR_NO_SPACE);
483 goto done;
486 /* Trim redundant trailing whitespace. */
487 len = strlen(*logmsg);
488 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
489 isspace((unsigned char)(*logmsg)[len - 1])) {
490 (*logmsg)[len - 1] = '\0';
491 len--;
494 /* Append a trailing newline if missing. */
495 if (len > 0 && (*logmsg)[len - 1] != '\n') {
496 (*logmsg)[len] = '\n';
497 (*logmsg)[len + 1] = '\0';
499 done:
500 if (err) {
501 free(*logmsg);
502 *logmsg = NULL;
504 return err;
507 const char *
508 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
510 return commit->logmsg;
513 const struct got_error *
514 got_object_parse_commit(struct got_commit_object **commit, char *buf,
515 size_t len)
517 const struct got_error *err = NULL;
518 char *s = buf;
519 size_t label_len;
520 ssize_t remain = (ssize_t)len;
522 if (remain == 0)
523 return got_error(GOT_ERR_BAD_OBJ_DATA);
525 *commit = got_object_commit_alloc_partial();
526 if (*commit == NULL)
527 return got_error_from_errno("got_object_commit_alloc_partial");
529 label_len = strlen(GOT_COMMIT_LABEL_TREE);
530 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
531 remain -= label_len;
532 if (remain < SHA1_DIGEST_STRING_LENGTH) {
533 err = got_error(GOT_ERR_BAD_OBJ_DATA);
534 goto done;
536 s += label_len;
537 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
538 err = got_error(GOT_ERR_BAD_OBJ_DATA);
539 goto done;
541 remain -= SHA1_DIGEST_STRING_LENGTH;
542 s += SHA1_DIGEST_STRING_LENGTH;
543 } else {
544 err = got_error(GOT_ERR_BAD_OBJ_DATA);
545 goto done;
548 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
549 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
550 remain -= label_len;
551 if (remain < SHA1_DIGEST_STRING_LENGTH) {
552 err = got_error(GOT_ERR_BAD_OBJ_DATA);
553 goto done;
555 s += label_len;
556 err = got_object_commit_add_parent(*commit, s);
557 if (err)
558 goto done;
560 remain -= SHA1_DIGEST_STRING_LENGTH;
561 s += SHA1_DIGEST_STRING_LENGTH;
564 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
565 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
566 char *p;
567 size_t slen;
569 remain -= label_len;
570 if (remain <= 0) {
571 err = got_error(GOT_ERR_BAD_OBJ_DATA);
572 goto done;
574 s += label_len;
575 p = memchr(s, '\n', remain);
576 if (p == NULL) {
577 err = got_error(GOT_ERR_BAD_OBJ_DATA);
578 goto done;
580 *p = '\0';
581 slen = strlen(s);
582 err = parse_commit_time(&(*commit)->author_time,
583 &(*commit)->author_gmtoff, s);
584 if (err)
585 goto done;
586 (*commit)->author = strdup(s);
587 if ((*commit)->author == NULL) {
588 err = got_error_from_errno("strdup");
589 goto done;
591 s += slen + 1;
592 remain -= slen + 1;
595 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
596 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
597 char *p;
598 size_t slen;
600 remain -= label_len;
601 if (remain <= 0) {
602 err = got_error(GOT_ERR_BAD_OBJ_DATA);
603 goto done;
605 s += label_len;
606 p = memchr(s, '\n', remain);
607 if (p == NULL) {
608 err = got_error(GOT_ERR_BAD_OBJ_DATA);
609 goto done;
611 *p = '\0';
612 slen = strlen(s);
613 err = parse_commit_time(&(*commit)->committer_time,
614 &(*commit)->committer_gmtoff, s);
615 if (err)
616 goto done;
617 (*commit)->committer = strdup(s);
618 if ((*commit)->committer == NULL) {
619 err = got_error_from_errno("strdup");
620 goto done;
622 s += slen + 1;
623 remain -= slen + 1;
626 (*commit)->logmsg = strndup(s, remain);
627 if ((*commit)->logmsg == NULL) {
628 err = got_error_from_errno("strndup");
629 goto done;
631 done:
632 if (err) {
633 got_object_commit_close(*commit);
634 *commit = NULL;
636 return err;
639 void
640 got_object_tree_close(struct got_tree_object *tree)
642 if (tree->refcnt > 0) {
643 tree->refcnt--;
644 if (tree->refcnt > 0)
645 return;
648 free(tree->entries);
649 free(tree);
652 static const struct got_error *
653 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
654 size_t *elen, char *buf,
655 size_t maxlen)
657 char *p, *space;
658 const struct got_error *err = NULL;
660 *name = NULL;
661 *elen = 0;
663 *pte = malloc(sizeof(**pte));
664 if (*pte == NULL)
665 return got_error_from_errno("malloc");
667 *elen = strnlen(buf, maxlen) + 1;
668 if (*elen > maxlen) {
669 free(*pte);
670 *pte = NULL;
671 return got_error(GOT_ERR_BAD_OBJ_DATA);
674 space = memchr(buf, ' ', *elen);
675 if (space == NULL || space <= buf) {
676 err = got_error(GOT_ERR_BAD_OBJ_DATA);
677 free(*pte);
678 *pte = NULL;
679 return err;
681 (*pte)->mode = 0;
682 p = buf;
683 while (p < space) {
684 if (*p < '0' && *p > '7') {
685 err = got_error(GOT_ERR_BAD_OBJ_DATA);
686 goto done;
688 (*pte)->mode <<= 3;
689 (*pte)->mode |= *p - '0';
690 p++;
693 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
694 err = got_error(GOT_ERR_BAD_OBJ_DATA);
695 goto done;
697 *name = space + 1;
698 buf += *elen;
699 (*pte)->id = buf;
700 *elen += SHA1_DIGEST_LENGTH;
701 done:
702 if (err) {
703 free(*pte);
704 *pte = NULL;
706 return err;
709 const struct got_error *
710 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
711 uint8_t *buf, size_t len)
713 const struct got_error *err = NULL;
714 size_t remain = len;
716 *nentries = 0;
717 if (remain == 0)
718 return NULL; /* tree is empty */
720 while (remain > 0) {
721 struct got_parsed_tree_entry *pte;
722 struct got_pathlist_entry *new = NULL;
723 const char *name;
724 size_t elen;
726 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
727 if (err)
728 goto done;
729 err = got_pathlist_insert(&new, entries, name, pte);
730 if (err)
731 goto done;
732 if (new == NULL) {
733 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
734 goto done;
736 buf += elen;
737 remain -= elen;
738 (*nentries)++;
741 if (remain != 0) {
742 err = got_error(GOT_ERR_BAD_OBJ_DATA);
743 goto done;
745 done:
746 if (err) {
747 got_object_parsed_tree_entries_free(entries);
748 *nentries = 0;
750 return err;
753 void
754 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
756 struct got_pathlist_entry *pe;
758 TAILQ_FOREACH(pe, entries, entry) {
759 struct got_parsed_tree_entry *pte = pe->data;
760 free(pte);
762 got_pathlist_free(entries);
765 void
766 got_object_tag_close(struct got_tag_object *tag)
768 if (tag->refcnt > 0) {
769 tag->refcnt--;
770 if (tag->refcnt > 0)
771 return;
774 free(tag->tag);
775 free(tag->tagger);
776 free(tag->tagmsg);
777 free(tag);
780 const struct got_error *
781 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
783 const struct got_error *err = NULL;
784 size_t remain = len;
785 char *s = buf;
786 size_t label_len;
788 if (remain == 0)
789 return got_error(GOT_ERR_BAD_OBJ_DATA);
791 *tag = calloc(1, sizeof(**tag));
792 if (*tag == NULL)
793 return got_error_from_errno("calloc");
795 label_len = strlen(GOT_TAG_LABEL_OBJECT);
796 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
797 remain -= label_len;
798 if (remain < SHA1_DIGEST_STRING_LENGTH) {
799 err = got_error(GOT_ERR_BAD_OBJ_DATA);
800 goto done;
802 s += label_len;
803 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
804 err = got_error(GOT_ERR_BAD_OBJ_DATA);
805 goto done;
807 remain -= SHA1_DIGEST_STRING_LENGTH;
808 s += SHA1_DIGEST_STRING_LENGTH;
809 } else {
810 err = got_error(GOT_ERR_BAD_OBJ_DATA);
811 goto done;
814 if (remain <= 0) {
815 err = got_error(GOT_ERR_BAD_OBJ_DATA);
816 goto done;
819 label_len = strlen(GOT_TAG_LABEL_TYPE);
820 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
821 remain -= label_len;
822 if (remain <= 0) {
823 err = got_error(GOT_ERR_BAD_OBJ_DATA);
824 goto done;
826 s += label_len;
827 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
828 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
829 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
830 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
831 s += label_len;
832 remain -= label_len;
833 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
834 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
835 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
836 label_len = strlen(GOT_OBJ_LABEL_TREE);
837 s += label_len;
838 remain -= label_len;
839 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
840 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
841 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
842 label_len = strlen(GOT_OBJ_LABEL_BLOB);
843 s += label_len;
844 remain -= label_len;
845 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
846 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
847 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
848 label_len = strlen(GOT_OBJ_LABEL_TAG);
849 s += label_len;
850 remain -= label_len;
851 } else {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
853 goto done;
856 if (remain <= 0 || *s != '\n') {
857 err = got_error(GOT_ERR_BAD_OBJ_DATA);
858 goto done;
860 s++;
861 remain--;
862 if (remain <= 0) {
863 err = got_error(GOT_ERR_BAD_OBJ_DATA);
864 goto done;
866 } else {
867 err = got_error(GOT_ERR_BAD_OBJ_DATA);
868 goto done;
871 label_len = strlen(GOT_TAG_LABEL_TAG);
872 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
873 char *p;
874 size_t slen;
875 remain -= label_len;
876 if (remain <= 0) {
877 err = got_error(GOT_ERR_BAD_OBJ_DATA);
878 goto done;
880 s += label_len;
881 p = memchr(s, '\n', remain);
882 if (p == NULL) {
883 err = got_error(GOT_ERR_BAD_OBJ_DATA);
884 goto done;
886 *p = '\0';
887 slen = strlen(s);
888 (*tag)->tag = strndup(s, slen);
889 if ((*tag)->tag == NULL) {
890 err = got_error_from_errno("strndup");
891 goto done;
893 s += slen + 1;
894 remain -= slen + 1;
895 if (remain <= 0) {
896 err = got_error(GOT_ERR_BAD_OBJ_DATA);
897 goto done;
899 } else {
900 err = got_error(GOT_ERR_BAD_OBJ_DATA);
901 goto done;
904 label_len = strlen(GOT_TAG_LABEL_TAGGER);
905 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
906 char *p;
907 size_t slen;
909 remain -= label_len;
910 if (remain <= 0) {
911 err = got_error(GOT_ERR_BAD_OBJ_DATA);
912 goto done;
914 s += label_len;
915 p = memchr(s, '\n', remain);
916 if (p == NULL) {
917 err = got_error(GOT_ERR_BAD_OBJ_DATA);
918 goto done;
920 *p = '\0';
921 slen = strlen(s);
922 err = parse_commit_time(&(*tag)->tagger_time,
923 &(*tag)->tagger_gmtoff, s);
924 if (err)
925 goto done;
926 (*tag)->tagger = strdup(s);
927 if ((*tag)->tagger == NULL) {
928 err = got_error_from_errno("strdup");
929 goto done;
931 s += slen + 1;
932 remain -= slen + 1;
933 if (remain < 0) {
934 err = got_error(GOT_ERR_BAD_OBJ_DATA);
935 goto done;
937 } else {
938 /* Some old tags in the Linux git repo have no tagger. */
939 (*tag)->tagger = strdup("");
940 if ((*tag)->tagger == NULL) {
941 err = got_error_from_errno("strdup");
942 goto done;
946 (*tag)->tagmsg = strndup(s, remain);
947 if ((*tag)->tagmsg == NULL) {
948 err = got_error_from_errno("strndup");
949 goto done;
951 done:
952 if (err) {
953 got_object_tag_close(*tag);
954 *tag = NULL;
956 return err;
959 const struct got_error *
960 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
962 const struct got_error *err = NULL;
963 static const size_t blocksize = 512;
964 size_t n, total, remain;
965 uint8_t *buf;
967 *outbuf = NULL;
968 *outlen = 0;
970 buf = malloc(blocksize);
971 if (buf == NULL)
972 return got_error_from_errno("malloc");
974 remain = blocksize;
975 total = 0;
976 for (;;) {
977 if (remain == 0) {
978 uint8_t *newbuf;
979 newbuf = reallocarray(buf, 1, total + blocksize);
980 if (newbuf == NULL) {
981 err = got_error_from_errno("reallocarray");
982 goto done;
984 buf = newbuf;
985 remain += blocksize;
987 n = fread(buf + total, 1, remain, f);
988 if (n == 0) {
989 if (ferror(f)) {
990 err = got_ferror(f, GOT_ERR_IO);
991 goto done;
993 break; /* EOF */
995 remain -= n;
996 total += n;
997 };
999 done:
1000 if (err == NULL) {
1001 *outbuf = buf;
1002 *outlen = total;
1003 } else
1004 free(buf);
1005 return err;