Blob


1 /*
2 * Copyright (c) 2018 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/stat.h>
18 #include <sys/queue.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sha1.h>
25 #include <zlib.h>
26 #include <ctype.h>
27 #include <limits.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_sha1.h"
33 #include "pack.h"
35 #ifndef MIN
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 #endif
39 #ifndef nitems
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #endif
43 #define GOT_OBJ_TAG_COMMIT "commit"
44 #define GOT_OBJ_TAG_TREE "tree"
45 #define GOT_OBJ_TAG_BLOB "blob"
47 #define GOT_COMMIT_TAG_TREE "tree "
48 #define GOT_COMMIT_TAG_PARENT "parent "
49 #define GOT_COMMIT_TAG_AUTHOR "author "
50 #define GOT_COMMIT_TAG_COMMITTER "committer "
52 char *
53 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
54 {
55 return got_sha1_digest_to_str(id->sha1, buf, size);
56 }
58 int
59 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
60 {
61 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
62 }
64 const char *
65 got_object_get_type_tag(int type)
66 {
67 switch (type) {
68 case GOT_OBJ_TYPE_COMMIT:
69 return GOT_OBJ_TAG_COMMIT;
70 case GOT_OBJ_TYPE_TREE:
71 return GOT_OBJ_TAG_TREE;
72 case GOT_OBJ_TYPE_BLOB:
73 return GOT_OBJ_TAG_BLOB;
74 }
76 return NULL;
77 }
79 static void
80 inflate_end(struct got_zstream_buf *zb)
81 {
82 free(zb->inbuf);
83 free(zb->outbuf);
84 inflateEnd(&zb->z);
85 }
87 static const struct got_error *
88 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
89 {
90 const struct got_error *err = NULL;
92 memset(zb, 0, sizeof(*zb));
94 zb->z.zalloc = Z_NULL;
95 zb->z.zfree = Z_NULL;
96 if (inflateInit(&zb->z) != Z_OK) {
97 err = got_error(GOT_ERR_IO);
98 goto done;
99 }
101 zb->inlen = zb->outlen = bufsize;
103 zb->inbuf = calloc(1, zb->inlen);
104 if (zb->inbuf == NULL) {
105 err = got_error(GOT_ERR_NO_MEM);
106 goto done;
109 zb->outbuf = calloc(1, zb->outlen);
110 if (zb->outbuf == NULL) {
111 err = got_error(GOT_ERR_NO_MEM);
112 goto done;
115 done:
116 if (err)
117 inflate_end(zb);
118 return err;
121 static const struct got_error *
122 inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
124 size_t last_total_out = zb->z.total_out;
125 z_stream *z = &zb->z;
126 int n, ret;
128 z->next_out = zb->outbuf;
129 z->avail_out = zb->outlen;
131 do {
132 if (z->avail_in == 0) {
133 int i;
134 n = fread(zb->inbuf, 1, zb->inlen, f);
135 if (n == 0) {
136 if (ferror(f))
137 return got_ferror(f, GOT_ERR_IO);
138 *outlenp = 0;
139 return NULL;
141 z->next_in = zb->inbuf;
142 z->avail_in = n;
144 ret = inflate(z, Z_SYNC_FLUSH);
145 } while (ret == Z_OK && z->avail_out > 0);
147 if (ret != Z_OK) {
148 if (ret != Z_STREAM_END)
149 return got_error(GOT_ERR_DECOMPRESSION);
150 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
153 *outlenp = z->total_out - last_total_out;
154 return NULL;
157 static const struct got_error *
158 parse_object_header(struct got_object **obj, char *buf, size_t len)
160 const char *obj_tags[] = {
161 GOT_OBJ_TAG_COMMIT,
162 GOT_OBJ_TAG_TREE,
163 GOT_OBJ_TAG_BLOB
164 };
165 const int obj_types[] = {
166 GOT_OBJ_TYPE_COMMIT,
167 GOT_OBJ_TYPE_TREE,
168 GOT_OBJ_TYPE_BLOB,
169 };
170 int type = 0;
171 size_t size = 0, hdrlen = 0;
172 int i;
173 char *p = strchr(buf, '\0');
175 if (p == NULL)
176 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 hdrlen = strlen(buf) + 1 /* '\0' */;
180 for (i = 0; i < nitems(obj_tags); i++) {
181 const char *tag = obj_tags[i];
182 size_t tlen = strlen(tag);
183 const char *errstr;
185 if (strncmp(buf, tag, tlen) != 0)
186 continue;
188 type = obj_types[i];
189 if (len <= tlen)
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
191 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
192 if (errstr != NULL)
193 return got_error(GOT_ERR_BAD_OBJ_HDR);
194 break;
197 if (type == 0)
198 return got_error(GOT_ERR_BAD_OBJ_HDR);
200 *obj = calloc(1, sizeof(**obj));
201 (*obj)->type = type;
202 (*obj)->hdrlen = hdrlen;
203 (*obj)->size = size;
204 return NULL;
207 static const struct got_error *
208 read_object_header(struct got_object **obj, struct got_repository *repo,
209 FILE *f)
211 const struct got_error *err;
212 struct got_zstream_buf zb;
213 char *buf;
214 size_t len;
215 const size_t zbsize = 64;
216 size_t outlen, totlen;
217 int i, ret;
219 buf = calloc(zbsize, sizeof(char));
220 if (buf == NULL)
221 return got_error(GOT_ERR_NO_MEM);
223 err = inflate_init(&zb, zbsize);
224 if (err)
225 return err;
227 i = 0;
228 totlen = 0;
229 do {
230 err = inflate_read(&zb, f, &outlen);
231 if (err)
232 goto done;
233 if (strchr(zb.outbuf, '\0') == NULL) {
234 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
235 if (buf == NULL) {
236 err = got_error(GOT_ERR_NO_MEM);
237 goto done;
240 memcpy(buf + totlen, zb.outbuf, outlen);
241 totlen += outlen;
242 i++;
243 } while (strchr(zb.outbuf, '\0') == NULL);
245 err = parse_object_header(obj, buf, totlen);
246 done:
247 inflate_end(&zb);
248 return err;
251 static const struct got_error *
252 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
254 const struct got_error *err = NULL;
255 char hex[SHA1_DIGEST_STRING_LENGTH];
256 char *path_objects = got_repo_get_path_objects(repo);
258 if (path_objects == NULL)
259 return got_error(GOT_ERR_NO_MEM);
261 got_object_id_str(id, hex, sizeof(hex));
263 if (asprintf(path, "%s/%.2x/%s", path_objects,
264 id->sha1[0], hex + 2) == -1)
265 err = got_error(GOT_ERR_NO_MEM);
267 free(path_objects);
268 return err;
271 const struct got_error *
272 open_object(FILE **f, struct got_object *obj, struct got_repository *repo)
274 const struct got_error *err = NULL;
275 char *path;
277 if (obj->flags & GOT_OBJ_FLAG_PACKED)
278 return got_packfile_extract_object(f, obj, repo);
280 err = object_path(&path, &obj->id, repo);
281 if (err)
282 return err;
283 *f = fopen(path, "rb");
284 if (*f == NULL) {
285 err = got_error_from_errno();
286 goto done;
288 done:
289 free(path);
290 return err;
293 const struct got_error *
294 got_object_open(struct got_object **obj, struct got_repository *repo,
295 struct got_object_id *id)
297 const struct got_error *err = NULL;
298 char *path;
299 FILE *f;
301 err = object_path(&path, id, repo);
302 if (err)
303 return err;
305 f = fopen(path, "rb");
306 if (f == NULL) {
307 if (errno != ENOENT) {
308 err = got_error_from_errno();
309 goto done;
311 err = got_packfile_open_object(obj, id, repo);
312 if (err)
313 goto done;
314 if (*obj == NULL)
315 err = got_error(GOT_ERR_NO_OBJ);
316 } else {
317 err = read_object_header(obj, repo, f);
318 if (err)
319 goto done;
320 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
322 done:
323 free(path);
324 if (err && f)
325 fclose(f);
326 return err;
330 void
331 got_object_close(struct got_object *obj)
333 free(obj->path_packfile);
334 free(obj);
337 static int
338 commit_object_valid(struct got_commit_object *commit)
340 int i;
341 int n;
343 if (commit == NULL)
344 return 0;
346 n = 0;
347 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
348 if (commit->tree_id.sha1[i] == 0)
349 n++;
351 if (n == SHA1_DIGEST_LENGTH)
352 return 0;
354 return 1;
357 static const struct got_error *
358 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
360 const struct got_error *err = NULL;
361 char *s = buf;
362 size_t tlen;
363 ssize_t remain = (ssize_t)len;
365 *commit = calloc(1, sizeof(**commit));
366 if (*commit == NULL)
367 return got_error(GOT_ERR_NO_MEM);
369 SIMPLEQ_INIT(&(*commit)->parent_ids);
371 tlen = strlen(GOT_COMMIT_TAG_TREE);
372 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
373 remain -= tlen;
374 if (remain < SHA1_DIGEST_STRING_LENGTH) {
375 err = got_error(GOT_ERR_BAD_OBJ_DATA);
376 goto done;
378 s += tlen;
379 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
380 err = got_error(GOT_ERR_BAD_OBJ_DATA);
381 goto done;
383 remain -= SHA1_DIGEST_STRING_LENGTH;
384 s += SHA1_DIGEST_STRING_LENGTH;
385 } else {
386 err = got_error(GOT_ERR_BAD_OBJ_DATA);
387 goto done;
390 tlen = strlen(GOT_COMMIT_TAG_PARENT);
391 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
392 struct got_parent_id *pid;
394 remain -= tlen;
395 if (remain < SHA1_DIGEST_STRING_LENGTH) {
396 err = got_error(GOT_ERR_BAD_OBJ_DATA);
397 goto done;
400 pid = calloc(1, sizeof(*pid));
401 if (pid == NULL) {
402 err = got_error(GOT_ERR_NO_MEM);
403 goto done;
405 s += tlen;
406 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
407 err = got_error(GOT_ERR_BAD_OBJ_DATA);
408 goto done;
410 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
411 (*commit)->nparents++;
413 s += SHA1_DIGEST_STRING_LENGTH;
416 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
417 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
418 char *p;
420 remain -= tlen;
421 if (remain <= 0) {
422 err = got_error(GOT_ERR_BAD_OBJ_DATA);
423 goto done;
425 s += tlen;
426 p = strchr(s, '\n');
427 if (p == NULL) {
428 err = got_error(GOT_ERR_BAD_OBJ_DATA);
429 goto done;
431 *p = '\0';
432 (*commit)->author = strdup(s);
433 if ((*commit)->author == NULL) {
434 err = got_error(GOT_ERR_NO_MEM);
435 goto done;
437 s += strlen((*commit)->author) + 1;
440 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
441 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
442 char *p;
444 remain -= tlen;
445 if (remain <= 0) {
446 err = got_error(GOT_ERR_BAD_OBJ_DATA);
447 goto done;
449 s += tlen;
450 p = strchr(s, '\n');
451 if (p == NULL) {
452 err = got_error(GOT_ERR_BAD_OBJ_DATA);
453 goto done;
455 *p = '\0';
456 (*commit)->committer = strdup(s);
457 if ((*commit)->committer == NULL) {
458 err = got_error(GOT_ERR_NO_MEM);
459 goto done;
461 s += strlen((*commit)->committer) + 1;
464 (*commit)->logmsg = strdup(s);
465 done:
466 if (err)
467 got_object_commit_close(*commit);
468 return err;
471 static void
472 tree_entry_close(struct got_tree_entry *te)
474 free(te->name);
475 free(te);
478 static const struct got_error *
479 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
480 size_t maxlen)
482 char *p = buf, *space;
483 const struct got_error *err = NULL;
484 char hex[SHA1_DIGEST_STRING_LENGTH];
486 *te = calloc(1, sizeof(**te));
487 if (*te == NULL)
488 return got_error(GOT_ERR_NO_MEM);
490 *elen = strlen(buf) + 1;
491 if (*elen > maxlen) {
492 free(*te);
493 return got_error(GOT_ERR_BAD_OBJ_DATA);
496 space = strchr(buf, ' ');
497 if (space == NULL) {
498 free(*te);
499 return got_error(GOT_ERR_BAD_OBJ_DATA);
501 while (*p != ' ') {
502 if (*p < '0' && *p > '7') {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 goto done;
506 (*te)->mode <<= 3;
507 (*te)->mode |= *p - '0';
508 p++;
511 (*te)->name = strdup(space + 1);
512 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
513 err = got_error(GOT_ERR_BAD_OBJ_DATA);
514 goto done;
516 buf += strlen(buf) + 1;
517 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
518 *elen += SHA1_DIGEST_LENGTH;
519 done:
520 if (err)
521 tree_entry_close(*te);
522 return err;
525 static const struct got_error *
526 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
527 char *buf, size_t len)
529 size_t remain = len;
530 int nentries;
532 *tree = calloc(1, sizeof(**tree));
533 if (*tree == NULL)
534 return got_error(GOT_ERR_NO_MEM);
536 SIMPLEQ_INIT(&(*tree)->entries);
538 while (remain > 0) {
539 struct got_tree_entry *te;
540 size_t elen;
542 parse_tree_entry(&te, &elen, buf, remain);
543 (*tree)->nentries++;
544 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
545 buf += elen;
546 remain -= elen;
549 if (remain != 0) {
550 got_object_tree_close(*tree);
551 return got_error(GOT_ERR_BAD_OBJ_DATA);
554 return NULL;
557 static const struct got_error *
558 read_commit_object(struct got_commit_object **commit,
559 struct got_repository *repo, struct got_object *obj, FILE *f)
561 const struct got_error *err = NULL;
562 struct got_zstream_buf zb;
563 size_t len;
564 char *p;
565 int i, ret;
567 err = inflate_init(&zb, 8192);
568 if (err)
569 return err;
571 do {
572 err = inflate_read(&zb, f, &len);
573 if (err || len == 0)
574 break;
575 } while (len < obj->hdrlen + obj->size);
577 if (len < obj->hdrlen + obj->size) {
578 err = got_error(GOT_ERR_BAD_OBJ_DATA);
579 goto done;
582 /* Skip object header. */
583 len -= obj->hdrlen;
584 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
585 done:
586 inflate_end(&zb);
587 return err;
590 const struct got_error *
591 got_object_commit_open(struct got_commit_object **commit,
592 struct got_repository *repo, struct got_object *obj)
594 const struct got_error *err = NULL;
595 FILE *f;
597 if (obj->type != GOT_OBJ_TYPE_COMMIT)
598 return got_error(GOT_ERR_OBJ_TYPE);
600 err = open_object(&f, obj, repo);
601 if (err)
602 return err;
604 err = read_commit_object(commit, repo, obj, f);
605 fclose(f);
606 return err;
609 void
610 got_object_commit_close(struct got_commit_object *commit)
612 struct got_parent_id *pid;
614 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
615 pid = SIMPLEQ_FIRST(&commit->parent_ids);
616 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
617 free(pid);
620 free(commit->author);
621 free(commit->committer);
622 free(commit->logmsg);
623 free(commit);
626 static const struct got_error *
627 read_tree_object(struct got_tree_object **tree,
628 struct got_repository *repo, struct got_object *obj, FILE *f)
630 const struct got_error *err = NULL;
631 struct got_zstream_buf zb;
632 size_t len;
633 char *p;
634 int i, ret;
636 err = inflate_init(&zb, 8192);
637 if (err)
638 return err;
640 do {
641 err = inflate_read(&zb, f, &len);
642 if (err || len == 0)
643 break;
644 } while (len < obj->hdrlen + obj->size);
646 if (len < obj->hdrlen + obj->size) {
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
648 goto done;
651 /* Skip object header. */
652 len -= obj->hdrlen;
653 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
654 done:
655 inflate_end(&zb);
656 return err;
659 const struct got_error *
660 got_object_tree_open(struct got_tree_object **tree,
661 struct got_repository *repo, struct got_object *obj)
663 const struct got_error *err = NULL;
664 FILE *f;
666 if (obj->type != GOT_OBJ_TYPE_TREE)
667 return got_error(GOT_ERR_OBJ_TYPE);
669 err = open_object(&f, obj, repo);
670 if (err)
671 return err;
673 err = read_tree_object(tree, repo, obj, f);
674 fclose(f);
675 return err;
678 void
679 got_object_tree_close(struct got_tree_object *tree)
681 struct got_tree_entry *te;
683 while (!SIMPLEQ_EMPTY(&tree->entries)) {
684 te = SIMPLEQ_FIRST(&tree->entries);
685 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
686 tree_entry_close(te);
689 free(tree);
692 const struct got_error *
693 got_object_blob_open(struct got_blob_object **blob,
694 struct got_repository *repo, struct got_object *obj, size_t blocksize)
696 const struct got_error *err = NULL;
698 if (obj->type != GOT_OBJ_TYPE_BLOB)
699 return got_error(GOT_ERR_OBJ_TYPE);
701 if (blocksize < obj->hdrlen)
702 return got_error(GOT_ERR_NO_SPACE);
704 *blob = calloc(1, sizeof(**blob));
705 if (*blob == NULL)
706 return got_error(GOT_ERR_NO_MEM);
708 err = open_object(&((*blob)->f), obj, repo);
709 if (err) {
710 free(*blob);
711 return err;
714 err = inflate_init(&(*blob)->zb, blocksize);
715 if (err != NULL) {
716 fclose((*blob)->f);
717 free(*blob);
718 return err;
721 (*blob)->hdrlen = obj->hdrlen;
722 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
724 return err;
727 void
728 got_object_blob_close(struct got_blob_object *blob)
730 inflate_end(&blob->zb);
731 fclose(blob->f);
732 free(blob);
735 const struct got_error *
736 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
738 return inflate_read(&blob->zb, blob->f, outlenp);