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->hash, id2->hash, 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 uint8_t sha1[SHA1_DIGEST_LENGTH];
294 SHA1_CTX sha1_ctx;
295 size_t len, consumed;
296 FILE *f = NULL;
298 *outbuf = NULL;
299 *size = 0;
300 *hdrlen = 0;
302 SHA1Init(&sha1_ctx);
303 memset(&csum, 0, sizeof(csum));
304 csum.output_sha1 = &sha1_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 SHA1Final(sha1, &sha1_ctx);
346 if (memcmp(expected_id->hash, sha1, SHA1_DIGEST_LENGTH) != 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_sha1_digest(qid->id.hash, id_str)) {
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 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_sha1_digest((*commit)->tree_id->hash, s)) {
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(&hash_ctx);
757 memset(&csum, 0, sizeof(csum));
758 csum.output_hash = &hash_ctx;
760 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
761 if (err)
762 return err;
764 SHA1Final(id.hash, &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.hash, &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 size_t remain = len;
978 char *s = buf;
979 size_t label_len;
981 if (remain == 0)
982 return got_error(GOT_ERR_BAD_OBJ_DATA);
984 *tag = calloc(1, sizeof(**tag));
985 if (*tag == NULL)
986 return got_error_from_errno("calloc");
988 label_len = strlen(GOT_TAG_LABEL_OBJECT);
989 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
990 remain -= label_len;
991 if (remain < SHA1_DIGEST_STRING_LENGTH) {
992 err = got_error(GOT_ERR_BAD_OBJ_DATA);
993 goto done;
995 s += label_len;
996 if (!got_parse_sha1_digest((*tag)->id.hash, s)) {
997 err = got_error(GOT_ERR_BAD_OBJ_DATA);
998 goto done;
1000 remain -= SHA1_DIGEST_STRING_LENGTH;
1001 s += SHA1_DIGEST_STRING_LENGTH;
1002 } else {
1003 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1004 goto done;
1007 if (remain <= 0) {
1008 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1009 goto done;
1012 label_len = strlen(GOT_TAG_LABEL_TYPE);
1013 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1014 remain -= label_len;
1015 if (remain <= 0) {
1016 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1017 goto done;
1019 s += label_len;
1020 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1021 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1022 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1023 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1024 s += label_len;
1025 remain -= label_len;
1026 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1027 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1028 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1029 label_len = strlen(GOT_OBJ_LABEL_TREE);
1030 s += label_len;
1031 remain -= label_len;
1032 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1033 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1034 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1035 label_len = strlen(GOT_OBJ_LABEL_BLOB);
1036 s += label_len;
1037 remain -= label_len;
1038 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1039 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1040 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1041 label_len = strlen(GOT_OBJ_LABEL_TAG);
1042 s += label_len;
1043 remain -= label_len;
1044 } else {
1045 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1046 goto done;
1049 if (remain <= 0 || *s != '\n') {
1050 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 goto done;
1053 s++;
1054 remain--;
1055 if (remain <= 0) {
1056 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1057 goto done;
1059 } else {
1060 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1061 goto done;
1064 label_len = strlen(GOT_TAG_LABEL_TAG);
1065 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1066 char *p;
1067 size_t slen;
1068 remain -= label_len;
1069 if (remain <= 0) {
1070 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1071 goto done;
1073 s += label_len;
1074 p = memchr(s, '\n', remain);
1075 if (p == NULL) {
1076 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1077 goto done;
1079 *p = '\0';
1080 slen = strlen(s);
1081 (*tag)->tag = strndup(s, slen);
1082 if ((*tag)->tag == NULL) {
1083 err = got_error_from_errno("strndup");
1084 goto done;
1086 s += slen + 1;
1087 remain -= slen + 1;
1088 if (remain <= 0) {
1089 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1090 goto done;
1092 } else {
1093 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1094 goto done;
1097 label_len = strlen(GOT_TAG_LABEL_TAGGER);
1098 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1099 char *p;
1100 size_t slen;
1102 remain -= label_len;
1103 if (remain <= 0) {
1104 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1105 goto done;
1107 s += label_len;
1108 p = memchr(s, '\n', remain);
1109 if (p == NULL) {
1110 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1111 goto done;
1113 *p = '\0';
1114 slen = strlen(s);
1115 err = parse_commit_time(&(*tag)->tagger_time,
1116 &(*tag)->tagger_gmtoff, s);
1117 if (err)
1118 goto done;
1119 (*tag)->tagger = strdup(s);
1120 if ((*tag)->tagger == NULL) {
1121 err = got_error_from_errno("strdup");
1122 goto done;
1124 s += slen + 1;
1125 remain -= slen + 1;
1126 if (remain < 0) {
1127 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1128 goto done;
1130 } else {
1131 /* Some old tags in the Linux git repo have no tagger. */
1132 (*tag)->tagger = strdup("");
1133 if ((*tag)->tagger == NULL) {
1134 err = got_error_from_errno("strdup");
1135 goto done;
1139 (*tag)->tagmsg = strndup(s, remain);
1140 if ((*tag)->tagmsg == NULL) {
1141 err = got_error_from_errno("strndup");
1142 goto done;
1144 done:
1145 if (err) {
1146 got_object_tag_close(*tag);
1147 *tag = NULL;
1149 return err;
1152 const struct got_error *
1153 got_object_read_tag(struct got_tag_object **tag, int fd,
1154 struct got_object_id *expected_id, size_t expected_size)
1156 const struct got_error *err = NULL;
1157 struct got_object *obj = NULL;
1158 size_t len;
1159 uint8_t *p;
1160 struct got_inflate_checksum csum;
1161 SHA1_CTX sha1_ctx;
1162 struct got_object_id id;
1164 SHA1Init(&sha1_ctx);
1165 memset(&csum, 0, sizeof(csum));
1166 csum.output_sha1 = &sha1_ctx;
1168 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1169 expected_size, fd);
1170 if (err)
1171 return err;
1173 SHA1Final(id.hash, &sha1_ctx);
1174 if (got_object_id_cmp(expected_id, &id) != 0) {
1175 err = got_error_checksum(expected_id);
1176 goto done;
1179 err = got_object_parse_header(&obj, p, len);
1180 if (err)
1181 goto done;
1183 if (len < obj->hdrlen + obj->size) {
1184 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1185 goto done;
1188 /* Skip object header. */
1189 len -= obj->hdrlen;
1190 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1191 done:
1192 free(p);
1193 if (obj)
1194 got_object_close(obj);
1195 return err;
1198 const struct got_error *
1199 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1201 const struct got_error *err = NULL;
1202 static const size_t blocksize = 512;
1203 size_t n, total, remain;
1204 uint8_t *buf;
1206 *outbuf = NULL;
1207 *outlen = 0;
1209 buf = malloc(blocksize);
1210 if (buf == NULL)
1211 return got_error_from_errno("malloc");
1213 remain = blocksize;
1214 total = 0;
1215 for (;;) {
1216 if (remain == 0) {
1217 uint8_t *newbuf;
1218 newbuf = reallocarray(buf, 1, total + blocksize);
1219 if (newbuf == NULL) {
1220 err = got_error_from_errno("reallocarray");
1221 goto done;
1223 buf = newbuf;
1224 remain += blocksize;
1226 n = fread(buf + total, 1, remain, f);
1227 if (n == 0) {
1228 if (ferror(f)) {
1229 err = got_ferror(f, GOT_ERR_IO);
1230 goto done;
1232 break; /* EOF */
1234 remain -= n;
1235 total += n;
1238 done:
1239 if (err == NULL) {
1240 *outbuf = buf;
1241 *outlen = total;
1242 } else
1243 free(buf);
1244 return err;