block_decoder.c 8.4 KB

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