git-zlib: use `struct z_stream_s` instead of typedef

Throughout the Git codebase we're using the typedeffed version of `z_stream`, which maps to `struct z_stream_s`. By using a typedef instead of the struct it becomes somewhat harder to predeclare the symbol so that headers depending on the struct can do so without having to pull in "zlib-compat.h". We don't yet have users that would really care about this: the only users that declare `z_stream` as a pointer are in "reftable/block.h", which is a header that is internal to the reftable library. But in the next step we're going to expose the `struct reftable_block` publicly, and that struct does contain a pointer to `z_stream`. And as the public header shouldn't depend on "reftable/system.h", which is an internal implementation detail, we won't have the typedef for `z_stream` readily available. Prepare for this change by using `struct z_stream_s` throughout our code base. In case zlib-ng is used we use a define to map from `z_stream_s` to `zng_stream_s`. Drop the pre-declaration of `struct z_stream` while at it. This struct does not exist in the first place, and the declaration wasn't needed because "reftable/block.h" already includes "reftable/basics.h" which transitively includes "reftable/system.h" and thus "git-zlib.h". Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 7, 2025 at 15:16 UTC ce76cec964ed1c8ad6c9fcee9fd833c0ec8cccf9
4 files changed +7 -9
Documentation/howto/recover-corrupted-object-harder.adoc
+2 -2
@@ -125,7 +125,7 @@ static int try_zlib(unsigned char *buf, int len)
125 {
126 /* make this absurdly large so we don't have to loop */
127 static unsigned char out[1024*1024];
128 - z_stream z;
128 + struct z_stream_s z;
129 int ret;
130
131 memset(&z, 0, sizeof(z));
@@ -278,7 +278,7 @@ int main(int argc, char **argv)
278 static unsigned char buf[25 * 1024 * 1024];
279 static unsigned char out[25 * 1024 * 1024];
280 int len;
281 - z_stream z;
281 + struct z_stream_s z;
282 int ret;
283
284 len = read(0, buf, sizeof(buf));
compat/zlib-compat.h
+2 -2
@@ -4,8 +4,8 @@
4 #ifdef HAVE_ZLIB_NG
5 # include <zlib-ng.h>
6
7 -# define z_stream zng_stream
8 -#define gz_header_s zng_gz_header_s
7 +# define z_stream_s zng_stream_s
8 +# define gz_header_s zng_gz_header_s
9
10 # define crc32(crc, buf, len) zng_crc32(crc, buf, len)
11
git-zlib.h
+1 -1
@@ -4,7 +4,7 @@
4 #include "compat/zlib-compat.h"
5
6 typedef struct git_zstream {
7 - z_stream z;
7 + struct z_stream_s z;
8 unsigned long avail_in;
9 unsigned long avail_out;
10 unsigned long total_in;
reftable/block.h
+2 -4
@@ -18,7 +18,7 @@
18 * allocation overhead.
19 */
20 struct block_writer {
21 - z_stream *zstream;
21 + struct z_stream_s *zstream;
22 unsigned char *compressed;
23 size_t compressed_cap;
24
@@ -62,8 +62,6 @@ int block_writer_finish(struct block_writer *w);
62 /* clears out internally allocated block_writer members. */
63 void block_writer_release(struct block_writer *bw);
64
65 -struct z_stream;
66 -
65 /*
66 * A block part of a reftable. Contains records as well as some metadata
67 * describing them.
@@ -78,7 +76,7 @@ struct reftable_block {
76 uint32_t hash_size;
77
78 /* Uncompressed data for log entries. */
81 - z_stream *zstream;
79 + struct z_stream_s *zstream;
80 unsigned char *uncompressed_data;
81 size_t uncompressed_cap;
82