Skip to content

Commit

Permalink
archive-zip: use deflateInit2() to ask for raw compressed data
Browse files Browse the repository at this point in the history
We use the function git_deflate_init() -- which wraps the zlib function
deflateInit() -- to initialize compression of ZIP file entries.  This
results in compressed data prefixed with a two-bytes long header and
followed by a four-bytes trailer.  ZIP file entries consist of ZIP
headers and raw compressed data instead, so we remove the zlib wrapper
before writing the result.

We can ask zlib for the the raw compressed data without the unwanted
parts in the first place by using deflateInit2() and specifying a
negative number of bits to size the window.  For that purpose, factor
out the function do_git_deflate_init() and add git_deflate_init_raw(),
which wraps it.  Then use the latter in archive-zip.c and get rid of
the code that stripped the zlib header and trailer.

Also rename the helper function zlib_deflate() to zlib_deflate_raw()
to reflect the change.

Thus we avoid generating data that we throw away anyway, the code
becomes shorter and some magic constants are removed.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
René Scharfe authored and gitster committed Mar 17, 2013
1 parent d3c1472 commit c3c2e1a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
36 changes: 14 additions & 22 deletions archive-zip.c
Expand Up @@ -111,16 +111,17 @@ static void copy_le32(unsigned char *dest, unsigned int n)
dest[3] = 0xff & (n >> 030);
}

static void *zlib_deflate(void *data, unsigned long size,
int compression_level, unsigned long *compressed_size)
static void *zlib_deflate_raw(void *data, unsigned long size,
int compression_level,
unsigned long *compressed_size)
{
git_zstream stream;
unsigned long maxsize;
void *buffer;
int result;

memset(&stream, 0, sizeof(stream));
git_deflate_init(&stream, compression_level);
git_deflate_init_raw(&stream, compression_level);
maxsize = git_deflate_bound(&stream, size);
buffer = xmalloc(maxsize);

Expand Down Expand Up @@ -265,14 +266,11 @@ static int write_zip_entry(struct archiver_args *args,
}

if (buffer && method == 8) {
deflated = zlib_deflate(buffer, size, args->compression_level,
&compressed_size);
if (deflated && compressed_size - 6 < size) {
/* ZLIB --> raw compressed data (see RFC 1950) */
/* CMF and FLG ... */
out = (unsigned char *)deflated + 2;
compressed_size -= 6; /* ... and ADLER32 */
} else {
out = deflated = zlib_deflate_raw(buffer, size,
args->compression_level,
&compressed_size);
if (!out || compressed_size >= size) {
out = buffer;
method = 0;
compressed_size = size;
}
Expand Down Expand Up @@ -353,7 +351,7 @@ static int write_zip_entry(struct archiver_args *args,
unsigned char compressed[STREAM_BUFFER_SIZE * 2];

memset(&zstream, 0, sizeof(zstream));
git_deflate_init(&zstream, args->compression_level);
git_deflate_init_raw(&zstream, args->compression_level);

compressed_size = 0;
zstream.next_out = compressed;
Expand All @@ -370,13 +368,10 @@ static int write_zip_entry(struct archiver_args *args,
result = git_deflate(&zstream, 0);
if (result != Z_OK)
die("deflate error (%d)", result);
out = compressed;
if (!compressed_size)
out += 2;
out_len = zstream.next_out - out;
out_len = zstream.next_out - compressed;

if (out_len > 0) {
write_or_die(1, out, out_len);
write_or_die(1, compressed, out_len);
compressed_size += out_len;
zstream.next_out = compressed;
zstream.avail_out = sizeof(compressed);
Expand All @@ -394,11 +389,8 @@ static int write_zip_entry(struct archiver_args *args,
die("deflate error (%d)", result);

git_deflate_end(&zstream);
out = compressed;
if (!compressed_size)
out += 2;
out_len = zstream.next_out - out - 4;
write_or_die(1, out, out_len);
out_len = zstream.next_out - compressed;
write_or_die(1, compressed, out_len);
compressed_size += out_len;
zip_offset += compressed_size;

Expand Down
1 change: 1 addition & 0 deletions cache.h
Expand Up @@ -34,6 +34,7 @@ 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_init_raw(git_zstream *, int level);
void git_deflate_end(git_zstream *);
int git_deflate_abort(git_zstream *);
int git_deflate_end_gently(git_zstream *);
Expand Down
25 changes: 19 additions & 6 deletions zlib.c
Expand Up @@ -168,13 +168,8 @@ void git_deflate_init(git_zstream *strm, int level)
strm->z.msg ? strm->z.msg : "no message");
}

void git_deflate_init_gzip(git_zstream *strm, int level)
static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
{
/*
* 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);
Expand All @@ -188,6 +183,24 @@ void git_deflate_init_gzip(git_zstream *strm, int level)
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.
*/
return do_git_deflate_init(strm, level, 15 + 16);
}

void git_deflate_init_raw(git_zstream *strm, int level)
{
/*
* Use default 15 bits, negate the value to get raw compressed
* data without zlib header and trailer.
*/
return do_git_deflate_init(strm, level, -15);
}

int git_deflate_abort(git_zstream *strm)
{
int status;
Expand Down

0 comments on commit c3c2e1a

Please sign in to comment.