Skip to content

Commit 55bb5c9

Browse files
committed
zlib: wrap deflate side of the API
Wrap deflateInit, deflate, and deflateEnd for everybody, and the sole use of deflateInit2 in remote-curl.c to tell the library to use gzip header and trailer in git_deflate_init_gzip(). There is only one caller that cares about the status from deflateEnd(). Introduce git_deflate_end_gently() to let that sole caller retrieve the status and act on it (i.e. die) for now, but we would probably want to make inflate_end/deflate_end die when they ran out of memory and get rid of the _gently() kind. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e86c1f commit 55bb5c9

File tree

10 files changed

+105
-41
lines changed

10 files changed

+105
-41
lines changed

archive-zip.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static void *zlib_deflate(void *data, unsigned long size,
9696
int result;
9797

9898
memset(&stream, 0, sizeof(stream));
99-
deflateInit(&stream, compression_level);
99+
git_deflate_init(&stream, compression_level);
100100
maxsize = deflateBound(&stream, size);
101101
buffer = xmalloc(maxsize);
102102

@@ -106,15 +106,15 @@ static void *zlib_deflate(void *data, unsigned long size,
106106
stream.avail_out = maxsize;
107107

108108
do {
109-
result = deflate(&stream, Z_FINISH);
109+
result = git_deflate(&stream, Z_FINISH);
110110
} while (result == Z_OK);
111111

112112
if (result != Z_STREAM_END) {
113113
free(buffer);
114114
return NULL;
115115
}
116116

117-
deflateEnd(&stream);
117+
git_deflate_end(&stream);
118118
*compressed_size = stream.total_out;
119119

120120
return buffer;

builtin/index-pack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,21 +671,21 @@ static int write_compressed(struct sha1file *f, void *in, unsigned int size)
671671
unsigned char outbuf[4096];
672672

673673
memset(&stream, 0, sizeof(stream));
674-
deflateInit(&stream, zlib_compression_level);
674+
git_deflate_init(&stream, zlib_compression_level);
675675
stream.next_in = in;
676676
stream.avail_in = size;
677677

678678
do {
679679
stream.next_out = outbuf;
680680
stream.avail_out = sizeof(outbuf);
681-
status = deflate(&stream, Z_FINISH);
681+
status = git_deflate(&stream, Z_FINISH);
682682
sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out);
683683
} while (status == Z_OK);
684684

685685
if (status != Z_STREAM_END)
686686
die("unable to deflate appended object (%d)", status);
687687
size = stream.total_out;
688-
deflateEnd(&stream);
688+
git_deflate_end(&stream);
689689
return size;
690690
}
691691

builtin/pack-objects.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static unsigned long do_compress(void **pptr, unsigned long size)
131131
unsigned long maxsize;
132132

133133
memset(&stream, 0, sizeof(stream));
134-
deflateInit(&stream, pack_compression_level);
134+
git_deflate_init(&stream, pack_compression_level);
135135
maxsize = deflateBound(&stream, size);
136136

137137
in = *pptr;
@@ -142,9 +142,9 @@ static unsigned long do_compress(void **pptr, unsigned long size)
142142
stream.avail_in = size;
143143
stream.next_out = out;
144144
stream.avail_out = maxsize;
145-
while (deflate(&stream, Z_FINISH) == Z_OK)
145+
while (git_deflate(&stream, Z_FINISH) == Z_OK)
146146
; /* nothing */
147-
deflateEnd(&stream);
147+
git_deflate_end(&stream);
148148

149149
free(in);
150150
return stream.total_out;

cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ void git_inflate_init_gzip_only(z_streamp strm);
2525
void git_inflate_end(z_streamp strm);
2626
int git_inflate(z_streamp strm, int flush);
2727

28+
void git_deflate_init(z_streamp strm, int level);
29+
void git_deflate_init_gzip(z_streamp strm, int level);
30+
void git_deflate_end(z_streamp strm);
31+
int git_deflate_end_gently(z_streamp strm);
32+
int git_deflate(z_streamp strm, int flush);
33+
2834
#if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
2935
#define DTYPE(de) ((de)->d_type)
3036
#else

diff.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,17 +1732,17 @@ static unsigned char *deflate_it(char *data,
17321732
z_stream stream;
17331733

17341734
memset(&stream, 0, sizeof(stream));
1735-
deflateInit(&stream, zlib_compression_level);
1735+
git_deflate_init(&stream, zlib_compression_level);
17361736
bound = deflateBound(&stream, size);
17371737
deflated = xmalloc(bound);
17381738
stream.next_out = deflated;
17391739
stream.avail_out = bound;
17401740

17411741
stream.next_in = (unsigned char *)data;
17421742
stream.avail_in = size;
1743-
while (deflate(&stream, Z_FINISH) == Z_OK)
1743+
while (git_deflate(&stream, Z_FINISH) == Z_OK)
17441744
; /* nothing */
1745-
deflateEnd(&stream);
1745+
git_deflate_end(&stream);
17461746
*result_size = stream.total_out;
17471747
return deflated;
17481748
}

fast-import.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ static int store_object(
10501050
delta = NULL;
10511051

10521052
memset(&s, 0, sizeof(s));
1053-
deflateInit(&s, pack_compression_level);
1053+
git_deflate_init(&s, pack_compression_level);
10541054
if (delta) {
10551055
s.next_in = delta;
10561056
s.avail_in = deltalen;
@@ -1060,9 +1060,9 @@ static int store_object(
10601060
}
10611061
s.avail_out = deflateBound(&s, s.avail_in);
10621062
s.next_out = out = xmalloc(s.avail_out);
1063-
while (deflate(&s, Z_FINISH) == Z_OK)
1064-
/* nothing */;
1065-
deflateEnd(&s);
1063+
while (git_deflate(&s, Z_FINISH) == Z_OK)
1064+
; /* nothing */
1065+
git_deflate_end(&s);
10661066

10671067
/* Determine if we should auto-checkpoint. */
10681068
if ((max_packsize && (pack_size + 60 + s.total_out) > max_packsize)
@@ -1078,14 +1078,14 @@ static int store_object(
10781078
delta = NULL;
10791079

10801080
memset(&s, 0, sizeof(s));
1081-
deflateInit(&s, pack_compression_level);
1081+
git_deflate_init(&s, pack_compression_level);
10821082
s.next_in = (void *)dat->buf;
10831083
s.avail_in = dat->len;
10841084
s.avail_out = deflateBound(&s, s.avail_in);
10851085
s.next_out = out = xrealloc(out, s.avail_out);
1086-
while (deflate(&s, Z_FINISH) == Z_OK)
1087-
/* nothing */;
1088-
deflateEnd(&s);
1086+
while (git_deflate(&s, Z_FINISH) == Z_OK)
1087+
; /* nothing */
1088+
git_deflate_end(&s);
10891089
}
10901090
}
10911091

@@ -1187,7 +1187,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
11871187
crc32_begin(pack_file);
11881188

11891189
memset(&s, 0, sizeof(s));
1190-
deflateInit(&s, pack_compression_level);
1190+
git_deflate_init(&s, pack_compression_level);
11911191

11921192
hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
11931193
if (out_sz <= hdrlen)
@@ -1209,7 +1209,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
12091209
len -= n;
12101210
}
12111211

1212-
status = deflate(&s, len ? 0 : Z_FINISH);
1212+
status = git_deflate(&s, len ? 0 : Z_FINISH);
12131213

12141214
if (!s.avail_out || status == Z_STREAM_END) {
12151215
size_t n = s.next_out - out_buf;
@@ -1228,7 +1228,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
12281228
die("unexpected deflate failure: %d", status);
12291229
}
12301230
}
1231-
deflateEnd(&s);
1231+
git_deflate_end(&s);
12321232
git_SHA1_Final(sha1, &c);
12331233

12341234
if (sha1out)

http-push.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ static void start_put(struct transfer_request *request)
359359

360360
/* Set it up */
361361
memset(&stream, 0, sizeof(stream));
362-
deflateInit(&stream, zlib_compression_level);
362+
git_deflate_init(&stream, zlib_compression_level);
363363
size = deflateBound(&stream, len + hdrlen);
364364
strbuf_init(&request->buffer.buf, size);
365365
request->buffer.posn = 0;
@@ -371,15 +371,15 @@ static void start_put(struct transfer_request *request)
371371
/* First header.. */
372372
stream.next_in = (void *)hdr;
373373
stream.avail_in = hdrlen;
374-
while (deflate(&stream, 0) == Z_OK)
375-
/* nothing */;
374+
while (git_deflate(&stream, 0) == Z_OK)
375+
; /* nothing */
376376

377377
/* Then the data itself.. */
378378
stream.next_in = unpacked;
379379
stream.avail_in = len;
380-
while (deflate(&stream, Z_FINISH) == Z_OK)
381-
/* nothing */;
382-
deflateEnd(&stream);
380+
while (git_deflate(&stream, Z_FINISH) == Z_OK)
381+
; /* nothing */
382+
git_deflate_end(&stream);
383383
free(unpacked);
384384

385385
request->buffer.buf.len = stream.total_out;

remote-curl.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,7 @@ static int post_rpc(struct rpc_state *rpc)
475475
int ret;
476476

477477
memset(&stream, 0, sizeof(stream));
478-
ret = deflateInit2(&stream, Z_BEST_COMPRESSION,
479-
Z_DEFLATED, (15 + 16),
480-
8, Z_DEFAULT_STRATEGY);
481-
if (ret != Z_OK)
482-
die("cannot deflate request; zlib init error %d", ret);
478+
git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
483479
size = deflateBound(&stream, rpc->len);
484480
gzip_body = xmalloc(size);
485481

@@ -488,11 +484,11 @@ static int post_rpc(struct rpc_state *rpc)
488484
stream.next_out = (unsigned char *)gzip_body;
489485
stream.avail_out = size;
490486

491-
ret = deflate(&stream, Z_FINISH);
487+
ret = git_deflate(&stream, Z_FINISH);
492488
if (ret != Z_STREAM_END)
493489
die("cannot deflate request; zlib deflate error %d", ret);
494490

495-
ret = deflateEnd(&stream);
491+
ret = git_deflate_end_gently(&stream);
496492
if (ret != Z_OK)
497493
die("cannot deflate request; zlib end error %d", ret);
498494

sha1_file.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,24 +2442,24 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
24422442

24432443
/* Set it up */
24442444
memset(&stream, 0, sizeof(stream));
2445-
deflateInit(&stream, zlib_compression_level);
2445+
git_deflate_init(&stream, zlib_compression_level);
24462446
stream.next_out = compressed;
24472447
stream.avail_out = sizeof(compressed);
24482448
git_SHA1_Init(&c);
24492449

24502450
/* First header.. */
24512451
stream.next_in = (unsigned char *)hdr;
24522452
stream.avail_in = hdrlen;
2453-
while (deflate(&stream, 0) == Z_OK)
2454-
/* nothing */;
2453+
while (git_deflate(&stream, 0) == Z_OK)
2454+
; /* nothing */
24552455
git_SHA1_Update(&c, hdr, hdrlen);
24562456

24572457
/* Then the data itself.. */
24582458
stream.next_in = (void *)buf;
24592459
stream.avail_in = len;
24602460
do {
24612461
unsigned char *in0 = stream.next_in;
2462-
ret = deflate(&stream, Z_FINISH);
2462+
ret = git_deflate(&stream, Z_FINISH);
24632463
git_SHA1_Update(&c, in0, stream.next_in - in0);
24642464
if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
24652465
die("unable to write sha1 file");
@@ -2469,7 +2469,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
24692469

24702470
if (ret != Z_STREAM_END)
24712471
die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret);
2472-
ret = deflateEnd(&stream);
2472+
ret = git_deflate_end_gently(&stream);
24732473
if (ret != Z_OK)
24742474
die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret);
24752475
git_SHA1_Final(parano_sha1, &c);

zlib.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,65 @@ int git_inflate(z_streamp strm, int flush)
7777
strm->msg ? strm->msg : "no message");
7878
return status;
7979
}
80+
81+
void git_deflate_init(z_streamp strm, int level)
82+
{
83+
int status = deflateInit(strm, level);
84+
85+
if (status == Z_OK)
86+
return;
87+
die("deflateInit: %s (%s)", zerr_to_string(status),
88+
strm->msg ? strm->msg : "no message");
89+
}
90+
91+
void git_deflate_init_gzip(z_streamp strm, int level)
92+
{
93+
/*
94+
* Use default 15 bits, +16 is to generate gzip header/trailer
95+
* instead of the zlib wrapper.
96+
*/
97+
const int windowBits = 15 + 16;
98+
int status = deflateInit2(strm, level,
99+
Z_DEFLATED, windowBits,
100+
8, Z_DEFAULT_STRATEGY);
101+
if (status == Z_OK)
102+
return;
103+
die("deflateInit2: %s (%s)", zerr_to_string(status),
104+
strm->msg ? strm->msg : "no message");
105+
}
106+
107+
void git_deflate_end(z_streamp strm)
108+
{
109+
int status = deflateEnd(strm);
110+
111+
if (status == Z_OK)
112+
return;
113+
error("deflateEnd: %s (%s)", zerr_to_string(status),
114+
strm->msg ? strm->msg : "no message");
115+
}
116+
117+
int git_deflate_end_gently(z_streamp strm)
118+
{
119+
return deflateEnd(strm);
120+
}
121+
122+
int git_deflate(z_streamp strm, int flush)
123+
{
124+
int status = deflate(strm, flush);
125+
126+
switch (status) {
127+
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
128+
case Z_BUF_ERROR:
129+
case Z_OK:
130+
case Z_STREAM_END:
131+
return status;
132+
133+
case Z_MEM_ERROR:
134+
die("deflate: out of memory");
135+
default:
136+
break;
137+
}
138+
error("deflate: %s (%s)", zerr_to_string(status),
139+
strm->msg ? strm->msg : "no message");
140+
return status;
141+
}

0 commit comments

Comments
 (0)