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/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <sha1.h>
30 #include <zlib.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <imsg.h>
34 #include <time.h>
35 #include <unistd.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
41 #include "got_path.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 struct got_object_id *
58 got_object_id_dup(struct got_object_id *id1)
59 {
60 struct got_object_id *id2;
62 id2 = malloc(sizeof(*id2));
63 if (id2 == NULL)
64 return NULL;
65 memcpy(id2, id1, sizeof(*id2));
66 return id2;
67 }
69 int
70 got_object_id_cmp(const struct got_object_id *id1,
71 const struct got_object_id *id2)
72 {
73 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
74 }
76 const struct got_error *
77 got_object_qid_alloc_partial(struct got_object_qid **qid)
78 {
79 const struct got_error *err = NULL;
81 *qid = malloc(sizeof(**qid));
82 if (*qid == NULL)
83 return got_error_from_errno("malloc");
85 (*qid)->id = malloc(sizeof(*((*qid)->id)));
86 if ((*qid)->id == NULL) {
87 err = got_error_from_errno("malloc");
88 got_object_qid_free(*qid);
89 *qid = NULL;
90 return err;
91 }
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 (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
126 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
127 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
128 free(delta);
131 free(obj);
134 void
135 got_object_qid_free(struct got_object_qid *qid)
137 free(qid->id);
138 free(qid);
141 void
142 got_object_id_queue_free(struct got_object_id_queue *ids)
144 struct got_object_qid *qid;
146 while (!SIMPLEQ_EMPTY(ids)) {
147 qid = SIMPLEQ_FIRST(ids);
148 SIMPLEQ_REMOVE_HEAD(ids, entry);
149 got_object_qid_free(qid);
153 const struct got_error *
154 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
156 const char *obj_labels[] = {
157 GOT_OBJ_LABEL_COMMIT,
158 GOT_OBJ_LABEL_TREE,
159 GOT_OBJ_LABEL_BLOB,
160 GOT_OBJ_LABEL_TAG,
161 };
162 const int obj_types[] = {
163 GOT_OBJ_TYPE_COMMIT,
164 GOT_OBJ_TYPE_TREE,
165 GOT_OBJ_TYPE_BLOB,
166 GOT_OBJ_TYPE_TAG,
167 };
168 int type = 0;
169 size_t size = 0, hdrlen = 0;
170 size_t i;
172 *obj = NULL;
174 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
175 if (hdrlen > len)
176 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 for (i = 0; i < nitems(obj_labels); i++) {
179 const char *label = obj_labels[i];
180 size_t label_len = strlen(label);
181 const char *errstr;
183 if (strncmp(buf, label, label_len) != 0)
184 continue;
186 type = obj_types[i];
187 if (len <= label_len)
188 return got_error(GOT_ERR_BAD_OBJ_HDR);
189 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
190 if (errstr != NULL)
191 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 break;
195 if (type == 0)
196 return got_error(GOT_ERR_BAD_OBJ_HDR);
198 *obj = calloc(1, sizeof(**obj));
199 if (*obj == NULL)
200 return got_error_from_errno("calloc");
201 (*obj)->type = type;
202 (*obj)->hdrlen = hdrlen;
203 (*obj)->size = size;
204 return NULL;
207 const struct got_error *
208 got_object_read_header(struct got_object **obj, int fd)
210 const struct got_error *err;
211 struct got_inflate_buf zb;
212 char *buf;
213 const size_t zbsize = 64;
214 size_t outlen, totlen;
215 int nbuf = 1;
217 *obj = NULL;
219 buf = malloc(zbsize);
220 if (buf == NULL)
221 return got_error_from_errno("malloc");
223 err = got_inflate_init(&zb, buf, zbsize, NULL);
224 if (err)
225 return err;
227 totlen = 0;
228 do {
229 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
230 if (err)
231 goto done;
232 if (outlen == 0)
233 break;
234 totlen += outlen;
235 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
236 char *newbuf;
237 nbuf++;
238 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
239 if (newbuf == NULL) {
240 err = got_error_from_errno("recallocarray");
241 goto done;
243 buf = newbuf;
244 zb.outbuf = newbuf + totlen;
245 zb.outlen = (nbuf * zbsize) - totlen;
247 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
249 err = got_object_parse_header(obj, buf, totlen);
250 done:
251 free(buf);
252 got_inflate_end(&zb);
253 return err;
256 struct got_commit_object *
257 got_object_commit_alloc_partial(void)
259 struct got_commit_object *commit;
261 commit = calloc(1, sizeof(*commit));
262 if (commit == NULL)
263 return NULL;
264 commit->tree_id = malloc(sizeof(*commit->tree_id));
265 if (commit->tree_id == NULL) {
266 free(commit);
267 return NULL;
270 SIMPLEQ_INIT(&commit->parent_ids);
272 return commit;
275 const struct got_error *
276 got_object_commit_add_parent(struct got_commit_object *commit,
277 const char *id_str)
279 const struct got_error *err = NULL;
280 struct got_object_qid *qid;
282 err = got_object_qid_alloc_partial(&qid);
283 if (err)
284 return err;
286 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
287 err = got_error(GOT_ERR_BAD_OBJ_DATA);
288 got_object_qid_free(qid);
289 return err;
292 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
293 commit->nparents++;
295 return NULL;
298 static const struct got_error *
299 parse_gmtoff(time_t *gmtoff, const char *tzstr)
301 int sign = 1;
302 const char *p = tzstr;
303 time_t h, m;
305 *gmtoff = 0;
307 if (*p == '-')
308 sign = -1;
309 else if (*p != '+')
310 return got_error(GOT_ERR_BAD_OBJ_DATA);
311 p++;
312 if (!isdigit(*p) && !isdigit(*(p + 1)))
313 return got_error(GOT_ERR_BAD_OBJ_DATA);
314 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
316 p += 2;
317 if (!isdigit(*p) && !isdigit(*(p + 1)))
318 return got_error(GOT_ERR_BAD_OBJ_DATA);
319 m = ((*p - '0') * 10) + (*(p + 1) - '0');
321 *gmtoff = (h * 60 * 60 + m * 60) * sign;
322 return NULL;
325 static const struct got_error *
326 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
328 const struct got_error *err = NULL;
329 const char *errstr;
330 char *space, *tzstr;
332 /* Parse and strip off trailing timezone indicator string. */
333 space = strrchr(committer, ' ');
334 if (space == NULL)
335 return got_error(GOT_ERR_BAD_OBJ_DATA);
336 tzstr = strdup(space + 1);
337 if (tzstr == NULL)
338 return got_error_from_errno("strdup");
339 err = parse_gmtoff(gmtoff, tzstr);
340 free(tzstr);
341 if (err) {
342 if (err->code != GOT_ERR_BAD_OBJ_DATA)
343 return err;
344 /* Old versions of Git omitted the timestamp. */
345 *time = 0;
346 *gmtoff = 0;
347 return NULL;
349 *space = '\0';
351 /* Timestamp is separated from committer name + email by space. */
352 space = strrchr(committer, ' ');
353 if (space == NULL)
354 return got_error(GOT_ERR_BAD_OBJ_DATA);
356 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
357 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
358 if (errstr)
359 return got_error(GOT_ERR_BAD_OBJ_DATA);
361 /* Strip off parsed time information, leaving just author and email. */
362 *space = '\0';
364 return NULL;
367 void
368 got_object_commit_close(struct got_commit_object *commit)
370 if (commit->refcnt > 0) {
371 commit->refcnt--;
372 if (commit->refcnt > 0)
373 return;
376 got_object_id_queue_free(&commit->parent_ids);
377 free(commit->tree_id);
378 free(commit->author);
379 free(commit->committer);
380 free(commit->logmsg);
381 free(commit);
384 struct got_object_id *
385 got_object_commit_get_tree_id(struct got_commit_object *commit)
387 return commit->tree_id;
390 int
391 got_object_commit_get_nparents(struct got_commit_object *commit)
393 return commit->nparents;
396 const struct got_object_id_queue *
397 got_object_commit_get_parent_ids(struct got_commit_object *commit)
399 return &commit->parent_ids;
402 const char *
403 got_object_commit_get_author(struct got_commit_object *commit)
405 return commit->author;
408 time_t
409 got_object_commit_get_author_time(struct got_commit_object *commit)
411 return commit->author_time;
414 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
416 return commit->author_gmtoff;
419 const char *
420 got_object_commit_get_committer(struct got_commit_object *commit)
422 return commit->committer;
425 time_t
426 got_object_commit_get_committer_time(struct got_commit_object *commit)
428 return commit->committer_time;
431 time_t
432 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
434 return commit->committer_gmtoff;
437 const struct got_error *
438 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
440 const struct got_error *err = NULL;
441 char *msg0, *msg, *line, *s;
442 size_t len;
443 int headers = 1;
445 *logmsg = NULL;
447 msg0 = strdup(commit->logmsg);
448 if (msg0 == NULL)
449 return got_error_from_errno("strdup");
451 /* Copy log message line by line to strip out unusual headers... */
452 msg = msg0;
453 do {
454 if ((line = strsep(&msg, "\n")) == NULL)
455 break;
457 if (headers == 1) {
458 if (line[0] != '\0' &&
459 strncmp(line, GOT_COMMIT_LABEL_TREE,
460 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
461 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
462 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
463 strncmp(line, GOT_COMMIT_LABEL_PARENT,
464 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
465 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
466 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
467 continue;
469 if (line[0] == '\0')
470 headers = 0;
473 if (asprintf(&s, "%s%s\n",
474 *logmsg ? *logmsg : "", line) == -1) {
475 err = got_error_from_errno("asprintf");
476 goto done;
478 free(*logmsg);
479 *logmsg = s;
481 } while (line);
483 if (*logmsg == NULL) {
484 /* log message does not contain \n */
485 *logmsg = strdup(commit->logmsg);
486 if (*logmsg == NULL) {
487 err = got_error_from_errno("strdup");
488 goto done;
492 /* Trim redundant trailing whitespace. */
493 len = strlen(*logmsg);
494 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
495 isspace((unsigned char)(*logmsg)[len - 1])) {
496 (*logmsg)[len - 1] = '\0';
497 len--;
499 done:
500 free(msg0);
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;