Commit Diff


commit - 1db76ab588bf1a976b1aebb8fb5bdbec3e718b69
commit + 4ca7b755cdcdddd626e7de549b567b22e096c116
blob - 0405a334afc01fcfdbfc20ec4cf636f30878f47b
blob + 630811b41d88d95732deb486b2e19d69aac4f813
--- lib/object.c
+++ lib/object.c
@@ -33,6 +33,7 @@
 #include "pack.h"
 #include "delta.h"
 #include "object.h"
+#include "zb.h"
 
 #ifndef MIN
 #define	MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
@@ -81,85 +82,7 @@ got_object_get_type(struct got_object *obj)
 	return 0;
 }
 
-static void
-inflate_end(struct got_zstream_buf *zb)
-{
-	free(zb->inbuf);
-	free(zb->outbuf);
-	inflateEnd(&zb->z);
-}
-
 static const struct got_error *
-inflate_init(struct got_zstream_buf *zb, size_t bufsize)
-{
-	const struct got_error *err = NULL;
-
-	memset(zb, 0, sizeof(*zb));
-
-	zb->z.zalloc = Z_NULL;
-	zb->z.zfree = Z_NULL;
-	if (inflateInit(&zb->z) != Z_OK) {
-		err = got_error(GOT_ERR_IO);
-		goto done;
-	}
-
-	zb->inlen = zb->outlen = bufsize;
-
-	zb->inbuf = calloc(1, zb->inlen);
-	if (zb->inbuf == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
-		goto done;
-	}
-
-	zb->outbuf = calloc(1, zb->outlen);
-	if (zb->outbuf == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
-		goto done;
-	}
-
-done:
-	if (err)
-		inflate_end(zb);
-	return err;
-}
-
-static const struct got_error *
-inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
-{
-	size_t last_total_out = zb->z.total_out;
-	z_stream *z = &zb->z;
-	int n, ret;
-
-	z->next_out = zb->outbuf;
-	z->avail_out = zb->outlen;
-
-	do {
-		if (z->avail_in == 0) {
-			int i;
-			n = fread(zb->inbuf, 1, zb->inlen, f);
-			if (n == 0) {
-				if (ferror(f))
-					return got_ferror(f, GOT_ERR_IO);
-				*outlenp = 0;
-				return NULL;
-			}
-			z->next_in = zb->inbuf;
-			z->avail_in = n;
-		}
-		ret = inflate(z, Z_SYNC_FLUSH);
-	} while (ret == Z_OK && z->avail_out > 0);
-
-	if (ret != Z_OK) {
-		if (ret != Z_STREAM_END)
-			return got_error(GOT_ERR_DECOMPRESSION);
-		zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
-	}
-
-	*outlenp = z->total_out - last_total_out;
-	return NULL;
-}
-
-static const struct got_error *
 parse_object_header(struct got_object **obj, char *buf, size_t len)
 {
 	const char *obj_tags[] = {
@@ -227,14 +150,14 @@ read_object_header(struct got_object **obj, struct got
 	if (buf == NULL)
 		return got_error(GOT_ERR_NO_MEM);
 
-	err = inflate_init(&zb, zbsize);
+	err = got_inflate_init(&zb, zbsize);
 	if (err)
 		return err;
 
 	i = 0;
 	totlen = 0;
 	do {
-		err = inflate_read(&zb, f, &outlen);
+		err = got_inflate_read(&zb, f, &outlen);
 		if (err)
 			goto done;
 		if (strchr(zb.outbuf, '\0') == NULL) {
@@ -251,7 +174,7 @@ read_object_header(struct got_object **obj, struct got
 
 	err = parse_object_header(obj, buf, totlen);
 done:
-	inflate_end(&zb);
+	got_inflate_end(&zb);
 	return err;
 }
 
@@ -580,12 +503,12 @@ read_commit_object(struct got_commit_object **commit,
 	char *p;
 	int i, ret;
 
-	err = inflate_init(&zb, 8192);
+	err = got_inflate_init(&zb, 8192);
 	if (err)
 		return err;
 
 	do {
-		err = inflate_read(&zb, f, &len);
+		err = got_inflate_read(&zb, f, &len);
 		if (err || len == 0)
 			break;
 	} while (len < obj->hdrlen + obj->size);
@@ -599,7 +522,7 @@ read_commit_object(struct got_commit_object **commit,
 	len -= obj->hdrlen;
 	err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
 done:
-	inflate_end(&zb);
+	got_inflate_end(&zb);
 	return err;
 }
 
@@ -649,12 +572,12 @@ read_tree_object(struct got_tree_object **tree,
 	char *p;
 	int i, ret;
 
-	err = inflate_init(&zb, 8192);
+	err = got_inflate_init(&zb, 8192);
 	if (err)
 		return err;
 
 	do {
-		err = inflate_read(&zb, f, &len);
+		err = got_inflate_read(&zb, f, &len);
 		if (err || len == 0)
 			break;
 	} while (len < obj->hdrlen + obj->size);
@@ -668,7 +591,7 @@ read_tree_object(struct got_tree_object **tree,
 	len -= obj->hdrlen;
 	err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
 done:
-	inflate_end(&zb);
+	got_inflate_end(&zb);
 	return err;
 }
 
@@ -727,7 +650,7 @@ got_object_blob_open(struct got_blob_object **blob,
 		return err;
 	}
 
-	err = inflate_init(&(*blob)->zb, blocksize);
+	err = got_inflate_init(&(*blob)->zb, blocksize);
 	if (err != NULL) {
 		fclose((*blob)->f);
 		free(*blob);
@@ -743,7 +666,7 @@ got_object_blob_open(struct got_blob_object **blob,
 void
 got_object_blob_close(struct got_blob_object *blob)
 {
-	inflate_end(&blob->zb);
+	got_inflate_end(&blob->zb);
 	fclose(blob->f);
 	free(blob);
 }
@@ -751,5 +674,5 @@ got_object_blob_close(struct got_blob_object *blob)
 const struct got_error *
 got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
 {
-	return inflate_read(&blob->zb, blob->f, outlenp);
+	return got_inflate_read(&blob->zb, blob->f, outlenp);
 }
blob - /dev/null
blob + 27a5186187c4c25687da18825582c01b29cd0092 (mode 644)
--- /dev/null
+++ lib/zb.c
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/queue.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sha1.h>
+#include <zlib.h>
+
+#include "got_error.h"
+#include "got_object.h"
+
+#include "zb.h"
+
+const struct got_error *
+got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
+{
+	const struct got_error *err = NULL;
+
+	memset(zb, 0, sizeof(*zb));
+
+	zb->z.zalloc = Z_NULL;
+	zb->z.zfree = Z_NULL;
+	if (inflateInit(&zb->z) != Z_OK) {
+		err = got_error(GOT_ERR_IO);
+		goto done;
+	}
+
+	zb->inlen = zb->outlen = bufsize;
+
+	zb->inbuf = calloc(1, zb->inlen);
+	if (zb->inbuf == NULL) {
+		err = got_error(GOT_ERR_NO_MEM);
+		goto done;
+	}
+
+	zb->outbuf = calloc(1, zb->outlen);
+	if (zb->outbuf == NULL) {
+		err = got_error(GOT_ERR_NO_MEM);
+		goto done;
+	}
+
+done:
+	if (err)
+		got_inflate_end(zb);
+	return err;
+}
+
+const struct got_error *
+got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
+{
+	size_t last_total_out = zb->z.total_out;
+	z_stream *z = &zb->z;
+	int n, ret;
+
+	z->next_out = zb->outbuf;
+	z->avail_out = zb->outlen;
+
+	do {
+		if (z->avail_in == 0) {
+			int i;
+			n = fread(zb->inbuf, 1, zb->inlen, f);
+			if (n == 0) {
+				if (ferror(f))
+					return got_ferror(f, GOT_ERR_IO);
+				*outlenp = 0;
+				return NULL;
+			}
+			z->next_in = zb->inbuf;
+			z->avail_in = n;
+		}
+		ret = inflate(z, Z_SYNC_FLUSH);
+	} while (ret == Z_OK && z->avail_out > 0);
+
+	if (ret != Z_OK) {
+		if (ret != Z_STREAM_END)
+			return got_error(GOT_ERR_DECOMPRESSION);
+		zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
+	}
+
+	*outlenp = z->total_out - last_total_out;
+	return NULL;
+}
+
+void
+got_inflate_end(struct got_zstream_buf *zb)
+{
+	free(zb->inbuf);
+	free(zb->outbuf);
+	inflateEnd(&zb->z);
+}
blob - /dev/null
blob + f5e21a1104d34f2659a176038e2d7147e1e90a0d (mode 644)
--- /dev/null
+++ lib/zb.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+const struct got_error *got_inflate_init(struct got_zstream_buf *, size_t);
+const struct got_error *got_inflate_read(struct got_zstream_buf *, FILE *,
+    size_t *);
+void got_inflate_end(struct got_zstream_buf *);
blob - 3c6b737f9189a97ed146ab04feee7a7b28bb0992
blob + d9eed8cc960dc2d7b88f971cfb87b456aeafbc2c
--- regress/packfiles/Makefile
+++ regress/packfiles/Makefile
@@ -2,7 +2,7 @@
 
 PROG = packfile_test
 SRCS = error.c pack.c repository.c object.c path.c sha1.c \
-	delta.c packfile_test.c
+	delta.c zb.c packfile_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
 LDADD = -lutil -lz
blob - ca55e0e5b22d644c5ad5e9f51a79b3d66fbd41b9
blob + 628910d979b4cf3e355218d5ffb6bb645337dfe1
--- regress/repository/Makefile
+++ regress/repository/Makefile
@@ -2,7 +2,7 @@
 
 PROG = repository_test
 SRCS = path.c repository.c error.c refs.c object.c sha1.c diff.c \
-	diffreg.c pack.c delta.c repository_test.c
+	diffreg.c pack.c delta.c zb.c repository_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include
 LDADD = -lutil -lz