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 *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 = SHA1_DIGEST_STRING_LENGTH;
94 *outbuf = malloc(len);
95 if (*outbuf == NULL)
96 return got_error_from_errno("malloc");
98 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
99 free(*outbuf);
100 *outbuf = NULL;
101 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
104 return NULL;
107 void
108 got_object_close(struct got_object *obj)
110 if (obj->refcnt > 0) {
111 obj->refcnt--;
112 if (obj->refcnt > 0)
113 return;
116 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
117 struct got_delta *delta;
118 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
119 delta = STAILQ_FIRST(&obj->deltas.entries);
120 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
121 free(delta);
124 free(obj);
127 const struct got_error *
128 got_object_raw_close(struct got_raw_object *obj)
130 const struct got_error *err = NULL;
132 if (obj->refcnt > 0) {
133 obj->refcnt--;
134 if (obj->refcnt > 0)
135 return NULL;
138 if (obj->f == NULL) {
139 if (obj->fd != -1) {
140 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
141 err = got_error_from_errno("munmap");
142 if (close(obj->fd) == -1 && err == NULL)
143 err = got_error_from_errno("close");
144 } else
145 free(obj->data);
146 } else {
147 if (fclose(obj->f) == EOF && err == NULL)
148 err = got_error_from_errno("fclose");
150 free(obj);
151 return err;
154 void
155 got_object_qid_free(struct got_object_qid *qid)
157 free(qid);
160 void
161 got_object_id_queue_free(struct got_object_id_queue *ids)
163 struct got_object_qid *qid;
165 while (!STAILQ_EMPTY(ids)) {
166 qid = STAILQ_FIRST(ids);
167 STAILQ_REMOVE_HEAD(ids, entry);
168 got_object_qid_free(qid);
172 const struct got_error *
173 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
175 const char *obj_labels[] = {
176 GOT_OBJ_LABEL_COMMIT,
177 GOT_OBJ_LABEL_TREE,
178 GOT_OBJ_LABEL_BLOB,
179 GOT_OBJ_LABEL_TAG,
180 };
181 const int obj_types[] = {
182 GOT_OBJ_TYPE_COMMIT,
183 GOT_OBJ_TYPE_TREE,
184 GOT_OBJ_TYPE_BLOB,
185 GOT_OBJ_TYPE_TAG,
186 };
187 int type = 0;
188 size_t size = 0;
189 size_t i;
190 char *end;
192 *obj = NULL;
194 end = memchr(buf, '\0', len);
195 if (end == NULL)
196 return got_error(GOT_ERR_BAD_OBJ_HDR);
198 for (i = 0; i < nitems(obj_labels); i++) {
199 const char *label = obj_labels[i];
200 size_t label_len = strlen(label);
201 const char *errstr;
203 if (len <= label_len || buf + label_len >= end ||
204 strncmp(buf, label, label_len) != 0)
205 continue;
207 type = obj_types[i];
208 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
209 if (errstr != NULL)
210 return got_error(GOT_ERR_BAD_OBJ_HDR);
211 break;
214 if (type == 0)
215 return got_error(GOT_ERR_BAD_OBJ_HDR);
217 *obj = calloc(1, sizeof(**obj));
218 if (*obj == NULL)
219 return got_error_from_errno("calloc");
220 (*obj)->type = type;
221 (*obj)->hdrlen = end - buf + 1;
222 (*obj)->size = size;
223 return NULL;
226 const struct got_error *
227 got_object_read_header(struct got_object **obj, int fd)
229 const struct got_error *err;
230 struct got_inflate_buf zb;
231 uint8_t *buf;
232 const size_t zbsize = 64;
233 size_t outlen, totlen;
234 int nbuf = 1;
236 *obj = NULL;
238 buf = malloc(zbsize);
239 if (buf == NULL)
240 return got_error_from_errno("malloc");
241 buf[0] = '\0';
243 err = got_inflate_init(&zb, buf, zbsize, NULL);
244 if (err)
245 return err;
247 totlen = 0;
248 do {
249 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
250 if (err)
251 goto done;
252 if (outlen == 0)
253 break;
254 totlen += outlen;
255 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
256 uint8_t *newbuf;
257 nbuf++;
258 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
259 if (newbuf == NULL) {
260 err = got_error_from_errno("recallocarray");
261 goto done;
263 buf = newbuf;
264 zb.outbuf = newbuf + totlen;
265 zb.outlen = (nbuf * zbsize) - totlen;
267 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
269 err = got_object_parse_header(obj, buf, totlen);
270 done:
271 free(buf);
272 got_inflate_end(&zb);
273 return err;
276 struct got_commit_object *
277 got_object_commit_alloc_partial(void)
279 struct got_commit_object *commit;
281 commit = calloc(1, sizeof(*commit));
282 if (commit == NULL)
283 return NULL;
284 commit->tree_id = malloc(sizeof(*commit->tree_id));
285 if (commit->tree_id == NULL) {
286 free(commit);
287 return NULL;
290 STAILQ_INIT(&commit->parent_ids);
292 return commit;
295 const struct got_error *
296 got_object_commit_add_parent(struct got_commit_object *commit,
297 const char *id_str)
299 const struct got_error *err = NULL;
300 struct got_object_qid *qid;
302 err = got_object_qid_alloc_partial(&qid);
303 if (err)
304 return err;
306 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
307 err = got_error(GOT_ERR_BAD_OBJ_DATA);
308 got_object_qid_free(qid);
309 return err;
312 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
313 commit->nparents++;
315 return NULL;
318 static const struct got_error *
319 parse_gmtoff(time_t *gmtoff, const char *tzstr)
321 int sign = 1;
322 const char *p = tzstr;
323 time_t h, m;
325 *gmtoff = 0;
327 if (*p == '-')
328 sign = -1;
329 else if (*p != '+')
330 return got_error(GOT_ERR_BAD_OBJ_DATA);
331 p++;
332 if (!isdigit(*p) && !isdigit(*(p + 1)))
333 return got_error(GOT_ERR_BAD_OBJ_DATA);
334 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
336 p += 2;
337 if (!isdigit(*p) && !isdigit(*(p + 1)))
338 return got_error(GOT_ERR_BAD_OBJ_DATA);
339 m = ((*p - '0') * 10) + (*(p + 1) - '0');
341 *gmtoff = (h * 60 * 60 + m * 60) * sign;
342 return NULL;
345 static const struct got_error *
346 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
348 const struct got_error *err = NULL;
349 const char *errstr;
350 char *space, *tzstr;
352 /* Parse and strip off trailing timezone indicator string. */
353 space = strrchr(committer, ' ');
354 if (space == NULL)
355 return got_error(GOT_ERR_BAD_OBJ_DATA);
356 tzstr = strdup(space + 1);
357 if (tzstr == NULL)
358 return got_error_from_errno("strdup");
359 err = parse_gmtoff(gmtoff, tzstr);
360 free(tzstr);
361 if (err) {
362 if (err->code != GOT_ERR_BAD_OBJ_DATA)
363 return err;
364 /* Old versions of Git omitted the timestamp. */
365 *time = 0;
366 *gmtoff = 0;
367 return NULL;
369 *space = '\0';
371 /* Timestamp is separated from committer name + email by space. */
372 space = strrchr(committer, ' ');
373 if (space == NULL)
374 return got_error(GOT_ERR_BAD_OBJ_DATA);
376 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
377 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
378 if (errstr)
379 return got_error(GOT_ERR_BAD_OBJ_DATA);
381 /* Strip off parsed time information, leaving just author and email. */
382 *space = '\0';
384 return NULL;
387 void
388 got_object_commit_close(struct got_commit_object *commit)
390 if (commit->refcnt > 0) {
391 commit->refcnt--;
392 if (commit->refcnt > 0)
393 return;
396 got_object_id_queue_free(&commit->parent_ids);
397 free(commit->tree_id);
398 free(commit->author);
399 free(commit->committer);
400 free(commit->logmsg);
401 free(commit);
404 struct got_object_id *
405 got_object_commit_get_tree_id(struct got_commit_object *commit)
407 return commit->tree_id;
410 int
411 got_object_commit_get_nparents(struct got_commit_object *commit)
413 return commit->nparents;
416 const struct got_object_id_queue *
417 got_object_commit_get_parent_ids(struct got_commit_object *commit)
419 return &commit->parent_ids;
422 const char *
423 got_object_commit_get_author(struct got_commit_object *commit)
425 return commit->author;
428 time_t
429 got_object_commit_get_author_time(struct got_commit_object *commit)
431 return commit->author_time;
434 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
436 return commit->author_gmtoff;
439 const char *
440 got_object_commit_get_committer(struct got_commit_object *commit)
442 return commit->committer;
445 time_t
446 got_object_commit_get_committer_time(struct got_commit_object *commit)
448 return commit->committer_time;
451 time_t
452 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
454 return commit->committer_gmtoff;
457 const struct got_error *
458 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
460 const struct got_error *err = NULL;
461 const char *src;
462 char *dst;
463 size_t len;
465 len = strlen(commit->logmsg);
466 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
467 if (*logmsg == NULL)
468 return got_error_from_errno("malloc");
470 /*
471 * Strip out unusual headers. Headers are separated from the commit
472 * message body by a single empty line.
473 */
474 src = commit->logmsg;
475 dst = *logmsg;
476 while (*src != '\0' && *src != '\n') {
477 int copy_header = 1, eol = 0;
478 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
479 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
480 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
481 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
482 strncmp(src, GOT_COMMIT_LABEL_PARENT,
483 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
484 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
485 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
486 copy_header = 0;
488 while (*src != '\0' && !eol) {
489 if (copy_header) {
490 *dst = *src;
491 dst++;
493 if (*src == '\n')
494 eol = 1;
495 src++;
498 *dst = '\0';
500 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
501 err = got_error(GOT_ERR_NO_SPACE);
502 goto done;
505 /* Trim redundant trailing whitespace. */
506 len = strlen(*logmsg);
507 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
508 isspace((unsigned char)(*logmsg)[len - 1])) {
509 (*logmsg)[len - 1] = '\0';
510 len--;
513 /* Append a trailing newline if missing. */
514 if (len > 0 && (*logmsg)[len - 1] != '\n') {
515 (*logmsg)[len] = '\n';
516 (*logmsg)[len + 1] = '\0';
518 done:
519 if (err) {
520 free(*logmsg);
521 *logmsg = NULL;
523 return err;
526 const char *
527 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
529 return commit->logmsg;
532 const struct got_error *
533 got_object_parse_commit(struct got_commit_object **commit, char *buf,
534 size_t len)
536 const struct got_error *err = NULL;
537 char *s = buf;
538 size_t label_len;
539 ssize_t remain = (ssize_t)len;
541 if (remain == 0)
542 return got_error(GOT_ERR_BAD_OBJ_DATA);
544 *commit = got_object_commit_alloc_partial();
545 if (*commit == NULL)
546 return got_error_from_errno("got_object_commit_alloc_partial");
548 label_len = strlen(GOT_COMMIT_LABEL_TREE);
549 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
550 remain -= label_len;
551 if (remain < SHA1_DIGEST_STRING_LENGTH) {
552 err = got_error(GOT_ERR_BAD_OBJ_DATA);
553 goto done;
555 s += label_len;
556 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
557 err = got_error(GOT_ERR_BAD_OBJ_DATA);
558 goto done;
560 remain -= SHA1_DIGEST_STRING_LENGTH;
561 s += SHA1_DIGEST_STRING_LENGTH;
562 } else {
563 err = got_error(GOT_ERR_BAD_OBJ_DATA);
564 goto done;
567 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
568 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
569 remain -= label_len;
570 if (remain < SHA1_DIGEST_STRING_LENGTH) {
571 err = got_error(GOT_ERR_BAD_OBJ_DATA);
572 goto done;
574 s += label_len;
575 err = got_object_commit_add_parent(*commit, s);
576 if (err)
577 goto done;
579 remain -= SHA1_DIGEST_STRING_LENGTH;
580 s += SHA1_DIGEST_STRING_LENGTH;
583 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
584 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
585 char *p;
586 size_t slen;
588 remain -= label_len;
589 if (remain <= 0) {
590 err = got_error(GOT_ERR_BAD_OBJ_DATA);
591 goto done;
593 s += label_len;
594 p = memchr(s, '\n', remain);
595 if (p == NULL) {
596 err = got_error(GOT_ERR_BAD_OBJ_DATA);
597 goto done;
599 *p = '\0';
600 slen = strlen(s);
601 err = parse_commit_time(&(*commit)->author_time,
602 &(*commit)->author_gmtoff, s);
603 if (err)
604 goto done;
605 (*commit)->author = strdup(s);
606 if ((*commit)->author == NULL) {
607 err = got_error_from_errno("strdup");
608 goto done;
610 s += slen + 1;
611 remain -= slen + 1;
614 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
615 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
616 char *p;
617 size_t slen;
619 remain -= label_len;
620 if (remain <= 0) {
621 err = got_error(GOT_ERR_BAD_OBJ_DATA);
622 goto done;
624 s += label_len;
625 p = memchr(s, '\n', remain);
626 if (p == NULL) {
627 err = got_error(GOT_ERR_BAD_OBJ_DATA);
628 goto done;
630 *p = '\0';
631 slen = strlen(s);
632 err = parse_commit_time(&(*commit)->committer_time,
633 &(*commit)->committer_gmtoff, s);
634 if (err)
635 goto done;
636 (*commit)->committer = strdup(s);
637 if ((*commit)->committer == NULL) {
638 err = got_error_from_errno("strdup");
639 goto done;
641 s += slen + 1;
642 remain -= slen + 1;
645 (*commit)->logmsg = strndup(s, remain);
646 if ((*commit)->logmsg == NULL) {
647 err = got_error_from_errno("strndup");
648 goto done;
650 done:
651 if (err) {
652 got_object_commit_close(*commit);
653 *commit = NULL;
655 return err;
658 void
659 got_object_tree_close(struct got_tree_object *tree)
661 if (tree->refcnt > 0) {
662 tree->refcnt--;
663 if (tree->refcnt > 0)
664 return;
667 free(tree->entries);
668 free(tree);
671 static const struct got_error *
672 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
673 size_t maxlen)
675 char *p, *space;
677 *elen = 0;
679 *elen = strnlen(buf, maxlen) + 1;
680 if (*elen > maxlen)
681 return got_error(GOT_ERR_BAD_OBJ_DATA);
683 space = memchr(buf, ' ', *elen);
684 if (space == NULL || space <= buf)
685 return got_error(GOT_ERR_BAD_OBJ_DATA);
687 pte->mode = 0;
688 p = buf;
689 while (p < space) {
690 if (*p < '0' && *p > '7')
691 return got_error(GOT_ERR_BAD_OBJ_DATA);
692 pte->mode <<= 3;
693 pte->mode |= *p - '0';
694 p++;
697 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
698 return got_error(GOT_ERR_BAD_OBJ_DATA);
700 pte->name = space + 1;
701 pte->namelen = strlen(pte->name);
702 buf += *elen;
703 pte->id = buf;
704 *elen += SHA1_DIGEST_LENGTH;
705 return NULL;
708 static int
709 pte_cmp(const void *pa, const void *pb)
711 const struct got_parsed_tree_entry *a = pa, *b = pb;
713 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
716 const struct got_error *
717 got_object_parse_tree(struct got_parsed_tree_entry **entries, int *nentries,
718 uint8_t *buf, size_t len)
720 const struct got_error *err = NULL;
721 size_t remain = len, totalloc;
722 const size_t nalloc = 16;
723 struct got_parsed_tree_entry *pte;
724 int i;
726 *nentries = 0;
727 if (remain == 0)
728 return NULL; /* tree is empty */
730 *entries = calloc(nalloc, sizeof(**entries));
731 if (*entries == NULL)
732 return got_error_from_errno("calloc");
733 totalloc = nalloc;
735 while (remain > 0) {
736 size_t elen;
738 if (*nentries >= totalloc) {
739 pte = recallocarray(*entries, totalloc,
740 totalloc + nalloc, sizeof(**entries));
741 if (pte == NULL) {
742 err = got_error_from_errno("recallocarray");
743 goto done;
745 *entries = pte;
746 totalloc += nalloc;
749 pte = &(*entries)[*nentries];
750 err = parse_tree_entry(pte, &elen, buf, remain);
751 if (err)
752 goto done;
753 buf += elen;
754 remain -= elen;
755 (*nentries)++;
758 if (remain != 0) {
759 err = got_error(GOT_ERR_BAD_OBJ_DATA);
760 goto done;
763 if (*nentries > 1) {
764 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
766 for (i = 0; i < *nentries - 1; i++) {
767 struct got_parsed_tree_entry *prev = &(*entries)[i];
768 pte = &(*entries)[i + 1];
769 if (got_path_cmp(prev->name, pte->name,
770 prev->namelen, pte->namelen) == 0) {
771 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
772 break;
776 done:
777 if (err) {
778 free(*entries);
779 *entries = NULL;
780 *nentries = 0;
782 return err;
785 void
786 got_object_tag_close(struct got_tag_object *tag)
788 if (tag->refcnt > 0) {
789 tag->refcnt--;
790 if (tag->refcnt > 0)
791 return;
794 free(tag->tag);
795 free(tag->tagger);
796 free(tag->tagmsg);
797 free(tag);
800 const struct got_error *
801 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
803 const struct got_error *err = NULL;
804 size_t remain = len;
805 char *s = buf;
806 size_t label_len;
808 if (remain == 0)
809 return got_error(GOT_ERR_BAD_OBJ_DATA);
811 *tag = calloc(1, sizeof(**tag));
812 if (*tag == NULL)
813 return got_error_from_errno("calloc");
815 label_len = strlen(GOT_TAG_LABEL_OBJECT);
816 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
817 remain -= label_len;
818 if (remain < SHA1_DIGEST_STRING_LENGTH) {
819 err = got_error(GOT_ERR_BAD_OBJ_DATA);
820 goto done;
822 s += label_len;
823 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
824 err = got_error(GOT_ERR_BAD_OBJ_DATA);
825 goto done;
827 remain -= SHA1_DIGEST_STRING_LENGTH;
828 s += SHA1_DIGEST_STRING_LENGTH;
829 } else {
830 err = got_error(GOT_ERR_BAD_OBJ_DATA);
831 goto done;
834 if (remain <= 0) {
835 err = got_error(GOT_ERR_BAD_OBJ_DATA);
836 goto done;
839 label_len = strlen(GOT_TAG_LABEL_TYPE);
840 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
841 remain -= label_len;
842 if (remain <= 0) {
843 err = got_error(GOT_ERR_BAD_OBJ_DATA);
844 goto done;
846 s += label_len;
847 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
848 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
849 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
850 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
851 s += label_len;
852 remain -= label_len;
853 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
854 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
855 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
856 label_len = strlen(GOT_OBJ_LABEL_TREE);
857 s += label_len;
858 remain -= label_len;
859 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
860 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
861 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
862 label_len = strlen(GOT_OBJ_LABEL_BLOB);
863 s += label_len;
864 remain -= label_len;
865 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
866 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
867 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
868 label_len = strlen(GOT_OBJ_LABEL_TAG);
869 s += label_len;
870 remain -= label_len;
871 } else {
872 err = got_error(GOT_ERR_BAD_OBJ_DATA);
873 goto done;
876 if (remain <= 0 || *s != '\n') {
877 err = got_error(GOT_ERR_BAD_OBJ_DATA);
878 goto done;
880 s++;
881 remain--;
882 if (remain <= 0) {
883 err = got_error(GOT_ERR_BAD_OBJ_DATA);
884 goto done;
886 } else {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
888 goto done;
891 label_len = strlen(GOT_TAG_LABEL_TAG);
892 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
893 char *p;
894 size_t slen;
895 remain -= label_len;
896 if (remain <= 0) {
897 err = got_error(GOT_ERR_BAD_OBJ_DATA);
898 goto done;
900 s += label_len;
901 p = memchr(s, '\n', remain);
902 if (p == NULL) {
903 err = got_error(GOT_ERR_BAD_OBJ_DATA);
904 goto done;
906 *p = '\0';
907 slen = strlen(s);
908 (*tag)->tag = strndup(s, slen);
909 if ((*tag)->tag == NULL) {
910 err = got_error_from_errno("strndup");
911 goto done;
913 s += slen + 1;
914 remain -= slen + 1;
915 if (remain <= 0) {
916 err = got_error(GOT_ERR_BAD_OBJ_DATA);
917 goto done;
919 } else {
920 err = got_error(GOT_ERR_BAD_OBJ_DATA);
921 goto done;
924 label_len = strlen(GOT_TAG_LABEL_TAGGER);
925 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
926 char *p;
927 size_t slen;
929 remain -= label_len;
930 if (remain <= 0) {
931 err = got_error(GOT_ERR_BAD_OBJ_DATA);
932 goto done;
934 s += label_len;
935 p = memchr(s, '\n', remain);
936 if (p == NULL) {
937 err = got_error(GOT_ERR_BAD_OBJ_DATA);
938 goto done;
940 *p = '\0';
941 slen = strlen(s);
942 err = parse_commit_time(&(*tag)->tagger_time,
943 &(*tag)->tagger_gmtoff, s);
944 if (err)
945 goto done;
946 (*tag)->tagger = strdup(s);
947 if ((*tag)->tagger == NULL) {
948 err = got_error_from_errno("strdup");
949 goto done;
951 s += slen + 1;
952 remain -= slen + 1;
953 if (remain < 0) {
954 err = got_error(GOT_ERR_BAD_OBJ_DATA);
955 goto done;
957 } else {
958 /* Some old tags in the Linux git repo have no tagger. */
959 (*tag)->tagger = strdup("");
960 if ((*tag)->tagger == NULL) {
961 err = got_error_from_errno("strdup");
962 goto done;
966 (*tag)->tagmsg = strndup(s, remain);
967 if ((*tag)->tagmsg == NULL) {
968 err = got_error_from_errno("strndup");
969 goto done;
971 done:
972 if (err) {
973 got_object_tag_close(*tag);
974 *tag = NULL;
976 return err;
979 const struct got_error *
980 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
982 const struct got_error *err = NULL;
983 static const size_t blocksize = 512;
984 size_t n, total, remain;
985 uint8_t *buf;
987 *outbuf = NULL;
988 *outlen = 0;
990 buf = malloc(blocksize);
991 if (buf == NULL)
992 return got_error_from_errno("malloc");
994 remain = blocksize;
995 total = 0;
996 for (;;) {
997 if (remain == 0) {
998 uint8_t *newbuf;
999 newbuf = reallocarray(buf, 1, total + blocksize);
1000 if (newbuf == NULL) {
1001 err = got_error_from_errno("reallocarray");
1002 goto done;
1004 buf = newbuf;
1005 remain += blocksize;
1007 n = fread(buf + total, 1, remain, f);
1008 if (n == 0) {
1009 if (ferror(f)) {
1010 err = got_ferror(f, GOT_ERR_IO);
1011 goto done;
1013 break; /* EOF */
1015 remain -= n;
1016 total += n;
1019 done:
1020 if (err == NULL) {
1021 *outbuf = buf;
1022 *outlen = total;
1023 } else
1024 free(buf);
1025 return err;