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 return err;
343 *space = '\0';
345 /* Timestamp is separated from committer name + email by space. */
346 space = strrchr(committer, ' ');
347 if (space == NULL)
348 return got_error(GOT_ERR_BAD_OBJ_DATA);
350 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
351 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
352 if (errstr)
353 return got_error(GOT_ERR_BAD_OBJ_DATA);
355 /* Strip off parsed time information, leaving just author and email. */
356 *space = '\0';
358 return NULL;
361 void
362 got_object_commit_close(struct got_commit_object *commit)
364 if (commit->refcnt > 0) {
365 commit->refcnt--;
366 if (commit->refcnt > 0)
367 return;
370 got_object_id_queue_free(&commit->parent_ids);
371 free(commit->tree_id);
372 free(commit->author);
373 free(commit->committer);
374 free(commit->logmsg);
375 free(commit);
378 struct got_object_id *
379 got_object_commit_get_tree_id(struct got_commit_object *commit)
381 return commit->tree_id;
384 int
385 got_object_commit_get_nparents(struct got_commit_object *commit)
387 return commit->nparents;
390 const struct got_object_id_queue *
391 got_object_commit_get_parent_ids(struct got_commit_object *commit)
393 return &commit->parent_ids;
396 const char *
397 got_object_commit_get_author(struct got_commit_object *commit)
399 return commit->author;
402 time_t
403 got_object_commit_get_author_time(struct got_commit_object *commit)
405 return commit->author_time;
408 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
410 return commit->author_gmtoff;
413 const char *
414 got_object_commit_get_committer(struct got_commit_object *commit)
416 return commit->committer;
419 time_t
420 got_object_commit_get_committer_time(struct got_commit_object *commit)
422 return commit->committer_time;
425 time_t
426 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
428 return commit->committer_gmtoff;
431 const struct got_error *
432 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
434 const struct got_error *err = NULL;
435 char *msg0, *msg, *line, *s;
436 size_t len;
437 int headers = 1;
439 *logmsg = NULL;
441 msg0 = strdup(commit->logmsg);
442 if (msg0 == NULL)
443 return got_error_from_errno("strdup");
445 /* Copy log message line by line to strip out unusual headers... */
446 msg = msg0;
447 do {
448 if ((line = strsep(&msg, "\n")) == NULL)
449 break;
451 if (headers == 1) {
452 if (line[0] != '\0' &&
453 strncmp(line, GOT_COMMIT_LABEL_TREE,
454 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
455 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
456 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
457 strncmp(line, GOT_COMMIT_LABEL_PARENT,
458 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
459 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
460 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
461 continue;
463 if (line[0] == '\0')
464 headers = 0;
467 if (asprintf(&s, "%s%s\n",
468 *logmsg ? *logmsg : "", line) == -1) {
469 err = got_error_from_errno("asprintf");
470 goto done;
472 free(*logmsg);
473 *logmsg = s;
475 } while (line);
477 if (*logmsg == NULL) {
478 /* log message does not contain \n */
479 *logmsg = strdup(commit->logmsg);
480 if (*logmsg == NULL) {
481 err = got_error_from_errno("strdup");
482 goto done;
486 /* Trim redundant trailing whitespace. */
487 len = strlen(*logmsg);
488 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
489 isspace((unsigned char)(*logmsg)[len - 1])) {
490 (*logmsg)[len - 1] = '\0';
491 len--;
493 done:
494 free(msg0);
495 if (err) {
496 free(*logmsg);
497 *logmsg = NULL;
499 return err;
502 const char *
503 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
505 return commit->logmsg;
508 const struct got_error *
509 got_object_parse_commit(struct got_commit_object **commit, char *buf,
510 size_t len)
512 const struct got_error *err = NULL;
513 char *s = buf;
514 size_t label_len;
515 ssize_t remain = (ssize_t)len;
517 if (remain == 0)
518 return got_error(GOT_ERR_BAD_OBJ_DATA);
520 *commit = got_object_commit_alloc_partial();
521 if (*commit == NULL)
522 return got_error_from_errno("got_object_commit_alloc_partial");
524 label_len = strlen(GOT_COMMIT_LABEL_TREE);
525 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
526 remain -= label_len;
527 if (remain < SHA1_DIGEST_STRING_LENGTH) {
528 err = got_error(GOT_ERR_BAD_OBJ_DATA);
529 goto done;
531 s += label_len;
532 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
533 err = got_error(GOT_ERR_BAD_OBJ_DATA);
534 goto done;
536 remain -= SHA1_DIGEST_STRING_LENGTH;
537 s += SHA1_DIGEST_STRING_LENGTH;
538 } else {
539 err = got_error(GOT_ERR_BAD_OBJ_DATA);
540 goto done;
543 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
544 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
545 remain -= label_len;
546 if (remain < SHA1_DIGEST_STRING_LENGTH) {
547 err = got_error(GOT_ERR_BAD_OBJ_DATA);
548 goto done;
550 s += label_len;
551 err = got_object_commit_add_parent(*commit, s);
552 if (err)
553 goto done;
555 remain -= SHA1_DIGEST_STRING_LENGTH;
556 s += SHA1_DIGEST_STRING_LENGTH;
559 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
560 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
561 char *p;
562 size_t slen;
564 remain -= label_len;
565 if (remain <= 0) {
566 err = got_error(GOT_ERR_BAD_OBJ_DATA);
567 goto done;
569 s += label_len;
570 p = memchr(s, '\n', remain);
571 if (p == NULL) {
572 err = got_error(GOT_ERR_BAD_OBJ_DATA);
573 goto done;
575 *p = '\0';
576 slen = strlen(s);
577 err = parse_commit_time(&(*commit)->author_time,
578 &(*commit)->author_gmtoff, s);
579 if (err)
580 goto done;
581 (*commit)->author = strdup(s);
582 if ((*commit)->author == NULL) {
583 err = got_error_from_errno("strdup");
584 goto done;
586 s += slen + 1;
587 remain -= slen + 1;
590 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
591 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
592 char *p;
593 size_t slen;
595 remain -= label_len;
596 if (remain <= 0) {
597 err = got_error(GOT_ERR_BAD_OBJ_DATA);
598 goto done;
600 s += label_len;
601 p = memchr(s, '\n', remain);
602 if (p == NULL) {
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 goto done;
606 *p = '\0';
607 slen = strlen(s);
608 err = parse_commit_time(&(*commit)->committer_time,
609 &(*commit)->committer_gmtoff, s);
610 if (err)
611 goto done;
612 (*commit)->committer = strdup(s);
613 if ((*commit)->committer == NULL) {
614 err = got_error_from_errno("strdup");
615 goto done;
617 s += slen + 1;
618 remain -= slen + 1;
621 (*commit)->logmsg = strndup(s, remain);
622 if ((*commit)->logmsg == NULL) {
623 err = got_error_from_errno("strndup");
624 goto done;
626 done:
627 if (err) {
628 got_object_commit_close(*commit);
629 *commit = NULL;
631 return err;
634 void
635 got_object_tree_close(struct got_tree_object *tree)
637 if (tree->refcnt > 0) {
638 tree->refcnt--;
639 if (tree->refcnt > 0)
640 return;
643 free(tree->entries);
644 free(tree);
647 static const struct got_error *
648 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
649 size_t *elen, char *buf,
650 size_t maxlen)
652 char *p, *space;
653 const struct got_error *err = NULL;
655 *name = NULL;
656 *elen = 0;
658 *pte = malloc(sizeof(**pte));
659 if (*pte == NULL)
660 return got_error_from_errno("malloc");
662 *elen = strnlen(buf, maxlen) + 1;
663 if (*elen > maxlen) {
664 free(*pte);
665 *pte = NULL;
666 return got_error(GOT_ERR_BAD_OBJ_DATA);
669 space = memchr(buf, ' ', *elen);
670 if (space == NULL || space <= buf) {
671 err = got_error(GOT_ERR_BAD_OBJ_DATA);
672 free(*pte);
673 *pte = NULL;
674 return err;
676 (*pte)->mode = 0;
677 p = buf;
678 while (p < space) {
679 if (*p < '0' && *p > '7') {
680 err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 goto done;
683 (*pte)->mode <<= 3;
684 (*pte)->mode |= *p - '0';
685 p++;
688 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
689 err = got_error(GOT_ERR_BAD_OBJ_DATA);
690 goto done;
692 *name = space + 1;
693 buf += *elen;
694 (*pte)->id = buf;
695 *elen += SHA1_DIGEST_LENGTH;
696 done:
697 if (err) {
698 free(*pte);
699 *pte = NULL;
701 return err;
704 const struct got_error *
705 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
706 uint8_t *buf, size_t len)
708 const struct got_error *err = NULL;
709 size_t remain = len;
711 *nentries = 0;
712 if (remain == 0)
713 return NULL; /* tree is empty */
715 while (remain > 0) {
716 struct got_parsed_tree_entry *pte;
717 struct got_pathlist_entry *new = NULL;
718 const char *name;
719 size_t elen;
721 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
722 if (err)
723 goto done;
724 err = got_pathlist_insert(&new, entries, name, pte);
725 if (err)
726 goto done;
727 if (new == NULL) {
728 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
729 goto done;
731 buf += elen;
732 remain -= elen;
733 (*nentries)++;
736 if (remain != 0) {
737 err = got_error(GOT_ERR_BAD_OBJ_DATA);
738 goto done;
740 done:
741 if (err) {
742 got_object_parsed_tree_entries_free(entries);
743 *nentries = 0;
745 return err;
748 void
749 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
751 struct got_pathlist_entry *pe;
753 TAILQ_FOREACH(pe, entries, entry) {
754 struct got_parsed_tree_entry *pte = pe->data;
755 free(pte);
757 got_pathlist_free(entries);
760 void
761 got_object_tag_close(struct got_tag_object *tag)
763 if (tag->refcnt > 0) {
764 tag->refcnt--;
765 if (tag->refcnt > 0)
766 return;
769 free(tag->tag);
770 free(tag->tagger);
771 free(tag->tagmsg);
772 free(tag);
775 const struct got_error *
776 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
778 const struct got_error *err = NULL;
779 size_t remain = len;
780 char *s = buf;
781 size_t label_len;
783 if (remain == 0)
784 return got_error(GOT_ERR_BAD_OBJ_DATA);
786 *tag = calloc(1, sizeof(**tag));
787 if (*tag == NULL)
788 return got_error_from_errno("calloc");
790 label_len = strlen(GOT_TAG_LABEL_OBJECT);
791 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
792 remain -= label_len;
793 if (remain < SHA1_DIGEST_STRING_LENGTH) {
794 err = got_error(GOT_ERR_BAD_OBJ_DATA);
795 goto done;
797 s += label_len;
798 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
799 err = got_error(GOT_ERR_BAD_OBJ_DATA);
800 goto done;
802 remain -= SHA1_DIGEST_STRING_LENGTH;
803 s += SHA1_DIGEST_STRING_LENGTH;
804 } else {
805 err = got_error(GOT_ERR_BAD_OBJ_DATA);
806 goto done;
809 if (remain <= 0) {
810 err = got_error(GOT_ERR_BAD_OBJ_DATA);
811 goto done;
814 label_len = strlen(GOT_TAG_LABEL_TYPE);
815 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
816 remain -= label_len;
817 if (remain <= 0) {
818 err = got_error(GOT_ERR_BAD_OBJ_DATA);
819 goto done;
821 s += label_len;
822 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
823 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
824 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
825 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
826 s += label_len;
827 remain -= label_len;
828 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
829 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
830 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
831 label_len = strlen(GOT_OBJ_LABEL_TREE);
832 s += label_len;
833 remain -= label_len;
834 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
835 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
836 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
837 label_len = strlen(GOT_OBJ_LABEL_BLOB);
838 s += label_len;
839 remain -= label_len;
840 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
841 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
842 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
843 label_len = strlen(GOT_OBJ_LABEL_TAG);
844 s += label_len;
845 remain -= label_len;
846 } else {
847 err = got_error(GOT_ERR_BAD_OBJ_DATA);
848 goto done;
851 if (remain <= 0 || *s != '\n') {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
853 goto done;
855 s++;
856 remain--;
857 if (remain <= 0) {
858 err = got_error(GOT_ERR_BAD_OBJ_DATA);
859 goto done;
861 } else {
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
863 goto done;
866 label_len = strlen(GOT_TAG_LABEL_TAG);
867 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
868 char *p;
869 size_t slen;
870 remain -= label_len;
871 if (remain <= 0) {
872 err = got_error(GOT_ERR_BAD_OBJ_DATA);
873 goto done;
875 s += label_len;
876 p = memchr(s, '\n', remain);
877 if (p == NULL) {
878 err = got_error(GOT_ERR_BAD_OBJ_DATA);
879 goto done;
881 *p = '\0';
882 slen = strlen(s);
883 (*tag)->tag = strndup(s, slen);
884 if ((*tag)->tag == NULL) {
885 err = got_error_from_errno("strndup");
886 goto done;
888 s += slen + 1;
889 remain -= slen + 1;
890 if (remain <= 0) {
891 err = got_error(GOT_ERR_BAD_OBJ_DATA);
892 goto done;
894 } else {
895 err = got_error(GOT_ERR_BAD_OBJ_DATA);
896 goto done;
899 label_len = strlen(GOT_TAG_LABEL_TAGGER);
900 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
901 char *p;
902 size_t slen;
904 remain -= label_len;
905 if (remain <= 0) {
906 err = got_error(GOT_ERR_BAD_OBJ_DATA);
907 goto done;
909 s += label_len;
910 p = memchr(s, '\n', remain);
911 if (p == NULL) {
912 err = got_error(GOT_ERR_BAD_OBJ_DATA);
913 goto done;
915 *p = '\0';
916 slen = strlen(s);
917 err = parse_commit_time(&(*tag)->tagger_time,
918 &(*tag)->tagger_gmtoff, s);
919 if (err)
920 goto done;
921 (*tag)->tagger = strdup(s);
922 if ((*tag)->tagger == NULL) {
923 err = got_error_from_errno("strdup");
924 goto done;
926 s += slen + 1;
927 remain -= slen + 1;
928 if (remain <= 0) {
929 err = got_error(GOT_ERR_BAD_OBJ_DATA);
930 goto done;
932 } else {
933 /* Some old tags in the Linux git repo have no tagger. */
934 (*tag)->tagger = strdup("");
935 if ((*tag)->tagger == NULL) {
936 err = got_error_from_errno("strdup");
937 goto done;
941 (*tag)->tagmsg = strndup(s, remain);
942 if ((*tag)->tagmsg == NULL) {
943 err = got_error_from_errno("strndup");
944 goto done;
946 done:
947 if (err) {
948 got_object_tag_close(*tag);
949 *tag = NULL;
951 return err;
954 const struct got_error *
955 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
957 const struct got_error *err = NULL;
958 static const size_t blocksize = 512;
959 size_t n, total, remain;
960 uint8_t *buf;
962 *outbuf = NULL;
963 *outlen = 0;
965 buf = malloc(blocksize);
966 if (buf == NULL)
967 return got_error_from_errno("malloc");
969 remain = blocksize;
970 total = 0;
971 for (;;) {
972 if (remain == 0) {
973 uint8_t *newbuf;
974 newbuf = reallocarray(buf, 1, total + blocksize);
975 if (newbuf == NULL) {
976 err = got_error_from_errno("reallocarray");
977 goto done;
979 buf = newbuf;
980 remain += blocksize;
982 n = fread(buf + total, 1, remain, f);
983 if (n == 0) {
984 if (ferror(f)) {
985 err = got_ferror(f, GOT_ERR_IO);
986 goto done;
988 break; /* EOF */
990 remain -= n;
991 total += n;
992 };
994 done:
995 if (err == NULL) {
996 *outbuf = buf;
997 *outlen = total;
998 } else
999 free(buf);
1000 return err;