1
0
Fork 0
mirror of https://github.com/pygos/pkg-utils.git synced 2024-11-21 20:39:46 +01:00

Add a way to pass options to compressors

Currently only used by lzma compressor to set the dictionary size.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2019-04-17 23:56:39 +02:00
parent e123eaf433
commit 214a5e917e
5 changed files with 28 additions and 13 deletions

View file

@ -28,7 +28,8 @@ typedef struct compressor_t {
const char *name;
PKG_COMPRESSION id;
compressor_stream_t *(*compression_stream)(struct compressor_t *cmp);
compressor_stream_t *(*compression_stream)(struct compressor_t *cmp,
void *options);
compressor_stream_t *(*uncompression_stream)(struct compressor_t *cmp);
} compressor_t;

View file

@ -17,6 +17,7 @@ typedef struct {
int used;
bool eof;
bool error;
size_t dict_size;
} lzma_stream_t;
static ssize_t lzma_write(compressor_stream_t *base,
@ -96,17 +97,20 @@ static ssize_t lzma_comp_block(compressor_stream_t *base,
const uint8_t *in, uint8_t *out,
size_t insize, size_t outsize)
{
lzma_stream_t *lzma = (lzma_stream_t *)base;
lzma_filter filters[5];
lzma_options_lzma opt;
size_t written = 0;
lzma_ret ret;
(void)base;
if (lzma_lzma_preset(&opt, LZMA_PRESET_DEFAULT)) {
fputs("error initializing LZMA options\n", stderr);
return -1;
}
if (lzma->dict_size)
opt.dict_size = lzma->dict_size;
filters[0].id = LZMA_FILTER_LZMA2;
filters[0].options = &opt;
@ -156,7 +160,7 @@ static void lzma_destroy(compressor_stream_t *base)
free(lzma);
}
static compressor_stream_t *create_stream(bool compress)
static compressor_stream_t *create_stream(bool compress, size_t dict_size)
{
lzma_stream_t *lzma = calloc(1, sizeof(*lzma));
compressor_stream_t *base;
@ -169,6 +173,7 @@ static compressor_stream_t *create_stream(bool compress)
}
lzma->action = LZMA_RUN;
lzma->dict_size = dict_size;
base = (compressor_stream_t *)lzma;
base->write = lzma_write;
@ -182,6 +187,9 @@ static compressor_stream_t *create_stream(bool compress)
if (lzma_lzma_preset(&opt_lzma2, LZMA_PRESET_DEFAULT))
goto fail;
if (dict_size)
opt_lzma2.dict_size = dict_size;
filters[0].id = LZMA_FILTER_LZMA2;
filters[0].options = &opt_lzma2;
@ -208,16 +216,16 @@ fail:
return NULL;
}
static compressor_stream_t *lzma_compress(compressor_t *cmp)
static compressor_stream_t *lzma_compress(compressor_t *cmp, void *options)
{
(void)cmp;
return create_stream(true);
return create_stream(true, options == NULL ? 0 : *((size_t *)options));
}
static compressor_stream_t *lzma_uncompress(compressor_t *cmp)
{
(void)cmp;
return create_stream(false);
return create_stream(false, 0);
}
compressor_t comp_lzma = {

View file

@ -75,11 +75,12 @@ static void dummy_destroy(compressor_stream_t *base)
free(base);
}
static compressor_stream_t *create_dummy_stream(compressor_t *cmp)
static compressor_stream_t *create_dummy_stream(compressor_t *cmp,
void *options)
{
dummy_stream_t *dummy = calloc(1, sizeof(*dummy));
compressor_stream_t *base;
(void)cmp;
(void)cmp; (void)options;
if (dummy == NULL) {
perror("creating dummy compressor stream");
@ -95,9 +96,14 @@ static compressor_stream_t *create_dummy_stream(compressor_t *cmp)
return base;
}
static compressor_stream_t *create_dummy_uncompressor(compressor_t *cmp)
{
return create_dummy_stream(cmp, NULL);
}
compressor_t comp_none = {
.name = "none",
.id = PKG_COMPRESSION_NONE,
.compression_stream = create_dummy_stream,
.uncompression_stream = create_dummy_stream,
.uncompression_stream = create_dummy_uncompressor,
};

View file

@ -188,9 +188,9 @@ static compressor_stream_t *create_stream(bool compress)
return base;
}
static compressor_stream_t *zlib_compress(compressor_t *cmp)
static compressor_stream_t *zlib_compress(compressor_t *cmp, void *options)
{
(void)cmp;
(void)cmp; (void)options;
return create_stream(true);
}

View file

@ -96,7 +96,7 @@ pkg_writer_t *pkg_writer_open(const char *path, bool force)
return NULL;
}
wr->stream = cmp->compression_stream(cmp);
wr->stream = cmp->compression_stream(cmp, NULL);
if (wr->stream == NULL) {
fputs("error creating compressor stream for package header\n",
stderr);
@ -153,7 +153,7 @@ int pkg_writer_start_record(pkg_writer_t *wr, uint32_t magic,
if (write_header(wr))
return -1;
wr->stream = cmp->compression_stream(cmp);
wr->stream = cmp->compression_stream(cmp, NULL);
if (wr->stream == NULL)
return -1;