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>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <zlib.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <time.h>
34 #include <unistd.h>
36 #include "got_compat.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_parse.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #endif
58 struct got_object_id *
59 got_object_id_dup(struct got_object_id *id1)
60 {
61 struct got_object_id *id2;
63 id2 = malloc(sizeof(*id2));
64 if (id2 == NULL)
65 return NULL;
66 memcpy(id2, id1, sizeof(*id2));
67 return id2;
68 }
70 int
71 got_object_id_cmp(const struct got_object_id *id1,
72 const struct got_object_id *id2)
73 {
74 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
75 }
77 const struct got_error *
78 got_object_qid_alloc_partial(struct got_object_qid **qid)
79 {
80 const struct got_error *err = NULL;
82 *qid = malloc(sizeof(**qid));
83 if (*qid == NULL)
84 return got_error_from_errno("malloc");
86 (*qid)->id = malloc(sizeof(*((*qid)->id)));
87 if ((*qid)->id == NULL) {
88 err = got_error_from_errno("malloc");
89 got_object_qid_free(*qid);
90 *qid = NULL;
91 return err;
92 }
93 (*qid)->data = NULL;
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("malloc");
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 (!STAILQ_EMPTY(&obj->deltas.entries)) {
128 delta = STAILQ_FIRST(&obj->deltas.entries);
129 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
130 free(delta);
133 free(obj);
136 const struct got_error *
137 got_object_raw_close(struct got_raw_object *obj)
139 const struct got_error *err = NULL;
141 if (obj->refcnt > 0) {
142 obj->refcnt--;
143 if (obj->refcnt > 0)
144 return NULL;
147 free(obj->read_buf);
148 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
149 err = got_error_from_errno("fclose");
150 free(obj->data);
151 free(obj);
152 return err;
155 void
156 got_object_qid_free(struct got_object_qid *qid)
158 free(qid->id);
159 free(qid);
162 void
163 got_object_id_queue_free(struct got_object_id_queue *ids)
165 struct got_object_qid *qid;
167 while (!STAILQ_EMPTY(ids)) {
168 qid = STAILQ_FIRST(ids);
169 STAILQ_REMOVE_HEAD(ids, entry);
170 got_object_qid_free(qid);
174 const struct got_error *
175 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
177 const char *obj_labels[] = {
178 GOT_OBJ_LABEL_COMMIT,
179 GOT_OBJ_LABEL_TREE,
180 GOT_OBJ_LABEL_BLOB,
181 GOT_OBJ_LABEL_TAG,
182 };
183 const int obj_types[] = {
184 GOT_OBJ_TYPE_COMMIT,
185 GOT_OBJ_TYPE_TREE,
186 GOT_OBJ_TYPE_BLOB,
187 GOT_OBJ_TYPE_TAG,
188 };
189 int type = 0;
190 size_t size = 0, hdrlen = 0;
191 size_t i;
193 *obj = NULL;
195 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
196 if (hdrlen > len)
197 return got_error(GOT_ERR_BAD_OBJ_HDR);
199 for (i = 0; i < nitems(obj_labels); i++) {
200 const char *label = obj_labels[i];
201 size_t label_len = strlen(label);
202 const char *errstr;
204 if (strncmp(buf, label, label_len) != 0)
205 continue;
207 type = obj_types[i];
208 if (len <= label_len)
209 return got_error(GOT_ERR_BAD_OBJ_HDR);
210 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
211 if (errstr != NULL)
212 return got_error(GOT_ERR_BAD_OBJ_HDR);
213 break;
216 if (type == 0)
217 return got_error(GOT_ERR_BAD_OBJ_HDR);
219 *obj = calloc(1, sizeof(**obj));
220 if (*obj == NULL)
221 return got_error_from_errno("calloc");
222 (*obj)->type = type;
223 (*obj)->hdrlen = hdrlen;
224 (*obj)->size = size;
225 return NULL;
228 const struct got_error *
229 got_object_read_header(struct got_object **obj, int fd)
231 const struct got_error *err;
232 struct got_inflate_buf zb;
233 uint8_t *buf;
234 const size_t zbsize = 64;
235 size_t outlen, totlen;
236 int nbuf = 1;
238 *obj = NULL;
240 buf = malloc(zbsize);
241 if (buf == NULL)
242 return got_error_from_errno("malloc");
244 err = got_inflate_init(&zb, buf, zbsize, NULL);
245 if (err)
246 return err;
248 totlen = 0;
249 do {
250 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
251 if (err)
252 goto done;
253 if (outlen == 0)
254 break;
255 totlen += outlen;
256 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
257 uint8_t *newbuf;
258 nbuf++;
259 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
260 if (newbuf == NULL) {
261 err = got_error_from_errno("recallocarray");
262 goto done;
264 buf = newbuf;
265 zb.outbuf = newbuf + totlen;
266 zb.outlen = (nbuf * zbsize) - totlen;
268 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
270 err = got_object_parse_header(obj, buf, totlen);
271 done:
272 free(buf);
273 got_inflate_end(&zb);
274 return err;
277 struct got_commit_object *
278 got_object_commit_alloc_partial(void)
280 struct got_commit_object *commit;
282 commit = calloc(1, sizeof(*commit));
283 if (commit == NULL)
284 return NULL;
285 commit->tree_id = malloc(sizeof(*commit->tree_id));
286 if (commit->tree_id == NULL) {
287 free(commit);
288 return NULL;
291 STAILQ_INIT(&commit->parent_ids);
293 return commit;
296 const struct got_error *
297 got_object_commit_add_parent(struct got_commit_object *commit,
298 const char *id_str)
300 const struct got_error *err = NULL;
301 struct got_object_qid *qid;
303 err = got_object_qid_alloc_partial(&qid);
304 if (err)
305 return err;
307 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
308 err = got_error(GOT_ERR_BAD_OBJ_DATA);
309 got_object_qid_free(qid);
310 return err;
313 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
314 commit->nparents++;
316 return NULL;
319 static const struct got_error *
320 parse_gmtoff(time_t *gmtoff, const char *tzstr)
322 int sign = 1;
323 const char *p = tzstr;
324 time_t h, m;
326 *gmtoff = 0;
328 if (*p == '-')
329 sign = -1;
330 else if (*p != '+')
331 return got_error(GOT_ERR_BAD_OBJ_DATA);
332 p++;
333 if (!isdigit(*p) && !isdigit(*(p + 1)))
334 return got_error(GOT_ERR_BAD_OBJ_DATA);
335 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
337 p += 2;
338 if (!isdigit(*p) && !isdigit(*(p + 1)))
339 return got_error(GOT_ERR_BAD_OBJ_DATA);
340 m = ((*p - '0') * 10) + (*(p + 1) - '0');
342 *gmtoff = (h * 60 * 60 + m * 60) * sign;
343 return NULL;
346 static const struct got_error *
347 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
349 const struct got_error *err = NULL;
350 const char *errstr;
351 char *space, *tzstr;
353 /* Parse and strip off trailing timezone indicator string. */
354 space = strrchr(committer, ' ');
355 if (space == NULL)
356 return got_error(GOT_ERR_BAD_OBJ_DATA);
357 tzstr = strdup(space + 1);
358 if (tzstr == NULL)
359 return got_error_from_errno("strdup");
360 err = parse_gmtoff(gmtoff, tzstr);
361 free(tzstr);
362 if (err) {
363 if (err->code != GOT_ERR_BAD_OBJ_DATA)
364 return err;
365 /* Old versions of Git omitted the timestamp. */
366 *time = 0;
367 *gmtoff = 0;
368 return NULL;
370 *space = '\0';
372 /* Timestamp is separated from committer name + email by space. */
373 space = strrchr(committer, ' ');
374 if (space == NULL)
375 return got_error(GOT_ERR_BAD_OBJ_DATA);
377 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
378 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
379 if (errstr)
380 return got_error(GOT_ERR_BAD_OBJ_DATA);
382 /* Strip off parsed time information, leaving just author and email. */
383 *space = '\0';
385 return NULL;
388 void
389 got_object_commit_close(struct got_commit_object *commit)
391 if (commit->refcnt > 0) {
392 commit->refcnt--;
393 if (commit->refcnt > 0)
394 return;
397 got_object_id_queue_free(&commit->parent_ids);
398 free(commit->tree_id);
399 free(commit->author);
400 free(commit->committer);
401 free(commit->logmsg);
402 free(commit);
405 struct got_object_id *
406 got_object_commit_get_tree_id(struct got_commit_object *commit)
408 return commit->tree_id;
411 int
412 got_object_commit_get_nparents(struct got_commit_object *commit)
414 return commit->nparents;
417 const struct got_object_id_queue *
418 got_object_commit_get_parent_ids(struct got_commit_object *commit)
420 return &commit->parent_ids;
423 const char *
424 got_object_commit_get_author(struct got_commit_object *commit)
426 return commit->author;
429 time_t
430 got_object_commit_get_author_time(struct got_commit_object *commit)
432 return commit->author_time;
435 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
437 return commit->author_gmtoff;
440 const char *
441 got_object_commit_get_committer(struct got_commit_object *commit)
443 return commit->committer;
446 time_t
447 got_object_commit_get_committer_time(struct got_commit_object *commit)
449 return commit->committer_time;
452 time_t
453 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
455 return commit->committer_gmtoff;
458 const struct got_error *
459 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
461 const struct got_error *err = NULL;
462 const char *src;
463 char *dst;
464 size_t len;
466 len = strlen(commit->logmsg);
467 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
468 if (*logmsg == NULL)
469 return got_error_from_errno("malloc");
471 /*
472 * Strip out unusual headers. Headers are separated from the commit
473 * message body by a single empty line.
474 */
475 src = commit->logmsg;
476 dst = *logmsg;
477 while (*src != '\0' && *src != '\n') {
478 int copy_header = 1, eol = 0;
479 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
480 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
481 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
482 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
483 strncmp(src, GOT_COMMIT_LABEL_PARENT,
484 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
485 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
486 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
487 copy_header = 0;
489 while (*src != '\0' && !eol) {
490 if (copy_header) {
491 *dst = *src;
492 dst++;
494 if (*src == '\n')
495 eol = 1;
496 src++;
499 *dst = '\0';
501 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
502 err = got_error(GOT_ERR_NO_SPACE);
503 goto done;
506 /* Trim redundant trailing whitespace. */
507 len = strlen(*logmsg);
508 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
509 isspace((unsigned char)(*logmsg)[len - 1])) {
510 (*logmsg)[len - 1] = '\0';
511 len--;
514 /* Append a trailing newline if missing. */
515 if (len > 0 && (*logmsg)[len - 1] != '\n') {
516 (*logmsg)[len] = '\n';
517 (*logmsg)[len + 1] = '\0';
519 done:
520 if (err) {
521 free(*logmsg);
522 *logmsg = NULL;
524 return err;
527 const char *
528 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
530 return commit->logmsg;
533 const struct got_error *
534 got_object_parse_commit(struct got_commit_object **commit, char *buf,
535 size_t len)
537 const struct got_error *err = NULL;
538 char *s = buf;
539 size_t label_len;
540 ssize_t remain = (ssize_t)len;
542 if (remain == 0)
543 return got_error(GOT_ERR_BAD_OBJ_DATA);
545 *commit = got_object_commit_alloc_partial();
546 if (*commit == NULL)
547 return got_error_from_errno("got_object_commit_alloc_partial");
549 label_len = strlen(GOT_COMMIT_LABEL_TREE);
550 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
551 remain -= label_len;
552 if (remain < SHA1_DIGEST_STRING_LENGTH) {
553 err = got_error(GOT_ERR_BAD_OBJ_DATA);
554 goto done;
556 s += label_len;
557 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
558 err = got_error(GOT_ERR_BAD_OBJ_DATA);
559 goto done;
561 remain -= SHA1_DIGEST_STRING_LENGTH;
562 s += SHA1_DIGEST_STRING_LENGTH;
563 } else {
564 err = got_error(GOT_ERR_BAD_OBJ_DATA);
565 goto done;
568 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
569 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
570 remain -= label_len;
571 if (remain < SHA1_DIGEST_STRING_LENGTH) {
572 err = got_error(GOT_ERR_BAD_OBJ_DATA);
573 goto done;
575 s += label_len;
576 err = got_object_commit_add_parent(*commit, s);
577 if (err)
578 goto done;
580 remain -= SHA1_DIGEST_STRING_LENGTH;
581 s += SHA1_DIGEST_STRING_LENGTH;
584 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
585 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
586 char *p;
587 size_t slen;
589 remain -= label_len;
590 if (remain <= 0) {
591 err = got_error(GOT_ERR_BAD_OBJ_DATA);
592 goto done;
594 s += label_len;
595 p = memchr(s, '\n', remain);
596 if (p == NULL) {
597 err = got_error(GOT_ERR_BAD_OBJ_DATA);
598 goto done;
600 *p = '\0';
601 slen = strlen(s);
602 err = parse_commit_time(&(*commit)->author_time,
603 &(*commit)->author_gmtoff, s);
604 if (err)
605 goto done;
606 (*commit)->author = strdup(s);
607 if ((*commit)->author == NULL) {
608 err = got_error_from_errno("strdup");
609 goto done;
611 s += slen + 1;
612 remain -= slen + 1;
615 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
616 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
617 char *p;
618 size_t slen;
620 remain -= label_len;
621 if (remain <= 0) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 goto done;
625 s += label_len;
626 p = memchr(s, '\n', remain);
627 if (p == NULL) {
628 err = got_error(GOT_ERR_BAD_OBJ_DATA);
629 goto done;
631 *p = '\0';
632 slen = strlen(s);
633 err = parse_commit_time(&(*commit)->committer_time,
634 &(*commit)->committer_gmtoff, s);
635 if (err)
636 goto done;
637 (*commit)->committer = strdup(s);
638 if ((*commit)->committer == NULL) {
639 err = got_error_from_errno("strdup");
640 goto done;
642 s += slen + 1;
643 remain -= slen + 1;
646 (*commit)->logmsg = strndup(s, remain);
647 if ((*commit)->logmsg == NULL) {
648 err = got_error_from_errno("strndup");
649 goto done;
651 done:
652 if (err) {
653 got_object_commit_close(*commit);
654 *commit = NULL;
656 return err;
659 void
660 got_object_tree_close(struct got_tree_object *tree)
662 if (tree->refcnt > 0) {
663 tree->refcnt--;
664 if (tree->refcnt > 0)
665 return;
668 free(tree->entries);
669 free(tree);
672 static const struct got_error *
673 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
674 size_t *elen, char *buf,
675 size_t maxlen)
677 char *p, *space;
678 const struct got_error *err = NULL;
680 *name = NULL;
681 *elen = 0;
683 *pte = malloc(sizeof(**pte));
684 if (*pte == NULL)
685 return got_error_from_errno("malloc");
687 *elen = strnlen(buf, maxlen) + 1;
688 if (*elen > maxlen) {
689 free(*pte);
690 *pte = NULL;
691 return got_error(GOT_ERR_BAD_OBJ_DATA);
694 space = memchr(buf, ' ', *elen);
695 if (space == NULL || space <= buf) {
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
697 free(*pte);
698 *pte = NULL;
699 return err;
701 (*pte)->mode = 0;
702 p = buf;
703 while (p < space) {
704 if (*p < '0' && *p > '7') {
705 err = got_error(GOT_ERR_BAD_OBJ_DATA);
706 goto done;
708 (*pte)->mode <<= 3;
709 (*pte)->mode |= *p - '0';
710 p++;
713 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
714 err = got_error(GOT_ERR_BAD_OBJ_DATA);
715 goto done;
717 *name = space + 1;
718 buf += *elen;
719 (*pte)->id = buf;
720 *elen += SHA1_DIGEST_LENGTH;
721 done:
722 if (err) {
723 free(*pte);
724 *pte = NULL;
726 return err;
729 const struct got_error *
730 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
731 uint8_t *buf, size_t len)
733 const struct got_error *err = NULL;
734 size_t remain = len;
736 *nentries = 0;
737 if (remain == 0)
738 return NULL; /* tree is empty */
740 while (remain > 0) {
741 struct got_parsed_tree_entry *pte;
742 struct got_pathlist_entry *new = NULL;
743 const char *name;
744 size_t elen;
746 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
747 if (err)
748 goto done;
749 err = got_pathlist_insert(&new, entries, name, pte);
750 if (err)
751 goto done;
752 if (new == NULL) {
753 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
754 goto done;
756 buf += elen;
757 remain -= elen;
758 (*nentries)++;
761 if (remain != 0) {
762 err = got_error(GOT_ERR_BAD_OBJ_DATA);
763 goto done;
765 done:
766 if (err) {
767 got_object_parsed_tree_entries_free(entries);
768 *nentries = 0;
770 return err;
773 void
774 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
776 struct got_pathlist_entry *pe;
778 TAILQ_FOREACH(pe, entries, entry) {
779 struct got_parsed_tree_entry *pte = pe->data;
780 free(pte);
782 got_pathlist_free(entries);
785 void
786 got_object_tag_close(struct got_tag_object *tag)
788 if (tag->refcnt > 0) {
789 tag->refcnt--;
790 if (tag->refcnt > 0)
791 return;
794 free(tag->tag);
795 free(tag->tagger);
796 free(tag->tagmsg);
797 free(tag);
800 const struct got_error *
801 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
803 const struct got_error *err = NULL;
804 size_t remain = len;
805 char *s = buf;
806 size_t label_len;
808 if (remain == 0)
809 return got_error(GOT_ERR_BAD_OBJ_DATA);
811 *tag = calloc(1, sizeof(**tag));
812 if (*tag == NULL)
813 return got_error_from_errno("calloc");
815 label_len = strlen(GOT_TAG_LABEL_OBJECT);
816 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
817 remain -= label_len;
818 if (remain < SHA1_DIGEST_STRING_LENGTH) {
819 err = got_error(GOT_ERR_BAD_OBJ_DATA);
820 goto done;
822 s += label_len;
823 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
824 err = got_error(GOT_ERR_BAD_OBJ_DATA);
825 goto done;
827 remain -= SHA1_DIGEST_STRING_LENGTH;
828 s += SHA1_DIGEST_STRING_LENGTH;
829 } else {
830 err = got_error(GOT_ERR_BAD_OBJ_DATA);
831 goto done;
834 if (remain <= 0) {
835 err = got_error(GOT_ERR_BAD_OBJ_DATA);
836 goto done;
839 label_len = strlen(GOT_TAG_LABEL_TYPE);
840 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
841 remain -= label_len;
842 if (remain <= 0) {
843 err = got_error(GOT_ERR_BAD_OBJ_DATA);
844 goto done;
846 s += label_len;
847 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
848 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
849 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
850 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
851 s += label_len;
852 remain -= label_len;
853 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
854 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
855 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
856 label_len = strlen(GOT_OBJ_LABEL_TREE);
857 s += label_len;
858 remain -= label_len;
859 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
860 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
861 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
862 label_len = strlen(GOT_OBJ_LABEL_BLOB);
863 s += label_len;
864 remain -= label_len;
865 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
866 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
867 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
868 label_len = strlen(GOT_OBJ_LABEL_TAG);
869 s += label_len;
870 remain -= label_len;
871 } else {
872 err = got_error(GOT_ERR_BAD_OBJ_DATA);
873 goto done;
876 if (remain <= 0 || *s != '\n') {
877 err = got_error(GOT_ERR_BAD_OBJ_DATA);
878 goto done;
880 s++;
881 remain--;
882 if (remain <= 0) {
883 err = got_error(GOT_ERR_BAD_OBJ_DATA);
884 goto done;
886 } else {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
888 goto done;
891 label_len = strlen(GOT_TAG_LABEL_TAG);
892 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
893 char *p;
894 size_t slen;
895 remain -= label_len;
896 if (remain <= 0) {
897 err = got_error(GOT_ERR_BAD_OBJ_DATA);
898 goto done;
900 s += label_len;
901 p = memchr(s, '\n', remain);
902 if (p == NULL) {
903 err = got_error(GOT_ERR_BAD_OBJ_DATA);
904 goto done;
906 *p = '\0';
907 slen = strlen(s);
908 (*tag)->tag = strndup(s, slen);
909 if ((*tag)->tag == NULL) {
910 err = got_error_from_errno("strndup");
911 goto done;
913 s += slen + 1;
914 remain -= slen + 1;
915 if (remain <= 0) {
916 err = got_error(GOT_ERR_BAD_OBJ_DATA);
917 goto done;
919 } else {
920 err = got_error(GOT_ERR_BAD_OBJ_DATA);
921 goto done;
924 label_len = strlen(GOT_TAG_LABEL_TAGGER);
925 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
926 char *p;
927 size_t slen;
929 remain -= label_len;
930 if (remain <= 0) {
931 err = got_error(GOT_ERR_BAD_OBJ_DATA);
932 goto done;
934 s += label_len;
935 p = memchr(s, '\n', remain);
936 if (p == NULL) {
937 err = got_error(GOT_ERR_BAD_OBJ_DATA);
938 goto done;
940 *p = '\0';
941 slen = strlen(s);
942 err = parse_commit_time(&(*tag)->tagger_time,
943 &(*tag)->tagger_gmtoff, s);
944 if (err)
945 goto done;
946 (*tag)->tagger = strdup(s);
947 if ((*tag)->tagger == NULL) {
948 err = got_error_from_errno("strdup");
949 goto done;
951 s += slen + 1;
952 remain -= slen + 1;
953 if (remain < 0) {
954 err = got_error(GOT_ERR_BAD_OBJ_DATA);
955 goto done;
957 } else {
958 /* Some old tags in the Linux git repo have no tagger. */
959 (*tag)->tagger = strdup("");
960 if ((*tag)->tagger == NULL) {
961 err = got_error_from_errno("strdup");
962 goto done;
966 (*tag)->tagmsg = strndup(s, remain);
967 if ((*tag)->tagmsg == NULL) {
968 err = got_error_from_errno("strndup");
969 goto done;
971 done:
972 if (err) {
973 got_object_tag_close(*tag);
974 *tag = NULL;
976 return err;
979 const struct got_error *
980 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
982 const struct got_error *err = NULL;
983 static const size_t blocksize = 512;
984 size_t n, total, remain;
985 uint8_t *buf;
987 *outbuf = NULL;
988 *outlen = 0;
990 buf = malloc(blocksize);
991 if (buf == NULL)
992 return got_error_from_errno("malloc");
994 remain = blocksize;
995 total = 0;
996 for (;;) {
997 if (remain == 0) {
998 uint8_t *newbuf;
999 newbuf = reallocarray(buf, 1, total + blocksize);
1000 if (newbuf == NULL) {
1001 err = got_error_from_errno("reallocarray");
1002 goto done;
1004 buf = newbuf;
1005 remain += blocksize;
1007 n = fread(buf + total, 1, remain, f);
1008 if (n == 0) {
1009 if (ferror(f)) {
1010 err = got_ferror(f, GOT_ERR_IO);
1011 goto done;
1013 break; /* EOF */
1015 remain -= n;
1016 total += n;
1019 done:
1020 if (err == NULL) {
1021 *outbuf = buf;
1022 *outlen = total;
1023 } else
1024 free(buf);
1025 return err;