Blob


1 /*
2 * Copyright (c) 2018, 2019 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/syslimits.h>
23 #include <sys/wait.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <imsg.h>
35 #include <time.h>
36 #include <unistd.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 int
59 got_object_id_cmp(const struct got_object_id *id1,
60 const struct got_object_id *id2)
61 {
62 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
63 }
65 const struct got_error *
66 got_object_qid_alloc_partial(struct got_object_qid **qid)
67 {
68 const struct got_error *err = NULL;
70 *qid = malloc(sizeof(**qid));
71 if (*qid == NULL)
72 return got_error_from_errno("malloc");
74 (*qid)->id = malloc(sizeof(*((*qid)->id)));
75 if ((*qid)->id == NULL) {
76 err = got_error_from_errno("malloc");
77 got_object_qid_free(*qid);
78 *qid = NULL;
79 return err;
80 }
82 return NULL;
83 }
85 const struct got_error *
86 got_object_id_str(char **outbuf, struct got_object_id *id)
87 {
88 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
90 *outbuf = malloc(len);
91 if (*outbuf == NULL)
92 return got_error_from_errno("malloc");
94 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
95 free(*outbuf);
96 *outbuf = NULL;
97 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
98 }
100 return NULL;
103 void
104 got_object_close(struct got_object *obj)
106 if (obj->refcnt > 0) {
107 obj->refcnt--;
108 if (obj->refcnt > 0)
109 return;
112 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
113 struct got_delta *delta;
114 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
115 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
116 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
117 free(delta);
120 if (obj->flags & GOT_OBJ_FLAG_PACKED)
121 free(obj->path_packfile);
122 free(obj);
125 void
126 got_object_qid_free(struct got_object_qid *qid)
128 free(qid->id);
129 free(qid);
132 void
133 got_object_id_queue_free(struct got_object_id_queue *ids)
135 struct got_object_qid *qid;
137 while (!SIMPLEQ_EMPTY(ids)) {
138 qid = SIMPLEQ_FIRST(ids);
139 SIMPLEQ_REMOVE_HEAD(ids, entry);
140 got_object_qid_free(qid);
144 const struct got_error *
145 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
147 const char *obj_labels[] = {
148 GOT_OBJ_LABEL_COMMIT,
149 GOT_OBJ_LABEL_TREE,
150 GOT_OBJ_LABEL_BLOB,
151 GOT_OBJ_LABEL_TAG,
152 };
153 const int obj_types[] = {
154 GOT_OBJ_TYPE_COMMIT,
155 GOT_OBJ_TYPE_TREE,
156 GOT_OBJ_TYPE_BLOB,
157 GOT_OBJ_TYPE_TAG,
158 };
159 int type = 0;
160 size_t size = 0, hdrlen = 0;
161 int i;
163 *obj = NULL;
165 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
166 if (hdrlen > len)
167 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 for (i = 0; i < nitems(obj_labels); i++) {
170 const char *label = obj_labels[i];
171 size_t label_len = strlen(label);
172 const char *errstr;
174 if (strncmp(buf, label, label_len) != 0)
175 continue;
177 type = obj_types[i];
178 if (len <= label_len)
179 return got_error(GOT_ERR_BAD_OBJ_HDR);
180 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
181 if (errstr != NULL)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 break;
186 if (type == 0)
187 return got_error(GOT_ERR_BAD_OBJ_HDR);
189 *obj = calloc(1, sizeof(**obj));
190 if (*obj == NULL)
191 return got_error_from_errno("calloc");
192 (*obj)->type = type;
193 (*obj)->hdrlen = hdrlen;
194 (*obj)->size = size;
195 return NULL;
198 const struct got_error *
199 got_object_read_header(struct got_object **obj, int fd)
201 const struct got_error *err;
202 struct got_inflate_buf zb;
203 char *buf;
204 const size_t zbsize = 64;
205 size_t outlen, totlen;
206 int nbuf = 1;
208 *obj = NULL;
210 buf = malloc(zbsize);
211 if (buf == NULL)
212 return got_error_from_errno("malloc");
214 err = got_inflate_init(&zb, buf, zbsize);
215 if (err)
216 return err;
218 totlen = 0;
219 do {
220 err = got_inflate_read_fd(&zb, fd, &outlen);
221 if (err)
222 goto done;
223 if (outlen == 0)
224 break;
225 totlen += outlen;
226 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
227 char *newbuf;
228 nbuf++;
229 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
230 if (newbuf == NULL) {
231 err = got_error_from_errno("recallocarray");
232 goto done;
234 buf = newbuf;
235 zb.outbuf = newbuf + totlen;
236 zb.outlen = (nbuf * zbsize) - totlen;
238 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
240 err = got_object_parse_header(obj, buf, totlen);
241 done:
242 free(buf);
243 got_inflate_end(&zb);
244 return err;
247 struct got_commit_object *
248 got_object_commit_alloc_partial(void)
250 struct got_commit_object *commit;
252 commit = calloc(1, sizeof(*commit));
253 if (commit == NULL)
254 return NULL;
255 commit->tree_id = malloc(sizeof(*commit->tree_id));
256 if (commit->tree_id == NULL) {
257 free(commit);
258 return NULL;
261 SIMPLEQ_INIT(&commit->parent_ids);
263 return commit;
266 const struct got_error *
267 got_object_commit_add_parent(struct got_commit_object *commit,
268 const char *id_str)
270 const struct got_error *err = NULL;
271 struct got_object_qid *qid;
273 err = got_object_qid_alloc_partial(&qid);
274 if (err)
275 return err;
277 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
278 err = got_error(GOT_ERR_BAD_OBJ_DATA);
279 got_object_qid_free(qid);
280 return err;
283 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
284 commit->nparents++;
286 return NULL;
289 static const struct got_error *
290 parse_gmtoff(time_t *gmtoff, const char *tzstr)
292 int sign = 1;
293 const char *p = tzstr;
294 time_t h, m;
296 *gmtoff = 0;
298 if (*p == '-')
299 sign = -1;
300 else if (*p != '+')
301 return got_error(GOT_ERR_BAD_OBJ_DATA);
302 p++;
303 if (!isdigit(*p) && !isdigit(*(p + 1)))
304 return got_error(GOT_ERR_BAD_OBJ_DATA);
305 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
307 p += 2;
308 if (!isdigit(*p) && !isdigit(*(p + 1)))
309 return got_error(GOT_ERR_BAD_OBJ_DATA);
310 m = ((*p - '0') * 10) + (*(p + 1) - '0');
312 *gmtoff = (h * 60 * 60 + m * 60) * sign;
313 return NULL;
316 static const struct got_error *
317 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
319 const struct got_error *err = NULL;
320 const char *errstr;
321 char *space, *tzstr;
323 /* Parse and strip off trailing timezone indicator string. */
324 space = strrchr(committer, ' ');
325 if (space == NULL)
326 return got_error(GOT_ERR_BAD_OBJ_DATA);
327 tzstr = strdup(space + 1);
328 if (tzstr == NULL)
329 return got_error_from_errno("strdup");
330 err = parse_gmtoff(gmtoff, tzstr);
331 free(tzstr);
332 if (err)
333 return err;
334 *space = '\0';
336 /* Timestamp is separated from committer name + email by space. */
337 space = strrchr(committer, ' ');
338 if (space == NULL)
339 return got_error(GOT_ERR_BAD_OBJ_DATA);
341 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
342 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
343 if (errstr)
344 return got_error(GOT_ERR_BAD_OBJ_DATA);
346 /* Strip off parsed time information, leaving just author and email. */
347 *space = '\0';
349 return NULL;
352 void
353 got_object_commit_close(struct got_commit_object *commit)
355 if (commit->refcnt > 0) {
356 commit->refcnt--;
357 if (commit->refcnt > 0)
358 return;
361 got_object_id_queue_free(&commit->parent_ids);
362 free(commit->tree_id);
363 free(commit->author);
364 free(commit->committer);
365 free(commit->logmsg);
366 free(commit);
369 struct got_object_id *
370 got_object_commit_get_tree_id(struct got_commit_object *commit)
372 return commit->tree_id;
375 int
376 got_object_commit_get_nparents(struct got_commit_object *commit)
378 return commit->nparents;
381 const struct got_object_id_queue *
382 got_object_commit_get_parent_ids(struct got_commit_object *commit)
384 return &commit->parent_ids;
387 const char *
388 got_object_commit_get_author(struct got_commit_object *commit)
390 return commit->author;
393 time_t
394 got_object_commit_get_author_time(struct got_commit_object *commit)
396 return commit->author_time;
399 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
401 return commit->author_gmtoff;
404 const char *
405 got_object_commit_get_committer(struct got_commit_object *commit)
407 return commit->committer;
410 time_t
411 got_object_commit_get_committer_time(struct got_commit_object *commit)
413 return commit->committer_time;
416 time_t
417 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
419 return commit->committer_gmtoff;
422 const struct got_error *
423 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
425 const struct got_error *err = NULL;
426 char *msg0, *msg, *line, *s;
427 size_t len;
428 int headers = 1;
430 *logmsg = NULL;
432 msg0 = strdup(commit->logmsg);
433 if (msg0 == NULL)
434 return got_error_from_errno("strdup");
436 /* Copy log message line by line to strip out unusual headers... */
437 msg = msg0;
438 do {
439 if ((line = strsep(&msg, "\n")) == NULL)
440 break;
442 if (headers == 1) {
443 if (line[0] != '\0' &&
444 strncmp(line, GOT_COMMIT_LABEL_TREE,
445 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
446 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
447 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
448 strncmp(line, GOT_COMMIT_LABEL_PARENT,
449 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
450 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
451 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
452 continue;
454 if (line[0] == '\0')
455 headers = 0;
458 if (asprintf(&s, "%s%s\n",
459 *logmsg ? *logmsg : "", line) == -1) {
460 err = got_error_from_errno("asprintf");
461 goto done;
463 free(*logmsg);
464 *logmsg = s;
466 } while (line);
468 /* Trim redundant trailing whitespace. */
469 len = strlen(*logmsg);
470 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
471 isspace((unsigned char)(*logmsg)[len - 1])) {
472 (*logmsg)[len - 1] = '\0';
473 len--;
475 done:
476 free(msg0);
477 if (err) {
478 free(*logmsg);
479 *logmsg = NULL;
481 return err;
484 const char *
485 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
487 return commit->logmsg;
490 const struct got_error *
491 got_object_parse_commit(struct got_commit_object **commit, char *buf,
492 size_t len)
494 const struct got_error *err = NULL;
495 char *s = buf;
496 size_t label_len;
497 ssize_t remain = (ssize_t)len;
499 if (remain == 0)
500 return got_error(GOT_ERR_BAD_OBJ_DATA);
502 *commit = got_object_commit_alloc_partial();
503 if (*commit == NULL)
504 return got_error_from_errno("got_object_commit_alloc_partial");
506 label_len = strlen(GOT_COMMIT_LABEL_TREE);
507 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
508 remain -= label_len;
509 if (remain < SHA1_DIGEST_STRING_LENGTH) {
510 err = got_error(GOT_ERR_BAD_OBJ_DATA);
511 goto done;
513 s += label_len;
514 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
515 err = got_error(GOT_ERR_BAD_OBJ_DATA);
516 goto done;
518 remain -= SHA1_DIGEST_STRING_LENGTH;
519 s += SHA1_DIGEST_STRING_LENGTH;
520 } else {
521 err = got_error(GOT_ERR_BAD_OBJ_DATA);
522 goto done;
525 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
526 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
527 remain -= label_len;
528 if (remain < SHA1_DIGEST_STRING_LENGTH) {
529 err = got_error(GOT_ERR_BAD_OBJ_DATA);
530 goto done;
532 s += label_len;
533 err = got_object_commit_add_parent(*commit, s);
534 if (err)
535 goto done;
537 remain -= SHA1_DIGEST_STRING_LENGTH;
538 s += SHA1_DIGEST_STRING_LENGTH;
541 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
542 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
543 char *p;
544 size_t slen;
546 remain -= label_len;
547 if (remain <= 0) {
548 err = got_error(GOT_ERR_BAD_OBJ_DATA);
549 goto done;
551 s += label_len;
552 p = memchr(s, '\n', remain);
553 if (p == NULL) {
554 err = got_error(GOT_ERR_BAD_OBJ_DATA);
555 goto done;
557 *p = '\0';
558 slen = strlen(s);
559 err = parse_commit_time(&(*commit)->author_time,
560 &(*commit)->author_gmtoff, s);
561 if (err)
562 goto done;
563 (*commit)->author = strdup(s);
564 if ((*commit)->author == NULL) {
565 err = got_error_from_errno("strdup");
566 goto done;
568 s += slen + 1;
569 remain -= slen + 1;
572 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
573 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
574 char *p;
575 size_t slen;
577 remain -= label_len;
578 if (remain <= 0) {
579 err = got_error(GOT_ERR_BAD_OBJ_DATA);
580 goto done;
582 s += label_len;
583 p = memchr(s, '\n', remain);
584 if (p == NULL) {
585 err = got_error(GOT_ERR_BAD_OBJ_DATA);
586 goto done;
588 *p = '\0';
589 slen = strlen(s);
590 err = parse_commit_time(&(*commit)->committer_time,
591 &(*commit)->committer_gmtoff, s);
592 if (err)
593 goto done;
594 (*commit)->committer = strdup(s);
595 if ((*commit)->committer == NULL) {
596 err = got_error_from_errno("strdup");
597 goto done;
599 s += slen + 1;
600 remain -= slen + 1;
603 (*commit)->logmsg = strndup(s, remain);
604 if ((*commit)->logmsg == NULL) {
605 err = got_error_from_errno("strndup");
606 goto done;
608 done:
609 if (err) {
610 got_object_commit_close(*commit);
611 *commit = NULL;
613 return err;
616 void
617 got_object_tree_entry_close(struct got_tree_entry *te)
619 free(te->id);
620 free(te->name);
621 free(te);
624 void
625 got_object_tree_entries_close(struct got_tree_entries *entries)
627 struct got_tree_entry *te;
629 while (!SIMPLEQ_EMPTY(&entries->head)) {
630 te = SIMPLEQ_FIRST(&entries->head);
631 SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
632 got_object_tree_entry_close(te);
636 void
637 got_object_tree_close(struct got_tree_object *tree)
639 if (tree->refcnt > 0) {
640 tree->refcnt--;
641 if (tree->refcnt > 0)
642 return;
645 got_object_tree_entries_close(&tree->entries);
646 free(tree);
649 struct got_tree_entry *
650 got_alloc_tree_entry_partial(void)
652 struct got_tree_entry *te;
654 te = malloc(sizeof(*te));
655 if (te == NULL)
656 return NULL;
658 te->id = malloc(sizeof(*te->id));
659 if (te->id == NULL) {
660 free(te);
661 te = NULL;
663 return te;
666 static const struct got_error *
667 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
668 size_t *elen, char *buf,
669 size_t maxlen)
671 char *p, *space;
672 const struct got_error *err = NULL;
674 *name = NULL;
675 *elen = 0;
677 *pte = malloc(sizeof(**pte));
678 if (*pte == NULL)
679 return got_error_from_errno("malloc");
681 *elen = strnlen(buf, maxlen) + 1;
682 if (*elen > maxlen) {
683 free(*pte);
684 *pte = NULL;
685 return got_error(GOT_ERR_BAD_OBJ_DATA);
688 space = memchr(buf, ' ', *elen);
689 if (space == NULL || space <= buf) {
690 err = got_error(GOT_ERR_BAD_OBJ_DATA);
691 free(*pte);
692 *pte = NULL;
693 return err;
695 (*pte)->mode = 0;
696 p = buf;
697 while (p < space) {
698 if (*p < '0' && *p > '7') {
699 err = got_error(GOT_ERR_BAD_OBJ_DATA);
700 goto done;
702 (*pte)->mode <<= 3;
703 (*pte)->mode |= *p - '0';
704 p++;
707 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
708 err = got_error(GOT_ERR_BAD_OBJ_DATA);
709 goto done;
711 *name = space + 1;
712 buf += *elen;
713 (*pte)->id = buf;
714 *elen += SHA1_DIGEST_LENGTH;
715 done:
716 if (err) {
717 free(*pte);
718 *pte = NULL;
720 return err;
723 const struct got_error *
724 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
725 uint8_t *buf, size_t len)
727 const struct got_error *err = NULL;
728 size_t remain = len;
730 *nentries = 0;
731 if (remain == 0)
732 return NULL; /* tree is empty */
734 while (remain > 0) {
735 struct got_parsed_tree_entry *pte;
736 struct got_pathlist_entry *new = NULL;
737 const char *name;
738 size_t elen;
740 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
741 if (err)
742 goto done;
743 err = got_pathlist_insert(&new, entries, name, pte);
744 if (err)
745 goto done;
746 if (new == NULL) {
747 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
748 goto done;
750 buf += elen;
751 remain -= elen;
752 (*nentries)++;
755 if (remain != 0) {
756 err = got_error(GOT_ERR_BAD_OBJ_DATA);
757 goto done;
759 done:
760 if (err) {
761 got_pathlist_free(entries);
762 *nentries = 0;
764 return err;
767 void
768 got_object_tag_close(struct got_tag_object *tag)
770 if (tag->refcnt > 0) {
771 tag->refcnt--;
772 if (tag->refcnt > 0)
773 return;
776 free(tag->tag);
777 free(tag->tagger);
778 free(tag->tagmsg);
779 free(tag);
782 const struct got_error *
783 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
785 const struct got_error *err = NULL;
786 size_t remain = len;
787 char *s = buf;
788 size_t label_len;
790 if (remain == 0)
791 return got_error(GOT_ERR_BAD_OBJ_DATA);
793 *tag = calloc(1, sizeof(**tag));
794 if (*tag == NULL)
795 return got_error_from_errno("calloc");
797 label_len = strlen(GOT_TAG_LABEL_OBJECT);
798 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
799 remain -= label_len;
800 if (remain < SHA1_DIGEST_STRING_LENGTH) {
801 err = got_error(GOT_ERR_BAD_OBJ_DATA);
802 goto done;
804 s += label_len;
805 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
806 err = got_error(GOT_ERR_BAD_OBJ_DATA);
807 goto done;
809 remain -= SHA1_DIGEST_STRING_LENGTH;
810 s += SHA1_DIGEST_STRING_LENGTH;
811 } else {
812 err = got_error(GOT_ERR_BAD_OBJ_DATA);
813 goto done;
816 if (remain <= 0) {
817 err = got_error(GOT_ERR_BAD_OBJ_DATA);
818 goto done;
821 label_len = strlen(GOT_TAG_LABEL_TYPE);
822 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
823 remain -= label_len;
824 if (remain <= 0) {
825 err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 goto done;
828 s += label_len;
829 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
830 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
831 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
832 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
833 s += label_len;
834 remain -= label_len;
835 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
836 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
837 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
838 label_len = strlen(GOT_OBJ_LABEL_TREE);
839 s += label_len;
840 remain -= label_len;
841 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
842 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
843 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
844 label_len = strlen(GOT_OBJ_LABEL_BLOB);
845 s += label_len;
846 remain -= label_len;
847 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
848 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
849 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
850 label_len = strlen(GOT_OBJ_LABEL_TAG);
851 s += label_len;
852 remain -= label_len;
853 } else {
854 err = got_error(GOT_ERR_BAD_OBJ_DATA);
855 goto done;
858 if (remain <= 0 || *s != '\n') {
859 err = got_error(GOT_ERR_BAD_OBJ_DATA);
860 goto done;
862 s++;
863 remain--;
864 if (remain <= 0) {
865 err = got_error(GOT_ERR_BAD_OBJ_DATA);
866 goto done;
868 } else {
869 err = got_error(GOT_ERR_BAD_OBJ_DATA);
870 goto done;
873 label_len = strlen(GOT_TAG_LABEL_TAG);
874 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
875 char *p;
876 size_t slen;
877 remain -= label_len;
878 if (remain <= 0) {
879 err = got_error(GOT_ERR_BAD_OBJ_DATA);
880 goto done;
882 s += label_len;
883 p = memchr(s, '\n', remain);
884 if (p == NULL) {
885 err = got_error(GOT_ERR_BAD_OBJ_DATA);
886 goto done;
888 *p = '\0';
889 slen = strlen(s);
890 (*tag)->tag = strndup(s, slen);
891 if ((*tag)->tag == NULL) {
892 err = got_error_from_errno("strndup");
893 goto done;
895 s += slen + 1;
896 remain -= slen + 1;
897 if (remain <= 0) {
898 err = got_error(GOT_ERR_BAD_OBJ_DATA);
899 goto done;
901 } else {
902 err = got_error(GOT_ERR_BAD_OBJ_DATA);
903 goto done;
906 label_len = strlen(GOT_TAG_LABEL_TAGGER);
907 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
908 char *p;
909 size_t slen;
911 remain -= label_len;
912 if (remain <= 0) {
913 err = got_error(GOT_ERR_BAD_OBJ_DATA);
914 goto done;
916 s += label_len;
917 p = memchr(s, '\n', remain);
918 if (p == NULL) {
919 err = got_error(GOT_ERR_BAD_OBJ_DATA);
920 goto done;
922 *p = '\0';
923 slen = strlen(s);
924 err = parse_commit_time(&(*tag)->tagger_time,
925 &(*tag)->tagger_gmtoff, s);
926 if (err)
927 goto done;
928 (*tag)->tagger = strdup(s);
929 if ((*tag)->tagger == NULL) {
930 err = got_error_from_errno("strdup");
931 goto done;
933 s += slen + 1;
934 remain -= slen + 1;
935 if (remain <= 0) {
936 err = got_error(GOT_ERR_BAD_OBJ_DATA);
937 goto done;
939 } else {
940 /* Some old tags in the Linux git repo have no tagger. */
941 (*tag)->tagger = strdup("");
942 if ((*tag)->tagger == NULL) {
943 err = got_error_from_errno("strdup");
944 goto done;
948 (*tag)->tagmsg = strndup(s, remain);
949 if ((*tag)->tagmsg == NULL) {
950 err = got_error_from_errno("strndup");
951 goto done;
953 done:
954 if (err) {
955 got_object_tag_close(*tag);
956 *tag = NULL;
958 return err;
961 const struct got_error *
962 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
964 const struct got_error *err = NULL;
965 static const size_t blocksize = 512;
966 size_t n, total, remain;
967 uint8_t *buf;
969 *outbuf = NULL;
970 *outlen = 0;
972 buf = malloc(blocksize);
973 if (buf == NULL)
974 return got_error_from_errno("malloc");
976 remain = blocksize;
977 total = 0;
978 for (;;) {
979 if (remain == 0) {
980 uint8_t *newbuf;
981 newbuf = reallocarray(buf, 1, total + blocksize);
982 if (newbuf == NULL) {
983 err = got_error_from_errno("reallocarray");
984 goto done;
986 buf = newbuf;
987 remain += blocksize;
989 n = fread(buf + total, 1, remain, f);
990 if (n == 0) {
991 if (ferror(f)) {
992 err = got_ferror(f, GOT_ERR_IO);
993 goto done;
995 break; /* EOF */
997 remain -= n;
998 total += n;
999 };
1001 done:
1002 if (err == NULL) {
1003 *outbuf = buf;
1004 *outlen = total;
1005 } else
1006 free(buf);
1007 return err;