Blob


1 /*
2 * Copyright (c) 2017 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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <zlib.h>
25 #include <ctype.h>
26 #include <limits.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
31 #include "got_sha1.h"
33 #ifndef MIN
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 #endif
37 #ifndef nitems
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 #endif
41 #define GOT_OBJ_TAG_COMMIT "commit"
42 #define GOT_OBJ_TAG_TREE "tree"
43 #define GOT_OBJ_TAG_BLOB "blob"
45 #define GOT_COMMIT_TAG_TREE "tree "
46 #define GOT_COMMIT_TAG_PARENT "parent "
47 #define GOT_COMMIT_TAG_AUTHOR "author "
48 #define GOT_COMMIT_TAG_COMMITTER "committer "
50 char *
51 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
52 {
53 char *p = buf;
54 char hex[3];
55 int i;
57 if (size < SHA1_DIGEST_STRING_LENGTH)
58 return NULL;
60 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
61 snprintf(hex, sizeof(hex), "%.2x", id->sha1[i]);
62 p[0] = hex[0];
63 p[1] = hex[1];
64 p += 2;
65 }
66 p[0] = '\0';
68 return buf;
69 }
71 static void
72 inflate_end(struct got_zstream_buf *zb)
73 {
74 free(zb->inbuf);
75 free(zb->outbuf);
76 inflateEnd(&zb->z);
77 }
79 static const struct got_error *
80 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
81 {
82 const struct got_error *err = NULL;
84 memset(zb, 0, sizeof(*zb));
86 zb->z.zalloc = Z_NULL;
87 zb->z.zfree = Z_NULL;
88 if (inflateInit(&zb->z) != Z_OK) {
89 err = got_error(GOT_ERR_IO);
90 goto done;
91 }
93 zb->inlen = zb->outlen = bufsize;
95 zb->inbuf = calloc(1, zb->inlen);
96 if (zb->inbuf == NULL) {
97 err = got_error(GOT_ERR_NO_MEM);
98 goto done;
99 }
101 zb->outbuf = calloc(1, zb->outlen);
102 if (zb->outbuf == NULL) {
103 err = got_error(GOT_ERR_NO_MEM);
104 goto done;
107 done:
108 if (err)
109 inflate_end(zb);
110 return err;
113 static const struct got_error *
114 inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
116 size_t last_total_out = zb->z.total_out;
117 z_stream *z = &zb->z;
118 int n, ret;
120 z->next_out = zb->outbuf;
121 z->avail_out = zb->outlen;
123 do {
124 if (z->avail_in == 0) {
125 int i;
126 n = fread(zb->inbuf, 1, zb->inlen, f);
127 if (n == 0) {
128 if (ferror(f))
129 return got_error(GOT_ERR_IO);
130 *outlenp = 0;
131 return NULL;
133 z->next_in = zb->inbuf;
134 z->avail_in = n;
136 ret = inflate(z, Z_SYNC_FLUSH);
137 } while (ret == Z_OK && z->avail_out > 0);
139 if (ret != Z_OK) {
140 if (ret != Z_STREAM_END)
141 return got_error(GOT_ERR_DECOMPRESSION);
142 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
145 *outlenp = z->total_out - last_total_out;
146 return NULL;
149 static const struct got_error *
150 parse_object_header(struct got_object **obj, char *buf, size_t len)
152 const char *obj_tags[] = {
153 GOT_OBJ_TAG_COMMIT,
154 GOT_OBJ_TAG_TREE,
155 GOT_OBJ_TAG_BLOB
156 };
157 const int obj_types[] = {
158 GOT_OBJ_TYPE_COMMIT,
159 GOT_OBJ_TYPE_TREE,
160 GOT_OBJ_TYPE_BLOB,
161 };
162 int type = 0;
163 size_t size = 0, hdrlen = 0;
164 int i;
165 char *p = strchr(buf, '\0');
167 if (p == NULL)
168 return got_error(GOT_ERR_BAD_OBJ_HDR);
170 hdrlen = strlen(buf) + 1 /* '\0' */;
172 for (i = 0; i < nitems(obj_tags); i++) {
173 const char *tag = obj_tags[i];
174 size_t tlen = strlen(tag);
175 const char *errstr;
177 if (strncmp(buf, tag, tlen) != 0)
178 continue;
180 type = obj_types[i];
181 if (len <= tlen)
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
184 if (errstr != NULL)
185 return got_error(GOT_ERR_BAD_OBJ_HDR);
186 break;
189 if (type == 0)
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 *obj = calloc(1, sizeof(**obj));
193 (*obj)->type = type;
194 (*obj)->hdrlen = hdrlen;
195 (*obj)->size = size;
196 return NULL;
199 static const struct got_error *
200 read_object_header(struct got_object **obj, struct got_repository *repo,
201 const char *path)
203 const struct got_error *err;
204 FILE *f;
205 struct got_zstream_buf zb;
206 char *buf;
207 size_t len;
208 const size_t zbsize = 64;
209 size_t outlen, totlen;
210 int i, ret;
212 f = fopen(path, "rb");
213 if (f == NULL)
214 return got_error(GOT_ERR_BAD_PATH);
216 buf = calloc(zbsize, sizeof(char));
217 if (buf == NULL)
218 return got_error(GOT_ERR_NO_MEM);
220 err = inflate_init(&zb, zbsize);
221 if (err) {
222 fclose(f);
223 return err;
226 i = 0;
227 totlen = 0;
228 do {
229 err = inflate_read(&zb, f, &outlen);
230 if (err)
231 goto done;
232 if (strchr(zb.outbuf, '\0') == NULL) {
233 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
234 if (buf == NULL) {
235 err = got_error(GOT_ERR_NO_MEM);
236 goto done;
239 memcpy(buf + totlen, zb.outbuf, outlen);
240 totlen += outlen;
241 i++;
242 } while (strchr(zb.outbuf, '\0') == NULL);
244 err = parse_object_header(obj, buf, totlen);
245 done:
246 inflate_end(&zb);
247 fclose(f);
248 return err;
251 static const struct got_error *
252 object_path(char **path, struct got_object_id *id,
253 struct got_repository *repo)
255 const struct got_error *err = NULL;
256 char hex[SHA1_DIGEST_STRING_LENGTH];
257 char *path_objects = got_repo_get_path_objects(repo);
259 if (path_objects == NULL)
260 return got_error(GOT_ERR_NO_MEM);
262 got_object_id_str(id, hex, sizeof(hex));
264 if (asprintf(path, "%s/%.2x/%s", path_objects,
265 id->sha1[0], hex + 2) == -1)
266 err = got_error(GOT_ERR_NO_MEM);
268 free(path_objects);
269 return err;
272 const struct got_error *
273 got_object_open(struct got_object **obj, struct got_repository *repo,
274 struct got_object_id *id)
276 const struct got_error *err = NULL;
277 char *path = NULL;
279 err = object_path(&path, id, repo);
280 if (err)
281 return err;
283 err = read_object_header(obj, repo, path);
284 if (err == NULL)
285 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
286 done:
287 free(path);
288 return err;
291 void
292 got_object_close(struct got_object *obj)
294 free(obj);
297 static int
298 commit_object_valid(struct got_commit_object *commit)
300 int i;
301 int n;
303 if (commit == NULL)
304 return 0;
306 n = 0;
307 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
308 if (commit->tree_id.sha1[i] == 0)
309 n++;
311 if (n == SHA1_DIGEST_LENGTH)
312 return 0;
314 return 1;
317 static const struct got_error *
318 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
320 const struct got_error *err = NULL;
321 char *s = buf;
322 size_t tlen;
323 ssize_t remain = (ssize_t)len;
325 *commit = calloc(1, sizeof(**commit));
326 if (*commit == NULL)
327 return got_error(GOT_ERR_NO_MEM);
329 SIMPLEQ_INIT(&(*commit)->parent_ids);
331 tlen = strlen(GOT_COMMIT_TAG_TREE);
332 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
333 remain -= tlen;
334 if (remain < SHA1_DIGEST_STRING_LENGTH) {
335 err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 goto done;
338 s += tlen;
339 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
340 err = got_error(GOT_ERR_BAD_OBJ_DATA);
341 goto done;
343 remain -= SHA1_DIGEST_STRING_LENGTH;
344 s += SHA1_DIGEST_STRING_LENGTH;
345 } else {
346 err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 goto done;
350 tlen = strlen(GOT_COMMIT_TAG_PARENT);
351 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
352 struct got_parent_id *pid;
354 remain -= tlen;
355 if (remain < SHA1_DIGEST_STRING_LENGTH) {
356 err = got_error(GOT_ERR_BAD_OBJ_DATA);
357 goto done;
360 pid = calloc(1, sizeof(*pid));
361 if (pid == NULL) {
362 err = got_error(GOT_ERR_NO_MEM);
363 goto done;
365 s += tlen;
366 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
367 err = got_error(GOT_ERR_BAD_OBJ_DATA);
368 goto done;
370 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
371 (*commit)->nparents++;
373 s += SHA1_DIGEST_STRING_LENGTH;
376 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
377 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
378 char *p;
380 remain -= tlen;
381 if (remain <= 0) {
382 err = got_error(GOT_ERR_BAD_OBJ_DATA);
383 goto done;
385 s += tlen;
386 p = strchr(s, '\n');
387 if (p == NULL) {
388 err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 goto done;
391 *p = '\0';
392 (*commit)->author = strdup(s);
393 if ((*commit)->author == NULL) {
394 err = got_error(GOT_ERR_NO_MEM);
395 goto done;
397 s += strlen((*commit)->author) + 1;
400 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
401 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
402 char *p;
404 remain -= tlen;
405 if (remain <= 0) {
406 err = got_error(GOT_ERR_BAD_OBJ_DATA);
407 goto done;
409 s += tlen;
410 p = strchr(s, '\n');
411 if (p == NULL) {
412 err = got_error(GOT_ERR_BAD_OBJ_DATA);
413 goto done;
415 *p = '\0';
416 (*commit)->committer = strdup(s);
417 if ((*commit)->committer == NULL) {
418 err = got_error(GOT_ERR_NO_MEM);
419 goto done;
421 s += strlen((*commit)->committer) + 1;
424 (*commit)->logmsg = strdup(s);
425 done:
426 if (err)
427 got_object_commit_close(*commit);
428 return err;
431 static void
432 tree_entry_close(struct got_tree_entry *te)
434 free(te->name);
435 free(te);
438 static const struct got_error *
439 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
440 size_t maxlen)
442 char *p = buf, *space;
443 const struct got_error *err = NULL;
444 char hex[SHA1_DIGEST_STRING_LENGTH];
446 *te = calloc(1, sizeof(**te));
447 if (*te == NULL)
448 return got_error(GOT_ERR_NO_MEM);
450 *elen = strlen(buf) + 1;
451 if (*elen > maxlen) {
452 free(*te);
453 return got_error(GOT_ERR_BAD_OBJ_DATA);
456 space = strchr(buf, ' ');
457 if (space == NULL) {
458 free(*te);
459 return got_error(GOT_ERR_BAD_OBJ_DATA);
461 while (*p != ' ') {
462 if (*p < '0' && *p > '7') {
463 err = got_error(GOT_ERR_BAD_OBJ_DATA);
464 goto done;
466 (*te)->mode <<= 3;
467 (*te)->mode |= *p - '0';
468 p++;
471 (*te)->name = strdup(space + 1);
472 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
473 err = got_error(GOT_ERR_BAD_OBJ_DATA);
474 goto done;
476 buf += strlen(buf) + 1;
477 memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
478 *elen += SHA1_DIGEST_LENGTH;
479 done:
480 if (err)
481 tree_entry_close(*te);
482 return err;
485 static const struct got_error *
486 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
487 char *buf, size_t len)
489 size_t remain = len;
490 int nentries;
492 *tree = calloc(1, sizeof(**tree));
493 if (*tree == NULL)
494 return got_error(GOT_ERR_NO_MEM);
496 SIMPLEQ_INIT(&(*tree)->entries);
498 while (remain > 0) {
499 struct got_tree_entry *te;
500 size_t elen;
502 parse_tree_entry(&te, &elen, buf, remain);
503 (*tree)->nentries++;
504 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
505 buf += elen;
506 remain -= elen;
509 if (remain != 0) {
510 got_object_tree_close(*tree);
511 return got_error(GOT_ERR_BAD_OBJ_DATA);
514 return NULL;
517 static const struct got_error *
518 read_commit_object(struct got_commit_object **commit,
519 struct got_repository *repo, struct got_object *obj, const char *path)
521 const struct got_error *err = NULL;
522 FILE *f;
523 struct got_zstream_buf zb;
524 size_t len;
525 char *p;
526 int i, ret;
528 f = fopen(path, "rb");
529 if (f == NULL)
530 return got_error(GOT_ERR_BAD_PATH);
532 err = inflate_init(&zb, 8192);
533 if (err) {
534 fclose(f);
535 return err;
538 do {
539 err = inflate_read(&zb, f, &len);
540 if (err || len == 0)
541 break;
542 } while (len < obj->hdrlen + obj->size);
544 if (len < obj->hdrlen + obj->size) {
545 err = got_error(GOT_ERR_BAD_OBJ_DATA);
546 goto done;
549 /* Skip object header. */
550 len -= obj->hdrlen;
551 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
552 done:
553 inflate_end(&zb);
554 fclose(f);
555 return err;
558 const struct got_error *
559 got_object_commit_open(struct got_commit_object **commit,
560 struct got_repository *repo, struct got_object *obj)
562 const struct got_error *err = NULL;
563 char *path = NULL;
565 if (obj->type != GOT_OBJ_TYPE_COMMIT)
566 return got_error(GOT_ERR_OBJ_TYPE);
568 err = object_path(&path, &obj->id, repo);
569 if (err)
570 return err;
572 err = read_commit_object(commit, repo, obj, path);
573 free(path);
574 return err;
577 void
578 got_object_commit_close(struct got_commit_object *commit)
580 struct got_parent_id *pid;
582 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
583 pid = SIMPLEQ_FIRST(&commit->parent_ids);
584 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
585 free(pid);
588 free(commit->author);
589 free(commit->committer);
590 free(commit->logmsg);
591 free(commit);
594 static const struct got_error *
595 read_tree_object(struct got_tree_object **tree,
596 struct got_repository *repo, struct got_object *obj, const char *path)
598 const struct got_error *err = NULL;
599 FILE *f;
600 struct got_zstream_buf zb;
601 size_t len;
602 char *p;
603 int i, ret;
605 f = fopen(path, "rb");
606 if (f == NULL)
607 return got_error(GOT_ERR_BAD_PATH);
609 err = inflate_init(&zb, 8192);
610 if (err) {
611 fclose(f);
612 return err;
615 do {
616 err = inflate_read(&zb, f, &len);
617 if (err || len == 0)
618 break;
619 } while (len < obj->hdrlen + obj->size);
621 if (len < obj->hdrlen + obj->size) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 goto done;
626 /* Skip object header. */
627 len -= obj->hdrlen;
628 err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
629 done:
630 inflate_end(&zb);
631 fclose(f);
632 return err;
635 const struct got_error *
636 got_object_tree_open(struct got_tree_object **tree,
637 struct got_repository *repo, struct got_object *obj)
639 const struct got_error *err = NULL;
640 char *path = NULL;
642 if (obj->type != GOT_OBJ_TYPE_TREE)
643 return got_error(GOT_ERR_OBJ_TYPE);
645 err = object_path(&path, &obj->id, repo);
646 if (err)
647 return err;
649 err = read_tree_object(tree, repo, obj, path);
650 free(path);
651 return err;
654 void
655 got_object_tree_close(struct got_tree_object *tree)
657 struct got_tree_entry *te;
659 while (!SIMPLEQ_EMPTY(&tree->entries)) {
660 te = SIMPLEQ_FIRST(&tree->entries);
661 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
662 tree_entry_close(te);
665 free(tree);
668 const struct got_error *
669 got_object_blob_open(struct got_blob_object **blob,
670 struct got_repository *repo, struct got_object *obj, size_t blocksize)
672 const struct got_error *err = NULL;
673 char *path;
675 if (obj->type != GOT_OBJ_TYPE_BLOB)
676 return got_error(GOT_ERR_OBJ_TYPE);
678 if (blocksize < obj->hdrlen)
679 return got_error(GOT_ERR_NO_SPACE);
681 err = object_path(&path, &obj->id, repo);
682 if (err)
683 return err;
685 *blob = calloc(1, sizeof(**blob));
686 if (*blob == NULL) {
687 free(path);
688 return got_error(GOT_ERR_NO_MEM);
691 (*blob)->f = fopen(path, "rb");
692 if ((*blob)->f == NULL) {
693 free(*blob);
694 free(path);
695 return got_error(GOT_ERR_BAD_PATH);
698 err = inflate_init(&(*blob)->zb, blocksize);
699 if (err != NULL) {
700 fclose((*blob)->f);
701 free(*blob);
702 free(path);
703 return err;
706 (*blob)->hdrlen = obj->hdrlen;
707 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
709 free(path);
710 return err;
713 void
714 got_object_blob_close(struct got_blob_object *blob)
716 inflate_end(&blob->zb);
717 fclose(blob->f);
718 free(blob);
721 const struct got_error *
722 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
724 return inflate_read(&blob->zb, blob->f, outlenp);