summaryrefslogtreecommitdiff
path: root/Utilities/cmlibarchive/libarchive/archive_write_add_filter_gzip.c
diff options
context:
space:
mode:
Diffstat (limited to 'Utilities/cmlibarchive/libarchive/archive_write_add_filter_gzip.c')
-rw-r--r--Utilities/cmlibarchive/libarchive/archive_write_add_filter_gzip.c208
1 files changed, 147 insertions, 61 deletions
diff --git a/Utilities/cmlibarchive/libarchive/archive_write_add_filter_gzip.c b/Utilities/cmlibarchive/libarchive/archive_write_add_filter_gzip.c
index d08ee265b..db18fd94a 100644
--- a/Utilities/cmlibarchive/libarchive/archive_write_add_filter_gzip.c
+++ b/Utilities/cmlibarchive/libarchive/archive_write_add_filter_gzip.c
@@ -43,6 +43,7 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_gzip.c 201
#include "archive.h"
#include "archive_private.h"
+#include "archive_string.h"
#include "archive_write_private.h"
#if ARCHIVE_VERSION_NUMBER < 4000000
@@ -54,24 +55,20 @@ archive_write_set_compression_gzip(struct archive *a)
}
#endif
-#ifndef HAVE_ZLIB_H
-int
-archive_write_add_filter_gzip(struct archive *a)
-{
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "gzip compression not supported on this platform");
- return (ARCHIVE_FATAL);
-}
-#else
/* Don't compile this if we don't have zlib. */
struct private_data {
int compression_level;
+ int timestamp;
+#ifdef HAVE_ZLIB_H
z_stream stream;
int64_t total_in;
unsigned char *compressed;
size_t compressed_buffer_size;
unsigned long crc;
+#else
+ struct archive_write_program_data *pdata;
+#endif
};
/*
@@ -88,8 +85,10 @@ static int archive_compressor_gzip_write(struct archive_write_filter *,
const void *, size_t);
static int archive_compressor_gzip_close(struct archive_write_filter *);
static int archive_compressor_gzip_free(struct archive_write_filter *);
+#ifdef HAVE_ZLIB_H
static int drive_compressor(struct archive_write_filter *,
struct private_data *, int finishing);
+#endif
/*
@@ -110,16 +109,72 @@ archive_write_add_filter_gzip(struct archive *_a)
return (ARCHIVE_FATAL);
}
f->data = data;
- data->compression_level = Z_DEFAULT_COMPRESSION;
f->open = &archive_compressor_gzip_open;
f->options = &archive_compressor_gzip_options;
f->close = &archive_compressor_gzip_close;
f->free = &archive_compressor_gzip_free;
- f->code = ARCHIVE_COMPRESSION_GZIP;
+ f->code = ARCHIVE_FILTER_GZIP;
f->name = "gzip";
+#ifdef HAVE_ZLIB_H
+ data->compression_level = Z_DEFAULT_COMPRESSION;
return (ARCHIVE_OK);
+#else
+ data->pdata = __archive_write_program_allocate();
+ if (data->pdata == NULL) {
+ free(data);
+ archive_set_error(&a->archive, ENOMEM, "Out of memory");
+ return (ARCHIVE_FATAL);
+ }
+ data->compression_level = 0;
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+ "Using external gzip program");
+ return (ARCHIVE_WARN);
+#endif
}
+static int
+archive_compressor_gzip_free(struct archive_write_filter *f)
+{
+ struct private_data *data = (struct private_data *)f->data;
+
+#ifdef HAVE_ZLIB_H
+ free(data->compressed);
+#else
+ __archive_write_program_free(data->pdata);
+#endif
+ free(data);
+ f->data = NULL;
+ return (ARCHIVE_OK);
+}
+
+/*
+ * Set write options.
+ */
+static int
+archive_compressor_gzip_options(struct archive_write_filter *f, const char *key,
+ const char *value)
+{
+ struct private_data *data = (struct private_data *)f->data;
+
+ if (strcmp(key, "compression-level") == 0) {
+ if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
+ value[1] != '\0')
+ return (ARCHIVE_WARN);
+ data->compression_level = value[0] - '0';
+ return (ARCHIVE_OK);
+ }
+ if (strcmp(key, "timestamp") == 0) {
+ data->timestamp = (value == NULL)?-1:1;
+ return (ARCHIVE_OK);
+ }
+
+ /* Note: The "warn" return is just to inform the options
+ * supervisor that we didn't handle it. It will generate
+ * a suitable error if no one used this option. */
+ return (ARCHIVE_WARN);
+}
+
+#ifdef HAVE_ZLIB_H
/*
* Setup callback.
*/
@@ -128,14 +183,23 @@ archive_compressor_gzip_open(struct archive_write_filter *f)
{
struct private_data *data = (struct private_data *)f->data;
int ret;
- time_t t;
ret = __archive_write_open_filter(f->next_filter);
if (ret != ARCHIVE_OK)
return (ret);
if (data->compressed == NULL) {
- data->compressed_buffer_size = 65536;
+ size_t bs = 65536, bpb;
+ if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
+ /* Buffer size should be a multiple number of
+ * the of bytes per block for performance. */
+ bpb = archive_write_get_bytes_per_block(f->archive);
+ if (bpb > bs)
+ bs = bpb;
+ else if (bpb != 0)
+ bs -= bs % bpb;
+ }
+ data->compressed_buffer_size = bs;
data->compressed
= (unsigned char *)malloc(data->compressed_buffer_size);
if (data->compressed == NULL) {
@@ -147,18 +211,21 @@ archive_compressor_gzip_open(struct archive_write_filter *f)
data->crc = crc32(0L, NULL, 0);
data->stream.next_out = data->compressed;
- data->stream.avail_out = data->compressed_buffer_size;
+ data->stream.avail_out = (uInt)data->compressed_buffer_size;
/* Prime output buffer with a gzip header. */
- t = time(NULL);
data->compressed[0] = 0x1f; /* GZip signature bytes */
data->compressed[1] = 0x8b;
data->compressed[2] = 0x08; /* "Deflate" compression */
data->compressed[3] = 0; /* No options */
- data->compressed[4] = (t)&0xff; /* Timestamp */
- data->compressed[5] = (t>>8)&0xff;
- data->compressed[6] = (t>>16)&0xff;
- data->compressed[7] = (t>>24)&0xff;
+ if (data->timestamp >= 0) {
+ time_t t = time(NULL);
+ data->compressed[4] = (uint8_t)(t)&0xff; /* Timestamp */
+ data->compressed[5] = (uint8_t)(t>>8)&0xff;
+ data->compressed[6] = (uint8_t)(t>>16)&0xff;
+ data->compressed[7] = (uint8_t)(t>>24)&0xff;
+ } else
+ memset(&data->compressed[4], 0, 4);
data->compressed[8] = 0; /* No deflate options */
data->compressed[9] = 3; /* OS=Unix */
data->stream.next_out += 10;
@@ -191,8 +258,8 @@ archive_compressor_gzip_open(struct archive_write_filter *f)
"compression library: invalid setup parameter");
break;
case Z_MEM_ERROR:
- archive_set_error(f->archive, ENOMEM, "Internal error initializing "
- "compression library");
+ archive_set_error(f->archive, ENOMEM,
+ "Internal error initializing compression library");
break;
case Z_VERSION_ERROR:
archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
@@ -205,25 +272,6 @@ archive_compressor_gzip_open(struct archive_write_filter *f)
}
/*
- * Set write options.
- */
-static int
-archive_compressor_gzip_options(struct archive_write_filter *f, const char *key,
- const char *value)
-{
- struct private_data *data = (struct private_data *)f->data;
-
- if (strcmp(key, "compression-level") == 0) {
- if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
- value[1] != '\0')
- return (ARCHIVE_WARN);
- data->compression_level = value[0] - '0';
- return (ARCHIVE_OK);
- }
- return (ARCHIVE_WARN);
-}
-
-/*
* Write data to the compressed stream.
*/
static int
@@ -234,12 +282,12 @@ archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
int ret;
/* Update statistics */
- data->crc = crc32(data->crc, (const Bytef *)buff, length);
+ data->crc = crc32(data->crc, (const Bytef *)buff, (uInt)length);
data->total_in += length;
/* Compress input data to output buffer */
SET_NEXT_IN(data, buff);
- data->stream.avail_in = length;
+ data->stream.avail_in = (uInt)length;
if ((ret = drive_compressor(f, data, 0)) != ARCHIVE_OK)
return (ret);
@@ -266,14 +314,14 @@ archive_compressor_gzip_close(struct archive_write_filter *f)
}
if (ret == ARCHIVE_OK) {
/* Build and write out 8-byte trailer. */
- trailer[0] = (data->crc)&0xff;
- trailer[1] = (data->crc >> 8)&0xff;
- trailer[2] = (data->crc >> 16)&0xff;
- trailer[3] = (data->crc >> 24)&0xff;
- trailer[4] = (data->total_in)&0xff;
- trailer[5] = (data->total_in >> 8)&0xff;
- trailer[6] = (data->total_in >> 16)&0xff;
- trailer[7] = (data->total_in >> 24)&0xff;
+ trailer[0] = (uint8_t)(data->crc)&0xff;
+ trailer[1] = (uint8_t)(data->crc >> 8)&0xff;
+ trailer[2] = (uint8_t)(data->crc >> 16)&0xff;
+ trailer[3] = (uint8_t)(data->crc >> 24)&0xff;
+ trailer[4] = (uint8_t)(data->total_in)&0xff;
+ trailer[5] = (uint8_t)(data->total_in >> 8)&0xff;
+ trailer[6] = (uint8_t)(data->total_in >> 16)&0xff;
+ trailer[7] = (uint8_t)(data->total_in >> 24)&0xff;
ret = __archive_write_filter(f->next_filter, trailer, 8);
}
@@ -289,16 +337,6 @@ archive_compressor_gzip_close(struct archive_write_filter *f)
return (r1 < ret ? r1 : ret);
}
-static int
-archive_compressor_gzip_free(struct archive_write_filter *f)
-{
- struct private_data *data = (struct private_data *)f->data;
- free(data->compressed);
- free(data);
- f->data = NULL;
- return (ARCHIVE_OK);
-}
-
/*
* Utility function to push input data through compressor,
* writing full output blocks as necessary.
@@ -320,7 +358,8 @@ drive_compressor(struct archive_write_filter *f,
if (ret != ARCHIVE_OK)
return (ARCHIVE_FATAL);
data->stream.next_out = data->compressed;
- data->stream.avail_out = data->compressed_buffer_size;
+ data->stream.avail_out =
+ (uInt)data->compressed_buffer_size;
}
/* If there's nothing to do, we're done. */
@@ -353,4 +392,51 @@ drive_compressor(struct archive_write_filter *f,
}
}
+#else /* HAVE_ZLIB_H */
+
+static int
+archive_compressor_gzip_open(struct archive_write_filter *f)
+{
+ struct private_data *data = (struct private_data *)f->data;
+ struct archive_string as;
+ int r;
+
+ archive_string_init(&as);
+ archive_strcpy(&as, "gzip");
+
+ /* Specify compression level. */
+ if (data->compression_level > 0) {
+ archive_strcat(&as, " -");
+ archive_strappend_char(&as, '0' + data->compression_level);
+ }
+ if (data->timestamp < 0)
+ /* Do not save timestamp. */
+ archive_strcat(&as, " -n");
+ else if (data->timestamp > 0)
+ /* Save timestamp. */
+ archive_strcat(&as, " -N");
+
+ f->write = archive_compressor_gzip_write;
+ r = __archive_write_program_open(f, data->pdata, as.s);
+ archive_string_free(&as);
+ return (r);
+}
+
+static int
+archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
+ size_t length)
+{
+ struct private_data *data = (struct private_data *)f->data;
+
+ return __archive_write_program_write(f, data->pdata, buff, length);
+}
+
+static int
+archive_compressor_gzip_close(struct archive_write_filter *f)
+{
+ struct private_data *data = (struct private_data *)f->data;
+
+ return __archive_write_program_close(f, data->pdata);
+}
+
#endif /* HAVE_ZLIB_H */