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_qid.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
57 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
60 const struct got_error *
61 got_object_type_label(const char **label, int obj_type)
63 const struct got_error *err = NULL;
66 case GOT_OBJ_TYPE_BLOB:
67 *label = GOT_OBJ_LABEL_BLOB;
69 case GOT_OBJ_TYPE_TREE:
70 *label = GOT_OBJ_LABEL_TREE;
72 case GOT_OBJ_TYPE_COMMIT:
73 *label = GOT_OBJ_LABEL_COMMIT;
75 case GOT_OBJ_TYPE_TAG:
76 *label = GOT_OBJ_LABEL_TAG;
80 err = got_error(GOT_ERR_OBJ_TYPE);
88 got_object_close(struct got_object *obj)
90 if (obj->refcnt > 0) {
96 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
97 struct got_delta *delta;
98 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
99 delta = STAILQ_FIRST(&obj->deltas.entries);
100 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
107 const struct got_error *
108 got_object_raw_close(struct got_raw_object *obj)
110 const struct got_error *err = NULL;
112 if (obj->refcnt > 0) {
121 if (obj->f == NULL) {
123 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
124 err = got_error_from_errno("munmap");
125 if (close(obj->fd) == -1 && err == NULL)
126 err = got_error_from_errno("close");
130 if (fclose(obj->f) == EOF && err == NULL)
131 err = got_error_from_errno("fclose");
137 const struct got_error *
138 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
140 const char *obj_labels[] = {
141 GOT_OBJ_LABEL_COMMIT,
146 const int obj_types[] = {
159 end = memchr(buf, '\0', len);
161 return got_error(GOT_ERR_BAD_OBJ_HDR);
163 for (i = 0; i < nitems(obj_labels); i++) {
164 const char *label = obj_labels[i];
165 size_t label_len = strlen(label);
168 if (len <= label_len || buf + label_len >= end ||
169 strncmp(buf, label, label_len) != 0)
173 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
175 return got_error(GOT_ERR_BAD_OBJ_HDR);
180 return got_error(GOT_ERR_BAD_OBJ_HDR);
182 *obj = calloc(1, sizeof(**obj));
184 return got_error_from_errno("calloc");
186 (*obj)->hdrlen = end - buf + 1;
191 const struct got_error *
192 got_object_read_header(struct got_object **obj, int fd)
194 const struct got_error *err;
195 struct got_inflate_buf zb;
197 const size_t zbsize = 64;
198 size_t outlen, totlen;
203 buf = malloc(zbsize);
205 return got_error_from_errno("malloc");
208 err = got_inflate_init(&zb, buf, zbsize, NULL);
214 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
220 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
223 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
224 if (newbuf == NULL) {
225 err = got_error_from_errno("recallocarray");
229 zb.outbuf = newbuf + totlen;
230 zb.outlen = (nbuf * zbsize) - totlen;
232 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
234 err = got_object_parse_header(obj, buf, totlen);
237 got_inflate_end(&zb);
241 const struct got_error *
242 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
243 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
246 const struct got_error *err = NULL;
247 struct got_object *obj;
248 struct got_inflate_checksum csum;
249 struct got_object_id id;
251 size_t len, consumed;
258 got_hash_init(&ctx, GOT_HASH_SHA1);
259 memset(&csum, 0, sizeof(csum));
260 csum.output_ctx = &ctx;
262 if (lseek(infd, SEEK_SET, 0) == -1)
263 return got_error_from_errno("lseek");
265 err = got_object_read_header(&obj, infd);
269 if (lseek(infd, SEEK_SET, 0) == -1)
270 return got_error_from_errno("lseek");
272 if (obj->size + obj->hdrlen <= max_in_mem_size) {
273 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
274 obj->size + obj->hdrlen, infd);
278 * XXX This uses an extra file descriptor for no good reason.
279 * We should have got_inflate_fd_to_fd().
283 return got_error_from_errno("dup");
286 err = got_error_from_errno("fdopen");
290 err = got_inflate_to_fd(&len, f, &csum, outfd);
295 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
296 err = got_error(GOT_ERR_BAD_OBJ_HDR);
300 got_hash_final_object_id(&ctx, &id);
301 if (got_object_id_cmp(expected_id, &id) != 0) {
302 err = got_error_checksum(expected_id);
307 *hdrlen = obj->hdrlen;
309 got_object_close(obj);
310 if (f && fclose(f) == EOF && err == NULL)
311 err = got_error_from_errno("fclose");
315 struct got_commit_object *
316 got_object_commit_alloc_partial(void)
318 struct got_commit_object *commit;
320 commit = calloc(1, sizeof(*commit));
323 commit->tree_id = malloc(sizeof(*commit->tree_id));
324 if (commit->tree_id == NULL) {
329 STAILQ_INIT(&commit->parent_ids);
334 const struct got_error *
335 got_object_commit_add_parent(struct got_commit_object *commit,
338 const struct got_error *err = NULL;
339 struct got_object_qid *qid;
341 err = got_object_qid_alloc_partial(&qid);
345 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 got_object_qid_free(qid);
351 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
357 static const struct got_error *
358 parse_gmtoff(time_t *gmtoff, const char *tzstr)
361 const char *p = tzstr;
369 return got_error(GOT_ERR_BAD_OBJ_DATA);
371 if (!isdigit((unsigned char)*p) &&
372 !isdigit((unsigned char)*(p + 1)))
373 return got_error(GOT_ERR_BAD_OBJ_DATA);
374 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
377 if (!isdigit((unsigned char)*p) &&
378 !isdigit((unsigned char)*(p + 1)))
379 return got_error(GOT_ERR_BAD_OBJ_DATA);
380 m = ((*p - '0') * 10) + (*(p + 1) - '0');
382 *gmtoff = (h * 60 * 60 + m * 60) * sign;
386 static const struct got_error *
387 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
389 const struct got_error *err = NULL;
393 /* Parse and strip off trailing timezone indicator string. */
394 space = strrchr(committer, ' ');
396 return got_error(GOT_ERR_BAD_OBJ_DATA);
397 tzstr = strdup(space + 1);
399 return got_error_from_errno("strdup");
400 err = parse_gmtoff(gmtoff, tzstr);
403 if (err->code != GOT_ERR_BAD_OBJ_DATA)
405 /* Old versions of Git omitted the timestamp. */
412 /* Timestamp is separated from committer name + email by space. */
413 space = strrchr(committer, ' ');
415 return got_error(GOT_ERR_BAD_OBJ_DATA);
417 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
418 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
420 return got_error(GOT_ERR_BAD_OBJ_DATA);
422 /* Strip off parsed time information, leaving just author and email. */
429 got_object_commit_close(struct got_commit_object *commit)
431 if (commit->refcnt > 0) {
433 if (commit->refcnt > 0)
437 got_object_id_queue_free(&commit->parent_ids);
438 free(commit->tree_id);
439 free(commit->author);
440 free(commit->committer);
441 free(commit->logmsg);
445 struct got_object_id *
446 got_object_commit_get_tree_id(struct got_commit_object *commit)
448 return commit->tree_id;
452 got_object_commit_get_nparents(struct got_commit_object *commit)
454 return commit->nparents;
457 const struct got_object_id_queue *
458 got_object_commit_get_parent_ids(struct got_commit_object *commit)
460 return &commit->parent_ids;
464 got_object_commit_get_author(struct got_commit_object *commit)
466 return commit->author;
470 got_object_commit_get_author_time(struct got_commit_object *commit)
472 return commit->author_time;
475 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
477 return commit->author_gmtoff;
481 got_object_commit_get_committer(struct got_commit_object *commit)
483 return commit->committer;
487 got_object_commit_get_committer_time(struct got_commit_object *commit)
489 return commit->committer_time;
493 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
495 return commit->committer_gmtoff;
498 const struct got_error *
499 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
501 const struct got_error *err = NULL;
506 len = strlen(commit->logmsg);
507 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
509 return got_error_from_errno("malloc");
512 * Strip out unusual headers. Headers are separated from the commit
513 * message body by a single empty line.
515 src = commit->logmsg;
517 while (*src != '\0' && *src != '\n') {
518 int copy_header = 1, eol = 0;
519 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
520 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
521 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
522 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
523 strncmp(src, GOT_COMMIT_LABEL_PARENT,
524 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
525 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
526 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
529 while (*src != '\0' && !eol) {
541 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
542 err = got_error(GOT_ERR_NO_SPACE);
546 /* Trim redundant trailing whitespace. */
547 len = strlen(*logmsg);
548 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
549 isspace((unsigned char)(*logmsg)[len - 1])) {
550 (*logmsg)[len - 1] = '\0';
554 /* Append a trailing newline if missing. */
555 if (len > 0 && (*logmsg)[len - 1] != '\n') {
556 (*logmsg)[len] = '\n';
557 (*logmsg)[len + 1] = '\0';
568 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
570 return commit->logmsg;
573 const struct got_error *
574 got_object_parse_commit(struct got_commit_object **commit, char *buf,
577 const struct got_error *err = NULL;
578 enum got_hash_algorithm algo = GOT_HASH_SHA1;
581 ssize_t remain = (ssize_t)len;
584 return got_error(GOT_ERR_BAD_OBJ_DATA);
586 *commit = got_object_commit_alloc_partial();
588 return got_error_from_errno("got_object_commit_alloc_partial");
590 label_len = strlen(GOT_COMMIT_LABEL_TREE);
591 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
593 if (remain < SHA1_DIGEST_STRING_LENGTH) {
594 err = got_error(GOT_ERR_BAD_OBJ_DATA);
598 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
599 err = got_error(GOT_ERR_BAD_OBJ_DATA);
602 remain -= SHA1_DIGEST_STRING_LENGTH;
603 s += SHA1_DIGEST_STRING_LENGTH;
605 err = got_error(GOT_ERR_BAD_OBJ_DATA);
609 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
610 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
612 if (remain < SHA1_DIGEST_STRING_LENGTH) {
613 err = got_error(GOT_ERR_BAD_OBJ_DATA);
617 err = got_object_commit_add_parent(*commit, s);
621 remain -= SHA1_DIGEST_STRING_LENGTH;
622 s += SHA1_DIGEST_STRING_LENGTH;
625 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
626 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
632 err = got_error(GOT_ERR_BAD_OBJ_DATA);
636 p = memchr(s, '\n', remain);
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 err = parse_commit_time(&(*commit)->author_time,
644 &(*commit)->author_gmtoff, s);
647 (*commit)->author = strdup(s);
648 if ((*commit)->author == NULL) {
649 err = got_error_from_errno("strdup");
656 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
657 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
663 err = got_error(GOT_ERR_BAD_OBJ_DATA);
667 p = memchr(s, '\n', remain);
669 err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 err = parse_commit_time(&(*commit)->committer_time,
675 &(*commit)->committer_gmtoff, s);
678 (*commit)->committer = strdup(s);
679 if ((*commit)->committer == NULL) {
680 err = got_error_from_errno("strdup");
687 (*commit)->logmsg = strndup(s, remain);
688 if ((*commit)->logmsg == NULL) {
689 err = got_error_from_errno("strndup");
694 got_object_commit_close(*commit);
700 const struct got_error *
701 got_object_read_commit(struct got_commit_object **commit, int fd,
702 struct got_object_id *expected_id, size_t expected_size)
704 struct got_object *obj = NULL;
705 const struct got_error *err = NULL;
708 struct got_inflate_checksum csum;
710 struct got_object_id id;
712 got_hash_init(&ctx, GOT_HASH_SHA1);
713 memset(&csum, 0, sizeof(csum));
714 csum.output_ctx = &ctx;
716 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
720 got_hash_final_object_id(&ctx, &id);
721 if (got_object_id_cmp(expected_id, &id) != 0) {
722 err = got_error_checksum(expected_id);
726 err = got_object_parse_header(&obj, p, len);
730 if (len < obj->hdrlen + obj->size) {
731 err = got_error(GOT_ERR_BAD_OBJ_DATA);
735 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
736 err = got_error(GOT_ERR_OBJ_TYPE);
740 /* Skip object header. */
742 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
746 got_object_close(obj);
751 got_object_tree_close(struct got_tree_object *tree)
753 if (tree->refcnt > 0) {
755 if (tree->refcnt > 0)
763 const struct got_error *
764 got_object_parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen,
765 char *buf, size_t maxlen)
771 *elen = strnlen(buf, maxlen) + 1;
773 return got_error(GOT_ERR_BAD_OBJ_DATA);
775 space = memchr(buf, ' ', *elen);
776 if (space == NULL || space <= buf)
777 return got_error(GOT_ERR_BAD_OBJ_DATA);
782 if (*p < '0' || *p > '7')
783 return got_error(GOT_ERR_BAD_OBJ_DATA);
785 pte->mode |= *p - '0';
789 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
790 return got_error(GOT_ERR_BAD_OBJ_DATA);
792 pte->name = space + 1;
793 pte->namelen = strlen(pte->name);
796 *elen += SHA1_DIGEST_LENGTH;
801 pte_cmp(const void *pa, const void *pb)
803 const struct got_parsed_tree_entry *a = pa, *b = pb;
805 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
808 const struct got_error *
809 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
810 size_t *nentries_alloc, uint8_t *buf, size_t len)
812 const struct got_error *err = NULL;
814 const size_t nalloc = 16;
815 struct got_parsed_tree_entry *pte;
820 return NULL; /* tree is empty */
825 if (*nentries >= *nentries_alloc) {
826 pte = recallocarray(*entries, *nentries_alloc,
827 *nentries_alloc + nalloc, sizeof(**entries));
829 err = got_error_from_errno("recallocarray");
833 *nentries_alloc += nalloc;
836 pte = &(*entries)[*nentries];
837 err = got_object_parse_tree_entry(pte, &elen, buf, remain);
846 err = got_error(GOT_ERR_BAD_OBJ_DATA);
851 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
853 for (i = 0; i < *nentries - 1; i++) {
854 struct got_parsed_tree_entry *prev = &(*entries)[i];
855 pte = &(*entries)[i + 1];
856 if (got_path_cmp(prev->name, pte->name,
857 prev->namelen, pte->namelen) == 0) {
858 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
869 const struct got_error *
870 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
871 size_t *nentries_alloc, uint8_t **p, int fd,
872 struct got_object_id *expected_id)
874 const struct got_error *err = NULL;
875 struct got_object *obj = NULL;
877 struct got_inflate_checksum csum;
879 struct got_object_id id;
881 got_hash_init(&ctx, GOT_HASH_SHA1);
882 memset(&csum, 0, sizeof(csum));
883 csum.output_ctx = &ctx;
885 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
889 got_hash_final_object_id(&ctx, &id);
890 if (got_object_id_cmp(expected_id, &id) != 0) {
891 err = got_error_checksum(expected_id);
895 err = got_object_parse_header(&obj, *p, len);
899 if (len < obj->hdrlen + obj->size) {
900 err = got_error(GOT_ERR_BAD_OBJ_DATA);
904 /* Skip object header. */
906 err = got_object_parse_tree(entries, nentries, nentries_alloc,
907 *p + obj->hdrlen, len);
910 got_object_close(obj);
915 got_object_tag_close(struct got_tag_object *tag)
917 if (tag->refcnt > 0) {
929 const struct got_error *
930 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
932 const struct got_error *err = NULL;
933 enum got_hash_algorithm algo = GOT_HASH_SHA1;
939 return got_error(GOT_ERR_BAD_OBJ_DATA);
941 *tag = calloc(1, sizeof(**tag));
943 return got_error_from_errno("calloc");
945 label_len = strlen(GOT_TAG_LABEL_OBJECT);
946 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
948 if (remain < SHA1_DIGEST_STRING_LENGTH) {
949 err = got_error(GOT_ERR_BAD_OBJ_DATA);
953 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
954 err = got_error(GOT_ERR_BAD_OBJ_DATA);
957 remain -= SHA1_DIGEST_STRING_LENGTH;
958 s += SHA1_DIGEST_STRING_LENGTH;
960 err = got_error(GOT_ERR_BAD_OBJ_DATA);
965 err = got_error(GOT_ERR_BAD_OBJ_DATA);
969 label_len = strlen(GOT_TAG_LABEL_TYPE);
970 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
973 err = got_error(GOT_ERR_BAD_OBJ_DATA);
977 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
978 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
979 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
980 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
983 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
984 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
985 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
986 label_len = strlen(GOT_OBJ_LABEL_TREE);
989 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
990 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
991 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
992 label_len = strlen(GOT_OBJ_LABEL_BLOB);
995 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
996 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
997 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
998 label_len = strlen(GOT_OBJ_LABEL_TAG);
1000 remain -= label_len;
1002 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1006 if (remain <= 0 || *s != '\n') {
1007 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1013 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1017 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1021 label_len = strlen(GOT_TAG_LABEL_TAG);
1022 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1025 remain -= label_len;
1027 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1031 p = memchr(s, '\n', remain);
1033 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1038 (*tag)->tag = strndup(s, slen);
1039 if ((*tag)->tag == NULL) {
1040 err = got_error_from_errno("strndup");
1046 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1050 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1054 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1055 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1059 remain -= label_len;
1061 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1065 p = memchr(s, '\n', remain);
1067 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1072 err = parse_commit_time(&(*tag)->tagger_time,
1073 &(*tag)->tagger_gmtoff, s);
1076 (*tag)->tagger = strdup(s);
1077 if ((*tag)->tagger == NULL) {
1078 err = got_error_from_errno("strdup");
1084 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1088 /* Some old tags in the Linux git repo have no tagger. */
1089 (*tag)->tagger = strdup("");
1090 if ((*tag)->tagger == NULL) {
1091 err = got_error_from_errno("strdup");
1096 (*tag)->tagmsg = strndup(s, remain);
1097 if ((*tag)->tagmsg == NULL) {
1098 err = got_error_from_errno("strndup");
1103 got_object_tag_close(*tag);
1109 const struct got_error *
1110 got_object_read_tag(struct got_tag_object **tag, int fd,
1111 struct got_object_id *expected_id, size_t expected_size)
1113 const struct got_error *err = NULL;
1114 struct got_object *obj = NULL;
1117 struct got_inflate_checksum csum;
1118 struct got_hash ctx;
1119 struct got_object_id id;
1121 got_hash_init(&ctx, GOT_HASH_SHA1);
1122 memset(&csum, 0, sizeof(csum));
1123 csum.output_ctx = &ctx;
1125 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1130 got_hash_final_object_id(&ctx, &id);
1131 if (got_object_id_cmp(expected_id, &id) != 0) {
1132 err = got_error_checksum(expected_id);
1136 err = got_object_parse_header(&obj, p, len);
1140 if (len < obj->hdrlen + obj->size) {
1141 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1145 /* Skip object header. */
1147 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1151 got_object_close(obj);