|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h> |
|
#include <stdlib.h> |
|
#include <stdio.h> |
|
#include <string.h> |
|
#include <errno.h> |
|
#include <lzma.h> |
|
|
|
|
|
static bool |
|
init_encoder(lzma_stream *strm) |
|
{ |
|
|
|
|
|
lzma_mt mt = { |
|
|
|
.flags = 0, |
|
|
|
|
|
.block_size = 0, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.timeout = 0, |
|
|
|
|
|
|
|
.preset = LZMA_PRESET_DEFAULT, |
|
.filters = NULL, |
|
|
|
|
|
|
|
.check = LZMA_CHECK_CRC64, |
|
}; |
|
|
|
|
|
mt.threads = lzma_cputhreads(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mt.threads == 0) |
|
mt.threads = 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const uint32_t threads_max = 8; |
|
if (mt.threads > threads_max) |
|
mt.threads = threads_max; |
|
|
|
|
|
lzma_ret ret = lzma_stream_encoder_mt(strm, &mt); |
|
|
|
if (ret == LZMA_OK) |
|
return true; |
|
|
|
const char *msg; |
|
switch (ret) { |
|
case LZMA_MEM_ERROR: |
|
msg = "Memory allocation failed"; |
|
break; |
|
|
|
case LZMA_OPTIONS_ERROR: |
|
|
|
|
|
|
|
msg = "Specified filter chain is not supported"; |
|
break; |
|
|
|
case LZMA_UNSUPPORTED_CHECK: |
|
msg = "Specified integrity check is not supported"; |
|
break; |
|
|
|
default: |
|
msg = "Unknown error, possibly a bug"; |
|
break; |
|
} |
|
|
|
fprintf(stderr, "Error initializing the encoder: %s (error code %u)\n", |
|
msg, ret); |
|
return false; |
|
} |
|
|
|
|
|
|
|
static bool |
|
compress(lzma_stream *strm, FILE *infile, FILE *outfile) |
|
{ |
|
lzma_action action = LZMA_RUN; |
|
|
|
uint8_t inbuf[BUFSIZ]; |
|
uint8_t outbuf[BUFSIZ]; |
|
|
|
strm->next_in = NULL; |
|
strm->avail_in = 0; |
|
strm->next_out = outbuf; |
|
strm->avail_out = sizeof(outbuf); |
|
|
|
while (true) { |
|
if (strm->avail_in == 0 && !feof(infile)) { |
|
strm->next_in = inbuf; |
|
strm->avail_in = fread(inbuf, 1, sizeof(inbuf), |
|
infile); |
|
|
|
if (ferror(infile)) { |
|
fprintf(stderr, "Read error: %s\n", |
|
strerror(errno)); |
|
return false; |
|
} |
|
|
|
if (feof(infile)) |
|
action = LZMA_FINISH; |
|
} |
|
|
|
lzma_ret ret = lzma_code(strm, action); |
|
|
|
if (strm->avail_out == 0 || ret == LZMA_STREAM_END) { |
|
size_t write_size = sizeof(outbuf) - strm->avail_out; |
|
|
|
if (fwrite(outbuf, 1, write_size, outfile) |
|
!= write_size) { |
|
fprintf(stderr, "Write error: %s\n", |
|
strerror(errno)); |
|
return false; |
|
} |
|
|
|
strm->next_out = outbuf; |
|
strm->avail_out = sizeof(outbuf); |
|
} |
|
|
|
if (ret != LZMA_OK) { |
|
if (ret == LZMA_STREAM_END) |
|
return true; |
|
|
|
const char *msg; |
|
switch (ret) { |
|
case LZMA_MEM_ERROR: |
|
msg = "Memory allocation failed"; |
|
break; |
|
|
|
case LZMA_DATA_ERROR: |
|
msg = "File size limits exceeded"; |
|
break; |
|
|
|
default: |
|
msg = "Unknown error, possibly a bug"; |
|
break; |
|
} |
|
|
|
fprintf(stderr, "Encoder error: %s (error code %u)\n", |
|
msg, ret); |
|
return false; |
|
} |
|
} |
|
} |
|
|
|
|
|
extern int |
|
main(void) |
|
{ |
|
lzma_stream strm = LZMA_STREAM_INIT; |
|
|
|
bool success = init_encoder(&strm); |
|
if (success) |
|
success = compress(&strm, stdin, stdout); |
|
|
|
lzma_end(&strm); |
|
|
|
if (fclose(stdout)) { |
|
fprintf(stderr, "Write error: %s\n", strerror(errno)); |
|
success = false; |
|
} |
|
|
|
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
|
} |
|
|