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((unsigned char)*p) &&
413 !isdigit((unsigned char)*(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((unsigned char)*p) &&
419 !isdigit((unsigned char)*(p + 1)))
420 return got_error(GOT_ERR_BAD_OBJ_DATA);
421 m = ((*p - '0') * 10) + (*(p + 1) - '0');
423 *gmtoff = (h * 60 * 60 + m * 60) * sign;
424 return NULL;
427 static const struct got_error *
428 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
430 const struct got_error *err = NULL;
431 const char *errstr;
432 char *space, *tzstr;
434 /* Parse and strip off trailing timezone indicator string. */
435 space = strrchr(committer, ' ');
436 if (space == NULL)
437 return got_error(GOT_ERR_BAD_OBJ_DATA);
438 tzstr = strdup(space + 1);
439 if (tzstr == NULL)
440 return got_error_from_errno("strdup");
441 err = parse_gmtoff(gmtoff, tzstr);
442 free(tzstr);
443 if (err) {
444 if (err->code != GOT_ERR_BAD_OBJ_DATA)
445 return err;
446 /* Old versions of Git omitted the timestamp. */
447 *time = 0;
448 *gmtoff = 0;
449 return NULL;
451 *space = '\0';
453 /* Timestamp is separated from committer name + email by space. */
454 space = strrchr(committer, ' ');
455 if (space == NULL)
456 return got_error(GOT_ERR_BAD_OBJ_DATA);
458 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
459 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
460 if (errstr)
461 return got_error(GOT_ERR_BAD_OBJ_DATA);
463 /* Strip off parsed time information, leaving just author and email. */
464 *space = '\0';
466 return NULL;
469 void
470 got_object_commit_close(struct got_commit_object *commit)
472 if (commit->refcnt > 0) {
473 commit->refcnt--;
474 if (commit->refcnt > 0)
475 return;
478 got_object_id_queue_free(&commit->parent_ids);
479 free(commit->tree_id);
480 free(commit->author);
481 free(commit->committer);
482 free(commit->logmsg);
483 free(commit);
486 struct got_object_id *
487 got_object_commit_get_tree_id(struct got_commit_object *commit)
489 return commit->tree_id;
492 int
493 got_object_commit_get_nparents(struct got_commit_object *commit)
495 return commit->nparents;
498 const struct got_object_id_queue *
499 got_object_commit_get_parent_ids(struct got_commit_object *commit)
501 return &commit->parent_ids;
504 const char *
505 got_object_commit_get_author(struct got_commit_object *commit)
507 return commit->author;
510 time_t
511 got_object_commit_get_author_time(struct got_commit_object *commit)
513 return commit->author_time;
516 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
518 return commit->author_gmtoff;
521 const char *
522 got_object_commit_get_committer(struct got_commit_object *commit)
524 return commit->committer;
527 time_t
528 got_object_commit_get_committer_time(struct got_commit_object *commit)
530 return commit->committer_time;
533 time_t
534 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
536 return commit->committer_gmtoff;
539 const struct got_error *
540 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
542 const struct got_error *err = NULL;
543 const char *src;
544 char *dst;
545 size_t len;
547 len = strlen(commit->logmsg);
548 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
549 if (*logmsg == NULL)
550 return got_error_from_errno("malloc");
552 /*
553 * Strip out unusual headers. Headers are separated from the commit
554 * message body by a single empty line.
555 */
556 src = commit->logmsg;
557 dst = *logmsg;
558 while (*src != '\0' && *src != '\n') {
559 int copy_header = 1, eol = 0;
560 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
561 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
562 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
563 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
564 strncmp(src, GOT_COMMIT_LABEL_PARENT,
565 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
566 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
567 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
568 copy_header = 0;
570 while (*src != '\0' && !eol) {
571 if (copy_header) {
572 *dst = *src;
573 dst++;
575 if (*src == '\n')
576 eol = 1;
577 src++;
580 *dst = '\0';
582 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
583 err = got_error(GOT_ERR_NO_SPACE);
584 goto done;
587 /* Trim redundant trailing whitespace. */
588 len = strlen(*logmsg);
589 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
590 isspace((unsigned char)(*logmsg)[len - 1])) {
591 (*logmsg)[len - 1] = '\0';
592 len--;
595 /* Append a trailing newline if missing. */
596 if (len > 0 && (*logmsg)[len - 1] != '\n') {
597 (*logmsg)[len] = '\n';
598 (*logmsg)[len + 1] = '\0';
600 done:
601 if (err) {
602 free(*logmsg);
603 *logmsg = NULL;
605 return err;
608 const char *
609 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
611 return commit->logmsg;
614 const struct got_error *
615 got_object_parse_commit(struct got_commit_object **commit, char *buf,
616 size_t len)
618 const struct got_error *err = NULL;
619 char *s = buf;
620 size_t label_len;
621 ssize_t remain = (ssize_t)len;
623 if (remain == 0)
624 return got_error(GOT_ERR_BAD_OBJ_DATA);
626 *commit = got_object_commit_alloc_partial();
627 if (*commit == NULL)
628 return got_error_from_errno("got_object_commit_alloc_partial");
630 label_len = strlen(GOT_COMMIT_LABEL_TREE);
631 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
632 remain -= label_len;
633 if (remain < SHA1_DIGEST_STRING_LENGTH) {
634 err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 goto done;
637 s += label_len;
638 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 goto done;
642 remain -= SHA1_DIGEST_STRING_LENGTH;
643 s += SHA1_DIGEST_STRING_LENGTH;
644 } else {
645 err = got_error(GOT_ERR_BAD_OBJ_DATA);
646 goto done;
649 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
650 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
651 remain -= label_len;
652 if (remain < SHA1_DIGEST_STRING_LENGTH) {
653 err = got_error(GOT_ERR_BAD_OBJ_DATA);
654 goto done;
656 s += label_len;
657 err = got_object_commit_add_parent(*commit, s);
658 if (err)
659 goto done;
661 remain -= SHA1_DIGEST_STRING_LENGTH;
662 s += SHA1_DIGEST_STRING_LENGTH;
665 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
666 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
667 char *p;
668 size_t slen;
670 remain -= label_len;
671 if (remain <= 0) {
672 err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 goto done;
675 s += label_len;
676 p = memchr(s, '\n', remain);
677 if (p == NULL) {
678 err = got_error(GOT_ERR_BAD_OBJ_DATA);
679 goto done;
681 *p = '\0';
682 slen = strlen(s);
683 err = parse_commit_time(&(*commit)->author_time,
684 &(*commit)->author_gmtoff, s);
685 if (err)
686 goto done;
687 (*commit)->author = strdup(s);
688 if ((*commit)->author == NULL) {
689 err = got_error_from_errno("strdup");
690 goto done;
692 s += slen + 1;
693 remain -= slen + 1;
696 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
697 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
698 char *p;
699 size_t slen;
701 remain -= label_len;
702 if (remain <= 0) {
703 err = got_error(GOT_ERR_BAD_OBJ_DATA);
704 goto done;
706 s += label_len;
707 p = memchr(s, '\n', remain);
708 if (p == NULL) {
709 err = got_error(GOT_ERR_BAD_OBJ_DATA);
710 goto done;
712 *p = '\0';
713 slen = strlen(s);
714 err = parse_commit_time(&(*commit)->committer_time,
715 &(*commit)->committer_gmtoff, s);
716 if (err)
717 goto done;
718 (*commit)->committer = strdup(s);
719 if ((*commit)->committer == NULL) {
720 err = got_error_from_errno("strdup");
721 goto done;
723 s += slen + 1;
724 remain -= slen + 1;
727 (*commit)->logmsg = strndup(s, remain);
728 if ((*commit)->logmsg == NULL) {
729 err = got_error_from_errno("strndup");
730 goto done;
732 done:
733 if (err) {
734 got_object_commit_close(*commit);
735 *commit = NULL;
737 return err;
740 const struct got_error *
741 got_object_read_commit(struct got_commit_object **commit, int fd,
742 struct got_object_id *expected_id, size_t expected_size)
744 struct got_object *obj = NULL;
745 const struct got_error *err = NULL;
746 size_t len;
747 uint8_t *p;
748 struct got_inflate_checksum csum;
749 SHA1_CTX sha1_ctx;
750 struct got_object_id id;
752 SHA1Init(&sha1_ctx);
753 memset(&csum, 0, sizeof(csum));
754 csum.output_sha1 = &sha1_ctx;
756 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
757 if (err)
758 return err;
760 SHA1Final(id.sha1, &sha1_ctx);
761 if (got_object_id_cmp(expected_id, &id) != 0) {
762 char buf[SHA1_DIGEST_STRING_LENGTH];
763 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
764 "checksum failure for object %s",
765 got_sha1_digest_to_str(expected_id->sha1, buf,
766 sizeof(buf)));
767 goto done;
770 err = got_object_parse_header(&obj, p, len);
771 if (err)
772 goto done;
774 if (len < obj->hdrlen + obj->size) {
775 err = got_error(GOT_ERR_BAD_OBJ_DATA);
776 goto done;
779 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
780 err = got_error(GOT_ERR_OBJ_TYPE);
781 goto done;
784 /* Skip object header. */
785 len -= obj->hdrlen;
786 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
787 done:
788 free(p);
789 if (obj)
790 got_object_close(obj);
791 return err;
794 void
795 got_object_tree_close(struct got_tree_object *tree)
797 if (tree->refcnt > 0) {
798 tree->refcnt--;
799 if (tree->refcnt > 0)
800 return;
803 free(tree->entries);
804 free(tree);
807 static const struct got_error *
808 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
809 size_t maxlen)
811 char *p, *space;
813 *elen = 0;
815 *elen = strnlen(buf, maxlen) + 1;
816 if (*elen > maxlen)
817 return got_error(GOT_ERR_BAD_OBJ_DATA);
819 space = memchr(buf, ' ', *elen);
820 if (space == NULL || space <= buf)
821 return got_error(GOT_ERR_BAD_OBJ_DATA);
823 pte->mode = 0;
824 p = buf;
825 while (p < space) {
826 if (*p < '0' || *p > '7')
827 return got_error(GOT_ERR_BAD_OBJ_DATA);
828 pte->mode <<= 3;
829 pte->mode |= *p - '0';
830 p++;
833 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
834 return got_error(GOT_ERR_BAD_OBJ_DATA);
836 pte->name = space + 1;
837 pte->namelen = strlen(pte->name);
838 buf += *elen;
839 pte->id = buf;
840 *elen += SHA1_DIGEST_LENGTH;
841 return NULL;
844 static int
845 pte_cmp(const void *pa, const void *pb)
847 const struct got_parsed_tree_entry *a = pa, *b = pb;
849 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
852 const struct got_error *
853 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
854 size_t *nentries_alloc, uint8_t *buf, size_t len)
856 const struct got_error *err = NULL;
857 size_t remain = len;
858 const size_t nalloc = 16;
859 struct got_parsed_tree_entry *pte;
860 int i;
862 *nentries = 0;
863 if (remain == 0)
864 return NULL; /* tree is empty */
866 while (remain > 0) {
867 size_t elen;
869 if (*nentries >= *nentries_alloc) {
870 pte = recallocarray(*entries, *nentries_alloc,
871 *nentries_alloc + nalloc, sizeof(**entries));
872 if (pte == NULL) {
873 err = got_error_from_errno("recallocarray");
874 goto done;
876 *entries = pte;
877 *nentries_alloc += nalloc;
880 pte = &(*entries)[*nentries];
881 err = parse_tree_entry(pte, &elen, buf, remain);
882 if (err)
883 goto done;
884 buf += elen;
885 remain -= elen;
886 (*nentries)++;
889 if (remain != 0) {
890 err = got_error(GOT_ERR_BAD_OBJ_DATA);
891 goto done;
894 if (*nentries > 1) {
895 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
897 for (i = 0; i < *nentries - 1; i++) {
898 struct got_parsed_tree_entry *prev = &(*entries)[i];
899 pte = &(*entries)[i + 1];
900 if (got_path_cmp(prev->name, pte->name,
901 prev->namelen, pte->namelen) == 0) {
902 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
903 break;
907 done:
908 if (err)
909 *nentries = 0;
910 return err;
913 const struct got_error *
914 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
915 size_t *nentries_alloc, uint8_t **p, int fd,
916 struct got_object_id *expected_id)
918 const struct got_error *err = NULL;
919 struct got_object *obj = NULL;
920 size_t len;
921 struct got_inflate_checksum csum;
922 SHA1_CTX sha1_ctx;
923 struct got_object_id id;
925 SHA1Init(&sha1_ctx);
926 memset(&csum, 0, sizeof(csum));
927 csum.output_sha1 = &sha1_ctx;
929 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
930 if (err)
931 return err;
933 SHA1Final(id.sha1, &sha1_ctx);
934 if (got_object_id_cmp(expected_id, &id) != 0) {
935 char buf[SHA1_DIGEST_STRING_LENGTH];
936 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
937 "checksum failure for object %s",
938 got_sha1_digest_to_str(expected_id->sha1, buf,
939 sizeof(buf)));
940 goto done;
943 err = got_object_parse_header(&obj, *p, len);
944 if (err)
945 goto done;
947 if (len < obj->hdrlen + obj->size) {
948 err = got_error(GOT_ERR_BAD_OBJ_DATA);
949 goto done;
952 /* Skip object header. */
953 len -= obj->hdrlen;
954 err = got_object_parse_tree(entries, nentries, nentries_alloc,
955 *p + obj->hdrlen, len);
956 done:
957 if (obj)
958 got_object_close(obj);
959 return err;
962 void
963 got_object_tag_close(struct got_tag_object *tag)
965 if (tag->refcnt > 0) {
966 tag->refcnt--;
967 if (tag->refcnt > 0)
968 return;
971 free(tag->tag);
972 free(tag->tagger);
973 free(tag->tagmsg);
974 free(tag);
977 const struct got_error *
978 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
980 const struct got_error *err = NULL;
981 size_t remain = len;
982 char *s = buf;
983 size_t label_len;
985 if (remain == 0)
986 return got_error(GOT_ERR_BAD_OBJ_DATA);
988 *tag = calloc(1, sizeof(**tag));
989 if (*tag == NULL)
990 return got_error_from_errno("calloc");
992 label_len = strlen(GOT_TAG_LABEL_OBJECT);
993 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
994 remain -= label_len;
995 if (remain < SHA1_DIGEST_STRING_LENGTH) {
996 err = got_error(GOT_ERR_BAD_OBJ_DATA);
997 goto done;
999 s += label_len;
1000 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
1001 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1002 goto done;
1004 remain -= SHA1_DIGEST_STRING_LENGTH;
1005 s += SHA1_DIGEST_STRING_LENGTH;
1006 } else {
1007 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1008 goto done;
1011 if (remain <= 0) {
1012 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1013 goto done;
1016 label_len = strlen(GOT_TAG_LABEL_TYPE);
1017 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1018 remain -= label_len;
1019 if (remain <= 0) {
1020 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1021 goto done;
1023 s += label_len;
1024 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1025 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1026 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1027 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1028 s += label_len;
1029 remain -= label_len;
1030 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1031 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1032 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1033 label_len = strlen(GOT_OBJ_LABEL_TREE);
1034 s += label_len;
1035 remain -= label_len;
1036 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1037 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1038 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1039 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1040 s += label_len;
1041 remain -= label_len;
1042 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1043 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1044 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1045 label_len = strlen(GOT_OBJ_LABEL_TAG);
1046 s += label_len;
1047 remain -= label_len;
1048 } else {
1049 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1050 goto done;
1053 if (remain <= 0 || *s != '\n') {
1054 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1055 goto done;
1057 s++;
1058 remain--;
1059 if (remain <= 0) {
1060 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1061 goto done;
1063 } else {
1064 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1065 goto done;
1068 label_len = strlen(GOT_TAG_LABEL_TAG);
1069 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1070 char *p;
1071 size_t slen;
1072 remain -= label_len;
1073 if (remain <= 0) {
1074 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1075 goto done;
1077 s += label_len;
1078 p = memchr(s, '\n', remain);
1079 if (p == NULL) {
1080 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1081 goto done;
1083 *p = '\0';
1084 slen = strlen(s);
1085 (*tag)->tag = strndup(s, slen);
1086 if ((*tag)->tag == NULL) {
1087 err = got_error_from_errno("strndup");
1088 goto done;
1090 s += slen + 1;
1091 remain -= slen + 1;
1092 if (remain <= 0) {
1093 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1094 goto done;
1096 } else {
1097 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1098 goto done;
1101 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1102 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1103 char *p;
1104 size_t slen;
1106 remain -= label_len;
1107 if (remain <= 0) {
1108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1109 goto done;
1111 s += label_len;
1112 p = memchr(s, '\n', remain);
1113 if (p == NULL) {
1114 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1115 goto done;
1117 *p = '\0';
1118 slen = strlen(s);
1119 err = parse_commit_time(&(*tag)->tagger_time,
1120 &(*tag)->tagger_gmtoff, s);
1121 if (err)
1122 goto done;
1123 (*tag)->tagger = strdup(s);
1124 if ((*tag)->tagger == NULL) {
1125 err = got_error_from_errno("strdup");
1126 goto done;
1128 s += slen + 1;
1129 remain -= slen + 1;
1130 if (remain < 0) {
1131 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1132 goto done;
1134 } else {
1135 /* Some old tags in the Linux git repo have no tagger. */
1136 (*tag)->tagger = strdup("");
1137 if ((*tag)->tagger == NULL) {
1138 err = got_error_from_errno("strdup");
1139 goto done;
1143 (*tag)->tagmsg = strndup(s, remain);
1144 if ((*tag)->tagmsg == NULL) {
1145 err = got_error_from_errno("strndup");
1146 goto done;
1148 done:
1149 if (err) {
1150 got_object_tag_close(*tag);
1151 *tag = NULL;
1153 return err;
1156 const struct got_error *
1157 got_object_read_tag(struct got_tag_object **tag, int fd,
1158 struct got_object_id *expected_id, size_t expected_size)
1160 const struct got_error *err = NULL;
1161 struct got_object *obj = NULL;
1162 size_t len;
1163 uint8_t *p;
1164 struct got_inflate_checksum csum;
1165 SHA1_CTX sha1_ctx;
1166 struct got_object_id id;
1168 SHA1Init(&sha1_ctx);
1169 memset(&csum, 0, sizeof(csum));
1170 csum.output_sha1 = &sha1_ctx;
1172 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1173 expected_size, fd);
1174 if (err)
1175 return err;
1177 SHA1Final(id.sha1, &sha1_ctx);
1178 if (got_object_id_cmp(expected_id, &id) != 0) {
1179 char buf[SHA1_DIGEST_STRING_LENGTH];
1180 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1181 "checksum failure for object %s",
1182 got_sha1_digest_to_str(expected_id->sha1, buf,
1183 sizeof(buf)));
1184 goto done;
1187 err = got_object_parse_header(&obj, p, len);
1188 if (err)
1189 goto done;
1191 if (len < obj->hdrlen + obj->size) {
1192 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1193 goto done;
1196 /* Skip object header. */
1197 len -= obj->hdrlen;
1198 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1199 done:
1200 free(p);
1201 if (obj)
1202 got_object_close(obj);
1203 return err;
1206 const struct got_error *
1207 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1209 const struct got_error *err = NULL;
1210 static const size_t blocksize = 512;
1211 size_t n, total, remain;
1212 uint8_t *buf;
1214 *outbuf = NULL;
1215 *outlen = 0;
1217 buf = malloc(blocksize);
1218 if (buf == NULL)
1219 return got_error_from_errno("malloc");
1221 remain = blocksize;
1222 total = 0;
1223 for (;;) {
1224 if (remain == 0) {
1225 uint8_t *newbuf;
1226 newbuf = reallocarray(buf, 1, total + blocksize);
1227 if (newbuf == NULL) {
1228 err = got_error_from_errno("reallocarray");
1229 goto done;
1231 buf = newbuf;
1232 remain += blocksize;
1234 n = fread(buf + total, 1, remain, f);
1235 if (n == 0) {
1236 if (ferror(f)) {
1237 err = got_ferror(f, GOT_ERR_IO);
1238 goto done;
1240 break; /* EOF */
1242 remain -= n;
1243 total += n;
1246 done:
1247 if (err == NULL) {
1248 *outbuf = buf;
1249 *outlen = total;
1250 } else
1251 free(buf);
1252 return err;