block_decoder.c 6.5 KB

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