2010-11-06 11:47:34 +00:00
|
|
|
/*
|
|
|
|
* zlib wrappers to make sure we don't silently miss errors
|
|
|
|
* at init time.
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
|
|
|
|
2011-06-10 17:31:34 +00:00
|
|
|
static const char *zerr_to_string(int status)
|
2010-11-06 11:47:34 +00:00
|
|
|
{
|
2011-06-10 17:31:34 +00:00
|
|
|
switch (status) {
|
2010-11-06 11:47:34 +00:00
|
|
|
case Z_MEM_ERROR:
|
2011-06-10 17:31:34 +00:00
|
|
|
return "out of memory";
|
2010-11-06 11:47:34 +00:00
|
|
|
case Z_VERSION_ERROR:
|
2011-06-10 17:31:34 +00:00
|
|
|
return "wrong version";
|
|
|
|
case Z_NEED_DICT:
|
|
|
|
return "needs dictionary";
|
|
|
|
case Z_DATA_ERROR:
|
|
|
|
return "data stream error";
|
|
|
|
case Z_STREAM_ERROR:
|
|
|
|
return "stream consistency error";
|
2010-11-06 11:47:34 +00:00
|
|
|
default:
|
2011-06-10 17:31:34 +00:00
|
|
|
return "unknown error";
|
2010-11-06 11:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
/*
|
|
|
|
* avail_in and avail_out in zlib are counted in uInt, which typically
|
|
|
|
* limits the size of the buffer we can use to 4GB when interacting
|
|
|
|
* with zlib in a single call to inflate/deflate.
|
|
|
|
*/
|
2011-06-10 19:15:17 +00:00
|
|
|
/* #define ZLIB_BUF_MAX ((uInt)-1) */
|
|
|
|
#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
|
2011-06-10 18:52:15 +00:00
|
|
|
static inline uInt zlib_buf_cap(unsigned long len)
|
|
|
|
{
|
2011-06-10 19:15:17 +00:00
|
|
|
return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
|
2011-06-10 18:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void zlib_pre_call(git_zstream *s)
|
|
|
|
{
|
|
|
|
s->z.next_in = s->next_in;
|
|
|
|
s->z.next_out = s->next_out;
|
|
|
|
s->z.total_in = s->total_in;
|
|
|
|
s->z.total_out = s->total_out;
|
|
|
|
s->z.avail_in = zlib_buf_cap(s->avail_in);
|
|
|
|
s->z.avail_out = zlib_buf_cap(s->avail_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zlib_post_call(git_zstream *s)
|
|
|
|
{
|
2011-06-10 19:15:17 +00:00
|
|
|
unsigned long bytes_consumed;
|
|
|
|
unsigned long bytes_produced;
|
|
|
|
|
|
|
|
bytes_consumed = s->z.next_in - s->next_in;
|
|
|
|
bytes_produced = s->z.next_out - s->next_out;
|
|
|
|
if (s->z.total_out != s->total_out + bytes_produced)
|
|
|
|
die("BUG: total_out mismatch");
|
|
|
|
if (s->z.total_in != s->total_in + bytes_consumed)
|
|
|
|
die("BUG: total_in mismatch");
|
|
|
|
|
|
|
|
s->total_out = s->z.total_out;
|
|
|
|
s->total_in = s->z.total_in;
|
2011-06-10 18:52:15 +00:00
|
|
|
s->next_in = s->z.next_in;
|
|
|
|
s->next_out = s->z.next_out;
|
2011-06-10 19:15:17 +00:00
|
|
|
s->avail_in -= bytes_consumed;
|
|
|
|
s->avail_out -= bytes_produced;
|
2011-06-10 18:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void git_inflate_init(git_zstream *strm)
|
2010-11-06 11:47:34 +00:00
|
|
|
{
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
2011-06-10 17:31:34 +00:00
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
zlib_pre_call(strm);
|
|
|
|
status = inflateInit(&strm->z);
|
|
|
|
zlib_post_call(strm);
|
2011-06-10 17:31:34 +00:00
|
|
|
if (status == Z_OK)
|
|
|
|
return;
|
|
|
|
die("inflateInit: %s (%s)", zerr_to_string(status),
|
2011-06-10 18:52:15 +00:00
|
|
|
strm->z.msg ? strm->z.msg : "no message");
|
2010-11-06 11:47:34 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
void git_inflate_init_gzip_only(git_zstream *strm)
|
2011-06-10 17:45:29 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Use default 15 bits, +16 is to accept only gzip and to
|
|
|
|
* yield Z_DATA_ERROR when fed zlib format.
|
|
|
|
*/
|
|
|
|
const int windowBits = 15 + 16;
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
2011-06-10 17:45:29 +00:00
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
zlib_pre_call(strm);
|
|
|
|
status = inflateInit2(&strm->z, windowBits);
|
|
|
|
zlib_post_call(strm);
|
2011-06-10 17:45:29 +00:00
|
|
|
if (status == Z_OK)
|
|
|
|
return;
|
|
|
|
die("inflateInit2: %s (%s)", zerr_to_string(status),
|
2011-06-10 18:52:15 +00:00
|
|
|
strm->z.msg ? strm->z.msg : "no message");
|
2011-06-10 17:45:29 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
void git_inflate_end(git_zstream *strm)
|
2010-11-06 11:47:34 +00:00
|
|
|
{
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
2010-11-06 11:47:34 +00:00
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
zlib_pre_call(strm);
|
|
|
|
status = inflateEnd(&strm->z);
|
|
|
|
zlib_post_call(strm);
|
2011-06-10 17:31:34 +00:00
|
|
|
if (status == Z_OK)
|
|
|
|
return;
|
|
|
|
error("inflateEnd: %s (%s)", zerr_to_string(status),
|
2011-06-10 18:52:15 +00:00
|
|
|
strm->z.msg ? strm->z.msg : "no message");
|
2011-06-10 17:31:34 +00:00
|
|
|
}
|
2010-11-06 11:47:34 +00:00
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
int git_inflate(git_zstream *strm, int flush)
|
2011-06-10 17:31:34 +00:00
|
|
|
{
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
2010-11-06 11:47:34 +00:00
|
|
|
|
2011-06-10 19:15:17 +00:00
|
|
|
for (;;) {
|
|
|
|
zlib_pre_call(strm);
|
|
|
|
/* Never say Z_FINISH unless we are feeding everything */
|
|
|
|
status = inflate(&strm->z,
|
|
|
|
(strm->z.avail_in != strm->avail_in)
|
|
|
|
? 0 : flush);
|
|
|
|
if (status == Z_MEM_ERROR)
|
|
|
|
die("inflate: out of memory");
|
|
|
|
zlib_post_call(strm);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Let zlib work another round, while we can still
|
|
|
|
* make progress.
|
|
|
|
*/
|
|
|
|
if ((strm->avail_out && !strm->z.avail_out) &&
|
|
|
|
(status == Z_OK || status == Z_BUF_ERROR))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-10 17:31:34 +00:00
|
|
|
switch (status) {
|
2010-11-06 11:47:34 +00:00
|
|
|
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
|
|
|
|
case Z_BUF_ERROR:
|
|
|
|
case Z_OK:
|
|
|
|
case Z_STREAM_END:
|
2011-06-10 17:31:34 +00:00
|
|
|
return status;
|
|
|
|
default:
|
|
|
|
break;
|
2010-11-06 11:47:34 +00:00
|
|
|
}
|
2011-06-10 17:31:34 +00:00
|
|
|
error("inflate: %s (%s)", zerr_to_string(status),
|
2011-06-10 18:52:15 +00:00
|
|
|
strm->z.msg ? strm->z.msg : "no message");
|
2011-06-10 17:31:34 +00:00
|
|
|
return status;
|
2010-11-06 11:47:34 +00:00
|
|
|
}
|
2011-06-10 17:55:10 +00:00
|
|
|
|
2011-06-10 18:18:17 +00:00
|
|
|
#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
|
|
|
|
#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
|
|
|
|
#endif
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
|
2011-06-10 18:18:17 +00:00
|
|
|
{
|
2011-06-10 18:52:15 +00:00
|
|
|
return deflateBound(&strm->z, size);
|
2011-06-10 18:18:17 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
void git_deflate_init(git_zstream *strm, int level)
|
2011-06-10 17:55:10 +00:00
|
|
|
{
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
2011-06-10 17:55:10 +00:00
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
zlib_pre_call(strm);
|
|
|
|
status = deflateInit(&strm->z, level);
|
|
|
|
zlib_post_call(strm);
|
2011-06-10 17:55:10 +00:00
|
|
|
if (status == Z_OK)
|
|
|
|
return;
|
|
|
|
die("deflateInit: %s (%s)", zerr_to_string(status),
|
2011-06-10 18:52:15 +00:00
|
|
|
strm->z.msg ? strm->z.msg : "no message");
|
2011-06-10 17:55:10 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
void git_deflate_init_gzip(git_zstream *strm, int level)
|
2011-06-10 17:55:10 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Use default 15 bits, +16 is to generate gzip header/trailer
|
|
|
|
* instead of the zlib wrapper.
|
|
|
|
*/
|
|
|
|
const int windowBits = 15 + 16;
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
|
|
|
|
|
|
|
zlib_pre_call(strm);
|
|
|
|
status = deflateInit2(&strm->z, level,
|
2011-06-10 17:55:10 +00:00
|
|
|
Z_DEFLATED, windowBits,
|
|
|
|
8, Z_DEFAULT_STRATEGY);
|
2011-06-10 18:52:15 +00:00
|
|
|
zlib_post_call(strm);
|
2011-06-10 17:55:10 +00:00
|
|
|
if (status == Z_OK)
|
|
|
|
return;
|
|
|
|
die("deflateInit2: %s (%s)", zerr_to_string(status),
|
2011-06-10 18:52:15 +00:00
|
|
|
strm->z.msg ? strm->z.msg : "no message");
|
2011-06-10 17:55:10 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
void git_deflate_end(git_zstream *strm)
|
2011-06-10 17:55:10 +00:00
|
|
|
{
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
2011-06-10 17:55:10 +00:00
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
zlib_pre_call(strm);
|
|
|
|
status = deflateEnd(&strm->z);
|
|
|
|
zlib_post_call(strm);
|
2011-06-10 17:55:10 +00:00
|
|
|
if (status == Z_OK)
|
|
|
|
return;
|
|
|
|
error("deflateEnd: %s (%s)", zerr_to_string(status),
|
2011-06-10 18:52:15 +00:00
|
|
|
strm->z.msg ? strm->z.msg : "no message");
|
2011-06-10 17:55:10 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
int git_deflate_end_gently(git_zstream *strm)
|
2011-06-10 17:55:10 +00:00
|
|
|
{
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
|
|
|
|
|
|
|
zlib_pre_call(strm);
|
|
|
|
status = deflateEnd(&strm->z);
|
|
|
|
zlib_post_call(strm);
|
|
|
|
return status;
|
2011-06-10 17:55:10 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 18:52:15 +00:00
|
|
|
int git_deflate(git_zstream *strm, int flush)
|
2011-06-10 17:55:10 +00:00
|
|
|
{
|
2011-06-10 18:52:15 +00:00
|
|
|
int status;
|
2011-06-10 17:55:10 +00:00
|
|
|
|
2011-06-10 19:15:17 +00:00
|
|
|
for (;;) {
|
|
|
|
zlib_pre_call(strm);
|
|
|
|
|
|
|
|
/* Never say Z_FINISH unless we are feeding everything */
|
|
|
|
status = deflate(&strm->z,
|
|
|
|
(strm->z.avail_in != strm->avail_in)
|
|
|
|
? 0 : flush);
|
|
|
|
if (status == Z_MEM_ERROR)
|
|
|
|
die("deflate: out of memory");
|
|
|
|
zlib_post_call(strm);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Let zlib work another round, while we can still
|
|
|
|
* make progress.
|
|
|
|
*/
|
|
|
|
if ((strm->avail_out && !strm->z.avail_out) &&
|
|
|
|
(status == Z_OK || status == Z_BUF_ERROR))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-10 17:55:10 +00:00
|
|
|
switch (status) {
|
|
|
|
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
|
|
|
|
case Z_BUF_ERROR:
|
|
|
|
case Z_OK:
|
|
|
|
case Z_STREAM_END:
|
|
|
|
return status;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
error("deflate: %s (%s)", zerr_to_string(status),
|
2011-06-10 18:52:15 +00:00
|
|
|
strm->z.msg ? strm->z.msg : "no message");
|
2011-06-10 17:55:10 +00:00
|
|
|
return status;
|
|
|
|
}
|