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 = GOT_OBJECT_ID_HEX_MAXLEN;
93 *outbuf = malloc(len);
94 if (*outbuf == NULL)
95 return got_error_from_errno("malloc");
97 if (got_object_id_hex(id, *outbuf, len) == NULL) {
98 free(*outbuf);
99 *outbuf = NULL;
100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
103 return NULL;
106 char *
107 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
109 return got_sha1_digest_to_str(id->sha1, buf, len);
112 void
113 got_object_close(struct got_object *obj)
115 if (obj->refcnt > 0) {
116 obj->refcnt--;
117 if (obj->refcnt > 0)
118 return;
121 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
122 struct got_delta *delta;
123 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
124 delta = STAILQ_FIRST(&obj->deltas.entries);
125 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
126 free(delta);
129 free(obj);
132 const struct got_error *
133 got_object_raw_close(struct got_raw_object *obj)
135 const struct got_error *err = NULL;
137 if (obj->refcnt > 0) {
138 obj->refcnt--;
139 if (obj->refcnt > 0)
140 return NULL;
143 if (obj->close_cb)
144 obj->close_cb(obj);
146 if (obj->f == NULL) {
147 if (obj->fd != -1) {
148 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
149 err = got_error_from_errno("munmap");
150 if (close(obj->fd) == -1 && err == NULL)
151 err = got_error_from_errno("close");
152 } else
153 free(obj->data);
154 } else {
155 if (fclose(obj->f) == EOF && err == NULL)
156 err = got_error_from_errno("fclose");
158 free(obj);
159 return err;
162 void
163 got_object_qid_free(struct got_object_qid *qid)
165 free(qid);
168 void
169 got_object_id_queue_free(struct got_object_id_queue *ids)
171 struct got_object_qid *qid;
173 while (!STAILQ_EMPTY(ids)) {
174 qid = STAILQ_FIRST(ids);
175 STAILQ_REMOVE_HEAD(ids, entry);
176 got_object_qid_free(qid);
180 const struct got_error *
181 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
183 const char *obj_labels[] = {
184 GOT_OBJ_LABEL_COMMIT,
185 GOT_OBJ_LABEL_TREE,
186 GOT_OBJ_LABEL_BLOB,
187 GOT_OBJ_LABEL_TAG,
188 };
189 const int obj_types[] = {
190 GOT_OBJ_TYPE_COMMIT,
191 GOT_OBJ_TYPE_TREE,
192 GOT_OBJ_TYPE_BLOB,
193 GOT_OBJ_TYPE_TAG,
194 };
195 int type = 0;
196 size_t size = 0;
197 size_t i;
198 char *end;
200 *obj = NULL;
202 end = memchr(buf, '\0', len);
203 if (end == NULL)
204 return got_error(GOT_ERR_BAD_OBJ_HDR);
206 for (i = 0; i < nitems(obj_labels); i++) {
207 const char *label = obj_labels[i];
208 size_t label_len = strlen(label);
209 const char *errstr;
211 if (len <= label_len || buf + label_len >= end ||
212 strncmp(buf, label, label_len) != 0)
213 continue;
215 type = obj_types[i];
216 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
217 if (errstr != NULL)
218 return got_error(GOT_ERR_BAD_OBJ_HDR);
219 break;
222 if (type == 0)
223 return got_error(GOT_ERR_BAD_OBJ_HDR);
225 *obj = calloc(1, sizeof(**obj));
226 if (*obj == NULL)
227 return got_error_from_errno("calloc");
228 (*obj)->type = type;
229 (*obj)->hdrlen = end - buf + 1;
230 (*obj)->size = size;
231 return NULL;
234 const struct got_error *
235 got_object_read_header(struct got_object **obj, int fd)
237 const struct got_error *err;
238 struct got_inflate_buf zb;
239 uint8_t *buf;
240 const size_t zbsize = 64;
241 size_t outlen, totlen;
242 int nbuf = 1;
244 *obj = NULL;
246 buf = malloc(zbsize);
247 if (buf == NULL)
248 return got_error_from_errno("malloc");
249 buf[0] = '\0';
251 err = got_inflate_init(&zb, buf, zbsize, NULL);
252 if (err)
253 return err;
255 totlen = 0;
256 do {
257 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
258 if (err)
259 goto done;
260 if (outlen == 0)
261 break;
262 totlen += outlen;
263 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
264 uint8_t *newbuf;
265 nbuf++;
266 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
267 if (newbuf == NULL) {
268 err = got_error_from_errno("recallocarray");
269 goto done;
271 buf = newbuf;
272 zb.outbuf = newbuf + totlen;
273 zb.outlen = (nbuf * zbsize) - totlen;
275 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
277 err = got_object_parse_header(obj, buf, totlen);
278 done:
279 free(buf);
280 got_inflate_end(&zb);
281 return err;
284 const struct got_error *
285 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
286 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
287 int infd)
289 const struct got_error *err = NULL;
290 struct got_object *obj;
291 struct got_inflate_checksum csum;
292 uint8_t sha1[SHA1_DIGEST_LENGTH];
293 SHA1_CTX sha1_ctx;
294 size_t len, consumed;
295 FILE *f = NULL;
297 *outbuf = NULL;
298 *size = 0;
299 *hdrlen = 0;
301 SHA1Init(&sha1_ctx);
302 memset(&csum, 0, sizeof(csum));
303 csum.output_sha1 = &sha1_ctx;
305 if (lseek(infd, SEEK_SET, 0) == -1)
306 return got_error_from_errno("lseek");
308 err = got_object_read_header(&obj, infd);
309 if (err)
310 return err;
312 if (lseek(infd, SEEK_SET, 0) == -1)
313 return got_error_from_errno("lseek");
315 if (obj->size + obj->hdrlen <= max_in_mem_size) {
316 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
317 obj->size + obj->hdrlen, infd);
318 } else {
319 int fd;
320 /*
321 * XXX This uses an extra file descriptor for no good reason.
322 * We should have got_inflate_fd_to_fd().
323 */
324 fd = dup(infd);
325 if (fd == -1)
326 return got_error_from_errno("dup");
327 f = fdopen(fd, "r");
328 if (f == NULL) {
329 err = got_error_from_errno("fdopen");
330 abort();
331 close(fd);
332 goto done;
334 err = got_inflate_to_fd(&len, f, &csum, outfd);
336 if (err)
337 goto done;
339 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
340 err = got_error(GOT_ERR_BAD_OBJ_HDR);
341 goto done;
344 SHA1Final(sha1, &sha1_ctx);
345 if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
346 err = got_error_checksum(expected_id);
347 goto done;
350 *size = obj->size;
351 *hdrlen = obj->hdrlen;
352 done:
353 got_object_close(obj);
354 if (f && fclose(f) == EOF && err == NULL)
355 err = got_error_from_errno("fclose");
356 return err;
359 struct got_commit_object *
360 got_object_commit_alloc_partial(void)
362 struct got_commit_object *commit;
364 commit = calloc(1, sizeof(*commit));
365 if (commit == NULL)
366 return NULL;
367 commit->tree_id = malloc(sizeof(*commit->tree_id));
368 if (commit->tree_id == NULL) {
369 free(commit);
370 return NULL;
373 STAILQ_INIT(&commit->parent_ids);
375 return commit;
378 const struct got_error *
379 got_object_commit_add_parent(struct got_commit_object *commit,
380 const char *id_str)
382 const struct got_error *err = NULL;
383 struct got_object_qid *qid;
385 err = got_object_qid_alloc_partial(&qid);
386 if (err)
387 return err;
389 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
390 err = got_error(GOT_ERR_BAD_OBJ_DATA);
391 got_object_qid_free(qid);
392 return err;
395 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
396 commit->nparents++;
398 return NULL;
401 static const struct got_error *
402 parse_gmtoff(time_t *gmtoff, const char *tzstr)
404 int sign = 1;
405 const char *p = tzstr;
406 time_t h, m;
408 *gmtoff = 0;
410 if (*p == '-')
411 sign = -1;
412 else if (*p != '+')
413 return got_error(GOT_ERR_BAD_OBJ_DATA);
414 p++;
415 if (!isdigit((unsigned char)*p) &&
416 !isdigit((unsigned char)*(p + 1)))
417 return got_error(GOT_ERR_BAD_OBJ_DATA);
418 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
420 p += 2;
421 if (!isdigit((unsigned char)*p) &&
422 !isdigit((unsigned char)*(p + 1)))
423 return got_error(GOT_ERR_BAD_OBJ_DATA);
424 m = ((*p - '0') * 10) + (*(p + 1) - '0');
426 *gmtoff = (h * 60 * 60 + m * 60) * sign;
427 return NULL;
430 static const struct got_error *
431 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
433 const struct got_error *err = NULL;
434 const char *errstr;
435 char *space, *tzstr;
437 /* Parse and strip off trailing timezone indicator string. */
438 space = strrchr(committer, ' ');
439 if (space == NULL)
440 return got_error(GOT_ERR_BAD_OBJ_DATA);
441 tzstr = strdup(space + 1);
442 if (tzstr == NULL)
443 return got_error_from_errno("strdup");
444 err = parse_gmtoff(gmtoff, tzstr);
445 free(tzstr);
446 if (err) {
447 if (err->code != GOT_ERR_BAD_OBJ_DATA)
448 return err;
449 /* Old versions of Git omitted the timestamp. */
450 *time = 0;
451 *gmtoff = 0;
452 return NULL;
454 *space = '\0';
456 /* Timestamp is separated from committer name + email by space. */
457 space = strrchr(committer, ' ');
458 if (space == NULL)
459 return got_error(GOT_ERR_BAD_OBJ_DATA);
461 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
462 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
463 if (errstr)
464 return got_error(GOT_ERR_BAD_OBJ_DATA);
466 /* Strip off parsed time information, leaving just author and email. */
467 *space = '\0';
469 return NULL;
472 void
473 got_object_commit_close(struct got_commit_object *commit)
475 if (commit->refcnt > 0) {
476 commit->refcnt--;
477 if (commit->refcnt > 0)
478 return;
481 got_object_id_queue_free(&commit->parent_ids);
482 free(commit->tree_id);
483 free(commit->author);
484 free(commit->committer);
485 free(commit->logmsg);
486 free(commit);
489 struct got_object_id *
490 got_object_commit_get_tree_id(struct got_commit_object *commit)
492 return commit->tree_id;
495 int
496 got_object_commit_get_nparents(struct got_commit_object *commit)
498 return commit->nparents;
501 const struct got_object_id_queue *
502 got_object_commit_get_parent_ids(struct got_commit_object *commit)
504 return &commit->parent_ids;
507 const char *
508 got_object_commit_get_author(struct got_commit_object *commit)
510 return commit->author;
513 time_t
514 got_object_commit_get_author_time(struct got_commit_object *commit)
516 return commit->author_time;
519 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
521 return commit->author_gmtoff;
524 const char *
525 got_object_commit_get_committer(struct got_commit_object *commit)
527 return commit->committer;
530 time_t
531 got_object_commit_get_committer_time(struct got_commit_object *commit)
533 return commit->committer_time;
536 time_t
537 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
539 return commit->committer_gmtoff;
542 const struct got_error *
543 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
545 const struct got_error *err = NULL;
546 const char *src;
547 char *dst;
548 size_t len;
550 len = strlen(commit->logmsg);
551 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
552 if (*logmsg == NULL)
553 return got_error_from_errno("malloc");
555 /*
556 * Strip out unusual headers. Headers are separated from the commit
557 * message body by a single empty line.
558 */
559 src = commit->logmsg;
560 dst = *logmsg;
561 while (*src != '\0' && *src != '\n') {
562 int copy_header = 1, eol = 0;
563 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
564 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
565 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
566 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
567 strncmp(src, GOT_COMMIT_LABEL_PARENT,
568 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
569 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
570 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
571 copy_header = 0;
573 while (*src != '\0' && !eol) {
574 if (copy_header) {
575 *dst = *src;
576 dst++;
578 if (*src == '\n')
579 eol = 1;
580 src++;
583 *dst = '\0';
585 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
586 err = got_error(GOT_ERR_NO_SPACE);
587 goto done;
590 /* Trim redundant trailing whitespace. */
591 len = strlen(*logmsg);
592 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
593 isspace((unsigned char)(*logmsg)[len - 1])) {
594 (*logmsg)[len - 1] = '\0';
595 len--;
598 /* Append a trailing newline if missing. */
599 if (len > 0 && (*logmsg)[len - 1] != '\n') {
600 (*logmsg)[len] = '\n';
601 (*logmsg)[len + 1] = '\0';
603 done:
604 if (err) {
605 free(*logmsg);
606 *logmsg = NULL;
608 return err;
611 const char *
612 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
614 return commit->logmsg;
617 const struct got_error *
618 got_object_parse_commit(struct got_commit_object **commit, char *buf,
619 size_t len)
621 const struct got_error *err = NULL;
622 char *s = buf;
623 size_t label_len;
624 ssize_t remain = (ssize_t)len;
626 if (remain == 0)
627 return got_error(GOT_ERR_BAD_OBJ_DATA);
629 *commit = got_object_commit_alloc_partial();
630 if (*commit == NULL)
631 return got_error_from_errno("got_object_commit_alloc_partial");
633 label_len = strlen(GOT_COMMIT_LABEL_TREE);
634 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
635 remain -= label_len;
636 if (remain < SHA1_DIGEST_STRING_LENGTH) {
637 err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 goto done;
640 s += label_len;
641 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
642 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 goto done;
645 remain -= SHA1_DIGEST_STRING_LENGTH;
646 s += SHA1_DIGEST_STRING_LENGTH;
647 } else {
648 err = got_error(GOT_ERR_BAD_OBJ_DATA);
649 goto done;
652 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
653 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
654 remain -= label_len;
655 if (remain < SHA1_DIGEST_STRING_LENGTH) {
656 err = got_error(GOT_ERR_BAD_OBJ_DATA);
657 goto done;
659 s += label_len;
660 err = got_object_commit_add_parent(*commit, s);
661 if (err)
662 goto done;
664 remain -= SHA1_DIGEST_STRING_LENGTH;
665 s += SHA1_DIGEST_STRING_LENGTH;
668 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
669 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
670 char *p;
671 size_t slen;
673 remain -= label_len;
674 if (remain <= 0) {
675 err = got_error(GOT_ERR_BAD_OBJ_DATA);
676 goto done;
678 s += label_len;
679 p = memchr(s, '\n', remain);
680 if (p == NULL) {
681 err = got_error(GOT_ERR_BAD_OBJ_DATA);
682 goto done;
684 *p = '\0';
685 slen = strlen(s);
686 err = parse_commit_time(&(*commit)->author_time,
687 &(*commit)->author_gmtoff, s);
688 if (err)
689 goto done;
690 (*commit)->author = strdup(s);
691 if ((*commit)->author == NULL) {
692 err = got_error_from_errno("strdup");
693 goto done;
695 s += slen + 1;
696 remain -= slen + 1;
699 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
700 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
701 char *p;
702 size_t slen;
704 remain -= label_len;
705 if (remain <= 0) {
706 err = got_error(GOT_ERR_BAD_OBJ_DATA);
707 goto done;
709 s += label_len;
710 p = memchr(s, '\n', remain);
711 if (p == NULL) {
712 err = got_error(GOT_ERR_BAD_OBJ_DATA);
713 goto done;
715 *p = '\0';
716 slen = strlen(s);
717 err = parse_commit_time(&(*commit)->committer_time,
718 &(*commit)->committer_gmtoff, s);
719 if (err)
720 goto done;
721 (*commit)->committer = strdup(s);
722 if ((*commit)->committer == NULL) {
723 err = got_error_from_errno("strdup");
724 goto done;
726 s += slen + 1;
727 remain -= slen + 1;
730 (*commit)->logmsg = strndup(s, remain);
731 if ((*commit)->logmsg == NULL) {
732 err = got_error_from_errno("strndup");
733 goto done;
735 done:
736 if (err) {
737 got_object_commit_close(*commit);
738 *commit = NULL;
740 return err;
743 const struct got_error *
744 got_object_read_commit(struct got_commit_object **commit, int fd,
745 struct got_object_id *expected_id, size_t expected_size)
747 struct got_object *obj = NULL;
748 const struct got_error *err = NULL;
749 size_t len;
750 uint8_t *p;
751 struct got_inflate_checksum csum;
752 SHA1_CTX sha1_ctx;
753 struct got_object_id id;
755 SHA1Init(&sha1_ctx);
756 memset(&csum, 0, sizeof(csum));
757 csum.output_sha1 = &sha1_ctx;
759 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
760 if (err)
761 return err;
763 SHA1Final(id.sha1, &sha1_ctx);
764 if (got_object_id_cmp(expected_id, &id) != 0) {
765 err = got_error_checksum(expected_id);
766 goto done;
769 err = got_object_parse_header(&obj, p, len);
770 if (err)
771 goto done;
773 if (len < obj->hdrlen + obj->size) {
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
775 goto done;
778 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
779 err = got_error(GOT_ERR_OBJ_TYPE);
780 goto done;
783 /* Skip object header. */
784 len -= obj->hdrlen;
785 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
786 done:
787 free(p);
788 if (obj)
789 got_object_close(obj);
790 return err;
793 void
794 got_object_tree_close(struct got_tree_object *tree)
796 if (tree->refcnt > 0) {
797 tree->refcnt--;
798 if (tree->refcnt > 0)
799 return;
802 free(tree->entries);
803 free(tree);
806 static const struct got_error *
807 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
808 size_t maxlen)
810 char *p, *space;
812 *elen = 0;
814 *elen = strnlen(buf, maxlen) + 1;
815 if (*elen > maxlen)
816 return got_error(GOT_ERR_BAD_OBJ_DATA);
818 space = memchr(buf, ' ', *elen);
819 if (space == NULL || space <= buf)
820 return got_error(GOT_ERR_BAD_OBJ_DATA);
822 pte->mode = 0;
823 p = buf;
824 while (p < space) {
825 if (*p < '0' || *p > '7')
826 return got_error(GOT_ERR_BAD_OBJ_DATA);
827 pte->mode <<= 3;
828 pte->mode |= *p - '0';
829 p++;
832 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
833 return got_error(GOT_ERR_BAD_OBJ_DATA);
835 pte->name = space + 1;
836 pte->namelen = strlen(pte->name);
837 buf += *elen;
838 pte->id = buf;
839 *elen += SHA1_DIGEST_LENGTH;
840 return NULL;
843 static int
844 pte_cmp(const void *pa, const void *pb)
846 const struct got_parsed_tree_entry *a = pa, *b = pb;
848 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
851 const struct got_error *
852 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
853 size_t *nentries_alloc, uint8_t *buf, size_t len)
855 const struct got_error *err = NULL;
856 size_t remain = len;
857 const size_t nalloc = 16;
858 struct got_parsed_tree_entry *pte;
859 int i;
861 *nentries = 0;
862 if (remain == 0)
863 return NULL; /* tree is empty */
865 while (remain > 0) {
866 size_t elen;
868 if (*nentries >= *nentries_alloc) {
869 pte = recallocarray(*entries, *nentries_alloc,
870 *nentries_alloc + nalloc, sizeof(**entries));
871 if (pte == NULL) {
872 err = got_error_from_errno("recallocarray");
873 goto done;
875 *entries = pte;
876 *nentries_alloc += nalloc;
879 pte = &(*entries)[*nentries];
880 err = parse_tree_entry(pte, &elen, buf, remain);
881 if (err)
882 goto done;
883 buf += elen;
884 remain -= elen;
885 (*nentries)++;
888 if (remain != 0) {
889 err = got_error(GOT_ERR_BAD_OBJ_DATA);
890 goto done;
893 if (*nentries > 1) {
894 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
896 for (i = 0; i < *nentries - 1; i++) {
897 struct got_parsed_tree_entry *prev = &(*entries)[i];
898 pte = &(*entries)[i + 1];
899 if (got_path_cmp(prev->name, pte->name,
900 prev->namelen, pte->namelen) == 0) {
901 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
902 break;
906 done:
907 if (err)
908 *nentries = 0;
909 return err;
912 const struct got_error *
913 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
914 size_t *nentries_alloc, uint8_t **p, int fd,
915 struct got_object_id *expected_id)
917 const struct got_error *err = NULL;
918 struct got_object *obj = NULL;
919 size_t len;
920 struct got_inflate_checksum csum;
921 SHA1_CTX sha1_ctx;
922 struct got_object_id id;
924 SHA1Init(&sha1_ctx);
925 memset(&csum, 0, sizeof(csum));
926 csum.output_sha1 = &sha1_ctx;
928 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
929 if (err)
930 return err;
932 SHA1Final(id.sha1, &sha1_ctx);
933 if (got_object_id_cmp(expected_id, &id) != 0) {
934 err = got_error_checksum(expected_id);
935 goto done;
938 err = got_object_parse_header(&obj, *p, len);
939 if (err)
940 goto done;
942 if (len < obj->hdrlen + obj->size) {
943 err = got_error(GOT_ERR_BAD_OBJ_DATA);
944 goto done;
947 /* Skip object header. */
948 len -= obj->hdrlen;
949 err = got_object_parse_tree(entries, nentries, nentries_alloc,
950 *p + obj->hdrlen, len);
951 done:
952 if (obj)
953 got_object_close(obj);
954 return err;
957 void
958 got_object_tag_close(struct got_tag_object *tag)
960 if (tag->refcnt > 0) {
961 tag->refcnt--;
962 if (tag->refcnt > 0)
963 return;
966 free(tag->tag);
967 free(tag->tagger);
968 free(tag->tagmsg);
969 free(tag);
972 const struct got_error *
973 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
975 const struct got_error *err = NULL;
976 size_t remain = len;
977 char *s = buf;
978 size_t label_len;
980 if (remain == 0)
981 return got_error(GOT_ERR_BAD_OBJ_DATA);
983 *tag = calloc(1, sizeof(**tag));
984 if (*tag == NULL)
985 return got_error_from_errno("calloc");
987 label_len = strlen(GOT_TAG_LABEL_OBJECT);
988 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
989 remain -= label_len;
990 if (remain < SHA1_DIGEST_STRING_LENGTH) {
991 err = got_error(GOT_ERR_BAD_OBJ_DATA);
992 goto done;
994 s += label_len;
995 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
996 err = got_error(GOT_ERR_BAD_OBJ_DATA);
997 goto done;
999 remain -= SHA1_DIGEST_STRING_LENGTH;
1000 s += SHA1_DIGEST_STRING_LENGTH;
1001 } else {
1002 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1003 goto done;
1006 if (remain <= 0) {
1007 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1008 goto done;
1011 label_len = strlen(GOT_TAG_LABEL_TYPE);
1012 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1013 remain -= label_len;
1014 if (remain <= 0) {
1015 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1016 goto done;
1018 s += label_len;
1019 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1020 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1021 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1022 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1023 s += label_len;
1024 remain -= label_len;
1025 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1026 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1027 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1028 label_len = strlen(GOT_OBJ_LABEL_TREE);
1029 s += label_len;
1030 remain -= label_len;
1031 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1032 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1033 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1034 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1035 s += label_len;
1036 remain -= label_len;
1037 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1038 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1039 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1040 label_len = strlen(GOT_OBJ_LABEL_TAG);
1041 s += label_len;
1042 remain -= label_len;
1043 } else {
1044 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1045 goto done;
1048 if (remain <= 0 || *s != '\n') {
1049 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1050 goto done;
1052 s++;
1053 remain--;
1054 if (remain <= 0) {
1055 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1056 goto done;
1058 } else {
1059 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1060 goto done;
1063 label_len = strlen(GOT_TAG_LABEL_TAG);
1064 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1065 char *p;
1066 size_t slen;
1067 remain -= label_len;
1068 if (remain <= 0) {
1069 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1070 goto done;
1072 s += label_len;
1073 p = memchr(s, '\n', remain);
1074 if (p == NULL) {
1075 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1076 goto done;
1078 *p = '\0';
1079 slen = strlen(s);
1080 (*tag)->tag = strndup(s, slen);
1081 if ((*tag)->tag == NULL) {
1082 err = got_error_from_errno("strndup");
1083 goto done;
1085 s += slen + 1;
1086 remain -= slen + 1;
1087 if (remain <= 0) {
1088 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1089 goto done;
1091 } else {
1092 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1093 goto done;
1096 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1097 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1098 char *p;
1099 size_t slen;
1101 remain -= label_len;
1102 if (remain <= 0) {
1103 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1104 goto done;
1106 s += label_len;
1107 p = memchr(s, '\n', remain);
1108 if (p == NULL) {
1109 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1110 goto done;
1112 *p = '\0';
1113 slen = strlen(s);
1114 err = parse_commit_time(&(*tag)->tagger_time,
1115 &(*tag)->tagger_gmtoff, s);
1116 if (err)
1117 goto done;
1118 (*tag)->tagger = strdup(s);
1119 if ((*tag)->tagger == NULL) {
1120 err = got_error_from_errno("strdup");
1121 goto done;
1123 s += slen + 1;
1124 remain -= slen + 1;
1125 if (remain < 0) {
1126 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1127 goto done;
1129 } else {
1130 /* Some old tags in the Linux git repo have no tagger. */
1131 (*tag)->tagger = strdup("");
1132 if ((*tag)->tagger == NULL) {
1133 err = got_error_from_errno("strdup");
1134 goto done;
1138 (*tag)->tagmsg = strndup(s, remain);
1139 if ((*tag)->tagmsg == NULL) {
1140 err = got_error_from_errno("strndup");
1141 goto done;
1143 done:
1144 if (err) {
1145 got_object_tag_close(*tag);
1146 *tag = NULL;
1148 return err;
1151 const struct got_error *
1152 got_object_read_tag(struct got_tag_object **tag, int fd,
1153 struct got_object_id *expected_id, size_t expected_size)
1155 const struct got_error *err = NULL;
1156 struct got_object *obj = NULL;
1157 size_t len;
1158 uint8_t *p;
1159 struct got_inflate_checksum csum;
1160 SHA1_CTX sha1_ctx;
1161 struct got_object_id id;
1163 SHA1Init(&sha1_ctx);
1164 memset(&csum, 0, sizeof(csum));
1165 csum.output_sha1 = &sha1_ctx;
1167 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1168 expected_size, fd);
1169 if (err)
1170 return err;
1172 SHA1Final(id.sha1, &sha1_ctx);
1173 if (got_object_id_cmp(expected_id, &id) != 0) {
1174 err = got_error_checksum(expected_id);
1175 goto done;
1178 err = got_object_parse_header(&obj, p, len);
1179 if (err)
1180 goto done;
1182 if (len < obj->hdrlen + obj->size) {
1183 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1184 goto done;
1187 /* Skip object header. */
1188 len -= obj->hdrlen;
1189 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1190 done:
1191 free(p);
1192 if (obj)
1193 got_object_close(obj);
1194 return err;
1197 const struct got_error *
1198 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1200 const struct got_error *err = NULL;
1201 static const size_t blocksize = 512;
1202 size_t n, total, remain;
1203 uint8_t *buf;
1205 *outbuf = NULL;
1206 *outlen = 0;
1208 buf = malloc(blocksize);
1209 if (buf == NULL)
1210 return got_error_from_errno("malloc");
1212 remain = blocksize;
1213 total = 0;
1214 for (;;) {
1215 if (remain == 0) {
1216 uint8_t *newbuf;
1217 newbuf = reallocarray(buf, 1, total + blocksize);
1218 if (newbuf == NULL) {
1219 err = got_error_from_errno("reallocarray");
1220 goto done;
1222 buf = newbuf;
1223 remain += blocksize;
1225 n = fread(buf + total, 1, remain, f);
1226 if (n == 0) {
1227 if (ferror(f)) {
1228 err = got_ferror(f, GOT_ERR_IO);
1229 goto done;
1231 break; /* EOF */
1233 remain -= n;
1234 total += n;
1237 done:
1238 if (err == NULL) {
1239 *outbuf = buf;
1240 *outlen = total;
1241 } else
1242 free(buf);
1243 return err;