block_encoder.c 5.5 KB

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