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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <sys/mman.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <zlib.h>
30 #include <ctype.h>
31 #include <limits.h>
32 #include <time.h>
33 #include <unistd.h>
35 #include "got_compat.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 }
92 (*qid)->data = NULL;
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 (!STAILQ_EMPTY(&obj->deltas.entries)) {
127 delta = STAILQ_FIRST(&obj->deltas.entries);
128 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
129 free(delta);
132 free(obj);
135 const struct got_error *
136 got_object_raw_close(struct got_raw_object *obj)
138 const struct got_error *err = NULL;
140 if (obj->refcnt > 0) {
141 obj->refcnt--;
142 if (obj->refcnt > 0)
143 return NULL;
146 if (obj->f == NULL) {
147 if (obj->fd != -1) {
148 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
149 err = got_error_from_errno("munmap");
150 if (close(obj->fd) == -1 && err == NULL)
151 err = got_error_from_errno("close");
152 } else
153 free(obj->data);
154 } else {
155 if (fclose(obj->f) == EOF && err == NULL)
156 err = got_error_from_errno("fclose");
158 free(obj);
159 return err;
162 void
163 got_object_qid_free(struct got_object_qid *qid)
165 free(qid->id);
166 free(qid);
169 void
170 got_object_id_queue_free(struct got_object_id_queue *ids)
172 struct got_object_qid *qid;
174 while (!STAILQ_EMPTY(ids)) {
175 qid = STAILQ_FIRST(ids);
176 STAILQ_REMOVE_HEAD(ids, entry);
177 got_object_qid_free(qid);
181 const struct got_error *
182 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
184 const char *obj_labels[] = {
185 GOT_OBJ_LABEL_COMMIT,
186 GOT_OBJ_LABEL_TREE,
187 GOT_OBJ_LABEL_BLOB,
188 GOT_OBJ_LABEL_TAG,
189 };
190 const int obj_types[] = {
191 GOT_OBJ_TYPE_COMMIT,
192 GOT_OBJ_TYPE_TREE,
193 GOT_OBJ_TYPE_BLOB,
194 GOT_OBJ_TYPE_TAG,
195 };
196 int type = 0;
197 size_t size = 0;
198 size_t i;
199 char *end;
201 *obj = NULL;
203 end = memchr(buf, '\0', len);
204 if (end == NULL)
205 return got_error(GOT_ERR_BAD_OBJ_HDR);
207 for (i = 0; i < nitems(obj_labels); i++) {
208 const char *label = obj_labels[i];
209 size_t label_len = strlen(label);
210 const char *errstr;
212 if (len <= label_len || buf + label_len >= end ||
213 strncmp(buf, label, label_len) != 0)
214 continue;
216 type = obj_types[i];
217 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
218 if (errstr != NULL)
219 return got_error(GOT_ERR_BAD_OBJ_HDR);
220 break;
223 if (type == 0)
224 return got_error(GOT_ERR_BAD_OBJ_HDR);
226 *obj = calloc(1, sizeof(**obj));
227 if (*obj == NULL)
228 return got_error_from_errno("calloc");
229 (*obj)->type = type;
230 (*obj)->hdrlen = end - buf + 1;
231 (*obj)->size = size;
232 return NULL;
235 const struct got_error *
236 got_object_read_header(struct got_object **obj, int fd)
238 const struct got_error *err;
239 struct got_inflate_buf zb;
240 uint8_t *buf;
241 const size_t zbsize = 64;
242 size_t outlen, totlen;
243 int nbuf = 1;
245 *obj = NULL;
247 buf = malloc(zbsize);
248 if (buf == NULL)
249 return got_error_from_errno("malloc");
250 buf[0] = '\0';
252 err = got_inflate_init(&zb, buf, zbsize, NULL);
253 if (err)
254 return err;
256 totlen = 0;
257 do {
258 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
259 if (err)
260 goto done;
261 if (outlen == 0)
262 break;
263 totlen += outlen;
264 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
265 uint8_t *newbuf;
266 nbuf++;
267 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
268 if (newbuf == NULL) {
269 err = got_error_from_errno("recallocarray");
270 goto done;
272 buf = newbuf;
273 zb.outbuf = newbuf + totlen;
274 zb.outlen = (nbuf * zbsize) - totlen;
276 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
278 err = got_object_parse_header(obj, buf, totlen);
279 done:
280 free(buf);
281 got_inflate_end(&zb);
282 return err;
285 struct got_commit_object *
286 got_object_commit_alloc_partial(void)
288 struct got_commit_object *commit;
290 commit = calloc(1, sizeof(*commit));
291 if (commit == NULL)
292 return NULL;
293 commit->tree_id = malloc(sizeof(*commit->tree_id));
294 if (commit->tree_id == NULL) {
295 free(commit);
296 return NULL;
299 STAILQ_INIT(&commit->parent_ids);
301 return commit;
304 const struct got_error *
305 got_object_commit_add_parent(struct got_commit_object *commit,
306 const char *id_str)
308 const struct got_error *err = NULL;
309 struct got_object_qid *qid;
311 err = got_object_qid_alloc_partial(&qid);
312 if (err)
313 return err;
315 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
316 err = got_error(GOT_ERR_BAD_OBJ_DATA);
317 got_object_qid_free(qid);
318 return err;
321 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
322 commit->nparents++;
324 return NULL;
327 static const struct got_error *
328 parse_gmtoff(time_t *gmtoff, const char *tzstr)
330 int sign = 1;
331 const char *p = tzstr;
332 time_t h, m;
334 *gmtoff = 0;
336 if (*p == '-')
337 sign = -1;
338 else if (*p != '+')
339 return got_error(GOT_ERR_BAD_OBJ_DATA);
340 p++;
341 if (!isdigit(*p) && !isdigit(*(p + 1)))
342 return got_error(GOT_ERR_BAD_OBJ_DATA);
343 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
345 p += 2;
346 if (!isdigit(*p) && !isdigit(*(p + 1)))
347 return got_error(GOT_ERR_BAD_OBJ_DATA);
348 m = ((*p - '0') * 10) + (*(p + 1) - '0');
350 *gmtoff = (h * 60 * 60 + m * 60) * sign;
351 return NULL;
354 static const struct got_error *
355 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
357 const struct got_error *err = NULL;
358 const char *errstr;
359 char *space, *tzstr;
361 /* Parse and strip off trailing timezone indicator string. */
362 space = strrchr(committer, ' ');
363 if (space == NULL)
364 return got_error(GOT_ERR_BAD_OBJ_DATA);
365 tzstr = strdup(space + 1);
366 if (tzstr == NULL)
367 return got_error_from_errno("strdup");
368 err = parse_gmtoff(gmtoff, tzstr);
369 free(tzstr);
370 if (err) {
371 if (err->code != GOT_ERR_BAD_OBJ_DATA)
372 return err;
373 /* Old versions of Git omitted the timestamp. */
374 *time = 0;
375 *gmtoff = 0;
376 return NULL;
378 *space = '\0';
380 /* Timestamp is separated from committer name + email by space. */
381 space = strrchr(committer, ' ');
382 if (space == NULL)
383 return got_error(GOT_ERR_BAD_OBJ_DATA);
385 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
386 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
387 if (errstr)
388 return got_error(GOT_ERR_BAD_OBJ_DATA);
390 /* Strip off parsed time information, leaving just author and email. */
391 *space = '\0';
393 return NULL;
396 void
397 got_object_commit_close(struct got_commit_object *commit)
399 if (commit->refcnt > 0) {
400 commit->refcnt--;
401 if (commit->refcnt > 0)
402 return;
405 got_object_id_queue_free(&commit->parent_ids);
406 free(commit->tree_id);
407 free(commit->author);
408 free(commit->committer);
409 free(commit->logmsg);
410 free(commit);
413 struct got_object_id *
414 got_object_commit_get_tree_id(struct got_commit_object *commit)
416 return commit->tree_id;
419 int
420 got_object_commit_get_nparents(struct got_commit_object *commit)
422 return commit->nparents;
425 const struct got_object_id_queue *
426 got_object_commit_get_parent_ids(struct got_commit_object *commit)
428 return &commit->parent_ids;
431 const char *
432 got_object_commit_get_author(struct got_commit_object *commit)
434 return commit->author;
437 time_t
438 got_object_commit_get_author_time(struct got_commit_object *commit)
440 return commit->author_time;
443 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
445 return commit->author_gmtoff;
448 const char *
449 got_object_commit_get_committer(struct got_commit_object *commit)
451 return commit->committer;
454 time_t
455 got_object_commit_get_committer_time(struct got_commit_object *commit)
457 return commit->committer_time;
460 time_t
461 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
463 return commit->committer_gmtoff;
466 const struct got_error *
467 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
469 const struct got_error *err = NULL;
470 const char *src;
471 char *dst;
472 size_t len;
474 len = strlen(commit->logmsg);
475 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
476 if (*logmsg == NULL)
477 return got_error_from_errno("malloc");
479 /*
480 * Strip out unusual headers. Headers are separated from the commit
481 * message body by a single empty line.
482 */
483 src = commit->logmsg;
484 dst = *logmsg;
485 while (*src != '\0' && *src != '\n') {
486 int copy_header = 1, eol = 0;
487 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
488 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
489 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
490 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
491 strncmp(src, GOT_COMMIT_LABEL_PARENT,
492 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
493 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
494 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
495 copy_header = 0;
497 while (*src != '\0' && !eol) {
498 if (copy_header) {
499 *dst = *src;
500 dst++;
502 if (*src == '\n')
503 eol = 1;
504 src++;
507 *dst = '\0';
509 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
510 err = got_error(GOT_ERR_NO_SPACE);
511 goto done;
514 /* Trim redundant trailing whitespace. */
515 len = strlen(*logmsg);
516 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
517 isspace((unsigned char)(*logmsg)[len - 1])) {
518 (*logmsg)[len - 1] = '\0';
519 len--;
522 /* Append a trailing newline if missing. */
523 if (len > 0 && (*logmsg)[len - 1] != '\n') {
524 (*logmsg)[len] = '\n';
525 (*logmsg)[len + 1] = '\0';
527 done:
528 if (err) {
529 free(*logmsg);
530 *logmsg = NULL;
532 return err;
535 const char *
536 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
538 return commit->logmsg;
541 const struct got_error *
542 got_object_parse_commit(struct got_commit_object **commit, char *buf,
543 size_t len)
545 const struct got_error *err = NULL;
546 char *s = buf;
547 size_t label_len;
548 ssize_t remain = (ssize_t)len;
550 if (remain == 0)
551 return got_error(GOT_ERR_BAD_OBJ_DATA);
553 *commit = got_object_commit_alloc_partial();
554 if (*commit == NULL)
555 return got_error_from_errno("got_object_commit_alloc_partial");
557 label_len = strlen(GOT_COMMIT_LABEL_TREE);
558 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
559 remain -= label_len;
560 if (remain < SHA1_DIGEST_STRING_LENGTH) {
561 err = got_error(GOT_ERR_BAD_OBJ_DATA);
562 goto done;
564 s += label_len;
565 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
566 err = got_error(GOT_ERR_BAD_OBJ_DATA);
567 goto done;
569 remain -= SHA1_DIGEST_STRING_LENGTH;
570 s += SHA1_DIGEST_STRING_LENGTH;
571 } else {
572 err = got_error(GOT_ERR_BAD_OBJ_DATA);
573 goto done;
576 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
577 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
578 remain -= label_len;
579 if (remain < SHA1_DIGEST_STRING_LENGTH) {
580 err = got_error(GOT_ERR_BAD_OBJ_DATA);
581 goto done;
583 s += label_len;
584 err = got_object_commit_add_parent(*commit, s);
585 if (err)
586 goto done;
588 remain -= SHA1_DIGEST_STRING_LENGTH;
589 s += SHA1_DIGEST_STRING_LENGTH;
592 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
593 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
594 char *p;
595 size_t slen;
597 remain -= label_len;
598 if (remain <= 0) {
599 err = got_error(GOT_ERR_BAD_OBJ_DATA);
600 goto done;
602 s += label_len;
603 p = memchr(s, '\n', remain);
604 if (p == NULL) {
605 err = got_error(GOT_ERR_BAD_OBJ_DATA);
606 goto done;
608 *p = '\0';
609 slen = strlen(s);
610 err = parse_commit_time(&(*commit)->author_time,
611 &(*commit)->author_gmtoff, s);
612 if (err)
613 goto done;
614 (*commit)->author = strdup(s);
615 if ((*commit)->author == NULL) {
616 err = got_error_from_errno("strdup");
617 goto done;
619 s += slen + 1;
620 remain -= slen + 1;
623 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
624 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
625 char *p;
626 size_t slen;
628 remain -= label_len;
629 if (remain <= 0) {
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
631 goto done;
633 s += label_len;
634 p = memchr(s, '\n', remain);
635 if (p == NULL) {
636 err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 goto done;
639 *p = '\0';
640 slen = strlen(s);
641 err = parse_commit_time(&(*commit)->committer_time,
642 &(*commit)->committer_gmtoff, s);
643 if (err)
644 goto done;
645 (*commit)->committer = strdup(s);
646 if ((*commit)->committer == NULL) {
647 err = got_error_from_errno("strdup");
648 goto done;
650 s += slen + 1;
651 remain -= slen + 1;
654 (*commit)->logmsg = strndup(s, remain);
655 if ((*commit)->logmsg == NULL) {
656 err = got_error_from_errno("strndup");
657 goto done;
659 done:
660 if (err) {
661 got_object_commit_close(*commit);
662 *commit = NULL;
664 return err;
667 void
668 got_object_tree_close(struct got_tree_object *tree)
670 if (tree->refcnt > 0) {
671 tree->refcnt--;
672 if (tree->refcnt > 0)
673 return;
676 free(tree->entries);
677 free(tree);
680 static const struct got_error *
681 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
682 size_t *elen, char *buf,
683 size_t maxlen)
685 char *p, *space;
686 const struct got_error *err = NULL;
688 *name = NULL;
689 *elen = 0;
691 *pte = malloc(sizeof(**pte));
692 if (*pte == NULL)
693 return got_error_from_errno("malloc");
695 *elen = strnlen(buf, maxlen) + 1;
696 if (*elen > maxlen) {
697 free(*pte);
698 *pte = NULL;
699 return got_error(GOT_ERR_BAD_OBJ_DATA);
702 space = memchr(buf, ' ', *elen);
703 if (space == NULL || space <= buf) {
704 err = got_error(GOT_ERR_BAD_OBJ_DATA);
705 free(*pte);
706 *pte = NULL;
707 return err;
709 (*pte)->mode = 0;
710 p = buf;
711 while (p < space) {
712 if (*p < '0' && *p > '7') {
713 err = got_error(GOT_ERR_BAD_OBJ_DATA);
714 goto done;
716 (*pte)->mode <<= 3;
717 (*pte)->mode |= *p - '0';
718 p++;
721 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
722 err = got_error(GOT_ERR_BAD_OBJ_DATA);
723 goto done;
725 *name = space + 1;
726 buf += *elen;
727 (*pte)->id = buf;
728 *elen += SHA1_DIGEST_LENGTH;
729 done:
730 if (err) {
731 free(*pte);
732 *pte = NULL;
734 return err;
737 const struct got_error *
738 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
739 uint8_t *buf, size_t len)
741 const struct got_error *err = NULL;
742 size_t remain = len;
744 *nentries = 0;
745 if (remain == 0)
746 return NULL; /* tree is empty */
748 while (remain > 0) {
749 struct got_parsed_tree_entry *pte;
750 struct got_pathlist_entry *new = NULL;
751 const char *name;
752 size_t elen;
754 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
755 if (err)
756 goto done;
757 err = got_pathlist_insert(&new, entries, name, pte);
758 if (err)
759 goto done;
760 if (new == NULL) {
761 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
762 goto done;
764 buf += elen;
765 remain -= elen;
766 (*nentries)++;
769 if (remain != 0) {
770 err = got_error(GOT_ERR_BAD_OBJ_DATA);
771 goto done;
773 done:
774 if (err) {
775 got_object_parsed_tree_entries_free(entries);
776 *nentries = 0;
778 return err;
781 void
782 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
784 struct got_pathlist_entry *pe;
786 TAILQ_FOREACH(pe, entries, entry) {
787 struct got_parsed_tree_entry *pte = pe->data;
788 free(pte);
790 got_pathlist_free(entries);
793 void
794 got_object_tag_close(struct got_tag_object *tag)
796 if (tag->refcnt > 0) {
797 tag->refcnt--;
798 if (tag->refcnt > 0)
799 return;
802 free(tag->tag);
803 free(tag->tagger);
804 free(tag->tagmsg);
805 free(tag);
808 const struct got_error *
809 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
811 const struct got_error *err = NULL;
812 size_t remain = len;
813 char *s = buf;
814 size_t label_len;
816 if (remain == 0)
817 return got_error(GOT_ERR_BAD_OBJ_DATA);
819 *tag = calloc(1, sizeof(**tag));
820 if (*tag == NULL)
821 return got_error_from_errno("calloc");
823 label_len = strlen(GOT_TAG_LABEL_OBJECT);
824 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
825 remain -= label_len;
826 if (remain < SHA1_DIGEST_STRING_LENGTH) {
827 err = got_error(GOT_ERR_BAD_OBJ_DATA);
828 goto done;
830 s += label_len;
831 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
832 err = got_error(GOT_ERR_BAD_OBJ_DATA);
833 goto done;
835 remain -= SHA1_DIGEST_STRING_LENGTH;
836 s += SHA1_DIGEST_STRING_LENGTH;
837 } else {
838 err = got_error(GOT_ERR_BAD_OBJ_DATA);
839 goto done;
842 if (remain <= 0) {
843 err = got_error(GOT_ERR_BAD_OBJ_DATA);
844 goto done;
847 label_len = strlen(GOT_TAG_LABEL_TYPE);
848 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
849 remain -= label_len;
850 if (remain <= 0) {
851 err = got_error(GOT_ERR_BAD_OBJ_DATA);
852 goto done;
854 s += label_len;
855 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
856 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
857 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
858 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
859 s += label_len;
860 remain -= label_len;
861 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
862 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
863 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
864 label_len = strlen(GOT_OBJ_LABEL_TREE);
865 s += label_len;
866 remain -= label_len;
867 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
868 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
869 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
870 label_len = strlen(GOT_OBJ_LABEL_BLOB);
871 s += label_len;
872 remain -= label_len;
873 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
874 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
875 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
876 label_len = strlen(GOT_OBJ_LABEL_TAG);
877 s += label_len;
878 remain -= label_len;
879 } else {
880 err = got_error(GOT_ERR_BAD_OBJ_DATA);
881 goto done;
884 if (remain <= 0 || *s != '\n') {
885 err = got_error(GOT_ERR_BAD_OBJ_DATA);
886 goto done;
888 s++;
889 remain--;
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_TAG);
900 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
901 char *p;
902 size_t slen;
903 remain -= label_len;
904 if (remain <= 0) {
905 err = got_error(GOT_ERR_BAD_OBJ_DATA);
906 goto done;
908 s += label_len;
909 p = memchr(s, '\n', remain);
910 if (p == NULL) {
911 err = got_error(GOT_ERR_BAD_OBJ_DATA);
912 goto done;
914 *p = '\0';
915 slen = strlen(s);
916 (*tag)->tag = strndup(s, slen);
917 if ((*tag)->tag == NULL) {
918 err = got_error_from_errno("strndup");
919 goto done;
921 s += slen + 1;
922 remain -= slen + 1;
923 if (remain <= 0) {
924 err = got_error(GOT_ERR_BAD_OBJ_DATA);
925 goto done;
927 } else {
928 err = got_error(GOT_ERR_BAD_OBJ_DATA);
929 goto done;
932 label_len = strlen(GOT_TAG_LABEL_TAGGER);
933 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
934 char *p;
935 size_t slen;
937 remain -= label_len;
938 if (remain <= 0) {
939 err = got_error(GOT_ERR_BAD_OBJ_DATA);
940 goto done;
942 s += label_len;
943 p = memchr(s, '\n', remain);
944 if (p == NULL) {
945 err = got_error(GOT_ERR_BAD_OBJ_DATA);
946 goto done;
948 *p = '\0';
949 slen = strlen(s);
950 err = parse_commit_time(&(*tag)->tagger_time,
951 &(*tag)->tagger_gmtoff, s);
952 if (err)
953 goto done;
954 (*tag)->tagger = strdup(s);
955 if ((*tag)->tagger == NULL) {
956 err = got_error_from_errno("strdup");
957 goto done;
959 s += slen + 1;
960 remain -= slen + 1;
961 if (remain < 0) {
962 err = got_error(GOT_ERR_BAD_OBJ_DATA);
963 goto done;
965 } else {
966 /* Some old tags in the Linux git repo have no tagger. */
967 (*tag)->tagger = strdup("");
968 if ((*tag)->tagger == NULL) {
969 err = got_error_from_errno("strdup");
970 goto done;
974 (*tag)->tagmsg = strndup(s, remain);
975 if ((*tag)->tagmsg == NULL) {
976 err = got_error_from_errno("strndup");
977 goto done;
979 done:
980 if (err) {
981 got_object_tag_close(*tag);
982 *tag = NULL;
984 return err;
987 const struct got_error *
988 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
990 const struct got_error *err = NULL;
991 static const size_t blocksize = 512;
992 size_t n, total, remain;
993 uint8_t *buf;
995 *outbuf = NULL;
996 *outlen = 0;
998 buf = malloc(blocksize);
999 if (buf == NULL)
1000 return got_error_from_errno("malloc");
1002 remain = blocksize;
1003 total = 0;
1004 for (;;) {
1005 if (remain == 0) {
1006 uint8_t *newbuf;
1007 newbuf = reallocarray(buf, 1, total + blocksize);
1008 if (newbuf == NULL) {
1009 err = got_error_from_errno("reallocarray");
1010 goto done;
1012 buf = newbuf;
1013 remain += blocksize;
1015 n = fread(buf + total, 1, remain, f);
1016 if (n == 0) {
1017 if (ferror(f)) {
1018 err = got_ferror(f, GOT_ERR_IO);
1019 goto done;
1021 break; /* EOF */
1023 remain -= n;
1024 total += n;
1027 done:
1028 if (err == NULL) {
1029 *outbuf = buf;
1030 *outlen = total;
1031 } else
1032 free(buf);
1033 return err;