Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/mman.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <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_sha1.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_privsep.h"
52 #include "got_lib_repository.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #endif
58 struct got_object_id *
59 got_object_id_dup(struct got_object_id *id1)
60 {
61 struct got_object_id *id2;
63 id2 = malloc(sizeof(*id2));
64 if (id2 == NULL)
65 return NULL;
66 memcpy(id2, id1, sizeof(*id2));
67 return id2;
68 }
70 int
71 got_object_id_cmp(const struct got_object_id *id1,
72 const struct got_object_id *id2)
73 {
74 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
75 }
77 const struct got_error *
78 got_object_qid_alloc_partial(struct got_object_qid **qid)
79 {
80 *qid = malloc(sizeof(**qid));
81 if (*qid == NULL)
82 return got_error_from_errno("malloc");
84 (*qid)->data = NULL;
85 return NULL;
86 }
88 const struct got_error *
89 got_object_id_str(char **outbuf, struct got_object_id *id)
90 {
91 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
93 *outbuf = malloc(len);
94 if (*outbuf == NULL)
95 return got_error_from_errno("malloc");
97 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
98 free(*outbuf);
99 *outbuf = NULL;
100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
103 return NULL;
106 void
107 got_object_close(struct got_object *obj)
109 if (obj->refcnt > 0) {
110 obj->refcnt--;
111 if (obj->refcnt > 0)
112 return;
115 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
116 struct got_delta *delta;
117 while (!STAILQ_EMPTY(&obj->deltas.entries)) {
118 delta = STAILQ_FIRST(&obj->deltas.entries);
119 STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
120 free(delta);
123 free(obj);
126 const struct got_error *
127 got_object_raw_close(struct got_raw_object *obj)
129 const struct got_error *err = NULL;
131 if (obj->refcnt > 0) {
132 obj->refcnt--;
133 if (obj->refcnt > 0)
134 return NULL;
137 if (obj->f == NULL) {
138 if (obj->fd != -1) {
139 if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
140 err = got_error_from_errno("munmap");
141 if (close(obj->fd) == -1 && err == NULL)
142 err = got_error_from_errno("close");
143 } else
144 free(obj->data);
145 } else {
146 if (fclose(obj->f) == EOF && err == NULL)
147 err = got_error_from_errno("fclose");
149 free(obj);
150 return err;
153 void
154 got_object_qid_free(struct got_object_qid *qid)
156 free(qid);
159 void
160 got_object_id_queue_free(struct got_object_id_queue *ids)
162 struct got_object_qid *qid;
164 while (!STAILQ_EMPTY(ids)) {
165 qid = STAILQ_FIRST(ids);
166 STAILQ_REMOVE_HEAD(ids, entry);
167 got_object_qid_free(qid);
171 const struct got_error *
172 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
174 const char *obj_labels[] = {
175 GOT_OBJ_LABEL_COMMIT,
176 GOT_OBJ_LABEL_TREE,
177 GOT_OBJ_LABEL_BLOB,
178 GOT_OBJ_LABEL_TAG,
179 };
180 const int obj_types[] = {
181 GOT_OBJ_TYPE_COMMIT,
182 GOT_OBJ_TYPE_TREE,
183 GOT_OBJ_TYPE_BLOB,
184 GOT_OBJ_TYPE_TAG,
185 };
186 int type = 0;
187 size_t size = 0;
188 size_t i;
189 char *end;
191 *obj = NULL;
193 end = memchr(buf, '\0', len);
194 if (end == NULL)
195 return got_error(GOT_ERR_BAD_OBJ_HDR);
197 for (i = 0; i < nitems(obj_labels); i++) {
198 const char *label = obj_labels[i];
199 size_t label_len = strlen(label);
200 const char *errstr;
202 if (len <= label_len || buf + label_len >= end ||
203 strncmp(buf, label, label_len) != 0)
204 continue;
206 type = obj_types[i];
207 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
208 if (errstr != NULL)
209 return got_error(GOT_ERR_BAD_OBJ_HDR);
210 break;
213 if (type == 0)
214 return got_error(GOT_ERR_BAD_OBJ_HDR);
216 *obj = calloc(1, sizeof(**obj));
217 if (*obj == NULL)
218 return got_error_from_errno("calloc");
219 (*obj)->type = type;
220 (*obj)->hdrlen = end - buf + 1;
221 (*obj)->size = size;
222 return NULL;
225 const struct got_error *
226 got_object_read_header(struct got_object **obj, int fd)
228 const struct got_error *err;
229 struct got_inflate_buf zb;
230 uint8_t *buf;
231 const size_t zbsize = 64;
232 size_t outlen, totlen;
233 int nbuf = 1;
235 *obj = NULL;
237 buf = malloc(zbsize);
238 if (buf == NULL)
239 return got_error_from_errno("malloc");
240 buf[0] = '\0';
242 err = got_inflate_init(&zb, buf, zbsize, NULL);
243 if (err)
244 return err;
246 totlen = 0;
247 do {
248 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
249 if (err)
250 goto done;
251 if (outlen == 0)
252 break;
253 totlen += outlen;
254 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
255 uint8_t *newbuf;
256 nbuf++;
257 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
258 if (newbuf == NULL) {
259 err = got_error_from_errno("recallocarray");
260 goto done;
262 buf = newbuf;
263 zb.outbuf = newbuf + totlen;
264 zb.outlen = (nbuf * zbsize) - totlen;
266 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
268 err = got_object_parse_header(obj, buf, totlen);
269 done:
270 free(buf);
271 got_inflate_end(&zb);
272 return err;
275 struct got_commit_object *
276 got_object_commit_alloc_partial(void)
278 struct got_commit_object *commit;
280 commit = calloc(1, sizeof(*commit));
281 if (commit == NULL)
282 return NULL;
283 commit->tree_id = malloc(sizeof(*commit->tree_id));
284 if (commit->tree_id == NULL) {
285 free(commit);
286 return NULL;
289 STAILQ_INIT(&commit->parent_ids);
291 return commit;
294 const struct got_error *
295 got_object_commit_add_parent(struct got_commit_object *commit,
296 const char *id_str)
298 const struct got_error *err = NULL;
299 struct got_object_qid *qid;
301 err = got_object_qid_alloc_partial(&qid);
302 if (err)
303 return err;
305 if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
306 err = got_error(GOT_ERR_BAD_OBJ_DATA);
307 got_object_qid_free(qid);
308 return err;
311 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
312 commit->nparents++;
314 return NULL;
317 static const struct got_error *
318 parse_gmtoff(time_t *gmtoff, const char *tzstr)
320 int sign = 1;
321 const char *p = tzstr;
322 time_t h, m;
324 *gmtoff = 0;
326 if (*p == '-')
327 sign = -1;
328 else if (*p != '+')
329 return got_error(GOT_ERR_BAD_OBJ_DATA);
330 p++;
331 if (!isdigit(*p) && !isdigit(*(p + 1)))
332 return got_error(GOT_ERR_BAD_OBJ_DATA);
333 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
335 p += 2;
336 if (!isdigit(*p) && !isdigit(*(p + 1)))
337 return got_error(GOT_ERR_BAD_OBJ_DATA);
338 m = ((*p - '0') * 10) + (*(p + 1) - '0');
340 *gmtoff = (h * 60 * 60 + m * 60) * sign;
341 return NULL;
344 static const struct got_error *
345 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
347 const struct got_error *err = NULL;
348 const char *errstr;
349 char *space, *tzstr;
351 /* Parse and strip off trailing timezone indicator string. */
352 space = strrchr(committer, ' ');
353 if (space == NULL)
354 return got_error(GOT_ERR_BAD_OBJ_DATA);
355 tzstr = strdup(space + 1);
356 if (tzstr == NULL)
357 return got_error_from_errno("strdup");
358 err = parse_gmtoff(gmtoff, tzstr);
359 free(tzstr);
360 if (err) {
361 if (err->code != GOT_ERR_BAD_OBJ_DATA)
362 return err;
363 /* Old versions of Git omitted the timestamp. */
364 *time = 0;
365 *gmtoff = 0;
366 return NULL;
368 *space = '\0';
370 /* Timestamp is separated from committer name + email by space. */
371 space = strrchr(committer, ' ');
372 if (space == NULL)
373 return got_error(GOT_ERR_BAD_OBJ_DATA);
375 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
376 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
377 if (errstr)
378 return got_error(GOT_ERR_BAD_OBJ_DATA);
380 /* Strip off parsed time information, leaving just author and email. */
381 *space = '\0';
383 return NULL;
386 void
387 got_object_commit_close(struct got_commit_object *commit)
389 if (commit->refcnt > 0) {
390 commit->refcnt--;
391 if (commit->refcnt > 0)
392 return;
395 got_object_id_queue_free(&commit->parent_ids);
396 free(commit->tree_id);
397 free(commit->author);
398 free(commit->committer);
399 free(commit->logmsg);
400 free(commit);
403 struct got_object_id *
404 got_object_commit_get_tree_id(struct got_commit_object *commit)
406 return commit->tree_id;
409 int
410 got_object_commit_get_nparents(struct got_commit_object *commit)
412 return commit->nparents;
415 const struct got_object_id_queue *
416 got_object_commit_get_parent_ids(struct got_commit_object *commit)
418 return &commit->parent_ids;
421 const char *
422 got_object_commit_get_author(struct got_commit_object *commit)
424 return commit->author;
427 time_t
428 got_object_commit_get_author_time(struct got_commit_object *commit)
430 return commit->author_time;
433 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
435 return commit->author_gmtoff;
438 const char *
439 got_object_commit_get_committer(struct got_commit_object *commit)
441 return commit->committer;
444 time_t
445 got_object_commit_get_committer_time(struct got_commit_object *commit)
447 return commit->committer_time;
450 time_t
451 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
453 return commit->committer_gmtoff;
456 const struct got_error *
457 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
459 const struct got_error *err = NULL;
460 const char *src;
461 char *dst;
462 size_t len;
464 len = strlen(commit->logmsg);
465 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
466 if (*logmsg == NULL)
467 return got_error_from_errno("malloc");
469 /*
470 * Strip out unusual headers. Headers are separated from the commit
471 * message body by a single empty line.
472 */
473 src = commit->logmsg;
474 dst = *logmsg;
475 while (*src != '\0' && *src != '\n') {
476 int copy_header = 1, eol = 0;
477 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
478 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
479 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
480 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
481 strncmp(src, GOT_COMMIT_LABEL_PARENT,
482 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
483 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
484 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
485 copy_header = 0;
487 while (*src != '\0' && !eol) {
488 if (copy_header) {
489 *dst = *src;
490 dst++;
492 if (*src == '\n')
493 eol = 1;
494 src++;
497 *dst = '\0';
499 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
500 err = got_error(GOT_ERR_NO_SPACE);
501 goto done;
504 /* Trim redundant trailing whitespace. */
505 len = strlen(*logmsg);
506 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
507 isspace((unsigned char)(*logmsg)[len - 1])) {
508 (*logmsg)[len - 1] = '\0';
509 len--;
512 /* Append a trailing newline if missing. */
513 if (len > 0 && (*logmsg)[len - 1] != '\n') {
514 (*logmsg)[len] = '\n';
515 (*logmsg)[len + 1] = '\0';
517 done:
518 if (err) {
519 free(*logmsg);
520 *logmsg = NULL;
522 return err;
525 const char *
526 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
528 return commit->logmsg;
531 const struct got_error *
532 got_object_parse_commit(struct got_commit_object **commit, char *buf,
533 size_t len)
535 const struct got_error *err = NULL;
536 char *s = buf;
537 size_t label_len;
538 ssize_t remain = (ssize_t)len;
540 if (remain == 0)
541 return got_error(GOT_ERR_BAD_OBJ_DATA);
543 *commit = got_object_commit_alloc_partial();
544 if (*commit == NULL)
545 return got_error_from_errno("got_object_commit_alloc_partial");
547 label_len = strlen(GOT_COMMIT_LABEL_TREE);
548 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
549 remain -= label_len;
550 if (remain < SHA1_DIGEST_STRING_LENGTH) {
551 err = got_error(GOT_ERR_BAD_OBJ_DATA);
552 goto done;
554 s += label_len;
555 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
556 err = got_error(GOT_ERR_BAD_OBJ_DATA);
557 goto done;
559 remain -= SHA1_DIGEST_STRING_LENGTH;
560 s += SHA1_DIGEST_STRING_LENGTH;
561 } else {
562 err = got_error(GOT_ERR_BAD_OBJ_DATA);
563 goto done;
566 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
567 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
568 remain -= label_len;
569 if (remain < SHA1_DIGEST_STRING_LENGTH) {
570 err = got_error(GOT_ERR_BAD_OBJ_DATA);
571 goto done;
573 s += label_len;
574 err = got_object_commit_add_parent(*commit, s);
575 if (err)
576 goto done;
578 remain -= SHA1_DIGEST_STRING_LENGTH;
579 s += SHA1_DIGEST_STRING_LENGTH;
582 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
583 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
584 char *p;
585 size_t slen;
587 remain -= label_len;
588 if (remain <= 0) {
589 err = got_error(GOT_ERR_BAD_OBJ_DATA);
590 goto done;
592 s += label_len;
593 p = memchr(s, '\n', remain);
594 if (p == NULL) {
595 err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 goto done;
598 *p = '\0';
599 slen = strlen(s);
600 err = parse_commit_time(&(*commit)->author_time,
601 &(*commit)->author_gmtoff, s);
602 if (err)
603 goto done;
604 (*commit)->author = strdup(s);
605 if ((*commit)->author == NULL) {
606 err = got_error_from_errno("strdup");
607 goto done;
609 s += slen + 1;
610 remain -= slen + 1;
613 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
614 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
615 char *p;
616 size_t slen;
618 remain -= label_len;
619 if (remain <= 0) {
620 err = got_error(GOT_ERR_BAD_OBJ_DATA);
621 goto done;
623 s += label_len;
624 p = memchr(s, '\n', remain);
625 if (p == NULL) {
626 err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 goto done;
629 *p = '\0';
630 slen = strlen(s);
631 err = parse_commit_time(&(*commit)->committer_time,
632 &(*commit)->committer_gmtoff, s);
633 if (err)
634 goto done;
635 (*commit)->committer = strdup(s);
636 if ((*commit)->committer == NULL) {
637 err = got_error_from_errno("strdup");
638 goto done;
640 s += slen + 1;
641 remain -= slen + 1;
644 (*commit)->logmsg = strndup(s, remain);
645 if ((*commit)->logmsg == NULL) {
646 err = got_error_from_errno("strndup");
647 goto done;
649 done:
650 if (err) {
651 got_object_commit_close(*commit);
652 *commit = NULL;
654 return err;
657 void
658 got_object_tree_close(struct got_tree_object *tree)
660 if (tree->refcnt > 0) {
661 tree->refcnt--;
662 if (tree->refcnt > 0)
663 return;
666 free(tree->entries);
667 free(tree);
670 static const struct got_error *
671 parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
672 size_t maxlen)
674 char *p, *space;
676 *elen = 0;
678 *elen = strnlen(buf, maxlen) + 1;
679 if (*elen > maxlen)
680 return got_error(GOT_ERR_BAD_OBJ_DATA);
682 space = memchr(buf, ' ', *elen);
683 if (space == NULL || space <= buf)
684 return got_error(GOT_ERR_BAD_OBJ_DATA);
686 pte->mode = 0;
687 p = buf;
688 while (p < space) {
689 if (*p < '0' && *p > '7')
690 return got_error(GOT_ERR_BAD_OBJ_DATA);
691 pte->mode <<= 3;
692 pte->mode |= *p - '0';
693 p++;
696 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
697 return got_error(GOT_ERR_BAD_OBJ_DATA);
699 pte->name = space + 1;
700 pte->namelen = strlen(pte->name);
701 buf += *elen;
702 pte->id = buf;
703 *elen += SHA1_DIGEST_LENGTH;
704 return NULL;
707 static int
708 pte_cmp(const void *pa, const void *pb)
710 const struct got_parsed_tree_entry *a = pa, *b = pb;
712 return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
715 const struct got_error *
716 got_object_parse_tree(struct got_parsed_tree_entry **entries, int *nentries,
717 uint8_t *buf, size_t len)
719 const struct got_error *err = NULL;
720 size_t remain = len, totalloc;
721 const size_t nalloc = 16;
722 struct got_parsed_tree_entry *pte;
723 int i;
725 *nentries = 0;
726 if (remain == 0)
727 return NULL; /* tree is empty */
729 *entries = calloc(nalloc, sizeof(**entries));
730 if (*entries == NULL)
731 return got_error_from_errno("calloc");
732 totalloc = nalloc;
734 while (remain > 0) {
735 size_t elen;
737 if (*nentries >= totalloc) {
738 pte = recallocarray(*entries, totalloc,
739 totalloc + nalloc, sizeof(**entries));
740 if (pte == NULL) {
741 err = got_error_from_errno("recallocarray");
742 goto done;
744 *entries = pte;
745 totalloc += nalloc;
748 pte = &(*entries)[*nentries];
749 err = parse_tree_entry(pte, &elen, buf, remain);
750 if (err)
751 goto done;
752 buf += elen;
753 remain -= elen;
754 (*nentries)++;
757 if (remain != 0) {
758 err = got_error(GOT_ERR_BAD_OBJ_DATA);
759 goto done;
762 if (*nentries > 1) {
763 mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
765 for (i = 0; i < *nentries - 1; i++) {
766 struct got_parsed_tree_entry *prev = &(*entries)[i];
767 pte = &(*entries)[i + 1];
768 if (got_path_cmp(prev->name, pte->name,
769 prev->namelen, pte->namelen) == 0) {
770 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
771 break;
775 done:
776 if (err) {
777 free(*entries);
778 *entries = NULL;
779 *nentries = 0;
781 return err;
784 void
785 got_object_tag_close(struct got_tag_object *tag)
787 if (tag->refcnt > 0) {
788 tag->refcnt--;
789 if (tag->refcnt > 0)
790 return;
793 free(tag->tag);
794 free(tag->tagger);
795 free(tag->tagmsg);
796 free(tag);
799 const struct got_error *
800 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
802 const struct got_error *err = NULL;
803 size_t remain = len;
804 char *s = buf;
805 size_t label_len;
807 if (remain == 0)
808 return got_error(GOT_ERR_BAD_OBJ_DATA);
810 *tag = calloc(1, sizeof(**tag));
811 if (*tag == NULL)
812 return got_error_from_errno("calloc");
814 label_len = strlen(GOT_TAG_LABEL_OBJECT);
815 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
816 remain -= label_len;
817 if (remain < SHA1_DIGEST_STRING_LENGTH) {
818 err = got_error(GOT_ERR_BAD_OBJ_DATA);
819 goto done;
821 s += label_len;
822 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
823 err = got_error(GOT_ERR_BAD_OBJ_DATA);
824 goto done;
826 remain -= SHA1_DIGEST_STRING_LENGTH;
827 s += SHA1_DIGEST_STRING_LENGTH;
828 } else {
829 err = got_error(GOT_ERR_BAD_OBJ_DATA);
830 goto done;
833 if (remain <= 0) {
834 err = got_error(GOT_ERR_BAD_OBJ_DATA);
835 goto done;
838 label_len = strlen(GOT_TAG_LABEL_TYPE);
839 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
840 remain -= label_len;
841 if (remain <= 0) {
842 err = got_error(GOT_ERR_BAD_OBJ_DATA);
843 goto done;
845 s += label_len;
846 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
847 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
848 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
849 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
850 s += label_len;
851 remain -= label_len;
852 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
853 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
854 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
855 label_len = strlen(GOT_OBJ_LABEL_TREE);
856 s += label_len;
857 remain -= label_len;
858 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
859 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
860 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
861 label_len = strlen(GOT_OBJ_LABEL_BLOB);
862 s += label_len;
863 remain -= label_len;
864 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
865 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
866 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
867 label_len = strlen(GOT_OBJ_LABEL_TAG);
868 s += label_len;
869 remain -= label_len;
870 } else {
871 err = got_error(GOT_ERR_BAD_OBJ_DATA);
872 goto done;
875 if (remain <= 0 || *s != '\n') {
876 err = got_error(GOT_ERR_BAD_OBJ_DATA);
877 goto done;
879 s++;
880 remain--;
881 if (remain <= 0) {
882 err = got_error(GOT_ERR_BAD_OBJ_DATA);
883 goto done;
885 } else {
886 err = got_error(GOT_ERR_BAD_OBJ_DATA);
887 goto done;
890 label_len = strlen(GOT_TAG_LABEL_TAG);
891 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
892 char *p;
893 size_t slen;
894 remain -= label_len;
895 if (remain <= 0) {
896 err = got_error(GOT_ERR_BAD_OBJ_DATA);
897 goto done;
899 s += label_len;
900 p = memchr(s, '\n', remain);
901 if (p == NULL) {
902 err = got_error(GOT_ERR_BAD_OBJ_DATA);
903 goto done;
905 *p = '\0';
906 slen = strlen(s);
907 (*tag)->tag = strndup(s, slen);
908 if ((*tag)->tag == NULL) {
909 err = got_error_from_errno("strndup");
910 goto done;
912 s += slen + 1;
913 remain -= slen + 1;
914 if (remain <= 0) {
915 err = got_error(GOT_ERR_BAD_OBJ_DATA);
916 goto done;
918 } else {
919 err = got_error(GOT_ERR_BAD_OBJ_DATA);
920 goto done;
923 label_len = strlen(GOT_TAG_LABEL_TAGGER);
924 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
925 char *p;
926 size_t slen;
928 remain -= label_len;
929 if (remain <= 0) {
930 err = got_error(GOT_ERR_BAD_OBJ_DATA);
931 goto done;
933 s += label_len;
934 p = memchr(s, '\n', remain);
935 if (p == NULL) {
936 err = got_error(GOT_ERR_BAD_OBJ_DATA);
937 goto done;
939 *p = '\0';
940 slen = strlen(s);
941 err = parse_commit_time(&(*tag)->tagger_time,
942 &(*tag)->tagger_gmtoff, s);
943 if (err)
944 goto done;
945 (*tag)->tagger = strdup(s);
946 if ((*tag)->tagger == NULL) {
947 err = got_error_from_errno("strdup");
948 goto done;
950 s += slen + 1;
951 remain -= slen + 1;
952 if (remain < 0) {
953 err = got_error(GOT_ERR_BAD_OBJ_DATA);
954 goto done;
956 } else {
957 /* Some old tags in the Linux git repo have no tagger. */
958 (*tag)->tagger = strdup("");
959 if ((*tag)->tagger == NULL) {
960 err = got_error_from_errno("strdup");
961 goto done;
965 (*tag)->tagmsg = strndup(s, remain);
966 if ((*tag)->tagmsg == NULL) {
967 err = got_error_from_errno("strndup");
968 goto done;
970 done:
971 if (err) {
972 got_object_tag_close(*tag);
973 *tag = NULL;
975 return err;
978 const struct got_error *
979 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
981 const struct got_error *err = NULL;
982 static const size_t blocksize = 512;
983 size_t n, total, remain;
984 uint8_t *buf;
986 *outbuf = NULL;
987 *outlen = 0;
989 buf = malloc(blocksize);
990 if (buf == NULL)
991 return got_error_from_errno("malloc");
993 remain = blocksize;
994 total = 0;
995 for (;;) {
996 if (remain == 0) {
997 uint8_t *newbuf;
998 newbuf = reallocarray(buf, 1, total + blocksize);
999 if (newbuf == NULL) {
1000 err = got_error_from_errno("reallocarray");
1001 goto done;
1003 buf = newbuf;
1004 remain += blocksize;
1006 n = fread(buf + total, 1, remain, f);
1007 if (n == 0) {
1008 if (ferror(f)) {
1009 err = got_ferror(f, GOT_ERR_IO);
1010 goto done;
1012 break; /* EOF */
1014 remain -= n;
1015 total += n;
1018 done:
1019 if (err == NULL) {
1020 *outbuf = buf;
1021 *outlen = total;
1022 } else
1023 free(buf);
1024 return err;