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/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/mman.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <zlib.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <time.h>
34 #include <unistd.h>
36 #include "got_compat.h"
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
42 #include "got_path.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_repository.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 struct got_object_id *
58 got_object_id_dup(struct got_object_id *id1)
59 {
60 struct got_object_id *id2;
62 id2 = malloc(sizeof(*id2));
63 if (id2 == NULL)
64 return NULL;
65 memcpy(id2, id1, sizeof(*id2));
66 return id2;
67 }
69 int
70 got_object_id_cmp(const struct got_object_id *id1,
71 const struct got_object_id *id2)
72 {
73 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
74 }
76 const struct got_error *
77 got_object_qid_alloc_partial(struct got_object_qid **qid)
78 {
79 *qid = malloc(sizeof(**qid));
80 if (*qid == NULL)
81 return got_error_from_errno("malloc");
83 (*qid)->data = NULL;
84 return NULL;
85 }
87 const struct got_error *
88 got_object_id_str(char **outbuf, struct got_object_id *id)
89 {
90 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
92 *outbuf = malloc(len);
93 if (*outbuf == NULL)
94 return got_error_from_errno("malloc");
96 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
97 free(*outbuf);
98 *outbuf = NULL;
99 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
102 return NULL;
105 void
106 got_object_close(struct got_object *obj)
108 if (obj->refcnt > 0) {
109 obj->refcnt--;
110 if (obj->refcnt > 0)
111 return;
114 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
115 struct got_delta *delta;
116 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
117 delta = STAILQ_FIRST(&obj->deltas.entries);
118 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
119 free(delta);
122 free(obj);
125 const struct got_error *
126 got_object_raw_close(struct got_raw_object *obj)
128 const struct got_error *err = NULL;
130 if (obj->refcnt > 0) {
131 obj->refcnt--;
132 if (obj->refcnt > 0)
133 return NULL;
136 if (obj->close_cb)
137 obj->close_cb(obj);
139 if (obj->f == NULL) {
140 if (obj->fd != -1) {
141 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
142 err = got_error_from_errno("munmap");
143 if (close(obj->fd) == -1 && err == NULL)
144 err = got_error_from_errno("close");
145 } else
146 free(obj->data);
147 } else {
148 if (fclose(obj->f) == EOF && err == NULL)
149 err = got_error_from_errno("fclose");
151 free(obj);
152 return err;
155 void
156 got_object_qid_free(struct got_object_qid *qid)
158 free(qid);
161 void
162 got_object_id_queue_free(struct got_object_id_queue *ids)
164 struct got_object_qid *qid;
166 while (!STAILQ_EMPTY(ids)) {
167 qid = STAILQ_FIRST(ids);
168 STAILQ_REMOVE_HEAD(ids, entry);
169 got_object_qid_free(qid);
173 const struct got_error *
174 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
176 const char *obj_labels[] = {
177 GOT_OBJ_LABEL_COMMIT,
178 GOT_OBJ_LABEL_TREE,
179 GOT_OBJ_LABEL_BLOB,
180 GOT_OBJ_LABEL_TAG,
181 };
182 const int obj_types[] = {
183 GOT_OBJ_TYPE_COMMIT,
184 GOT_OBJ_TYPE_TREE,
185 GOT_OBJ_TYPE_BLOB,
186 GOT_OBJ_TYPE_TAG,
187 };
188 int type = 0;
189 size_t size = 0;
190 size_t i;
191 char *end;
193 *obj = NULL;
195 end = memchr(buf, '\0', len);
196 if (end == NULL)
197 return got_error(GOT_ERR_BAD_OBJ_HDR);
199 for (i = 0; i < nitems(obj_labels); i++) {
200 const char *label = obj_labels[i];
201 size_t label_len = strlen(label);
202 const char *errstr;
204 if (len <= label_len || buf + label_len >= end ||
205 strncmp(buf, label, label_len) != 0)
206 continue;
208 type = obj_types[i];
209 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
210 if (errstr != NULL)
211 return got_error(GOT_ERR_BAD_OBJ_HDR);
212 break;
215 if (type == 0)
216 return got_error(GOT_ERR_BAD_OBJ_HDR);
218 *obj = calloc(1, sizeof(**obj));
219 if (*obj == NULL)
220 return got_error_from_errno("calloc");
221 (*obj)->type = type;
222 (*obj)->hdrlen = end - buf + 1;
223 (*obj)->size = size;
224 return NULL;
227 const struct got_error *
228 got_object_read_header(struct got_object **obj, int fd)
230 const struct got_error *err;
231 struct got_inflate_buf zb;
232 uint8_t *buf;
233 const size_t zbsize = 64;
234 size_t outlen, totlen;
235 int nbuf = 1;
237 *obj = NULL;
239 buf = malloc(zbsize);
240 if (buf == NULL)
241 return got_error_from_errno("malloc");
242 buf[0] = '\0';
244 err = got_inflate_init(&zb, buf, zbsize, NULL);
245 if (err)
246 return err;
248 totlen = 0;
249 do {
250 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
251 if (err)
252 goto done;
253 if (outlen == 0)
254 break;
255 totlen += outlen;
256 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
257 uint8_t *newbuf;
258 nbuf++;
259 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
260 if (newbuf == NULL) {
261 err = got_error_from_errno("recallocarray");
262 goto done;
264 buf = newbuf;
265 zb.outbuf = newbuf + totlen;
266 zb.outlen = (nbuf * zbsize) - totlen;
268 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
270 err = got_object_parse_header(obj, buf, totlen);
271 done:
272 free(buf);
273 got_inflate_end(&zb);
274 return err;
277 const struct got_error *
278 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
279 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
280 int infd)
282 const struct got_error *err = NULL;
283 struct got_object *obj;
284 struct got_inflate_checksum csum;
285 uint8_t sha1[SHA1_DIGEST_LENGTH];
286 SHA1_CTX sha1_ctx;
287 size_t len, consumed;
288 FILE *f = NULL;
290 *outbuf = NULL;
291 *size = 0;
292 *hdrlen = 0;
294 SHA1Init(&sha1_ctx);
295 memset(&csum, 0, sizeof(csum));
296 csum.output_sha1 = &sha1_ctx;
298 if (lseek(infd, SEEK_SET, 0) == -1)
299 return got_error_from_errno("lseek");
301 err = got_object_read_header(&obj, infd);
302 if (err)
303 return err;
305 if (lseek(infd, SEEK_SET, 0) == -1)
306 return got_error_from_errno("lseek");
308 if (obj->size + obj->hdrlen <= max_in_mem_size) {
309 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
310 obj->size + obj->hdrlen, infd);
311 } else {
312 int fd;
313 /*
314 * XXX This uses an extra file descriptor for no good reason.
315 * We should have got_inflate_fd_to_fd().
316 */
317 fd = dup(infd);
318 if (fd == -1)
319 return got_error_from_errno("dup");
320 f = fdopen(fd, "r");
321 if (f == NULL) {
322 err = got_error_from_errno("fdopen");
323 abort();
324 close(fd);
325 goto done;
327 err = got_inflate_to_fd(&len, f, &csum, outfd);
329 if (err)
330 goto done;
332 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
333 err = got_error(GOT_ERR_BAD_OBJ_HDR);
334 goto done;
337 SHA1Final(sha1, &sha1_ctx);
338 if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
339 char buf[SHA1_DIGEST_STRING_LENGTH];
340 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
341 "checksum failure for object %s",
342 got_sha1_digest_to_str(expected_id->sha1, buf,
343 sizeof(buf)));
344 goto done;
347 *size = obj->size;
348 *hdrlen = obj->hdrlen;
349 done:
350 got_object_close(obj);
351 if (f && fclose(f) == EOF && err == NULL)
352 err = got_error_from_errno("fclose");
353 return err;
356 struct got_commit_object *
357 got_object_commit_alloc_partial(void)
359 struct got_commit_object *commit;
361 commit = calloc(1, sizeof(*commit));
362 if (commit == NULL)
363 return NULL;
364 commit->tree_id = malloc(sizeof(*commit->tree_id));
365 if (commit->tree_id == NULL) {
366 free(commit);
367 return NULL;
370 STAILQ_INIT(&commit->parent_ids);
372 return commit;
375 const struct got_error *
376 got_object_commit_add_parent(struct got_commit_object *commit,
377 const char *id_str)
379 const struct got_error *err = NULL;
380 struct got_object_qid *qid;
382 err = got_object_qid_alloc_partial(&qid);
383 if (err)
384 return err;
386 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
387 err = got_error(GOT_ERR_BAD_OBJ_DATA);
388 got_object_qid_free(qid);
389 return err;
392 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
393 commit->nparents++;
395 return NULL;
398 static const struct got_error *
399 parse_gmtoff(time_t *gmtoff, const char *tzstr)
401 int sign = 1;
402 const char *p = tzstr;
403 time_t h, m;
405 *gmtoff = 0;
407 if (*p == '-')
408 sign = -1;
409 else if (*p != '+')
410 return got_error(GOT_ERR_BAD_OBJ_DATA);
411 p++;
412 if (!isdigit(*p) && !isdigit(*(p + 1)))
413 return got_error(GOT_ERR_BAD_OBJ_DATA);
414 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
416 p += 2;
417 if (!isdigit(*p) && !isdigit(*(p + 1)))
418 return got_error(GOT_ERR_BAD_OBJ_DATA);
419 m = ((*p - '0') * 10) + (*(p + 1) - '0');
421 *gmtoff = (h * 60 * 60 + m * 60) * sign;
422 return NULL;
425 static const struct got_error *
426 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
428 const struct got_error *err = NULL;
429 const char *errstr;
430 char *space, *tzstr;
432 /* Parse and strip off trailing timezone indicator string. */
433 space = strrchr(committer, ' ');
434 if (space == NULL)
435 return got_error(GOT_ERR_BAD_OBJ_DATA);
436 tzstr = strdup(space + 1);
437 if (tzstr == NULL)
438 return got_error_from_errno("strdup");
439 err = parse_gmtoff(gmtoff, tzstr);
440 free(tzstr);
441 if (err) {
442 if (err->code != GOT_ERR_BAD_OBJ_DATA)
443 return err;
444 /* Old versions of Git omitted the timestamp. */
445 *time = 0;
446 *gmtoff = 0;
447 return NULL;
449 *space = '\0';
451 /* Timestamp is separated from committer name + email by space. */
452 space = strrchr(committer, ' ');
453 if (space == NULL)
454 return got_error(GOT_ERR_BAD_OBJ_DATA);
456 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
457 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
458 if (errstr)
459 return got_error(GOT_ERR_BAD_OBJ_DATA);
461 /* Strip off parsed time information, leaving just author and email. */
462 *space = '\0';
464 return NULL;
467 void
468 got_object_commit_close(struct got_commit_object *commit)
470 if (commit->refcnt > 0) {
471 commit->refcnt--;
472 if (commit->refcnt > 0)
473 return;
476 got_object_id_queue_free(&commit->parent_ids);
477 free(commit->tree_id);
478 free(commit->author);
479 free(commit->committer);
480 free(commit->logmsg);
481 free(commit);
484 struct got_object_id *
485 got_object_commit_get_tree_id(struct got_commit_object *commit)
487 return commit->tree_id;
490 int
491 got_object_commit_get_nparents(struct got_commit_object *commit)
493 return commit->nparents;
496 const struct got_object_id_queue *
497 got_object_commit_get_parent_ids(struct got_commit_object *commit)
499 return &commit->parent_ids;
502 const char *
503 got_object_commit_get_author(struct got_commit_object *commit)
505 return commit->author;
508 time_t
509 got_object_commit_get_author_time(struct got_commit_object *commit)
511 return commit->author_time;
514 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
516 return commit->author_gmtoff;
519 const char *
520 got_object_commit_get_committer(struct got_commit_object *commit)
522 return commit->committer;
525 time_t
526 got_object_commit_get_committer_time(struct got_commit_object *commit)
528 return commit->committer_time;
531 time_t
532 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
534 return commit->committer_gmtoff;
537 const struct got_error *
538 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
540 const struct got_error *err = NULL;
541 const char *src;
542 char *dst;
543 size_t len;
545 len = strlen(commit->logmsg);
546 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
547 if (*logmsg == NULL)
548 return got_error_from_errno("malloc");
550 /*
551 * Strip out unusual headers. Headers are separated from the commit
552 * message body by a single empty line.
553 */
554 src = commit->logmsg;
555 dst = *logmsg;
556 while (*src != '\0' && *src != '\n') {
557 int copy_header = 1, eol = 0;
558 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
559 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
560 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
561 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
562 strncmp(src, GOT_COMMIT_LABEL_PARENT,
563 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
564 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
565 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
566 copy_header = 0;
568 while (*src != '\0' && !eol) {
569 if (copy_header) {
570 *dst = *src;
571 dst++;
573 if (*src == '\n')
574 eol = 1;
575 src++;
578 *dst = '\0';
580 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
581 err = got_error(GOT_ERR_NO_SPACE);
582 goto done;
585 /* Trim redundant trailing whitespace. */
586 len = strlen(*logmsg);
587 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
588 isspace((unsigned char)(*logmsg)[len - 1])) {
589 (*logmsg)[len - 1] = '\0';
590 len--;
593 /* Append a trailing newline if missing. */
594 if (len > 0 && (*logmsg)[len - 1] != '\n') {
595 (*logmsg)[len] = '\n';
596 (*logmsg)[len + 1] = '\0';
598 done:
599 if (err) {
600 free(*logmsg);
601 *logmsg = NULL;
603 return err;
606 const char *
607 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
609 return commit->logmsg;
612 const struct got_error *
613 got_object_parse_commit(struct got_commit_object **commit, char *buf,
614 size_t len)
616 const struct got_error *err = NULL;
617 char *s = buf;
618 size_t label_len;
619 ssize_t remain = (ssize_t)len;
621 if (remain == 0)
622 return got_error(GOT_ERR_BAD_OBJ_DATA);
624 *commit = got_object_commit_alloc_partial();
625 if (*commit == NULL)
626 return got_error_from_errno("got_object_commit_alloc_partial");
628 label_len = strlen(GOT_COMMIT_LABEL_TREE);
629 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
630 remain -= label_len;
631 if (remain < SHA1_DIGEST_STRING_LENGTH) {
632 err = got_error(GOT_ERR_BAD_OBJ_DATA);
633 goto done;
635 s += label_len;
636 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 goto done;
640 remain -= SHA1_DIGEST_STRING_LENGTH;
641 s += SHA1_DIGEST_STRING_LENGTH;
642 } else {
643 err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 goto done;
647 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
648 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
649 remain -= label_len;
650 if (remain < SHA1_DIGEST_STRING_LENGTH) {
651 err = got_error(GOT_ERR_BAD_OBJ_DATA);
652 goto done;
654 s += label_len;
655 err = got_object_commit_add_parent(*commit, s);
656 if (err)
657 goto done;
659 remain -= SHA1_DIGEST_STRING_LENGTH;
660 s += SHA1_DIGEST_STRING_LENGTH;
663 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
664 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
665 char *p;
666 size_t slen;
668 remain -= label_len;
669 if (remain <= 0) {
670 err = got_error(GOT_ERR_BAD_OBJ_DATA);
671 goto done;
673 s += label_len;
674 p = memchr(s, '\n', remain);
675 if (p == NULL) {
676 err = got_error(GOT_ERR_BAD_OBJ_DATA);
677 goto done;
679 *p = '\0';
680 slen = strlen(s);
681 err = parse_commit_time(&(*commit)->author_time,
682 &(*commit)->author_gmtoff, s);
683 if (err)
684 goto done;
685 (*commit)->author = strdup(s);
686 if ((*commit)->author == NULL) {
687 err = got_error_from_errno("strdup");
688 goto done;
690 s += slen + 1;
691 remain -= slen + 1;
694 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
695 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
696 char *p;
697 size_t slen;
699 remain -= label_len;
700 if (remain <= 0) {
701 err = got_error(GOT_ERR_BAD_OBJ_DATA);
702 goto done;
704 s += label_len;
705 p = memchr(s, '\n', remain);
706 if (p == NULL) {
707 err = got_error(GOT_ERR_BAD_OBJ_DATA);
708 goto done;
710 *p = '\0';
711 slen = strlen(s);
712 err = parse_commit_time(&(*commit)->committer_time,
713 &(*commit)->committer_gmtoff, s);
714 if (err)
715 goto done;
716 (*commit)->committer = strdup(s);
717 if ((*commit)->committer == NULL) {
718 err = got_error_from_errno("strdup");
719 goto done;
721 s += slen + 1;
722 remain -= slen + 1;
725 (*commit)->logmsg = strndup(s, remain);
726 if ((*commit)->logmsg == NULL) {
727 err = got_error_from_errno("strndup");
728 goto done;
730 done:
731 if (err) {
732 got_object_commit_close(*commit);
733 *commit = NULL;
735 return err;
738 const struct got_error *
739 got_object_read_commit(struct got_commit_object **commit, int fd,
740 struct got_object_id *expected_id, size_t expected_size)
742 struct got_object *obj = NULL;
743 const struct got_error *err = NULL;
744 size_t len;
745 uint8_t *p;
746 struct got_inflate_checksum csum;
747 SHA1_CTX sha1_ctx;
748 struct got_object_id id;
750 SHA1Init(&sha1_ctx);
751 memset(&csum, 0, sizeof(csum));
752 csum.output_sha1 = &sha1_ctx;
754 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
755 if (err)
756 return err;
758 SHA1Final(id.sha1, &sha1_ctx);
759 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
760 char buf[SHA1_DIGEST_STRING_LENGTH];
761 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
762 "checksum failure for object %s",
763 got_sha1_digest_to_str(expected_id->sha1, buf,
764 sizeof(buf)));
765 goto done;
768 err = got_object_parse_header(&obj, p, len);
769 if (err)
770 goto done;
772 if (len < obj->hdrlen + obj->size) {
773 err = got_error(GOT_ERR_BAD_OBJ_DATA);
774 goto done;
777 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
778 err = got_error(GOT_ERR_OBJ_TYPE);
779 goto done;
782 /* Skip object header. */
783 len -= obj->hdrlen;
784 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
785 done:
786 free(p);
787 if (obj)
788 got_object_close(obj);
789 return err;
792 void
793 got_object_tree_close(struct got_tree_object *tree)
795 if (tree->refcnt > 0) {
796 tree->refcnt--;
797 if (tree->refcnt > 0)
798 return;
801 free(tree->entries);
802 free(tree);
805 static const struct got_error *
806 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
807 size_t maxlen)
809 char *p, *space;
811 *elen = 0;
813 *elen = strnlen(buf, maxlen) + 1;
814 if (*elen > maxlen)
815 return got_error(GOT_ERR_BAD_OBJ_DATA);
817 space = memchr(buf, ' ', *elen);
818 if (space == NULL || space <= buf)
819 return got_error(GOT_ERR_BAD_OBJ_DATA);
821 pte->mode = 0;
822 p = buf;
823 while (p < space) {
824 if (*p < '0' && *p > '7')
825 return got_error(GOT_ERR_BAD_OBJ_DATA);
826 pte->mode <<= 3;
827 pte->mode |= *p - '0';
828 p++;
831 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
832 return got_error(GOT_ERR_BAD_OBJ_DATA);
834 pte->name = space + 1;
835 pte->namelen = strlen(pte->name);
836 buf += *elen;
837 pte->id = buf;
838 *elen += SHA1_DIGEST_LENGTH;
839 return NULL;
842 static int
843 pte_cmp(const void *pa, const void *pb)
845 const struct got_parsed_tree_entry *a = pa, *b = pb;
847 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
850 const struct got_error *
851 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
852 size_t *nentries_alloc, uint8_t *buf, size_t len)
854 const struct got_error *err = NULL;
855 size_t remain = len;
856 const size_t nalloc = 16;
857 struct got_parsed_tree_entry *pte;
858 int i;
860 *nentries = 0;
861 if (remain == 0)
862 return NULL; /* tree is empty */
864 while (remain > 0) {
865 size_t elen;
867 if (*nentries >= *nentries_alloc) {
868 pte = recallocarray(*entries, *nentries_alloc,
869 *nentries_alloc + nalloc, sizeof(**entries));
870 if (pte == NULL) {
871 err = got_error_from_errno("recallocarray");
872 goto done;
874 *entries = pte;
875 *nentries_alloc += nalloc;
878 pte = &(*entries)[*nentries];
879 err = parse_tree_entry(pte, &elen, buf, remain);
880 if (err)
881 goto done;
882 buf += elen;
883 remain -= elen;
884 (*nentries)++;
887 if (remain != 0) {
888 err = got_error(GOT_ERR_BAD_OBJ_DATA);
889 goto done;
892 if (*nentries > 1) {
893 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
895 for (i = 0; i < *nentries - 1; i++) {
896 struct got_parsed_tree_entry *prev = &(*entries)[i];
897 pte = &(*entries)[i + 1];
898 if (got_path_cmp(prev->name, pte->name,
899 prev->namelen, pte->namelen) == 0) {
900 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
901 break;
905 done:
906 if (err)
907 *nentries = 0;
908 return err;
911 const struct got_error *
912 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
913 size_t *nentries_alloc, uint8_t **p, int fd,
914 struct got_object_id *expected_id)
916 const struct got_error *err = NULL;
917 struct got_object *obj = NULL;
918 size_t len;
919 struct got_inflate_checksum csum;
920 SHA1_CTX sha1_ctx;
921 struct got_object_id id;
923 SHA1Init(&sha1_ctx);
924 memset(&csum, 0, sizeof(csum));
925 csum.output_sha1 = &sha1_ctx;
927 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
928 if (err)
929 return err;
931 SHA1Final(id.sha1, &sha1_ctx);
932 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
933 char buf[SHA1_DIGEST_STRING_LENGTH];
934 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
935 "checksum failure for object %s",
936 got_sha1_digest_to_str(expected_id->sha1, buf,
937 sizeof(buf)));
938 goto done;
941 err = got_object_parse_header(&obj, *p, len);
942 if (err)
943 goto done;
945 if (len < obj->hdrlen + obj->size) {
946 err = got_error(GOT_ERR_BAD_OBJ_DATA);
947 goto done;
950 /* Skip object header. */
951 len -= obj->hdrlen;
952 err = got_object_parse_tree(entries, nentries, nentries_alloc,
953 *p + obj->hdrlen, len);
954 done:
955 if (obj)
956 got_object_close(obj);
957 return err;
960 void
961 got_object_tag_close(struct got_tag_object *tag)
963 if (tag->refcnt > 0) {
964 tag->refcnt--;
965 if (tag->refcnt > 0)
966 return;
969 free(tag->tag);
970 free(tag->tagger);
971 free(tag->tagmsg);
972 free(tag);
975 const struct got_error *
976 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
978 const struct got_error *err = NULL;
979 size_t remain = len;
980 char *s = buf;
981 size_t label_len;
983 if (remain == 0)
984 return got_error(GOT_ERR_BAD_OBJ_DATA);
986 *tag = calloc(1, sizeof(**tag));
987 if (*tag == NULL)
988 return got_error_from_errno("calloc");
990 label_len = strlen(GOT_TAG_LABEL_OBJECT);
991 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
992 remain -= label_len;
993 if (remain < SHA1_DIGEST_STRING_LENGTH) {
994 err = got_error(GOT_ERR_BAD_OBJ_DATA);
995 goto done;
997 s += label_len;
998 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
999 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1000 goto done;
1002 remain -= SHA1_DIGEST_STRING_LENGTH;
1003 s += SHA1_DIGEST_STRING_LENGTH;
1004 } else {
1005 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1006 goto done;
1009 if (remain <= 0) {
1010 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1011 goto done;
1014 label_len = strlen(GOT_TAG_LABEL_TYPE);
1015 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1016 remain -= label_len;
1017 if (remain <= 0) {
1018 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1019 goto done;
1021 s += label_len;
1022 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1023 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1024 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1025 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1026 s += label_len;
1027 remain -= label_len;
1028 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1029 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1030 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1031 label_len = strlen(GOT_OBJ_LABEL_TREE);
1032 s += label_len;
1033 remain -= label_len;
1034 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1035 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1036 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1037 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1038 s += label_len;
1039 remain -= label_len;
1040 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1041 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1042 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1043 label_len = strlen(GOT_OBJ_LABEL_TAG);
1044 s += label_len;
1045 remain -= label_len;
1046 } else {
1047 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1048 goto done;
1051 if (remain <= 0 || *s != '\n') {
1052 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1053 goto done;
1055 s++;
1056 remain--;
1057 if (remain <= 0) {
1058 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1059 goto done;
1061 } else {
1062 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1063 goto done;
1066 label_len = strlen(GOT_TAG_LABEL_TAG);
1067 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1068 char *p;
1069 size_t slen;
1070 remain -= label_len;
1071 if (remain <= 0) {
1072 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1073 goto done;
1075 s += label_len;
1076 p = memchr(s, '\n', remain);
1077 if (p == NULL) {
1078 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1079 goto done;
1081 *p = '\0';
1082 slen = strlen(s);
1083 (*tag)->tag = strndup(s, slen);
1084 if ((*tag)->tag == NULL) {
1085 err = got_error_from_errno("strndup");
1086 goto done;
1088 s += slen + 1;
1089 remain -= slen + 1;
1090 if (remain <= 0) {
1091 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1092 goto done;
1094 } else {
1095 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1096 goto done;
1099 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1100 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1101 char *p;
1102 size_t slen;
1104 remain -= label_len;
1105 if (remain <= 0) {
1106 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1107 goto done;
1109 s += label_len;
1110 p = memchr(s, '\n', remain);
1111 if (p == NULL) {
1112 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1113 goto done;
1115 *p = '\0';
1116 slen = strlen(s);
1117 err = parse_commit_time(&(*tag)->tagger_time,
1118 &(*tag)->tagger_gmtoff, s);
1119 if (err)
1120 goto done;
1121 (*tag)->tagger = strdup(s);
1122 if ((*tag)->tagger == NULL) {
1123 err = got_error_from_errno("strdup");
1124 goto done;
1126 s += slen + 1;
1127 remain -= slen + 1;
1128 if (remain < 0) {
1129 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1130 goto done;
1132 } else {
1133 /* Some old tags in the Linux git repo have no tagger. */
1134 (*tag)->tagger = strdup("");
1135 if ((*tag)->tagger == NULL) {
1136 err = got_error_from_errno("strdup");
1137 goto done;
1141 (*tag)->tagmsg = strndup(s, remain);
1142 if ((*tag)->tagmsg == NULL) {
1143 err = got_error_from_errno("strndup");
1144 goto done;
1146 done:
1147 if (err) {
1148 got_object_tag_close(*tag);
1149 *tag = NULL;
1151 return err;
1154 const struct got_error *
1155 got_object_read_tag(struct got_tag_object **tag, int fd,
1156 struct got_object_id *expected_id, size_t expected_size)
1158 const struct got_error *err = NULL;
1159 struct got_object *obj = NULL;
1160 size_t len;
1161 uint8_t *p;
1162 struct got_inflate_checksum csum;
1163 SHA1_CTX sha1_ctx;
1164 struct got_object_id id;
1166 SHA1Init(&sha1_ctx);
1167 memset(&csum, 0, sizeof(csum));
1168 csum.output_sha1 = &sha1_ctx;
1170 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1171 expected_size, fd);
1172 if (err)
1173 return err;
1175 SHA1Final(id.sha1, &sha1_ctx);
1176 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
1177 char buf[SHA1_DIGEST_STRING_LENGTH];
1178 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1179 "checksum failure for object %s",
1180 got_sha1_digest_to_str(expected_id->sha1, buf,
1181 sizeof(buf)));
1182 goto done;
1185 err = got_object_parse_header(&obj, p, len);
1186 if (err)
1187 goto done;
1189 if (len < obj->hdrlen + obj->size) {
1190 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1191 goto done;
1194 /* Skip object header. */
1195 len -= obj->hdrlen;
1196 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1197 done:
1198 free(p);
1199 if (obj)
1200 got_object_close(obj);
1201 return err;
1204 const struct got_error *
1205 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1207 const struct got_error *err = NULL;
1208 static const size_t blocksize = 512;
1209 size_t n, total, remain;
1210 uint8_t *buf;
1212 *outbuf = NULL;
1213 *outlen = 0;
1215 buf = malloc(blocksize);
1216 if (buf == NULL)
1217 return got_error_from_errno("malloc");
1219 remain = blocksize;
1220 total = 0;
1221 for (;;) {
1222 if (remain == 0) {
1223 uint8_t *newbuf;
1224 newbuf = reallocarray(buf, 1, total + blocksize);
1225 if (newbuf == NULL) {
1226 err = got_error_from_errno("reallocarray");
1227 goto done;
1229 buf = newbuf;
1230 remain += blocksize;
1232 n = fread(buf + total, 1, remain, f);
1233 if (n == 0) {
1234 if (ferror(f)) {
1235 err = got_ferror(f, GOT_ERR_IO);
1236 goto done;
1238 break; /* EOF */
1240 remain -= n;
1241 total += n;
1244 done:
1245 if (err == NULL) {
1246 *outbuf = buf;
1247 *outlen = total;
1248 } else
1249 free(buf);
1250 return err;