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 void
114 got_object_close(struct got_object *obj)
116 if (obj->refcnt > 0) {
117 obj->refcnt--;
118 if (obj->refcnt > 0)
119 return;
122 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
123 struct got_delta *delta;
124 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
125 delta = STAILQ_FIRST(&obj->deltas.entries);
126 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
127 free(delta);
130 free(obj);
133 const struct got_error *
134 got_object_raw_close(struct got_raw_object *obj)
136 const struct got_error *err = NULL;
138 if (obj->refcnt > 0) {
139 obj->refcnt--;
140 if (obj->refcnt > 0)
141 return NULL;
144 if (obj->close_cb)
145 obj->close_cb(obj);
147 if (obj->f == NULL) {
148 if (obj->fd != -1) {
149 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
150 err = got_error_from_errno("munmap");
151 if (close(obj->fd) == -1 && err == NULL)
152 err = got_error_from_errno("close");
153 } else
154 free(obj->data);
155 } else {
156 if (fclose(obj->f) == EOF && err == NULL)
157 err = got_error_from_errno("fclose");
159 free(obj);
160 return err;
163 void
164 got_object_qid_free(struct got_object_qid *qid)
166 free(qid);
169 void
170 got_object_id_queue_free(struct got_object_id_queue *ids)
172 struct got_object_qid *qid;
174 while (!STAILQ_EMPTY(ids)) {
175 qid = STAILQ_FIRST(ids);
176 STAILQ_REMOVE_HEAD(ids, entry);
177 got_object_qid_free(qid);
181 const struct got_error *
182 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
184 const char *obj_labels[] = {
185 GOT_OBJ_LABEL_COMMIT,
186 GOT_OBJ_LABEL_TREE,
187 GOT_OBJ_LABEL_BLOB,
188 GOT_OBJ_LABEL_TAG,
189 };
190 const int obj_types[] = {
191 GOT_OBJ_TYPE_COMMIT,
192 GOT_OBJ_TYPE_TREE,
193 GOT_OBJ_TYPE_BLOB,
194 GOT_OBJ_TYPE_TAG,
195 };
196 int type = 0;
197 size_t size = 0;
198 size_t i;
199 char *end;
201 *obj = NULL;
203 end = memchr(buf, '\0', len);
204 if (end == NULL)
205 return got_error(GOT_ERR_BAD_OBJ_HDR);
207 for (i = 0; i < nitems(obj_labels); i++) {
208 const char *label = obj_labels[i];
209 size_t label_len = strlen(label);
210 const char *errstr;
212 if (len <= label_len || buf + label_len >= end ||
213 strncmp(buf, label, label_len) != 0)
214 continue;
216 type = obj_types[i];
217 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
218 if (errstr != NULL)
219 return got_error(GOT_ERR_BAD_OBJ_HDR);
220 break;
223 if (type == 0)
224 return got_error(GOT_ERR_BAD_OBJ_HDR);
226 *obj = calloc(1, sizeof(**obj));
227 if (*obj == NULL)
228 return got_error_from_errno("calloc");
229 (*obj)->type = type;
230 (*obj)->hdrlen = end - buf + 1;
231 (*obj)->size = size;
232 return NULL;
235 const struct got_error *
236 got_object_read_header(struct got_object **obj, int fd)
238 const struct got_error *err;
239 struct got_inflate_buf zb;
240 uint8_t *buf;
241 const size_t zbsize = 64;
242 size_t outlen, totlen;
243 int nbuf = 1;
245 *obj = NULL;
247 buf = malloc(zbsize);
248 if (buf == NULL)
249 return got_error_from_errno("malloc");
250 buf[0] = '\0';
252 err = got_inflate_init(&zb, buf, zbsize, NULL);
253 if (err)
254 return err;
256 totlen = 0;
257 do {
258 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
259 if (err)
260 goto done;
261 if (outlen == 0)
262 break;
263 totlen += outlen;
264 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
265 uint8_t *newbuf;
266 nbuf++;
267 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
268 if (newbuf == NULL) {
269 err = got_error_from_errno("recallocarray");
270 goto done;
272 buf = newbuf;
273 zb.outbuf = newbuf + totlen;
274 zb.outlen = (nbuf * zbsize) - totlen;
276 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
278 err = got_object_parse_header(obj, buf, totlen);
279 done:
280 free(buf);
281 got_inflate_end(&zb);
282 return err;
285 const struct got_error *
286 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
287 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
288 int infd)
290 const struct got_error *err = NULL;
291 struct got_object *obj;
292 struct got_inflate_checksum csum;
293 struct got_object_id id;
294 struct got_hash ctx;
295 size_t len, consumed;
296 FILE *f = NULL;
298 *outbuf = NULL;
299 *size = 0;
300 *hdrlen = 0;
302 got_hash_init(&ctx, GOT_HASH_SHA1);
303 memset(&csum, 0, sizeof(csum));
304 csum.output_ctx = &ctx;
306 if (lseek(infd, SEEK_SET, 0) == -1)
307 return got_error_from_errno("lseek");
309 err = got_object_read_header(&obj, infd);
310 if (err)
311 return err;
313 if (lseek(infd, SEEK_SET, 0) == -1)
314 return got_error_from_errno("lseek");
316 if (obj->size + obj->hdrlen <= max_in_mem_size) {
317 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
318 obj->size + obj->hdrlen, infd);
319 } else {
320 int fd;
321 /*
322 * XXX This uses an extra file descriptor for no good reason.
323 * We should have got_inflate_fd_to_fd().
324 */
325 fd = dup(infd);
326 if (fd == -1)
327 return got_error_from_errno("dup");
328 f = fdopen(fd, "r");
329 if (f == NULL) {
330 err = got_error_from_errno("fdopen");
331 abort();
332 close(fd);
333 goto done;
335 err = got_inflate_to_fd(&len, f, &csum, outfd);
337 if (err)
338 goto done;
340 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
341 err = got_error(GOT_ERR_BAD_OBJ_HDR);
342 goto done;
345 got_hash_final_object_id(&ctx, &id);
346 if (got_object_id_cmp(expected_id, &id) != 0) {
347 err = got_error_checksum(expected_id);
348 goto done;
351 *size = obj->size;
352 *hdrlen = obj->hdrlen;
353 done:
354 got_object_close(obj);
355 if (f && fclose(f) == EOF && err == NULL)
356 err = got_error_from_errno("fclose");
357 return err;
360 struct got_commit_object *
361 got_object_commit_alloc_partial(void)
363 struct got_commit_object *commit;
365 commit = calloc(1, sizeof(*commit));
366 if (commit == NULL)
367 return NULL;
368 commit->tree_id = malloc(sizeof(*commit->tree_id));
369 if (commit->tree_id == NULL) {
370 free(commit);
371 return NULL;
374 STAILQ_INIT(&commit->parent_ids);
376 return commit;
379 const struct got_error *
380 got_object_commit_add_parent(struct got_commit_object *commit,
381 const char *id_str)
383 const struct got_error *err = NULL;
384 struct got_object_qid *qid;
386 err = got_object_qid_alloc_partial(&qid);
387 if (err)
388 return err;
390 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
391 err = got_error(GOT_ERR_BAD_OBJ_DATA);
392 got_object_qid_free(qid);
393 return err;
396 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
397 commit->nparents++;
399 return NULL;
402 static const struct got_error *
403 parse_gmtoff(time_t *gmtoff, const char *tzstr)
405 int sign = 1;
406 const char *p = tzstr;
407 time_t h, m;
409 *gmtoff = 0;
411 if (*p == '-')
412 sign = -1;
413 else if (*p != '+')
414 return got_error(GOT_ERR_BAD_OBJ_DATA);
415 p++;
416 if (!isdigit((unsigned char)*p) &&
417 !isdigit((unsigned char)*(p + 1)))
418 return got_error(GOT_ERR_BAD_OBJ_DATA);
419 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
421 p += 2;
422 if (!isdigit((unsigned char)*p) &&
423 !isdigit((unsigned char)*(p + 1)))
424 return got_error(GOT_ERR_BAD_OBJ_DATA);
425 m = ((*p - '0') * 10) + (*(p + 1) - '0');
427 *gmtoff = (h * 60 * 60 + m * 60) * sign;
428 return NULL;
431 static const struct got_error *
432 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
434 const struct got_error *err = NULL;
435 const char *errstr;
436 char *space, *tzstr;
438 /* Parse and strip off trailing timezone indicator string. */
439 space = strrchr(committer, ' ');
440 if (space == NULL)
441 return got_error(GOT_ERR_BAD_OBJ_DATA);
442 tzstr = strdup(space + 1);
443 if (tzstr == NULL)
444 return got_error_from_errno("strdup");
445 err = parse_gmtoff(gmtoff, tzstr);
446 free(tzstr);
447 if (err) {
448 if (err->code != GOT_ERR_BAD_OBJ_DATA)
449 return err;
450 /* Old versions of Git omitted the timestamp. */
451 *time = 0;
452 *gmtoff = 0;
453 return NULL;
455 *space = '\0';
457 /* Timestamp is separated from committer name + email by space. */
458 space = strrchr(committer, ' ');
459 if (space == NULL)
460 return got_error(GOT_ERR_BAD_OBJ_DATA);
462 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
463 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
464 if (errstr)
465 return got_error(GOT_ERR_BAD_OBJ_DATA);
467 /* Strip off parsed time information, leaving just author and email. */
468 *space = '\0';
470 return NULL;
473 void
474 got_object_commit_close(struct got_commit_object *commit)
476 if (commit->refcnt > 0) {
477 commit->refcnt--;
478 if (commit->refcnt > 0)
479 return;
482 got_object_id_queue_free(&commit->parent_ids);
483 free(commit->tree_id);
484 free(commit->author);
485 free(commit->committer);
486 free(commit->logmsg);
487 free(commit);
490 struct got_object_id *
491 got_object_commit_get_tree_id(struct got_commit_object *commit)
493 return commit->tree_id;
496 int
497 got_object_commit_get_nparents(struct got_commit_object *commit)
499 return commit->nparents;
502 const struct got_object_id_queue *
503 got_object_commit_get_parent_ids(struct got_commit_object *commit)
505 return &commit->parent_ids;
508 const char *
509 got_object_commit_get_author(struct got_commit_object *commit)
511 return commit->author;
514 time_t
515 got_object_commit_get_author_time(struct got_commit_object *commit)
517 return commit->author_time;
520 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
522 return commit->author_gmtoff;
525 const char *
526 got_object_commit_get_committer(struct got_commit_object *commit)
528 return commit->committer;
531 time_t
532 got_object_commit_get_committer_time(struct got_commit_object *commit)
534 return commit->committer_time;
537 time_t
538 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
540 return commit->committer_gmtoff;
543 const struct got_error *
544 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
546 const struct got_error *err = NULL;
547 const char *src;
548 char *dst;
549 size_t len;
551 len = strlen(commit->logmsg);
552 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
553 if (*logmsg == NULL)
554 return got_error_from_errno("malloc");
556 /*
557 * Strip out unusual headers. Headers are separated from the commit
558 * message body by a single empty line.
559 */
560 src = commit->logmsg;
561 dst = *logmsg;
562 while (*src != '\0' && *src != '\n') {
563 int copy_header = 1, eol = 0;
564 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
565 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
566 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
567 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
568 strncmp(src, GOT_COMMIT_LABEL_PARENT,
569 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
570 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
571 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
572 copy_header = 0;
574 while (*src != '\0' && !eol) {
575 if (copy_header) {
576 *dst = *src;
577 dst++;
579 if (*src == '\n')
580 eol = 1;
581 src++;
584 *dst = '\0';
586 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
587 err = got_error(GOT_ERR_NO_SPACE);
588 goto done;
591 /* Trim redundant trailing whitespace. */
592 len = strlen(*logmsg);
593 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
594 isspace((unsigned char)(*logmsg)[len - 1])) {
595 (*logmsg)[len - 1] = '\0';
596 len--;
599 /* Append a trailing newline if missing. */
600 if (len > 0 && (*logmsg)[len - 1] != '\n') {
601 (*logmsg)[len] = '\n';
602 (*logmsg)[len + 1] = '\0';
604 done:
605 if (err) {
606 free(*logmsg);
607 *logmsg = NULL;
609 return err;
612 const char *
613 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
615 return commit->logmsg;
618 const struct got_error *
619 got_object_parse_commit(struct got_commit_object **commit, char *buf,
620 size_t len)
622 const struct got_error *err = NULL;
623 enum got_hash_algorithm algo = GOT_HASH_SHA1;
624 char *s = buf;
625 size_t label_len;
626 ssize_t remain = (ssize_t)len;
628 if (remain == 0)
629 return got_error(GOT_ERR_BAD_OBJ_DATA);
631 *commit = got_object_commit_alloc_partial();
632 if (*commit == NULL)
633 return got_error_from_errno("got_object_commit_alloc_partial");
635 label_len = strlen(GOT_COMMIT_LABEL_TREE);
636 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
637 remain -= label_len;
638 if (remain < SHA1_DIGEST_STRING_LENGTH) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 goto done;
642 s += label_len;
643 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
644 err = got_error(GOT_ERR_BAD_OBJ_DATA);
645 goto done;
647 remain -= SHA1_DIGEST_STRING_LENGTH;
648 s += SHA1_DIGEST_STRING_LENGTH;
649 } else {
650 err = got_error(GOT_ERR_BAD_OBJ_DATA);
651 goto done;
654 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
655 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
656 remain -= label_len;
657 if (remain < SHA1_DIGEST_STRING_LENGTH) {
658 err = got_error(GOT_ERR_BAD_OBJ_DATA);
659 goto done;
661 s += label_len;
662 err = got_object_commit_add_parent(*commit, s);
663 if (err)
664 goto done;
666 remain -= SHA1_DIGEST_STRING_LENGTH;
667 s += SHA1_DIGEST_STRING_LENGTH;
670 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
671 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
672 char *p;
673 size_t slen;
675 remain -= label_len;
676 if (remain <= 0) {
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 goto done;
680 s += label_len;
681 p = memchr(s, '\n', remain);
682 if (p == NULL) {
683 err = got_error(GOT_ERR_BAD_OBJ_DATA);
684 goto done;
686 *p = '\0';
687 slen = strlen(s);
688 err = parse_commit_time(&(*commit)->author_time,
689 &(*commit)->author_gmtoff, s);
690 if (err)
691 goto done;
692 (*commit)->author = strdup(s);
693 if ((*commit)->author == NULL) {
694 err = got_error_from_errno("strdup");
695 goto done;
697 s += slen + 1;
698 remain -= slen + 1;
701 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
702 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
703 char *p;
704 size_t slen;
706 remain -= label_len;
707 if (remain <= 0) {
708 err = got_error(GOT_ERR_BAD_OBJ_DATA);
709 goto done;
711 s += label_len;
712 p = memchr(s, '\n', remain);
713 if (p == NULL) {
714 err = got_error(GOT_ERR_BAD_OBJ_DATA);
715 goto done;
717 *p = '\0';
718 slen = strlen(s);
719 err = parse_commit_time(&(*commit)->committer_time,
720 &(*commit)->committer_gmtoff, s);
721 if (err)
722 goto done;
723 (*commit)->committer = strdup(s);
724 if ((*commit)->committer == NULL) {
725 err = got_error_from_errno("strdup");
726 goto done;
728 s += slen + 1;
729 remain -= slen + 1;
732 (*commit)->logmsg = strndup(s, remain);
733 if ((*commit)->logmsg == NULL) {
734 err = got_error_from_errno("strndup");
735 goto done;
737 done:
738 if (err) {
739 got_object_commit_close(*commit);
740 *commit = NULL;
742 return err;
745 const struct got_error *
746 got_object_read_commit(struct got_commit_object **commit, int fd,
747 struct got_object_id *expected_id, size_t expected_size)
749 struct got_object *obj = NULL;
750 const struct got_error *err = NULL;
751 size_t len;
752 uint8_t *p;
753 struct got_inflate_checksum csum;
754 struct got_hash ctx;
755 struct got_object_id id;
757 got_hash_init(&ctx, GOT_HASH_SHA1);
758 memset(&csum, 0, sizeof(csum));
759 csum.output_ctx = &ctx;
761 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
762 if (err)
763 return err;
765 got_hash_final_object_id(&ctx, &id);
766 if (got_object_id_cmp(expected_id, &id) != 0) {
767 err = got_error_checksum(expected_id);
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 struct got_hash ctx;
924 struct got_object_id id;
926 got_hash_init(&ctx, GOT_HASH_SHA1);
927 memset(&csum, 0, sizeof(csum));
928 csum.output_ctx = &ctx;
930 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
931 if (err)
932 return err;
934 got_hash_final_object_id(&ctx, &id);
935 if (got_object_id_cmp(expected_id, &id) != 0) {
936 err = got_error_checksum(expected_id);
937 goto done;
940 err = got_object_parse_header(&obj, *p, len);
941 if (err)
942 goto done;
944 if (len < obj->hdrlen + obj->size) {
945 err = got_error(GOT_ERR_BAD_OBJ_DATA);
946 goto done;
949 /* Skip object header. */
950 len -= obj->hdrlen;
951 err = got_object_parse_tree(entries, nentries, nentries_alloc,
952 *p + obj->hdrlen, len);
953 done:
954 if (obj)
955 got_object_close(obj);
956 return err;
959 void
960 got_object_tag_close(struct got_tag_object *tag)
962 if (tag->refcnt > 0) {
963 tag->refcnt--;
964 if (tag->refcnt > 0)
965 return;
968 free(tag->tag);
969 free(tag->tagger);
970 free(tag->tagmsg);
971 free(tag);
974 const struct got_error *
975 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
977 const struct got_error *err = NULL;
978 enum got_hash_algorithm algo = GOT_HASH_SHA1;
979 size_t remain = len;
980 char *s = buf;
981 size_t label_len;
983 if (remain == 0)
984 return got_error(GOT_ERR_BAD_OBJ_DATA);
986 *tag = calloc(1, sizeof(**tag));
987 if (*tag == NULL)
988 return got_error_from_errno("calloc");
990 label_len = strlen(GOT_TAG_LABEL_OBJECT);
991 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
992 remain -= label_len;
993 if (remain < SHA1_DIGEST_STRING_LENGTH) {
994 err = got_error(GOT_ERR_BAD_OBJ_DATA);
995 goto done;
997 s += label_len;
998 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
999 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1000 goto done;
1002 remain -= SHA1_DIGEST_STRING_LENGTH;
1003 s += SHA1_DIGEST_STRING_LENGTH;
1004 } else {
1005 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1006 goto done;
1009 if (remain <= 0) {
1010 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1011 goto done;
1014 label_len = strlen(GOT_TAG_LABEL_TYPE);
1015 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1016 remain -= label_len;
1017 if (remain <= 0) {
1018 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1019 goto done;
1021 s += label_len;
1022 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1023 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1024 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1025 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1026 s += label_len;
1027 remain -= label_len;
1028 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1029 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1030 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1031 label_len = strlen(GOT_OBJ_LABEL_TREE);
1032 s += label_len;
1033 remain -= label_len;
1034 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1035 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1036 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1037 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1038 s += label_len;
1039 remain -= label_len;
1040 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1041 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1042 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1043 label_len = strlen(GOT_OBJ_LABEL_TAG);
1044 s += label_len;
1045 remain -= label_len;
1046 } else {
1047 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1048 goto done;
1051 if (remain <= 0 || *s != '\n') {
1052 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1053 goto done;
1055 s++;
1056 remain--;
1057 if (remain <= 0) {
1058 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1059 goto done;
1061 } else {
1062 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1063 goto done;
1066 label_len = strlen(GOT_TAG_LABEL_TAG);
1067 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1068 char *p;
1069 size_t slen;
1070 remain -= label_len;
1071 if (remain <= 0) {
1072 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1073 goto done;
1075 s += label_len;
1076 p = memchr(s, '\n', remain);
1077 if (p == NULL) {
1078 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1079 goto done;
1081 *p = '\0';
1082 slen = strlen(s);
1083 (*tag)->tag = strndup(s, slen);
1084 if ((*tag)->tag == NULL) {
1085 err = got_error_from_errno("strndup");
1086 goto done;
1088 s += slen + 1;
1089 remain -= slen + 1;
1090 if (remain <= 0) {
1091 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1092 goto done;
1094 } else {
1095 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1096 goto done;
1099 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1100 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1101 char *p;
1102 size_t slen;
1104 remain -= label_len;
1105 if (remain <= 0) {
1106 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1107 goto done;
1109 s += label_len;
1110 p = memchr(s, '\n', remain);
1111 if (p == NULL) {
1112 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1113 goto done;
1115 *p = '\0';
1116 slen = strlen(s);
1117 err = parse_commit_time(&(*tag)->tagger_time,
1118 &(*tag)->tagger_gmtoff, s);
1119 if (err)
1120 goto done;
1121 (*tag)->tagger = strdup(s);
1122 if ((*tag)->tagger == NULL) {
1123 err = got_error_from_errno("strdup");
1124 goto done;
1126 s += slen + 1;
1127 remain -= slen + 1;
1128 if (remain < 0) {
1129 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1130 goto done;
1132 } else {
1133 /* Some old tags in the Linux git repo have no tagger. */
1134 (*tag)->tagger = strdup("");
1135 if ((*tag)->tagger == NULL) {
1136 err = got_error_from_errno("strdup");
1137 goto done;
1141 (*tag)->tagmsg = strndup(s, remain);
1142 if ((*tag)->tagmsg == NULL) {
1143 err = got_error_from_errno("strndup");
1144 goto done;
1146 done:
1147 if (err) {
1148 got_object_tag_close(*tag);
1149 *tag = NULL;
1151 return err;
1154 const struct got_error *
1155 got_object_read_tag(struct got_tag_object **tag, int fd,
1156 struct got_object_id *expected_id, size_t expected_size)
1158 const struct got_error *err = NULL;
1159 struct got_object *obj = NULL;
1160 size_t len;
1161 uint8_t *p;
1162 struct got_inflate_checksum csum;
1163 struct got_hash ctx;
1164 struct got_object_id id;
1166 got_hash_init(&ctx, GOT_HASH_SHA1);
1167 memset(&csum, 0, sizeof(csum));
1168 csum.output_ctx = &ctx;
1170 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1171 expected_size, fd);
1172 if (err)
1173 return err;
1175 got_hash_final_object_id(&ctx, &id);
1176 if (got_object_id_cmp(expected_id, &id) != 0) {
1177 err = got_error_checksum(expected_id);
1178 goto done;
1181 err = got_object_parse_header(&obj, p, len);
1182 if (err)
1183 goto done;
1185 if (len < obj->hdrlen + obj->size) {
1186 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1187 goto done;
1190 /* Skip object header. */
1191 len -= obj->hdrlen;
1192 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1193 done:
1194 free(p);
1195 if (obj)
1196 got_object_close(obj);
1197 return err;
1200 const struct got_error *
1201 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1203 const struct got_error *err = NULL;
1204 static const size_t blocksize = 512;
1205 size_t n, total, remain;
1206 uint8_t *buf;
1208 *outbuf = NULL;
1209 *outlen = 0;
1211 buf = malloc(blocksize);
1212 if (buf == NULL)
1213 return got_error_from_errno("malloc");
1215 remain = blocksize;
1216 total = 0;
1217 for (;;) {
1218 if (remain == 0) {
1219 uint8_t *newbuf;
1220 newbuf = reallocarray(buf, 1, total + blocksize);
1221 if (newbuf == NULL) {
1222 err = got_error_from_errno("reallocarray");
1223 goto done;
1225 buf = newbuf;
1226 remain += blocksize;
1228 n = fread(buf + total, 1, remain, f);
1229 if (n == 0) {
1230 if (ferror(f)) {
1231 err = got_ferror(f, GOT_ERR_IO);
1232 goto done;
1234 break; /* EOF */
1236 remain -= n;
1237 total += n;
1240 done:
1241 if (err == NULL) {
1242 *outbuf = buf;
1243 *outlen = total;
1244 } else
1245 free(buf);
1246 return err;