block_encoder.c 5.8 KB

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