git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Marco Costalba <mcostalba@gmail.com>
To: gitster@pobox.com
Cc: git@vger.kernel.org, Marco Costalba <mcostalba@gmail.com>
Subject: [PATCH 01/11] Introduce stream compress helpers
Date: Sat,  2 Feb 2008 12:35:46 +0100	[thread overview]
Message-ID: <1201952156-6764-1-git-send-email-mcostalba@gmail.com> (raw)

When decompressing a zlib stream use this
helpers instead of calling low level zlib
function.

This patch introduces the necessary framework,
still no code change.

This is the first step in generalizing compress and
decompress functions avoiding zlib directly calls.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
---
 Makefile   |    4 ++--
 compress.c |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 compress.h |   12 ++++++++++++
 3 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 compress.c
 create mode 100644 compress.h

diff --git a/Makefile b/Makefile
index 91a460f..f70f995 100644
--- a/Makefile
+++ b/Makefile
@@ -292,7 +292,7 @@ LIB_FILE=libgit.a
 XDIFF_LIB=xdiff/lib.a
 
 LIB_H = \
-	archive.h blob.h cache.h cache-tree.h commit.h csum-file.h delta.h grep.h \
+	archive.h blob.h cache.h cache-tree.h commit.h compress.h csum-file.h delta.h grep.h \
 	diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \
 	run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
 	tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
@@ -305,7 +305,7 @@ DIFF_OBJS = \
 	diffcore-delta.o log-tree.o
 
 LIB_OBJS = \
-	blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \
+	blob.o commit.o compress.o connect.o csum-file.o cache-tree.o base85.o \
 	date.o diff-delta.o entry.o exec_cmd.o ident.o \
 	pretty.o interpolate.o hash.o \
 	lockfile.o \
diff --git a/compress.c b/compress.c
new file mode 100644
index 0000000..f6986c3
--- /dev/null
+++ b/compress.c
@@ -0,0 +1,58 @@
+#include "cache.h"
+#include "compress.h"
+
+unsigned long compress_alloc(z_stream *stream, int level, unsigned long size)
+{
+	memset(stream, 0, sizeof(*stream));
+	deflateInit(stream, level);
+	return deflateBound(stream, size);
+}
+
+int compress_start(z_stream *stream,
+                   unsigned char *in, unsigned long in_size,
+                   unsigned char *out, unsigned long out_size)
+{
+	stream->next_out = (out ? out : xmalloc(out_size));
+	stream->avail_out = out_size;
+	stream->next_in = in;
+	stream->avail_in = in_size;
+	return Z_OK;
+}
+
+int compress_next(z_stream *stream, int flush)
+{
+	int result;
+
+	do {
+		result = deflate(stream, flush);
+	} while (result == Z_OK);
+
+	return result;
+}
+
+unsigned long compress_free(z_stream *stream)
+{
+	deflateEnd(stream);
+	return stream->total_out;
+}
+
+unsigned long compress_all(int level, unsigned char *data,
+                           unsigned long size, unsigned char **out)
+{
+	int bound, result;
+	z_stream stream;
+
+	bound = compress_alloc(&stream, level, size);
+	compress_start(&stream, data, size, NULL, bound);
+
+	*out = stream.next_out;
+	result = compress_next(&stream, Z_FINISH);
+
+	if (result != Z_STREAM_END) {
+		compress_free(&stream);
+		free(*out);
+		*out = NULL;
+		return 0;
+	}
+	return compress_free(&stream);
+}
diff --git a/compress.h b/compress.h
new file mode 100644
index 0000000..d73c365
--- /dev/null
+++ b/compress.h
@@ -0,0 +1,12 @@
+#ifndef COMPRESS_H
+#define COMPRESS_H
+
+extern unsigned long compress_alloc(z_stream *stream, int level, unsigned long size);
+extern int compress_start(z_stream *stream, unsigned char *in, unsigned long in_size,
+                           unsigned char *out, unsigned long out_size);
+extern int compress_next(z_stream *stream, int flush);
+extern unsigned long compress_free(z_stream *stream);
+extern unsigned long compress_all(int level, unsigned char *data, unsigned long size,
+                                  unsigned char **out);
+
+#endif
-- 
1.5.4.rc4.39.g524a

             reply	other threads:[~2008-02-02 11:37 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-02 11:35 Marco Costalba [this message]
2008-02-02 11:35 ` [PATCH 02/11] Use new compress helpers in git files Marco Costalba
2008-02-02 11:35   ` [PATCH 03/11] Use new compress helpers in fast-import Marco Costalba
2008-02-02 11:35     ` [PATCH 04/11] Use new compress helpers in http-push.c Marco Costalba
2008-02-02 11:35       ` [PATCH 05/11] Use new compress helpers in sha1_file.c Marco Costalba
2008-02-02 11:35         ` [PATCH 06/11] Better error handling in compress_all() Marco Costalba
2008-02-02 11:35           ` [PATCH 07/11] Introduce stream decompress helpers Marco Costalba
2008-02-02 11:35             ` [PATCH 08/11] Use new decompress_all() helper in git Marco Costalba
2008-02-02 11:35               ` [PATCH 09/11] Convert http-push.c and http-walker.c Marco Costalba
2008-02-02 11:35                 ` [PATCH 10/11] Convert builtin-pack/unpack Marco Costalba
2008-02-02 11:35                   ` [PATCH 11/11] Convert sha1_file.c to use decompress helpers Marco Costalba
2008-02-04  2:08                   ` [PATCH 10/11] Convert builtin-pack/unpack Junio C Hamano
2008-02-04  2:07               ` [PATCH 08/11] Use new decompress_all() helper in git Junio C Hamano
2008-02-04  2:07             ` [PATCH 07/11] Introduce stream decompress helpers Junio C Hamano
2008-02-03 22:54           ` [PATCH 06/11] Better error handling in compress_all() Junio C Hamano
2008-02-03 22:53       ` [PATCH 04/11] Use new compress helpers in http-push.c Junio C Hamano
2008-02-03 22:53     ` [PATCH 03/11] Use new compress helpers in fast-import Junio C Hamano
2008-02-04  1:48       ` Shawn O. Pearce
2008-02-04  1:41     ` Shawn O. Pearce
2008-02-03 22:54   ` [PATCH 02/11] Use new compress helpers in git files Junio C Hamano
2008-02-03 22:53 ` [PATCH 01/11] Introduce stream compress helpers Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1201952156-6764-1-git-send-email-mcostalba@gmail.com \
    --to=mcostalba@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).