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 void
132 got_object_id_queue_free(struct got_object_id_queue *ids)
134 struct got_object_qid *qid;
136 while (!SIMPLEQ_EMPTY(ids)) {
137 qid = SIMPLEQ_FIRST(ids);
138 SIMPLEQ_REMOVE_HEAD(ids, entry);
139 got_object_qid_free(qid);
143 const struct got_error *
144 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
146 const char *obj_labels[] = {
147 GOT_OBJ_LABEL_COMMIT,
148 GOT_OBJ_LABEL_TREE,
149 GOT_OBJ_LABEL_BLOB,
150 GOT_OBJ_LABEL_TAG,
151 };
152 const int obj_types[] = {
153 GOT_OBJ_TYPE_COMMIT,
154 GOT_OBJ_TYPE_TREE,
155 GOT_OBJ_TYPE_BLOB,
156 GOT_OBJ_TYPE_TAG,
157 };
158 int type = 0;
159 size_t size = 0, hdrlen = 0;
160 int i;
162 *obj = NULL;
164 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
165 if (hdrlen > len)
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
168 for (i = 0; i < nitems(obj_labels); i++) {
169 const char *label = obj_labels[i];
170 size_t label_len = strlen(label);
171 const char *errstr;
173 if (strncmp(buf, label, label_len) != 0)
174 continue;
176 type = obj_types[i];
177 if (len <= label_len)
178 return got_error(GOT_ERR_BAD_OBJ_HDR);
179 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
180 if (errstr != NULL)
181 return got_error(GOT_ERR_BAD_OBJ_HDR);
182 break;
185 if (type == 0)
186 return got_error(GOT_ERR_BAD_OBJ_HDR);
188 *obj = calloc(1, sizeof(**obj));
189 if (*obj == NULL)
190 return got_error_from_errno("calloc");
191 (*obj)->type = type;
192 (*obj)->hdrlen = hdrlen;
193 (*obj)->size = size;
194 return NULL;
197 const struct got_error *
198 got_object_read_header(struct got_object **obj, int fd)
200 const struct got_error *err;
201 struct got_inflate_buf zb;
202 char *buf;
203 const size_t zbsize = 64;
204 size_t outlen, totlen;
205 int nbuf = 1;
207 *obj = NULL;
209 buf = malloc(zbsize);
210 if (buf == NULL)
211 return got_error_from_errno("malloc");
213 err = got_inflate_init(&zb, buf, zbsize);
214 if (err)
215 return err;
217 totlen = 0;
218 do {
219 err = got_inflate_read_fd(&zb, fd, &outlen);
220 if (err)
221 goto done;
222 if (outlen == 0)
223 break;
224 totlen += outlen;
225 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
226 char *newbuf;
227 nbuf++;
228 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
229 if (newbuf == NULL) {
230 err = got_error_from_errno("recallocarray");
231 goto done;
233 buf = newbuf;
234 zb.outbuf = newbuf + totlen;
235 zb.outlen = (nbuf * zbsize) - totlen;
237 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
239 err = got_object_parse_header(obj, buf, totlen);
240 done:
241 free(buf);
242 got_inflate_end(&zb);
243 return err;
246 struct got_commit_object *
247 got_object_commit_alloc_partial(void)
249 struct got_commit_object *commit;
251 commit = calloc(1, sizeof(*commit));
252 if (commit == NULL)
253 return NULL;
254 commit->tree_id = malloc(sizeof(*commit->tree_id));
255 if (commit->tree_id == NULL) {
256 free(commit);
257 return NULL;
260 SIMPLEQ_INIT(&commit->parent_ids);
262 return commit;
265 const struct got_error *
266 got_object_commit_add_parent(struct got_commit_object *commit,
267 const char *id_str)
269 const struct got_error *err = NULL;
270 struct got_object_qid *qid;
272 err = got_object_qid_alloc_partial(&qid);
273 if (err)
274 return err;
276 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
277 err = got_error(GOT_ERR_BAD_OBJ_DATA);
278 free(qid->id);
279 free(qid);
280 return err;
283 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
284 commit->nparents++;
286 return NULL;
289 static const struct got_error *
290 parse_gmtoff(time_t *gmtoff, const char *tzstr)
292 int sign = 1;
293 const char *p = tzstr;
294 time_t h, m;
296 *gmtoff = 0;
298 if (*p == '-')
299 sign = -1;
300 else if (*p != '+')
301 return got_error(GOT_ERR_BAD_OBJ_DATA);
302 p++;
303 if (!isdigit(*p) && !isdigit(*(p + 1)))
304 return got_error(GOT_ERR_BAD_OBJ_DATA);
305 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
307 p += 2;
308 if (!isdigit(*p) && !isdigit(*(p + 1)))
309 return got_error(GOT_ERR_BAD_OBJ_DATA);
310 m = ((*p - '0') * 10) + (*(p + 1) - '0');
312 *gmtoff = (h * 60 * 60 + m * 60) * sign;
313 return NULL;
316 static const struct got_error *
317 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
319 const struct got_error *err = NULL;
320 const char *errstr;
321 char *space, *tzstr;
323 /* Parse and strip off trailing timezone indicator string. */
324 space = strrchr(committer, ' ');
325 if (space == NULL)
326 return got_error(GOT_ERR_BAD_OBJ_DATA);
327 tzstr = strdup(space + 1);
328 if (tzstr == NULL)
329 return got_error_from_errno("strdup");
330 err = parse_gmtoff(gmtoff, tzstr);
331 free(tzstr);
332 if (err)
333 return err;
334 *space = '\0';
336 /* Timestamp is separated from committer name + email by space. */
337 space = strrchr(committer, ' ');
338 if (space == NULL)
339 return got_error(GOT_ERR_BAD_OBJ_DATA);
341 /* Timestamp parsed here is expressed in comitter's local time. */
342 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
343 if (errstr)
344 return got_error(GOT_ERR_BAD_OBJ_DATA);
346 /* Express the time stamp in UTC. */
347 *time -= *gmtoff;
349 /* Strip off parsed time information, leaving just author and email. */
350 *space = '\0';
352 return NULL;
355 void
356 got_object_commit_close(struct got_commit_object *commit)
358 if (commit->refcnt > 0) {
359 commit->refcnt--;
360 if (commit->refcnt > 0)
361 return;
364 got_object_id_queue_free(&commit->parent_ids);
365 free(commit->tree_id);
366 free(commit->author);
367 free(commit->committer);
368 free(commit->logmsg);
369 free(commit);
372 struct got_object_id *
373 got_object_commit_get_tree_id(struct got_commit_object *commit)
375 return commit->tree_id;
378 int
379 got_object_commit_get_nparents(struct got_commit_object *commit)
381 return commit->nparents;
384 const struct got_object_id_queue *
385 got_object_commit_get_parent_ids(struct got_commit_object *commit)
387 return &commit->parent_ids;
390 const char *
391 got_object_commit_get_author(struct got_commit_object *commit)
393 return commit->author;
396 time_t
397 got_object_commit_get_author_time(struct got_commit_object *commit)
399 return commit->author_time;
402 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
404 return commit->author_gmtoff;
407 const char *
408 got_object_commit_get_committer(struct got_commit_object *commit)
410 return commit->committer;
413 time_t
414 got_object_commit_get_committer_time(struct got_commit_object *commit)
416 return commit->committer_time;
419 time_t
420 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
422 return commit->committer_gmtoff;
425 const char *
426 got_object_commit_get_logmsg(struct got_commit_object *commit)
428 return commit->logmsg;
431 const struct got_error *
432 got_object_parse_commit(struct got_commit_object **commit, char *buf,
433 size_t len)
435 const struct got_error *err = NULL;
436 char *s = buf;
437 size_t label_len;
438 ssize_t remain = (ssize_t)len;
440 *commit = got_object_commit_alloc_partial();
441 if (*commit == NULL)
442 return got_error_from_errno("got_object_commit_alloc_partial");
444 label_len = strlen(GOT_COMMIT_LABEL_TREE);
445 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
446 remain -= label_len;
447 if (remain < SHA1_DIGEST_STRING_LENGTH) {
448 err = got_error(GOT_ERR_BAD_OBJ_DATA);
449 goto done;
451 s += label_len;
452 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
453 err = got_error(GOT_ERR_BAD_OBJ_DATA);
454 goto done;
456 remain -= SHA1_DIGEST_STRING_LENGTH;
457 s += SHA1_DIGEST_STRING_LENGTH;
458 } else {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
460 goto done;
463 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
464 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
465 remain -= label_len;
466 if (remain < SHA1_DIGEST_STRING_LENGTH) {
467 err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 goto done;
470 s += label_len;
471 err = got_object_commit_add_parent(*commit, s);
472 if (err)
473 goto done;
475 remain -= SHA1_DIGEST_STRING_LENGTH;
476 s += SHA1_DIGEST_STRING_LENGTH;
479 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
480 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
481 char *p;
482 size_t slen;
484 remain -= label_len;
485 if (remain <= 0) {
486 err = got_error(GOT_ERR_BAD_OBJ_DATA);
487 goto done;
489 s += label_len;
490 p = memchr(s, '\n', remain);
491 if (p == NULL) {
492 err = got_error(GOT_ERR_BAD_OBJ_DATA);
493 goto done;
495 *p = '\0';
496 slen = strlen(s);
497 err = parse_commit_time(&(*commit)->author_time,
498 &(*commit)->author_gmtoff, s);
499 if (err)
500 goto done;
501 (*commit)->author = strdup(s);
502 if ((*commit)->author == NULL) {
503 err = got_error_from_errno("strdup");
504 goto done;
506 s += slen + 1;
507 remain -= slen + 1;
510 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
511 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
512 char *p;
513 size_t slen;
515 remain -= label_len;
516 if (remain <= 0) {
517 err = got_error(GOT_ERR_BAD_OBJ_DATA);
518 goto done;
520 s += label_len;
521 p = memchr(s, '\n', remain);
522 if (p == NULL) {
523 err = got_error(GOT_ERR_BAD_OBJ_DATA);
524 goto done;
526 *p = '\0';
527 slen = strlen(s);
528 err = parse_commit_time(&(*commit)->committer_time,
529 &(*commit)->committer_gmtoff, s);
530 if (err)
531 goto done;
532 (*commit)->committer = strdup(s);
533 if ((*commit)->committer == NULL) {
534 err = got_error_from_errno("strdup");
535 goto done;
537 s += slen + 1;
538 remain -= slen + 1;
541 (*commit)->logmsg = strndup(s, remain);
542 if ((*commit)->logmsg == NULL) {
543 err = got_error_from_errno("strndup");
544 goto done;
546 done:
547 if (err) {
548 got_object_commit_close(*commit);
549 *commit = NULL;
551 return err;
554 void
555 got_object_tree_entry_close(struct got_tree_entry *te)
557 free(te->id);
558 free(te->name);
559 free(te);
562 void
563 got_object_tree_entries_close(struct got_tree_entries *entries)
565 struct got_tree_entry *te;
567 while (!SIMPLEQ_EMPTY(&entries->head)) {
568 te = SIMPLEQ_FIRST(&entries->head);
569 SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
570 got_object_tree_entry_close(te);
574 void
575 got_object_tree_close(struct got_tree_object *tree)
577 if (tree->refcnt > 0) {
578 tree->refcnt--;
579 if (tree->refcnt > 0)
580 return;
583 got_object_tree_entries_close(&tree->entries);
584 free(tree);
587 struct got_tree_entry *
588 got_alloc_tree_entry_partial(void)
590 struct got_tree_entry *te;
592 te = malloc(sizeof(*te));
593 if (te == NULL)
594 return NULL;
596 te->id = malloc(sizeof(*te->id));
597 if (te->id == NULL) {
598 free(te);
599 te = NULL;
601 return te;
604 static const struct got_error *
605 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
606 size_t maxlen)
608 char *p, *space;
609 const struct got_error *err = NULL;
611 *te = got_alloc_tree_entry_partial();
612 if (*te == NULL)
613 return got_error_from_errno("got_alloc_tree_entry_partial");
615 *elen = strnlen(buf, maxlen) + 1;
616 if (*elen > maxlen) {
617 free(*te);
618 *te = NULL;
619 return got_error(GOT_ERR_BAD_OBJ_DATA);
622 space = memchr(buf, ' ', *elen);
623 if (space == NULL || space <= buf) {
624 err = got_error(GOT_ERR_BAD_OBJ_DATA);
625 free(*te);
626 *te = NULL;
627 return err;
629 (*te)->mode = 0;
630 p = buf;
631 while (p < space) {
632 if (*p < '0' && *p > '7') {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 goto done;
636 (*te)->mode <<= 3;
637 (*te)->mode |= *p - '0';
638 p++;
641 (*te)->name = strdup(space + 1);
642 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
643 err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 goto done;
646 buf += *elen;
647 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
648 *elen += SHA1_DIGEST_LENGTH;
649 done:
650 if (err) {
651 got_object_tree_entry_close(*te);
652 *te = NULL;
654 return err;
657 const struct got_error *
658 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
660 const struct got_error *err;
661 size_t remain = len;
662 struct got_pathlist_head pathlist;
663 struct got_pathlist_entry *pe;
665 TAILQ_INIT(&pathlist);
667 *tree = calloc(1, sizeof(**tree));
668 if (*tree == NULL)
669 return got_error_from_errno("calloc");
671 SIMPLEQ_INIT(&(*tree)->entries.head);
673 while (remain > 0) {
674 struct got_tree_entry *te;
675 struct got_pathlist_entry *new = NULL;
676 size_t elen;
678 err = parse_tree_entry(&te, &elen, buf, remain);
679 if (err)
680 goto done;
681 err = got_pathlist_insert(&new, &pathlist, te->name, te);
682 if (err)
683 goto done;
684 if (new == NULL) {
685 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
686 goto done;
688 buf += elen;
689 remain -= elen;
692 if (remain != 0) {
693 got_object_tree_close(*tree);
694 *tree = NULL;
695 err = got_error(GOT_ERR_BAD_OBJ_DATA);
696 goto done;
699 TAILQ_FOREACH(pe, &pathlist, entry) {
700 struct got_tree_entry *te = pe->data;
701 (*tree)->entries.nentries++;
702 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
704 done:
705 got_pathlist_free(&pathlist);
706 return err;
709 void
710 got_object_tag_close(struct got_tag_object *tag)
712 free(tag->tag);
713 free(tag->tagger);
714 free(tag->tagmsg);
715 free(tag);
718 const struct got_error *
719 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
721 const struct got_error *err = NULL;
722 size_t remain = len;
723 char *s = buf;
724 size_t label_len;
726 *tag = calloc(1, sizeof(**tag));
727 if (*tag == NULL)
728 return got_error_from_errno("calloc");
730 label_len = strlen(GOT_TAG_LABEL_OBJECT);
731 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
732 remain -= label_len;
733 if (remain < SHA1_DIGEST_STRING_LENGTH) {
734 err = got_error(GOT_ERR_BAD_OBJ_DATA);
735 goto done;
737 s += label_len;
738 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
739 err = got_error(GOT_ERR_BAD_OBJ_DATA);
740 goto done;
742 remain -= SHA1_DIGEST_STRING_LENGTH;
743 s += SHA1_DIGEST_STRING_LENGTH;
744 } else {
745 err = got_error(GOT_ERR_BAD_OBJ_DATA);
746 goto done;
749 if (remain <= 0) {
750 err = got_error(GOT_ERR_BAD_OBJ_DATA);
751 goto done;
754 label_len = strlen(GOT_TAG_LABEL_TYPE);
755 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
756 remain -= label_len;
757 if (remain <= 0) {
758 err = got_error(GOT_ERR_BAD_OBJ_DATA);
759 goto done;
761 s += label_len;
762 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
763 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
764 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
765 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
766 s += label_len;
767 remain -= label_len;
768 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
769 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
770 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
771 label_len = strlen(GOT_OBJ_LABEL_TREE);
772 s += label_len;
773 remain -= label_len;
774 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
775 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
776 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
777 label_len = strlen(GOT_OBJ_LABEL_BLOB);
778 s += label_len;
779 remain -= label_len;
780 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
781 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
782 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
783 label_len = strlen(GOT_OBJ_LABEL_TAG);
784 s += label_len;
785 remain -= label_len;
786 } else {
787 err = got_error(GOT_ERR_BAD_OBJ_DATA);
788 goto done;
791 if (remain <= 0 || *s != '\n') {
792 err = got_error(GOT_ERR_BAD_OBJ_DATA);
793 goto done;
795 s++;
796 remain--;
797 if (remain <= 0) {
798 err = got_error(GOT_ERR_BAD_OBJ_DATA);
799 goto done;
801 } else {
802 err = got_error(GOT_ERR_BAD_OBJ_DATA);
803 goto done;
806 label_len = strlen(GOT_TAG_LABEL_TAG);
807 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
808 char *p;
809 size_t slen;
810 remain -= label_len;
811 if (remain <= 0) {
812 err = got_error(GOT_ERR_BAD_OBJ_DATA);
813 goto done;
815 s += label_len;
816 p = memchr(s, '\n', remain);
817 if (p == NULL) {
818 err = got_error(GOT_ERR_BAD_OBJ_DATA);
819 goto done;
821 *p = '\0';
822 slen = strlen(s);
823 (*tag)->tag = strndup(s, slen);
824 if ((*tag)->tag == NULL) {
825 err = got_error_from_errno("strndup");
826 goto done;
828 s += slen + 1;
829 remain -= slen + 1;
830 if (remain <= 0) {
831 err = got_error(GOT_ERR_BAD_OBJ_DATA);
832 goto done;
834 } else {
835 err = got_error(GOT_ERR_BAD_OBJ_DATA);
836 goto done;
839 label_len = strlen(GOT_TAG_LABEL_TAGGER);
840 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
841 char *p;
842 size_t slen;
844 remain -= label_len;
845 if (remain <= 0) {
846 err = got_error(GOT_ERR_BAD_OBJ_DATA);
847 goto done;
849 s += label_len;
850 p = memchr(s, '\n', remain);
851 if (p == NULL) {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
853 goto done;
855 *p = '\0';
856 slen = strlen(s);
857 err = parse_commit_time(&(*tag)->tagger_time,
858 &(*tag)->tagger_gmtoff, s);
859 if (err)
860 goto done;
861 (*tag)->tagger = strdup(s);
862 if ((*tag)->tagger == NULL) {
863 err = got_error_from_errno("strdup");
864 goto done;
866 s += slen + 1;
867 remain -= slen + 1;
868 if (remain <= 0) {
869 err = got_error(GOT_ERR_BAD_OBJ_DATA);
870 goto done;
872 } else {
873 /* Some old tags in the Linux git repo have no tagger. */
874 (*tag)->tagger = strdup("");
875 if ((*tag)->tagger == NULL) {
876 err = got_error_from_errno("strdup");
877 goto done;
881 (*tag)->tagmsg = strndup(s, remain);
882 if ((*tag)->tagmsg == NULL) {
883 err = got_error_from_errno("strndup");
884 goto done;
886 done:
887 if (err) {
888 got_object_tag_close(*tag);
889 *tag = NULL;
891 return err;
894 const struct got_error *
895 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
897 const struct got_error *err = NULL;
898 static const size_t blocksize = 512;
899 size_t n, total, remain;
900 uint8_t *buf;
902 *outbuf = NULL;
903 *outlen = 0;
905 buf = malloc(blocksize);
906 if (buf == NULL)
907 return got_error_from_errno("malloc");
909 remain = blocksize;
910 total = 0;
911 for (;;) {
912 if (remain == 0) {
913 uint8_t *newbuf;
914 newbuf = reallocarray(buf, 1, total + blocksize);
915 if (newbuf == NULL) {
916 err = got_error_from_errno("reallocarray");
917 goto done;
919 buf = newbuf;
920 remain += blocksize;
922 n = fread(buf + total, 1, remain, f);
923 if (n == 0) {
924 if (ferror(f)) {
925 err = got_ferror(f, GOT_ERR_IO);
926 goto done;
928 break; /* EOF */
930 remain -= n;
931 total += n;
932 };
934 done:
935 if (err == NULL) {
936 *outbuf = buf;
937 *outlen = total;
938 } else
939 free(buf);
940 return err;