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/syslimits.h>
23 #include <sys/wait.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <imsg.h>
35 #include <time.h>
36 #include <unistd.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_privsep.h"
52 #include "got_lib_repository.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #endif
58 int
59 got_object_id_cmp(const struct got_object_id *id1,
60 const struct got_object_id *id2)
61 {
62 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
63 }
65 const struct got_error *
66 got_object_qid_alloc_partial(struct got_object_qid **qid)
67 {
68 const struct got_error *err = NULL;
70 *qid = malloc(sizeof(**qid));
71 if (*qid == NULL)
72 return got_error_from_errno("malloc");
74 (*qid)->id = malloc(sizeof(*((*qid)->id)));
75 if ((*qid)->id == NULL) {
76 err = got_error_from_errno("malloc");
77 got_object_qid_free(*qid);
78 *qid = NULL;
79 return err;
80 }
82 return NULL;
83 }
85 const struct got_error *
86 got_object_id_str(char **outbuf, struct got_object_id *id)
87 {
88 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
90 *outbuf = malloc(len);
91 if (*outbuf == NULL)
92 return got_error_from_errno("malloc");
94 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
95 free(*outbuf);
96 *outbuf = NULL;
97 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
98 }
100 return NULL;
103 void
104 got_object_close(struct got_object *obj)
106 if (obj->refcnt > 0) {
107 obj->refcnt--;
108 if (obj->refcnt > 0)
109 return;
112 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
113 struct got_delta *delta;
114 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
115 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
116 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
117 free(delta);
120 free(obj);
123 void
124 got_object_qid_free(struct got_object_qid *qid)
126 free(qid->id);
127 free(qid);
130 void
131 got_object_id_queue_free(struct got_object_id_queue *ids)
133 struct got_object_qid *qid;
135 while (!SIMPLEQ_EMPTY(ids)) {
136 qid = SIMPLEQ_FIRST(ids);
137 SIMPLEQ_REMOVE_HEAD(ids, entry);
138 got_object_qid_free(qid);
142 const struct got_error *
143 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
145 const char *obj_labels[] = {
146 GOT_OBJ_LABEL_COMMIT,
147 GOT_OBJ_LABEL_TREE,
148 GOT_OBJ_LABEL_BLOB,
149 GOT_OBJ_LABEL_TAG,
150 };
151 const int obj_types[] = {
152 GOT_OBJ_TYPE_COMMIT,
153 GOT_OBJ_TYPE_TREE,
154 GOT_OBJ_TYPE_BLOB,
155 GOT_OBJ_TYPE_TAG,
156 };
157 int type = 0;
158 size_t size = 0, hdrlen = 0;
159 int i;
161 *obj = NULL;
163 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
164 if (hdrlen > len)
165 return got_error(GOT_ERR_BAD_OBJ_HDR);
167 for (i = 0; i < nitems(obj_labels); i++) {
168 const char *label = obj_labels[i];
169 size_t label_len = strlen(label);
170 const char *errstr;
172 if (strncmp(buf, label, label_len) != 0)
173 continue;
175 type = obj_types[i];
176 if (len <= label_len)
177 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
179 if (errstr != NULL)
180 return got_error(GOT_ERR_BAD_OBJ_HDR);
181 break;
184 if (type == 0)
185 return got_error(GOT_ERR_BAD_OBJ_HDR);
187 *obj = calloc(1, sizeof(**obj));
188 if (*obj == NULL)
189 return got_error_from_errno("calloc");
190 (*obj)->type = type;
191 (*obj)->hdrlen = hdrlen;
192 (*obj)->size = size;
193 return NULL;
196 const struct got_error *
197 got_object_read_header(struct got_object **obj, int fd)
199 const struct got_error *err;
200 struct got_inflate_buf zb;
201 char *buf;
202 const size_t zbsize = 64;
203 size_t outlen, totlen;
204 int nbuf = 1;
206 *obj = NULL;
208 buf = malloc(zbsize);
209 if (buf == NULL)
210 return got_error_from_errno("malloc");
212 err = got_inflate_init(&zb, buf, zbsize);
213 if (err)
214 return err;
216 totlen = 0;
217 do {
218 err = got_inflate_read_fd(&zb, fd, &outlen);
219 if (err)
220 goto done;
221 if (outlen == 0)
222 break;
223 totlen += outlen;
224 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
225 char *newbuf;
226 nbuf++;
227 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
228 if (newbuf == NULL) {
229 err = got_error_from_errno("recallocarray");
230 goto done;
232 buf = newbuf;
233 zb.outbuf = newbuf + totlen;
234 zb.outlen = (nbuf * zbsize) - totlen;
236 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
238 err = got_object_parse_header(obj, buf, totlen);
239 done:
240 free(buf);
241 got_inflate_end(&zb);
242 return err;
245 struct got_commit_object *
246 got_object_commit_alloc_partial(void)
248 struct got_commit_object *commit;
250 commit = calloc(1, sizeof(*commit));
251 if (commit == NULL)
252 return NULL;
253 commit->tree_id = malloc(sizeof(*commit->tree_id));
254 if (commit->tree_id == NULL) {
255 free(commit);
256 return NULL;
259 SIMPLEQ_INIT(&commit->parent_ids);
261 return commit;
264 const struct got_error *
265 got_object_commit_add_parent(struct got_commit_object *commit,
266 const char *id_str)
268 const struct got_error *err = NULL;
269 struct got_object_qid *qid;
271 err = got_object_qid_alloc_partial(&qid);
272 if (err)
273 return err;
275 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
276 err = got_error(GOT_ERR_BAD_OBJ_DATA);
277 got_object_qid_free(qid);
278 return err;
281 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
282 commit->nparents++;
284 return NULL;
287 static const struct got_error *
288 parse_gmtoff(time_t *gmtoff, const char *tzstr)
290 int sign = 1;
291 const char *p = tzstr;
292 time_t h, m;
294 *gmtoff = 0;
296 if (*p == '-')
297 sign = -1;
298 else if (*p != '+')
299 return got_error(GOT_ERR_BAD_OBJ_DATA);
300 p++;
301 if (!isdigit(*p) && !isdigit(*(p + 1)))
302 return got_error(GOT_ERR_BAD_OBJ_DATA);
303 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
305 p += 2;
306 if (!isdigit(*p) && !isdigit(*(p + 1)))
307 return got_error(GOT_ERR_BAD_OBJ_DATA);
308 m = ((*p - '0') * 10) + (*(p + 1) - '0');
310 *gmtoff = (h * 60 * 60 + m * 60) * sign;
311 return NULL;
314 static const struct got_error *
315 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
317 const struct got_error *err = NULL;
318 const char *errstr;
319 char *space, *tzstr;
321 /* Parse and strip off trailing timezone indicator string. */
322 space = strrchr(committer, ' ');
323 if (space == NULL)
324 return got_error(GOT_ERR_BAD_OBJ_DATA);
325 tzstr = strdup(space + 1);
326 if (tzstr == NULL)
327 return got_error_from_errno("strdup");
328 err = parse_gmtoff(gmtoff, tzstr);
329 free(tzstr);
330 if (err)
331 return err;
332 *space = '\0';
334 /* Timestamp is separated from committer name + email by space. */
335 space = strrchr(committer, ' ');
336 if (space == NULL)
337 return got_error(GOT_ERR_BAD_OBJ_DATA);
339 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
340 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
341 if (errstr)
342 return got_error(GOT_ERR_BAD_OBJ_DATA);
344 /* Strip off parsed time information, leaving just author and email. */
345 *space = '\0';
347 return NULL;
350 void
351 got_object_commit_close(struct got_commit_object *commit)
353 if (commit->refcnt > 0) {
354 commit->refcnt--;
355 if (commit->refcnt > 0)
356 return;
359 got_object_id_queue_free(&commit->parent_ids);
360 free(commit->tree_id);
361 free(commit->author);
362 free(commit->committer);
363 free(commit->logmsg);
364 free(commit);
367 struct got_object_id *
368 got_object_commit_get_tree_id(struct got_commit_object *commit)
370 return commit->tree_id;
373 int
374 got_object_commit_get_nparents(struct got_commit_object *commit)
376 return commit->nparents;
379 const struct got_object_id_queue *
380 got_object_commit_get_parent_ids(struct got_commit_object *commit)
382 return &commit->parent_ids;
385 const char *
386 got_object_commit_get_author(struct got_commit_object *commit)
388 return commit->author;
391 time_t
392 got_object_commit_get_author_time(struct got_commit_object *commit)
394 return commit->author_time;
397 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
399 return commit->author_gmtoff;
402 const char *
403 got_object_commit_get_committer(struct got_commit_object *commit)
405 return commit->committer;
408 time_t
409 got_object_commit_get_committer_time(struct got_commit_object *commit)
411 return commit->committer_time;
414 time_t
415 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
417 return commit->committer_gmtoff;
420 const struct got_error *
421 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
423 const struct got_error *err = NULL;
424 char *msg0, *msg, *line, *s;
425 size_t len;
426 int headers = 1;
428 *logmsg = NULL;
430 msg0 = strdup(commit->logmsg);
431 if (msg0 == NULL)
432 return got_error_from_errno("strdup");
434 /* Copy log message line by line to strip out unusual headers... */
435 msg = msg0;
436 do {
437 if ((line = strsep(&msg, "\n")) == NULL)
438 break;
440 if (headers == 1) {
441 if (line[0] != '\0' &&
442 strncmp(line, GOT_COMMIT_LABEL_TREE,
443 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
444 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
445 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
446 strncmp(line, GOT_COMMIT_LABEL_PARENT,
447 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
448 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
449 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
450 continue;
452 if (line[0] == '\0')
453 headers = 0;
456 if (asprintf(&s, "%s%s\n",
457 *logmsg ? *logmsg : "", line) == -1) {
458 err = got_error_from_errno("asprintf");
459 goto done;
461 free(*logmsg);
462 *logmsg = s;
464 } while (line);
466 /* Trim redundant trailing whitespace. */
467 len = strlen(*logmsg);
468 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
469 isspace((unsigned char)(*logmsg)[len - 1])) {
470 (*logmsg)[len - 1] = '\0';
471 len--;
473 done:
474 free(msg0);
475 if (err) {
476 free(*logmsg);
477 *logmsg = NULL;
479 return err;
482 const char *
483 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
485 return commit->logmsg;
488 const struct got_error *
489 got_object_parse_commit(struct got_commit_object **commit, char *buf,
490 size_t len)
492 const struct got_error *err = NULL;
493 char *s = buf;
494 size_t label_len;
495 ssize_t remain = (ssize_t)len;
497 if (remain == 0)
498 return got_error(GOT_ERR_BAD_OBJ_DATA);
500 *commit = got_object_commit_alloc_partial();
501 if (*commit == NULL)
502 return got_error_from_errno("got_object_commit_alloc_partial");
504 label_len = strlen(GOT_COMMIT_LABEL_TREE);
505 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
506 remain -= label_len;
507 if (remain < SHA1_DIGEST_STRING_LENGTH) {
508 err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 goto done;
511 s += label_len;
512 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
513 err = got_error(GOT_ERR_BAD_OBJ_DATA);
514 goto done;
516 remain -= SHA1_DIGEST_STRING_LENGTH;
517 s += SHA1_DIGEST_STRING_LENGTH;
518 } else {
519 err = got_error(GOT_ERR_BAD_OBJ_DATA);
520 goto done;
523 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
524 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
525 remain -= label_len;
526 if (remain < SHA1_DIGEST_STRING_LENGTH) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 goto done;
530 s += label_len;
531 err = got_object_commit_add_parent(*commit, s);
532 if (err)
533 goto done;
535 remain -= SHA1_DIGEST_STRING_LENGTH;
536 s += SHA1_DIGEST_STRING_LENGTH;
539 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
540 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
541 char *p;
542 size_t slen;
544 remain -= label_len;
545 if (remain <= 0) {
546 err = got_error(GOT_ERR_BAD_OBJ_DATA);
547 goto done;
549 s += label_len;
550 p = memchr(s, '\n', remain);
551 if (p == NULL) {
552 err = got_error(GOT_ERR_BAD_OBJ_DATA);
553 goto done;
555 *p = '\0';
556 slen = strlen(s);
557 err = parse_commit_time(&(*commit)->author_time,
558 &(*commit)->author_gmtoff, s);
559 if (err)
560 goto done;
561 (*commit)->author = strdup(s);
562 if ((*commit)->author == NULL) {
563 err = got_error_from_errno("strdup");
564 goto done;
566 s += slen + 1;
567 remain -= slen + 1;
570 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
571 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
572 char *p;
573 size_t slen;
575 remain -= label_len;
576 if (remain <= 0) {
577 err = got_error(GOT_ERR_BAD_OBJ_DATA);
578 goto done;
580 s += label_len;
581 p = memchr(s, '\n', remain);
582 if (p == NULL) {
583 err = got_error(GOT_ERR_BAD_OBJ_DATA);
584 goto done;
586 *p = '\0';
587 slen = strlen(s);
588 err = parse_commit_time(&(*commit)->committer_time,
589 &(*commit)->committer_gmtoff, s);
590 if (err)
591 goto done;
592 (*commit)->committer = strdup(s);
593 if ((*commit)->committer == NULL) {
594 err = got_error_from_errno("strdup");
595 goto done;
597 s += slen + 1;
598 remain -= slen + 1;
601 (*commit)->logmsg = strndup(s, remain);
602 if ((*commit)->logmsg == NULL) {
603 err = got_error_from_errno("strndup");
604 goto done;
606 done:
607 if (err) {
608 got_object_commit_close(*commit);
609 *commit = NULL;
611 return err;
614 void
615 got_object_tree_close(struct got_tree_object *tree)
617 if (tree->refcnt > 0) {
618 tree->refcnt--;
619 if (tree->refcnt > 0)
620 return;
623 free(tree->entries);
624 free(tree);
627 static const struct got_error *
628 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
629 size_t *elen, char *buf,
630 size_t maxlen)
632 char *p, *space;
633 const struct got_error *err = NULL;
635 *name = NULL;
636 *elen = 0;
638 *pte = malloc(sizeof(**pte));
639 if (*pte == NULL)
640 return got_error_from_errno("malloc");
642 *elen = strnlen(buf, maxlen) + 1;
643 if (*elen > maxlen) {
644 free(*pte);
645 *pte = NULL;
646 return got_error(GOT_ERR_BAD_OBJ_DATA);
649 space = memchr(buf, ' ', *elen);
650 if (space == NULL || space <= buf) {
651 err = got_error(GOT_ERR_BAD_OBJ_DATA);
652 free(*pte);
653 *pte = NULL;
654 return err;
656 (*pte)->mode = 0;
657 p = buf;
658 while (p < space) {
659 if (*p < '0' && *p > '7') {
660 err = got_error(GOT_ERR_BAD_OBJ_DATA);
661 goto done;
663 (*pte)->mode <<= 3;
664 (*pte)->mode |= *p - '0';
665 p++;
668 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
669 err = got_error(GOT_ERR_BAD_OBJ_DATA);
670 goto done;
672 *name = space + 1;
673 buf += *elen;
674 (*pte)->id = buf;
675 *elen += SHA1_DIGEST_LENGTH;
676 done:
677 if (err) {
678 free(*pte);
679 *pte = NULL;
681 return err;
684 const struct got_error *
685 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
686 uint8_t *buf, size_t len)
688 const struct got_error *err = NULL;
689 size_t remain = len;
691 *nentries = 0;
692 if (remain == 0)
693 return NULL; /* tree is empty */
695 while (remain > 0) {
696 struct got_parsed_tree_entry *pte;
697 struct got_pathlist_entry *new = NULL;
698 const char *name;
699 size_t elen;
701 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
702 if (err)
703 goto done;
704 err = got_pathlist_insert(&new, entries, name, pte);
705 if (err)
706 goto done;
707 if (new == NULL) {
708 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
709 goto done;
711 buf += elen;
712 remain -= elen;
713 (*nentries)++;
716 if (remain != 0) {
717 err = got_error(GOT_ERR_BAD_OBJ_DATA);
718 goto done;
720 done:
721 if (err) {
722 got_object_parsed_tree_entries_free(entries);
723 *nentries = 0;
725 return err;
728 void
729 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
731 struct got_pathlist_entry *pe;
733 TAILQ_FOREACH(pe, entries, entry) {
734 struct got_parsed_tree_entry *pte = pe->data;
735 free(pte);
737 got_pathlist_free(entries);
740 void
741 got_object_tag_close(struct got_tag_object *tag)
743 if (tag->refcnt > 0) {
744 tag->refcnt--;
745 if (tag->refcnt > 0)
746 return;
749 free(tag->tag);
750 free(tag->tagger);
751 free(tag->tagmsg);
752 free(tag);
755 const struct got_error *
756 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
758 const struct got_error *err = NULL;
759 size_t remain = len;
760 char *s = buf;
761 size_t label_len;
763 if (remain == 0)
764 return got_error(GOT_ERR_BAD_OBJ_DATA);
766 *tag = calloc(1, sizeof(**tag));
767 if (*tag == NULL)
768 return got_error_from_errno("calloc");
770 label_len = strlen(GOT_TAG_LABEL_OBJECT);
771 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
772 remain -= label_len;
773 if (remain < SHA1_DIGEST_STRING_LENGTH) {
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
775 goto done;
777 s += label_len;
778 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
779 err = got_error(GOT_ERR_BAD_OBJ_DATA);
780 goto done;
782 remain -= SHA1_DIGEST_STRING_LENGTH;
783 s += SHA1_DIGEST_STRING_LENGTH;
784 } else {
785 err = got_error(GOT_ERR_BAD_OBJ_DATA);
786 goto done;
789 if (remain <= 0) {
790 err = got_error(GOT_ERR_BAD_OBJ_DATA);
791 goto done;
794 label_len = strlen(GOT_TAG_LABEL_TYPE);
795 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
796 remain -= label_len;
797 if (remain <= 0) {
798 err = got_error(GOT_ERR_BAD_OBJ_DATA);
799 goto done;
801 s += label_len;
802 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
803 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
804 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
805 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
806 s += label_len;
807 remain -= label_len;
808 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
809 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
810 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
811 label_len = strlen(GOT_OBJ_LABEL_TREE);
812 s += label_len;
813 remain -= label_len;
814 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
815 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
816 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
817 label_len = strlen(GOT_OBJ_LABEL_BLOB);
818 s += label_len;
819 remain -= label_len;
820 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
821 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
822 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
823 label_len = strlen(GOT_OBJ_LABEL_TAG);
824 s += label_len;
825 remain -= label_len;
826 } else {
827 err = got_error(GOT_ERR_BAD_OBJ_DATA);
828 goto done;
831 if (remain <= 0 || *s != '\n') {
832 err = got_error(GOT_ERR_BAD_OBJ_DATA);
833 goto done;
835 s++;
836 remain--;
837 if (remain <= 0) {
838 err = got_error(GOT_ERR_BAD_OBJ_DATA);
839 goto done;
841 } else {
842 err = got_error(GOT_ERR_BAD_OBJ_DATA);
843 goto done;
846 label_len = strlen(GOT_TAG_LABEL_TAG);
847 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
848 char *p;
849 size_t slen;
850 remain -= label_len;
851 if (remain <= 0) {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
853 goto done;
855 s += label_len;
856 p = memchr(s, '\n', remain);
857 if (p == NULL) {
858 err = got_error(GOT_ERR_BAD_OBJ_DATA);
859 goto done;
861 *p = '\0';
862 slen = strlen(s);
863 (*tag)->tag = strndup(s, slen);
864 if ((*tag)->tag == NULL) {
865 err = got_error_from_errno("strndup");
866 goto done;
868 s += slen + 1;
869 remain -= slen + 1;
870 if (remain <= 0) {
871 err = got_error(GOT_ERR_BAD_OBJ_DATA);
872 goto done;
874 } else {
875 err = got_error(GOT_ERR_BAD_OBJ_DATA);
876 goto done;
879 label_len = strlen(GOT_TAG_LABEL_TAGGER);
880 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
881 char *p;
882 size_t slen;
884 remain -= label_len;
885 if (remain <= 0) {
886 err = got_error(GOT_ERR_BAD_OBJ_DATA);
887 goto done;
889 s += label_len;
890 p = memchr(s, '\n', remain);
891 if (p == NULL) {
892 err = got_error(GOT_ERR_BAD_OBJ_DATA);
893 goto done;
895 *p = '\0';
896 slen = strlen(s);
897 err = parse_commit_time(&(*tag)->tagger_time,
898 &(*tag)->tagger_gmtoff, s);
899 if (err)
900 goto done;
901 (*tag)->tagger = strdup(s);
902 if ((*tag)->tagger == NULL) {
903 err = got_error_from_errno("strdup");
904 goto done;
906 s += slen + 1;
907 remain -= slen + 1;
908 if (remain <= 0) {
909 err = got_error(GOT_ERR_BAD_OBJ_DATA);
910 goto done;
912 } else {
913 /* Some old tags in the Linux git repo have no tagger. */
914 (*tag)->tagger = strdup("");
915 if ((*tag)->tagger == NULL) {
916 err = got_error_from_errno("strdup");
917 goto done;
921 (*tag)->tagmsg = strndup(s, remain);
922 if ((*tag)->tagmsg == NULL) {
923 err = got_error_from_errno("strndup");
924 goto done;
926 done:
927 if (err) {
928 got_object_tag_close(*tag);
929 *tag = NULL;
931 return err;
934 const struct got_error *
935 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
937 const struct got_error *err = NULL;
938 static const size_t blocksize = 512;
939 size_t n, total, remain;
940 uint8_t *buf;
942 *outbuf = NULL;
943 *outlen = 0;
945 buf = malloc(blocksize);
946 if (buf == NULL)
947 return got_error_from_errno("malloc");
949 remain = blocksize;
950 total = 0;
951 for (;;) {
952 if (remain == 0) {
953 uint8_t *newbuf;
954 newbuf = reallocarray(buf, 1, total + blocksize);
955 if (newbuf == NULL) {
956 err = got_error_from_errno("reallocarray");
957 goto done;
959 buf = newbuf;
960 remain += blocksize;
962 n = fread(buf + total, 1, remain, f);
963 if (n == 0) {
964 if (ferror(f)) {
965 err = got_ferror(f, GOT_ERR_IO);
966 goto done;
968 break; /* EOF */
970 remain -= n;
971 total += n;
972 };
974 done:
975 if (err == NULL) {
976 *outbuf = buf;
977 *outlen = total;
978 } else
979 free(buf);
980 return err;