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 return got_sha1_digest_to_str(id->sha1, buf, len);
121 got_object_close(struct got_object *obj)
123 if (obj->refcnt > 0) {
129 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
130 struct got_delta *delta;
131 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
132 delta = STAILQ_FIRST(&obj->deltas.entries);
133 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
140 const struct got_error *
141 got_object_raw_close(struct got_raw_object *obj)
143 const struct got_error *err = NULL;
145 if (obj->refcnt > 0) {
154 if (obj->f == NULL) {
156 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
157 err = got_error_from_errno("munmap");
158 if (close(obj->fd) == -1 && err == NULL)
159 err = got_error_from_errno("close");
163 if (fclose(obj->f) == EOF && err == NULL)
164 err = got_error_from_errno("fclose");
171 got_object_qid_free(struct got_object_qid *qid)
177 got_object_id_queue_free(struct got_object_id_queue *ids)
179 struct got_object_qid *qid;
181 while (!STAILQ_EMPTY(ids)) {
182 qid = STAILQ_FIRST(ids);
183 STAILQ_REMOVE_HEAD(ids, entry);
184 got_object_qid_free(qid);
188 const struct got_error *
189 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
191 const char *obj_labels[] = {
192 GOT_OBJ_LABEL_COMMIT,
197 const int obj_types[] = {
210 end = memchr(buf, '\0', len);
212 return got_error(GOT_ERR_BAD_OBJ_HDR);
214 for (i = 0; i < nitems(obj_labels); i++) {
215 const char *label = obj_labels[i];
216 size_t label_len = strlen(label);
219 if (len <= label_len || buf + label_len >= end ||
220 strncmp(buf, label, label_len) != 0)
224 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
226 return got_error(GOT_ERR_BAD_OBJ_HDR);
231 return got_error(GOT_ERR_BAD_OBJ_HDR);
233 *obj = calloc(1, sizeof(**obj));
235 return got_error_from_errno("calloc");
237 (*obj)->hdrlen = end - buf + 1;
242 const struct got_error *
243 got_object_read_header(struct got_object **obj, int fd)
245 const struct got_error *err;
246 struct got_inflate_buf zb;
248 const size_t zbsize = 64;
249 size_t outlen, totlen;
254 buf = malloc(zbsize);
256 return got_error_from_errno("malloc");
259 err = got_inflate_init(&zb, buf, zbsize, NULL);
265 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
271 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
274 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
275 if (newbuf == NULL) {
276 err = got_error_from_errno("recallocarray");
280 zb.outbuf = newbuf + totlen;
281 zb.outlen = (nbuf * zbsize) - totlen;
283 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
285 err = got_object_parse_header(obj, buf, totlen);
288 got_inflate_end(&zb);
292 const struct got_error *
293 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
294 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
297 const struct got_error *err = NULL;
298 struct got_object *obj;
299 struct got_inflate_checksum csum;
300 uint8_t sha256[SHA256_DIGEST_LENGTH];
302 uint8_t sha1[SHA1_DIGEST_LENGTH];
304 size_t len, consumed;
312 memset(&csum, 0, sizeof(csum));
313 if (expected_id->algo == GOT_HASH_SHA256) {
314 SHA256Init(&sha256_ctx);
315 csum.output_sha256 = &sha256_ctx;
318 csum.output_sha1 = &sha1_ctx;
321 if (lseek(infd, SEEK_SET, 0) == -1)
322 return got_error_from_errno("lseek");
324 err = got_object_read_header(&obj, infd);
328 if (lseek(infd, SEEK_SET, 0) == -1)
329 return got_error_from_errno("lseek");
331 if (obj->size + obj->hdrlen <= max_in_mem_size) {
332 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
333 obj->size + obj->hdrlen, infd);
337 * XXX This uses an extra file descriptor for no good reason.
338 * We should have got_inflate_fd_to_fd().
342 return got_error_from_errno("dup");
345 err = got_error_from_errno("fdopen");
350 err = got_inflate_to_fd(&len, f, &csum, outfd);
355 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
356 err = got_error(GOT_ERR_BAD_OBJ_HDR);
360 if (expected_id->algo == GOT_HASH_SHA256) {
361 SHA256Final(sha256, &sha256_ctx);
362 r = memcmp(expected_id->hash, sha256, sizeof(sha256));
364 SHA1Final(sha1, &sha1_ctx);
365 r = memcmp(expected_id->hash, sha1, sizeof(sha1));
369 err = got_error_checksum(expected_id);
374 *hdrlen = obj->hdrlen;
376 got_object_close(obj);
377 if (f && fclose(f) == EOF && err == NULL)
378 err = got_error_from_errno("fclose");
382 struct got_commit_object *
383 got_object_commit_alloc_partial(void)
385 struct got_commit_object *commit;
387 commit = calloc(1, sizeof(*commit));
390 commit->tree_id = malloc(sizeof(*commit->tree_id));
391 if (commit->tree_id == NULL) {
396 STAILQ_INIT(&commit->parent_ids);
401 const struct got_error *
402 got_object_commit_add_parent(struct got_commit_object *commit,
403 const char *id_str, int algo)
405 const struct got_error *err = NULL;
406 struct got_object_qid *qid;
408 err = got_object_qid_alloc_partial(&qid);
412 if (!got_parse_hash_digest(qid->id.hash, id_str, algo, NULL)) {
413 err = got_error(GOT_ERR_BAD_OBJ_DATA);
414 got_object_qid_free(qid);
419 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
425 static const struct got_error *
426 parse_gmtoff(time_t *gmtoff, const char *tzstr)
429 const char *p = tzstr;
437 return got_error(GOT_ERR_BAD_OBJ_DATA);
439 if (!isdigit((unsigned char)*p) &&
440 !isdigit((unsigned char)*(p + 1)))
441 return got_error(GOT_ERR_BAD_OBJ_DATA);
442 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
445 if (!isdigit((unsigned char)*p) &&
446 !isdigit((unsigned char)*(p + 1)))
447 return got_error(GOT_ERR_BAD_OBJ_DATA);
448 m = ((*p - '0') * 10) + (*(p + 1) - '0');
450 *gmtoff = (h * 60 * 60 + m * 60) * sign;
454 static const struct got_error *
455 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
457 const struct got_error *err = NULL;
461 /* Parse and strip off trailing timezone indicator string. */
462 space = strrchr(committer, ' ');
464 return got_error(GOT_ERR_BAD_OBJ_DATA);
465 tzstr = strdup(space + 1);
467 return got_error_from_errno("strdup");
468 err = parse_gmtoff(gmtoff, tzstr);
471 if (err->code != GOT_ERR_BAD_OBJ_DATA)
473 /* Old versions of Git omitted the timestamp. */
480 /* Timestamp is separated from committer name + email by space. */
481 space = strrchr(committer, ' ');
483 return got_error(GOT_ERR_BAD_OBJ_DATA);
485 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
486 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
488 return got_error(GOT_ERR_BAD_OBJ_DATA);
490 /* Strip off parsed time information, leaving just author and email. */
497 got_object_commit_close(struct got_commit_object *commit)
499 if (commit->refcnt > 0) {
501 if (commit->refcnt > 0)
505 got_object_id_queue_free(&commit->parent_ids);
506 free(commit->tree_id);
507 free(commit->author);
508 free(commit->committer);
509 free(commit->logmsg);
513 struct got_object_id *
514 got_object_commit_get_tree_id(struct got_commit_object *commit)
516 return commit->tree_id;
520 got_object_commit_get_nparents(struct got_commit_object *commit)
522 return commit->nparents;
525 const struct got_object_id_queue *
526 got_object_commit_get_parent_ids(struct got_commit_object *commit)
528 return &commit->parent_ids;
532 got_object_commit_get_author(struct got_commit_object *commit)
534 return commit->author;
538 got_object_commit_get_author_time(struct got_commit_object *commit)
540 return commit->author_time;
543 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
545 return commit->author_gmtoff;
549 got_object_commit_get_committer(struct got_commit_object *commit)
551 return commit->committer;
555 got_object_commit_get_committer_time(struct got_commit_object *commit)
557 return commit->committer_time;
561 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
563 return commit->committer_gmtoff;
566 const struct got_error *
567 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
569 const struct got_error *err = NULL;
574 len = strlen(commit->logmsg);
575 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
577 return got_error_from_errno("malloc");
580 * Strip out unusual headers. Headers are separated from the commit
581 * message body by a single empty line.
583 src = commit->logmsg;
585 while (*src != '\0' && *src != '\n') {
586 int copy_header = 1, eol = 0;
587 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
588 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
589 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
590 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
591 strncmp(src, GOT_COMMIT_LABEL_PARENT,
592 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
593 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
594 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
597 while (*src != '\0' && !eol) {
609 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
610 err = got_error(GOT_ERR_NO_SPACE);
614 /* Trim redundant trailing whitespace. */
615 len = strlen(*logmsg);
616 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
617 isspace((unsigned char)(*logmsg)[len - 1])) {
618 (*logmsg)[len - 1] = '\0';
622 /* Append a trailing newline if missing. */
623 if (len > 0 && (*logmsg)[len - 1] != '\n') {
624 (*logmsg)[len] = '\n';
625 (*logmsg)[len + 1] = '\0';
636 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
638 return commit->logmsg;
641 const struct got_error *
642 got_object_parse_commit(struct got_commit_object **commit, char *buf,
643 size_t len, int algo)
645 const struct got_error *err = NULL;
648 size_t digest_strlen;
649 ssize_t remain = (ssize_t)len;
651 if (algo == GOT_HASH_SHA256)
652 digest_strlen = SHA256_DIGEST_STRING_LENGTH;
654 digest_strlen = SHA1_DIGEST_STRING_LENGTH;
657 return got_error(GOT_ERR_BAD_OBJ_DATA);
659 *commit = got_object_commit_alloc_partial();
661 return got_error_from_errno("got_object_commit_alloc_partial");
663 label_len = strlen(GOT_COMMIT_LABEL_TREE);
664 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
666 if (remain < digest_strlen) {
667 err = got_error(GOT_ERR_BAD_OBJ_DATA);
671 if (!got_parse_hash_digest((*commit)->tree_id->hash, s,
673 err = got_error(GOT_ERR_BAD_OBJ_DATA);
676 (*commit)->tree_id->algo = algo;
677 remain -= digest_strlen;
680 err = got_error(GOT_ERR_BAD_OBJ_DATA);
684 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
685 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
687 if (remain < digest_strlen) {
688 err = got_error(GOT_ERR_BAD_OBJ_DATA);
692 err = got_object_commit_add_parent(*commit, s, algo);
696 remain -= digest_strlen;
700 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
701 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
707 err = got_error(GOT_ERR_BAD_OBJ_DATA);
711 p = memchr(s, '\n', remain);
713 err = got_error(GOT_ERR_BAD_OBJ_DATA);
718 err = parse_commit_time(&(*commit)->author_time,
719 &(*commit)->author_gmtoff, s);
722 (*commit)->author = strdup(s);
723 if ((*commit)->author == NULL) {
724 err = got_error_from_errno("strdup");
731 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
732 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
738 err = got_error(GOT_ERR_BAD_OBJ_DATA);
742 p = memchr(s, '\n', remain);
744 err = got_error(GOT_ERR_BAD_OBJ_DATA);
749 err = parse_commit_time(&(*commit)->committer_time,
750 &(*commit)->committer_gmtoff, s);
753 (*commit)->committer = strdup(s);
754 if ((*commit)->committer == NULL) {
755 err = got_error_from_errno("strdup");
762 (*commit)->logmsg = strndup(s, remain);
763 if ((*commit)->logmsg == NULL) {
764 err = got_error_from_errno("strndup");
769 got_object_commit_close(*commit);
775 const struct got_error *
776 got_object_read_commit(struct got_commit_object **commit, int fd,
777 struct got_object_id *expected_id, size_t expected_size)
779 struct got_object *obj = NULL;
780 const struct got_error *err = NULL;
783 struct got_inflate_checksum csum;
786 struct got_object_id id;
788 memset(&csum, 0, sizeof(csum));
789 id.algo = expected_id->algo;
790 if (expected_id->algo == GOT_HASH_SHA256) {
791 SHA256Init(&sha256_ctx);
792 csum.output_sha256 = &sha256_ctx;
795 csum.output_sha1 = &sha1_ctx;
798 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
802 if (expected_id->algo == GOT_HASH_SHA256)
803 SHA256Final(id.hash, &sha256_ctx);
805 SHA1Final(id.hash, &sha1_ctx);
807 if (got_object_id_cmp(expected_id, &id) != 0) {
808 err = got_error_checksum(expected_id);
812 err = got_object_parse_header(&obj, p, len);
816 if (len < obj->hdrlen + obj->size) {
817 err = got_error(GOT_ERR_BAD_OBJ_DATA);
821 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
822 err = got_error(GOT_ERR_OBJ_TYPE);
826 /* Skip object header. */
828 err = got_object_parse_commit(commit, p + obj->hdrlen, len,
833 got_object_close(obj);
838 got_object_tree_close(struct got_tree_object *tree)
840 if (tree->refcnt > 0) {
842 if (tree->refcnt > 0)
850 static const struct got_error *
851 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
852 size_t maxlen, size_t idlen)
858 *elen = strnlen(buf, maxlen) + 1;
860 return got_error(GOT_ERR_BAD_OBJ_DATA);
862 space = memchr(buf, ' ', *elen);
863 if (space == NULL || space <= buf)
864 return got_error(GOT_ERR_BAD_OBJ_DATA);
869 if (*p < '0' || *p > '7')
870 return got_error(GOT_ERR_BAD_OBJ_DATA);
872 pte->mode |= *p - '0';
876 if (*elen > maxlen || maxlen - *elen < idlen)
877 return got_error(GOT_ERR_BAD_OBJ_DATA);
879 pte->name = space + 1;
880 pte->namelen = strlen(pte->name);
889 pte_cmp(const void *pa, const void *pb)
891 const struct got_parsed_tree_entry *a = pa, *b = pb;
893 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
896 const struct got_error *
897 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
898 size_t *nentries_alloc, uint8_t *buf, size_t len, int algo)
900 const struct got_error *err = NULL;
902 const size_t nalloc = 16;
903 struct got_parsed_tree_entry *pte;
904 size_t idlen = SHA256_DIGEST_LENGTH;
907 if (algo != GOT_HASH_SHA256)
908 idlen = SHA1_DIGEST_LENGTH;
912 return NULL; /* tree is empty */
917 if (*nentries >= *nentries_alloc) {
918 pte = recallocarray(*entries, *nentries_alloc,
919 *nentries_alloc + nalloc, sizeof(**entries));
921 err = got_error_from_errno("recallocarray");
925 *nentries_alloc += nalloc;
928 pte = &(*entries)[*nentries];
929 err = parse_tree_entry(pte, &elen, buf, remain, idlen);
938 err = got_error(GOT_ERR_BAD_OBJ_DATA);
943 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
945 for (i = 0; i < *nentries - 1; i++) {
946 struct got_parsed_tree_entry *prev = &(*entries)[i];
947 pte = &(*entries)[i + 1];
948 if (got_path_cmp(prev->name, pte->name,
949 prev->namelen, pte->namelen) == 0) {
950 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
961 const struct got_error *
962 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
963 size_t *nentries_alloc, uint8_t **p, int fd,
964 struct got_object_id *expected_id)
966 const struct got_error *err = NULL;
967 struct got_object *obj = NULL;
969 struct got_inflate_checksum csum;
972 struct got_object_id id;
974 memset(&csum, 0, sizeof(csum));
975 id.algo = expected_id->algo;
976 if (expected_id->algo == GOT_HASH_SHA256) {
977 SHA256Init(&sha256_ctx);
978 csum.output_sha256 = &sha256_ctx;
981 csum.output_sha1 = &sha1_ctx;
984 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
988 if (expected_id->algo == GOT_HASH_SHA256)
989 SHA256Final(id.hash, &sha256_ctx);
991 SHA1Final(id.hash, &sha1_ctx);
992 if (got_object_id_cmp(expected_id, &id) != 0) {
993 err = got_error_checksum(expected_id);
997 err = got_object_parse_header(&obj, *p, len);
1001 if (len < obj->hdrlen + obj->size) {
1002 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1006 /* Skip object header. */
1008 err = got_object_parse_tree(entries, nentries, nentries_alloc,
1009 *p + obj->hdrlen, len, expected_id->algo);
1012 got_object_close(obj);
1017 got_object_tag_close(struct got_tag_object *tag)
1019 if (tag->refcnt > 0) {
1021 if (tag->refcnt > 0)
1031 const struct got_error *
1032 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len,
1035 const struct got_error *err = NULL;
1036 size_t remain = len;
1038 size_t label_len, digest_strlen = SHA256_DIGEST_STRING_LENGTH;
1040 if (algo != GOT_HASH_SHA256)
1041 digest_strlen = SHA1_DIGEST_STRING_LENGTH;
1044 return got_error(GOT_ERR_BAD_OBJ_DATA);
1046 *tag = calloc(1, sizeof(**tag));
1048 return got_error_from_errno("calloc");
1050 label_len = strlen(GOT_TAG_LABEL_OBJECT);
1051 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
1052 remain -= label_len;
1053 if (remain < digest_strlen) {
1054 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1058 if (!got_parse_hash_digest((*tag)->id.hash, s, algo, NULL)) {
1059 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 (*tag)->id.algo = algo;
1063 remain -= digest_strlen;
1066 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1071 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1075 label_len = strlen(GOT_TAG_LABEL_TYPE);
1076 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1077 remain -= label_len;
1079 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1083 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1084 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1085 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1086 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1088 remain -= label_len;
1089 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1090 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1091 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1092 label_len = strlen(GOT_OBJ_LABEL_TREE);
1094 remain -= label_len;
1095 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1096 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1097 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1098 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1100 remain -= label_len;
1101 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1102 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1103 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1104 label_len = strlen(GOT_OBJ_LABEL_TAG);
1106 remain -= label_len;
1108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1112 if (remain <= 0 || *s != '\n') {
1113 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1119 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1123 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1127 label_len = strlen(GOT_TAG_LABEL_TAG);
1128 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1131 remain -= label_len;
1133 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1137 p = memchr(s, '\n', remain);
1139 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1144 (*tag)->tag = strndup(s, slen);
1145 if ((*tag)->tag == NULL) {
1146 err = got_error_from_errno("strndup");
1152 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1156 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1160 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1161 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1165 remain -= label_len;
1167 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1171 p = memchr(s, '\n', remain);
1173 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1178 err = parse_commit_time(&(*tag)->tagger_time,
1179 &(*tag)->tagger_gmtoff, s);
1182 (*tag)->tagger = strdup(s);
1183 if ((*tag)->tagger == NULL) {
1184 err = got_error_from_errno("strdup");
1190 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1194 /* Some old tags in the Linux git repo have no tagger. */
1195 (*tag)->tagger = strdup("");
1196 if ((*tag)->tagger == NULL) {
1197 err = got_error_from_errno("strdup");
1202 (*tag)->tagmsg = strndup(s, remain);
1203 if ((*tag)->tagmsg == NULL) {
1204 err = got_error_from_errno("strndup");
1209 got_object_tag_close(*tag);
1215 const struct got_error *
1216 got_object_read_tag(struct got_tag_object **tag, int fd,
1217 struct got_object_id *expected_id, size_t expected_size)
1219 const struct got_error *err = NULL;
1220 struct got_object *obj = NULL;
1223 struct got_inflate_checksum csum;
1224 SHA2_CTX sha256_ctx;
1226 struct got_object_id id;
1228 memset(&csum, 0, sizeof(csum));
1229 id.algo = expected_id->algo;
1230 if (expected_id->algo == GOT_HASH_SHA256) {
1231 SHA256Init(&sha256_ctx);
1232 csum.output_sha256 = &sha256_ctx;
1234 SHA1Init(&sha1_ctx);
1235 csum.output_sha1 = &sha1_ctx;
1238 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1243 if (expected_id->algo == GOT_HASH_SHA256)
1244 SHA256Final(id.hash, &sha256_ctx);
1246 SHA1Final(id.hash, &sha1_ctx);
1247 if (got_object_id_cmp(expected_id, &id) != 0) {
1248 err = got_error_checksum(expected_id);
1252 err = got_object_parse_header(&obj, p, len);
1256 if (len < obj->hdrlen + obj->size) {
1257 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1261 /* Skip object header. */
1263 err = got_object_parse_tag(tag, p + obj->hdrlen, len, expected_id->algo);
1267 got_object_close(obj);
1271 const struct got_error *
1272 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1274 const struct got_error *err = NULL;
1275 static const size_t blocksize = 512;
1276 size_t n, total, remain;
1282 buf = malloc(blocksize);
1284 return got_error_from_errno("malloc");
1291 newbuf = reallocarray(buf, 1, total + blocksize);
1292 if (newbuf == NULL) {
1293 err = got_error_from_errno("reallocarray");
1297 remain += blocksize;
1299 n = fread(buf + total, 1, remain, f);
1302 err = got_ferror(f, GOT_ERR_IO);