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