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 = 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_sha1_digest(qid->id.sha1, id_str)) {
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;
624 ssize_t remain = (ssize_t)len;
627 return got_error(GOT_ERR_BAD_OBJ_DATA);
629 *commit = got_object_commit_alloc_partial();
631 return got_error_from_errno("got_object_commit_alloc_partial");
633 label_len = strlen(GOT_COMMIT_LABEL_TREE);
634 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
636 if (remain < SHA1_DIGEST_STRING_LENGTH) {
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
642 err = got_error(GOT_ERR_BAD_OBJ_DATA);
645 remain -= SHA1_DIGEST_STRING_LENGTH;
646 s += SHA1_DIGEST_STRING_LENGTH;
648 err = got_error(GOT_ERR_BAD_OBJ_DATA);
652 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
653 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
655 if (remain < SHA1_DIGEST_STRING_LENGTH) {
656 err = got_error(GOT_ERR_BAD_OBJ_DATA);
660 err = got_object_commit_add_parent(*commit, s);
664 remain -= SHA1_DIGEST_STRING_LENGTH;
665 s += SHA1_DIGEST_STRING_LENGTH;
668 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
669 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
675 err = got_error(GOT_ERR_BAD_OBJ_DATA);
679 p = memchr(s, '\n', remain);
681 err = got_error(GOT_ERR_BAD_OBJ_DATA);
686 err = parse_commit_time(&(*commit)->author_time,
687 &(*commit)->author_gmtoff, s);
690 (*commit)->author = strdup(s);
691 if ((*commit)->author == NULL) {
692 err = got_error_from_errno("strdup");
699 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
700 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
706 err = got_error(GOT_ERR_BAD_OBJ_DATA);
710 p = memchr(s, '\n', remain);
712 err = got_error(GOT_ERR_BAD_OBJ_DATA);
717 err = parse_commit_time(&(*commit)->committer_time,
718 &(*commit)->committer_gmtoff, s);
721 (*commit)->committer = strdup(s);
722 if ((*commit)->committer == NULL) {
723 err = got_error_from_errno("strdup");
730 (*commit)->logmsg = strndup(s, remain);
731 if ((*commit)->logmsg == NULL) {
732 err = got_error_from_errno("strndup");
737 got_object_commit_close(*commit);
743 const struct got_error *
744 got_object_read_commit(struct got_commit_object **commit, int fd,
745 struct got_object_id *expected_id, size_t expected_size)
747 struct got_object *obj = NULL;
748 const struct got_error *err = NULL;
751 struct got_inflate_checksum csum;
753 struct got_object_id id;
756 memset(&csum, 0, sizeof(csum));
757 csum.output_sha1 = &sha1_ctx;
759 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
763 SHA1Final(id.sha1, &sha1_ctx);
764 if (got_object_id_cmp(expected_id, &id) != 0) {
765 err = got_error_checksum(expected_id);
769 err = got_object_parse_header(&obj, p, len);
773 if (len < obj->hdrlen + obj->size) {
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
778 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
779 err = got_error(GOT_ERR_OBJ_TYPE);
783 /* Skip object header. */
785 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
789 got_object_close(obj);
794 got_object_tree_close(struct got_tree_object *tree)
796 if (tree->refcnt > 0) {
798 if (tree->refcnt > 0)
806 static const struct got_error *
807 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
814 *elen = strnlen(buf, maxlen) + 1;
816 return got_error(GOT_ERR_BAD_OBJ_DATA);
818 space = memchr(buf, ' ', *elen);
819 if (space == NULL || space <= buf)
820 return got_error(GOT_ERR_BAD_OBJ_DATA);
825 if (*p < '0' || *p > '7')
826 return got_error(GOT_ERR_BAD_OBJ_DATA);
828 pte->mode |= *p - '0';
832 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
833 return got_error(GOT_ERR_BAD_OBJ_DATA);
835 pte->name = space + 1;
836 pte->namelen = strlen(pte->name);
839 *elen += SHA1_DIGEST_LENGTH;
844 pte_cmp(const void *pa, const void *pb)
846 const struct got_parsed_tree_entry *a = pa, *b = pb;
848 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
851 const struct got_error *
852 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
853 size_t *nentries_alloc, uint8_t *buf, size_t len)
855 const struct got_error *err = NULL;
857 const size_t nalloc = 16;
858 struct got_parsed_tree_entry *pte;
863 return NULL; /* tree is empty */
868 if (*nentries >= *nentries_alloc) {
869 pte = recallocarray(*entries, *nentries_alloc,
870 *nentries_alloc + nalloc, sizeof(**entries));
872 err = got_error_from_errno("recallocarray");
876 *nentries_alloc += nalloc;
879 pte = &(*entries)[*nentries];
880 err = parse_tree_entry(pte, &elen, buf, remain);
889 err = got_error(GOT_ERR_BAD_OBJ_DATA);
894 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
896 for (i = 0; i < *nentries - 1; i++) {
897 struct got_parsed_tree_entry *prev = &(*entries)[i];
898 pte = &(*entries)[i + 1];
899 if (got_path_cmp(prev->name, pte->name,
900 prev->namelen, pte->namelen) == 0) {
901 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
912 const struct got_error *
913 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
914 size_t *nentries_alloc, uint8_t **p, int fd,
915 struct got_object_id *expected_id)
917 const struct got_error *err = NULL;
918 struct got_object *obj = NULL;
920 struct got_inflate_checksum csum;
922 struct got_object_id id;
925 memset(&csum, 0, sizeof(csum));
926 csum.output_sha1 = &sha1_ctx;
928 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
932 SHA1Final(id.sha1, &sha1_ctx);
933 if (got_object_id_cmp(expected_id, &id) != 0) {
934 err = got_error_checksum(expected_id);
938 err = got_object_parse_header(&obj, *p, len);
942 if (len < obj->hdrlen + obj->size) {
943 err = got_error(GOT_ERR_BAD_OBJ_DATA);
947 /* Skip object header. */
949 err = got_object_parse_tree(entries, nentries, nentries_alloc,
950 *p + obj->hdrlen, len);
953 got_object_close(obj);
958 got_object_tag_close(struct got_tag_object *tag)
960 if (tag->refcnt > 0) {
972 const struct got_error *
973 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
975 const struct got_error *err = NULL;
981 return got_error(GOT_ERR_BAD_OBJ_DATA);
983 *tag = calloc(1, sizeof(**tag));
985 return got_error_from_errno("calloc");
987 label_len = strlen(GOT_TAG_LABEL_OBJECT);
988 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
990 if (remain < SHA1_DIGEST_STRING_LENGTH) {
991 err = got_error(GOT_ERR_BAD_OBJ_DATA);
995 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
996 err = got_error(GOT_ERR_BAD_OBJ_DATA);
999 remain -= SHA1_DIGEST_STRING_LENGTH;
1000 s += SHA1_DIGEST_STRING_LENGTH;
1002 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1007 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1011 label_len = strlen(GOT_TAG_LABEL_TYPE);
1012 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1013 remain -= label_len;
1015 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1019 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1020 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1021 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1022 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1024 remain -= label_len;
1025 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1026 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1027 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1028 label_len = strlen(GOT_OBJ_LABEL_TREE);
1030 remain -= label_len;
1031 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1032 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1033 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1034 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1036 remain -= label_len;
1037 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1038 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1039 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1040 label_len = strlen(GOT_OBJ_LABEL_TAG);
1042 remain -= label_len;
1044 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1048 if (remain <= 0 || *s != '\n') {
1049 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1055 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1059 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1063 label_len = strlen(GOT_TAG_LABEL_TAG);
1064 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1067 remain -= label_len;
1069 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1073 p = memchr(s, '\n', remain);
1075 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1080 (*tag)->tag = strndup(s, slen);
1081 if ((*tag)->tag == NULL) {
1082 err = got_error_from_errno("strndup");
1088 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1092 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1096 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1097 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1101 remain -= label_len;
1103 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1107 p = memchr(s, '\n', remain);
1109 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1114 err = parse_commit_time(&(*tag)->tagger_time,
1115 &(*tag)->tagger_gmtoff, s);
1118 (*tag)->tagger = strdup(s);
1119 if ((*tag)->tagger == NULL) {
1120 err = got_error_from_errno("strdup");
1126 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1130 /* Some old tags in the Linux git repo have no tagger. */
1131 (*tag)->tagger = strdup("");
1132 if ((*tag)->tagger == NULL) {
1133 err = got_error_from_errno("strdup");
1138 (*tag)->tagmsg = strndup(s, remain);
1139 if ((*tag)->tagmsg == NULL) {
1140 err = got_error_from_errno("strndup");
1145 got_object_tag_close(*tag);
1151 const struct got_error *
1152 got_object_read_tag(struct got_tag_object **tag, int fd,
1153 struct got_object_id *expected_id, size_t expected_size)
1155 const struct got_error *err = NULL;
1156 struct got_object *obj = NULL;
1159 struct got_inflate_checksum csum;
1161 struct got_object_id id;
1163 SHA1Init(&sha1_ctx);
1164 memset(&csum, 0, sizeof(csum));
1165 csum.output_sha1 = &sha1_ctx;
1167 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1172 SHA1Final(id.sha1, &sha1_ctx);
1173 if (got_object_id_cmp(expected_id, &id) != 0) {
1174 err = got_error_checksum(expected_id);
1178 err = got_object_parse_header(&obj, p, len);
1182 if (len < obj->hdrlen + obj->size) {
1183 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1187 /* Skip object header. */
1189 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1193 got_object_close(obj);
1197 const struct got_error *
1198 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1200 const struct got_error *err = NULL;
1201 static const size_t blocksize = 512;
1202 size_t n, total, remain;
1208 buf = malloc(blocksize);
1210 return got_error_from_errno("malloc");
1217 newbuf = reallocarray(buf, 1, total + blocksize);
1218 if (newbuf == NULL) {
1219 err = got_error_from_errno("reallocarray");
1223 remain += blocksize;
1225 n = fread(buf + total, 1, remain, f);
1228 err = got_ferror(f, GOT_ERR_IO);