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");
291 err = got_inflate_to_fd(&len, f, &csum, outfd);
296 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
297 err = got_error(GOT_ERR_BAD_OBJ_HDR);
301 got_hash_final_object_id(&ctx, &id);
302 if (got_object_id_cmp(expected_id, &id) != 0) {
303 err = got_error_checksum(expected_id);
308 *hdrlen = obj->hdrlen;
310 got_object_close(obj);
311 if (f && fclose(f) == EOF && err == NULL)
312 err = got_error_from_errno("fclose");
316 struct got_commit_object *
317 got_object_commit_alloc_partial(void)
319 struct got_commit_object *commit;
321 commit = calloc(1, sizeof(*commit));
324 commit->tree_id = malloc(sizeof(*commit->tree_id));
325 if (commit->tree_id == NULL) {
330 STAILQ_INIT(&commit->parent_ids);
335 const struct got_error *
336 got_object_commit_add_parent(struct got_commit_object *commit,
339 const struct got_error *err = NULL;
340 struct got_object_qid *qid;
342 err = got_object_qid_alloc_partial(&qid);
346 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
347 err = got_error(GOT_ERR_BAD_OBJ_DATA);
348 got_object_qid_free(qid);
352 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
358 static const struct got_error *
359 parse_gmtoff(time_t *gmtoff, const char *tzstr)
362 const char *p = tzstr;
370 return got_error(GOT_ERR_BAD_OBJ_DATA);
372 if (!isdigit((unsigned char)*p) &&
373 !isdigit((unsigned char)*(p + 1)))
374 return got_error(GOT_ERR_BAD_OBJ_DATA);
375 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
378 if (!isdigit((unsigned char)*p) &&
379 !isdigit((unsigned char)*(p + 1)))
380 return got_error(GOT_ERR_BAD_OBJ_DATA);
381 m = ((*p - '0') * 10) + (*(p + 1) - '0');
383 *gmtoff = (h * 60 * 60 + m * 60) * sign;
387 static const struct got_error *
388 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
390 const struct got_error *err = NULL;
394 /* Parse and strip off trailing timezone indicator string. */
395 space = strrchr(committer, ' ');
397 return got_error(GOT_ERR_BAD_OBJ_DATA);
398 tzstr = strdup(space + 1);
400 return got_error_from_errno("strdup");
401 err = parse_gmtoff(gmtoff, tzstr);
404 if (err->code != GOT_ERR_BAD_OBJ_DATA)
406 /* Old versions of Git omitted the timestamp. */
413 /* Timestamp is separated from committer name + email by space. */
414 space = strrchr(committer, ' ');
416 return got_error(GOT_ERR_BAD_OBJ_DATA);
418 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
419 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
421 return got_error(GOT_ERR_BAD_OBJ_DATA);
423 /* Strip off parsed time information, leaving just author and email. */
430 got_object_commit_close(struct got_commit_object *commit)
432 if (commit->refcnt > 0) {
434 if (commit->refcnt > 0)
438 got_object_id_queue_free(&commit->parent_ids);
439 free(commit->tree_id);
440 free(commit->author);
441 free(commit->committer);
442 free(commit->logmsg);
446 struct got_object_id *
447 got_object_commit_get_tree_id(struct got_commit_object *commit)
449 return commit->tree_id;
453 got_object_commit_get_nparents(struct got_commit_object *commit)
455 return commit->nparents;
458 const struct got_object_id_queue *
459 got_object_commit_get_parent_ids(struct got_commit_object *commit)
461 return &commit->parent_ids;
465 got_object_commit_get_author(struct got_commit_object *commit)
467 return commit->author;
471 got_object_commit_get_author_time(struct got_commit_object *commit)
473 return commit->author_time;
476 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
478 return commit->author_gmtoff;
482 got_object_commit_get_committer(struct got_commit_object *commit)
484 return commit->committer;
488 got_object_commit_get_committer_time(struct got_commit_object *commit)
490 return commit->committer_time;
494 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
496 return commit->committer_gmtoff;
499 const struct got_error *
500 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
502 const struct got_error *err = NULL;
507 len = strlen(commit->logmsg);
508 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
510 return got_error_from_errno("malloc");
513 * Strip out unusual headers. Headers are separated from the commit
514 * message body by a single empty line.
516 src = commit->logmsg;
518 while (*src != '\0' && *src != '\n') {
519 int copy_header = 1, eol = 0;
520 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
521 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
522 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
523 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
524 strncmp(src, GOT_COMMIT_LABEL_PARENT,
525 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
526 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
527 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
530 while (*src != '\0' && !eol) {
542 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
543 err = got_error(GOT_ERR_NO_SPACE);
547 /* Trim redundant trailing whitespace. */
548 len = strlen(*logmsg);
549 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
550 isspace((unsigned char)(*logmsg)[len - 1])) {
551 (*logmsg)[len - 1] = '\0';
555 /* Append a trailing newline if missing. */
556 if (len > 0 && (*logmsg)[len - 1] != '\n') {
557 (*logmsg)[len] = '\n';
558 (*logmsg)[len + 1] = '\0';
569 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
571 return commit->logmsg;
574 const struct got_error *
575 got_object_parse_commit(struct got_commit_object **commit, char *buf,
578 const struct got_error *err = NULL;
579 enum got_hash_algorithm algo = GOT_HASH_SHA1;
582 ssize_t remain = (ssize_t)len;
585 return got_error(GOT_ERR_BAD_OBJ_DATA);
587 *commit = got_object_commit_alloc_partial();
589 return got_error_from_errno("got_object_commit_alloc_partial");
591 label_len = strlen(GOT_COMMIT_LABEL_TREE);
592 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
594 if (remain < SHA1_DIGEST_STRING_LENGTH) {
595 err = got_error(GOT_ERR_BAD_OBJ_DATA);
599 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
600 err = got_error(GOT_ERR_BAD_OBJ_DATA);
603 remain -= SHA1_DIGEST_STRING_LENGTH;
604 s += SHA1_DIGEST_STRING_LENGTH;
606 err = got_error(GOT_ERR_BAD_OBJ_DATA);
610 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
611 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
613 if (remain < SHA1_DIGEST_STRING_LENGTH) {
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
618 err = got_object_commit_add_parent(*commit, s);
622 remain -= SHA1_DIGEST_STRING_LENGTH;
623 s += SHA1_DIGEST_STRING_LENGTH;
626 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
627 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 p = memchr(s, '\n', remain);
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 err = parse_commit_time(&(*commit)->author_time,
645 &(*commit)->author_gmtoff, s);
648 (*commit)->author = strdup(s);
649 if ((*commit)->author == NULL) {
650 err = got_error_from_errno("strdup");
657 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
658 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
664 err = got_error(GOT_ERR_BAD_OBJ_DATA);
668 p = memchr(s, '\n', remain);
670 err = got_error(GOT_ERR_BAD_OBJ_DATA);
675 err = parse_commit_time(&(*commit)->committer_time,
676 &(*commit)->committer_gmtoff, s);
679 (*commit)->committer = strdup(s);
680 if ((*commit)->committer == NULL) {
681 err = got_error_from_errno("strdup");
688 (*commit)->logmsg = strndup(s, remain);
689 if ((*commit)->logmsg == NULL) {
690 err = got_error_from_errno("strndup");
695 got_object_commit_close(*commit);
701 const struct got_error *
702 got_object_read_commit(struct got_commit_object **commit, int fd,
703 struct got_object_id *expected_id, size_t expected_size)
705 struct got_object *obj = NULL;
706 const struct got_error *err = NULL;
709 struct got_inflate_checksum csum;
711 struct got_object_id id;
713 got_hash_init(&ctx, GOT_HASH_SHA1);
714 memset(&csum, 0, sizeof(csum));
715 csum.output_ctx = &ctx;
717 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
721 got_hash_final_object_id(&ctx, &id);
722 if (got_object_id_cmp(expected_id, &id) != 0) {
723 err = got_error_checksum(expected_id);
727 err = got_object_parse_header(&obj, p, len);
731 if (len < obj->hdrlen + obj->size) {
732 err = got_error(GOT_ERR_BAD_OBJ_DATA);
736 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
737 err = got_error(GOT_ERR_OBJ_TYPE);
741 /* Skip object header. */
743 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
747 got_object_close(obj);
752 got_object_tree_close(struct got_tree_object *tree)
754 if (tree->refcnt > 0) {
756 if (tree->refcnt > 0)
764 const struct got_error *
765 got_object_parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen,
766 char *buf, size_t maxlen)
772 *elen = strnlen(buf, maxlen) + 1;
774 return got_error(GOT_ERR_BAD_OBJ_DATA);
776 space = memchr(buf, ' ', *elen);
777 if (space == NULL || space <= buf)
778 return got_error(GOT_ERR_BAD_OBJ_DATA);
783 if (*p < '0' || *p > '7')
784 return got_error(GOT_ERR_BAD_OBJ_DATA);
786 pte->mode |= *p - '0';
790 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
791 return got_error(GOT_ERR_BAD_OBJ_DATA);
793 pte->name = space + 1;
794 pte->namelen = strlen(pte->name);
797 *elen += SHA1_DIGEST_LENGTH;
802 pte_cmp(const void *pa, const void *pb)
804 const struct got_parsed_tree_entry *a = pa, *b = pb;
806 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
809 const struct got_error *
810 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
811 size_t *nentries_alloc, uint8_t *buf, size_t len)
813 const struct got_error *err = NULL;
815 const size_t nalloc = 16;
816 struct got_parsed_tree_entry *pte;
821 return NULL; /* tree is empty */
826 if (*nentries >= *nentries_alloc) {
827 pte = recallocarray(*entries, *nentries_alloc,
828 *nentries_alloc + nalloc, sizeof(**entries));
830 err = got_error_from_errno("recallocarray");
834 *nentries_alloc += nalloc;
837 pte = &(*entries)[*nentries];
838 err = got_object_parse_tree_entry(pte, &elen, buf, remain);
847 err = got_error(GOT_ERR_BAD_OBJ_DATA);
852 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
854 for (i = 0; i < *nentries - 1; i++) {
855 struct got_parsed_tree_entry *prev = &(*entries)[i];
856 pte = &(*entries)[i + 1];
857 if (got_path_cmp(prev->name, pte->name,
858 prev->namelen, pte->namelen) == 0) {
859 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
870 const struct got_error *
871 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
872 size_t *nentries_alloc, uint8_t **p, int fd,
873 struct got_object_id *expected_id)
875 const struct got_error *err = NULL;
876 struct got_object *obj = NULL;
878 struct got_inflate_checksum csum;
880 struct got_object_id id;
882 got_hash_init(&ctx, GOT_HASH_SHA1);
883 memset(&csum, 0, sizeof(csum));
884 csum.output_ctx = &ctx;
886 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
890 got_hash_final_object_id(&ctx, &id);
891 if (got_object_id_cmp(expected_id, &id) != 0) {
892 err = got_error_checksum(expected_id);
896 err = got_object_parse_header(&obj, *p, len);
900 if (len < obj->hdrlen + obj->size) {
901 err = got_error(GOT_ERR_BAD_OBJ_DATA);
905 /* Skip object header. */
907 err = got_object_parse_tree(entries, nentries, nentries_alloc,
908 *p + obj->hdrlen, len);
911 got_object_close(obj);
916 got_object_tag_close(struct got_tag_object *tag)
918 if (tag->refcnt > 0) {
930 const struct got_error *
931 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
933 const struct got_error *err = NULL;
934 enum got_hash_algorithm algo = GOT_HASH_SHA1;
940 return got_error(GOT_ERR_BAD_OBJ_DATA);
942 *tag = calloc(1, sizeof(**tag));
944 return got_error_from_errno("calloc");
946 label_len = strlen(GOT_TAG_LABEL_OBJECT);
947 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
949 if (remain < SHA1_DIGEST_STRING_LENGTH) {
950 err = got_error(GOT_ERR_BAD_OBJ_DATA);
954 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
955 err = got_error(GOT_ERR_BAD_OBJ_DATA);
958 remain -= SHA1_DIGEST_STRING_LENGTH;
959 s += SHA1_DIGEST_STRING_LENGTH;
961 err = got_error(GOT_ERR_BAD_OBJ_DATA);
966 err = got_error(GOT_ERR_BAD_OBJ_DATA);
970 label_len = strlen(GOT_TAG_LABEL_TYPE);
971 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
974 err = got_error(GOT_ERR_BAD_OBJ_DATA);
978 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
979 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
980 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
981 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
984 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
985 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
986 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
987 label_len = strlen(GOT_OBJ_LABEL_TREE);
990 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
991 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
992 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
993 label_len = strlen(GOT_OBJ_LABEL_BLOB);
996 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
997 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
998 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
999 label_len = strlen(GOT_OBJ_LABEL_TAG);
1001 remain -= label_len;
1003 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1007 if (remain <= 0 || *s != '\n') {
1008 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1014 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1018 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1022 label_len = strlen(GOT_TAG_LABEL_TAG);
1023 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1026 remain -= label_len;
1028 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1032 p = memchr(s, '\n', remain);
1034 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1039 (*tag)->tag = strndup(s, slen);
1040 if ((*tag)->tag == NULL) {
1041 err = got_error_from_errno("strndup");
1047 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1055 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1056 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1060 remain -= label_len;
1062 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1066 p = memchr(s, '\n', remain);
1068 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1073 err = parse_commit_time(&(*tag)->tagger_time,
1074 &(*tag)->tagger_gmtoff, s);
1077 (*tag)->tagger = strdup(s);
1078 if ((*tag)->tagger == NULL) {
1079 err = got_error_from_errno("strdup");
1085 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1089 /* Some old tags in the Linux git repo have no tagger. */
1090 (*tag)->tagger = strdup("");
1091 if ((*tag)->tagger == NULL) {
1092 err = got_error_from_errno("strdup");
1097 (*tag)->tagmsg = strndup(s, remain);
1098 if ((*tag)->tagmsg == NULL) {
1099 err = got_error_from_errno("strndup");
1104 got_object_tag_close(*tag);
1110 const struct got_error *
1111 got_object_read_tag(struct got_tag_object **tag, int fd,
1112 struct got_object_id *expected_id, size_t expected_size)
1114 const struct got_error *err = NULL;
1115 struct got_object *obj = NULL;
1118 struct got_inflate_checksum csum;
1119 struct got_hash ctx;
1120 struct got_object_id id;
1122 got_hash_init(&ctx, GOT_HASH_SHA1);
1123 memset(&csum, 0, sizeof(csum));
1124 csum.output_ctx = &ctx;
1126 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1131 got_hash_final_object_id(&ctx, &id);
1132 if (got_object_id_cmp(expected_id, &id) != 0) {
1133 err = got_error_checksum(expected_id);
1137 err = got_object_parse_header(&obj, p, len);
1141 if (len < obj->hdrlen + obj->size) {
1142 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1146 /* Skip object header. */
1148 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1152 got_object_close(obj);