]> granicus.if.org Git - xz/commitdiff
Fix a design error in liblzma API.
authorLasse Collin <lasse.collin@tukaani.org>
Sat, 14 Nov 2009 16:59:19 +0000 (18:59 +0200)
committerLasse Collin <lasse.collin@tukaani.org>
Sat, 14 Nov 2009 16:59:19 +0000 (18:59 +0200)
Originally the idea was that using LZMA_FULL_FLUSH
with Stream encoder would read the filter chain
from the same array that was used to intialize the
Stream encoder. Since most apps wouldn't use
LZMA_FULL_FLUSH, most apps wouldn't need to keep
the filter chain available after initializing the
Stream encoder. However, due to my mistake, it
actually required keeping the array always available.

Since setting the new filter chain via the array
used at initialization time is not a nice way to do
it for a couple of reasons, this commit ditches it
and introduces lzma_filters_update(). This new function
replaces also the "persistent" flag used by LZMA2
(and to-be-designed Subblock filter), which was also
an ugly thing to do.

Thanks to Alexey Tourbin for reminding me about the problem
that Stream encoder used to require keeping the filter
chain allocated.

20 files changed:
src/liblzma/api/lzma/filter.h
src/liblzma/api/lzma/lzma.h
src/liblzma/common/block_encoder.c
src/liblzma/common/common.c
src/liblzma/common/common.h
src/liblzma/common/easy_encoder.c
src/liblzma/common/filter_common.c
src/liblzma/common/filter_encoder.c
src/liblzma/common/filter_encoder.h
src/liblzma/common/stream_encoder.c
src/liblzma/delta/delta_common.c
src/liblzma/delta/delta_decoder.c
src/liblzma/delta/delta_encoder.c
src/liblzma/delta/delta_private.h
src/liblzma/lz/lz_encoder.c
src/liblzma/lz/lz_encoder.h
src/liblzma/lzma/lzma2_encoder.c
src/liblzma/lzma/lzma_encoder_presets.c
src/liblzma/simple/simple_coder.c
src/xz/options.c

index c4bc69d3a02a15a17bfcdff76462328da56b1955..01393dd4bd67b58ebf6a20815db7ab509a9ad3d5 100644 (file)
@@ -197,6 +197,36 @@ extern LZMA_API(lzma_ret) lzma_raw_decoder(
                lzma_nothrow lzma_attr_warn_unused_result;
 
 
+/**
+ * \brief       Update the filter chain in the encoder
+ *
+ * This function is for advanced users only. This function has two slightly
+ * different purposes:
+ *
+ *  - After LZMA_FULL_FLUSH when using Stream encoder: Set a new filter
+ *    chain, which will be used starting from the next Block.
+ *
+ *  - After LZMA_SYNC_FLUSH using Raw, Block, or Stream encoder: Change
+ *    the filter-specific options in the middle of encoding. The actual
+ *    filters in the chain (Filter IDs) cannot be changed. In the future,
+ *    it might become possible to change the filter options without
+ *    using LZMA_SYNC_FLUSH.
+ *
+ * While rarely useful, this function may be called also when no data has
+ * been compressed yet. In that case, this function will behave as if
+ * LZMA_FULL_FLUSH (Stream encoder) or LZMA_SYNC_FLUSH (Raw or Block
+ * encoder) had been used right before calling this function.
+ *
+ * \return      - LZMA_OK
+ *              - LZMA_MEM_ERROR
+ *              - LZMA_MEMLIMIT_ERROR
+ *              - LZMA_OPTIONS_ERROR
+ *              - LZMA_PROG_ERROR
+ */
+extern LZMA_API(lzma_ret) lzma_filters_update(
+               lzma_stream *strm, const lzma_filter *filters);
+
+
 /**
  * \brief       Single-call raw encoder
  *
index 28ebbb14b9022559d017c418e54997ff1a2ab41b..989425e31d5d5228639353018e310c750527ce87 100644 (file)
@@ -298,19 +298,6 @@ typedef struct {
 #      define LZMA_PB_MAX      4
 #      define LZMA_PB_DEFAULT  2
 
-       /**
-        * \brief       Indicate if the options structure is persistent
-        *
-        * If this is true, the application must keep this options structure
-        * available after the LZMA2 encoder has been initialized. With
-        * persistent structure it is possible to change some encoder options
-        * in the middle of the encoding process without resetting the encoder.
-        *
-        * This option is used only by LZMA2. LZMA1 ignores this and it is
-        * safe to not initialize this when encoding with LZMA1.
-        */
-       lzma_bool persistent;
-
        /** Compression mode */
        lzma_mode mode;
 
index 567889aa2f6415d643001ae7e8435a154c3d0b76..ca5152357e7d9f527b132d289ad8ad11a9a30b10 100644 (file)
@@ -142,6 +142,19 @@ block_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
 }
 
 
+static lzma_ret
+block_encoder_update(lzma_coder *coder, lzma_allocator *allocator,
+               const lzma_filter *filters lzma_attribute((unused)),
+               const lzma_filter *reversed_filters)
+{
+       if (coder->sequence != SEQ_CODE)
+               return LZMA_PROG_ERROR;
+
+       return lzma_next_filter_update(
+                       &coder->next, allocator, reversed_filters);
+}
+
+
 extern lzma_ret
 lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
                lzma_block *block)
@@ -167,6 +180,7 @@ lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
 
                next->code = &block_encode;
                next->end = &block_encoder_end;
+               next->update = &block_encoder_update;
                next->coder->next = LZMA_NEXT_CODER_INIT;
        }
 
index 3bdf32522a32bb8994a47b307f9b2e341dc75a5c..edce90cdff595b90f78b9da7ee9fa0b33855b056 100644 (file)
@@ -92,12 +92,30 @@ lzma_next_filter_init(lzma_next_coder *next, lzma_allocator *allocator,
                const lzma_filter_info *filters)
 {
        lzma_next_coder_init(filters[0].init, next, allocator);
-
+       next->id = filters[0].id;
        return filters[0].init == NULL
                        ? LZMA_OK : filters[0].init(next, allocator, filters);
 }
 
 
+extern lzma_ret
+lzma_next_filter_update(lzma_next_coder *next, lzma_allocator *allocator,
+               const lzma_filter *reversed_filters)
+{
+       // Check that the application isn't trying to change the Filter ID.
+       // End of filters is indicated with LZMA_VLI_UNKNOWN in both
+       // reversed_filters[0].id and next->id.
+       if (reversed_filters[0].id != next->id)
+               return LZMA_PROG_ERROR;
+
+       if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
+               return LZMA_OK;
+
+       assert(next->update != NULL);
+       return next->update(next->coder, allocator, NULL, reversed_filters);
+}
+
+
 extern void
 lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
 {
index 81f51421790d438461a28f4275c5f648009e4763..6551e39f716df3718614663002ce826e5bf8fc63 100644 (file)
@@ -109,6 +109,10 @@ typedef void (*lzma_end_function)(
 /// an array of lzma_filter_info structures. This array is used with
 /// lzma_next_filter_init to initialize the filter chain.
 struct lzma_filter_info_s {
+       /// Filter ID. This is used only by the encoder
+       /// with lzma_filters_update().
+       lzma_vli id;
+
        /// Pointer to function used to initialize the filter.
        /// This is NULL to indicate end of array.
        lzma_init_function init;
@@ -123,6 +127,10 @@ struct lzma_next_coder_s {
        /// Pointer to coder-specific data
        lzma_coder *coder;
 
+       /// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
+       /// point to a filter coder.
+       lzma_vli id;
+
        /// "Pointer" to init function. This is never called here.
        /// We need only to detect if we are initializing a coder
        /// that was allocated earlier. See lzma_next_coder_init and
@@ -145,6 +153,12 @@ struct lzma_next_coder_s {
        /// If new_memlimit == 0, the limit is not changed.
        lzma_ret (*memconfig)(lzma_coder *coder, uint64_t *memusage,
                        uint64_t *old_memlimit, uint64_t new_memlimit);
+
+       /// Update the filter-specific options or the whole filter chain
+       /// in the encoder.
+       lzma_ret (*update)(lzma_coder *coder, lzma_allocator *allocator,
+                       const lzma_filter *filters,
+                       const lzma_filter *reversed_filters);
 };
 
 
@@ -153,10 +167,12 @@ struct lzma_next_coder_s {
        (lzma_next_coder){ \
                .coder = NULL, \
                .init = (uintptr_t)(NULL), \
+               .id = LZMA_VLI_UNKNOWN, \
                .code = NULL, \
                .end = NULL, \
                .get_check = NULL, \
                .memconfig = NULL, \
+               .update = NULL, \
        }
 
 
@@ -212,6 +228,12 @@ extern lzma_ret lzma_strm_init(lzma_stream *strm);
 extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
                lzma_allocator *allocator, const lzma_filter_info *filters);
 
+/// Update the next filter in the chain, if any. This checks that
+/// the application is not trying to change the Filter IDs.
+extern lzma_ret lzma_next_filter_update(
+               lzma_next_coder *next, lzma_allocator *allocator,
+               const lzma_filter *reversed_filters);
+
 /// Frees the memory allocated for next->coder either using next->end or,
 /// if next->end is NULL, using lzma_free.
 extern void lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator);
index 5e2641c99f2e678e10135cf3e85c982eb932c007..d13ccd7351f15341801f35caa7882a3d00098d7f 100644 (file)
 #include "stream_encoder.h"
 
 
-struct lzma_coder_s {
-       lzma_next_coder stream_encoder;
-       lzma_options_easy opt_easy;
-};
-
-
-static lzma_ret
-easy_encode(lzma_coder *coder, lzma_allocator *allocator,
-               const uint8_t *restrict in, size_t *restrict in_pos,
-               size_t in_size, uint8_t *restrict out,
-               size_t *restrict out_pos, size_t out_size, lzma_action action)
-{
-       return coder->stream_encoder.code(
-                       coder->stream_encoder.coder, allocator,
-                       in, in_pos, in_size, out, out_pos, out_size, action);
-}
-
-
-static void
-easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
-{
-       lzma_next_end(&coder->stream_encoder, allocator);
-       lzma_free(coder, allocator);
-       return;
-}
-
-
-static lzma_ret
-easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
-               uint32_t preset, lzma_check check)
-{
-       lzma_next_coder_init(&easy_encoder_init, next, allocator);
-
-       if (next->coder == NULL) {
-               next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
-               if (next->coder == NULL)
-                       return LZMA_MEM_ERROR;
-
-               next->code = &easy_encode;
-               next->end = &easy_encoder_end;
-
-               next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
-       }
-
-       if (lzma_easy_preset(&next->coder->opt_easy, preset))
-               return LZMA_OPTIONS_ERROR;
-
-       return lzma_stream_encoder_init(&next->coder->stream_encoder,
-                       allocator, next->coder->opt_easy.filters, check);
-}
-
-
 extern LZMA_API(lzma_ret)
 lzma_easy_encoder(lzma_stream *strm, uint32_t preset, lzma_check check)
 {
-       lzma_next_strm_init(easy_encoder_init, strm, preset, check);
-
-       strm->internal->supported_actions[LZMA_RUN] = true;
-       strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
-       strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
-       strm->internal->supported_actions[LZMA_FINISH] = true;
+       lzma_options_easy opt_easy;
+       if (lzma_easy_preset(&opt_easy, preset))
+               return LZMA_OPTIONS_ERROR;
 
-       return LZMA_OK;
+       return lzma_stream_encoder(strm, opt_easy.filters, check);
 }
index 055093f703f4292c775d70b9b1bfcf939eefce36..2322d7deec9aa576921ef1bc5f2cf9d9f37a83cc 100644 (file)
@@ -270,6 +270,7 @@ lzma_raw_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
                        if (fc == NULL || fc->init == NULL)
                                return LZMA_OPTIONS_ERROR;
 
+                       filters[j].id = options[i].id;
                        filters[j].init = fc->init;
                        filters[j].options = options[i].options;
                }
@@ -280,12 +281,14 @@ lzma_raw_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
                        if (fc == NULL || fc->init == NULL)
                                return LZMA_OPTIONS_ERROR;
 
+                       filters[i].id = options[i].id;
                        filters[i].init = fc->init;
                        filters[i].options = options[i].options;
                }
        }
 
        // Terminate the array.
+       filters[count].id = LZMA_VLI_UNKNOWN;
        filters[count].init = NULL;
 
        // Initialize the filters.
index d6fb82e511b6c416983f9fa5c99d1dda3bd2d3fa..3b0493feb7b58d9e040ab8e99ab133eba0f8bc93 100644 (file)
@@ -180,6 +180,33 @@ lzma_filter_encoder_is_supported(lzma_vli id)
 }
 
 
+extern LZMA_API(lzma_ret)
+lzma_filters_update(lzma_stream *strm, const lzma_filter *filters)
+{
+       if (strm->internal->next.update == NULL)
+               return LZMA_PROG_ERROR;
+
+       // Validate the filter chain.
+       if (lzma_raw_encoder_memusage(filters) == UINT64_MAX)
+               return LZMA_OPTIONS_ERROR;
+
+       // The actual filter chain in the encoder is reversed. Some things
+       // still want the normal order chain, so we provide both.
+       size_t count = 1;
+       while (filters[count].id != LZMA_VLI_UNKNOWN)
+               ++count;
+
+       lzma_filter reversed_filters[LZMA_FILTERS_MAX + 1];
+       for (size_t i = 0; i < count; ++i)
+               reversed_filters[count - i - 1] = filters[i];
+
+       reversed_filters[count].id = LZMA_VLI_UNKNOWN;
+
+       return strm->internal->next.update(strm->internal->next.coder,
+                       strm->allocator, filters, reversed_filters);
+}
+
+
 extern lzma_ret
 lzma_raw_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
                const lzma_filter *options)
index 5b65cd305a9421356f6feff51fbf9632b41e3226..a978932def75d5c2313afcf37c7fcb210da8a58d 100644 (file)
@@ -22,6 +22,6 @@ extern lzma_vli lzma_chunk_size(const lzma_filter *filters);
 
 extern lzma_ret lzma_raw_encoder_init(
                lzma_next_coder *next, lzma_allocator *allocator,
-               const lzma_filter *options);
+               const lzma_filter *filters);
 
 #endif
index 292efc8248b0817b3424d2c2c2a628a1f5944ec6..705ec0ebf1b615e2f7fce75609a4e13d30f84800 100644 (file)
@@ -25,12 +25,20 @@ struct lzma_coder_s {
                SEQ_STREAM_FOOTER,
        } sequence;
 
+       /// True if Block encoder has been initialized by
+       /// lzma_stream_encoder_init() or stream_encoder_update()
+       /// and thus doesn't need to be initialized in stream_encode().
+       bool block_encoder_is_initialized;
+
        /// Block
        lzma_next_coder block_encoder;
 
        /// Options for the Block encoder
        lzma_block block_options;
 
+       /// The filter chain currently in use
+       lzma_filter filters[LZMA_FILTERS_MAX + 1];
+
        /// Index encoder. This is separate from Block encoder, because this
        /// doesn't take much memory, and when encoding multiple Streams
        /// with the same encoding options we avoid reallocating memory.
@@ -117,12 +125,16 @@ stream_encode(lzma_coder *coder, lzma_allocator *allocator,
                        break;
                }
 
-               // Initialize the Block encoder except if this is the first
-               // Block, because stream_encoder_init() has already
-               // initialized it.
-               if (lzma_index_count(coder->index) != 0)
+               // Initialize the Block encoder unless it was already
+               // initialized by lzma_stream_encoder_init() or
+               // stream_encoder_update().
+               if (!coder->block_encoder_is_initialized)
                        return_if_error(block_encoder_init(coder, allocator));
 
+               // Make it false so that we don't skip the initialization
+               // with the next Block.
+               coder->block_encoder_is_initialized = false;
+
                // Encode the Block Header. This shouldn't fail since we have
                // already initialized the Block encoder.
                if (lzma_block_header_encode(&coder->block_options,
@@ -202,11 +214,54 @@ stream_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
        lzma_next_end(&coder->block_encoder, allocator);
        lzma_next_end(&coder->index_encoder, allocator);
        lzma_index_end(coder->index, allocator);
+
+       for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
+               lzma_free(coder->filters[i].options, allocator);
+
        lzma_free(coder, allocator);
        return;
 }
 
 
+static lzma_ret
+stream_encoder_update(lzma_coder *coder, lzma_allocator *allocator,
+               const lzma_filter *filters,
+               const lzma_filter *reversed_filters)
+{
+       if (coder->sequence <= SEQ_BLOCK_INIT) {
+               // There is no incomplete Block waiting to be finished,
+               // thus we can change the whole filter chain. Start by
+               // trying to initialize the Block encoder with the new
+               // chain. This way we detect if the chain is valid.
+               coder->block_encoder_is_initialized = false;
+               coder->block_options.filters = (lzma_filter *)(filters);
+               const lzma_ret ret = block_encoder_init(coder, allocator);
+               coder->block_options.filters = coder->filters;
+               if (ret != LZMA_OK)
+                       return ret;
+
+               coder->block_encoder_is_initialized = true;
+
+       } else if (coder->sequence <= SEQ_BLOCK_ENCODE) {
+               // We are in the middle of a Block. Try to update only
+               // the filter-specific options.
+               return_if_error(coder->block_encoder.update(
+                               coder->block_encoder.coder, allocator,
+                               filters, reversed_filters));
+       } else {
+               // Trying to update the filter chain when we are already
+               // encoding Index or Stream Footer.
+               return LZMA_PROG_ERROR;
+       }
+
+       // Free the copy of the old chain and make a copy of the new chain.
+       for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
+               lzma_free(coder->filters[i].options, allocator);
+
+       return lzma_filters_copy(filters, coder->filters, allocator);
+}
+
+
 extern lzma_ret
 lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
                const lzma_filter *filters, lzma_check check)
@@ -223,6 +278,7 @@ lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
 
                next->code = &stream_encode;
                next->end = &stream_encoder_end;
+               next->update = &stream_encoder_update;
 
                next->coder->block_encoder = LZMA_NEXT_CODER_INIT;
                next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
@@ -233,7 +289,7 @@ lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
        next->coder->sequence = SEQ_STREAM_HEADER;
        next->coder->block_options.version = 0;
        next->coder->block_options.check = check;
-       next->coder->block_options.filters = (lzma_filter *)(filters);
+       next->coder->filters[0].id = LZMA_VLI_UNKNOWN;
 
        // Initialize the Index
        next->coder->index = lzma_index_init(next->coder->index, allocator);
@@ -251,11 +307,11 @@ lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
        next->coder->buffer_pos = 0;
        next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
 
-       // Initialize the Block encoder. This way we detect if the given
-       // filters are supported by the current liblzma build, and the
-       // application doesn't need to keep the filters structure available
-       // unless it is going to use LZMA_FULL_FLUSH.
-       return block_encoder_init(next->coder, allocator);
+       // Initialize the Block encoder. This way we detect unsupported
+       // filter chains when initializing the Stream encoder instead of
+       // giving an error after Stream Header has already written out.
+       return stream_encoder_update(
+                       next->coder, allocator, filters, NULL);
 }
 
 
index 6d55ff6538e12d01a30ff4e7ef55f392d4883087..930ad215131ca99c30c0605bfcde6dbd05a1e7c3 100644 (file)
@@ -25,7 +25,7 @@ delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
 
 extern lzma_ret
 lzma_delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
-               const lzma_filter_info *filters, lzma_code_function code)
+               const lzma_filter_info *filters)
 {
        // Allocate memory for the decoder if needed.
        if (next->coder == NULL) {
@@ -38,9 +38,6 @@ lzma_delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
                next->coder->next = LZMA_NEXT_CODER_INIT;
        }
 
-       // Coding function is different for encoder and decoder.
-       next->code = code;
-
        // Validate the options.
        if (lzma_delta_coder_memusage(filters[0].options) == UINT64_MAX)
                return LZMA_OPTIONS_ERROR;
index 2ddf163d8d05ef0f7aef3b852a5b08dccae5cb08..2cf60d5bdc7cf36c95c658e7f16ffe378527b09c 100644 (file)
@@ -50,7 +50,8 @@ extern lzma_ret
 lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
                const lzma_filter_info *filters)
 {
-       return lzma_delta_coder_init(next, allocator, filters, &delta_decode);
+       next->code = &delta_decode;
+       return lzma_delta_coder_init(next, allocator, filters);
 }
 
 
index 0244673eae3a94ac03e027384ddeed9c3e0dbea8..80d0d1764ddc99c50e9babfb2365ceaa05a8ae75 100644 (file)
@@ -83,11 +83,26 @@ delta_encode(lzma_coder *coder, lzma_allocator *allocator,
 }
 
 
+static lzma_ret
+delta_encoder_update(lzma_coder *coder, lzma_allocator *allocator,
+               const lzma_filter *filters_null lzma_attribute((unused)),
+               const lzma_filter *reversed_filters)
+{
+       // Delta doesn't and will never support changing the options in
+       // the middle of encoding. If the app tries to change them, we
+       // simply ignore them.
+       return lzma_next_filter_update(
+                       &coder->next, allocator, reversed_filters + 1);
+}
+
+
 extern lzma_ret
 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
                const lzma_filter_info *filters)
 {
-       return lzma_delta_coder_init(next, allocator, filters, &delta_encode);
+       next->code = &delta_encode;
+       next->update = &delta_encoder_update;
+       return lzma_delta_coder_init(next, allocator, filters);
 }
 
 
index 69be82e2f7dd3d1c616ff76036f6f5f30731cb5e..62b7fed86e45fe98b9c83050e55ab1420e3ef65b 100644 (file)
@@ -32,6 +32,6 @@ struct lzma_coder_s {
 
 extern lzma_ret lzma_delta_coder_init(
                lzma_next_coder *next, lzma_allocator *allocator,
-               const lzma_filter_info *filters, lzma_code_function code);
+               const lzma_filter_info *filters);
 
 #endif
index 0e7b7d1dd024e27db023a9f21975fcd1e2be44c4..bf6327d899fea77442a9c1ae35aba5607421337f 100644 (file)
@@ -475,6 +475,22 @@ lz_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
 }
 
 
+static lzma_ret
+lz_encoder_update(lzma_coder *coder, lzma_allocator *allocator,
+               const lzma_filter *filters_null lzma_attribute((unused)),
+               const lzma_filter *reversed_filters)
+{
+       if (coder->lz.options_update == NULL)
+               return LZMA_PROG_ERROR;
+
+       return_if_error(coder->lz.options_update(
+                       coder->lz.coder, reversed_filters));
+
+       return lzma_next_filter_update(
+                       &coder->next, allocator, reversed_filters + 1);
+}
+
+
 extern lzma_ret
 lzma_lz_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
                const lzma_filter_info *filters,
@@ -495,6 +511,7 @@ lzma_lz_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
 
                next->code = &lz_encode;
                next->end = &lz_encoder_end;
+               next->update = &lz_encoder_update;
 
                next->coder->lz.coder = NULL;
                next->coder->lz.code = NULL;
index e7d3f875aab889649ad41e21106fd01aac311ade..401185ef37bab3159072ebf64cd6f9c3b918dc58 100644 (file)
@@ -201,6 +201,10 @@ typedef struct {
        /// Free allocated resources
        void (*end)(lzma_coder *coder, lzma_allocator *allocator);
 
+       /// Update the options in the middle of the encoding.
+       lzma_ret (*options_update)(lzma_coder *coder,
+                       const lzma_filter *filter);
+
 } lzma_lz_encoder;
 
 
index 8db813685f6be7a3ee41c84a0fd8bfb9cd4e76c3..aa3216cc5e269f64949721231d2ee9f07c71a383 100644 (file)
@@ -29,10 +29,6 @@ struct lzma_coder_s {
        /// LZMA encoder
        lzma_coder *lzma;
 
-       /// If this is not NULL, we will check new options from this
-       /// structure when starting a new chunk.
-       const lzma_options_lzma *opt_new;
-
        /// LZMA options currently in use.
        lzma_options_lzma opt_cur;
 
@@ -155,25 +151,6 @@ lzma2_encode(lzma_coder *restrict coder, lzma_mf *restrict mf,
                                        ? LZMA_OK : LZMA_STREAM_END;
                }
 
-               // Look if there are new options. At least for now,
-               // only lc/lp/pb can be changed.
-               if (coder->opt_new != NULL
-                               && (coder->opt_cur.lc != coder->opt_new->lc
-                               || coder->opt_cur.lp != coder->opt_new->lp
-                               || coder->opt_cur.pb != coder->opt_new->pb)) {
-                       // Options have been changed, copy them to opt_cur.
-                       // These get validated as part of
-                       // lzma_lzma_encoder_reset() below.
-                       coder->opt_cur.lc = coder->opt_new->lc;
-                       coder->opt_cur.lp = coder->opt_new->lp;
-                       coder->opt_cur.pb = coder->opt_new->pb;
-
-                       // We need to write the new options and reset
-                       // the encoder state.
-                       coder->need_properties = true;
-                       coder->need_state_reset = true;
-               }
-
                if (coder->need_state_reset)
                        return_if_error(lzma_lzma_encoder_reset(
                                        coder->lzma, &coder->opt_cur));
@@ -293,6 +270,39 @@ lzma2_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
 }
 
 
+static lzma_ret
+lzma2_encoder_options_update(lzma_coder *coder, const lzma_filter *filter)
+{
+       // New options can be set only when there is no incomplete chunk.
+        // This is the case at the beginning of the raw stream and right
+        // after LZMA_SYNC_FLUSH.
+        if (filter->options == NULL || coder->sequence != SEQ_INIT)
+               return LZMA_PROG_ERROR;
+
+       // Look if there are new options. At least for now,
+       // only lc/lp/pb can be changed.
+       const lzma_options_lzma *opt = filter->options;
+       if (coder->opt_cur.lc != opt->lc || coder->opt_cur.lp != opt->lp
+                       || coder->opt_cur.pb != opt->pb) {
+               // Validate the options.
+               if (opt->lc > LZMA_LCLP_MAX || opt->lp > LZMA_LCLP_MAX
+                               || opt->lc + opt->lp > LZMA_LCLP_MAX
+                               || opt->pb > LZMA_PB_MAX)
+                       return LZMA_OPTIONS_ERROR;
+
+               // The new options will be used when the encoder starts
+               // a new LZMA2 chunk.
+               coder->opt_cur.lc = opt->lc;
+               coder->opt_cur.lp = opt->lp;
+               coder->opt_cur.pb = opt->pb;
+               coder->need_properties = true;
+               coder->need_state_reset = true;
+       }
+
+       return LZMA_OK;
+}
+
+
 static lzma_ret
 lzma2_encoder_init(lzma_lz_encoder *lz, lzma_allocator *allocator,
                const void *options, lzma_lz_options *lz_options)
@@ -307,13 +317,12 @@ lzma2_encoder_init(lzma_lz_encoder *lz, lzma_allocator *allocator,
 
                lz->code = &lzma2_encode;
                lz->end = &lzma2_encoder_end;
+               lz->options_update = &lzma2_encoder_options_update;
 
                lz->coder->lzma = NULL;
        }
 
        lz->coder->opt_cur = *(const lzma_options_lzma *)(options);
-       lz->coder->opt_new = lz->coder->opt_cur.persistent
-                               ? options : NULL;
 
        lz->coder->sequence = SEQ_INIT;
        lz->coder->need_properties = true;
index 68900a9bed47a4c2031477aa403ef41530154480..c4c9c146f559104c8e1ba519ab2a040cf18bbb21 100644 (file)
@@ -33,7 +33,6 @@ lzma_lzma_preset(lzma_options_lzma *options, uint32_t preset)
        options->lp = LZMA_LP_DEFAULT;
        options->pb = LZMA_PB_DEFAULT;
 
-       options->persistent = false;
        options->mode = level <= 2 ? LZMA_MODE_FAST : LZMA_MODE_NORMAL;
 
        options->nice_len = level == 0 ? 8 : level <= 5 ? 32 : 64;
index 497949a3d0933b05f5d4bda116a1741b4cac7844..52c5ca6de0bca58c16b37b38a6e79a8673f91bc7 100644 (file)
@@ -210,6 +210,17 @@ simple_coder_end(lzma_coder *coder, lzma_allocator *allocator)
 }
 
 
+static lzma_ret
+simple_coder_update(lzma_coder *coder, lzma_allocator *allocator,
+               const lzma_filter *filters_null lzma_attribute((unused)),
+               const lzma_filter *reversed_filters)
+{
+       // No update support, just call the next filter in the chain.
+       return lzma_next_filter_update(
+                       &coder->next, allocator, reversed_filters + 1);
+}
+
+
 extern lzma_ret
 lzma_simple_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
                const lzma_filter_info *filters,
@@ -231,6 +242,7 @@ lzma_simple_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
 
                next->code = &simple_code;
                next->end = &simple_coder_end;
+               next->update = &simple_coder_update;
 
                next->coder->next = LZMA_NEXT_CODER_INIT;
                next->coder->filter = filter;
index 6fdf3a2612dd42662007751491330eead0cbe328..00b34a83ae1bbdb3a1da588dfd1a93941a0b64f2 100644 (file)
@@ -414,7 +414,6 @@ options_lzma(const char *str)
                .lc = LZMA_LC_DEFAULT,
                .lp = LZMA_LP_DEFAULT,
                .pb = LZMA_PB_DEFAULT,
-               .persistent = false,
                .mode = LZMA_MODE_NORMAL,
                .nice_len = 64,
                .mf = LZMA_MF_BT4,