block_encoder.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file block_encoder.c
  5. /// \brief Encodes .xz Blocks
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "block_encoder.h"
  11. #include "filter_encoder.h"
  12. #include "check.h"
  13. typedef struct {
  14. /// The filters in the chain; initialized with lzma_raw_decoder_init().
  15. lzma_next_coder next;
  16. /// Encoding options; we also write Unpadded Size, Compressed Size,
  17. /// and Uncompressed Size back to this structure when the encoding
  18. /// has been finished.
  19. lzma_block *block;
  20. enum {
  21. SEQ_CODE,
  22. SEQ_PADDING,
  23. SEQ_CHECK,
  24. } sequence;
  25. /// Compressed Size calculated while encoding
  26. lzma_vli compressed_size;
  27. /// Uncompressed Size calculated while encoding
  28. lzma_vli uncompressed_size;
  29. /// Position in the Check field
  30. size_t pos;
  31. /// Check of the uncompressed data
  32. lzma_check_state check;
  33. } lzma_block_coder;
  34. static lzma_ret
  35. block_encode(void *coder_ptr, const lzma_allocator *allocator,
  36. const uint8_t *restrict in, size_t *restrict in_pos,
  37. size_t in_size, uint8_t *restrict out,
  38. size_t *restrict out_pos, size_t out_size, lzma_action action)
  39. {
  40. lzma_block_coder *coder = coder_ptr;
  41. // Check that our amount of input stays in proper limits.
  42. if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
  43. return LZMA_DATA_ERROR;
  44. switch (coder->sequence) {
  45. case SEQ_CODE: {
  46. const size_t in_start = *in_pos;
  47. const size_t out_start = *out_pos;
  48. const lzma_ret ret = coder->next.code(coder->next.coder,
  49. allocator, in, in_pos, in_size,
  50. out, out_pos, out_size, action);
  51. const size_t in_used = *in_pos - in_start;
  52. const size_t out_used = *out_pos - out_start;
  53. if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
  54. return LZMA_DATA_ERROR;
  55. coder->compressed_size += out_used;
  56. // No need to check for overflow because we have already
  57. // checked it at the beginning of this function.
  58. coder->uncompressed_size += in_used;
  59. // Call lzma_check_update() only if input was consumed. This
  60. // avoids null pointer + 0 (undefined behavior) when in == 0.
  61. if (in_used > 0)
  62. lzma_check_update(&coder->check, coder->block->check,
  63. in + in_start, in_used);
  64. if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
  65. return ret;
  66. assert(*in_pos == in_size);
  67. assert(action == LZMA_FINISH);
  68. // Copy the values into coder->block. The caller
  69. // may use this information to construct Index.
  70. coder->block->compressed_size = coder->compressed_size;
  71. coder->block->uncompressed_size = coder->uncompressed_size;
  72. coder->sequence = SEQ_PADDING;
  73. }
  74. // Fall through
  75. case SEQ_PADDING:
  76. // Pad Compressed Data to a multiple of four bytes. We can
  77. // use coder->compressed_size for this since we don't need
  78. // it for anything else anymore.
  79. while (coder->compressed_size & 3) {
  80. if (*out_pos >= out_size)
  81. return LZMA_OK;
  82. out[*out_pos] = 0x00;
  83. ++*out_pos;
  84. ++coder->compressed_size;
  85. }
  86. if (coder->block->check == LZMA_CHECK_NONE)
  87. return LZMA_STREAM_END;
  88. lzma_check_finish(&coder->check, coder->block->check);
  89. coder->sequence = SEQ_CHECK;
  90. // Fall through
  91. case SEQ_CHECK: {
  92. const size_t check_size = lzma_check_size(coder->block->check);
  93. lzma_bufcpy(coder->check.buffer.u8, &coder->pos, check_size,
  94. out, out_pos, out_size);
  95. if (coder->pos < check_size)
  96. return LZMA_OK;
  97. memcpy(coder->block->raw_check, coder->check.buffer.u8,
  98. check_size);
  99. return LZMA_STREAM_END;
  100. }
  101. }
  102. return LZMA_PROG_ERROR;
  103. }
  104. static void
  105. block_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
  106. {
  107. lzma_block_coder *coder = coder_ptr;
  108. lzma_next_end(&coder->next, allocator);
  109. lzma_free(coder, allocator);
  110. return;
  111. }
  112. static lzma_ret
  113. block_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
  114. const lzma_filter *filters lzma_attribute((__unused__)),
  115. const lzma_filter *reversed_filters)
  116. {
  117. lzma_block_coder *coder = coder_ptr;
  118. if (coder->sequence != SEQ_CODE)
  119. return LZMA_PROG_ERROR;
  120. return lzma_next_filter_update(
  121. &coder->next, allocator, reversed_filters);
  122. }
  123. extern lzma_ret
  124. lzma_block_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  125. lzma_block *block)
  126. {
  127. lzma_next_coder_init(&lzma_block_encoder_init, next, allocator);
  128. if (block == NULL)
  129. return LZMA_PROG_ERROR;
  130. // The contents of the structure may depend on the version so
  131. // check the version first.
  132. if (block->version > 1)
  133. return LZMA_OPTIONS_ERROR;
  134. // If the Check ID is not supported, we cannot calculate the check and
  135. // thus not create a proper Block.
  136. if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
  137. return LZMA_PROG_ERROR;
  138. if (!lzma_check_is_supported(block->check))
  139. return LZMA_UNSUPPORTED_CHECK;
  140. // Allocate and initialize *next->coder if needed.
  141. lzma_block_coder *coder = next->coder;
  142. if (coder == NULL) {
  143. coder = lzma_alloc(sizeof(lzma_block_coder), allocator);
  144. if (coder == NULL)
  145. return LZMA_MEM_ERROR;
  146. next->coder = coder;
  147. next->code = &block_encode;
  148. next->end = &block_encoder_end;
  149. next->update = &block_encoder_update;
  150. coder->next = LZMA_NEXT_CODER_INIT;
  151. }
  152. // Basic initializations
  153. coder->sequence = SEQ_CODE;
  154. coder->block = block;
  155. coder->compressed_size = 0;
  156. coder->uncompressed_size = 0;
  157. coder->pos = 0;
  158. // Initialize the check
  159. lzma_check_init(&coder->check, block->check);
  160. // Initialize the requested filters.
  161. return lzma_raw_encoder_init(&coder->next, allocator, block->filters);
  162. }
  163. extern LZMA_API(lzma_ret)
  164. lzma_block_encoder(lzma_stream *strm, lzma_block *block)
  165. {
  166. lzma_next_strm_init(lzma_block_encoder_init, strm, block);
  167. strm->internal->supported_actions[LZMA_RUN] = true;
  168. strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
  169. strm->internal->supported_actions[LZMA_FINISH] = true;
  170. return LZMA_OK;
  171. }