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 #define GOT_OBJ_TAG_COMMIT "commit"
58 #define GOT_OBJ_TAG_TREE "tree"
59 #define GOT_OBJ_TAG_BLOB "blob"
60 #define GOT_OBJ_TAG_TAG "tag"
62 #define GOT_COMMIT_TAG_TREE "tree "
63 #define GOT_COMMIT_TAG_PARENT "parent "
64 #define GOT_COMMIT_TAG_AUTHOR "author "
65 #define GOT_COMMIT_TAG_COMMITTER "committer "
67 #define GOT_TAG_TAG_OBJECT "object "
68 #define GOT_TAG_TAG_TYPE "type "
69 #define GOT_TAG_TAG_TAG "tag "
70 #define GOT_TAG_TAG_TAGGER "tagger "
72 int
73 got_object_id_cmp(const struct got_object_id *id1,
74 const struct got_object_id *id2)
75 {
76 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
77 }
79 const struct got_error *
80 got_object_qid_alloc_partial(struct got_object_qid **qid)
81 {
82 const struct got_error *err = NULL;
84 *qid = malloc(sizeof(**qid));
85 if (*qid == NULL)
86 return got_error_from_errno();
88 (*qid)->id = malloc(sizeof(*((*qid)->id)));
89 if ((*qid)->id == NULL) {
90 err = got_error_from_errno();
91 got_object_qid_free(*qid);
92 *qid = NULL;
93 return err;
94 }
96 return NULL;
97 }
99 const struct got_error *
100 got_object_id_str(char **outbuf, struct got_object_id *id)
102 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
104 *outbuf = malloc(len);
105 if (*outbuf == NULL)
106 return got_error_from_errno();
108 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
109 free(*outbuf);
110 *outbuf = NULL;
111 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
114 return NULL;
117 void
118 got_object_close(struct got_object *obj)
120 if (obj->refcnt > 0) {
121 obj->refcnt--;
122 if (obj->refcnt > 0)
123 return;
126 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
127 struct got_delta *delta;
128 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
129 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
130 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
131 got_delta_close(delta);
134 if (obj->flags & GOT_OBJ_FLAG_PACKED)
135 free(obj->path_packfile);
136 free(obj);
139 void
140 got_object_qid_free(struct got_object_qid *qid)
142 free(qid->id);
143 free(qid);
146 const struct got_error *
147 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
149 const char *obj_tags[] = {
150 GOT_OBJ_TAG_COMMIT,
151 GOT_OBJ_TAG_TREE,
152 GOT_OBJ_TAG_BLOB,
153 GOT_OBJ_TAG_TAG,
154 };
155 const int obj_types[] = {
156 GOT_OBJ_TYPE_COMMIT,
157 GOT_OBJ_TYPE_TREE,
158 GOT_OBJ_TYPE_BLOB,
159 GOT_OBJ_TYPE_TAG,
160 };
161 int type = 0;
162 size_t size = 0, hdrlen = 0;
163 int i;
164 char *p = strchr(buf, '\0');
166 *obj = NULL;
168 if (p == NULL)
169 return got_error(GOT_ERR_BAD_OBJ_HDR);
171 hdrlen = strlen(buf) + 1 /* '\0' */;
173 for (i = 0; i < nitems(obj_tags); i++) {
174 const char *tag = obj_tags[i];
175 size_t tlen = strlen(tag);
176 const char *errstr;
178 if (strncmp(buf, tag, tlen) != 0)
179 continue;
181 type = obj_types[i];
182 if (len <= tlen)
183 return got_error(GOT_ERR_BAD_OBJ_HDR);
184 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
185 if (errstr != NULL)
186 return got_error(GOT_ERR_BAD_OBJ_HDR);
187 break;
190 if (type == 0)
191 return got_error(GOT_ERR_BAD_OBJ_HDR);
193 *obj = calloc(1, sizeof(**obj));
194 if (*obj == NULL)
195 return got_error_from_errno();
196 (*obj)->type = type;
197 (*obj)->hdrlen = hdrlen;
198 (*obj)->size = size;
199 return NULL;
202 const struct got_error *
203 got_object_read_header(struct got_object **obj, int fd)
205 const struct got_error *err;
206 struct got_zstream_buf zb;
207 char *buf;
208 const size_t zbsize = 64;
209 size_t outlen, totlen;
210 int nbuf = 1;
212 *obj = NULL;
214 buf = malloc(zbsize);
215 if (buf == NULL)
216 return got_error_from_errno();
218 err = got_inflate_init(&zb, buf, zbsize);
219 if (err)
220 return err;
222 totlen = 0;
223 do {
224 err = got_inflate_read_fd(&zb, fd, &outlen);
225 if (err)
226 goto done;
227 if (outlen == 0)
228 break;
229 totlen += outlen;
230 if (strchr(zb.outbuf, '\0') == NULL) {
231 char *newbuf;
232 nbuf++;
233 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
234 if (newbuf == NULL) {
235 err = got_error_from_errno();
236 goto done;
238 buf = newbuf;
239 zb.outbuf = newbuf + totlen;
240 zb.outlen = (nbuf * zbsize) - totlen;
242 } while (strchr(zb.outbuf, '\0') == NULL);
244 err = got_object_parse_header(obj, buf, totlen);
245 done:
246 free(buf);
247 got_inflate_end(&zb);
248 return err;
251 struct got_commit_object *
252 got_object_commit_alloc_partial(void)
254 struct got_commit_object *commit;
256 commit = calloc(1, sizeof(*commit));
257 if (commit == NULL)
258 return NULL;
259 commit->tree_id = malloc(sizeof(*commit->tree_id));
260 if (commit->tree_id == NULL) {
261 free(commit);
262 return NULL;
265 SIMPLEQ_INIT(&commit->parent_ids);
267 return commit;
270 const struct got_error *
271 got_object_commit_add_parent(struct got_commit_object *commit,
272 const char *id_str)
274 const struct got_error *err = NULL;
275 struct got_object_qid *qid;
277 err = got_object_qid_alloc_partial(&qid);
278 if (err)
279 return err;
281 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
282 err = got_error(GOT_ERR_BAD_OBJ_DATA);
283 free(qid->id);
284 free(qid);
285 return err;
288 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
289 commit->nparents++;
291 return NULL;
294 static const struct got_error *
295 parse_gmtoff(time_t *gmtoff, const char *tzstr)
297 int sign = 1;
298 const char *p = tzstr;
299 time_t h, m;
301 *gmtoff = 0;
303 if (*p == '-')
304 sign = -1;
305 else if (*p != '+')
306 return got_error(GOT_ERR_BAD_OBJ_DATA);
307 p++;
308 if (!isdigit(*p) && !isdigit(*(p + 1)))
309 return got_error(GOT_ERR_BAD_OBJ_DATA);
310 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
312 p += 2;
313 if (!isdigit(*p) && !isdigit(*(p + 1)))
314 return got_error(GOT_ERR_BAD_OBJ_DATA);
315 m = ((*p - '0') * 10) + (*(p + 1) - '0');
317 *gmtoff = (h * 60 * 60 + m * 60) * sign;
318 return NULL;
321 static const struct got_error *
322 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
324 const struct got_error *err = NULL;
325 const char *errstr;
326 char *space, *tzstr;
328 /* Parse and strip off trailing timezone indicator string. */
329 space = strrchr(committer, ' ');
330 if (space == NULL)
331 return got_error(GOT_ERR_BAD_OBJ_DATA);
332 tzstr = strdup(space + 1);
333 if (tzstr == NULL)
334 return got_error_from_errno();
335 err = parse_gmtoff(gmtoff, tzstr);
336 free(tzstr);
337 if (err)
338 return err;
339 *space = '\0';
341 /* Timestamp is separated from committer name + email by space. */
342 space = strrchr(committer, ' ');
343 if (space == NULL)
344 return got_error(GOT_ERR_BAD_OBJ_DATA);
346 /* Timestamp parsed here is expressed in comitter's local time. */
347 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
348 if (errstr)
349 return got_error(GOT_ERR_BAD_OBJ_DATA);
351 /* Express the time stamp in UTC. */
352 *time -= *gmtoff;
354 /* Strip off parsed time information, leaving just author and email. */
355 *space = '\0';
357 return NULL;
360 void
361 got_object_commit_close(struct got_commit_object *commit)
363 struct got_object_qid *qid;
365 if (commit->refcnt > 0) {
366 commit->refcnt--;
367 if (commit->refcnt > 0)
368 return;
371 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
372 qid = SIMPLEQ_FIRST(&commit->parent_ids);
373 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
374 got_object_qid_free(qid);
377 free(commit->tree_id);
378 free(commit->author);
379 free(commit->committer);
380 free(commit->logmsg);
381 free(commit);
384 struct got_object_id *
385 got_object_commit_get_tree_id(struct got_commit_object *commit)
387 return commit->tree_id;
390 int
391 got_object_commit_get_nparents(struct got_commit_object *commit)
393 return commit->nparents;
396 const struct got_object_id_queue *
397 got_object_commit_get_parent_ids(struct got_commit_object *commit)
399 return &commit->parent_ids;
402 const char *
403 got_object_commit_get_author(struct got_commit_object *commit)
405 return commit->author;
408 time_t
409 got_object_commit_get_author_time(struct got_commit_object *commit)
411 return commit->author_time;
414 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
416 return commit->author_gmtoff;
419 const char *
420 got_object_commit_get_committer(struct got_commit_object *commit)
422 return commit->committer;
425 time_t
426 got_object_commit_get_committer_time(struct got_commit_object *commit)
428 return commit->committer_time;
431 time_t
432 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
434 return commit->committer_gmtoff;
437 const char *
438 got_object_commit_get_logmsg(struct got_commit_object *commit)
440 return commit->logmsg;
443 const struct got_error *
444 got_object_parse_commit(struct got_commit_object **commit, char *buf,
445 size_t len)
447 const struct got_error *err = NULL;
448 char *s = buf;
449 size_t tlen;
450 ssize_t remain = (ssize_t)len;
452 *commit = got_object_commit_alloc_partial();
453 if (*commit == NULL)
454 return got_error_from_errno();
456 tlen = strlen(GOT_COMMIT_TAG_TREE);
457 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
458 remain -= tlen;
459 if (remain < SHA1_DIGEST_STRING_LENGTH) {
460 err = got_error(GOT_ERR_BAD_OBJ_DATA);
461 goto done;
463 s += tlen;
464 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
465 err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 goto done;
468 remain -= SHA1_DIGEST_STRING_LENGTH;
469 s += SHA1_DIGEST_STRING_LENGTH;
470 } else {
471 err = got_error(GOT_ERR_BAD_OBJ_DATA);
472 goto done;
475 tlen = strlen(GOT_COMMIT_TAG_PARENT);
476 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
477 remain -= tlen;
478 if (remain < SHA1_DIGEST_STRING_LENGTH) {
479 err = got_error(GOT_ERR_BAD_OBJ_DATA);
480 goto done;
482 s += tlen;
483 err = got_object_commit_add_parent(*commit, s);
484 if (err)
485 goto done;
487 remain -= SHA1_DIGEST_STRING_LENGTH;
488 s += SHA1_DIGEST_STRING_LENGTH;
491 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
492 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
493 char *p;
494 size_t slen;
496 remain -= tlen;
497 if (remain <= 0) {
498 err = got_error(GOT_ERR_BAD_OBJ_DATA);
499 goto done;
501 s += tlen;
502 p = strchr(s, '\n');
503 if (p == NULL) {
504 err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 goto done;
507 *p = '\0';
508 slen = strlen(s);
509 err = parse_commit_time(&(*commit)->author_time,
510 &(*commit)->author_gmtoff, s);
511 if (err)
512 goto done;
513 (*commit)->author = strdup(s);
514 if ((*commit)->author == NULL) {
515 err = got_error_from_errno();
516 goto done;
518 s += slen + 1;
519 remain -= slen + 1;
522 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
523 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
524 char *p;
525 size_t slen;
527 remain -= tlen;
528 if (remain <= 0) {
529 err = got_error(GOT_ERR_BAD_OBJ_DATA);
530 goto done;
532 s += tlen;
533 p = strchr(s, '\n');
534 if (p == NULL) {
535 err = got_error(GOT_ERR_BAD_OBJ_DATA);
536 goto done;
538 *p = '\0';
539 slen = strlen(s);
540 err = parse_commit_time(&(*commit)->committer_time,
541 &(*commit)->committer_gmtoff, s);
542 if (err)
543 goto done;
544 (*commit)->committer = strdup(s);
545 if ((*commit)->committer == NULL) {
546 err = got_error_from_errno();
547 goto done;
549 s += slen + 1;
550 remain -= slen + 1;
553 (*commit)->logmsg = strndup(s, remain);
554 if ((*commit)->logmsg == NULL) {
555 err = got_error_from_errno();
556 goto done;
558 done:
559 if (err) {
560 got_object_commit_close(*commit);
561 *commit = NULL;
563 return err;
566 void
567 got_object_tree_entry_close(struct got_tree_entry *te)
569 free(te->id);
570 free(te->name);
571 free(te);
574 void
575 got_object_tree_close(struct got_tree_object *tree)
577 struct got_tree_entry *te;
579 if (tree->refcnt > 0) {
580 tree->refcnt--;
581 if (tree->refcnt > 0)
582 return;
585 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
586 te = SIMPLEQ_FIRST(&tree->entries.head);
587 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
588 got_object_tree_entry_close(te);
591 free(tree);
594 struct got_tree_entry *
595 got_alloc_tree_entry_partial(void)
597 struct got_tree_entry *te;
599 te = malloc(sizeof(*te));
600 if (te == NULL)
601 return NULL;
603 te->id = malloc(sizeof(*te->id));
604 if (te->id == NULL) {
605 free(te);
606 te = NULL;
608 return te;
611 static const struct got_error *
612 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
613 size_t maxlen)
615 char *p = buf, *space;
616 const struct got_error *err = NULL;
618 *te = got_alloc_tree_entry_partial();
619 if (*te == NULL)
620 return got_error_from_errno();
622 *elen = strlen(buf) + 1;
623 if (*elen > maxlen) {
624 free(*te);
625 *te = NULL;
626 return got_error(GOT_ERR_BAD_OBJ_DATA);
629 space = strchr(buf, ' ');
630 if (space == NULL) {
631 err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 free(*te);
633 *te = NULL;
634 return err;
636 (*te)->mode = 0;
637 while (*p != ' ') {
638 if (*p < '0' && *p > '7') {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 goto done;
642 (*te)->mode <<= 3;
643 (*te)->mode |= *p - '0';
644 p++;
647 (*te)->name = strdup(space + 1);
648 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
649 err = got_error(GOT_ERR_BAD_OBJ_DATA);
650 goto done;
652 buf += *elen;
653 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
654 *elen += SHA1_DIGEST_LENGTH;
655 done:
656 if (err) {
657 got_object_tree_entry_close(*te);
658 *te = NULL;
660 return err;
663 const struct got_error *
664 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
666 const struct got_error *err;
667 size_t remain = len;
668 struct got_pathlist_head pathlist;
669 struct got_pathlist_entry *pe;
671 TAILQ_INIT(&pathlist);
673 *tree = calloc(1, sizeof(**tree));
674 if (*tree == NULL)
675 return got_error_from_errno();
677 SIMPLEQ_INIT(&(*tree)->entries.head);
679 while (remain > 0) {
680 struct got_tree_entry *te;
681 struct got_pathlist_entry *new = NULL;
682 size_t elen;
684 err = parse_tree_entry(&te, &elen, buf, remain);
685 if (err)
686 goto done;
687 err = got_pathlist_insert(&new, &pathlist, te->name, te);
688 if (err)
689 goto done;
690 if (new == NULL) {
691 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
692 goto done;
694 buf += elen;
695 remain -= elen;
698 if (remain != 0) {
699 got_object_tree_close(*tree);
700 *tree = NULL;
701 err = got_error(GOT_ERR_BAD_OBJ_DATA);
702 goto done;
705 TAILQ_FOREACH(pe, &pathlist, entry) {
706 struct got_tree_entry *te = pe->data;
707 (*tree)->entries.nentries++;
708 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
710 done:
711 got_pathlist_free(&pathlist);
712 return err;
715 void
716 got_object_tag_close(struct got_tag_object *tag)
718 free(tag->tag);
719 free(tag->tagger);
720 free(tag->tagmsg);
721 free(tag);
724 const struct got_error *
725 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
727 const struct got_error *err = NULL;
728 size_t remain = len;
729 char *s = buf;
730 size_t tlen;
732 *tag = calloc(1, sizeof(**tag));
733 if (*tag == NULL)
734 return got_error_from_errno();
736 tlen = strlen(GOT_TAG_TAG_OBJECT);
737 if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
738 remain -= tlen;
739 if (remain < SHA1_DIGEST_STRING_LENGTH) {
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
741 goto done;
743 s += tlen;
744 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
745 err = got_error(GOT_ERR_BAD_OBJ_DATA);
746 goto done;
748 remain -= SHA1_DIGEST_STRING_LENGTH;
749 s += SHA1_DIGEST_STRING_LENGTH;
750 } else {
751 err = got_error(GOT_ERR_BAD_OBJ_DATA);
752 goto done;
755 if (remain <= 0) {
756 err = got_error(GOT_ERR_BAD_OBJ_DATA);
757 goto done;
760 tlen = strlen(GOT_TAG_TAG_TYPE);
761 if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
762 remain -= tlen;
763 if (remain <= 0) {
764 err = got_error(GOT_ERR_BAD_OBJ_DATA);
765 goto done;
767 s += tlen;
768 if (strncmp(s, GOT_OBJ_TAG_COMMIT,
769 strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
770 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
771 tlen = strlen(GOT_OBJ_TAG_COMMIT);
772 s += tlen;
773 remain -= tlen;
774 } else if (strncmp(s, GOT_OBJ_TAG_TREE,
775 strlen(GOT_OBJ_TAG_TREE)) == 0) {
776 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
777 tlen = strlen(GOT_OBJ_TAG_TREE);
778 s += tlen;
779 remain -= tlen;
780 } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
781 strlen(GOT_OBJ_TAG_BLOB)) == 0) {
782 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
783 tlen = strlen(GOT_OBJ_TAG_BLOB);
784 s += tlen;
785 remain -= tlen;
786 } else if (strncmp(s, GOT_OBJ_TAG_TAG,
787 strlen(GOT_OBJ_TAG_TAG)) == 0) {
788 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
789 tlen = strlen(GOT_OBJ_TAG_TAG);
790 s += tlen;
791 remain -= tlen;
792 } else {
793 err = got_error(GOT_ERR_BAD_OBJ_DATA);
794 goto done;
797 if (remain <= 0 || *s != '\n') {
798 err = got_error(GOT_ERR_BAD_OBJ_DATA);
799 goto done;
801 s++;
802 remain--;
803 if (remain <= 0) {
804 err = got_error(GOT_ERR_BAD_OBJ_DATA);
805 goto done;
807 } else {
808 err = got_error(GOT_ERR_BAD_OBJ_DATA);
809 goto done;
812 tlen = strlen(GOT_TAG_TAG_TAG);
813 if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
814 char *p;
815 size_t slen;
816 remain -= tlen;
817 if (remain <= 0) {
818 err = got_error(GOT_ERR_BAD_OBJ_DATA);
819 goto done;
821 s += tlen;
822 p = strchr(s, '\n');
823 if (p == NULL) {
824 err = got_error(GOT_ERR_BAD_OBJ_DATA);
825 goto done;
827 *p = '\0';
828 slen = strlen(s);
829 (*tag)->tag = strndup(s, slen);
830 if ((*tag)->tag == NULL) {
831 err = got_error_from_errno();
832 goto done;
834 s += slen + 1;
835 remain -= slen + 1;
836 if (remain <= 0) {
837 err = got_error(GOT_ERR_BAD_OBJ_DATA);
838 goto done;
840 } else {
841 err = got_error(GOT_ERR_BAD_OBJ_DATA);
842 goto done;
845 tlen = strlen(GOT_TAG_TAG_TAGGER);
846 if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
847 char *p;
848 size_t slen;
850 remain -= tlen;
851 if (remain <= 0) {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
853 goto done;
855 s += tlen;
856 p = strchr(s, '\n');
857 if (p == NULL) {
858 err = got_error(GOT_ERR_BAD_OBJ_DATA);
859 goto done;
861 *p = '\0';
862 slen = strlen(s);
863 err = parse_commit_time(&(*tag)->tagger_time,
864 &(*tag)->tagger_gmtoff, s);
865 if (err)
866 goto done;
867 (*tag)->tagger = strdup(s);
868 if ((*tag)->tagger == NULL) {
869 err = got_error_from_errno();
870 goto done;
872 s += slen + 1;
873 remain -= slen + 1;
874 if (remain <= 0) {
875 err = got_error(GOT_ERR_BAD_OBJ_DATA);
876 goto done;
878 } else {
879 /* Some old tags in the Linux git repo have no tagger. */
880 (*tag)->tagger = strdup("");
881 if ((*tag)->tagger == NULL) {
882 err = got_error_from_errno();
883 goto done;
887 (*tag)->tagmsg = strndup(s, remain);
888 if ((*tag)->tagmsg == NULL) {
889 err = got_error_from_errno();
890 goto done;
892 done:
893 if (err) {
894 got_object_tag_close(*tag);
895 *tag = NULL;
897 return err;
900 const struct got_error *
901 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
903 const struct got_error *err = NULL;
904 static const size_t blocksize = 512;
905 size_t n, total, remain;
906 uint8_t *buf;
908 *outbuf = NULL;
909 *outlen = 0;
911 buf = malloc(blocksize);
912 if (buf == NULL)
913 return got_error_from_errno();
915 remain = blocksize;
916 total = 0;
917 while (1) {
918 if (remain == 0) {
919 uint8_t *newbuf;
920 newbuf = reallocarray(buf, 1, total + blocksize);
921 if (newbuf == NULL) {
922 err = got_error_from_errno();
923 goto done;
925 buf = newbuf;
926 remain += blocksize;
928 n = fread(buf + total, 1, remain, f);
929 if (n == 0) {
930 if (ferror(f)) {
931 err = got_ferror(f, GOT_ERR_IO);
932 goto done;
934 break; /* EOF */
936 remain -= n;
937 total += n;
938 };
940 done:
941 if (err == NULL) {
942 *outbuf = buf;
943 *outlen = total;
944 } else
945 free(buf);
946 return err;