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 <zlib.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <imsg.h>
36 #include <time.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
43 #include "got_path.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"
54 #ifndef nitems
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #endif
58 struct got_object_id *
59 got_object_id_dup(struct got_object_id *id1)
60 {
61 struct got_object_id *id2;
63 id2 = malloc(sizeof(*id2));
64 if (id2 == NULL)
65 return NULL;
66 memcpy(id2, id1, sizeof(*id2));
67 return id2;
68 }
70 int
71 got_object_id_cmp(const struct got_object_id *id1,
72 const struct got_object_id *id2)
73 {
74 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
75 }
77 const struct got_error *
78 got_object_qid_alloc_partial(struct got_object_qid **qid)
79 {
80 *qid = malloc(sizeof(**qid));
81 if (*qid == NULL)
82 return got_error_from_errno("malloc");
84 (*qid)->data = NULL;
85 return NULL;
86 }
88 const struct got_error *
89 got_object_id_str(char **outbuf, struct got_object_id *id)
90 {
91 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
93 *outbuf = malloc(len);
94 if (*outbuf == NULL)
95 return got_error_from_errno("malloc");
97 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
98 free(*outbuf);
99 *outbuf = NULL;
100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
103 return NULL;
106 void
107 got_object_close(struct got_object *obj)
109 if (obj->refcnt > 0) {
110 obj->refcnt--;
111 if (obj->refcnt > 0)
112 return;
115 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
116 struct got_delta *delta;
117 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
118 delta = STAILQ_FIRST(&obj->deltas.entries);
119 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
120 free(delta);
123 free(obj);
126 const struct got_error *
127 got_object_raw_close(struct got_raw_object *obj)
129 const struct got_error *err = NULL;
131 if (obj->refcnt > 0) {
132 obj->refcnt--;
133 if (obj->refcnt > 0)
134 return NULL;
137 if (obj->close_cb)
138 obj->close_cb(obj);
140 if (obj->f == NULL) {
141 if (obj->fd != -1) {
142 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
143 err = got_error_from_errno("munmap");
144 if (close(obj->fd) == -1 && err == NULL)
145 err = got_error_from_errno("close");
146 } else
147 free(obj->data);
148 } else {
149 if (fclose(obj->f) == EOF && err == NULL)
150 err = got_error_from_errno("fclose");
152 free(obj);
153 return err;
156 void
157 got_object_qid_free(struct got_object_qid *qid)
159 free(qid);
162 void
163 got_object_id_queue_free(struct got_object_id_queue *ids)
165 struct got_object_qid *qid;
167 while (!STAILQ_EMPTY(ids)) {
168 qid = STAILQ_FIRST(ids);
169 STAILQ_REMOVE_HEAD(ids, entry);
170 got_object_qid_free(qid);
174 const struct got_error *
175 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
177 const char *obj_labels[] = {
178 GOT_OBJ_LABEL_COMMIT,
179 GOT_OBJ_LABEL_TREE,
180 GOT_OBJ_LABEL_BLOB,
181 GOT_OBJ_LABEL_TAG,
182 };
183 const int obj_types[] = {
184 GOT_OBJ_TYPE_COMMIT,
185 GOT_OBJ_TYPE_TREE,
186 GOT_OBJ_TYPE_BLOB,
187 GOT_OBJ_TYPE_TAG,
188 };
189 int type = 0;
190 size_t size = 0;
191 size_t i;
192 char *end;
194 *obj = NULL;
196 end = memchr(buf, '\0', len);
197 if (end == NULL)
198 return got_error(GOT_ERR_BAD_OBJ_HDR);
200 for (i = 0; i < nitems(obj_labels); i++) {
201 const char *label = obj_labels[i];
202 size_t label_len = strlen(label);
203 const char *errstr;
205 if (len <= label_len || buf + label_len >= end ||
206 strncmp(buf, label, label_len) != 0)
207 continue;
209 type = obj_types[i];
210 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
211 if (errstr != NULL)
212 return got_error(GOT_ERR_BAD_OBJ_HDR);
213 break;
216 if (type == 0)
217 return got_error(GOT_ERR_BAD_OBJ_HDR);
219 *obj = calloc(1, sizeof(**obj));
220 if (*obj == NULL)
221 return got_error_from_errno("calloc");
222 (*obj)->type = type;
223 (*obj)->hdrlen = end - buf + 1;
224 (*obj)->size = size;
225 return NULL;
228 const struct got_error *
229 got_object_read_header(struct got_object **obj, int fd)
231 const struct got_error *err;
232 struct got_inflate_buf zb;
233 uint8_t *buf;
234 const size_t zbsize = 64;
235 size_t outlen, totlen;
236 int nbuf = 1;
238 *obj = NULL;
240 buf = malloc(zbsize);
241 if (buf == NULL)
242 return got_error_from_errno("malloc");
243 buf[0] = '\0';
245 err = got_inflate_init(&zb, buf, zbsize, NULL);
246 if (err)
247 return err;
249 totlen = 0;
250 do {
251 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
252 if (err)
253 goto done;
254 if (outlen == 0)
255 break;
256 totlen += outlen;
257 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
258 uint8_t *newbuf;
259 nbuf++;
260 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
261 if (newbuf == NULL) {
262 err = got_error_from_errno("recallocarray");
263 goto done;
265 buf = newbuf;
266 zb.outbuf = newbuf + totlen;
267 zb.outlen = (nbuf * zbsize) - totlen;
269 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
271 err = got_object_parse_header(obj, buf, totlen);
272 done:
273 free(buf);
274 got_inflate_end(&zb);
275 return err;
278 const struct got_error *
279 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
280 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
281 int infd)
283 const struct got_error *err = NULL;
284 struct got_object *obj;
285 struct got_inflate_checksum csum;
286 uint8_t sha1[SHA1_DIGEST_LENGTH];
287 SHA1_CTX sha1_ctx;
288 size_t len, consumed;
289 FILE *f = NULL;
291 *outbuf = NULL;
292 *size = 0;
293 *hdrlen = 0;
295 SHA1Init(&sha1_ctx);
296 memset(&csum, 0, sizeof(csum));
297 csum.output_sha1 = &sha1_ctx;
299 if (lseek(infd, SEEK_SET, 0) == -1)
300 return got_error_from_errno("lseek");
302 err = got_object_read_header(&obj, infd);
303 if (err)
304 return err;
306 if (lseek(infd, SEEK_SET, 0) == -1)
307 return got_error_from_errno("lseek");
309 if (obj->size + obj->hdrlen <= max_in_mem_size) {
310 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
311 obj->size + obj->hdrlen, infd);
312 } else {
313 int fd;
314 /*
315 * XXX This uses an extra file descriptor for no good reason.
316 * We should have got_inflate_fd_to_fd().
317 */
318 fd = dup(infd);
319 if (fd == -1)
320 return got_error_from_errno("dup");
321 f = fdopen(fd, "r");
322 if (f == NULL) {
323 err = got_error_from_errno("fdopen");
324 abort();
325 close(fd);
326 goto done;
328 err = got_inflate_to_fd(&len, f, &csum, outfd);
330 if (err)
331 goto done;
333 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
334 err = got_error(GOT_ERR_BAD_OBJ_HDR);
335 goto done;
338 SHA1Final(sha1, &sha1_ctx);
339 if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
340 char buf[SHA1_DIGEST_STRING_LENGTH];
341 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
342 "checksum failure for object %s",
343 got_sha1_digest_to_str(expected_id->sha1, buf,
344 sizeof(buf)));
345 goto done;
348 *size = obj->size;
349 *hdrlen = obj->hdrlen;
350 done:
351 got_object_close(obj);
352 if (f && fclose(f) == EOF && err == NULL)
353 err = got_error_from_errno("fclose");
354 return err;
357 struct got_commit_object *
358 got_object_commit_alloc_partial(void)
360 struct got_commit_object *commit;
362 commit = calloc(1, sizeof(*commit));
363 if (commit == NULL)
364 return NULL;
365 commit->tree_id = malloc(sizeof(*commit->tree_id));
366 if (commit->tree_id == NULL) {
367 free(commit);
368 return NULL;
371 STAILQ_INIT(&commit->parent_ids);
373 return commit;
376 const struct got_error *
377 got_object_commit_add_parent(struct got_commit_object *commit,
378 const char *id_str)
380 const struct got_error *err = NULL;
381 struct got_object_qid *qid;
383 err = got_object_qid_alloc_partial(&qid);
384 if (err)
385 return err;
387 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
388 err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 got_object_qid_free(qid);
390 return err;
393 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
394 commit->nparents++;
396 return NULL;
399 static const struct got_error *
400 parse_gmtoff(time_t *gmtoff, const char *tzstr)
402 int sign = 1;
403 const char *p = tzstr;
404 time_t h, m;
406 *gmtoff = 0;
408 if (*p == '-')
409 sign = -1;
410 else if (*p != '+')
411 return got_error(GOT_ERR_BAD_OBJ_DATA);
412 p++;
413 if (!isdigit(*p) && !isdigit(*(p + 1)))
414 return got_error(GOT_ERR_BAD_OBJ_DATA);
415 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
417 p += 2;
418 if (!isdigit(*p) && !isdigit(*(p + 1)))
419 return got_error(GOT_ERR_BAD_OBJ_DATA);
420 m = ((*p - '0') * 10) + (*(p + 1) - '0');
422 *gmtoff = (h * 60 * 60 + m * 60) * sign;
423 return NULL;
426 static const struct got_error *
427 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
429 const struct got_error *err = NULL;
430 const char *errstr;
431 char *space, *tzstr;
433 /* Parse and strip off trailing timezone indicator string. */
434 space = strrchr(committer, ' ');
435 if (space == NULL)
436 return got_error(GOT_ERR_BAD_OBJ_DATA);
437 tzstr = strdup(space + 1);
438 if (tzstr == NULL)
439 return got_error_from_errno("strdup");
440 err = parse_gmtoff(gmtoff, tzstr);
441 free(tzstr);
442 if (err) {
443 if (err->code != GOT_ERR_BAD_OBJ_DATA)
444 return err;
445 /* Old versions of Git omitted the timestamp. */
446 *time = 0;
447 *gmtoff = 0;
448 return NULL;
450 *space = '\0';
452 /* Timestamp is separated from committer name + email by space. */
453 space = strrchr(committer, ' ');
454 if (space == NULL)
455 return got_error(GOT_ERR_BAD_OBJ_DATA);
457 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
458 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
459 if (errstr)
460 return got_error(GOT_ERR_BAD_OBJ_DATA);
462 /* Strip off parsed time information, leaving just author and email. */
463 *space = '\0';
465 return NULL;
468 void
469 got_object_commit_close(struct got_commit_object *commit)
471 if (commit->refcnt > 0) {
472 commit->refcnt--;
473 if (commit->refcnt > 0)
474 return;
477 got_object_id_queue_free(&commit->parent_ids);
478 free(commit->tree_id);
479 free(commit->author);
480 free(commit->committer);
481 free(commit->logmsg);
482 free(commit);
485 struct got_object_id *
486 got_object_commit_get_tree_id(struct got_commit_object *commit)
488 return commit->tree_id;
491 int
492 got_object_commit_get_nparents(struct got_commit_object *commit)
494 return commit->nparents;
497 const struct got_object_id_queue *
498 got_object_commit_get_parent_ids(struct got_commit_object *commit)
500 return &commit->parent_ids;
503 const char *
504 got_object_commit_get_author(struct got_commit_object *commit)
506 return commit->author;
509 time_t
510 got_object_commit_get_author_time(struct got_commit_object *commit)
512 return commit->author_time;
515 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
517 return commit->author_gmtoff;
520 const char *
521 got_object_commit_get_committer(struct got_commit_object *commit)
523 return commit->committer;
526 time_t
527 got_object_commit_get_committer_time(struct got_commit_object *commit)
529 return commit->committer_time;
532 time_t
533 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
535 return commit->committer_gmtoff;
538 const struct got_error *
539 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
541 const struct got_error *err = NULL;
542 const char *src;
543 char *dst;
544 size_t len;
546 len = strlen(commit->logmsg);
547 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
548 if (*logmsg == NULL)
549 return got_error_from_errno("malloc");
551 /*
552 * Strip out unusual headers. Headers are separated from the commit
553 * message body by a single empty line.
554 */
555 src = commit->logmsg;
556 dst = *logmsg;
557 while (*src != '\0' && *src != '\n') {
558 int copy_header = 1, eol = 0;
559 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
560 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
561 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
562 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
563 strncmp(src, GOT_COMMIT_LABEL_PARENT,
564 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
565 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
566 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
567 copy_header = 0;
569 while (*src != '\0' && !eol) {
570 if (copy_header) {
571 *dst = *src;
572 dst++;
574 if (*src == '\n')
575 eol = 1;
576 src++;
579 *dst = '\0';
581 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
582 err = got_error(GOT_ERR_NO_SPACE);
583 goto done;
586 /* Trim redundant trailing whitespace. */
587 len = strlen(*logmsg);
588 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
589 isspace((unsigned char)(*logmsg)[len - 1])) {
590 (*logmsg)[len - 1] = '\0';
591 len--;
594 /* Append a trailing newline if missing. */
595 if (len > 0 && (*logmsg)[len - 1] != '\n') {
596 (*logmsg)[len] = '\n';
597 (*logmsg)[len + 1] = '\0';
599 done:
600 if (err) {
601 free(*logmsg);
602 *logmsg = NULL;
604 return err;
607 const char *
608 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
610 return commit->logmsg;
613 const struct got_error *
614 got_object_parse_commit(struct got_commit_object **commit, char *buf,
615 size_t len)
617 const struct got_error *err = NULL;
618 char *s = buf;
619 size_t label_len;
620 ssize_t remain = (ssize_t)len;
622 if (remain == 0)
623 return got_error(GOT_ERR_BAD_OBJ_DATA);
625 *commit = got_object_commit_alloc_partial();
626 if (*commit == NULL)
627 return got_error_from_errno("got_object_commit_alloc_partial");
629 label_len = strlen(GOT_COMMIT_LABEL_TREE);
630 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
631 remain -= label_len;
632 if (remain < SHA1_DIGEST_STRING_LENGTH) {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 goto done;
636 s += label_len;
637 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
639 goto done;
641 remain -= SHA1_DIGEST_STRING_LENGTH;
642 s += SHA1_DIGEST_STRING_LENGTH;
643 } else {
644 err = got_error(GOT_ERR_BAD_OBJ_DATA);
645 goto done;
648 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
649 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
650 remain -= label_len;
651 if (remain < SHA1_DIGEST_STRING_LENGTH) {
652 err = got_error(GOT_ERR_BAD_OBJ_DATA);
653 goto done;
655 s += label_len;
656 err = got_object_commit_add_parent(*commit, s);
657 if (err)
658 goto done;
660 remain -= SHA1_DIGEST_STRING_LENGTH;
661 s += SHA1_DIGEST_STRING_LENGTH;
664 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
665 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
666 char *p;
667 size_t slen;
669 remain -= label_len;
670 if (remain <= 0) {
671 err = got_error(GOT_ERR_BAD_OBJ_DATA);
672 goto done;
674 s += label_len;
675 p = memchr(s, '\n', remain);
676 if (p == NULL) {
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 goto done;
680 *p = '\0';
681 slen = strlen(s);
682 err = parse_commit_time(&(*commit)->author_time,
683 &(*commit)->author_gmtoff, s);
684 if (err)
685 goto done;
686 (*commit)->author = strdup(s);
687 if ((*commit)->author == NULL) {
688 err = got_error_from_errno("strdup");
689 goto done;
691 s += slen + 1;
692 remain -= slen + 1;
695 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
696 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
697 char *p;
698 size_t slen;
700 remain -= label_len;
701 if (remain <= 0) {
702 err = got_error(GOT_ERR_BAD_OBJ_DATA);
703 goto done;
705 s += label_len;
706 p = memchr(s, '\n', remain);
707 if (p == NULL) {
708 err = got_error(GOT_ERR_BAD_OBJ_DATA);
709 goto done;
711 *p = '\0';
712 slen = strlen(s);
713 err = parse_commit_time(&(*commit)->committer_time,
714 &(*commit)->committer_gmtoff, s);
715 if (err)
716 goto done;
717 (*commit)->committer = strdup(s);
718 if ((*commit)->committer == NULL) {
719 err = got_error_from_errno("strdup");
720 goto done;
722 s += slen + 1;
723 remain -= slen + 1;
726 (*commit)->logmsg = strndup(s, remain);
727 if ((*commit)->logmsg == NULL) {
728 err = got_error_from_errno("strndup");
729 goto done;
731 done:
732 if (err) {
733 got_object_commit_close(*commit);
734 *commit = NULL;
736 return err;
739 const struct got_error *
740 got_object_read_commit(struct got_commit_object **commit, int fd,
741 struct got_object_id *expected_id, size_t expected_size)
743 struct got_object *obj = NULL;
744 const struct got_error *err = NULL;
745 size_t len;
746 uint8_t *p;
747 struct got_inflate_checksum csum;
748 SHA1_CTX sha1_ctx;
749 struct got_object_id id;
751 SHA1Init(&sha1_ctx);
752 memset(&csum, 0, sizeof(csum));
753 csum.output_sha1 = &sha1_ctx;
755 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
756 if (err)
757 return err;
759 SHA1Final(id.sha1, &sha1_ctx);
760 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
761 char buf[SHA1_DIGEST_STRING_LENGTH];
762 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
763 "checksum failure for object %s",
764 got_sha1_digest_to_str(expected_id->sha1, buf,
765 sizeof(buf)));
766 goto done;
769 err = got_object_parse_header(&obj, p, len);
770 if (err)
771 goto done;
773 if (len < obj->hdrlen + obj->size) {
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
775 goto done;
778 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
779 err = got_error(GOT_ERR_OBJ_TYPE);
780 goto done;
783 /* Skip object header. */
784 len -= obj->hdrlen;
785 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
786 done:
787 free(p);
788 if (obj)
789 got_object_close(obj);
790 return err;
793 void
794 got_object_tree_close(struct got_tree_object *tree)
796 if (tree->refcnt > 0) {
797 tree->refcnt--;
798 if (tree->refcnt > 0)
799 return;
802 free(tree->entries);
803 free(tree);
806 static const struct got_error *
807 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
808 size_t maxlen)
810 char *p, *space;
812 *elen = 0;
814 *elen = strnlen(buf, maxlen) + 1;
815 if (*elen > maxlen)
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);
822 pte->mode = 0;
823 p = buf;
824 while (p < space) {
825 if (*p < '0' && *p > '7')
826 return got_error(GOT_ERR_BAD_OBJ_DATA);
827 pte->mode <<= 3;
828 pte->mode |= *p - '0';
829 p++;
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);
837 buf += *elen;
838 pte->id = buf;
839 *elen += SHA1_DIGEST_LENGTH;
840 return NULL;
843 static int
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;
856 size_t remain = len;
857 const size_t nalloc = 16;
858 struct got_parsed_tree_entry *pte;
859 int i;
861 *nentries = 0;
862 if (remain == 0)
863 return NULL; /* tree is empty */
865 while (remain > 0) {
866 size_t elen;
868 if (*nentries >= *nentries_alloc) {
869 pte = recallocarray(*entries, *nentries_alloc,
870 *nentries_alloc + nalloc, sizeof(**entries));
871 if (pte == NULL) {
872 err = got_error_from_errno("recallocarray");
873 goto done;
875 *entries = pte;
876 *nentries_alloc += nalloc;
879 pte = &(*entries)[*nentries];
880 err = parse_tree_entry(pte, &elen, buf, remain);
881 if (err)
882 goto done;
883 buf += elen;
884 remain -= elen;
885 (*nentries)++;
888 if (remain != 0) {
889 err = got_error(GOT_ERR_BAD_OBJ_DATA);
890 goto done;
893 if (*nentries > 1) {
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);
902 break;
906 done:
907 if (err)
908 *nentries = 0;
909 return err;
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;
919 size_t len;
920 struct got_inflate_checksum csum;
921 SHA1_CTX sha1_ctx;
922 struct got_object_id id;
924 SHA1Init(&sha1_ctx);
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);
929 if (err)
930 return err;
932 SHA1Final(id.sha1, &sha1_ctx);
933 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
934 char buf[SHA1_DIGEST_STRING_LENGTH];
935 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
936 "checksum failure for object %s",
937 got_sha1_digest_to_str(expected_id->sha1, buf,
938 sizeof(buf)));
939 goto done;
942 err = got_object_parse_header(&obj, *p, len);
943 if (err)
944 goto done;
946 if (len < obj->hdrlen + obj->size) {
947 err = got_error(GOT_ERR_BAD_OBJ_DATA);
948 goto done;
951 /* Skip object header. */
952 len -= obj->hdrlen;
953 err = got_object_parse_tree(entries, nentries, nentries_alloc,
954 *p + obj->hdrlen, len);
955 done:
956 if (obj)
957 got_object_close(obj);
958 return err;
961 void
962 got_object_tag_close(struct got_tag_object *tag)
964 if (tag->refcnt > 0) {
965 tag->refcnt--;
966 if (tag->refcnt > 0)
967 return;
970 free(tag->tag);
971 free(tag->tagger);
972 free(tag->tagmsg);
973 free(tag);
976 const struct got_error *
977 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
979 const struct got_error *err = NULL;
980 size_t remain = len;
981 char *s = buf;
982 size_t label_len;
984 if (remain == 0)
985 return got_error(GOT_ERR_BAD_OBJ_DATA);
987 *tag = calloc(1, sizeof(**tag));
988 if (*tag == NULL)
989 return got_error_from_errno("calloc");
991 label_len = strlen(GOT_TAG_LABEL_OBJECT);
992 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
993 remain -= label_len;
994 if (remain < SHA1_DIGEST_STRING_LENGTH) {
995 err = got_error(GOT_ERR_BAD_OBJ_DATA);
996 goto done;
998 s += label_len;
999 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
1000 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1001 goto done;
1003 remain -= SHA1_DIGEST_STRING_LENGTH;
1004 s += SHA1_DIGEST_STRING_LENGTH;
1005 } else {
1006 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1007 goto done;
1010 if (remain <= 0) {
1011 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1012 goto done;
1015 label_len = strlen(GOT_TAG_LABEL_TYPE);
1016 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1017 remain -= label_len;
1018 if (remain <= 0) {
1019 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1020 goto done;
1022 s += label_len;
1023 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1024 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1025 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1026 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1027 s += label_len;
1028 remain -= label_len;
1029 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1030 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1031 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1032 label_len = strlen(GOT_OBJ_LABEL_TREE);
1033 s += label_len;
1034 remain -= label_len;
1035 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1036 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1037 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1038 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1039 s += label_len;
1040 remain -= label_len;
1041 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1042 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1043 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1044 label_len = strlen(GOT_OBJ_LABEL_TAG);
1045 s += label_len;
1046 remain -= label_len;
1047 } else {
1048 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1049 goto done;
1052 if (remain <= 0 || *s != '\n') {
1053 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1054 goto done;
1056 s++;
1057 remain--;
1058 if (remain <= 0) {
1059 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1060 goto done;
1062 } else {
1063 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1064 goto done;
1067 label_len = strlen(GOT_TAG_LABEL_TAG);
1068 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1069 char *p;
1070 size_t slen;
1071 remain -= label_len;
1072 if (remain <= 0) {
1073 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1074 goto done;
1076 s += label_len;
1077 p = memchr(s, '\n', remain);
1078 if (p == NULL) {
1079 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1080 goto done;
1082 *p = '\0';
1083 slen = strlen(s);
1084 (*tag)->tag = strndup(s, slen);
1085 if ((*tag)->tag == NULL) {
1086 err = got_error_from_errno("strndup");
1087 goto done;
1089 s += slen + 1;
1090 remain -= slen + 1;
1091 if (remain <= 0) {
1092 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1093 goto done;
1095 } else {
1096 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1097 goto done;
1100 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1101 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1102 char *p;
1103 size_t slen;
1105 remain -= label_len;
1106 if (remain <= 0) {
1107 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1108 goto done;
1110 s += label_len;
1111 p = memchr(s, '\n', remain);
1112 if (p == NULL) {
1113 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1114 goto done;
1116 *p = '\0';
1117 slen = strlen(s);
1118 err = parse_commit_time(&(*tag)->tagger_time,
1119 &(*tag)->tagger_gmtoff, s);
1120 if (err)
1121 goto done;
1122 (*tag)->tagger = strdup(s);
1123 if ((*tag)->tagger == NULL) {
1124 err = got_error_from_errno("strdup");
1125 goto done;
1127 s += slen + 1;
1128 remain -= slen + 1;
1129 if (remain < 0) {
1130 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1131 goto done;
1133 } else {
1134 /* Some old tags in the Linux git repo have no tagger. */
1135 (*tag)->tagger = strdup("");
1136 if ((*tag)->tagger == NULL) {
1137 err = got_error_from_errno("strdup");
1138 goto done;
1142 (*tag)->tagmsg = strndup(s, remain);
1143 if ((*tag)->tagmsg == NULL) {
1144 err = got_error_from_errno("strndup");
1145 goto done;
1147 done:
1148 if (err) {
1149 got_object_tag_close(*tag);
1150 *tag = NULL;
1152 return err;
1155 const struct got_error *
1156 got_object_read_tag(struct got_tag_object **tag, int fd,
1157 struct got_object_id *expected_id, size_t expected_size)
1159 const struct got_error *err = NULL;
1160 struct got_object *obj = NULL;
1161 size_t len;
1162 uint8_t *p;
1163 struct got_inflate_checksum csum;
1164 SHA1_CTX sha1_ctx;
1165 struct got_object_id id;
1167 SHA1Init(&sha1_ctx);
1168 memset(&csum, 0, sizeof(csum));
1169 csum.output_sha1 = &sha1_ctx;
1171 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1172 expected_size, fd);
1173 if (err)
1174 return err;
1176 SHA1Final(id.sha1, &sha1_ctx);
1177 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
1178 char buf[SHA1_DIGEST_STRING_LENGTH];
1179 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1180 "checksum failure for object %s",
1181 got_sha1_digest_to_str(expected_id->sha1, buf,
1182 sizeof(buf)));
1183 goto done;
1186 err = got_object_parse_header(&obj, p, len);
1187 if (err)
1188 goto done;
1190 if (len < obj->hdrlen + obj->size) {
1191 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1192 goto done;
1195 /* Skip object header. */
1196 len -= obj->hdrlen;
1197 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1198 done:
1199 free(p);
1200 if (obj)
1201 got_object_close(obj);
1202 return err;
1205 const struct got_error *
1206 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1208 const struct got_error *err = NULL;
1209 static const size_t blocksize = 512;
1210 size_t n, total, remain;
1211 uint8_t *buf;
1213 *outbuf = NULL;
1214 *outlen = 0;
1216 buf = malloc(blocksize);
1217 if (buf == NULL)
1218 return got_error_from_errno("malloc");
1220 remain = blocksize;
1221 total = 0;
1222 for (;;) {
1223 if (remain == 0) {
1224 uint8_t *newbuf;
1225 newbuf = reallocarray(buf, 1, total + blocksize);
1226 if (newbuf == NULL) {
1227 err = got_error_from_errno("reallocarray");
1228 goto done;
1230 buf = newbuf;
1231 remain += blocksize;
1233 n = fread(buf + total, 1, remain, f);
1234 if (n == 0) {
1235 if (ferror(f)) {
1236 err = got_ferror(f, GOT_ERR_IO);
1237 goto done;
1239 break; /* EOF */
1241 remain -= n;
1242 total += n;
1245 done:
1246 if (err == NULL) {
1247 *outbuf = buf;
1248 *outlen = total;
1249 } else
1250 free(buf);
1251 return err;