From 961c04d778dc06c5f6c7920397a266c66583d21f Mon Sep 17 00:00:00 2001 From: Dave Vasilevsky Date: Sat, 19 Dec 2009 23:19:55 -0500 Subject: [PATCH] Build encoder separately --- xz.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/xz.c b/xz.c index e42f49e..4cbb64f 100644 --- a/xz.c +++ b/xz.c @@ -3,29 +3,31 @@ #include "lzma.h" -#define CHUNK (1024 * 1024) + +lzma_ret setup_encoder(lzma_stream *stream); int main(void) { - lzma_stream stream = LZMA_STREAM_INIT; + size_t chunk = 1024 * 1024; - lzma_ret err = lzma_easy_encoder(&stream, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC32); + lzma_stream stream = LZMA_STREAM_INIT; + lzma_ret err = setup_encoder(&stream); if (LZMA_OK != err) { fprintf(stderr, "Can't initialize encoder, error #%d.\n", err); exit(1); } - uint8_t inbuf[CHUNK], outbuf[CHUNK]; + uint8_t inbuf[chunk], outbuf[chunk]; stream.avail_in = 0; size_t io; do { stream.next_out = outbuf; - stream.avail_out = CHUNK; + stream.avail_out = chunk; lzma_action action; if (stream.avail_in == 0) { - io = fread(inbuf, 1, CHUNK, stdin); - if (ferror(stdin) || (!feof(stdin) && CHUNK != io)) { + io = fread(inbuf, 1, chunk, stdin); + if (ferror(stdin) || (!feof(stdin) && chunk != io)) { fprintf(stderr, "Read error\n"); exit(1); } @@ -40,7 +42,7 @@ int main(void) { exit(1); } - size_t write_bytes = CHUNK - stream.avail_out; + size_t write_bytes = chunk - stream.avail_out; io = fwrite(outbuf, 1, write_bytes, stdout); if (io != write_bytes) { fprintf(stderr, "Write error\n"); @@ -50,3 +52,15 @@ int main(void) { return 0; } + +lzma_ret setup_encoder(lzma_stream *stream) { + lzma_options_lzma *opts = malloc(sizeof(lzma_options_lzma)); + lzma_lzma_preset(opts, LZMA_PRESET_DEFAULT); + + lzma_filter *filters = malloc(2 * sizeof(lzma_filter)); + filters[0].id = LZMA_FILTER_LZMA2; + filters[0].options = opts; + filters[1].id = LZMA_VLI_UNKNOWN; + + return lzma_stream_encoder(stream, filters, LZMA_CHECK_CRC32); +}