static void *zlib_deflate(void *data, unsigned long size,
int compression_level, unsigned long *compressed_size)
{
- z_stream stream;
+ git_zstream stream;
unsigned long maxsize;
void *buffer;
int result;
static char *inflate_it(const void *data, unsigned long size,
unsigned long inflated_size)
{
- z_stream stream;
+ git_zstream stream;
void *out;
int st;
static void *unpack_entry_data(unsigned long offset, unsigned long size)
{
int status;
- z_stream stream;
+ git_zstream stream;
void *buf = xmalloc(size);
memset(&stream, 0, sizeof(stream));
off_t from = obj[0].idx.offset + obj[0].hdr_size;
unsigned long len = obj[1].idx.offset - from;
unsigned char *data, *inbuf;
- z_stream stream;
+ git_zstream stream;
int status;
data = xmalloc(obj->size);
static int write_compressed(struct sha1file *f, void *in, unsigned int size)
{
- z_stream stream;
+ git_zstream stream;
int status;
unsigned char outbuf[4096];
static unsigned long do_compress(void **pptr, unsigned long size)
{
- z_stream stream;
+ git_zstream stream;
void *in, *out;
unsigned long maxsize;
off_t len,
unsigned long expect)
{
- z_stream stream;
+ git_zstream stream;
unsigned char fakebuf[4096], *in;
int st;
off_t len)
{
unsigned char *in;
- unsigned int avail;
+ unsigned long avail;
while (len) {
in = use_pack(p, w_curs, offset, &avail);
if (avail > len)
- avail = (unsigned int)len;
+ avail = (unsigned long)len;
sha1write(f, in, avail);
offset += avail;
len -= avail;
const unsigned char *base_ref = NULL;
struct object_entry *base_entry;
unsigned long used, used_0;
- unsigned int avail;
+ unsigned long avail;
off_t ofs;
unsigned char *buf, c;
static void *get_data(unsigned long size)
{
- z_stream stream;
+ git_zstream stream;
void *buf = xmalloc(size);
memset(&stream, 0, sizeof(stream));
#endif
#include <zlib.h>
-
-void git_inflate_init(z_streamp strm);
-void git_inflate_init_gzip_only(z_streamp strm);
-void git_inflate_end(z_streamp strm);
-int git_inflate(z_streamp strm, int flush);
-
-void git_deflate_init(z_streamp strm, int level);
-void git_deflate_init_gzip(z_streamp strm, int level);
-void git_deflate_end(z_streamp strm);
-int git_deflate_end_gently(z_streamp strm);
-int git_deflate(z_streamp strm, int flush);
-unsigned long git_deflate_bound(z_streamp, unsigned long);
+typedef struct git_zstream {
+ z_stream z;
+ unsigned long avail_in;
+ unsigned long avail_out;
+ unsigned long total_in;
+ unsigned long total_out;
+ unsigned char *next_in;
+ unsigned char *next_out;
+} git_zstream;
+
+void git_inflate_init(git_zstream *);
+void git_inflate_init_gzip_only(git_zstream *);
+void git_inflate_end(git_zstream *);
+int git_inflate(git_zstream *, int flush);
+
+void git_deflate_init(git_zstream *, int level);
+void git_deflate_init_gzip(git_zstream *, int level);
+void git_deflate_end(git_zstream *);
+int git_deflate_end_gently(git_zstream *);
+int git_deflate(git_zstream *, int flush);
+unsigned long git_deflate_bound(git_zstream *, unsigned long);
#if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
#define DTYPE(de) ((de)->d_type)
extern void pack_report(void);
extern int open_pack_index(struct packed_git *);
extern void close_pack_index(struct packed_git *);
-extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);
+extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
extern void close_pack_windows(struct packed_git *);
extern void unuse_pack(struct pack_window **);
extern void free_pack_by_name(const char *);
{
int bound;
unsigned char *deflated;
- z_stream stream;
+ git_zstream stream;
memset(&stream, 0, sizeof(stream));
git_deflate_init(&stream, zlib_compression_level);
unsigned char sha1[20];
unsigned long hdrlen, deltalen;
git_SHA_CTX c;
- z_stream s;
+ git_zstream s;
hdrlen = sprintf((char *)hdr,"%s %lu", typename(type),
(unsigned long)dat->len) + 1;
off_t offset;
git_SHA_CTX c;
git_SHA_CTX pack_file_ctx;
- z_stream s;
+ git_zstream s;
int status = Z_OK;
/* Determine if we should auto-checkpoint. */
static void inflate_request(const char *prog_name, int out)
{
- z_stream stream;
+ git_zstream stream;
unsigned char in_buf[8192];
unsigned char out_buf[8192];
unsigned long cnt = 0;
unsigned long len;
int hdrlen;
ssize_t size;
- z_stream stream;
+ git_zstream stream;
unpacked = read_sha1_file(request->obj->sha1, &type, &len);
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
unsigned char sha1[20];
unsigned char real_sha1[20];
git_SHA_CTX c;
- z_stream stream;
+ git_zstream stream;
int zret;
int rename;
struct active_request_slot *slot;
uint32_t data_crc = crc32(0, NULL, 0);
do {
- unsigned int avail;
+ unsigned long avail;
void *data = use_pack(p, w_curs, offset, &avail);
if (avail > len)
avail = len;
git_SHA1_Init(&ctx);
do {
- unsigned int remaining;
+ unsigned long remaining;
unsigned char *in = use_pack(p, w_curs, offset, &remaining);
offset += remaining;
if (!pack_sig_ofs)
* the transfer time.
*/
size_t size;
- z_stream stream;
+ git_zstream stream;
int ret;
memset(&stream, 0, sizeof(stream));
unsigned char *use_pack(struct packed_git *p,
struct pack_window **w_cursor,
off_t offset,
- unsigned int *left)
+ unsigned long *left)
{
struct pack_window *win = *w_cursor;
return used;
}
-static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
+static int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
{
unsigned long size, used;
static const char valid_loose_object_type[8] = {
return 0;
}
-static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
+static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
{
int bytes = strlen(buffer) + 1;
unsigned char *buf = xmallocz(size);
static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1)
{
int ret;
- z_stream stream;
+ git_zstream stream;
char hdr[8192];
ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
{
const unsigned char *data;
unsigned char delta_head[20], *in;
- z_stream stream;
+ git_zstream stream;
int st;
memset(&stream, 0, sizeof(stream));
unsigned long *sizep)
{
unsigned char *base;
- unsigned int left;
+ unsigned long left;
unsigned long used;
enum object_type type;
unsigned long size)
{
int st;
- z_stream stream;
+ git_zstream stream;
unsigned char *buffer, *in;
buffer = xmallocz(size);
int status;
unsigned long mapsize, size;
void *map;
- z_stream stream;
+ git_zstream stream;
char hdr[32];
map = map_sha1_file(sha1, &mapsize);
{
int fd, ret;
unsigned char compressed[4096];
- z_stream stream;
+ git_zstream stream;
git_SHA_CTX c;
unsigned char parano_sha1[20];
char *filename;
}
}
-void git_inflate_init(z_streamp strm)
+/*
+ * 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.
+ */
+#define ZLIB_BUF_MAX ((uInt)-1)
+static inline uInt zlib_buf_cap(unsigned long len)
+{
+ if (ZLIB_BUF_MAX < len)
+ die("working buffer for zlib too large");
+ return len;
+}
+
+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)
+{
+ s->next_in = s->z.next_in;
+ s->next_out = s->z.next_out;
+ s->total_in = s->z.total_in;
+ s->total_out = s->z.total_out;
+ s->avail_in = s->z.avail_in;
+ s->avail_out = s->z.avail_out;
+}
+
+void git_inflate_init(git_zstream *strm)
{
- int status = inflateInit(strm);
+ int status;
+ zlib_pre_call(strm);
+ status = inflateInit(&strm->z);
+ zlib_post_call(strm);
if (status == Z_OK)
return;
die("inflateInit: %s (%s)", zerr_to_string(status),
- strm->msg ? strm->msg : "no message");
+ strm->z.msg ? strm->z.msg : "no message");
}
-void git_inflate_init_gzip_only(z_streamp strm)
+void git_inflate_init_gzip_only(git_zstream *strm)
{
/*
* 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;
- int status = inflateInit2(strm, windowBits);
+ int status;
+ zlib_pre_call(strm);
+ status = inflateInit2(&strm->z, windowBits);
+ zlib_post_call(strm);
if (status == Z_OK)
return;
die("inflateInit2: %s (%s)", zerr_to_string(status),
- strm->msg ? strm->msg : "no message");
+ strm->z.msg ? strm->z.msg : "no message");
}
-void git_inflate_end(z_streamp strm)
+void git_inflate_end(git_zstream *strm)
{
- int status = inflateEnd(strm);
+ int status;
+ zlib_pre_call(strm);
+ status = inflateEnd(&strm->z);
+ zlib_post_call(strm);
if (status == Z_OK)
return;
error("inflateEnd: %s (%s)", zerr_to_string(status),
- strm->msg ? strm->msg : "no message");
+ strm->z.msg ? strm->z.msg : "no message");
}
-int git_inflate(z_streamp strm, int flush)
+int git_inflate(git_zstream *strm, int flush)
{
- int status = inflate(strm, flush);
+ int status;
+ zlib_pre_call(strm);
+ status = inflate(&strm->z, flush);
+ zlib_post_call(strm);
switch (status) {
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
case Z_BUF_ERROR:
break;
}
error("inflate: %s (%s)", zerr_to_string(status),
- strm->msg ? strm->msg : "no message");
+ strm->z.msg ? strm->z.msg : "no message");
return status;
}
#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
#endif
-unsigned long git_deflate_bound(z_streamp strm, unsigned long size)
+unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
{
- return deflateBound(strm, size);
+ return deflateBound(&strm->z, size);
}
-void git_deflate_init(z_streamp strm, int level)
+void git_deflate_init(git_zstream *strm, int level)
{
- int status = deflateInit(strm, level);
+ int status;
+ zlib_pre_call(strm);
+ status = deflateInit(&strm->z, level);
+ zlib_post_call(strm);
if (status == Z_OK)
return;
die("deflateInit: %s (%s)", zerr_to_string(status),
- strm->msg ? strm->msg : "no message");
+ strm->z.msg ? strm->z.msg : "no message");
}
-void git_deflate_init_gzip(z_streamp strm, int level)
+void git_deflate_init_gzip(git_zstream *strm, int level)
{
/*
* Use default 15 bits, +16 is to generate gzip header/trailer
* instead of the zlib wrapper.
*/
const int windowBits = 15 + 16;
- int status = deflateInit2(strm, level,
+ int status;
+
+ zlib_pre_call(strm);
+ status = deflateInit2(&strm->z, level,
Z_DEFLATED, windowBits,
8, Z_DEFAULT_STRATEGY);
+ zlib_post_call(strm);
if (status == Z_OK)
return;
die("deflateInit2: %s (%s)", zerr_to_string(status),
- strm->msg ? strm->msg : "no message");
+ strm->z.msg ? strm->z.msg : "no message");
}
-void git_deflate_end(z_streamp strm)
+void git_deflate_end(git_zstream *strm)
{
- int status = deflateEnd(strm);
+ int status;
+ zlib_pre_call(strm);
+ status = deflateEnd(&strm->z);
+ zlib_post_call(strm);
if (status == Z_OK)
return;
error("deflateEnd: %s (%s)", zerr_to_string(status),
- strm->msg ? strm->msg : "no message");
+ strm->z.msg ? strm->z.msg : "no message");
}
-int git_deflate_end_gently(z_streamp strm)
+int git_deflate_end_gently(git_zstream *strm)
{
- return deflateEnd(strm);
+ int status;
+
+ zlib_pre_call(strm);
+ status = deflateEnd(&strm->z);
+ zlib_post_call(strm);
+ return status;
}
-int git_deflate(z_streamp strm, int flush)
+int git_deflate(git_zstream *strm, int flush)
{
- int status = deflate(strm, flush);
+ int status;
+ zlib_pre_call(strm);
+ status = deflate(&strm->z, flush);
+ zlib_post_call(strm);
switch (status) {
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
case Z_BUF_ERROR:
break;
}
error("deflate: %s (%s)", zerr_to_string(status),
- strm->msg ? strm->msg : "no message");
+ strm->z.msg ? strm->z.msg : "no message");
return status;
}