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; const char *name;
PKG_COMPRESSION id; 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_stream_t *(*uncompression_stream)(struct compressor_t *cmp);
} compressor_t; } compressor_t;

View file

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

View file

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

View file

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