| 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 | { |
| 10 | switch (status) { |
| 11 | case Z_MEM_ERROR: |
| 12 | return "out of memory"; |
| 13 | case Z_VERSION_ERROR: |
| 14 | return "wrong version"; |
| 15 | case Z_NEED_DICT: |
| 16 | return "needs dictionary"; |
| 17 | case Z_DATA_ERROR: |
| 18 | return "data stream error"; |
| 19 | case Z_STREAM_ERROR: |
| 20 | return "stream consistency error"; |
| 21 | default: |
| 22 | return "unknown error"; |
| 23 | } |
| 24 | } |
| 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 | #define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */ |
| 33 | |
| 34 | /* uLong is 32-bit on Windows, even on 64-bit systems */ |
| 35 | #define ULONG_MAX_VALUE maximum_unsigned_value_of_type(uLong) |
| 36 | static inline uInt zlib_buf_cap(unsigned long len) |
| 37 | { |
| 38 | return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len; |
| 39 | } |
| 40 | |
| 41 | static inline uLong zlib_uLong_cap(size_t s) |
| 42 | { |
| 43 | return s < ULONG_MAX_VALUE ? (uLong)s : ULONG_MAX_VALUE; |
| 44 | } |
| 45 | |
| 46 | static void zlib_pre_call(git_zstream *s) |
| 47 | { |
| 48 | s->z.next_in = s->next_in; |
| 49 | s->z.next_out = s->next_out; |
| 50 | s->z.total_in = zlib_uLong_cap(s->total_in); |
| 51 | s->z.total_out = zlib_uLong_cap(s->total_out); |
| 52 | s->z.avail_in = zlib_buf_cap(s->avail_in); |
| 53 | s->z.avail_out = zlib_buf_cap(s->avail_out); |
| 54 | } |
| 55 | |
| 56 | static void zlib_post_call(git_zstream *s, int status) |
| 57 | { |
| 58 | size_t bytes_consumed; |
| 59 | size_t bytes_produced; |
| 60 | |
| 61 | bytes_consumed = s->z.next_in - s->next_in; |
| 62 | bytes_produced = s->z.next_out - s->next_out; |
| 63 | /* |
| 64 | * zlib's total_out/total_in are uLong which may wrap for >4GB. |
| 65 | * We track our own totals and verify only the low bits match. |
| 66 | */ |
| 67 | if ((s->z.total_out & ULONG_MAX_VALUE) != |
| 68 | ((zlib_uLong_cap(s->total_out) + bytes_produced) & ULONG_MAX_VALUE)) |
| 69 | BUG("total_out mismatch"); |
| 70 | /* |
| 71 | * zlib does not update total_in when it returns Z_NEED_DICT, |
| 72 | * causing a mismatch here. Skip the sanity check in that case. |
| 73 | */ |
| 74 | if (status != Z_NEED_DICT && |
| 75 | (s->z.total_in & ULONG_MAX_VALUE) != |
| 76 | ((zlib_uLong_cap(s->total_in) + bytes_consumed) & ULONG_MAX_VALUE)) |
| 77 | BUG("total_in mismatch"); |
| 78 | |
| 79 | s->total_out += bytes_produced; |
| 80 | s->total_in += bytes_consumed; |
| 81 | /* zlib-ng marks `next_in` as `const`, so we have to cast it away. */ |
| 82 | s->next_in = (unsigned char *) s->z.next_in; |
| 83 | s->next_out = s->z.next_out; |
| 84 | s->avail_in -= bytes_consumed; |
| 85 | s->avail_out -= bytes_produced; |
| 86 | } |
| 87 | |
| 88 | void git_inflate_init(git_zstream *strm) |
| 89 | { |
| 90 | int status; |
| 91 | |
| 92 | zlib_pre_call(strm); |
| 93 | status = inflateInit(&strm->z); |
| 94 | zlib_post_call(strm, status); |
| 95 | if (status == Z_OK) |
| 96 | return; |
| 97 | die("inflateInit: %s (%s)", zerr_to_string(status), |
| 98 | strm->z.msg ? strm->z.msg : "no message"); |
| 99 | } |
| 100 | |
| 101 | void git_inflate_init_gzip_only(git_zstream *strm) |
| 102 | { |
| 103 | /* |
| 104 | * Use default 15 bits, +16 is to accept only gzip and to |
| 105 | * yield Z_DATA_ERROR when fed zlib format. |
| 106 | */ |
| 107 | const int windowBits = 15 + 16; |
| 108 | int status; |
| 109 | |
| 110 | zlib_pre_call(strm); |
| 111 | status = inflateInit2(&strm->z, windowBits); |
| 112 | zlib_post_call(strm, status); |
| 113 | if (status == Z_OK) |
| 114 | return; |
| 115 | die("inflateInit2: %s (%s)", zerr_to_string(status), |
| 116 | strm->z.msg ? strm->z.msg : "no message"); |
| 117 | } |
| 118 | |
| 119 | void git_inflate_end(git_zstream *strm) |
| 120 | { |
| 121 | int status; |
| 122 | |
| 123 | zlib_pre_call(strm); |
| 124 | status = inflateEnd(&strm->z); |
| 125 | zlib_post_call(strm, status); |
| 126 | if (status == Z_OK) |
| 127 | return; |
| 128 | error("inflateEnd: %s (%s)", zerr_to_string(status), |
| 129 | strm->z.msg ? strm->z.msg : "no message"); |
| 130 | } |
| 131 | |
| 132 | int git_inflate(git_zstream *strm, int flush) |
| 133 | { |
| 134 | int status; |
| 135 | |
| 136 | for (;;) { |
| 137 | zlib_pre_call(strm); |
| 138 | /* Never say Z_FINISH unless we are feeding everything */ |
| 139 | status = inflate(&strm->z, |
| 140 | (strm->z.avail_in != strm->avail_in) |
| 141 | ? 0 : flush); |
| 142 | if (status == Z_MEM_ERROR) |
| 143 | die("inflate: out of memory"); |
| 144 | zlib_post_call(strm, status); |
| 145 | |
| 146 | /* |
| 147 | * Let zlib work another round, while we can still |
| 148 | * make progress. |
| 149 | */ |
| 150 | if ((strm->avail_out && !strm->z.avail_out) && |
| 151 | (status == Z_OK || status == Z_BUF_ERROR)) |
| 152 | continue; |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | switch (status) { |
| 157 | /* Z_BUF_ERROR: normal, needs more space in the output buffer */ |
| 158 | case Z_BUF_ERROR: |
| 159 | case Z_OK: |
| 160 | case Z_STREAM_END: |
| 161 | return status; |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | error("inflate: %s (%s)", zerr_to_string(status), |
| 166 | strm->z.msg ? strm->z.msg : "no message"); |
| 167 | return status; |
| 168 | } |
| 169 | |
| 170 | size_t git_deflate_bound(git_zstream *strm, size_t size) |
| 171 | { |
| 172 | #if SIZE_MAX > ULONG_MAX |
| 173 | if (size > maximum_unsigned_value_of_type(uLong)) |
| 174 | /* |
| 175 | * deflateBound() takes uLong, which is 32-bit on |
| 176 | * Windows. For inputs above that range, return zlib's |
| 177 | * stored-block formula (the conservative path it would |
| 178 | * itself use for an unknown stream state) plus the |
| 179 | * worst-case wrapper overhead. |
| 180 | */ |
| 181 | return size + (size >> 5) + (size >> 7) + (size >> 11) |
| 182 | + 7 + 18; |
| 183 | #endif |
| 184 | return deflateBound(&strm->z, (uLong)size); |
| 185 | } |
| 186 | |
| 187 | void git_deflate_init(git_zstream *strm, int level) |
| 188 | { |
| 189 | int status; |
| 190 | |
| 191 | memset(strm, 0, sizeof(*strm)); |
| 192 | zlib_pre_call(strm); |
| 193 | status = deflateInit(&strm->z, level); |
| 194 | zlib_post_call(strm, status); |
| 195 | if (status == Z_OK) |
| 196 | return; |
| 197 | die("deflateInit: %s (%s)", zerr_to_string(status), |
| 198 | strm->z.msg ? strm->z.msg : "no message"); |
| 199 | } |
| 200 | |
| 201 | static void do_git_deflate_init(git_zstream *strm, int level, int windowBits) |
| 202 | { |
| 203 | int status; |
| 204 | |
| 205 | memset(strm, 0, sizeof(*strm)); |
| 206 | zlib_pre_call(strm); |
| 207 | status = deflateInit2(&strm->z, level, |
| 208 | Z_DEFLATED, windowBits, |
| 209 | 8, Z_DEFAULT_STRATEGY); |
| 210 | zlib_post_call(strm, status); |
| 211 | if (status == Z_OK) |
| 212 | return; |
| 213 | die("deflateInit2: %s (%s)", zerr_to_string(status), |
| 214 | strm->z.msg ? strm->z.msg : "no message"); |
| 215 | } |
| 216 | |
| 217 | void git_deflate_init_gzip(git_zstream *strm, int level) |
| 218 | { |
| 219 | /* |
| 220 | * Use default 15 bits, +16 is to generate gzip header/trailer |
| 221 | * instead of the zlib wrapper. |
| 222 | */ |
| 223 | do_git_deflate_init(strm, level, 15 + 16); |
| 224 | } |
| 225 | |
| 226 | void git_deflate_init_raw(git_zstream *strm, int level) |
| 227 | { |
| 228 | /* |
| 229 | * Use default 15 bits, negate the value to get raw compressed |
| 230 | * data without zlib header and trailer. |
| 231 | */ |
| 232 | do_git_deflate_init(strm, level, -15); |
| 233 | } |
| 234 | |
| 235 | int git_deflate_abort(git_zstream *strm) |
| 236 | { |
| 237 | int status; |
| 238 | |
| 239 | zlib_pre_call(strm); |
| 240 | status = deflateEnd(&strm->z); |
| 241 | zlib_post_call(strm, status); |
| 242 | return status; |
| 243 | } |
| 244 | |
| 245 | void git_deflate_end(git_zstream *strm) |
| 246 | { |
| 247 | int status = git_deflate_abort(strm); |
| 248 | |
| 249 | if (status == Z_OK) |
| 250 | return; |
| 251 | error("deflateEnd: %s (%s)", zerr_to_string(status), |
| 252 | strm->z.msg ? strm->z.msg : "no message"); |
| 253 | } |
| 254 | |
| 255 | int git_deflate_end_gently(git_zstream *strm) |
| 256 | { |
| 257 | int status; |
| 258 | |
| 259 | zlib_pre_call(strm); |
| 260 | status = deflateEnd(&strm->z); |
| 261 | zlib_post_call(strm, status); |
| 262 | return status; |
| 263 | } |
| 264 | |
| 265 | int git_deflate(git_zstream *strm, int flush) |
| 266 | { |
| 267 | int status; |
| 268 | |
| 269 | for (;;) { |
| 270 | zlib_pre_call(strm); |
| 271 | |
| 272 | /* Never say Z_FINISH unless we are feeding everything */ |
| 273 | status = deflate(&strm->z, |
| 274 | (strm->z.avail_in != strm->avail_in) |
| 275 | ? 0 : flush); |
| 276 | if (status == Z_MEM_ERROR) |
| 277 | die("deflate: out of memory"); |
| 278 | zlib_post_call(strm, status); |
| 279 | |
| 280 | /* |
| 281 | * Let zlib work another round, while we can still |
| 282 | * make progress. |
| 283 | */ |
| 284 | if ((strm->avail_out && !strm->z.avail_out) && |
| 285 | (status == Z_OK || status == Z_BUF_ERROR)) |
| 286 | continue; |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | switch (status) { |
| 291 | /* Z_BUF_ERROR: normal, needs more space in the output buffer */ |
| 292 | case Z_BUF_ERROR: |
| 293 | case Z_OK: |
| 294 | case Z_STREAM_END: |
| 295 | return status; |
| 296 | default: |
| 297 | break; |
| 298 | } |
| 299 | error("deflate: %s (%s)", zerr_to_string(status), |
| 300 | strm->z.msg ? strm->z.msg : "no message"); |
| 301 | return status; |
| 302 | } |