1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file subblock_encoder.c
4 /// \brief Encoder of the Subblock filter
6 // Copyright (C) 2007, 2008 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "subblock_encoder.h"
21 #include "raw_encoder.h"
24 #define REPEAT_COUNT_MAX (1U << 28)
26 /// Number of bytes the data chunk being repeated must be before we care
27 /// about alignment. This is somewhat arbitrary. It just doesn't make sense
28 /// to waste bytes for alignment when the data chunk is very small.
30 /// TODO Rename and use this also for Subblock Data?
31 #define RLE_MIN_SIZE_FOR_ALIGN 3
33 #define write_byte(b) \
37 ++coder->alignment.out_pos; \
64 /// Pointer to the options given by the application. This is used
65 /// for two-way communication with the application.
66 lzma_options_subblock *options;
68 /// Position in various arrays.
71 /// Holds subblock.size - 1 or rle.size - 1 when encoding size
72 /// of Data or Repeat Count.
76 /// This is a copy of options->alignment, or
77 /// LZMA_SUBBLOCK_ALIGNMENT_DEFAULT if options is NULL.
80 /// Number of input bytes that we have already read but
81 /// not yet started writing out.
84 /// Number of input bytes which we have processed and started
85 /// writing out. 32-bit integer is enough since we care only
86 /// about the lowest bits when fixing alignment.
89 /// Number of bytes written out.
94 /// Pointer to allocated buffer holding the Data field
95 /// of Subblock Type "Data".
98 /// Number of bytes in the buffer.
101 /// Allocated size of the buffer.
106 /// Buffer to hold the data that may be coded with
107 /// Subblock Type `Repeating Data'.
108 uint8_t buffer[LZMA_SUBBLOCK_RLE_MAX];
110 /// Number of bytes in buffer[].
113 /// Number of times the first `size' bytes of buffer[]
114 /// will be repeated.
128 /// This is a copy of options->allow_subfilters. We use
129 /// this to verify that the application doesn't change
130 /// the value of allow_subfilters.
133 /// When this is true, application is not allowed to modify
134 /// options->subblock_mode. We may still modify it here.
137 /// True if we have encoded at least one byte of data with
141 /// Track the amount of input available once
142 /// LZMA_SUBFILTER_FINISH has been enabled.
143 /// This is needed for sanity checking (kind
144 /// of duplicating what common/code.c does).
147 /// Buffer for the Filter Flags field written after
148 /// the `Set Subfilter' indicator.
151 /// Size of Filter Flags field.
154 /// Pointers to Subfilter.
155 lzma_next_coder subcoder;
162 uint8_t buffer[LZMA_BUFFER_SIZE];
167 /// \brief Aligns the output buffer
169 /// Aligns the output buffer so that after skew bytes the output position is
170 /// a multiple of coder->alignment.multiple.
172 subblock_align(lzma_coder *coder, uint8_t *restrict out,
173 size_t *restrict out_pos, size_t out_size, uint32_t skew)
175 assert(*out_pos < out_size);
177 const uint32_t target = coder->alignment.in_pos
178 % coder->alignment.multiple;
180 while ((coder->alignment.out_pos + skew)
181 % coder->alignment.multiple != target) {
182 // Zero indicates padding.
185 // Check if output buffer got full and indicate it to
187 if (*out_pos == out_size)
191 coder->alignment.in_pos += coder->alignment.in_pending;
192 coder->alignment.in_pending = 0;
194 // Output buffer is not full.
199 /// \brief Checks if buffer contains repeated data
201 /// \param needle Buffer containing a single repeat chunk
202 /// \param needle_size Size of needle in bytes
203 /// \param buf Buffer to search for repeated needles
204 /// \param buf_chunks Buffer size is buf_chunks * needle_size.
206 /// \return True if the whole buf is filled with repeated needles.
209 is_repeating(const uint8_t *restrict needle, size_t needle_size,
210 const uint8_t *restrict buf, size_t buf_chunks)
212 while (buf_chunks-- != 0) {
213 if (memcmp(buf, needle, needle_size) != 0)
223 /// \brief Optimizes the repeating style and updates coder->sequence
225 subblock_rle_flush(lzma_coder *coder)
227 // The Subblock decoder can use memset() when the size of the data
228 // being repeated is one byte, so we check if the RLE buffer is
229 // filled with a single repeating byte.
230 if (coder->rle.size > 1) {
231 const uint8_t b = coder->rle.buffer[0];
234 if (coder->rle.buffer[i] != b)
237 if (++i == coder->rle.size) {
238 // TODO Integer overflow check maybe,
239 // although this needs at least 2**63 bytes
240 // of input until it gets triggered...
241 coder->rle.count *= coder->rle.size;
248 if (coder->rle.count > REPEAT_COUNT_MAX)
249 coder->tmp = REPEAT_COUNT_MAX - 1;
251 coder->tmp = coder->rle.count - 1;
253 coder->sequence = SEQ_RLE_COUNT_0;
259 /// \brief Resizes coder->subblock.data for a new size limit
261 subblock_data_size(lzma_coder *coder, lzma_allocator *allocator,
264 // Verify that the new limit is valid.
265 if (new_limit < LZMA_SUBBLOCK_DATA_SIZE_MIN
266 || new_limit > LZMA_SUBBLOCK_DATA_SIZE_MAX)
267 return LZMA_HEADER_ERROR;
269 // Ff the new limit is different than the previous one, we need
270 // to reallocate the data buffer.
271 if (new_limit != coder->subblock.limit) {
272 lzma_free(coder->subblock.data, allocator);
273 coder->subblock.data = lzma_alloc(new_limit, allocator);
274 if (coder->subblock.data == NULL)
275 return LZMA_MEM_ERROR;
278 coder->subblock.limit = new_limit;
285 subblock_buffer(lzma_coder *coder, lzma_allocator *allocator,
286 const uint8_t *restrict in, size_t *restrict in_pos,
287 size_t in_size, uint8_t *restrict out,
288 size_t *restrict out_pos, size_t out_size, lzma_action action)
290 // Changing allow_subfilter is not allowed.
291 if (coder->options != NULL && coder->subfilter.allow
292 != coder->options->allow_subfilters)
293 return LZMA_PROG_ERROR;
295 // Check if we need to do something special with the Subfilter.
296 if (coder->subfilter.allow) {
297 assert(coder->options != NULL);
299 // See if subfilter_mode has been changed.
300 switch (coder->options->subfilter_mode) {
301 case LZMA_SUBFILTER_NONE:
302 if (coder->subfilter.mode != SUB_NONE)
303 return LZMA_PROG_ERROR;
306 case LZMA_SUBFILTER_SET:
307 if (coder->subfilter.mode_locked
308 || coder->subfilter.mode != SUB_NONE)
309 return LZMA_PROG_ERROR;
311 coder->subfilter.mode = SUB_SET;
312 coder->subfilter.got_input = false;
314 if (coder->sequence == SEQ_FILL)
315 coder->sequence = SEQ_FLUSH;
319 case LZMA_SUBFILTER_RUN:
320 if (coder->subfilter.mode != SUB_RUN)
321 return LZMA_PROG_ERROR;
325 case LZMA_SUBFILTER_FINISH: {
326 const size_t in_avail = in_size - *in_pos;
328 if (coder->subfilter.mode == SUB_RUN) {
329 if (coder->subfilter.mode_locked)
330 return LZMA_PROG_ERROR;
332 coder->subfilter.mode = SUB_FINISH;
333 coder->subfilter.in_avail = in_avail;
335 } else if (coder->subfilter.mode != SUB_FINISH
336 || coder->subfilter.in_avail
338 return LZMA_PROG_ERROR;
345 return LZMA_HEADER_ERROR;
348 // If we are sync-flushing or finishing, the application may
349 // no longer change subfilter_mode. Note that this check is
350 // done after checking the new subfilter_mode above; this
351 // way the application may e.g. set LZMA_SUBFILTER_SET and
352 // LZMA_SYNC_FLUSH at the same time, but it cannot modify
353 // subfilter_mode on the later lzma_code() calls before
354 // we have returned LZMA_STREAM_END.
355 if (action != LZMA_RUN)
356 coder->subfilter.mode_locked = true;
360 while (*out_pos < out_size)
361 switch (coder->sequence) {
363 // Grab the new Subblock Data Size and reallocate the buffer.
364 if (coder->subblock.size == 0 && coder->options != NULL
365 && coder->options->subblock_data_size
366 != coder->subblock.limit)
367 return_if_error(subblock_data_size(coder,
368 allocator, coder->options
369 ->subblock_data_size));
371 if (coder->subfilter.mode == SUB_NONE) {
372 assert(coder->subfilter.subcoder.code == NULL);
374 // No Subfilter is enabled, just copy the data as is.
375 coder->alignment.in_pending += bufcpy(
377 coder->subblock.data,
378 &coder->subblock.size,
379 coder->subblock.limit);
381 // If we ran out of input before the whole buffer
382 // was filled, return to application.
383 if (coder->subblock.size < coder->subblock.limit
384 && action == LZMA_RUN)
388 assert(coder->options->subfilter_mode
389 != LZMA_SUBFILTER_SET);
391 // Using LZMA_FINISH automatically toggles
392 // LZMA_SUBFILTER_FINISH.
394 // NOTE: It is possible that application had set
395 // LZMA_SUBFILTER_SET and LZMA_FINISH at the same
396 // time. In that case it is possible that we will
397 // cycle to LZMA_SUBFILTER_RUN, LZMA_SUBFILTER_FINISH,
398 // and back to LZMA_SUBFILTER_NONE in a single
399 // Subblock encoder function call.
400 if (action == LZMA_FINISH) {
401 coder->options->subfilter_mode
402 = LZMA_SUBFILTER_FINISH;
403 coder->subfilter.mode = SUB_FINISH;
406 const size_t in_start = *in_pos;
408 const lzma_ret ret = coder->subfilter.subcoder.code(
409 coder->subfilter.subcoder.coder,
410 allocator, in, in_pos, in_size,
411 coder->subblock.data,
412 &coder->subblock.size,
413 coder->subblock.limit,
414 coder->subfilter.mode == SUB_FINISH
415 ? LZMA_FINISH : action);
417 const size_t in_used = *in_pos - in_start;
418 coder->alignment.in_pending += in_used;
420 coder->subfilter.got_input = true;
422 coder->subfilter.in_avail = in_size - *in_pos;
424 if (ret == LZMA_STREAM_END) {
425 // All currently available input must have
427 assert(*in_pos == in_size);
429 // Flush now. Even if coder->subblock.size
430 // happened to be zero, we still need to go
431 // to SEQ_FLUSH to possibly finish RLE or
432 // write the Subfilter Unset indicator.
433 coder->sequence = SEQ_FLUSH;
435 if (coder->subfilter.mode == SUB_RUN) {
436 // Flushing with Subfilter enabled.
437 assert(action == LZMA_SYNC_FLUSH);
438 coder->subfilter.mode = SUB_FLUSH;
442 // Subfilter finished its job.
443 assert(coder->subfilter.mode == SUB_FINISH
444 || action == LZMA_FINISH);
446 // At least one byte of input must have been
447 // encoded with the Subfilter. This is
448 // required by the file format specification.
449 if (!coder->subfilter.got_input)
450 return LZMA_PROG_ERROR;
452 // We don't strictly need to do this, but
453 // doing it sounds like a good idea, because
454 // otherwise the Subfilter's memory could be
455 // left allocated for long time, and would
456 // just waste memory.
457 lzma_next_coder_end(&coder->subfilter.subcoder,
460 // We need to flush the currently buffered
461 // data and write Unset Subfilter marker.
462 // Note that we cannot set
463 // coder->options->subfilter_mode to
464 // LZMA_SUBFILTER_NONE yet, because we
465 // haven't written the Unset Subfilter
467 coder->subfilter.mode = SUB_END_MARKER;
468 coder->sequence = SEQ_FLUSH;
472 // Return if we couldn't fill the buffer or
473 // if an error occurred.
474 if (coder->subblock.size < coder->subblock.limit
479 coder->sequence = SEQ_FLUSH;
481 // SEQ_FILL doesn't produce any output so falling through
482 // to SEQ_FLUSH is safe.
483 assert(*out_pos < out_size);
488 if (coder->options != NULL) {
489 // Update the alignment variable.
490 coder->alignment.multiple = coder->options->alignment;
491 if (coder->alignment.multiple
492 < LZMA_SUBBLOCK_ALIGNMENT_MIN
493 || coder->alignment.multiple
494 > LZMA_SUBBLOCK_ALIGNMENT_MAX)
495 return LZMA_HEADER_ERROR;
497 // Run-length encoder
499 // First check if there is some data pending and we
500 // have an obvious need to flush it immediatelly.
501 if (coder->rle.count > 0
503 != coder->options->rle
504 || coder->subblock.size
505 % coder->rle.size)) {
506 subblock_rle_flush(coder);
510 // Grab the (possibly new) RLE chunk size and
512 coder->rle.size = coder->options->rle;
513 if (coder->rle.size > LZMA_SUBBLOCK_RLE_MAX)
514 return LZMA_HEADER_ERROR;
516 if (coder->subblock.size != 0
518 != LZMA_SUBBLOCK_RLE_OFF
519 && coder->subblock.size
520 % coder->rle.size == 0) {
522 // Initialize coder->rle.buffer if we don't
523 // have RLE already running.
524 if (coder->rle.count == 0)
525 memcpy(coder->rle.buffer,
526 coder->subblock.data,
529 // Test if coder->subblock.data is repeating.
530 const size_t count = coder->subblock.size
532 if (is_repeating(coder->rle.buffer,
534 coder->subblock.data, count)) {
535 if (LZMA_VLI_VALUE_MAX - count
537 return LZMA_PROG_ERROR;
539 coder->rle.count += count;
540 coder->subblock.size = 0;
542 } else if (coder->rle.count > 0) {
543 // It's not repeating or at least not
544 // with the same byte sequence as the
545 // earlier Subblock Data buffers. We
546 // have some data pending in the RLE
547 // buffer already, so do a flush.
548 // Once flushed, we will check again
549 // if the Subblock Data happens to
550 // contain a different repeating
552 subblock_rle_flush(coder);
558 // If we now have some data left in coder->subblock, the RLE
559 // buffer is empty and we must write a regular Subblock Data.
560 if (coder->subblock.size > 0) {
561 assert(coder->rle.count == 0);
562 coder->tmp = coder->subblock.size - 1;
563 coder->sequence = SEQ_DATA_SIZE_0;
567 // Check if we should enable Subfilter.
568 if (coder->subfilter.mode == SUB_SET) {
569 if (coder->rle.count > 0)
570 subblock_rle_flush(coder);
572 coder->sequence = SEQ_SUBFILTER_INIT;
576 // Check if we have just finished Subfiltering.
577 if (coder->subfilter.mode == SUB_END_MARKER) {
578 if (coder->rle.count > 0) {
579 subblock_rle_flush(coder);
583 coder->options->subfilter_mode = LZMA_SUBFILTER_NONE;
584 coder->subfilter.mode = SUB_NONE;
587 if (*out_pos == out_size)
591 // Check if we have already written everything.
592 if (action != LZMA_RUN && *in_pos == in_size
593 && (coder->subfilter.mode == SUB_NONE
594 || coder->subfilter.mode == SUB_FLUSH)) {
595 if (coder->rle.count > 0) {
596 subblock_rle_flush(coder);
600 if (action == LZMA_SYNC_FLUSH) {
601 if (coder->subfilter.mode == SUB_FLUSH)
602 coder->subfilter.mode = SUB_RUN;
604 coder->subfilter.mode_locked = false;
605 coder->sequence = SEQ_FILL;
607 } else if (coder->use_eopm) {
608 assert(action == LZMA_FINISH);
610 // NOTE: No need to use write_byte() here
611 // since we are finishing.
612 out[*out_pos] = 0x10;
616 return LZMA_STREAM_END;
619 // Otherwise we have more work to do.
620 coder->sequence = SEQ_FILL;
623 case SEQ_RLE_COUNT_0:
624 // Make the Data field properly aligned, but only if the data
625 // chunk to be repeated isn't extremely small. We have four
626 // bytes for Count and one byte for Size, thus the number five.
627 if (coder->rle.size >= RLE_MIN_SIZE_FOR_ALIGN
629 coder, out, out_pos, out_size, 5))
632 assert(coder->rle.count > 0);
634 write_byte(0x30 | (coder->tmp & 0x0F));
636 coder->sequence = SEQ_RLE_COUNT_1;
639 case SEQ_RLE_COUNT_1:
640 write_byte(coder->tmp >> 4);
641 coder->sequence = SEQ_RLE_COUNT_2;
644 case SEQ_RLE_COUNT_2:
645 write_byte(coder->tmp >> 12);
646 coder->sequence = SEQ_RLE_COUNT_3;
649 case SEQ_RLE_COUNT_3:
650 write_byte(coder->tmp >> 20);
652 if (coder->rle.count > REPEAT_COUNT_MAX)
653 coder->rle.count -= REPEAT_COUNT_MAX;
655 coder->rle.count = 0;
657 coder->sequence = SEQ_RLE_SIZE;
661 assert(coder->rle.size >= LZMA_SUBBLOCK_RLE_MIN);
662 assert(coder->rle.size <= LZMA_SUBBLOCK_RLE_MAX);
663 write_byte(coder->rle.size - 1);
664 coder->sequence = SEQ_RLE_DATA;
668 bufcpy(coder->rle.buffer, &coder->pos, coder->rle.size,
669 out, out_pos, out_size);
670 if (coder->pos < coder->rle.size)
673 coder->alignment.out_pos += coder->rle.size;
676 coder->sequence = SEQ_FLUSH;
679 case SEQ_DATA_SIZE_0:
680 // We need four bytes for the Size field.
681 if (subblock_align(coder, out, out_pos, out_size, 4))
684 write_byte(0x20 | (coder->tmp & 0x0F));
685 coder->sequence = SEQ_DATA_SIZE_1;
688 case SEQ_DATA_SIZE_1:
689 write_byte(coder->tmp >> 4);
690 coder->sequence = SEQ_DATA_SIZE_2;
693 case SEQ_DATA_SIZE_2:
694 write_byte(coder->tmp >> 12);
695 coder->sequence = SEQ_DATA_SIZE_3;
698 case SEQ_DATA_SIZE_3:
699 write_byte(coder->tmp >> 20);
700 coder->sequence = SEQ_DATA;
704 bufcpy(coder->subblock.data, &coder->pos,
705 coder->subblock.size, out, out_pos, out_size);
706 if (coder->pos < coder->subblock.size)
709 coder->alignment.out_pos += coder->subblock.size;
710 coder->subblock.size = 0;
712 coder->sequence = SEQ_FLUSH;
715 case SEQ_SUBFILTER_INIT: {
716 assert(coder->subblock.size == 0);
717 assert(coder->rle.count == 0);
718 assert(coder->subfilter.mode == SUB_SET);
719 assert(coder->options != NULL);
721 // There must be a filter specified.
722 if (coder->options->subfilter_options.id
723 == LZMA_VLI_VALUE_UNKNOWN)
724 return LZMA_HEADER_ERROR;
726 // Initialize a raw encoder to work as a Subfilter.
727 lzma_options_filter options[2];
728 options[0] = coder->options->subfilter_options;
729 options[1].id = LZMA_VLI_VALUE_UNKNOWN;
731 return_if_error(lzma_raw_encoder_init(
732 &coder->subfilter.subcoder, allocator,
733 options, LZMA_VLI_VALUE_UNKNOWN, false));
735 // Encode the Filter Flags field into a buffer. This should
736 // never fail since we have already successfully initialized
737 // the Subfilter itself. Check it still, and return
738 // LZMA_PROG_ERROR instead of whatever the ret would say.
739 lzma_ret ret = lzma_filter_flags_size(
740 &coder->subfilter.flags_size, options);
741 assert(ret == LZMA_OK);
743 return LZMA_PROG_ERROR;
745 coder->subfilter.flags = lzma_alloc(
746 coder->subfilter.flags_size, allocator);
747 if (coder->subfilter.flags == NULL)
748 return LZMA_MEM_ERROR;
750 // Now we have a big-enough buffer. Encode the Filter Flags.
751 // Like above, this should never fail.
753 ret = lzma_filter_flags_encode(coder->subfilter.flags,
754 &dummy, coder->subfilter.flags_size, options);
755 assert(ret == LZMA_OK);
756 assert(dummy == coder->subfilter.flags_size);
757 if (ret != LZMA_OK || dummy != coder->subfilter.flags_size)
758 return LZMA_PROG_ERROR;
760 // Write a Subblock indicating a new Subfilter.
763 coder->options->subfilter_mode = LZMA_SUBFILTER_RUN;
764 coder->subfilter.mode = SUB_RUN;
765 coder->alignment.out_pos += coder->subfilter.flags_size;
766 coder->sequence = SEQ_SUBFILTER_FLAGS;
768 // It is safe to fall through because SEQ_SUBFILTER_FLAGS
769 // uses bufcpy() which doesn't write unless there is output
775 case SEQ_SUBFILTER_FLAGS:
776 // Copy the Filter Flags to the output stream.
777 bufcpy(coder->subfilter.flags, &coder->pos,
778 coder->subfilter.flags_size,
779 out, out_pos, out_size);
780 if (coder->pos < coder->subfilter.flags_size)
783 lzma_free(coder->subfilter.flags, allocator);
784 coder->subfilter.flags = NULL;
787 coder->sequence = SEQ_FILL;
791 return LZMA_PROG_ERROR;
799 subblock_encode(lzma_coder *coder, lzma_allocator *allocator,
800 const uint8_t *restrict in, size_t *restrict in_pos,
801 size_t in_size, uint8_t *restrict out,
802 size_t *restrict out_pos, size_t out_size, lzma_action action)
804 if (coder->next.code == NULL)
805 return subblock_buffer(coder, allocator, in, in_pos, in_size,
806 out, out_pos, out_size, action);
808 while (*out_pos < out_size
809 && (*in_pos < in_size || action != LZMA_RUN)) {
810 if (!coder->next_finished
811 && coder->temp.pos == coder->temp.size) {
813 coder->temp.size = 0;
815 const lzma_ret ret = coder->next.code(coder->next.coder,
816 allocator, in, in_pos, in_size,
817 coder->temp.buffer, &coder->temp.size,
818 LZMA_BUFFER_SIZE, action);
819 if (ret == LZMA_STREAM_END) {
820 assert(action != LZMA_RUN);
821 coder->next_finished = true;
822 } else if (coder->temp.size == 0 || ret != LZMA_OK) {
827 const lzma_ret ret = subblock_buffer(coder, allocator,
828 coder->temp.buffer, &coder->temp.pos,
829 coder->temp.size, out, out_pos, out_size,
830 coder->next_finished ? LZMA_FINISH : LZMA_RUN);
831 if (ret == LZMA_STREAM_END) {
832 assert(action != LZMA_RUN);
833 assert(coder->next_finished);
834 return LZMA_STREAM_END;
846 subblock_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
848 lzma_next_coder_end(&coder->next, allocator);
849 lzma_next_coder_end(&coder->subfilter.subcoder, allocator);
850 lzma_free(coder->subblock.data, allocator);
851 lzma_free(coder->subfilter.flags, allocator);
857 lzma_subblock_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
858 const lzma_filter_info *filters)
860 if (next->coder == NULL) {
861 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
862 if (next->coder == NULL)
863 return LZMA_MEM_ERROR;
865 next->code = &subblock_encode;
866 next->end = &subblock_encoder_end;
868 next->coder->next = LZMA_NEXT_CODER_INIT;
869 next->coder->subblock.data = NULL;
870 next->coder->subblock.limit = 0;
871 next->coder->subfilter.subcoder = LZMA_NEXT_CODER_INIT;
873 lzma_next_coder_end(&next->coder->subfilter.subcoder,
875 lzma_free(next->coder->subfilter.flags, allocator);
878 next->coder->subfilter.flags = NULL;
880 next->coder->next_finished = false;
881 next->coder->sequence = SEQ_FILL;
882 next->coder->options = filters[0].options;
883 next->coder->use_eopm = filters[0].uncompressed_size
884 == LZMA_VLI_VALUE_UNKNOWN;
885 next->coder->pos = 0;
887 next->coder->alignment.in_pending = 0;
888 next->coder->alignment.in_pos = 0;
889 next->coder->alignment.out_pos = 0;
890 next->coder->subblock.size = 0;
891 next->coder->rle.count = 0;
892 next->coder->subfilter.mode = SUB_NONE;
893 next->coder->subfilter.mode_locked = false;
895 next->coder->temp.pos = 0;
896 next->coder->temp.size = 0;
898 // Grab some values from the options structure if it is available.
899 size_t subblock_size_limit;
900 if (next->coder->options != NULL) {
901 if (next->coder->options->alignment
902 < LZMA_SUBBLOCK_ALIGNMENT_MIN
903 || next->coder->options->alignment
904 > LZMA_SUBBLOCK_ALIGNMENT_MAX) {
905 subblock_encoder_end(next->coder, allocator);
906 return LZMA_HEADER_ERROR;
908 next->coder->alignment.multiple
909 = next->coder->options->alignment;
910 next->coder->subfilter.allow
911 = next->coder->options->allow_subfilters;
912 subblock_size_limit = next->coder->options->subblock_data_size;
914 next->coder->alignment.multiple
915 = LZMA_SUBBLOCK_ALIGNMENT_DEFAULT;
916 next->coder->subfilter.allow = false;
917 subblock_size_limit = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT;
920 return_if_error(subblock_data_size(next->coder, allocator,
921 subblock_size_limit));
923 return lzma_next_filter_init(
924 &next->coder->next, allocator, filters + 1);