block_decoder.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. if (!coder->ignore_check)
  101. lzma_check_update(&coder->check, coder->block->check,
  102. out + out_start, out_used);
  103. if (ret != LZMA_STREAM_END)
  104. return ret;
  105. // Compressed and Uncompressed Sizes are now at their final
  106. // values. Verify that they match the values given to us.
  107. if (!is_size_valid(coder->compressed_size,
  108. coder->block->compressed_size)
  109. || !is_size_valid(coder->uncompressed_size,
  110. coder->block->uncompressed_size))
  111. return LZMA_DATA_ERROR;
  112. // Copy the values into coder->block. The caller
  113. // may use this information to construct Index.
  114. coder->block->compressed_size = coder->compressed_size;
  115. coder->block->uncompressed_size = coder->uncompressed_size;
  116. coder->sequence = SEQ_PADDING;
  117. }
  118. // Fall through
  119. case SEQ_PADDING:
  120. // Compressed Data is padded to a multiple of four bytes.
  121. while (coder->compressed_size & 3) {
  122. if (*in_pos >= in_size)
  123. return LZMA_OK;
  124. // We use compressed_size here just get the Padding
  125. // right. The actual Compressed Size was stored to
  126. // coder->block already, and won't be modified by
  127. // us anymore.
  128. ++coder->compressed_size;
  129. if (in[(*in_pos)++] != 0x00)
  130. return LZMA_DATA_ERROR;
  131. }
  132. if (coder->block->check == LZMA_CHECK_NONE)
  133. return LZMA_STREAM_END;
  134. if (!coder->ignore_check)
  135. lzma_check_finish(&coder->check, coder->block->check);
  136. coder->sequence = SEQ_CHECK;
  137. // Fall through
  138. case SEQ_CHECK: {
  139. const size_t check_size = lzma_check_size(coder->block->check);
  140. lzma_bufcpy(in, in_pos, in_size, coder->block->raw_check,
  141. &coder->check_pos, check_size);
  142. if (coder->check_pos < check_size)
  143. return LZMA_OK;
  144. // Validate the Check only if we support it.
  145. // coder->check.buffer may be uninitialized
  146. // when the Check ID is not supported.
  147. if (!coder->ignore_check
  148. && lzma_check_is_supported(coder->block->check)
  149. && memcmp(coder->block->raw_check,
  150. coder->check.buffer.u8,
  151. check_size) != 0)
  152. return LZMA_DATA_ERROR;
  153. return LZMA_STREAM_END;
  154. }
  155. }
  156. return LZMA_PROG_ERROR;
  157. }
  158. static void
  159. block_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  160. {
  161. lzma_block_coder *coder = coder_ptr;
  162. lzma_next_end(&coder->next, allocator);
  163. lzma_free(coder, allocator);
  164. return;
  165. }
  166. extern lzma_ret
  167. lzma_block_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  168. lzma_block *block)
  169. {
  170. lzma_next_coder_init(&lzma_block_decoder_init, next, allocator);
  171. // Validate the options. lzma_block_unpadded_size() does that for us
  172. // except for Uncompressed Size and filters. Filters are validated
  173. // by the raw decoder.
  174. if (lzma_block_unpadded_size(block) == 0
  175. || !lzma_vli_is_valid(block->uncompressed_size))
  176. return LZMA_PROG_ERROR;
  177. // Allocate *next->coder if needed.
  178. lzma_block_coder *coder = next->coder;
  179. if (coder == NULL) {
  180. coder = lzma_alloc(sizeof(lzma_block_coder), allocator);
  181. if (coder == NULL)
  182. return LZMA_MEM_ERROR;
  183. next->coder = coder;
  184. next->code = &block_decode;
  185. next->end = &block_decoder_end;
  186. coder->next = LZMA_NEXT_CODER_INIT;
  187. }
  188. // Basic initializations
  189. coder->sequence = SEQ_CODE;
  190. coder->block = block;
  191. coder->compressed_size = 0;
  192. coder->uncompressed_size = 0;
  193. // If Compressed Size is not known, we calculate the maximum allowed
  194. // value so that encoded size of the Block (including Block Padding)
  195. // is still a valid VLI and a multiple of four.
  196. coder->compressed_limit
  197. = block->compressed_size == LZMA_VLI_UNKNOWN
  198. ? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
  199. - block->header_size
  200. - lzma_check_size(block->check)
  201. : block->compressed_size;
  202. // With Uncompressed Size this is simpler. If Block Header lacks
  203. // the size info, then LZMA_VLI_MAX is the maximum possible
  204. // Uncompressed Size.
  205. coder->uncompressed_limit
  206. = block->uncompressed_size == LZMA_VLI_UNKNOWN
  207. ? LZMA_VLI_MAX
  208. : block->uncompressed_size;
  209. // Initialize the check. It's caller's problem if the Check ID is not
  210. // supported, and the Block decoder cannot verify the Check field.
  211. // Caller can test lzma_check_is_supported(block->check).
  212. coder->check_pos = 0;
  213. lzma_check_init(&coder->check, block->check);
  214. coder->ignore_check = block->version >= 1
  215. ? block->ignore_check : false;
  216. // Initialize the filter chain.
  217. return lzma_raw_decoder_init(&coder->next, allocator,
  218. block->filters);
  219. }
  220. extern LZMA_API(lzma_ret)
  221. lzma_block_decoder(lzma_stream *strm, lzma_block *block)
  222. {
  223. lzma_next_strm_init(lzma_block_decoder_init, strm, block);
  224. strm->internal->supported_actions[LZMA_RUN] = true;
  225. strm->internal->supported_actions[LZMA_FINISH] = true;
  226. return LZMA_OK;
  227. }