Blob


1 /*
2 * Copyright (c) 2018, 2019 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_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 int
58 got_object_id_cmp(const struct got_object_id *id1,
59 const struct got_object_id *id2)
60 {
61 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
62 }
64 const struct got_error *
65 got_object_qid_alloc_partial(struct got_object_qid **qid)
66 {
67 const struct got_error *err = NULL;
69 *qid = malloc(sizeof(**qid));
70 if (*qid == NULL)
71 return got_error_from_errno("malloc");
73 (*qid)->id = malloc(sizeof(*((*qid)->id)));
74 if ((*qid)->id == NULL) {
75 err = got_error_from_errno("malloc");
76 got_object_qid_free(*qid);
77 *qid = NULL;
78 return err;
79 }
81 return NULL;
82 }
84 const struct got_error *
85 got_object_id_str(char **outbuf, struct got_object_id *id)
86 {
87 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
89 *outbuf = malloc(len);
90 if (*outbuf == NULL)
91 return got_error_from_errno("malloc");
93 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
94 free(*outbuf);
95 *outbuf = NULL;
96 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
97 }
99 return NULL;
102 void
103 got_object_close(struct got_object *obj)
105 if (obj->refcnt > 0) {
106 obj->refcnt--;
107 if (obj->refcnt > 0)
108 return;
111 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
112 struct got_delta *delta;
113 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
114 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
115 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
116 free(delta->delta_buf);
117 free(delta);
120 if (obj->flags & GOT_OBJ_FLAG_PACKED)
121 free(obj->path_packfile);
122 free(obj);
125 void
126 got_object_qid_free(struct got_object_qid *qid)
128 free(qid->id);
129 free(qid);
132 void
133 got_object_id_queue_free(struct got_object_id_queue *ids)
135 struct got_object_qid *qid;
137 while (!SIMPLEQ_EMPTY(ids)) {
138 qid = SIMPLEQ_FIRST(ids);
139 SIMPLEQ_REMOVE_HEAD(ids, entry);
140 got_object_qid_free(qid);
144 const struct got_error *
145 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
147 const char *obj_labels[] = {
148 GOT_OBJ_LABEL_COMMIT,
149 GOT_OBJ_LABEL_TREE,
150 GOT_OBJ_LABEL_BLOB,
151 GOT_OBJ_LABEL_TAG,
152 };
153 const int obj_types[] = {
154 GOT_OBJ_TYPE_COMMIT,
155 GOT_OBJ_TYPE_TREE,
156 GOT_OBJ_TYPE_BLOB,
157 GOT_OBJ_TYPE_TAG,
158 };
159 int type = 0;
160 size_t size = 0, hdrlen = 0;
161 int i;
163 *obj = NULL;
165 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
166 if (hdrlen > len)
167 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 for (i = 0; i < nitems(obj_labels); i++) {
170 const char *label = obj_labels[i];
171 size_t label_len = strlen(label);
172 const char *errstr;
174 if (strncmp(buf, label, label_len) != 0)
175 continue;
177 type = obj_types[i];
178 if (len <= label_len)
179 return got_error(GOT_ERR_BAD_OBJ_HDR);
180 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
181 if (errstr != NULL)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 break;
186 if (type == 0)
187 return got_error(GOT_ERR_BAD_OBJ_HDR);
189 *obj = calloc(1, sizeof(**obj));
190 if (*obj == NULL)
191 return got_error_from_errno("calloc");
192 (*obj)->type = type;
193 (*obj)->hdrlen = hdrlen;
194 (*obj)->size = size;
195 return NULL;
198 const struct got_error *
199 got_object_read_header(struct got_object **obj, int fd)
201 const struct got_error *err;
202 struct got_inflate_buf zb;
203 char *buf;
204 const size_t zbsize = 64;
205 size_t outlen, totlen;
206 int nbuf = 1;
208 *obj = NULL;
210 buf = malloc(zbsize);
211 if (buf == NULL)
212 return got_error_from_errno("malloc");
214 err = got_inflate_init(&zb, buf, zbsize);
215 if (err)
216 return err;
218 totlen = 0;
219 do {
220 err = got_inflate_read_fd(&zb, fd, &outlen);
221 if (err)
222 goto done;
223 if (outlen == 0)
224 break;
225 totlen += outlen;
226 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
227 char *newbuf;
228 nbuf++;
229 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
230 if (newbuf == NULL) {
231 err = got_error_from_errno("recallocarray");
232 goto done;
234 buf = newbuf;
235 zb.outbuf = newbuf + totlen;
236 zb.outlen = (nbuf * zbsize) - totlen;
238 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
240 err = got_object_parse_header(obj, buf, totlen);
241 done:
242 free(buf);
243 got_inflate_end(&zb);
244 return err;
247 struct got_commit_object *
248 got_object_commit_alloc_partial(void)
250 struct got_commit_object *commit;
252 commit = calloc(1, sizeof(*commit));
253 if (commit == NULL)
254 return NULL;
255 commit->tree_id = malloc(sizeof(*commit->tree_id));
256 if (commit->tree_id == NULL) {
257 free(commit);
258 return NULL;
261 SIMPLEQ_INIT(&commit->parent_ids);
263 return commit;
266 const struct got_error *
267 got_object_commit_add_parent(struct got_commit_object *commit,
268 const char *id_str)
270 const struct got_error *err = NULL;
271 struct got_object_qid *qid;
273 err = got_object_qid_alloc_partial(&qid);
274 if (err)
275 return err;
277 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
278 err = got_error(GOT_ERR_BAD_OBJ_DATA);
279 free(qid->id);
280 free(qid);
281 return err;
284 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
285 commit->nparents++;
287 return NULL;
290 static const struct got_error *
291 parse_gmtoff(time_t *gmtoff, const char *tzstr)
293 int sign = 1;
294 const char *p = tzstr;
295 time_t h, m;
297 *gmtoff = 0;
299 if (*p == '-')
300 sign = -1;
301 else if (*p != '+')
302 return got_error(GOT_ERR_BAD_OBJ_DATA);
303 p++;
304 if (!isdigit(*p) && !isdigit(*(p + 1)))
305 return got_error(GOT_ERR_BAD_OBJ_DATA);
306 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
308 p += 2;
309 if (!isdigit(*p) && !isdigit(*(p + 1)))
310 return got_error(GOT_ERR_BAD_OBJ_DATA);
311 m = ((*p - '0') * 10) + (*(p + 1) - '0');
313 *gmtoff = (h * 60 * 60 + m * 60) * sign;
314 return NULL;
317 static const struct got_error *
318 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
320 const struct got_error *err = NULL;
321 const char *errstr;
322 char *space, *tzstr;
324 /* Parse and strip off trailing timezone indicator string. */
325 space = strrchr(committer, ' ');
326 if (space == NULL)
327 return got_error(GOT_ERR_BAD_OBJ_DATA);
328 tzstr = strdup(space + 1);
329 if (tzstr == NULL)
330 return got_error_from_errno("strdup");
331 err = parse_gmtoff(gmtoff, tzstr);
332 free(tzstr);
333 if (err)
334 return err;
335 *space = '\0';
337 /* Timestamp is separated from committer name + email by space. */
338 space = strrchr(committer, ' ');
339 if (space == NULL)
340 return got_error(GOT_ERR_BAD_OBJ_DATA);
342 /* Timestamp parsed here is expressed in comitter's local time. */
343 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
344 if (errstr)
345 return got_error(GOT_ERR_BAD_OBJ_DATA);
347 /* Express the time stamp in UTC. */
348 *time -= *gmtoff;
350 /* Strip off parsed time information, leaving just author and email. */
351 *space = '\0';
353 return NULL;
356 void
357 got_object_commit_close(struct got_commit_object *commit)
359 if (commit->refcnt > 0) {
360 commit->refcnt--;
361 if (commit->refcnt > 0)
362 return;
365 got_object_id_queue_free(&commit->parent_ids);
366 free(commit->tree_id);
367 free(commit->author);
368 free(commit->committer);
369 free(commit->logmsg);
370 free(commit);
373 struct got_object_id *
374 got_object_commit_get_tree_id(struct got_commit_object *commit)
376 return commit->tree_id;
379 int
380 got_object_commit_get_nparents(struct got_commit_object *commit)
382 return commit->nparents;
385 const struct got_object_id_queue *
386 got_object_commit_get_parent_ids(struct got_commit_object *commit)
388 return &commit->parent_ids;
391 const char *
392 got_object_commit_get_author(struct got_commit_object *commit)
394 return commit->author;
397 time_t
398 got_object_commit_get_author_time(struct got_commit_object *commit)
400 return commit->author_time;
403 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
405 return commit->author_gmtoff;
408 const char *
409 got_object_commit_get_committer(struct got_commit_object *commit)
411 return commit->committer;
414 time_t
415 got_object_commit_get_committer_time(struct got_commit_object *commit)
417 return commit->committer_time;
420 time_t
421 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
423 return commit->committer_gmtoff;
426 const char *
427 got_object_commit_get_logmsg(struct got_commit_object *commit)
429 return commit->logmsg;
432 const struct got_error *
433 got_object_parse_commit(struct got_commit_object **commit, char *buf,
434 size_t len)
436 const struct got_error *err = NULL;
437 char *s = buf;
438 size_t label_len;
439 ssize_t remain = (ssize_t)len;
441 *commit = got_object_commit_alloc_partial();
442 if (*commit == NULL)
443 return got_error_from_errno("got_object_commit_alloc_partial");
445 label_len = strlen(GOT_COMMIT_LABEL_TREE);
446 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
447 remain -= label_len;
448 if (remain < SHA1_DIGEST_STRING_LENGTH) {
449 err = got_error(GOT_ERR_BAD_OBJ_DATA);
450 goto done;
452 s += label_len;
453 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
454 err = got_error(GOT_ERR_BAD_OBJ_DATA);
455 goto done;
457 remain -= SHA1_DIGEST_STRING_LENGTH;
458 s += SHA1_DIGEST_STRING_LENGTH;
459 } else {
460 err = got_error(GOT_ERR_BAD_OBJ_DATA);
461 goto done;
464 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
465 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
466 remain -= label_len;
467 if (remain < SHA1_DIGEST_STRING_LENGTH) {
468 err = got_error(GOT_ERR_BAD_OBJ_DATA);
469 goto done;
471 s += label_len;
472 err = got_object_commit_add_parent(*commit, s);
473 if (err)
474 goto done;
476 remain -= SHA1_DIGEST_STRING_LENGTH;
477 s += SHA1_DIGEST_STRING_LENGTH;
480 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
481 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
482 char *p;
483 size_t slen;
485 remain -= label_len;
486 if (remain <= 0) {
487 err = got_error(GOT_ERR_BAD_OBJ_DATA);
488 goto done;
490 s += label_len;
491 p = memchr(s, '\n', remain);
492 if (p == NULL) {
493 err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 goto done;
496 *p = '\0';
497 slen = strlen(s);
498 err = parse_commit_time(&(*commit)->author_time,
499 &(*commit)->author_gmtoff, s);
500 if (err)
501 goto done;
502 (*commit)->author = strdup(s);
503 if ((*commit)->author == NULL) {
504 err = got_error_from_errno("strdup");
505 goto done;
507 s += slen + 1;
508 remain -= slen + 1;
511 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
512 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
513 char *p;
514 size_t slen;
516 remain -= label_len;
517 if (remain <= 0) {
518 err = got_error(GOT_ERR_BAD_OBJ_DATA);
519 goto done;
521 s += label_len;
522 p = memchr(s, '\n', remain);
523 if (p == NULL) {
524 err = got_error(GOT_ERR_BAD_OBJ_DATA);
525 goto done;
527 *p = '\0';
528 slen = strlen(s);
529 err = parse_commit_time(&(*commit)->committer_time,
530 &(*commit)->committer_gmtoff, s);
531 if (err)
532 goto done;
533 (*commit)->committer = strdup(s);
534 if ((*commit)->committer == NULL) {
535 err = got_error_from_errno("strdup");
536 goto done;
538 s += slen + 1;
539 remain -= slen + 1;
542 (*commit)->logmsg = strndup(s, remain);
543 if ((*commit)->logmsg == NULL) {
544 err = got_error_from_errno("strndup");
545 goto done;
547 done:
548 if (err) {
549 got_object_commit_close(*commit);
550 *commit = NULL;
552 return err;
555 void
556 got_object_tree_entry_close(struct got_tree_entry *te)
558 free(te->id);
559 free(te->name);
560 free(te);
563 void
564 got_object_tree_entries_close(struct got_tree_entries *entries)
566 struct got_tree_entry *te;
568 while (!SIMPLEQ_EMPTY(&entries->head)) {
569 te = SIMPLEQ_FIRST(&entries->head);
570 SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
571 got_object_tree_entry_close(te);
575 void
576 got_object_tree_close(struct got_tree_object *tree)
578 if (tree->refcnt > 0) {
579 tree->refcnt--;
580 if (tree->refcnt > 0)
581 return;
584 got_object_tree_entries_close(&tree->entries);
585 free(tree);
588 struct got_tree_entry *
589 got_alloc_tree_entry_partial(void)
591 struct got_tree_entry *te;
593 te = malloc(sizeof(*te));
594 if (te == NULL)
595 return NULL;
597 te->id = malloc(sizeof(*te->id));
598 if (te->id == NULL) {
599 free(te);
600 te = NULL;
602 return te;
605 static const struct got_error *
606 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
607 size_t maxlen)
609 char *p, *space;
610 const struct got_error *err = NULL;
612 *te = got_alloc_tree_entry_partial();
613 if (*te == NULL)
614 return got_error_from_errno("got_alloc_tree_entry_partial");
616 *elen = strnlen(buf, maxlen) + 1;
617 if (*elen > maxlen) {
618 free(*te);
619 *te = NULL;
620 return got_error(GOT_ERR_BAD_OBJ_DATA);
623 space = memchr(buf, ' ', *elen);
624 if (space == NULL || space <= buf) {
625 err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 free(*te);
627 *te = NULL;
628 return err;
630 (*te)->mode = 0;
631 p = buf;
632 while (p < space) {
633 if (*p < '0' && *p > '7') {
634 err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 goto done;
637 (*te)->mode <<= 3;
638 (*te)->mode |= *p - '0';
639 p++;
642 (*te)->name = strdup(space + 1);
643 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
644 err = got_error(GOT_ERR_BAD_OBJ_DATA);
645 goto done;
647 buf += *elen;
648 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
649 *elen += SHA1_DIGEST_LENGTH;
650 done:
651 if (err) {
652 got_object_tree_entry_close(*te);
653 *te = NULL;
655 return err;
658 const struct got_error *
659 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
661 const struct got_error *err;
662 size_t remain = len;
663 struct got_pathlist_head pathlist;
664 struct got_pathlist_entry *pe;
666 TAILQ_INIT(&pathlist);
668 *tree = calloc(1, sizeof(**tree));
669 if (*tree == NULL)
670 return got_error_from_errno("calloc");
672 SIMPLEQ_INIT(&(*tree)->entries.head);
674 while (remain > 0) {
675 struct got_tree_entry *te;
676 struct got_pathlist_entry *new = NULL;
677 size_t elen;
679 err = parse_tree_entry(&te, &elen, buf, remain);
680 if (err)
681 goto done;
682 err = got_pathlist_insert(&new, &pathlist, te->name, te);
683 if (err)
684 goto done;
685 if (new == NULL) {
686 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
687 goto done;
689 buf += elen;
690 remain -= elen;
693 if (remain != 0) {
694 got_object_tree_close(*tree);
695 *tree = NULL;
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
697 goto done;
700 TAILQ_FOREACH(pe, &pathlist, entry) {
701 struct got_tree_entry *te = pe->data;
702 (*tree)->entries.nentries++;
703 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
705 done:
706 got_pathlist_free(&pathlist);
707 return err;
710 void
711 got_object_tag_close(struct got_tag_object *tag)
713 free(tag->tag);
714 free(tag->tagger);
715 free(tag->tagmsg);
716 free(tag);
719 const struct got_error *
720 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
722 const struct got_error *err = NULL;
723 size_t remain = len;
724 char *s = buf;
725 size_t label_len;
727 *tag = calloc(1, sizeof(**tag));
728 if (*tag == NULL)
729 return got_error_from_errno("calloc");
731 label_len = strlen(GOT_TAG_LABEL_OBJECT);
732 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
733 remain -= label_len;
734 if (remain < SHA1_DIGEST_STRING_LENGTH) {
735 err = got_error(GOT_ERR_BAD_OBJ_DATA);
736 goto done;
738 s += label_len;
739 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
741 goto done;
743 remain -= SHA1_DIGEST_STRING_LENGTH;
744 s += SHA1_DIGEST_STRING_LENGTH;
745 } else {
746 err = got_error(GOT_ERR_BAD_OBJ_DATA);
747 goto done;
750 if (remain <= 0) {
751 err = got_error(GOT_ERR_BAD_OBJ_DATA);
752 goto done;
755 label_len = strlen(GOT_TAG_LABEL_TYPE);
756 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
757 remain -= label_len;
758 if (remain <= 0) {
759 err = got_error(GOT_ERR_BAD_OBJ_DATA);
760 goto done;
762 s += label_len;
763 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
764 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
765 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
766 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
767 s += label_len;
768 remain -= label_len;
769 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
770 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
771 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
772 label_len = strlen(GOT_OBJ_LABEL_TREE);
773 s += label_len;
774 remain -= label_len;
775 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
776 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
777 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
778 label_len = strlen(GOT_OBJ_LABEL_BLOB);
779 s += label_len;
780 remain -= label_len;
781 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
782 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
783 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
784 label_len = strlen(GOT_OBJ_LABEL_TAG);
785 s += label_len;
786 remain -= label_len;
787 } else {
788 err = got_error(GOT_ERR_BAD_OBJ_DATA);
789 goto done;
792 if (remain <= 0 || *s != '\n') {
793 err = got_error(GOT_ERR_BAD_OBJ_DATA);
794 goto done;
796 s++;
797 remain--;
798 if (remain <= 0) {
799 err = got_error(GOT_ERR_BAD_OBJ_DATA);
800 goto done;
802 } else {
803 err = got_error(GOT_ERR_BAD_OBJ_DATA);
804 goto done;
807 label_len = strlen(GOT_TAG_LABEL_TAG);
808 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
809 char *p;
810 size_t slen;
811 remain -= label_len;
812 if (remain <= 0) {
813 err = got_error(GOT_ERR_BAD_OBJ_DATA);
814 goto done;
816 s += label_len;
817 p = memchr(s, '\n', remain);
818 if (p == NULL) {
819 err = got_error(GOT_ERR_BAD_OBJ_DATA);
820 goto done;
822 *p = '\0';
823 slen = strlen(s);
824 (*tag)->tag = strndup(s, slen);
825 if ((*tag)->tag == NULL) {
826 err = got_error_from_errno("strndup");
827 goto done;
829 s += slen + 1;
830 remain -= slen + 1;
831 if (remain <= 0) {
832 err = got_error(GOT_ERR_BAD_OBJ_DATA);
833 goto done;
835 } else {
836 err = got_error(GOT_ERR_BAD_OBJ_DATA);
837 goto done;
840 label_len = strlen(GOT_TAG_LABEL_TAGGER);
841 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
842 char *p;
843 size_t slen;
845 remain -= label_len;
846 if (remain <= 0) {
847 err = got_error(GOT_ERR_BAD_OBJ_DATA);
848 goto done;
850 s += label_len;
851 p = memchr(s, '\n', remain);
852 if (p == NULL) {
853 err = got_error(GOT_ERR_BAD_OBJ_DATA);
854 goto done;
856 *p = '\0';
857 slen = strlen(s);
858 err = parse_commit_time(&(*tag)->tagger_time,
859 &(*tag)->tagger_gmtoff, s);
860 if (err)
861 goto done;
862 (*tag)->tagger = strdup(s);
863 if ((*tag)->tagger == NULL) {
864 err = got_error_from_errno("strdup");
865 goto done;
867 s += slen + 1;
868 remain -= slen + 1;
869 if (remain <= 0) {
870 err = got_error(GOT_ERR_BAD_OBJ_DATA);
871 goto done;
873 } else {
874 /* Some old tags in the Linux git repo have no tagger. */
875 (*tag)->tagger = strdup("");
876 if ((*tag)->tagger == NULL) {
877 err = got_error_from_errno("strdup");
878 goto done;
882 (*tag)->tagmsg = strndup(s, remain);
883 if ((*tag)->tagmsg == NULL) {
884 err = got_error_from_errno("strndup");
885 goto done;
887 done:
888 if (err) {
889 got_object_tag_close(*tag);
890 *tag = NULL;
892 return err;
895 const struct got_error *
896 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
898 const struct got_error *err = NULL;
899 static const size_t blocksize = 512;
900 size_t n, total, remain;
901 uint8_t *buf;
903 *outbuf = NULL;
904 *outlen = 0;
906 buf = malloc(blocksize);
907 if (buf == NULL)
908 return got_error_from_errno("malloc");
910 remain = blocksize;
911 total = 0;
912 for (;;) {
913 if (remain == 0) {
914 uint8_t *newbuf;
915 newbuf = reallocarray(buf, 1, total + blocksize);
916 if (newbuf == NULL) {
917 err = got_error_from_errno("reallocarray");
918 goto done;
920 buf = newbuf;
921 remain += blocksize;
923 n = fread(buf + total, 1, remain, f);
924 if (n == 0) {
925 if (ferror(f)) {
926 err = got_ferror(f, GOT_ERR_IO);
927 goto done;
929 break; /* EOF */
931 remain -= n;
932 total += n;
933 };
935 done:
936 if (err == NULL) {
937 *outbuf = buf;
938 *outlen = total;
939 } else
940 free(buf);
941 return err;