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 <sha2.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <imsg.h>
37 #include <time.h>
38 #include <unistd.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_hash.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_repository.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #endif
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
61 {
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
65 if (id2 == NULL)
66 return NULL;
67 memcpy(id2, id1, sizeof(*id2));
68 return id2;
69 }
71 int
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
74 {
75 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
76 }
78 const struct got_error *
79 got_object_qid_alloc_partial(struct got_object_qid **qid)
80 {
81 *qid = malloc(sizeof(**qid));
82 if (*qid == NULL)
83 return got_error_from_errno("malloc");
85 (*qid)->data = NULL;
86 return NULL;
87 }
89 const struct got_error *
90 got_object_id_str(char **outbuf, struct got_object_id *id)
91 {
92 static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN;
94 *outbuf = malloc(len);
95 if (*outbuf == NULL)
96 return got_error_from_errno("malloc");
98 if (got_object_id_hex(id, *outbuf, len) == NULL) {
99 free(*outbuf);
100 *outbuf = NULL;
101 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
104 return NULL;
107 char *
108 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
110 return got_sha1_digest_to_str(id->sha1, buf, len);
113 const struct got_error *
114 got_object_type_label(const char **label, int obj_type)
116 const struct got_error *err = NULL;
118 switch (obj_type) {
119 case GOT_OBJ_TYPE_BLOB:
120 *label = GOT_OBJ_LABEL_BLOB;
121 break;
122 case GOT_OBJ_TYPE_TREE:
123 *label = GOT_OBJ_LABEL_TREE;
124 break;
125 case GOT_OBJ_TYPE_COMMIT:
126 *label = GOT_OBJ_LABEL_COMMIT;
127 break;
128 case GOT_OBJ_TYPE_TAG:
129 *label = GOT_OBJ_LABEL_TAG;
130 break;
131 default:
132 *label = NULL;
133 err = got_error(GOT_ERR_OBJ_TYPE);
134 break;
137 return err;
140 void
141 got_object_close(struct got_object *obj)
143 if (obj->refcnt > 0) {
144 obj->refcnt--;
145 if (obj->refcnt > 0)
146 return;
149 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
150 struct got_delta *delta;
151 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
152 delta = STAILQ_FIRST(&obj->deltas.entries);
153 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
154 free(delta);
157 free(obj);
160 const struct got_error *
161 got_object_raw_close(struct got_raw_object *obj)
163 const struct got_error *err = NULL;
165 if (obj->refcnt > 0) {
166 obj->refcnt--;
167 if (obj->refcnt > 0)
168 return NULL;
171 if (obj->close_cb)
172 obj->close_cb(obj);
174 if (obj->f == NULL) {
175 if (obj->fd != -1) {
176 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
177 err = got_error_from_errno("munmap");
178 if (close(obj->fd) == -1 && err == NULL)
179 err = got_error_from_errno("close");
180 } else
181 free(obj->data);
182 } else {
183 if (fclose(obj->f) == EOF && err == NULL)
184 err = got_error_from_errno("fclose");
186 free(obj);
187 return err;
190 void
191 got_object_qid_free(struct got_object_qid *qid)
193 free(qid);
196 void
197 got_object_id_queue_free(struct got_object_id_queue *ids)
199 struct got_object_qid *qid;
201 while (!STAILQ_EMPTY(ids)) {
202 qid = STAILQ_FIRST(ids);
203 STAILQ_REMOVE_HEAD(ids, entry);
204 got_object_qid_free(qid);
208 const struct got_error *
209 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
211 const char *obj_labels[] = {
212 GOT_OBJ_LABEL_COMMIT,
213 GOT_OBJ_LABEL_TREE,
214 GOT_OBJ_LABEL_BLOB,
215 GOT_OBJ_LABEL_TAG,
216 };
217 const int obj_types[] = {
218 GOT_OBJ_TYPE_COMMIT,
219 GOT_OBJ_TYPE_TREE,
220 GOT_OBJ_TYPE_BLOB,
221 GOT_OBJ_TYPE_TAG,
222 };
223 int type = 0;
224 size_t size = 0;
225 size_t i;
226 char *end;
228 *obj = NULL;
230 end = memchr(buf, '\0', len);
231 if (end == NULL)
232 return got_error(GOT_ERR_BAD_OBJ_HDR);
234 for (i = 0; i < nitems(obj_labels); i++) {
235 const char *label = obj_labels[i];
236 size_t label_len = strlen(label);
237 const char *errstr;
239 if (len <= label_len || buf + label_len >= end ||
240 strncmp(buf, label, label_len) != 0)
241 continue;
243 type = obj_types[i];
244 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
245 if (errstr != NULL)
246 return got_error(GOT_ERR_BAD_OBJ_HDR);
247 break;
250 if (type == 0)
251 return got_error(GOT_ERR_BAD_OBJ_HDR);
253 *obj = calloc(1, sizeof(**obj));
254 if (*obj == NULL)
255 return got_error_from_errno("calloc");
256 (*obj)->type = type;
257 (*obj)->hdrlen = end - buf + 1;
258 (*obj)->size = size;
259 return NULL;
262 const struct got_error *
263 got_object_read_header(struct got_object **obj, int fd)
265 const struct got_error *err;
266 struct got_inflate_buf zb;
267 uint8_t *buf;
268 const size_t zbsize = 64;
269 size_t outlen, totlen;
270 int nbuf = 1;
272 *obj = NULL;
274 buf = malloc(zbsize);
275 if (buf == NULL)
276 return got_error_from_errno("malloc");
277 buf[0] = '\0';
279 err = got_inflate_init(&zb, buf, zbsize, NULL);
280 if (err)
281 return err;
283 totlen = 0;
284 do {
285 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
286 if (err)
287 goto done;
288 if (outlen == 0)
289 break;
290 totlen += outlen;
291 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
292 uint8_t *newbuf;
293 nbuf++;
294 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
295 if (newbuf == NULL) {
296 err = got_error_from_errno("recallocarray");
297 goto done;
299 buf = newbuf;
300 zb.outbuf = newbuf + totlen;
301 zb.outlen = (nbuf * zbsize) - totlen;
303 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
305 err = got_object_parse_header(obj, buf, totlen);
306 done:
307 free(buf);
308 got_inflate_end(&zb);
309 return err;
312 const struct got_error *
313 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
314 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
315 int infd)
317 const struct got_error *err = NULL;
318 struct got_object *obj;
319 struct got_inflate_checksum csum;
320 struct got_object_id id;
321 struct got_hash ctx;
322 size_t len, consumed;
323 FILE *f = NULL;
325 *outbuf = NULL;
326 *size = 0;
327 *hdrlen = 0;
329 got_hash_init(&ctx, GOT_HASH_SHA1);
330 memset(&csum, 0, sizeof(csum));
331 csum.output_ctx = &ctx;
333 if (lseek(infd, SEEK_SET, 0) == -1)
334 return got_error_from_errno("lseek");
336 err = got_object_read_header(&obj, infd);
337 if (err)
338 return err;
340 if (lseek(infd, SEEK_SET, 0) == -1)
341 return got_error_from_errno("lseek");
343 if (obj->size + obj->hdrlen <= max_in_mem_size) {
344 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
345 obj->size + obj->hdrlen, infd);
346 } else {
347 int fd;
348 /*
349 * XXX This uses an extra file descriptor for no good reason.
350 * We should have got_inflate_fd_to_fd().
351 */
352 fd = dup(infd);
353 if (fd == -1)
354 return got_error_from_errno("dup");
355 f = fdopen(fd, "r");
356 if (f == NULL) {
357 err = got_error_from_errno("fdopen");
358 abort();
359 close(fd);
360 goto done;
362 err = got_inflate_to_fd(&len, f, &csum, outfd);
364 if (err)
365 goto done;
367 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
368 err = got_error(GOT_ERR_BAD_OBJ_HDR);
369 goto done;
372 got_hash_final_object_id(&ctx, &id);
373 if (got_object_id_cmp(expected_id, &id) != 0) {
374 err = got_error_checksum(expected_id);
375 goto done;
378 *size = obj->size;
379 *hdrlen = obj->hdrlen;
380 done:
381 got_object_close(obj);
382 if (f && fclose(f) == EOF && err == NULL)
383 err = got_error_from_errno("fclose");
384 return err;
387 struct got_commit_object *
388 got_object_commit_alloc_partial(void)
390 struct got_commit_object *commit;
392 commit = calloc(1, sizeof(*commit));
393 if (commit == NULL)
394 return NULL;
395 commit->tree_id = malloc(sizeof(*commit->tree_id));
396 if (commit->tree_id == NULL) {
397 free(commit);
398 return NULL;
401 STAILQ_INIT(&commit->parent_ids);
403 return commit;
406 const struct got_error *
407 got_object_commit_add_parent(struct got_commit_object *commit,
408 const char *id_str)
410 const struct got_error *err = NULL;
411 struct got_object_qid *qid;
413 err = got_object_qid_alloc_partial(&qid);
414 if (err)
415 return err;
417 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
418 err = got_error(GOT_ERR_BAD_OBJ_DATA);
419 got_object_qid_free(qid);
420 return err;
423 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
424 commit->nparents++;
426 return NULL;
429 static const struct got_error *
430 parse_gmtoff(time_t *gmtoff, const char *tzstr)
432 int sign = 1;
433 const char *p = tzstr;
434 time_t h, m;
436 *gmtoff = 0;
438 if (*p == '-')
439 sign = -1;
440 else if (*p != '+')
441 return got_error(GOT_ERR_BAD_OBJ_DATA);
442 p++;
443 if (!isdigit((unsigned char)*p) &&
444 !isdigit((unsigned char)*(p + 1)))
445 return got_error(GOT_ERR_BAD_OBJ_DATA);
446 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
448 p += 2;
449 if (!isdigit((unsigned char)*p) &&
450 !isdigit((unsigned char)*(p + 1)))
451 return got_error(GOT_ERR_BAD_OBJ_DATA);
452 m = ((*p - '0') * 10) + (*(p + 1) - '0');
454 *gmtoff = (h * 60 * 60 + m * 60) * sign;
455 return NULL;
458 static const struct got_error *
459 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
461 const struct got_error *err = NULL;
462 const char *errstr;
463 char *space, *tzstr;
465 /* Parse and strip off trailing timezone indicator string. */
466 space = strrchr(committer, ' ');
467 if (space == NULL)
468 return got_error(GOT_ERR_BAD_OBJ_DATA);
469 tzstr = strdup(space + 1);
470 if (tzstr == NULL)
471 return got_error_from_errno("strdup");
472 err = parse_gmtoff(gmtoff, tzstr);
473 free(tzstr);
474 if (err) {
475 if (err->code != GOT_ERR_BAD_OBJ_DATA)
476 return err;
477 /* Old versions of Git omitted the timestamp. */
478 *time = 0;
479 *gmtoff = 0;
480 return NULL;
482 *space = '\0';
484 /* Timestamp is separated from committer name + email by space. */
485 space = strrchr(committer, ' ');
486 if (space == NULL)
487 return got_error(GOT_ERR_BAD_OBJ_DATA);
489 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
490 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
491 if (errstr)
492 return got_error(GOT_ERR_BAD_OBJ_DATA);
494 /* Strip off parsed time information, leaving just author and email. */
495 *space = '\0';
497 return NULL;
500 void
501 got_object_commit_close(struct got_commit_object *commit)
503 if (commit->refcnt > 0) {
504 commit->refcnt--;
505 if (commit->refcnt > 0)
506 return;
509 got_object_id_queue_free(&commit->parent_ids);
510 free(commit->tree_id);
511 free(commit->author);
512 free(commit->committer);
513 free(commit->logmsg);
514 free(commit);
517 struct got_object_id *
518 got_object_commit_get_tree_id(struct got_commit_object *commit)
520 return commit->tree_id;
523 int
524 got_object_commit_get_nparents(struct got_commit_object *commit)
526 return commit->nparents;
529 const struct got_object_id_queue *
530 got_object_commit_get_parent_ids(struct got_commit_object *commit)
532 return &commit->parent_ids;
535 const char *
536 got_object_commit_get_author(struct got_commit_object *commit)
538 return commit->author;
541 time_t
542 got_object_commit_get_author_time(struct got_commit_object *commit)
544 return commit->author_time;
547 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
549 return commit->author_gmtoff;
552 const char *
553 got_object_commit_get_committer(struct got_commit_object *commit)
555 return commit->committer;
558 time_t
559 got_object_commit_get_committer_time(struct got_commit_object *commit)
561 return commit->committer_time;
564 time_t
565 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
567 return commit->committer_gmtoff;
570 const struct got_error *
571 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
573 const struct got_error *err = NULL;
574 const char *src;
575 char *dst;
576 size_t len;
578 len = strlen(commit->logmsg);
579 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
580 if (*logmsg == NULL)
581 return got_error_from_errno("malloc");
583 /*
584 * Strip out unusual headers. Headers are separated from the commit
585 * message body by a single empty line.
586 */
587 src = commit->logmsg;
588 dst = *logmsg;
589 while (*src != '\0' && *src != '\n') {
590 int copy_header = 1, eol = 0;
591 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
592 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
593 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
594 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
595 strncmp(src, GOT_COMMIT_LABEL_PARENT,
596 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
597 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
598 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
599 copy_header = 0;
601 while (*src != '\0' && !eol) {
602 if (copy_header) {
603 *dst = *src;
604 dst++;
606 if (*src == '\n')
607 eol = 1;
608 src++;
611 *dst = '\0';
613 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
614 err = got_error(GOT_ERR_NO_SPACE);
615 goto done;
618 /* Trim redundant trailing whitespace. */
619 len = strlen(*logmsg);
620 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
621 isspace((unsigned char)(*logmsg)[len - 1])) {
622 (*logmsg)[len - 1] = '\0';
623 len--;
626 /* Append a trailing newline if missing. */
627 if (len > 0 && (*logmsg)[len - 1] != '\n') {
628 (*logmsg)[len] = '\n';
629 (*logmsg)[len + 1] = '\0';
631 done:
632 if (err) {
633 free(*logmsg);
634 *logmsg = NULL;
636 return err;
639 const char *
640 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
642 return commit->logmsg;
645 const struct got_error *
646 got_object_parse_commit(struct got_commit_object **commit, char *buf,
647 size_t len)
649 const struct got_error *err = NULL;
650 enum got_hash_algorithm algo = GOT_HASH_SHA1;
651 char *s = buf;
652 size_t label_len;
653 ssize_t remain = (ssize_t)len;
655 if (remain == 0)
656 return got_error(GOT_ERR_BAD_OBJ_DATA);
658 *commit = got_object_commit_alloc_partial();
659 if (*commit == NULL)
660 return got_error_from_errno("got_object_commit_alloc_partial");
662 label_len = strlen(GOT_COMMIT_LABEL_TREE);
663 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
664 remain -= label_len;
665 if (remain < SHA1_DIGEST_STRING_LENGTH) {
666 err = got_error(GOT_ERR_BAD_OBJ_DATA);
667 goto done;
669 s += label_len;
670 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
671 err = got_error(GOT_ERR_BAD_OBJ_DATA);
672 goto done;
674 remain -= SHA1_DIGEST_STRING_LENGTH;
675 s += SHA1_DIGEST_STRING_LENGTH;
676 } else {
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 goto done;
681 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
682 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
683 remain -= label_len;
684 if (remain < SHA1_DIGEST_STRING_LENGTH) {
685 err = got_error(GOT_ERR_BAD_OBJ_DATA);
686 goto done;
688 s += label_len;
689 err = got_object_commit_add_parent(*commit, s);
690 if (err)
691 goto done;
693 remain -= SHA1_DIGEST_STRING_LENGTH;
694 s += SHA1_DIGEST_STRING_LENGTH;
697 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
698 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, 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)->author_time,
716 &(*commit)->author_gmtoff, s);
717 if (err)
718 goto done;
719 (*commit)->author = strdup(s);
720 if ((*commit)->author == NULL) {
721 err = got_error_from_errno("strdup");
722 goto done;
724 s += slen + 1;
725 remain -= slen + 1;
728 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
729 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
730 char *p;
731 size_t slen;
733 remain -= label_len;
734 if (remain <= 0) {
735 err = got_error(GOT_ERR_BAD_OBJ_DATA);
736 goto done;
738 s += label_len;
739 p = memchr(s, '\n', remain);
740 if (p == NULL) {
741 err = got_error(GOT_ERR_BAD_OBJ_DATA);
742 goto done;
744 *p = '\0';
745 slen = strlen(s);
746 err = parse_commit_time(&(*commit)->committer_time,
747 &(*commit)->committer_gmtoff, s);
748 if (err)
749 goto done;
750 (*commit)->committer = strdup(s);
751 if ((*commit)->committer == NULL) {
752 err = got_error_from_errno("strdup");
753 goto done;
755 s += slen + 1;
756 remain -= slen + 1;
759 (*commit)->logmsg = strndup(s, remain);
760 if ((*commit)->logmsg == NULL) {
761 err = got_error_from_errno("strndup");
762 goto done;
764 done:
765 if (err) {
766 got_object_commit_close(*commit);
767 *commit = NULL;
769 return err;
772 const struct got_error *
773 got_object_read_commit(struct got_commit_object **commit, int fd,
774 struct got_object_id *expected_id, size_t expected_size)
776 struct got_object *obj = NULL;
777 const struct got_error *err = NULL;
778 size_t len;
779 uint8_t *p;
780 struct got_inflate_checksum csum;
781 struct got_hash ctx;
782 struct got_object_id id;
784 got_hash_init(&ctx, GOT_HASH_SHA1);
785 memset(&csum, 0, sizeof(csum));
786 csum.output_ctx = &ctx;
788 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
789 if (err)
790 return err;
792 got_hash_final_object_id(&ctx, &id);
793 if (got_object_id_cmp(expected_id, &id) != 0) {
794 err = got_error_checksum(expected_id);
795 goto done;
798 err = got_object_parse_header(&obj, p, len);
799 if (err)
800 goto done;
802 if (len < obj->hdrlen + obj->size) {
803 err = got_error(GOT_ERR_BAD_OBJ_DATA);
804 goto done;
807 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
808 err = got_error(GOT_ERR_OBJ_TYPE);
809 goto done;
812 /* Skip object header. */
813 len -= obj->hdrlen;
814 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
815 done:
816 free(p);
817 if (obj)
818 got_object_close(obj);
819 return err;
822 void
823 got_object_tree_close(struct got_tree_object *tree)
825 if (tree->refcnt > 0) {
826 tree->refcnt--;
827 if (tree->refcnt > 0)
828 return;
831 free(tree->entries);
832 free(tree);
835 static const struct got_error *
836 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
837 size_t maxlen)
839 char *p, *space;
841 *elen = 0;
843 *elen = strnlen(buf, maxlen) + 1;
844 if (*elen > maxlen)
845 return got_error(GOT_ERR_BAD_OBJ_DATA);
847 space = memchr(buf, ' ', *elen);
848 if (space == NULL || space <= buf)
849 return got_error(GOT_ERR_BAD_OBJ_DATA);
851 pte->mode = 0;
852 p = buf;
853 while (p < space) {
854 if (*p < '0' || *p > '7')
855 return got_error(GOT_ERR_BAD_OBJ_DATA);
856 pte->mode <<= 3;
857 pte->mode |= *p - '0';
858 p++;
861 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
862 return got_error(GOT_ERR_BAD_OBJ_DATA);
864 pte->name = space + 1;
865 pte->namelen = strlen(pte->name);
866 buf += *elen;
867 pte->id = buf;
868 *elen += SHA1_DIGEST_LENGTH;
869 return NULL;
872 static int
873 pte_cmp(const void *pa, const void *pb)
875 const struct got_parsed_tree_entry *a = pa, *b = pb;
877 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
880 const struct got_error *
881 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
882 size_t *nentries_alloc, uint8_t *buf, size_t len)
884 const struct got_error *err = NULL;
885 size_t remain = len;
886 const size_t nalloc = 16;
887 struct got_parsed_tree_entry *pte;
888 int i;
890 *nentries = 0;
891 if (remain == 0)
892 return NULL; /* tree is empty */
894 while (remain > 0) {
895 size_t elen;
897 if (*nentries >= *nentries_alloc) {
898 pte = recallocarray(*entries, *nentries_alloc,
899 *nentries_alloc + nalloc, sizeof(**entries));
900 if (pte == NULL) {
901 err = got_error_from_errno("recallocarray");
902 goto done;
904 *entries = pte;
905 *nentries_alloc += nalloc;
908 pte = &(*entries)[*nentries];
909 err = parse_tree_entry(pte, &elen, buf, remain);
910 if (err)
911 goto done;
912 buf += elen;
913 remain -= elen;
914 (*nentries)++;
917 if (remain != 0) {
918 err = got_error(GOT_ERR_BAD_OBJ_DATA);
919 goto done;
922 if (*nentries > 1) {
923 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
925 for (i = 0; i < *nentries - 1; i++) {
926 struct got_parsed_tree_entry *prev = &(*entries)[i];
927 pte = &(*entries)[i + 1];
928 if (got_path_cmp(prev->name, pte->name,
929 prev->namelen, pte->namelen) == 0) {
930 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
931 break;
935 done:
936 if (err)
937 *nentries = 0;
938 return err;
941 const struct got_error *
942 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
943 size_t *nentries_alloc, uint8_t **p, int fd,
944 struct got_object_id *expected_id)
946 const struct got_error *err = NULL;
947 struct got_object *obj = NULL;
948 size_t len;
949 struct got_inflate_checksum csum;
950 struct got_hash ctx;
951 struct got_object_id id;
953 got_hash_init(&ctx, GOT_HASH_SHA1);
954 memset(&csum, 0, sizeof(csum));
955 csum.output_ctx = &ctx;
957 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
958 if (err)
959 return err;
961 got_hash_final_object_id(&ctx, &id);
962 if (got_object_id_cmp(expected_id, &id) != 0) {
963 err = got_error_checksum(expected_id);
964 goto done;
967 err = got_object_parse_header(&obj, *p, len);
968 if (err)
969 goto done;
971 if (len < obj->hdrlen + obj->size) {
972 err = got_error(GOT_ERR_BAD_OBJ_DATA);
973 goto done;
976 /* Skip object header. */
977 len -= obj->hdrlen;
978 err = got_object_parse_tree(entries, nentries, nentries_alloc,
979 *p + obj->hdrlen, len);
980 done:
981 if (obj)
982 got_object_close(obj);
983 return err;
986 void
987 got_object_tag_close(struct got_tag_object *tag)
989 if (tag->refcnt > 0) {
990 tag->refcnt--;
991 if (tag->refcnt > 0)
992 return;
995 free(tag->tag);
996 free(tag->tagger);
997 free(tag->tagmsg);
998 free(tag);
1001 const struct got_error *
1002 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
1004 const struct got_error *err = NULL;
1005 enum got_hash_algorithm algo = GOT_HASH_SHA1;
1006 size_t remain = len;
1007 char *s = buf;
1008 size_t label_len;
1010 if (remain == 0)
1011 return got_error(GOT_ERR_BAD_OBJ_DATA);
1013 *tag = calloc(1, sizeof(**tag));
1014 if (*tag == NULL)
1015 return got_error_from_errno("calloc");
1017 label_len = strlen(GOT_TAG_LABEL_OBJECT);
1018 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
1019 remain -= label_len;
1020 if (remain < SHA1_DIGEST_STRING_LENGTH) {
1021 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1022 goto done;
1024 s += label_len;
1025 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
1026 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1027 goto done;
1029 remain -= SHA1_DIGEST_STRING_LENGTH;
1030 s += SHA1_DIGEST_STRING_LENGTH;
1031 } else {
1032 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1033 goto done;
1036 if (remain <= 0) {
1037 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1038 goto done;
1041 label_len = strlen(GOT_TAG_LABEL_TYPE);
1042 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1043 remain -= label_len;
1044 if (remain <= 0) {
1045 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1046 goto done;
1048 s += label_len;
1049 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1050 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1051 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1052 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1053 s += label_len;
1054 remain -= label_len;
1055 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1056 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1057 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1058 label_len = strlen(GOT_OBJ_LABEL_TREE);
1059 s += label_len;
1060 remain -= label_len;
1061 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1062 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1063 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1064 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1065 s += label_len;
1066 remain -= label_len;
1067 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1068 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1069 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1070 label_len = strlen(GOT_OBJ_LABEL_TAG);
1071 s += label_len;
1072 remain -= label_len;
1073 } else {
1074 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1075 goto done;
1078 if (remain <= 0 || *s != '\n') {
1079 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1080 goto done;
1082 s++;
1083 remain--;
1084 if (remain <= 0) {
1085 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1086 goto done;
1088 } else {
1089 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1090 goto done;
1093 label_len = strlen(GOT_TAG_LABEL_TAG);
1094 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1095 char *p;
1096 size_t slen;
1097 remain -= label_len;
1098 if (remain <= 0) {
1099 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1100 goto done;
1102 s += label_len;
1103 p = memchr(s, '\n', remain);
1104 if (p == NULL) {
1105 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1106 goto done;
1108 *p = '\0';
1109 slen = strlen(s);
1110 (*tag)->tag = strndup(s, slen);
1111 if ((*tag)->tag == NULL) {
1112 err = got_error_from_errno("strndup");
1113 goto done;
1115 s += slen + 1;
1116 remain -= slen + 1;
1117 if (remain <= 0) {
1118 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1119 goto done;
1121 } else {
1122 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1123 goto done;
1126 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1127 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1128 char *p;
1129 size_t slen;
1131 remain -= label_len;
1132 if (remain <= 0) {
1133 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1134 goto done;
1136 s += label_len;
1137 p = memchr(s, '\n', remain);
1138 if (p == NULL) {
1139 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1140 goto done;
1142 *p = '\0';
1143 slen = strlen(s);
1144 err = parse_commit_time(&(*tag)->tagger_time,
1145 &(*tag)->tagger_gmtoff, s);
1146 if (err)
1147 goto done;
1148 (*tag)->tagger = strdup(s);
1149 if ((*tag)->tagger == NULL) {
1150 err = got_error_from_errno("strdup");
1151 goto done;
1153 s += slen + 1;
1154 remain -= slen + 1;
1155 if (remain < 0) {
1156 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1157 goto done;
1159 } else {
1160 /* Some old tags in the Linux git repo have no tagger. */
1161 (*tag)->tagger = strdup("");
1162 if ((*tag)->tagger == NULL) {
1163 err = got_error_from_errno("strdup");
1164 goto done;
1168 (*tag)->tagmsg = strndup(s, remain);
1169 if ((*tag)->tagmsg == NULL) {
1170 err = got_error_from_errno("strndup");
1171 goto done;
1173 done:
1174 if (err) {
1175 got_object_tag_close(*tag);
1176 *tag = NULL;
1178 return err;
1181 const struct got_error *
1182 got_object_read_tag(struct got_tag_object **tag, int fd,
1183 struct got_object_id *expected_id, size_t expected_size)
1185 const struct got_error *err = NULL;
1186 struct got_object *obj = NULL;
1187 size_t len;
1188 uint8_t *p;
1189 struct got_inflate_checksum csum;
1190 struct got_hash ctx;
1191 struct got_object_id id;
1193 got_hash_init(&ctx, GOT_HASH_SHA1);
1194 memset(&csum, 0, sizeof(csum));
1195 csum.output_ctx = &ctx;
1197 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1198 expected_size, fd);
1199 if (err)
1200 return err;
1202 got_hash_final_object_id(&ctx, &id);
1203 if (got_object_id_cmp(expected_id, &id) != 0) {
1204 err = got_error_checksum(expected_id);
1205 goto done;
1208 err = got_object_parse_header(&obj, p, len);
1209 if (err)
1210 goto done;
1212 if (len < obj->hdrlen + obj->size) {
1213 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1214 goto done;
1217 /* Skip object header. */
1218 len -= obj->hdrlen;
1219 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1220 done:
1221 free(p);
1222 if (obj)
1223 got_object_close(obj);
1224 return err;
1227 const struct got_error *
1228 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1230 const struct got_error *err = NULL;
1231 static const size_t blocksize = 512;
1232 size_t n, total, remain;
1233 uint8_t *buf;
1235 *outbuf = NULL;
1236 *outlen = 0;
1238 buf = malloc(blocksize);
1239 if (buf == NULL)
1240 return got_error_from_errno("malloc");
1242 remain = blocksize;
1243 total = 0;
1244 for (;;) {
1245 if (remain == 0) {
1246 uint8_t *newbuf;
1247 newbuf = reallocarray(buf, 1, total + blocksize);
1248 if (newbuf == NULL) {
1249 err = got_error_from_errno("reallocarray");
1250 goto done;
1252 buf = newbuf;
1253 remain += blocksize;
1255 n = fread(buf + total, 1, remain, f);
1256 if (n == 0) {
1257 if (ferror(f)) {
1258 err = got_ferror(f, GOT_ERR_IO);
1259 goto done;
1261 break; /* EOF */
1263 remain -= n;
1264 total += n;
1267 done:
1268 if (err == NULL) {
1269 *outbuf = buf;
1270 *outlen = total;
1271 } else
1272 free(buf);
1273 return err;