Coverage Report

Created: 2024-09-08 06:23

/src/git/git-zlib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * zlib wrappers to make sure we don't silently miss errors
3
 * at init time.
4
 */
5
#include "git-compat-util.h"
6
#include "git-zlib.h"
7
8
static const char *zerr_to_string(int status)
9
0
{
10
0
  switch (status) {
11
0
  case Z_MEM_ERROR:
12
0
    return "out of memory";
13
0
  case Z_VERSION_ERROR:
14
0
    return "wrong version";
15
0
  case Z_NEED_DICT:
16
0
    return "needs dictionary";
17
0
  case Z_DATA_ERROR:
18
0
    return "data stream error";
19
0
  case Z_STREAM_ERROR:
20
0
    return "stream consistency error";
21
0
  default:
22
0
    return "unknown error";
23
0
  }
24
0
}
25
26
/*
27
 * avail_in and avail_out in zlib are counted in uInt, which typically
28
 * limits the size of the buffer we can use to 4GB when interacting
29
 * with zlib in a single call to inflate/deflate.
30
 */
31
/* #define ZLIB_BUF_MAX ((uInt)-1) */
32
0
#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
33
static inline uInt zlib_buf_cap(unsigned long len)
34
0
{
35
0
  return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
36
0
}
37
38
static void zlib_pre_call(git_zstream *s)
39
0
{
40
0
  s->z.next_in = s->next_in;
41
0
  s->z.next_out = s->next_out;
42
0
  s->z.total_in = s->total_in;
43
0
  s->z.total_out = s->total_out;
44
0
  s->z.avail_in = zlib_buf_cap(s->avail_in);
45
0
  s->z.avail_out = zlib_buf_cap(s->avail_out);
46
0
}
47
48
static void zlib_post_call(git_zstream *s)
49
0
{
50
0
  unsigned long bytes_consumed;
51
0
  unsigned long bytes_produced;
52
53
0
  bytes_consumed = s->z.next_in - s->next_in;
54
0
  bytes_produced = s->z.next_out - s->next_out;
55
0
  if (s->z.total_out != s->total_out + bytes_produced)
56
0
    BUG("total_out mismatch");
57
0
  if (s->z.total_in != s->total_in + bytes_consumed)
58
0
    BUG("total_in mismatch");
59
60
0
  s->total_out = s->z.total_out;
61
0
  s->total_in = s->z.total_in;
62
0
  s->next_in = s->z.next_in;
63
0
  s->next_out = s->z.next_out;
64
0
  s->avail_in -= bytes_consumed;
65
0
  s->avail_out -= bytes_produced;
66
0
}
67
68
void git_inflate_init(git_zstream *strm)
69
0
{
70
0
  int status;
71
72
0
  zlib_pre_call(strm);
73
0
  status = inflateInit(&strm->z);
74
0
  zlib_post_call(strm);
75
0
  if (status == Z_OK)
76
0
    return;
77
0
  die("inflateInit: %s (%s)", zerr_to_string(status),
78
0
      strm->z.msg ? strm->z.msg : "no message");
79
0
}
80
81
void git_inflate_init_gzip_only(git_zstream *strm)
82
0
{
83
  /*
84
   * Use default 15 bits, +16 is to accept only gzip and to
85
   * yield Z_DATA_ERROR when fed zlib format.
86
   */
87
0
  const int windowBits = 15 + 16;
88
0
  int status;
89
90
0
  zlib_pre_call(strm);
91
0
  status = inflateInit2(&strm->z, windowBits);
92
0
  zlib_post_call(strm);
93
0
  if (status == Z_OK)
94
0
    return;
95
0
  die("inflateInit2: %s (%s)", zerr_to_string(status),
96
0
      strm->z.msg ? strm->z.msg : "no message");
97
0
}
98
99
void git_inflate_end(git_zstream *strm)
100
0
{
101
0
  int status;
102
103
0
  zlib_pre_call(strm);
104
0
  status = inflateEnd(&strm->z);
105
0
  zlib_post_call(strm);
106
0
  if (status == Z_OK)
107
0
    return;
108
0
  error("inflateEnd: %s (%s)", zerr_to_string(status),
109
0
        strm->z.msg ? strm->z.msg : "no message");
110
0
}
111
112
int git_inflate(git_zstream *strm, int flush)
113
0
{
114
0
  int status;
115
116
0
  for (;;) {
117
0
    zlib_pre_call(strm);
118
    /* Never say Z_FINISH unless we are feeding everything */
119
0
    status = inflate(&strm->z,
120
0
         (strm->z.avail_in != strm->avail_in)
121
0
         ? 0 : flush);
122
0
    if (status == Z_MEM_ERROR)
123
0
      die("inflate: out of memory");
124
0
    zlib_post_call(strm);
125
126
    /*
127
     * Let zlib work another round, while we can still
128
     * make progress.
129
     */
130
0
    if ((strm->avail_out && !strm->z.avail_out) &&
131
0
        (status == Z_OK || status == Z_BUF_ERROR))
132
0
      continue;
133
0
    break;
134
0
  }
135
136
0
  switch (status) {
137
  /* Z_BUF_ERROR: normal, needs more space in the output buffer */
138
0
  case Z_BUF_ERROR:
139
0
  case Z_OK:
140
0
  case Z_STREAM_END:
141
0
    return status;
142
0
  default:
143
0
    break;
144
0
  }
145
0
  error("inflate: %s (%s)", zerr_to_string(status),
146
0
        strm->z.msg ? strm->z.msg : "no message");
147
0
  return status;
148
0
}
149
150
#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
151
#define deflateBound(c,s)  ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
152
#endif
153
154
unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
155
0
{
156
0
  return deflateBound(&strm->z, size);
157
0
}
158
159
void git_deflate_init(git_zstream *strm, int level)
160
0
{
161
0
  int status;
162
163
0
  memset(strm, 0, sizeof(*strm));
164
0
  zlib_pre_call(strm);
165
0
  status = deflateInit(&strm->z, level);
166
0
  zlib_post_call(strm);
167
0
  if (status == Z_OK)
168
0
    return;
169
0
  die("deflateInit: %s (%s)", zerr_to_string(status),
170
0
      strm->z.msg ? strm->z.msg : "no message");
171
0
}
172
173
static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
174
0
{
175
0
  int status;
176
177
0
  memset(strm, 0, sizeof(*strm));
178
0
  zlib_pre_call(strm);
179
0
  status = deflateInit2(&strm->z, level,
180
0
          Z_DEFLATED, windowBits,
181
0
          8, Z_DEFAULT_STRATEGY);
182
0
  zlib_post_call(strm);
183
0
  if (status == Z_OK)
184
0
    return;
185
0
  die("deflateInit2: %s (%s)", zerr_to_string(status),
186
0
      strm->z.msg ? strm->z.msg : "no message");
187
0
}
188
189
void git_deflate_init_gzip(git_zstream *strm, int level)
190
0
{
191
  /*
192
   * Use default 15 bits, +16 is to generate gzip header/trailer
193
   * instead of the zlib wrapper.
194
   */
195
0
  do_git_deflate_init(strm, level, 15 + 16);
196
0
}
197
198
void git_deflate_init_raw(git_zstream *strm, int level)
199
0
{
200
  /*
201
   * Use default 15 bits, negate the value to get raw compressed
202
   * data without zlib header and trailer.
203
   */
204
0
  do_git_deflate_init(strm, level, -15);
205
0
}
206
207
int git_deflate_abort(git_zstream *strm)
208
0
{
209
0
  int status;
210
211
0
  zlib_pre_call(strm);
212
0
  status = deflateEnd(&strm->z);
213
0
  zlib_post_call(strm);
214
0
  return status;
215
0
}
216
217
void git_deflate_end(git_zstream *strm)
218
0
{
219
0
  int status = git_deflate_abort(strm);
220
221
0
  if (status == Z_OK)
222
0
    return;
223
0
  error("deflateEnd: %s (%s)", zerr_to_string(status),
224
0
        strm->z.msg ? strm->z.msg : "no message");
225
0
}
226
227
int git_deflate_end_gently(git_zstream *strm)
228
0
{
229
0
  int status;
230
231
0
  zlib_pre_call(strm);
232
0
  status = deflateEnd(&strm->z);
233
0
  zlib_post_call(strm);
234
0
  return status;
235
0
}
236
237
int git_deflate(git_zstream *strm, int flush)
238
0
{
239
0
  int status;
240
241
0
  for (;;) {
242
0
    zlib_pre_call(strm);
243
244
    /* Never say Z_FINISH unless we are feeding everything */
245
0
    status = deflate(&strm->z,
246
0
         (strm->z.avail_in != strm->avail_in)
247
0
         ? 0 : flush);
248
0
    if (status == Z_MEM_ERROR)
249
0
      die("deflate: out of memory");
250
0
    zlib_post_call(strm);
251
252
    /*
253
     * Let zlib work another round, while we can still
254
     * make progress.
255
     */
256
0
    if ((strm->avail_out && !strm->z.avail_out) &&
257
0
        (status == Z_OK || status == Z_BUF_ERROR))
258
0
      continue;
259
0
    break;
260
0
  }
261
262
0
  switch (status) {
263
  /* Z_BUF_ERROR: normal, needs more space in the output buffer */
264
0
  case Z_BUF_ERROR:
265
0
  case Z_OK:
266
0
  case Z_STREAM_END:
267
0
    return status;
268
0
  default:
269
0
    break;
270
0
  }
271
0
  error("deflate: %s (%s)", zerr_to_string(status),
272
0
        strm->z.msg ? strm->z.msg : "no message");
273
0
  return status;
274
0
}