stream_decoder.c 13 KB

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