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 return got_sha1_digest_to_str(id->sha1, buf, len);
120 void
121 got_object_close(struct got_object *obj)
123 if (obj->refcnt > 0) {
124 obj->refcnt--;
125 if (obj->refcnt > 0)
126 return;
129 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
130 struct got_delta *delta;
131 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
132 delta = STAILQ_FIRST(&obj->deltas.entries);
133 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
134 free(delta);
137 free(obj);
140 const struct got_error *
141 got_object_raw_close(struct got_raw_object *obj)
143 const struct got_error *err = NULL;
145 if (obj->refcnt > 0) {
146 obj->refcnt--;
147 if (obj->refcnt > 0)
148 return NULL;
151 if (obj->close_cb)
152 obj->close_cb(obj);
154 if (obj->f == NULL) {
155 if (obj->fd != -1) {
156 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
157 err = got_error_from_errno("munmap");
158 if (close(obj->fd) == -1 && err == NULL)
159 err = got_error_from_errno("close");
160 } else
161 free(obj->data);
162 } else {
163 if (fclose(obj->f) == EOF && err == NULL)
164 err = got_error_from_errno("fclose");
166 free(obj);
167 return err;
170 void
171 got_object_qid_free(struct got_object_qid *qid)
173 free(qid);
176 void
177 got_object_id_queue_free(struct got_object_id_queue *ids)
179 struct got_object_qid *qid;
181 while (!STAILQ_EMPTY(ids)) {
182 qid = STAILQ_FIRST(ids);
183 STAILQ_REMOVE_HEAD(ids, entry);
184 got_object_qid_free(qid);
188 const struct got_error *
189 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
191 const char *obj_labels[] = {
192 GOT_OBJ_LABEL_COMMIT,
193 GOT_OBJ_LABEL_TREE,
194 GOT_OBJ_LABEL_BLOB,
195 GOT_OBJ_LABEL_TAG,
196 };
197 const int obj_types[] = {
198 GOT_OBJ_TYPE_COMMIT,
199 GOT_OBJ_TYPE_TREE,
200 GOT_OBJ_TYPE_BLOB,
201 GOT_OBJ_TYPE_TAG,
202 };
203 int type = 0;
204 size_t size = 0;
205 size_t i;
206 char *end;
208 *obj = NULL;
210 end = memchr(buf, '\0', len);
211 if (end == NULL)
212 return got_error(GOT_ERR_BAD_OBJ_HDR);
214 for (i = 0; i < nitems(obj_labels); i++) {
215 const char *label = obj_labels[i];
216 size_t label_len = strlen(label);
217 const char *errstr;
219 if (len <= label_len || buf + label_len >= end ||
220 strncmp(buf, label, label_len) != 0)
221 continue;
223 type = obj_types[i];
224 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
225 if (errstr != NULL)
226 return got_error(GOT_ERR_BAD_OBJ_HDR);
227 break;
230 if (type == 0)
231 return got_error(GOT_ERR_BAD_OBJ_HDR);
233 *obj = calloc(1, sizeof(**obj));
234 if (*obj == NULL)
235 return got_error_from_errno("calloc");
236 (*obj)->type = type;
237 (*obj)->hdrlen = end - buf + 1;
238 (*obj)->size = size;
239 return NULL;
242 const struct got_error *
243 got_object_read_header(struct got_object **obj, int fd)
245 const struct got_error *err;
246 struct got_inflate_buf zb;
247 uint8_t *buf;
248 const size_t zbsize = 64;
249 size_t outlen, totlen;
250 int nbuf = 1;
252 *obj = NULL;
254 buf = malloc(zbsize);
255 if (buf == NULL)
256 return got_error_from_errno("malloc");
257 buf[0] = '\0';
259 err = got_inflate_init(&zb, buf, zbsize, NULL);
260 if (err)
261 return err;
263 totlen = 0;
264 do {
265 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
266 if (err)
267 goto done;
268 if (outlen == 0)
269 break;
270 totlen += outlen;
271 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
272 uint8_t *newbuf;
273 nbuf++;
274 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
275 if (newbuf == NULL) {
276 err = got_error_from_errno("recallocarray");
277 goto done;
279 buf = newbuf;
280 zb.outbuf = newbuf + totlen;
281 zb.outlen = (nbuf * zbsize) - totlen;
283 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
285 err = got_object_parse_header(obj, buf, totlen);
286 done:
287 free(buf);
288 got_inflate_end(&zb);
289 return err;
292 const struct got_error *
293 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
294 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
295 int infd)
297 const struct got_error *err = NULL;
298 struct got_object *obj;
299 struct got_inflate_checksum csum;
300 uint8_t sha256[SHA256_DIGEST_LENGTH];
301 SHA2_CTX sha256_ctx;
302 uint8_t sha1[SHA1_DIGEST_LENGTH];
303 SHA1_CTX sha1_ctx;
304 size_t len, consumed;
305 FILE *f = NULL;
306 int r;
308 *outbuf = NULL;
309 *size = 0;
310 *hdrlen = 0;
312 memset(&csum, 0, sizeof(csum));
313 if (expected_id->algo == GOT_HASH_SHA256) {
314 SHA256Init(&sha256_ctx);
315 csum.output_sha256 = &sha256_ctx;
316 } else {
317 SHA1Init(&sha1_ctx);
318 csum.output_sha1 = &sha1_ctx;
321 if (lseek(infd, SEEK_SET, 0) == -1)
322 return got_error_from_errno("lseek");
324 err = got_object_read_header(&obj, infd);
325 if (err)
326 return err;
328 if (lseek(infd, SEEK_SET, 0) == -1)
329 return got_error_from_errno("lseek");
331 if (obj->size + obj->hdrlen <= max_in_mem_size) {
332 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
333 obj->size + obj->hdrlen, infd);
334 } else {
335 int fd;
336 /*
337 * XXX This uses an extra file descriptor for no good reason.
338 * We should have got_inflate_fd_to_fd().
339 */
340 fd = dup(infd);
341 if (fd == -1)
342 return got_error_from_errno("dup");
343 f = fdopen(fd, "r");
344 if (f == NULL) {
345 err = got_error_from_errno("fdopen");
346 abort();
347 close(fd);
348 goto done;
350 err = got_inflate_to_fd(&len, f, &csum, outfd);
352 if (err)
353 goto done;
355 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
356 err = got_error(GOT_ERR_BAD_OBJ_HDR);
357 goto done;
360 if (expected_id->algo == GOT_HASH_SHA256) {
361 SHA256Final(sha256, &sha256_ctx);
362 r = memcmp(expected_id->hash, sha256, sizeof(sha256));
363 } else {
364 SHA1Final(sha1, &sha1_ctx);
365 r = memcmp(expected_id->hash, sha1, sizeof(sha1));
368 if (r != 0) {
369 err = got_error_checksum(expected_id);
370 goto done;
373 *size = obj->size;
374 *hdrlen = obj->hdrlen;
375 done:
376 got_object_close(obj);
377 if (f && fclose(f) == EOF && err == NULL)
378 err = got_error_from_errno("fclose");
379 return err;
382 struct got_commit_object *
383 got_object_commit_alloc_partial(void)
385 struct got_commit_object *commit;
387 commit = calloc(1, sizeof(*commit));
388 if (commit == NULL)
389 return NULL;
390 commit->tree_id = malloc(sizeof(*commit->tree_id));
391 if (commit->tree_id == NULL) {
392 free(commit);
393 return NULL;
396 STAILQ_INIT(&commit->parent_ids);
398 return commit;
401 const struct got_error *
402 got_object_commit_add_parent(struct got_commit_object *commit,
403 const char *id_str, int algo)
405 const struct got_error *err = NULL;
406 struct got_object_qid *qid;
408 err = got_object_qid_alloc_partial(&qid);
409 if (err)
410 return err;
412 if (!got_parse_hash_digest(qid->id.hash, id_str, algo, NULL)) {
413 err = got_error(GOT_ERR_BAD_OBJ_DATA);
414 got_object_qid_free(qid);
415 return err;
417 qid->id.algo = algo;
419 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
420 commit->nparents++;
422 return NULL;
425 static const struct got_error *
426 parse_gmtoff(time_t *gmtoff, const char *tzstr)
428 int sign = 1;
429 const char *p = tzstr;
430 time_t h, m;
432 *gmtoff = 0;
434 if (*p == '-')
435 sign = -1;
436 else if (*p != '+')
437 return got_error(GOT_ERR_BAD_OBJ_DATA);
438 p++;
439 if (!isdigit((unsigned char)*p) &&
440 !isdigit((unsigned char)*(p + 1)))
441 return got_error(GOT_ERR_BAD_OBJ_DATA);
442 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
444 p += 2;
445 if (!isdigit((unsigned char)*p) &&
446 !isdigit((unsigned char)*(p + 1)))
447 return got_error(GOT_ERR_BAD_OBJ_DATA);
448 m = ((*p - '0') * 10) + (*(p + 1) - '0');
450 *gmtoff = (h * 60 * 60 + m * 60) * sign;
451 return NULL;
454 static const struct got_error *
455 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
457 const struct got_error *err = NULL;
458 const char *errstr;
459 char *space, *tzstr;
461 /* Parse and strip off trailing timezone indicator string. */
462 space = strrchr(committer, ' ');
463 if (space == NULL)
464 return got_error(GOT_ERR_BAD_OBJ_DATA);
465 tzstr = strdup(space + 1);
466 if (tzstr == NULL)
467 return got_error_from_errno("strdup");
468 err = parse_gmtoff(gmtoff, tzstr);
469 free(tzstr);
470 if (err) {
471 if (err->code != GOT_ERR_BAD_OBJ_DATA)
472 return err;
473 /* Old versions of Git omitted the timestamp. */
474 *time = 0;
475 *gmtoff = 0;
476 return NULL;
478 *space = '\0';
480 /* Timestamp is separated from committer name + email by space. */
481 space = strrchr(committer, ' ');
482 if (space == NULL)
483 return got_error(GOT_ERR_BAD_OBJ_DATA);
485 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
486 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
487 if (errstr)
488 return got_error(GOT_ERR_BAD_OBJ_DATA);
490 /* Strip off parsed time information, leaving just author and email. */
491 *space = '\0';
493 return NULL;
496 void
497 got_object_commit_close(struct got_commit_object *commit)
499 if (commit->refcnt > 0) {
500 commit->refcnt--;
501 if (commit->refcnt > 0)
502 return;
505 got_object_id_queue_free(&commit->parent_ids);
506 free(commit->tree_id);
507 free(commit->author);
508 free(commit->committer);
509 free(commit->logmsg);
510 free(commit);
513 struct got_object_id *
514 got_object_commit_get_tree_id(struct got_commit_object *commit)
516 return commit->tree_id;
519 int
520 got_object_commit_get_nparents(struct got_commit_object *commit)
522 return commit->nparents;
525 const struct got_object_id_queue *
526 got_object_commit_get_parent_ids(struct got_commit_object *commit)
528 return &commit->parent_ids;
531 const char *
532 got_object_commit_get_author(struct got_commit_object *commit)
534 return commit->author;
537 time_t
538 got_object_commit_get_author_time(struct got_commit_object *commit)
540 return commit->author_time;
543 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
545 return commit->author_gmtoff;
548 const char *
549 got_object_commit_get_committer(struct got_commit_object *commit)
551 return commit->committer;
554 time_t
555 got_object_commit_get_committer_time(struct got_commit_object *commit)
557 return commit->committer_time;
560 time_t
561 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
563 return commit->committer_gmtoff;
566 const struct got_error *
567 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
569 const struct got_error *err = NULL;
570 const char *src;
571 char *dst;
572 size_t len;
574 len = strlen(commit->logmsg);
575 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
576 if (*logmsg == NULL)
577 return got_error_from_errno("malloc");
579 /*
580 * Strip out unusual headers. Headers are separated from the commit
581 * message body by a single empty line.
582 */
583 src = commit->logmsg;
584 dst = *logmsg;
585 while (*src != '\0' && *src != '\n') {
586 int copy_header = 1, eol = 0;
587 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
588 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
589 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
590 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
591 strncmp(src, GOT_COMMIT_LABEL_PARENT,
592 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
593 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
594 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
595 copy_header = 0;
597 while (*src != '\0' && !eol) {
598 if (copy_header) {
599 *dst = *src;
600 dst++;
602 if (*src == '\n')
603 eol = 1;
604 src++;
607 *dst = '\0';
609 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
610 err = got_error(GOT_ERR_NO_SPACE);
611 goto done;
614 /* Trim redundant trailing whitespace. */
615 len = strlen(*logmsg);
616 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
617 isspace((unsigned char)(*logmsg)[len - 1])) {
618 (*logmsg)[len - 1] = '\0';
619 len--;
622 /* Append a trailing newline if missing. */
623 if (len > 0 && (*logmsg)[len - 1] != '\n') {
624 (*logmsg)[len] = '\n';
625 (*logmsg)[len + 1] = '\0';
627 done:
628 if (err) {
629 free(*logmsg);
630 *logmsg = NULL;
632 return err;
635 const char *
636 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
638 return commit->logmsg;
641 const struct got_error *
642 got_object_parse_commit(struct got_commit_object **commit, char *buf,
643 size_t len, int algo)
645 const struct got_error *err = NULL;
646 char *s = buf;
647 size_t label_len;
648 size_t digest_strlen;
649 ssize_t remain = (ssize_t)len;
651 if (algo == GOT_HASH_SHA256)
652 digest_strlen = SHA256_DIGEST_STRING_LENGTH;
653 else
654 digest_strlen = SHA1_DIGEST_STRING_LENGTH;
656 if (remain == 0)
657 return got_error(GOT_ERR_BAD_OBJ_DATA);
659 *commit = got_object_commit_alloc_partial();
660 if (*commit == NULL)
661 return got_error_from_errno("got_object_commit_alloc_partial");
663 label_len = strlen(GOT_COMMIT_LABEL_TREE);
664 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
665 remain -= label_len;
666 if (remain < digest_strlen) {
667 err = got_error(GOT_ERR_BAD_OBJ_DATA);
668 goto done;
670 s += label_len;
671 if (!got_parse_hash_digest((*commit)->tree_id->hash, s,
672 algo, NULL)) {
673 err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 goto done;
676 (*commit)->tree_id->algo = algo;
677 remain -= digest_strlen;
678 s += digest_strlen;
679 } else {
680 err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 goto done;
684 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
685 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
686 remain -= label_len;
687 if (remain < digest_strlen) {
688 err = got_error(GOT_ERR_BAD_OBJ_DATA);
689 goto done;
691 s += label_len;
692 err = got_object_commit_add_parent(*commit, s, algo);
693 if (err)
694 goto done;
696 remain -= digest_strlen;
697 s += digest_strlen;
700 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
701 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
702 char *p;
703 size_t slen;
705 remain -= label_len;
706 if (remain <= 0) {
707 err = got_error(GOT_ERR_BAD_OBJ_DATA);
708 goto done;
710 s += label_len;
711 p = memchr(s, '\n', remain);
712 if (p == NULL) {
713 err = got_error(GOT_ERR_BAD_OBJ_DATA);
714 goto done;
716 *p = '\0';
717 slen = strlen(s);
718 err = parse_commit_time(&(*commit)->author_time,
719 &(*commit)->author_gmtoff, s);
720 if (err)
721 goto done;
722 (*commit)->author = strdup(s);
723 if ((*commit)->author == NULL) {
724 err = got_error_from_errno("strdup");
725 goto done;
727 s += slen + 1;
728 remain -= slen + 1;
731 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
732 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
733 char *p;
734 size_t slen;
736 remain -= label_len;
737 if (remain <= 0) {
738 err = got_error(GOT_ERR_BAD_OBJ_DATA);
739 goto done;
741 s += label_len;
742 p = memchr(s, '\n', remain);
743 if (p == NULL) {
744 err = got_error(GOT_ERR_BAD_OBJ_DATA);
745 goto done;
747 *p = '\0';
748 slen = strlen(s);
749 err = parse_commit_time(&(*commit)->committer_time,
750 &(*commit)->committer_gmtoff, s);
751 if (err)
752 goto done;
753 (*commit)->committer = strdup(s);
754 if ((*commit)->committer == NULL) {
755 err = got_error_from_errno("strdup");
756 goto done;
758 s += slen + 1;
759 remain -= slen + 1;
762 (*commit)->logmsg = strndup(s, remain);
763 if ((*commit)->logmsg == NULL) {
764 err = got_error_from_errno("strndup");
765 goto done;
767 done:
768 if (err) {
769 got_object_commit_close(*commit);
770 *commit = NULL;
772 return err;
775 const struct got_error *
776 got_object_read_commit(struct got_commit_object **commit, int fd,
777 struct got_object_id *expected_id, size_t expected_size)
779 struct got_object *obj = NULL;
780 const struct got_error *err = NULL;
781 size_t len;
782 uint8_t *p;
783 struct got_inflate_checksum csum;
784 SHA1_CTX sha1_ctx;
785 SHA2_CTX sha256_ctx;
786 struct got_object_id id;
788 memset(&csum, 0, sizeof(csum));
789 id.algo = expected_id->algo;
790 if (expected_id->algo == GOT_HASH_SHA256) {
791 SHA256Init(&sha256_ctx);
792 csum.output_sha256 = &sha256_ctx;
793 } else {
794 SHA1Init(&sha1_ctx);
795 csum.output_sha1 = &sha1_ctx;
798 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
799 if (err)
800 return err;
802 if (expected_id->algo == GOT_HASH_SHA256)
803 SHA256Final(id.hash, &sha256_ctx);
804 else
805 SHA1Final(id.hash, &sha1_ctx);
807 if (got_object_id_cmp(expected_id, &id) != 0) {
808 err = got_error_checksum(expected_id);
809 goto done;
812 err = got_object_parse_header(&obj, p, len);
813 if (err)
814 goto done;
816 if (len < obj->hdrlen + obj->size) {
817 err = got_error(GOT_ERR_BAD_OBJ_DATA);
818 goto done;
821 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
822 err = got_error(GOT_ERR_OBJ_TYPE);
823 goto done;
826 /* Skip object header. */
827 len -= obj->hdrlen;
828 err = got_object_parse_commit(commit, p + obj->hdrlen, len,
829 expected_id->algo);
830 done:
831 free(p);
832 if (obj)
833 got_object_close(obj);
834 return err;
837 void
838 got_object_tree_close(struct got_tree_object *tree)
840 if (tree->refcnt > 0) {
841 tree->refcnt--;
842 if (tree->refcnt > 0)
843 return;
846 free(tree->entries);
847 free(tree);
850 static const struct got_error *
851 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
852 size_t maxlen, size_t idlen)
854 char *p, *space;
856 *elen = 0;
858 *elen = strnlen(buf, maxlen) + 1;
859 if (*elen > maxlen)
860 return got_error(GOT_ERR_BAD_OBJ_DATA);
862 space = memchr(buf, ' ', *elen);
863 if (space == NULL || space <= buf)
864 return got_error(GOT_ERR_BAD_OBJ_DATA);
866 pte->mode = 0;
867 p = buf;
868 while (p < space) {
869 if (*p < '0' || *p > '7')
870 return got_error(GOT_ERR_BAD_OBJ_DATA);
871 pte->mode <<= 3;
872 pte->mode |= *p - '0';
873 p++;
876 if (*elen > maxlen || maxlen - *elen < idlen)
877 return got_error(GOT_ERR_BAD_OBJ_DATA);
879 pte->name = space + 1;
880 pte->namelen = strlen(pte->name);
881 buf += *elen;
882 pte->id = buf;
883 pte->idlen = idlen;
884 *elen += idlen;
885 return NULL;
888 static int
889 pte_cmp(const void *pa, const void *pb)
891 const struct got_parsed_tree_entry *a = pa, *b = pb;
893 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
896 const struct got_error *
897 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
898 size_t *nentries_alloc, uint8_t *buf, size_t len, int algo)
900 const struct got_error *err = NULL;
901 size_t remain = len;
902 const size_t nalloc = 16;
903 struct got_parsed_tree_entry *pte;
904 size_t idlen = SHA256_DIGEST_LENGTH;
905 int i;
907 if (algo != GOT_HASH_SHA256)
908 idlen = SHA1_DIGEST_LENGTH;
910 *nentries = 0;
911 if (remain == 0)
912 return NULL; /* tree is empty */
914 while (remain > 0) {
915 size_t elen;
917 if (*nentries >= *nentries_alloc) {
918 pte = recallocarray(*entries, *nentries_alloc,
919 *nentries_alloc + nalloc, sizeof(**entries));
920 if (pte == NULL) {
921 err = got_error_from_errno("recallocarray");
922 goto done;
924 *entries = pte;
925 *nentries_alloc += nalloc;
928 pte = &(*entries)[*nentries];
929 err = parse_tree_entry(pte, &elen, buf, remain, idlen);
930 if (err)
931 goto done;
932 buf += elen;
933 remain -= elen;
934 (*nentries)++;
937 if (remain != 0) {
938 err = got_error(GOT_ERR_BAD_OBJ_DATA);
939 goto done;
942 if (*nentries > 1) {
943 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
945 for (i = 0; i < *nentries - 1; i++) {
946 struct got_parsed_tree_entry *prev = &(*entries)[i];
947 pte = &(*entries)[i + 1];
948 if (got_path_cmp(prev->name, pte->name,
949 prev->namelen, pte->namelen) == 0) {
950 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
951 break;
955 done:
956 if (err)
957 *nentries = 0;
958 return err;
961 const struct got_error *
962 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
963 size_t *nentries_alloc, uint8_t **p, int fd,
964 struct got_object_id *expected_id)
966 const struct got_error *err = NULL;
967 struct got_object *obj = NULL;
968 size_t len;
969 struct got_inflate_checksum csum;
970 SHA2_CTX sha256_ctx;
971 SHA1_CTX sha1_ctx;
972 struct got_object_id id;
974 memset(&csum, 0, sizeof(csum));
975 id.algo = expected_id->algo;
976 if (expected_id->algo == GOT_HASH_SHA256) {
977 SHA256Init(&sha256_ctx);
978 csum.output_sha256 = &sha256_ctx;
979 } else {
980 SHA1Init(&sha1_ctx);
981 csum.output_sha1 = &sha1_ctx;
984 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
985 if (err)
986 return err;
988 if (expected_id->algo == GOT_HASH_SHA256)
989 SHA256Final(id.hash, &sha256_ctx);
990 else
991 SHA1Final(id.hash, &sha1_ctx);
992 if (got_object_id_cmp(expected_id, &id) != 0) {
993 err = got_error_checksum(expected_id);
994 goto done;
997 err = got_object_parse_header(&obj, *p, len);
998 if (err)
999 goto done;
1001 if (len < obj->hdrlen + obj->size) {
1002 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1003 goto done;
1006 /* Skip object header. */
1007 len -= obj->hdrlen;
1008 err = got_object_parse_tree(entries, nentries, nentries_alloc,
1009 *p + obj->hdrlen, len, expected_id->algo);
1010 done:
1011 if (obj)
1012 got_object_close(obj);
1013 return err;
1016 void
1017 got_object_tag_close(struct got_tag_object *tag)
1019 if (tag->refcnt > 0) {
1020 tag->refcnt--;
1021 if (tag->refcnt > 0)
1022 return;
1025 free(tag->tag);
1026 free(tag->tagger);
1027 free(tag->tagmsg);
1028 free(tag);
1031 const struct got_error *
1032 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len,
1033 int algo)
1035 const struct got_error *err = NULL;
1036 size_t remain = len;
1037 char *s = buf;
1038 size_t label_len, digest_strlen = SHA256_DIGEST_STRING_LENGTH;
1040 if (algo != GOT_HASH_SHA256)
1041 digest_strlen = SHA1_DIGEST_STRING_LENGTH;
1043 if (remain == 0)
1044 return got_error(GOT_ERR_BAD_OBJ_DATA);
1046 *tag = calloc(1, sizeof(**tag));
1047 if (*tag == NULL)
1048 return got_error_from_errno("calloc");
1050 label_len = strlen(GOT_TAG_LABEL_OBJECT);
1051 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
1052 remain -= label_len;
1053 if (remain < digest_strlen) {
1054 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1055 goto done;
1057 s += label_len;
1058 if (!got_parse_hash_digest((*tag)->id.hash, s, algo, NULL)) {
1059 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1060 goto done;
1062 (*tag)->id.algo = algo;
1063 remain -= digest_strlen;
1064 s += digest_strlen;
1065 } else {
1066 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1067 goto done;
1070 if (remain <= 0) {
1071 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1072 goto done;
1075 label_len = strlen(GOT_TAG_LABEL_TYPE);
1076 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1077 remain -= label_len;
1078 if (remain <= 0) {
1079 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1080 goto done;
1082 s += label_len;
1083 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1084 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1085 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1086 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1087 s += label_len;
1088 remain -= label_len;
1089 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1090 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1091 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1092 label_len = strlen(GOT_OBJ_LABEL_TREE);
1093 s += label_len;
1094 remain -= label_len;
1095 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1096 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1097 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1098 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1099 s += label_len;
1100 remain -= label_len;
1101 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1102 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1103 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1104 label_len = strlen(GOT_OBJ_LABEL_TAG);
1105 s += label_len;
1106 remain -= label_len;
1107 } else {
1108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1109 goto done;
1112 if (remain <= 0 || *s != '\n') {
1113 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1114 goto done;
1116 s++;
1117 remain--;
1118 if (remain <= 0) {
1119 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1120 goto done;
1122 } else {
1123 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1124 goto done;
1127 label_len = strlen(GOT_TAG_LABEL_TAG);
1128 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1129 char *p;
1130 size_t slen;
1131 remain -= label_len;
1132 if (remain <= 0) {
1133 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1134 goto done;
1136 s += label_len;
1137 p = memchr(s, '\n', remain);
1138 if (p == NULL) {
1139 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1140 goto done;
1142 *p = '\0';
1143 slen = strlen(s);
1144 (*tag)->tag = strndup(s, slen);
1145 if ((*tag)->tag == NULL) {
1146 err = got_error_from_errno("strndup");
1147 goto done;
1149 s += slen + 1;
1150 remain -= slen + 1;
1151 if (remain <= 0) {
1152 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1153 goto done;
1155 } else {
1156 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1157 goto done;
1160 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1161 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1162 char *p;
1163 size_t slen;
1165 remain -= label_len;
1166 if (remain <= 0) {
1167 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1168 goto done;
1170 s += label_len;
1171 p = memchr(s, '\n', remain);
1172 if (p == NULL) {
1173 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1174 goto done;
1176 *p = '\0';
1177 slen = strlen(s);
1178 err = parse_commit_time(&(*tag)->tagger_time,
1179 &(*tag)->tagger_gmtoff, s);
1180 if (err)
1181 goto done;
1182 (*tag)->tagger = strdup(s);
1183 if ((*tag)->tagger == NULL) {
1184 err = got_error_from_errno("strdup");
1185 goto done;
1187 s += slen + 1;
1188 remain -= slen + 1;
1189 if (remain < 0) {
1190 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1191 goto done;
1193 } else {
1194 /* Some old tags in the Linux git repo have no tagger. */
1195 (*tag)->tagger = strdup("");
1196 if ((*tag)->tagger == NULL) {
1197 err = got_error_from_errno("strdup");
1198 goto done;
1202 (*tag)->tagmsg = strndup(s, remain);
1203 if ((*tag)->tagmsg == NULL) {
1204 err = got_error_from_errno("strndup");
1205 goto done;
1207 done:
1208 if (err) {
1209 got_object_tag_close(*tag);
1210 *tag = NULL;
1212 return err;
1215 const struct got_error *
1216 got_object_read_tag(struct got_tag_object **tag, int fd,
1217 struct got_object_id *expected_id, size_t expected_size)
1219 const struct got_error *err = NULL;
1220 struct got_object *obj = NULL;
1221 size_t len;
1222 uint8_t *p;
1223 struct got_inflate_checksum csum;
1224 SHA2_CTX sha256_ctx;
1225 SHA1_CTX sha1_ctx;
1226 struct got_object_id id;
1228 memset(&csum, 0, sizeof(csum));
1229 id.algo = expected_id->algo;
1230 if (expected_id->algo == GOT_HASH_SHA256) {
1231 SHA256Init(&sha256_ctx);
1232 csum.output_sha256 = &sha256_ctx;
1233 } else {
1234 SHA1Init(&sha1_ctx);
1235 csum.output_sha1 = &sha1_ctx;
1238 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1239 expected_size, fd);
1240 if (err)
1241 return err;
1243 if (expected_id->algo == GOT_HASH_SHA256)
1244 SHA256Final(id.hash, &sha256_ctx);
1245 else
1246 SHA1Final(id.hash, &sha1_ctx);
1247 if (got_object_id_cmp(expected_id, &id) != 0) {
1248 err = got_error_checksum(expected_id);
1249 goto done;
1252 err = got_object_parse_header(&obj, p, len);
1253 if (err)
1254 goto done;
1256 if (len < obj->hdrlen + obj->size) {
1257 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1258 goto done;
1261 /* Skip object header. */
1262 len -= obj->hdrlen;
1263 err = got_object_parse_tag(tag, p + obj->hdrlen, len, expected_id->algo);
1264 done:
1265 free(p);
1266 if (obj)
1267 got_object_close(obj);
1268 return err;
1271 const struct got_error *
1272 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1274 const struct got_error *err = NULL;
1275 static const size_t blocksize = 512;
1276 size_t n, total, remain;
1277 uint8_t *buf;
1279 *outbuf = NULL;
1280 *outlen = 0;
1282 buf = malloc(blocksize);
1283 if (buf == NULL)
1284 return got_error_from_errno("malloc");
1286 remain = blocksize;
1287 total = 0;
1288 for (;;) {
1289 if (remain == 0) {
1290 uint8_t *newbuf;
1291 newbuf = reallocarray(buf, 1, total + blocksize);
1292 if (newbuf == NULL) {
1293 err = got_error_from_errno("reallocarray");
1294 goto done;
1296 buf = newbuf;
1297 remain += blocksize;
1299 n = fread(buf + total, 1, remain, f);
1300 if (n == 0) {
1301 if (ferror(f)) {
1302 err = got_ferror(f, GOT_ERR_IO);
1303 goto done;
1305 break; /* EOF */
1307 remain -= n;
1308 total += n;
1311 done:
1312 if (err == NULL) {
1313 *outbuf = buf;
1314 *outlen = total;
1315 } else
1316 free(buf);
1317 return err;