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, size_t len)
445 const struct got_error *err = NULL;
446 char *s = buf;
447 size_t tlen;
448 ssize_t remain = (ssize_t)len;
450 *commit = got_object_commit_alloc_partial();
451 if (*commit == NULL)
452 return got_error_from_errno();
454 tlen = strlen(GOT_COMMIT_TAG_TREE);
455 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
456 remain -= tlen;
457 if (remain < SHA1_DIGEST_STRING_LENGTH) {
458 err = got_error(GOT_ERR_BAD_OBJ_DATA);
459 goto done;
461 s += tlen;
462 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
463 err = got_error(GOT_ERR_BAD_OBJ_DATA);
464 goto done;
466 remain -= SHA1_DIGEST_STRING_LENGTH;
467 s += SHA1_DIGEST_STRING_LENGTH;
468 } else {
469 err = got_error(GOT_ERR_BAD_OBJ_DATA);
470 goto done;
473 tlen = strlen(GOT_COMMIT_TAG_PARENT);
474 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
475 remain -= tlen;
476 if (remain < SHA1_DIGEST_STRING_LENGTH) {
477 err = got_error(GOT_ERR_BAD_OBJ_DATA);
478 goto done;
480 s += tlen;
481 err = got_object_commit_add_parent(*commit, s);
482 if (err)
483 goto done;
485 remain -= SHA1_DIGEST_STRING_LENGTH;
486 s += SHA1_DIGEST_STRING_LENGTH;
489 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
490 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
491 char *p;
492 size_t slen;
494 remain -= tlen;
495 if (remain <= 0) {
496 err = got_error(GOT_ERR_BAD_OBJ_DATA);
497 goto done;
499 s += tlen;
500 p = strchr(s, '\n');
501 if (p == NULL) {
502 err = got_error(GOT_ERR_BAD_OBJ_DATA);
503 goto done;
505 *p = '\0';
506 slen = strlen(s);
507 err = parse_commit_time(&(*commit)->author_time,
508 &(*commit)->author_gmtoff, s);
509 if (err)
510 goto done;
511 (*commit)->author = strdup(s);
512 if ((*commit)->author == NULL) {
513 err = got_error_from_errno();
514 goto done;
516 s += slen + 1;
517 remain -= slen + 1;
520 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
521 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
522 char *p;
523 size_t slen;
525 remain -= tlen;
526 if (remain <= 0) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 goto done;
530 s += tlen;
531 p = strchr(s, '\n');
532 if (p == NULL) {
533 err = got_error(GOT_ERR_BAD_OBJ_DATA);
534 goto done;
536 *p = '\0';
537 slen = strlen(s);
538 err = parse_commit_time(&(*commit)->committer_time,
539 &(*commit)->committer_gmtoff, s);
540 if (err)
541 goto done;
542 (*commit)->committer = strdup(s);
543 if ((*commit)->committer == NULL) {
544 err = got_error_from_errno();
545 goto done;
547 s += slen + 1;
548 remain -= slen + 1;
551 (*commit)->logmsg = strndup(s, remain);
552 if ((*commit)->logmsg == NULL) {
553 err = got_error_from_errno();
554 goto done;
556 done:
557 if (err) {
558 got_object_commit_close(*commit);
559 *commit = NULL;
561 return err;
564 void
565 got_object_tree_entry_close(struct got_tree_entry *te)
567 free(te->id);
568 free(te->name);
569 free(te);
572 void
573 got_object_tree_close(struct got_tree_object *tree)
575 struct got_tree_entry *te;
577 if (tree->refcnt > 0) {
578 tree->refcnt--;
579 if (tree->refcnt > 0)
580 return;
583 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
584 te = SIMPLEQ_FIRST(&tree->entries.head);
585 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
586 got_object_tree_entry_close(te);
589 free(tree);
592 struct got_tree_entry *
593 got_alloc_tree_entry_partial(void)
595 struct got_tree_entry *te;
597 te = malloc(sizeof(*te));
598 if (te == NULL)
599 return NULL;
601 te->id = malloc(sizeof(*te->id));
602 if (te->id == NULL) {
603 free(te);
604 te = NULL;
606 return te;
609 static const struct got_error *
610 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
611 size_t maxlen)
613 char *p = buf, *space;
614 const struct got_error *err = NULL;
616 *te = got_alloc_tree_entry_partial();
617 if (*te == NULL)
618 return got_error_from_errno();
620 *elen = strlen(buf) + 1;
621 if (*elen > maxlen) {
622 free(*te);
623 *te = NULL;
624 return got_error(GOT_ERR_BAD_OBJ_DATA);
627 space = strchr(buf, ' ');
628 if (space == NULL) {
629 err = got_error(GOT_ERR_BAD_OBJ_DATA);
630 free(*te);
631 *te = NULL;
632 return err;
634 (*te)->mode = 0;
635 while (*p != ' ') {
636 if (*p < '0' && *p > '7') {
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 goto done;
640 (*te)->mode <<= 3;
641 (*te)->mode |= *p - '0';
642 p++;
645 (*te)->name = strdup(space + 1);
646 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
648 goto done;
650 buf += *elen;
651 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
652 *elen += SHA1_DIGEST_LENGTH;
653 done:
654 if (err) {
655 got_object_tree_entry_close(*te);
656 *te = NULL;
658 return err;
661 const struct got_error *
662 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
664 const struct got_error *err;
665 size_t remain = len;
667 *tree = calloc(1, sizeof(**tree));
668 if (*tree == NULL)
669 return got_error_from_errno();
671 SIMPLEQ_INIT(&(*tree)->entries.head);
673 while (remain > 0) {
674 struct got_tree_entry *te;
675 size_t elen;
677 err = parse_tree_entry(&te, &elen, buf, remain);
678 if (err)
679 return err;
680 (*tree)->entries.nentries++;
681 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
682 buf += elen;
683 remain -= elen;
686 if (remain != 0) {
687 got_object_tree_close(*tree);
688 *tree = NULL;
689 return got_error(GOT_ERR_BAD_OBJ_DATA);
692 return NULL;
695 void
696 got_object_tag_close(struct got_tag_object *tag)
698 free(tag->tag);
699 free(tag->tagger);
700 free(tag->tagmsg);
701 free(tag);
704 const struct got_error *
705 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
707 const struct got_error *err = NULL;
708 size_t remain = len;
709 char *s = buf;
710 size_t tlen;
712 *tag = calloc(1, sizeof(**tag));
713 if (*tag == NULL)
714 return got_error_from_errno();
716 tlen = strlen(GOT_TAG_TAG_OBJECT);
717 if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
718 remain -= tlen;
719 if (remain < SHA1_DIGEST_STRING_LENGTH) {
720 err = got_error(GOT_ERR_BAD_OBJ_DATA);
721 goto done;
723 s += tlen;
724 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
725 err = got_error(GOT_ERR_BAD_OBJ_DATA);
726 goto done;
728 remain -= SHA1_DIGEST_STRING_LENGTH;
729 s += SHA1_DIGEST_STRING_LENGTH;
730 } else {
731 err = got_error(GOT_ERR_BAD_OBJ_DATA);
732 goto done;
735 if (remain <= 0) {
736 err = got_error(GOT_ERR_BAD_OBJ_DATA);
737 goto done;
740 tlen = strlen(GOT_TAG_TAG_TYPE);
741 if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
742 remain -= tlen;
743 if (remain <= 0) {
744 err = got_error(GOT_ERR_BAD_OBJ_DATA);
745 goto done;
747 s += tlen;
748 if (strncmp(s, GOT_OBJ_TAG_COMMIT,
749 strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
750 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
751 tlen = strlen(GOT_OBJ_TAG_COMMIT);
752 s += tlen;
753 remain -= tlen;
754 } else if (strncmp(s, GOT_OBJ_TAG_TREE,
755 strlen(GOT_OBJ_TAG_TREE)) == 0) {
756 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
757 tlen = strlen(GOT_OBJ_TAG_TREE);
758 s += tlen;
759 remain -= tlen;
760 } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
761 strlen(GOT_OBJ_TAG_BLOB)) == 0) {
762 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
763 tlen = strlen(GOT_OBJ_TAG_BLOB);
764 s += tlen;
765 remain -= tlen;
766 } else if (strncmp(s, GOT_OBJ_TAG_TAG,
767 strlen(GOT_OBJ_TAG_TAG)) == 0) {
768 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
769 tlen = strlen(GOT_OBJ_TAG_TAG);
770 s += tlen;
771 remain -= tlen;
772 } else {
773 err = got_error(GOT_ERR_BAD_OBJ_DATA);
774 goto done;
777 if (remain <= 0 || *s != '\n') {
778 err = got_error(GOT_ERR_BAD_OBJ_DATA);
779 goto done;
781 s++;
782 remain--;
783 if (remain <= 0) {
784 err = got_error(GOT_ERR_BAD_OBJ_DATA);
785 goto done;
787 } else {
788 err = got_error(GOT_ERR_BAD_OBJ_DATA);
789 goto done;
792 tlen = strlen(GOT_TAG_TAG_TAG);
793 if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
794 char *p;
795 size_t slen;
796 remain -= tlen;
797 if (remain <= 0) {
798 err = got_error(GOT_ERR_BAD_OBJ_DATA);
799 goto done;
801 s += tlen;
802 p = strchr(s, '\n');
803 if (p == NULL) {
804 err = got_error(GOT_ERR_BAD_OBJ_DATA);
805 goto done;
807 *p = '\0';
808 slen = strlen(s);
809 (*tag)->tag = strndup(s, slen);
810 if ((*tag)->tag == NULL) {
811 err = got_error_from_errno();
812 goto done;
814 s += slen + 1;
815 remain -= slen + 1;
816 if (remain <= 0) {
817 err = got_error(GOT_ERR_BAD_OBJ_DATA);
818 goto done;
820 } else {
821 err = got_error(GOT_ERR_BAD_OBJ_DATA);
822 goto done;
825 tlen = strlen(GOT_TAG_TAG_TAGGER);
826 if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
827 char *p;
828 size_t slen;
830 remain -= tlen;
831 if (remain <= 0) {
832 err = got_error(GOT_ERR_BAD_OBJ_DATA);
833 goto done;
835 s += tlen;
836 p = strchr(s, '\n');
837 if (p == NULL) {
838 err = got_error(GOT_ERR_BAD_OBJ_DATA);
839 goto done;
841 *p = '\0';
842 slen = strlen(s);
843 err = parse_commit_time(&(*tag)->tagger_time,
844 &(*tag)->tagger_gmtoff, s);
845 if (err)
846 goto done;
847 (*tag)->tagger = strdup(s);
848 if ((*tag)->tagger == NULL) {
849 err = got_error_from_errno();
850 goto done;
852 s += slen + 1;
853 remain -= slen + 1;
854 if (remain <= 0) {
855 err = got_error(GOT_ERR_BAD_OBJ_DATA);
856 goto done;
858 } else {
859 err = got_error(GOT_ERR_BAD_OBJ_DATA);
860 goto done;
863 (*tag)->tagmsg = strndup(s, remain);
864 if ((*tag)->tagmsg == NULL) {
865 err = got_error_from_errno();
866 goto done;
868 done:
869 if (err) {
870 got_object_tag_close(*tag);
871 *tag = NULL;
873 return err;
876 const struct got_error *
877 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
879 const struct got_error *err = NULL;
880 static const size_t blocksize = 512;
881 size_t n, total, remain;
882 uint8_t *buf;
884 *outbuf = NULL;
885 *outlen = 0;
887 buf = malloc(blocksize);
888 if (buf == NULL)
889 return got_error_from_errno();
891 remain = blocksize;
892 total = 0;
893 while (1) {
894 if (remain == 0) {
895 uint8_t *newbuf;
896 newbuf = reallocarray(buf, 1, total + blocksize);
897 if (newbuf == NULL) {
898 err = got_error_from_errno();
899 goto done;
901 buf = newbuf;
902 remain += blocksize;
904 n = fread(buf + total, 1, remain, f);
905 if (n == 0) {
906 if (ferror(f)) {
907 err = got_ferror(f, GOT_ERR_IO);
908 goto done;
910 break; /* EOF */
912 remain -= n;
913 total += n;
914 };
916 done:
917 if (err == NULL) {
918 *outbuf = buf;
919 *outlen = total;
920 } else
921 free(buf);
922 return err;