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 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;
150 *obj = NULL;
152 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
153 if (hdrlen > len)
154 return got_error(GOT_ERR_BAD_OBJ_HDR);
156 for (i = 0; i < nitems(obj_labels); i++) {
157 const char *label = obj_labels[i];
158 size_t label_len = strlen(label);
159 const char *errstr;
161 if (strncmp(buf, label, label_len) != 0)
162 continue;
164 type = obj_types[i];
165 if (len <= label_len)
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
167 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
168 if (errstr != NULL)
169 return got_error(GOT_ERR_BAD_OBJ_HDR);
170 break;
173 if (type == 0)
174 return got_error(GOT_ERR_BAD_OBJ_HDR);
176 *obj = calloc(1, sizeof(**obj));
177 if (*obj == NULL)
178 return got_error_from_errno("calloc");
179 (*obj)->type = type;
180 (*obj)->hdrlen = hdrlen;
181 (*obj)->size = size;
182 return NULL;
185 const struct got_error *
186 got_object_read_header(struct got_object **obj, int fd)
188 const struct got_error *err;
189 struct got_inflate_buf zb;
190 char *buf;
191 const size_t zbsize = 64;
192 size_t outlen, totlen;
193 int nbuf = 1;
195 *obj = NULL;
197 buf = malloc(zbsize);
198 if (buf == NULL)
199 return got_error_from_errno("malloc");
201 err = got_inflate_init(&zb, buf, zbsize);
202 if (err)
203 return err;
205 totlen = 0;
206 do {
207 err = got_inflate_read_fd(&zb, fd, &outlen);
208 if (err)
209 goto done;
210 if (outlen == 0)
211 break;
212 totlen += outlen;
213 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
214 char *newbuf;
215 nbuf++;
216 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
217 if (newbuf == NULL) {
218 err = got_error_from_errno("recallocarray");
219 goto done;
221 buf = newbuf;
222 zb.outbuf = newbuf + totlen;
223 zb.outlen = (nbuf * zbsize) - totlen;
225 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
227 err = got_object_parse_header(obj, buf, totlen);
228 done:
229 free(buf);
230 got_inflate_end(&zb);
231 return err;
234 struct got_commit_object *
235 got_object_commit_alloc_partial(void)
237 struct got_commit_object *commit;
239 commit = calloc(1, sizeof(*commit));
240 if (commit == NULL)
241 return NULL;
242 commit->tree_id = malloc(sizeof(*commit->tree_id));
243 if (commit->tree_id == NULL) {
244 free(commit);
245 return NULL;
248 SIMPLEQ_INIT(&commit->parent_ids);
250 return commit;
253 const struct got_error *
254 got_object_commit_add_parent(struct got_commit_object *commit,
255 const char *id_str)
257 const struct got_error *err = NULL;
258 struct got_object_qid *qid;
260 err = got_object_qid_alloc_partial(&qid);
261 if (err)
262 return err;
264 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
265 err = got_error(GOT_ERR_BAD_OBJ_DATA);
266 free(qid->id);
267 free(qid);
268 return err;
271 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
272 commit->nparents++;
274 return NULL;
277 static const struct got_error *
278 parse_gmtoff(time_t *gmtoff, const char *tzstr)
280 int sign = 1;
281 const char *p = tzstr;
282 time_t h, m;
284 *gmtoff = 0;
286 if (*p == '-')
287 sign = -1;
288 else if (*p != '+')
289 return got_error(GOT_ERR_BAD_OBJ_DATA);
290 p++;
291 if (!isdigit(*p) && !isdigit(*(p + 1)))
292 return got_error(GOT_ERR_BAD_OBJ_DATA);
293 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
295 p += 2;
296 if (!isdigit(*p) && !isdigit(*(p + 1)))
297 return got_error(GOT_ERR_BAD_OBJ_DATA);
298 m = ((*p - '0') * 10) + (*(p + 1) - '0');
300 *gmtoff = (h * 60 * 60 + m * 60) * sign;
301 return NULL;
304 static const struct got_error *
305 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
307 const struct got_error *err = NULL;
308 const char *errstr;
309 char *space, *tzstr;
311 /* Parse and strip off trailing timezone indicator string. */
312 space = strrchr(committer, ' ');
313 if (space == NULL)
314 return got_error(GOT_ERR_BAD_OBJ_DATA);
315 tzstr = strdup(space + 1);
316 if (tzstr == NULL)
317 return got_error_from_errno("strdup");
318 err = parse_gmtoff(gmtoff, tzstr);
319 free(tzstr);
320 if (err)
321 return err;
322 *space = '\0';
324 /* Timestamp is separated from committer name + email by space. */
325 space = strrchr(committer, ' ');
326 if (space == NULL)
327 return got_error(GOT_ERR_BAD_OBJ_DATA);
329 /* Timestamp parsed here is expressed in comitter's local time. */
330 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
331 if (errstr)
332 return got_error(GOT_ERR_BAD_OBJ_DATA);
334 /* Express the time stamp in UTC. */
335 *time -= *gmtoff;
337 /* Strip off parsed time information, leaving just author and email. */
338 *space = '\0';
340 return NULL;
343 void
344 got_object_commit_close(struct got_commit_object *commit)
346 struct got_object_qid *qid;
348 if (commit->refcnt > 0) {
349 commit->refcnt--;
350 if (commit->refcnt > 0)
351 return;
354 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
355 qid = SIMPLEQ_FIRST(&commit->parent_ids);
356 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
357 got_object_qid_free(qid);
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 char *
421 got_object_commit_get_logmsg(struct got_commit_object *commit)
423 return commit->logmsg;
426 const struct got_error *
427 got_object_parse_commit(struct got_commit_object **commit, char *buf,
428 size_t len)
430 const struct got_error *err = NULL;
431 char *s = buf;
432 size_t label_len;
433 ssize_t remain = (ssize_t)len;
435 *commit = got_object_commit_alloc_partial();
436 if (*commit == NULL)
437 return got_error_from_errno("got_object_commit_alloc_partial");
439 label_len = strlen(GOT_COMMIT_LABEL_TREE);
440 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
441 remain -= label_len;
442 if (remain < SHA1_DIGEST_STRING_LENGTH) {
443 err = got_error(GOT_ERR_BAD_OBJ_DATA);
444 goto done;
446 s += label_len;
447 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
448 err = got_error(GOT_ERR_BAD_OBJ_DATA);
449 goto done;
451 remain -= SHA1_DIGEST_STRING_LENGTH;
452 s += SHA1_DIGEST_STRING_LENGTH;
453 } else {
454 err = got_error(GOT_ERR_BAD_OBJ_DATA);
455 goto done;
458 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
459 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
460 remain -= label_len;
461 if (remain < SHA1_DIGEST_STRING_LENGTH) {
462 err = got_error(GOT_ERR_BAD_OBJ_DATA);
463 goto done;
465 s += label_len;
466 err = got_object_commit_add_parent(*commit, s);
467 if (err)
468 goto done;
470 remain -= SHA1_DIGEST_STRING_LENGTH;
471 s += SHA1_DIGEST_STRING_LENGTH;
474 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
475 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
476 char *p;
477 size_t slen;
479 remain -= label_len;
480 if (remain <= 0) {
481 err = got_error(GOT_ERR_BAD_OBJ_DATA);
482 goto done;
484 s += label_len;
485 p = memchr(s, '\n', remain);
486 if (p == NULL) {
487 err = got_error(GOT_ERR_BAD_OBJ_DATA);
488 goto done;
490 *p = '\0';
491 slen = strlen(s);
492 err = parse_commit_time(&(*commit)->author_time,
493 &(*commit)->author_gmtoff, s);
494 if (err)
495 goto done;
496 (*commit)->author = strdup(s);
497 if ((*commit)->author == NULL) {
498 err = got_error_from_errno("strdup");
499 goto done;
501 s += slen + 1;
502 remain -= slen + 1;
505 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
506 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
507 char *p;
508 size_t slen;
510 remain -= label_len;
511 if (remain <= 0) {
512 err = got_error(GOT_ERR_BAD_OBJ_DATA);
513 goto done;
515 s += label_len;
516 p = memchr(s, '\n', remain);
517 if (p == NULL) {
518 err = got_error(GOT_ERR_BAD_OBJ_DATA);
519 goto done;
521 *p = '\0';
522 slen = strlen(s);
523 err = parse_commit_time(&(*commit)->committer_time,
524 &(*commit)->committer_gmtoff, s);
525 if (err)
526 goto done;
527 (*commit)->committer = strdup(s);
528 if ((*commit)->committer == NULL) {
529 err = got_error_from_errno("strdup");
530 goto done;
532 s += slen + 1;
533 remain -= slen + 1;
536 (*commit)->logmsg = strndup(s, remain);
537 if ((*commit)->logmsg == NULL) {
538 err = got_error_from_errno("strndup");
539 goto done;
541 done:
542 if (err) {
543 got_object_commit_close(*commit);
544 *commit = NULL;
546 return err;
549 void
550 got_object_tree_entry_close(struct got_tree_entry *te)
552 free(te->id);
553 free(te->name);
554 free(te);
557 void
558 got_object_tree_entries_close(struct got_tree_entries *entries)
560 struct got_tree_entry *te;
562 while (!SIMPLEQ_EMPTY(&entries->head)) {
563 te = SIMPLEQ_FIRST(&entries->head);
564 SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
565 got_object_tree_entry_close(te);
569 void
570 got_object_tree_close(struct got_tree_object *tree)
572 if (tree->refcnt > 0) {
573 tree->refcnt--;
574 if (tree->refcnt > 0)
575 return;
578 got_object_tree_entries_close(&tree->entries);
579 free(tree);
582 struct got_tree_entry *
583 got_alloc_tree_entry_partial(void)
585 struct got_tree_entry *te;
587 te = malloc(sizeof(*te));
588 if (te == NULL)
589 return NULL;
591 te->id = malloc(sizeof(*te->id));
592 if (te->id == NULL) {
593 free(te);
594 te = NULL;
596 return te;
599 static const struct got_error *
600 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
601 size_t maxlen)
603 char *p, *space;
604 const struct got_error *err = NULL;
606 *te = got_alloc_tree_entry_partial();
607 if (*te == NULL)
608 return got_error_from_errno("got_alloc_tree_entry_partial");
610 *elen = strnlen(buf, maxlen) + 1;
611 if (*elen > maxlen) {
612 free(*te);
613 *te = NULL;
614 return got_error(GOT_ERR_BAD_OBJ_DATA);
617 space = memchr(buf, ' ', *elen);
618 if (space == NULL || space <= buf) {
619 err = got_error(GOT_ERR_BAD_OBJ_DATA);
620 free(*te);
621 *te = NULL;
622 return err;
624 (*te)->mode = 0;
625 p = buf;
626 while (p < space) {
627 if (*p < '0' && *p > '7') {
628 err = got_error(GOT_ERR_BAD_OBJ_DATA);
629 goto done;
631 (*te)->mode <<= 3;
632 (*te)->mode |= *p - '0';
633 p++;
636 (*te)->name = strdup(space + 1);
637 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
639 goto done;
641 buf += *elen;
642 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
643 *elen += SHA1_DIGEST_LENGTH;
644 done:
645 if (err) {
646 got_object_tree_entry_close(*te);
647 *te = NULL;
649 return err;
652 const struct got_error *
653 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
655 const struct got_error *err;
656 size_t remain = len;
657 struct got_pathlist_head pathlist;
658 struct got_pathlist_entry *pe;
660 TAILQ_INIT(&pathlist);
662 *tree = calloc(1, sizeof(**tree));
663 if (*tree == NULL)
664 return got_error_from_errno("calloc");
666 SIMPLEQ_INIT(&(*tree)->entries.head);
668 while (remain > 0) {
669 struct got_tree_entry *te;
670 struct got_pathlist_entry *new = NULL;
671 size_t elen;
673 err = parse_tree_entry(&te, &elen, buf, remain);
674 if (err)
675 goto done;
676 err = got_pathlist_insert(&new, &pathlist, te->name, te);
677 if (err)
678 goto done;
679 if (new == NULL) {
680 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
681 goto done;
683 buf += elen;
684 remain -= elen;
687 if (remain != 0) {
688 got_object_tree_close(*tree);
689 *tree = NULL;
690 err = got_error(GOT_ERR_BAD_OBJ_DATA);
691 goto done;
694 TAILQ_FOREACH(pe, &pathlist, entry) {
695 struct got_tree_entry *te = pe->data;
696 (*tree)->entries.nentries++;
697 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
699 done:
700 got_pathlist_free(&pathlist);
701 return err;
704 void
705 got_object_tag_close(struct got_tag_object *tag)
707 free(tag->tag);
708 free(tag->tagger);
709 free(tag->tagmsg);
710 free(tag);
713 const struct got_error *
714 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
716 const struct got_error *err = NULL;
717 size_t remain = len;
718 char *s = buf;
719 size_t label_len;
721 *tag = calloc(1, sizeof(**tag));
722 if (*tag == NULL)
723 return got_error_from_errno("calloc");
725 label_len = strlen(GOT_TAG_LABEL_OBJECT);
726 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
727 remain -= label_len;
728 if (remain < SHA1_DIGEST_STRING_LENGTH) {
729 err = got_error(GOT_ERR_BAD_OBJ_DATA);
730 goto done;
732 s += label_len;
733 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
734 err = got_error(GOT_ERR_BAD_OBJ_DATA);
735 goto done;
737 remain -= SHA1_DIGEST_STRING_LENGTH;
738 s += SHA1_DIGEST_STRING_LENGTH;
739 } else {
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
741 goto done;
744 if (remain <= 0) {
745 err = got_error(GOT_ERR_BAD_OBJ_DATA);
746 goto done;
749 label_len = strlen(GOT_TAG_LABEL_TYPE);
750 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
751 remain -= label_len;
752 if (remain <= 0) {
753 err = got_error(GOT_ERR_BAD_OBJ_DATA);
754 goto done;
756 s += label_len;
757 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
758 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
759 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
760 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
761 s += label_len;
762 remain -= label_len;
763 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
764 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
765 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
766 label_len = strlen(GOT_OBJ_LABEL_TREE);
767 s += label_len;
768 remain -= label_len;
769 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
770 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
771 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
772 label_len = strlen(GOT_OBJ_LABEL_BLOB);
773 s += label_len;
774 remain -= label_len;
775 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
776 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
777 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
778 label_len = strlen(GOT_OBJ_LABEL_TAG);
779 s += label_len;
780 remain -= label_len;
781 } else {
782 err = got_error(GOT_ERR_BAD_OBJ_DATA);
783 goto done;
786 if (remain <= 0 || *s != '\n') {
787 err = got_error(GOT_ERR_BAD_OBJ_DATA);
788 goto done;
790 s++;
791 remain--;
792 if (remain <= 0) {
793 err = got_error(GOT_ERR_BAD_OBJ_DATA);
794 goto done;
796 } else {
797 err = got_error(GOT_ERR_BAD_OBJ_DATA);
798 goto done;
801 label_len = strlen(GOT_TAG_LABEL_TAG);
802 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
803 char *p;
804 size_t slen;
805 remain -= label_len;
806 if (remain <= 0) {
807 err = got_error(GOT_ERR_BAD_OBJ_DATA);
808 goto done;
810 s += label_len;
811 p = memchr(s, '\n', remain);
812 if (p == NULL) {
813 err = got_error(GOT_ERR_BAD_OBJ_DATA);
814 goto done;
816 *p = '\0';
817 slen = strlen(s);
818 (*tag)->tag = strndup(s, slen);
819 if ((*tag)->tag == NULL) {
820 err = got_error_from_errno("strndup");
821 goto done;
823 s += slen + 1;
824 remain -= slen + 1;
825 if (remain <= 0) {
826 err = got_error(GOT_ERR_BAD_OBJ_DATA);
827 goto done;
829 } else {
830 err = got_error(GOT_ERR_BAD_OBJ_DATA);
831 goto done;
834 label_len = strlen(GOT_TAG_LABEL_TAGGER);
835 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
836 char *p;
837 size_t slen;
839 remain -= label_len;
840 if (remain <= 0) {
841 err = got_error(GOT_ERR_BAD_OBJ_DATA);
842 goto done;
844 s += label_len;
845 p = memchr(s, '\n', remain);
846 if (p == NULL) {
847 err = got_error(GOT_ERR_BAD_OBJ_DATA);
848 goto done;
850 *p = '\0';
851 slen = strlen(s);
852 err = parse_commit_time(&(*tag)->tagger_time,
853 &(*tag)->tagger_gmtoff, s);
854 if (err)
855 goto done;
856 (*tag)->tagger = strdup(s);
857 if ((*tag)->tagger == NULL) {
858 err = got_error_from_errno("strdup");
859 goto done;
861 s += slen + 1;
862 remain -= slen + 1;
863 if (remain <= 0) {
864 err = got_error(GOT_ERR_BAD_OBJ_DATA);
865 goto done;
867 } else {
868 /* Some old tags in the Linux git repo have no tagger. */
869 (*tag)->tagger = strdup("");
870 if ((*tag)->tagger == NULL) {
871 err = got_error_from_errno("strdup");
872 goto done;
876 (*tag)->tagmsg = strndup(s, remain);
877 if ((*tag)->tagmsg == NULL) {
878 err = got_error_from_errno("strndup");
879 goto done;
881 done:
882 if (err) {
883 got_object_tag_close(*tag);
884 *tag = NULL;
886 return err;
889 const struct got_error *
890 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
892 const struct got_error *err = NULL;
893 static const size_t blocksize = 512;
894 size_t n, total, remain;
895 uint8_t *buf;
897 *outbuf = NULL;
898 *outlen = 0;
900 buf = malloc(blocksize);
901 if (buf == NULL)
902 return got_error_from_errno("malloc");
904 remain = blocksize;
905 total = 0;
906 for (;;) {
907 if (remain == 0) {
908 uint8_t *newbuf;
909 newbuf = reallocarray(buf, 1, total + blocksize);
910 if (newbuf == NULL) {
911 err = got_error_from_errno("reallocarray");
912 goto done;
914 buf = newbuf;
915 remain += blocksize;
917 n = fread(buf + total, 1, remain, f);
918 if (n == 0) {
919 if (ferror(f)) {
920 err = got_ferror(f, GOT_ERR_IO);
921 goto done;
923 break; /* EOF */
925 remain -= n;
926 total += n;
927 };
929 done:
930 if (err == NULL) {
931 *outbuf = buf;
932 *outlen = total;
933 } else
934 free(buf);
935 return err;