Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/mman.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <imsg.h>
36 #include <time.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_repository.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #endif
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
61 {
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
65 if (id2 == NULL)
66 return NULL;
67 memcpy(id2, id1, sizeof(*id2));
68 return id2;
69 }
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("malloc");
87 (*qid)->id = malloc(sizeof(*((*qid)->id)));
88 if ((*qid)->id == NULL) {
89 err = got_error_from_errno("malloc");
90 got_object_qid_free(*qid);
91 *qid = NULL;
92 return err;
93 }
94 (*qid)->data = NULL;
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("malloc");
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 (!STAILQ_EMPTY(&obj->deltas.entries)) {
129 delta = STAILQ_FIRST(&obj->deltas.entries);
130 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
131 free(delta);
134 free(obj);
137 const struct got_error *
138 got_object_raw_close(struct got_raw_object *obj)
140 const struct got_error *err = NULL;
142 if (obj->refcnt > 0) {
143 obj->refcnt--;
144 if (obj->refcnt > 0)
145 return NULL;
148 if (obj->f == NULL) {
149 if (obj->fd != -1) {
150 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
151 err = got_error_from_errno("munmap");
152 if (close(obj->fd) == -1 && err == NULL)
153 err = got_error_from_errno("close");
154 } else
155 free(obj->data);
156 } else {
157 if (fclose(obj->f) == EOF && err == NULL)
158 err = got_error_from_errno("fclose");
160 free(obj);
161 return err;
164 void
165 got_object_qid_free(struct got_object_qid *qid)
167 free(qid->id);
168 free(qid);
171 void
172 got_object_id_queue_free(struct got_object_id_queue *ids)
174 struct got_object_qid *qid;
176 while (!STAILQ_EMPTY(ids)) {
177 qid = STAILQ_FIRST(ids);
178 STAILQ_REMOVE_HEAD(ids, entry);
179 got_object_qid_free(qid);
183 const struct got_error *
184 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
186 const char *obj_labels[] = {
187 GOT_OBJ_LABEL_COMMIT,
188 GOT_OBJ_LABEL_TREE,
189 GOT_OBJ_LABEL_BLOB,
190 GOT_OBJ_LABEL_TAG,
191 };
192 const int obj_types[] = {
193 GOT_OBJ_TYPE_COMMIT,
194 GOT_OBJ_TYPE_TREE,
195 GOT_OBJ_TYPE_BLOB,
196 GOT_OBJ_TYPE_TAG,
197 };
198 int type = 0;
199 size_t size = 0, hdrlen = 0;
200 size_t i;
202 *obj = NULL;
204 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
205 if (hdrlen > len)
206 return got_error(GOT_ERR_BAD_OBJ_HDR);
208 for (i = 0; i < nitems(obj_labels); i++) {
209 const char *label = obj_labels[i];
210 size_t label_len = strlen(label);
211 const char *errstr;
213 if (strncmp(buf, label, label_len) != 0)
214 continue;
216 type = obj_types[i];
217 if (len <= label_len)
218 return got_error(GOT_ERR_BAD_OBJ_HDR);
219 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
220 if (errstr != NULL)
221 return got_error(GOT_ERR_BAD_OBJ_HDR);
222 break;
225 if (type == 0)
226 return got_error(GOT_ERR_BAD_OBJ_HDR);
228 *obj = calloc(1, sizeof(**obj));
229 if (*obj == NULL)
230 return got_error_from_errno("calloc");
231 (*obj)->type = type;
232 (*obj)->hdrlen = hdrlen;
233 (*obj)->size = size;
234 return NULL;
237 const struct got_error *
238 got_object_read_header(struct got_object **obj, int fd)
240 const struct got_error *err;
241 struct got_inflate_buf zb;
242 uint8_t *buf;
243 const size_t zbsize = 64;
244 size_t outlen, totlen;
245 int nbuf = 1;
247 *obj = NULL;
249 buf = malloc(zbsize);
250 if (buf == NULL)
251 return got_error_from_errno("malloc");
253 err = got_inflate_init(&zb, buf, zbsize, NULL);
254 if (err)
255 return err;
257 totlen = 0;
258 do {
259 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
260 if (err)
261 goto done;
262 if (outlen == 0)
263 break;
264 totlen += outlen;
265 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
266 uint8_t *newbuf;
267 nbuf++;
268 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
269 if (newbuf == NULL) {
270 err = got_error_from_errno("recallocarray");
271 goto done;
273 buf = newbuf;
274 zb.outbuf = newbuf + totlen;
275 zb.outlen = (nbuf * zbsize) - totlen;
277 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
279 err = got_object_parse_header(obj, buf, totlen);
280 done:
281 free(buf);
282 got_inflate_end(&zb);
283 return err;
286 struct got_commit_object *
287 got_object_commit_alloc_partial(void)
289 struct got_commit_object *commit;
291 commit = calloc(1, sizeof(*commit));
292 if (commit == NULL)
293 return NULL;
294 commit->tree_id = malloc(sizeof(*commit->tree_id));
295 if (commit->tree_id == NULL) {
296 free(commit);
297 return NULL;
300 STAILQ_INIT(&commit->parent_ids);
302 return commit;
305 const struct got_error *
306 got_object_commit_add_parent(struct got_commit_object *commit,
307 const char *id_str)
309 const struct got_error *err = NULL;
310 struct got_object_qid *qid;
312 err = got_object_qid_alloc_partial(&qid);
313 if (err)
314 return err;
316 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
317 err = got_error(GOT_ERR_BAD_OBJ_DATA);
318 got_object_qid_free(qid);
319 return err;
322 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
323 commit->nparents++;
325 return NULL;
328 static const struct got_error *
329 parse_gmtoff(time_t *gmtoff, const char *tzstr)
331 int sign = 1;
332 const char *p = tzstr;
333 time_t h, m;
335 *gmtoff = 0;
337 if (*p == '-')
338 sign = -1;
339 else if (*p != '+')
340 return got_error(GOT_ERR_BAD_OBJ_DATA);
341 p++;
342 if (!isdigit(*p) && !isdigit(*(p + 1)))
343 return got_error(GOT_ERR_BAD_OBJ_DATA);
344 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
346 p += 2;
347 if (!isdigit(*p) && !isdigit(*(p + 1)))
348 return got_error(GOT_ERR_BAD_OBJ_DATA);
349 m = ((*p - '0') * 10) + (*(p + 1) - '0');
351 *gmtoff = (h * 60 * 60 + m * 60) * sign;
352 return NULL;
355 static const struct got_error *
356 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
358 const struct got_error *err = NULL;
359 const char *errstr;
360 char *space, *tzstr;
362 /* Parse and strip off trailing timezone indicator string. */
363 space = strrchr(committer, ' ');
364 if (space == NULL)
365 return got_error(GOT_ERR_BAD_OBJ_DATA);
366 tzstr = strdup(space + 1);
367 if (tzstr == NULL)
368 return got_error_from_errno("strdup");
369 err = parse_gmtoff(gmtoff, tzstr);
370 free(tzstr);
371 if (err) {
372 if (err->code != GOT_ERR_BAD_OBJ_DATA)
373 return err;
374 /* Old versions of Git omitted the timestamp. */
375 *time = 0;
376 *gmtoff = 0;
377 return NULL;
379 *space = '\0';
381 /* Timestamp is separated from committer name + email by space. */
382 space = strrchr(committer, ' ');
383 if (space == NULL)
384 return got_error(GOT_ERR_BAD_OBJ_DATA);
386 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
387 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
388 if (errstr)
389 return got_error(GOT_ERR_BAD_OBJ_DATA);
391 /* Strip off parsed time information, leaving just author and email. */
392 *space = '\0';
394 return NULL;
397 void
398 got_object_commit_close(struct got_commit_object *commit)
400 if (commit->refcnt > 0) {
401 commit->refcnt--;
402 if (commit->refcnt > 0)
403 return;
406 got_object_id_queue_free(&commit->parent_ids);
407 free(commit->tree_id);
408 free(commit->author);
409 free(commit->committer);
410 free(commit->logmsg);
411 free(commit);
414 struct got_object_id *
415 got_object_commit_get_tree_id(struct got_commit_object *commit)
417 return commit->tree_id;
420 int
421 got_object_commit_get_nparents(struct got_commit_object *commit)
423 return commit->nparents;
426 const struct got_object_id_queue *
427 got_object_commit_get_parent_ids(struct got_commit_object *commit)
429 return &commit->parent_ids;
432 const char *
433 got_object_commit_get_author(struct got_commit_object *commit)
435 return commit->author;
438 time_t
439 got_object_commit_get_author_time(struct got_commit_object *commit)
441 return commit->author_time;
444 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
446 return commit->author_gmtoff;
449 const char *
450 got_object_commit_get_committer(struct got_commit_object *commit)
452 return commit->committer;
455 time_t
456 got_object_commit_get_committer_time(struct got_commit_object *commit)
458 return commit->committer_time;
461 time_t
462 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
464 return commit->committer_gmtoff;
467 const struct got_error *
468 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
470 const struct got_error *err = NULL;
471 const char *src;
472 char *dst;
473 size_t len;
475 len = strlen(commit->logmsg);
476 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
477 if (*logmsg == NULL)
478 return got_error_from_errno("malloc");
480 /*
481 * Strip out unusual headers. Headers are separated from the commit
482 * message body by a single empty line.
483 */
484 src = commit->logmsg;
485 dst = *logmsg;
486 while (*src != '\0' && *src != '\n') {
487 int copy_header = 1, eol = 0;
488 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
489 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
490 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
491 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
492 strncmp(src, GOT_COMMIT_LABEL_PARENT,
493 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
494 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
495 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
496 copy_header = 0;
498 while (*src != '\0' && !eol) {
499 if (copy_header) {
500 *dst = *src;
501 dst++;
503 if (*src == '\n')
504 eol = 1;
505 src++;
508 *dst = '\0';
510 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
511 err = got_error(GOT_ERR_NO_SPACE);
512 goto done;
515 /* Trim redundant trailing whitespace. */
516 len = strlen(*logmsg);
517 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
518 isspace((unsigned char)(*logmsg)[len - 1])) {
519 (*logmsg)[len - 1] = '\0';
520 len--;
523 /* Append a trailing newline if missing. */
524 if (len > 0 && (*logmsg)[len - 1] != '\n') {
525 (*logmsg)[len] = '\n';
526 (*logmsg)[len + 1] = '\0';
528 done:
529 if (err) {
530 free(*logmsg);
531 *logmsg = NULL;
533 return err;
536 const char *
537 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
539 return commit->logmsg;
542 const struct got_error *
543 got_object_parse_commit(struct got_commit_object **commit, char *buf,
544 size_t len)
546 const struct got_error *err = NULL;
547 char *s = buf;
548 size_t label_len;
549 ssize_t remain = (ssize_t)len;
551 if (remain == 0)
552 return got_error(GOT_ERR_BAD_OBJ_DATA);
554 *commit = got_object_commit_alloc_partial();
555 if (*commit == NULL)
556 return got_error_from_errno("got_object_commit_alloc_partial");
558 label_len = strlen(GOT_COMMIT_LABEL_TREE);
559 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
560 remain -= label_len;
561 if (remain < SHA1_DIGEST_STRING_LENGTH) {
562 err = got_error(GOT_ERR_BAD_OBJ_DATA);
563 goto done;
565 s += label_len;
566 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
567 err = got_error(GOT_ERR_BAD_OBJ_DATA);
568 goto done;
570 remain -= SHA1_DIGEST_STRING_LENGTH;
571 s += SHA1_DIGEST_STRING_LENGTH;
572 } else {
573 err = got_error(GOT_ERR_BAD_OBJ_DATA);
574 goto done;
577 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
578 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
579 remain -= label_len;
580 if (remain < SHA1_DIGEST_STRING_LENGTH) {
581 err = got_error(GOT_ERR_BAD_OBJ_DATA);
582 goto done;
584 s += label_len;
585 err = got_object_commit_add_parent(*commit, s);
586 if (err)
587 goto done;
589 remain -= SHA1_DIGEST_STRING_LENGTH;
590 s += SHA1_DIGEST_STRING_LENGTH;
593 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
594 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
595 char *p;
596 size_t slen;
598 remain -= label_len;
599 if (remain <= 0) {
600 err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 goto done;
603 s += label_len;
604 p = memchr(s, '\n', remain);
605 if (p == NULL) {
606 err = got_error(GOT_ERR_BAD_OBJ_DATA);
607 goto done;
609 *p = '\0';
610 slen = strlen(s);
611 err = parse_commit_time(&(*commit)->author_time,
612 &(*commit)->author_gmtoff, s);
613 if (err)
614 goto done;
615 (*commit)->author = strdup(s);
616 if ((*commit)->author == NULL) {
617 err = got_error_from_errno("strdup");
618 goto done;
620 s += slen + 1;
621 remain -= slen + 1;
624 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
625 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
626 char *p;
627 size_t slen;
629 remain -= label_len;
630 if (remain <= 0) {
631 err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 goto done;
634 s += label_len;
635 p = memchr(s, '\n', remain);
636 if (p == NULL) {
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 goto done;
640 *p = '\0';
641 slen = strlen(s);
642 err = parse_commit_time(&(*commit)->committer_time,
643 &(*commit)->committer_gmtoff, s);
644 if (err)
645 goto done;
646 (*commit)->committer = strdup(s);
647 if ((*commit)->committer == NULL) {
648 err = got_error_from_errno("strdup");
649 goto done;
651 s += slen + 1;
652 remain -= slen + 1;
655 (*commit)->logmsg = strndup(s, remain);
656 if ((*commit)->logmsg == NULL) {
657 err = got_error_from_errno("strndup");
658 goto done;
660 done:
661 if (err) {
662 got_object_commit_close(*commit);
663 *commit = NULL;
665 return err;
668 void
669 got_object_tree_close(struct got_tree_object *tree)
671 if (tree->refcnt > 0) {
672 tree->refcnt--;
673 if (tree->refcnt > 0)
674 return;
677 free(tree->entries);
678 free(tree);
681 static const struct got_error *
682 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
683 size_t *elen, char *buf,
684 size_t maxlen)
686 char *p, *space;
687 const struct got_error *err = NULL;
689 *name = NULL;
690 *elen = 0;
692 *pte = malloc(sizeof(**pte));
693 if (*pte == NULL)
694 return got_error_from_errno("malloc");
696 *elen = strnlen(buf, maxlen) + 1;
697 if (*elen > maxlen) {
698 free(*pte);
699 *pte = NULL;
700 return got_error(GOT_ERR_BAD_OBJ_DATA);
703 space = memchr(buf, ' ', *elen);
704 if (space == NULL || space <= buf) {
705 err = got_error(GOT_ERR_BAD_OBJ_DATA);
706 free(*pte);
707 *pte = NULL;
708 return err;
710 (*pte)->mode = 0;
711 p = buf;
712 while (p < space) {
713 if (*p < '0' && *p > '7') {
714 err = got_error(GOT_ERR_BAD_OBJ_DATA);
715 goto done;
717 (*pte)->mode <<= 3;
718 (*pte)->mode |= *p - '0';
719 p++;
722 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
723 err = got_error(GOT_ERR_BAD_OBJ_DATA);
724 goto done;
726 *name = space + 1;
727 buf += *elen;
728 (*pte)->id = buf;
729 *elen += SHA1_DIGEST_LENGTH;
730 done:
731 if (err) {
732 free(*pte);
733 *pte = NULL;
735 return err;
738 const struct got_error *
739 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
740 uint8_t *buf, size_t len)
742 const struct got_error *err = NULL;
743 size_t remain = len;
745 *nentries = 0;
746 if (remain == 0)
747 return NULL; /* tree is empty */
749 while (remain > 0) {
750 struct got_parsed_tree_entry *pte;
751 struct got_pathlist_entry *new = NULL;
752 const char *name;
753 size_t elen;
755 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
756 if (err)
757 goto done;
758 err = got_pathlist_insert(&new, entries, name, pte);
759 if (err)
760 goto done;
761 if (new == NULL) {
762 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
763 goto done;
765 buf += elen;
766 remain -= elen;
767 (*nentries)++;
770 if (remain != 0) {
771 err = got_error(GOT_ERR_BAD_OBJ_DATA);
772 goto done;
774 done:
775 if (err) {
776 got_object_parsed_tree_entries_free(entries);
777 *nentries = 0;
779 return err;
782 void
783 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
785 struct got_pathlist_entry *pe;
787 TAILQ_FOREACH(pe, entries, entry) {
788 struct got_parsed_tree_entry *pte = pe->data;
789 free(pte);
791 got_pathlist_free(entries);
794 void
795 got_object_tag_close(struct got_tag_object *tag)
797 if (tag->refcnt > 0) {
798 tag->refcnt--;
799 if (tag->refcnt > 0)
800 return;
803 free(tag->tag);
804 free(tag->tagger);
805 free(tag->tagmsg);
806 free(tag);
809 const struct got_error *
810 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
812 const struct got_error *err = NULL;
813 size_t remain = len;
814 char *s = buf;
815 size_t label_len;
817 if (remain == 0)
818 return got_error(GOT_ERR_BAD_OBJ_DATA);
820 *tag = calloc(1, sizeof(**tag));
821 if (*tag == NULL)
822 return got_error_from_errno("calloc");
824 label_len = strlen(GOT_TAG_LABEL_OBJECT);
825 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
826 remain -= label_len;
827 if (remain < SHA1_DIGEST_STRING_LENGTH) {
828 err = got_error(GOT_ERR_BAD_OBJ_DATA);
829 goto done;
831 s += label_len;
832 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
833 err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 goto done;
836 remain -= SHA1_DIGEST_STRING_LENGTH;
837 s += SHA1_DIGEST_STRING_LENGTH;
838 } else {
839 err = got_error(GOT_ERR_BAD_OBJ_DATA);
840 goto done;
843 if (remain <= 0) {
844 err = got_error(GOT_ERR_BAD_OBJ_DATA);
845 goto done;
848 label_len = strlen(GOT_TAG_LABEL_TYPE);
849 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
850 remain -= label_len;
851 if (remain <= 0) {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
853 goto done;
855 s += label_len;
856 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
857 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
858 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
859 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
860 s += label_len;
861 remain -= label_len;
862 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
863 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
864 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
865 label_len = strlen(GOT_OBJ_LABEL_TREE);
866 s += label_len;
867 remain -= label_len;
868 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
869 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
870 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
871 label_len = strlen(GOT_OBJ_LABEL_BLOB);
872 s += label_len;
873 remain -= label_len;
874 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
875 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
876 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
877 label_len = strlen(GOT_OBJ_LABEL_TAG);
878 s += label_len;
879 remain -= label_len;
880 } else {
881 err = got_error(GOT_ERR_BAD_OBJ_DATA);
882 goto done;
885 if (remain <= 0 || *s != '\n') {
886 err = got_error(GOT_ERR_BAD_OBJ_DATA);
887 goto done;
889 s++;
890 remain--;
891 if (remain <= 0) {
892 err = got_error(GOT_ERR_BAD_OBJ_DATA);
893 goto done;
895 } else {
896 err = got_error(GOT_ERR_BAD_OBJ_DATA);
897 goto done;
900 label_len = strlen(GOT_TAG_LABEL_TAG);
901 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
902 char *p;
903 size_t slen;
904 remain -= label_len;
905 if (remain <= 0) {
906 err = got_error(GOT_ERR_BAD_OBJ_DATA);
907 goto done;
909 s += label_len;
910 p = memchr(s, '\n', remain);
911 if (p == NULL) {
912 err = got_error(GOT_ERR_BAD_OBJ_DATA);
913 goto done;
915 *p = '\0';
916 slen = strlen(s);
917 (*tag)->tag = strndup(s, slen);
918 if ((*tag)->tag == NULL) {
919 err = got_error_from_errno("strndup");
920 goto done;
922 s += slen + 1;
923 remain -= slen + 1;
924 if (remain <= 0) {
925 err = got_error(GOT_ERR_BAD_OBJ_DATA);
926 goto done;
928 } else {
929 err = got_error(GOT_ERR_BAD_OBJ_DATA);
930 goto done;
933 label_len = strlen(GOT_TAG_LABEL_TAGGER);
934 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
935 char *p;
936 size_t slen;
938 remain -= label_len;
939 if (remain <= 0) {
940 err = got_error(GOT_ERR_BAD_OBJ_DATA);
941 goto done;
943 s += label_len;
944 p = memchr(s, '\n', remain);
945 if (p == NULL) {
946 err = got_error(GOT_ERR_BAD_OBJ_DATA);
947 goto done;
949 *p = '\0';
950 slen = strlen(s);
951 err = parse_commit_time(&(*tag)->tagger_time,
952 &(*tag)->tagger_gmtoff, s);
953 if (err)
954 goto done;
955 (*tag)->tagger = strdup(s);
956 if ((*tag)->tagger == NULL) {
957 err = got_error_from_errno("strdup");
958 goto done;
960 s += slen + 1;
961 remain -= slen + 1;
962 if (remain < 0) {
963 err = got_error(GOT_ERR_BAD_OBJ_DATA);
964 goto done;
966 } else {
967 /* Some old tags in the Linux git repo have no tagger. */
968 (*tag)->tagger = strdup("");
969 if ((*tag)->tagger == NULL) {
970 err = got_error_from_errno("strdup");
971 goto done;
975 (*tag)->tagmsg = strndup(s, remain);
976 if ((*tag)->tagmsg == NULL) {
977 err = got_error_from_errno("strndup");
978 goto done;
980 done:
981 if (err) {
982 got_object_tag_close(*tag);
983 *tag = NULL;
985 return err;
988 const struct got_error *
989 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
991 const struct got_error *err = NULL;
992 static const size_t blocksize = 512;
993 size_t n, total, remain;
994 uint8_t *buf;
996 *outbuf = NULL;
997 *outlen = 0;
999 buf = malloc(blocksize);
1000 if (buf == NULL)
1001 return got_error_from_errno("malloc");
1003 remain = blocksize;
1004 total = 0;
1005 for (;;) {
1006 if (remain == 0) {
1007 uint8_t *newbuf;
1008 newbuf = reallocarray(buf, 1, total + blocksize);
1009 if (newbuf == NULL) {
1010 err = got_error_from_errno("reallocarray");
1011 goto done;
1013 buf = newbuf;
1014 remain += blocksize;
1016 n = fread(buf + total, 1, remain, f);
1017 if (n == 0) {
1018 if (ferror(f)) {
1019 err = got_ferror(f, GOT_ERR_IO);
1020 goto done;
1022 break; /* EOF */
1024 remain -= n;
1025 total += n;
1028 done:
1029 if (err == NULL) {
1030 *outbuf = buf;
1031 *outlen = total;
1032 } else
1033 free(buf);
1034 return err;