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->hash, id2->hash, 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 uint8_t sha1[SHA1_DIGEST_LENGTH];
295 size_t len, consumed;
303 memset(&csum, 0, sizeof(csum));
304 csum.output_sha1 = &sha1_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 SHA1Final(sha1, &sha1_ctx);
346 if (memcmp(expected_id->hash, sha1, SHA1_DIGEST_LENGTH) != 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_sha1_digest(qid->id.hash, id_str)) {
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;
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_sha1_digest((*commit)->tree_id->hash, s)) {
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_hash = &hash_ctx;
760 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
764 SHA1Final(id.hash, &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.hash, &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;
982 return got_error(GOT_ERR_BAD_OBJ_DATA);
984 *tag = calloc(1, sizeof(**tag));
986 return got_error_from_errno("calloc");
988 label_len = strlen(GOT_TAG_LABEL_OBJECT);
989 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
991 if (remain < SHA1_DIGEST_STRING_LENGTH) {
992 err = got_error(GOT_ERR_BAD_OBJ_DATA);
996 if (!got_parse_sha1_digest((*tag)->id.hash, s)) {
997 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1000 remain -= SHA1_DIGEST_STRING_LENGTH;
1001 s += SHA1_DIGEST_STRING_LENGTH;
1003 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1008 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1012 label_len = strlen(GOT_TAG_LABEL_TYPE);
1013 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1014 remain -= label_len;
1016 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1020 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1021 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1022 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1023 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1025 remain -= label_len;
1026 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1027 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1028 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1029 label_len = strlen(GOT_OBJ_LABEL_TREE);
1031 remain -= label_len;
1032 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1033 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1034 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1035 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1037 remain -= label_len;
1038 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1039 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1040 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1041 label_len = strlen(GOT_OBJ_LABEL_TAG);
1043 remain -= label_len;
1045 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1049 if (remain <= 0 || *s != '\n') {
1050 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1056 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1060 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1064 label_len = strlen(GOT_TAG_LABEL_TAG);
1065 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1068 remain -= label_len;
1070 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1074 p = memchr(s, '\n', remain);
1076 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1081 (*tag)->tag = strndup(s, slen);
1082 if ((*tag)->tag == NULL) {
1083 err = got_error_from_errno("strndup");
1089 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1093 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1097 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1098 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1102 remain -= label_len;
1104 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1108 p = memchr(s, '\n', remain);
1110 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1115 err = parse_commit_time(&(*tag)->tagger_time,
1116 &(*tag)->tagger_gmtoff, s);
1119 (*tag)->tagger = strdup(s);
1120 if ((*tag)->tagger == NULL) {
1121 err = got_error_from_errno("strdup");
1127 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1131 /* Some old tags in the Linux git repo have no tagger. */
1132 (*tag)->tagger = strdup("");
1133 if ((*tag)->tagger == NULL) {
1134 err = got_error_from_errno("strdup");
1139 (*tag)->tagmsg = strndup(s, remain);
1140 if ((*tag)->tagmsg == NULL) {
1141 err = got_error_from_errno("strndup");
1146 got_object_tag_close(*tag);
1152 const struct got_error *
1153 got_object_read_tag(struct got_tag_object **tag, int fd,
1154 struct got_object_id *expected_id, size_t expected_size)
1156 const struct got_error *err = NULL;
1157 struct got_object *obj = NULL;
1160 struct got_inflate_checksum csum;
1162 struct got_object_id id;
1164 SHA1Init(&sha1_ctx);
1165 memset(&csum, 0, sizeof(csum));
1166 csum.output_sha1 = &sha1_ctx;
1168 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1173 SHA1Final(id.hash, &sha1_ctx);
1174 if (got_object_id_cmp(expected_id, &id) != 0) {
1175 err = got_error_checksum(expected_id);
1179 err = got_object_parse_header(&obj, p, len);
1183 if (len < obj->hdrlen + obj->size) {
1184 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1188 /* Skip object header. */
1190 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1194 got_object_close(obj);
1198 const struct got_error *
1199 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1201 const struct got_error *err = NULL;
1202 static const size_t blocksize = 512;
1203 size_t n, total, remain;
1209 buf = malloc(blocksize);
1211 return got_error_from_errno("malloc");
1218 newbuf = reallocarray(buf, 1, total + blocksize);
1219 if (newbuf == NULL) {
1220 err = got_error_from_errno("reallocarray");
1224 remain += blocksize;
1226 n = fread(buf + total, 1, remain, f);
1229 err = got_ferror(f, GOT_ERR_IO);