threads / patch / 11567

patch, 5 partsAdd zlib decompress helper functions

Subject: [PATCH 1/5] Add zlib decompress helper functions

## tl;dr

3 messages between Jan 10, 2008 and Jan 10, 2008. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Marco Costalba· Jan 10, 2008, 21:04 UTC · lore

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 |   52 ++++++++++++++++++++++++++
 compress.h |   14 ++++++++++++++
 3 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 compress.c
 create mode 100644 compress.h
Show changes to 3 files +68 −2

Makefile, compress.c, compress.h

diff --git a/Makefile b/Makefile
index 21c80e6..89bd99d 100644
--- a/Makefile
+++ b/Makefile
@@ -288,7 +288,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 \
@@ -301,7 +301,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..f40e09c
--- /dev/null
+++ b/compress.c
@@ -0,0 +1,52 @@
+#include "cache.h"
+#include "compress.h"
+
+unsigned long z_deflate_init(z_stream *stream, int level, unsigned long size)
+{
+	memset(stream, 0, sizeof(*stream));
+	deflateInit(stream, level);
+	return deflateBound(stream, size);
+}
+
+int z_deflate_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 z_deflate_next(z_stream *stream, int flush)
+{
+	int result;
+
+	do {
+		result = deflate(stream, flush);
+	} while (result == Z_OK);
+
+	return result;
+}
+
+unsigned long z_deflate_all(int level, unsigned char *data,
+                            unsigned long size, unsigned char **out)
+{
+	int bound, result;
+	z_stream stream;
+
+	bound = z_deflate_init(&stream, level, size);
+	z_deflate_start(&stream, data, size, NULL, bound);
+
+	*out = stream.next_out;
+	result = z_deflate_next(&stream, Z_FINISH);
+
+	if (result != Z_STREAM_END) {
+		deflateEnd(&stream);
+		free(*out);
+		*out = NULL;
+		return 0;
+	}
+	deflateEnd(&stream);
+	return stream.total_out;
+}
diff --git a/compress.h b/compress.h
new file mode 100644
index 0000000..926450c
--- /dev/null
+++ b/compress.h
@@ -0,0 +1,14 @@
+#ifndef COMPRESS_H
+#define COMPRESS_H
+
+extern unsigned long z_deflate_init(z_stream *stream, int level,
unsigned long size);
+
+extern int z_deflate_start(z_stream *stream, unsigned char *in,
unsigned long in_size,
+                           unsigned char *out, unsigned long out_size);
+
+extern int z_deflate_next(z_stream *stream, int flush);
+
+extern unsigned long z_deflate_all(int level, unsigned char *data,
+                                   unsigned long size, unsigned char **out);
+
+#endif
-- 
1.5.4.rc2.89.g1b3f
Linus Torvalds· Jan 10, 2008, 21:57 UTC · re: Marco Costalba · lore

Re: [PATCH 1/5] Add zlib decompress helper functions

On Thu, 10 Jan 2008, Marco Costalba wrote:
>
> When decompressing a zlib stream use this
> helpers instead of calling low level zlib
> function.
I really *really* hate your naming.
> This is the first step in generalizing compress and
> decompress functions avoiding zlib directly calls.

If that's the goal, why keep the horrible "z_" prefix, and why the opaque and non-obvious "inflate"/"deflate" names?

I'd suggest that you just replace all "z_deflate_" with "compress_" and "z_inflate_" with "decompress_".

Yes, it would still leave zlib-specific stuff in there (the return codes, the "z_stream" type thing etc), but at least it would be a _step_ towards more readable code and code that is less obviously zlib-specific.

With those changes, I'd heartily recommend merging this even if we never actually switch away from zlib, if only because zlib has all these horrible names.

		Linus
[ How many people really know that "inflate" means "uncompress", without 
  having to think about it a bit?
  I guarantee that any computer person immediately knows the difference 
  between "compress" and "decompress" without even thinking, but ask 
  somebody what "inflate" vs "deflate" does, and they'll be able to answer 
  you, but they'll first have to think about an air mattress or something.
  Yeah, yeah, old-time zip users probably think the whole xxflate thing 
  makes sense, and I'm just grouchy because _I_ always have to think 
  about it. ]
Marco Costalba· Jan 10, 2008, 22:04 UTC · re: Linus Torvalds · lore

Re: [PATCH 1/5] Add zlib decompress helper functions

On Jan 10, 2008 10:57 PM, Linus Torvalds <torvalds@linux-foundation.org> wrote:
Show 10 quoted lines
>
>
> On Thu, 10 Jan 2008, Marco Costalba wrote:
> >
> > When decompressing a zlib stream use this
> > helpers instead of calling low level zlib
> > function.
>
> I really *really* hate your naming.
>
I agree 100% it was chosen only to keep zlib conventions.
>
> [ How many people really know that "inflate" means "uncompress", without
>   having to think about it a bit?
>

I misnamed the whole patch series but the last one due to deflate being tot intuitive !!!

Ok I wrote the e-mails very quickly and cut and paste was heavily involved, but when I realized that I called all the series 'decompress helpers' I went blush!

Marco

← back to recent threads