block_decoder.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file block_decoder.c
  5. /// \brief Decodes .xz Blocks
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "block_decoder.h"
  11. #include "filter_decoder.h"
  12. #include "check.h"
  13. typedef struct {
  14. enum {
  15. SEQ_CODE,
  16. SEQ_PADDING,
  17. SEQ_CHECK,
  18. } sequence;
  19. /// The filters in the chain; initialized with lzma_raw_decoder_init().
  20. lzma_next_coder next;
  21. /// Decoding options; we also write Compressed Size and Uncompressed
  22. /// Size back to this structure when the decoding has been finished.
  23. lzma_block *block;
  24. /// Compressed Size calculated while decoding
  25. lzma_vli compressed_size;
  26. /// Uncompressed Size calculated while decoding
  27. lzma_vli uncompressed_size;
  28. /// Maximum allowed Compressed Size; this takes into account the
  29. /// size of the Block Header and Check fields when Compressed Size
  30. /// is unknown.
  31. lzma_vli compressed_limit;
  32. /// Maximum allowed Uncompressed Size.
  33. lzma_vli uncompressed_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. is_size_valid(lzma_vli size, lzma_vli reference)
  43. {
  44. return reference == LZMA_VLI_UNKNOWN || reference == size;
  45. }
  46. static lzma_ret
  47. block_decode(void *coder_ptr, const lzma_allocator *allocator,
  48. const uint8_t *restrict in, size_t *restrict in_pos,
  49. size_t in_size, uint8_t *restrict out,
  50. size_t *restrict out_pos, size_t out_size, lzma_action action)
  51. {
  52. lzma_block_coder *coder = coder_ptr;
  53. switch (coder->sequence) {
  54. case SEQ_CODE: {
  55. const size_t in_start = *in_pos;
  56. const size_t out_start = *out_pos;
  57. // Limit the amount of input and output space that we give
  58. // to the raw decoder based on the information we have
  59. // (or don't have) from Block Header.
  60. const size_t in_stop = *in_pos + (size_t)my_min(
  61. in_size - *in_pos,
  62. coder->compressed_limit - coder->compressed_size);
  63. const size_t out_stop = *out_pos + (size_t)my_min(
  64. out_size - *out_pos,
  65. coder->uncompressed_limit - coder->uncompressed_size);
  66. const lzma_ret ret = coder->next.code(coder->next.coder,
  67. allocator, in, in_pos, in_stop,
  68. out, out_pos, out_stop, action);
  69. const size_t in_used = *in_pos - in_start;
  70. const size_t out_used = *out_pos - out_start;
  71. // Because we have limited the input and output sizes,
  72. // we know that these cannot grow too big or overflow.
  73. coder->compressed_size += in_used;
  74. coder->uncompressed_size += out_used;
  75. if (ret == LZMA_OK) {
  76. const bool comp_done = coder->compressed_size
  77. == coder->block->compressed_size;
  78. const bool uncomp_done = coder->uncompressed_size
  79. == coder->block->uncompressed_size;
  80. // If both input and output amounts match the sizes
  81. // in Block Header but we still got LZMA_OK instead
  82. // of LZMA_STREAM_END, the file is broken.
  83. if (comp_done && uncomp_done)
  84. return LZMA_DATA_ERROR;
  85. // If the decoder has consumed all the input that it
  86. // needs but it still couldn't fill the output buffer
  87. // or return LZMA_STREAM_END, the file is broken.
  88. if (comp_done && *out_pos < out_size)
  89. return LZMA_DATA_ERROR;
  90. // If the decoder has produced all the output but
  91. // it still didn't return LZMA_STREAM_END or consume
  92. // more input (for example, detecting an end of
  93. // payload marker may need more input but produce
  94. // no output) the file is broken.
  95. if (uncomp_done && *in_pos < in_size)
  96. return LZMA_DATA_ERROR;
  97. }
  98. // Don't waste time updating the integrity check if it will be
  99. // ignored. Also skip it if no new output was produced. This
  100. // avoids null pointer + 0 (undefined behavior) when out == 0.
  101. if (!coder->ignore_check && out_used > 0)
  102. lzma_check_update(&coder->check, coder->block->check,
  103. out + out_start, out_used);
  104. if (ret != LZMA_STREAM_END)
  105. return ret;
  106. // Compressed and Uncompressed Sizes are now at their final
  107. // values. Verify that they match the values given to us.
  108. if (!is_size_valid(coder->compressed_size,
  109. coder->block->compressed_size)
  110. || !is_size_valid(coder->uncompressed_size,
  111. coder->block->uncompressed_size))
  112. return LZMA_DATA_ERROR;
  113. // Copy the values into coder->block. The caller
  114. // may use this information to construct Index.
  115. coder->block->compressed_size = coder->compressed_size;
  116. coder->block->uncompressed_size = coder->uncompressed_size;
  117. coder->sequence = SEQ_PADDING;
  118. }
  119. // Fall through
  120. case SEQ_PADDING:
  121. // Compressed Data is padded to a multiple of four bytes.
  122. while (coder->compressed_size & 3) {
  123. if (*in_pos >= in_size)
  124. return LZMA_OK;
  125. // We use compressed_size here just get the Padding
  126. // right. The actual Compressed Size was stored to
  127. // coder->block already, and won't be modified by
  128. // us anymore.
  129. ++coder->compressed_size;
  130. if (in[(*in_pos)++] != 0x00)
  131. return LZMA_DATA_ERROR;
  132. }
  133. if (coder->block->check == LZMA_CHECK_NONE)
  134. return LZMA_STREAM_END;
  135. if (!coder->ignore_check)
  136. lzma_check_finish(&coder->check, coder->block->check);
  137. coder->sequence = SEQ_CHECK;
  138. // Fall through
  139. case SEQ_CHECK: {
  140. const size_t check_size = lzma_check_size(coder->block->check);
  141. lzma_bufcpy(in, in_pos, in_size, coder->block->raw_check,
  142. &coder->check_pos, check_size);
  143. if (coder->check_pos < check_size)
  144. return LZMA_OK;
  145. // Validate the Check only if we support it.
  146. // coder->check.buffer may be uninitialized
  147. // when the Check ID is not supported.
  148. if (!coder->ignore_check
  149. && lzma_check_is_supported(coder->block->check)
  150. && memcmp(coder->block->raw_check,
  151. coder->check.buffer.u8,
  152. check_size) != 0)
  153. return LZMA_DATA_ERROR;
  154. return LZMA_STREAM_END;
  155. }
  156. }
  157. return LZMA_PROG_ERROR;
  158. }
  159. static void
  160. block_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  161. {
  162. lzma_block_coder *coder = coder_ptr;
  163. lzma_next_end(&coder->next, allocator);
  164. lzma_free(coder, allocator);
  165. return;
  166. }
  167. extern lzma_ret
  168. lzma_block_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  169. lzma_block *block)
  170. {
  171. lzma_next_coder_init(&lzma_block_decoder_init, next, allocator);
  172. // Validate the options. lzma_block_unpadded_size() does that for us
  173. // except for Uncompressed Size and filters. Filters are validated
  174. // by the raw decoder.
  175. if (lzma_block_unpadded_size(block) == 0
  176. || !lzma_vli_is_valid(block->uncompressed_size))
  177. return LZMA_PROG_ERROR;
  178. // Allocate *next->coder if needed.
  179. lzma_block_coder *coder = next->coder;
  180. if (coder == NULL) {
  181. coder = lzma_alloc(sizeof(lzma_block_coder), allocator);
  182. if (coder == NULL)
  183. return LZMA_MEM_ERROR;
  184. next->coder = coder;
  185. next->code = &block_decode;
  186. next->end = &block_decoder_end;
  187. coder->next = LZMA_NEXT_CODER_INIT;
  188. }
  189. // Basic initializations
  190. coder->sequence = SEQ_CODE;
  191. coder->block = block;
  192. coder->compressed_size = 0;
  193. coder->uncompressed_size = 0;
  194. // If Compressed Size is not known, we calculate the maximum allowed
  195. // value so that encoded size of the Block (including Block Padding)
  196. // is still a valid VLI and a multiple of four.
  197. coder->compressed_limit
  198. = block->compressed_size == LZMA_VLI_UNKNOWN
  199. ? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
  200. - block->header_size
  201. - lzma_check_size(block->check)
  202. : block->compressed_size;
  203. // With Uncompressed Size this is simpler. If Block Header lacks
  204. // the size info, then LZMA_VLI_MAX is the maximum possible
  205. // Uncompressed Size.
  206. coder->uncompressed_limit
  207. = block->uncompressed_size == LZMA_VLI_UNKNOWN
  208. ? LZMA_VLI_MAX
  209. : block->uncompressed_size;
  210. // Initialize the check. It's caller's problem if the Check ID is not
  211. // supported, and the Block decoder cannot verify the Check field.
  212. // Caller can test lzma_check_is_supported(block->check).
  213. coder->check_pos = 0;
  214. lzma_check_init(&coder->check, block->check);
  215. coder->ignore_check = block->version >= 1
  216. ? block->ignore_check : false;
  217. // Initialize the filter chain.
  218. return lzma_raw_decoder_init(&coder->next, allocator,
  219. block->filters);
  220. }
  221. extern LZMA_API(lzma_ret)
  222. lzma_block_decoder(lzma_stream *strm, lzma_block *block)
  223. {
  224. lzma_next_strm_init(lzma_block_decoder_init, strm, block);
  225. strm->internal->supported_actions[LZMA_RUN] = true;
  226. strm->internal->supported_actions[LZMA_FINISH] = true;
  227. return LZMA_OK;
  228. }