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>
21 #include <sys/socket.h>
36 #include "got_compat.h"
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_repository.h"
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 struct got_object_id *
58 got_object_id_dup(struct got_object_id *id1)
60 struct got_object_id *id2;
62 id2 = malloc(sizeof(*id2));
65 memcpy(id2, id1, sizeof(*id2));
70 got_object_id_cmp(const struct got_object_id *id1,
71 const struct got_object_id *id2)
73 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
76 const struct got_error *
77 got_object_qid_alloc_partial(struct got_object_qid **qid)
79 *qid = malloc(sizeof(**qid));
81 return got_error_from_errno("malloc");
87 const struct got_error *
88 got_object_id_str(char **outbuf, struct got_object_id *id)
90 static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN;
92 *outbuf = malloc(len);
94 return got_error_from_errno("malloc");
96 if (got_object_id_hex(id, *outbuf, len) == NULL) {
99 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
106 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
108 return got_sha1_digest_to_str(id->sha1, buf, len);
112 got_object_close(struct got_object *obj)
114 if (obj->refcnt > 0) {
120 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
121 struct got_delta *delta;
122 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
123 delta = STAILQ_FIRST(&obj->deltas.entries);
124 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
131 const struct got_error *
132 got_object_raw_close(struct got_raw_object *obj)
134 const struct got_error *err = NULL;
136 if (obj->refcnt > 0) {
145 if (obj->f == NULL) {
147 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
148 err = got_error_from_errno("munmap");
149 if (close(obj->fd) == -1 && err == NULL)
150 err = got_error_from_errno("close");
154 if (fclose(obj->f) == EOF && err == NULL)
155 err = got_error_from_errno("fclose");
162 got_object_qid_free(struct got_object_qid *qid)
168 got_object_id_queue_free(struct got_object_id_queue *ids)
170 struct got_object_qid *qid;
172 while (!STAILQ_EMPTY(ids)) {
173 qid = STAILQ_FIRST(ids);
174 STAILQ_REMOVE_HEAD(ids, entry);
175 got_object_qid_free(qid);
179 const struct got_error *
180 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
182 const char *obj_labels[] = {
183 GOT_OBJ_LABEL_COMMIT,
188 const int obj_types[] = {
201 end = memchr(buf, '\0', len);
203 return got_error(GOT_ERR_BAD_OBJ_HDR);
205 for (i = 0; i < nitems(obj_labels); i++) {
206 const char *label = obj_labels[i];
207 size_t label_len = strlen(label);
210 if (len <= label_len || buf + label_len >= end ||
211 strncmp(buf, label, label_len) != 0)
215 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
217 return got_error(GOT_ERR_BAD_OBJ_HDR);
222 return got_error(GOT_ERR_BAD_OBJ_HDR);
224 *obj = calloc(1, sizeof(**obj));
226 return got_error_from_errno("calloc");
228 (*obj)->hdrlen = end - buf + 1;
233 const struct got_error *
234 got_object_read_header(struct got_object **obj, int fd)
236 const struct got_error *err;
237 struct got_inflate_buf zb;
239 const size_t zbsize = 64;
240 size_t outlen, totlen;
245 buf = malloc(zbsize);
247 return got_error_from_errno("malloc");
250 err = got_inflate_init(&zb, buf, zbsize, NULL);
256 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
262 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
265 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
266 if (newbuf == NULL) {
267 err = got_error_from_errno("recallocarray");
271 zb.outbuf = newbuf + totlen;
272 zb.outlen = (nbuf * zbsize) - totlen;
274 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
276 err = got_object_parse_header(obj, buf, totlen);
279 got_inflate_end(&zb);
283 const struct got_error *
284 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
285 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
288 const struct got_error *err = NULL;
289 struct got_object *obj;
290 struct got_inflate_checksum csum;
291 uint8_t sha1[SHA1_DIGEST_LENGTH];
293 size_t len, consumed;
301 memset(&csum, 0, sizeof(csum));
302 csum.output_sha1 = &sha1_ctx;
304 if (lseek(infd, SEEK_SET, 0) == -1)
305 return got_error_from_errno("lseek");
307 err = got_object_read_header(&obj, infd);
311 if (lseek(infd, SEEK_SET, 0) == -1)
312 return got_error_from_errno("lseek");
314 if (obj->size + obj->hdrlen <= max_in_mem_size) {
315 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
316 obj->size + obj->hdrlen, infd);
320 * XXX This uses an extra file descriptor for no good reason.
321 * We should have got_inflate_fd_to_fd().
325 return got_error_from_errno("dup");
328 err = got_error_from_errno("fdopen");
333 err = got_inflate_to_fd(&len, f, &csum, outfd);
338 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
339 err = got_error(GOT_ERR_BAD_OBJ_HDR);
343 SHA1Final(sha1, &sha1_ctx);
344 if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
345 err = got_error_checksum(expected_id);
350 *hdrlen = obj->hdrlen;
352 got_object_close(obj);
353 if (f && fclose(f) == EOF && err == NULL)
354 err = got_error_from_errno("fclose");
358 struct got_commit_object *
359 got_object_commit_alloc_partial(void)
361 struct got_commit_object *commit;
363 commit = calloc(1, sizeof(*commit));
366 commit->tree_id = malloc(sizeof(*commit->tree_id));
367 if (commit->tree_id == NULL) {
372 STAILQ_INIT(&commit->parent_ids);
377 const struct got_error *
378 got_object_commit_add_parent(struct got_commit_object *commit,
381 const struct got_error *err = NULL;
382 struct got_object_qid *qid;
384 err = got_object_qid_alloc_partial(&qid);
388 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
389 err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 got_object_qid_free(qid);
394 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
400 static const struct got_error *
401 parse_gmtoff(time_t *gmtoff, const char *tzstr)
404 const char *p = tzstr;
412 return got_error(GOT_ERR_BAD_OBJ_DATA);
414 if (!isdigit((unsigned char)*p) &&
415 !isdigit((unsigned char)*(p + 1)))
416 return got_error(GOT_ERR_BAD_OBJ_DATA);
417 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
420 if (!isdigit((unsigned char)*p) &&
421 !isdigit((unsigned char)*(p + 1)))
422 return got_error(GOT_ERR_BAD_OBJ_DATA);
423 m = ((*p - '0') * 10) + (*(p + 1) - '0');
425 *gmtoff = (h * 60 * 60 + m * 60) * sign;
429 static const struct got_error *
430 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
432 const struct got_error *err = NULL;
436 /* Parse and strip off trailing timezone indicator string. */
437 space = strrchr(committer, ' ');
439 return got_error(GOT_ERR_BAD_OBJ_DATA);
440 tzstr = strdup(space + 1);
442 return got_error_from_errno("strdup");
443 err = parse_gmtoff(gmtoff, tzstr);
446 if (err->code != GOT_ERR_BAD_OBJ_DATA)
448 /* Old versions of Git omitted the timestamp. */
455 /* Timestamp is separated from committer name + email by space. */
456 space = strrchr(committer, ' ');
458 return got_error(GOT_ERR_BAD_OBJ_DATA);
460 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
461 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
463 return got_error(GOT_ERR_BAD_OBJ_DATA);
465 /* Strip off parsed time information, leaving just author and email. */
472 got_object_commit_close(struct got_commit_object *commit)
474 if (commit->refcnt > 0) {
476 if (commit->refcnt > 0)
480 got_object_id_queue_free(&commit->parent_ids);
481 free(commit->tree_id);
482 free(commit->author);
483 free(commit->committer);
484 free(commit->logmsg);
488 struct got_object_id *
489 got_object_commit_get_tree_id(struct got_commit_object *commit)
491 return commit->tree_id;
495 got_object_commit_get_nparents(struct got_commit_object *commit)
497 return commit->nparents;
500 const struct got_object_id_queue *
501 got_object_commit_get_parent_ids(struct got_commit_object *commit)
503 return &commit->parent_ids;
507 got_object_commit_get_author(struct got_commit_object *commit)
509 return commit->author;
513 got_object_commit_get_author_time(struct got_commit_object *commit)
515 return commit->author_time;
518 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
520 return commit->author_gmtoff;
524 got_object_commit_get_committer(struct got_commit_object *commit)
526 return commit->committer;
530 got_object_commit_get_committer_time(struct got_commit_object *commit)
532 return commit->committer_time;
536 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
538 return commit->committer_gmtoff;
541 const struct got_error *
542 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
544 const struct got_error *err = NULL;
549 len = strlen(commit->logmsg);
550 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
552 return got_error_from_errno("malloc");
555 * Strip out unusual headers. Headers are separated from the commit
556 * message body by a single empty line.
558 src = commit->logmsg;
560 while (*src != '\0' && *src != '\n') {
561 int copy_header = 1, eol = 0;
562 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
563 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
564 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
565 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
566 strncmp(src, GOT_COMMIT_LABEL_PARENT,
567 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
568 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
569 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
572 while (*src != '\0' && !eol) {
584 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
585 err = got_error(GOT_ERR_NO_SPACE);
589 /* Trim redundant trailing whitespace. */
590 len = strlen(*logmsg);
591 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
592 isspace((unsigned char)(*logmsg)[len - 1])) {
593 (*logmsg)[len - 1] = '\0';
597 /* Append a trailing newline if missing. */
598 if (len > 0 && (*logmsg)[len - 1] != '\n') {
599 (*logmsg)[len] = '\n';
600 (*logmsg)[len + 1] = '\0';
611 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
613 return commit->logmsg;
616 const struct got_error *
617 got_object_parse_commit(struct got_commit_object **commit, char *buf,
620 const struct got_error *err = NULL;
623 ssize_t remain = (ssize_t)len;
626 return got_error(GOT_ERR_BAD_OBJ_DATA);
628 *commit = got_object_commit_alloc_partial();
630 return got_error_from_errno("got_object_commit_alloc_partial");
632 label_len = strlen(GOT_COMMIT_LABEL_TREE);
633 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
635 if (remain < SHA1_DIGEST_STRING_LENGTH) {
636 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
641 err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 remain -= SHA1_DIGEST_STRING_LENGTH;
645 s += SHA1_DIGEST_STRING_LENGTH;
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
651 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
652 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
654 if (remain < SHA1_DIGEST_STRING_LENGTH) {
655 err = got_error(GOT_ERR_BAD_OBJ_DATA);
659 err = got_object_commit_add_parent(*commit, s);
663 remain -= SHA1_DIGEST_STRING_LENGTH;
664 s += SHA1_DIGEST_STRING_LENGTH;
667 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
668 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
674 err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 p = memchr(s, '\n', remain);
680 err = got_error(GOT_ERR_BAD_OBJ_DATA);
685 err = parse_commit_time(&(*commit)->author_time,
686 &(*commit)->author_gmtoff, s);
689 (*commit)->author = strdup(s);
690 if ((*commit)->author == NULL) {
691 err = got_error_from_errno("strdup");
698 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
699 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
705 err = got_error(GOT_ERR_BAD_OBJ_DATA);
709 p = memchr(s, '\n', remain);
711 err = got_error(GOT_ERR_BAD_OBJ_DATA);
716 err = parse_commit_time(&(*commit)->committer_time,
717 &(*commit)->committer_gmtoff, s);
720 (*commit)->committer = strdup(s);
721 if ((*commit)->committer == NULL) {
722 err = got_error_from_errno("strdup");
729 (*commit)->logmsg = strndup(s, remain);
730 if ((*commit)->logmsg == NULL) {
731 err = got_error_from_errno("strndup");
736 got_object_commit_close(*commit);
742 const struct got_error *
743 got_object_read_commit(struct got_commit_object **commit, int fd,
744 struct got_object_id *expected_id, size_t expected_size)
746 struct got_object *obj = NULL;
747 const struct got_error *err = NULL;
750 struct got_inflate_checksum csum;
752 struct got_object_id id;
755 memset(&csum, 0, sizeof(csum));
756 csum.output_sha1 = &sha1_ctx;
758 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
762 SHA1Final(id.sha1, &sha1_ctx);
763 if (got_object_id_cmp(expected_id, &id) != 0) {
764 err = got_error_checksum(expected_id);
768 err = got_object_parse_header(&obj, p, len);
772 if (len < obj->hdrlen + obj->size) {
773 err = got_error(GOT_ERR_BAD_OBJ_DATA);
777 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
778 err = got_error(GOT_ERR_OBJ_TYPE);
782 /* Skip object header. */
784 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
788 got_object_close(obj);
793 got_object_tree_close(struct got_tree_object *tree)
795 if (tree->refcnt > 0) {
797 if (tree->refcnt > 0)
805 static const struct got_error *
806 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
813 *elen = strnlen(buf, maxlen) + 1;
815 return got_error(GOT_ERR_BAD_OBJ_DATA);
817 space = memchr(buf, ' ', *elen);
818 if (space == NULL || space <= buf)
819 return got_error(GOT_ERR_BAD_OBJ_DATA);
824 if (*p < '0' || *p > '7')
825 return got_error(GOT_ERR_BAD_OBJ_DATA);
827 pte->mode |= *p - '0';
831 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
832 return got_error(GOT_ERR_BAD_OBJ_DATA);
834 pte->name = space + 1;
835 pte->namelen = strlen(pte->name);
838 *elen += SHA1_DIGEST_LENGTH;
843 pte_cmp(const void *pa, const void *pb)
845 const struct got_parsed_tree_entry *a = pa, *b = pb;
847 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
850 const struct got_error *
851 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
852 size_t *nentries_alloc, uint8_t *buf, size_t len)
854 const struct got_error *err = NULL;
856 const size_t nalloc = 16;
857 struct got_parsed_tree_entry *pte;
862 return NULL; /* tree is empty */
867 if (*nentries >= *nentries_alloc) {
868 pte = recallocarray(*entries, *nentries_alloc,
869 *nentries_alloc + nalloc, sizeof(**entries));
871 err = got_error_from_errno("recallocarray");
875 *nentries_alloc += nalloc;
878 pte = &(*entries)[*nentries];
879 err = parse_tree_entry(pte, &elen, buf, remain);
888 err = got_error(GOT_ERR_BAD_OBJ_DATA);
893 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
895 for (i = 0; i < *nentries - 1; i++) {
896 struct got_parsed_tree_entry *prev = &(*entries)[i];
897 pte = &(*entries)[i + 1];
898 if (got_path_cmp(prev->name, pte->name,
899 prev->namelen, pte->namelen) == 0) {
900 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
911 const struct got_error *
912 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
913 size_t *nentries_alloc, uint8_t **p, int fd,
914 struct got_object_id *expected_id)
916 const struct got_error *err = NULL;
917 struct got_object *obj = NULL;
919 struct got_inflate_checksum csum;
921 struct got_object_id id;
924 memset(&csum, 0, sizeof(csum));
925 csum.output_sha1 = &sha1_ctx;
927 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
931 SHA1Final(id.sha1, &sha1_ctx);
932 if (got_object_id_cmp(expected_id, &id) != 0) {
933 err = got_error_checksum(expected_id);
937 err = got_object_parse_header(&obj, *p, len);
941 if (len < obj->hdrlen + obj->size) {
942 err = got_error(GOT_ERR_BAD_OBJ_DATA);
946 /* Skip object header. */
948 err = got_object_parse_tree(entries, nentries, nentries_alloc,
949 *p + obj->hdrlen, len);
952 got_object_close(obj);
957 got_object_tag_close(struct got_tag_object *tag)
959 if (tag->refcnt > 0) {
971 const struct got_error *
972 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
974 const struct got_error *err = NULL;
980 return got_error(GOT_ERR_BAD_OBJ_DATA);
982 *tag = calloc(1, sizeof(**tag));
984 return got_error_from_errno("calloc");
986 label_len = strlen(GOT_TAG_LABEL_OBJECT);
987 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
989 if (remain < SHA1_DIGEST_STRING_LENGTH) {
990 err = got_error(GOT_ERR_BAD_OBJ_DATA);
994 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
995 err = got_error(GOT_ERR_BAD_OBJ_DATA);
998 remain -= SHA1_DIGEST_STRING_LENGTH;
999 s += SHA1_DIGEST_STRING_LENGTH;
1001 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1006 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1010 label_len = strlen(GOT_TAG_LABEL_TYPE);
1011 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1012 remain -= label_len;
1014 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1018 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1019 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1020 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1021 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1023 remain -= label_len;
1024 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1025 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1026 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1027 label_len = strlen(GOT_OBJ_LABEL_TREE);
1029 remain -= label_len;
1030 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1031 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1032 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1033 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1035 remain -= label_len;
1036 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1037 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1038 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1039 label_len = strlen(GOT_OBJ_LABEL_TAG);
1041 remain -= label_len;
1043 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1047 if (remain <= 0 || *s != '\n') {
1048 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1054 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1058 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 label_len = strlen(GOT_TAG_LABEL_TAG);
1063 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1066 remain -= label_len;
1068 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1072 p = memchr(s, '\n', remain);
1074 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1079 (*tag)->tag = strndup(s, slen);
1080 if ((*tag)->tag == NULL) {
1081 err = got_error_from_errno("strndup");
1087 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1091 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1095 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1096 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1100 remain -= label_len;
1102 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1106 p = memchr(s, '\n', remain);
1108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1113 err = parse_commit_time(&(*tag)->tagger_time,
1114 &(*tag)->tagger_gmtoff, s);
1117 (*tag)->tagger = strdup(s);
1118 if ((*tag)->tagger == NULL) {
1119 err = got_error_from_errno("strdup");
1125 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1129 /* Some old tags in the Linux git repo have no tagger. */
1130 (*tag)->tagger = strdup("");
1131 if ((*tag)->tagger == NULL) {
1132 err = got_error_from_errno("strdup");
1137 (*tag)->tagmsg = strndup(s, remain);
1138 if ((*tag)->tagmsg == NULL) {
1139 err = got_error_from_errno("strndup");
1144 got_object_tag_close(*tag);
1150 const struct got_error *
1151 got_object_read_tag(struct got_tag_object **tag, int fd,
1152 struct got_object_id *expected_id, size_t expected_size)
1154 const struct got_error *err = NULL;
1155 struct got_object *obj = NULL;
1158 struct got_inflate_checksum csum;
1160 struct got_object_id id;
1162 SHA1Init(&sha1_ctx);
1163 memset(&csum, 0, sizeof(csum));
1164 csum.output_sha1 = &sha1_ctx;
1166 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1171 SHA1Final(id.sha1, &sha1_ctx);
1172 if (got_object_id_cmp(expected_id, &id) != 0) {
1173 err = got_error_checksum(expected_id);
1177 err = got_object_parse_header(&obj, p, len);
1181 if (len < obj->hdrlen + obj->size) {
1182 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1186 /* Skip object header. */
1188 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1192 got_object_close(obj);
1196 const struct got_error *
1197 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1199 const struct got_error *err = NULL;
1200 static const size_t blocksize = 512;
1201 size_t n, total, remain;
1207 buf = malloc(blocksize);
1209 return got_error_from_errno("malloc");
1216 newbuf = reallocarray(buf, 1, total + blocksize);
1217 if (newbuf == NULL) {
1218 err = got_error_from_errno("reallocarray");
1222 remain += blocksize;
1224 n = fread(buf + total, 1, remain, f);
1227 err = got_ferror(f, GOT_ERR_IO);