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 /// Maximum number of repeats that a single Repeating Data can indicate.
25 /// This is directly from the file format specification.
26 #define REPEAT_COUNT_MAX (1U << 28)
28 /// Number of bytes the data chunk (not including the header part) must be
29 /// before we care about alignment. This is somewhat arbitrary. It just
30 /// doesn't make sense to waste bytes for alignment when the data chunk
32 #define MIN_CHUNK_SIZE_FOR_ALIGN 4
34 /// Number of bytes of the header part of Subblock Type `Data'. This is
35 /// used as the `skew' argument for subblock_align().
36 #define ALIGN_SKEW_DATA 4
38 /// Like above but for Repeating Data.
39 #define ALIGN_SKEW_REPEATING_DATA 5
41 /// Writes one byte to output buffer and updates the alignment counter.
42 #define write_byte(b) \
44 assert(*out_pos < out_size); \
47 ++coder->alignment.out_pos; \
74 /// Pointer to the options given by the application. This is used
75 /// for two-way communication with the application.
76 lzma_options_subblock *options;
78 /// Position in various arrays.
81 /// Holds subblock.size - 1 or rle.size - 1 when encoding size
82 /// of Data or Repeat Count.
86 /// This is a copy of options->alignment, or
87 /// LZMA_SUBBLOCK_ALIGNMENT_DEFAULT if options is NULL.
90 /// Number of input bytes which we have processed and started
91 /// writing out. 32-bit integer is enough since we care only
92 /// about the lowest bits when fixing alignment.
95 /// Number of bytes written out.
100 /// Pointer to allocated buffer holding the Data field
101 /// of Subblock Type "Data".
104 /// Number of bytes in the buffer.
107 /// Allocated size of the buffer.
110 /// Number of input bytes that we have already read but
111 /// not yet started writing out. This can be different
112 /// to `size' when using Subfilter. That's why we track
113 /// in_pending separately for RLE (see below).
118 /// Buffer to hold the data that may be coded with
119 /// Subblock Type `Repeating Data'.
120 uint8_t buffer[LZMA_SUBBLOCK_RLE_MAX];
122 /// Number of bytes in buffer[].
125 /// Number of times the first `size' bytes of buffer[]
126 /// will be repeated.
129 /// Like subblock.in_pending above, but for RLE.
143 /// This is a copy of options->allow_subfilters. We use
144 /// this to verify that the application doesn't change
145 /// the value of allow_subfilters.
148 /// When this is true, application is not allowed to modify
149 /// options->subblock_mode. We may still modify it here.
152 /// True if we have encoded at least one byte of data with
156 /// Track the amount of input available once
157 /// LZMA_SUBFILTER_FINISH has been enabled.
158 /// This is needed for sanity checking (kind
159 /// of duplicating what common/code.c does).
162 /// Buffer for the Filter Flags field written after
163 /// the `Set Subfilter' indicator.
166 /// Size of Filter Flags field.
169 /// Pointers to Subfilter.
170 lzma_next_coder subcoder;
174 /// Temporary buffer used when we are not the last filter in the chain.
178 uint8_t buffer[LZMA_BUFFER_SIZE];
183 /// \brief Aligns the output buffer
185 /// Aligns the output buffer so that after skew bytes the output position is
186 /// a multiple of coder->alignment.multiple.
188 subblock_align(lzma_coder *coder, uint8_t *restrict out,
189 size_t *restrict out_pos, size_t out_size,
190 size_t chunk_size, uint32_t skew)
192 assert(*out_pos < out_size);
194 // Fix the alignment only if it makes sense at least a little.
195 if (chunk_size >= MIN_CHUNK_SIZE_FOR_ALIGN) {
196 const uint32_t target = coder->alignment.in_pos
197 % coder->alignment.multiple;
199 while ((coder->alignment.out_pos + skew)
200 % coder->alignment.multiple != target) {
201 // Zero indicates padding.
204 // Check if output buffer got full and indicate it to
206 if (*out_pos == out_size)
211 // Output buffer is not full.
216 /// \brief Checks if buffer contains repeated data
218 /// \param needle Buffer containing a single repeat chunk
219 /// \param needle_size Size of needle in bytes
220 /// \param buf Buffer to search for repeated needles
221 /// \param buf_chunks Buffer size is buf_chunks * needle_size.
223 /// \return True if the whole buf is filled with repeated needles.
226 is_repeating(const uint8_t *restrict needle, size_t needle_size,
227 const uint8_t *restrict buf, size_t buf_chunks)
229 while (buf_chunks-- != 0) {
230 if (memcmp(buf, needle, needle_size) != 0)
240 /// \brief Optimizes the repeating style and updates coder->sequence
242 subblock_rle_flush(lzma_coder *coder)
244 // The Subblock decoder can use memset() when the size of the data
245 // being repeated is one byte, so we check if the RLE buffer is
246 // filled with a single repeating byte.
247 if (coder->rle.size > 1) {
248 const uint8_t b = coder->rle.buffer[0];
251 if (coder->rle.buffer[i] != b)
254 if (++i == coder->rle.size) {
255 // TODO Integer overflow check maybe,
256 // although this needs at least 2**63 bytes
257 // of input until it gets triggered...
258 coder->rle.count *= coder->rle.size;
265 if (coder->rle.count == 1) {
266 // The buffer should be repeated only once. It is
267 // waste of space to use Repeating Data. Instead,
268 // write a regular Data Subblock. See SEQ_RLE_COUNT_0
269 // in subblock_buffer() for more info.
270 coder->tmp = coder->rle.size - 1;
271 } else if (coder->rle.count > REPEAT_COUNT_MAX) {
272 // There's so much to repeat that it doesn't fit into
273 // 28-bit integer. We will write two or more Subblocks
274 // of type Repeating Data.
275 coder->tmp = REPEAT_COUNT_MAX - 1;
277 coder->tmp = coder->rle.count - 1;
280 coder->sequence = SEQ_RLE_COUNT_0;
286 /// \brief Resizes coder->subblock.data for a new size limit
288 subblock_data_size(lzma_coder *coder, lzma_allocator *allocator,
291 // Verify that the new limit is valid.
292 if (new_limit < LZMA_SUBBLOCK_DATA_SIZE_MIN
293 || new_limit > LZMA_SUBBLOCK_DATA_SIZE_MAX)
294 return LZMA_HEADER_ERROR;
296 // Ff the new limit is different than the previous one, we need
297 // to reallocate the data buffer.
298 if (new_limit != coder->subblock.limit) {
299 lzma_free(coder->subblock.data, allocator);
300 coder->subblock.data = lzma_alloc(new_limit, allocator);
301 if (coder->subblock.data == NULL)
302 return LZMA_MEM_ERROR;
305 coder->subblock.limit = new_limit;
312 subblock_buffer(lzma_coder *coder, lzma_allocator *allocator,
313 const uint8_t *restrict in, size_t *restrict in_pos,
314 size_t in_size, uint8_t *restrict out,
315 size_t *restrict out_pos, size_t out_size, lzma_action action)
317 // Changing allow_subfilter is not allowed.
318 if (coder->options != NULL && coder->subfilter.allow
319 != coder->options->allow_subfilters)
320 return LZMA_PROG_ERROR;
322 // Check if we need to do something special with the Subfilter.
323 if (coder->subfilter.allow) {
324 assert(coder->options != NULL);
326 // See if subfilter_mode has been changed.
327 switch (coder->options->subfilter_mode) {
328 case LZMA_SUBFILTER_NONE:
329 if (coder->subfilter.mode != SUB_NONE)
330 return LZMA_PROG_ERROR;
333 case LZMA_SUBFILTER_SET:
334 if (coder->subfilter.mode_locked
335 || coder->subfilter.mode != SUB_NONE)
336 return LZMA_PROG_ERROR;
338 coder->subfilter.mode = SUB_SET;
339 coder->subfilter.got_input = false;
341 if (coder->sequence == SEQ_FILL)
342 coder->sequence = SEQ_FLUSH;
346 case LZMA_SUBFILTER_RUN:
347 if (coder->subfilter.mode != SUB_RUN)
348 return LZMA_PROG_ERROR;
352 case LZMA_SUBFILTER_FINISH: {
353 const size_t in_avail = in_size - *in_pos;
355 if (coder->subfilter.mode == SUB_RUN) {
356 if (coder->subfilter.mode_locked)
357 return LZMA_PROG_ERROR;
359 coder->subfilter.mode = SUB_FINISH;
360 coder->subfilter.in_avail = in_avail;
362 } else if (coder->subfilter.mode != SUB_FINISH
363 || coder->subfilter.in_avail
365 return LZMA_PROG_ERROR;
372 return LZMA_HEADER_ERROR;
375 // If we are sync-flushing or finishing, the application may
376 // no longer change subfilter_mode. Note that this check is
377 // done after checking the new subfilter_mode above; this
378 // way the application may e.g. set LZMA_SUBFILTER_SET and
379 // LZMA_SYNC_FLUSH at the same time, but it cannot modify
380 // subfilter_mode on the later lzma_code() calls before
381 // we have returned LZMA_STREAM_END.
382 if (action != LZMA_RUN)
383 coder->subfilter.mode_locked = true;
387 while (*out_pos < out_size)
388 switch (coder->sequence) {
390 // Grab the new Subblock Data Size and reallocate the buffer.
391 if (coder->subblock.size == 0 && coder->options != NULL
392 && coder->options->subblock_data_size
393 != coder->subblock.limit)
394 return_if_error(subblock_data_size(coder,
395 allocator, coder->options
396 ->subblock_data_size));
398 if (coder->subfilter.mode == SUB_NONE) {
399 assert(coder->subfilter.subcoder.code == NULL);
401 // No Subfilter is enabled, just copy the data as is.
402 coder->subblock.in_pending += bufcpy(
404 coder->subblock.data,
405 &coder->subblock.size,
406 coder->subblock.limit);
408 // If we ran out of input before the whole buffer
409 // was filled, return to application.
410 if (coder->subblock.size < coder->subblock.limit
411 && action == LZMA_RUN)
415 assert(coder->options->subfilter_mode
416 != LZMA_SUBFILTER_SET);
418 // Using LZMA_FINISH automatically toggles
419 // LZMA_SUBFILTER_FINISH.
421 // NOTE: It is possible that application had set
422 // LZMA_SUBFILTER_SET and LZMA_FINISH at the same
423 // time. In that case it is possible that we will
424 // cycle to LZMA_SUBFILTER_RUN, LZMA_SUBFILTER_FINISH,
425 // and back to LZMA_SUBFILTER_NONE in a single
426 // Subblock encoder function call.
427 if (action == LZMA_FINISH) {
428 coder->options->subfilter_mode
429 = LZMA_SUBFILTER_FINISH;
430 coder->subfilter.mode = SUB_FINISH;
433 const size_t in_start = *in_pos;
435 const lzma_ret ret = coder->subfilter.subcoder.code(
436 coder->subfilter.subcoder.coder,
437 allocator, in, in_pos, in_size,
438 coder->subblock.data,
439 &coder->subblock.size,
440 coder->subblock.limit,
441 coder->subfilter.mode == SUB_FINISH
442 ? LZMA_FINISH : action);
444 const size_t in_used = *in_pos - in_start;
445 coder->subblock.in_pending += in_used;
447 coder->subfilter.got_input = true;
449 coder->subfilter.in_avail = in_size - *in_pos;
451 if (ret == LZMA_STREAM_END) {
452 // All currently available input must have
454 assert(*in_pos == in_size);
456 // Flush now. Even if coder->subblock.size
457 // happened to be zero, we still need to go
458 // to SEQ_FLUSH to possibly finish RLE or
459 // write the Subfilter Unset indicator.
460 coder->sequence = SEQ_FLUSH;
462 if (coder->subfilter.mode == SUB_RUN) {
463 // Flushing with Subfilter enabled.
464 assert(action == LZMA_SYNC_FLUSH);
465 coder->subfilter.mode = SUB_FLUSH;
469 // Subfilter finished its job.
470 assert(coder->subfilter.mode == SUB_FINISH
471 || action == LZMA_FINISH);
473 // At least one byte of input must have been
474 // encoded with the Subfilter. This is
475 // required by the file format specification.
476 if (!coder->subfilter.got_input)
477 return LZMA_PROG_ERROR;
479 // We don't strictly need to do this, but
480 // doing it sounds like a good idea, because
481 // otherwise the Subfilter's memory could be
482 // left allocated for long time, and would
483 // just waste memory.
484 lzma_next_coder_end(&coder->subfilter.subcoder,
487 // We need to flush the currently buffered
488 // data and write Unset Subfilter marker.
489 // Note that we cannot set
490 // coder->options->subfilter_mode to
491 // LZMA_SUBFILTER_NONE yet, because we
492 // haven't written the Unset Subfilter
494 coder->subfilter.mode = SUB_END_MARKER;
495 coder->sequence = SEQ_FLUSH;
499 // Return if we couldn't fill the buffer or
500 // if an error occurred.
501 if (coder->subblock.size < coder->subblock.limit
506 coder->sequence = SEQ_FLUSH;
508 // SEQ_FILL doesn't produce any output so falling through
509 // to SEQ_FLUSH is safe.
510 assert(*out_pos < out_size);
515 if (coder->options != NULL) {
516 // Update the alignment variable.
517 coder->alignment.multiple = coder->options->alignment;
518 if (coder->alignment.multiple
519 < LZMA_SUBBLOCK_ALIGNMENT_MIN
520 || coder->alignment.multiple
521 > LZMA_SUBBLOCK_ALIGNMENT_MAX)
522 return LZMA_HEADER_ERROR;
524 // Run-length encoder
526 // First check if there is some data pending and we
527 // have an obvious need to flush it immediatelly.
528 if (coder->rle.count > 0
530 != coder->options->rle
531 || coder->subblock.size
532 % coder->rle.size)) {
533 subblock_rle_flush(coder);
537 // Grab the (possibly new) RLE chunk size and
539 coder->rle.size = coder->options->rle;
540 if (coder->rle.size > LZMA_SUBBLOCK_RLE_MAX)
541 return LZMA_HEADER_ERROR;
543 if (coder->subblock.size != 0
545 != LZMA_SUBBLOCK_RLE_OFF
546 && coder->subblock.size
547 % coder->rle.size == 0) {
549 // Initialize coder->rle.buffer if we don't
550 // have RLE already running.
551 if (coder->rle.count == 0)
552 memcpy(coder->rle.buffer,
553 coder->subblock.data,
556 // Test if coder->subblock.data is repeating.
557 // If coder->rle.count would overflow, we
558 // force flushing. Forced flushing shouldn't
559 // really happen in real-world situations.
560 const size_t count = coder->subblock.size
562 if (UINT64_MAX - count > coder->rle.count
566 coder->subblock.data,
568 coder->rle.count += count;
569 coder->rle.in_pending += coder
570 ->subblock.in_pending;
571 coder->subblock.in_pending = 0;
572 coder->subblock.size = 0;
574 } else if (coder->rle.count > 0) {
575 // It's not repeating or at least not
576 // with the same byte sequence as the
577 // earlier Subblock Data buffers. We
578 // have some data pending in the RLE
579 // buffer already, so do a flush.
580 // Once flushed, we will check again
581 // if the Subblock Data happens to
582 // contain a different repeating
584 subblock_rle_flush(coder);
590 // If we now have some data left in coder->subblock, the RLE
591 // buffer is empty and we must write a regular Subblock Data.
592 if (coder->subblock.size > 0) {
593 assert(coder->rle.count == 0);
594 coder->tmp = coder->subblock.size - 1;
595 coder->sequence = SEQ_DATA_SIZE_0;
599 // Check if we should enable Subfilter.
600 if (coder->subfilter.mode == SUB_SET) {
601 if (coder->rle.count > 0)
602 subblock_rle_flush(coder);
604 coder->sequence = SEQ_SUBFILTER_INIT;
608 // Check if we have just finished Subfiltering.
609 if (coder->subfilter.mode == SUB_END_MARKER) {
610 if (coder->rle.count > 0) {
611 subblock_rle_flush(coder);
615 coder->options->subfilter_mode = LZMA_SUBFILTER_NONE;
616 coder->subfilter.mode = SUB_NONE;
619 if (*out_pos == out_size)
623 // Check if we have already written everything.
624 if (action != LZMA_RUN && *in_pos == in_size
625 && (coder->subfilter.mode == SUB_NONE
626 || coder->subfilter.mode == SUB_FLUSH)) {
627 if (coder->rle.count > 0) {
628 subblock_rle_flush(coder);
632 if (action == LZMA_SYNC_FLUSH) {
633 if (coder->subfilter.mode == SUB_FLUSH)
634 coder->subfilter.mode = SUB_RUN;
636 coder->subfilter.mode_locked = false;
637 coder->sequence = SEQ_FILL;
639 } else if (coder->use_eopm) {
640 assert(action == LZMA_FINISH);
642 // NOTE: No need to use write_byte() here
643 // since we are finishing.
644 out[*out_pos] = 0x10;
648 return LZMA_STREAM_END;
651 // Otherwise we have more work to do.
652 coder->sequence = SEQ_FILL;
655 case SEQ_RLE_COUNT_0:
656 assert(coder->rle.count > 0);
658 if (coder->rle.count == 1) {
659 // The buffer should be repeated only once. Fix
660 // the alignment and write the first byte of
661 // Subblock Type `Data'.
662 if (subblock_align(coder, out, out_pos, out_size,
663 coder->rle.size, ALIGN_SKEW_DATA))
666 write_byte(0x20 | (coder->tmp & 0x0F));
669 // We have something to actually repeat, which should
670 // mean that it takes less space with run-length
672 if (subblock_align(coder, out, out_pos, out_size,
674 ALIGN_SKEW_REPEATING_DATA))
677 write_byte(0x30 | (coder->tmp & 0x0F));
680 // NOTE: If we have to write more than one Repeating Data
681 // due to rle.count > REPEAT_COUNT_MAX, the subsequent
682 // Repeating Data Subblocks may get wrong alignment, because
683 // we add rle.in_pending to alignment.in_pos at once instead
684 // of adding only as much as this particular Repeating Data
685 // consumed input data. Correct alignment is always restored
686 // after all the required Repeating Data Subblocks have been
687 // written. This problem occurs in such a weird cases that
688 // it's not worth fixing.
689 coder->alignment.out_pos += coder->rle.size;
690 coder->alignment.in_pos += coder->rle.in_pending;
691 coder->rle.in_pending = 0;
693 coder->sequence = SEQ_RLE_COUNT_1;
696 case SEQ_RLE_COUNT_1:
697 write_byte(coder->tmp >> 4);
698 coder->sequence = SEQ_RLE_COUNT_2;
701 case SEQ_RLE_COUNT_2:
702 write_byte(coder->tmp >> 12);
703 coder->sequence = SEQ_RLE_COUNT_3;
706 case SEQ_RLE_COUNT_3:
707 write_byte(coder->tmp >> 20);
709 // Again, see if we are writing regular Data or Repeating Data.
710 // In the former case, we skip SEQ_RLE_SIZE.
711 if (coder->rle.count == 1)
712 coder->sequence = SEQ_RLE_DATA;
714 coder->sequence = SEQ_RLE_SIZE;
716 if (coder->rle.count > REPEAT_COUNT_MAX)
717 coder->rle.count -= REPEAT_COUNT_MAX;
719 coder->rle.count = 0;
724 assert(coder->rle.size >= LZMA_SUBBLOCK_RLE_MIN);
725 assert(coder->rle.size <= LZMA_SUBBLOCK_RLE_MAX);
726 write_byte(coder->rle.size - 1);
727 coder->sequence = SEQ_RLE_DATA;
731 bufcpy(coder->rle.buffer, &coder->pos, coder->rle.size,
732 out, out_pos, out_size);
733 if (coder->pos < coder->rle.size)
737 coder->sequence = SEQ_FLUSH;
740 case SEQ_DATA_SIZE_0:
741 // We need four bytes for the Size field.
742 if (subblock_align(coder, out, out_pos, out_size,
743 coder->subblock.size, ALIGN_SKEW_DATA))
746 coder->alignment.out_pos += coder->subblock.size;
747 coder->alignment.in_pos += coder->subblock.in_pending;
748 coder->subblock.in_pending = 0;
750 write_byte(0x20 | (coder->tmp & 0x0F));
751 coder->sequence = SEQ_DATA_SIZE_1;
754 case SEQ_DATA_SIZE_1:
755 write_byte(coder->tmp >> 4);
756 coder->sequence = SEQ_DATA_SIZE_2;
759 case SEQ_DATA_SIZE_2:
760 write_byte(coder->tmp >> 12);
761 coder->sequence = SEQ_DATA_SIZE_3;
764 case SEQ_DATA_SIZE_3:
765 write_byte(coder->tmp >> 20);
766 coder->sequence = SEQ_DATA;
770 bufcpy(coder->subblock.data, &coder->pos,
771 coder->subblock.size, out, out_pos, out_size);
772 if (coder->pos < coder->subblock.size)
775 coder->subblock.size = 0;
777 coder->sequence = SEQ_FLUSH;
780 case SEQ_SUBFILTER_INIT: {
781 assert(coder->subblock.size == 0);
782 assert(coder->subblock.in_pending == 0);
783 assert(coder->rle.count == 0);
784 assert(coder->rle.in_pending == 0);
785 assert(coder->subfilter.mode == SUB_SET);
786 assert(coder->options != NULL);
788 // There must be a filter specified.
789 if (coder->options->subfilter_options.id
790 == LZMA_VLI_VALUE_UNKNOWN)
791 return LZMA_HEADER_ERROR;
793 // Initialize a raw encoder to work as a Subfilter.
794 lzma_options_filter options[2];
795 options[0] = coder->options->subfilter_options;
796 options[1].id = LZMA_VLI_VALUE_UNKNOWN;
798 return_if_error(lzma_raw_encoder_init(
799 &coder->subfilter.subcoder, allocator,
800 options, LZMA_VLI_VALUE_UNKNOWN, false));
802 // Encode the Filter Flags field into a buffer. This should
803 // never fail since we have already successfully initialized
804 // the Subfilter itself. Check it still, and return
805 // LZMA_PROG_ERROR instead of whatever the ret would say.
806 lzma_ret ret = lzma_filter_flags_size(
807 &coder->subfilter.flags_size, options);
808 assert(ret == LZMA_OK);
810 return LZMA_PROG_ERROR;
812 coder->subfilter.flags = lzma_alloc(
813 coder->subfilter.flags_size, allocator);
814 if (coder->subfilter.flags == NULL)
815 return LZMA_MEM_ERROR;
817 // Now we have a big-enough buffer. Encode the Filter Flags.
818 // Like above, this should never fail.
820 ret = lzma_filter_flags_encode(coder->subfilter.flags,
821 &dummy, coder->subfilter.flags_size, options);
822 assert(ret == LZMA_OK);
823 assert(dummy == coder->subfilter.flags_size);
824 if (ret != LZMA_OK || dummy != coder->subfilter.flags_size)
825 return LZMA_PROG_ERROR;
827 // Write a Subblock indicating a new Subfilter.
830 coder->options->subfilter_mode = LZMA_SUBFILTER_RUN;
831 coder->subfilter.mode = SUB_RUN;
832 coder->alignment.out_pos += coder->subfilter.flags_size;
833 coder->sequence = SEQ_SUBFILTER_FLAGS;
835 // It is safe to fall through because SEQ_SUBFILTER_FLAGS
836 // uses bufcpy() which doesn't write unless there is output
842 case SEQ_SUBFILTER_FLAGS:
843 // Copy the Filter Flags to the output stream.
844 bufcpy(coder->subfilter.flags, &coder->pos,
845 coder->subfilter.flags_size,
846 out, out_pos, out_size);
847 if (coder->pos < coder->subfilter.flags_size)
850 lzma_free(coder->subfilter.flags, allocator);
851 coder->subfilter.flags = NULL;
854 coder->sequence = SEQ_FILL;
858 return LZMA_PROG_ERROR;
866 subblock_encode(lzma_coder *coder, lzma_allocator *allocator,
867 const uint8_t *restrict in, size_t *restrict in_pos,
868 size_t in_size, uint8_t *restrict out,
869 size_t *restrict out_pos, size_t out_size, lzma_action action)
871 if (coder->next.code == NULL)
872 return subblock_buffer(coder, allocator, in, in_pos, in_size,
873 out, out_pos, out_size, action);
875 while (*out_pos < out_size
876 && (*in_pos < in_size || action != LZMA_RUN)) {
877 if (!coder->next_finished
878 && coder->temp.pos == coder->temp.size) {
880 coder->temp.size = 0;
882 const lzma_ret ret = coder->next.code(coder->next.coder,
883 allocator, in, in_pos, in_size,
884 coder->temp.buffer, &coder->temp.size,
885 LZMA_BUFFER_SIZE, action);
886 if (ret == LZMA_STREAM_END) {
887 assert(action != LZMA_RUN);
888 coder->next_finished = true;
889 } else if (coder->temp.size == 0 || ret != LZMA_OK) {
894 const lzma_ret ret = subblock_buffer(coder, allocator,
895 coder->temp.buffer, &coder->temp.pos,
896 coder->temp.size, out, out_pos, out_size,
897 coder->next_finished ? LZMA_FINISH : LZMA_RUN);
898 if (ret == LZMA_STREAM_END) {
899 assert(action != LZMA_RUN);
900 assert(coder->next_finished);
901 return LZMA_STREAM_END;
913 subblock_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
915 lzma_next_coder_end(&coder->next, allocator);
916 lzma_next_coder_end(&coder->subfilter.subcoder, allocator);
917 lzma_free(coder->subblock.data, allocator);
918 lzma_free(coder->subfilter.flags, allocator);
924 lzma_subblock_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
925 const lzma_filter_info *filters)
927 if (next->coder == NULL) {
928 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
929 if (next->coder == NULL)
930 return LZMA_MEM_ERROR;
932 next->code = &subblock_encode;
933 next->end = &subblock_encoder_end;
935 next->coder->next = LZMA_NEXT_CODER_INIT;
936 next->coder->subblock.data = NULL;
937 next->coder->subblock.limit = 0;
938 next->coder->subfilter.subcoder = LZMA_NEXT_CODER_INIT;
940 lzma_next_coder_end(&next->coder->subfilter.subcoder,
942 lzma_free(next->coder->subfilter.flags, allocator);
945 next->coder->subfilter.flags = NULL;
947 next->coder->next_finished = false;
948 next->coder->sequence = SEQ_FILL;
949 next->coder->options = filters[0].options;
950 next->coder->use_eopm = filters[0].uncompressed_size
951 == LZMA_VLI_VALUE_UNKNOWN;
952 next->coder->pos = 0;
954 next->coder->alignment.in_pos = 0;
955 next->coder->alignment.out_pos = 0;
956 next->coder->subblock.size = 0;
957 next->coder->subblock.in_pending = 0;
958 next->coder->rle.count = 0;
959 next->coder->rle.in_pending = 0;
960 next->coder->subfilter.mode = SUB_NONE;
961 next->coder->subfilter.mode_locked = false;
963 next->coder->temp.pos = 0;
964 next->coder->temp.size = 0;
966 // Grab some values from the options structure if it is available.
967 size_t subblock_size_limit;
968 if (next->coder->options != NULL) {
969 if (next->coder->options->alignment
970 < LZMA_SUBBLOCK_ALIGNMENT_MIN
971 || next->coder->options->alignment
972 > LZMA_SUBBLOCK_ALIGNMENT_MAX) {
973 subblock_encoder_end(next->coder, allocator);
974 return LZMA_HEADER_ERROR;
976 next->coder->alignment.multiple
977 = next->coder->options->alignment;
978 next->coder->subfilter.allow
979 = next->coder->options->allow_subfilters;
980 subblock_size_limit = next->coder->options->subblock_data_size;
982 next->coder->alignment.multiple
983 = LZMA_SUBBLOCK_ALIGNMENT_DEFAULT;
984 next->coder->subfilter.allow = false;
985 subblock_size_limit = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT;
988 return_if_error(subblock_data_size(next->coder, allocator,
989 subblock_size_limit));
991 return lzma_next_filter_init(
992 &next->coder->next, allocator, filters + 1);