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; \
73 /// Pointer to the options given by the application. This is used
74 /// for two-way communication with the application.
75 lzma_options_subblock *options;
77 /// Position in various arrays.
80 /// Holds subblock.size - 1 or rle.size - 1 when encoding size
81 /// of Data or Repeat Count.
85 /// This is a copy of options->alignment, or
86 /// LZMA_SUBBLOCK_ALIGNMENT_DEFAULT if options is NULL.
89 /// Number of input bytes which we have processed and started
90 /// writing out. 32-bit integer is enough since we care only
91 /// about the lowest bits when fixing alignment.
94 /// Number of bytes written out.
99 /// Pointer to allocated buffer holding the Data field
100 /// of Subblock Type "Data".
103 /// Number of bytes in the buffer.
106 /// Allocated size of the buffer.
109 /// Number of input bytes that we have already read but
110 /// not yet started writing out. This can be different
111 /// to `size' when using Subfilter. That's why we track
112 /// in_pending separately for RLE (see below).
117 /// Buffer to hold the data that may be coded with
118 /// Subblock Type `Repeating Data'.
119 uint8_t buffer[LZMA_SUBBLOCK_RLE_MAX];
121 /// Number of bytes in buffer[].
124 /// Number of times the first `size' bytes of buffer[]
125 /// will be repeated.
128 /// Like subblock.in_pending above, but for RLE.
142 /// This is a copy of options->allow_subfilters. We use
143 /// this to verify that the application doesn't change
144 /// the value of allow_subfilters.
147 /// When this is true, application is not allowed to modify
148 /// options->subblock_mode. We may still modify it here.
151 /// True if we have encoded at least one byte of data with
155 /// Track the amount of input available once
156 /// LZMA_SUBFILTER_FINISH has been enabled.
157 /// This is needed for sanity checking (kind
158 /// of duplicating what common/code.c does).
161 /// Buffer for the Filter Flags field written after
162 /// the `Set Subfilter' indicator.
165 /// Size of Filter Flags field.
168 /// Pointers to Subfilter.
169 lzma_next_coder subcoder;
173 /// Temporary buffer used when we are not the last filter in the chain.
177 uint8_t buffer[LZMA_BUFFER_SIZE];
182 /// \brief Aligns the output buffer
184 /// Aligns the output buffer so that after skew bytes the output position is
185 /// a multiple of coder->alignment.multiple.
187 subblock_align(lzma_coder *coder, uint8_t *restrict out,
188 size_t *restrict out_pos, size_t out_size,
189 size_t chunk_size, uint32_t skew)
191 assert(*out_pos < out_size);
193 // Fix the alignment only if it makes sense at least a little.
194 if (chunk_size >= MIN_CHUNK_SIZE_FOR_ALIGN) {
195 const uint32_t target = coder->alignment.in_pos
196 % coder->alignment.multiple;
198 while ((coder->alignment.out_pos + skew)
199 % coder->alignment.multiple != target) {
200 // Zero indicates padding.
203 // Check if output buffer got full and indicate it to
205 if (*out_pos == out_size)
210 // Output buffer is not full.
215 /// \brief Checks if buffer contains repeated data
217 /// \param needle Buffer containing a single repeat chunk
218 /// \param needle_size Size of needle in bytes
219 /// \param buf Buffer to search for repeated needles
220 /// \param buf_chunks Buffer size is buf_chunks * needle_size.
222 /// \return True if the whole buf is filled with repeated needles.
225 is_repeating(const uint8_t *restrict needle, size_t needle_size,
226 const uint8_t *restrict buf, size_t buf_chunks)
228 while (buf_chunks-- != 0) {
229 if (memcmp(buf, needle, needle_size) != 0)
239 /// \brief Optimizes the repeating style and updates coder->sequence
241 subblock_rle_flush(lzma_coder *coder)
243 // The Subblock decoder can use memset() when the size of the data
244 // being repeated is one byte, so we check if the RLE buffer is
245 // filled with a single repeating byte.
246 if (coder->rle.size > 1) {
247 const uint8_t b = coder->rle.buffer[0];
250 if (coder->rle.buffer[i] != b)
253 if (++i == coder->rle.size) {
254 // TODO Integer overflow check maybe,
255 // although this needs at least 2**63 bytes
256 // of input until it gets triggered...
257 coder->rle.count *= coder->rle.size;
264 if (coder->rle.count == 1) {
265 // The buffer should be repeated only once. It is
266 // waste of space to use Repeating Data. Instead,
267 // write a regular Data Subblock. See SEQ_RLE_COUNT_0
268 // in subblock_buffer() for more info.
269 coder->tmp = coder->rle.size - 1;
270 } else if (coder->rle.count > REPEAT_COUNT_MAX) {
271 // There's so much to repeat that it doesn't fit into
272 // 28-bit integer. We will write two or more Subblocks
273 // of type Repeating Data.
274 coder->tmp = REPEAT_COUNT_MAX - 1;
276 coder->tmp = coder->rle.count - 1;
279 coder->sequence = SEQ_RLE_COUNT_0;
285 /// \brief Resizes coder->subblock.data for a new size limit
287 subblock_data_size(lzma_coder *coder, lzma_allocator *allocator,
290 // Verify that the new limit is valid.
291 if (new_limit < LZMA_SUBBLOCK_DATA_SIZE_MIN
292 || new_limit > LZMA_SUBBLOCK_DATA_SIZE_MAX)
293 return LZMA_HEADER_ERROR;
295 // Ff the new limit is different than the previous one, we need
296 // to reallocate the data buffer.
297 if (new_limit != coder->subblock.limit) {
298 lzma_free(coder->subblock.data, allocator);
299 coder->subblock.data = lzma_alloc(new_limit, allocator);
300 if (coder->subblock.data == NULL)
301 return LZMA_MEM_ERROR;
304 coder->subblock.limit = new_limit;
311 subblock_buffer(lzma_coder *coder, lzma_allocator *allocator,
312 const uint8_t *restrict in, size_t *restrict in_pos,
313 size_t in_size, uint8_t *restrict out,
314 size_t *restrict out_pos, size_t out_size, lzma_action action)
316 // Changing allow_subfilter is not allowed.
317 if (coder->options != NULL && coder->subfilter.allow
318 != coder->options->allow_subfilters)
319 return LZMA_PROG_ERROR;
321 // Check if we need to do something special with the Subfilter.
322 if (coder->subfilter.allow) {
323 assert(coder->options != NULL);
325 // See if subfilter_mode has been changed.
326 switch (coder->options->subfilter_mode) {
327 case LZMA_SUBFILTER_NONE:
328 if (coder->subfilter.mode != SUB_NONE)
329 return LZMA_PROG_ERROR;
332 case LZMA_SUBFILTER_SET:
333 if (coder->subfilter.mode_locked
334 || coder->subfilter.mode != SUB_NONE)
335 return LZMA_PROG_ERROR;
337 coder->subfilter.mode = SUB_SET;
338 coder->subfilter.got_input = false;
340 if (coder->sequence == SEQ_FILL)
341 coder->sequence = SEQ_FLUSH;
345 case LZMA_SUBFILTER_RUN:
346 if (coder->subfilter.mode != SUB_RUN)
347 return LZMA_PROG_ERROR;
351 case LZMA_SUBFILTER_FINISH: {
352 const size_t in_avail = in_size - *in_pos;
354 if (coder->subfilter.mode == SUB_RUN) {
355 if (coder->subfilter.mode_locked)
356 return LZMA_PROG_ERROR;
358 coder->subfilter.mode = SUB_FINISH;
359 coder->subfilter.in_avail = in_avail;
361 } else if (coder->subfilter.mode != SUB_FINISH
362 || coder->subfilter.in_avail
364 return LZMA_PROG_ERROR;
371 return LZMA_HEADER_ERROR;
374 // If we are sync-flushing or finishing, the application may
375 // no longer change subfilter_mode. Note that this check is
376 // done after checking the new subfilter_mode above; this
377 // way the application may e.g. set LZMA_SUBFILTER_SET and
378 // LZMA_SYNC_FLUSH at the same time, but it cannot modify
379 // subfilter_mode on the later lzma_code() calls before
380 // we have returned LZMA_STREAM_END.
381 if (action != LZMA_RUN)
382 coder->subfilter.mode_locked = true;
386 while (*out_pos < out_size)
387 switch (coder->sequence) {
389 // Grab the new Subblock Data Size and reallocate the buffer.
390 if (coder->subblock.size == 0 && coder->options != NULL
391 && coder->options->subblock_data_size
392 != coder->subblock.limit)
393 return_if_error(subblock_data_size(coder,
394 allocator, coder->options
395 ->subblock_data_size));
397 if (coder->subfilter.mode == SUB_NONE) {
398 assert(coder->subfilter.subcoder.code == NULL);
400 // No Subfilter is enabled, just copy the data as is.
401 coder->subblock.in_pending += bufcpy(
403 coder->subblock.data,
404 &coder->subblock.size,
405 coder->subblock.limit);
407 // If we ran out of input before the whole buffer
408 // was filled, return to application.
409 if (coder->subblock.size < coder->subblock.limit
410 && action == LZMA_RUN)
414 assert(coder->options->subfilter_mode
415 != LZMA_SUBFILTER_SET);
417 // Using LZMA_FINISH automatically toggles
418 // LZMA_SUBFILTER_FINISH.
420 // NOTE: It is possible that application had set
421 // LZMA_SUBFILTER_SET and LZMA_FINISH at the same
422 // time. In that case it is possible that we will
423 // cycle to LZMA_SUBFILTER_RUN, LZMA_SUBFILTER_FINISH,
424 // and back to LZMA_SUBFILTER_NONE in a single
425 // Subblock encoder function call.
426 if (action == LZMA_FINISH) {
427 coder->options->subfilter_mode
428 = LZMA_SUBFILTER_FINISH;
429 coder->subfilter.mode = SUB_FINISH;
432 const size_t in_start = *in_pos;
434 const lzma_ret ret = coder->subfilter.subcoder.code(
435 coder->subfilter.subcoder.coder,
436 allocator, in, in_pos, in_size,
437 coder->subblock.data,
438 &coder->subblock.size,
439 coder->subblock.limit,
440 coder->subfilter.mode == SUB_FINISH
441 ? LZMA_FINISH : action);
443 const size_t in_used = *in_pos - in_start;
444 coder->subblock.in_pending += in_used;
446 coder->subfilter.got_input = true;
448 coder->subfilter.in_avail = in_size - *in_pos;
450 if (ret == LZMA_STREAM_END) {
451 // All currently available input must have
453 assert(*in_pos == in_size);
455 // Flush now. Even if coder->subblock.size
456 // happened to be zero, we still need to go
457 // to SEQ_FLUSH to possibly finish RLE or
458 // write the Subfilter Unset indicator.
459 coder->sequence = SEQ_FLUSH;
461 if (coder->subfilter.mode == SUB_RUN) {
462 // Flushing with Subfilter enabled.
463 assert(action == LZMA_SYNC_FLUSH);
464 coder->subfilter.mode = SUB_FLUSH;
468 // Subfilter finished its job.
469 assert(coder->subfilter.mode == SUB_FINISH
470 || action == LZMA_FINISH);
472 // At least one byte of input must have been
473 // encoded with the Subfilter. This is
474 // required by the file format specification.
475 if (!coder->subfilter.got_input)
476 return LZMA_PROG_ERROR;
478 // We don't strictly need to do this, but
479 // doing it sounds like a good idea, because
480 // otherwise the Subfilter's memory could be
481 // left allocated for long time, and would
482 // just waste memory.
483 lzma_next_coder_end(&coder->subfilter.subcoder,
486 // We need to flush the currently buffered
487 // data and write Unset Subfilter marker.
488 // Note that we cannot set
489 // coder->options->subfilter_mode to
490 // LZMA_SUBFILTER_NONE yet, because we
491 // haven't written the Unset Subfilter
493 coder->subfilter.mode = SUB_END_MARKER;
494 coder->sequence = SEQ_FLUSH;
498 // Return if we couldn't fill the buffer or
499 // if an error occurred.
500 if (coder->subblock.size < coder->subblock.limit
505 coder->sequence = SEQ_FLUSH;
507 // SEQ_FILL doesn't produce any output so falling through
508 // to SEQ_FLUSH is safe.
509 assert(*out_pos < out_size);
514 if (coder->options != NULL) {
515 // Update the alignment variable.
516 coder->alignment.multiple = coder->options->alignment;
517 if (coder->alignment.multiple
518 < LZMA_SUBBLOCK_ALIGNMENT_MIN
519 || coder->alignment.multiple
520 > LZMA_SUBBLOCK_ALIGNMENT_MAX)
521 return LZMA_HEADER_ERROR;
523 // Run-length encoder
525 // First check if there is some data pending and we
526 // have an obvious need to flush it immediatelly.
527 if (coder->rle.count > 0
529 != coder->options->rle
530 || coder->subblock.size
531 % coder->rle.size)) {
532 subblock_rle_flush(coder);
536 // Grab the (possibly new) RLE chunk size and
538 coder->rle.size = coder->options->rle;
539 if (coder->rle.size > LZMA_SUBBLOCK_RLE_MAX)
540 return LZMA_HEADER_ERROR;
542 if (coder->subblock.size != 0
544 != LZMA_SUBBLOCK_RLE_OFF
545 && coder->subblock.size
546 % coder->rle.size == 0) {
548 // Initialize coder->rle.buffer if we don't
549 // have RLE already running.
550 if (coder->rle.count == 0)
551 memcpy(coder->rle.buffer,
552 coder->subblock.data,
555 // Test if coder->subblock.data is repeating.
556 // If coder->rle.count would overflow, we
557 // force flushing. Forced flushing shouldn't
558 // really happen in real-world situations.
559 const size_t count = coder->subblock.size
561 if (UINT64_MAX - count > coder->rle.count
565 coder->subblock.data,
567 coder->rle.count += count;
568 coder->rle.in_pending += coder
569 ->subblock.in_pending;
570 coder->subblock.in_pending = 0;
571 coder->subblock.size = 0;
573 } else if (coder->rle.count > 0) {
574 // It's not repeating or at least not
575 // with the same byte sequence as the
576 // earlier Subblock Data buffers. We
577 // have some data pending in the RLE
578 // buffer already, so do a flush.
579 // Once flushed, we will check again
580 // if the Subblock Data happens to
581 // contain a different repeating
583 subblock_rle_flush(coder);
589 // If we now have some data left in coder->subblock, the RLE
590 // buffer is empty and we must write a regular Subblock Data.
591 if (coder->subblock.size > 0) {
592 assert(coder->rle.count == 0);
593 coder->tmp = coder->subblock.size - 1;
594 coder->sequence = SEQ_DATA_SIZE_0;
598 // Check if we should enable Subfilter.
599 if (coder->subfilter.mode == SUB_SET) {
600 if (coder->rle.count > 0)
601 subblock_rle_flush(coder);
603 coder->sequence = SEQ_SUBFILTER_INIT;
607 // Check if we have just finished Subfiltering.
608 if (coder->subfilter.mode == SUB_END_MARKER) {
609 if (coder->rle.count > 0) {
610 subblock_rle_flush(coder);
614 coder->options->subfilter_mode = LZMA_SUBFILTER_NONE;
615 coder->subfilter.mode = SUB_NONE;
618 if (*out_pos == out_size)
622 // Check if we have already written everything.
623 if (action != LZMA_RUN && *in_pos == in_size
624 && (coder->subfilter.mode == SUB_NONE
625 || coder->subfilter.mode == SUB_FLUSH)) {
626 if (coder->rle.count > 0) {
627 subblock_rle_flush(coder);
631 if (action == LZMA_SYNC_FLUSH) {
632 if (coder->subfilter.mode == SUB_FLUSH)
633 coder->subfilter.mode = SUB_RUN;
635 coder->subfilter.mode_locked = false;
636 coder->sequence = SEQ_FILL;
639 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,
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);
919 lzma_free(coder, allocator);
925 lzma_subblock_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
926 const lzma_filter_info *filters)
928 if (next->coder == NULL) {
929 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
930 if (next->coder == NULL)
931 return LZMA_MEM_ERROR;
933 next->code = &subblock_encode;
934 next->end = &subblock_encoder_end;
936 next->coder->next = LZMA_NEXT_CODER_INIT;
937 next->coder->subblock.data = NULL;
938 next->coder->subblock.limit = 0;
939 next->coder->subfilter.subcoder = LZMA_NEXT_CODER_INIT;
941 lzma_next_coder_end(&next->coder->subfilter.subcoder,
943 lzma_free(next->coder->subfilter.flags, allocator);
946 next->coder->subfilter.flags = NULL;
948 next->coder->next_finished = false;
949 next->coder->sequence = SEQ_FILL;
950 next->coder->options = filters[0].options;
951 next->coder->pos = 0;
953 next->coder->alignment.in_pos = 0;
954 next->coder->alignment.out_pos = 0;
955 next->coder->subblock.size = 0;
956 next->coder->subblock.in_pending = 0;
957 next->coder->rle.count = 0;
958 next->coder->rle.in_pending = 0;
959 next->coder->subfilter.mode = SUB_NONE;
960 next->coder->subfilter.mode_locked = false;
962 next->coder->temp.pos = 0;
963 next->coder->temp.size = 0;
965 // Grab some values from the options structure if it is available.
966 size_t subblock_size_limit;
967 if (next->coder->options != NULL) {
968 if (next->coder->options->alignment
969 < LZMA_SUBBLOCK_ALIGNMENT_MIN
970 || next->coder->options->alignment
971 > LZMA_SUBBLOCK_ALIGNMENT_MAX) {
972 subblock_encoder_end(next->coder, allocator);
973 return LZMA_HEADER_ERROR;
975 next->coder->alignment.multiple
976 = next->coder->options->alignment;
977 next->coder->subfilter.allow
978 = next->coder->options->allow_subfilters;
979 subblock_size_limit = next->coder->options->subblock_data_size;
981 next->coder->alignment.multiple
982 = LZMA_SUBBLOCK_ALIGNMENT_DEFAULT;
983 next->coder->subfilter.allow = false;
984 subblock_size_limit = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT;
987 return_if_error(subblock_data_size(next->coder, allocator,
988 subblock_size_limit));
990 return lzma_next_filter_init(
991 &next->coder->next, allocator, filters + 1);