From 2fcda89939c903106c429e109083d43d894049e0 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Fri, 21 Jun 2013 21:50:26 +0300 Subject: [PATCH] xz: Fix interaction between preset and custom filter chains. There was somewhat illogical behavior when --extreme was specified and mixed with custom filter chains. Before this commit, "xz -9 --lzma2 -e" was equivalent to "xz --lzma2". After it is equivalent to "xz -6e" (all earlier preset options get forgotten when a custom filter chain is specified and the default preset is 6 to which -e is applied). I find this less illogical. This also affects the meaning of "xz -9e --lzma2 -7". Earlier it was equivalent to "xz -7e" (the -e specified before a custom filter chain wasn't forgotten). Now it is "xz -7". Note that "xz -7e" still is the same as "xz -e7". Hopefully very few cared about this in the first place, so pretty much no one should even notice this change. Thanks to Conley Moorhous. --- src/xz/coder.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/xz/coder.c b/src/xz/coder.c index a98be97f..54864506 100644 --- a/src/xz/coder.c +++ b/src/xz/coder.c @@ -43,12 +43,7 @@ static io_buf out_buf; static uint32_t filters_count = 0; /// Number of the preset (0-9) -static uint32_t preset_number = 6; - -/// If a preset is used (no custom filter chain) and preset_extreme is true, -/// a significantly slower compression is used to achieve slightly better -/// compression ratio. -static bool preset_extreme = false; +static uint32_t preset_number = LZMA_PRESET_DEFAULT; /// Integrity check type static lzma_check check; @@ -74,11 +69,9 @@ coder_set_check(lzma_check new_check) } -extern void -coder_set_preset(uint32_t new_preset) +static void +forget_filter_chain(void) { - preset_number = new_preset; - // Setting a preset makes us forget a possibly defined custom // filter chain. while (filters_count > 0) { @@ -91,10 +84,21 @@ coder_set_preset(uint32_t new_preset) } +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_extreme = true; + preset_number |= LZMA_PRESET_EXTREME; + forget_filter_chain(); return; } @@ -109,6 +113,12 @@ coder_add_filter(lzma_vli id, void *options) filters[filters_count].options = options; ++filters_count; + // Setting a custom filter chain makes us forget the preset options. + // This makes a difference if one specifies e.g. "xz -9 --lzma2 -e" + // where the custom filter chain resets the preset level back to + // the default 6, making the example equivalent to "xz -6e". + preset_number = LZMA_PRESET_DEFAULT; + return; } @@ -154,9 +164,6 @@ coder_set_compression_settings(void) } // Get the preset for LZMA1 or LZMA2. - if (preset_extreme) - preset_number |= LZMA_PRESET_EXTREME; - if (lzma_lzma_preset(&opt_lzma, preset_number)) message_bug();