]> git.ipfire.org Git - thirdparty/git.git/blame - zlib.c
zlib: wrap deflateBound() too
[thirdparty/git.git] / zlib.c
CommitLineData
b0613ce0
JN
1/*
2 * zlib wrappers to make sure we don't silently miss errors
3 * at init time.
4 */
5#include "cache.h"
6
1a507fc1 7static const char *zerr_to_string(int status)
b0613ce0 8{
1a507fc1 9 switch (status) {
b0613ce0 10 case Z_MEM_ERROR:
1a507fc1 11 return "out of memory";
b0613ce0 12 case Z_VERSION_ERROR:
1a507fc1
JH
13 return "wrong version";
14 case Z_NEED_DICT:
15 return "needs dictionary";
16 case Z_DATA_ERROR:
17 return "data stream error";
18 case Z_STREAM_ERROR:
19 return "stream consistency error";
b0613ce0 20 default:
1a507fc1 21 return "unknown error";
b0613ce0 22 }
b0613ce0
JN
23}
24
1a507fc1 25void git_inflate_init(z_streamp strm)
b0613ce0 26{
1a507fc1
JH
27 int status = inflateInit(strm);
28
29 if (status == Z_OK)
30 return;
31 die("inflateInit: %s (%s)", zerr_to_string(status),
32 strm->msg ? strm->msg : "no message");
b0613ce0
JN
33}
34
5e86c1fb
JH
35void git_inflate_init_gzip_only(z_streamp strm)
36{
37 /*
38 * Use default 15 bits, +16 is to accept only gzip and to
39 * yield Z_DATA_ERROR when fed zlib format.
40 */
41 const int windowBits = 15 + 16;
42 int status = inflateInit2(strm, windowBits);
43
44 if (status == Z_OK)
45 return;
46 die("inflateInit2: %s (%s)", zerr_to_string(status),
47 strm->msg ? strm->msg : "no message");
48}
49
1a507fc1 50void git_inflate_end(z_streamp strm)
b0613ce0 51{
1a507fc1 52 int status = inflateEnd(strm);
b0613ce0 53
1a507fc1
JH
54 if (status == Z_OK)
55 return;
56 error("inflateEnd: %s (%s)", zerr_to_string(status),
57 strm->msg ? strm->msg : "no message");
58}
b0613ce0 59
1a507fc1
JH
60int git_inflate(z_streamp strm, int flush)
61{
62 int status = inflate(strm, flush);
b0613ce0 63
1a507fc1 64 switch (status) {
b0613ce0
JN
65 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
66 case Z_BUF_ERROR:
67 case Z_OK:
68 case Z_STREAM_END:
1a507fc1
JH
69 return status;
70
71 case Z_MEM_ERROR:
72 die("inflate: out of memory");
73 default:
74 break;
b0613ce0 75 }
1a507fc1
JH
76 error("inflate: %s (%s)", zerr_to_string(status),
77 strm->msg ? strm->msg : "no message");
78 return status;
b0613ce0 79}
55bb5c91 80
225a6f10
JH
81#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
82#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
83#endif
84
85unsigned long git_deflate_bound(z_streamp strm, unsigned long size)
86{
87 return deflateBound(strm, size);
88}
89
55bb5c91
JH
90void git_deflate_init(z_streamp strm, int level)
91{
92 int status = deflateInit(strm, level);
93
94 if (status == Z_OK)
95 return;
96 die("deflateInit: %s (%s)", zerr_to_string(status),
97 strm->msg ? strm->msg : "no message");
98}
99
100void git_deflate_init_gzip(z_streamp strm, int level)
101{
102 /*
103 * Use default 15 bits, +16 is to generate gzip header/trailer
104 * instead of the zlib wrapper.
105 */
106 const int windowBits = 15 + 16;
107 int status = deflateInit2(strm, level,
108 Z_DEFLATED, windowBits,
109 8, Z_DEFAULT_STRATEGY);
110 if (status == Z_OK)
111 return;
112 die("deflateInit2: %s (%s)", zerr_to_string(status),
113 strm->msg ? strm->msg : "no message");
114}
115
116void git_deflate_end(z_streamp strm)
117{
118 int status = deflateEnd(strm);
119
120 if (status == Z_OK)
121 return;
122 error("deflateEnd: %s (%s)", zerr_to_string(status),
123 strm->msg ? strm->msg : "no message");
124}
125
126int git_deflate_end_gently(z_streamp strm)
127{
128 return deflateEnd(strm);
129}
130
131int git_deflate(z_streamp strm, int flush)
132{
133 int status = deflate(strm, flush);
134
135 switch (status) {
136 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
137 case Z_BUF_ERROR:
138 case Z_OK:
139 case Z_STREAM_END:
140 return status;
141
142 case Z_MEM_ERROR:
143 die("deflate: out of memory");
144 default:
145 break;
146 }
147 error("deflate: %s (%s)", zerr_to_string(status),
148 strm->msg ? strm->msg : "no message");
149 return status;
150}