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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <zlib.h>
29 #include <ctype.h>
30 #include <limits.h>
31 #include <time.h>
32 #include <unistd.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
39 #include "got_opentemp.h"
40 #include "got_path.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.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 struct got_object_id *
57 got_object_id_dup(struct got_object_id *id1)
58 {
59 struct got_object_id *id2;
61 id2 = malloc(sizeof(*id2));
62 if (id2 == NULL)
63 return NULL;
64 memcpy(id2, id1, sizeof(*id2));
65 return id2;
66 }
68 int
69 got_object_id_cmp(const struct got_object_id *id1,
70 const struct got_object_id *id2)
71 {
72 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
73 }
75 const struct got_error *
76 got_object_qid_alloc_partial(struct got_object_qid **qid)
77 {
78 const struct got_error *err = NULL;
80 *qid = malloc(sizeof(**qid));
81 if (*qid == NULL)
82 return got_error_from_errno("malloc");
84 (*qid)->id = malloc(sizeof(*((*qid)->id)));
85 if ((*qid)->id == NULL) {
86 err = got_error_from_errno("malloc");
87 got_object_qid_free(*qid);
88 *qid = NULL;
89 return err;
90 }
91 (*qid)->data = NULL;
93 return NULL;
94 }
96 const struct got_error *
97 got_object_id_str(char **outbuf, struct got_object_id *id)
98 {
99 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
101 *outbuf = malloc(len);
102 if (*outbuf == NULL)
103 return got_error_from_errno("malloc");
105 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
106 free(*outbuf);
107 *outbuf = NULL;
108 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
111 return NULL;
114 void
115 got_object_close(struct got_object *obj)
117 if (obj->refcnt > 0) {
118 obj->refcnt--;
119 if (obj->refcnt > 0)
120 return;
123 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
124 struct got_delta *delta;
125 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
126 delta = STAILQ_FIRST(&obj->deltas.entries);
127 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
128 free(delta);
131 free(obj);
134 const struct got_error *
135 got_object_raw_close(struct got_raw_object *obj)
137 const struct got_error *err = NULL;
139 if (obj->refcnt > 0) {
140 obj->refcnt--;
141 if (obj->refcnt > 0)
142 return NULL;
145 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
146 err = got_error_from_errno("fclose");
147 free(obj->data);
148 free(obj);
149 return err;
152 void
153 got_object_qid_free(struct got_object_qid *qid)
155 free(qid->id);
156 free(qid);
159 void
160 got_object_id_queue_free(struct got_object_id_queue *ids)
162 struct got_object_qid *qid;
164 while (!STAILQ_EMPTY(ids)) {
165 qid = STAILQ_FIRST(ids);
166 STAILQ_REMOVE_HEAD(ids, entry);
167 got_object_qid_free(qid);
171 const struct got_error *
172 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
174 const char *obj_labels[] = {
175 GOT_OBJ_LABEL_COMMIT,
176 GOT_OBJ_LABEL_TREE,
177 GOT_OBJ_LABEL_BLOB,
178 GOT_OBJ_LABEL_TAG,
179 };
180 const int obj_types[] = {
181 GOT_OBJ_TYPE_COMMIT,
182 GOT_OBJ_TYPE_TREE,
183 GOT_OBJ_TYPE_BLOB,
184 GOT_OBJ_TYPE_TAG,
185 };
186 int type = 0;
187 size_t size = 0, hdrlen = 0;
188 size_t i;
190 *obj = NULL;
192 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
193 if (hdrlen > len)
194 return got_error(GOT_ERR_BAD_OBJ_HDR);
196 for (i = 0; i < nitems(obj_labels); i++) {
197 const char *label = obj_labels[i];
198 size_t label_len = strlen(label);
199 const char *errstr;
201 if (strncmp(buf, label, label_len) != 0)
202 continue;
204 type = obj_types[i];
205 if (len <= label_len)
206 return got_error(GOT_ERR_BAD_OBJ_HDR);
207 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
208 if (errstr != NULL)
209 return got_error(GOT_ERR_BAD_OBJ_HDR);
210 break;
213 if (type == 0)
214 return got_error(GOT_ERR_BAD_OBJ_HDR);
216 *obj = calloc(1, sizeof(**obj));
217 if (*obj == NULL)
218 return got_error_from_errno("calloc");
219 (*obj)->type = type;
220 (*obj)->hdrlen = hdrlen;
221 (*obj)->size = size;
222 return NULL;
225 const struct got_error *
226 got_object_read_header(struct got_object **obj, int fd)
228 const struct got_error *err;
229 struct got_inflate_buf zb;
230 uint8_t *buf;
231 const size_t zbsize = 64;
232 size_t outlen, totlen;
233 int nbuf = 1;
235 *obj = NULL;
237 buf = malloc(zbsize);
238 if (buf == NULL)
239 return got_error_from_errno("malloc");
241 err = got_inflate_init(&zb, buf, zbsize, NULL);
242 if (err)
243 return err;
245 totlen = 0;
246 do {
247 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
248 if (err)
249 goto done;
250 if (outlen == 0)
251 break;
252 totlen += outlen;
253 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
254 uint8_t *newbuf;
255 nbuf++;
256 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
257 if (newbuf == NULL) {
258 err = got_error_from_errno("recallocarray");
259 goto done;
261 buf = newbuf;
262 zb.outbuf = newbuf + totlen;
263 zb.outlen = (nbuf * zbsize) - totlen;
265 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
267 err = got_object_parse_header(obj, buf, totlen);
268 done:
269 free(buf);
270 got_inflate_end(&zb);
271 return err;
274 struct got_commit_object *
275 got_object_commit_alloc_partial(void)
277 struct got_commit_object *commit;
279 commit = calloc(1, sizeof(*commit));
280 if (commit == NULL)
281 return NULL;
282 commit->tree_id = malloc(sizeof(*commit->tree_id));
283 if (commit->tree_id == NULL) {
284 free(commit);
285 return NULL;
288 STAILQ_INIT(&commit->parent_ids);
290 return commit;
293 const struct got_error *
294 got_object_commit_add_parent(struct got_commit_object *commit,
295 const char *id_str)
297 const struct got_error *err = NULL;
298 struct got_object_qid *qid;
300 err = got_object_qid_alloc_partial(&qid);
301 if (err)
302 return err;
304 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
305 err = got_error(GOT_ERR_BAD_OBJ_DATA);
306 got_object_qid_free(qid);
307 return err;
310 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
311 commit->nparents++;
313 return NULL;
316 static const struct got_error *
317 parse_gmtoff(time_t *gmtoff, const char *tzstr)
319 int sign = 1;
320 const char *p = tzstr;
321 time_t h, m;
323 *gmtoff = 0;
325 if (*p == '-')
326 sign = -1;
327 else if (*p != '+')
328 return got_error(GOT_ERR_BAD_OBJ_DATA);
329 p++;
330 if (!isdigit(*p) && !isdigit(*(p + 1)))
331 return got_error(GOT_ERR_BAD_OBJ_DATA);
332 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
334 p += 2;
335 if (!isdigit(*p) && !isdigit(*(p + 1)))
336 return got_error(GOT_ERR_BAD_OBJ_DATA);
337 m = ((*p - '0') * 10) + (*(p + 1) - '0');
339 *gmtoff = (h * 60 * 60 + m * 60) * sign;
340 return NULL;
343 static const struct got_error *
344 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
346 const struct got_error *err = NULL;
347 const char *errstr;
348 char *space, *tzstr;
350 /* Parse and strip off trailing timezone indicator string. */
351 space = strrchr(committer, ' ');
352 if (space == NULL)
353 return got_error(GOT_ERR_BAD_OBJ_DATA);
354 tzstr = strdup(space + 1);
355 if (tzstr == NULL)
356 return got_error_from_errno("strdup");
357 err = parse_gmtoff(gmtoff, tzstr);
358 free(tzstr);
359 if (err) {
360 if (err->code != GOT_ERR_BAD_OBJ_DATA)
361 return err;
362 /* Old versions of Git omitted the timestamp. */
363 *time = 0;
364 *gmtoff = 0;
365 return NULL;
367 *space = '\0';
369 /* Timestamp is separated from committer name + email by space. */
370 space = strrchr(committer, ' ');
371 if (space == NULL)
372 return got_error(GOT_ERR_BAD_OBJ_DATA);
374 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
375 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
376 if (errstr)
377 return got_error(GOT_ERR_BAD_OBJ_DATA);
379 /* Strip off parsed time information, leaving just author and email. */
380 *space = '\0';
382 return NULL;
385 void
386 got_object_commit_close(struct got_commit_object *commit)
388 if (commit->refcnt > 0) {
389 commit->refcnt--;
390 if (commit->refcnt > 0)
391 return;
394 got_object_id_queue_free(&commit->parent_ids);
395 free(commit->tree_id);
396 free(commit->author);
397 free(commit->committer);
398 free(commit->logmsg);
399 free(commit);
402 struct got_object_id *
403 got_object_commit_get_tree_id(struct got_commit_object *commit)
405 return commit->tree_id;
408 int
409 got_object_commit_get_nparents(struct got_commit_object *commit)
411 return commit->nparents;
414 const struct got_object_id_queue *
415 got_object_commit_get_parent_ids(struct got_commit_object *commit)
417 return &commit->parent_ids;
420 const char *
421 got_object_commit_get_author(struct got_commit_object *commit)
423 return commit->author;
426 time_t
427 got_object_commit_get_author_time(struct got_commit_object *commit)
429 return commit->author_time;
432 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
434 return commit->author_gmtoff;
437 const char *
438 got_object_commit_get_committer(struct got_commit_object *commit)
440 return commit->committer;
443 time_t
444 got_object_commit_get_committer_time(struct got_commit_object *commit)
446 return commit->committer_time;
449 time_t
450 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
452 return commit->committer_gmtoff;
455 const struct got_error *
456 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
458 const struct got_error *err = NULL;
459 const char *src;
460 char *dst;
461 size_t len;
463 len = strlen(commit->logmsg);
464 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
465 if (*logmsg == NULL)
466 return got_error_from_errno("malloc");
468 /*
469 * Strip out unusual headers. Headers are separated from the commit
470 * message body by a single empty line.
471 */
472 src = commit->logmsg;
473 dst = *logmsg;
474 while (*src != '\0' && *src != '\n') {
475 int copy_header = 1, eol = 0;
476 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
477 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
478 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
479 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
480 strncmp(src, GOT_COMMIT_LABEL_PARENT,
481 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
482 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
483 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
484 copy_header = 0;
486 while (*src != '\0' && !eol) {
487 if (copy_header) {
488 *dst = *src;
489 dst++;
491 if (*src == '\n')
492 eol = 1;
493 src++;
496 *dst = '\0';
498 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
499 err = got_error(GOT_ERR_NO_SPACE);
500 goto done;
503 /* Trim redundant trailing whitespace. */
504 len = strlen(*logmsg);
505 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
506 isspace((unsigned char)(*logmsg)[len - 1])) {
507 (*logmsg)[len - 1] = '\0';
508 len--;
511 /* Append a trailing newline if missing. */
512 if (len > 0 && (*logmsg)[len - 1] != '\n') {
513 (*logmsg)[len] = '\n';
514 (*logmsg)[len + 1] = '\0';
516 done:
517 if (err) {
518 free(*logmsg);
519 *logmsg = NULL;
521 return err;
524 const char *
525 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
527 return commit->logmsg;
530 const struct got_error *
531 got_object_parse_commit(struct got_commit_object **commit, char *buf,
532 size_t len)
534 const struct got_error *err = NULL;
535 char *s = buf;
536 size_t label_len;
537 ssize_t remain = (ssize_t)len;
539 if (remain == 0)
540 return got_error(GOT_ERR_BAD_OBJ_DATA);
542 *commit = got_object_commit_alloc_partial();
543 if (*commit == NULL)
544 return got_error_from_errno("got_object_commit_alloc_partial");
546 label_len = strlen(GOT_COMMIT_LABEL_TREE);
547 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
548 remain -= label_len;
549 if (remain < SHA1_DIGEST_STRING_LENGTH) {
550 err = got_error(GOT_ERR_BAD_OBJ_DATA);
551 goto done;
553 s += label_len;
554 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
555 err = got_error(GOT_ERR_BAD_OBJ_DATA);
556 goto done;
558 remain -= SHA1_DIGEST_STRING_LENGTH;
559 s += SHA1_DIGEST_STRING_LENGTH;
560 } else {
561 err = got_error(GOT_ERR_BAD_OBJ_DATA);
562 goto done;
565 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
566 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
567 remain -= label_len;
568 if (remain < SHA1_DIGEST_STRING_LENGTH) {
569 err = got_error(GOT_ERR_BAD_OBJ_DATA);
570 goto done;
572 s += label_len;
573 err = got_object_commit_add_parent(*commit, s);
574 if (err)
575 goto done;
577 remain -= SHA1_DIGEST_STRING_LENGTH;
578 s += SHA1_DIGEST_STRING_LENGTH;
581 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
582 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
583 char *p;
584 size_t slen;
586 remain -= label_len;
587 if (remain <= 0) {
588 err = got_error(GOT_ERR_BAD_OBJ_DATA);
589 goto done;
591 s += label_len;
592 p = memchr(s, '\n', remain);
593 if (p == NULL) {
594 err = got_error(GOT_ERR_BAD_OBJ_DATA);
595 goto done;
597 *p = '\0';
598 slen = strlen(s);
599 err = parse_commit_time(&(*commit)->author_time,
600 &(*commit)->author_gmtoff, s);
601 if (err)
602 goto done;
603 (*commit)->author = strdup(s);
604 if ((*commit)->author == NULL) {
605 err = got_error_from_errno("strdup");
606 goto done;
608 s += slen + 1;
609 remain -= slen + 1;
612 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
613 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
614 char *p;
615 size_t slen;
617 remain -= label_len;
618 if (remain <= 0) {
619 err = got_error(GOT_ERR_BAD_OBJ_DATA);
620 goto done;
622 s += label_len;
623 p = memchr(s, '\n', remain);
624 if (p == NULL) {
625 err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 goto done;
628 *p = '\0';
629 slen = strlen(s);
630 err = parse_commit_time(&(*commit)->committer_time,
631 &(*commit)->committer_gmtoff, s);
632 if (err)
633 goto done;
634 (*commit)->committer = strdup(s);
635 if ((*commit)->committer == NULL) {
636 err = got_error_from_errno("strdup");
637 goto done;
639 s += slen + 1;
640 remain -= slen + 1;
643 (*commit)->logmsg = strndup(s, remain);
644 if ((*commit)->logmsg == NULL) {
645 err = got_error_from_errno("strndup");
646 goto done;
648 done:
649 if (err) {
650 got_object_commit_close(*commit);
651 *commit = NULL;
653 return err;
656 void
657 got_object_tree_close(struct got_tree_object *tree)
659 if (tree->refcnt > 0) {
660 tree->refcnt--;
661 if (tree->refcnt > 0)
662 return;
665 free(tree->entries);
666 free(tree);
669 static const struct got_error *
670 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
671 size_t *elen, char *buf,
672 size_t maxlen)
674 char *p, *space;
675 const struct got_error *err = NULL;
677 *name = NULL;
678 *elen = 0;
680 *pte = malloc(sizeof(**pte));
681 if (*pte == NULL)
682 return got_error_from_errno("malloc");
684 *elen = strnlen(buf, maxlen) + 1;
685 if (*elen > maxlen) {
686 free(*pte);
687 *pte = NULL;
688 return got_error(GOT_ERR_BAD_OBJ_DATA);
691 space = memchr(buf, ' ', *elen);
692 if (space == NULL || space <= buf) {
693 err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 free(*pte);
695 *pte = NULL;
696 return err;
698 (*pte)->mode = 0;
699 p = buf;
700 while (p < space) {
701 if (*p < '0' && *p > '7') {
702 err = got_error(GOT_ERR_BAD_OBJ_DATA);
703 goto done;
705 (*pte)->mode <<= 3;
706 (*pte)->mode |= *p - '0';
707 p++;
710 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
711 err = got_error(GOT_ERR_BAD_OBJ_DATA);
712 goto done;
714 *name = space + 1;
715 buf += *elen;
716 (*pte)->id = buf;
717 *elen += SHA1_DIGEST_LENGTH;
718 done:
719 if (err) {
720 free(*pte);
721 *pte = NULL;
723 return err;
726 const struct got_error *
727 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
728 uint8_t *buf, size_t len)
730 const struct got_error *err = NULL;
731 size_t remain = len;
733 *nentries = 0;
734 if (remain == 0)
735 return NULL; /* tree is empty */
737 while (remain > 0) {
738 struct got_parsed_tree_entry *pte;
739 struct got_pathlist_entry *new = NULL;
740 const char *name;
741 size_t elen;
743 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
744 if (err)
745 goto done;
746 err = got_pathlist_insert(&new, entries, name, pte);
747 if (err)
748 goto done;
749 if (new == NULL) {
750 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
751 goto done;
753 buf += elen;
754 remain -= elen;
755 (*nentries)++;
758 if (remain != 0) {
759 err = got_error(GOT_ERR_BAD_OBJ_DATA);
760 goto done;
762 done:
763 if (err) {
764 got_object_parsed_tree_entries_free(entries);
765 *nentries = 0;
767 return err;
770 void
771 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
773 struct got_pathlist_entry *pe;
775 TAILQ_FOREACH(pe, entries, entry) {
776 struct got_parsed_tree_entry *pte = pe->data;
777 free(pte);
779 got_pathlist_free(entries);
782 void
783 got_object_tag_close(struct got_tag_object *tag)
785 if (tag->refcnt > 0) {
786 tag->refcnt--;
787 if (tag->refcnt > 0)
788 return;
791 free(tag->tag);
792 free(tag->tagger);
793 free(tag->tagmsg);
794 free(tag);
797 const struct got_error *
798 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
800 const struct got_error *err = NULL;
801 size_t remain = len;
802 char *s = buf;
803 size_t label_len;
805 if (remain == 0)
806 return got_error(GOT_ERR_BAD_OBJ_DATA);
808 *tag = calloc(1, sizeof(**tag));
809 if (*tag == NULL)
810 return got_error_from_errno("calloc");
812 label_len = strlen(GOT_TAG_LABEL_OBJECT);
813 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
814 remain -= label_len;
815 if (remain < SHA1_DIGEST_STRING_LENGTH) {
816 err = got_error(GOT_ERR_BAD_OBJ_DATA);
817 goto done;
819 s += label_len;
820 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
821 err = got_error(GOT_ERR_BAD_OBJ_DATA);
822 goto done;
824 remain -= SHA1_DIGEST_STRING_LENGTH;
825 s += SHA1_DIGEST_STRING_LENGTH;
826 } else {
827 err = got_error(GOT_ERR_BAD_OBJ_DATA);
828 goto done;
831 if (remain <= 0) {
832 err = got_error(GOT_ERR_BAD_OBJ_DATA);
833 goto done;
836 label_len = strlen(GOT_TAG_LABEL_TYPE);
837 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
838 remain -= label_len;
839 if (remain <= 0) {
840 err = got_error(GOT_ERR_BAD_OBJ_DATA);
841 goto done;
843 s += label_len;
844 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
845 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
846 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
847 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
848 s += label_len;
849 remain -= label_len;
850 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
851 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
852 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
853 label_len = strlen(GOT_OBJ_LABEL_TREE);
854 s += label_len;
855 remain -= label_len;
856 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
857 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
858 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
859 label_len = strlen(GOT_OBJ_LABEL_BLOB);
860 s += label_len;
861 remain -= label_len;
862 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
863 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
864 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
865 label_len = strlen(GOT_OBJ_LABEL_TAG);
866 s += label_len;
867 remain -= label_len;
868 } else {
869 err = got_error(GOT_ERR_BAD_OBJ_DATA);
870 goto done;
873 if (remain <= 0 || *s != '\n') {
874 err = got_error(GOT_ERR_BAD_OBJ_DATA);
875 goto done;
877 s++;
878 remain--;
879 if (remain <= 0) {
880 err = got_error(GOT_ERR_BAD_OBJ_DATA);
881 goto done;
883 } else {
884 err = got_error(GOT_ERR_BAD_OBJ_DATA);
885 goto done;
888 label_len = strlen(GOT_TAG_LABEL_TAG);
889 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
890 char *p;
891 size_t slen;
892 remain -= label_len;
893 if (remain <= 0) {
894 err = got_error(GOT_ERR_BAD_OBJ_DATA);
895 goto done;
897 s += label_len;
898 p = memchr(s, '\n', remain);
899 if (p == NULL) {
900 err = got_error(GOT_ERR_BAD_OBJ_DATA);
901 goto done;
903 *p = '\0';
904 slen = strlen(s);
905 (*tag)->tag = strndup(s, slen);
906 if ((*tag)->tag == NULL) {
907 err = got_error_from_errno("strndup");
908 goto done;
910 s += slen + 1;
911 remain -= slen + 1;
912 if (remain <= 0) {
913 err = got_error(GOT_ERR_BAD_OBJ_DATA);
914 goto done;
916 } else {
917 err = got_error(GOT_ERR_BAD_OBJ_DATA);
918 goto done;
921 label_len = strlen(GOT_TAG_LABEL_TAGGER);
922 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
923 char *p;
924 size_t slen;
926 remain -= label_len;
927 if (remain <= 0) {
928 err = got_error(GOT_ERR_BAD_OBJ_DATA);
929 goto done;
931 s += label_len;
932 p = memchr(s, '\n', remain);
933 if (p == NULL) {
934 err = got_error(GOT_ERR_BAD_OBJ_DATA);
935 goto done;
937 *p = '\0';
938 slen = strlen(s);
939 err = parse_commit_time(&(*tag)->tagger_time,
940 &(*tag)->tagger_gmtoff, s);
941 if (err)
942 goto done;
943 (*tag)->tagger = strdup(s);
944 if ((*tag)->tagger == NULL) {
945 err = got_error_from_errno("strdup");
946 goto done;
948 s += slen + 1;
949 remain -= slen + 1;
950 if (remain < 0) {
951 err = got_error(GOT_ERR_BAD_OBJ_DATA);
952 goto done;
954 } else {
955 /* Some old tags in the Linux git repo have no tagger. */
956 (*tag)->tagger = strdup("");
957 if ((*tag)->tagger == NULL) {
958 err = got_error_from_errno("strdup");
959 goto done;
963 (*tag)->tagmsg = strndup(s, remain);
964 if ((*tag)->tagmsg == NULL) {
965 err = got_error_from_errno("strndup");
966 goto done;
968 done:
969 if (err) {
970 got_object_tag_close(*tag);
971 *tag = NULL;
973 return err;
976 const struct got_error *
977 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
979 const struct got_error *err = NULL;
980 static const size_t blocksize = 512;
981 size_t n, total, remain;
982 uint8_t *buf;
984 *outbuf = NULL;
985 *outlen = 0;
987 buf = malloc(blocksize);
988 if (buf == NULL)
989 return got_error_from_errno("malloc");
991 remain = blocksize;
992 total = 0;
993 for (;;) {
994 if (remain == 0) {
995 uint8_t *newbuf;
996 newbuf = reallocarray(buf, 1, total + blocksize);
997 if (newbuf == NULL) {
998 err = got_error_from_errno("reallocarray");
999 goto done;
1001 buf = newbuf;
1002 remain += blocksize;
1004 n = fread(buf + total, 1, remain, f);
1005 if (n == 0) {
1006 if (ferror(f)) {
1007 err = got_ferror(f, GOT_ERR_IO);
1008 goto done;
1010 break; /* EOF */
1012 remain -= n;
1013 total += n;
1016 done:
1017 if (err == NULL) {
1018 *outbuf = buf;
1019 *outlen = total;
1020 } else
1021 free(buf);
1022 return err;