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((unsigned char)*p) &&
414 !isdigit((unsigned char)*(p + 1)))
415 return got_error(GOT_ERR_BAD_OBJ_DATA);
416 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
419 if (!isdigit((unsigned char)*p) &&
420 !isdigit((unsigned char)*(p + 1)))
421 return got_error(GOT_ERR_BAD_OBJ_DATA);
422 m = ((*p - '0') * 10) + (*(p + 1) - '0');
424 *gmtoff = (h * 60 * 60 + m * 60) * sign;
428 static const struct got_error *
429 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
431 const struct got_error *err = NULL;
435 /* Parse and strip off trailing timezone indicator string. */
436 space = strrchr(committer, ' ');
438 return got_error(GOT_ERR_BAD_OBJ_DATA);
439 tzstr = strdup(space + 1);
441 return got_error_from_errno("strdup");
442 err = parse_gmtoff(gmtoff, tzstr);
445 if (err->code != GOT_ERR_BAD_OBJ_DATA)
447 /* Old versions of Git omitted the timestamp. */
454 /* Timestamp is separated from committer name + email by space. */
455 space = strrchr(committer, ' ');
457 return got_error(GOT_ERR_BAD_OBJ_DATA);
459 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
460 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
462 return got_error(GOT_ERR_BAD_OBJ_DATA);
464 /* Strip off parsed time information, leaving just author and email. */
471 got_object_commit_close(struct got_commit_object *commit)
473 if (commit->refcnt > 0) {
475 if (commit->refcnt > 0)
479 got_object_id_queue_free(&commit->parent_ids);
480 free(commit->tree_id);
481 free(commit->author);
482 free(commit->committer);
483 free(commit->logmsg);
487 struct got_object_id *
488 got_object_commit_get_tree_id(struct got_commit_object *commit)
490 return commit->tree_id;
494 got_object_commit_get_nparents(struct got_commit_object *commit)
496 return commit->nparents;
499 const struct got_object_id_queue *
500 got_object_commit_get_parent_ids(struct got_commit_object *commit)
502 return &commit->parent_ids;
506 got_object_commit_get_author(struct got_commit_object *commit)
508 return commit->author;
512 got_object_commit_get_author_time(struct got_commit_object *commit)
514 return commit->author_time;
517 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
519 return commit->author_gmtoff;
523 got_object_commit_get_committer(struct got_commit_object *commit)
525 return commit->committer;
529 got_object_commit_get_committer_time(struct got_commit_object *commit)
531 return commit->committer_time;
535 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
537 return commit->committer_gmtoff;
540 const struct got_error *
541 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
543 const struct got_error *err = NULL;
548 len = strlen(commit->logmsg);
549 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
551 return got_error_from_errno("malloc");
554 * Strip out unusual headers. Headers are separated from the commit
555 * message body by a single empty line.
557 src = commit->logmsg;
559 while (*src != '\0' && *src != '\n') {
560 int copy_header = 1, eol = 0;
561 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
562 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
563 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
564 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
565 strncmp(src, GOT_COMMIT_LABEL_PARENT,
566 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
567 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
568 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
571 while (*src != '\0' && !eol) {
583 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
584 err = got_error(GOT_ERR_NO_SPACE);
588 /* Trim redundant trailing whitespace. */
589 len = strlen(*logmsg);
590 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
591 isspace((unsigned char)(*logmsg)[len - 1])) {
592 (*logmsg)[len - 1] = '\0';
596 /* Append a trailing newline if missing. */
597 if (len > 0 && (*logmsg)[len - 1] != '\n') {
598 (*logmsg)[len] = '\n';
599 (*logmsg)[len + 1] = '\0';
610 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
612 return commit->logmsg;
615 const struct got_error *
616 got_object_parse_commit(struct got_commit_object **commit, char *buf,
619 const struct got_error *err = NULL;
622 ssize_t remain = (ssize_t)len;
625 return got_error(GOT_ERR_BAD_OBJ_DATA);
627 *commit = got_object_commit_alloc_partial();
629 return got_error_from_errno("got_object_commit_alloc_partial");
631 label_len = strlen(GOT_COMMIT_LABEL_TREE);
632 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
634 if (remain < SHA1_DIGEST_STRING_LENGTH) {
635 err = got_error(GOT_ERR_BAD_OBJ_DATA);
639 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
640 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 remain -= SHA1_DIGEST_STRING_LENGTH;
644 s += SHA1_DIGEST_STRING_LENGTH;
646 err = got_error(GOT_ERR_BAD_OBJ_DATA);
650 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
651 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
653 if (remain < SHA1_DIGEST_STRING_LENGTH) {
654 err = got_error(GOT_ERR_BAD_OBJ_DATA);
658 err = got_object_commit_add_parent(*commit, s);
662 remain -= SHA1_DIGEST_STRING_LENGTH;
663 s += SHA1_DIGEST_STRING_LENGTH;
666 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
667 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
673 err = got_error(GOT_ERR_BAD_OBJ_DATA);
677 p = memchr(s, '\n', remain);
679 err = got_error(GOT_ERR_BAD_OBJ_DATA);
684 err = parse_commit_time(&(*commit)->author_time,
685 &(*commit)->author_gmtoff, s);
688 (*commit)->author = strdup(s);
689 if ((*commit)->author == NULL) {
690 err = got_error_from_errno("strdup");
697 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
698 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
704 err = got_error(GOT_ERR_BAD_OBJ_DATA);
708 p = memchr(s, '\n', remain);
710 err = got_error(GOT_ERR_BAD_OBJ_DATA);
715 err = parse_commit_time(&(*commit)->committer_time,
716 &(*commit)->committer_gmtoff, s);
719 (*commit)->committer = strdup(s);
720 if ((*commit)->committer == NULL) {
721 err = got_error_from_errno("strdup");
728 (*commit)->logmsg = strndup(s, remain);
729 if ((*commit)->logmsg == NULL) {
730 err = got_error_from_errno("strndup");
735 got_object_commit_close(*commit);
741 const struct got_error *
742 got_object_read_commit(struct got_commit_object **commit, int fd,
743 struct got_object_id *expected_id, size_t expected_size)
745 struct got_object *obj = NULL;
746 const struct got_error *err = NULL;
749 struct got_inflate_checksum csum;
751 struct got_object_id id;
754 memset(&csum, 0, sizeof(csum));
755 csum.output_sha1 = &sha1_ctx;
757 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
761 SHA1Final(id.sha1, &sha1_ctx);
762 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
763 char buf[SHA1_DIGEST_STRING_LENGTH];
764 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
765 "checksum failure for object %s",
766 got_sha1_digest_to_str(expected_id->sha1, buf,
771 err = got_object_parse_header(&obj, p, len);
775 if (len < obj->hdrlen + obj->size) {
776 err = got_error(GOT_ERR_BAD_OBJ_DATA);
780 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
781 err = got_error(GOT_ERR_OBJ_TYPE);
785 /* Skip object header. */
787 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
791 got_object_close(obj);
796 got_object_tree_close(struct got_tree_object *tree)
798 if (tree->refcnt > 0) {
800 if (tree->refcnt > 0)
808 static const struct got_error *
809 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
816 *elen = strnlen(buf, maxlen) + 1;
818 return got_error(GOT_ERR_BAD_OBJ_DATA);
820 space = memchr(buf, ' ', *elen);
821 if (space == NULL || space <= buf)
822 return got_error(GOT_ERR_BAD_OBJ_DATA);
827 if (*p < '0' && *p > '7')
828 return got_error(GOT_ERR_BAD_OBJ_DATA);
830 pte->mode |= *p - '0';
834 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
835 return got_error(GOT_ERR_BAD_OBJ_DATA);
837 pte->name = space + 1;
838 pte->namelen = strlen(pte->name);
841 *elen += SHA1_DIGEST_LENGTH;
846 pte_cmp(const void *pa, const void *pb)
848 const struct got_parsed_tree_entry *a = pa, *b = pb;
850 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
853 const struct got_error *
854 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
855 size_t *nentries_alloc, uint8_t *buf, size_t len)
857 const struct got_error *err = NULL;
859 const size_t nalloc = 16;
860 struct got_parsed_tree_entry *pte;
865 return NULL; /* tree is empty */
870 if (*nentries >= *nentries_alloc) {
871 pte = recallocarray(*entries, *nentries_alloc,
872 *nentries_alloc + nalloc, sizeof(**entries));
874 err = got_error_from_errno("recallocarray");
878 *nentries_alloc += nalloc;
881 pte = &(*entries)[*nentries];
882 err = parse_tree_entry(pte, &elen, buf, remain);
891 err = got_error(GOT_ERR_BAD_OBJ_DATA);
896 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
898 for (i = 0; i < *nentries - 1; i++) {
899 struct got_parsed_tree_entry *prev = &(*entries)[i];
900 pte = &(*entries)[i + 1];
901 if (got_path_cmp(prev->name, pte->name,
902 prev->namelen, pte->namelen) == 0) {
903 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
914 const struct got_error *
915 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
916 size_t *nentries_alloc, uint8_t **p, int fd,
917 struct got_object_id *expected_id)
919 const struct got_error *err = NULL;
920 struct got_object *obj = NULL;
922 struct got_inflate_checksum csum;
924 struct got_object_id id;
927 memset(&csum, 0, sizeof(csum));
928 csum.output_sha1 = &sha1_ctx;
930 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
934 SHA1Final(id.sha1, &sha1_ctx);
935 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
936 char buf[SHA1_DIGEST_STRING_LENGTH];
937 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
938 "checksum failure for object %s",
939 got_sha1_digest_to_str(expected_id->sha1, buf,
944 err = got_object_parse_header(&obj, *p, len);
948 if (len < obj->hdrlen + obj->size) {
949 err = got_error(GOT_ERR_BAD_OBJ_DATA);
953 /* Skip object header. */
955 err = got_object_parse_tree(entries, nentries, nentries_alloc,
956 *p + obj->hdrlen, len);
959 got_object_close(obj);
964 got_object_tag_close(struct got_tag_object *tag)
966 if (tag->refcnt > 0) {
978 const struct got_error *
979 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
981 const struct got_error *err = NULL;
987 return got_error(GOT_ERR_BAD_OBJ_DATA);
989 *tag = calloc(1, sizeof(**tag));
991 return got_error_from_errno("calloc");
993 label_len = strlen(GOT_TAG_LABEL_OBJECT);
994 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
996 if (remain < SHA1_DIGEST_STRING_LENGTH) {
997 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1001 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
1002 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1005 remain -= SHA1_DIGEST_STRING_LENGTH;
1006 s += SHA1_DIGEST_STRING_LENGTH;
1008 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1013 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1017 label_len = strlen(GOT_TAG_LABEL_TYPE);
1018 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1019 remain -= label_len;
1021 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1025 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1026 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1027 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1028 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1030 remain -= label_len;
1031 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1032 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1033 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1034 label_len = strlen(GOT_OBJ_LABEL_TREE);
1036 remain -= label_len;
1037 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1038 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1039 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1040 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1042 remain -= label_len;
1043 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1044 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1045 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1046 label_len = strlen(GOT_OBJ_LABEL_TAG);
1048 remain -= label_len;
1050 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1054 if (remain <= 0 || *s != '\n') {
1055 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1061 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1065 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1069 label_len = strlen(GOT_TAG_LABEL_TAG);
1070 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1073 remain -= label_len;
1075 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1079 p = memchr(s, '\n', remain);
1081 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1086 (*tag)->tag = strndup(s, slen);
1087 if ((*tag)->tag == NULL) {
1088 err = got_error_from_errno("strndup");
1094 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1098 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1102 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1103 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1107 remain -= label_len;
1109 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1113 p = memchr(s, '\n', remain);
1115 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1120 err = parse_commit_time(&(*tag)->tagger_time,
1121 &(*tag)->tagger_gmtoff, s);
1124 (*tag)->tagger = strdup(s);
1125 if ((*tag)->tagger == NULL) {
1126 err = got_error_from_errno("strdup");
1132 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1136 /* Some old tags in the Linux git repo have no tagger. */
1137 (*tag)->tagger = strdup("");
1138 if ((*tag)->tagger == NULL) {
1139 err = got_error_from_errno("strdup");
1144 (*tag)->tagmsg = strndup(s, remain);
1145 if ((*tag)->tagmsg == NULL) {
1146 err = got_error_from_errno("strndup");
1151 got_object_tag_close(*tag);
1157 const struct got_error *
1158 got_object_read_tag(struct got_tag_object **tag, int fd,
1159 struct got_object_id *expected_id, size_t expected_size)
1161 const struct got_error *err = NULL;
1162 struct got_object *obj = NULL;
1165 struct got_inflate_checksum csum;
1167 struct got_object_id id;
1169 SHA1Init(&sha1_ctx);
1170 memset(&csum, 0, sizeof(csum));
1171 csum.output_sha1 = &sha1_ctx;
1173 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1178 SHA1Final(id.sha1, &sha1_ctx);
1179 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
1180 char buf[SHA1_DIGEST_STRING_LENGTH];
1181 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1182 "checksum failure for object %s",
1183 got_sha1_digest_to_str(expected_id->sha1, buf,
1188 err = got_object_parse_header(&obj, p, len);
1192 if (len < obj->hdrlen + obj->size) {
1193 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1197 /* Skip object header. */
1199 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1203 got_object_close(obj);
1207 const struct got_error *
1208 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1210 const struct got_error *err = NULL;
1211 static const size_t blocksize = 512;
1212 size_t n, total, remain;
1218 buf = malloc(blocksize);
1220 return got_error_from_errno("malloc");
1227 newbuf = reallocarray(buf, 1, total + blocksize);
1228 if (newbuf == NULL) {
1229 err = got_error_from_errno("reallocarray");
1233 remain += blocksize;
1235 n = fread(buf + total, 1, remain, f);
1238 err = got_ferror(f, GOT_ERR_IO);