#include "private.h"
#include "tuklib_integer.h"
enum coder_init_ret {
CODER_INIT_NORMAL,
CODER_INIT_PASSTHRU,
CODER_INIT_ERROR,
};
enum operation_mode opt_mode = MODE_COMPRESS;
enum format_type opt_format = FORMAT_AUTO;
bool opt_auto_adjust = true;
bool opt_single_stream = false;
uint64_t opt_block_size = 0;
block_list_entry *opt_block_list = NULL;
uint64_t block_list_largest;
uint32_t block_list_chain_mask;
static lzma_stream strm = LZMA_STREAM_INIT;
#define NUM_FILTER_CHAIN_MAX 10
static lzma_filter chains[NUM_FILTER_CHAIN_MAX][LZMA_FILTERS_MAX + 1];
static uint32_t chains_used_mask = 1U << 0;
static io_buf in_buf;
static io_buf out_buf;
static uint32_t filters_count = 0;
static uint32_t preset_number = LZMA_PRESET_DEFAULT;
static bool string_to_filter_used = false;
static lzma_check check;
static bool check_default = true;
static bool allow_trailing_input;
#ifdef MYTHREAD_ENABLED
static lzma_mt mt_options = {
.flags = 0,
.timeout = 300,
};
#endif
extern void
coder_set_check(lzma_check new_check)
{
check = new_check;
check_default = false;
return;
}
static void
forget_filter_chain(void)
{
if (filters_count > 0) {
lzma_filters_free(chains[0], NULL);
filters_count = 0;
}
string_to_filter_used = false;
return;
}
extern void
coder_set_preset(uint32_t new_preset)
{
preset_number &= ~LZMA_PRESET_LEVEL_MASK;
preset_number |= new_preset;
forget_filter_chain();
return;
}
extern void
coder_set_extreme(void)
{
preset_number |= LZMA_PRESET_EXTREME;
forget_filter_chain();
return;
}
extern void
coder_add_filter(lzma_vli id, void *options)
{
if (filters_count == LZMA_FILTERS_MAX)
message_fatal(_("Maximum number of filters is four"));
if (string_to_filter_used)
forget_filter_chain();
chains[0][filters_count].id = id;
chains[0][filters_count].options = options;
chains[0][++filters_count].id = LZMA_VLI_UNKNOWN;
preset_number = LZMA_PRESET_DEFAULT;
return;
}
static void
str_to_filters(const char *str, uint32_t index, uint32_t flags)
{
int error_pos;
const char *err = lzma_str_to_filters(str, &error_pos,
chains[index], flags, NULL);
if (err != NULL) {
char filter_num[2] = "";
if (index > 0)
filter_num[0] = '0' + index;
message(V_ERROR, _("Error in --filters%s=FILTERS option:"),
filter_num);
message(V_ERROR, "%s", str);
message(V_ERROR, "%*s^", error_pos, "");
message_fatal("%s", err);
}
}
extern void
coder_add_filters_from_str(const char *filter_str)
{
forget_filter_chain();
preset_number = LZMA_PRESET_DEFAULT;
string_to_filter_used = true;
str_to_filters(filter_str, 0, LZMA_STR_ALL_FILTERS);
for (filters_count = 0; chains[0][filters_count].id
!= LZMA_VLI_UNKNOWN;
++filters_count) ;
assert(filters_count > 0);
return;
}
extern void
coder_add_block_filters(const char *str, size_t slot)
{
if (chains_used_mask & (1U << slot))
lzma_filters_free(chains[slot], NULL);
str_to_filters(str, slot, 0);
chains_used_mask |= 1U << slot;
}
tuklib_attr_noreturn
static void
memlimit_too_small(uint64_t memory_usage)
{
message(V_ERROR, _("Memory usage limit is too low for the given "
"filter setup."));
message_mem_needed(V_ERROR, memory_usage);
tuklib_exit(E_ERROR, E_ERROR, false);
}
#ifdef HAVE_ENCODERS
static uint64_t
get_chains_memusage(uint64_t *chains_memusages, const lzma_mt *mt, bool encode)
{
uint64_t max_memusage = 0;
#ifdef MYTHREAD_ENABLED
lzma_mt mt_local;
if (mt != NULL)
mt_local = *mt;
#else
(void)mt;
#endif
for (uint32_t i = 0; i < ARRAY_SIZE(chains); i++) {
if (!(chains_used_mask & (1U << i)))
continue;
uint64_t memusage = UINT64_MAX;
#ifdef MYTHREAD_ENABLED
if (mt != NULL) {
assert(encode);
mt_local.filters = chains[i];
memusage = lzma_stream_encoder_mt_memusage(&mt_local);
} else
#endif
if (encode) {
memusage = lzma_raw_encoder_memusage(chains[i]);
}
#ifdef HAVE_DECODERS
else {
memusage = lzma_raw_decoder_memusage(chains[i]);
}
#endif
if (chains_memusages != NULL)
chains_memusages[i] = memusage;
if (memusage > max_memusage)
max_memusage = memusage;
}
return max_memusage;
}
#endif
extern void
coder_set_compression_settings(void)
{
#ifdef HAVE_LZIP_DECODER
assert(opt_format != FORMAT_LZIP);
#endif
if (check_default) {
check = LZMA_CHECK_CRC64;
if (!lzma_check_is_supported(check))
check = LZMA_CHECK_CRC32;
}
#ifdef HAVE_ENCODERS
if (opt_block_list != NULL) {
assert(opt_mode == MODE_COMPRESS);
assert(opt_format == FORMAT_XZ);
const uint32_t missing_chains_mask
= (block_list_chain_mask ^ chains_used_mask)
& block_list_chain_mask;
if (missing_chains_mask != 0) {
const unsigned first_missing
= (unsigned)ctz32(missing_chains_mask);
message_fatal(_("filter chain %u used by "
"--block-list but not specified "
"with --filters%u="),
first_missing, first_missing);
}
chains_used_mask &= block_list_chain_mask;
} else {
chains_used_mask = 1U << 0;
}
#endif
static lzma_options_lzma opt_lzma;
lzma_filter *default_filters = chains[0];
if (filters_count == 0 && chains_used_mask & 1) {
if (opt_format == FORMAT_RAW) {
message(V_WARNING, _("Using a preset in raw mode "
"is discouraged."));
message(V_WARNING, _("The exact options of the "
"presets may vary between software "
"versions."));
}
if (lzma_lzma_preset(&opt_lzma, preset_number))
message_bug();
default_filters[0].id = opt_format == FORMAT_LZMA
? LZMA_FILTER_LZMA1 : LZMA_FILTER_LZMA2;
default_filters[0].options = &opt_lzma;
filters_count = 1;
default_filters[1].id = LZMA_VLI_UNKNOWN;
}
if (opt_format == FORMAT_LZMA && (filters_count != 1
|| default_filters[0].id != LZMA_FILTER_LZMA1))
message_fatal(_("The .lzma format supports only "
"the LZMA1 filter"));
if (opt_format == FORMAT_XZ && chains_used_mask & 1)
for (size_t i = 0; i < filters_count; ++i)
if (default_filters[i].id == LZMA_FILTER_LZMA1)
message_fatal(_("LZMA1 cannot be used "
"with the .xz format"));
if (chains_used_mask & 1) {
message_filters_show(V_DEBUG, default_filters);
}
if (opt_mode == MODE_COMPRESS && opt_flush_timeout != 0) {
for (unsigned i = 0; i < ARRAY_SIZE(chains); ++i) {
if (!(chains_used_mask & (1U << i)))
continue;
const lzma_filter *fc = chains[i];
for (size_t j = 0; fc[j].id != LZMA_VLI_UNKNOWN; j++) {
switch (fc[j].id) {
case LZMA_FILTER_LZMA2:
case LZMA_FILTER_DELTA:
break;
default:
message_fatal(_("Filter chain %u is "
"incompatible with "
"--flush-timeout"),
i);
}
}
}
if (hardware_threads_is_mt()) {
message(V_WARNING, _("Switching to single-threaded "
"mode due to --flush-timeout"));
hardware_threads_set(1);
}
}
uint64_t memory_limit = hardware_memlimit_get(opt_mode);
uint64_t memory_usage = UINT64_MAX;
#ifdef HAVE_ENCODERS
uint64_t encoder_memusages[ARRAY_SIZE(chains)];
#endif
if (opt_mode == MODE_COMPRESS) {
#ifdef HAVE_ENCODERS
# ifdef MYTHREAD_ENABLED
if (opt_format == FORMAT_XZ && hardware_threads_is_mt()) {
memory_limit = hardware_memlimit_mtenc_get();
mt_options.threads = hardware_threads_get();
uint64_t block_size = opt_block_size;
if (block_size == 0) {
for (unsigned i = 0; i < ARRAY_SIZE(chains);
i++) {
if (!(chains_used_mask & (1U << i)))
continue;
uint64_t size = lzma_mt_block_size(
chains[i]);
if (size == UINT64_MAX)
message_fatal(_("Unsupported "
"options in filter "
"chain %u"), i);
if (size > block_size)
block_size = size;
}
if (block_list_largest > 0 && block_size
> block_list_largest)
block_size = block_list_largest;
}
mt_options.block_size = block_size;
mt_options.check = check;
memory_usage = get_chains_memusage(encoder_memusages,
&mt_options, true);
if (memory_usage != UINT64_MAX)
message(V_DEBUG, _("Using up to %" PRIu32
" threads."),
mt_options.threads);
} else
# endif
{
memory_usage = get_chains_memusage(encoder_memusages,
NULL, true);
}
#endif
} else {
#ifdef HAVE_DECODERS
memory_usage = lzma_raw_decoder_memusage(default_filters);
#endif
}
if (memory_usage == UINT64_MAX)
message_fatal(_("Unsupported filter chain or filter options"));
message_mem_needed(V_DEBUG, memory_usage);
#if defined(HAVE_ENCODERS) && defined(HAVE_DECODERS)
if (opt_mode == MODE_COMPRESS && message_verbosity_get() >= V_DEBUG) {
const uint64_t decmem = get_chains_memusage(NULL, NULL, false);
if (decmem != UINT64_MAX)
message(V_DEBUG, _("Decompression will need "
"%s MiB of memory."), uint64_to_str(
round_up_to_mib(decmem), 0));
}
#endif
if (memory_usage <= memory_limit)
return;
if (opt_format == FORMAT_RAW)
memlimit_too_small(memory_usage);
assert(opt_mode == MODE_COMPRESS);
#ifdef HAVE_ENCODERS
# ifdef MYTHREAD_ENABLED
if (opt_format == FORMAT_XZ && hardware_threads_is_mt()) {
while (mt_options.threads > 1) {
--mt_options.threads;
memory_usage = get_chains_memusage(encoder_memusages,
&mt_options, true);
if (memory_usage == UINT64_MAX)
message_bug();
if (memory_usage <= memory_limit) {
message(V_DEBUG, _("Reduced the number of "
"threads from %s to %s to not exceed "
"the memory usage limit of %s MiB"),
uint64_to_str(
hardware_threads_get(), 0),
uint64_to_str(mt_options.threads, 1),
uint64_to_str(round_up_to_mib(
memory_limit), 2));
return;
}
}
if (hardware_memlimit_mtenc_is_default()) {
message(V_DEBUG, _("Reduced the number of threads "
"from %s to one. The automatic memory usage "
"limit of %s MiB is still being exceeded. "
"%s MiB of memory is required. "
"Continuing anyway."),
uint64_to_str(hardware_threads_get(), 0),
uint64_to_str(
round_up_to_mib(memory_limit), 1),
uint64_to_str(
round_up_to_mib(memory_usage), 2));
return;
}
if (!opt_auto_adjust)
memlimit_too_small(memory_usage);
hardware_threads_set(1);
memory_usage = get_chains_memusage(encoder_memusages,
NULL, true);
message(V_WARNING, _("Switching to single-threaded mode "
"to not exceed the memory usage limit of %s MiB"),
uint64_to_str(round_up_to_mib(memory_limit), 0));
}
# endif
if (memory_usage <= memory_limit)
return;
if (!opt_auto_adjust)
memlimit_too_small(memory_usage);
for (unsigned i = 0; i < ARRAY_SIZE(chains); i++) {
if (!(chains_used_mask & (1U << i)))
continue;
if (encoder_memusages[i] <= memory_limit)
continue;
unsigned j = 0;
while (chains[i][j].id != LZMA_FILTER_LZMA2
&& chains[i][j].id != LZMA_FILTER_LZMA1) {
if (chains[i][j].id == LZMA_VLI_UNKNOWN)
memlimit_too_small(encoder_memusages[i]);
++j;
}
lzma_options_lzma *opt = chains[i][j].options;
const uint32_t orig_dict_size = opt->dict_size;
opt->dict_size &= ~((UINT32_C(1) << 20) - 1);
while (true) {
if (opt->dict_size < (UINT32_C(1) << 20))
memlimit_too_small(encoder_memusages[i]);
encoder_memusages[i]
= lzma_raw_encoder_memusage(chains[i]);
if (encoder_memusages[i] == UINT64_MAX)
message_bug();
if (encoder_memusages[i] <= memory_limit)
break;
opt->dict_size -= UINT32_C(1) << 20;
}
const char lzma_num = chains[i][j].id == LZMA_FILTER_LZMA2
? '2' : '1';
const char *from_size = uint64_to_str(orig_dict_size >> 20, 0);
const char *to_size = uint64_to_str(opt->dict_size >> 20, 1);
const char *limit_size = uint64_to_str(round_up_to_mib(
memory_limit), 2);
if (i == 0)
message(V_WARNING, _("Adjusted LZMA%c dictionary "
"size from %s MiB to %s MiB to not exceed the "
"memory usage limit of %s MiB"),
lzma_num, from_size, to_size, limit_size);
else
message(V_WARNING, _("Adjusted LZMA%c dictionary size "
"for --filters%u from %s MiB to %s MiB to not "
"exceed the memory usage limit of %s MiB"),
lzma_num, i, from_size, to_size, limit_size);
}
#endif
return;
}
#ifdef HAVE_DECODERS
static bool
is_format_xz(void)
{
static const uint8_t magic[6] = { 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00 };
return strm.avail_in >= sizeof(magic)
&& memcmp(in_buf.u8, magic, sizeof(magic)) == 0;
}
static bool
is_format_lzma(void)
{
if (strm.avail_in < 13)
return false;
lzma_filter filter = { .id = LZMA_FILTER_LZMA1 };
if (lzma_properties_decode(&filter, NULL, in_buf.u8, 5) != LZMA_OK)
return false;
lzma_options_lzma *opt = filter.options;
const uint32_t dict_size = opt->dict_size;
free(opt);
if (dict_size != UINT32_MAX) {
uint32_t d = dict_size - 1;
d |= d >> 2;
d |= d >> 3;
d |= d >> 4;
d |= d >> 8;
d |= d >> 16;
++d;
if (d != dict_size || dict_size == 0)
return false;
}
uint64_t uncompressed_size = 0;
for (size_t i = 0; i < 8; ++i)
uncompressed_size |= (uint64_t)(in_buf.u8[5 + i]) << (i * 8);
if (uncompressed_size != UINT64_MAX
&& uncompressed_size > (UINT64_C(1) << 38))
return false;
return true;
}
#ifdef HAVE_LZIP_DECODER
static bool
is_format_lzip(void)
{
static const uint8_t magic[4] = { 0x4C, 0x5A, 0x49, 0x50 };
return strm.avail_in >= sizeof(magic)
&& memcmp(in_buf.u8, magic, sizeof(magic)) == 0;
}
#endif
#endif
static enum coder_init_ret
coder_init(file_pair *pair)
{
lzma_ret ret = LZMA_PROG_ERROR;
allow_trailing_input = false;
lzma_filter *active_filters = opt_block_list == NULL
? chains[0]
: chains[opt_block_list[0].chain_num];
if (opt_mode == MODE_COMPRESS) {
#ifdef HAVE_ENCODERS
switch (opt_format) {
case FORMAT_AUTO:
assert(0);
break;
case FORMAT_XZ:
# ifdef MYTHREAD_ENABLED
mt_options.filters = active_filters;
if (hardware_threads_is_mt())
ret = lzma_stream_encoder_mt(
&strm, &mt_options);
else
# endif
ret = lzma_stream_encoder(
&strm, active_filters, check);
break;
case FORMAT_LZMA:
ret = lzma_alone_encoder(&strm,
active_filters[0].options);
break;
# ifdef HAVE_LZIP_DECODER
case FORMAT_LZIP:
assert(0);
ret = LZMA_PROG_ERROR;
break;
# endif
case FORMAT_RAW:
ret = lzma_raw_encoder(&strm, active_filters);
break;
}
#endif
} else {
#ifdef HAVE_DECODERS
uint32_t flags = 0;
if (opt_ignore_check)
flags |= LZMA_IGNORE_CHECK;
else
flags |= LZMA_TELL_UNSUPPORTED_CHECK;
if (opt_single_stream)
allow_trailing_input = true;
else
flags |= LZMA_CONCATENATED;
enum format_type init_format = FORMAT_AUTO;
switch (opt_format) {
case FORMAT_AUTO:
if (is_format_xz())
init_format = FORMAT_XZ;
# ifdef HAVE_LZIP_DECODER
else if (is_format_lzip())
init_format = FORMAT_LZIP;
# endif
else if (is_format_lzma())
init_format = FORMAT_LZMA;
break;
case FORMAT_XZ:
if (is_format_xz())
init_format = FORMAT_XZ;
break;
case FORMAT_LZMA:
if (is_format_lzma())
init_format = FORMAT_LZMA;
break;
# ifdef HAVE_LZIP_DECODER
case FORMAT_LZIP:
if (is_format_lzip())
init_format = FORMAT_LZIP;
break;
# endif
case FORMAT_RAW:
init_format = FORMAT_RAW;
break;
}
switch (init_format) {
case FORMAT_AUTO:
if (opt_mode == MODE_DECOMPRESS
&& opt_stdout && opt_force) {
strm.total_in = 0;
strm.total_out = 0;
return CODER_INIT_PASSTHRU;
}
ret = LZMA_FORMAT_ERROR;
break;
case FORMAT_XZ:
# ifdef MYTHREAD_ENABLED
mt_options.flags = flags;
mt_options.threads = hardware_threads_get();
mt_options.memlimit_stop
= hardware_memlimit_get(MODE_DECOMPRESS);
mt_options.memlimit_threading
= mt_options.threads == 1
? 0 : hardware_memlimit_mtdec_get();
ret = lzma_stream_decoder_mt(&strm, &mt_options);
# else
ret = lzma_stream_decoder(&strm,
hardware_memlimit_get(
MODE_DECOMPRESS), flags);
# endif
break;
case FORMAT_LZMA:
ret = lzma_alone_decoder(&strm,
hardware_memlimit_get(
MODE_DECOMPRESS));
break;
# ifdef HAVE_LZIP_DECODER
case FORMAT_LZIP:
allow_trailing_input = true;
ret = lzma_lzip_decoder(&strm,
hardware_memlimit_get(
MODE_DECOMPRESS), flags);
break;
# endif
case FORMAT_RAW:
ret = lzma_raw_decoder(&strm, active_filters);
break;
}
if (ret == LZMA_OK && init_format != FORMAT_RAW) {
strm.next_out = NULL;
strm.avail_out = 0;
while ((ret = lzma_code(&strm, LZMA_RUN))
== LZMA_UNSUPPORTED_CHECK)
message_warning(_("%s: %s"), pair->src_name,
message_strm(ret));
if (ret == LZMA_STREAM_END)
ret = LZMA_OK;
}
#endif
}
if (ret != LZMA_OK) {
message_error(_("%s: %s"), pair->src_name, message_strm(ret));
if (ret == LZMA_MEMLIMIT_ERROR)
message_mem_needed(V_ERROR, lzma_memusage(&strm));
return CODER_INIT_ERROR;
}
return CODER_INIT_NORMAL;
}
#ifdef HAVE_ENCODERS
static void
split_block(uint64_t *block_remaining,
uint64_t *next_block_remaining,
size_t *list_pos)
{
if (*next_block_remaining > 0) {
assert(!hardware_threads_is_mt());
assert(opt_block_size > 0);
assert(opt_block_list != NULL);
if (*next_block_remaining > opt_block_size) {
*block_remaining = opt_block_size;
} else {
*block_remaining = *next_block_remaining;
}
*next_block_remaining -= *block_remaining;
} else {
if (opt_block_list[*list_pos + 1].size != 0) {
++*list_pos;
if (opt_block_list[*list_pos - 1].chain_num
!= opt_block_list[*list_pos].chain_num) {
const unsigned chain_num
= opt_block_list[*list_pos].chain_num;
const lzma_filter *next = chains[chain_num];
const lzma_ret ret = lzma_filters_update(
&strm, next);
if (ret != LZMA_OK) {
message_fatal(
_("Error changing to "
"filter chain %u: %s"),
chain_num,
message_strm(ret));
}
}
}
*block_remaining = opt_block_list[*list_pos].size;
if (!hardware_threads_is_mt() && opt_block_size > 0
&& *block_remaining > opt_block_size) {
*next_block_remaining
= *block_remaining - opt_block_size;
*block_remaining = opt_block_size;
}
}
}
#endif
static bool
coder_write_output(file_pair *pair)
{
if (opt_mode != MODE_TEST) {
if (io_write(pair, &out_buf, IO_BUFFER_SIZE - strm.avail_out))
return true;
}
strm.next_out = out_buf.u8;
strm.avail_out = IO_BUFFER_SIZE;
return false;
}
static bool
coder_normal(file_pair *pair)
{
lzma_action action = pair->src_eof ? LZMA_FINISH : LZMA_RUN;
lzma_ret ret;
bool success = false;
#ifdef HAVE_ENCODERS
uint64_t block_remaining = UINT64_MAX;
uint64_t next_block_remaining = 0;
size_t list_pos = 0;
if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_XZ) {
if (!hardware_threads_is_mt() && opt_block_size > 0)
block_remaining = opt_block_size;
if (opt_block_list != NULL) {
if (block_remaining < opt_block_list[list_pos].size) {
assert(!hardware_threads_is_mt());
next_block_remaining =
opt_block_list[list_pos].size
- block_remaining;
} else {
block_remaining =
opt_block_list[list_pos].size;
}
}
}
#endif
strm.next_out = out_buf.u8;
strm.avail_out = IO_BUFFER_SIZE;
while (!user_abort) {
if (strm.avail_in == 0 && action == LZMA_RUN) {
strm.next_in = in_buf.u8;
#ifdef HAVE_ENCODERS
const size_t read_size = my_min(block_remaining,
IO_BUFFER_SIZE);
#else
const size_t read_size = IO_BUFFER_SIZE;
#endif
strm.avail_in = io_read(pair, &in_buf, read_size);
if (strm.avail_in == SIZE_MAX)
break;
if (pair->src_eof) {
action = LZMA_FINISH;
}
#ifdef HAVE_ENCODERS
else if (block_remaining != UINT64_MAX) {
block_remaining -= strm.avail_in;
if (block_remaining == 0)
action = LZMA_FULL_BARRIER;
}
if (action == LZMA_RUN && pair->flush_needed)
action = LZMA_SYNC_FLUSH;
#endif
}
ret = lzma_code(&strm, action);
if (strm.avail_out == 0) {
if (coder_write_output(pair))
break;
}
#ifdef HAVE_ENCODERS
if (ret == LZMA_STREAM_END && (action == LZMA_SYNC_FLUSH
|| action == LZMA_FULL_BARRIER)) {
if (action == LZMA_SYNC_FLUSH) {
if (coder_write_output(pair))
break;
pair->src_has_seen_input = false;
pair->flush_needed = false;
} else {
if (opt_block_list == NULL) {
assert(!hardware_threads_is_mt());
assert(opt_block_size > 0);
block_remaining = opt_block_size;
} else {
split_block(&block_remaining,
&next_block_remaining,
&list_pos);
}
}
action = LZMA_RUN;
} else
#endif
if (ret != LZMA_OK) {
const bool stop = ret != LZMA_UNSUPPORTED_CHECK;
if (stop) {
if (coder_write_output(pair))
break;
}
if (ret == LZMA_STREAM_END) {
if (allow_trailing_input) {
io_fix_src_pos(pair, strm.avail_in);
success = true;
break;
}
if (strm.avail_in == 0 && !pair->src_eof) {
strm.avail_in = io_read(
pair, &in_buf, 1);
if (strm.avail_in == SIZE_MAX)
break;
assert(strm.avail_in == 0
|| strm.avail_in == 1);
}
if (strm.avail_in == 0) {
assert(pair->src_eof);
success = true;
break;
}
ret = LZMA_DATA_ERROR;
assert(stop);
}
if (stop) {
message_error(_("%s: %s"), pair->src_name,
message_strm(ret));
} else {
message_warning(_("%s: %s"), pair->src_name,
message_strm(ret));
assert(opt_mode != MODE_COMPRESS);
}
if (ret == LZMA_MEMLIMIT_ERROR) {
message_mem_needed(V_ERROR,
lzma_memusage(&strm));
}
if (stop)
break;
}
message_progress_update();
}
return success;
}
static bool
coder_passthru(file_pair *pair)
{
while (strm.avail_in != 0) {
if (user_abort)
return false;
if (io_write(pair, &in_buf, strm.avail_in))
return false;
strm.total_in += strm.avail_in;
strm.total_out = strm.total_in;
message_progress_update();
strm.avail_in = io_read(pair, &in_buf, IO_BUFFER_SIZE);
if (strm.avail_in == SIZE_MAX)
return false;
}
return true;
}
extern void
coder_run(const char *filename)
{
message_filename(filename);
file_pair *pair = io_open_src(filename);
if (pair == NULL)
return;
bool success = false;
if (opt_mode == MODE_COMPRESS) {
strm.next_in = NULL;
strm.avail_in = 0;
} else {
strm.next_in = in_buf.u8;
strm.avail_in = io_read(pair, &in_buf, IO_BUFFER_SIZE);
}
if (strm.avail_in != SIZE_MAX) {
const enum coder_init_ret init_ret = coder_init(pair);
if (init_ret != CODER_INIT_ERROR && !user_abort) {
if (opt_mode == MODE_TEST || !io_open_dest(pair)) {
mytime_set_start_time();
const bool is_passthru = init_ret
== CODER_INIT_PASSTHRU;
const uint64_t in_size
= pair->src_st.st_size <= 0
? 0 : (uint64_t)(pair->src_st.st_size);
message_progress_start(&strm,
is_passthru, in_size);
if (is_passthru)
success = coder_passthru(pair);
else
success = coder_normal(pair);
message_progress_end(success);
}
}
}
io_close(pair, success);
return;
}
#ifndef NDEBUG
extern void
coder_free(void)
{
for (uint32_t i = 1; i < ARRAY_SIZE(chains); i++) {
if (chains_used_mask & (1U << i))
lzma_filters_free(chains[i], NULL);
}
lzma_end(&strm);
return;
}
#endif