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 = GOT_OBJECT_ID_HEX_MAXLEN;
92 *outbuf = malloc(len);
93 if (*outbuf == NULL)
94 return got_error_from_errno("malloc");
96 if (got_object_id_hex(id, *outbuf, len) == NULL) {
97 free(*outbuf);
98 *outbuf = NULL;
99 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
102 return NULL;
105 char *
106 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
108 return got_sha1_digest_to_str(id->sha1, buf, len);
111 void
112 got_object_close(struct got_object *obj)
114 if (obj->refcnt > 0) {
115 obj->refcnt--;
116 if (obj->refcnt > 0)
117 return;
120 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
121 struct got_delta *delta;
122 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
123 delta = STAILQ_FIRST(&obj->deltas.entries);
124 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
125 free(delta);
128 free(obj);
131 const struct got_error *
132 got_object_raw_close(struct got_raw_object *obj)
134 const struct got_error *err = NULL;
136 if (obj->refcnt > 0) {
137 obj->refcnt--;
138 if (obj->refcnt > 0)
139 return NULL;
142 if (obj->close_cb)
143 obj->close_cb(obj);
145 if (obj->f == NULL) {
146 if (obj->fd != -1) {
147 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
148 err = got_error_from_errno("munmap");
149 if (close(obj->fd) == -1 && err == NULL)
150 err = got_error_from_errno("close");
151 } else
152 free(obj->data);
153 } else {
154 if (fclose(obj->f) == EOF && err == NULL)
155 err = got_error_from_errno("fclose");
157 free(obj);
158 return err;
161 void
162 got_object_qid_free(struct got_object_qid *qid)
164 free(qid);
167 void
168 got_object_id_queue_free(struct got_object_id_queue *ids)
170 struct got_object_qid *qid;
172 while (!STAILQ_EMPTY(ids)) {
173 qid = STAILQ_FIRST(ids);
174 STAILQ_REMOVE_HEAD(ids, entry);
175 got_object_qid_free(qid);
179 const struct got_error *
180 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
182 const char *obj_labels[] = {
183 GOT_OBJ_LABEL_COMMIT,
184 GOT_OBJ_LABEL_TREE,
185 GOT_OBJ_LABEL_BLOB,
186 GOT_OBJ_LABEL_TAG,
187 };
188 const int obj_types[] = {
189 GOT_OBJ_TYPE_COMMIT,
190 GOT_OBJ_TYPE_TREE,
191 GOT_OBJ_TYPE_BLOB,
192 GOT_OBJ_TYPE_TAG,
193 };
194 int type = 0;
195 size_t size = 0;
196 size_t i;
197 char *end;
199 *obj = NULL;
201 end = memchr(buf, '\0', len);
202 if (end == NULL)
203 return got_error(GOT_ERR_BAD_OBJ_HDR);
205 for (i = 0; i < nitems(obj_labels); i++) {
206 const char *label = obj_labels[i];
207 size_t label_len = strlen(label);
208 const char *errstr;
210 if (len <= label_len || buf + label_len >= end ||
211 strncmp(buf, label, label_len) != 0)
212 continue;
214 type = obj_types[i];
215 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
216 if (errstr != NULL)
217 return got_error(GOT_ERR_BAD_OBJ_HDR);
218 break;
221 if (type == 0)
222 return got_error(GOT_ERR_BAD_OBJ_HDR);
224 *obj = calloc(1, sizeof(**obj));
225 if (*obj == NULL)
226 return got_error_from_errno("calloc");
227 (*obj)->type = type;
228 (*obj)->hdrlen = end - buf + 1;
229 (*obj)->size = size;
230 return NULL;
233 const struct got_error *
234 got_object_read_header(struct got_object **obj, int fd)
236 const struct got_error *err;
237 struct got_inflate_buf zb;
238 uint8_t *buf;
239 const size_t zbsize = 64;
240 size_t outlen, totlen;
241 int nbuf = 1;
243 *obj = NULL;
245 buf = malloc(zbsize);
246 if (buf == NULL)
247 return got_error_from_errno("malloc");
248 buf[0] = '\0';
250 err = got_inflate_init(&zb, buf, zbsize, NULL);
251 if (err)
252 return err;
254 totlen = 0;
255 do {
256 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
257 if (err)
258 goto done;
259 if (outlen == 0)
260 break;
261 totlen += outlen;
262 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
263 uint8_t *newbuf;
264 nbuf++;
265 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
266 if (newbuf == NULL) {
267 err = got_error_from_errno("recallocarray");
268 goto done;
270 buf = newbuf;
271 zb.outbuf = newbuf + totlen;
272 zb.outlen = (nbuf * zbsize) - totlen;
274 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
276 err = got_object_parse_header(obj, buf, totlen);
277 done:
278 free(buf);
279 got_inflate_end(&zb);
280 return err;
283 const struct got_error *
284 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
285 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
286 int infd)
288 const struct got_error *err = NULL;
289 struct got_object *obj;
290 struct got_inflate_checksum csum;
291 uint8_t sha1[SHA1_DIGEST_LENGTH];
292 SHA1_CTX sha1_ctx;
293 size_t len, consumed;
294 FILE *f = NULL;
296 *outbuf = NULL;
297 *size = 0;
298 *hdrlen = 0;
300 SHA1Init(&sha1_ctx);
301 memset(&csum, 0, sizeof(csum));
302 csum.output_sha1 = &sha1_ctx;
304 if (lseek(infd, SEEK_SET, 0) == -1)
305 return got_error_from_errno("lseek");
307 err = got_object_read_header(&obj, infd);
308 if (err)
309 return err;
311 if (lseek(infd, SEEK_SET, 0) == -1)
312 return got_error_from_errno("lseek");
314 if (obj->size + obj->hdrlen <= max_in_mem_size) {
315 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
316 obj->size + obj->hdrlen, infd);
317 } else {
318 int fd;
319 /*
320 * XXX This uses an extra file descriptor for no good reason.
321 * We should have got_inflate_fd_to_fd().
322 */
323 fd = dup(infd);
324 if (fd == -1)
325 return got_error_from_errno("dup");
326 f = fdopen(fd, "r");
327 if (f == NULL) {
328 err = got_error_from_errno("fdopen");
329 abort();
330 close(fd);
331 goto done;
333 err = got_inflate_to_fd(&len, f, &csum, outfd);
335 if (err)
336 goto done;
338 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
339 err = got_error(GOT_ERR_BAD_OBJ_HDR);
340 goto done;
343 SHA1Final(sha1, &sha1_ctx);
344 if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
345 err = got_error_checksum(expected_id);
346 goto done;
349 *size = obj->size;
350 *hdrlen = obj->hdrlen;
351 done:
352 got_object_close(obj);
353 if (f && fclose(f) == EOF && err == NULL)
354 err = got_error_from_errno("fclose");
355 return err;
358 struct got_commit_object *
359 got_object_commit_alloc_partial(void)
361 struct got_commit_object *commit;
363 commit = calloc(1, sizeof(*commit));
364 if (commit == NULL)
365 return NULL;
366 commit->tree_id = malloc(sizeof(*commit->tree_id));
367 if (commit->tree_id == NULL) {
368 free(commit);
369 return NULL;
372 STAILQ_INIT(&commit->parent_ids);
374 return commit;
377 const struct got_error *
378 got_object_commit_add_parent(struct got_commit_object *commit,
379 const char *id_str)
381 const struct got_error *err = NULL;
382 struct got_object_qid *qid;
384 err = got_object_qid_alloc_partial(&qid);
385 if (err)
386 return err;
388 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
389 err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 got_object_qid_free(qid);
391 return err;
394 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
395 commit->nparents++;
397 return NULL;
400 static const struct got_error *
401 parse_gmtoff(time_t *gmtoff, const char *tzstr)
403 int sign = 1;
404 const char *p = tzstr;
405 time_t h, m;
407 *gmtoff = 0;
409 if (*p == '-')
410 sign = -1;
411 else if (*p != '+')
412 return got_error(GOT_ERR_BAD_OBJ_DATA);
413 p++;
414 if (!isdigit((unsigned char)*p) &&
415 !isdigit((unsigned char)*(p + 1)))
416 return got_error(GOT_ERR_BAD_OBJ_DATA);
417 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
419 p += 2;
420 if (!isdigit((unsigned char)*p) &&
421 !isdigit((unsigned char)*(p + 1)))
422 return got_error(GOT_ERR_BAD_OBJ_DATA);
423 m = ((*p - '0') * 10) + (*(p + 1) - '0');
425 *gmtoff = (h * 60 * 60 + m * 60) * sign;
426 return NULL;
429 static const struct got_error *
430 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
432 const struct got_error *err = NULL;
433 const char *errstr;
434 char *space, *tzstr;
436 /* Parse and strip off trailing timezone indicator string. */
437 space = strrchr(committer, ' ');
438 if (space == NULL)
439 return got_error(GOT_ERR_BAD_OBJ_DATA);
440 tzstr = strdup(space + 1);
441 if (tzstr == NULL)
442 return got_error_from_errno("strdup");
443 err = parse_gmtoff(gmtoff, tzstr);
444 free(tzstr);
445 if (err) {
446 if (err->code != GOT_ERR_BAD_OBJ_DATA)
447 return err;
448 /* Old versions of Git omitted the timestamp. */
449 *time = 0;
450 *gmtoff = 0;
451 return NULL;
453 *space = '\0';
455 /* Timestamp is separated from committer name + email by space. */
456 space = strrchr(committer, ' ');
457 if (space == NULL)
458 return got_error(GOT_ERR_BAD_OBJ_DATA);
460 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
461 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
462 if (errstr)
463 return got_error(GOT_ERR_BAD_OBJ_DATA);
465 /* Strip off parsed time information, leaving just author and email. */
466 *space = '\0';
468 return NULL;
471 void
472 got_object_commit_close(struct got_commit_object *commit)
474 if (commit->refcnt > 0) {
475 commit->refcnt--;
476 if (commit->refcnt > 0)
477 return;
480 got_object_id_queue_free(&commit->parent_ids);
481 free(commit->tree_id);
482 free(commit->author);
483 free(commit->committer);
484 free(commit->logmsg);
485 free(commit);
488 struct got_object_id *
489 got_object_commit_get_tree_id(struct got_commit_object *commit)
491 return commit->tree_id;
494 int
495 got_object_commit_get_nparents(struct got_commit_object *commit)
497 return commit->nparents;
500 const struct got_object_id_queue *
501 got_object_commit_get_parent_ids(struct got_commit_object *commit)
503 return &commit->parent_ids;
506 const char *
507 got_object_commit_get_author(struct got_commit_object *commit)
509 return commit->author;
512 time_t
513 got_object_commit_get_author_time(struct got_commit_object *commit)
515 return commit->author_time;
518 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
520 return commit->author_gmtoff;
523 const char *
524 got_object_commit_get_committer(struct got_commit_object *commit)
526 return commit->committer;
529 time_t
530 got_object_commit_get_committer_time(struct got_commit_object *commit)
532 return commit->committer_time;
535 time_t
536 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
538 return commit->committer_gmtoff;
541 const struct got_error *
542 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
544 const struct got_error *err = NULL;
545 const char *src;
546 char *dst;
547 size_t len;
549 len = strlen(commit->logmsg);
550 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
551 if (*logmsg == NULL)
552 return got_error_from_errno("malloc");
554 /*
555 * Strip out unusual headers. Headers are separated from the commit
556 * message body by a single empty line.
557 */
558 src = commit->logmsg;
559 dst = *logmsg;
560 while (*src != '\0' && *src != '\n') {
561 int copy_header = 1, eol = 0;
562 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
563 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
564 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
565 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
566 strncmp(src, GOT_COMMIT_LABEL_PARENT,
567 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
568 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
569 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
570 copy_header = 0;
572 while (*src != '\0' && !eol) {
573 if (copy_header) {
574 *dst = *src;
575 dst++;
577 if (*src == '\n')
578 eol = 1;
579 src++;
582 *dst = '\0';
584 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
585 err = got_error(GOT_ERR_NO_SPACE);
586 goto done;
589 /* Trim redundant trailing whitespace. */
590 len = strlen(*logmsg);
591 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
592 isspace((unsigned char)(*logmsg)[len - 1])) {
593 (*logmsg)[len - 1] = '\0';
594 len--;
597 /* Append a trailing newline if missing. */
598 if (len > 0 && (*logmsg)[len - 1] != '\n') {
599 (*logmsg)[len] = '\n';
600 (*logmsg)[len + 1] = '\0';
602 done:
603 if (err) {
604 free(*logmsg);
605 *logmsg = NULL;
607 return err;
610 const char *
611 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
613 return commit->logmsg;
616 const struct got_error *
617 got_object_parse_commit(struct got_commit_object **commit, char *buf,
618 size_t len)
620 const struct got_error *err = NULL;
621 char *s = buf;
622 size_t label_len;
623 ssize_t remain = (ssize_t)len;
625 if (remain == 0)
626 return got_error(GOT_ERR_BAD_OBJ_DATA);
628 *commit = got_object_commit_alloc_partial();
629 if (*commit == NULL)
630 return got_error_from_errno("got_object_commit_alloc_partial");
632 label_len = strlen(GOT_COMMIT_LABEL_TREE);
633 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
634 remain -= label_len;
635 if (remain < SHA1_DIGEST_STRING_LENGTH) {
636 err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 goto done;
639 s += label_len;
640 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
641 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 goto done;
644 remain -= SHA1_DIGEST_STRING_LENGTH;
645 s += SHA1_DIGEST_STRING_LENGTH;
646 } else {
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
648 goto done;
651 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
652 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
653 remain -= label_len;
654 if (remain < SHA1_DIGEST_STRING_LENGTH) {
655 err = got_error(GOT_ERR_BAD_OBJ_DATA);
656 goto done;
658 s += label_len;
659 err = got_object_commit_add_parent(*commit, s);
660 if (err)
661 goto done;
663 remain -= SHA1_DIGEST_STRING_LENGTH;
664 s += SHA1_DIGEST_STRING_LENGTH;
667 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
668 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
669 char *p;
670 size_t slen;
672 remain -= label_len;
673 if (remain <= 0) {
674 err = got_error(GOT_ERR_BAD_OBJ_DATA);
675 goto done;
677 s += label_len;
678 p = memchr(s, '\n', remain);
679 if (p == NULL) {
680 err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 goto done;
683 *p = '\0';
684 slen = strlen(s);
685 err = parse_commit_time(&(*commit)->author_time,
686 &(*commit)->author_gmtoff, s);
687 if (err)
688 goto done;
689 (*commit)->author = strdup(s);
690 if ((*commit)->author == NULL) {
691 err = got_error_from_errno("strdup");
692 goto done;
694 s += slen + 1;
695 remain -= slen + 1;
698 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
699 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
700 char *p;
701 size_t slen;
703 remain -= label_len;
704 if (remain <= 0) {
705 err = got_error(GOT_ERR_BAD_OBJ_DATA);
706 goto done;
708 s += label_len;
709 p = memchr(s, '\n', remain);
710 if (p == NULL) {
711 err = got_error(GOT_ERR_BAD_OBJ_DATA);
712 goto done;
714 *p = '\0';
715 slen = strlen(s);
716 err = parse_commit_time(&(*commit)->committer_time,
717 &(*commit)->committer_gmtoff, s);
718 if (err)
719 goto done;
720 (*commit)->committer = strdup(s);
721 if ((*commit)->committer == NULL) {
722 err = got_error_from_errno("strdup");
723 goto done;
725 s += slen + 1;
726 remain -= slen + 1;
729 (*commit)->logmsg = strndup(s, remain);
730 if ((*commit)->logmsg == NULL) {
731 err = got_error_from_errno("strndup");
732 goto done;
734 done:
735 if (err) {
736 got_object_commit_close(*commit);
737 *commit = NULL;
739 return err;
742 const struct got_error *
743 got_object_read_commit(struct got_commit_object **commit, int fd,
744 struct got_object_id *expected_id, size_t expected_size)
746 struct got_object *obj = NULL;
747 const struct got_error *err = NULL;
748 size_t len;
749 uint8_t *p;
750 struct got_inflate_checksum csum;
751 SHA1_CTX sha1_ctx;
752 struct got_object_id id;
754 SHA1Init(&sha1_ctx);
755 memset(&csum, 0, sizeof(csum));
756 csum.output_sha1 = &sha1_ctx;
758 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
759 if (err)
760 return err;
762 SHA1Final(id.sha1, &sha1_ctx);
763 if (got_object_id_cmp(expected_id, &id) != 0) {
764 err = got_error_checksum(expected_id);
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 (got_object_id_cmp(expected_id, &id) != 0) {
933 err = got_error_checksum(expected_id);
934 goto done;
937 err = got_object_parse_header(&obj, *p, len);
938 if (err)
939 goto done;
941 if (len < obj->hdrlen + obj->size) {
942 err = got_error(GOT_ERR_BAD_OBJ_DATA);
943 goto done;
946 /* Skip object header. */
947 len -= obj->hdrlen;
948 err = got_object_parse_tree(entries, nentries, nentries_alloc,
949 *p + obj->hdrlen, len);
950 done:
951 if (obj)
952 got_object_close(obj);
953 return err;
956 void
957 got_object_tag_close(struct got_tag_object *tag)
959 if (tag->refcnt > 0) {
960 tag->refcnt--;
961 if (tag->refcnt > 0)
962 return;
965 free(tag->tag);
966 free(tag->tagger);
967 free(tag->tagmsg);
968 free(tag);
971 const struct got_error *
972 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
974 const struct got_error *err = NULL;
975 size_t remain = len;
976 char *s = buf;
977 size_t label_len;
979 if (remain == 0)
980 return got_error(GOT_ERR_BAD_OBJ_DATA);
982 *tag = calloc(1, sizeof(**tag));
983 if (*tag == NULL)
984 return got_error_from_errno("calloc");
986 label_len = strlen(GOT_TAG_LABEL_OBJECT);
987 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
988 remain -= label_len;
989 if (remain < SHA1_DIGEST_STRING_LENGTH) {
990 err = got_error(GOT_ERR_BAD_OBJ_DATA);
991 goto done;
993 s += label_len;
994 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
995 err = got_error(GOT_ERR_BAD_OBJ_DATA);
996 goto done;
998 remain -= SHA1_DIGEST_STRING_LENGTH;
999 s += SHA1_DIGEST_STRING_LENGTH;
1000 } else {
1001 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1002 goto done;
1005 if (remain <= 0) {
1006 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1007 goto done;
1010 label_len = strlen(GOT_TAG_LABEL_TYPE);
1011 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1012 remain -= label_len;
1013 if (remain <= 0) {
1014 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1015 goto done;
1017 s += label_len;
1018 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1019 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1020 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1021 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1022 s += label_len;
1023 remain -= label_len;
1024 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1025 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1026 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1027 label_len = strlen(GOT_OBJ_LABEL_TREE);
1028 s += label_len;
1029 remain -= label_len;
1030 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1031 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1032 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1033 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1034 s += label_len;
1035 remain -= label_len;
1036 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1037 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1038 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1039 label_len = strlen(GOT_OBJ_LABEL_TAG);
1040 s += label_len;
1041 remain -= label_len;
1042 } else {
1043 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1044 goto done;
1047 if (remain <= 0 || *s != '\n') {
1048 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1049 goto done;
1051 s++;
1052 remain--;
1053 if (remain <= 0) {
1054 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1055 goto done;
1057 } else {
1058 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1059 goto done;
1062 label_len = strlen(GOT_TAG_LABEL_TAG);
1063 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1064 char *p;
1065 size_t slen;
1066 remain -= label_len;
1067 if (remain <= 0) {
1068 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1069 goto done;
1071 s += label_len;
1072 p = memchr(s, '\n', remain);
1073 if (p == NULL) {
1074 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1075 goto done;
1077 *p = '\0';
1078 slen = strlen(s);
1079 (*tag)->tag = strndup(s, slen);
1080 if ((*tag)->tag == NULL) {
1081 err = got_error_from_errno("strndup");
1082 goto done;
1084 s += slen + 1;
1085 remain -= slen + 1;
1086 if (remain <= 0) {
1087 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1088 goto done;
1090 } else {
1091 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1092 goto done;
1095 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1096 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1097 char *p;
1098 size_t slen;
1100 remain -= label_len;
1101 if (remain <= 0) {
1102 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1103 goto done;
1105 s += label_len;
1106 p = memchr(s, '\n', remain);
1107 if (p == NULL) {
1108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1109 goto done;
1111 *p = '\0';
1112 slen = strlen(s);
1113 err = parse_commit_time(&(*tag)->tagger_time,
1114 &(*tag)->tagger_gmtoff, s);
1115 if (err)
1116 goto done;
1117 (*tag)->tagger = strdup(s);
1118 if ((*tag)->tagger == NULL) {
1119 err = got_error_from_errno("strdup");
1120 goto done;
1122 s += slen + 1;
1123 remain -= slen + 1;
1124 if (remain < 0) {
1125 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1126 goto done;
1128 } else {
1129 /* Some old tags in the Linux git repo have no tagger. */
1130 (*tag)->tagger = strdup("");
1131 if ((*tag)->tagger == NULL) {
1132 err = got_error_from_errno("strdup");
1133 goto done;
1137 (*tag)->tagmsg = strndup(s, remain);
1138 if ((*tag)->tagmsg == NULL) {
1139 err = got_error_from_errno("strndup");
1140 goto done;
1142 done:
1143 if (err) {
1144 got_object_tag_close(*tag);
1145 *tag = NULL;
1147 return err;
1150 const struct got_error *
1151 got_object_read_tag(struct got_tag_object **tag, int fd,
1152 struct got_object_id *expected_id, size_t expected_size)
1154 const struct got_error *err = NULL;
1155 struct got_object *obj = NULL;
1156 size_t len;
1157 uint8_t *p;
1158 struct got_inflate_checksum csum;
1159 SHA1_CTX sha1_ctx;
1160 struct got_object_id id;
1162 SHA1Init(&sha1_ctx);
1163 memset(&csum, 0, sizeof(csum));
1164 csum.output_sha1 = &sha1_ctx;
1166 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1167 expected_size, fd);
1168 if (err)
1169 return err;
1171 SHA1Final(id.sha1, &sha1_ctx);
1172 if (got_object_id_cmp(expected_id, &id) != 0) {
1173 err = got_error_checksum(expected_id);
1174 goto done;
1177 err = got_object_parse_header(&obj, p, len);
1178 if (err)
1179 goto done;
1181 if (len < obj->hdrlen + obj->size) {
1182 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1183 goto done;
1186 /* Skip object header. */
1187 len -= obj->hdrlen;
1188 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1189 done:
1190 free(p);
1191 if (obj)
1192 got_object_close(obj);
1193 return err;
1196 const struct got_error *
1197 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1199 const struct got_error *err = NULL;
1200 static const size_t blocksize = 512;
1201 size_t n, total, remain;
1202 uint8_t *buf;
1204 *outbuf = NULL;
1205 *outlen = 0;
1207 buf = malloc(blocksize);
1208 if (buf == NULL)
1209 return got_error_from_errno("malloc");
1211 remain = blocksize;
1212 total = 0;
1213 for (;;) {
1214 if (remain == 0) {
1215 uint8_t *newbuf;
1216 newbuf = reallocarray(buf, 1, total + blocksize);
1217 if (newbuf == NULL) {
1218 err = got_error_from_errno("reallocarray");
1219 goto done;
1221 buf = newbuf;
1222 remain += blocksize;
1224 n = fread(buf + total, 1, remain, f);
1225 if (n == 0) {
1226 if (ferror(f)) {
1227 err = got_ferror(f, GOT_ERR_IO);
1228 goto done;
1230 break; /* EOF */
1232 remain -= n;
1233 total += n;
1236 done:
1237 if (err == NULL) {
1238 *outbuf = buf;
1239 *outlen = total;
1240 } else
1241 free(buf);
1242 return err;