Blob


1 /*
2 * Copyright (c) 2018, 2019 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_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 int
58 got_object_id_cmp(const struct got_object_id *id1,
59 const struct got_object_id *id2)
60 {
61 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
62 }
64 const struct got_error *
65 got_object_qid_alloc_partial(struct got_object_qid **qid)
66 {
67 const struct got_error *err = NULL;
69 *qid = malloc(sizeof(**qid));
70 if (*qid == NULL)
71 return got_error_from_errno("malloc");
73 (*qid)->id = malloc(sizeof(*((*qid)->id)));
74 if ((*qid)->id == NULL) {
75 err = got_error_from_errno("malloc");
76 got_object_qid_free(*qid);
77 *qid = NULL;
78 return err;
79 }
81 return NULL;
82 }
84 const struct got_error *
85 got_object_id_str(char **outbuf, struct got_object_id *id)
86 {
87 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
89 *outbuf = malloc(len);
90 if (*outbuf == NULL)
91 return got_error_from_errno("malloc");
93 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
94 free(*outbuf);
95 *outbuf = NULL;
96 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
97 }
99 return NULL;
102 void
103 got_object_close(struct got_object *obj)
105 if (obj->refcnt > 0) {
106 obj->refcnt--;
107 if (obj->refcnt > 0)
108 return;
111 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
112 struct got_delta *delta;
113 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
114 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
115 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
116 free(delta->delta_buf);
117 free(delta);
120 if (obj->flags & GOT_OBJ_FLAG_PACKED)
121 free(obj->path_packfile);
122 free(obj);
125 void
126 got_object_qid_free(struct got_object_qid *qid)
128 free(qid->id);
129 free(qid);
132 void
133 got_object_id_queue_free(struct got_object_id_queue *ids)
135 struct got_object_qid *qid;
137 while (!SIMPLEQ_EMPTY(ids)) {
138 qid = SIMPLEQ_FIRST(ids);
139 SIMPLEQ_REMOVE_HEAD(ids, entry);
140 got_object_qid_free(qid);
144 const struct got_error *
145 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
147 const char *obj_labels[] = {
148 GOT_OBJ_LABEL_COMMIT,
149 GOT_OBJ_LABEL_TREE,
150 GOT_OBJ_LABEL_BLOB,
151 GOT_OBJ_LABEL_TAG,
152 };
153 const int obj_types[] = {
154 GOT_OBJ_TYPE_COMMIT,
155 GOT_OBJ_TYPE_TREE,
156 GOT_OBJ_TYPE_BLOB,
157 GOT_OBJ_TYPE_TAG,
158 };
159 int type = 0;
160 size_t size = 0, hdrlen = 0;
161 int i;
163 *obj = NULL;
165 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
166 if (hdrlen > len)
167 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 for (i = 0; i < nitems(obj_labels); i++) {
170 const char *label = obj_labels[i];
171 size_t label_len = strlen(label);
172 const char *errstr;
174 if (strncmp(buf, label, label_len) != 0)
175 continue;
177 type = obj_types[i];
178 if (len <= label_len)
179 return got_error(GOT_ERR_BAD_OBJ_HDR);
180 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
181 if (errstr != NULL)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 break;
186 if (type == 0)
187 return got_error(GOT_ERR_BAD_OBJ_HDR);
189 *obj = calloc(1, sizeof(**obj));
190 if (*obj == NULL)
191 return got_error_from_errno("calloc");
192 (*obj)->type = type;
193 (*obj)->hdrlen = hdrlen;
194 (*obj)->size = size;
195 return NULL;
198 const struct got_error *
199 got_object_read_header(struct got_object **obj, int fd)
201 const struct got_error *err;
202 struct got_inflate_buf zb;
203 char *buf;
204 const size_t zbsize = 64;
205 size_t outlen, totlen;
206 int nbuf = 1;
208 *obj = NULL;
210 buf = malloc(zbsize);
211 if (buf == NULL)
212 return got_error_from_errno("malloc");
214 err = got_inflate_init(&zb, buf, zbsize);
215 if (err)
216 return err;
218 totlen = 0;
219 do {
220 err = got_inflate_read_fd(&zb, fd, &outlen);
221 if (err)
222 goto done;
223 if (outlen == 0)
224 break;
225 totlen += outlen;
226 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
227 char *newbuf;
228 nbuf++;
229 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
230 if (newbuf == NULL) {
231 err = got_error_from_errno("recallocarray");
232 goto done;
234 buf = newbuf;
235 zb.outbuf = newbuf + totlen;
236 zb.outlen = (nbuf * zbsize) - totlen;
238 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
240 err = got_object_parse_header(obj, buf, totlen);
241 done:
242 free(buf);
243 got_inflate_end(&zb);
244 return err;
247 struct got_commit_object *
248 got_object_commit_alloc_partial(void)
250 struct got_commit_object *commit;
252 commit = calloc(1, sizeof(*commit));
253 if (commit == NULL)
254 return NULL;
255 commit->tree_id = malloc(sizeof(*commit->tree_id));
256 if (commit->tree_id == NULL) {
257 free(commit);
258 return NULL;
261 SIMPLEQ_INIT(&commit->parent_ids);
263 return commit;
266 const struct got_error *
267 got_object_commit_add_parent(struct got_commit_object *commit,
268 const char *id_str)
270 const struct got_error *err = NULL;
271 struct got_object_qid *qid;
273 err = got_object_qid_alloc_partial(&qid);
274 if (err)
275 return err;
277 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
278 err = got_error(GOT_ERR_BAD_OBJ_DATA);
279 got_object_qid_free(qid);
280 return err;
283 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
284 commit->nparents++;
286 return NULL;
289 static const struct got_error *
290 parse_gmtoff(time_t *gmtoff, const char *tzstr)
292 int sign = 1;
293 const char *p = tzstr;
294 time_t h, m;
296 *gmtoff = 0;
298 if (*p == '-')
299 sign = -1;
300 else if (*p != '+')
301 return got_error(GOT_ERR_BAD_OBJ_DATA);
302 p++;
303 if (!isdigit(*p) && !isdigit(*(p + 1)))
304 return got_error(GOT_ERR_BAD_OBJ_DATA);
305 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
307 p += 2;
308 if (!isdigit(*p) && !isdigit(*(p + 1)))
309 return got_error(GOT_ERR_BAD_OBJ_DATA);
310 m = ((*p - '0') * 10) + (*(p + 1) - '0');
312 *gmtoff = (h * 60 * 60 + m * 60) * sign;
313 return NULL;
316 static const struct got_error *
317 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
319 const struct got_error *err = NULL;
320 const char *errstr;
321 char *space, *tzstr;
323 /* Parse and strip off trailing timezone indicator string. */
324 space = strrchr(committer, ' ');
325 if (space == NULL)
326 return got_error(GOT_ERR_BAD_OBJ_DATA);
327 tzstr = strdup(space + 1);
328 if (tzstr == NULL)
329 return got_error_from_errno("strdup");
330 err = parse_gmtoff(gmtoff, tzstr);
331 free(tzstr);
332 if (err)
333 return err;
334 *space = '\0';
336 /* Timestamp is separated from committer name + email by space. */
337 space = strrchr(committer, ' ');
338 if (space == NULL)
339 return got_error(GOT_ERR_BAD_OBJ_DATA);
341 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
342 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
343 if (errstr)
344 return got_error(GOT_ERR_BAD_OBJ_DATA);
346 /* Strip off parsed time information, leaving just author and email. */
347 *space = '\0';
349 return NULL;
352 void
353 got_object_commit_close(struct got_commit_object *commit)
355 if (commit->refcnt > 0) {
356 commit->refcnt--;
357 if (commit->refcnt > 0)
358 return;
361 got_object_id_queue_free(&commit->parent_ids);
362 free(commit->tree_id);
363 free(commit->author);
364 free(commit->committer);
365 free(commit->logmsg);
366 free(commit);
369 struct got_object_id *
370 got_object_commit_get_tree_id(struct got_commit_object *commit)
372 return commit->tree_id;
375 int
376 got_object_commit_get_nparents(struct got_commit_object *commit)
378 return commit->nparents;
381 const struct got_object_id_queue *
382 got_object_commit_get_parent_ids(struct got_commit_object *commit)
384 return &commit->parent_ids;
387 const char *
388 got_object_commit_get_author(struct got_commit_object *commit)
390 return commit->author;
393 time_t
394 got_object_commit_get_author_time(struct got_commit_object *commit)
396 return commit->author_time;
399 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
401 return commit->author_gmtoff;
404 const char *
405 got_object_commit_get_committer(struct got_commit_object *commit)
407 return commit->committer;
410 time_t
411 got_object_commit_get_committer_time(struct got_commit_object *commit)
413 return commit->committer_time;
416 time_t
417 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
419 return commit->committer_gmtoff;
422 #define GOT_GPG_BEGIN_STR "gpgsig -----BEGIN PGP SIGNATURE-----"
423 #define GOT_GPG_END_STR " -----END PGP SIGNATURE-----"
425 const struct got_error *
426 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
428 const struct got_error *err = NULL;
429 int gpgsig = 0;
430 char *msg0, *msg, *line, *s;
431 size_t len;
433 *logmsg = NULL;
435 msg0 = strdup(commit->logmsg);
436 if (msg0 == NULL)
437 return got_error_from_errno("strdup");
439 /* Copy log message line by line to strip out GPG sigs... */
440 msg = msg0;
441 do {
442 line = strsep(&msg, "\n");
444 if (line) {
445 /* Skip over GPG signatures. */
446 if (gpgsig) {
447 if (strcmp(line, GOT_GPG_END_STR) == 0) {
448 gpgsig = 0;
449 /* Skip empty line after sig. */
450 line = strsep(&msg, "\n");
452 continue;
453 } else if (strcmp(line, GOT_GPG_BEGIN_STR) == 0) {
454 gpgsig = 1;
455 continue;
457 if (asprintf(&s, "%s%s\n",
458 *logmsg ? *logmsg : "", line) == -1) {
459 err = got_error_from_errno("asprintf");
460 goto done;
462 free(*logmsg);
463 *logmsg = s;
465 } while (line);
467 /* Trim redundant trailing whitespace. */
468 len = strlen(*logmsg);
469 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
470 isspace((unsigned char)(*logmsg)[len - 1])) {
471 (*logmsg)[len - 1] = '\0';
472 len--;
474 done:
475 free(msg0);
476 if (err) {
477 free(*logmsg);
478 *logmsg = NULL;
480 return err;
483 const char *
484 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
486 return commit->logmsg;
489 const struct got_error *
490 got_object_parse_commit(struct got_commit_object **commit, char *buf,
491 size_t len)
493 const struct got_error *err = NULL;
494 char *s = buf;
495 size_t label_len;
496 ssize_t remain = (ssize_t)len;
498 *commit = got_object_commit_alloc_partial();
499 if (*commit == NULL)
500 return got_error_from_errno("got_object_commit_alloc_partial");
502 label_len = strlen(GOT_COMMIT_LABEL_TREE);
503 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
504 remain -= label_len;
505 if (remain < SHA1_DIGEST_STRING_LENGTH) {
506 err = got_error(GOT_ERR_BAD_OBJ_DATA);
507 goto done;
509 s += label_len;
510 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
511 err = got_error(GOT_ERR_BAD_OBJ_DATA);
512 goto done;
514 remain -= SHA1_DIGEST_STRING_LENGTH;
515 s += SHA1_DIGEST_STRING_LENGTH;
516 } else {
517 err = got_error(GOT_ERR_BAD_OBJ_DATA);
518 goto done;
521 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
522 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
523 remain -= label_len;
524 if (remain < SHA1_DIGEST_STRING_LENGTH) {
525 err = got_error(GOT_ERR_BAD_OBJ_DATA);
526 goto done;
528 s += label_len;
529 err = got_object_commit_add_parent(*commit, s);
530 if (err)
531 goto done;
533 remain -= SHA1_DIGEST_STRING_LENGTH;
534 s += SHA1_DIGEST_STRING_LENGTH;
537 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
538 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
539 char *p;
540 size_t slen;
542 remain -= label_len;
543 if (remain <= 0) {
544 err = got_error(GOT_ERR_BAD_OBJ_DATA);
545 goto done;
547 s += label_len;
548 p = memchr(s, '\n', remain);
549 if (p == NULL) {
550 err = got_error(GOT_ERR_BAD_OBJ_DATA);
551 goto done;
553 *p = '\0';
554 slen = strlen(s);
555 err = parse_commit_time(&(*commit)->author_time,
556 &(*commit)->author_gmtoff, s);
557 if (err)
558 goto done;
559 (*commit)->author = strdup(s);
560 if ((*commit)->author == NULL) {
561 err = got_error_from_errno("strdup");
562 goto done;
564 s += slen + 1;
565 remain -= slen + 1;
568 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
569 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
570 char *p;
571 size_t slen;
573 remain -= label_len;
574 if (remain <= 0) {
575 err = got_error(GOT_ERR_BAD_OBJ_DATA);
576 goto done;
578 s += label_len;
579 p = memchr(s, '\n', remain);
580 if (p == NULL) {
581 err = got_error(GOT_ERR_BAD_OBJ_DATA);
582 goto done;
584 *p = '\0';
585 slen = strlen(s);
586 err = parse_commit_time(&(*commit)->committer_time,
587 &(*commit)->committer_gmtoff, s);
588 if (err)
589 goto done;
590 (*commit)->committer = strdup(s);
591 if ((*commit)->committer == NULL) {
592 err = got_error_from_errno("strdup");
593 goto done;
595 s += slen + 1;
596 remain -= slen + 1;
599 (*commit)->logmsg = strndup(s, remain);
600 if ((*commit)->logmsg == NULL) {
601 err = got_error_from_errno("strndup");
602 goto done;
604 done:
605 if (err) {
606 got_object_commit_close(*commit);
607 *commit = NULL;
609 return err;
612 void
613 got_object_tree_entry_close(struct got_tree_entry *te)
615 free(te->id);
616 free(te->name);
617 free(te);
620 void
621 got_object_tree_entries_close(struct got_tree_entries *entries)
623 struct got_tree_entry *te;
625 while (!SIMPLEQ_EMPTY(&entries->head)) {
626 te = SIMPLEQ_FIRST(&entries->head);
627 SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
628 got_object_tree_entry_close(te);
632 void
633 got_object_tree_close(struct got_tree_object *tree)
635 if (tree->refcnt > 0) {
636 tree->refcnt--;
637 if (tree->refcnt > 0)
638 return;
641 got_object_tree_entries_close(&tree->entries);
642 free(tree);
645 struct got_tree_entry *
646 got_alloc_tree_entry_partial(void)
648 struct got_tree_entry *te;
650 te = malloc(sizeof(*te));
651 if (te == NULL)
652 return NULL;
654 te->id = malloc(sizeof(*te->id));
655 if (te->id == NULL) {
656 free(te);
657 te = NULL;
659 return te;
662 static const struct got_error *
663 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
664 size_t maxlen)
666 char *p, *space;
667 const struct got_error *err = NULL;
669 *elen = 0;
671 *te = got_alloc_tree_entry_partial();
672 if (*te == NULL)
673 return got_error_from_errno("got_alloc_tree_entry_partial");
675 *elen = strnlen(buf, maxlen) + 1;
676 if (*elen > maxlen) {
677 free(*te);
678 *te = NULL;
679 return got_error(GOT_ERR_BAD_OBJ_DATA);
682 space = memchr(buf, ' ', *elen);
683 if (space == NULL || space <= buf) {
684 err = got_error(GOT_ERR_BAD_OBJ_DATA);
685 free(*te);
686 *te = NULL;
687 return err;
689 (*te)->mode = 0;
690 p = buf;
691 while (p < space) {
692 if (*p < '0' && *p > '7') {
693 err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 goto done;
696 (*te)->mode <<= 3;
697 (*te)->mode |= *p - '0';
698 p++;
701 (*te)->name = strdup(space + 1);
702 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
703 err = got_error(GOT_ERR_BAD_OBJ_DATA);
704 goto done;
706 buf += *elen;
707 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
708 *elen += SHA1_DIGEST_LENGTH;
709 done:
710 if (err) {
711 got_object_tree_entry_close(*te);
712 *te = NULL;
714 return err;
717 const struct got_error *
718 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
720 const struct got_error *err;
721 size_t remain = len;
722 struct got_pathlist_head pathlist;
723 struct got_pathlist_entry *pe;
725 TAILQ_INIT(&pathlist);
727 *tree = calloc(1, sizeof(**tree));
728 if (*tree == NULL)
729 return got_error_from_errno("calloc");
731 SIMPLEQ_INIT(&(*tree)->entries.head);
733 while (remain > 0) {
734 struct got_tree_entry *te;
735 struct got_pathlist_entry *new = NULL;
736 size_t elen;
738 err = parse_tree_entry(&te, &elen, buf, remain);
739 if (err)
740 goto done;
741 err = got_pathlist_insert(&new, &pathlist, te->name, te);
742 if (err)
743 goto done;
744 if (new == NULL) {
745 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
746 goto done;
748 buf += elen;
749 remain -= elen;
752 if (remain != 0) {
753 got_object_tree_close(*tree);
754 *tree = NULL;
755 err = got_error(GOT_ERR_BAD_OBJ_DATA);
756 goto done;
759 TAILQ_FOREACH(pe, &pathlist, entry) {
760 struct got_tree_entry *te = pe->data;
761 (*tree)->entries.nentries++;
762 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
764 done:
765 got_pathlist_free(&pathlist);
766 return err;
769 void
770 got_object_tag_close(struct got_tag_object *tag)
772 if (tag->refcnt > 0) {
773 tag->refcnt--;
774 if (tag->refcnt > 0)
775 return;
778 free(tag->tag);
779 free(tag->tagger);
780 free(tag->tagmsg);
781 free(tag);
784 const struct got_error *
785 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
787 const struct got_error *err = NULL;
788 size_t remain = len;
789 char *s = buf;
790 size_t label_len;
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;