block_decoder.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file block_decoder.c
  4. /// \brief Decodes .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_decoder.h"
  13. #include "filter_decoder.h"
  14. #include "check.h"
  15. typedef struct {
  16. enum {
  17. SEQ_CODE,
  18. SEQ_PADDING,
  19. SEQ_CHECK,
  20. } sequence;
  21. /// The filters in the chain; initialized with lzma_raw_decoder_init().
  22. lzma_next_coder next;
  23. /// Decoding options; we also write Compressed Size and Uncompressed
  24. /// Size back to this structure when the decoding has been finished.
  25. lzma_block *block;
  26. /// Compressed Size calculated while decoding
  27. lzma_vli compressed_size;
  28. /// Uncompressed Size calculated while decoding
  29. lzma_vli uncompressed_size;
  30. /// Maximum allowed Compressed Size; this takes into account the
  31. /// size of the Block Header and Check fields when Compressed Size
  32. /// is unknown.
  33. lzma_vli compressed_limit;
  34. /// Position when reading the Check field
  35. size_t check_pos;
  36. /// Check of the uncompressed data
  37. lzma_check_state check;
  38. /// True if the integrity check won't be calculated and verified.
  39. bool ignore_check;
  40. } lzma_block_coder;
  41. static inline bool
  42. update_size(lzma_vli *size, lzma_vli add, lzma_vli limit)
  43. {
  44. if (limit > LZMA_VLI_MAX)
  45. limit = LZMA_VLI_MAX;
  46. if (limit < *size || limit - *size < add)
  47. return true;
  48. *size += add;
  49. return false;
  50. }
  51. static inline bool
  52. is_size_valid(lzma_vli size, lzma_vli reference)
  53. {
  54. return reference == LZMA_VLI_UNKNOWN || reference == size;
  55. }
  56. static lzma_ret
  57. block_decode(void *coder_ptr, const lzma_allocator *allocator,
  58. const uint8_t *restrict in, size_t *restrict in_pos,
  59. size_t in_size, uint8_t *restrict out,
  60. size_t *restrict out_pos, size_t out_size, lzma_action action)
  61. {
  62. lzma_block_coder *coder = coder_ptr;
  63. switch (coder->sequence) {
  64. case SEQ_CODE: {
  65. const size_t in_start = *in_pos;
  66. const size_t out_start = *out_pos;
  67. const lzma_ret ret = coder->next.code(coder->next.coder,
  68. allocator, in, in_pos, in_size,
  69. out, out_pos, out_size, action);
  70. const size_t in_used = *in_pos - in_start;
  71. const size_t out_used = *out_pos - out_start;
  72. // NOTE: We compare to compressed_limit here, which prevents
  73. // the total size of the Block growing past LZMA_VLI_MAX.
  74. if (update_size(&coder->compressed_size, in_used,
  75. coder->compressed_limit)
  76. || update_size(&coder->uncompressed_size,
  77. out_used,
  78. coder->block->uncompressed_size))
  79. return LZMA_DATA_ERROR;
  80. if (!coder->ignore_check)
  81. lzma_check_update(&coder->check, coder->block->check,
  82. out + out_start, out_used);
  83. if (ret != LZMA_STREAM_END)
  84. return ret;
  85. // Compressed and Uncompressed Sizes are now at their final
  86. // values. Verify that they match the values given to us.
  87. if (!is_size_valid(coder->compressed_size,
  88. coder->block->compressed_size)
  89. || !is_size_valid(coder->uncompressed_size,
  90. coder->block->uncompressed_size))
  91. return LZMA_DATA_ERROR;
  92. // Copy the values into coder->block. The caller
  93. // may use this information to construct Index.
  94. coder->block->compressed_size = coder->compressed_size;
  95. coder->block->uncompressed_size = coder->uncompressed_size;
  96. coder->sequence = SEQ_PADDING;
  97. }
  98. // Fall through
  99. case SEQ_PADDING:
  100. // Compressed Data is padded to a multiple of four bytes.
  101. while (coder->compressed_size & 3) {
  102. if (*in_pos >= in_size)
  103. return LZMA_OK;
  104. // We use compressed_size here just get the Padding
  105. // right. The actual Compressed Size was stored to
  106. // coder->block already, and won't be modified by
  107. // us anymore.
  108. ++coder->compressed_size;
  109. if (in[(*in_pos)++] != 0x00)
  110. return LZMA_DATA_ERROR;
  111. }
  112. if (coder->block->check == LZMA_CHECK_NONE)
  113. return LZMA_STREAM_END;
  114. if (!coder->ignore_check)
  115. lzma_check_finish(&coder->check, coder->block->check);
  116. coder->sequence = SEQ_CHECK;
  117. // Fall through
  118. case SEQ_CHECK: {
  119. const size_t check_size = lzma_check_size(coder->block->check);
  120. lzma_bufcpy(in, in_pos, in_size, coder->block->raw_check,
  121. &coder->check_pos, check_size);
  122. if (coder->check_pos < check_size)
  123. return LZMA_OK;
  124. // Validate the Check only if we support it.
  125. // coder->check.buffer may be uninitialized
  126. // when the Check ID is not supported.
  127. if (!coder->ignore_check
  128. && lzma_check_is_supported(coder->block->check)
  129. && memcmp(coder->block->raw_check,
  130. coder->check.buffer.u8,
  131. check_size) != 0)
  132. return LZMA_DATA_ERROR;
  133. return LZMA_STREAM_END;
  134. }
  135. }
  136. return LZMA_PROG_ERROR;
  137. }
  138. static void
  139. block_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  140. {
  141. lzma_block_coder *coder = coder_ptr;
  142. lzma_next_end(&coder->next, allocator);
  143. lzma_free(coder, allocator);
  144. return;
  145. }
  146. extern lzma_ret
  147. lzma_block_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  148. lzma_block *block)
  149. {
  150. lzma_next_coder_init(&lzma_block_decoder_init, next, allocator);
  151. // Validate the options. lzma_block_unpadded_size() does that for us
  152. // except for Uncompressed Size and filters. Filters are validated
  153. // by the raw decoder.
  154. if (lzma_block_unpadded_size(block) == 0
  155. || !lzma_vli_is_valid(block->uncompressed_size))
  156. return LZMA_PROG_ERROR;
  157. // Allocate *next->coder if needed.
  158. lzma_block_coder *coder = next->coder;
  159. if (coder == NULL) {
  160. coder = lzma_alloc(sizeof(lzma_block_coder), allocator);
  161. if (coder == NULL)
  162. return LZMA_MEM_ERROR;
  163. next->coder = coder;
  164. next->code = &block_decode;
  165. next->end = &block_decoder_end;
  166. coder->next = LZMA_NEXT_CODER_INIT;
  167. }
  168. // Basic initializations
  169. coder->sequence = SEQ_CODE;
  170. coder->block = block;
  171. coder->compressed_size = 0;
  172. coder->uncompressed_size = 0;
  173. // If Compressed Size is not known, we calculate the maximum allowed
  174. // value so that encoded size of the Block (including Block Padding)
  175. // is still a valid VLI and a multiple of four.
  176. coder->compressed_limit
  177. = block->compressed_size == LZMA_VLI_UNKNOWN
  178. ? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
  179. - block->header_size
  180. - lzma_check_size(block->check)
  181. : block->compressed_size;
  182. // Initialize the check. It's caller's problem if the Check ID is not
  183. // supported, and the Block decoder cannot verify the Check field.
  184. // Caller can test lzma_check_is_supported(block->check).
  185. coder->check_pos = 0;
  186. lzma_check_init(&coder->check, block->check);
  187. coder->ignore_check = block->version >= 1
  188. ? block->ignore_check : false;
  189. // Initialize the filter chain.
  190. return lzma_raw_decoder_init(&coder->next, allocator,
  191. block->filters);
  192. }
  193. extern LZMA_API(lzma_ret)
  194. lzma_block_decoder(lzma_stream *strm, lzma_block *block)
  195. {
  196. lzma_next_strm_init(lzma_block_decoder_init, strm, block);
  197. strm->internal->supported_actions[LZMA_RUN] = true;
  198. strm->internal->supported_actions[LZMA_FINISH] = true;
  199. return LZMA_OK;
  200. }