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>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_repository.h"
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 struct got_object_id *
59 got_object_id_dup(struct got_object_id *id1)
61 struct got_object_id *id2;
63 id2 = malloc(sizeof(*id2));
66 memcpy(id2, id1, sizeof(*id2));
71 got_object_id_cmp(const struct got_object_id *id1,
72 const struct got_object_id *id2)
74 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
77 const struct got_error *
78 got_object_qid_alloc_partial(struct got_object_qid **qid)
80 *qid = malloc(sizeof(**qid));
82 return got_error_from_errno("malloc");
88 const struct got_error *
89 got_object_id_str(char **outbuf, struct got_object_id *id)
91 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
93 *outbuf = malloc(len);
95 return got_error_from_errno("malloc");
97 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
107 got_object_close(struct got_object *obj)
109 if (obj->refcnt > 0) {
115 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
116 struct got_delta *delta;
117 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
118 delta = STAILQ_FIRST(&obj->deltas.entries);
119 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
126 const struct got_error *
127 got_object_raw_close(struct got_raw_object *obj)
129 const struct got_error *err = NULL;
131 if (obj->refcnt > 0) {
140 if (obj->f == NULL) {
142 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
143 err = got_error_from_errno("munmap");
144 if (close(obj->fd) == -1 && err == NULL)
145 err = got_error_from_errno("close");
149 if (fclose(obj->f) == EOF && err == NULL)
150 err = got_error_from_errno("fclose");
157 got_object_qid_free(struct got_object_qid *qid)
163 got_object_id_queue_free(struct got_object_id_queue *ids)
165 struct got_object_qid *qid;
167 while (!STAILQ_EMPTY(ids)) {
168 qid = STAILQ_FIRST(ids);
169 STAILQ_REMOVE_HEAD(ids, entry);
170 got_object_qid_free(qid);
174 const struct got_error *
175 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
177 const char *obj_labels[] = {
178 GOT_OBJ_LABEL_COMMIT,
183 const int obj_types[] = {
196 end = memchr(buf, '\0', len);
198 return got_error(GOT_ERR_BAD_OBJ_HDR);
200 for (i = 0; i < nitems(obj_labels); i++) {
201 const char *label = obj_labels[i];
202 size_t label_len = strlen(label);
205 if (len <= label_len || buf + label_len >= end ||
206 strncmp(buf, label, label_len) != 0)
210 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
212 return got_error(GOT_ERR_BAD_OBJ_HDR);
217 return got_error(GOT_ERR_BAD_OBJ_HDR);
219 *obj = calloc(1, sizeof(**obj));
221 return got_error_from_errno("calloc");
223 (*obj)->hdrlen = end - buf + 1;
228 const struct got_error *
229 got_object_read_header(struct got_object **obj, int fd)
231 const struct got_error *err;
232 struct got_inflate_buf zb;
234 const size_t zbsize = 64;
235 size_t outlen, totlen;
240 buf = malloc(zbsize);
242 return got_error_from_errno("malloc");
245 err = got_inflate_init(&zb, buf, zbsize, NULL);
251 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
257 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
260 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
261 if (newbuf == NULL) {
262 err = got_error_from_errno("recallocarray");
266 zb.outbuf = newbuf + totlen;
267 zb.outlen = (nbuf * zbsize) - totlen;
269 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
271 err = got_object_parse_header(obj, buf, totlen);
274 got_inflate_end(&zb);
278 const struct got_error *
279 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
280 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
283 const struct got_error *err = NULL;
284 struct got_object *obj;
285 struct got_inflate_checksum csum;
286 uint8_t sha1[SHA1_DIGEST_LENGTH];
288 size_t len, consumed;
296 memset(&csum, 0, sizeof(csum));
297 csum.output_sha1 = &sha1_ctx;
299 if (lseek(infd, SEEK_SET, 0) == -1)
300 return got_error_from_errno("lseek");
302 err = got_object_read_header(&obj, infd);
306 if (lseek(infd, SEEK_SET, 0) == -1)
307 return got_error_from_errno("lseek");
309 if (obj->size + obj->hdrlen <= max_in_mem_size) {
310 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
311 obj->size + obj->hdrlen, infd);
315 * XXX This uses an extra file descriptor for no good reason.
316 * We should have got_inflate_fd_to_fd().
320 return got_error_from_errno("dup");
323 err = got_error_from_errno("fdopen");
328 err = got_inflate_to_fd(&len, f, &csum, outfd);
333 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
334 err = got_error(GOT_ERR_BAD_OBJ_HDR);
338 SHA1Final(sha1, &sha1_ctx);
339 if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
340 char buf[SHA1_DIGEST_STRING_LENGTH];
341 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
342 "checksum failure for object %s",
343 got_sha1_digest_to_str(expected_id->sha1, buf,
349 *hdrlen = obj->hdrlen;
351 got_object_close(obj);
352 if (f && fclose(f) == EOF && err == NULL)
353 err = got_error_from_errno("fclose");
357 struct got_commit_object *
358 got_object_commit_alloc_partial(void)
360 struct got_commit_object *commit;
362 commit = calloc(1, sizeof(*commit));
365 commit->tree_id = malloc(sizeof(*commit->tree_id));
366 if (commit->tree_id == NULL) {
371 STAILQ_INIT(&commit->parent_ids);
376 const struct got_error *
377 got_object_commit_add_parent(struct got_commit_object *commit,
380 const struct got_error *err = NULL;
381 struct got_object_qid *qid;
383 err = got_object_qid_alloc_partial(&qid);
387 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
388 err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 got_object_qid_free(qid);
393 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
399 static const struct got_error *
400 parse_gmtoff(time_t *gmtoff, const char *tzstr)
403 const char *p = tzstr;
411 return got_error(GOT_ERR_BAD_OBJ_DATA);
413 if (!isdigit(*p) && !isdigit(*(p + 1)))
414 return got_error(GOT_ERR_BAD_OBJ_DATA);
415 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
418 if (!isdigit(*p) && !isdigit(*(p + 1)))
419 return got_error(GOT_ERR_BAD_OBJ_DATA);
420 m = ((*p - '0') * 10) + (*(p + 1) - '0');
422 *gmtoff = (h * 60 * 60 + m * 60) * sign;
426 static const struct got_error *
427 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
429 const struct got_error *err = NULL;
433 /* Parse and strip off trailing timezone indicator string. */
434 space = strrchr(committer, ' ');
436 return got_error(GOT_ERR_BAD_OBJ_DATA);
437 tzstr = strdup(space + 1);
439 return got_error_from_errno("strdup");
440 err = parse_gmtoff(gmtoff, tzstr);
443 if (err->code != GOT_ERR_BAD_OBJ_DATA)
445 /* Old versions of Git omitted the timestamp. */
452 /* Timestamp is separated from committer name + email by space. */
453 space = strrchr(committer, ' ');
455 return got_error(GOT_ERR_BAD_OBJ_DATA);
457 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
458 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
460 return got_error(GOT_ERR_BAD_OBJ_DATA);
462 /* Strip off parsed time information, leaving just author and email. */
469 got_object_commit_close(struct got_commit_object *commit)
471 if (commit->refcnt > 0) {
473 if (commit->refcnt > 0)
477 got_object_id_queue_free(&commit->parent_ids);
478 free(commit->tree_id);
479 free(commit->author);
480 free(commit->committer);
481 free(commit->logmsg);
485 struct got_object_id *
486 got_object_commit_get_tree_id(struct got_commit_object *commit)
488 return commit->tree_id;
492 got_object_commit_get_nparents(struct got_commit_object *commit)
494 return commit->nparents;
497 const struct got_object_id_queue *
498 got_object_commit_get_parent_ids(struct got_commit_object *commit)
500 return &commit->parent_ids;
504 got_object_commit_get_author(struct got_commit_object *commit)
506 return commit->author;
510 got_object_commit_get_author_time(struct got_commit_object *commit)
512 return commit->author_time;
515 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
517 return commit->author_gmtoff;
521 got_object_commit_get_committer(struct got_commit_object *commit)
523 return commit->committer;
527 got_object_commit_get_committer_time(struct got_commit_object *commit)
529 return commit->committer_time;
533 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
535 return commit->committer_gmtoff;
538 const struct got_error *
539 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
541 const struct got_error *err = NULL;
546 len = strlen(commit->logmsg);
547 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
549 return got_error_from_errno("malloc");
552 * Strip out unusual headers. Headers are separated from the commit
553 * message body by a single empty line.
555 src = commit->logmsg;
557 while (*src != '\0' && *src != '\n') {
558 int copy_header = 1, eol = 0;
559 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
560 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
561 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
562 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
563 strncmp(src, GOT_COMMIT_LABEL_PARENT,
564 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
565 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
566 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
569 while (*src != '\0' && !eol) {
581 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
582 err = got_error(GOT_ERR_NO_SPACE);
586 /* Trim redundant trailing whitespace. */
587 len = strlen(*logmsg);
588 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
589 isspace((unsigned char)(*logmsg)[len - 1])) {
590 (*logmsg)[len - 1] = '\0';
594 /* Append a trailing newline if missing. */
595 if (len > 0 && (*logmsg)[len - 1] != '\n') {
596 (*logmsg)[len] = '\n';
597 (*logmsg)[len + 1] = '\0';
608 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
610 return commit->logmsg;
613 const struct got_error *
614 got_object_parse_commit(struct got_commit_object **commit, char *buf,
617 const struct got_error *err = NULL;
620 ssize_t remain = (ssize_t)len;
623 return got_error(GOT_ERR_BAD_OBJ_DATA);
625 *commit = got_object_commit_alloc_partial();
627 return got_error_from_errno("got_object_commit_alloc_partial");
629 label_len = strlen(GOT_COMMIT_LABEL_TREE);
630 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
632 if (remain < SHA1_DIGEST_STRING_LENGTH) {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 remain -= SHA1_DIGEST_STRING_LENGTH;
642 s += SHA1_DIGEST_STRING_LENGTH;
644 err = got_error(GOT_ERR_BAD_OBJ_DATA);
648 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
649 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
651 if (remain < SHA1_DIGEST_STRING_LENGTH) {
652 err = got_error(GOT_ERR_BAD_OBJ_DATA);
656 err = got_object_commit_add_parent(*commit, s);
660 remain -= SHA1_DIGEST_STRING_LENGTH;
661 s += SHA1_DIGEST_STRING_LENGTH;
664 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
665 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
671 err = got_error(GOT_ERR_BAD_OBJ_DATA);
675 p = memchr(s, '\n', remain);
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
682 err = parse_commit_time(&(*commit)->author_time,
683 &(*commit)->author_gmtoff, s);
686 (*commit)->author = strdup(s);
687 if ((*commit)->author == NULL) {
688 err = got_error_from_errno("strdup");
695 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
696 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
702 err = got_error(GOT_ERR_BAD_OBJ_DATA);
706 p = memchr(s, '\n', remain);
708 err = got_error(GOT_ERR_BAD_OBJ_DATA);
713 err = parse_commit_time(&(*commit)->committer_time,
714 &(*commit)->committer_gmtoff, s);
717 (*commit)->committer = strdup(s);
718 if ((*commit)->committer == NULL) {
719 err = got_error_from_errno("strdup");
726 (*commit)->logmsg = strndup(s, remain);
727 if ((*commit)->logmsg == NULL) {
728 err = got_error_from_errno("strndup");
733 got_object_commit_close(*commit);
739 const struct got_error *
740 got_object_read_commit(struct got_commit_object **commit, int fd,
741 struct got_object_id *expected_id, size_t expected_size)
743 struct got_object *obj = NULL;
744 const struct got_error *err = NULL;
747 struct got_inflate_checksum csum;
749 struct got_object_id id;
752 memset(&csum, 0, sizeof(csum));
753 csum.output_sha1 = &sha1_ctx;
755 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
759 SHA1Final(id.sha1, &sha1_ctx);
760 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
761 char buf[SHA1_DIGEST_STRING_LENGTH];
762 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
763 "checksum failure for object %s",
764 got_sha1_digest_to_str(expected_id->sha1, buf,
769 err = got_object_parse_header(&obj, p, len);
773 if (len < obj->hdrlen + obj->size) {
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
778 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
779 err = got_error(GOT_ERR_OBJ_TYPE);
783 /* Skip object header. */
785 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
789 got_object_close(obj);
794 got_object_tree_close(struct got_tree_object *tree)
796 if (tree->refcnt > 0) {
798 if (tree->refcnt > 0)
806 static const struct got_error *
807 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
814 *elen = strnlen(buf, maxlen) + 1;
816 return got_error(GOT_ERR_BAD_OBJ_DATA);
818 space = memchr(buf, ' ', *elen);
819 if (space == NULL || space <= buf)
820 return got_error(GOT_ERR_BAD_OBJ_DATA);
825 if (*p < '0' && *p > '7')
826 return got_error(GOT_ERR_BAD_OBJ_DATA);
828 pte->mode |= *p - '0';
832 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
833 return got_error(GOT_ERR_BAD_OBJ_DATA);
835 pte->name = space + 1;
836 pte->namelen = strlen(pte->name);
839 *elen += SHA1_DIGEST_LENGTH;
844 pte_cmp(const void *pa, const void *pb)
846 const struct got_parsed_tree_entry *a = pa, *b = pb;
848 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
851 const struct got_error *
852 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
853 size_t *nentries_alloc, uint8_t *buf, size_t len)
855 const struct got_error *err = NULL;
857 const size_t nalloc = 16;
858 struct got_parsed_tree_entry *pte;
863 return NULL; /* tree is empty */
868 if (*nentries >= *nentries_alloc) {
869 pte = recallocarray(*entries, *nentries_alloc,
870 *nentries_alloc + nalloc, sizeof(**entries));
872 err = got_error_from_errno("recallocarray");
876 *nentries_alloc += nalloc;
879 pte = &(*entries)[*nentries];
880 err = parse_tree_entry(pte, &elen, buf, remain);
889 err = got_error(GOT_ERR_BAD_OBJ_DATA);
894 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
896 for (i = 0; i < *nentries - 1; i++) {
897 struct got_parsed_tree_entry *prev = &(*entries)[i];
898 pte = &(*entries)[i + 1];
899 if (got_path_cmp(prev->name, pte->name,
900 prev->namelen, pte->namelen) == 0) {
901 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
912 const struct got_error *
913 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
914 size_t *nentries_alloc, uint8_t **p, int fd,
915 struct got_object_id *expected_id)
917 const struct got_error *err = NULL;
918 struct got_object *obj = NULL;
920 struct got_inflate_checksum csum;
922 struct got_object_id id;
925 memset(&csum, 0, sizeof(csum));
926 csum.output_sha1 = &sha1_ctx;
928 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
932 SHA1Final(id.sha1, &sha1_ctx);
933 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
934 char buf[SHA1_DIGEST_STRING_LENGTH];
935 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
936 "checksum failure for object %s",
937 got_sha1_digest_to_str(expected_id->sha1, buf,
942 err = got_object_parse_header(&obj, *p, len);
946 if (len < obj->hdrlen + obj->size) {
947 err = got_error(GOT_ERR_BAD_OBJ_DATA);
951 /* Skip object header. */
953 err = got_object_parse_tree(entries, nentries, nentries_alloc,
954 *p + obj->hdrlen, len);
957 got_object_close(obj);
962 got_object_tag_close(struct got_tag_object *tag)
964 if (tag->refcnt > 0) {
976 const struct got_error *
977 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
979 const struct got_error *err = NULL;
985 return got_error(GOT_ERR_BAD_OBJ_DATA);
987 *tag = calloc(1, sizeof(**tag));
989 return got_error_from_errno("calloc");
991 label_len = strlen(GOT_TAG_LABEL_OBJECT);
992 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
994 if (remain < SHA1_DIGEST_STRING_LENGTH) {
995 err = got_error(GOT_ERR_BAD_OBJ_DATA);
999 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
1000 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1003 remain -= SHA1_DIGEST_STRING_LENGTH;
1004 s += SHA1_DIGEST_STRING_LENGTH;
1006 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1011 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1015 label_len = strlen(GOT_TAG_LABEL_TYPE);
1016 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1017 remain -= label_len;
1019 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1023 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1024 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1025 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1026 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1028 remain -= label_len;
1029 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1030 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1031 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1032 label_len = strlen(GOT_OBJ_LABEL_TREE);
1034 remain -= label_len;
1035 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1036 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1037 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1038 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1040 remain -= label_len;
1041 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1042 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1043 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1044 label_len = strlen(GOT_OBJ_LABEL_TAG);
1046 remain -= label_len;
1048 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1052 if (remain <= 0 || *s != '\n') {
1053 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1059 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1063 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1067 label_len = strlen(GOT_TAG_LABEL_TAG);
1068 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1071 remain -= label_len;
1073 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1077 p = memchr(s, '\n', remain);
1079 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1084 (*tag)->tag = strndup(s, slen);
1085 if ((*tag)->tag == NULL) {
1086 err = got_error_from_errno("strndup");
1092 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1096 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1100 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1101 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1105 remain -= label_len;
1107 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1111 p = memchr(s, '\n', remain);
1113 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1118 err = parse_commit_time(&(*tag)->tagger_time,
1119 &(*tag)->tagger_gmtoff, s);
1122 (*tag)->tagger = strdup(s);
1123 if ((*tag)->tagger == NULL) {
1124 err = got_error_from_errno("strdup");
1130 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1134 /* Some old tags in the Linux git repo have no tagger. */
1135 (*tag)->tagger = strdup("");
1136 if ((*tag)->tagger == NULL) {
1137 err = got_error_from_errno("strdup");
1142 (*tag)->tagmsg = strndup(s, remain);
1143 if ((*tag)->tagmsg == NULL) {
1144 err = got_error_from_errno("strndup");
1149 got_object_tag_close(*tag);
1155 const struct got_error *
1156 got_object_read_tag(struct got_tag_object **tag, int fd,
1157 struct got_object_id *expected_id, size_t expected_size)
1159 const struct got_error *err = NULL;
1160 struct got_object *obj = NULL;
1163 struct got_inflate_checksum csum;
1165 struct got_object_id id;
1167 SHA1Init(&sha1_ctx);
1168 memset(&csum, 0, sizeof(csum));
1169 csum.output_sha1 = &sha1_ctx;
1171 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1176 SHA1Final(id.sha1, &sha1_ctx);
1177 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
1178 char buf[SHA1_DIGEST_STRING_LENGTH];
1179 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1180 "checksum failure for object %s",
1181 got_sha1_digest_to_str(expected_id->sha1, buf,
1186 err = got_object_parse_header(&obj, p, len);
1190 if (len < obj->hdrlen + obj->size) {
1191 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1195 /* Skip object header. */
1197 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1201 got_object_close(obj);
1205 const struct got_error *
1206 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1208 const struct got_error *err = NULL;
1209 static const size_t blocksize = 512;
1210 size_t n, total, remain;
1216 buf = malloc(blocksize);
1218 return got_error_from_errno("malloc");
1225 newbuf = reallocarray(buf, 1, total + blocksize);
1226 if (newbuf == NULL) {
1227 err = got_error_from_errno("reallocarray");
1231 remain += blocksize;
1233 n = fread(buf + total, 1, remain, f);
1236 err = got_ferror(f, GOT_ERR_IO);