#ifndef HAVE_BZLIB
# define i_stream_create_bz2 NULL
-# define o_stream_create_bz2 NULL
# define o_stream_create_bz2_auto NULL
-# define compression_get_min_level_bz2 NULL
-# define compression_get_default_level_bz2 NULL
-# define compression_get_max_level_bz2 NULL
#endif
#ifndef HAVE_LZ4
# define i_stream_create_lz4 NULL
-# define o_stream_create_lz4 NULL
# define o_stream_create_lz4_auto NULL
-# define compression_get_min_level_lz4 NULL
-# define compression_get_default_level_lz4 NULL
-# define compression_get_max_level_lz4 NULL
#endif
#ifndef HAVE_ZSTD
# define i_stream_create_zstd NULL
-# define o_stream_create_zstd NULL
# define o_stream_create_zstd_auto NULL
-# define compression_get_min_level_zstd NULL
-# define compression_get_default_level_zstd NULL
-# define compression_get_max_level_zstd NULL
#endif
static bool is_compressed_zlib(struct istream *input)
for (i = 0; compression_handlers[i].name != NULL; i++) {
if (strcmp(name, compression_handlers[i].name) == 0) {
if (compression_handlers[i].create_istream == NULL ||
- compression_handlers[i].create_ostream == NULL) {
+ compression_handlers[i].create_ostream_auto == NULL) {
/* Handler is known but not compiled in */
return 0;
}
if (path_len > len &&
strcmp(path + path_len - len, compression_handlers[i].ext) == 0) {
if (compression_handlers[i].create_istream == NULL ||
- compression_handlers[i].create_ostream == NULL) {
+ compression_handlers[i].create_ostream_auto == NULL) {
/* Handler is known but not compiled in */
return 0;
}
.ext = ".gz",
.is_compressed = is_compressed_zlib,
.create_istream = i_stream_create_gz,
- .create_ostream = o_stream_create_gz,
.create_ostream_auto = o_stream_create_gz_auto,
- .get_min_level = compression_get_min_level_gz,
- .get_default_level = compression_get_default_level_gz,
- .get_max_level = compression_get_max_level_gz,
},
{
.name = "bz2",
.ext = ".bz2",
.is_compressed = is_compressed_bzlib,
.create_istream = i_stream_create_bz2,
- .create_ostream = o_stream_create_bz2,
.create_ostream_auto = o_stream_create_bz2_auto,
- .get_min_level = compression_get_min_level_bz2,
- .get_default_level = compression_get_default_level_bz2,
- .get_max_level = compression_get_max_level_bz2,
},
{
.name = "deflate",
.ext = NULL,
.is_compressed = NULL,
.create_istream = i_stream_create_deflate,
- .create_ostream = o_stream_create_deflate,
.create_ostream_auto = o_stream_create_deflate_auto,
- .get_min_level = compression_get_min_level_gz,
- .get_default_level = compression_get_default_level_gz,
- .get_max_level = compression_get_max_level_gz,
},
{
.name = "lz4",
.ext = ".lz4",
.is_compressed = is_compressed_lz4,
.create_istream = i_stream_create_lz4,
- .create_ostream = o_stream_create_lz4,
.create_ostream_auto = o_stream_create_lz4_auto,
- .get_min_level = compression_get_min_level_lz4, /* does not actually support any of this */
- .get_default_level = compression_get_default_level_lz4,
- .get_max_level = compression_get_max_level_lz4,
},
{
.name = "zstd",
.ext = ".zstd",
.is_compressed = is_compressed_zstd,
.create_istream = i_stream_create_zstd,
- .create_ostream = o_stream_create_zstd,
.create_ostream_auto = o_stream_create_zstd_auto,
- .get_min_level = compression_get_min_level_zstd,
- .get_default_level = compression_get_default_level_zstd,
- .get_max_level = compression_get_max_level_zstd,
},
{
.name = "unsupported",
const char *ext;
bool (*is_compressed)(struct istream *input);
struct istream *(*create_istream)(struct istream *input);
- struct ostream *(*create_ostream)(struct ostream *output, int level);
struct ostream *(*create_ostream_auto)(struct ostream *output, struct event *event);
- /* returns minimum level */
- int (*get_min_level)(void);
- /* the default can be -1 (e.g. gz), so the return value of this has to
- be used as-is. */
- int (*get_default_level)(void);
- /* returns maximum level */
- int (*get_max_level)(void);
};
extern const struct compression_handler compression_handlers[];
return TRUE;
}
-/* in bzlib, level is actually block size. From bzlib manual:
-
- The block size affects both the compression ratio achieved,
- and the amount of memory needed for compression and decompression.
-
- BlockSize 1 through BlockSize 9 specify the block size to be 100,000 bytes
- through 900,000 bytes respectively. The default is to use the maximum block
- size.
-
- Larger block sizes give rapidly diminishing marginal returns.
- Most of the compression comes from the first two or three hundred k of
- block size, a fact worth bearing in mind when using bzip2 on small machines.
- It is also important to appreciate that the decompression memory
- requirement is set at compression time by the choice of block size.
-
- * In general, try and use the largest block size memory constraints
- allow, since that maximises the compression achieved.
- * Compression and decompression speed are virtually unaffected by block
- size.
-
- Another significant point applies to files which fit in a single block -
- that means most files you'd encounter using a large block size. The
- amount of real memory touched is proportional to the size of the file,
- since the file is smaller than a block. For example, compressing a file
- 20,000 bytes long with the flag BlockSize 9 will cause the compressor to
- allocate around 7600k of memory, but only touch 400k + 20000 * 8 = 560 kbytes
- of it. Similarly, the decompressor will allocate 3700k but only
- touch 100k + 20000 * 4 = 180 kbytes.
-*/
-
-int compression_get_min_level_bz2(void)
-{
- return 1;
-}
-
-int compression_get_default_level_bz2(void)
-{
- /* default is maximum level */
- return 9;
-}
-
-int compression_get_max_level_bz2(void)
-{
- return 9;
-}
-
static void o_stream_bzlib_close(struct iostream_private *stream,
bool close_parent)
{
return bytes;
}
-struct ostream *o_stream_create_bz2(struct ostream *output, int level)
+static struct ostream *o_stream_create_bz2(struct ostream *output, int level)
{
struct bzlib_ostream *zstream;
int ret;
unsigned int outbuf_offset, outbuf_used;
};
-/* There is no actual compression level in LZ4, so for legacy
- reasons we allow 1-9 to avoid breaking anyone's config. */
-int compression_get_min_level_lz4(void)
-{
- return 1;
-}
-
-int compression_get_default_level_lz4(void)
-{
- return 1;
-}
-
-int compression_get_max_level_lz4(void)
-{
- return 9;
-}
-
static void o_stream_lz4_close(struct iostream_private *stream,
bool close_parent)
{
return bytes;
}
-struct ostream *o_stream_create_lz4(struct ostream *output, int level)
+struct ostream *
+o_stream_create_lz4_auto(struct ostream *output,
+ struct event *event ATTR_UNUSED)
{
struct iostream_lz4_header *hdr;
struct lz4_ostream *zstream;
- i_assert(level >= 1 && level <= 9);
-
zstream = i_new(struct lz4_ostream, 1);
zstream->ostream.sendv = o_stream_lz4_sendv;
zstream->ostream.flush = o_stream_lz4_flush;
o_stream_get_fd(output));
}
-struct ostream *
-o_stream_create_lz4_auto(struct ostream *output,
- struct event *event ATTR_UNUSED)
-{
- return o_stream_create_lz4(output, 1);
-}
-
#endif
return TRUE;
}
-int compression_get_min_level_gz(void)
-{
- return Z_NO_COMPRESSION;
-}
-
-int compression_get_default_level_gz(void)
-{
- return Z_DEFAULT_COMPRESSION;
-}
-
-int compression_get_max_level_gz(void)
-{
- return Z_BEST_COMPRESSION;
-}
-
static void o_stream_zlib_close(struct iostream_private *stream,
bool close_parent)
{
o_stream_get_fd(output));
}
-struct ostream *o_stream_create_gz(struct ostream *output, int level)
-{
- return o_stream_create_zlib(output, level, TRUE);
-}
-
-struct ostream *o_stream_create_deflate(struct ostream *output, int level)
-{
- return o_stream_create_zlib(output, level, FALSE);
-}
-
static struct ostream *
o_stream_create_zlib_auto(struct ostream *output, struct event *event, bool gz)
{
#ifndef OSTREAM_ZLIB_H
#define OSTREAM_ZLIB_H
-struct ostream *o_stream_create_gz(struct ostream *output, int level);
-struct ostream *o_stream_create_deflate(struct ostream *output, int level);
-struct ostream *o_stream_create_bz2(struct ostream *output, int level);
-struct ostream *o_stream_create_lz4(struct ostream *output, int level);
-struct ostream *o_stream_create_zstd(struct ostream *output, int level);
-
struct ostream *o_stream_create_gz_auto(struct ostream *output, struct event *event);
struct ostream *o_stream_create_deflate_auto(struct ostream *output, struct event *event);
struct ostream *o_stream_create_bz2_auto(struct ostream *output, struct event *event);
struct ostream *o_stream_create_lz4_auto(struct ostream *output, struct event *event);
struct ostream *o_stream_create_zstd_auto(struct ostream *output, struct event *event);
-int compression_get_min_level_gz(void);
-int compression_get_default_level_gz(void);
-int compression_get_max_level_gz(void);
-
-int compression_get_min_level_bz2(void);
-int compression_get_default_level_bz2(void);
-int compression_get_max_level_bz2(void);
-
-int compression_get_min_level_lz4(void);
-int compression_get_default_level_lz4(void);
-int compression_get_max_level_lz4(void);
-
-int compression_get_min_level_zstd(void);
-int compression_get_default_level_zstd(void);
-int compression_get_max_level_zstd(void);
-
#endif
return TRUE;
}
-int compression_get_min_level_zstd(void)
-{
- return ZSTD_minCLevel();
-}
-
-int compression_get_default_level_zstd(void)
-{
-#ifdef ZSTD_CLEVEL_DEFAULT
- return ZSTD_CLEVEL_DEFAULT;
-#else
- /* Documentation says 3 is default */
- return 3;
-#endif
-}
-
-int compression_get_max_level_zstd(void)
-{
- return ZSTD_maxCLevel();
-}
-
static void o_stream_zstd_write_error(struct zstd_ostream *zstream, size_t err)
{
ZSTD_ErrorCode errcode = zstd_version_errcode(ZSTD_getErrorCode(err));
o_stream_close(zstream->ostream.parent);
}
-struct ostream *
+static struct ostream *
o_stream_create_zstd(struct ostream *output, int level)
{
struct zstd_ostream *zstream;
size_t ret;
- i_assert(level >= compression_get_min_level_zstd() &&
- level <= compression_get_max_level_zstd());
+ i_assert(level >= ZSTD_minCLevel() && level <= ZSTD_maxCLevel());
zstd_version_check();