Merge branch 'jc/zlib-wrap' into maint

* jc/zlib-wrap:
  zlib: allow feeding more than 4GB in one go
  zlib: zlib can only process 4GB at a time
  zlib: wrap deflateBound() too
  zlib: wrap deflate side of the API
  zlib: wrap inflateInit2 used to accept only for gzip format
  zlib: wrap remaining calls to direct inflate/inflateEnd
  zlib wrapper: refactor error message formatter
This commit is contained in:
Junio C Hamano 2011-08-16 11:23:26 -07:00
commit a35d78c0f4
15 changed files with 328 additions and 131 deletions

View File

@ -90,14 +90,14 @@ static void copy_le32(unsigned char *dest, unsigned int n)
static void *zlib_deflate(void *data, unsigned long size, static void *zlib_deflate(void *data, unsigned long size,
int compression_level, unsigned long *compressed_size) int compression_level, unsigned long *compressed_size)
{ {
z_stream stream; git_zstream stream;
unsigned long maxsize; unsigned long maxsize;
void *buffer; void *buffer;
int result; int result;
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
deflateInit(&stream, compression_level); git_deflate_init(&stream, compression_level);
maxsize = deflateBound(&stream, size); maxsize = git_deflate_bound(&stream, size);
buffer = xmalloc(maxsize); buffer = xmalloc(maxsize);
stream.next_in = data; stream.next_in = data;
@ -106,7 +106,7 @@ static void *zlib_deflate(void *data, unsigned long size,
stream.avail_out = maxsize; stream.avail_out = maxsize;
do { do {
result = deflate(&stream, Z_FINISH); result = git_deflate(&stream, Z_FINISH);
} while (result == Z_OK); } while (result == Z_OK);
if (result != Z_STREAM_END) { if (result != Z_STREAM_END) {
@ -114,7 +114,7 @@ static void *zlib_deflate(void *data, unsigned long size,
return NULL; return NULL;
} }
deflateEnd(&stream); git_deflate_end(&stream);
*compressed_size = stream.total_out; *compressed_size = stream.total_out;
return buffer; return buffer;

View File

@ -1634,7 +1634,7 @@ static inline int metadata_changes(struct patch *patch)
static char *inflate_it(const void *data, unsigned long size, static char *inflate_it(const void *data, unsigned long size,
unsigned long inflated_size) unsigned long inflated_size)
{ {
z_stream stream; git_zstream stream;
void *out; void *out;
int st; int st;

View File

@ -265,7 +265,7 @@ static void unlink_base_data(struct base_data *c)
static void *unpack_entry_data(unsigned long offset, unsigned long size) static void *unpack_entry_data(unsigned long offset, unsigned long size)
{ {
int status; int status;
z_stream stream; git_zstream stream;
void *buf = xmalloc(size); void *buf = xmalloc(size);
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
@ -355,7 +355,7 @@ static void *get_data_from_pack(struct object_entry *obj)
off_t from = obj[0].idx.offset + obj[0].hdr_size; off_t from = obj[0].idx.offset + obj[0].hdr_size;
unsigned long len = obj[1].idx.offset - from; unsigned long len = obj[1].idx.offset - from;
unsigned char *data, *inbuf; unsigned char *data, *inbuf;
z_stream stream; git_zstream stream;
int status; int status;
data = xmalloc(obj->size); data = xmalloc(obj->size);
@ -666,26 +666,26 @@ static void parse_pack_objects(unsigned char *sha1)
static int write_compressed(struct sha1file *f, void *in, unsigned int size) static int write_compressed(struct sha1file *f, void *in, unsigned int size)
{ {
z_stream stream; git_zstream stream;
int status; int status;
unsigned char outbuf[4096]; unsigned char outbuf[4096];
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
deflateInit(&stream, zlib_compression_level); git_deflate_init(&stream, zlib_compression_level);
stream.next_in = in; stream.next_in = in;
stream.avail_in = size; stream.avail_in = size;
do { do {
stream.next_out = outbuf; stream.next_out = outbuf;
stream.avail_out = sizeof(outbuf); stream.avail_out = sizeof(outbuf);
status = deflate(&stream, Z_FINISH); status = git_deflate(&stream, Z_FINISH);
sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out); sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out);
} while (status == Z_OK); } while (status == Z_OK);
if (status != Z_STREAM_END) if (status != Z_STREAM_END)
die("unable to deflate appended object (%d)", status); die("unable to deflate appended object (%d)", status);
size = stream.total_out; size = stream.total_out;
deflateEnd(&stream); git_deflate_end(&stream);
return size; return size;
} }

View File

@ -126,13 +126,13 @@ static void *get_delta(struct object_entry *entry)
static unsigned long do_compress(void **pptr, unsigned long size) static unsigned long do_compress(void **pptr, unsigned long size)
{ {
z_stream stream; git_zstream stream;
void *in, *out; void *in, *out;
unsigned long maxsize; unsigned long maxsize;
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
deflateInit(&stream, pack_compression_level); git_deflate_init(&stream, pack_compression_level);
maxsize = deflateBound(&stream, size); maxsize = git_deflate_bound(&stream, size);
in = *pptr; in = *pptr;
out = xmalloc(maxsize); out = xmalloc(maxsize);
@ -142,9 +142,9 @@ static unsigned long do_compress(void **pptr, unsigned long size)
stream.avail_in = size; stream.avail_in = size;
stream.next_out = out; stream.next_out = out;
stream.avail_out = maxsize; stream.avail_out = maxsize;
while (deflate(&stream, Z_FINISH) == Z_OK) while (git_deflate(&stream, Z_FINISH) == Z_OK)
; /* nothing */ ; /* nothing */
deflateEnd(&stream); git_deflate_end(&stream);
free(in); free(in);
return stream.total_out; return stream.total_out;
@ -160,7 +160,7 @@ static int check_pack_inflate(struct packed_git *p,
off_t len, off_t len,
unsigned long expect) unsigned long expect)
{ {
z_stream stream; git_zstream stream;
unsigned char fakebuf[4096], *in; unsigned char fakebuf[4096], *in;
int st; int st;
@ -187,12 +187,12 @@ static void copy_pack_data(struct sha1file *f,
off_t len) off_t len)
{ {
unsigned char *in; unsigned char *in;
unsigned int avail; unsigned long avail;
while (len) { while (len) {
in = use_pack(p, w_curs, offset, &avail); in = use_pack(p, w_curs, offset, &avail);
if (avail > len) if (avail > len)
avail = (unsigned int)len; avail = (unsigned long)len;
sha1write(f, in, avail); sha1write(f, in, avail);
offset += avail; offset += avail;
len -= avail; len -= avail;
@ -994,7 +994,7 @@ static void check_object(struct object_entry *entry)
const unsigned char *base_ref = NULL; const unsigned char *base_ref = NULL;
struct object_entry *base_entry; struct object_entry *base_entry;
unsigned long used, used_0; unsigned long used, used_0;
unsigned int avail; unsigned long avail;
off_t ofs; off_t ofs;
unsigned char *buf, c; unsigned char *buf, c;

View File

@ -90,7 +90,7 @@ static void use(int bytes)
static void *get_data(unsigned long size) static void *get_data(unsigned long size)
{ {
z_stream stream; git_zstream stream;
void *buf = xmalloc(size); void *buf = xmalloc(size);
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));

28
cache.h
View File

@ -16,13 +16,27 @@
#endif #endif
#include <zlib.h> #include <zlib.h>
#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200 typedef struct git_zstream {
#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11) z_stream z;
#endif 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(z_streamp strm); void git_inflate_init(git_zstream *);
void git_inflate_end(z_streamp strm); void git_inflate_init_gzip_only(git_zstream *);
int git_inflate(z_streamp strm, int flush); 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) #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
#define DTYPE(de) ((de)->d_type) #define DTYPE(de) ((de)->d_type)
@ -1009,7 +1023,7 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
extern void pack_report(void); extern void pack_report(void);
extern int open_pack_index(struct packed_git *); extern int open_pack_index(struct packed_git *);
extern void close_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 close_pack_windows(struct packed_git *);
extern void unuse_pack(struct pack_window **); extern void unuse_pack(struct pack_window **);
extern void free_pack_by_name(const char *); extern void free_pack_by_name(const char *);

10
diff.c
View File

@ -1839,20 +1839,20 @@ static unsigned char *deflate_it(char *data,
{ {
int bound; int bound;
unsigned char *deflated; unsigned char *deflated;
z_stream stream; git_zstream stream;
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
deflateInit(&stream, zlib_compression_level); git_deflate_init(&stream, zlib_compression_level);
bound = deflateBound(&stream, size); bound = git_deflate_bound(&stream, size);
deflated = xmalloc(bound); deflated = xmalloc(bound);
stream.next_out = deflated; stream.next_out = deflated;
stream.avail_out = bound; stream.avail_out = bound;
stream.next_in = (unsigned char *)data; stream.next_in = (unsigned char *)data;
stream.avail_in = size; stream.avail_in = size;
while (deflate(&stream, Z_FINISH) == Z_OK) while (git_deflate(&stream, Z_FINISH) == Z_OK)
; /* nothing */ ; /* nothing */
deflateEnd(&stream); git_deflate_end(&stream);
*result_size = stream.total_out; *result_size = stream.total_out;
return deflated; return deflated;
} }

View File

@ -1017,7 +1017,7 @@ static int store_object(
unsigned char sha1[20]; unsigned char sha1[20];
unsigned long hdrlen, deltalen; unsigned long hdrlen, deltalen;
git_SHA_CTX c; git_SHA_CTX c;
z_stream s; git_zstream s;
hdrlen = sprintf((char *)hdr,"%s %lu", typename(type), hdrlen = sprintf((char *)hdr,"%s %lu", typename(type),
(unsigned long)dat->len) + 1; (unsigned long)dat->len) + 1;
@ -1050,7 +1050,7 @@ static int store_object(
delta = NULL; delta = NULL;
memset(&s, 0, sizeof(s)); memset(&s, 0, sizeof(s));
deflateInit(&s, pack_compression_level); git_deflate_init(&s, pack_compression_level);
if (delta) { if (delta) {
s.next_in = delta; s.next_in = delta;
s.avail_in = deltalen; s.avail_in = deltalen;
@ -1058,11 +1058,11 @@ static int store_object(
s.next_in = (void *)dat->buf; s.next_in = (void *)dat->buf;
s.avail_in = dat->len; s.avail_in = dat->len;
} }
s.avail_out = deflateBound(&s, s.avail_in); s.avail_out = git_deflate_bound(&s, s.avail_in);
s.next_out = out = xmalloc(s.avail_out); s.next_out = out = xmalloc(s.avail_out);
while (deflate(&s, Z_FINISH) == Z_OK) while (git_deflate(&s, Z_FINISH) == Z_OK)
/* nothing */; ; /* nothing */
deflateEnd(&s); git_deflate_end(&s);
/* Determine if we should auto-checkpoint. */ /* Determine if we should auto-checkpoint. */
if ((max_packsize && (pack_size + 60 + s.total_out) > max_packsize) if ((max_packsize && (pack_size + 60 + s.total_out) > max_packsize)
@ -1078,14 +1078,14 @@ static int store_object(
delta = NULL; delta = NULL;
memset(&s, 0, sizeof(s)); memset(&s, 0, sizeof(s));
deflateInit(&s, pack_compression_level); git_deflate_init(&s, pack_compression_level);
s.next_in = (void *)dat->buf; s.next_in = (void *)dat->buf;
s.avail_in = dat->len; s.avail_in = dat->len;
s.avail_out = deflateBound(&s, s.avail_in); s.avail_out = git_deflate_bound(&s, s.avail_in);
s.next_out = out = xrealloc(out, s.avail_out); s.next_out = out = xrealloc(out, s.avail_out);
while (deflate(&s, Z_FINISH) == Z_OK) while (git_deflate(&s, Z_FINISH) == Z_OK)
/* nothing */; ; /* nothing */
deflateEnd(&s); git_deflate_end(&s);
} }
} }
@ -1163,7 +1163,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
off_t offset; off_t offset;
git_SHA_CTX c; git_SHA_CTX c;
git_SHA_CTX pack_file_ctx; git_SHA_CTX pack_file_ctx;
z_stream s; git_zstream s;
int status = Z_OK; int status = Z_OK;
/* Determine if we should auto-checkpoint. */ /* Determine if we should auto-checkpoint. */
@ -1187,7 +1187,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
crc32_begin(pack_file); crc32_begin(pack_file);
memset(&s, 0, sizeof(s)); memset(&s, 0, sizeof(s));
deflateInit(&s, pack_compression_level); git_deflate_init(&s, pack_compression_level);
hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf); hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
if (out_sz <= hdrlen) if (out_sz <= hdrlen)
@ -1209,7 +1209,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
len -= n; len -= n;
} }
status = deflate(&s, len ? 0 : Z_FINISH); status = git_deflate(&s, len ? 0 : Z_FINISH);
if (!s.avail_out || status == Z_STREAM_END) { if (!s.avail_out || status == Z_STREAM_END) {
size_t n = s.next_out - out_buf; size_t n = s.next_out - out_buf;
@ -1228,7 +1228,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
die("unexpected deflate failure: %d", status); die("unexpected deflate failure: %d", status);
} }
} }
deflateEnd(&s); git_deflate_end(&s);
git_SHA1_Final(sha1, &c); git_SHA1_Final(sha1, &c);
if (sha1out) if (sha1out)

View File

@ -271,16 +271,13 @@ static struct rpc_service *select_service(const char *name)
static void inflate_request(const char *prog_name, int out) static void inflate_request(const char *prog_name, int out)
{ {
z_stream stream; git_zstream stream;
unsigned char in_buf[8192]; unsigned char in_buf[8192];
unsigned char out_buf[8192]; unsigned char out_buf[8192];
unsigned long cnt = 0; unsigned long cnt = 0;
int ret;
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
ret = inflateInit2(&stream, (15 + 16)); git_inflate_init_gzip_only(&stream);
if (ret != Z_OK)
die("cannot start zlib inflater, zlib err %d", ret);
while (1) { while (1) {
ssize_t n = xread(0, in_buf, sizeof(in_buf)); ssize_t n = xread(0, in_buf, sizeof(in_buf));
@ -296,7 +293,7 @@ static void inflate_request(const char *prog_name, int out)
stream.next_out = out_buf; stream.next_out = out_buf;
stream.avail_out = sizeof(out_buf); stream.avail_out = sizeof(out_buf);
ret = inflate(&stream, Z_NO_FLUSH); ret = git_inflate(&stream, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) if (ret != Z_OK && ret != Z_STREAM_END)
die("zlib error inflating request, result %d", ret); die("zlib error inflating request, result %d", ret);
@ -311,7 +308,7 @@ static void inflate_request(const char *prog_name, int out)
} }
done: done:
inflateEnd(&stream); git_inflate_end(&stream);
close(out); close(out);
} }

View File

@ -377,15 +377,15 @@ static void start_put(struct transfer_request *request)
unsigned long len; unsigned long len;
int hdrlen; int hdrlen;
ssize_t size; ssize_t size;
z_stream stream; git_zstream stream;
unpacked = read_sha1_file(request->obj->sha1, &type, &len); unpacked = read_sha1_file(request->obj->sha1, &type, &len);
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1; hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
/* Set it up */ /* Set it up */
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
deflateInit(&stream, zlib_compression_level); git_deflate_init(&stream, zlib_compression_level);
size = deflateBound(&stream, len + hdrlen); size = git_deflate_bound(&stream, len + hdrlen);
strbuf_init(&request->buffer.buf, size); strbuf_init(&request->buffer.buf, size);
request->buffer.posn = 0; request->buffer.posn = 0;
@ -396,15 +396,15 @@ static void start_put(struct transfer_request *request)
/* First header.. */ /* First header.. */
stream.next_in = (void *)hdr; stream.next_in = (void *)hdr;
stream.avail_in = hdrlen; stream.avail_in = hdrlen;
while (deflate(&stream, 0) == Z_OK) while (git_deflate(&stream, 0) == Z_OK)
/* nothing */; ; /* nothing */
/* Then the data itself.. */ /* Then the data itself.. */
stream.next_in = unpacked; stream.next_in = unpacked;
stream.avail_in = len; stream.avail_in = len;
while (deflate(&stream, Z_FINISH) == Z_OK) while (git_deflate(&stream, Z_FINISH) == Z_OK)
/* nothing */; ; /* nothing */
deflateEnd(&stream); git_deflate_end(&stream);
free(unpacked); free(unpacked);
request->buffer.buf.len = stream.total_out; request->buffer.buf.len = stream.total_out;

2
http.h
View File

@ -172,7 +172,7 @@ struct http_object_request {
unsigned char sha1[20]; unsigned char sha1[20];
unsigned char real_sha1[20]; unsigned char real_sha1[20];
git_SHA_CTX c; git_SHA_CTX c;
z_stream stream; git_zstream stream;
int zret; int zret;
int rename; int rename;
struct active_request_slot *slot; struct active_request_slot *slot;

View File

@ -26,7 +26,7 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
uint32_t data_crc = crc32(0, NULL, 0); uint32_t data_crc = crc32(0, NULL, 0);
do { do {
unsigned int avail; unsigned long avail;
void *data = use_pack(p, w_curs, offset, &avail); void *data = use_pack(p, w_curs, offset, &avail);
if (avail > len) if (avail > len)
avail = len; avail = len;
@ -61,7 +61,7 @@ static int verify_packfile(struct packed_git *p,
git_SHA1_Init(&ctx); git_SHA1_Init(&ctx);
do { do {
unsigned int remaining; unsigned long remaining;
unsigned char *in = use_pack(p, w_curs, offset, &remaining); unsigned char *in = use_pack(p, w_curs, offset, &remaining);
offset += remaining; offset += remaining;
if (!pack_sig_ofs) if (!pack_sig_ofs)

View File

@ -473,16 +473,12 @@ static int post_rpc(struct rpc_state *rpc)
* the transfer time. * the transfer time.
*/ */
size_t size; size_t size;
z_stream stream; git_zstream stream;
int ret; int ret;
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
ret = deflateInit2(&stream, Z_BEST_COMPRESSION, git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
Z_DEFLATED, (15 + 16), size = git_deflate_bound(&stream, rpc->len);
8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
die("cannot deflate request; zlib init error %d", ret);
size = deflateBound(&stream, rpc->len);
gzip_body = xmalloc(size); gzip_body = xmalloc(size);
stream.next_in = (unsigned char *)rpc->buf; stream.next_in = (unsigned char *)rpc->buf;
@ -490,11 +486,11 @@ static int post_rpc(struct rpc_state *rpc)
stream.next_out = (unsigned char *)gzip_body; stream.next_out = (unsigned char *)gzip_body;
stream.avail_out = size; stream.avail_out = size;
ret = deflate(&stream, Z_FINISH); ret = git_deflate(&stream, Z_FINISH);
if (ret != Z_STREAM_END) if (ret != Z_STREAM_END)
die("cannot deflate request; zlib deflate error %d", ret); die("cannot deflate request; zlib deflate error %d", ret);
ret = deflateEnd(&stream); ret = git_deflate_end_gently(&stream);
if (ret != Z_OK) if (ret != Z_OK)
die("cannot deflate request; zlib end error %d", ret); die("cannot deflate request; zlib end error %d", ret);

View File

@ -834,7 +834,7 @@ static int in_window(struct pack_window *win, off_t offset)
unsigned char *use_pack(struct packed_git *p, unsigned char *use_pack(struct packed_git *p,
struct pack_window **w_cursor, struct pack_window **w_cursor,
off_t offset, off_t offset,
unsigned int *left) unsigned long *left)
{ {
struct pack_window *win = *w_cursor; struct pack_window *win = *w_cursor;
@ -1254,7 +1254,7 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
return used; 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; unsigned long size, used;
static const char valid_loose_object_type[8] = { static const char valid_loose_object_type[8] = {
@ -1296,7 +1296,7 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
return git_inflate(stream, 0); return git_inflate(stream, 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; int bytes = strlen(buffer) + 1;
unsigned char *buf = xmallocz(size); unsigned char *buf = xmallocz(size);
@ -1395,7 +1395,7 @@ static int parse_sha1_header(const char *hdr, unsigned long *sizep)
static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1) static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1)
{ {
int ret; int ret;
z_stream stream; git_zstream stream;
char hdr[8192]; char hdr[8192];
ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)); ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
@ -1411,7 +1411,7 @@ unsigned long get_size_from_delta(struct packed_git *p,
{ {
const unsigned char *data; const unsigned char *data;
unsigned char delta_head[20], *in; unsigned char delta_head[20], *in;
z_stream stream; git_zstream stream;
int st; int st;
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
@ -1533,7 +1533,7 @@ static int unpack_object_header(struct packed_git *p,
unsigned long *sizep) unsigned long *sizep)
{ {
unsigned char *base; unsigned char *base;
unsigned int left; unsigned long left;
unsigned long used; unsigned long used;
enum object_type type; enum object_type type;
@ -1646,7 +1646,7 @@ static void *unpack_compressed_entry(struct packed_git *p,
unsigned long size) unsigned long size)
{ {
int st; int st;
z_stream stream; git_zstream stream;
unsigned char *buffer, *in; unsigned char *buffer, *in;
buffer = xmallocz(size); buffer = xmallocz(size);
@ -2079,7 +2079,7 @@ static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *size
int status; int status;
unsigned long mapsize, size; unsigned long mapsize, size;
void *map; void *map;
z_stream stream; git_zstream stream;
char hdr[32]; char hdr[32];
map = map_sha1_file(sha1, &mapsize); map = map_sha1_file(sha1, &mapsize);
@ -2428,7 +2428,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
{ {
int fd, ret; int fd, ret;
unsigned char compressed[4096]; unsigned char compressed[4096];
z_stream stream; git_zstream stream;
git_SHA_CTX c; git_SHA_CTX c;
unsigned char parano_sha1[20]; unsigned char parano_sha1[20];
char *filename; char *filename;
@ -2445,7 +2445,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
/* Set it up */ /* Set it up */
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
deflateInit(&stream, zlib_compression_level); git_deflate_init(&stream, zlib_compression_level);
stream.next_out = compressed; stream.next_out = compressed;
stream.avail_out = sizeof(compressed); stream.avail_out = sizeof(compressed);
git_SHA1_Init(&c); git_SHA1_Init(&c);
@ -2453,8 +2453,8 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
/* First header.. */ /* First header.. */
stream.next_in = (unsigned char *)hdr; stream.next_in = (unsigned char *)hdr;
stream.avail_in = hdrlen; stream.avail_in = hdrlen;
while (deflate(&stream, 0) == Z_OK) while (git_deflate(&stream, 0) == Z_OK)
/* nothing */; ; /* nothing */
git_SHA1_Update(&c, hdr, hdrlen); git_SHA1_Update(&c, hdr, hdrlen);
/* Then the data itself.. */ /* Then the data itself.. */
@ -2462,7 +2462,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
stream.avail_in = len; stream.avail_in = len;
do { do {
unsigned char *in0 = stream.next_in; unsigned char *in0 = stream.next_in;
ret = deflate(&stream, Z_FINISH); ret = git_deflate(&stream, Z_FINISH);
git_SHA1_Update(&c, in0, stream.next_in - in0); git_SHA1_Update(&c, in0, stream.next_in - in0);
if (write_buffer(fd, compressed, stream.next_out - compressed) < 0) if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
die("unable to write sha1 file"); die("unable to write sha1 file");
@ -2472,7 +2472,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
if (ret != Z_STREAM_END) if (ret != Z_STREAM_END)
die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret); die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret);
ret = deflateEnd(&stream); ret = git_deflate_end_gently(&stream);
if (ret != Z_OK) if (ret != Z_OK)
die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret); die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret);
git_SHA1_Final(parano_sha1, &c); git_SHA1_Final(parano_sha1, &c);

272
zlib.c
View File

@ -4,58 +4,248 @@
*/ */
#include "cache.h" #include "cache.h"
void git_inflate_init(z_streamp strm) static const char *zerr_to_string(int status)
{ {
const char *err; switch (status) {
switch (inflateInit(strm)) {
case Z_OK:
return;
case Z_MEM_ERROR: case Z_MEM_ERROR:
err = "out of memory"; return "out of memory";
break;
case Z_VERSION_ERROR: case Z_VERSION_ERROR:
err = "wrong version"; return "wrong version";
break;
default:
err = "error";
}
die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
}
void git_inflate_end(z_streamp strm)
{
if (inflateEnd(strm) != Z_OK)
error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
}
int git_inflate(z_streamp strm, int flush)
{
int ret = inflate(strm, flush);
const char *err;
switch (ret) {
/* Out of memory is fatal. */
case Z_MEM_ERROR:
die("inflate: out of memory");
/* Data corruption errors: we may want to recover from them (fsck) */
case Z_NEED_DICT: case Z_NEED_DICT:
err = "needs dictionary"; break; return "needs dictionary";
case Z_DATA_ERROR: case Z_DATA_ERROR:
err = "data stream error"; break; return "data stream error";
case Z_STREAM_ERROR: case Z_STREAM_ERROR:
err = "stream consistency error"; break; return "stream consistency error";
default: default:
err = "unknown error"; break; return "unknown error";
}
}
/*
* 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) */
#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
static inline uInt zlib_buf_cap(unsigned long len)
{
return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : 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)
{
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;
s->next_in = s->z.next_in;
s->next_out = s->z.next_out;
s->avail_in -= bytes_consumed;
s->avail_out -= bytes_produced;
}
void git_inflate_init(git_zstream *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->z.msg ? strm->z.msg : "no message");
}
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;
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->z.msg ? strm->z.msg : "no message");
}
void git_inflate_end(git_zstream *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->z.msg ? strm->z.msg : "no message");
}
int git_inflate(git_zstream *strm, int flush)
{
int status;
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;
}
switch (status) {
/* Z_BUF_ERROR: normal, needs more space in the output buffer */ /* Z_BUF_ERROR: normal, needs more space in the output buffer */
case Z_BUF_ERROR: case Z_BUF_ERROR:
case Z_OK: case Z_OK:
case Z_STREAM_END: case Z_STREAM_END:
return ret; return status;
default:
break;
} }
error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message"); error("inflate: %s (%s)", zerr_to_string(status),
return ret; strm->z.msg ? strm->z.msg : "no message");
return status;
}
#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
#endif
unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
{
return deflateBound(&strm->z, size);
}
void git_deflate_init(git_zstream *strm, int 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->z.msg ? strm->z.msg : "no message");
}
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;
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->z.msg ? strm->z.msg : "no message");
}
void git_deflate_end(git_zstream *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->z.msg ? strm->z.msg : "no message");
}
int git_deflate_end_gently(git_zstream *strm)
{
int status;
zlib_pre_call(strm);
status = deflateEnd(&strm->z);
zlib_post_call(strm);
return status;
}
int git_deflate(git_zstream *strm, int flush)
{
int status;
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;
}
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),
strm->z.msg ? strm->z.msg : "no message");
return status;
} }