mirror of
https://github.com/vasi/pixz
synced 2024-11-03 09:40:24 +00:00
Build encoder separately
This commit is contained in:
parent
59b14a5dbf
commit
961c04d778
30
xz.c
30
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user