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 (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
127 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
128 SIMPLEQ_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 (!SIMPLEQ_EMPTY(ids)) {
148 qid = SIMPLEQ_FIRST(ids);
149 SIMPLEQ_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 SIMPLEQ_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 SIMPLEQ_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 char *msg0, *msg, *line, *s;
443 size_t len;
444 int headers = 1;
446 *logmsg = NULL;
448 msg0 = strdup(commit->logmsg);
449 if (msg0 == NULL)
450 return got_error_from_errno("strdup");
452 /* Copy log message line by line to strip out unusual headers... */
453 msg = msg0;
454 do {
455 if ((line = strsep(&msg, "\n")) == NULL)
456 break;
458 if (headers == 1) {
459 if (line[0] != '\0' &&
460 strncmp(line, GOT_COMMIT_LABEL_TREE,
461 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
462 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
463 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
464 strncmp(line, GOT_COMMIT_LABEL_PARENT,
465 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
466 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
467 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
468 continue;
470 if (line[0] == '\0')
471 headers = 0;
474 if (asprintf(&s, "%s%s\n",
475 *logmsg ? *logmsg : "", line) == -1) {
476 err = got_error_from_errno("asprintf");
477 goto done;
479 free(*logmsg);
480 *logmsg = s;
482 } while (line);
484 if (*logmsg == NULL) {
485 /* log message does not contain \n */
486 *logmsg = strdup(commit->logmsg);
487 if (*logmsg == NULL) {
488 err = got_error_from_errno("strdup");
489 goto done;
493 /* Trim redundant trailing whitespace. */
494 len = strlen(*logmsg);
495 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
496 isspace((unsigned char)(*logmsg)[len - 1])) {
497 (*logmsg)[len - 1] = '\0';
498 len--;
500 done:
501 free(msg0);
502 if (err) {
503 free(*logmsg);
504 *logmsg = NULL;
506 return err;
509 const char *
510 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
512 return commit->logmsg;
515 const struct got_error *
516 got_object_parse_commit(struct got_commit_object **commit, char *buf,
517 size_t len)
519 const struct got_error *err = NULL;
520 char *s = buf;
521 size_t label_len;
522 ssize_t remain = (ssize_t)len;
524 if (remain == 0)
525 return got_error(GOT_ERR_BAD_OBJ_DATA);
527 *commit = got_object_commit_alloc_partial();
528 if (*commit == NULL)
529 return got_error_from_errno("got_object_commit_alloc_partial");
531 label_len = strlen(GOT_COMMIT_LABEL_TREE);
532 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
533 remain -= label_len;
534 if (remain < SHA1_DIGEST_STRING_LENGTH) {
535 err = got_error(GOT_ERR_BAD_OBJ_DATA);
536 goto done;
538 s += label_len;
539 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
540 err = got_error(GOT_ERR_BAD_OBJ_DATA);
541 goto done;
543 remain -= SHA1_DIGEST_STRING_LENGTH;
544 s += SHA1_DIGEST_STRING_LENGTH;
545 } else {
546 err = got_error(GOT_ERR_BAD_OBJ_DATA);
547 goto done;
550 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
551 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
552 remain -= label_len;
553 if (remain < SHA1_DIGEST_STRING_LENGTH) {
554 err = got_error(GOT_ERR_BAD_OBJ_DATA);
555 goto done;
557 s += label_len;
558 err = got_object_commit_add_parent(*commit, s);
559 if (err)
560 goto done;
562 remain -= SHA1_DIGEST_STRING_LENGTH;
563 s += SHA1_DIGEST_STRING_LENGTH;
566 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
567 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
568 char *p;
569 size_t slen;
571 remain -= label_len;
572 if (remain <= 0) {
573 err = got_error(GOT_ERR_BAD_OBJ_DATA);
574 goto done;
576 s += label_len;
577 p = memchr(s, '\n', remain);
578 if (p == NULL) {
579 err = got_error(GOT_ERR_BAD_OBJ_DATA);
580 goto done;
582 *p = '\0';
583 slen = strlen(s);
584 err = parse_commit_time(&(*commit)->author_time,
585 &(*commit)->author_gmtoff, s);
586 if (err)
587 goto done;
588 (*commit)->author = strdup(s);
589 if ((*commit)->author == NULL) {
590 err = got_error_from_errno("strdup");
591 goto done;
593 s += slen + 1;
594 remain -= slen + 1;
597 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
598 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
599 char *p;
600 size_t slen;
602 remain -= label_len;
603 if (remain <= 0) {
604 err = got_error(GOT_ERR_BAD_OBJ_DATA);
605 goto done;
607 s += label_len;
608 p = memchr(s, '\n', remain);
609 if (p == NULL) {
610 err = got_error(GOT_ERR_BAD_OBJ_DATA);
611 goto done;
613 *p = '\0';
614 slen = strlen(s);
615 err = parse_commit_time(&(*commit)->committer_time,
616 &(*commit)->committer_gmtoff, s);
617 if (err)
618 goto done;
619 (*commit)->committer = strdup(s);
620 if ((*commit)->committer == NULL) {
621 err = got_error_from_errno("strdup");
622 goto done;
624 s += slen + 1;
625 remain -= slen + 1;
628 (*commit)->logmsg = strndup(s, remain);
629 if ((*commit)->logmsg == NULL) {
630 err = got_error_from_errno("strndup");
631 goto done;
633 done:
634 if (err) {
635 got_object_commit_close(*commit);
636 *commit = NULL;
638 return err;
641 void
642 got_object_tree_close(struct got_tree_object *tree)
644 if (tree->refcnt > 0) {
645 tree->refcnt--;
646 if (tree->refcnt > 0)
647 return;
650 free(tree->entries);
651 free(tree);
654 static const struct got_error *
655 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
656 size_t *elen, char *buf,
657 size_t maxlen)
659 char *p, *space;
660 const struct got_error *err = NULL;
662 *name = NULL;
663 *elen = 0;
665 *pte = malloc(sizeof(**pte));
666 if (*pte == NULL)
667 return got_error_from_errno("malloc");
669 *elen = strnlen(buf, maxlen) + 1;
670 if (*elen > maxlen) {
671 free(*pte);
672 *pte = NULL;
673 return got_error(GOT_ERR_BAD_OBJ_DATA);
676 space = memchr(buf, ' ', *elen);
677 if (space == NULL || space <= buf) {
678 err = got_error(GOT_ERR_BAD_OBJ_DATA);
679 free(*pte);
680 *pte = NULL;
681 return err;
683 (*pte)->mode = 0;
684 p = buf;
685 while (p < space) {
686 if (*p < '0' && *p > '7') {
687 err = got_error(GOT_ERR_BAD_OBJ_DATA);
688 goto done;
690 (*pte)->mode <<= 3;
691 (*pte)->mode |= *p - '0';
692 p++;
695 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
697 goto done;
699 *name = space + 1;
700 buf += *elen;
701 (*pte)->id = buf;
702 *elen += SHA1_DIGEST_LENGTH;
703 done:
704 if (err) {
705 free(*pte);
706 *pte = NULL;
708 return err;
711 const struct got_error *
712 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
713 uint8_t *buf, size_t len)
715 const struct got_error *err = NULL;
716 size_t remain = len;
718 *nentries = 0;
719 if (remain == 0)
720 return NULL; /* tree is empty */
722 while (remain > 0) {
723 struct got_parsed_tree_entry *pte;
724 struct got_pathlist_entry *new = NULL;
725 const char *name;
726 size_t elen;
728 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
729 if (err)
730 goto done;
731 err = got_pathlist_insert(&new, entries, name, pte);
732 if (err)
733 goto done;
734 if (new == NULL) {
735 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
736 goto done;
738 buf += elen;
739 remain -= elen;
740 (*nentries)++;
743 if (remain != 0) {
744 err = got_error(GOT_ERR_BAD_OBJ_DATA);
745 goto done;
747 done:
748 if (err) {
749 got_object_parsed_tree_entries_free(entries);
750 *nentries = 0;
752 return err;
755 void
756 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
758 struct got_pathlist_entry *pe;
760 TAILQ_FOREACH(pe, entries, entry) {
761 struct got_parsed_tree_entry *pte = pe->data;
762 free(pte);
764 got_pathlist_free(entries);
767 void
768 got_object_tag_close(struct got_tag_object *tag)
770 if (tag->refcnt > 0) {
771 tag->refcnt--;
772 if (tag->refcnt > 0)
773 return;
776 free(tag->tag);
777 free(tag->tagger);
778 free(tag->tagmsg);
779 free(tag);
782 const struct got_error *
783 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
785 const struct got_error *err = NULL;
786 size_t remain = len;
787 char *s = buf;
788 size_t label_len;
790 if (remain == 0)
791 return got_error(GOT_ERR_BAD_OBJ_DATA);
793 *tag = calloc(1, sizeof(**tag));
794 if (*tag == NULL)
795 return got_error_from_errno("calloc");
797 label_len = strlen(GOT_TAG_LABEL_OBJECT);
798 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
799 remain -= label_len;
800 if (remain < SHA1_DIGEST_STRING_LENGTH) {
801 err = got_error(GOT_ERR_BAD_OBJ_DATA);
802 goto done;
804 s += label_len;
805 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
806 err = got_error(GOT_ERR_BAD_OBJ_DATA);
807 goto done;
809 remain -= SHA1_DIGEST_STRING_LENGTH;
810 s += SHA1_DIGEST_STRING_LENGTH;
811 } else {
812 err = got_error(GOT_ERR_BAD_OBJ_DATA);
813 goto done;
816 if (remain <= 0) {
817 err = got_error(GOT_ERR_BAD_OBJ_DATA);
818 goto done;
821 label_len = strlen(GOT_TAG_LABEL_TYPE);
822 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
823 remain -= label_len;
824 if (remain <= 0) {
825 err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 goto done;
828 s += label_len;
829 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
830 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
831 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
832 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
833 s += label_len;
834 remain -= label_len;
835 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
836 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
837 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
838 label_len = strlen(GOT_OBJ_LABEL_TREE);
839 s += label_len;
840 remain -= label_len;
841 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
842 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
843 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
844 label_len = strlen(GOT_OBJ_LABEL_BLOB);
845 s += label_len;
846 remain -= label_len;
847 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
848 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
849 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
850 label_len = strlen(GOT_OBJ_LABEL_TAG);
851 s += label_len;
852 remain -= label_len;
853 } else {
854 err = got_error(GOT_ERR_BAD_OBJ_DATA);
855 goto done;
858 if (remain <= 0 || *s != '\n') {
859 err = got_error(GOT_ERR_BAD_OBJ_DATA);
860 goto done;
862 s++;
863 remain--;
864 if (remain <= 0) {
865 err = got_error(GOT_ERR_BAD_OBJ_DATA);
866 goto done;
868 } else {
869 err = got_error(GOT_ERR_BAD_OBJ_DATA);
870 goto done;
873 label_len = strlen(GOT_TAG_LABEL_TAG);
874 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
875 char *p;
876 size_t slen;
877 remain -= label_len;
878 if (remain <= 0) {
879 err = got_error(GOT_ERR_BAD_OBJ_DATA);
880 goto done;
882 s += label_len;
883 p = memchr(s, '\n', remain);
884 if (p == NULL) {
885 err = got_error(GOT_ERR_BAD_OBJ_DATA);
886 goto done;
888 *p = '\0';
889 slen = strlen(s);
890 (*tag)->tag = strndup(s, slen);
891 if ((*tag)->tag == NULL) {
892 err = got_error_from_errno("strndup");
893 goto done;
895 s += slen + 1;
896 remain -= slen + 1;
897 if (remain <= 0) {
898 err = got_error(GOT_ERR_BAD_OBJ_DATA);
899 goto done;
901 } else {
902 err = got_error(GOT_ERR_BAD_OBJ_DATA);
903 goto done;
906 label_len = strlen(GOT_TAG_LABEL_TAGGER);
907 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
908 char *p;
909 size_t slen;
911 remain -= label_len;
912 if (remain <= 0) {
913 err = got_error(GOT_ERR_BAD_OBJ_DATA);
914 goto done;
916 s += label_len;
917 p = memchr(s, '\n', remain);
918 if (p == NULL) {
919 err = got_error(GOT_ERR_BAD_OBJ_DATA);
920 goto done;
922 *p = '\0';
923 slen = strlen(s);
924 err = parse_commit_time(&(*tag)->tagger_time,
925 &(*tag)->tagger_gmtoff, s);
926 if (err)
927 goto done;
928 (*tag)->tagger = strdup(s);
929 if ((*tag)->tagger == NULL) {
930 err = got_error_from_errno("strdup");
931 goto done;
933 s += slen + 1;
934 remain -= slen + 1;
935 if (remain < 0) {
936 err = got_error(GOT_ERR_BAD_OBJ_DATA);
937 goto done;
939 } else {
940 /* Some old tags in the Linux git repo have no tagger. */
941 (*tag)->tagger = strdup("");
942 if ((*tag)->tagger == NULL) {
943 err = got_error_from_errno("strdup");
944 goto done;
948 (*tag)->tagmsg = strndup(s, remain);
949 if ((*tag)->tagmsg == NULL) {
950 err = got_error_from_errno("strndup");
951 goto done;
953 done:
954 if (err) {
955 got_object_tag_close(*tag);
956 *tag = NULL;
958 return err;
961 const struct got_error *
962 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
964 const struct got_error *err = NULL;
965 static const size_t blocksize = 512;
966 size_t n, total, remain;
967 uint8_t *buf;
969 *outbuf = NULL;
970 *outlen = 0;
972 buf = malloc(blocksize);
973 if (buf == NULL)
974 return got_error_from_errno("malloc");
976 remain = blocksize;
977 total = 0;
978 for (;;) {
979 if (remain == 0) {
980 uint8_t *newbuf;
981 newbuf = reallocarray(buf, 1, total + blocksize);
982 if (newbuf == NULL) {
983 err = got_error_from_errno("reallocarray");
984 goto done;
986 buf = newbuf;
987 remain += blocksize;
989 n = fread(buf + total, 1, remain, f);
990 if (n == 0) {
991 if (ferror(f)) {
992 err = got_ferror(f, GOT_ERR_IO);
993 goto done;
995 break; /* EOF */
997 remain -= n;
998 total += n;
999 };
1001 done:
1002 if (err == NULL) {
1003 *outbuf = buf;
1004 *outlen = total;
1005 } else
1006 free(buf);
1007 return err;