2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <sys/socket.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
46 #include "got_lib_hash.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_repository.h"
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
67 memcpy(id2, id1, sizeof(*id2));
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
75 if (id1->algo != id2->algo)
78 if (id1->algo == GOT_HASH_SHA256)
79 return memcmp(id1->hash, id2->hash, SHA256_DIGEST_LENGTH);
80 if (id1->algo == GOT_HASH_SHA1)
81 return memcmp(id1->hash, id2->hash, SHA1_DIGEST_LENGTH);
85 const struct got_error *
86 got_object_qid_alloc_partial(struct got_object_qid **qid)
88 *qid = malloc(sizeof(**qid));
90 return got_error_from_errno("malloc");
96 const struct got_error *
97 got_object_id_str(char **outbuf, struct got_object_id *id)
99 static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN;
101 *outbuf = malloc(len);
103 return got_error_from_errno("malloc");
105 if (got_object_id_hex(id, *outbuf, len) == NULL) {
108 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
115 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
117 if (id->algo == GOT_HASH_SHA256)
118 return got_sha256_digest_to_str(id->hash, buf, len);
119 return got_sha1_digest_to_str(id->hash, buf, len);
123 got_object_close(struct got_object *obj)
125 if (obj->refcnt > 0) {
131 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
132 struct got_delta *delta;
133 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
134 delta = STAILQ_FIRST(&obj->deltas.entries);
135 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
142 const struct got_error *
143 got_object_raw_close(struct got_raw_object *obj)
145 const struct got_error *err = NULL;
147 if (obj->refcnt > 0) {
156 if (obj->f == NULL) {
158 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
159 err = got_error_from_errno("munmap");
160 if (close(obj->fd) == -1 && err == NULL)
161 err = got_error_from_errno("close");
165 if (fclose(obj->f) == EOF && err == NULL)
166 err = got_error_from_errno("fclose");
173 got_object_qid_free(struct got_object_qid *qid)
179 got_object_id_queue_free(struct got_object_id_queue *ids)
181 struct got_object_qid *qid;
183 while (!STAILQ_EMPTY(ids)) {
184 qid = STAILQ_FIRST(ids);
185 STAILQ_REMOVE_HEAD(ids, entry);
186 got_object_qid_free(qid);
190 const struct got_error *
191 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
193 const char *obj_labels[] = {
194 GOT_OBJ_LABEL_COMMIT,
199 const int obj_types[] = {
212 end = memchr(buf, '\0', len);
214 return got_error(GOT_ERR_BAD_OBJ_HDR);
216 for (i = 0; i < nitems(obj_labels); i++) {
217 const char *label = obj_labels[i];
218 size_t label_len = strlen(label);
221 if (len <= label_len || buf + label_len >= end ||
222 strncmp(buf, label, label_len) != 0)
226 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
228 return got_error(GOT_ERR_BAD_OBJ_HDR);
233 return got_error(GOT_ERR_BAD_OBJ_HDR);
235 *obj = calloc(1, sizeof(**obj));
237 return got_error_from_errno("calloc");
239 (*obj)->hdrlen = end - buf + 1;
244 const struct got_error *
245 got_object_read_header(struct got_object **obj, int fd)
247 const struct got_error *err;
248 struct got_inflate_buf zb;
250 const size_t zbsize = 64;
251 size_t outlen, totlen;
256 buf = malloc(zbsize);
258 return got_error_from_errno("malloc");
261 err = got_inflate_init(&zb, buf, zbsize, NULL);
267 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
273 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
276 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
277 if (newbuf == NULL) {
278 err = got_error_from_errno("recallocarray");
282 zb.outbuf = newbuf + totlen;
283 zb.outlen = (nbuf * zbsize) - totlen;
285 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
287 err = got_object_parse_header(obj, buf, totlen);
290 got_inflate_end(&zb);
294 const struct got_error *
295 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
296 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
299 const struct got_error *err = NULL;
300 struct got_object *obj;
301 struct got_inflate_checksum csum;
302 uint8_t sha256[SHA256_DIGEST_LENGTH];
304 uint8_t sha1[SHA1_DIGEST_LENGTH];
306 size_t len, consumed;
314 memset(&csum, 0, sizeof(csum));
315 if (expected_id->algo == GOT_HASH_SHA256) {
316 SHA256Init(&sha256_ctx);
317 csum.output_sha256 = &sha256_ctx;
320 csum.output_sha1 = &sha1_ctx;
323 if (lseek(infd, SEEK_SET, 0) == -1)
324 return got_error_from_errno("lseek");
326 err = got_object_read_header(&obj, infd);
330 if (lseek(infd, SEEK_SET, 0) == -1)
331 return got_error_from_errno("lseek");
333 if (obj->size + obj->hdrlen <= max_in_mem_size) {
334 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
335 obj->size + obj->hdrlen, infd);
339 * XXX This uses an extra file descriptor for no good reason.
340 * We should have got_inflate_fd_to_fd().
344 return got_error_from_errno("dup");
347 err = got_error_from_errno("fdopen");
352 err = got_inflate_to_fd(&len, f, &csum, outfd);
357 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
358 err = got_error(GOT_ERR_BAD_OBJ_HDR);
362 if (expected_id->algo == GOT_HASH_SHA256) {
363 SHA256Final(sha256, &sha256_ctx);
364 r = memcmp(expected_id->hash, sha256, sizeof(sha256));
366 SHA1Final(sha1, &sha1_ctx);
367 r = memcmp(expected_id->hash, sha1, sizeof(sha1));
371 err = got_error_checksum(expected_id);
376 *hdrlen = obj->hdrlen;
378 got_object_close(obj);
379 if (f && fclose(f) == EOF && err == NULL)
380 err = got_error_from_errno("fclose");
384 struct got_commit_object *
385 got_object_commit_alloc_partial(void)
387 struct got_commit_object *commit;
389 commit = calloc(1, sizeof(*commit));
392 commit->tree_id = malloc(sizeof(*commit->tree_id));
393 if (commit->tree_id == NULL) {
398 STAILQ_INIT(&commit->parent_ids);
403 const struct got_error *
404 got_object_commit_add_parent(struct got_commit_object *commit,
405 const char *id_str, int algo)
407 const struct got_error *err = NULL;
408 struct got_object_qid *qid;
410 err = got_object_qid_alloc_partial(&qid);
414 if (!got_parse_hash_digest(qid->id.hash, id_str, algo, NULL)) {
415 err = got_error(GOT_ERR_BAD_OBJ_DATA);
416 got_object_qid_free(qid);
421 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
427 static const struct got_error *
428 parse_gmtoff(time_t *gmtoff, const char *tzstr)
431 const char *p = tzstr;
439 return got_error(GOT_ERR_BAD_OBJ_DATA);
441 if (!isdigit((unsigned char)*p) &&
442 !isdigit((unsigned char)*(p + 1)))
443 return got_error(GOT_ERR_BAD_OBJ_DATA);
444 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
447 if (!isdigit((unsigned char)*p) &&
448 !isdigit((unsigned char)*(p + 1)))
449 return got_error(GOT_ERR_BAD_OBJ_DATA);
450 m = ((*p - '0') * 10) + (*(p + 1) - '0');
452 *gmtoff = (h * 60 * 60 + m * 60) * sign;
456 static const struct got_error *
457 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
459 const struct got_error *err = NULL;
463 /* Parse and strip off trailing timezone indicator string. */
464 space = strrchr(committer, ' ');
466 return got_error(GOT_ERR_BAD_OBJ_DATA);
467 tzstr = strdup(space + 1);
469 return got_error_from_errno("strdup");
470 err = parse_gmtoff(gmtoff, tzstr);
473 if (err->code != GOT_ERR_BAD_OBJ_DATA)
475 /* Old versions of Git omitted the timestamp. */
482 /* Timestamp is separated from committer name + email by space. */
483 space = strrchr(committer, ' ');
485 return got_error(GOT_ERR_BAD_OBJ_DATA);
487 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
488 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
490 return got_error(GOT_ERR_BAD_OBJ_DATA);
492 /* Strip off parsed time information, leaving just author and email. */
499 got_object_commit_close(struct got_commit_object *commit)
501 if (commit->refcnt > 0) {
503 if (commit->refcnt > 0)
507 got_object_id_queue_free(&commit->parent_ids);
508 free(commit->tree_id);
509 free(commit->author);
510 free(commit->committer);
511 free(commit->logmsg);
515 struct got_object_id *
516 got_object_commit_get_tree_id(struct got_commit_object *commit)
518 return commit->tree_id;
522 got_object_commit_get_nparents(struct got_commit_object *commit)
524 return commit->nparents;
527 const struct got_object_id_queue *
528 got_object_commit_get_parent_ids(struct got_commit_object *commit)
530 return &commit->parent_ids;
534 got_object_commit_get_author(struct got_commit_object *commit)
536 return commit->author;
540 got_object_commit_get_author_time(struct got_commit_object *commit)
542 return commit->author_time;
545 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
547 return commit->author_gmtoff;
551 got_object_commit_get_committer(struct got_commit_object *commit)
553 return commit->committer;
557 got_object_commit_get_committer_time(struct got_commit_object *commit)
559 return commit->committer_time;
563 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
565 return commit->committer_gmtoff;
568 const struct got_error *
569 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
571 const struct got_error *err = NULL;
576 len = strlen(commit->logmsg);
577 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
579 return got_error_from_errno("malloc");
582 * Strip out unusual headers. Headers are separated from the commit
583 * message body by a single empty line.
585 src = commit->logmsg;
587 while (*src != '\0' && *src != '\n') {
588 int copy_header = 1, eol = 0;
589 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
590 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
591 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
592 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
593 strncmp(src, GOT_COMMIT_LABEL_PARENT,
594 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
595 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
596 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
599 while (*src != '\0' && !eol) {
611 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
612 err = got_error(GOT_ERR_NO_SPACE);
616 /* Trim redundant trailing whitespace. */
617 len = strlen(*logmsg);
618 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
619 isspace((unsigned char)(*logmsg)[len - 1])) {
620 (*logmsg)[len - 1] = '\0';
624 /* Append a trailing newline if missing. */
625 if (len > 0 && (*logmsg)[len - 1] != '\n') {
626 (*logmsg)[len] = '\n';
627 (*logmsg)[len + 1] = '\0';
638 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
640 return commit->logmsg;
643 const struct got_error *
644 got_object_parse_commit(struct got_commit_object **commit, char *buf,
645 size_t len, int algo)
647 const struct got_error *err = NULL;
650 size_t digest_strlen;
651 ssize_t remain = (ssize_t)len;
653 if (algo == GOT_HASH_SHA256)
654 digest_strlen = SHA256_DIGEST_STRING_LENGTH;
656 digest_strlen = SHA1_DIGEST_STRING_LENGTH;
659 return got_error(GOT_ERR_BAD_OBJ_DATA);
661 *commit = got_object_commit_alloc_partial();
663 return got_error_from_errno("got_object_commit_alloc_partial");
665 label_len = strlen(GOT_COMMIT_LABEL_TREE);
666 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
668 if (remain < digest_strlen) {
669 err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 if (!got_parse_hash_digest((*commit)->tree_id->hash, s,
675 err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 (*commit)->tree_id->algo = algo;
679 remain -= digest_strlen;
682 err = got_error(GOT_ERR_BAD_OBJ_DATA);
686 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
687 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
689 if (remain < digest_strlen) {
690 err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 err = got_object_commit_add_parent(*commit, s, algo);
698 remain -= digest_strlen;
702 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
703 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
709 err = got_error(GOT_ERR_BAD_OBJ_DATA);
713 p = memchr(s, '\n', remain);
715 err = got_error(GOT_ERR_BAD_OBJ_DATA);
720 err = parse_commit_time(&(*commit)->author_time,
721 &(*commit)->author_gmtoff, s);
724 (*commit)->author = strdup(s);
725 if ((*commit)->author == NULL) {
726 err = got_error_from_errno("strdup");
733 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
734 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
744 p = memchr(s, '\n', remain);
746 err = got_error(GOT_ERR_BAD_OBJ_DATA);
751 err = parse_commit_time(&(*commit)->committer_time,
752 &(*commit)->committer_gmtoff, s);
755 (*commit)->committer = strdup(s);
756 if ((*commit)->committer == NULL) {
757 err = got_error_from_errno("strdup");
764 (*commit)->logmsg = strndup(s, remain);
765 if ((*commit)->logmsg == NULL) {
766 err = got_error_from_errno("strndup");
771 got_object_commit_close(*commit);
777 const struct got_error *
778 got_object_read_commit(struct got_commit_object **commit, int fd,
779 struct got_object_id *expected_id, size_t expected_size)
781 struct got_object *obj = NULL;
782 const struct got_error *err = NULL;
785 struct got_inflate_checksum csum;
788 struct got_object_id id;
790 memset(&csum, 0, sizeof(csum));
791 id.algo = expected_id->algo;
792 if (expected_id->algo == GOT_HASH_SHA256) {
793 SHA256Init(&sha256_ctx);
794 csum.output_sha256 = &sha256_ctx;
797 csum.output_sha1 = &sha1_ctx;
800 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
804 if (expected_id->algo == GOT_HASH_SHA256)
805 SHA256Final(id.hash, &sha256_ctx);
807 SHA1Final(id.hash, &sha1_ctx);
809 if (got_object_id_cmp(expected_id, &id) != 0) {
810 err = got_error_checksum(expected_id);
814 err = got_object_parse_header(&obj, p, len);
818 if (len < obj->hdrlen + obj->size) {
819 err = got_error(GOT_ERR_BAD_OBJ_DATA);
823 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
824 err = got_error(GOT_ERR_OBJ_TYPE);
828 /* Skip object header. */
830 err = got_object_parse_commit(commit, p + obj->hdrlen, len,
835 got_object_close(obj);
840 got_object_tree_close(struct got_tree_object *tree)
842 if (tree->refcnt > 0) {
844 if (tree->refcnt > 0)
852 static const struct got_error *
853 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
854 size_t maxlen, size_t idlen)
860 *elen = strnlen(buf, maxlen) + 1;
862 return got_error(GOT_ERR_BAD_OBJ_DATA);
864 space = memchr(buf, ' ', *elen);
865 if (space == NULL || space <= buf)
866 return got_error(GOT_ERR_BAD_OBJ_DATA);
871 if (*p < '0' || *p > '7')
872 return got_error(GOT_ERR_BAD_OBJ_DATA);
874 pte->mode |= *p - '0';
878 if (*elen > maxlen || maxlen - *elen < idlen)
879 return got_error(GOT_ERR_BAD_OBJ_DATA);
881 pte->name = space + 1;
882 pte->namelen = strlen(pte->name);
891 pte_cmp(const void *pa, const void *pb)
893 const struct got_parsed_tree_entry *a = pa, *b = pb;
895 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
898 const struct got_error *
899 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
900 size_t *nentries_alloc, uint8_t *buf, size_t len, int algo)
902 const struct got_error *err = NULL;
904 const size_t nalloc = 16;
905 struct got_parsed_tree_entry *pte;
906 size_t idlen = SHA256_DIGEST_LENGTH;
909 if (algo != GOT_HASH_SHA256)
910 idlen = SHA1_DIGEST_LENGTH;
914 return NULL; /* tree is empty */
919 if (*nentries >= *nentries_alloc) {
920 pte = recallocarray(*entries, *nentries_alloc,
921 *nentries_alloc + nalloc, sizeof(**entries));
923 err = got_error_from_errno("recallocarray");
927 *nentries_alloc += nalloc;
930 pte = &(*entries)[*nentries];
931 err = parse_tree_entry(pte, &elen, buf, remain, idlen);
940 err = got_error(GOT_ERR_BAD_OBJ_DATA);
945 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
947 for (i = 0; i < *nentries - 1; i++) {
948 struct got_parsed_tree_entry *prev = &(*entries)[i];
949 pte = &(*entries)[i + 1];
950 if (got_path_cmp(prev->name, pte->name,
951 prev->namelen, pte->namelen) == 0) {
952 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
963 const struct got_error *
964 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
965 size_t *nentries_alloc, uint8_t **p, int fd,
966 struct got_object_id *expected_id)
968 const struct got_error *err = NULL;
969 struct got_object *obj = NULL;
971 struct got_inflate_checksum csum;
974 struct got_object_id id;
976 memset(&csum, 0, sizeof(csum));
977 id.algo = expected_id->algo;
978 if (expected_id->algo == GOT_HASH_SHA256) {
979 SHA256Init(&sha256_ctx);
980 csum.output_sha256 = &sha256_ctx;
983 csum.output_sha1 = &sha1_ctx;
986 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
990 if (expected_id->algo == GOT_HASH_SHA256)
991 SHA256Final(id.hash, &sha256_ctx);
993 SHA1Final(id.hash, &sha1_ctx);
994 if (got_object_id_cmp(expected_id, &id) != 0) {
995 err = got_error_checksum(expected_id);
999 err = got_object_parse_header(&obj, *p, len);
1003 if (len < obj->hdrlen + obj->size) {
1004 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1008 /* Skip object header. */
1010 err = got_object_parse_tree(entries, nentries, nentries_alloc,
1011 *p + obj->hdrlen, len, expected_id->algo);
1014 got_object_close(obj);
1019 got_object_tag_close(struct got_tag_object *tag)
1021 if (tag->refcnt > 0) {
1023 if (tag->refcnt > 0)
1033 const struct got_error *
1034 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len,
1037 const struct got_error *err = NULL;
1038 size_t remain = len;
1040 size_t label_len, digest_strlen = SHA256_DIGEST_STRING_LENGTH;
1042 if (algo != GOT_HASH_SHA256)
1043 digest_strlen = SHA1_DIGEST_STRING_LENGTH;
1046 return got_error(GOT_ERR_BAD_OBJ_DATA);
1048 *tag = calloc(1, sizeof(**tag));
1050 return got_error_from_errno("calloc");
1052 label_len = strlen(GOT_TAG_LABEL_OBJECT);
1053 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
1054 remain -= label_len;
1055 if (remain < digest_strlen) {
1056 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1060 if (!got_parse_hash_digest((*tag)->id.hash, s, algo, NULL)) {
1061 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1064 (*tag)->id.algo = algo;
1065 remain -= digest_strlen;
1068 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1073 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1077 label_len = strlen(GOT_TAG_LABEL_TYPE);
1078 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1079 remain -= label_len;
1081 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1085 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1086 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1087 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1088 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1090 remain -= label_len;
1091 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1092 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1093 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1094 label_len = strlen(GOT_OBJ_LABEL_TREE);
1096 remain -= label_len;
1097 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1098 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1099 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1100 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1102 remain -= label_len;
1103 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1104 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1105 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1106 label_len = strlen(GOT_OBJ_LABEL_TAG);
1108 remain -= label_len;
1110 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1114 if (remain <= 0 || *s != '\n') {
1115 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1121 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1125 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1129 label_len = strlen(GOT_TAG_LABEL_TAG);
1130 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1133 remain -= label_len;
1135 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1139 p = memchr(s, '\n', remain);
1141 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1146 (*tag)->tag = strndup(s, slen);
1147 if ((*tag)->tag == NULL) {
1148 err = got_error_from_errno("strndup");
1154 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1158 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1162 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1163 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1167 remain -= label_len;
1169 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1173 p = memchr(s, '\n', remain);
1175 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1180 err = parse_commit_time(&(*tag)->tagger_time,
1181 &(*tag)->tagger_gmtoff, s);
1184 (*tag)->tagger = strdup(s);
1185 if ((*tag)->tagger == NULL) {
1186 err = got_error_from_errno("strdup");
1192 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1196 /* Some old tags in the Linux git repo have no tagger. */
1197 (*tag)->tagger = strdup("");
1198 if ((*tag)->tagger == NULL) {
1199 err = got_error_from_errno("strdup");
1204 (*tag)->tagmsg = strndup(s, remain);
1205 if ((*tag)->tagmsg == NULL) {
1206 err = got_error_from_errno("strndup");
1211 got_object_tag_close(*tag);
1217 const struct got_error *
1218 got_object_read_tag(struct got_tag_object **tag, int fd,
1219 struct got_object_id *expected_id, size_t expected_size)
1221 const struct got_error *err = NULL;
1222 struct got_object *obj = NULL;
1225 struct got_inflate_checksum csum;
1226 SHA2_CTX sha256_ctx;
1228 struct got_object_id id;
1230 memset(&csum, 0, sizeof(csum));
1231 id.algo = expected_id->algo;
1232 if (expected_id->algo == GOT_HASH_SHA256) {
1233 SHA256Init(&sha256_ctx);
1234 csum.output_sha256 = &sha256_ctx;
1236 SHA1Init(&sha1_ctx);
1237 csum.output_sha1 = &sha1_ctx;
1240 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1245 if (expected_id->algo == GOT_HASH_SHA256)
1246 SHA256Final(id.hash, &sha256_ctx);
1248 SHA1Final(id.hash, &sha1_ctx);
1249 if (got_object_id_cmp(expected_id, &id) != 0) {
1250 err = got_error_checksum(expected_id);
1254 err = got_object_parse_header(&obj, p, len);
1258 if (len < obj->hdrlen + obj->size) {
1259 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1263 /* Skip object header. */
1265 err = got_object_parse_tag(tag, p + obj->hdrlen, len, expected_id->algo);
1269 got_object_close(obj);
1273 const struct got_error *
1274 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1276 const struct got_error *err = NULL;
1277 static const size_t blocksize = 512;
1278 size_t n, total, remain;
1284 buf = malloc(blocksize);
1286 return got_error_from_errno("malloc");
1293 newbuf = reallocarray(buf, 1, total + blocksize);
1294 if (newbuf == NULL) {
1295 err = got_error_from_errno("reallocarray");
1299 remain += blocksize;
1301 n = fread(buf + total, 1, remain, f);
1304 err = got_ferror(f, GOT_ERR_IO);