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);
114 got_object_close(struct got_object *obj)
116 if (obj->refcnt > 0) {
122 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
123 struct got_delta *delta;
124 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
125 delta = STAILQ_FIRST(&obj->deltas.entries);
126 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
133 const struct got_error *
134 got_object_raw_close(struct got_raw_object *obj)
136 const struct got_error *err = NULL;
138 if (obj->refcnt > 0) {
147 if (obj->f == NULL) {
149 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
150 err = got_error_from_errno("munmap");
151 if (close(obj->fd) == -1 && err == NULL)
152 err = got_error_from_errno("close");
156 if (fclose(obj->f) == EOF && err == NULL)
157 err = got_error_from_errno("fclose");
164 got_object_qid_free(struct got_object_qid *qid)
170 got_object_id_queue_free(struct got_object_id_queue *ids)
172 struct got_object_qid *qid;
174 while (!STAILQ_EMPTY(ids)) {
175 qid = STAILQ_FIRST(ids);
176 STAILQ_REMOVE_HEAD(ids, entry);
177 got_object_qid_free(qid);
181 const struct got_error *
182 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
184 const char *obj_labels[] = {
185 GOT_OBJ_LABEL_COMMIT,
190 const int obj_types[] = {
203 end = memchr(buf, '\0', len);
205 return got_error(GOT_ERR_BAD_OBJ_HDR);
207 for (i = 0; i < nitems(obj_labels); i++) {
208 const char *label = obj_labels[i];
209 size_t label_len = strlen(label);
212 if (len <= label_len || buf + label_len >= end ||
213 strncmp(buf, label, label_len) != 0)
217 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
219 return got_error(GOT_ERR_BAD_OBJ_HDR);
224 return got_error(GOT_ERR_BAD_OBJ_HDR);
226 *obj = calloc(1, sizeof(**obj));
228 return got_error_from_errno("calloc");
230 (*obj)->hdrlen = end - buf + 1;
235 const struct got_error *
236 got_object_read_header(struct got_object **obj, int fd)
238 const struct got_error *err;
239 struct got_inflate_buf zb;
241 const size_t zbsize = 64;
242 size_t outlen, totlen;
247 buf = malloc(zbsize);
249 return got_error_from_errno("malloc");
252 err = got_inflate_init(&zb, buf, zbsize, NULL);
258 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
264 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
267 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
268 if (newbuf == NULL) {
269 err = got_error_from_errno("recallocarray");
273 zb.outbuf = newbuf + totlen;
274 zb.outlen = (nbuf * zbsize) - totlen;
276 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
278 err = got_object_parse_header(obj, buf, totlen);
281 got_inflate_end(&zb);
285 const struct got_error *
286 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
287 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
290 const struct got_error *err = NULL;
291 struct got_object *obj;
292 struct got_inflate_checksum csum;
293 struct got_object_id id;
295 size_t len, consumed;
302 got_hash_init(&ctx, GOT_HASH_SHA1);
303 memset(&csum, 0, sizeof(csum));
304 csum.output_ctx = &ctx;
306 if (lseek(infd, SEEK_SET, 0) == -1)
307 return got_error_from_errno("lseek");
309 err = got_object_read_header(&obj, infd);
313 if (lseek(infd, SEEK_SET, 0) == -1)
314 return got_error_from_errno("lseek");
316 if (obj->size + obj->hdrlen <= max_in_mem_size) {
317 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
318 obj->size + obj->hdrlen, infd);
322 * XXX This uses an extra file descriptor for no good reason.
323 * We should have got_inflate_fd_to_fd().
327 return got_error_from_errno("dup");
330 err = got_error_from_errno("fdopen");
335 err = got_inflate_to_fd(&len, f, &csum, outfd);
340 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
341 err = got_error(GOT_ERR_BAD_OBJ_HDR);
345 got_hash_final_object_id(&ctx, &id);
346 if (got_object_id_cmp(expected_id, &id) != 0) {
347 err = got_error_checksum(expected_id);
352 *hdrlen = obj->hdrlen;
354 got_object_close(obj);
355 if (f && fclose(f) == EOF && err == NULL)
356 err = got_error_from_errno("fclose");
360 struct got_commit_object *
361 got_object_commit_alloc_partial(void)
363 struct got_commit_object *commit;
365 commit = calloc(1, sizeof(*commit));
368 commit->tree_id = malloc(sizeof(*commit->tree_id));
369 if (commit->tree_id == NULL) {
374 STAILQ_INIT(&commit->parent_ids);
379 const struct got_error *
380 got_object_commit_add_parent(struct got_commit_object *commit,
383 const struct got_error *err = NULL;
384 struct got_object_qid *qid;
386 err = got_object_qid_alloc_partial(&qid);
390 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
391 err = got_error(GOT_ERR_BAD_OBJ_DATA);
392 got_object_qid_free(qid);
396 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
402 static const struct got_error *
403 parse_gmtoff(time_t *gmtoff, const char *tzstr)
406 const char *p = tzstr;
414 return got_error(GOT_ERR_BAD_OBJ_DATA);
416 if (!isdigit((unsigned char)*p) &&
417 !isdigit((unsigned char)*(p + 1)))
418 return got_error(GOT_ERR_BAD_OBJ_DATA);
419 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
422 if (!isdigit((unsigned char)*p) &&
423 !isdigit((unsigned char)*(p + 1)))
424 return got_error(GOT_ERR_BAD_OBJ_DATA);
425 m = ((*p - '0') * 10) + (*(p + 1) - '0');
427 *gmtoff = (h * 60 * 60 + m * 60) * sign;
431 static const struct got_error *
432 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
434 const struct got_error *err = NULL;
438 /* Parse and strip off trailing timezone indicator string. */
439 space = strrchr(committer, ' ');
441 return got_error(GOT_ERR_BAD_OBJ_DATA);
442 tzstr = strdup(space + 1);
444 return got_error_from_errno("strdup");
445 err = parse_gmtoff(gmtoff, tzstr);
448 if (err->code != GOT_ERR_BAD_OBJ_DATA)
450 /* Old versions of Git omitted the timestamp. */
457 /* Timestamp is separated from committer name + email by space. */
458 space = strrchr(committer, ' ');
460 return got_error(GOT_ERR_BAD_OBJ_DATA);
462 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
463 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
465 return got_error(GOT_ERR_BAD_OBJ_DATA);
467 /* Strip off parsed time information, leaving just author and email. */
474 got_object_commit_close(struct got_commit_object *commit)
476 if (commit->refcnt > 0) {
478 if (commit->refcnt > 0)
482 got_object_id_queue_free(&commit->parent_ids);
483 free(commit->tree_id);
484 free(commit->author);
485 free(commit->committer);
486 free(commit->logmsg);
490 struct got_object_id *
491 got_object_commit_get_tree_id(struct got_commit_object *commit)
493 return commit->tree_id;
497 got_object_commit_get_nparents(struct got_commit_object *commit)
499 return commit->nparents;
502 const struct got_object_id_queue *
503 got_object_commit_get_parent_ids(struct got_commit_object *commit)
505 return &commit->parent_ids;
509 got_object_commit_get_author(struct got_commit_object *commit)
511 return commit->author;
515 got_object_commit_get_author_time(struct got_commit_object *commit)
517 return commit->author_time;
520 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
522 return commit->author_gmtoff;
526 got_object_commit_get_committer(struct got_commit_object *commit)
528 return commit->committer;
532 got_object_commit_get_committer_time(struct got_commit_object *commit)
534 return commit->committer_time;
538 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
540 return commit->committer_gmtoff;
543 const struct got_error *
544 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
546 const struct got_error *err = NULL;
551 len = strlen(commit->logmsg);
552 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
554 return got_error_from_errno("malloc");
557 * Strip out unusual headers. Headers are separated from the commit
558 * message body by a single empty line.
560 src = commit->logmsg;
562 while (*src != '\0' && *src != '\n') {
563 int copy_header = 1, eol = 0;
564 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
565 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
566 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
567 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
568 strncmp(src, GOT_COMMIT_LABEL_PARENT,
569 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
570 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
571 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
574 while (*src != '\0' && !eol) {
586 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
587 err = got_error(GOT_ERR_NO_SPACE);
591 /* Trim redundant trailing whitespace. */
592 len = strlen(*logmsg);
593 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
594 isspace((unsigned char)(*logmsg)[len - 1])) {
595 (*logmsg)[len - 1] = '\0';
599 /* Append a trailing newline if missing. */
600 if (len > 0 && (*logmsg)[len - 1] != '\n') {
601 (*logmsg)[len] = '\n';
602 (*logmsg)[len + 1] = '\0';
613 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
615 return commit->logmsg;
618 const struct got_error *
619 got_object_parse_commit(struct got_commit_object **commit, char *buf,
622 const struct got_error *err = NULL;
623 enum got_hash_algorithm algo = GOT_HASH_SHA1;
626 ssize_t remain = (ssize_t)len;
629 return got_error(GOT_ERR_BAD_OBJ_DATA);
631 *commit = got_object_commit_alloc_partial();
633 return got_error_from_errno("got_object_commit_alloc_partial");
635 label_len = strlen(GOT_COMMIT_LABEL_TREE);
636 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
638 if (remain < SHA1_DIGEST_STRING_LENGTH) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
644 err = got_error(GOT_ERR_BAD_OBJ_DATA);
647 remain -= SHA1_DIGEST_STRING_LENGTH;
648 s += SHA1_DIGEST_STRING_LENGTH;
650 err = got_error(GOT_ERR_BAD_OBJ_DATA);
654 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
655 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
657 if (remain < SHA1_DIGEST_STRING_LENGTH) {
658 err = got_error(GOT_ERR_BAD_OBJ_DATA);
662 err = got_object_commit_add_parent(*commit, s);
666 remain -= SHA1_DIGEST_STRING_LENGTH;
667 s += SHA1_DIGEST_STRING_LENGTH;
670 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
671 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 p = memchr(s, '\n', remain);
683 err = got_error(GOT_ERR_BAD_OBJ_DATA);
688 err = parse_commit_time(&(*commit)->author_time,
689 &(*commit)->author_gmtoff, s);
692 (*commit)->author = strdup(s);
693 if ((*commit)->author == NULL) {
694 err = got_error_from_errno("strdup");
701 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
702 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
708 err = got_error(GOT_ERR_BAD_OBJ_DATA);
712 p = memchr(s, '\n', remain);
714 err = got_error(GOT_ERR_BAD_OBJ_DATA);
719 err = parse_commit_time(&(*commit)->committer_time,
720 &(*commit)->committer_gmtoff, s);
723 (*commit)->committer = strdup(s);
724 if ((*commit)->committer == NULL) {
725 err = got_error_from_errno("strdup");
732 (*commit)->logmsg = strndup(s, remain);
733 if ((*commit)->logmsg == NULL) {
734 err = got_error_from_errno("strndup");
739 got_object_commit_close(*commit);
745 const struct got_error *
746 got_object_read_commit(struct got_commit_object **commit, int fd,
747 struct got_object_id *expected_id, size_t expected_size)
749 struct got_object *obj = NULL;
750 const struct got_error *err = NULL;
753 struct got_inflate_checksum csum;
755 struct got_object_id id;
757 got_hash_init(&ctx, GOT_HASH_SHA1);
758 memset(&csum, 0, sizeof(csum));
759 csum.output_ctx = &ctx;
761 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
765 got_hash_final_object_id(&ctx, &id);
766 if (got_object_id_cmp(expected_id, &id) != 0) {
767 err = got_error_checksum(expected_id);
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;
926 got_hash_init(&ctx, GOT_HASH_SHA1);
927 memset(&csum, 0, sizeof(csum));
928 csum.output_ctx = &ctx;
930 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
934 got_hash_final_object_id(&ctx, &id);
935 if (got_object_id_cmp(expected_id, &id) != 0) {
936 err = got_error_checksum(expected_id);
940 err = got_object_parse_header(&obj, *p, len);
944 if (len < obj->hdrlen + obj->size) {
945 err = got_error(GOT_ERR_BAD_OBJ_DATA);
949 /* Skip object header. */
951 err = got_object_parse_tree(entries, nentries, nentries_alloc,
952 *p + obj->hdrlen, len);
955 got_object_close(obj);
960 got_object_tag_close(struct got_tag_object *tag)
962 if (tag->refcnt > 0) {
974 const struct got_error *
975 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
977 const struct got_error *err = NULL;
978 enum got_hash_algorithm algo = GOT_HASH_SHA1;
984 return got_error(GOT_ERR_BAD_OBJ_DATA);
986 *tag = calloc(1, sizeof(**tag));
988 return got_error_from_errno("calloc");
990 label_len = strlen(GOT_TAG_LABEL_OBJECT);
991 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
993 if (remain < SHA1_DIGEST_STRING_LENGTH) {
994 err = got_error(GOT_ERR_BAD_OBJ_DATA);
998 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
999 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1002 remain -= SHA1_DIGEST_STRING_LENGTH;
1003 s += SHA1_DIGEST_STRING_LENGTH;
1005 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1010 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1014 label_len = strlen(GOT_TAG_LABEL_TYPE);
1015 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1016 remain -= label_len;
1018 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1022 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1023 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1024 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1025 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1027 remain -= label_len;
1028 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1029 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1030 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1031 label_len = strlen(GOT_OBJ_LABEL_TREE);
1033 remain -= label_len;
1034 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1035 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1036 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1037 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1039 remain -= label_len;
1040 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1041 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1042 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1043 label_len = strlen(GOT_OBJ_LABEL_TAG);
1045 remain -= label_len;
1047 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 if (remain <= 0 || *s != '\n') {
1052 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1058 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1066 label_len = strlen(GOT_TAG_LABEL_TAG);
1067 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1070 remain -= label_len;
1072 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1076 p = memchr(s, '\n', remain);
1078 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1083 (*tag)->tag = strndup(s, slen);
1084 if ((*tag)->tag == NULL) {
1085 err = got_error_from_errno("strndup");
1091 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1095 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1099 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1100 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1104 remain -= label_len;
1106 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1110 p = memchr(s, '\n', remain);
1112 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1117 err = parse_commit_time(&(*tag)->tagger_time,
1118 &(*tag)->tagger_gmtoff, s);
1121 (*tag)->tagger = strdup(s);
1122 if ((*tag)->tagger == NULL) {
1123 err = got_error_from_errno("strdup");
1129 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1133 /* Some old tags in the Linux git repo have no tagger. */
1134 (*tag)->tagger = strdup("");
1135 if ((*tag)->tagger == NULL) {
1136 err = got_error_from_errno("strdup");
1141 (*tag)->tagmsg = strndup(s, remain);
1142 if ((*tag)->tagmsg == NULL) {
1143 err = got_error_from_errno("strndup");
1148 got_object_tag_close(*tag);
1154 const struct got_error *
1155 got_object_read_tag(struct got_tag_object **tag, int fd,
1156 struct got_object_id *expected_id, size_t expected_size)
1158 const struct got_error *err = NULL;
1159 struct got_object *obj = NULL;
1162 struct got_inflate_checksum csum;
1163 struct got_hash ctx;
1164 struct got_object_id id;
1166 got_hash_init(&ctx, GOT_HASH_SHA1);
1167 memset(&csum, 0, sizeof(csum));
1168 csum.output_ctx = &ctx;
1170 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1175 got_hash_final_object_id(&ctx, &id);
1176 if (got_object_id_cmp(expected_id, &id) != 0) {
1177 err = got_error_checksum(expected_id);
1181 err = got_object_parse_header(&obj, p, len);
1185 if (len < obj->hdrlen + obj->size) {
1186 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1190 /* Skip object header. */
1192 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1196 got_object_close(obj);
1200 const struct got_error *
1201 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1203 const struct got_error *err = NULL;
1204 static const size_t blocksize = 512;
1205 size_t n, total, remain;
1211 buf = malloc(blocksize);
1213 return got_error_from_errno("malloc");
1220 newbuf = reallocarray(buf, 1, total + blocksize);
1221 if (newbuf == NULL) {
1222 err = got_error_from_errno("reallocarray");
1226 remain += blocksize;
1228 n = fread(buf + total, 1, remain, f);
1231 err = got_ferror(f, GOT_ERR_IO);