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((unsigned char)*p) &&
413 !isdigit((unsigned char)*(p + 1)))
414 return got_error(GOT_ERR_BAD_OBJ_DATA);
415 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
418 if (!isdigit((unsigned char)*p) &&
419 !isdigit((unsigned char)*(p + 1)))
420 return got_error(GOT_ERR_BAD_OBJ_DATA);
421 m = ((*p - '0') * 10) + (*(p + 1) - '0');
423 *gmtoff = (h * 60 * 60 + m * 60) * sign;
427 static const struct got_error *
428 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
430 const struct got_error *err = NULL;
434 /* Parse and strip off trailing timezone indicator string. */
435 space = strrchr(committer, ' ');
437 return got_error(GOT_ERR_BAD_OBJ_DATA);
438 tzstr = strdup(space + 1);
440 return got_error_from_errno("strdup");
441 err = parse_gmtoff(gmtoff, tzstr);
444 if (err->code != GOT_ERR_BAD_OBJ_DATA)
446 /* Old versions of Git omitted the timestamp. */
453 /* Timestamp is separated from committer name + email by space. */
454 space = strrchr(committer, ' ');
456 return got_error(GOT_ERR_BAD_OBJ_DATA);
458 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
459 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
461 return got_error(GOT_ERR_BAD_OBJ_DATA);
463 /* Strip off parsed time information, leaving just author and email. */
470 got_object_commit_close(struct got_commit_object *commit)
472 if (commit->refcnt > 0) {
474 if (commit->refcnt > 0)
478 got_object_id_queue_free(&commit->parent_ids);
479 free(commit->tree_id);
480 free(commit->author);
481 free(commit->committer);
482 free(commit->logmsg);
486 struct got_object_id *
487 got_object_commit_get_tree_id(struct got_commit_object *commit)
489 return commit->tree_id;
493 got_object_commit_get_nparents(struct got_commit_object *commit)
495 return commit->nparents;
498 const struct got_object_id_queue *
499 got_object_commit_get_parent_ids(struct got_commit_object *commit)
501 return &commit->parent_ids;
505 got_object_commit_get_author(struct got_commit_object *commit)
507 return commit->author;
511 got_object_commit_get_author_time(struct got_commit_object *commit)
513 return commit->author_time;
516 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
518 return commit->author_gmtoff;
522 got_object_commit_get_committer(struct got_commit_object *commit)
524 return commit->committer;
528 got_object_commit_get_committer_time(struct got_commit_object *commit)
530 return commit->committer_time;
534 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
536 return commit->committer_gmtoff;
539 const struct got_error *
540 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
542 const struct got_error *err = NULL;
547 len = strlen(commit->logmsg);
548 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
550 return got_error_from_errno("malloc");
553 * Strip out unusual headers. Headers are separated from the commit
554 * message body by a single empty line.
556 src = commit->logmsg;
558 while (*src != '\0' && *src != '\n') {
559 int copy_header = 1, eol = 0;
560 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
561 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
562 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
563 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
564 strncmp(src, GOT_COMMIT_LABEL_PARENT,
565 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
566 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
567 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
570 while (*src != '\0' && !eol) {
582 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
583 err = got_error(GOT_ERR_NO_SPACE);
587 /* Trim redundant trailing whitespace. */
588 len = strlen(*logmsg);
589 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
590 isspace((unsigned char)(*logmsg)[len - 1])) {
591 (*logmsg)[len - 1] = '\0';
595 /* Append a trailing newline if missing. */
596 if (len > 0 && (*logmsg)[len - 1] != '\n') {
597 (*logmsg)[len] = '\n';
598 (*logmsg)[len + 1] = '\0';
609 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
611 return commit->logmsg;
614 const struct got_error *
615 got_object_parse_commit(struct got_commit_object **commit, char *buf,
618 const struct got_error *err = NULL;
621 ssize_t remain = (ssize_t)len;
624 return got_error(GOT_ERR_BAD_OBJ_DATA);
626 *commit = got_object_commit_alloc_partial();
628 return got_error_from_errno("got_object_commit_alloc_partial");
630 label_len = strlen(GOT_COMMIT_LABEL_TREE);
631 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
633 if (remain < SHA1_DIGEST_STRING_LENGTH) {
634 err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 remain -= SHA1_DIGEST_STRING_LENGTH;
643 s += SHA1_DIGEST_STRING_LENGTH;
645 err = got_error(GOT_ERR_BAD_OBJ_DATA);
649 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
650 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
652 if (remain < SHA1_DIGEST_STRING_LENGTH) {
653 err = got_error(GOT_ERR_BAD_OBJ_DATA);
657 err = got_object_commit_add_parent(*commit, s);
661 remain -= SHA1_DIGEST_STRING_LENGTH;
662 s += SHA1_DIGEST_STRING_LENGTH;
665 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
666 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
672 err = got_error(GOT_ERR_BAD_OBJ_DATA);
676 p = memchr(s, '\n', remain);
678 err = got_error(GOT_ERR_BAD_OBJ_DATA);
683 err = parse_commit_time(&(*commit)->author_time,
684 &(*commit)->author_gmtoff, s);
687 (*commit)->author = strdup(s);
688 if ((*commit)->author == NULL) {
689 err = got_error_from_errno("strdup");
696 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
697 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
703 err = got_error(GOT_ERR_BAD_OBJ_DATA);
707 p = memchr(s, '\n', remain);
709 err = got_error(GOT_ERR_BAD_OBJ_DATA);
714 err = parse_commit_time(&(*commit)->committer_time,
715 &(*commit)->committer_gmtoff, s);
718 (*commit)->committer = strdup(s);
719 if ((*commit)->committer == NULL) {
720 err = got_error_from_errno("strdup");
727 (*commit)->logmsg = strndup(s, remain);
728 if ((*commit)->logmsg == NULL) {
729 err = got_error_from_errno("strndup");
734 got_object_commit_close(*commit);
740 const struct got_error *
741 got_object_read_commit(struct got_commit_object **commit, int fd,
742 struct got_object_id *expected_id, size_t expected_size)
744 struct got_object *obj = NULL;
745 const struct got_error *err = NULL;
748 struct got_inflate_checksum csum;
750 struct got_object_id id;
753 memset(&csum, 0, sizeof(csum));
754 csum.output_sha1 = &sha1_ctx;
756 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
760 SHA1Final(id.sha1, &sha1_ctx);
761 if (got_object_id_cmp(expected_id, &id) != 0) {
762 char buf[SHA1_DIGEST_STRING_LENGTH];
763 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
764 "checksum failure for object %s",
765 got_sha1_digest_to_str(expected_id->sha1, buf,
770 err = got_object_parse_header(&obj, p, len);
774 if (len < obj->hdrlen + obj->size) {
775 err = got_error(GOT_ERR_BAD_OBJ_DATA);
779 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
780 err = got_error(GOT_ERR_OBJ_TYPE);
784 /* Skip object header. */
786 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
790 got_object_close(obj);
795 got_object_tree_close(struct got_tree_object *tree)
797 if (tree->refcnt > 0) {
799 if (tree->refcnt > 0)
807 static const struct got_error *
808 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
815 *elen = strnlen(buf, maxlen) + 1;
817 return got_error(GOT_ERR_BAD_OBJ_DATA);
819 space = memchr(buf, ' ', *elen);
820 if (space == NULL || space <= buf)
821 return got_error(GOT_ERR_BAD_OBJ_DATA);
826 if (*p < '0' || *p > '7')
827 return got_error(GOT_ERR_BAD_OBJ_DATA);
829 pte->mode |= *p - '0';
833 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
834 return got_error(GOT_ERR_BAD_OBJ_DATA);
836 pte->name = space + 1;
837 pte->namelen = strlen(pte->name);
840 *elen += SHA1_DIGEST_LENGTH;
845 pte_cmp(const void *pa, const void *pb)
847 const struct got_parsed_tree_entry *a = pa, *b = pb;
849 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
852 const struct got_error *
853 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
854 size_t *nentries_alloc, uint8_t *buf, size_t len)
856 const struct got_error *err = NULL;
858 const size_t nalloc = 16;
859 struct got_parsed_tree_entry *pte;
864 return NULL; /* tree is empty */
869 if (*nentries >= *nentries_alloc) {
870 pte = recallocarray(*entries, *nentries_alloc,
871 *nentries_alloc + nalloc, sizeof(**entries));
873 err = got_error_from_errno("recallocarray");
877 *nentries_alloc += nalloc;
880 pte = &(*entries)[*nentries];
881 err = parse_tree_entry(pte, &elen, buf, remain);
890 err = got_error(GOT_ERR_BAD_OBJ_DATA);
895 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
897 for (i = 0; i < *nentries - 1; i++) {
898 struct got_parsed_tree_entry *prev = &(*entries)[i];
899 pte = &(*entries)[i + 1];
900 if (got_path_cmp(prev->name, pte->name,
901 prev->namelen, pte->namelen) == 0) {
902 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
913 const struct got_error *
914 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
915 size_t *nentries_alloc, uint8_t **p, int fd,
916 struct got_object_id *expected_id)
918 const struct got_error *err = NULL;
919 struct got_object *obj = NULL;
921 struct got_inflate_checksum csum;
923 struct got_object_id id;
926 memset(&csum, 0, sizeof(csum));
927 csum.output_sha1 = &sha1_ctx;
929 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
933 SHA1Final(id.sha1, &sha1_ctx);
934 if (got_object_id_cmp(expected_id, &id) != 0) {
935 char buf[SHA1_DIGEST_STRING_LENGTH];
936 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
937 "checksum failure for object %s",
938 got_sha1_digest_to_str(expected_id->sha1, buf,
943 err = got_object_parse_header(&obj, *p, len);
947 if (len < obj->hdrlen + obj->size) {
948 err = got_error(GOT_ERR_BAD_OBJ_DATA);
952 /* Skip object header. */
954 err = got_object_parse_tree(entries, nentries, nentries_alloc,
955 *p + obj->hdrlen, len);
958 got_object_close(obj);
963 got_object_tag_close(struct got_tag_object *tag)
965 if (tag->refcnt > 0) {
977 const struct got_error *
978 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
980 const struct got_error *err = NULL;
986 return got_error(GOT_ERR_BAD_OBJ_DATA);
988 *tag = calloc(1, sizeof(**tag));
990 return got_error_from_errno("calloc");
992 label_len = strlen(GOT_TAG_LABEL_OBJECT);
993 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
995 if (remain < SHA1_DIGEST_STRING_LENGTH) {
996 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1000 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
1001 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1004 remain -= SHA1_DIGEST_STRING_LENGTH;
1005 s += SHA1_DIGEST_STRING_LENGTH;
1007 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1012 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1016 label_len = strlen(GOT_TAG_LABEL_TYPE);
1017 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1018 remain -= label_len;
1020 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1024 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1025 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1026 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1027 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1029 remain -= label_len;
1030 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1031 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1032 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1033 label_len = strlen(GOT_OBJ_LABEL_TREE);
1035 remain -= label_len;
1036 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1037 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1038 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1039 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1041 remain -= label_len;
1042 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1043 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1044 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1045 label_len = strlen(GOT_OBJ_LABEL_TAG);
1047 remain -= label_len;
1049 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1053 if (remain <= 0 || *s != '\n') {
1054 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1060 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1064 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1068 label_len = strlen(GOT_TAG_LABEL_TAG);
1069 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1072 remain -= label_len;
1074 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1078 p = memchr(s, '\n', remain);
1080 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1085 (*tag)->tag = strndup(s, slen);
1086 if ((*tag)->tag == NULL) {
1087 err = got_error_from_errno("strndup");
1093 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1097 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1101 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1102 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1106 remain -= label_len;
1108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1112 p = memchr(s, '\n', remain);
1114 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1119 err = parse_commit_time(&(*tag)->tagger_time,
1120 &(*tag)->tagger_gmtoff, s);
1123 (*tag)->tagger = strdup(s);
1124 if ((*tag)->tagger == NULL) {
1125 err = got_error_from_errno("strdup");
1131 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1135 /* Some old tags in the Linux git repo have no tagger. */
1136 (*tag)->tagger = strdup("");
1137 if ((*tag)->tagger == NULL) {
1138 err = got_error_from_errno("strdup");
1143 (*tag)->tagmsg = strndup(s, remain);
1144 if ((*tag)->tagmsg == NULL) {
1145 err = got_error_from_errno("strndup");
1150 got_object_tag_close(*tag);
1156 const struct got_error *
1157 got_object_read_tag(struct got_tag_object **tag, int fd,
1158 struct got_object_id *expected_id, size_t expected_size)
1160 const struct got_error *err = NULL;
1161 struct got_object *obj = NULL;
1164 struct got_inflate_checksum csum;
1166 struct got_object_id id;
1168 SHA1Init(&sha1_ctx);
1169 memset(&csum, 0, sizeof(csum));
1170 csum.output_sha1 = &sha1_ctx;
1172 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1177 SHA1Final(id.sha1, &sha1_ctx);
1178 if (got_object_id_cmp(expected_id, &id) != 0) {
1179 char buf[SHA1_DIGEST_STRING_LENGTH];
1180 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1181 "checksum failure for object %s",
1182 got_sha1_digest_to_str(expected_id->sha1, buf,
1187 err = got_object_parse_header(&obj, p, len);
1191 if (len < obj->hdrlen + obj->size) {
1192 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1196 /* Skip object header. */
1198 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1202 got_object_close(obj);
1206 const struct got_error *
1207 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1209 const struct got_error *err = NULL;
1210 static const size_t blocksize = 512;
1211 size_t n, total, remain;
1217 buf = malloc(blocksize);
1219 return got_error_from_errno("malloc");
1226 newbuf = reallocarray(buf, 1, total + blocksize);
1227 if (newbuf == NULL) {
1228 err = got_error_from_errno("reallocarray");
1232 remain += blocksize;
1234 n = fread(buf + total, 1, remain, f);
1237 err = got_ferror(f, GOT_ERR_IO);