1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file subblock_encoder.c
4 /// \brief Encoder of the Subblock filter
6 // Copyright (C) 2007 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; \
63 lzma_options_subblock *options;
65 lzma_vli uncompressed_size;
84 uint8_t buffer[LZMA_SUBBLOCK_RLE_MAX];
103 lzma_next_coder subcoder;
110 uint8_t buffer[LZMA_BUFFER_SIZE];
115 /// \brief Aligns the output buffer
117 /// Aligns the output buffer so that after skew bytes the output position is
118 /// a multiple of coder->alignment.multiple.
120 subblock_align(lzma_coder *coder, uint8_t *restrict out,
121 size_t *restrict out_pos, size_t out_size, uint32_t skew)
123 assert(*out_pos < out_size);
125 const uint32_t target = coder->alignment.in_pos
126 % coder->alignment.multiple;
128 while ((coder->alignment.out_pos + skew)
129 % coder->alignment.multiple != target) {
130 // Zero indicates padding.
133 // Check if output buffer got full and indicate it to
135 if (*out_pos == out_size)
139 coder->alignment.in_pos += coder->alignment.in_pending;
140 coder->alignment.in_pending = 0;
142 // Output buffer is not full.
147 /// \brief Checks if buffer contains repeated data
149 /// \param needle Buffer containing a single repeat chunk
150 /// \param needle_size Size of needle in bytes
151 /// \param buf Buffer to search for repeated needles
152 /// \param buf_chunks Buffer size is buf_chunks * needle_size.
154 /// \return True if the whole buf is filled with repeated needles.
157 is_repeating(const uint8_t *restrict needle, size_t needle_size,
158 const uint8_t *restrict buf, size_t buf_chunks)
160 while (buf_chunks-- != 0) {
161 if (memcmp(buf, needle, needle_size) != 0)
171 /// \brief Optimizes the repeating style and updates coder->sequence
173 subblock_rle_flush(lzma_coder *coder)
175 // The Subblock decoder can use memset() when the size of the data
176 // being repeated is one byte, so we check if the RLE buffer is
177 // filled with a single repeating byte.
178 if (coder->rle.size > 1) {
179 const uint8_t b = coder->rle.buffer[0];
182 if (coder->rle.buffer[i] != b)
185 if (++i == coder->rle.size) {
186 // TODO Integer overflow check maybe,
187 // although this needs at least 2**63 bytes
188 // of input until it gets triggered...
189 coder->rle.count *= coder->rle.size;
196 if (coder->rle.count > REPEAT_COUNT_MAX)
197 coder->tmp = REPEAT_COUNT_MAX - 1;
199 coder->tmp = coder->rle.count - 1;
201 coder->sequence = SEQ_RLE_COUNT_0;
207 /// \brief Resizes coder->subblock.data for a new size limit
209 subblock_data_size(lzma_coder *coder, lzma_allocator *allocator,
212 // Verify that the new limit is valid.
213 if (new_limit < LZMA_SUBBLOCK_DATA_SIZE_MIN
214 || new_limit > LZMA_SUBBLOCK_DATA_SIZE_MAX)
215 return LZMA_HEADER_ERROR;
217 // Ff the new limit is different than the previous one, we need
218 // to reallocate the data buffer.
219 if (new_limit != coder->subblock.limit) {
220 lzma_free(coder->subblock.data, allocator);
221 coder->subblock.data = lzma_alloc(new_limit, allocator);
222 if (coder->subblock.data == NULL)
223 return LZMA_MEM_ERROR;
226 coder->subblock.limit = new_limit;
233 subblock_buffer(lzma_coder *coder, lzma_allocator *allocator,
234 const uint8_t *restrict in, size_t *restrict in_pos,
235 size_t in_size, uint8_t *restrict out,
236 size_t *restrict out_pos, size_t out_size, lzma_action action)
238 // Verify that there is a sane amount of input.
239 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN) {
240 const lzma_vli in_avail = in_size - *in_pos;
241 if (action == LZMA_FINISH) {
242 if (in_avail != coder->uncompressed_size)
243 return LZMA_DATA_ERROR;
245 if (in_avail > coder->uncompressed_size)
246 return LZMA_DATA_ERROR;
250 // Check if we need to do something special with the Subfilter.
251 if (coder->options != NULL && coder->options->allow_subfilters) {
252 switch (coder->options->subfilter_mode) {
253 case LZMA_SUBFILTER_NONE:
254 if (coder->subfilter.mode != SUB_NONE)
255 return LZMA_PROG_ERROR;
258 case LZMA_SUBFILTER_SET:
259 if (coder->subfilter.mode != SUB_NONE)
260 return LZMA_HEADER_ERROR;
262 coder->subfilter.mode = SUB_SET;
263 coder->subfilter.got_input = false;
265 if (coder->sequence == SEQ_FILL)
266 coder->sequence = SEQ_FLUSH;
270 case LZMA_SUBFILTER_RUN:
271 if (coder->subfilter.mode != SUB_RUN)
272 return LZMA_PROG_ERROR;
275 case LZMA_SUBFILTER_FINISH:
276 if (coder->subfilter.mode == SUB_RUN)
277 coder->subfilter.mode = SUB_FINISH;
278 else if (coder->subfilter.mode != SUB_FINISH)
279 return LZMA_PROG_ERROR;
281 if (!coder->subfilter.got_input)
282 return LZMA_PROG_ERROR;
287 return LZMA_HEADER_ERROR;
292 while (*out_pos < out_size)
293 switch (coder->sequence) {
295 // Grab the new Subblock Data Size and reallocate the buffer.
296 if (coder->subblock.size == 0 && coder->options != NULL
297 && coder->options->subblock_data_size
298 != coder->subblock.limit) {
299 const lzma_ret ret = subblock_data_size(coder,
300 allocator, coder->options
301 ->subblock_data_size);
306 if (coder->subfilter.mode == SUB_NONE) {
307 assert(coder->subfilter.subcoder.code == NULL);
309 // No Subfilter is enabled, just copy the data as is.
310 // NOTE: uncompressed_size cannot overflow because we
311 // have checked/ it in the beginning of this function.
312 const size_t in_used = bufcpy(in, in_pos, in_size,
313 coder->subblock.data,
314 &coder->subblock.size,
315 coder->subblock.limit);
317 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
318 coder->uncompressed_size -= in_used;
320 coder->alignment.in_pending += in_used;
323 const size_t in_start = *in_pos;
326 if (coder->subfilter.mode == SUB_FINISH) {
327 // Let the Subfilter write out pending data,
328 // but don't give it any new input anymore.
330 ret = coder->subfilter.subcoder.code(coder
331 ->subfilter.subcoder.coder,
332 allocator, NULL, &dummy, 0,
333 coder->subblock.data,
334 &coder->subblock.size,
335 coder->subblock.limit,
338 // Give our input data to the Subfilter. Note
339 // that action can be LZMA_FINISH. In that
340 // case, we filter everything until the end
341 // of the input. The application isn't required
342 // to separately set LZMA_SUBBLOCK_FINISH.
343 ret = coder->subfilter.subcoder.code(coder
344 ->subfilter.subcoder.coder,
345 allocator, in, in_pos, in_size,
346 coder->subblock.data,
347 &coder->subblock.size,
348 coder->subblock.limit,
352 const size_t in_used = *in_pos - in_start;
355 coder->subfilter.got_input = true;
357 // NOTE: uncompressed_size cannot overflow because we
358 // have checked it in the beginning of this function.
359 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
360 coder->uncompressed_size -= *in_pos - in_start;
362 coder->alignment.in_pending += in_used;
364 if (ret == LZMA_STREAM_END) {
365 // We don't strictly need to do this, but
366 // doing it sounds like a good idea, because
367 // otherwise the Subfilter's memory could be
368 // left allocated for long time, and would
369 // just waste memory.
370 lzma_next_coder_end(&coder->subfilter.subcoder,
373 assert(coder->options != NULL);
374 coder->options->subfilter_mode
375 = LZMA_SUBFILTER_NONE;
377 assert(coder->subfilter.mode == SUB_FINISH
378 || action == LZMA_FINISH);
379 coder->subfilter.mode = SUB_END_MARKER;
381 // Flush now. Even if coder->subblock.size
382 // happens to be zero, we still need to go
383 // to SEQ_FLUSH to write the Subfilter Unset
385 coder->sequence = SEQ_FLUSH;
389 // Return if an error occurred.
394 // If we ran out of input before the whole buffer
395 // was filled, return to application.
396 if (coder->subblock.size < coder->subblock.limit
397 && action != LZMA_FINISH)
400 coder->sequence = SEQ_FLUSH;
406 if (coder->options != NULL) {
407 // Update the alignment variable.
408 coder->alignment.multiple = coder->options->alignment;
409 if (coder->alignment.multiple
410 < LZMA_SUBBLOCK_ALIGNMENT_MIN
411 || coder->alignment.multiple
412 > LZMA_SUBBLOCK_ALIGNMENT_MAX)
413 return LZMA_HEADER_ERROR;
415 // Run-length encoder
417 // First check if there is some data pending and we
418 // have an obvious need to flush it immediatelly.
419 if (coder->rle.count > 0
421 != coder->options->rle
422 || coder->subblock.size
423 % coder->rle.size)) {
424 subblock_rle_flush(coder);
428 // Grab the (possibly new) RLE chunk size and
430 coder->rle.size = coder->options->rle;
431 if (coder->rle.size > LZMA_SUBBLOCK_RLE_MAX)
432 return LZMA_HEADER_ERROR;
434 if (coder->subblock.size != 0
436 != LZMA_SUBBLOCK_RLE_OFF
437 && coder->subblock.size
438 % coder->rle.size == 0) {
440 // Initialize coder->rle.buffer if we don't
441 // have RLE already running.
442 if (coder->rle.count == 0)
443 memcpy(coder->rle.buffer,
444 coder->subblock.data,
447 // Test if coder->subblock.data is repeating.
448 const size_t count = coder->subblock.size
450 if (is_repeating(coder->rle.buffer,
452 coder->subblock.data, count)) {
453 if (LZMA_VLI_VALUE_MAX - count
455 return LZMA_PROG_ERROR;
457 coder->rle.count += count;
458 coder->subblock.size = 0;
460 } else if (coder->rle.count > 0) {
461 // It's not repeating or at least not
462 // with the same byte sequence as the
463 // earlier Subblock Data buffers. We
464 // have some data pending in the RLE
465 // buffer already, so do a flush.
466 // Once flushed, we will check again
467 // if the Subblock Data happens to
468 // contain a different repeating
470 subblock_rle_flush(coder);
476 // If we now have some data left in coder->subblock, the RLE
477 // buffer is empty and we must write a regular Subblock Data.
478 if (coder->subblock.size > 0) {
479 assert(coder->rle.count == 0);
480 coder->tmp = coder->subblock.size - 1;
481 coder->sequence = SEQ_DATA_SIZE_0;
485 // Check if we should enable Subfilter.
486 if (coder->subfilter.mode == SUB_SET) {
487 if (coder->rle.count > 0)
488 subblock_rle_flush(coder);
490 coder->sequence = SEQ_SUBFILTER_INIT;
494 // Check if we have just finished Subfiltering.
495 if (coder->subfilter.mode == SUB_END_MARKER) {
496 if (coder->rle.count > 0) {
497 subblock_rle_flush(coder);
502 coder->subfilter.mode = SUB_NONE;
503 if (*out_pos == out_size)
507 // Check if we have already written everything.
508 if (action == LZMA_FINISH && *in_pos == in_size
509 && coder->subfilter.mode == SUB_NONE) {
510 if (coder->rle.count > 0) {
511 subblock_rle_flush(coder);
515 if (coder->uncompressed_size
516 == LZMA_VLI_VALUE_UNKNOWN) {
517 // NOTE: No need to use write_byte() here
518 // since we are finishing.
519 out[*out_pos] = 0x10;
521 } else if (coder->uncompressed_size != 0) {
522 return LZMA_DATA_ERROR;
525 return LZMA_STREAM_END;
528 // Otherwise we have more work to do.
529 coder->sequence = SEQ_FILL;
532 case SEQ_RLE_COUNT_0:
533 // Make the Data field properly aligned, but only if the data
534 // chunk to be repeated isn't extremely small. We have four
535 // bytes for Count and one byte for Size, thus the number five.
536 if (coder->rle.size >= RLE_MIN_SIZE_FOR_ALIGN
538 coder, out, out_pos, out_size, 5))
541 assert(coder->rle.count > 0);
543 write_byte(0x30 | (coder->tmp & 0x0F));
545 coder->sequence = SEQ_RLE_COUNT_1;
548 case SEQ_RLE_COUNT_1:
549 write_byte(coder->tmp >> 4);
550 coder->sequence = SEQ_RLE_COUNT_2;
553 case SEQ_RLE_COUNT_2:
554 write_byte(coder->tmp >> 12);
555 coder->sequence = SEQ_RLE_COUNT_3;
558 case SEQ_RLE_COUNT_3:
559 write_byte(coder->tmp >> 20);
561 if (coder->rle.count > REPEAT_COUNT_MAX)
562 coder->rle.count -= REPEAT_COUNT_MAX;
564 coder->rle.count = 0;
566 coder->sequence = SEQ_RLE_SIZE;
570 assert(coder->rle.size >= LZMA_SUBBLOCK_RLE_MIN);
571 assert(coder->rle.size <= LZMA_SUBBLOCK_RLE_MAX);
572 write_byte(coder->rle.size - 1);
573 coder->sequence = SEQ_RLE_DATA;
577 bufcpy(coder->rle.buffer, &coder->pos, coder->rle.size,
578 out, out_pos, out_size);
579 if (coder->pos < coder->rle.size)
582 coder->alignment.out_pos += coder->rle.size;
585 coder->sequence = SEQ_FLUSH;
588 case SEQ_DATA_SIZE_0:
589 // We need four bytes for the Size field.
590 if (subblock_align(coder, out, out_pos, out_size, 4))
593 write_byte(0x20 | (coder->tmp & 0x0F));
594 coder->sequence = SEQ_DATA_SIZE_1;
597 case SEQ_DATA_SIZE_1:
598 write_byte(coder->tmp >> 4);
599 coder->sequence = SEQ_DATA_SIZE_2;
602 case SEQ_DATA_SIZE_2:
603 write_byte(coder->tmp >> 12);
604 coder->sequence = SEQ_DATA_SIZE_3;
607 case SEQ_DATA_SIZE_3:
608 write_byte(coder->tmp >> 20);
609 coder->sequence = SEQ_DATA;
613 bufcpy(coder->subblock.data, &coder->pos,
614 coder->subblock.size, out, out_pos, out_size);
615 if (coder->pos < coder->subblock.size)
618 coder->alignment.out_pos += coder->subblock.size;
620 coder->subblock.size = 0;
622 coder->sequence = SEQ_FLUSH;
625 case SEQ_SUBFILTER_INIT: {
626 assert(coder->subblock.size == 0);
627 assert(coder->rle.count == 0);
628 assert(coder->subfilter.mode == SUB_SET);
629 assert(coder->options != NULL);
631 // There must be a filter specified.
632 if (coder->options->subfilter_options.id
633 == LZMA_VLI_VALUE_UNKNOWN)
634 return LZMA_HEADER_ERROR;
636 // Initialize a raw encoder to work as a Subfilter.
637 lzma_options_filter options[2];
638 options[0] = coder->options->subfilter_options;
639 options[1].id = LZMA_VLI_VALUE_UNKNOWN;
641 lzma_ret ret = lzma_raw_encoder_init(
642 &coder->subfilter.subcoder, allocator,
643 options, LZMA_VLI_VALUE_UNKNOWN, false);
647 // Encode the Filter Flags field into a buffer. This should
648 // never fail since we have already successfully initialized
649 // the Subfilter itself. Check it still, and return
650 // LZMA_PROG_ERROR instead of whatever the ret would say.
651 ret = lzma_filter_flags_size(
652 &coder->subfilter.flags_size, options);
653 assert(ret == LZMA_OK);
655 return LZMA_PROG_ERROR;
657 coder->subfilter.flags = lzma_alloc(
658 coder->subfilter.flags_size, allocator);
659 if (coder->subfilter.flags == NULL)
660 return LZMA_MEM_ERROR;
662 // Now we have a big-enough buffer. Encode the Filter Flags.
663 // Like above, this should never fail.
665 ret = lzma_filter_flags_encode(coder->subfilter.flags,
666 &dummy, coder->subfilter.flags_size, options);
667 assert(ret == LZMA_OK);
668 assert(dummy == coder->subfilter.flags_size);
669 if (ret != LZMA_OK || dummy != coder->subfilter.flags_size)
670 return LZMA_PROG_ERROR;
672 // Write a Subblock indicating a new Subfilter.
675 coder->options->subfilter_mode = LZMA_SUBFILTER_RUN;
676 coder->subfilter.mode = SUB_RUN;
677 coder->sequence = SEQ_SUBFILTER_FLAGS;
682 case SEQ_SUBFILTER_FLAGS:
683 // Copy the Filter Flags to the output stream.
684 bufcpy(coder->subfilter.flags, &coder->pos,
685 coder->subfilter.flags_size,
686 out, out_pos, out_size);
687 if (coder->pos < coder->subfilter.flags_size)
690 lzma_free(coder->subfilter.flags, allocator);
691 coder->subfilter.flags = NULL;
694 coder->sequence = SEQ_FILL;
698 return LZMA_PROG_ERROR;
706 subblock_encode(lzma_coder *coder, lzma_allocator *allocator,
707 const uint8_t *restrict in, size_t *restrict in_pos,
708 size_t in_size, uint8_t *restrict out,
709 size_t *restrict out_pos, size_t out_size, lzma_action action)
711 if (coder->next.code == NULL)
712 return subblock_buffer(coder, allocator, in, in_pos, in_size,
713 out, out_pos, out_size, action);
715 while (*out_pos < out_size
716 && (*in_pos < in_size || action == LZMA_FINISH)) {
717 if (!coder->next_finished
718 && coder->temp.pos == coder->temp.size) {
720 coder->temp.size = 0;
722 const lzma_ret ret = coder->next.code(coder->next.coder,
723 allocator, in, in_pos, in_size,
724 coder->temp.buffer, &coder->temp.size,
725 LZMA_BUFFER_SIZE, action);
726 if (ret == LZMA_STREAM_END) {
727 assert(action == LZMA_FINISH);
728 coder->next_finished = true;
729 } else if (coder->temp.size == 0 || ret != LZMA_OK) {
734 const lzma_ret ret = subblock_buffer(coder, allocator,
735 coder->temp.buffer, &coder->temp.pos,
736 coder->temp.size, out, out_pos, out_size,
737 coder->next_finished ? LZMA_FINISH : LZMA_RUN);
738 if (ret == LZMA_STREAM_END) {
739 assert(action == LZMA_FINISH);
740 assert(coder->next_finished);
741 return LZMA_STREAM_END;
753 subblock_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
755 lzma_next_coder_end(&coder->next, allocator);
756 lzma_next_coder_end(&coder->subfilter.subcoder, allocator);
757 lzma_free(coder->subblock.data, allocator);
758 lzma_free(coder->subfilter.flags, allocator);
764 lzma_subblock_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
765 const lzma_filter_info *filters)
767 if (next->coder == NULL) {
768 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
769 if (next->coder == NULL)
770 return LZMA_MEM_ERROR;
772 next->coder->next = LZMA_NEXT_CODER_INIT;
773 next->coder->subblock.data = NULL;
774 next->coder->subblock.limit = 0;
775 next->coder->subfilter.subcoder = LZMA_NEXT_CODER_INIT;
777 lzma_next_coder_end(&next->coder->subfilter.subcoder,
779 lzma_free(next->coder->subfilter.flags, allocator);
782 next->coder->subfilter.flags = NULL;
784 next->coder->next_finished = false;
785 next->coder->sequence = SEQ_FILL;
786 next->coder->options = filters[0].options;
787 next->coder->uncompressed_size = filters[0].uncompressed_size;
788 next->coder->pos = 0;
790 next->coder->alignment.in_pending = 0;
791 next->coder->alignment.in_pos = 0;
792 next->coder->alignment.out_pos = 0;
793 next->coder->subblock.size = 0;
794 next->coder->rle.count = 0;
795 next->coder->subfilter.mode = SUB_NONE;
797 next->coder->temp.pos = 0;
798 next->coder->temp.size = 0;
800 // Grab some values from the options structure if it is available.
801 size_t subblock_size_limit;
802 if (next->coder->options != NULL) {
803 if (next->coder->options->alignment
804 < LZMA_SUBBLOCK_ALIGNMENT_MIN
805 || next->coder->options->alignment
806 > LZMA_SUBBLOCK_ALIGNMENT_MAX) {
807 subblock_encoder_end(next->coder, allocator);
808 return LZMA_HEADER_ERROR;
810 next->coder->alignment.multiple
811 = next->coder->options->alignment;
812 subblock_size_limit = next->coder->options->subblock_data_size;
814 next->coder->alignment.multiple
815 = LZMA_SUBBLOCK_ALIGNMENT_DEFAULT;
816 subblock_size_limit = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT;
820 const lzma_ret ret = subblock_data_size(next->coder, allocator,
821 subblock_size_limit);
822 if (ret != LZMA_OK) {
823 subblock_encoder_end(next->coder, allocator);
829 const lzma_ret ret = lzma_next_filter_init(&next->coder->next,
830 allocator, filters + 1);
831 if (ret != LZMA_OK) {
832 subblock_encoder_end(next->coder, allocator);
837 next->code = &subblock_encode;
838 next->end = &subblock_encoder_end;