Blob


1 /*
2 * Copyright (c) 2018 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"
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_cache.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_repository.h"
51 #include "got_lib_path.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();
73 (*qid)->id = malloc(sizeof(*((*qid)->id)));
74 if ((*qid)->id == NULL) {
75 err = got_error_from_errno();
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();
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 got_delta_close(delta);
119 if (obj->flags & GOT_OBJ_FLAG_PACKED)
120 free(obj->path_packfile);
121 free(obj);
124 void
125 got_object_qid_free(struct got_object_qid *qid)
127 free(qid->id);
128 free(qid);
131 const struct got_error *
132 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
134 const char *obj_labels[] = {
135 GOT_OBJ_LABEL_COMMIT,
136 GOT_OBJ_LABEL_TREE,
137 GOT_OBJ_LABEL_BLOB,
138 GOT_OBJ_LABEL_TAG,
139 };
140 const int obj_types[] = {
141 GOT_OBJ_TYPE_COMMIT,
142 GOT_OBJ_TYPE_TREE,
143 GOT_OBJ_TYPE_BLOB,
144 GOT_OBJ_TYPE_TAG,
145 };
146 int type = 0;
147 size_t size = 0, hdrlen = 0;
148 int i;
149 char *p = strchr(buf, '\0');
151 *obj = NULL;
153 if (p == NULL)
154 return got_error(GOT_ERR_BAD_OBJ_HDR);
156 hdrlen = strlen(buf) + 1 /* '\0' */;
158 for (i = 0; i < nitems(obj_labels); i++) {
159 const char *label = obj_labels[i];
160 size_t label_len = strlen(label);
161 const char *errstr;
163 if (strncmp(buf, label, label_len) != 0)
164 continue;
166 type = obj_types[i];
167 if (len <= label_len)
168 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
170 if (errstr != NULL)
171 return got_error(GOT_ERR_BAD_OBJ_HDR);
172 break;
175 if (type == 0)
176 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 *obj = calloc(1, sizeof(**obj));
179 if (*obj == NULL)
180 return got_error_from_errno();
181 (*obj)->type = type;
182 (*obj)->hdrlen = hdrlen;
183 (*obj)->size = size;
184 return NULL;
187 const struct got_error *
188 got_object_read_header(struct got_object **obj, int fd)
190 const struct got_error *err;
191 struct got_inflate_buf zb;
192 char *buf;
193 const size_t zbsize = 64;
194 size_t outlen, totlen;
195 int nbuf = 1;
197 *obj = NULL;
199 buf = malloc(zbsize);
200 if (buf == NULL)
201 return got_error_from_errno();
203 err = got_inflate_init(&zb, buf, zbsize);
204 if (err)
205 return err;
207 totlen = 0;
208 do {
209 err = got_inflate_read_fd(&zb, fd, &outlen);
210 if (err)
211 goto done;
212 if (outlen == 0)
213 break;
214 totlen += outlen;
215 if (strchr(zb.outbuf, '\0') == NULL) {
216 char *newbuf;
217 nbuf++;
218 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
219 if (newbuf == NULL) {
220 err = got_error_from_errno();
221 goto done;
223 buf = newbuf;
224 zb.outbuf = newbuf + totlen;
225 zb.outlen = (nbuf * zbsize) - totlen;
227 } while (strchr(zb.outbuf, '\0') == NULL);
229 err = got_object_parse_header(obj, buf, totlen);
230 done:
231 free(buf);
232 got_inflate_end(&zb);
233 return err;
236 struct got_commit_object *
237 got_object_commit_alloc_partial(void)
239 struct got_commit_object *commit;
241 commit = calloc(1, sizeof(*commit));
242 if (commit == NULL)
243 return NULL;
244 commit->tree_id = malloc(sizeof(*commit->tree_id));
245 if (commit->tree_id == NULL) {
246 free(commit);
247 return NULL;
250 SIMPLEQ_INIT(&commit->parent_ids);
252 return commit;
255 const struct got_error *
256 got_object_commit_add_parent(struct got_commit_object *commit,
257 const char *id_str)
259 const struct got_error *err = NULL;
260 struct got_object_qid *qid;
262 err = got_object_qid_alloc_partial(&qid);
263 if (err)
264 return err;
266 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
267 err = got_error(GOT_ERR_BAD_OBJ_DATA);
268 free(qid->id);
269 free(qid);
270 return err;
273 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
274 commit->nparents++;
276 return NULL;
279 static const struct got_error *
280 parse_gmtoff(time_t *gmtoff, const char *tzstr)
282 int sign = 1;
283 const char *p = tzstr;
284 time_t h, m;
286 *gmtoff = 0;
288 if (*p == '-')
289 sign = -1;
290 else if (*p != '+')
291 return got_error(GOT_ERR_BAD_OBJ_DATA);
292 p++;
293 if (!isdigit(*p) && !isdigit(*(p + 1)))
294 return got_error(GOT_ERR_BAD_OBJ_DATA);
295 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
297 p += 2;
298 if (!isdigit(*p) && !isdigit(*(p + 1)))
299 return got_error(GOT_ERR_BAD_OBJ_DATA);
300 m = ((*p - '0') * 10) + (*(p + 1) - '0');
302 *gmtoff = (h * 60 * 60 + m * 60) * sign;
303 return NULL;
306 static const struct got_error *
307 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
309 const struct got_error *err = NULL;
310 const char *errstr;
311 char *space, *tzstr;
313 /* Parse and strip off trailing timezone indicator string. */
314 space = strrchr(committer, ' ');
315 if (space == NULL)
316 return got_error(GOT_ERR_BAD_OBJ_DATA);
317 tzstr = strdup(space + 1);
318 if (tzstr == NULL)
319 return got_error_from_errno();
320 err = parse_gmtoff(gmtoff, tzstr);
321 free(tzstr);
322 if (err)
323 return err;
324 *space = '\0';
326 /* Timestamp is separated from committer name + email by space. */
327 space = strrchr(committer, ' ');
328 if (space == NULL)
329 return got_error(GOT_ERR_BAD_OBJ_DATA);
331 /* Timestamp parsed here is expressed in comitter's local time. */
332 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
333 if (errstr)
334 return got_error(GOT_ERR_BAD_OBJ_DATA);
336 /* Express the time stamp in UTC. */
337 *time -= *gmtoff;
339 /* Strip off parsed time information, leaving just author and email. */
340 *space = '\0';
342 return NULL;
345 void
346 got_object_commit_close(struct got_commit_object *commit)
348 struct got_object_qid *qid;
350 if (commit->refcnt > 0) {
351 commit->refcnt--;
352 if (commit->refcnt > 0)
353 return;
356 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
357 qid = SIMPLEQ_FIRST(&commit->parent_ids);
358 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
359 got_object_qid_free(qid);
362 free(commit->tree_id);
363 free(commit->author);
364 free(commit->committer);
365 free(commit->logmsg);
366 free(commit);
369 struct got_object_id *
370 got_object_commit_get_tree_id(struct got_commit_object *commit)
372 return commit->tree_id;
375 int
376 got_object_commit_get_nparents(struct got_commit_object *commit)
378 return commit->nparents;
381 const struct got_object_id_queue *
382 got_object_commit_get_parent_ids(struct got_commit_object *commit)
384 return &commit->parent_ids;
387 const char *
388 got_object_commit_get_author(struct got_commit_object *commit)
390 return commit->author;
393 time_t
394 got_object_commit_get_author_time(struct got_commit_object *commit)
396 return commit->author_time;
399 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
401 return commit->author_gmtoff;
404 const char *
405 got_object_commit_get_committer(struct got_commit_object *commit)
407 return commit->committer;
410 time_t
411 got_object_commit_get_committer_time(struct got_commit_object *commit)
413 return commit->committer_time;
416 time_t
417 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
419 return commit->committer_gmtoff;
422 const char *
423 got_object_commit_get_logmsg(struct got_commit_object *commit)
425 return commit->logmsg;
428 const struct got_error *
429 got_object_parse_commit(struct got_commit_object **commit, char *buf,
430 size_t len)
432 const struct got_error *err = NULL;
433 char *s = buf;
434 size_t label_len;
435 ssize_t remain = (ssize_t)len;
437 *commit = got_object_commit_alloc_partial();
438 if (*commit == NULL)
439 return got_error_from_errno();
441 label_len = strlen(GOT_COMMIT_LABEL_TREE);
442 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
443 remain -= label_len;
444 if (remain < SHA1_DIGEST_STRING_LENGTH) {
445 err = got_error(GOT_ERR_BAD_OBJ_DATA);
446 goto done;
448 s += label_len;
449 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
450 err = got_error(GOT_ERR_BAD_OBJ_DATA);
451 goto done;
453 remain -= SHA1_DIGEST_STRING_LENGTH;
454 s += SHA1_DIGEST_STRING_LENGTH;
455 } else {
456 err = got_error(GOT_ERR_BAD_OBJ_DATA);
457 goto done;
460 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
461 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
462 remain -= label_len;
463 if (remain < SHA1_DIGEST_STRING_LENGTH) {
464 err = got_error(GOT_ERR_BAD_OBJ_DATA);
465 goto done;
467 s += label_len;
468 err = got_object_commit_add_parent(*commit, s);
469 if (err)
470 goto done;
472 remain -= SHA1_DIGEST_STRING_LENGTH;
473 s += SHA1_DIGEST_STRING_LENGTH;
476 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
477 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
478 char *p;
479 size_t slen;
481 remain -= label_len;
482 if (remain <= 0) {
483 err = got_error(GOT_ERR_BAD_OBJ_DATA);
484 goto done;
486 s += label_len;
487 p = strchr(s, '\n');
488 if (p == NULL) {
489 err = got_error(GOT_ERR_BAD_OBJ_DATA);
490 goto done;
492 *p = '\0';
493 slen = strlen(s);
494 err = parse_commit_time(&(*commit)->author_time,
495 &(*commit)->author_gmtoff, s);
496 if (err)
497 goto done;
498 (*commit)->author = strdup(s);
499 if ((*commit)->author == NULL) {
500 err = got_error_from_errno();
501 goto done;
503 s += slen + 1;
504 remain -= slen + 1;
507 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
508 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
509 char *p;
510 size_t slen;
512 remain -= label_len;
513 if (remain <= 0) {
514 err = got_error(GOT_ERR_BAD_OBJ_DATA);
515 goto done;
517 s += label_len;
518 p = strchr(s, '\n');
519 if (p == NULL) {
520 err = got_error(GOT_ERR_BAD_OBJ_DATA);
521 goto done;
523 *p = '\0';
524 slen = strlen(s);
525 err = parse_commit_time(&(*commit)->committer_time,
526 &(*commit)->committer_gmtoff, s);
527 if (err)
528 goto done;
529 (*commit)->committer = strdup(s);
530 if ((*commit)->committer == NULL) {
531 err = got_error_from_errno();
532 goto done;
534 s += slen + 1;
535 remain -= slen + 1;
538 (*commit)->logmsg = strndup(s, remain);
539 if ((*commit)->logmsg == NULL) {
540 err = got_error_from_errno();
541 goto done;
543 done:
544 if (err) {
545 got_object_commit_close(*commit);
546 *commit = NULL;
548 return err;
551 void
552 got_object_tree_entry_close(struct got_tree_entry *te)
554 free(te->id);
555 free(te->name);
556 free(te);
559 void
560 got_object_tree_close(struct got_tree_object *tree)
562 struct got_tree_entry *te;
564 if (tree->refcnt > 0) {
565 tree->refcnt--;
566 if (tree->refcnt > 0)
567 return;
570 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
571 te = SIMPLEQ_FIRST(&tree->entries.head);
572 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
573 got_object_tree_entry_close(te);
576 free(tree);
579 struct got_tree_entry *
580 got_alloc_tree_entry_partial(void)
582 struct got_tree_entry *te;
584 te = malloc(sizeof(*te));
585 if (te == NULL)
586 return NULL;
588 te->id = malloc(sizeof(*te->id));
589 if (te->id == NULL) {
590 free(te);
591 te = NULL;
593 return te;
596 static const struct got_error *
597 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
598 size_t maxlen)
600 char *p = buf, *space;
601 const struct got_error *err = NULL;
603 *te = got_alloc_tree_entry_partial();
604 if (*te == NULL)
605 return got_error_from_errno();
607 *elen = strlen(buf) + 1;
608 if (*elen > maxlen) {
609 free(*te);
610 *te = NULL;
611 return got_error(GOT_ERR_BAD_OBJ_DATA);
614 space = strchr(buf, ' ');
615 if (space == NULL) {
616 err = got_error(GOT_ERR_BAD_OBJ_DATA);
617 free(*te);
618 *te = NULL;
619 return err;
621 (*te)->mode = 0;
622 while (*p != ' ') {
623 if (*p < '0' && *p > '7') {
624 err = got_error(GOT_ERR_BAD_OBJ_DATA);
625 goto done;
627 (*te)->mode <<= 3;
628 (*te)->mode |= *p - '0';
629 p++;
632 (*te)->name = strdup(space + 1);
633 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
634 err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 goto done;
637 buf += *elen;
638 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
639 *elen += SHA1_DIGEST_LENGTH;
640 done:
641 if (err) {
642 got_object_tree_entry_close(*te);
643 *te = NULL;
645 return err;
648 const struct got_error *
649 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
651 const struct got_error *err;
652 size_t remain = len;
653 struct got_pathlist_head pathlist;
654 struct got_pathlist_entry *pe;
656 TAILQ_INIT(&pathlist);
658 *tree = calloc(1, sizeof(**tree));
659 if (*tree == NULL)
660 return got_error_from_errno();
662 SIMPLEQ_INIT(&(*tree)->entries.head);
664 while (remain > 0) {
665 struct got_tree_entry *te;
666 struct got_pathlist_entry *new = NULL;
667 size_t elen;
669 err = parse_tree_entry(&te, &elen, buf, remain);
670 if (err)
671 goto done;
672 err = got_pathlist_insert(&new, &pathlist, te->name, te);
673 if (err)
674 goto done;
675 if (new == NULL) {
676 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
677 goto done;
679 buf += elen;
680 remain -= elen;
683 if (remain != 0) {
684 got_object_tree_close(*tree);
685 *tree = NULL;
686 err = got_error(GOT_ERR_BAD_OBJ_DATA);
687 goto done;
690 TAILQ_FOREACH(pe, &pathlist, entry) {
691 struct got_tree_entry *te = pe->data;
692 (*tree)->entries.nentries++;
693 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
695 done:
696 got_pathlist_free(&pathlist);
697 return err;
700 void
701 got_object_tag_close(struct got_tag_object *tag)
703 free(tag->tag);
704 free(tag->tagger);
705 free(tag->tagmsg);
706 free(tag);
709 const struct got_error *
710 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
712 const struct got_error *err = NULL;
713 size_t remain = len;
714 char *s = buf;
715 size_t label_len;
717 *tag = calloc(1, sizeof(**tag));
718 if (*tag == NULL)
719 return got_error_from_errno();
721 label_len = strlen(GOT_TAG_LABEL_OBJECT);
722 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
723 remain -= label_len;
724 if (remain < SHA1_DIGEST_STRING_LENGTH) {
725 err = got_error(GOT_ERR_BAD_OBJ_DATA);
726 goto done;
728 s += label_len;
729 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
730 err = got_error(GOT_ERR_BAD_OBJ_DATA);
731 goto done;
733 remain -= SHA1_DIGEST_STRING_LENGTH;
734 s += SHA1_DIGEST_STRING_LENGTH;
735 } else {
736 err = got_error(GOT_ERR_BAD_OBJ_DATA);
737 goto done;
740 if (remain <= 0) {
741 err = got_error(GOT_ERR_BAD_OBJ_DATA);
742 goto done;
745 label_len = strlen(GOT_TAG_LABEL_TYPE);
746 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
747 remain -= label_len;
748 if (remain <= 0) {
749 err = got_error(GOT_ERR_BAD_OBJ_DATA);
750 goto done;
752 s += label_len;
753 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
754 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
755 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
756 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
757 s += label_len;
758 remain -= label_len;
759 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
760 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
761 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
762 label_len = strlen(GOT_OBJ_LABEL_TREE);
763 s += label_len;
764 remain -= label_len;
765 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
766 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
767 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
768 label_len = strlen(GOT_OBJ_LABEL_BLOB);
769 s += label_len;
770 remain -= label_len;
771 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
772 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
773 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
774 label_len = strlen(GOT_OBJ_LABEL_TAG);
775 s += label_len;
776 remain -= label_len;
777 } else {
778 err = got_error(GOT_ERR_BAD_OBJ_DATA);
779 goto done;
782 if (remain <= 0 || *s != '\n') {
783 err = got_error(GOT_ERR_BAD_OBJ_DATA);
784 goto done;
786 s++;
787 remain--;
788 if (remain <= 0) {
789 err = got_error(GOT_ERR_BAD_OBJ_DATA);
790 goto done;
792 } else {
793 err = got_error(GOT_ERR_BAD_OBJ_DATA);
794 goto done;
797 label_len = strlen(GOT_TAG_LABEL_TAG);
798 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
799 char *p;
800 size_t slen;
801 remain -= label_len;
802 if (remain <= 0) {
803 err = got_error(GOT_ERR_BAD_OBJ_DATA);
804 goto done;
806 s += label_len;
807 p = strchr(s, '\n');
808 if (p == NULL) {
809 err = got_error(GOT_ERR_BAD_OBJ_DATA);
810 goto done;
812 *p = '\0';
813 slen = strlen(s);
814 (*tag)->tag = strndup(s, slen);
815 if ((*tag)->tag == NULL) {
816 err = got_error_from_errno();
817 goto done;
819 s += slen + 1;
820 remain -= slen + 1;
821 if (remain <= 0) {
822 err = got_error(GOT_ERR_BAD_OBJ_DATA);
823 goto done;
825 } else {
826 err = got_error(GOT_ERR_BAD_OBJ_DATA);
827 goto done;
830 label_len = strlen(GOT_TAG_LABEL_TAGGER);
831 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
832 char *p;
833 size_t slen;
835 remain -= label_len;
836 if (remain <= 0) {
837 err = got_error(GOT_ERR_BAD_OBJ_DATA);
838 goto done;
840 s += label_len;
841 p = strchr(s, '\n');
842 if (p == NULL) {
843 err = got_error(GOT_ERR_BAD_OBJ_DATA);
844 goto done;
846 *p = '\0';
847 slen = strlen(s);
848 err = parse_commit_time(&(*tag)->tagger_time,
849 &(*tag)->tagger_gmtoff, s);
850 if (err)
851 goto done;
852 (*tag)->tagger = strdup(s);
853 if ((*tag)->tagger == NULL) {
854 err = got_error_from_errno();
855 goto done;
857 s += slen + 1;
858 remain -= slen + 1;
859 if (remain <= 0) {
860 err = got_error(GOT_ERR_BAD_OBJ_DATA);
861 goto done;
863 } else {
864 /* Some old tags in the Linux git repo have no tagger. */
865 (*tag)->tagger = strdup("");
866 if ((*tag)->tagger == NULL) {
867 err = got_error_from_errno();
868 goto done;
872 (*tag)->tagmsg = strndup(s, remain);
873 if ((*tag)->tagmsg == NULL) {
874 err = got_error_from_errno();
875 goto done;
877 done:
878 if (err) {
879 got_object_tag_close(*tag);
880 *tag = NULL;
882 return err;
885 const struct got_error *
886 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
888 const struct got_error *err = NULL;
889 static const size_t blocksize = 512;
890 size_t n, total, remain;
891 uint8_t *buf;
893 *outbuf = NULL;
894 *outlen = 0;
896 buf = malloc(blocksize);
897 if (buf == NULL)
898 return got_error_from_errno();
900 remain = blocksize;
901 total = 0;
902 while (1) {
903 if (remain == 0) {
904 uint8_t *newbuf;
905 newbuf = reallocarray(buf, 1, total + blocksize);
906 if (newbuf == NULL) {
907 err = got_error_from_errno();
908 goto done;
910 buf = newbuf;
911 remain += blocksize;
913 n = fread(buf + total, 1, remain, f);
914 if (n == 0) {
915 if (ferror(f)) {
916 err = got_ferror(f, GOT_ERR_IO);
917 goto done;
919 break; /* EOF */
921 remain -= n;
922 total += n;
923 };
925 done:
926 if (err == NULL) {
927 *outbuf = buf;
928 *outlen = total;
929 } else
930 free(buf);
931 return err;