portable: configure: use config.h
[got-portable.git] / lib / object_parse.c
blob6d050459574522d3b15ab4d56ebd14f4c5c2bf9c
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
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 <zlib.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <time.h>
34 #include <unistd.h>
36 #include "got_compat.h"
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
42 #include "got_path.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_repository.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 struct got_object_id *
58 got_object_id_dup(struct got_object_id *id1)
60 struct got_object_id *id2;
62 id2 = malloc(sizeof(*id2));
63 if (id2 == NULL)
64 return NULL;
65 memcpy(id2, id1, sizeof(*id2));
66 return id2;
69 int
70 got_object_id_cmp(const struct got_object_id *id1,
71 const struct got_object_id *id2)
73 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
76 const struct got_error *
77 got_object_qid_alloc_partial(struct got_object_qid **qid)
79 *qid = malloc(sizeof(**qid));
80 if (*qid == NULL)
81 return got_error_from_errno("malloc");
83 (*qid)->data = NULL;
84 return NULL;
87 const struct got_error *
88 got_object_id_str(char **outbuf, struct got_object_id *id)
90 static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN;
92 *outbuf = malloc(len);
93 if (*outbuf == NULL)
94 return got_error_from_errno("malloc");
96 if (got_object_id_hex(id, *outbuf, len) == NULL) {
97 free(*outbuf);
98 *outbuf = NULL;
99 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
102 return NULL;
105 char *
106 got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
108 return got_sha1_digest_to_str(id->sha1, buf, len);
111 void
112 got_object_close(struct got_object *obj)
114 if (obj->refcnt > 0) {
115 obj->refcnt--;
116 if (obj->refcnt > 0)
117 return;
120 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
121 struct got_delta *delta;
122 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
123 delta = STAILQ_FIRST(&obj->deltas.entries);
124 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
125 free(delta);
128 free(obj);
131 const struct got_error *
132 got_object_raw_close(struct got_raw_object *obj)
134 const struct got_error *err = NULL;
136 if (obj->refcnt > 0) {
137 obj->refcnt--;
138 if (obj->refcnt > 0)
139 return NULL;
142 if (obj->close_cb)
143 obj->close_cb(obj);
145 if (obj->f == NULL) {
146 if (obj->fd != -1) {
147 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
148 err = got_error_from_errno("munmap");
149 if (close(obj->fd) == -1 && err == NULL)
150 err = got_error_from_errno("close");
151 } else
152 free(obj->data);
153 } else {
154 if (fclose(obj->f) == EOF && err == NULL)
155 err = got_error_from_errno("fclose");
157 free(obj);
158 return err;
161 void
162 got_object_qid_free(struct got_object_qid *qid)
164 free(qid);
167 void
168 got_object_id_queue_free(struct got_object_id_queue *ids)
170 struct got_object_qid *qid;
172 while (!STAILQ_EMPTY(ids)) {
173 qid = STAILQ_FIRST(ids);
174 STAILQ_REMOVE_HEAD(ids, entry);
175 got_object_qid_free(qid);
179 const struct got_error *
180 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
182 const char *obj_labels[] = {
183 GOT_OBJ_LABEL_COMMIT,
184 GOT_OBJ_LABEL_TREE,
185 GOT_OBJ_LABEL_BLOB,
186 GOT_OBJ_LABEL_TAG,
188 const int obj_types[] = {
189 GOT_OBJ_TYPE_COMMIT,
190 GOT_OBJ_TYPE_TREE,
191 GOT_OBJ_TYPE_BLOB,
192 GOT_OBJ_TYPE_TAG,
194 int type = 0;
195 size_t size = 0;
196 size_t i;
197 char *end;
199 *obj = NULL;
201 end = memchr(buf, '\0', len);
202 if (end == NULL)
203 return got_error(GOT_ERR_BAD_OBJ_HDR);
205 for (i = 0; i < nitems(obj_labels); i++) {
206 const char *label = obj_labels[i];
207 size_t label_len = strlen(label);
208 const char *errstr;
210 if (len <= label_len || buf + label_len >= end ||
211 strncmp(buf, label, label_len) != 0)
212 continue;
214 type = obj_types[i];
215 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
216 if (errstr != NULL)
217 return got_error(GOT_ERR_BAD_OBJ_HDR);
218 break;
221 if (type == 0)
222 return got_error(GOT_ERR_BAD_OBJ_HDR);
224 *obj = calloc(1, sizeof(**obj));
225 if (*obj == NULL)
226 return got_error_from_errno("calloc");
227 (*obj)->type = type;
228 (*obj)->hdrlen = end - buf + 1;
229 (*obj)->size = size;
230 return NULL;
233 const struct got_error *
234 got_object_read_header(struct got_object **obj, int fd)
236 const struct got_error *err;
237 struct got_inflate_buf zb;
238 uint8_t *buf;
239 const size_t zbsize = 64;
240 size_t outlen, totlen;
241 int nbuf = 1;
243 *obj = NULL;
245 buf = malloc(zbsize);
246 if (buf == NULL)
247 return got_error_from_errno("malloc");
248 buf[0] = '\0';
250 err = got_inflate_init(&zb, buf, zbsize, NULL);
251 if (err)
252 return err;
254 totlen = 0;
255 do {
256 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
257 if (err)
258 goto done;
259 if (outlen == 0)
260 break;
261 totlen += outlen;
262 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
263 uint8_t *newbuf;
264 nbuf++;
265 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
266 if (newbuf == NULL) {
267 err = got_error_from_errno("recallocarray");
268 goto done;
270 buf = newbuf;
271 zb.outbuf = newbuf + totlen;
272 zb.outlen = (nbuf * zbsize) - totlen;
274 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
276 err = got_object_parse_header(obj, buf, totlen);
277 done:
278 free(buf);
279 got_inflate_end(&zb);
280 return err;
283 const struct got_error *
284 got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
285 size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
286 int infd)
288 const struct got_error *err = NULL;
289 struct got_object *obj;
290 struct got_inflate_checksum csum;
291 struct got_object_id id;
292 struct got_hash ctx;
293 size_t len, consumed;
294 FILE *f = NULL;
296 *outbuf = NULL;
297 *size = 0;
298 *hdrlen = 0;
300 got_hash_init(&ctx, GOT_HASH_SHA1);
301 memset(&csum, 0, sizeof(csum));
302 csum.output_ctx = &ctx;
304 if (lseek(infd, SEEK_SET, 0) == -1)
305 return got_error_from_errno("lseek");
307 err = got_object_read_header(&obj, infd);
308 if (err)
309 return err;
311 if (lseek(infd, SEEK_SET, 0) == -1)
312 return got_error_from_errno("lseek");
314 if (obj->size + obj->hdrlen <= max_in_mem_size) {
315 err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
316 obj->size + obj->hdrlen, infd);
317 } else {
318 int fd;
320 * XXX This uses an extra file descriptor for no good reason.
321 * We should have got_inflate_fd_to_fd().
323 fd = dup(infd);
324 if (fd == -1)
325 return got_error_from_errno("dup");
326 f = fdopen(fd, "r");
327 if (f == NULL) {
328 err = got_error_from_errno("fdopen");
329 abort();
330 close(fd);
331 goto done;
333 err = got_inflate_to_fd(&len, f, &csum, outfd);
335 if (err)
336 goto done;
338 if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
339 err = got_error(GOT_ERR_BAD_OBJ_HDR);
340 goto done;
343 got_hash_final_object_id(&ctx, &id);
344 if (got_object_id_cmp(expected_id, &id) != 0) {
345 err = got_error_checksum(expected_id);
346 goto done;
349 *size = obj->size;
350 *hdrlen = obj->hdrlen;
351 done:
352 got_object_close(obj);
353 if (f && fclose(f) == EOF && err == NULL)
354 err = got_error_from_errno("fclose");
355 return err;
358 struct got_commit_object *
359 got_object_commit_alloc_partial(void)
361 struct got_commit_object *commit;
363 commit = calloc(1, sizeof(*commit));
364 if (commit == NULL)
365 return NULL;
366 commit->tree_id = malloc(sizeof(*commit->tree_id));
367 if (commit->tree_id == NULL) {
368 free(commit);
369 return NULL;
372 STAILQ_INIT(&commit->parent_ids);
374 return commit;
377 const struct got_error *
378 got_object_commit_add_parent(struct got_commit_object *commit,
379 const char *id_str)
381 const struct got_error *err = NULL;
382 struct got_object_qid *qid;
384 err = got_object_qid_alloc_partial(&qid);
385 if (err)
386 return err;
388 if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
389 err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 got_object_qid_free(qid);
391 return err;
394 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
395 commit->nparents++;
397 return NULL;
400 static const struct got_error *
401 parse_gmtoff(time_t *gmtoff, const char *tzstr)
403 int sign = 1;
404 const char *p = tzstr;
405 time_t h, m;
407 *gmtoff = 0;
409 if (*p == '-')
410 sign = -1;
411 else if (*p != '+')
412 return got_error(GOT_ERR_BAD_OBJ_DATA);
413 p++;
414 if (!isdigit((unsigned char)*p) &&
415 !isdigit((unsigned char)*(p + 1)))
416 return got_error(GOT_ERR_BAD_OBJ_DATA);
417 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
419 p += 2;
420 if (!isdigit((unsigned char)*p) &&
421 !isdigit((unsigned char)*(p + 1)))
422 return got_error(GOT_ERR_BAD_OBJ_DATA);
423 m = ((*p - '0') * 10) + (*(p + 1) - '0');
425 *gmtoff = (h * 60 * 60 + m * 60) * sign;
426 return NULL;
429 static const struct got_error *
430 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
432 const struct got_error *err = NULL;
433 const char *errstr;
434 char *space, *tzstr;
436 /* Parse and strip off trailing timezone indicator string. */
437 space = strrchr(committer, ' ');
438 if (space == NULL)
439 return got_error(GOT_ERR_BAD_OBJ_DATA);
440 tzstr = strdup(space + 1);
441 if (tzstr == NULL)
442 return got_error_from_errno("strdup");
443 err = parse_gmtoff(gmtoff, tzstr);
444 free(tzstr);
445 if (err) {
446 if (err->code != GOT_ERR_BAD_OBJ_DATA)
447 return err;
448 /* Old versions of Git omitted the timestamp. */
449 *time = 0;
450 *gmtoff = 0;
451 return NULL;
453 *space = '\0';
455 /* Timestamp is separated from committer name + email by space. */
456 space = strrchr(committer, ' ');
457 if (space == NULL)
458 return got_error(GOT_ERR_BAD_OBJ_DATA);
460 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
461 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
462 if (errstr)
463 return got_error(GOT_ERR_BAD_OBJ_DATA);
465 /* Strip off parsed time information, leaving just author and email. */
466 *space = '\0';
468 return NULL;
471 void
472 got_object_commit_close(struct got_commit_object *commit)
474 if (commit->refcnt > 0) {
475 commit->refcnt--;
476 if (commit->refcnt > 0)
477 return;
480 got_object_id_queue_free(&commit->parent_ids);
481 free(commit->tree_id);
482 free(commit->author);
483 free(commit->committer);
484 free(commit->logmsg);
485 free(commit);
488 struct got_object_id *
489 got_object_commit_get_tree_id(struct got_commit_object *commit)
491 return commit->tree_id;
495 got_object_commit_get_nparents(struct got_commit_object *commit)
497 return commit->nparents;
500 const struct got_object_id_queue *
501 got_object_commit_get_parent_ids(struct got_commit_object *commit)
503 return &commit->parent_ids;
506 const char *
507 got_object_commit_get_author(struct got_commit_object *commit)
509 return commit->author;
512 time_t
513 got_object_commit_get_author_time(struct got_commit_object *commit)
515 return commit->author_time;
518 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
520 return commit->author_gmtoff;
523 const char *
524 got_object_commit_get_committer(struct got_commit_object *commit)
526 return commit->committer;
529 time_t
530 got_object_commit_get_committer_time(struct got_commit_object *commit)
532 return commit->committer_time;
535 time_t
536 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
538 return commit->committer_gmtoff;
541 const struct got_error *
542 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
544 const struct got_error *err = NULL;
545 const char *src;
546 char *dst;
547 size_t len;
549 len = strlen(commit->logmsg);
550 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
551 if (*logmsg == NULL)
552 return got_error_from_errno("malloc");
555 * Strip out unusual headers. Headers are separated from the commit
556 * message body by a single empty line.
558 src = commit->logmsg;
559 dst = *logmsg;
560 while (*src != '\0' && *src != '\n') {
561 int copy_header = 1, eol = 0;
562 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
563 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
564 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
565 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
566 strncmp(src, GOT_COMMIT_LABEL_PARENT,
567 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
568 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
569 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
570 copy_header = 0;
572 while (*src != '\0' && !eol) {
573 if (copy_header) {
574 *dst = *src;
575 dst++;
577 if (*src == '\n')
578 eol = 1;
579 src++;
582 *dst = '\0';
584 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
585 err = got_error(GOT_ERR_NO_SPACE);
586 goto done;
589 /* Trim redundant trailing whitespace. */
590 len = strlen(*logmsg);
591 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
592 isspace((unsigned char)(*logmsg)[len - 1])) {
593 (*logmsg)[len - 1] = '\0';
594 len--;
597 /* Append a trailing newline if missing. */
598 if (len > 0 && (*logmsg)[len - 1] != '\n') {
599 (*logmsg)[len] = '\n';
600 (*logmsg)[len + 1] = '\0';
602 done:
603 if (err) {
604 free(*logmsg);
605 *logmsg = NULL;
607 return err;
610 const char *
611 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
613 return commit->logmsg;
616 const struct got_error *
617 got_object_parse_commit(struct got_commit_object **commit, char *buf,
618 size_t len)
620 const struct got_error *err = NULL;
621 enum got_hash_algorithm algo = GOT_HASH_SHA1;
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_object_id((*commit)->tree_id, s, algo)) {
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 struct got_hash ctx;
753 struct got_object_id id;
755 got_hash_init(&ctx, GOT_HASH_SHA1);
756 memset(&csum, 0, sizeof(csum));
757 csum.output_ctx = &ctx;
759 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
760 if (err)
761 return err;
763 got_hash_final_object_id(&ctx, &id);
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 struct got_hash ctx;
922 struct got_object_id id;
924 got_hash_init(&ctx, GOT_HASH_SHA1);
925 memset(&csum, 0, sizeof(csum));
926 csum.output_ctx = &ctx;
928 err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
929 if (err)
930 return err;
932 got_hash_final_object_id(&ctx, &id);
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 enum got_hash_algorithm algo = GOT_HASH_SHA1;
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_object_id(&(*tag)->id, s, algo)) {
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 struct got_hash ctx;
1162 struct got_object_id id;
1164 got_hash_init(&ctx, GOT_HASH_SHA1);
1165 memset(&csum, 0, sizeof(csum));
1166 csum.output_ctx = &ctx;
1168 err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1169 expected_size, fd);
1170 if (err)
1171 return err;
1173 got_hash_final_object_id(&ctx, &id);
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;