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((unsigned char)*p) &&
414 !isdigit((unsigned char)*(p + 1)))
415 return got_error(GOT_ERR_BAD_OBJ_DATA);
416 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
418 p += 2;
419 if (!isdigit((unsigned char)*p) &&
420 !isdigit((unsigned char)*(p + 1)))
421 return got_error(GOT_ERR_BAD_OBJ_DATA);
422 m = ((*p - '0') * 10) + (*(p + 1) - '0');
424 *gmtoff = (h * 60 * 60 + m * 60) * sign;
425 return NULL;
428 static const struct got_error *
429 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
431 const struct got_error *err = NULL;
432 const char *errstr;
433 char *space, *tzstr;
435 /* Parse and strip off trailing timezone indicator string. */
436 space = strrchr(committer, ' ');
437 if (space == NULL)
438 return got_error(GOT_ERR_BAD_OBJ_DATA);
439 tzstr = strdup(space + 1);
440 if (tzstr == NULL)
441 return got_error_from_errno("strdup");
442 err = parse_gmtoff(gmtoff, tzstr);
443 free(tzstr);
444 if (err) {
445 if (err->code != GOT_ERR_BAD_OBJ_DATA)
446 return err;
447 /* Old versions of Git omitted the timestamp. */
448 *time = 0;
449 *gmtoff = 0;
450 return NULL;
452 *space = '\0';
454 /* Timestamp is separated from committer name + email by space. */
455 space = strrchr(committer, ' ');
456 if (space == NULL)
457 return got_error(GOT_ERR_BAD_OBJ_DATA);
459 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
460 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
461 if (errstr)
462 return got_error(GOT_ERR_BAD_OBJ_DATA);
464 /* Strip off parsed time information, leaving just author and email. */
465 *space = '\0';
467 return NULL;
470 void
471 got_object_commit_close(struct got_commit_object *commit)
473 if (commit->refcnt > 0) {
474 commit->refcnt--;
475 if (commit->refcnt > 0)
476 return;
479 got_object_id_queue_free(&commit->parent_ids);
480 free(commit->tree_id);
481 free(commit->author);
482 free(commit->committer);
483 free(commit->logmsg);
484 free(commit);
487 struct got_object_id *
488 got_object_commit_get_tree_id(struct got_commit_object *commit)
490 return commit->tree_id;
493 int
494 got_object_commit_get_nparents(struct got_commit_object *commit)
496 return commit->nparents;
499 const struct got_object_id_queue *
500 got_object_commit_get_parent_ids(struct got_commit_object *commit)
502 return &commit->parent_ids;
505 const char *
506 got_object_commit_get_author(struct got_commit_object *commit)
508 return commit->author;
511 time_t
512 got_object_commit_get_author_time(struct got_commit_object *commit)
514 return commit->author_time;
517 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
519 return commit->author_gmtoff;
522 const char *
523 got_object_commit_get_committer(struct got_commit_object *commit)
525 return commit->committer;
528 time_t
529 got_object_commit_get_committer_time(struct got_commit_object *commit)
531 return commit->committer_time;
534 time_t
535 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
537 return commit->committer_gmtoff;
540 const struct got_error *
541 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
543 const struct got_error *err = NULL;
544 const char *src;
545 char *dst;
546 size_t len;
548 len = strlen(commit->logmsg);
549 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
550 if (*logmsg == NULL)
551 return got_error_from_errno("malloc");
553 /*
554 * Strip out unusual headers. Headers are separated from the commit
555 * message body by a single empty line.
556 */
557 src = commit->logmsg;
558 dst = *logmsg;
559 while (*src != '\0' && *src != '\n') {
560 int copy_header = 1, eol = 0;
561 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
562 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
563 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
564 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
565 strncmp(src, GOT_COMMIT_LABEL_PARENT,
566 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
567 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
568 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
569 copy_header = 0;
571 while (*src != '\0' && !eol) {
572 if (copy_header) {
573 *dst = *src;
574 dst++;
576 if (*src == '\n')
577 eol = 1;
578 src++;
581 *dst = '\0';
583 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
584 err = got_error(GOT_ERR_NO_SPACE);
585 goto done;
588 /* Trim redundant trailing whitespace. */
589 len = strlen(*logmsg);
590 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
591 isspace((unsigned char)(*logmsg)[len - 1])) {
592 (*logmsg)[len - 1] = '\0';
593 len--;
596 /* Append a trailing newline if missing. */
597 if (len > 0 && (*logmsg)[len - 1] != '\n') {
598 (*logmsg)[len] = '\n';
599 (*logmsg)[len + 1] = '\0';
601 done:
602 if (err) {
603 free(*logmsg);
604 *logmsg = NULL;
606 return err;
609 const char *
610 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
612 return commit->logmsg;
615 const struct got_error *
616 got_object_parse_commit(struct got_commit_object **commit, char *buf,
617 size_t len)
619 const struct got_error *err = NULL;
620 char *s = buf;
621 size_t label_len;
622 ssize_t remain = (ssize_t)len;
624 if (remain == 0)
625 return got_error(GOT_ERR_BAD_OBJ_DATA);
627 *commit = got_object_commit_alloc_partial();
628 if (*commit == NULL)
629 return got_error_from_errno("got_object_commit_alloc_partial");
631 label_len = strlen(GOT_COMMIT_LABEL_TREE);
632 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
633 remain -= label_len;
634 if (remain < SHA1_DIGEST_STRING_LENGTH) {
635 err = got_error(GOT_ERR_BAD_OBJ_DATA);
636 goto done;
638 s += label_len;
639 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
640 err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 goto done;
643 remain -= SHA1_DIGEST_STRING_LENGTH;
644 s += SHA1_DIGEST_STRING_LENGTH;
645 } else {
646 err = got_error(GOT_ERR_BAD_OBJ_DATA);
647 goto done;
650 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
651 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
652 remain -= label_len;
653 if (remain < SHA1_DIGEST_STRING_LENGTH) {
654 err = got_error(GOT_ERR_BAD_OBJ_DATA);
655 goto done;
657 s += label_len;
658 err = got_object_commit_add_parent(*commit, s);
659 if (err)
660 goto done;
662 remain -= SHA1_DIGEST_STRING_LENGTH;
663 s += SHA1_DIGEST_STRING_LENGTH;
666 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
667 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
668 char *p;
669 size_t slen;
671 remain -= label_len;
672 if (remain <= 0) {
673 err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 goto done;
676 s += label_len;
677 p = memchr(s, '\n', remain);
678 if (p == NULL) {
679 err = got_error(GOT_ERR_BAD_OBJ_DATA);
680 goto done;
682 *p = '\0';
683 slen = strlen(s);
684 err = parse_commit_time(&(*commit)->author_time,
685 &(*commit)->author_gmtoff, s);
686 if (err)
687 goto done;
688 (*commit)->author = strdup(s);
689 if ((*commit)->author == NULL) {
690 err = got_error_from_errno("strdup");
691 goto done;
693 s += slen + 1;
694 remain -= slen + 1;
697 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
698 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
699 char *p;
700 size_t slen;
702 remain -= label_len;
703 if (remain <= 0) {
704 err = got_error(GOT_ERR_BAD_OBJ_DATA);
705 goto done;
707 s += label_len;
708 p = memchr(s, '\n', remain);
709 if (p == NULL) {
710 err = got_error(GOT_ERR_BAD_OBJ_DATA);
711 goto done;
713 *p = '\0';
714 slen = strlen(s);
715 err = parse_commit_time(&(*commit)->committer_time,
716 &(*commit)->committer_gmtoff, s);
717 if (err)
718 goto done;
719 (*commit)->committer = strdup(s);
720 if ((*commit)->committer == NULL) {
721 err = got_error_from_errno("strdup");
722 goto done;
724 s += slen + 1;
725 remain -= slen + 1;
728 (*commit)->logmsg = strndup(s, remain);
729 if ((*commit)->logmsg == NULL) {
730 err = got_error_from_errno("strndup");
731 goto done;
733 done:
734 if (err) {
735 got_object_commit_close(*commit);
736 *commit = NULL;
738 return err;
741 const struct got_error *
742 got_object_read_commit(struct got_commit_object **commit, int fd,
743 struct got_object_id *expected_id, size_t expected_size)
745 struct got_object *obj = NULL;
746 const struct got_error *err = NULL;
747 size_t len;
748 uint8_t *p;
749 struct got_inflate_checksum csum;
750 SHA1_CTX sha1_ctx;
751 struct got_object_id id;
753 SHA1Init(&sha1_ctx);
754 memset(&csum, 0, sizeof(csum));
755 csum.output_sha1 = &sha1_ctx;
757 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
758 if (err)
759 return err;
761 SHA1Final(id.sha1, &sha1_ctx);
762 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
763 char buf[SHA1_DIGEST_STRING_LENGTH];
764 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
765 "checksum failure for object %s",
766 got_sha1_digest_to_str(expected_id->sha1, buf,
767 sizeof(buf)));
768 goto done;
771 err = got_object_parse_header(&obj, p, len);
772 if (err)
773 goto done;
775 if (len < obj->hdrlen + obj->size) {
776 err = got_error(GOT_ERR_BAD_OBJ_DATA);
777 goto done;
780 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
781 err = got_error(GOT_ERR_OBJ_TYPE);
782 goto done;
785 /* Skip object header. */
786 len -= obj->hdrlen;
787 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
788 done:
789 free(p);
790 if (obj)
791 got_object_close(obj);
792 return err;
795 void
796 got_object_tree_close(struct got_tree_object *tree)
798 if (tree->refcnt > 0) {
799 tree->refcnt--;
800 if (tree->refcnt > 0)
801 return;
804 free(tree->entries);
805 free(tree);
808 static const struct got_error *
809 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
810 size_t maxlen)
812 char *p, *space;
814 *elen = 0;
816 *elen = strnlen(buf, maxlen) + 1;
817 if (*elen > maxlen)
818 return got_error(GOT_ERR_BAD_OBJ_DATA);
820 space = memchr(buf, ' ', *elen);
821 if (space == NULL || space <= buf)
822 return got_error(GOT_ERR_BAD_OBJ_DATA);
824 pte->mode = 0;
825 p = buf;
826 while (p < space) {
827 if (*p < '0' || *p > '7')
828 return got_error(GOT_ERR_BAD_OBJ_DATA);
829 pte->mode <<= 3;
830 pte->mode |= *p - '0';
831 p++;
834 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
835 return got_error(GOT_ERR_BAD_OBJ_DATA);
837 pte->name = space + 1;
838 pte->namelen = strlen(pte->name);
839 buf += *elen;
840 pte->id = buf;
841 *elen += SHA1_DIGEST_LENGTH;
842 return NULL;
845 static int
846 pte_cmp(const void *pa, const void *pb)
848 const struct got_parsed_tree_entry *a = pa, *b = pb;
850 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
853 const struct got_error *
854 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
855 size_t *nentries_alloc, uint8_t *buf, size_t len)
857 const struct got_error *err = NULL;
858 size_t remain = len;
859 const size_t nalloc = 16;
860 struct got_parsed_tree_entry *pte;
861 int i;
863 *nentries = 0;
864 if (remain == 0)
865 return NULL; /* tree is empty */
867 while (remain > 0) {
868 size_t elen;
870 if (*nentries >= *nentries_alloc) {
871 pte = recallocarray(*entries, *nentries_alloc,
872 *nentries_alloc + nalloc, sizeof(**entries));
873 if (pte == NULL) {
874 err = got_error_from_errno("recallocarray");
875 goto done;
877 *entries = pte;
878 *nentries_alloc += nalloc;
881 pte = &(*entries)[*nentries];
882 err = parse_tree_entry(pte, &elen, buf, remain);
883 if (err)
884 goto done;
885 buf += elen;
886 remain -= elen;
887 (*nentries)++;
890 if (remain != 0) {
891 err = got_error(GOT_ERR_BAD_OBJ_DATA);
892 goto done;
895 if (*nentries > 1) {
896 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
898 for (i = 0; i < *nentries - 1; i++) {
899 struct got_parsed_tree_entry *prev = &(*entries)[i];
900 pte = &(*entries)[i + 1];
901 if (got_path_cmp(prev->name, pte->name,
902 prev->namelen, pte->namelen) == 0) {
903 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
904 break;
908 done:
909 if (err)
910 *nentries = 0;
911 return err;
914 const struct got_error *
915 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
916 size_t *nentries_alloc, uint8_t **p, int fd,
917 struct got_object_id *expected_id)
919 const struct got_error *err = NULL;
920 struct got_object *obj = NULL;
921 size_t len;
922 struct got_inflate_checksum csum;
923 SHA1_CTX sha1_ctx;
924 struct got_object_id id;
926 SHA1Init(&sha1_ctx);
927 memset(&csum, 0, sizeof(csum));
928 csum.output_sha1 = &sha1_ctx;
930 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
931 if (err)
932 return err;
934 SHA1Final(id.sha1, &sha1_ctx);
935 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
936 char buf[SHA1_DIGEST_STRING_LENGTH];
937 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
938 "checksum failure for object %s",
939 got_sha1_digest_to_str(expected_id->sha1, buf,
940 sizeof(buf)));
941 goto done;
944 err = got_object_parse_header(&obj, *p, len);
945 if (err)
946 goto done;
948 if (len < obj->hdrlen + obj->size) {
949 err = got_error(GOT_ERR_BAD_OBJ_DATA);
950 goto done;
953 /* Skip object header. */
954 len -= obj->hdrlen;
955 err = got_object_parse_tree(entries, nentries, nentries_alloc,
956 *p + obj->hdrlen, len);
957 done:
958 if (obj)
959 got_object_close(obj);
960 return err;
963 void
964 got_object_tag_close(struct got_tag_object *tag)
966 if (tag->refcnt > 0) {
967 tag->refcnt--;
968 if (tag->refcnt > 0)
969 return;
972 free(tag->tag);
973 free(tag->tagger);
974 free(tag->tagmsg);
975 free(tag);
978 const struct got_error *
979 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
981 const struct got_error *err = NULL;
982 size_t remain = len;
983 char *s = buf;
984 size_t label_len;
986 if (remain == 0)
987 return got_error(GOT_ERR_BAD_OBJ_DATA);
989 *tag = calloc(1, sizeof(**tag));
990 if (*tag == NULL)
991 return got_error_from_errno("calloc");
993 label_len = strlen(GOT_TAG_LABEL_OBJECT);
994 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
995 remain -= label_len;
996 if (remain < SHA1_DIGEST_STRING_LENGTH) {
997 err = got_error(GOT_ERR_BAD_OBJ_DATA);
998 goto done;
1000 s += label_len;
1001 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
1002 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1003 goto done;
1005 remain -= SHA1_DIGEST_STRING_LENGTH;
1006 s += SHA1_DIGEST_STRING_LENGTH;
1007 } else {
1008 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1009 goto done;
1012 if (remain <= 0) {
1013 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1014 goto done;
1017 label_len = strlen(GOT_TAG_LABEL_TYPE);
1018 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1019 remain -= label_len;
1020 if (remain <= 0) {
1021 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1022 goto done;
1024 s += label_len;
1025 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1026 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1027 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1028 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1029 s += label_len;
1030 remain -= label_len;
1031 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1032 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1033 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1034 label_len = strlen(GOT_OBJ_LABEL_TREE);
1035 s += label_len;
1036 remain -= label_len;
1037 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1038 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1039 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1040 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1041 s += label_len;
1042 remain -= label_len;
1043 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1044 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1045 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1046 label_len = strlen(GOT_OBJ_LABEL_TAG);
1047 s += label_len;
1048 remain -= label_len;
1049 } else {
1050 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 goto done;
1054 if (remain <= 0 || *s != '\n') {
1055 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1056 goto done;
1058 s++;
1059 remain--;
1060 if (remain <= 0) {
1061 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 goto done;
1064 } else {
1065 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1066 goto done;
1069 label_len = strlen(GOT_TAG_LABEL_TAG);
1070 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1071 char *p;
1072 size_t slen;
1073 remain -= label_len;
1074 if (remain <= 0) {
1075 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1076 goto done;
1078 s += label_len;
1079 p = memchr(s, '\n', remain);
1080 if (p == NULL) {
1081 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1082 goto done;
1084 *p = '\0';
1085 slen = strlen(s);
1086 (*tag)->tag = strndup(s, slen);
1087 if ((*tag)->tag == NULL) {
1088 err = got_error_from_errno("strndup");
1089 goto done;
1091 s += slen + 1;
1092 remain -= slen + 1;
1093 if (remain <= 0) {
1094 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1095 goto done;
1097 } else {
1098 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1099 goto done;
1102 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1103 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1104 char *p;
1105 size_t slen;
1107 remain -= label_len;
1108 if (remain <= 0) {
1109 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1110 goto done;
1112 s += label_len;
1113 p = memchr(s, '\n', remain);
1114 if (p == NULL) {
1115 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1116 goto done;
1118 *p = '\0';
1119 slen = strlen(s);
1120 err = parse_commit_time(&(*tag)->tagger_time,
1121 &(*tag)->tagger_gmtoff, s);
1122 if (err)
1123 goto done;
1124 (*tag)->tagger = strdup(s);
1125 if ((*tag)->tagger == NULL) {
1126 err = got_error_from_errno("strdup");
1127 goto done;
1129 s += slen + 1;
1130 remain -= slen + 1;
1131 if (remain < 0) {
1132 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1133 goto done;
1135 } else {
1136 /* Some old tags in the Linux git repo have no tagger. */
1137 (*tag)->tagger = strdup("");
1138 if ((*tag)->tagger == NULL) {
1139 err = got_error_from_errno("strdup");
1140 goto done;
1144 (*tag)->tagmsg = strndup(s, remain);
1145 if ((*tag)->tagmsg == NULL) {
1146 err = got_error_from_errno("strndup");
1147 goto done;
1149 done:
1150 if (err) {
1151 got_object_tag_close(*tag);
1152 *tag = NULL;
1154 return err;
1157 const struct got_error *
1158 got_object_read_tag(struct got_tag_object **tag, int fd,
1159 struct got_object_id *expected_id, size_t expected_size)
1161 const struct got_error *err = NULL;
1162 struct got_object *obj = NULL;
1163 size_t len;
1164 uint8_t *p;
1165 struct got_inflate_checksum csum;
1166 SHA1_CTX sha1_ctx;
1167 struct got_object_id id;
1169 SHA1Init(&sha1_ctx);
1170 memset(&csum, 0, sizeof(csum));
1171 csum.output_sha1 = &sha1_ctx;
1173 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1174 expected_size, fd);
1175 if (err)
1176 return err;
1178 SHA1Final(id.sha1, &sha1_ctx);
1179 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
1180 char buf[SHA1_DIGEST_STRING_LENGTH];
1181 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1182 "checksum failure for object %s",
1183 got_sha1_digest_to_str(expected_id->sha1, buf,
1184 sizeof(buf)));
1185 goto done;
1188 err = got_object_parse_header(&obj, p, len);
1189 if (err)
1190 goto done;
1192 if (len < obj->hdrlen + obj->size) {
1193 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1194 goto done;
1197 /* Skip object header. */
1198 len -= obj->hdrlen;
1199 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1200 done:
1201 free(p);
1202 if (obj)
1203 got_object_close(obj);
1204 return err;
1207 const struct got_error *
1208 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1210 const struct got_error *err = NULL;
1211 static const size_t blocksize = 512;
1212 size_t n, total, remain;
1213 uint8_t *buf;
1215 *outbuf = NULL;
1216 *outlen = 0;
1218 buf = malloc(blocksize);
1219 if (buf == NULL)
1220 return got_error_from_errno("malloc");
1222 remain = blocksize;
1223 total = 0;
1224 for (;;) {
1225 if (remain == 0) {
1226 uint8_t *newbuf;
1227 newbuf = reallocarray(buf, 1, total + blocksize);
1228 if (newbuf == NULL) {
1229 err = got_error_from_errno("reallocarray");
1230 goto done;
1232 buf = newbuf;
1233 remain += blocksize;
1235 n = fread(buf + total, 1, remain, f);
1236 if (n == 0) {
1237 if (ferror(f)) {
1238 err = got_ferror(f, GOT_ERR_IO);
1239 goto done;
1241 break; /* EOF */
1243 remain -= n;
1244 total += n;
1247 done:
1248 if (err == NULL) {
1249 *outbuf = buf;
1250 *outlen = total;
1251 } else
1252 free(buf);
1253 return err;