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>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
46 #include "got_lib_hash.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_repository.h"
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
67 memcpy(id2, id1, sizeof(*id2));
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
75 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
78 const struct got_error *
79 got_object_qid_alloc_partial(struct got_object_qid **qid)
81 *qid = malloc(sizeof(**qid));
83 return got_error_from_errno("malloc");
89 const struct got_error *
90 got_object_id_str(char **outbuf, struct got_object_id *id)
92 static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN;
94 *outbuf = malloc(len);
96 return got_error_from_errno("malloc");
98 if (got_object_id_hex(id, *outbuf, len) == NULL) {
101 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
108 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
110 return got_sha1_digest_to_str(id->sha1, buf, len);
113 const struct got_error *
114 got_object_type_label(const char **label, int obj_type)
116 const struct got_error *err = NULL;
119 case GOT_OBJ_TYPE_BLOB:
120 *label = GOT_OBJ_LABEL_BLOB;
122 case GOT_OBJ_TYPE_TREE:
123 *label = GOT_OBJ_LABEL_TREE;
125 case GOT_OBJ_TYPE_COMMIT:
126 *label = GOT_OBJ_LABEL_COMMIT;
128 case GOT_OBJ_TYPE_TAG:
129 *label = GOT_OBJ_LABEL_TAG;
133 err = got_error(GOT_ERR_OBJ_TYPE);
141 got_object_close(struct got_object *obj)
143 if (obj->refcnt > 0) {
149 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
150 struct got_delta *delta;
151 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
152 delta = STAILQ_FIRST(&obj->deltas.entries);
153 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
160 const struct got_error *
161 got_object_raw_close(struct got_raw_object *obj)
163 const struct got_error *err = NULL;
165 if (obj->refcnt > 0) {
174 if (obj->f == NULL) {
176 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
177 err = got_error_from_errno("munmap");
178 if (close(obj->fd) == -1 && err == NULL)
179 err = got_error_from_errno("close");
183 if (fclose(obj->f) == EOF && err == NULL)
184 err = got_error_from_errno("fclose");
191 got_object_qid_free(struct got_object_qid *qid)
197 got_object_id_queue_free(struct got_object_id_queue *ids)
199 struct got_object_qid *qid;
201 while (!STAILQ_EMPTY(ids)) {
202 qid = STAILQ_FIRST(ids);
203 STAILQ_REMOVE_HEAD(ids, entry);
204 got_object_qid_free(qid);
208 const struct got_error *
209 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
211 const char *obj_labels[] = {
212 GOT_OBJ_LABEL_COMMIT,
217 const int obj_types[] = {
230 end = memchr(buf, '\0', len);
232 return got_error(GOT_ERR_BAD_OBJ_HDR);
234 for (i = 0; i < nitems(obj_labels); i++) {
235 const char *label = obj_labels[i];
236 size_t label_len = strlen(label);
239 if (len <= label_len || buf + label_len >= end ||
240 strncmp(buf, label, label_len) != 0)
244 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
246 return got_error(GOT_ERR_BAD_OBJ_HDR);
251 return got_error(GOT_ERR_BAD_OBJ_HDR);
253 *obj = calloc(1, sizeof(**obj));
255 return got_error_from_errno("calloc");
257 (*obj)->hdrlen = end - buf + 1;
262 const struct got_error *
263 got_object_read_header(struct got_object **obj, int fd)
265 const struct got_error *err;
266 struct got_inflate_buf zb;
268 const size_t zbsize = 64;
269 size_t outlen, totlen;
274 buf = malloc(zbsize);
276 return got_error_from_errno("malloc");
279 err = got_inflate_init(&zb, buf, zbsize, NULL);
285 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
291 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
294 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
295 if (newbuf == NULL) {
296 err = got_error_from_errno("recallocarray");
300 zb.outbuf = newbuf + totlen;
301 zb.outlen = (nbuf * zbsize) - totlen;
303 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
305 err = got_object_parse_header(obj, buf, totlen);
308 got_inflate_end(&zb);
312 const struct got_error *
313 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
314 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
317 const struct got_error *err = NULL;
318 struct got_object *obj;
319 struct got_inflate_checksum csum;
320 struct got_object_id id;
322 size_t len, consumed;
329 got_hash_init(&ctx, GOT_HASH_SHA1);
330 memset(&csum, 0, sizeof(csum));
331 csum.output_ctx = &ctx;
333 if (lseek(infd, SEEK_SET, 0) == -1)
334 return got_error_from_errno("lseek");
336 err = got_object_read_header(&obj, infd);
340 if (lseek(infd, SEEK_SET, 0) == -1)
341 return got_error_from_errno("lseek");
343 if (obj->size + obj->hdrlen <= max_in_mem_size) {
344 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
345 obj->size + obj->hdrlen, infd);
349 * XXX This uses an extra file descriptor for no good reason.
350 * We should have got_inflate_fd_to_fd().
354 return got_error_from_errno("dup");
357 err = got_error_from_errno("fdopen");
362 err = got_inflate_to_fd(&len, f, &csum, outfd);
367 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
368 err = got_error(GOT_ERR_BAD_OBJ_HDR);
372 got_hash_final_object_id(&ctx, &id);
373 if (got_object_id_cmp(expected_id, &id) != 0) {
374 err = got_error_checksum(expected_id);
379 *hdrlen = obj->hdrlen;
381 got_object_close(obj);
382 if (f && fclose(f) == EOF && err == NULL)
383 err = got_error_from_errno("fclose");
387 struct got_commit_object *
388 got_object_commit_alloc_partial(void)
390 struct got_commit_object *commit;
392 commit = calloc(1, sizeof(*commit));
395 commit->tree_id = malloc(sizeof(*commit->tree_id));
396 if (commit->tree_id == NULL) {
401 STAILQ_INIT(&commit->parent_ids);
406 const struct got_error *
407 got_object_commit_add_parent(struct got_commit_object *commit,
410 const struct got_error *err = NULL;
411 struct got_object_qid *qid;
413 err = got_object_qid_alloc_partial(&qid);
417 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
418 err = got_error(GOT_ERR_BAD_OBJ_DATA);
419 got_object_qid_free(qid);
423 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
429 static const struct got_error *
430 parse_gmtoff(time_t *gmtoff, const char *tzstr)
433 const char *p = tzstr;
441 return got_error(GOT_ERR_BAD_OBJ_DATA);
443 if (!isdigit((unsigned char)*p) &&
444 !isdigit((unsigned char)*(p + 1)))
445 return got_error(GOT_ERR_BAD_OBJ_DATA);
446 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
449 if (!isdigit((unsigned char)*p) &&
450 !isdigit((unsigned char)*(p + 1)))
451 return got_error(GOT_ERR_BAD_OBJ_DATA);
452 m = ((*p - '0') * 10) + (*(p + 1) - '0');
454 *gmtoff = (h * 60 * 60 + m * 60) * sign;
458 static const struct got_error *
459 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
461 const struct got_error *err = NULL;
465 /* Parse and strip off trailing timezone indicator string. */
466 space = strrchr(committer, ' ');
468 return got_error(GOT_ERR_BAD_OBJ_DATA);
469 tzstr = strdup(space + 1);
471 return got_error_from_errno("strdup");
472 err = parse_gmtoff(gmtoff, tzstr);
475 if (err->code != GOT_ERR_BAD_OBJ_DATA)
477 /* Old versions of Git omitted the timestamp. */
484 /* Timestamp is separated from committer name + email by space. */
485 space = strrchr(committer, ' ');
487 return got_error(GOT_ERR_BAD_OBJ_DATA);
489 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
490 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
492 return got_error(GOT_ERR_BAD_OBJ_DATA);
494 /* Strip off parsed time information, leaving just author and email. */
501 got_object_commit_close(struct got_commit_object *commit)
503 if (commit->refcnt > 0) {
505 if (commit->refcnt > 0)
509 got_object_id_queue_free(&commit->parent_ids);
510 free(commit->tree_id);
511 free(commit->author);
512 free(commit->committer);
513 free(commit->logmsg);
517 struct got_object_id *
518 got_object_commit_get_tree_id(struct got_commit_object *commit)
520 return commit->tree_id;
524 got_object_commit_get_nparents(struct got_commit_object *commit)
526 return commit->nparents;
529 const struct got_object_id_queue *
530 got_object_commit_get_parent_ids(struct got_commit_object *commit)
532 return &commit->parent_ids;
536 got_object_commit_get_author(struct got_commit_object *commit)
538 return commit->author;
542 got_object_commit_get_author_time(struct got_commit_object *commit)
544 return commit->author_time;
547 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
549 return commit->author_gmtoff;
553 got_object_commit_get_committer(struct got_commit_object *commit)
555 return commit->committer;
559 got_object_commit_get_committer_time(struct got_commit_object *commit)
561 return commit->committer_time;
565 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
567 return commit->committer_gmtoff;
570 const struct got_error *
571 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
573 const struct got_error *err = NULL;
578 len = strlen(commit->logmsg);
579 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
581 return got_error_from_errno("malloc");
584 * Strip out unusual headers. Headers are separated from the commit
585 * message body by a single empty line.
587 src = commit->logmsg;
589 while (*src != '\0' && *src != '\n') {
590 int copy_header = 1, eol = 0;
591 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
592 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
593 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
594 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
595 strncmp(src, GOT_COMMIT_LABEL_PARENT,
596 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
597 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
598 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
601 while (*src != '\0' && !eol) {
613 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
614 err = got_error(GOT_ERR_NO_SPACE);
618 /* Trim redundant trailing whitespace. */
619 len = strlen(*logmsg);
620 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
621 isspace((unsigned char)(*logmsg)[len - 1])) {
622 (*logmsg)[len - 1] = '\0';
626 /* Append a trailing newline if missing. */
627 if (len > 0 && (*logmsg)[len - 1] != '\n') {
628 (*logmsg)[len] = '\n';
629 (*logmsg)[len + 1] = '\0';
640 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
642 return commit->logmsg;
645 const struct got_error *
646 got_object_parse_commit(struct got_commit_object **commit, char *buf,
649 const struct got_error *err = NULL;
650 enum got_hash_algorithm algo = GOT_HASH_SHA1;
653 ssize_t remain = (ssize_t)len;
656 return got_error(GOT_ERR_BAD_OBJ_DATA);
658 *commit = got_object_commit_alloc_partial();
660 return got_error_from_errno("got_object_commit_alloc_partial");
662 label_len = strlen(GOT_COMMIT_LABEL_TREE);
663 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
665 if (remain < SHA1_DIGEST_STRING_LENGTH) {
666 err = got_error(GOT_ERR_BAD_OBJ_DATA);
670 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
671 err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 remain -= SHA1_DIGEST_STRING_LENGTH;
675 s += SHA1_DIGEST_STRING_LENGTH;
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
682 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
684 if (remain < SHA1_DIGEST_STRING_LENGTH) {
685 err = got_error(GOT_ERR_BAD_OBJ_DATA);
689 err = got_object_commit_add_parent(*commit, s);
693 remain -= SHA1_DIGEST_STRING_LENGTH;
694 s += SHA1_DIGEST_STRING_LENGTH;
697 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
698 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, 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)->author_time,
716 &(*commit)->author_gmtoff, s);
719 (*commit)->author = strdup(s);
720 if ((*commit)->author == NULL) {
721 err = got_error_from_errno("strdup");
728 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
729 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
735 err = got_error(GOT_ERR_BAD_OBJ_DATA);
739 p = memchr(s, '\n', remain);
741 err = got_error(GOT_ERR_BAD_OBJ_DATA);
746 err = parse_commit_time(&(*commit)->committer_time,
747 &(*commit)->committer_gmtoff, s);
750 (*commit)->committer = strdup(s);
751 if ((*commit)->committer == NULL) {
752 err = got_error_from_errno("strdup");
759 (*commit)->logmsg = strndup(s, remain);
760 if ((*commit)->logmsg == NULL) {
761 err = got_error_from_errno("strndup");
766 got_object_commit_close(*commit);
772 const struct got_error *
773 got_object_read_commit(struct got_commit_object **commit, int fd,
774 struct got_object_id *expected_id, size_t expected_size)
776 struct got_object *obj = NULL;
777 const struct got_error *err = NULL;
780 struct got_inflate_checksum csum;
782 struct got_object_id id;
784 got_hash_init(&ctx, GOT_HASH_SHA1);
785 memset(&csum, 0, sizeof(csum));
786 csum.output_ctx = &ctx;
788 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
792 got_hash_final_object_id(&ctx, &id);
793 if (got_object_id_cmp(expected_id, &id) != 0) {
794 err = got_error_checksum(expected_id);
798 err = got_object_parse_header(&obj, p, len);
802 if (len < obj->hdrlen + obj->size) {
803 err = got_error(GOT_ERR_BAD_OBJ_DATA);
807 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
808 err = got_error(GOT_ERR_OBJ_TYPE);
812 /* Skip object header. */
814 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
818 got_object_close(obj);
823 got_object_tree_close(struct got_tree_object *tree)
825 if (tree->refcnt > 0) {
827 if (tree->refcnt > 0)
835 static const struct got_error *
836 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
843 *elen = strnlen(buf, maxlen) + 1;
845 return got_error(GOT_ERR_BAD_OBJ_DATA);
847 space = memchr(buf, ' ', *elen);
848 if (space == NULL || space <= buf)
849 return got_error(GOT_ERR_BAD_OBJ_DATA);
854 if (*p < '0' || *p > '7')
855 return got_error(GOT_ERR_BAD_OBJ_DATA);
857 pte->mode |= *p - '0';
861 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
862 return got_error(GOT_ERR_BAD_OBJ_DATA);
864 pte->name = space + 1;
865 pte->namelen = strlen(pte->name);
868 *elen += SHA1_DIGEST_LENGTH;
873 pte_cmp(const void *pa, const void *pb)
875 const struct got_parsed_tree_entry *a = pa, *b = pb;
877 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
880 const struct got_error *
881 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
882 size_t *nentries_alloc, uint8_t *buf, size_t len)
884 const struct got_error *err = NULL;
886 const size_t nalloc = 16;
887 struct got_parsed_tree_entry *pte;
892 return NULL; /* tree is empty */
897 if (*nentries >= *nentries_alloc) {
898 pte = recallocarray(*entries, *nentries_alloc,
899 *nentries_alloc + nalloc, sizeof(**entries));
901 err = got_error_from_errno("recallocarray");
905 *nentries_alloc += nalloc;
908 pte = &(*entries)[*nentries];
909 err = parse_tree_entry(pte, &elen, buf, remain);
918 err = got_error(GOT_ERR_BAD_OBJ_DATA);
923 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
925 for (i = 0; i < *nentries - 1; i++) {
926 struct got_parsed_tree_entry *prev = &(*entries)[i];
927 pte = &(*entries)[i + 1];
928 if (got_path_cmp(prev->name, pte->name,
929 prev->namelen, pte->namelen) == 0) {
930 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
941 const struct got_error *
942 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
943 size_t *nentries_alloc, uint8_t **p, int fd,
944 struct got_object_id *expected_id)
946 const struct got_error *err = NULL;
947 struct got_object *obj = NULL;
949 struct got_inflate_checksum csum;
951 struct got_object_id id;
953 got_hash_init(&ctx, GOT_HASH_SHA1);
954 memset(&csum, 0, sizeof(csum));
955 csum.output_ctx = &ctx;
957 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
961 got_hash_final_object_id(&ctx, &id);
962 if (got_object_id_cmp(expected_id, &id) != 0) {
963 err = got_error_checksum(expected_id);
967 err = got_object_parse_header(&obj, *p, len);
971 if (len < obj->hdrlen + obj->size) {
972 err = got_error(GOT_ERR_BAD_OBJ_DATA);
976 /* Skip object header. */
978 err = got_object_parse_tree(entries, nentries, nentries_alloc,
979 *p + obj->hdrlen, len);
982 got_object_close(obj);
987 got_object_tag_close(struct got_tag_object *tag)
989 if (tag->refcnt > 0) {
1001 const struct got_error *
1002 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
1004 const struct got_error *err = NULL;
1005 enum got_hash_algorithm algo = GOT_HASH_SHA1;
1006 size_t remain = len;
1011 return got_error(GOT_ERR_BAD_OBJ_DATA);
1013 *tag = calloc(1, sizeof(**tag));
1015 return got_error_from_errno("calloc");
1017 label_len = strlen(GOT_TAG_LABEL_OBJECT);
1018 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
1019 remain -= label_len;
1020 if (remain < SHA1_DIGEST_STRING_LENGTH) {
1021 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1025 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
1026 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1029 remain -= SHA1_DIGEST_STRING_LENGTH;
1030 s += SHA1_DIGEST_STRING_LENGTH;
1032 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1037 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1041 label_len = strlen(GOT_TAG_LABEL_TYPE);
1042 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1043 remain -= label_len;
1045 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1049 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1050 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1051 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1052 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1054 remain -= label_len;
1055 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1056 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1057 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1058 label_len = strlen(GOT_OBJ_LABEL_TREE);
1060 remain -= label_len;
1061 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1062 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1063 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1064 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1066 remain -= label_len;
1067 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1068 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1069 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1070 label_len = strlen(GOT_OBJ_LABEL_TAG);
1072 remain -= label_len;
1074 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1078 if (remain <= 0 || *s != '\n') {
1079 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1085 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1089 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1093 label_len = strlen(GOT_TAG_LABEL_TAG);
1094 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1097 remain -= label_len;
1099 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1103 p = memchr(s, '\n', remain);
1105 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1110 (*tag)->tag = strndup(s, slen);
1111 if ((*tag)->tag == NULL) {
1112 err = got_error_from_errno("strndup");
1118 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1122 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1126 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1127 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1131 remain -= label_len;
1133 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1137 p = memchr(s, '\n', remain);
1139 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1144 err = parse_commit_time(&(*tag)->tagger_time,
1145 &(*tag)->tagger_gmtoff, s);
1148 (*tag)->tagger = strdup(s);
1149 if ((*tag)->tagger == NULL) {
1150 err = got_error_from_errno("strdup");
1156 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1160 /* Some old tags in the Linux git repo have no tagger. */
1161 (*tag)->tagger = strdup("");
1162 if ((*tag)->tagger == NULL) {
1163 err = got_error_from_errno("strdup");
1168 (*tag)->tagmsg = strndup(s, remain);
1169 if ((*tag)->tagmsg == NULL) {
1170 err = got_error_from_errno("strndup");
1175 got_object_tag_close(*tag);
1181 const struct got_error *
1182 got_object_read_tag(struct got_tag_object **tag, int fd,
1183 struct got_object_id *expected_id, size_t expected_size)
1185 const struct got_error *err = NULL;
1186 struct got_object *obj = NULL;
1189 struct got_inflate_checksum csum;
1190 struct got_hash ctx;
1191 struct got_object_id id;
1193 got_hash_init(&ctx, GOT_HASH_SHA1);
1194 memset(&csum, 0, sizeof(csum));
1195 csum.output_ctx = &ctx;
1197 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1202 got_hash_final_object_id(&ctx, &id);
1203 if (got_object_id_cmp(expected_id, &id) != 0) {
1204 err = got_error_checksum(expected_id);
1208 err = got_object_parse_header(&obj, p, len);
1212 if (len < obj->hdrlen + obj->size) {
1213 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1217 /* Skip object header. */
1219 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1223 got_object_close(obj);
1227 const struct got_error *
1228 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1230 const struct got_error *err = NULL;
1231 static const size_t blocksize = 512;
1232 size_t n, total, remain;
1238 buf = malloc(blocksize);
1240 return got_error_from_errno("malloc");
1247 newbuf = reallocarray(buf, 1, total + blocksize);
1248 if (newbuf == NULL) {
1249 err = got_error_from_errno("reallocarray");
1253 remain += blocksize;
1255 n = fread(buf + total, 1, remain, f);
1258 err = got_ferror(f, GOT_ERR_IO);