{"thread":{"id":"11591","subject":"[PATCH 6/6] Convert sha1_file.c to use decompress helpers","startedAt":"2008-01-11T19:01:58Z","lastAt":"2008-01-11T19:01:58Z","messageCount":1,"participants":["Marco Costalba"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"65051","messageId":"e5bfff550801111101x7148644bqc0e8dc2ee1f63671@mail.gmail.com","threadId":"11591","inReplyTo":null,"subject":"[PATCH 6/6] Convert sha1_file.c to use decompress helpers","fromName":"Marco Costalba","fromEmail":"mcostalba@gmail.com","sentAt":"2008-01-11T19:01:58Z","receivedAt":"2008-01-11T19:01:58Z","isPatch":true,"sender":{"key":"mcostalba@gmail.com","avatar":null},"body":"This is \"The King\".\n\nIt is the most difficult file to convert and some\ndecompression functions have been created just for it.\n\nAnyhow the lines of code removed (45) far surpass the\nones added (26).\n\nSigned-off-by: Marco Costalba <mcostalba@gmail.com>\n---\n\nThis is the last patch all git has been converted to\ncompress/decompress helpers.\n\n\nNo more open coded zlib calls in git now.\n\n\n sha1_file.c |   71 +++++++++++++++++++++------------------\n 1 files changed, 26 insertions(+), 45 deletions(-)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 6c94bd5..708727a 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1063,16 +1063,11 @@ static int unpack_sha1_header(z_stream *stream,\n \tenum object_type type;\n\n \t/* Get the data stream */\n-\tmemset(stream, 0, sizeof(*stream));\n-\tstream->next_in = map;\n-\tstream->avail_in = mapsize;\n-\tstream->next_out = buffer;\n-\tstream->avail_out = bufsiz;\n+\tdecompress_alloc(stream);\n+\tdecompress_into(stream, buffer, bufsiz);\n\n-\tif (legacy_loose_object(map)) {\n-\t\tinflateInit(stream);\n-\t\treturn inflate(stream, 0);\n-\t}\n+\tif (legacy_loose_object(map))\n+\t\treturn decompress_next_from(stream, map, mapsize, Z_NO_FLUSH);\n\n\n \t/*\n@@ -1089,9 +1084,7 @@ static int unpack_sha1_header(z_stream *stream,\n \tmapsize -= used;\n\n \t/* Set up the stream for the rest.. */\n-\tstream->next_in = map;\n-\tstream->avail_in = mapsize;\n-\tinflateInit(stream);\n+\tdecompress_from(stream, map, mapsize);\n\n \t/* And generate the fake traditional header */\n \tstream->total_out = 1 + snprintf(buffer, bufsiz, \"%s %lu\",\n@@ -1125,14 +1118,13 @@ static void *unpack_sha1_rest(z_stream *stream,\n \t\t * we also want to check that zlib tells us that all\n \t\t * went well with status == Z_STREAM_END at the end.\n \t\t */\n-\t\tstream->next_out = buf + bytes;\n-\t\tstream->avail_out = size - bytes;\n+\t\tdecompress_into(stream, buf + bytes, size - bytes);\n \t\twhile (status == Z_OK)\n-\t\t\tstatus = inflate(stream, Z_FINISH);\n+\t\t\tstatus = decompress_next(stream, Z_FINISH);\n \t}\n \tbuf[size] = 0;\n \tif (status == Z_STREAM_END && !stream->avail_in) {\n-\t\tinflateEnd(stream);\n+\t\tdecompress_free(stream);\n \t\treturn buf;\n \t}\n\n@@ -1217,20 +1209,18 @@ unsigned long get_size_from_delta(struct\n \tunsigned char delta_head[20], *in;\n \tz_stream stream;\n \tint st;\n+\tunsigned int in_size = 0;\n\n-\tmemset(&stream, 0, sizeof(stream));\n-\tstream.next_out = delta_head;\n-\tstream.avail_out = sizeof(delta_head);\n+\tdecompress_alloc(&stream);\n+\tdecompress_into(&stream, delta_head, sizeof(delta_head));\n\n-\tinflateInit(&stream);\n \tdo {\n-\t\tin = use_pack(p, w_curs, curpos, &stream.avail_in);\n-\t\tstream.next_in = in;\n-\t\tst = inflate(&stream, Z_FINISH);\n+\t\tin = use_pack(p, w_curs, curpos, &in_size);\n+\t\tst = decompress_next_from(&stream, in, in_size, Z_FINISH);\n \t\tcurpos += stream.next_in - in;\n \t} while ((st == Z_OK || st == Z_BUF_ERROR) &&\n \t\t stream.total_out < sizeof(delta_head));\n-\tinflateEnd(&stream);\n+\tdecompress_free(&stream);\n \tif ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head))\n \t\tdie(\"delta data unpack-initial failed\");\n\n@@ -1307,7 +1297,7 @@ static int packed_delta_info(struct packed_git *p,\n \t/* We choose to only get the type of the base object and\n \t * ignore potentially corrupt pack file that expects the delta\n \t * based on a base with a wrong size.  This saves tons of\n-\t * inflate() calls.\n+\t * decompress() calls.\n \t */\n \tif (sizep)\n \t\t*sizep = get_size_from_delta(p, w_curs, curpos);\n@@ -1428,21 +1418,18 @@ static void *unpack_compressed_entry(struct\n \tint st;\n \tz_stream stream;\n \tunsigned char *buffer, *in;\n+\tunsigned int in_size = 0;\n\n \tbuffer = xmalloc(size + 1);\n \tbuffer[size] = 0;\n-\tmemset(&stream, 0, sizeof(stream));\n-\tstream.next_out = buffer;\n-\tstream.avail_out = size;\n-\n-\tinflateInit(&stream);\n+\tdecompress_alloc(&stream);\n+\tdecompress_into(&stream, buffer, size);\n \tdo {\n-\t\tin = use_pack(p, w_curs, curpos, &stream.avail_in);\n-\t\tstream.next_in = in;\n-\t\tst = inflate(&stream, Z_FINISH);\n+\t\tin = use_pack(p, w_curs, curpos, &in_size);\n+\t\tst = decompress_next_from(&stream, in, in_size, Z_FINISH);\n \t\tcurpos += stream.next_in - in;\n \t} while (st == Z_OK || st == Z_BUF_ERROR);\n-\tinflateEnd(&stream);\n+\tdecompress_free(&stream);\n \tif ((st != Z_STREAM_END) || stream.total_out != size) {\n \t\tfree(buffer);\n \t\treturn NULL;\n@@ -1788,7 +1775,7 @@ static int sha1_loose_object_info(const unsigned\n \t\tstatus = error(\"unable to parse %s header\", sha1_to_hex(sha1));\n \telse if (sizep)\n \t\t*sizep = size;\n-\tinflateEnd(&stream);\n+\tdecompress_free(&stream);\n \tmunmap(map, mapsize);\n \treturn status;\n }\n@@ -2196,21 +2183,15 @@ int write_sha1_from_fd(const unsigned char\n*sha1, int fd,\n \t\t\treturn error(\"unable to create temporary sha1 filename %s: %s\\n\",\ntmpfile, strerror(errno));\n \t}\n\n-\tmemset(&stream, 0, sizeof(stream));\n-\n-\tinflateInit(&stream);\n-\n+\tdecompress_alloc(&stream);\n \tSHA1_Init(&c);\n\n \tdo {\n \t\tssize_t size;\n \t\tif (*bufposn) {\n-\t\t\tstream.avail_in = *bufposn;\n-\t\t\tstream.next_in = (unsigned char *) buffer;\n+\t\t\tdecompress_from(&stream, (unsigned char *) buffer, *bufposn);\n \t\t\tdo {\n-\t\t\t\tstream.next_out = discard;\n-\t\t\t\tstream.avail_out = sizeof(discard);\n-\t\t\t\tret = inflate(&stream, Z_SYNC_FLUSH);\n+\t\t\t\tret = decompress_next_into(&stream, discard, sizeof(discard),\nZ_SYNC_FLUSH);\n \t\t\t\tSHA1_Update(&c, discard, sizeof(discard) -\n \t\t\t\t\t    stream.avail_out);\n \t\t\t} while (stream.avail_in && ret == Z_OK);\n@@ -2233,7 +2214,7 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd,\n \t\t}\n \t\t*bufposn += size;\n \t} while (1);\n-\tinflateEnd(&stream);\n+\tdecompress_free(&stream);\n\n \tfchmod(local, 0444);\n \tif (close(local) != 0)\n-- \n1.5.4.rc2.90.gf158-dirty\n"}]}