stream_decoder.c 12 KB

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