block_encoder.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. lzma_check_update(&coder->check, coder->block->check,
  62. in + in_start, in_used);
  63. if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
  64. return ret;
  65. assert(*in_pos == in_size);
  66. assert(action == LZMA_FINISH);
  67. // Copy the values into coder->block. The caller
  68. // may use this information to construct Index.
  69. coder->block->compressed_size = coder->compressed_size;
  70. coder->block->uncompressed_size = coder->uncompressed_size;
  71. coder->sequence = SEQ_PADDING;
  72. }
  73. // Fall through
  74. case SEQ_PADDING:
  75. // Pad Compressed Data to a multiple of four bytes. We can
  76. // use coder->compressed_size for this since we don't need
  77. // it for anything else anymore.
  78. while (coder->compressed_size & 3) {
  79. if (*out_pos >= out_size)
  80. return LZMA_OK;
  81. out[*out_pos] = 0x00;
  82. ++*out_pos;
  83. ++coder->compressed_size;
  84. }
  85. if (coder->block->check == LZMA_CHECK_NONE)
  86. return LZMA_STREAM_END;
  87. lzma_check_finish(&coder->check, coder->block->check);
  88. coder->sequence = SEQ_CHECK;
  89. // Fall through
  90. case SEQ_CHECK: {
  91. const size_t check_size = lzma_check_size(coder->block->check);
  92. lzma_bufcpy(coder->check.buffer.u8, &coder->pos, check_size,
  93. out, out_pos, out_size);
  94. if (coder->pos < check_size)
  95. return LZMA_OK;
  96. memcpy(coder->block->raw_check, coder->check.buffer.u8,
  97. check_size);
  98. return LZMA_STREAM_END;
  99. }
  100. }
  101. return LZMA_PROG_ERROR;
  102. }
  103. static void
  104. block_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
  105. {
  106. lzma_block_coder *coder = coder_ptr;
  107. lzma_next_end(&coder->next, allocator);
  108. lzma_free(coder, allocator);
  109. return;
  110. }
  111. static lzma_ret
  112. block_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
  113. const lzma_filter *filters lzma_attribute((__unused__)),
  114. const lzma_filter *reversed_filters)
  115. {
  116. lzma_block_coder *coder = coder_ptr;
  117. if (coder->sequence != SEQ_CODE)
  118. return LZMA_PROG_ERROR;
  119. return lzma_next_filter_update(
  120. &coder->next, allocator, reversed_filters);
  121. }
  122. extern lzma_ret
  123. lzma_block_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  124. lzma_block *block)
  125. {
  126. lzma_next_coder_init(&lzma_block_encoder_init, next, allocator);
  127. if (block == NULL)
  128. return LZMA_PROG_ERROR;
  129. // The contents of the structure may depend on the version so
  130. // check the version first.
  131. if (block->version > 1)
  132. return LZMA_OPTIONS_ERROR;
  133. // If the Check ID is not supported, we cannot calculate the check and
  134. // thus not create a proper Block.
  135. if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
  136. return LZMA_PROG_ERROR;
  137. if (!lzma_check_is_supported(block->check))
  138. return LZMA_UNSUPPORTED_CHECK;
  139. // Allocate and initialize *next->coder if needed.
  140. lzma_block_coder *coder = next->coder;
  141. if (coder == NULL) {
  142. coder = lzma_alloc(sizeof(lzma_block_coder), allocator);
  143. if (coder == NULL)
  144. return LZMA_MEM_ERROR;
  145. next->coder = coder;
  146. next->code = &block_encode;
  147. next->end = &block_encoder_end;
  148. next->update = &block_encoder_update;
  149. coder->next = LZMA_NEXT_CODER_INIT;
  150. }
  151. // Basic initializations
  152. coder->sequence = SEQ_CODE;
  153. coder->block = block;
  154. coder->compressed_size = 0;
  155. coder->uncompressed_size = 0;
  156. coder->pos = 0;
  157. // Initialize the check
  158. lzma_check_init(&coder->check, block->check);
  159. // Initialize the requested filters.
  160. return lzma_raw_encoder_init(&coder->next, allocator, block->filters);
  161. }
  162. extern LZMA_API(lzma_ret)
  163. lzma_block_encoder(lzma_stream *strm, lzma_block *block)
  164. {
  165. lzma_next_strm_init(lzma_block_encoder_init, strm, block);
  166. strm->internal->supported_actions[LZMA_RUN] = true;
  167. strm->internal->supported_actions[LZMA_FINISH] = true;
  168. return LZMA_OK;
  169. }