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 = SHA1_DIGEST_STRING_LENGTH;
92 *outbuf = malloc(len);
94 return got_error_from_errno("malloc");
96 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
99 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
106 got_object_close(struct got_object *obj)
108 if (obj->refcnt > 0) {
114 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
115 struct got_delta *delta;
116 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
117 delta = STAILQ_FIRST(&obj->deltas.entries);
118 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
125 const struct got_error *
126 got_object_raw_close(struct got_raw_object *obj)
128 const struct got_error *err = NULL;
130 if (obj->refcnt > 0) {
139 if (obj->f == NULL) {
141 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
142 err = got_error_from_errno("munmap");
143 if (close(obj->fd) == -1 && err == NULL)
144 err = got_error_from_errno("close");
148 if (fclose(obj->f) == EOF && err == NULL)
149 err = got_error_from_errno("fclose");
156 got_object_qid_free(struct got_object_qid *qid)
162 got_object_id_queue_free(struct got_object_id_queue *ids)
164 struct got_object_qid *qid;
166 while (!STAILQ_EMPTY(ids)) {
167 qid = STAILQ_FIRST(ids);
168 STAILQ_REMOVE_HEAD(ids, entry);
169 got_object_qid_free(qid);
173 const struct got_error *
174 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
176 const char *obj_labels[] = {
177 GOT_OBJ_LABEL_COMMIT,
182 const int obj_types[] = {
195 end = memchr(buf, '\0', len);
197 return got_error(GOT_ERR_BAD_OBJ_HDR);
199 for (i = 0; i < nitems(obj_labels); i++) {
200 const char *label = obj_labels[i];
201 size_t label_len = strlen(label);
204 if (len <= label_len || buf + label_len >= end ||
205 strncmp(buf, label, label_len) != 0)
209 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
211 return got_error(GOT_ERR_BAD_OBJ_HDR);
216 return got_error(GOT_ERR_BAD_OBJ_HDR);
218 *obj = calloc(1, sizeof(**obj));
220 return got_error_from_errno("calloc");
222 (*obj)->hdrlen = end - buf + 1;
227 const struct got_error *
228 got_object_read_header(struct got_object **obj, int fd)
230 const struct got_error *err;
231 struct got_inflate_buf zb;
233 const size_t zbsize = 64;
234 size_t outlen, totlen;
239 buf = malloc(zbsize);
241 return got_error_from_errno("malloc");
244 err = got_inflate_init(&zb, buf, zbsize, NULL);
250 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
256 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
259 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
260 if (newbuf == NULL) {
261 err = got_error_from_errno("recallocarray");
265 zb.outbuf = newbuf + totlen;
266 zb.outlen = (nbuf * zbsize) - totlen;
268 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
270 err = got_object_parse_header(obj, buf, totlen);
273 got_inflate_end(&zb);
277 const struct got_error *
278 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
279 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
282 const struct got_error *err = NULL;
283 struct got_object *obj;
284 struct got_inflate_checksum csum;
285 uint8_t sha1[SHA1_DIGEST_LENGTH];
287 size_t len, consumed;
295 memset(&csum, 0, sizeof(csum));
296 csum.output_sha1 = &sha1_ctx;
298 if (lseek(infd, SEEK_SET, 0) == -1)
299 return got_error_from_errno("lseek");
301 err = got_object_read_header(&obj, infd);
305 if (lseek(infd, SEEK_SET, 0) == -1)
306 return got_error_from_errno("lseek");
308 if (obj->size + obj->hdrlen <= max_in_mem_size) {
309 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
310 obj->size + obj->hdrlen, infd);
314 * XXX This uses an extra file descriptor for no good reason.
315 * We should have got_inflate_fd_to_fd().
319 return got_error_from_errno("dup");
322 err = got_error_from_errno("fdopen");
327 err = got_inflate_to_fd(&len, f, &csum, outfd);
332 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
333 err = got_error(GOT_ERR_BAD_OBJ_HDR);
337 SHA1Final(sha1, &sha1_ctx);
338 if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
339 char buf[SHA1_DIGEST_STRING_LENGTH];
340 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
341 "checksum failure for object %s",
342 got_sha1_digest_to_str(expected_id->sha1, buf,
348 *hdrlen = obj->hdrlen;
350 got_object_close(obj);
351 if (f && fclose(f) == EOF && err == NULL)
352 err = got_error_from_errno("fclose");
356 struct got_commit_object *
357 got_object_commit_alloc_partial(void)
359 struct got_commit_object *commit;
361 commit = calloc(1, sizeof(*commit));
364 commit->tree_id = malloc(sizeof(*commit->tree_id));
365 if (commit->tree_id == NULL) {
370 STAILQ_INIT(&commit->parent_ids);
375 const struct got_error *
376 got_object_commit_add_parent(struct got_commit_object *commit,
379 const struct got_error *err = NULL;
380 struct got_object_qid *qid;
382 err = got_object_qid_alloc_partial(&qid);
386 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
387 err = got_error(GOT_ERR_BAD_OBJ_DATA);
388 got_object_qid_free(qid);
392 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
398 static const struct got_error *
399 parse_gmtoff(time_t *gmtoff, const char *tzstr)
402 const char *p = tzstr;
410 return got_error(GOT_ERR_BAD_OBJ_DATA);
412 if (!isdigit(*p) && !isdigit(*(p + 1)))
413 return got_error(GOT_ERR_BAD_OBJ_DATA);
414 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
417 if (!isdigit(*p) && !isdigit(*(p + 1)))
418 return got_error(GOT_ERR_BAD_OBJ_DATA);
419 m = ((*p - '0') * 10) + (*(p + 1) - '0');
421 *gmtoff = (h * 60 * 60 + m * 60) * sign;
425 static const struct got_error *
426 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
428 const struct got_error *err = NULL;
432 /* Parse and strip off trailing timezone indicator string. */
433 space = strrchr(committer, ' ');
435 return got_error(GOT_ERR_BAD_OBJ_DATA);
436 tzstr = strdup(space + 1);
438 return got_error_from_errno("strdup");
439 err = parse_gmtoff(gmtoff, tzstr);
442 if (err->code != GOT_ERR_BAD_OBJ_DATA)
444 /* Old versions of Git omitted the timestamp. */
451 /* Timestamp is separated from committer name + email by space. */
452 space = strrchr(committer, ' ');
454 return got_error(GOT_ERR_BAD_OBJ_DATA);
456 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
457 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
459 return got_error(GOT_ERR_BAD_OBJ_DATA);
461 /* Strip off parsed time information, leaving just author and email. */
468 got_object_commit_close(struct got_commit_object *commit)
470 if (commit->refcnt > 0) {
472 if (commit->refcnt > 0)
476 got_object_id_queue_free(&commit->parent_ids);
477 free(commit->tree_id);
478 free(commit->author);
479 free(commit->committer);
480 free(commit->logmsg);
484 struct got_object_id *
485 got_object_commit_get_tree_id(struct got_commit_object *commit)
487 return commit->tree_id;
491 got_object_commit_get_nparents(struct got_commit_object *commit)
493 return commit->nparents;
496 const struct got_object_id_queue *
497 got_object_commit_get_parent_ids(struct got_commit_object *commit)
499 return &commit->parent_ids;
503 got_object_commit_get_author(struct got_commit_object *commit)
505 return commit->author;
509 got_object_commit_get_author_time(struct got_commit_object *commit)
511 return commit->author_time;
514 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
516 return commit->author_gmtoff;
520 got_object_commit_get_committer(struct got_commit_object *commit)
522 return commit->committer;
526 got_object_commit_get_committer_time(struct got_commit_object *commit)
528 return commit->committer_time;
532 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
534 return commit->committer_gmtoff;
537 const struct got_error *
538 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
540 const struct got_error *err = NULL;
545 len = strlen(commit->logmsg);
546 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
548 return got_error_from_errno("malloc");
551 * Strip out unusual headers. Headers are separated from the commit
552 * message body by a single empty line.
554 src = commit->logmsg;
556 while (*src != '\0' && *src != '\n') {
557 int copy_header = 1, eol = 0;
558 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
559 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
560 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
561 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
562 strncmp(src, GOT_COMMIT_LABEL_PARENT,
563 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
564 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
565 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
568 while (*src != '\0' && !eol) {
580 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
581 err = got_error(GOT_ERR_NO_SPACE);
585 /* Trim redundant trailing whitespace. */
586 len = strlen(*logmsg);
587 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
588 isspace((unsigned char)(*logmsg)[len - 1])) {
589 (*logmsg)[len - 1] = '\0';
593 /* Append a trailing newline if missing. */
594 if (len > 0 && (*logmsg)[len - 1] != '\n') {
595 (*logmsg)[len] = '\n';
596 (*logmsg)[len + 1] = '\0';
607 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
609 return commit->logmsg;
612 const struct got_error *
613 got_object_parse_commit(struct got_commit_object **commit, char *buf,
616 const struct got_error *err = NULL;
619 ssize_t remain = (ssize_t)len;
622 return got_error(GOT_ERR_BAD_OBJ_DATA);
624 *commit = got_object_commit_alloc_partial();
626 return got_error_from_errno("got_object_commit_alloc_partial");
628 label_len = strlen(GOT_COMMIT_LABEL_TREE);
629 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
631 if (remain < SHA1_DIGEST_STRING_LENGTH) {
632 err = got_error(GOT_ERR_BAD_OBJ_DATA);
636 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 remain -= SHA1_DIGEST_STRING_LENGTH;
641 s += SHA1_DIGEST_STRING_LENGTH;
643 err = got_error(GOT_ERR_BAD_OBJ_DATA);
647 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
648 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
650 if (remain < SHA1_DIGEST_STRING_LENGTH) {
651 err = got_error(GOT_ERR_BAD_OBJ_DATA);
655 err = got_object_commit_add_parent(*commit, s);
659 remain -= SHA1_DIGEST_STRING_LENGTH;
660 s += SHA1_DIGEST_STRING_LENGTH;
663 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
664 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
670 err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 p = memchr(s, '\n', remain);
676 err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 err = parse_commit_time(&(*commit)->author_time,
682 &(*commit)->author_gmtoff, s);
685 (*commit)->author = strdup(s);
686 if ((*commit)->author == NULL) {
687 err = got_error_from_errno("strdup");
694 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
695 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
701 err = got_error(GOT_ERR_BAD_OBJ_DATA);
705 p = memchr(s, '\n', remain);
707 err = got_error(GOT_ERR_BAD_OBJ_DATA);
712 err = parse_commit_time(&(*commit)->committer_time,
713 &(*commit)->committer_gmtoff, s);
716 (*commit)->committer = strdup(s);
717 if ((*commit)->committer == NULL) {
718 err = got_error_from_errno("strdup");
725 (*commit)->logmsg = strndup(s, remain);
726 if ((*commit)->logmsg == NULL) {
727 err = got_error_from_errno("strndup");
732 got_object_commit_close(*commit);
738 const struct got_error *
739 got_object_read_commit(struct got_commit_object **commit, int fd,
740 struct got_object_id *expected_id, size_t expected_size)
742 struct got_object *obj = NULL;
743 const struct got_error *err = NULL;
746 struct got_inflate_checksum csum;
748 struct got_object_id id;
751 memset(&csum, 0, sizeof(csum));
752 csum.output_sha1 = &sha1_ctx;
754 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
758 SHA1Final(id.sha1, &sha1_ctx);
759 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
760 char buf[SHA1_DIGEST_STRING_LENGTH];
761 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
762 "checksum failure for object %s",
763 got_sha1_digest_to_str(expected_id->sha1, buf,
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 (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
933 char buf[SHA1_DIGEST_STRING_LENGTH];
934 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
935 "checksum failure for object %s",
936 got_sha1_digest_to_str(expected_id->sha1, buf,
941 err = got_object_parse_header(&obj, *p, len);
945 if (len < obj->hdrlen + obj->size) {
946 err = got_error(GOT_ERR_BAD_OBJ_DATA);
950 /* Skip object header. */
952 err = got_object_parse_tree(entries, nentries, nentries_alloc,
953 *p + obj->hdrlen, len);
956 got_object_close(obj);
961 got_object_tag_close(struct got_tag_object *tag)
963 if (tag->refcnt > 0) {
975 const struct got_error *
976 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
978 const struct got_error *err = NULL;
984 return got_error(GOT_ERR_BAD_OBJ_DATA);
986 *tag = calloc(1, sizeof(**tag));
988 return got_error_from_errno("calloc");
990 label_len = strlen(GOT_TAG_LABEL_OBJECT);
991 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
993 if (remain < SHA1_DIGEST_STRING_LENGTH) {
994 err = got_error(GOT_ERR_BAD_OBJ_DATA);
998 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
999 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1002 remain -= SHA1_DIGEST_STRING_LENGTH;
1003 s += SHA1_DIGEST_STRING_LENGTH;
1005 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1010 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1014 label_len = strlen(GOT_TAG_LABEL_TYPE);
1015 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1016 remain -= label_len;
1018 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1022 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1023 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1024 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1025 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1027 remain -= label_len;
1028 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1029 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1030 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1031 label_len = strlen(GOT_OBJ_LABEL_TREE);
1033 remain -= label_len;
1034 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1035 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1036 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1037 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1039 remain -= label_len;
1040 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1041 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1042 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1043 label_len = strlen(GOT_OBJ_LABEL_TAG);
1045 remain -= label_len;
1047 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 if (remain <= 0 || *s != '\n') {
1052 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1058 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1066 label_len = strlen(GOT_TAG_LABEL_TAG);
1067 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1070 remain -= label_len;
1072 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1076 p = memchr(s, '\n', remain);
1078 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1083 (*tag)->tag = strndup(s, slen);
1084 if ((*tag)->tag == NULL) {
1085 err = got_error_from_errno("strndup");
1091 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1095 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1099 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1100 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1104 remain -= label_len;
1106 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1110 p = memchr(s, '\n', remain);
1112 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1117 err = parse_commit_time(&(*tag)->tagger_time,
1118 &(*tag)->tagger_gmtoff, s);
1121 (*tag)->tagger = strdup(s);
1122 if ((*tag)->tagger == NULL) {
1123 err = got_error_from_errno("strdup");
1129 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1133 /* Some old tags in the Linux git repo have no tagger. */
1134 (*tag)->tagger = strdup("");
1135 if ((*tag)->tagger == NULL) {
1136 err = got_error_from_errno("strdup");
1141 (*tag)->tagmsg = strndup(s, remain);
1142 if ((*tag)->tagmsg == NULL) {
1143 err = got_error_from_errno("strndup");
1148 got_object_tag_close(*tag);
1154 const struct got_error *
1155 got_object_read_tag(struct got_tag_object **tag, int fd,
1156 struct got_object_id *expected_id, size_t expected_size)
1158 const struct got_error *err = NULL;
1159 struct got_object *obj = NULL;
1162 struct got_inflate_checksum csum;
1164 struct got_object_id id;
1166 SHA1Init(&sha1_ctx);
1167 memset(&csum, 0, sizeof(csum));
1168 csum.output_sha1 = &sha1_ctx;
1170 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1175 SHA1Final(id.sha1, &sha1_ctx);
1176 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
1177 char buf[SHA1_DIGEST_STRING_LENGTH];
1178 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1179 "checksum failure for object %s",
1180 got_sha1_digest_to_str(expected_id->sha1, buf,
1185 err = got_object_parse_header(&obj, p, len);
1189 if (len < obj->hdrlen + obj->size) {
1190 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1194 /* Skip object header. */
1196 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1200 got_object_close(obj);
1204 const struct got_error *
1205 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1207 const struct got_error *err = NULL;
1208 static const size_t blocksize = 512;
1209 size_t n, total, remain;
1215 buf = malloc(blocksize);
1217 return got_error_from_errno("malloc");
1224 newbuf = reallocarray(buf, 1, total + blocksize);
1225 if (newbuf == NULL) {
1226 err = got_error_from_errno("reallocarray");
1230 remain += blocksize;
1232 n = fread(buf + total, 1, remain, f);
1235 err = got_ferror(f, GOT_ERR_IO);