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);
119 if (obj->flags & GOT_OBJ_FLAG_PACKED)
120 free(obj->path_packfile);
121 free(obj);
124 void
125 got_object_qid_free(struct got_object_qid *qid)
127 free(qid->id);
128 free(qid);
131 void
132 got_object_id_queue_free(struct got_object_id_queue *ids)
134 struct got_object_qid *qid;
136 while (!SIMPLEQ_EMPTY(ids)) {
137 qid = SIMPLEQ_FIRST(ids);
138 SIMPLEQ_REMOVE_HEAD(ids, entry);
139 got_object_qid_free(qid);
143 const struct got_error *
144 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
146 const char *obj_labels[] = {
147 GOT_OBJ_LABEL_COMMIT,
148 GOT_OBJ_LABEL_TREE,
149 GOT_OBJ_LABEL_BLOB,
150 GOT_OBJ_LABEL_TAG,
151 };
152 const int obj_types[] = {
153 GOT_OBJ_TYPE_COMMIT,
154 GOT_OBJ_TYPE_TREE,
155 GOT_OBJ_TYPE_BLOB,
156 GOT_OBJ_TYPE_TAG,
157 };
158 int type = 0;
159 size_t size = 0, hdrlen = 0;
160 int i;
162 *obj = NULL;
164 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
165 if (hdrlen > len)
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
168 for (i = 0; i < nitems(obj_labels); i++) {
169 const char *label = obj_labels[i];
170 size_t label_len = strlen(label);
171 const char *errstr;
173 if (strncmp(buf, label, label_len) != 0)
174 continue;
176 type = obj_types[i];
177 if (len <= label_len)
178 return got_error(GOT_ERR_BAD_OBJ_HDR);
179 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
180 if (errstr != NULL)
181 return got_error(GOT_ERR_BAD_OBJ_HDR);
182 break;
185 if (type == 0)
186 return got_error(GOT_ERR_BAD_OBJ_HDR);
188 *obj = calloc(1, sizeof(**obj));
189 if (*obj == NULL)
190 return got_error_from_errno("calloc");
191 (*obj)->type = type;
192 (*obj)->hdrlen = hdrlen;
193 (*obj)->size = size;
194 return NULL;
197 const struct got_error *
198 got_object_read_header(struct got_object **obj, int fd)
200 const struct got_error *err;
201 struct got_inflate_buf zb;
202 char *buf;
203 const size_t zbsize = 64;
204 size_t outlen, totlen;
205 int nbuf = 1;
207 *obj = NULL;
209 buf = malloc(zbsize);
210 if (buf == NULL)
211 return got_error_from_errno("malloc");
213 err = got_inflate_init(&zb, buf, zbsize);
214 if (err)
215 return err;
217 totlen = 0;
218 do {
219 err = got_inflate_read_fd(&zb, fd, &outlen);
220 if (err)
221 goto done;
222 if (outlen == 0)
223 break;
224 totlen += outlen;
225 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
226 char *newbuf;
227 nbuf++;
228 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
229 if (newbuf == NULL) {
230 err = got_error_from_errno("recallocarray");
231 goto done;
233 buf = newbuf;
234 zb.outbuf = newbuf + totlen;
235 zb.outlen = (nbuf * zbsize) - totlen;
237 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
239 err = got_object_parse_header(obj, buf, totlen);
240 done:
241 free(buf);
242 got_inflate_end(&zb);
243 return err;
246 struct got_commit_object *
247 got_object_commit_alloc_partial(void)
249 struct got_commit_object *commit;
251 commit = calloc(1, sizeof(*commit));
252 if (commit == NULL)
253 return NULL;
254 commit->tree_id = malloc(sizeof(*commit->tree_id));
255 if (commit->tree_id == NULL) {
256 free(commit);
257 return NULL;
260 SIMPLEQ_INIT(&commit->parent_ids);
262 return commit;
265 const struct got_error *
266 got_object_commit_add_parent(struct got_commit_object *commit,
267 const char *id_str)
269 const struct got_error *err = NULL;
270 struct got_object_qid *qid;
272 err = got_object_qid_alloc_partial(&qid);
273 if (err)
274 return err;
276 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
277 err = got_error(GOT_ERR_BAD_OBJ_DATA);
278 got_object_qid_free(qid);
279 return err;
282 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
283 commit->nparents++;
285 return NULL;
288 static const struct got_error *
289 parse_gmtoff(time_t *gmtoff, const char *tzstr)
291 int sign = 1;
292 const char *p = tzstr;
293 time_t h, m;
295 *gmtoff = 0;
297 if (*p == '-')
298 sign = -1;
299 else if (*p != '+')
300 return got_error(GOT_ERR_BAD_OBJ_DATA);
301 p++;
302 if (!isdigit(*p) && !isdigit(*(p + 1)))
303 return got_error(GOT_ERR_BAD_OBJ_DATA);
304 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
306 p += 2;
307 if (!isdigit(*p) && !isdigit(*(p + 1)))
308 return got_error(GOT_ERR_BAD_OBJ_DATA);
309 m = ((*p - '0') * 10) + (*(p + 1) - '0');
311 *gmtoff = (h * 60 * 60 + m * 60) * sign;
312 return NULL;
315 static const struct got_error *
316 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
318 const struct got_error *err = NULL;
319 const char *errstr;
320 char *space, *tzstr;
322 /* Parse and strip off trailing timezone indicator string. */
323 space = strrchr(committer, ' ');
324 if (space == NULL)
325 return got_error(GOT_ERR_BAD_OBJ_DATA);
326 tzstr = strdup(space + 1);
327 if (tzstr == NULL)
328 return got_error_from_errno("strdup");
329 err = parse_gmtoff(gmtoff, tzstr);
330 free(tzstr);
331 if (err)
332 return err;
333 *space = '\0';
335 /* Timestamp is separated from committer name + email by space. */
336 space = strrchr(committer, ' ');
337 if (space == NULL)
338 return got_error(GOT_ERR_BAD_OBJ_DATA);
340 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
341 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
342 if (errstr)
343 return got_error(GOT_ERR_BAD_OBJ_DATA);
345 /* Strip off parsed time information, leaving just author and email. */
346 *space = '\0';
348 return NULL;
351 void
352 got_object_commit_close(struct got_commit_object *commit)
354 if (commit->refcnt > 0) {
355 commit->refcnt--;
356 if (commit->refcnt > 0)
357 return;
360 got_object_id_queue_free(&commit->parent_ids);
361 free(commit->tree_id);
362 free(commit->author);
363 free(commit->committer);
364 free(commit->logmsg);
365 free(commit);
368 struct got_object_id *
369 got_object_commit_get_tree_id(struct got_commit_object *commit)
371 return commit->tree_id;
374 int
375 got_object_commit_get_nparents(struct got_commit_object *commit)
377 return commit->nparents;
380 const struct got_object_id_queue *
381 got_object_commit_get_parent_ids(struct got_commit_object *commit)
383 return &commit->parent_ids;
386 const char *
387 got_object_commit_get_author(struct got_commit_object *commit)
389 return commit->author;
392 time_t
393 got_object_commit_get_author_time(struct got_commit_object *commit)
395 return commit->author_time;
398 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
400 return commit->author_gmtoff;
403 const char *
404 got_object_commit_get_committer(struct got_commit_object *commit)
406 return commit->committer;
409 time_t
410 got_object_commit_get_committer_time(struct got_commit_object *commit)
412 return commit->committer_time;
415 time_t
416 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
418 return commit->committer_gmtoff;
421 const struct got_error *
422 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
424 const struct got_error *err = NULL;
425 char *msg0, *msg, *line, *s;
426 size_t len;
427 int headers = 1;
429 *logmsg = NULL;
431 msg0 = strdup(commit->logmsg);
432 if (msg0 == NULL)
433 return got_error_from_errno("strdup");
435 /* Copy log message line by line to strip out unusual headers... */
436 msg = msg0;
437 do {
438 if ((line = strsep(&msg, "\n")) == NULL)
439 break;
441 if (headers == 1) {
442 if (line[0] != '\0' &&
443 strncmp(line, GOT_COMMIT_LABEL_TREE,
444 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
445 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
446 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
447 strncmp(line, GOT_COMMIT_LABEL_PARENT,
448 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
449 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
450 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
451 continue;
453 if (line[0] == '\0')
454 headers = 0;
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 if (remain == 0)
499 return got_error(GOT_ERR_BAD_OBJ_DATA);
501 *commit = got_object_commit_alloc_partial();
502 if (*commit == NULL)
503 return got_error_from_errno("got_object_commit_alloc_partial");
505 label_len = strlen(GOT_COMMIT_LABEL_TREE);
506 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
507 remain -= label_len;
508 if (remain < SHA1_DIGEST_STRING_LENGTH) {
509 err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 goto done;
512 s += label_len;
513 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
514 err = got_error(GOT_ERR_BAD_OBJ_DATA);
515 goto done;
517 remain -= SHA1_DIGEST_STRING_LENGTH;
518 s += SHA1_DIGEST_STRING_LENGTH;
519 } else {
520 err = got_error(GOT_ERR_BAD_OBJ_DATA);
521 goto done;
524 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
525 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, 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 err = got_object_commit_add_parent(*commit, s);
533 if (err)
534 goto done;
536 remain -= SHA1_DIGEST_STRING_LENGTH;
537 s += SHA1_DIGEST_STRING_LENGTH;
540 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
541 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
542 char *p;
543 size_t slen;
545 remain -= label_len;
546 if (remain <= 0) {
547 err = got_error(GOT_ERR_BAD_OBJ_DATA);
548 goto done;
550 s += label_len;
551 p = memchr(s, '\n', remain);
552 if (p == NULL) {
553 err = got_error(GOT_ERR_BAD_OBJ_DATA);
554 goto done;
556 *p = '\0';
557 slen = strlen(s);
558 err = parse_commit_time(&(*commit)->author_time,
559 &(*commit)->author_gmtoff, s);
560 if (err)
561 goto done;
562 (*commit)->author = strdup(s);
563 if ((*commit)->author == NULL) {
564 err = got_error_from_errno("strdup");
565 goto done;
567 s += slen + 1;
568 remain -= slen + 1;
571 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
572 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
573 char *p;
574 size_t slen;
576 remain -= label_len;
577 if (remain <= 0) {
578 err = got_error(GOT_ERR_BAD_OBJ_DATA);
579 goto done;
581 s += label_len;
582 p = memchr(s, '\n', remain);
583 if (p == NULL) {
584 err = got_error(GOT_ERR_BAD_OBJ_DATA);
585 goto done;
587 *p = '\0';
588 slen = strlen(s);
589 err = parse_commit_time(&(*commit)->committer_time,
590 &(*commit)->committer_gmtoff, s);
591 if (err)
592 goto done;
593 (*commit)->committer = strdup(s);
594 if ((*commit)->committer == NULL) {
595 err = got_error_from_errno("strdup");
596 goto done;
598 s += slen + 1;
599 remain -= slen + 1;
602 (*commit)->logmsg = strndup(s, remain);
603 if ((*commit)->logmsg == NULL) {
604 err = got_error_from_errno("strndup");
605 goto done;
607 done:
608 if (err) {
609 got_object_commit_close(*commit);
610 *commit = NULL;
612 return err;
615 void
616 got_object_tree_entry_close(struct got_tree_entry *te)
618 free(te->id);
619 free(te->name);
620 free(te);
623 void
624 got_object_tree_entries_close(struct got_tree_entries *entries)
626 struct got_tree_entry *te;
628 while (!SIMPLEQ_EMPTY(&entries->head)) {
629 te = SIMPLEQ_FIRST(&entries->head);
630 SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
631 got_object_tree_entry_close(te);
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 got_object_tree_entries_close(&tree->entries);
645 free(tree);
648 struct got_tree_entry *
649 got_alloc_tree_entry_partial(void)
651 struct got_tree_entry *te;
653 te = malloc(sizeof(*te));
654 if (te == NULL)
655 return NULL;
657 te->id = malloc(sizeof(*te->id));
658 if (te->id == NULL) {
659 free(te);
660 te = NULL;
662 return te;
665 static const struct got_error *
666 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
667 size_t maxlen)
669 char *p, *space;
670 const struct got_error *err = NULL;
672 *elen = 0;
674 *te = got_alloc_tree_entry_partial();
675 if (*te == NULL)
676 return got_error_from_errno("got_alloc_tree_entry_partial");
678 *elen = strnlen(buf, maxlen) + 1;
679 if (*elen > maxlen) {
680 free(*te);
681 *te = NULL;
682 return got_error(GOT_ERR_BAD_OBJ_DATA);
685 space = memchr(buf, ' ', *elen);
686 if (space == NULL || space <= buf) {
687 err = got_error(GOT_ERR_BAD_OBJ_DATA);
688 free(*te);
689 *te = NULL;
690 return err;
692 (*te)->mode = 0;
693 p = buf;
694 while (p < space) {
695 if (*p < '0' && *p > '7') {
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
697 goto done;
699 (*te)->mode <<= 3;
700 (*te)->mode |= *p - '0';
701 p++;
704 (*te)->name = strdup(space + 1);
705 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
706 err = got_error(GOT_ERR_BAD_OBJ_DATA);
707 goto done;
709 buf += *elen;
710 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
711 *elen += SHA1_DIGEST_LENGTH;
712 done:
713 if (err) {
714 got_object_tree_entry_close(*te);
715 *te = NULL;
717 return err;
720 const struct got_error *
721 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
723 const struct got_error *err;
724 size_t remain = len;
725 struct got_pathlist_head pathlist;
726 struct got_pathlist_entry *pe;
728 TAILQ_INIT(&pathlist);
730 *tree = calloc(1, sizeof(**tree));
731 if (*tree == NULL)
732 return got_error_from_errno("calloc");
734 SIMPLEQ_INIT(&(*tree)->entries.head);
736 if (remain == 0)
737 return NULL; /* tree is empty */
739 while (remain > 0) {
740 struct got_tree_entry *te;
741 struct got_pathlist_entry *new = NULL;
742 size_t elen;
744 err = parse_tree_entry(&te, &elen, buf, remain);
745 if (err)
746 goto done;
747 err = got_pathlist_insert(&new, &pathlist, te->name, te);
748 if (err)
749 goto done;
750 if (new == NULL) {
751 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
752 goto done;
754 buf += elen;
755 remain -= elen;
758 if (remain != 0) {
759 got_object_tree_close(*tree);
760 *tree = NULL;
761 err = got_error(GOT_ERR_BAD_OBJ_DATA);
762 goto done;
765 TAILQ_FOREACH(pe, &pathlist, entry) {
766 struct got_tree_entry *te = pe->data;
767 (*tree)->entries.nentries++;
768 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
770 done:
771 got_pathlist_free(&pathlist);
772 return err;
775 void
776 got_object_tag_close(struct got_tag_object *tag)
778 if (tag->refcnt > 0) {
779 tag->refcnt--;
780 if (tag->refcnt > 0)
781 return;
784 free(tag->tag);
785 free(tag->tagger);
786 free(tag->tagmsg);
787 free(tag);
790 const struct got_error *
791 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
793 const struct got_error *err = NULL;
794 size_t remain = len;
795 char *s = buf;
796 size_t label_len;
798 if (remain == 0)
799 return got_error(GOT_ERR_BAD_OBJ_DATA);
801 *tag = calloc(1, sizeof(**tag));
802 if (*tag == NULL)
803 return got_error_from_errno("calloc");
805 label_len = strlen(GOT_TAG_LABEL_OBJECT);
806 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
807 remain -= label_len;
808 if (remain < SHA1_DIGEST_STRING_LENGTH) {
809 err = got_error(GOT_ERR_BAD_OBJ_DATA);
810 goto done;
812 s += label_len;
813 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
814 err = got_error(GOT_ERR_BAD_OBJ_DATA);
815 goto done;
817 remain -= SHA1_DIGEST_STRING_LENGTH;
818 s += SHA1_DIGEST_STRING_LENGTH;
819 } else {
820 err = got_error(GOT_ERR_BAD_OBJ_DATA);
821 goto done;
824 if (remain <= 0) {
825 err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 goto done;
829 label_len = strlen(GOT_TAG_LABEL_TYPE);
830 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
831 remain -= label_len;
832 if (remain <= 0) {
833 err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 goto done;
836 s += label_len;
837 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
838 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
839 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
840 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
841 s += label_len;
842 remain -= label_len;
843 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
844 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
845 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
846 label_len = strlen(GOT_OBJ_LABEL_TREE);
847 s += label_len;
848 remain -= label_len;
849 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
850 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
851 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
852 label_len = strlen(GOT_OBJ_LABEL_BLOB);
853 s += label_len;
854 remain -= label_len;
855 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
856 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
857 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
858 label_len = strlen(GOT_OBJ_LABEL_TAG);
859 s += label_len;
860 remain -= label_len;
861 } else {
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
863 goto done;
866 if (remain <= 0 || *s != '\n') {
867 err = got_error(GOT_ERR_BAD_OBJ_DATA);
868 goto done;
870 s++;
871 remain--;
872 if (remain <= 0) {
873 err = got_error(GOT_ERR_BAD_OBJ_DATA);
874 goto done;
876 } else {
877 err = got_error(GOT_ERR_BAD_OBJ_DATA);
878 goto done;
881 label_len = strlen(GOT_TAG_LABEL_TAG);
882 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
883 char *p;
884 size_t slen;
885 remain -= label_len;
886 if (remain <= 0) {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
888 goto done;
890 s += label_len;
891 p = memchr(s, '\n', remain);
892 if (p == NULL) {
893 err = got_error(GOT_ERR_BAD_OBJ_DATA);
894 goto done;
896 *p = '\0';
897 slen = strlen(s);
898 (*tag)->tag = strndup(s, slen);
899 if ((*tag)->tag == NULL) {
900 err = got_error_from_errno("strndup");
901 goto done;
903 s += slen + 1;
904 remain -= slen + 1;
905 if (remain <= 0) {
906 err = got_error(GOT_ERR_BAD_OBJ_DATA);
907 goto done;
909 } else {
910 err = got_error(GOT_ERR_BAD_OBJ_DATA);
911 goto done;
914 label_len = strlen(GOT_TAG_LABEL_TAGGER);
915 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
916 char *p;
917 size_t slen;
919 remain -= label_len;
920 if (remain <= 0) {
921 err = got_error(GOT_ERR_BAD_OBJ_DATA);
922 goto done;
924 s += label_len;
925 p = memchr(s, '\n', remain);
926 if (p == NULL) {
927 err = got_error(GOT_ERR_BAD_OBJ_DATA);
928 goto done;
930 *p = '\0';
931 slen = strlen(s);
932 err = parse_commit_time(&(*tag)->tagger_time,
933 &(*tag)->tagger_gmtoff, s);
934 if (err)
935 goto done;
936 (*tag)->tagger = strdup(s);
937 if ((*tag)->tagger == NULL) {
938 err = got_error_from_errno("strdup");
939 goto done;
941 s += slen + 1;
942 remain -= slen + 1;
943 if (remain <= 0) {
944 err = got_error(GOT_ERR_BAD_OBJ_DATA);
945 goto done;
947 } else {
948 /* Some old tags in the Linux git repo have no tagger. */
949 (*tag)->tagger = strdup("");
950 if ((*tag)->tagger == NULL) {
951 err = got_error_from_errno("strdup");
952 goto done;
956 (*tag)->tagmsg = strndup(s, remain);
957 if ((*tag)->tagmsg == NULL) {
958 err = got_error_from_errno("strndup");
959 goto done;
961 done:
962 if (err) {
963 got_object_tag_close(*tag);
964 *tag = NULL;
966 return err;
969 const struct got_error *
970 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
972 const struct got_error *err = NULL;
973 static const size_t blocksize = 512;
974 size_t n, total, remain;
975 uint8_t *buf;
977 *outbuf = NULL;
978 *outlen = 0;
980 buf = malloc(blocksize);
981 if (buf == NULL)
982 return got_error_from_errno("malloc");
984 remain = blocksize;
985 total = 0;
986 for (;;) {
987 if (remain == 0) {
988 uint8_t *newbuf;
989 newbuf = reallocarray(buf, 1, total + blocksize);
990 if (newbuf == NULL) {
991 err = got_error_from_errno("reallocarray");
992 goto done;
994 buf = newbuf;
995 remain += blocksize;
997 n = fread(buf + total, 1, remain, f);
998 if (n == 0) {
999 if (ferror(f)) {
1000 err = got_ferror(f, GOT_ERR_IO);
1001 goto done;
1003 break; /* EOF */
1005 remain -= n;
1006 total += n;
1009 done:
1010 if (err == NULL) {
1011 *outbuf = buf;
1012 *outlen = total;
1013 } else
1014 free(buf);
1015 return err;