Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/mman.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha2.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <time.h>
35 #include <unistd.h>
37 #include "got_compat.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_hash.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_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
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 enum got_hash_algorithm algo = GOT_HASH_SHA1;
623 char *s = buf;
624 size_t label_len;
625 ssize_t remain = (ssize_t)len;
627 if (remain == 0)
628 return got_error(GOT_ERR_BAD_OBJ_DATA);
630 *commit = got_object_commit_alloc_partial();
631 if (*commit == NULL)
632 return got_error_from_errno("got_object_commit_alloc_partial");
634 label_len = strlen(GOT_COMMIT_LABEL_TREE);
635 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
636 remain -= label_len;
637 if (remain < SHA1_DIGEST_STRING_LENGTH) {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
639 goto done;
641 s += label_len;
642 if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
643 err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 goto done;
646 remain -= SHA1_DIGEST_STRING_LENGTH;
647 s += SHA1_DIGEST_STRING_LENGTH;
648 } else {
649 err = got_error(GOT_ERR_BAD_OBJ_DATA);
650 goto done;
653 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
654 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
655 remain -= label_len;
656 if (remain < SHA1_DIGEST_STRING_LENGTH) {
657 err = got_error(GOT_ERR_BAD_OBJ_DATA);
658 goto done;
660 s += label_len;
661 err = got_object_commit_add_parent(*commit, s);
662 if (err)
663 goto done;
665 remain -= SHA1_DIGEST_STRING_LENGTH;
666 s += SHA1_DIGEST_STRING_LENGTH;
669 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
670 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
671 char *p;
672 size_t slen;
674 remain -= label_len;
675 if (remain <= 0) {
676 err = got_error(GOT_ERR_BAD_OBJ_DATA);
677 goto done;
679 s += label_len;
680 p = memchr(s, '\n', remain);
681 if (p == NULL) {
682 err = got_error(GOT_ERR_BAD_OBJ_DATA);
683 goto done;
685 *p = '\0';
686 slen = strlen(s);
687 err = parse_commit_time(&(*commit)->author_time,
688 &(*commit)->author_gmtoff, s);
689 if (err)
690 goto done;
691 (*commit)->author = strdup(s);
692 if ((*commit)->author == NULL) {
693 err = got_error_from_errno("strdup");
694 goto done;
696 s += slen + 1;
697 remain -= slen + 1;
700 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
701 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
702 char *p;
703 size_t slen;
705 remain -= label_len;
706 if (remain <= 0) {
707 err = got_error(GOT_ERR_BAD_OBJ_DATA);
708 goto done;
710 s += label_len;
711 p = memchr(s, '\n', remain);
712 if (p == NULL) {
713 err = got_error(GOT_ERR_BAD_OBJ_DATA);
714 goto done;
716 *p = '\0';
717 slen = strlen(s);
718 err = parse_commit_time(&(*commit)->committer_time,
719 &(*commit)->committer_gmtoff, s);
720 if (err)
721 goto done;
722 (*commit)->committer = strdup(s);
723 if ((*commit)->committer == NULL) {
724 err = got_error_from_errno("strdup");
725 goto done;
727 s += slen + 1;
728 remain -= slen + 1;
731 (*commit)->logmsg = strndup(s, remain);
732 if ((*commit)->logmsg == NULL) {
733 err = got_error_from_errno("strndup");
734 goto done;
736 done:
737 if (err) {
738 got_object_commit_close(*commit);
739 *commit = NULL;
741 return err;
744 const struct got_error *
745 got_object_read_commit(struct got_commit_object **commit, int fd,
746 struct got_object_id *expected_id, size_t expected_size)
748 struct got_object *obj = NULL;
749 const struct got_error *err = NULL;
750 size_t len;
751 uint8_t *p;
752 struct got_inflate_checksum csum;
753 SHA1_CTX sha1_ctx;
754 struct got_object_id id;
756 SHA1Init(&sha1_ctx);
757 memset(&csum, 0, sizeof(csum));
758 csum.output_sha1 = &sha1_ctx;
760 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
761 if (err)
762 return err;
764 SHA1Final(id.sha1, &sha1_ctx);
765 if (got_object_id_cmp(expected_id, &id) != 0) {
766 err = got_error_checksum(expected_id);
767 goto done;
770 err = got_object_parse_header(&obj, p, len);
771 if (err)
772 goto done;
774 if (len < obj->hdrlen + obj->size) {
775 err = got_error(GOT_ERR_BAD_OBJ_DATA);
776 goto done;
779 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
780 err = got_error(GOT_ERR_OBJ_TYPE);
781 goto done;
784 /* Skip object header. */
785 len -= obj->hdrlen;
786 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
787 done:
788 free(p);
789 if (obj)
790 got_object_close(obj);
791 return err;
794 void
795 got_object_tree_close(struct got_tree_object *tree)
797 if (tree->refcnt > 0) {
798 tree->refcnt--;
799 if (tree->refcnt > 0)
800 return;
803 free(tree->entries);
804 free(tree);
807 static const struct got_error *
808 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
809 size_t maxlen)
811 char *p, *space;
813 *elen = 0;
815 *elen = strnlen(buf, maxlen) + 1;
816 if (*elen > maxlen)
817 return got_error(GOT_ERR_BAD_OBJ_DATA);
819 space = memchr(buf, ' ', *elen);
820 if (space == NULL || space <= buf)
821 return got_error(GOT_ERR_BAD_OBJ_DATA);
823 pte->mode = 0;
824 p = buf;
825 while (p < space) {
826 if (*p < '0' || *p > '7')
827 return got_error(GOT_ERR_BAD_OBJ_DATA);
828 pte->mode <<= 3;
829 pte->mode |= *p - '0';
830 p++;
833 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
834 return got_error(GOT_ERR_BAD_OBJ_DATA);
836 pte->name = space + 1;
837 pte->namelen = strlen(pte->name);
838 buf += *elen;
839 pte->id = buf;
840 *elen += SHA1_DIGEST_LENGTH;
841 return NULL;
844 static int
845 pte_cmp(const void *pa, const void *pb)
847 const struct got_parsed_tree_entry *a = pa, *b = pb;
849 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
852 const struct got_error *
853 got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
854 size_t *nentries_alloc, uint8_t *buf, size_t len)
856 const struct got_error *err = NULL;
857 size_t remain = len;
858 const size_t nalloc = 16;
859 struct got_parsed_tree_entry *pte;
860 int i;
862 *nentries = 0;
863 if (remain == 0)
864 return NULL; /* tree is empty */
866 while (remain > 0) {
867 size_t elen;
869 if (*nentries >= *nentries_alloc) {
870 pte = recallocarray(*entries, *nentries_alloc,
871 *nentries_alloc + nalloc, sizeof(**entries));
872 if (pte == NULL) {
873 err = got_error_from_errno("recallocarray");
874 goto done;
876 *entries = pte;
877 *nentries_alloc += nalloc;
880 pte = &(*entries)[*nentries];
881 err = parse_tree_entry(pte, &elen, buf, remain);
882 if (err)
883 goto done;
884 buf += elen;
885 remain -= elen;
886 (*nentries)++;
889 if (remain != 0) {
890 err = got_error(GOT_ERR_BAD_OBJ_DATA);
891 goto done;
894 if (*nentries > 1) {
895 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
897 for (i = 0; i < *nentries - 1; i++) {
898 struct got_parsed_tree_entry *prev = &(*entries)[i];
899 pte = &(*entries)[i + 1];
900 if (got_path_cmp(prev->name, pte->name,
901 prev->namelen, pte->namelen) == 0) {
902 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
903 break;
907 done:
908 if (err)
909 *nentries = 0;
910 return err;
913 const struct got_error *
914 got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
915 size_t *nentries_alloc, uint8_t **p, int fd,
916 struct got_object_id *expected_id)
918 const struct got_error *err = NULL;
919 struct got_object *obj = NULL;
920 size_t len;
921 struct got_inflate_checksum csum;
922 SHA1_CTX sha1_ctx;
923 struct got_object_id id;
925 SHA1Init(&sha1_ctx);
926 memset(&csum, 0, sizeof(csum));
927 csum.output_sha1 = &sha1_ctx;
929 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
930 if (err)
931 return err;
933 SHA1Final(id.sha1, &sha1_ctx);
934 if (got_object_id_cmp(expected_id, &id) != 0) {
935 err = got_error_checksum(expected_id);
936 goto done;
939 err = got_object_parse_header(&obj, *p, len);
940 if (err)
941 goto done;
943 if (len < obj->hdrlen + obj->size) {
944 err = got_error(GOT_ERR_BAD_OBJ_DATA);
945 goto done;
948 /* Skip object header. */
949 len -= obj->hdrlen;
950 err = got_object_parse_tree(entries, nentries, nentries_alloc,
951 *p + obj->hdrlen, len);
952 done:
953 if (obj)
954 got_object_close(obj);
955 return err;
958 void
959 got_object_tag_close(struct got_tag_object *tag)
961 if (tag->refcnt > 0) {
962 tag->refcnt--;
963 if (tag->refcnt > 0)
964 return;
967 free(tag->tag);
968 free(tag->tagger);
969 free(tag->tagmsg);
970 free(tag);
973 const struct got_error *
974 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
976 const struct got_error *err = NULL;
977 enum got_hash_algorithm algo = GOT_HASH_SHA1;
978 size_t remain = len;
979 char *s = buf;
980 size_t label_len;
982 if (remain == 0)
983 return got_error(GOT_ERR_BAD_OBJ_DATA);
985 *tag = calloc(1, sizeof(**tag));
986 if (*tag == NULL)
987 return got_error_from_errno("calloc");
989 label_len = strlen(GOT_TAG_LABEL_OBJECT);
990 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
991 remain -= label_len;
992 if (remain < SHA1_DIGEST_STRING_LENGTH) {
993 err = got_error(GOT_ERR_BAD_OBJ_DATA);
994 goto done;
996 s += label_len;
997 if (!got_parse_object_id(&(*tag)->id, s, algo)) {
998 err = got_error(GOT_ERR_BAD_OBJ_DATA);
999 goto done;
1001 remain -= SHA1_DIGEST_STRING_LENGTH;
1002 s += SHA1_DIGEST_STRING_LENGTH;
1003 } else {
1004 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1005 goto done;
1008 if (remain <= 0) {
1009 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1010 goto done;
1013 label_len = strlen(GOT_TAG_LABEL_TYPE);
1014 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1015 remain -= label_len;
1016 if (remain <= 0) {
1017 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1018 goto done;
1020 s += label_len;
1021 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1022 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1023 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1024 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1025 s += label_len;
1026 remain -= label_len;
1027 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1028 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1029 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1030 label_len = strlen(GOT_OBJ_LABEL_TREE);
1031 s += label_len;
1032 remain -= label_len;
1033 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1034 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1035 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1036 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1037 s += label_len;
1038 remain -= label_len;
1039 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1040 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1041 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1042 label_len = strlen(GOT_OBJ_LABEL_TAG);
1043 s += label_len;
1044 remain -= label_len;
1045 } else {
1046 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1047 goto done;
1050 if (remain <= 0 || *s != '\n') {
1051 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1052 goto done;
1054 s++;
1055 remain--;
1056 if (remain <= 0) {
1057 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1058 goto done;
1060 } else {
1061 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 goto done;
1065 label_len = strlen(GOT_TAG_LABEL_TAG);
1066 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1067 char *p;
1068 size_t slen;
1069 remain -= label_len;
1070 if (remain <= 0) {
1071 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1072 goto done;
1074 s += label_len;
1075 p = memchr(s, '\n', remain);
1076 if (p == NULL) {
1077 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1078 goto done;
1080 *p = '\0';
1081 slen = strlen(s);
1082 (*tag)->tag = strndup(s, slen);
1083 if ((*tag)->tag == NULL) {
1084 err = got_error_from_errno("strndup");
1085 goto done;
1087 s += slen + 1;
1088 remain -= slen + 1;
1089 if (remain <= 0) {
1090 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1091 goto done;
1093 } else {
1094 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1095 goto done;
1098 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1099 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1100 char *p;
1101 size_t slen;
1103 remain -= label_len;
1104 if (remain <= 0) {
1105 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1106 goto done;
1108 s += label_len;
1109 p = memchr(s, '\n', remain);
1110 if (p == NULL) {
1111 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1112 goto done;
1114 *p = '\0';
1115 slen = strlen(s);
1116 err = parse_commit_time(&(*tag)->tagger_time,
1117 &(*tag)->tagger_gmtoff, s);
1118 if (err)
1119 goto done;
1120 (*tag)->tagger = strdup(s);
1121 if ((*tag)->tagger == NULL) {
1122 err = got_error_from_errno("strdup");
1123 goto done;
1125 s += slen + 1;
1126 remain -= slen + 1;
1127 if (remain < 0) {
1128 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1129 goto done;
1131 } else {
1132 /* Some old tags in the Linux git repo have no tagger. */
1133 (*tag)->tagger = strdup("");
1134 if ((*tag)->tagger == NULL) {
1135 err = got_error_from_errno("strdup");
1136 goto done;
1140 (*tag)->tagmsg = strndup(s, remain);
1141 if ((*tag)->tagmsg == NULL) {
1142 err = got_error_from_errno("strndup");
1143 goto done;
1145 done:
1146 if (err) {
1147 got_object_tag_close(*tag);
1148 *tag = NULL;
1150 return err;
1153 const struct got_error *
1154 got_object_read_tag(struct got_tag_object **tag, int fd,
1155 struct got_object_id *expected_id, size_t expected_size)
1157 const struct got_error *err = NULL;
1158 struct got_object *obj = NULL;
1159 size_t len;
1160 uint8_t *p;
1161 struct got_inflate_checksum csum;
1162 SHA1_CTX sha1_ctx;
1163 struct got_object_id id;
1165 SHA1Init(&sha1_ctx);
1166 memset(&csum, 0, sizeof(csum));
1167 csum.output_sha1 = &sha1_ctx;
1169 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1170 expected_size, fd);
1171 if (err)
1172 return err;
1174 SHA1Final(id.sha1, &sha1_ctx);
1175 if (got_object_id_cmp(expected_id, &id) != 0) {
1176 err = got_error_checksum(expected_id);
1177 goto done;
1180 err = got_object_parse_header(&obj, p, len);
1181 if (err)
1182 goto done;
1184 if (len < obj->hdrlen + obj->size) {
1185 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1186 goto done;
1189 /* Skip object header. */
1190 len -= obj->hdrlen;
1191 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1192 done:
1193 free(p);
1194 if (obj)
1195 got_object_close(obj);
1196 return err;
1199 const struct got_error *
1200 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1202 const struct got_error *err = NULL;
1203 static const size_t blocksize = 512;
1204 size_t n, total, remain;
1205 uint8_t *buf;
1207 *outbuf = NULL;
1208 *outlen = 0;
1210 buf = malloc(blocksize);
1211 if (buf == NULL)
1212 return got_error_from_errno("malloc");
1214 remain = blocksize;
1215 total = 0;
1216 for (;;) {
1217 if (remain == 0) {
1218 uint8_t *newbuf;
1219 newbuf = reallocarray(buf, 1, total + blocksize);
1220 if (newbuf == NULL) {
1221 err = got_error_from_errno("reallocarray");
1222 goto done;
1224 buf = newbuf;
1225 remain += blocksize;
1227 n = fread(buf + total, 1, remain, f);
1228 if (n == 0) {
1229 if (ferror(f)) {
1230 err = got_ferror(f, GOT_ERR_IO);
1231 goto done;
1233 break; /* EOF */
1235 remain -= n;
1236 total += n;
1239 done:
1240 if (err == NULL) {
1241 *outbuf = buf;
1242 *outlen = total;
1243 } else
1244 free(buf);
1245 return err;