Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/mman.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <imsg.h>
37 #include <time.h>
38 #include <unistd.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_path.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"
55 #ifndef nitems
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #endif
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
61 {
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
65 if (id2 == NULL)
66 return NULL;
67 memcpy(id2, id1, sizeof(*id2));
68 return id2;
69 }
71 int
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
74 {
75 if (id1->algo != id2->algo)
76 return -1;
78 if (id1->algo == GOT_HASH_SHA256)
79 return memcmp(id1->hash, id2->hash, SHA256_DIGEST_LENGTH);
80 if (id1->algo == GOT_HASH_SHA1)
81 return memcmp(id1->hash, id2->hash, SHA1_DIGEST_LENGTH);
82 return -1;
83 }
85 const struct got_error *
86 got_object_qid_alloc_partial(struct got_object_qid **qid)
87 {
88 *qid = malloc(sizeof(**qid));
89 if (*qid == NULL)
90 return got_error_from_errno("malloc");
92 (*qid)->data = NULL;
93 return NULL;
94 }
96 const struct got_error *
97 got_object_id_str(char **outbuf, struct got_object_id *id)
98 {
99 static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN;
101 *outbuf = malloc(len);
102 if (*outbuf == NULL)
103 return got_error_from_errno("malloc");
105 if (got_object_id_hex(id, *outbuf, len) == NULL) {
106 free(*outbuf);
107 *outbuf = NULL;
108 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
111 return NULL;
114 char *
115 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
117 if (id->algo == GOT_HASH_SHA256)
118 return got_sha256_digest_to_str(id->hash, buf, len);
119 return got_sha1_digest_to_str(id->hash, buf, len);
122 void
123 got_object_close(struct got_object *obj)
125 if (obj->refcnt > 0) {
126 obj->refcnt--;
127 if (obj->refcnt > 0)
128 return;
131 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
132 struct got_delta *delta;
133 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
134 delta = STAILQ_FIRST(&obj->deltas.entries);
135 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
136 free(delta);
139 free(obj);
142 const struct got_error *
143 got_object_raw_close(struct got_raw_object *obj)
145 const struct got_error *err = NULL;
147 if (obj->refcnt > 0) {
148 obj->refcnt--;
149 if (obj->refcnt > 0)
150 return NULL;
153 if (obj->close_cb)
154 obj->close_cb(obj);
156 if (obj->f == NULL) {
157 if (obj->fd != -1) {
158 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
159 err = got_error_from_errno("munmap");
160 if (close(obj->fd) == -1 && err == NULL)
161 err = got_error_from_errno("close");
162 } else
163 free(obj->data);
164 } else {
165 if (fclose(obj->f) == EOF && err == NULL)
166 err = got_error_from_errno("fclose");
168 free(obj);
169 return err;
172 void
173 got_object_qid_free(struct got_object_qid *qid)
175 free(qid);
178 void
179 got_object_id_queue_free(struct got_object_id_queue *ids)
181 struct got_object_qid *qid;
183 while (!STAILQ_EMPTY(ids)) {
184 qid = STAILQ_FIRST(ids);
185 STAILQ_REMOVE_HEAD(ids, entry);
186 got_object_qid_free(qid);
190 const struct got_error *
191 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
193 const char *obj_labels[] = {
194 GOT_OBJ_LABEL_COMMIT,
195 GOT_OBJ_LABEL_TREE,
196 GOT_OBJ_LABEL_BLOB,
197 GOT_OBJ_LABEL_TAG,
198 };
199 const int obj_types[] = {
200 GOT_OBJ_TYPE_COMMIT,
201 GOT_OBJ_TYPE_TREE,
202 GOT_OBJ_TYPE_BLOB,
203 GOT_OBJ_TYPE_TAG,
204 };
205 int type = 0;
206 size_t size = 0;
207 size_t i;
208 char *end;
210 *obj = NULL;
212 end = memchr(buf, '\0', len);
213 if (end == NULL)
214 return got_error(GOT_ERR_BAD_OBJ_HDR);
216 for (i = 0; i < nitems(obj_labels); i++) {
217 const char *label = obj_labels[i];
218 size_t label_len = strlen(label);
219 const char *errstr;
221 if (len <= label_len || buf + label_len >= end ||
222 strncmp(buf, label, label_len) != 0)
223 continue;
225 type = obj_types[i];
226 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
227 if (errstr != NULL)
228 return got_error(GOT_ERR_BAD_OBJ_HDR);
229 break;
232 if (type == 0)
233 return got_error(GOT_ERR_BAD_OBJ_HDR);
235 *obj = calloc(1, sizeof(**obj));
236 if (*obj == NULL)
237 return got_error_from_errno("calloc");
238 (*obj)->type = type;
239 (*obj)->hdrlen = end - buf + 1;
240 (*obj)->size = size;
241 return NULL;
244 const struct got_error *
245 got_object_read_header(struct got_object **obj, int fd)
247 const struct got_error *err;
248 struct got_inflate_buf zb;
249 uint8_t *buf;
250 const size_t zbsize = 64;
251 size_t outlen, totlen;
252 int nbuf = 1;
254 *obj = NULL;
256 buf = malloc(zbsize);
257 if (buf == NULL)
258 return got_error_from_errno("malloc");
259 buf[0] = '\0';
261 err = got_inflate_init(&zb, buf, zbsize, NULL);
262 if (err)
263 return err;
265 totlen = 0;
266 do {
267 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
268 if (err)
269 goto done;
270 if (outlen == 0)
271 break;
272 totlen += outlen;
273 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
274 uint8_t *newbuf;
275 nbuf++;
276 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
277 if (newbuf == NULL) {
278 err = got_error_from_errno("recallocarray");
279 goto done;
281 buf = newbuf;
282 zb.outbuf = newbuf + totlen;
283 zb.outlen = (nbuf * zbsize) - totlen;
285 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
287 err = got_object_parse_header(obj, buf, totlen);
288 done:
289 free(buf);
290 got_inflate_end(&zb);
291 return err;
294 const struct got_error *
295 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
296 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
297 int infd)
299 const struct got_error *err = NULL;
300 struct got_object *obj;
301 struct got_inflate_checksum csum;
302 uint8_t sha256[SHA256_DIGEST_LENGTH];
303 SHA2_CTX sha256_ctx;
304 uint8_t sha1[SHA1_DIGEST_LENGTH];
305 SHA1_CTX sha1_ctx;
306 size_t len, consumed;
307 FILE *f = NULL;
308 int r;
310 *outbuf = NULL;
311 *size = 0;
312 *hdrlen = 0;
314 memset(&csum, 0, sizeof(csum));
315 if (expected_id->algo == GOT_HASH_SHA256) {
316 SHA256Init(&sha256_ctx);
317 csum.output_sha256 = &sha256_ctx;
318 } else {
319 SHA1Init(&sha1_ctx);
320 csum.output_sha1 = &sha1_ctx;
323 if (lseek(infd, SEEK_SET, 0) == -1)
324 return got_error_from_errno("lseek");
326 err = got_object_read_header(&obj, infd);
327 if (err)
328 return err;
330 if (lseek(infd, SEEK_SET, 0) == -1)
331 return got_error_from_errno("lseek");
333 if (obj->size + obj->hdrlen <= max_in_mem_size) {
334 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
335 obj->size + obj->hdrlen, infd);
336 } else {
337 int fd;
338 /*
339 * XXX This uses an extra file descriptor for no good reason.
340 * We should have got_inflate_fd_to_fd().
341 */
342 fd = dup(infd);
343 if (fd == -1)
344 return got_error_from_errno("dup");
345 f = fdopen(fd, "r");
346 if (f == NULL) {
347 err = got_error_from_errno("fdopen");
348 abort();
349 close(fd);
350 goto done;
352 err = got_inflate_to_fd(&len, f, &csum, outfd);
354 if (err)
355 goto done;
357 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
358 err = got_error(GOT_ERR_BAD_OBJ_HDR);
359 goto done;
362 if (expected_id->algo == GOT_HASH_SHA256) {
363 SHA256Final(sha256, &sha256_ctx);
364 r = memcmp(expected_id->hash, sha256, sizeof(sha256));
365 } else {
366 SHA1Final(sha1, &sha1_ctx);
367 r = memcmp(expected_id->hash, sha1, sizeof(sha1));
370 if (r != 0) {
371 err = got_error_checksum(expected_id);
372 goto done;
375 *size = obj->size;
376 *hdrlen = obj->hdrlen;
377 done:
378 got_object_close(obj);
379 if (f && fclose(f) == EOF && err == NULL)
380 err = got_error_from_errno("fclose");
381 return err;
384 struct got_commit_object *
385 got_object_commit_alloc_partial(void)
387 struct got_commit_object *commit;
389 commit = calloc(1, sizeof(*commit));
390 if (commit == NULL)
391 return NULL;
392 commit->tree_id = malloc(sizeof(*commit->tree_id));
393 if (commit->tree_id == NULL) {
394 free(commit);
395 return NULL;
398 STAILQ_INIT(&commit->parent_ids);
400 return commit;
403 const struct got_error *
404 got_object_commit_add_parent(struct got_commit_object *commit,
405 const char *id_str, int algo)
407 const struct got_error *err = NULL;
408 struct got_object_qid *qid;
410 err = got_object_qid_alloc_partial(&qid);
411 if (err)
412 return err;
414 if (!got_parse_hash_digest(qid->id.hash, id_str, algo, NULL)) {
415 err = got_error(GOT_ERR_BAD_OBJ_DATA);
416 got_object_qid_free(qid);
417 return err;
419 qid->id.algo = algo;
421 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
422 commit->nparents++;
424 return NULL;
427 static const struct got_error *
428 parse_gmtoff(time_t *gmtoff, const char *tzstr)
430 int sign = 1;
431 const char *p = tzstr;
432 time_t h, m;
434 *gmtoff = 0;
436 if (*p == '-')
437 sign = -1;
438 else if (*p != '+')
439 return got_error(GOT_ERR_BAD_OBJ_DATA);
440 p++;
441 if (!isdigit((unsigned char)*p) &&
442 !isdigit((unsigned char)*(p + 1)))
443 return got_error(GOT_ERR_BAD_OBJ_DATA);
444 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
446 p += 2;
447 if (!isdigit((unsigned char)*p) &&
448 !isdigit((unsigned char)*(p + 1)))
449 return got_error(GOT_ERR_BAD_OBJ_DATA);
450 m = ((*p - '0') * 10) + (*(p + 1) - '0');
452 *gmtoff = (h * 60 * 60 + m * 60) * sign;
453 return NULL;
456 static const struct got_error *
457 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
459 const struct got_error *err = NULL;
460 const char *errstr;
461 char *space, *tzstr;
463 /* Parse and strip off trailing timezone indicator string. */
464 space = strrchr(committer, ' ');
465 if (space == NULL)
466 return got_error(GOT_ERR_BAD_OBJ_DATA);
467 tzstr = strdup(space + 1);
468 if (tzstr == NULL)
469 return got_error_from_errno("strdup");
470 err = parse_gmtoff(gmtoff, tzstr);
471 free(tzstr);
472 if (err) {
473 if (err->code != GOT_ERR_BAD_OBJ_DATA)
474 return err;
475 /* Old versions of Git omitted the timestamp. */
476 *time = 0;
477 *gmtoff = 0;
478 return NULL;
480 *space = '\0';
482 /* Timestamp is separated from committer name + email by space. */
483 space = strrchr(committer, ' ');
484 if (space == NULL)
485 return got_error(GOT_ERR_BAD_OBJ_DATA);
487 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
488 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
489 if (errstr)
490 return got_error(GOT_ERR_BAD_OBJ_DATA);
492 /* Strip off parsed time information, leaving just author and email. */
493 *space = '\0';
495 return NULL;
498 void
499 got_object_commit_close(struct got_commit_object *commit)
501 if (commit->refcnt > 0) {
502 commit->refcnt--;
503 if (commit->refcnt > 0)
504 return;
507 got_object_id_queue_free(&commit->parent_ids);
508 free(commit->tree_id);
509 free(commit->author);
510 free(commit->committer);
511 free(commit->logmsg);
512 free(commit);
515 struct got_object_id *
516 got_object_commit_get_tree_id(struct got_commit_object *commit)
518 return commit->tree_id;
521 int
522 got_object_commit_get_nparents(struct got_commit_object *commit)
524 return commit->nparents;
527 const struct got_object_id_queue *
528 got_object_commit_get_parent_ids(struct got_commit_object *commit)
530 return &commit->parent_ids;
533 const char *
534 got_object_commit_get_author(struct got_commit_object *commit)
536 return commit->author;
539 time_t
540 got_object_commit_get_author_time(struct got_commit_object *commit)
542 return commit->author_time;
545 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
547 return commit->author_gmtoff;
550 const char *
551 got_object_commit_get_committer(struct got_commit_object *commit)
553 return commit->committer;
556 time_t
557 got_object_commit_get_committer_time(struct got_commit_object *commit)
559 return commit->committer_time;
562 time_t
563 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
565 return commit->committer_gmtoff;
568 const struct got_error *
569 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
571 const struct got_error *err = NULL;
572 const char *src;
573 char *dst;
574 size_t len;
576 len = strlen(commit->logmsg);
577 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
578 if (*logmsg == NULL)
579 return got_error_from_errno("malloc");
581 /*
582 * Strip out unusual headers. Headers are separated from the commit
583 * message body by a single empty line.
584 */
585 src = commit->logmsg;
586 dst = *logmsg;
587 while (*src != '\0' && *src != '\n') {
588 int copy_header = 1, eol = 0;
589 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
590 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
591 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
592 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
593 strncmp(src, GOT_COMMIT_LABEL_PARENT,
594 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
595 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
596 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
597 copy_header = 0;
599 while (*src != '\0' && !eol) {
600 if (copy_header) {
601 *dst = *src;
602 dst++;
604 if (*src == '\n')
605 eol = 1;
606 src++;
609 *dst = '\0';
611 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
612 err = got_error(GOT_ERR_NO_SPACE);
613 goto done;
616 /* Trim redundant trailing whitespace. */
617 len = strlen(*logmsg);
618 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
619 isspace((unsigned char)(*logmsg)[len - 1])) {
620 (*logmsg)[len - 1] = '\0';
621 len--;
624 /* Append a trailing newline if missing. */
625 if (len > 0 && (*logmsg)[len - 1] != '\n') {
626 (*logmsg)[len] = '\n';
627 (*logmsg)[len + 1] = '\0';
629 done:
630 if (err) {
631 free(*logmsg);
632 *logmsg = NULL;
634 return err;
637 const char *
638 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
640 return commit->logmsg;
643 const struct got_error *
644 got_object_parse_commit(struct got_commit_object **commit, char *buf,
645 size_t len, int algo)
647 const struct got_error *err = NULL;
648 char *s = buf;
649 size_t label_len;
650 size_t digest_strlen;
651 ssize_t remain = (ssize_t)len;
653 if (algo == GOT_HASH_SHA256)
654 digest_strlen = SHA256_DIGEST_STRING_LENGTH;
655 else
656 digest_strlen = SHA1_DIGEST_STRING_LENGTH;
658 if (remain == 0)
659 return got_error(GOT_ERR_BAD_OBJ_DATA);
661 *commit = got_object_commit_alloc_partial();
662 if (*commit == NULL)
663 return got_error_from_errno("got_object_commit_alloc_partial");
665 label_len = strlen(GOT_COMMIT_LABEL_TREE);
666 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
667 remain -= label_len;
668 if (remain < digest_strlen) {
669 err = got_error(GOT_ERR_BAD_OBJ_DATA);
670 goto done;
672 s += label_len;
673 if (!got_parse_hash_digest((*commit)->tree_id->hash, s,
674 algo, NULL)) {
675 err = got_error(GOT_ERR_BAD_OBJ_DATA);
676 goto done;
678 (*commit)->tree_id->algo = algo;
679 remain -= digest_strlen;
680 s += digest_strlen;
681 } else {
682 err = got_error(GOT_ERR_BAD_OBJ_DATA);
683 goto done;
686 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
687 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
688 remain -= label_len;
689 if (remain < digest_strlen) {
690 err = got_error(GOT_ERR_BAD_OBJ_DATA);
691 goto done;
693 s += label_len;
694 err = got_object_commit_add_parent(*commit, s, algo);
695 if (err)
696 goto done;
698 remain -= digest_strlen;
699 s += digest_strlen;
702 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
703 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
704 char *p;
705 size_t slen;
707 remain -= label_len;
708 if (remain <= 0) {
709 err = got_error(GOT_ERR_BAD_OBJ_DATA);
710 goto done;
712 s += label_len;
713 p = memchr(s, '\n', remain);
714 if (p == NULL) {
715 err = got_error(GOT_ERR_BAD_OBJ_DATA);
716 goto done;
718 *p = '\0';
719 slen = strlen(s);
720 err = parse_commit_time(&(*commit)->author_time,
721 &(*commit)->author_gmtoff, s);
722 if (err)
723 goto done;
724 (*commit)->author = strdup(s);
725 if ((*commit)->author == NULL) {
726 err = got_error_from_errno("strdup");
727 goto done;
729 s += slen + 1;
730 remain -= slen + 1;
733 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
734 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
735 char *p;
736 size_t slen;
738 remain -= label_len;
739 if (remain <= 0) {
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
741 goto done;
743 s += label_len;
744 p = memchr(s, '\n', remain);
745 if (p == NULL) {
746 err = got_error(GOT_ERR_BAD_OBJ_DATA);
747 goto done;
749 *p = '\0';
750 slen = strlen(s);
751 err = parse_commit_time(&(*commit)->committer_time,
752 &(*commit)->committer_gmtoff, s);
753 if (err)
754 goto done;
755 (*commit)->committer = strdup(s);
756 if ((*commit)->committer == NULL) {
757 err = got_error_from_errno("strdup");
758 goto done;
760 s += slen + 1;
761 remain -= slen + 1;
764 (*commit)->logmsg = strndup(s, remain);
765 if ((*commit)->logmsg == NULL) {
766 err = got_error_from_errno("strndup");
767 goto done;
769 done:
770 if (err) {
771 got_object_commit_close(*commit);
772 *commit = NULL;
774 return err;
777 const struct got_error *
778 got_object_read_commit(struct got_commit_object **commit, int fd,
779 struct got_object_id *expected_id, size_t expected_size)
781 struct got_object *obj = NULL;
782 const struct got_error *err = NULL;
783 size_t len;
784 uint8_t *p;
785 struct got_inflate_checksum csum;
786 SHA1_CTX sha1_ctx;
787 SHA2_CTX sha256_ctx;
788 struct got_object_id id;
790 memset(&csum, 0, sizeof(csum));
791 id.algo = expected_id->algo;
792 if (expected_id->algo == GOT_HASH_SHA256) {
793 SHA256Init(&sha256_ctx);
794 csum.output_sha256 = &sha256_ctx;
795 } else {
796 SHA1Init(&sha1_ctx);
797 csum.output_sha1 = &sha1_ctx;
800 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
801 if (err)
802 return err;
804 if (expected_id->algo == GOT_HASH_SHA256)
805 SHA256Final(id.hash, &sha256_ctx);
806 else
807 SHA1Final(id.hash, &sha1_ctx);
809 if (got_object_id_cmp(expected_id, &id) != 0) {
810 err = got_error_checksum(expected_id);
811 goto done;
814 err = got_object_parse_header(&obj, p, len);
815 if (err)
816 goto done;
818 if (len < obj->hdrlen + obj->size) {
819 err = got_error(GOT_ERR_BAD_OBJ_DATA);
820 goto done;
823 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
824 err = got_error(GOT_ERR_OBJ_TYPE);
825 goto done;
828 /* Skip object header. */
829 len -= obj->hdrlen;
830 err = got_object_parse_commit(commit, p + obj->hdrlen, len,
831 expected_id->algo);
832 done:
833 free(p);
834 if (obj)
835 got_object_close(obj);
836 return err;
839 void
840 got_object_tree_close(struct got_tree_object *tree)
842 if (tree->refcnt > 0) {
843 tree->refcnt--;
844 if (tree->refcnt > 0)
845 return;
848 free(tree->entries);
849 free(tree);
852 static const struct got_error *
853 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
854 size_t maxlen, size_t idlen)
856 char *p, *space;
858 *elen = 0;
860 *elen = strnlen(buf, maxlen) + 1;
861 if (*elen > maxlen)
862 return got_error(GOT_ERR_BAD_OBJ_DATA);
864 space = memchr(buf, ' ', *elen);
865 if (space == NULL || space <= buf)
866 return got_error(GOT_ERR_BAD_OBJ_DATA);
868 pte->mode = 0;
869 p = buf;
870 while (p < space) {
871 if (*p < '0' || *p > '7')
872 return got_error(GOT_ERR_BAD_OBJ_DATA);
873 pte->mode <<= 3;
874 pte->mode |= *p - '0';
875 p++;
878 if (*elen > maxlen || maxlen - *elen < idlen)
879 return got_error(GOT_ERR_BAD_OBJ_DATA);
881 pte->name = space + 1;
882 pte->namelen = strlen(pte->name);
883 buf += *elen;
884 pte->id = buf;
885 pte->idlen = idlen;
886 *elen += idlen;
887 return NULL;
890 static int
891 pte_cmp(const void *pa, const void *pb)
893 const struct got_parsed_tree_entry *a = pa, *b = pb;
895 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
898 const struct got_error *
899 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
900 size_t *nentries_alloc, uint8_t *buf, size_t len, int algo)
902 const struct got_error *err = NULL;
903 size_t remain = len;
904 const size_t nalloc = 16;
905 struct got_parsed_tree_entry *pte;
906 size_t idlen = SHA256_DIGEST_LENGTH;
907 int i;
909 if (algo != GOT_HASH_SHA256)
910 idlen = SHA1_DIGEST_LENGTH;
912 *nentries = 0;
913 if (remain == 0)
914 return NULL; /* tree is empty */
916 while (remain > 0) {
917 size_t elen;
919 if (*nentries >= *nentries_alloc) {
920 pte = recallocarray(*entries, *nentries_alloc,
921 *nentries_alloc + nalloc, sizeof(**entries));
922 if (pte == NULL) {
923 err = got_error_from_errno("recallocarray");
924 goto done;
926 *entries = pte;
927 *nentries_alloc += nalloc;
930 pte = &(*entries)[*nentries];
931 err = parse_tree_entry(pte, &elen, buf, remain, idlen);
932 if (err)
933 goto done;
934 buf += elen;
935 remain -= elen;
936 (*nentries)++;
939 if (remain != 0) {
940 err = got_error(GOT_ERR_BAD_OBJ_DATA);
941 goto done;
944 if (*nentries > 1) {
945 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
947 for (i = 0; i < *nentries - 1; i++) {
948 struct got_parsed_tree_entry *prev = &(*entries)[i];
949 pte = &(*entries)[i + 1];
950 if (got_path_cmp(prev->name, pte->name,
951 prev->namelen, pte->namelen) == 0) {
952 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
953 break;
957 done:
958 if (err)
959 *nentries = 0;
960 return err;
963 const struct got_error *
964 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
965 size_t *nentries_alloc, uint8_t **p, int fd,
966 struct got_object_id *expected_id)
968 const struct got_error *err = NULL;
969 struct got_object *obj = NULL;
970 size_t len;
971 struct got_inflate_checksum csum;
972 SHA2_CTX sha256_ctx;
973 SHA1_CTX sha1_ctx;
974 struct got_object_id id;
976 memset(&csum, 0, sizeof(csum));
977 id.algo = expected_id->algo;
978 if (expected_id->algo == GOT_HASH_SHA256) {
979 SHA256Init(&sha256_ctx);
980 csum.output_sha256 = &sha256_ctx;
981 } else {
982 SHA1Init(&sha1_ctx);
983 csum.output_sha1 = &sha1_ctx;
986 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
987 if (err)
988 return err;
990 if (expected_id->algo == GOT_HASH_SHA256)
991 SHA256Final(id.hash, &sha256_ctx);
992 else
993 SHA1Final(id.hash, &sha1_ctx);
994 if (got_object_id_cmp(expected_id, &id) != 0) {
995 err = got_error_checksum(expected_id);
996 goto done;
999 err = got_object_parse_header(&obj, *p, len);
1000 if (err)
1001 goto done;
1003 if (len < obj->hdrlen + obj->size) {
1004 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1005 goto done;
1008 /* Skip object header. */
1009 len -= obj->hdrlen;
1010 err = got_object_parse_tree(entries, nentries, nentries_alloc,
1011 *p + obj->hdrlen, len, expected_id->algo);
1012 done:
1013 if (obj)
1014 got_object_close(obj);
1015 return err;
1018 void
1019 got_object_tag_close(struct got_tag_object *tag)
1021 if (tag->refcnt > 0) {
1022 tag->refcnt--;
1023 if (tag->refcnt > 0)
1024 return;
1027 free(tag->tag);
1028 free(tag->tagger);
1029 free(tag->tagmsg);
1030 free(tag);
1033 const struct got_error *
1034 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len,
1035 int algo)
1037 const struct got_error *err = NULL;
1038 size_t remain = len;
1039 char *s = buf;
1040 size_t label_len, digest_strlen = SHA256_DIGEST_STRING_LENGTH;
1042 if (algo != GOT_HASH_SHA256)
1043 digest_strlen = SHA1_DIGEST_STRING_LENGTH;
1045 if (remain == 0)
1046 return got_error(GOT_ERR_BAD_OBJ_DATA);
1048 *tag = calloc(1, sizeof(**tag));
1049 if (*tag == NULL)
1050 return got_error_from_errno("calloc");
1052 label_len = strlen(GOT_TAG_LABEL_OBJECT);
1053 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
1054 remain -= label_len;
1055 if (remain < digest_strlen) {
1056 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1057 goto done;
1059 s += label_len;
1060 if (!got_parse_hash_digest((*tag)->id.hash, s, algo, NULL)) {
1061 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 goto done;
1064 (*tag)->id.algo = algo;
1065 remain -= digest_strlen;
1066 s += digest_strlen;
1067 } else {
1068 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1069 goto done;
1072 if (remain <= 0) {
1073 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1074 goto done;
1077 label_len = strlen(GOT_TAG_LABEL_TYPE);
1078 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1079 remain -= label_len;
1080 if (remain <= 0) {
1081 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1082 goto done;
1084 s += label_len;
1085 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1086 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1087 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1088 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1089 s += label_len;
1090 remain -= label_len;
1091 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1092 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1093 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1094 label_len = strlen(GOT_OBJ_LABEL_TREE);
1095 s += label_len;
1096 remain -= label_len;
1097 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1098 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1099 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1100 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1101 s += label_len;
1102 remain -= label_len;
1103 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1104 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1105 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1106 label_len = strlen(GOT_OBJ_LABEL_TAG);
1107 s += label_len;
1108 remain -= label_len;
1109 } else {
1110 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1111 goto done;
1114 if (remain <= 0 || *s != '\n') {
1115 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1116 goto done;
1118 s++;
1119 remain--;
1120 if (remain <= 0) {
1121 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1122 goto done;
1124 } else {
1125 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1126 goto done;
1129 label_len = strlen(GOT_TAG_LABEL_TAG);
1130 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1131 char *p;
1132 size_t slen;
1133 remain -= label_len;
1134 if (remain <= 0) {
1135 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1136 goto done;
1138 s += label_len;
1139 p = memchr(s, '\n', remain);
1140 if (p == NULL) {
1141 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1142 goto done;
1144 *p = '\0';
1145 slen = strlen(s);
1146 (*tag)->tag = strndup(s, slen);
1147 if ((*tag)->tag == NULL) {
1148 err = got_error_from_errno("strndup");
1149 goto done;
1151 s += slen + 1;
1152 remain -= slen + 1;
1153 if (remain <= 0) {
1154 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1155 goto done;
1157 } else {
1158 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1159 goto done;
1162 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1163 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1164 char *p;
1165 size_t slen;
1167 remain -= label_len;
1168 if (remain <= 0) {
1169 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1170 goto done;
1172 s += label_len;
1173 p = memchr(s, '\n', remain);
1174 if (p == NULL) {
1175 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1176 goto done;
1178 *p = '\0';
1179 slen = strlen(s);
1180 err = parse_commit_time(&(*tag)->tagger_time,
1181 &(*tag)->tagger_gmtoff, s);
1182 if (err)
1183 goto done;
1184 (*tag)->tagger = strdup(s);
1185 if ((*tag)->tagger == NULL) {
1186 err = got_error_from_errno("strdup");
1187 goto done;
1189 s += slen + 1;
1190 remain -= slen + 1;
1191 if (remain < 0) {
1192 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1193 goto done;
1195 } else {
1196 /* Some old tags in the Linux git repo have no tagger. */
1197 (*tag)->tagger = strdup("");
1198 if ((*tag)->tagger == NULL) {
1199 err = got_error_from_errno("strdup");
1200 goto done;
1204 (*tag)->tagmsg = strndup(s, remain);
1205 if ((*tag)->tagmsg == NULL) {
1206 err = got_error_from_errno("strndup");
1207 goto done;
1209 done:
1210 if (err) {
1211 got_object_tag_close(*tag);
1212 *tag = NULL;
1214 return err;
1217 const struct got_error *
1218 got_object_read_tag(struct got_tag_object **tag, int fd,
1219 struct got_object_id *expected_id, size_t expected_size)
1221 const struct got_error *err = NULL;
1222 struct got_object *obj = NULL;
1223 size_t len;
1224 uint8_t *p;
1225 struct got_inflate_checksum csum;
1226 SHA2_CTX sha256_ctx;
1227 SHA1_CTX sha1_ctx;
1228 struct got_object_id id;
1230 memset(&csum, 0, sizeof(csum));
1231 id.algo = expected_id->algo;
1232 if (expected_id->algo == GOT_HASH_SHA256) {
1233 SHA256Init(&sha256_ctx);
1234 csum.output_sha256 = &sha256_ctx;
1235 } else {
1236 SHA1Init(&sha1_ctx);
1237 csum.output_sha1 = &sha1_ctx;
1240 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1241 expected_size, fd);
1242 if (err)
1243 return err;
1245 if (expected_id->algo == GOT_HASH_SHA256)
1246 SHA256Final(id.hash, &sha256_ctx);
1247 else
1248 SHA1Final(id.hash, &sha1_ctx);
1249 if (got_object_id_cmp(expected_id, &id) != 0) {
1250 err = got_error_checksum(expected_id);
1251 goto done;
1254 err = got_object_parse_header(&obj, p, len);
1255 if (err)
1256 goto done;
1258 if (len < obj->hdrlen + obj->size) {
1259 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1260 goto done;
1263 /* Skip object header. */
1264 len -= obj->hdrlen;
1265 err = got_object_parse_tag(tag, p + obj->hdrlen, len, expected_id->algo);
1266 done:
1267 free(p);
1268 if (obj)
1269 got_object_close(obj);
1270 return err;
1273 const struct got_error *
1274 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1276 const struct got_error *err = NULL;
1277 static const size_t blocksize = 512;
1278 size_t n, total, remain;
1279 uint8_t *buf;
1281 *outbuf = NULL;
1282 *outlen = 0;
1284 buf = malloc(blocksize);
1285 if (buf == NULL)
1286 return got_error_from_errno("malloc");
1288 remain = blocksize;
1289 total = 0;
1290 for (;;) {
1291 if (remain == 0) {
1292 uint8_t *newbuf;
1293 newbuf = reallocarray(buf, 1, total + blocksize);
1294 if (newbuf == NULL) {
1295 err = got_error_from_errno("reallocarray");
1296 goto done;
1298 buf = newbuf;
1299 remain += blocksize;
1301 n = fread(buf + total, 1, remain, f);
1302 if (n == 0) {
1303 if (ferror(f)) {
1304 err = got_ferror(f, GOT_ERR_IO);
1305 goto done;
1307 break; /* EOF */
1309 remain -= n;
1310 total += n;
1313 done:
1314 if (err == NULL) {
1315 *outbuf = buf;
1316 *outlen = total;
1317 } else
1318 free(buf);
1319 return err;