stream_decoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file stream_decoder.c
  4. /// \brief Decodes .xz Streams
  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 "stream_decoder.h"
  13. #include "block_decoder.h"
  14. typedef struct {
  15. enum {
  16. SEQ_STREAM_HEADER,
  17. SEQ_BLOCK_HEADER,
  18. SEQ_BLOCK,
  19. SEQ_INDEX,
  20. SEQ_STREAM_FOOTER,
  21. SEQ_STREAM_PADDING,
  22. } sequence;
  23. /// Block or Metadata decoder. This takes little memory and the same
  24. /// data structure can be used to decode every Block Header, so it's
  25. /// a good idea to have a separate lzma_next_coder structure for it.
  26. lzma_next_coder block_decoder;
  27. /// Block options decoded by the Block Header decoder and used by
  28. /// the Block decoder.
  29. lzma_block block_options;
  30. /// Stream Flags from Stream Header
  31. lzma_stream_flags stream_flags;
  32. /// Index is hashed so that it can be compared to the sizes of Blocks
  33. /// with O(1) memory usage.
  34. lzma_index_hash *index_hash;
  35. /// Memory usage limit
  36. uint64_t memlimit;
  37. /// Amount of memory actually needed (only an estimate)
  38. uint64_t memusage;
  39. /// If true, LZMA_NO_CHECK is returned if the Stream has
  40. /// no integrity check.
  41. bool tell_no_check;
  42. /// If true, LZMA_UNSUPPORTED_CHECK is returned if the Stream has
  43. /// an integrity check that isn't supported by this liblzma build.
  44. bool tell_unsupported_check;
  45. /// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
  46. bool tell_any_check;
  47. /// If true, we will tell the Block decoder to skip calculating
  48. /// and verifying the integrity check.
  49. bool ignore_check;
  50. /// If true, we will decode concatenated Streams that possibly have
  51. /// Stream Padding between or after them. LZMA_STREAM_END is returned
  52. /// once the application isn't giving us any new input, and we aren't
  53. /// in the middle of a Stream, and possible Stream Padding is a
  54. /// multiple of four bytes.
  55. bool concatenated;
  56. /// When decoding concatenated Streams, this is true as long as we
  57. /// are decoding the first Stream. This is needed to avoid misleading
  58. /// LZMA_FORMAT_ERROR in case the later Streams don't have valid magic
  59. /// bytes.
  60. bool first_stream;
  61. /// Write position in buffer[] and position in Stream Padding
  62. size_t pos;
  63. /// Buffer to hold Stream Header, Block Header, and Stream Footer.
  64. /// Block Header has biggest maximum size.
  65. uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
  66. } lzma_stream_coder;
  67. static lzma_ret
  68. stream_decoder_reset(lzma_stream_coder *coder, const lzma_allocator *allocator)
  69. {
  70. // Initialize the Index hash used to verify the Index.
  71. coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
  72. if (coder->index_hash == NULL)
  73. return LZMA_MEM_ERROR;
  74. // Reset the rest of the variables.
  75. coder->sequence = SEQ_STREAM_HEADER;
  76. coder->pos = 0;
  77. return LZMA_OK;
  78. }
  79. static lzma_ret
  80. stream_decode(void *coder_ptr, const lzma_allocator *allocator,
  81. const uint8_t *restrict in, size_t *restrict in_pos,
  82. size_t in_size, uint8_t *restrict out,
  83. size_t *restrict out_pos, size_t out_size, lzma_action action)
  84. {
  85. lzma_stream_coder *coder = coder_ptr;
  86. // When decoding the actual Block, it may be able to produce more
  87. // output even if we don't give it any new input.
  88. while (true)
  89. switch (coder->sequence) {
  90. case SEQ_STREAM_HEADER: {
  91. // Copy the Stream Header to the internal buffer.
  92. lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
  93. LZMA_STREAM_HEADER_SIZE);
  94. // Return if we didn't get the whole Stream Header yet.
  95. if (coder->pos < LZMA_STREAM_HEADER_SIZE)
  96. return LZMA_OK;
  97. coder->pos = 0;
  98. // Decode the Stream Header.
  99. const lzma_ret ret = lzma_stream_header_decode(
  100. &coder->stream_flags, coder->buffer);
  101. if (ret != LZMA_OK)
  102. return ret == LZMA_FORMAT_ERROR && !coder->first_stream
  103. ? LZMA_DATA_ERROR : ret;
  104. // If we are decoding concatenated Streams, and the later
  105. // Streams have invalid Header Magic Bytes, we give
  106. // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
  107. coder->first_stream = false;
  108. // Copy the type of the Check so that Block Header and Block
  109. // decoders see it.
  110. coder->block_options.check = coder->stream_flags.check;
  111. // Even if we return LZMA_*_CHECK below, we want
  112. // to continue from Block Header decoding.
  113. coder->sequence = SEQ_BLOCK_HEADER;
  114. // Detect if there's no integrity check or if it is
  115. // unsupported if those were requested by the application.
  116. if (coder->tell_no_check && coder->stream_flags.check
  117. == LZMA_CHECK_NONE)
  118. return LZMA_NO_CHECK;
  119. if (coder->tell_unsupported_check
  120. && !lzma_check_is_supported(
  121. coder->stream_flags.check))
  122. return LZMA_UNSUPPORTED_CHECK;
  123. if (coder->tell_any_check)
  124. return LZMA_GET_CHECK;
  125. }
  126. // Fall through
  127. case SEQ_BLOCK_HEADER: {
  128. if (*in_pos >= in_size)
  129. return LZMA_OK;
  130. if (coder->pos == 0) {
  131. // Detect if it's Index.
  132. if (in[*in_pos] == 0x00) {
  133. coder->sequence = SEQ_INDEX;
  134. break;
  135. }
  136. // Calculate the size of the Block Header. Note that
  137. // Block Header decoder wants to see this byte too
  138. // so don't advance *in_pos.
  139. coder->block_options.header_size
  140. = lzma_block_header_size_decode(
  141. in[*in_pos]);
  142. }
  143. // Copy the Block Header to the internal buffer.
  144. lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
  145. coder->block_options.header_size);
  146. // Return if we didn't get the whole Block Header yet.
  147. if (coder->pos < coder->block_options.header_size)
  148. return LZMA_OK;
  149. coder->pos = 0;
  150. // Version 1 is needed to support the .ignore_check option.
  151. coder->block_options.version = 1;
  152. // Set up a buffer to hold the filter chain. Block Header
  153. // decoder will initialize all members of this array so
  154. // we don't need to do it here.
  155. lzma_filter filters[LZMA_FILTERS_MAX + 1];
  156. coder->block_options.filters = filters;
  157. // Decode the Block Header.
  158. return_if_error(lzma_block_header_decode(&coder->block_options,
  159. allocator, coder->buffer));
  160. // If LZMA_IGNORE_CHECK was used, this flag needs to be set.
  161. // It has to be set after lzma_block_header_decode() because
  162. // it always resets this to false.
  163. coder->block_options.ignore_check = coder->ignore_check;
  164. // Check the memory usage limit.
  165. const uint64_t memusage = lzma_raw_decoder_memusage(filters);
  166. lzma_ret ret;
  167. if (memusage == UINT64_MAX) {
  168. // One or more unknown Filter IDs.
  169. ret = LZMA_OPTIONS_ERROR;
  170. } else {
  171. // Now we can set coder->memusage since we know that
  172. // the filter chain is valid. We don't want
  173. // lzma_memusage() to return UINT64_MAX in case of
  174. // invalid filter chain.
  175. coder->memusage = memusage;
  176. if (memusage > coder->memlimit) {
  177. // The chain would need too much memory.
  178. ret = LZMA_MEMLIMIT_ERROR;
  179. } else {
  180. // Memory usage is OK.
  181. // Initialize the Block decoder.
  182. ret = lzma_block_decoder_init(
  183. &coder->block_decoder,
  184. allocator,
  185. &coder->block_options);
  186. }
  187. }
  188. // Free the allocated filter options since they are needed
  189. // only to initialize the Block decoder.
  190. for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
  191. lzma_free(filters[i].options, allocator);
  192. coder->block_options.filters = NULL;
  193. // Check if memory usage calculation and Block enocoder
  194. // initialization succeeded.
  195. if (ret != LZMA_OK)
  196. return ret;
  197. coder->sequence = SEQ_BLOCK;
  198. }
  199. // Fall through
  200. case SEQ_BLOCK: {
  201. const lzma_ret ret = coder->block_decoder.code(
  202. coder->block_decoder.coder, allocator,
  203. in, in_pos, in_size, out, out_pos, out_size,
  204. action);
  205. if (ret != LZMA_STREAM_END)
  206. return ret;
  207. // Block decoded successfully. Add the new size pair to
  208. // the Index hash.
  209. return_if_error(lzma_index_hash_append(coder->index_hash,
  210. lzma_block_unpadded_size(
  211. &coder->block_options),
  212. coder->block_options.uncompressed_size));
  213. coder->sequence = SEQ_BLOCK_HEADER;
  214. break;
  215. }
  216. case SEQ_INDEX: {
  217. // If we don't have any input, don't call
  218. // lzma_index_hash_decode() since it would return
  219. // LZMA_BUF_ERROR, which we must not do here.
  220. if (*in_pos >= in_size)
  221. return LZMA_OK;
  222. // Decode the Index and compare it to the hash calculated
  223. // from the sizes of the Blocks (if any).
  224. const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
  225. in, in_pos, in_size);
  226. if (ret != LZMA_STREAM_END)
  227. return ret;
  228. coder->sequence = SEQ_STREAM_FOOTER;
  229. }
  230. // Fall through
  231. case SEQ_STREAM_FOOTER: {
  232. // Copy the Stream Footer to the internal buffer.
  233. lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
  234. LZMA_STREAM_HEADER_SIZE);
  235. // Return if we didn't get the whole Stream Footer yet.
  236. if (coder->pos < LZMA_STREAM_HEADER_SIZE)
  237. return LZMA_OK;
  238. coder->pos = 0;
  239. // Decode the Stream Footer. The decoder gives
  240. // LZMA_FORMAT_ERROR if the magic bytes don't match,
  241. // so convert that return code to LZMA_DATA_ERROR.
  242. lzma_stream_flags footer_flags;
  243. const lzma_ret ret = lzma_stream_footer_decode(
  244. &footer_flags, coder->buffer);
  245. if (ret != LZMA_OK)
  246. return ret == LZMA_FORMAT_ERROR
  247. ? LZMA_DATA_ERROR : ret;
  248. // Check that Index Size stored in the Stream Footer matches
  249. // the real size of the Index field.
  250. if (lzma_index_hash_size(coder->index_hash)
  251. != footer_flags.backward_size)
  252. return LZMA_DATA_ERROR;
  253. // Compare that the Stream Flags fields are identical in
  254. // both Stream Header and Stream Footer.
  255. return_if_error(lzma_stream_flags_compare(
  256. &coder->stream_flags, &footer_flags));
  257. if (!coder->concatenated)
  258. return LZMA_STREAM_END;
  259. coder->sequence = SEQ_STREAM_PADDING;
  260. }
  261. // Fall through
  262. case SEQ_STREAM_PADDING:
  263. assert(coder->concatenated);
  264. // Skip over possible Stream Padding.
  265. while (true) {
  266. if (*in_pos >= in_size) {
  267. // Unless LZMA_FINISH was used, we cannot
  268. // know if there's more input coming later.
  269. if (action != LZMA_FINISH)
  270. return LZMA_OK;
  271. // Stream Padding must be a multiple of
  272. // four bytes.
  273. return coder->pos == 0
  274. ? LZMA_STREAM_END
  275. : LZMA_DATA_ERROR;
  276. }
  277. // If the byte is not zero, it probably indicates
  278. // beginning of a new Stream (or the file is corrupt).
  279. if (in[*in_pos] != 0x00)
  280. break;
  281. ++*in_pos;
  282. coder->pos = (coder->pos + 1) & 3;
  283. }
  284. // Stream Padding must be a multiple of four bytes (empty
  285. // Stream Padding is OK).
  286. if (coder->pos != 0) {
  287. ++*in_pos;
  288. return LZMA_DATA_ERROR;
  289. }
  290. // Prepare to decode the next Stream.
  291. return_if_error(stream_decoder_reset(coder, allocator));
  292. break;
  293. default:
  294. assert(0);
  295. return LZMA_PROG_ERROR;
  296. }
  297. // Never reached
  298. }
  299. static void
  300. stream_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  301. {
  302. lzma_stream_coder *coder = coder_ptr;
  303. lzma_next_end(&coder->block_decoder, allocator);
  304. lzma_index_hash_end(coder->index_hash, allocator);
  305. lzma_free(coder, allocator);
  306. return;
  307. }
  308. static lzma_check
  309. stream_decoder_get_check(const void *coder_ptr)
  310. {
  311. const lzma_stream_coder *coder = coder_ptr;
  312. return coder->stream_flags.check;
  313. }
  314. static lzma_ret
  315. stream_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
  316. uint64_t *old_memlimit, uint64_t new_memlimit)
  317. {
  318. lzma_stream_coder *coder = coder_ptr;
  319. *memusage = coder->memusage;
  320. *old_memlimit = coder->memlimit;
  321. if (new_memlimit != 0) {
  322. if (new_memlimit < coder->memusage)
  323. return LZMA_MEMLIMIT_ERROR;
  324. coder->memlimit = new_memlimit;
  325. }
  326. return LZMA_OK;
  327. }
  328. extern lzma_ret
  329. lzma_stream_decoder_init(
  330. lzma_next_coder *next, const lzma_allocator *allocator,
  331. uint64_t memlimit, uint32_t flags)
  332. {
  333. lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator);
  334. if (flags & ~LZMA_SUPPORTED_FLAGS)
  335. return LZMA_OPTIONS_ERROR;
  336. lzma_stream_coder *coder = next->coder;
  337. if (coder == NULL) {
  338. coder = lzma_alloc(sizeof(lzma_stream_coder), allocator);
  339. if (coder == NULL)
  340. return LZMA_MEM_ERROR;
  341. next->coder = coder;
  342. next->code = &stream_decode;
  343. next->end = &stream_decoder_end;
  344. next->get_check = &stream_decoder_get_check;
  345. next->memconfig = &stream_decoder_memconfig;
  346. coder->block_decoder = LZMA_NEXT_CODER_INIT;
  347. coder->index_hash = NULL;
  348. }
  349. coder->memlimit = my_max(1, memlimit);
  350. coder->memusage = LZMA_MEMUSAGE_BASE;
  351. coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
  352. coder->tell_unsupported_check
  353. = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
  354. coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
  355. coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0;
  356. coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
  357. coder->first_stream = true;
  358. return stream_decoder_reset(coder, allocator);
  359. }
  360. extern LZMA_API(lzma_ret)
  361. lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  362. {
  363. lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
  364. strm->internal->supported_actions[LZMA_RUN] = true;
  365. strm->internal->supported_actions[LZMA_FINISH] = true;
  366. return LZMA_OK;
  367. }