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/syslimits.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 }
94 return NULL;
95 }
97 const struct got_error *
98 got_object_id_str(char **outbuf, struct got_object_id *id)
99 {
100 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
102 *outbuf = malloc(len);
103 if (*outbuf == NULL)
104 return got_error_from_errno("malloc");
106 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
107 free(*outbuf);
108 *outbuf = NULL;
109 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
112 return NULL;
115 void
116 got_object_close(struct got_object *obj)
118 if (obj->refcnt > 0) {
119 obj->refcnt--;
120 if (obj->refcnt > 0)
121 return;
124 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
125 struct got_delta *delta;
126 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
127 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
128 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
129 free(delta);
132 free(obj);
135 void
136 got_object_qid_free(struct got_object_qid *qid)
138 free(qid->id);
139 free(qid);
142 void
143 got_object_id_queue_free(struct got_object_id_queue *ids)
145 struct got_object_qid *qid;
147 while (!SIMPLEQ_EMPTY(ids)) {
148 qid = SIMPLEQ_FIRST(ids);
149 SIMPLEQ_REMOVE_HEAD(ids, entry);
150 got_object_qid_free(qid);
154 const struct got_error *
155 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
157 const char *obj_labels[] = {
158 GOT_OBJ_LABEL_COMMIT,
159 GOT_OBJ_LABEL_TREE,
160 GOT_OBJ_LABEL_BLOB,
161 GOT_OBJ_LABEL_TAG,
162 };
163 const int obj_types[] = {
164 GOT_OBJ_TYPE_COMMIT,
165 GOT_OBJ_TYPE_TREE,
166 GOT_OBJ_TYPE_BLOB,
167 GOT_OBJ_TYPE_TAG,
168 };
169 int type = 0;
170 size_t size = 0, hdrlen = 0;
171 int i;
173 *obj = NULL;
175 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
176 if (hdrlen > len)
177 return got_error(GOT_ERR_BAD_OBJ_HDR);
179 for (i = 0; i < nitems(obj_labels); i++) {
180 const char *label = obj_labels[i];
181 size_t label_len = strlen(label);
182 const char *errstr;
184 if (strncmp(buf, label, label_len) != 0)
185 continue;
187 type = obj_types[i];
188 if (len <= label_len)
189 return got_error(GOT_ERR_BAD_OBJ_HDR);
190 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
191 if (errstr != NULL)
192 return got_error(GOT_ERR_BAD_OBJ_HDR);
193 break;
196 if (type == 0)
197 return got_error(GOT_ERR_BAD_OBJ_HDR);
199 *obj = calloc(1, sizeof(**obj));
200 if (*obj == NULL)
201 return got_error_from_errno("calloc");
202 (*obj)->type = type;
203 (*obj)->hdrlen = hdrlen;
204 (*obj)->size = size;
205 return NULL;
208 const struct got_error *
209 got_object_read_header(struct got_object **obj, int fd)
211 const struct got_error *err;
212 struct got_inflate_buf zb;
213 char *buf;
214 const size_t zbsize = 64;
215 size_t outlen, totlen;
216 int nbuf = 1;
218 *obj = NULL;
220 buf = malloc(zbsize);
221 if (buf == NULL)
222 return got_error_from_errno("malloc");
224 err = got_inflate_init(&zb, buf, zbsize, NULL);
225 if (err)
226 return err;
228 totlen = 0;
229 do {
230 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
231 if (err)
232 goto done;
233 if (outlen == 0)
234 break;
235 totlen += outlen;
236 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
237 char *newbuf;
238 nbuf++;
239 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
240 if (newbuf == NULL) {
241 err = got_error_from_errno("recallocarray");
242 goto done;
244 buf = newbuf;
245 zb.outbuf = newbuf + totlen;
246 zb.outlen = (nbuf * zbsize) - totlen;
248 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
250 err = got_object_parse_header(obj, buf, totlen);
251 done:
252 free(buf);
253 got_inflate_end(&zb);
254 return err;
257 struct got_commit_object *
258 got_object_commit_alloc_partial(void)
260 struct got_commit_object *commit;
262 commit = calloc(1, sizeof(*commit));
263 if (commit == NULL)
264 return NULL;
265 commit->tree_id = malloc(sizeof(*commit->tree_id));
266 if (commit->tree_id == NULL) {
267 free(commit);
268 return NULL;
271 SIMPLEQ_INIT(&commit->parent_ids);
273 return commit;
276 const struct got_error *
277 got_object_commit_add_parent(struct got_commit_object *commit,
278 const char *id_str)
280 const struct got_error *err = NULL;
281 struct got_object_qid *qid;
283 err = got_object_qid_alloc_partial(&qid);
284 if (err)
285 return err;
287 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
288 err = got_error(GOT_ERR_BAD_OBJ_DATA);
289 got_object_qid_free(qid);
290 return err;
293 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
294 commit->nparents++;
296 return NULL;
299 static const struct got_error *
300 parse_gmtoff(time_t *gmtoff, const char *tzstr)
302 int sign = 1;
303 const char *p = tzstr;
304 time_t h, m;
306 *gmtoff = 0;
308 if (*p == '-')
309 sign = -1;
310 else if (*p != '+')
311 return got_error(GOT_ERR_BAD_OBJ_DATA);
312 p++;
313 if (!isdigit(*p) && !isdigit(*(p + 1)))
314 return got_error(GOT_ERR_BAD_OBJ_DATA);
315 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
317 p += 2;
318 if (!isdigit(*p) && !isdigit(*(p + 1)))
319 return got_error(GOT_ERR_BAD_OBJ_DATA);
320 m = ((*p - '0') * 10) + (*(p + 1) - '0');
322 *gmtoff = (h * 60 * 60 + m * 60) * sign;
323 return NULL;
326 static const struct got_error *
327 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
329 const struct got_error *err = NULL;
330 const char *errstr;
331 char *space, *tzstr;
333 /* Parse and strip off trailing timezone indicator string. */
334 space = strrchr(committer, ' ');
335 if (space == NULL)
336 return got_error(GOT_ERR_BAD_OBJ_DATA);
337 tzstr = strdup(space + 1);
338 if (tzstr == NULL)
339 return got_error_from_errno("strdup");
340 err = parse_gmtoff(gmtoff, tzstr);
341 free(tzstr);
342 if (err)
343 return err;
344 *space = '\0';
346 /* Timestamp is separated from committer name + email by space. */
347 space = strrchr(committer, ' ');
348 if (space == NULL)
349 return got_error(GOT_ERR_BAD_OBJ_DATA);
351 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
352 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
353 if (errstr)
354 return got_error(GOT_ERR_BAD_OBJ_DATA);
356 /* Strip off parsed time information, leaving just author and email. */
357 *space = '\0';
359 return NULL;
362 void
363 got_object_commit_close(struct got_commit_object *commit)
365 if (commit->refcnt > 0) {
366 commit->refcnt--;
367 if (commit->refcnt > 0)
368 return;
371 got_object_id_queue_free(&commit->parent_ids);
372 free(commit->tree_id);
373 free(commit->author);
374 free(commit->committer);
375 free(commit->logmsg);
376 free(commit);
379 struct got_object_id *
380 got_object_commit_get_tree_id(struct got_commit_object *commit)
382 return commit->tree_id;
385 int
386 got_object_commit_get_nparents(struct got_commit_object *commit)
388 return commit->nparents;
391 const struct got_object_id_queue *
392 got_object_commit_get_parent_ids(struct got_commit_object *commit)
394 return &commit->parent_ids;
397 const char *
398 got_object_commit_get_author(struct got_commit_object *commit)
400 return commit->author;
403 time_t
404 got_object_commit_get_author_time(struct got_commit_object *commit)
406 return commit->author_time;
409 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
411 return commit->author_gmtoff;
414 const char *
415 got_object_commit_get_committer(struct got_commit_object *commit)
417 return commit->committer;
420 time_t
421 got_object_commit_get_committer_time(struct got_commit_object *commit)
423 return commit->committer_time;
426 time_t
427 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
429 return commit->committer_gmtoff;
432 const struct got_error *
433 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
435 const struct got_error *err = NULL;
436 char *msg0, *msg, *line, *s;
437 size_t len;
438 int headers = 1;
440 *logmsg = NULL;
442 msg0 = strdup(commit->logmsg);
443 if (msg0 == NULL)
444 return got_error_from_errno("strdup");
446 /* Copy log message line by line to strip out unusual headers... */
447 msg = msg0;
448 do {
449 if ((line = strsep(&msg, "\n")) == NULL)
450 break;
452 if (headers == 1) {
453 if (line[0] != '\0' &&
454 strncmp(line, GOT_COMMIT_LABEL_TREE,
455 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
456 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
457 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
458 strncmp(line, GOT_COMMIT_LABEL_PARENT,
459 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
460 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
461 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
462 continue;
464 if (line[0] == '\0')
465 headers = 0;
468 if (asprintf(&s, "%s%s\n",
469 *logmsg ? *logmsg : "", line) == -1) {
470 err = got_error_from_errno("asprintf");
471 goto done;
473 free(*logmsg);
474 *logmsg = s;
476 } while (line);
478 if (*logmsg == NULL) {
479 /* log message does not contain \n */
480 *logmsg = strdup(commit->logmsg);
481 if (*logmsg == NULL) {
482 err = got_error_from_errno("strdup");
483 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--;
494 done:
495 free(msg0);
496 if (err) {
497 free(*logmsg);
498 *logmsg = NULL;
500 return err;
503 const char *
504 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
506 return commit->logmsg;
509 const struct got_error *
510 got_object_parse_commit(struct got_commit_object **commit, char *buf,
511 size_t len)
513 const struct got_error *err = NULL;
514 char *s = buf;
515 size_t label_len;
516 ssize_t remain = (ssize_t)len;
518 if (remain == 0)
519 return got_error(GOT_ERR_BAD_OBJ_DATA);
521 *commit = got_object_commit_alloc_partial();
522 if (*commit == NULL)
523 return got_error_from_errno("got_object_commit_alloc_partial");
525 label_len = strlen(GOT_COMMIT_LABEL_TREE);
526 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
527 remain -= label_len;
528 if (remain < SHA1_DIGEST_STRING_LENGTH) {
529 err = got_error(GOT_ERR_BAD_OBJ_DATA);
530 goto done;
532 s += label_len;
533 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
534 err = got_error(GOT_ERR_BAD_OBJ_DATA);
535 goto done;
537 remain -= SHA1_DIGEST_STRING_LENGTH;
538 s += SHA1_DIGEST_STRING_LENGTH;
539 } else {
540 err = got_error(GOT_ERR_BAD_OBJ_DATA);
541 goto done;
544 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
545 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
546 remain -= label_len;
547 if (remain < SHA1_DIGEST_STRING_LENGTH) {
548 err = got_error(GOT_ERR_BAD_OBJ_DATA);
549 goto done;
551 s += label_len;
552 err = got_object_commit_add_parent(*commit, s);
553 if (err)
554 goto done;
556 remain -= SHA1_DIGEST_STRING_LENGTH;
557 s += SHA1_DIGEST_STRING_LENGTH;
560 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
561 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
562 char *p;
563 size_t slen;
565 remain -= label_len;
566 if (remain <= 0) {
567 err = got_error(GOT_ERR_BAD_OBJ_DATA);
568 goto done;
570 s += label_len;
571 p = memchr(s, '\n', remain);
572 if (p == NULL) {
573 err = got_error(GOT_ERR_BAD_OBJ_DATA);
574 goto done;
576 *p = '\0';
577 slen = strlen(s);
578 err = parse_commit_time(&(*commit)->author_time,
579 &(*commit)->author_gmtoff, s);
580 if (err)
581 goto done;
582 (*commit)->author = strdup(s);
583 if ((*commit)->author == NULL) {
584 err = got_error_from_errno("strdup");
585 goto done;
587 s += slen + 1;
588 remain -= slen + 1;
591 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
592 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
593 char *p;
594 size_t slen;
596 remain -= label_len;
597 if (remain <= 0) {
598 err = got_error(GOT_ERR_BAD_OBJ_DATA);
599 goto done;
601 s += label_len;
602 p = memchr(s, '\n', remain);
603 if (p == NULL) {
604 err = got_error(GOT_ERR_BAD_OBJ_DATA);
605 goto done;
607 *p = '\0';
608 slen = strlen(s);
609 err = parse_commit_time(&(*commit)->committer_time,
610 &(*commit)->committer_gmtoff, s);
611 if (err)
612 goto done;
613 (*commit)->committer = strdup(s);
614 if ((*commit)->committer == NULL) {
615 err = got_error_from_errno("strdup");
616 goto done;
618 s += slen + 1;
619 remain -= slen + 1;
622 (*commit)->logmsg = strndup(s, remain);
623 if ((*commit)->logmsg == NULL) {
624 err = got_error_from_errno("strndup");
625 goto done;
627 done:
628 if (err) {
629 got_object_commit_close(*commit);
630 *commit = NULL;
632 return err;
635 void
636 got_object_tree_close(struct got_tree_object *tree)
638 if (tree->refcnt > 0) {
639 tree->refcnt--;
640 if (tree->refcnt > 0)
641 return;
644 free(tree->entries);
645 free(tree);
648 static const struct got_error *
649 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
650 size_t *elen, char *buf,
651 size_t maxlen)
653 char *p, *space;
654 const struct got_error *err = NULL;
656 *name = NULL;
657 *elen = 0;
659 *pte = malloc(sizeof(**pte));
660 if (*pte == NULL)
661 return got_error_from_errno("malloc");
663 *elen = strnlen(buf, maxlen) + 1;
664 if (*elen > maxlen) {
665 free(*pte);
666 *pte = NULL;
667 return got_error(GOT_ERR_BAD_OBJ_DATA);
670 space = memchr(buf, ' ', *elen);
671 if (space == NULL || space <= buf) {
672 err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 free(*pte);
674 *pte = NULL;
675 return err;
677 (*pte)->mode = 0;
678 p = buf;
679 while (p < space) {
680 if (*p < '0' && *p > '7') {
681 err = got_error(GOT_ERR_BAD_OBJ_DATA);
682 goto done;
684 (*pte)->mode <<= 3;
685 (*pte)->mode |= *p - '0';
686 p++;
689 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
690 err = got_error(GOT_ERR_BAD_OBJ_DATA);
691 goto done;
693 *name = space + 1;
694 buf += *elen;
695 (*pte)->id = buf;
696 *elen += SHA1_DIGEST_LENGTH;
697 done:
698 if (err) {
699 free(*pte);
700 *pte = NULL;
702 return err;
705 const struct got_error *
706 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
707 uint8_t *buf, size_t len)
709 const struct got_error *err = NULL;
710 size_t remain = len;
712 *nentries = 0;
713 if (remain == 0)
714 return NULL; /* tree is empty */
716 while (remain > 0) {
717 struct got_parsed_tree_entry *pte;
718 struct got_pathlist_entry *new = NULL;
719 const char *name;
720 size_t elen;
722 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
723 if (err)
724 goto done;
725 err = got_pathlist_insert(&new, entries, name, pte);
726 if (err)
727 goto done;
728 if (new == NULL) {
729 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
730 goto done;
732 buf += elen;
733 remain -= elen;
734 (*nentries)++;
737 if (remain != 0) {
738 err = got_error(GOT_ERR_BAD_OBJ_DATA);
739 goto done;
741 done:
742 if (err) {
743 got_object_parsed_tree_entries_free(entries);
744 *nentries = 0;
746 return err;
749 void
750 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
752 struct got_pathlist_entry *pe;
754 TAILQ_FOREACH(pe, entries, entry) {
755 struct got_parsed_tree_entry *pte = pe->data;
756 free(pte);
758 got_pathlist_free(entries);
761 void
762 got_object_tag_close(struct got_tag_object *tag)
764 if (tag->refcnt > 0) {
765 tag->refcnt--;
766 if (tag->refcnt > 0)
767 return;
770 free(tag->tag);
771 free(tag->tagger);
772 free(tag->tagmsg);
773 free(tag);
776 const struct got_error *
777 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
779 const struct got_error *err = NULL;
780 size_t remain = len;
781 char *s = buf;
782 size_t label_len;
784 if (remain == 0)
785 return got_error(GOT_ERR_BAD_OBJ_DATA);
787 *tag = calloc(1, sizeof(**tag));
788 if (*tag == NULL)
789 return got_error_from_errno("calloc");
791 label_len = strlen(GOT_TAG_LABEL_OBJECT);
792 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
793 remain -= label_len;
794 if (remain < SHA1_DIGEST_STRING_LENGTH) {
795 err = got_error(GOT_ERR_BAD_OBJ_DATA);
796 goto done;
798 s += label_len;
799 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
800 err = got_error(GOT_ERR_BAD_OBJ_DATA);
801 goto done;
803 remain -= SHA1_DIGEST_STRING_LENGTH;
804 s += SHA1_DIGEST_STRING_LENGTH;
805 } else {
806 err = got_error(GOT_ERR_BAD_OBJ_DATA);
807 goto done;
810 if (remain <= 0) {
811 err = got_error(GOT_ERR_BAD_OBJ_DATA);
812 goto done;
815 label_len = strlen(GOT_TAG_LABEL_TYPE);
816 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
817 remain -= label_len;
818 if (remain <= 0) {
819 err = got_error(GOT_ERR_BAD_OBJ_DATA);
820 goto done;
822 s += label_len;
823 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
824 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
825 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
826 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
827 s += label_len;
828 remain -= label_len;
829 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
830 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
831 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
832 label_len = strlen(GOT_OBJ_LABEL_TREE);
833 s += label_len;
834 remain -= label_len;
835 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
836 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
837 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
838 label_len = strlen(GOT_OBJ_LABEL_BLOB);
839 s += label_len;
840 remain -= label_len;
841 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
842 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
843 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
844 label_len = strlen(GOT_OBJ_LABEL_TAG);
845 s += label_len;
846 remain -= label_len;
847 } else {
848 err = got_error(GOT_ERR_BAD_OBJ_DATA);
849 goto done;
852 if (remain <= 0 || *s != '\n') {
853 err = got_error(GOT_ERR_BAD_OBJ_DATA);
854 goto done;
856 s++;
857 remain--;
858 if (remain <= 0) {
859 err = got_error(GOT_ERR_BAD_OBJ_DATA);
860 goto done;
862 } else {
863 err = got_error(GOT_ERR_BAD_OBJ_DATA);
864 goto done;
867 label_len = strlen(GOT_TAG_LABEL_TAG);
868 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
869 char *p;
870 size_t slen;
871 remain -= label_len;
872 if (remain <= 0) {
873 err = got_error(GOT_ERR_BAD_OBJ_DATA);
874 goto done;
876 s += label_len;
877 p = memchr(s, '\n', remain);
878 if (p == NULL) {
879 err = got_error(GOT_ERR_BAD_OBJ_DATA);
880 goto done;
882 *p = '\0';
883 slen = strlen(s);
884 (*tag)->tag = strndup(s, slen);
885 if ((*tag)->tag == NULL) {
886 err = got_error_from_errno("strndup");
887 goto done;
889 s += slen + 1;
890 remain -= slen + 1;
891 if (remain <= 0) {
892 err = got_error(GOT_ERR_BAD_OBJ_DATA);
893 goto done;
895 } else {
896 err = got_error(GOT_ERR_BAD_OBJ_DATA);
897 goto done;
900 label_len = strlen(GOT_TAG_LABEL_TAGGER);
901 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
902 char *p;
903 size_t slen;
905 remain -= label_len;
906 if (remain <= 0) {
907 err = got_error(GOT_ERR_BAD_OBJ_DATA);
908 goto done;
910 s += label_len;
911 p = memchr(s, '\n', remain);
912 if (p == NULL) {
913 err = got_error(GOT_ERR_BAD_OBJ_DATA);
914 goto done;
916 *p = '\0';
917 slen = strlen(s);
918 err = parse_commit_time(&(*tag)->tagger_time,
919 &(*tag)->tagger_gmtoff, s);
920 if (err)
921 goto done;
922 (*tag)->tagger = strdup(s);
923 if ((*tag)->tagger == NULL) {
924 err = got_error_from_errno("strdup");
925 goto done;
927 s += slen + 1;
928 remain -= slen + 1;
929 if (remain <= 0) {
930 err = got_error(GOT_ERR_BAD_OBJ_DATA);
931 goto done;
933 } else {
934 /* Some old tags in the Linux git repo have no tagger. */
935 (*tag)->tagger = strdup("");
936 if ((*tag)->tagger == NULL) {
937 err = got_error_from_errno("strdup");
938 goto done;
942 (*tag)->tagmsg = strndup(s, remain);
943 if ((*tag)->tagmsg == NULL) {
944 err = got_error_from_errno("strndup");
945 goto done;
947 done:
948 if (err) {
949 got_object_tag_close(*tag);
950 *tag = NULL;
952 return err;
955 const struct got_error *
956 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
958 const struct got_error *err = NULL;
959 static const size_t blocksize = 512;
960 size_t n, total, remain;
961 uint8_t *buf;
963 *outbuf = NULL;
964 *outlen = 0;
966 buf = malloc(blocksize);
967 if (buf == NULL)
968 return got_error_from_errno("malloc");
970 remain = blocksize;
971 total = 0;
972 for (;;) {
973 if (remain == 0) {
974 uint8_t *newbuf;
975 newbuf = reallocarray(buf, 1, total + blocksize);
976 if (newbuf == NULL) {
977 err = got_error_from_errno("reallocarray");
978 goto done;
980 buf = newbuf;
981 remain += blocksize;
983 n = fread(buf + total, 1, remain, f);
984 if (n == 0) {
985 if (ferror(f)) {
986 err = got_ferror(f, GOT_ERR_IO);
987 goto done;
989 break; /* EOF */
991 remain -= n;
992 total += n;
993 };
995 done:
996 if (err == NULL) {
997 *outbuf = buf;
998 *outlen = total;
999 } else
1000 free(buf);
1001 return err;