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 struct got_object_id *
59 got_object_id_dup(struct got_object_id *id1)
60 {
61 struct got_object_id *id2;
63 id2 = malloc(sizeof(*id2));
64 if (id2 == NULL)
65 return NULL;
66 memcpy(id2, id1, sizeof(*id2));
67 return id2;
68 }
70 int
71 got_object_id_cmp(const struct got_object_id *id1,
72 const struct got_object_id *id2)
73 {
74 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
75 }
77 const struct got_error *
78 got_object_qid_alloc_partial(struct got_object_qid **qid)
79 {
80 const struct got_error *err = NULL;
82 *qid = malloc(sizeof(**qid));
83 if (*qid == NULL)
84 return got_error_from_errno("malloc");
86 (*qid)->id = malloc(sizeof(*((*qid)->id)));
87 if ((*qid)->id == NULL) {
88 err = got_error_from_errno("malloc");
89 got_object_qid_free(*qid);
90 *qid = NULL;
91 return err;
92 }
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 int 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);
225 if (err)
226 return err;
228 totlen = 0;
229 do {
230 err = got_inflate_read_fd(&zb, fd, &outlen);
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 return err;
344 *space = '\0';
346 /* Timestamp is separated from committer name + email by space. */
347 space = strrchr(committer, ' ');
348 if (space == NULL)
349 return got_error(GOT_ERR_BAD_OBJ_DATA);
351 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
352 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
353 if (errstr)
354 return got_error(GOT_ERR_BAD_OBJ_DATA);
356 /* Strip off parsed time information, leaving just author and email. */
357 *space = '\0';
359 return NULL;
362 void
363 got_object_commit_close(struct got_commit_object *commit)
365 if (commit->refcnt > 0) {
366 commit->refcnt--;
367 if (commit->refcnt > 0)
368 return;
371 got_object_id_queue_free(&commit->parent_ids);
372 free(commit->tree_id);
373 free(commit->author);
374 free(commit->committer);
375 free(commit->logmsg);
376 free(commit);
379 struct got_object_id *
380 got_object_commit_get_tree_id(struct got_commit_object *commit)
382 return commit->tree_id;
385 int
386 got_object_commit_get_nparents(struct got_commit_object *commit)
388 return commit->nparents;
391 const struct got_object_id_queue *
392 got_object_commit_get_parent_ids(struct got_commit_object *commit)
394 return &commit->parent_ids;
397 const char *
398 got_object_commit_get_author(struct got_commit_object *commit)
400 return commit->author;
403 time_t
404 got_object_commit_get_author_time(struct got_commit_object *commit)
406 return commit->author_time;
409 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
411 return commit->author_gmtoff;
414 const char *
415 got_object_commit_get_committer(struct got_commit_object *commit)
417 return commit->committer;
420 time_t
421 got_object_commit_get_committer_time(struct got_commit_object *commit)
423 return commit->committer_time;
426 time_t
427 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
429 return commit->committer_gmtoff;
432 const struct got_error *
433 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
435 const struct got_error *err = NULL;
436 char *msg0, *msg, *line, *s;
437 size_t len;
438 int headers = 1;
440 *logmsg = NULL;
442 msg0 = strdup(commit->logmsg);
443 if (msg0 == NULL)
444 return got_error_from_errno("strdup");
446 /* Copy log message line by line to strip out unusual headers... */
447 msg = msg0;
448 do {
449 if ((line = strsep(&msg, "\n")) == NULL)
450 break;
452 if (headers == 1) {
453 if (line[0] != '\0' &&
454 strncmp(line, GOT_COMMIT_LABEL_TREE,
455 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
456 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
457 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
458 strncmp(line, GOT_COMMIT_LABEL_PARENT,
459 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
460 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
461 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
462 continue;
464 if (line[0] == '\0')
465 headers = 0;
468 if (asprintf(&s, "%s%s\n",
469 *logmsg ? *logmsg : "", line) == -1) {
470 err = got_error_from_errno("asprintf");
471 goto done;
473 free(*logmsg);
474 *logmsg = s;
476 } while (line);
478 /* Trim redundant trailing whitespace. */
479 len = strlen(*logmsg);
480 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
481 isspace((unsigned char)(*logmsg)[len - 1])) {
482 (*logmsg)[len - 1] = '\0';
483 len--;
485 done:
486 free(msg0);
487 if (err) {
488 free(*logmsg);
489 *logmsg = NULL;
491 return err;
494 const char *
495 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
497 return commit->logmsg;
500 const struct got_error *
501 got_object_parse_commit(struct got_commit_object **commit, char *buf,
502 size_t len)
504 const struct got_error *err = NULL;
505 char *s = buf;
506 size_t label_len;
507 ssize_t remain = (ssize_t)len;
509 if (remain == 0)
510 return got_error(GOT_ERR_BAD_OBJ_DATA);
512 *commit = got_object_commit_alloc_partial();
513 if (*commit == NULL)
514 return got_error_from_errno("got_object_commit_alloc_partial");
516 label_len = strlen(GOT_COMMIT_LABEL_TREE);
517 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
518 remain -= label_len;
519 if (remain < SHA1_DIGEST_STRING_LENGTH) {
520 err = got_error(GOT_ERR_BAD_OBJ_DATA);
521 goto done;
523 s += label_len;
524 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
525 err = got_error(GOT_ERR_BAD_OBJ_DATA);
526 goto done;
528 remain -= SHA1_DIGEST_STRING_LENGTH;
529 s += SHA1_DIGEST_STRING_LENGTH;
530 } else {
531 err = got_error(GOT_ERR_BAD_OBJ_DATA);
532 goto done;
535 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
536 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
537 remain -= label_len;
538 if (remain < SHA1_DIGEST_STRING_LENGTH) {
539 err = got_error(GOT_ERR_BAD_OBJ_DATA);
540 goto done;
542 s += label_len;
543 err = got_object_commit_add_parent(*commit, s);
544 if (err)
545 goto done;
547 remain -= SHA1_DIGEST_STRING_LENGTH;
548 s += SHA1_DIGEST_STRING_LENGTH;
551 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
552 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
553 char *p;
554 size_t slen;
556 remain -= label_len;
557 if (remain <= 0) {
558 err = got_error(GOT_ERR_BAD_OBJ_DATA);
559 goto done;
561 s += label_len;
562 p = memchr(s, '\n', remain);
563 if (p == NULL) {
564 err = got_error(GOT_ERR_BAD_OBJ_DATA);
565 goto done;
567 *p = '\0';
568 slen = strlen(s);
569 err = parse_commit_time(&(*commit)->author_time,
570 &(*commit)->author_gmtoff, s);
571 if (err)
572 goto done;
573 (*commit)->author = strdup(s);
574 if ((*commit)->author == NULL) {
575 err = got_error_from_errno("strdup");
576 goto done;
578 s += slen + 1;
579 remain -= slen + 1;
582 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
583 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
584 char *p;
585 size_t slen;
587 remain -= label_len;
588 if (remain <= 0) {
589 err = got_error(GOT_ERR_BAD_OBJ_DATA);
590 goto done;
592 s += label_len;
593 p = memchr(s, '\n', remain);
594 if (p == NULL) {
595 err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 goto done;
598 *p = '\0';
599 slen = strlen(s);
600 err = parse_commit_time(&(*commit)->committer_time,
601 &(*commit)->committer_gmtoff, s);
602 if (err)
603 goto done;
604 (*commit)->committer = strdup(s);
605 if ((*commit)->committer == NULL) {
606 err = got_error_from_errno("strdup");
607 goto done;
609 s += slen + 1;
610 remain -= slen + 1;
613 (*commit)->logmsg = strndup(s, remain);
614 if ((*commit)->logmsg == NULL) {
615 err = got_error_from_errno("strndup");
616 goto done;
618 done:
619 if (err) {
620 got_object_commit_close(*commit);
621 *commit = NULL;
623 return err;
626 void
627 got_object_tree_close(struct got_tree_object *tree)
629 if (tree->refcnt > 0) {
630 tree->refcnt--;
631 if (tree->refcnt > 0)
632 return;
635 free(tree->entries);
636 free(tree);
639 static const struct got_error *
640 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
641 size_t *elen, char *buf,
642 size_t maxlen)
644 char *p, *space;
645 const struct got_error *err = NULL;
647 *name = NULL;
648 *elen = 0;
650 *pte = malloc(sizeof(**pte));
651 if (*pte == NULL)
652 return got_error_from_errno("malloc");
654 *elen = strnlen(buf, maxlen) + 1;
655 if (*elen > maxlen) {
656 free(*pte);
657 *pte = NULL;
658 return got_error(GOT_ERR_BAD_OBJ_DATA);
661 space = memchr(buf, ' ', *elen);
662 if (space == NULL || space <= buf) {
663 err = got_error(GOT_ERR_BAD_OBJ_DATA);
664 free(*pte);
665 *pte = NULL;
666 return err;
668 (*pte)->mode = 0;
669 p = buf;
670 while (p < space) {
671 if (*p < '0' && *p > '7') {
672 err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 goto done;
675 (*pte)->mode <<= 3;
676 (*pte)->mode |= *p - '0';
677 p++;
680 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
681 err = got_error(GOT_ERR_BAD_OBJ_DATA);
682 goto done;
684 *name = space + 1;
685 buf += *elen;
686 (*pte)->id = buf;
687 *elen += SHA1_DIGEST_LENGTH;
688 done:
689 if (err) {
690 free(*pte);
691 *pte = NULL;
693 return err;
696 const struct got_error *
697 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
698 uint8_t *buf, size_t len)
700 const struct got_error *err = NULL;
701 size_t remain = len;
703 *nentries = 0;
704 if (remain == 0)
705 return NULL; /* tree is empty */
707 while (remain > 0) {
708 struct got_parsed_tree_entry *pte;
709 struct got_pathlist_entry *new = NULL;
710 const char *name;
711 size_t elen;
713 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
714 if (err)
715 goto done;
716 err = got_pathlist_insert(&new, entries, name, pte);
717 if (err)
718 goto done;
719 if (new == NULL) {
720 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
721 goto done;
723 buf += elen;
724 remain -= elen;
725 (*nentries)++;
728 if (remain != 0) {
729 err = got_error(GOT_ERR_BAD_OBJ_DATA);
730 goto done;
732 done:
733 if (err) {
734 got_object_parsed_tree_entries_free(entries);
735 *nentries = 0;
737 return err;
740 void
741 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
743 struct got_pathlist_entry *pe;
745 TAILQ_FOREACH(pe, entries, entry) {
746 struct got_parsed_tree_entry *pte = pe->data;
747 free(pte);
749 got_pathlist_free(entries);
752 void
753 got_object_tag_close(struct got_tag_object *tag)
755 if (tag->refcnt > 0) {
756 tag->refcnt--;
757 if (tag->refcnt > 0)
758 return;
761 free(tag->tag);
762 free(tag->tagger);
763 free(tag->tagmsg);
764 free(tag);
767 const struct got_error *
768 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
770 const struct got_error *err = NULL;
771 size_t remain = len;
772 char *s = buf;
773 size_t label_len;
775 if (remain == 0)
776 return got_error(GOT_ERR_BAD_OBJ_DATA);
778 *tag = calloc(1, sizeof(**tag));
779 if (*tag == NULL)
780 return got_error_from_errno("calloc");
782 label_len = strlen(GOT_TAG_LABEL_OBJECT);
783 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
784 remain -= label_len;
785 if (remain < SHA1_DIGEST_STRING_LENGTH) {
786 err = got_error(GOT_ERR_BAD_OBJ_DATA);
787 goto done;
789 s += label_len;
790 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
791 err = got_error(GOT_ERR_BAD_OBJ_DATA);
792 goto done;
794 remain -= SHA1_DIGEST_STRING_LENGTH;
795 s += SHA1_DIGEST_STRING_LENGTH;
796 } else {
797 err = got_error(GOT_ERR_BAD_OBJ_DATA);
798 goto done;
801 if (remain <= 0) {
802 err = got_error(GOT_ERR_BAD_OBJ_DATA);
803 goto done;
806 label_len = strlen(GOT_TAG_LABEL_TYPE);
807 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
808 remain -= label_len;
809 if (remain <= 0) {
810 err = got_error(GOT_ERR_BAD_OBJ_DATA);
811 goto done;
813 s += label_len;
814 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
815 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
816 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
817 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
818 s += label_len;
819 remain -= label_len;
820 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
821 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
822 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
823 label_len = strlen(GOT_OBJ_LABEL_TREE);
824 s += label_len;
825 remain -= label_len;
826 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
827 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
828 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
829 label_len = strlen(GOT_OBJ_LABEL_BLOB);
830 s += label_len;
831 remain -= label_len;
832 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
833 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
834 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
835 label_len = strlen(GOT_OBJ_LABEL_TAG);
836 s += label_len;
837 remain -= label_len;
838 } else {
839 err = got_error(GOT_ERR_BAD_OBJ_DATA);
840 goto done;
843 if (remain <= 0 || *s != '\n') {
844 err = got_error(GOT_ERR_BAD_OBJ_DATA);
845 goto done;
847 s++;
848 remain--;
849 if (remain <= 0) {
850 err = got_error(GOT_ERR_BAD_OBJ_DATA);
851 goto done;
853 } else {
854 err = got_error(GOT_ERR_BAD_OBJ_DATA);
855 goto done;
858 label_len = strlen(GOT_TAG_LABEL_TAG);
859 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
860 char *p;
861 size_t slen;
862 remain -= label_len;
863 if (remain <= 0) {
864 err = got_error(GOT_ERR_BAD_OBJ_DATA);
865 goto done;
867 s += label_len;
868 p = memchr(s, '\n', remain);
869 if (p == NULL) {
870 err = got_error(GOT_ERR_BAD_OBJ_DATA);
871 goto done;
873 *p = '\0';
874 slen = strlen(s);
875 (*tag)->tag = strndup(s, slen);
876 if ((*tag)->tag == NULL) {
877 err = got_error_from_errno("strndup");
878 goto done;
880 s += slen + 1;
881 remain -= slen + 1;
882 if (remain <= 0) {
883 err = got_error(GOT_ERR_BAD_OBJ_DATA);
884 goto done;
886 } else {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
888 goto done;
891 label_len = strlen(GOT_TAG_LABEL_TAGGER);
892 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
893 char *p;
894 size_t slen;
896 remain -= label_len;
897 if (remain <= 0) {
898 err = got_error(GOT_ERR_BAD_OBJ_DATA);
899 goto done;
901 s += label_len;
902 p = memchr(s, '\n', remain);
903 if (p == NULL) {
904 err = got_error(GOT_ERR_BAD_OBJ_DATA);
905 goto done;
907 *p = '\0';
908 slen = strlen(s);
909 err = parse_commit_time(&(*tag)->tagger_time,
910 &(*tag)->tagger_gmtoff, s);
911 if (err)
912 goto done;
913 (*tag)->tagger = strdup(s);
914 if ((*tag)->tagger == NULL) {
915 err = got_error_from_errno("strdup");
916 goto done;
918 s += slen + 1;
919 remain -= slen + 1;
920 if (remain <= 0) {
921 err = got_error(GOT_ERR_BAD_OBJ_DATA);
922 goto done;
924 } else {
925 /* Some old tags in the Linux git repo have no tagger. */
926 (*tag)->tagger = strdup("");
927 if ((*tag)->tagger == NULL) {
928 err = got_error_from_errno("strdup");
929 goto done;
933 (*tag)->tagmsg = strndup(s, remain);
934 if ((*tag)->tagmsg == NULL) {
935 err = got_error_from_errno("strndup");
936 goto done;
938 done:
939 if (err) {
940 got_object_tag_close(*tag);
941 *tag = NULL;
943 return err;
946 const struct got_error *
947 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
949 const struct got_error *err = NULL;
950 static const size_t blocksize = 512;
951 size_t n, total, remain;
952 uint8_t *buf;
954 *outbuf = NULL;
955 *outlen = 0;
957 buf = malloc(blocksize);
958 if (buf == NULL)
959 return got_error_from_errno("malloc");
961 remain = blocksize;
962 total = 0;
963 for (;;) {
964 if (remain == 0) {
965 uint8_t *newbuf;
966 newbuf = reallocarray(buf, 1, total + blocksize);
967 if (newbuf == NULL) {
968 err = got_error_from_errno("reallocarray");
969 goto done;
971 buf = newbuf;
972 remain += blocksize;
974 n = fread(buf + total, 1, remain, f);
975 if (n == 0) {
976 if (ferror(f)) {
977 err = got_ferror(f, GOT_ERR_IO);
978 goto done;
980 break; /* EOF */
982 remain -= n;
983 total += n;
984 };
986 done:
987 if (err == NULL) {
988 *outbuf = buf;
989 *outlen = total;
990 } else
991 free(buf);
992 return err;