{"thread":{"id":"11575","subject":"[PATCH 1/5] Introduce stream compress helpers (v2 and complete)","startedAt":"2008-01-11T07:35:45Z","lastAt":"2008-01-11T07:35:45Z","messageCount":1,"participants":["Marco Costalba"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"64988","messageId":"e5bfff550801102335j2681b9c6p8eca731c7df2bc67@mail.gmail.com","threadId":"11575","inReplyTo":null,"subject":"[PATCH 1/5] Introduce stream compress helpers (v2 and complete)","fromName":"Marco Costalba","fromEmail":"mcostalba@gmail.com","sentAt":"2008-01-11T07:35:45Z","receivedAt":"2008-01-11T07:35:45Z","isPatch":true,"sender":{"key":"mcostalba@gmail.com","avatar":null},"body":"When decompressing a zlib stream use this\nhelpers instead of calling low level zlib\nfunction.\n\nThis patch introduces the necessary framework,\nstill no code change.\n\nThis is the first step in generalizing compress and\ndecompress functions avoiding zlib directly calls.\n\nSigned-off-by: Marco Costalba <mcostalba@gmail.com>\n---\n\nThis is the full compress cleanup series, no more open coded\nzlib compress calls are around anymore\n\nNew in this version is:\n\n- Renaming according to Linus suggestions\n- Added of a compress_free() that seems to suit nicely\n- git is zlib compress functions free now\n\n\nNext step will be decompress helpers\n\n\n Makefile   |    4 ++--\n compress.c |   58 ++++++++++++++++++++++++++++++++++++\n compress.h |   12 ++++++++++++\n 3 files changed, 72 insertions(+), 2 deletions(-)\n create mode 100644 compress.c\n create mode 100644 compress.h\n\ndiff --git a/Makefile b/Makefile\nindex 21c80e6..89bd99d 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -288,7 +288,7 @@ LIB_FILE=libgit.a\n XDIFF_LIB=xdiff/lib.a\n\n LIB_H = \\\n-\tarchive.h blob.h cache.h cache-tree.h commit.h csum-file.h delta.h grep.h \\\n+\tarchive.h blob.h cache.h cache-tree.h commit.h compress.h\ncsum-file.h delta.h grep.h \\\n \tdiff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \\\n \trun-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \\\n \ttree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \\\n@@ -301,7 +301,7 @@ DIFF_OBJS = \\\n \tdiffcore-delta.o log-tree.o\n\n LIB_OBJS = \\\n-\tblob.o commit.o connect.o csum-file.o cache-tree.o base85.o \\\n+\tblob.o commit.o compress.o connect.o csum-file.o cache-tree.o base85.o \\\n \tdate.o diff-delta.o entry.o exec_cmd.o ident.o \\\n \tpretty.o interpolate.o hash.o \\\n \tlockfile.o \\\ndiff --git a/compress.c b/compress.c\nnew file mode 100644\nindex 0000000..be771a9\n--- /dev/null\n+++ b/compress.c\n@@ -0,0 +1,58 @@\n+#include \"cache.h\"\n+#include \"compress.h\"\n+\n+unsigned long compress_alloc(z_stream *stream, int level, unsigned long size)\n+{\n+\tmemset(stream, 0, sizeof(*stream));\n+\tdeflateInit(stream, level);\n+\treturn deflateBound(stream, size);\n+}\n+\n+int compress_start(z_stream *stream,\n+                   unsigned char *in, unsigned long in_size,\n+                   unsigned char *out, unsigned long out_size)\n+{\n+\tstream->next_out = (out ? out : xmalloc(out_size));\n+\tstream->avail_out = out_size;\n+\tstream->next_in = in;\n+\tstream->avail_in = in_size;\n+\treturn Z_OK;\n+}\n+\n+int compress_next(z_stream *stream, int flush)\n+{\n+\tint result;\n+\n+\tdo {\n+\t\tresult = deflate(stream, flush);\n+\t} while (result == Z_OK);\n+\n+\treturn result;\n+}\n+\n+unsigned long compress_free(z_stream *stream)\n+{\n+\tdeflateEnd(stream);\n+\treturn stream->total_out;\n+}\n+\n+unsigned long compress_all(int level, unsigned char *data,\n+                           unsigned long size, unsigned char **out)\n+{\n+\tint bound, result;\n+\tz_stream stream;\n+\n+\tbound = compress_alloc(&stream, level, size);\n+\tcompress_start(&stream, data, size, NULL, bound);\n+\n+\t*out = stream.next_out;\n+\tresult = compress_next(&stream, Z_FINISH);\n+\n+\tif (result != Z_STREAM_END) {\n+\t\tcompress_free(&stream);\n+\t\tfree(*out);\n+\t\t*out = NULL;\n+\t\treturn 0;\n+\t}\n+\treturn compress_free(&stream);\n+}\ndiff --git a/compress.h b/compress.h\nnew file mode 100644\nindex 0000000..16b0147\n--- /dev/null\n+++ b/compress.h\n@@ -0,0 +1,12 @@\n+#ifndef COMPRESS_H\n+#define COMPRESS_H\n+\n+extern unsigned long compress_alloc(z_stream *stream, int level,\nunsigned long size);\n+extern int compress_start(z_stream *stream, unsigned char *in,\nunsigned long in_size,\n+                           unsigned char *out, unsigned long out_size);\n+extern int compress_next(z_stream *stream, int flush);\n+extern unsigned long compress_free(z_stream *stream);\n+extern unsigned long compress_all(int level, unsigned char *data,\nunsigned long size,\n+                                  unsigned char **out);\n+\n+#endif\n-- \n1.5.4.rc2.89.g1b3f-dirty\n"}]}