{"thread":{"id":"11588","subject":"[PATCH 3/6] Use new decompress_all() helper in git","startedAt":"2008-01-11T18:56:17Z","lastAt":"2008-01-11T18:56:17Z","messageCount":1,"participants":["Marco Costalba"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"65046","messageId":"e5bfff550801111056v3b214442sf21e7eb6cc4da@mail.gmail.com","threadId":"11588","inReplyTo":null,"subject":"[PATCH 3/6] Use new decompress_all() helper in git","fromName":"Marco Costalba","fromEmail":"mcostalba@gmail.com","sentAt":"2008-01-11T18:56:17Z","receivedAt":"2008-01-11T18:56:17Z","isPatch":true,"sender":{"key":"mcostalba@gmail.com","avatar":null},"body":"Only in two places is possible to really simplify\ndeflate code with the all_in_one decompress_all()\n\nSigned-off-by: Marco Costalba <mcostalba@gmail.com>\n---\n builtin-apply.c |   23 +++++++++--------------\n index-pack.c    |   30 +++++++-----------------------\n 2 files changed, 16 insertions(+), 37 deletions(-)\n\ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex d57bb6e..6dd2b8c 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -8,6 +8,7 @@\n  */\n #include \"cache.h\"\n #include \"cache-tree.h\"\n+#include \"compress.h\"\n #include \"quote.h\"\n #include \"blob.h\"\n #include \"delta.h\"\n@@ -1105,23 +1106,17 @@ static inline int metadata_changes(struct\n static char *inflate_it(const void *data, unsigned long size,\n \t\t\tunsigned long inflated_size)\n {\n-\tz_stream stream;\n-\tvoid *out;\n-\tint st;\n+\tunsigned char *out;\n+\tunsigned long out_size;\n\n-\tmemset(&stream, 0, sizeof(stream));\n+\tout = xmalloc(inflated_size);\n+\tout_size = decompress_all((unsigned char *)data, size, out, inflated_size);\n\n-\tstream.next_in = (unsigned char *)data;\n-\tstream.avail_in = size;\n-\tstream.next_out = out = xmalloc(inflated_size);\n-\tstream.avail_out = inflated_size;\n-\tinflateInit(&stream);\n-\tst = inflate(&stream, Z_FINISH);\n-\tif ((st != Z_STREAM_END) || stream.total_out != inflated_size) {\n+\tif (out_size != inflated_size) {\n \t\tfree(out);\n \t\treturn NULL;\n \t}\n-\treturn out;\n+\treturn (char *)out;\n }\n\n static struct fragment *parse_binary_hunk(char **buf_p,\ndiff --git a/index-pack.c b/index-pack.c\nindex 880088e..30d7837 100644\n--- a/index-pack.c\n+++ b/index-pack.c\n@@ -169,24 +169,18 @@ static void *unpack_entry_data(unsigned long offset,\n \tz_stream stream;\n \tvoid *buf = xmalloc(size);\n\n-\tmemset(&stream, 0, sizeof(stream));\n-\tstream.next_out = buf;\n-\tstream.avail_out = size;\n-\tstream.next_in = fill(1);\n-\tstream.avail_in = input_len;\n-\tinflateInit(&stream);\n+\tdecompress_alloc(&stream);\n+\tdecompress_into(&stream, buf, size);\n\n \tfor (;;) {\n-\t\tint ret = inflate(&stream, 0);\n+\t\tint ret = decompress_next_from(&stream, fill(1), input_len, Z_NO_FLUSH);\n \t\tuse(input_len - stream.avail_in);\n \t\tif (stream.total_out == size && ret == Z_STREAM_END)\n \t\t\tbreak;\n \t\tif (ret != Z_OK)\n-\t\t\tbad_object(offset, \"inflate returned %d\", ret);\n-\t\tstream.next_in = fill(1);\n-\t\tstream.avail_in = input_len;\n+\t\t\tbad_object(offset, \"decompress returned %d\", ret);\n \t}\n-\tinflateEnd(&stream);\n+\tdecompress_free(&stream);\n \treturn buf;\n }\n\n@@ -261,8 +255,6 @@ static void *get_data_from_pack(struct object_entry\n \tunsigned long len = obj[1].idx.offset - from;\n \tunsigned long rdy = 0;\n \tunsigned char *src, *data;\n-\tz_stream stream;\n-\tint st;\n\n \tsrc = xmalloc(len);\n \tdata = src;\n@@ -273,16 +265,8 @@ static void *get_data_from_pack(struct object_entry\n \t\trdy += n;\n \t} while (rdy < len);\n \tdata = xmalloc(obj->size);\n-\tmemset(&stream, 0, sizeof(stream));\n-\tstream.next_out = data;\n-\tstream.avail_out = obj->size;\n-\tstream.next_in = src;\n-\tstream.avail_in = len;\n-\tinflateInit(&stream);\n-\twhile ((st = inflate(&stream, Z_FINISH)) == Z_OK);\n-\tinflateEnd(&stream);\n-\tif (st != Z_STREAM_END || stream.total_out != obj->size)\n-\t\tdie(\"serious inflate inconsistency\");\n+\tif (decompress_all(src, len, data, obj->size) != obj->size)\n+\t\tdie(\"serious decompress inconsistency\");\n \tfree(src);\n \treturn data;\n }\n-- \n1.5.4.rc2.90.gf158-dirty\n"}]}