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 <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"
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 void
137 got_object_qid_free(struct got_object_qid *qid)
139 free(qid->id);
140 free(qid);
143 void
144 got_object_id_queue_free(struct got_object_id_queue *ids)
146 struct got_object_qid *qid;
148 while (!STAILQ_EMPTY(ids)) {
149 qid = STAILQ_FIRST(ids);
150 STAILQ_REMOVE_HEAD(ids, entry);
151 got_object_qid_free(qid);
155 const struct got_error *
156 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
158 const char *obj_labels[] = {
159 GOT_OBJ_LABEL_COMMIT,
160 GOT_OBJ_LABEL_TREE,
161 GOT_OBJ_LABEL_BLOB,
162 GOT_OBJ_LABEL_TAG,
163 };
164 const int obj_types[] = {
165 GOT_OBJ_TYPE_COMMIT,
166 GOT_OBJ_TYPE_TREE,
167 GOT_OBJ_TYPE_BLOB,
168 GOT_OBJ_TYPE_TAG,
169 };
170 int type = 0;
171 size_t size = 0, hdrlen = 0;
172 size_t i;
174 *obj = NULL;
176 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
177 if (hdrlen > len)
178 return got_error(GOT_ERR_BAD_OBJ_HDR);
180 for (i = 0; i < nitems(obj_labels); i++) {
181 const char *label = obj_labels[i];
182 size_t label_len = strlen(label);
183 const char *errstr;
185 if (strncmp(buf, label, label_len) != 0)
186 continue;
188 type = obj_types[i];
189 if (len <= label_len)
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
191 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
192 if (errstr != NULL)
193 return got_error(GOT_ERR_BAD_OBJ_HDR);
194 break;
197 if (type == 0)
198 return got_error(GOT_ERR_BAD_OBJ_HDR);
200 *obj = calloc(1, sizeof(**obj));
201 if (*obj == NULL)
202 return got_error_from_errno("calloc");
203 (*obj)->type = type;
204 (*obj)->hdrlen = hdrlen;
205 (*obj)->size = size;
206 return NULL;
209 const struct got_error *
210 got_object_read_header(struct got_object **obj, int fd)
212 const struct got_error *err;
213 struct got_inflate_buf zb;
214 uint8_t *buf;
215 const size_t zbsize = 64;
216 size_t outlen, totlen;
217 int nbuf = 1;
219 *obj = NULL;
221 buf = malloc(zbsize);
222 if (buf == NULL)
223 return got_error_from_errno("malloc");
225 err = got_inflate_init(&zb, buf, zbsize, NULL);
226 if (err)
227 return err;
229 totlen = 0;
230 do {
231 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
232 if (err)
233 goto done;
234 if (outlen == 0)
235 break;
236 totlen += outlen;
237 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
238 uint8_t *newbuf;
239 nbuf++;
240 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
241 if (newbuf == NULL) {
242 err = got_error_from_errno("recallocarray");
243 goto done;
245 buf = newbuf;
246 zb.outbuf = newbuf + totlen;
247 zb.outlen = (nbuf * zbsize) - totlen;
249 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
251 err = got_object_parse_header(obj, buf, totlen);
252 done:
253 free(buf);
254 got_inflate_end(&zb);
255 return err;
258 struct got_commit_object *
259 got_object_commit_alloc_partial(void)
261 struct got_commit_object *commit;
263 commit = calloc(1, sizeof(*commit));
264 if (commit == NULL)
265 return NULL;
266 commit->tree_id = malloc(sizeof(*commit->tree_id));
267 if (commit->tree_id == NULL) {
268 free(commit);
269 return NULL;
272 STAILQ_INIT(&commit->parent_ids);
274 return commit;
277 const struct got_error *
278 got_object_commit_add_parent(struct got_commit_object *commit,
279 const char *id_str)
281 const struct got_error *err = NULL;
282 struct got_object_qid *qid;
284 err = got_object_qid_alloc_partial(&qid);
285 if (err)
286 return err;
288 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
289 err = got_error(GOT_ERR_BAD_OBJ_DATA);
290 got_object_qid_free(qid);
291 return err;
294 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
295 commit->nparents++;
297 return NULL;
300 static const struct got_error *
301 parse_gmtoff(time_t *gmtoff, const char *tzstr)
303 int sign = 1;
304 const char *p = tzstr;
305 time_t h, m;
307 *gmtoff = 0;
309 if (*p == '-')
310 sign = -1;
311 else if (*p != '+')
312 return got_error(GOT_ERR_BAD_OBJ_DATA);
313 p++;
314 if (!isdigit(*p) && !isdigit(*(p + 1)))
315 return got_error(GOT_ERR_BAD_OBJ_DATA);
316 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
318 p += 2;
319 if (!isdigit(*p) && !isdigit(*(p + 1)))
320 return got_error(GOT_ERR_BAD_OBJ_DATA);
321 m = ((*p - '0') * 10) + (*(p + 1) - '0');
323 *gmtoff = (h * 60 * 60 + m * 60) * sign;
324 return NULL;
327 static const struct got_error *
328 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
330 const struct got_error *err = NULL;
331 const char *errstr;
332 char *space, *tzstr;
334 /* Parse and strip off trailing timezone indicator string. */
335 space = strrchr(committer, ' ');
336 if (space == NULL)
337 return got_error(GOT_ERR_BAD_OBJ_DATA);
338 tzstr = strdup(space + 1);
339 if (tzstr == NULL)
340 return got_error_from_errno("strdup");
341 err = parse_gmtoff(gmtoff, tzstr);
342 free(tzstr);
343 if (err) {
344 if (err->code != GOT_ERR_BAD_OBJ_DATA)
345 return err;
346 /* Old versions of Git omitted the timestamp. */
347 *time = 0;
348 *gmtoff = 0;
349 return NULL;
351 *space = '\0';
353 /* Timestamp is separated from committer name + email by space. */
354 space = strrchr(committer, ' ');
355 if (space == NULL)
356 return got_error(GOT_ERR_BAD_OBJ_DATA);
358 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
359 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
360 if (errstr)
361 return got_error(GOT_ERR_BAD_OBJ_DATA);
363 /* Strip off parsed time information, leaving just author and email. */
364 *space = '\0';
366 return NULL;
369 void
370 got_object_commit_close(struct got_commit_object *commit)
372 if (commit->refcnt > 0) {
373 commit->refcnt--;
374 if (commit->refcnt > 0)
375 return;
378 got_object_id_queue_free(&commit->parent_ids);
379 free(commit->tree_id);
380 free(commit->author);
381 free(commit->committer);
382 free(commit->logmsg);
383 free(commit);
386 struct got_object_id *
387 got_object_commit_get_tree_id(struct got_commit_object *commit)
389 return commit->tree_id;
392 int
393 got_object_commit_get_nparents(struct got_commit_object *commit)
395 return commit->nparents;
398 const struct got_object_id_queue *
399 got_object_commit_get_parent_ids(struct got_commit_object *commit)
401 return &commit->parent_ids;
404 const char *
405 got_object_commit_get_author(struct got_commit_object *commit)
407 return commit->author;
410 time_t
411 got_object_commit_get_author_time(struct got_commit_object *commit)
413 return commit->author_time;
416 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
418 return commit->author_gmtoff;
421 const char *
422 got_object_commit_get_committer(struct got_commit_object *commit)
424 return commit->committer;
427 time_t
428 got_object_commit_get_committer_time(struct got_commit_object *commit)
430 return commit->committer_time;
433 time_t
434 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
436 return commit->committer_gmtoff;
439 const struct got_error *
440 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
442 const struct got_error *err = NULL;
443 const char *src;
444 char *dst;
445 size_t len;
447 len = strlen(commit->logmsg);
448 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
449 if (*logmsg == NULL)
450 return got_error_from_errno("malloc");
452 /*
453 * Strip out unusual headers. Headers are separated from the commit
454 * message body by a single empty line.
455 */
456 src = commit->logmsg;
457 dst = *logmsg;
458 while (*src != '\0' && *src != '\n') {
459 int copy_header = 1, eol = 0;
460 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
461 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
462 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
463 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
464 strncmp(src, GOT_COMMIT_LABEL_PARENT,
465 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
466 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
467 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
468 copy_header = 0;
470 while (*src != '\0' && !eol) {
471 if (copy_header) {
472 *dst = *src;
473 dst++;
475 if (*src == '\n')
476 eol = 1;
477 src++;
480 *dst = '\0';
482 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
483 err = got_error(GOT_ERR_NO_SPACE);
484 goto done;
487 /* Trim redundant trailing whitespace. */
488 len = strlen(*logmsg);
489 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
490 isspace((unsigned char)(*logmsg)[len - 1])) {
491 (*logmsg)[len - 1] = '\0';
492 len--;
495 /* Append a trailing newline if missing. */
496 if (len > 0 && (*logmsg)[len - 1] != '\n') {
497 (*logmsg)[len] = '\n';
498 (*logmsg)[len + 1] = '\0';
500 done:
501 if (err) {
502 free(*logmsg);
503 *logmsg = NULL;
505 return err;
508 const char *
509 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
511 return commit->logmsg;
514 const struct got_error *
515 got_object_parse_commit(struct got_commit_object **commit, char *buf,
516 size_t len)
518 const struct got_error *err = NULL;
519 char *s = buf;
520 size_t label_len;
521 ssize_t remain = (ssize_t)len;
523 if (remain == 0)
524 return got_error(GOT_ERR_BAD_OBJ_DATA);
526 *commit = got_object_commit_alloc_partial();
527 if (*commit == NULL)
528 return got_error_from_errno("got_object_commit_alloc_partial");
530 label_len = strlen(GOT_COMMIT_LABEL_TREE);
531 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
532 remain -= label_len;
533 if (remain < SHA1_DIGEST_STRING_LENGTH) {
534 err = got_error(GOT_ERR_BAD_OBJ_DATA);
535 goto done;
537 s += label_len;
538 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
539 err = got_error(GOT_ERR_BAD_OBJ_DATA);
540 goto done;
542 remain -= SHA1_DIGEST_STRING_LENGTH;
543 s += SHA1_DIGEST_STRING_LENGTH;
544 } else {
545 err = got_error(GOT_ERR_BAD_OBJ_DATA);
546 goto done;
549 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
550 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, 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 err = got_object_commit_add_parent(*commit, s);
558 if (err)
559 goto done;
561 remain -= SHA1_DIGEST_STRING_LENGTH;
562 s += SHA1_DIGEST_STRING_LENGTH;
565 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
566 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
567 char *p;
568 size_t slen;
570 remain -= label_len;
571 if (remain <= 0) {
572 err = got_error(GOT_ERR_BAD_OBJ_DATA);
573 goto done;
575 s += label_len;
576 p = memchr(s, '\n', remain);
577 if (p == NULL) {
578 err = got_error(GOT_ERR_BAD_OBJ_DATA);
579 goto done;
581 *p = '\0';
582 slen = strlen(s);
583 err = parse_commit_time(&(*commit)->author_time,
584 &(*commit)->author_gmtoff, s);
585 if (err)
586 goto done;
587 (*commit)->author = strdup(s);
588 if ((*commit)->author == NULL) {
589 err = got_error_from_errno("strdup");
590 goto done;
592 s += slen + 1;
593 remain -= slen + 1;
596 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
597 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
598 char *p;
599 size_t slen;
601 remain -= label_len;
602 if (remain <= 0) {
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 goto done;
606 s += label_len;
607 p = memchr(s, '\n', remain);
608 if (p == NULL) {
609 err = got_error(GOT_ERR_BAD_OBJ_DATA);
610 goto done;
612 *p = '\0';
613 slen = strlen(s);
614 err = parse_commit_time(&(*commit)->committer_time,
615 &(*commit)->committer_gmtoff, s);
616 if (err)
617 goto done;
618 (*commit)->committer = strdup(s);
619 if ((*commit)->committer == NULL) {
620 err = got_error_from_errno("strdup");
621 goto done;
623 s += slen + 1;
624 remain -= slen + 1;
627 (*commit)->logmsg = strndup(s, remain);
628 if ((*commit)->logmsg == NULL) {
629 err = got_error_from_errno("strndup");
630 goto done;
632 done:
633 if (err) {
634 got_object_commit_close(*commit);
635 *commit = NULL;
637 return err;
640 void
641 got_object_tree_close(struct got_tree_object *tree)
643 if (tree->refcnt > 0) {
644 tree->refcnt--;
645 if (tree->refcnt > 0)
646 return;
649 free(tree->entries);
650 free(tree);
653 static const struct got_error *
654 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
655 size_t *elen, char *buf,
656 size_t maxlen)
658 char *p, *space;
659 const struct got_error *err = NULL;
661 *name = NULL;
662 *elen = 0;
664 *pte = malloc(sizeof(**pte));
665 if (*pte == NULL)
666 return got_error_from_errno("malloc");
668 *elen = strnlen(buf, maxlen) + 1;
669 if (*elen > maxlen) {
670 free(*pte);
671 *pte = NULL;
672 return got_error(GOT_ERR_BAD_OBJ_DATA);
675 space = memchr(buf, ' ', *elen);
676 if (space == NULL || space <= buf) {
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 free(*pte);
679 *pte = NULL;
680 return err;
682 (*pte)->mode = 0;
683 p = buf;
684 while (p < space) {
685 if (*p < '0' && *p > '7') {
686 err = got_error(GOT_ERR_BAD_OBJ_DATA);
687 goto done;
689 (*pte)->mode <<= 3;
690 (*pte)->mode |= *p - '0';
691 p++;
694 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
695 err = got_error(GOT_ERR_BAD_OBJ_DATA);
696 goto done;
698 *name = space + 1;
699 buf += *elen;
700 (*pte)->id = buf;
701 *elen += SHA1_DIGEST_LENGTH;
702 done:
703 if (err) {
704 free(*pte);
705 *pte = NULL;
707 return err;
710 const struct got_error *
711 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
712 uint8_t *buf, size_t len)
714 const struct got_error *err = NULL;
715 size_t remain = len;
717 *nentries = 0;
718 if (remain == 0)
719 return NULL; /* tree is empty */
721 while (remain > 0) {
722 struct got_parsed_tree_entry *pte;
723 struct got_pathlist_entry *new = NULL;
724 const char *name;
725 size_t elen;
727 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
728 if (err)
729 goto done;
730 err = got_pathlist_insert(&new, entries, name, pte);
731 if (err)
732 goto done;
733 if (new == NULL) {
734 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
735 goto done;
737 buf += elen;
738 remain -= elen;
739 (*nentries)++;
742 if (remain != 0) {
743 err = got_error(GOT_ERR_BAD_OBJ_DATA);
744 goto done;
746 done:
747 if (err) {
748 got_object_parsed_tree_entries_free(entries);
749 *nentries = 0;
751 return err;
754 void
755 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
757 struct got_pathlist_entry *pe;
759 TAILQ_FOREACH(pe, entries, entry) {
760 struct got_parsed_tree_entry *pte = pe->data;
761 free(pte);
763 got_pathlist_free(entries);
766 void
767 got_object_tag_close(struct got_tag_object *tag)
769 if (tag->refcnt > 0) {
770 tag->refcnt--;
771 if (tag->refcnt > 0)
772 return;
775 free(tag->tag);
776 free(tag->tagger);
777 free(tag->tagmsg);
778 free(tag);
781 const struct got_error *
782 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
784 const struct got_error *err = NULL;
785 size_t remain = len;
786 char *s = buf;
787 size_t label_len;
789 if (remain == 0)
790 return got_error(GOT_ERR_BAD_OBJ_DATA);
792 *tag = calloc(1, sizeof(**tag));
793 if (*tag == NULL)
794 return got_error_from_errno("calloc");
796 label_len = strlen(GOT_TAG_LABEL_OBJECT);
797 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
798 remain -= label_len;
799 if (remain < SHA1_DIGEST_STRING_LENGTH) {
800 err = got_error(GOT_ERR_BAD_OBJ_DATA);
801 goto done;
803 s += label_len;
804 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
805 err = got_error(GOT_ERR_BAD_OBJ_DATA);
806 goto done;
808 remain -= SHA1_DIGEST_STRING_LENGTH;
809 s += SHA1_DIGEST_STRING_LENGTH;
810 } else {
811 err = got_error(GOT_ERR_BAD_OBJ_DATA);
812 goto done;
815 if (remain <= 0) {
816 err = got_error(GOT_ERR_BAD_OBJ_DATA);
817 goto done;
820 label_len = strlen(GOT_TAG_LABEL_TYPE);
821 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
822 remain -= label_len;
823 if (remain <= 0) {
824 err = got_error(GOT_ERR_BAD_OBJ_DATA);
825 goto done;
827 s += label_len;
828 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
829 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
830 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
831 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
832 s += label_len;
833 remain -= label_len;
834 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
835 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
836 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
837 label_len = strlen(GOT_OBJ_LABEL_TREE);
838 s += label_len;
839 remain -= label_len;
840 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
841 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
842 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
843 label_len = strlen(GOT_OBJ_LABEL_BLOB);
844 s += label_len;
845 remain -= label_len;
846 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
847 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
848 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
849 label_len = strlen(GOT_OBJ_LABEL_TAG);
850 s += label_len;
851 remain -= label_len;
852 } else {
853 err = got_error(GOT_ERR_BAD_OBJ_DATA);
854 goto done;
857 if (remain <= 0 || *s != '\n') {
858 err = got_error(GOT_ERR_BAD_OBJ_DATA);
859 goto done;
861 s++;
862 remain--;
863 if (remain <= 0) {
864 err = got_error(GOT_ERR_BAD_OBJ_DATA);
865 goto done;
867 } else {
868 err = got_error(GOT_ERR_BAD_OBJ_DATA);
869 goto done;
872 label_len = strlen(GOT_TAG_LABEL_TAG);
873 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
874 char *p;
875 size_t slen;
876 remain -= label_len;
877 if (remain <= 0) {
878 err = got_error(GOT_ERR_BAD_OBJ_DATA);
879 goto done;
881 s += label_len;
882 p = memchr(s, '\n', remain);
883 if (p == NULL) {
884 err = got_error(GOT_ERR_BAD_OBJ_DATA);
885 goto done;
887 *p = '\0';
888 slen = strlen(s);
889 (*tag)->tag = strndup(s, slen);
890 if ((*tag)->tag == NULL) {
891 err = got_error_from_errno("strndup");
892 goto done;
894 s += slen + 1;
895 remain -= slen + 1;
896 if (remain <= 0) {
897 err = got_error(GOT_ERR_BAD_OBJ_DATA);
898 goto done;
900 } else {
901 err = got_error(GOT_ERR_BAD_OBJ_DATA);
902 goto done;
905 label_len = strlen(GOT_TAG_LABEL_TAGGER);
906 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
907 char *p;
908 size_t slen;
910 remain -= label_len;
911 if (remain <= 0) {
912 err = got_error(GOT_ERR_BAD_OBJ_DATA);
913 goto done;
915 s += label_len;
916 p = memchr(s, '\n', remain);
917 if (p == NULL) {
918 err = got_error(GOT_ERR_BAD_OBJ_DATA);
919 goto done;
921 *p = '\0';
922 slen = strlen(s);
923 err = parse_commit_time(&(*tag)->tagger_time,
924 &(*tag)->tagger_gmtoff, s);
925 if (err)
926 goto done;
927 (*tag)->tagger = strdup(s);
928 if ((*tag)->tagger == NULL) {
929 err = got_error_from_errno("strdup");
930 goto done;
932 s += slen + 1;
933 remain -= slen + 1;
934 if (remain < 0) {
935 err = got_error(GOT_ERR_BAD_OBJ_DATA);
936 goto done;
938 } else {
939 /* Some old tags in the Linux git repo have no tagger. */
940 (*tag)->tagger = strdup("");
941 if ((*tag)->tagger == NULL) {
942 err = got_error_from_errno("strdup");
943 goto done;
947 (*tag)->tagmsg = strndup(s, remain);
948 if ((*tag)->tagmsg == NULL) {
949 err = got_error_from_errno("strndup");
950 goto done;
952 done:
953 if (err) {
954 got_object_tag_close(*tag);
955 *tag = NULL;
957 return err;
960 const struct got_error *
961 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
963 const struct got_error *err = NULL;
964 static const size_t blocksize = 512;
965 size_t n, total, remain;
966 uint8_t *buf;
968 *outbuf = NULL;
969 *outlen = 0;
971 buf = malloc(blocksize);
972 if (buf == NULL)
973 return got_error_from_errno("malloc");
975 remain = blocksize;
976 total = 0;
977 for (;;) {
978 if (remain == 0) {
979 uint8_t *newbuf;
980 newbuf = reallocarray(buf, 1, total + blocksize);
981 if (newbuf == NULL) {
982 err = got_error_from_errno("reallocarray");
983 goto done;
985 buf = newbuf;
986 remain += blocksize;
988 n = fread(buf + total, 1, remain, f);
989 if (n == 0) {
990 if (ferror(f)) {
991 err = got_ferror(f, GOT_ERR_IO);
992 goto done;
994 break; /* EOF */
996 remain -= n;
997 total += n;
998 };
1000 done:
1001 if (err == NULL) {
1002 *outbuf = buf;
1003 *outlen = total;
1004 } else
1005 free(buf);
1006 return err;