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>
37 #include "got_compat.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_hash.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 = GOT_OBJECT_ID_HEX_MAXLEN;
93 *outbuf = malloc(len);
95 return got_error_from_errno("malloc");
97 if (got_object_id_hex(id, *outbuf, len) == NULL) {
100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
107 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
109 return got_sha1_digest_to_str(id->sha1, buf, len);
113 got_object_close(struct got_object *obj)
115 if (obj->refcnt > 0) {
121 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
122 struct got_delta *delta;
123 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
124 delta = STAILQ_FIRST(&obj->deltas.entries);
125 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
132 const struct got_error *
133 got_object_raw_close(struct got_raw_object *obj)
135 const struct got_error *err = NULL;
137 if (obj->refcnt > 0) {
146 if (obj->f == NULL) {
148 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
149 err = got_error_from_errno("munmap");
150 if (close(obj->fd) == -1 && err == NULL)
151 err = got_error_from_errno("close");
155 if (fclose(obj->f) == EOF && err == NULL)
156 err = got_error_from_errno("fclose");
163 got_object_qid_free(struct got_object_qid *qid)
169 got_object_id_queue_free(struct got_object_id_queue *ids)
171 struct got_object_qid *qid;
173 while (!STAILQ_EMPTY(ids)) {
174 qid = STAILQ_FIRST(ids);
175 STAILQ_REMOVE_HEAD(ids, entry);
176 got_object_qid_free(qid);
180 const struct got_error *
181 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
183 const char *obj_labels[] = {
184 GOT_OBJ_LABEL_COMMIT,
189 const int obj_types[] = {
202 end = memchr(buf, '\0', len);
204 return got_error(GOT_ERR_BAD_OBJ_HDR);
206 for (i = 0; i < nitems(obj_labels); i++) {
207 const char *label = obj_labels[i];
208 size_t label_len = strlen(label);
211 if (len <= label_len || buf + label_len >= end ||
212 strncmp(buf, label, label_len) != 0)
216 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
218 return got_error(GOT_ERR_BAD_OBJ_HDR);
223 return got_error(GOT_ERR_BAD_OBJ_HDR);
225 *obj = calloc(1, sizeof(**obj));
227 return got_error_from_errno("calloc");
229 (*obj)->hdrlen = end - buf + 1;
234 const struct got_error *
235 got_object_read_header(struct got_object **obj, int fd)
237 const struct got_error *err;
238 struct got_inflate_buf zb;
240 const size_t zbsize = 64;
241 size_t outlen, totlen;
246 buf = malloc(zbsize);
248 return got_error_from_errno("malloc");
251 err = got_inflate_init(&zb, buf, zbsize, NULL);
257 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
263 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
266 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
267 if (newbuf == NULL) {
268 err = got_error_from_errno("recallocarray");
272 zb.outbuf = newbuf + totlen;
273 zb.outlen = (nbuf * zbsize) - totlen;
275 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
277 err = got_object_parse_header(obj, buf, totlen);
280 got_inflate_end(&zb);
284 const struct got_error *
285 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
286 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
289 const struct got_error *err = NULL;
290 struct got_object *obj;
291 struct got_inflate_checksum csum;
292 uint8_t sha1[SHA1_DIGEST_LENGTH];
294 size_t len, consumed;
302 memset(&csum, 0, sizeof(csum));
303 csum.output_sha1 = &sha1_ctx;
305 if (lseek(infd, SEEK_SET, 0) == -1)
306 return got_error_from_errno("lseek");
308 err = got_object_read_header(&obj, infd);
312 if (lseek(infd, SEEK_SET, 0) == -1)
313 return got_error_from_errno("lseek");
315 if (obj->size + obj->hdrlen <= max_in_mem_size) {
316 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
317 obj->size + obj->hdrlen, infd);
321 * XXX This uses an extra file descriptor for no good reason.
322 * We should have got_inflate_fd_to_fd().
326 return got_error_from_errno("dup");
329 err = got_error_from_errno("fdopen");
334 err = got_inflate_to_fd(&len, f, &csum, outfd);
339 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
340 err = got_error(GOT_ERR_BAD_OBJ_HDR);
344 SHA1Final(sha1, &sha1_ctx);
345 if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
346 err = got_error_checksum(expected_id);
351 *hdrlen = obj->hdrlen;
353 got_object_close(obj);
354 if (f && fclose(f) == EOF && err == NULL)
355 err = got_error_from_errno("fclose");
359 struct got_commit_object *
360 got_object_commit_alloc_partial(void)
362 struct got_commit_object *commit;
364 commit = calloc(1, sizeof(*commit));
367 commit->tree_id = malloc(sizeof(*commit->tree_id));
368 if (commit->tree_id == NULL) {
373 STAILQ_INIT(&commit->parent_ids);
378 const struct got_error *
379 got_object_commit_add_parent(struct got_commit_object *commit,
382 const struct got_error *err = NULL;
383 struct got_object_qid *qid;
385 err = got_object_qid_alloc_partial(&qid);
389 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
390 err = got_error(GOT_ERR_BAD_OBJ_DATA);
391 got_object_qid_free(qid);
395 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
401 static const struct got_error *
402 parse_gmtoff(time_t *gmtoff, const char *tzstr)
405 const char *p = tzstr;
413 return got_error(GOT_ERR_BAD_OBJ_DATA);
415 if (!isdigit((unsigned char)*p) &&
416 !isdigit((unsigned char)*(p + 1)))
417 return got_error(GOT_ERR_BAD_OBJ_DATA);
418 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
421 if (!isdigit((unsigned char)*p) &&
422 !isdigit((unsigned char)*(p + 1)))
423 return got_error(GOT_ERR_BAD_OBJ_DATA);
424 m = ((*p - '0') * 10) + (*(p + 1) - '0');
426 *gmtoff = (h * 60 * 60 + m * 60) * sign;
430 static const struct got_error *
431 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
433 const struct got_error *err = NULL;
437 /* Parse and strip off trailing timezone indicator string. */
438 space = strrchr(committer, ' ');
440 return got_error(GOT_ERR_BAD_OBJ_DATA);
441 tzstr = strdup(space + 1);
443 return got_error_from_errno("strdup");
444 err = parse_gmtoff(gmtoff, tzstr);
447 if (err->code != GOT_ERR_BAD_OBJ_DATA)
449 /* Old versions of Git omitted the timestamp. */
456 /* Timestamp is separated from committer name + email by space. */
457 space = strrchr(committer, ' ');
459 return got_error(GOT_ERR_BAD_OBJ_DATA);
461 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
462 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
464 return got_error(GOT_ERR_BAD_OBJ_DATA);
466 /* Strip off parsed time information, leaving just author and email. */
473 got_object_commit_close(struct got_commit_object *commit)
475 if (commit->refcnt > 0) {
477 if (commit->refcnt > 0)
481 got_object_id_queue_free(&commit->parent_ids);
482 free(commit->tree_id);
483 free(commit->author);
484 free(commit->committer);
485 free(commit->logmsg);
489 struct got_object_id *
490 got_object_commit_get_tree_id(struct got_commit_object *commit)
492 return commit->tree_id;
496 got_object_commit_get_nparents(struct got_commit_object *commit)
498 return commit->nparents;
501 const struct got_object_id_queue *
502 got_object_commit_get_parent_ids(struct got_commit_object *commit)
504 return &commit->parent_ids;
508 got_object_commit_get_author(struct got_commit_object *commit)
510 return commit->author;
514 got_object_commit_get_author_time(struct got_commit_object *commit)
516 return commit->author_time;
519 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
521 return commit->author_gmtoff;
525 got_object_commit_get_committer(struct got_commit_object *commit)
527 return commit->committer;
531 got_object_commit_get_committer_time(struct got_commit_object *commit)
533 return commit->committer_time;
537 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
539 return commit->committer_gmtoff;
542 const struct got_error *
543 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
545 const struct got_error *err = NULL;
550 len = strlen(commit->logmsg);
551 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
553 return got_error_from_errno("malloc");
556 * Strip out unusual headers. Headers are separated from the commit
557 * message body by a single empty line.
559 src = commit->logmsg;
561 while (*src != '\0' && *src != '\n') {
562 int copy_header = 1, eol = 0;
563 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
564 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
565 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
566 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
567 strncmp(src, GOT_COMMIT_LABEL_PARENT,
568 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
569 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
570 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
573 while (*src != '\0' && !eol) {
585 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
586 err = got_error(GOT_ERR_NO_SPACE);
590 /* Trim redundant trailing whitespace. */
591 len = strlen(*logmsg);
592 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
593 isspace((unsigned char)(*logmsg)[len - 1])) {
594 (*logmsg)[len - 1] = '\0';
598 /* Append a trailing newline if missing. */
599 if (len > 0 && (*logmsg)[len - 1] != '\n') {
600 (*logmsg)[len] = '\n';
601 (*logmsg)[len + 1] = '\0';
612 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
614 return commit->logmsg;
617 const struct got_error *
618 got_object_parse_commit(struct got_commit_object **commit, char *buf,
621 const struct got_error *err = NULL;
622 enum got_hash_algorithm algo = GOT_HASH_SHA1;
625 ssize_t remain = (ssize_t)len;
628 return got_error(GOT_ERR_BAD_OBJ_DATA);
630 *commit = got_object_commit_alloc_partial();
632 return got_error_from_errno("got_object_commit_alloc_partial");
634 label_len = strlen(GOT_COMMIT_LABEL_TREE);
635 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
637 if (remain < SHA1_DIGEST_STRING_LENGTH) {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
643 err = got_error(GOT_ERR_BAD_OBJ_DATA);
646 remain -= SHA1_DIGEST_STRING_LENGTH;
647 s += SHA1_DIGEST_STRING_LENGTH;
649 err = got_error(GOT_ERR_BAD_OBJ_DATA);
653 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
654 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
656 if (remain < SHA1_DIGEST_STRING_LENGTH) {
657 err = got_error(GOT_ERR_BAD_OBJ_DATA);
661 err = got_object_commit_add_parent(*commit, s);
665 remain -= SHA1_DIGEST_STRING_LENGTH;
666 s += SHA1_DIGEST_STRING_LENGTH;
669 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
670 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
676 err = got_error(GOT_ERR_BAD_OBJ_DATA);
680 p = memchr(s, '\n', remain);
682 err = got_error(GOT_ERR_BAD_OBJ_DATA);
687 err = parse_commit_time(&(*commit)->author_time,
688 &(*commit)->author_gmtoff, s);
691 (*commit)->author = strdup(s);
692 if ((*commit)->author == NULL) {
693 err = got_error_from_errno("strdup");
700 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
701 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
707 err = got_error(GOT_ERR_BAD_OBJ_DATA);
711 p = memchr(s, '\n', remain);
713 err = got_error(GOT_ERR_BAD_OBJ_DATA);
718 err = parse_commit_time(&(*commit)->committer_time,
719 &(*commit)->committer_gmtoff, s);
722 (*commit)->committer = strdup(s);
723 if ((*commit)->committer == NULL) {
724 err = got_error_from_errno("strdup");
731 (*commit)->logmsg = strndup(s, remain);
732 if ((*commit)->logmsg == NULL) {
733 err = got_error_from_errno("strndup");
738 got_object_commit_close(*commit);
744 const struct got_error *
745 got_object_read_commit(struct got_commit_object **commit, int fd,
746 struct got_object_id *expected_id, size_t expected_size)
748 struct got_object *obj = NULL;
749 const struct got_error *err = NULL;
752 struct got_inflate_checksum csum;
754 struct got_object_id id;
757 memset(&csum, 0, sizeof(csum));
758 csum.output_sha1 = &sha1_ctx;
760 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
764 SHA1Final(id.sha1, &sha1_ctx);
765 if (got_object_id_cmp(expected_id, &id) != 0) {
766 err = got_error_checksum(expected_id);
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 err = got_error_checksum(expected_id);
939 err = got_object_parse_header(&obj, *p, len);
943 if (len < obj->hdrlen + obj->size) {
944 err = got_error(GOT_ERR_BAD_OBJ_DATA);
948 /* Skip object header. */
950 err = got_object_parse_tree(entries, nentries, nentries_alloc,
951 *p + obj->hdrlen, len);
954 got_object_close(obj);
959 got_object_tag_close(struct got_tag_object *tag)
961 if (tag->refcnt > 0) {
973 const struct got_error *
974 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
976 const struct got_error *err = NULL;
977 enum got_hash_algorithm algo = GOT_HASH_SHA1;
983 return got_error(GOT_ERR_BAD_OBJ_DATA);
985 *tag = calloc(1, sizeof(**tag));
987 return got_error_from_errno("calloc");
989 label_len = strlen(GOT_TAG_LABEL_OBJECT);
990 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
992 if (remain < SHA1_DIGEST_STRING_LENGTH) {
993 err = got_error(GOT_ERR_BAD_OBJ_DATA);
997 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
998 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1001 remain -= SHA1_DIGEST_STRING_LENGTH;
1002 s += SHA1_DIGEST_STRING_LENGTH;
1004 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1009 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1013 label_len = strlen(GOT_TAG_LABEL_TYPE);
1014 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1015 remain -= label_len;
1017 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1021 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1022 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1023 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1024 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1026 remain -= label_len;
1027 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1028 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1029 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1030 label_len = strlen(GOT_OBJ_LABEL_TREE);
1032 remain -= label_len;
1033 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1034 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1035 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1036 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1038 remain -= label_len;
1039 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1040 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1041 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1042 label_len = strlen(GOT_OBJ_LABEL_TAG);
1044 remain -= label_len;
1046 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1050 if (remain <= 0 || *s != '\n') {
1051 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1057 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1061 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1065 label_len = strlen(GOT_TAG_LABEL_TAG);
1066 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1069 remain -= label_len;
1071 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1075 p = memchr(s, '\n', remain);
1077 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1082 (*tag)->tag = strndup(s, slen);
1083 if ((*tag)->tag == NULL) {
1084 err = got_error_from_errno("strndup");
1090 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1094 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1098 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1099 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1103 remain -= label_len;
1105 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1109 p = memchr(s, '\n', remain);
1111 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1116 err = parse_commit_time(&(*tag)->tagger_time,
1117 &(*tag)->tagger_gmtoff, s);
1120 (*tag)->tagger = strdup(s);
1121 if ((*tag)->tagger == NULL) {
1122 err = got_error_from_errno("strdup");
1128 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1132 /* Some old tags in the Linux git repo have no tagger. */
1133 (*tag)->tagger = strdup("");
1134 if ((*tag)->tagger == NULL) {
1135 err = got_error_from_errno("strdup");
1140 (*tag)->tagmsg = strndup(s, remain);
1141 if ((*tag)->tagmsg == NULL) {
1142 err = got_error_from_errno("strndup");
1147 got_object_tag_close(*tag);
1153 const struct got_error *
1154 got_object_read_tag(struct got_tag_object **tag, int fd,
1155 struct got_object_id *expected_id, size_t expected_size)
1157 const struct got_error *err = NULL;
1158 struct got_object *obj = NULL;
1161 struct got_inflate_checksum csum;
1163 struct got_object_id id;
1165 SHA1Init(&sha1_ctx);
1166 memset(&csum, 0, sizeof(csum));
1167 csum.output_sha1 = &sha1_ctx;
1169 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1174 SHA1Final(id.sha1, &sha1_ctx);
1175 if (got_object_id_cmp(expected_id, &id) != 0) {
1176 err = got_error_checksum(expected_id);
1180 err = got_object_parse_header(&obj, p, len);
1184 if (len < obj->hdrlen + obj->size) {
1185 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1189 /* Skip object header. */
1191 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1195 got_object_close(obj);
1199 const struct got_error *
1200 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1202 const struct got_error *err = NULL;
1203 static const size_t blocksize = 512;
1204 size_t n, total, remain;
1210 buf = malloc(blocksize);
1212 return got_error_from_errno("malloc");
1219 newbuf = reallocarray(buf, 1, total + blocksize);
1220 if (newbuf == NULL) {
1221 err = got_error_from_errno("reallocarray");
1225 remain += blocksize;
1227 n = fread(buf + total, 1, remain, f);
1230 err = got_ferror(f, GOT_ERR_IO);