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"
52 #ifndef nitems
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #endif
56 #define GOT_OBJ_TAG_COMMIT "commit"
57 #define GOT_OBJ_TAG_TREE "tree"
58 #define GOT_OBJ_TAG_BLOB "blob"
59 #define GOT_OBJ_TAG_TAG "tag"
61 #define GOT_COMMIT_TAG_TREE "tree "
62 #define GOT_COMMIT_TAG_PARENT "parent "
63 #define GOT_COMMIT_TAG_AUTHOR "author "
64 #define GOT_COMMIT_TAG_COMMITTER "committer "
66 #define GOT_TAG_TAG_OBJECT "object "
67 #define GOT_TAG_TAG_TYPE "type "
68 #define GOT_TAG_TAG_TAG "tag "
69 #define GOT_TAG_TAG_TAGGER "tagger "
71 int
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
74 {
75 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
76 }
78 const struct got_error *
79 got_object_qid_alloc_partial(struct got_object_qid **qid)
80 {
81 const struct got_error *err = NULL;
83 *qid = malloc(sizeof(**qid));
84 if (*qid == NULL)
85 return got_error_from_errno();
87 (*qid)->id = malloc(sizeof(*((*qid)->id)));
88 if ((*qid)->id == NULL) {
89 err = got_error_from_errno();
90 got_object_qid_free(*qid);
91 *qid = NULL;
92 return err;
93 }
95 return NULL;
96 }
98 const struct got_error *
99 got_object_id_str(char **outbuf, struct got_object_id *id)
101 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
103 *outbuf = malloc(len);
104 if (*outbuf == NULL)
105 return got_error_from_errno();
107 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
108 free(*outbuf);
109 *outbuf = NULL;
110 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
113 return NULL;
116 void
117 got_object_close(struct got_object *obj)
119 if (obj->refcnt > 0) {
120 obj->refcnt--;
121 if (obj->refcnt > 0)
122 return;
125 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
126 struct got_delta *delta;
127 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
128 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
129 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
130 got_delta_close(delta);
133 if (obj->flags & GOT_OBJ_FLAG_PACKED)
134 free(obj->path_packfile);
135 free(obj);
138 void
139 got_object_qid_free(struct got_object_qid *qid)
141 free(qid->id);
142 free(qid);
145 const struct got_error *
146 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
148 const char *obj_tags[] = {
149 GOT_OBJ_TAG_COMMIT,
150 GOT_OBJ_TAG_TREE,
151 GOT_OBJ_TAG_BLOB,
152 GOT_OBJ_TAG_TAG,
153 };
154 const int obj_types[] = {
155 GOT_OBJ_TYPE_COMMIT,
156 GOT_OBJ_TYPE_TREE,
157 GOT_OBJ_TYPE_BLOB,
158 GOT_OBJ_TYPE_TAG,
159 };
160 int type = 0;
161 size_t size = 0, hdrlen = 0;
162 int i;
163 char *p = strchr(buf, '\0');
165 *obj = NULL;
167 if (p == NULL)
168 return got_error(GOT_ERR_BAD_OBJ_HDR);
170 hdrlen = strlen(buf) + 1 /* '\0' */;
172 for (i = 0; i < nitems(obj_tags); i++) {
173 const char *tag = obj_tags[i];
174 size_t tlen = strlen(tag);
175 const char *errstr;
177 if (strncmp(buf, tag, tlen) != 0)
178 continue;
180 type = obj_types[i];
181 if (len <= tlen)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
184 if (errstr != NULL)
185 return got_error(GOT_ERR_BAD_OBJ_HDR);
186 break;
189 if (type == 0)
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 *obj = calloc(1, sizeof(**obj));
193 if (*obj == NULL)
194 return got_error_from_errno();
195 (*obj)->type = type;
196 (*obj)->hdrlen = hdrlen;
197 (*obj)->size = size;
198 return NULL;
201 const struct got_error *
202 got_object_read_header(struct got_object **obj, int fd)
204 const struct got_error *err;
205 struct got_zstream_buf zb;
206 char *buf;
207 const size_t zbsize = 64;
208 size_t outlen, totlen;
209 int nbuf = 1;
211 *obj = NULL;
213 buf = malloc(zbsize);
214 if (buf == NULL)
215 return got_error_from_errno();
217 err = got_inflate_init(&zb, buf, zbsize);
218 if (err)
219 return err;
221 totlen = 0;
222 do {
223 err = got_inflate_read_fd(&zb, fd, &outlen);
224 if (err)
225 goto done;
226 if (outlen == 0)
227 break;
228 totlen += outlen;
229 if (strchr(zb.outbuf, '\0') == NULL) {
230 char *newbuf;
231 nbuf++;
232 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
233 if (newbuf == NULL) {
234 err = got_error_from_errno();
235 goto done;
237 buf = newbuf;
238 zb.outbuf = newbuf + totlen;
239 zb.outlen = (nbuf * zbsize) - totlen;
241 } while (strchr(zb.outbuf, '\0') == NULL);
243 err = got_object_parse_header(obj, buf, totlen);
244 done:
245 free(buf);
246 got_inflate_end(&zb);
247 return err;
250 struct got_commit_object *
251 got_object_commit_alloc_partial(void)
253 struct got_commit_object *commit;
255 commit = calloc(1, sizeof(*commit));
256 if (commit == NULL)
257 return NULL;
258 commit->tree_id = malloc(sizeof(*commit->tree_id));
259 if (commit->tree_id == NULL) {
260 free(commit);
261 return NULL;
264 SIMPLEQ_INIT(&commit->parent_ids);
266 return commit;
269 const struct got_error *
270 got_object_commit_add_parent(struct got_commit_object *commit,
271 const char *id_str)
273 const struct got_error *err = NULL;
274 struct got_object_qid *qid;
276 err = got_object_qid_alloc_partial(&qid);
277 if (err)
278 return err;
280 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
281 err = got_error(GOT_ERR_BAD_OBJ_DATA);
282 free(qid->id);
283 free(qid);
284 return err;
287 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
288 commit->nparents++;
290 return NULL;
293 static const struct got_error *
294 parse_gmtoff(time_t *gmtoff, const char *tzstr)
296 int sign = 1;
297 const char *p = tzstr;
298 time_t h, m;
300 *gmtoff = 0;
302 if (*p == '-')
303 sign = -1;
304 else if (*p != '+')
305 return got_error(GOT_ERR_BAD_OBJ_DATA);
306 p++;
307 if (!isdigit(*p) && !isdigit(*(p + 1)))
308 return got_error(GOT_ERR_BAD_OBJ_DATA);
309 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
311 p += 2;
312 if (!isdigit(*p) && !isdigit(*(p + 1)))
313 return got_error(GOT_ERR_BAD_OBJ_DATA);
314 m = ((*p - '0') * 10) + (*(p + 1) - '0');
316 *gmtoff = (h * 60 * 60 + m * 60) * sign;
317 return NULL;
320 static const struct got_error *
321 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
323 const struct got_error *err = NULL;
324 const char *errstr;
325 char *space, *tzstr;
327 /* Parse and strip off trailing timezone indicator string. */
328 space = strrchr(committer, ' ');
329 if (space == NULL)
330 return got_error(GOT_ERR_BAD_OBJ_DATA);
331 tzstr = strdup(space + 1);
332 if (tzstr == NULL)
333 return got_error_from_errno();
334 err = parse_gmtoff(gmtoff, tzstr);
335 free(tzstr);
336 if (err)
337 return err;
338 *space = '\0';
340 /* Timestamp is separated from committer name + email by space. */
341 space = strrchr(committer, ' ');
342 if (space == NULL)
343 return got_error(GOT_ERR_BAD_OBJ_DATA);
345 /* Timestamp parsed here is expressed in comitter's local time. */
346 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
347 if (errstr)
348 return got_error(GOT_ERR_BAD_OBJ_DATA);
350 /* Express the time stamp in UTC. */
351 *time -= *gmtoff;
353 /* Strip off parsed time information, leaving just author and email. */
354 *space = '\0';
356 return NULL;
359 void
360 got_object_commit_close(struct got_commit_object *commit)
362 struct got_object_qid *qid;
364 if (commit->refcnt > 0) {
365 commit->refcnt--;
366 if (commit->refcnt > 0)
367 return;
370 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
371 qid = SIMPLEQ_FIRST(&commit->parent_ids);
372 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
373 got_object_qid_free(qid);
376 free(commit->tree_id);
377 free(commit->author);
378 free(commit->committer);
379 free(commit->logmsg);
380 free(commit);
383 struct got_object_id *
384 got_object_commit_get_tree_id(struct got_commit_object *commit)
386 return commit->tree_id;
389 int
390 got_object_commit_get_nparents(struct got_commit_object *commit)
392 return commit->nparents;
395 const struct got_object_id_queue *
396 got_object_commit_get_parent_ids(struct got_commit_object *commit)
398 return &commit->parent_ids;
401 const char *
402 got_object_commit_get_author(struct got_commit_object *commit)
404 return commit->author;
407 time_t
408 got_object_commit_get_author_time(struct got_commit_object *commit)
410 return commit->author_time;
413 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
415 return commit->author_gmtoff;
418 const char *
419 got_object_commit_get_committer(struct got_commit_object *commit)
421 return commit->committer;
424 time_t
425 got_object_commit_get_committer_time(struct got_commit_object *commit)
427 return commit->committer_time;
430 time_t
431 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
433 return commit->committer_gmtoff;
436 const char *
437 got_object_commit_get_logmsg(struct got_commit_object *commit)
439 return commit->logmsg;
442 const struct got_error *
443 got_object_parse_commit(struct got_commit_object **commit, char *buf,
444 size_t len)
446 const struct got_error *err = NULL;
447 char *s = buf;
448 size_t tlen;
449 ssize_t remain = (ssize_t)len;
451 *commit = got_object_commit_alloc_partial();
452 if (*commit == NULL)
453 return got_error_from_errno();
455 tlen = strlen(GOT_COMMIT_TAG_TREE);
456 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
457 remain -= tlen;
458 if (remain < SHA1_DIGEST_STRING_LENGTH) {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
460 goto done;
462 s += tlen;
463 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
464 err = got_error(GOT_ERR_BAD_OBJ_DATA);
465 goto done;
467 remain -= SHA1_DIGEST_STRING_LENGTH;
468 s += SHA1_DIGEST_STRING_LENGTH;
469 } else {
470 err = got_error(GOT_ERR_BAD_OBJ_DATA);
471 goto done;
474 tlen = strlen(GOT_COMMIT_TAG_PARENT);
475 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
476 remain -= tlen;
477 if (remain < SHA1_DIGEST_STRING_LENGTH) {
478 err = got_error(GOT_ERR_BAD_OBJ_DATA);
479 goto done;
481 s += tlen;
482 err = got_object_commit_add_parent(*commit, s);
483 if (err)
484 goto done;
486 remain -= SHA1_DIGEST_STRING_LENGTH;
487 s += SHA1_DIGEST_STRING_LENGTH;
490 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
491 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
492 char *p;
493 size_t slen;
495 remain -= tlen;
496 if (remain <= 0) {
497 err = got_error(GOT_ERR_BAD_OBJ_DATA);
498 goto done;
500 s += tlen;
501 p = strchr(s, '\n');
502 if (p == NULL) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 goto done;
506 *p = '\0';
507 slen = strlen(s);
508 err = parse_commit_time(&(*commit)->author_time,
509 &(*commit)->author_gmtoff, s);
510 if (err)
511 goto done;
512 (*commit)->author = strdup(s);
513 if ((*commit)->author == NULL) {
514 err = got_error_from_errno();
515 goto done;
517 s += slen + 1;
518 remain -= slen + 1;
521 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
522 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
523 char *p;
524 size_t slen;
526 remain -= tlen;
527 if (remain <= 0) {
528 err = got_error(GOT_ERR_BAD_OBJ_DATA);
529 goto done;
531 s += tlen;
532 p = strchr(s, '\n');
533 if (p == NULL) {
534 err = got_error(GOT_ERR_BAD_OBJ_DATA);
535 goto done;
537 *p = '\0';
538 slen = strlen(s);
539 err = parse_commit_time(&(*commit)->committer_time,
540 &(*commit)->committer_gmtoff, s);
541 if (err)
542 goto done;
543 (*commit)->committer = strdup(s);
544 if ((*commit)->committer == NULL) {
545 err = got_error_from_errno();
546 goto done;
548 s += slen + 1;
549 remain -= slen + 1;
552 (*commit)->logmsg = strndup(s, remain);
553 if ((*commit)->logmsg == NULL) {
554 err = got_error_from_errno();
555 goto done;
557 done:
558 if (err) {
559 got_object_commit_close(*commit);
560 *commit = NULL;
562 return err;
565 void
566 got_object_tree_entry_close(struct got_tree_entry *te)
568 free(te->id);
569 free(te->name);
570 free(te);
573 void
574 got_object_tree_close(struct got_tree_object *tree)
576 struct got_tree_entry *te;
578 if (tree->refcnt > 0) {
579 tree->refcnt--;
580 if (tree->refcnt > 0)
581 return;
584 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
585 te = SIMPLEQ_FIRST(&tree->entries.head);
586 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
587 got_object_tree_entry_close(te);
590 free(tree);
593 struct got_tree_entry *
594 got_alloc_tree_entry_partial(void)
596 struct got_tree_entry *te;
598 te = malloc(sizeof(*te));
599 if (te == NULL)
600 return NULL;
602 te->id = malloc(sizeof(*te->id));
603 if (te->id == NULL) {
604 free(te);
605 te = NULL;
607 return te;
610 static const struct got_error *
611 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
612 size_t maxlen)
614 char *p = buf, *space;
615 const struct got_error *err = NULL;
617 *te = got_alloc_tree_entry_partial();
618 if (*te == NULL)
619 return got_error_from_errno();
621 *elen = strlen(buf) + 1;
622 if (*elen > maxlen) {
623 free(*te);
624 *te = NULL;
625 return got_error(GOT_ERR_BAD_OBJ_DATA);
628 space = strchr(buf, ' ');
629 if (space == NULL) {
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
631 free(*te);
632 *te = NULL;
633 return err;
635 (*te)->mode = 0;
636 while (*p != ' ') {
637 if (*p < '0' && *p > '7') {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
639 goto done;
641 (*te)->mode <<= 3;
642 (*te)->mode |= *p - '0';
643 p++;
646 (*te)->name = strdup(space + 1);
647 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
648 err = got_error(GOT_ERR_BAD_OBJ_DATA);
649 goto done;
651 buf += *elen;
652 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
653 *elen += SHA1_DIGEST_LENGTH;
654 done:
655 if (err) {
656 got_object_tree_entry_close(*te);
657 *te = NULL;
659 return err;
662 const struct got_error *
663 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
665 const struct got_error *err;
666 size_t remain = len;
668 *tree = calloc(1, sizeof(**tree));
669 if (*tree == NULL)
670 return got_error_from_errno();
672 SIMPLEQ_INIT(&(*tree)->entries.head);
674 while (remain > 0) {
675 struct got_tree_entry *te;
676 size_t elen;
678 err = parse_tree_entry(&te, &elen, buf, remain);
679 if (err)
680 return err;
681 (*tree)->entries.nentries++;
682 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
683 buf += elen;
684 remain -= elen;
687 if (remain != 0) {
688 got_object_tree_close(*tree);
689 *tree = NULL;
690 return got_error(GOT_ERR_BAD_OBJ_DATA);
693 return NULL;
696 void
697 got_object_tag_close(struct got_tag_object *tag)
699 free(tag->tag);
700 free(tag->tagger);
701 free(tag->tagmsg);
702 free(tag);
705 const struct got_error *
706 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
708 const struct got_error *err = NULL;
709 size_t remain = len;
710 char *s = buf;
711 size_t tlen;
713 *tag = calloc(1, sizeof(**tag));
714 if (*tag == NULL)
715 return got_error_from_errno();
717 tlen = strlen(GOT_TAG_TAG_OBJECT);
718 if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
719 remain -= tlen;
720 if (remain < SHA1_DIGEST_STRING_LENGTH) {
721 err = got_error(GOT_ERR_BAD_OBJ_DATA);
722 goto done;
724 s += tlen;
725 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
726 err = got_error(GOT_ERR_BAD_OBJ_DATA);
727 goto done;
729 remain -= SHA1_DIGEST_STRING_LENGTH;
730 s += SHA1_DIGEST_STRING_LENGTH;
731 } else {
732 err = got_error(GOT_ERR_BAD_OBJ_DATA);
733 goto done;
736 if (remain <= 0) {
737 err = got_error(GOT_ERR_BAD_OBJ_DATA);
738 goto done;
741 tlen = strlen(GOT_TAG_TAG_TYPE);
742 if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
743 remain -= tlen;
744 if (remain <= 0) {
745 err = got_error(GOT_ERR_BAD_OBJ_DATA);
746 goto done;
748 s += tlen;
749 if (strncmp(s, GOT_OBJ_TAG_COMMIT,
750 strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
751 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
752 tlen = strlen(GOT_OBJ_TAG_COMMIT);
753 s += tlen;
754 remain -= tlen;
755 } else if (strncmp(s, GOT_OBJ_TAG_TREE,
756 strlen(GOT_OBJ_TAG_TREE)) == 0) {
757 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
758 tlen = strlen(GOT_OBJ_TAG_TREE);
759 s += tlen;
760 remain -= tlen;
761 } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
762 strlen(GOT_OBJ_TAG_BLOB)) == 0) {
763 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
764 tlen = strlen(GOT_OBJ_TAG_BLOB);
765 s += tlen;
766 remain -= tlen;
767 } else if (strncmp(s, GOT_OBJ_TAG_TAG,
768 strlen(GOT_OBJ_TAG_TAG)) == 0) {
769 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
770 tlen = strlen(GOT_OBJ_TAG_TAG);
771 s += tlen;
772 remain -= tlen;
773 } else {
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
775 goto done;
778 if (remain <= 0 || *s != '\n') {
779 err = got_error(GOT_ERR_BAD_OBJ_DATA);
780 goto done;
782 s++;
783 remain--;
784 if (remain <= 0) {
785 err = got_error(GOT_ERR_BAD_OBJ_DATA);
786 goto done;
788 } else {
789 err = got_error(GOT_ERR_BAD_OBJ_DATA);
790 goto done;
793 tlen = strlen(GOT_TAG_TAG_TAG);
794 if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
795 char *p;
796 size_t slen;
797 remain -= tlen;
798 if (remain <= 0) {
799 err = got_error(GOT_ERR_BAD_OBJ_DATA);
800 goto done;
802 s += tlen;
803 p = strchr(s, '\n');
804 if (p == NULL) {
805 err = got_error(GOT_ERR_BAD_OBJ_DATA);
806 goto done;
808 *p = '\0';
809 slen = strlen(s);
810 (*tag)->tag = strndup(s, slen);
811 if ((*tag)->tag == NULL) {
812 err = got_error_from_errno();
813 goto done;
815 s += slen + 1;
816 remain -= slen + 1;
817 if (remain <= 0) {
818 err = got_error(GOT_ERR_BAD_OBJ_DATA);
819 goto done;
821 } else {
822 err = got_error(GOT_ERR_BAD_OBJ_DATA);
823 goto done;
826 tlen = strlen(GOT_TAG_TAG_TAGGER);
827 if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
828 char *p;
829 size_t slen;
831 remain -= tlen;
832 if (remain <= 0) {
833 err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 goto done;
836 s += tlen;
837 p = strchr(s, '\n');
838 if (p == NULL) {
839 err = got_error(GOT_ERR_BAD_OBJ_DATA);
840 goto done;
842 *p = '\0';
843 slen = strlen(s);
844 err = parse_commit_time(&(*tag)->tagger_time,
845 &(*tag)->tagger_gmtoff, s);
846 if (err)
847 goto done;
848 (*tag)->tagger = strdup(s);
849 if ((*tag)->tagger == NULL) {
850 err = got_error_from_errno();
851 goto done;
853 s += slen + 1;
854 remain -= slen + 1;
855 if (remain <= 0) {
856 err = got_error(GOT_ERR_BAD_OBJ_DATA);
857 goto done;
859 } else {
860 err = got_error(GOT_ERR_BAD_OBJ_DATA);
861 goto done;
864 (*tag)->tagmsg = strndup(s, remain);
865 if ((*tag)->tagmsg == NULL) {
866 err = got_error_from_errno();
867 goto done;
869 done:
870 if (err) {
871 got_object_tag_close(*tag);
872 *tag = NULL;
874 return err;
877 const struct got_error *
878 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
880 const struct got_error *err = NULL;
881 static const size_t blocksize = 512;
882 size_t n, total, remain;
883 uint8_t *buf;
885 *outbuf = NULL;
886 *outlen = 0;
888 buf = malloc(blocksize);
889 if (buf == NULL)
890 return got_error_from_errno();
892 remain = blocksize;
893 total = 0;
894 while (1) {
895 if (remain == 0) {
896 uint8_t *newbuf;
897 newbuf = reallocarray(buf, 1, total + blocksize);
898 if (newbuf == NULL) {
899 err = got_error_from_errno();
900 goto done;
902 buf = newbuf;
903 remain += blocksize;
905 n = fread(buf + total, 1, remain, f);
906 if (n == 0) {
907 if (ferror(f)) {
908 err = got_ferror(f, GOT_ERR_IO);
909 goto done;
911 break; /* EOF */
913 remain -= n;
914 total += n;
915 };
917 done:
918 if (err == NULL) {
919 *outbuf = buf;
920 *outlen = total;
921 } else
922 free(buf);
923 return err;