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_privsep.h"
53 #include "got_lib_repository.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #endif
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
61 {
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
65 if (id2 == NULL)
66 return NULL;
67 memcpy(id2, id1, sizeof(*id2));
68 return id2;
69 }
71 int
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
74 {
75 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
76 }
78 const struct got_error *
79 got_object_qid_alloc_partial(struct got_object_qid **qid)
80 {
81 const struct got_error *err = NULL;
83 *qid = malloc(sizeof(**qid));
84 if (*qid == NULL)
85 return got_error_from_errno("malloc");
87 (*qid)->id = malloc(sizeof(*((*qid)->id)));
88 if ((*qid)->id == NULL) {
89 err = got_error_from_errno("malloc");
90 got_object_qid_free(*qid);
91 *qid = NULL;
92 return err;
93 }
94 (*qid)->data = NULL;
96 return NULL;
97 }
99 const struct got_error *
100 got_object_id_str(char **outbuf, struct got_object_id *id)
102 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
104 *outbuf = malloc(len);
105 if (*outbuf == NULL)
106 return got_error_from_errno("malloc");
108 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
109 free(*outbuf);
110 *outbuf = NULL;
111 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
114 return NULL;
117 void
118 got_object_close(struct got_object *obj)
120 if (obj->refcnt > 0) {
121 obj->refcnt--;
122 if (obj->refcnt > 0)
123 return;
126 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
127 struct got_delta *delta;
128 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
129 delta = STAILQ_FIRST(&obj->deltas.entries);
130 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
131 free(delta);
134 free(obj);
137 const struct got_error *
138 got_object_raw_close(struct got_raw_object *obj)
140 const struct got_error *err = NULL;
142 if (obj->refcnt > 0) {
143 obj->refcnt--;
144 if (obj->refcnt > 0)
145 return NULL;
148 if (obj->f == NULL) {
149 if (obj->fd != -1) {
150 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
151 err = got_error_from_errno("munmap");
152 if (close(obj->fd) == -1 && err == NULL)
153 err = got_error_from_errno("close");
154 } else
155 free(obj->data);
156 } else {
157 if (fclose(obj->f) == EOF && err == NULL)
158 err = got_error_from_errno("fclose");
160 free(obj);
161 return err;
164 void
165 got_object_qid_free(struct got_object_qid *qid)
167 free(qid->id);
168 free(qid);
171 void
172 got_object_id_queue_free(struct got_object_id_queue *ids)
174 struct got_object_qid *qid;
176 while (!STAILQ_EMPTY(ids)) {
177 qid = STAILQ_FIRST(ids);
178 STAILQ_REMOVE_HEAD(ids, entry);
179 got_object_qid_free(qid);
183 const struct got_error *
184 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
186 const char *obj_labels[] = {
187 GOT_OBJ_LABEL_COMMIT,
188 GOT_OBJ_LABEL_TREE,
189 GOT_OBJ_LABEL_BLOB,
190 GOT_OBJ_LABEL_TAG,
191 };
192 const int obj_types[] = {
193 GOT_OBJ_TYPE_COMMIT,
194 GOT_OBJ_TYPE_TREE,
195 GOT_OBJ_TYPE_BLOB,
196 GOT_OBJ_TYPE_TAG,
197 };
198 int type = 0;
199 size_t size = 0;
200 size_t i;
201 char *end;
203 *obj = NULL;
205 end = memchr(buf, '\0', len);
206 if (end == NULL)
207 return got_error(GOT_ERR_BAD_OBJ_HDR);
209 for (i = 0; i < nitems(obj_labels); i++) {
210 const char *label = obj_labels[i];
211 size_t label_len = strlen(label);
212 const char *errstr;
214 if (len <= label_len || buf + label_len >= end ||
215 strncmp(buf, label, label_len) != 0)
216 continue;
218 type = obj_types[i];
219 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
220 if (errstr != NULL)
221 return got_error(GOT_ERR_BAD_OBJ_HDR);
222 break;
225 if (type == 0)
226 return got_error(GOT_ERR_BAD_OBJ_HDR);
228 *obj = calloc(1, sizeof(**obj));
229 if (*obj == NULL)
230 return got_error_from_errno("calloc");
231 (*obj)->type = type;
232 (*obj)->hdrlen = end - buf + 1;
233 (*obj)->size = size;
234 return NULL;
237 const struct got_error *
238 got_object_read_header(struct got_object **obj, int fd)
240 const struct got_error *err;
241 struct got_inflate_buf zb;
242 uint8_t *buf;
243 const size_t zbsize = 64;
244 size_t outlen, totlen;
245 int nbuf = 1;
247 *obj = NULL;
249 buf = malloc(zbsize);
250 if (buf == NULL)
251 return got_error_from_errno("malloc");
252 buf[0] = '\0';
254 err = got_inflate_init(&zb, buf, zbsize, NULL);
255 if (err)
256 return err;
258 totlen = 0;
259 do {
260 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
261 if (err)
262 goto done;
263 if (outlen == 0)
264 break;
265 totlen += outlen;
266 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
267 uint8_t *newbuf;
268 nbuf++;
269 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
270 if (newbuf == NULL) {
271 err = got_error_from_errno("recallocarray");
272 goto done;
274 buf = newbuf;
275 zb.outbuf = newbuf + totlen;
276 zb.outlen = (nbuf * zbsize) - totlen;
278 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
280 err = got_object_parse_header(obj, buf, totlen);
281 done:
282 free(buf);
283 got_inflate_end(&zb);
284 return err;
287 struct got_commit_object *
288 got_object_commit_alloc_partial(void)
290 struct got_commit_object *commit;
292 commit = calloc(1, sizeof(*commit));
293 if (commit == NULL)
294 return NULL;
295 commit->tree_id = malloc(sizeof(*commit->tree_id));
296 if (commit->tree_id == NULL) {
297 free(commit);
298 return NULL;
301 STAILQ_INIT(&commit->parent_ids);
303 return commit;
306 const struct got_error *
307 got_object_commit_add_parent(struct got_commit_object *commit,
308 const char *id_str)
310 const struct got_error *err = NULL;
311 struct got_object_qid *qid;
313 err = got_object_qid_alloc_partial(&qid);
314 if (err)
315 return err;
317 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
318 err = got_error(GOT_ERR_BAD_OBJ_DATA);
319 got_object_qid_free(qid);
320 return err;
323 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
324 commit->nparents++;
326 return NULL;
329 static const struct got_error *
330 parse_gmtoff(time_t *gmtoff, const char *tzstr)
332 int sign = 1;
333 const char *p = tzstr;
334 time_t h, m;
336 *gmtoff = 0;
338 if (*p == '-')
339 sign = -1;
340 else if (*p != '+')
341 return got_error(GOT_ERR_BAD_OBJ_DATA);
342 p++;
343 if (!isdigit(*p) && !isdigit(*(p + 1)))
344 return got_error(GOT_ERR_BAD_OBJ_DATA);
345 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
347 p += 2;
348 if (!isdigit(*p) && !isdigit(*(p + 1)))
349 return got_error(GOT_ERR_BAD_OBJ_DATA);
350 m = ((*p - '0') * 10) + (*(p + 1) - '0');
352 *gmtoff = (h * 60 * 60 + m * 60) * sign;
353 return NULL;
356 static const struct got_error *
357 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
359 const struct got_error *err = NULL;
360 const char *errstr;
361 char *space, *tzstr;
363 /* Parse and strip off trailing timezone indicator string. */
364 space = strrchr(committer, ' ');
365 if (space == NULL)
366 return got_error(GOT_ERR_BAD_OBJ_DATA);
367 tzstr = strdup(space + 1);
368 if (tzstr == NULL)
369 return got_error_from_errno("strdup");
370 err = parse_gmtoff(gmtoff, tzstr);
371 free(tzstr);
372 if (err) {
373 if (err->code != GOT_ERR_BAD_OBJ_DATA)
374 return err;
375 /* Old versions of Git omitted the timestamp. */
376 *time = 0;
377 *gmtoff = 0;
378 return NULL;
380 *space = '\0';
382 /* Timestamp is separated from committer name + email by space. */
383 space = strrchr(committer, ' ');
384 if (space == NULL)
385 return got_error(GOT_ERR_BAD_OBJ_DATA);
387 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
388 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
389 if (errstr)
390 return got_error(GOT_ERR_BAD_OBJ_DATA);
392 /* Strip off parsed time information, leaving just author and email. */
393 *space = '\0';
395 return NULL;
398 void
399 got_object_commit_close(struct got_commit_object *commit)
401 if (commit->refcnt > 0) {
402 commit->refcnt--;
403 if (commit->refcnt > 0)
404 return;
407 got_object_id_queue_free(&commit->parent_ids);
408 free(commit->tree_id);
409 free(commit->author);
410 free(commit->committer);
411 free(commit->logmsg);
412 free(commit);
415 struct got_object_id *
416 got_object_commit_get_tree_id(struct got_commit_object *commit)
418 return commit->tree_id;
421 int
422 got_object_commit_get_nparents(struct got_commit_object *commit)
424 return commit->nparents;
427 const struct got_object_id_queue *
428 got_object_commit_get_parent_ids(struct got_commit_object *commit)
430 return &commit->parent_ids;
433 const char *
434 got_object_commit_get_author(struct got_commit_object *commit)
436 return commit->author;
439 time_t
440 got_object_commit_get_author_time(struct got_commit_object *commit)
442 return commit->author_time;
445 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
447 return commit->author_gmtoff;
450 const char *
451 got_object_commit_get_committer(struct got_commit_object *commit)
453 return commit->committer;
456 time_t
457 got_object_commit_get_committer_time(struct got_commit_object *commit)
459 return commit->committer_time;
462 time_t
463 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
465 return commit->committer_gmtoff;
468 const struct got_error *
469 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
471 const struct got_error *err = NULL;
472 const char *src;
473 char *dst;
474 size_t len;
476 len = strlen(commit->logmsg);
477 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
478 if (*logmsg == NULL)
479 return got_error_from_errno("malloc");
481 /*
482 * Strip out unusual headers. Headers are separated from the commit
483 * message body by a single empty line.
484 */
485 src = commit->logmsg;
486 dst = *logmsg;
487 while (*src != '\0' && *src != '\n') {
488 int copy_header = 1, eol = 0;
489 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
490 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
491 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
492 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
493 strncmp(src, GOT_COMMIT_LABEL_PARENT,
494 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
495 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
496 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
497 copy_header = 0;
499 while (*src != '\0' && !eol) {
500 if (copy_header) {
501 *dst = *src;
502 dst++;
504 if (*src == '\n')
505 eol = 1;
506 src++;
509 *dst = '\0';
511 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
512 err = got_error(GOT_ERR_NO_SPACE);
513 goto done;
516 /* Trim redundant trailing whitespace. */
517 len = strlen(*logmsg);
518 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
519 isspace((unsigned char)(*logmsg)[len - 1])) {
520 (*logmsg)[len - 1] = '\0';
521 len--;
524 /* Append a trailing newline if missing. */
525 if (len > 0 && (*logmsg)[len - 1] != '\n') {
526 (*logmsg)[len] = '\n';
527 (*logmsg)[len + 1] = '\0';
529 done:
530 if (err) {
531 free(*logmsg);
532 *logmsg = NULL;
534 return err;
537 const char *
538 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
540 return commit->logmsg;
543 const struct got_error *
544 got_object_parse_commit(struct got_commit_object **commit, char *buf,
545 size_t len)
547 const struct got_error *err = NULL;
548 char *s = buf;
549 size_t label_len;
550 ssize_t remain = (ssize_t)len;
552 if (remain == 0)
553 return got_error(GOT_ERR_BAD_OBJ_DATA);
555 *commit = got_object_commit_alloc_partial();
556 if (*commit == NULL)
557 return got_error_from_errno("got_object_commit_alloc_partial");
559 label_len = strlen(GOT_COMMIT_LABEL_TREE);
560 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
561 remain -= label_len;
562 if (remain < SHA1_DIGEST_STRING_LENGTH) {
563 err = got_error(GOT_ERR_BAD_OBJ_DATA);
564 goto done;
566 s += label_len;
567 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
568 err = got_error(GOT_ERR_BAD_OBJ_DATA);
569 goto done;
571 remain -= SHA1_DIGEST_STRING_LENGTH;
572 s += SHA1_DIGEST_STRING_LENGTH;
573 } else {
574 err = got_error(GOT_ERR_BAD_OBJ_DATA);
575 goto done;
578 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
579 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
580 remain -= label_len;
581 if (remain < SHA1_DIGEST_STRING_LENGTH) {
582 err = got_error(GOT_ERR_BAD_OBJ_DATA);
583 goto done;
585 s += label_len;
586 err = got_object_commit_add_parent(*commit, s);
587 if (err)
588 goto done;
590 remain -= SHA1_DIGEST_STRING_LENGTH;
591 s += SHA1_DIGEST_STRING_LENGTH;
594 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
595 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
596 char *p;
597 size_t slen;
599 remain -= label_len;
600 if (remain <= 0) {
601 err = got_error(GOT_ERR_BAD_OBJ_DATA);
602 goto done;
604 s += label_len;
605 p = memchr(s, '\n', remain);
606 if (p == NULL) {
607 err = got_error(GOT_ERR_BAD_OBJ_DATA);
608 goto done;
610 *p = '\0';
611 slen = strlen(s);
612 err = parse_commit_time(&(*commit)->author_time,
613 &(*commit)->author_gmtoff, s);
614 if (err)
615 goto done;
616 (*commit)->author = strdup(s);
617 if ((*commit)->author == NULL) {
618 err = got_error_from_errno("strdup");
619 goto done;
621 s += slen + 1;
622 remain -= slen + 1;
625 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
626 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
627 char *p;
628 size_t slen;
630 remain -= label_len;
631 if (remain <= 0) {
632 err = got_error(GOT_ERR_BAD_OBJ_DATA);
633 goto done;
635 s += label_len;
636 p = memchr(s, '\n', remain);
637 if (p == NULL) {
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
639 goto done;
641 *p = '\0';
642 slen = strlen(s);
643 err = parse_commit_time(&(*commit)->committer_time,
644 &(*commit)->committer_gmtoff, s);
645 if (err)
646 goto done;
647 (*commit)->committer = strdup(s);
648 if ((*commit)->committer == NULL) {
649 err = got_error_from_errno("strdup");
650 goto done;
652 s += slen + 1;
653 remain -= slen + 1;
656 (*commit)->logmsg = strndup(s, remain);
657 if ((*commit)->logmsg == NULL) {
658 err = got_error_from_errno("strndup");
659 goto done;
661 done:
662 if (err) {
663 got_object_commit_close(*commit);
664 *commit = NULL;
666 return err;
669 void
670 got_object_tree_close(struct got_tree_object *tree)
672 if (tree->refcnt > 0) {
673 tree->refcnt--;
674 if (tree->refcnt > 0)
675 return;
678 free(tree->entries);
679 free(tree);
682 static const struct got_error *
683 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
684 size_t *elen, char *buf,
685 size_t maxlen)
687 char *p, *space;
688 const struct got_error *err = NULL;
690 *name = NULL;
691 *elen = 0;
693 *pte = malloc(sizeof(**pte));
694 if (*pte == NULL)
695 return got_error_from_errno("malloc");
697 *elen = strnlen(buf, maxlen) + 1;
698 if (*elen > maxlen) {
699 free(*pte);
700 *pte = NULL;
701 return got_error(GOT_ERR_BAD_OBJ_DATA);
704 space = memchr(buf, ' ', *elen);
705 if (space == NULL || space <= buf) {
706 err = got_error(GOT_ERR_BAD_OBJ_DATA);
707 free(*pte);
708 *pte = NULL;
709 return err;
711 (*pte)->mode = 0;
712 p = buf;
713 while (p < space) {
714 if (*p < '0' && *p > '7') {
715 err = got_error(GOT_ERR_BAD_OBJ_DATA);
716 goto done;
718 (*pte)->mode <<= 3;
719 (*pte)->mode |= *p - '0';
720 p++;
723 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
724 err = got_error(GOT_ERR_BAD_OBJ_DATA);
725 goto done;
727 *name = space + 1;
728 buf += *elen;
729 (*pte)->id = buf;
730 *elen += SHA1_DIGEST_LENGTH;
731 done:
732 if (err) {
733 free(*pte);
734 *pte = NULL;
736 return err;
739 const struct got_error *
740 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
741 uint8_t *buf, size_t len)
743 const struct got_error *err = NULL;
744 size_t remain = len;
746 *nentries = 0;
747 if (remain == 0)
748 return NULL; /* tree is empty */
750 while (remain > 0) {
751 struct got_parsed_tree_entry *pte;
752 struct got_pathlist_entry *new = NULL;
753 const char *name;
754 size_t elen;
756 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
757 if (err)
758 goto done;
759 err = got_pathlist_insert(&new, entries, name, pte);
760 if (err)
761 goto done;
762 if (new == NULL) {
763 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
764 goto done;
766 buf += elen;
767 remain -= elen;
768 (*nentries)++;
771 if (remain != 0) {
772 err = got_error(GOT_ERR_BAD_OBJ_DATA);
773 goto done;
775 done:
776 if (err) {
777 got_object_parsed_tree_entries_free(entries);
778 *nentries = 0;
780 return err;
783 void
784 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
786 struct got_pathlist_entry *pe;
788 TAILQ_FOREACH(pe, entries, entry) {
789 struct got_parsed_tree_entry *pte = pe->data;
790 free(pte);
792 got_pathlist_free(entries);
795 void
796 got_object_tag_close(struct got_tag_object *tag)
798 if (tag->refcnt > 0) {
799 tag->refcnt--;
800 if (tag->refcnt > 0)
801 return;
804 free(tag->tag);
805 free(tag->tagger);
806 free(tag->tagmsg);
807 free(tag);
810 const struct got_error *
811 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
813 const struct got_error *err = NULL;
814 size_t remain = len;
815 char *s = buf;
816 size_t label_len;
818 if (remain == 0)
819 return got_error(GOT_ERR_BAD_OBJ_DATA);
821 *tag = calloc(1, sizeof(**tag));
822 if (*tag == NULL)
823 return got_error_from_errno("calloc");
825 label_len = strlen(GOT_TAG_LABEL_OBJECT);
826 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
827 remain -= label_len;
828 if (remain < SHA1_DIGEST_STRING_LENGTH) {
829 err = got_error(GOT_ERR_BAD_OBJ_DATA);
830 goto done;
832 s += label_len;
833 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
834 err = got_error(GOT_ERR_BAD_OBJ_DATA);
835 goto done;
837 remain -= SHA1_DIGEST_STRING_LENGTH;
838 s += SHA1_DIGEST_STRING_LENGTH;
839 } else {
840 err = got_error(GOT_ERR_BAD_OBJ_DATA);
841 goto done;
844 if (remain <= 0) {
845 err = got_error(GOT_ERR_BAD_OBJ_DATA);
846 goto done;
849 label_len = strlen(GOT_TAG_LABEL_TYPE);
850 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
851 remain -= label_len;
852 if (remain <= 0) {
853 err = got_error(GOT_ERR_BAD_OBJ_DATA);
854 goto done;
856 s += label_len;
857 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
858 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
859 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
860 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
861 s += label_len;
862 remain -= label_len;
863 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
864 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
865 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
866 label_len = strlen(GOT_OBJ_LABEL_TREE);
867 s += label_len;
868 remain -= label_len;
869 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
870 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
871 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
872 label_len = strlen(GOT_OBJ_LABEL_BLOB);
873 s += label_len;
874 remain -= label_len;
875 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
876 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
877 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
878 label_len = strlen(GOT_OBJ_LABEL_TAG);
879 s += label_len;
880 remain -= label_len;
881 } else {
882 err = got_error(GOT_ERR_BAD_OBJ_DATA);
883 goto done;
886 if (remain <= 0 || *s != '\n') {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
888 goto done;
890 s++;
891 remain--;
892 if (remain <= 0) {
893 err = got_error(GOT_ERR_BAD_OBJ_DATA);
894 goto done;
896 } else {
897 err = got_error(GOT_ERR_BAD_OBJ_DATA);
898 goto done;
901 label_len = strlen(GOT_TAG_LABEL_TAG);
902 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
903 char *p;
904 size_t slen;
905 remain -= label_len;
906 if (remain <= 0) {
907 err = got_error(GOT_ERR_BAD_OBJ_DATA);
908 goto done;
910 s += label_len;
911 p = memchr(s, '\n', remain);
912 if (p == NULL) {
913 err = got_error(GOT_ERR_BAD_OBJ_DATA);
914 goto done;
916 *p = '\0';
917 slen = strlen(s);
918 (*tag)->tag = strndup(s, slen);
919 if ((*tag)->tag == NULL) {
920 err = got_error_from_errno("strndup");
921 goto done;
923 s += slen + 1;
924 remain -= slen + 1;
925 if (remain <= 0) {
926 err = got_error(GOT_ERR_BAD_OBJ_DATA);
927 goto done;
929 } else {
930 err = got_error(GOT_ERR_BAD_OBJ_DATA);
931 goto done;
934 label_len = strlen(GOT_TAG_LABEL_TAGGER);
935 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
936 char *p;
937 size_t slen;
939 remain -= label_len;
940 if (remain <= 0) {
941 err = got_error(GOT_ERR_BAD_OBJ_DATA);
942 goto done;
944 s += label_len;
945 p = memchr(s, '\n', remain);
946 if (p == NULL) {
947 err = got_error(GOT_ERR_BAD_OBJ_DATA);
948 goto done;
950 *p = '\0';
951 slen = strlen(s);
952 err = parse_commit_time(&(*tag)->tagger_time,
953 &(*tag)->tagger_gmtoff, s);
954 if (err)
955 goto done;
956 (*tag)->tagger = strdup(s);
957 if ((*tag)->tagger == NULL) {
958 err = got_error_from_errno("strdup");
959 goto done;
961 s += slen + 1;
962 remain -= slen + 1;
963 if (remain < 0) {
964 err = got_error(GOT_ERR_BAD_OBJ_DATA);
965 goto done;
967 } else {
968 /* Some old tags in the Linux git repo have no tagger. */
969 (*tag)->tagger = strdup("");
970 if ((*tag)->tagger == NULL) {
971 err = got_error_from_errno("strdup");
972 goto done;
976 (*tag)->tagmsg = strndup(s, remain);
977 if ((*tag)->tagmsg == NULL) {
978 err = got_error_from_errno("strndup");
979 goto done;
981 done:
982 if (err) {
983 got_object_tag_close(*tag);
984 *tag = NULL;
986 return err;
989 const struct got_error *
990 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
992 const struct got_error *err = NULL;
993 static const size_t blocksize = 512;
994 size_t n, total, remain;
995 uint8_t *buf;
997 *outbuf = NULL;
998 *outlen = 0;
1000 buf = malloc(blocksize);
1001 if (buf == NULL)
1002 return got_error_from_errno("malloc");
1004 remain = blocksize;
1005 total = 0;
1006 for (;;) {
1007 if (remain == 0) {
1008 uint8_t *newbuf;
1009 newbuf = reallocarray(buf, 1, total + blocksize);
1010 if (newbuf == NULL) {
1011 err = got_error_from_errno("reallocarray");
1012 goto done;
1014 buf = newbuf;
1015 remain += blocksize;
1017 n = fread(buf + total, 1, remain, f);
1018 if (n == 0) {
1019 if (ferror(f)) {
1020 err = got_ferror(f, GOT_ERR_IO);
1021 goto done;
1023 break; /* EOF */
1025 remain -= n;
1026 total += n;
1029 done:
1030 if (err == NULL) {
1031 *outbuf = buf;
1032 *outlen = total;
1033 } else
1034 free(buf);
1035 return err;