stream_decoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include "index.h"
  15. typedef struct {
  16. enum {
  17. SEQ_STREAM_HEADER,
  18. SEQ_BLOCK_HEADER,
  19. SEQ_BLOCK_INIT,
  20. SEQ_BLOCK_RUN,
  21. SEQ_INDEX,
  22. SEQ_STREAM_FOOTER,
  23. SEQ_STREAM_PADDING,
  24. } sequence;
  25. /// Block decoder
  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 (LZMA_FINISH),
  53. /// and we aren't in the middle of a Stream, and possible
  54. /// Stream Padding is a 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] == INDEX_INDICATOR) {
  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. coder->sequence = SEQ_BLOCK_INIT;
  151. }
  152. // Fall through
  153. case SEQ_BLOCK_INIT: {
  154. // Checking memusage and doing the initialization needs
  155. // its own sequence point because we need to be able to
  156. // retry if we return LZMA_MEMLIMIT_ERROR.
  157. // Version 1 is needed to support the .ignore_check option.
  158. coder->block_options.version = 1;
  159. // Set up a buffer to hold the filter chain. Block Header
  160. // decoder will initialize all members of this array so
  161. // we don't need to do it here.
  162. lzma_filter filters[LZMA_FILTERS_MAX + 1];
  163. coder->block_options.filters = filters;
  164. // Decode the Block Header.
  165. return_if_error(lzma_block_header_decode(&coder->block_options,
  166. allocator, coder->buffer));
  167. // If LZMA_IGNORE_CHECK was used, this flag needs to be set.
  168. // It has to be set after lzma_block_header_decode() because
  169. // it always resets this to false.
  170. coder->block_options.ignore_check = coder->ignore_check;
  171. // Check the memory usage limit.
  172. const uint64_t memusage = lzma_raw_decoder_memusage(filters);
  173. lzma_ret ret;
  174. if (memusage == UINT64_MAX) {
  175. // One or more unknown Filter IDs.
  176. ret = LZMA_OPTIONS_ERROR;
  177. } else {
  178. // Now we can set coder->memusage since we know that
  179. // the filter chain is valid. We don't want
  180. // lzma_memusage() to return UINT64_MAX in case of
  181. // invalid filter chain.
  182. coder->memusage = memusage;
  183. if (memusage > coder->memlimit) {
  184. // The chain would need too much memory.
  185. ret = LZMA_MEMLIMIT_ERROR;
  186. } else {
  187. // Memory usage is OK.
  188. // Initialize the Block decoder.
  189. ret = lzma_block_decoder_init(
  190. &coder->block_decoder,
  191. allocator,
  192. &coder->block_options);
  193. }
  194. }
  195. // Free the allocated filter options since they are needed
  196. // only to initialize the Block decoder.
  197. lzma_filters_free(filters, allocator);
  198. coder->block_options.filters = NULL;
  199. // Check if memory usage calculation and Block decoder
  200. // initialization succeeded.
  201. if (ret != LZMA_OK)
  202. return ret;
  203. coder->sequence = SEQ_BLOCK_RUN;
  204. }
  205. // Fall through
  206. case SEQ_BLOCK_RUN: {
  207. const lzma_ret ret = coder->block_decoder.code(
  208. coder->block_decoder.coder, allocator,
  209. in, in_pos, in_size, out, out_pos, out_size,
  210. action);
  211. if (ret != LZMA_STREAM_END)
  212. return ret;
  213. // Block decoded successfully. Add the new size pair to
  214. // the Index hash.
  215. return_if_error(lzma_index_hash_append(coder->index_hash,
  216. lzma_block_unpadded_size(
  217. &coder->block_options),
  218. coder->block_options.uncompressed_size));
  219. coder->sequence = SEQ_BLOCK_HEADER;
  220. break;
  221. }
  222. case SEQ_INDEX: {
  223. // If we don't have any input, don't call
  224. // lzma_index_hash_decode() since it would return
  225. // LZMA_BUF_ERROR, which we must not do here.
  226. if (*in_pos >= in_size)
  227. return LZMA_OK;
  228. // Decode the Index and compare it to the hash calculated
  229. // from the sizes of the Blocks (if any).
  230. const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
  231. in, in_pos, in_size);
  232. if (ret != LZMA_STREAM_END)
  233. return ret;
  234. coder->sequence = SEQ_STREAM_FOOTER;
  235. }
  236. // Fall through
  237. case SEQ_STREAM_FOOTER: {
  238. // Copy the Stream Footer to the internal buffer.
  239. lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
  240. LZMA_STREAM_HEADER_SIZE);
  241. // Return if we didn't get the whole Stream Footer yet.
  242. if (coder->pos < LZMA_STREAM_HEADER_SIZE)
  243. return LZMA_OK;
  244. coder->pos = 0;
  245. // Decode the Stream Footer. The decoder gives
  246. // LZMA_FORMAT_ERROR if the magic bytes don't match,
  247. // so convert that return code to LZMA_DATA_ERROR.
  248. lzma_stream_flags footer_flags;
  249. const lzma_ret ret = lzma_stream_footer_decode(
  250. &footer_flags, coder->buffer);
  251. if (ret != LZMA_OK)
  252. return ret == LZMA_FORMAT_ERROR
  253. ? LZMA_DATA_ERROR : ret;
  254. // Check that Index Size stored in the Stream Footer matches
  255. // the real size of the Index field.
  256. if (lzma_index_hash_size(coder->index_hash)
  257. != footer_flags.backward_size)
  258. return LZMA_DATA_ERROR;
  259. // Compare that the Stream Flags fields are identical in
  260. // both Stream Header and Stream Footer.
  261. return_if_error(lzma_stream_flags_compare(
  262. &coder->stream_flags, &footer_flags));
  263. if (!coder->concatenated)
  264. return LZMA_STREAM_END;
  265. coder->sequence = SEQ_STREAM_PADDING;
  266. }
  267. // Fall through
  268. case SEQ_STREAM_PADDING:
  269. assert(coder->concatenated);
  270. // Skip over possible Stream Padding.
  271. while (true) {
  272. if (*in_pos >= in_size) {
  273. // Unless LZMA_FINISH was used, we cannot
  274. // know if there's more input coming later.
  275. if (action != LZMA_FINISH)
  276. return LZMA_OK;
  277. // Stream Padding must be a multiple of
  278. // four bytes.
  279. return coder->pos == 0
  280. ? LZMA_STREAM_END
  281. : LZMA_DATA_ERROR;
  282. }
  283. // If the byte is not zero, it probably indicates
  284. // beginning of a new Stream (or the file is corrupt).
  285. if (in[*in_pos] != 0x00)
  286. break;
  287. ++*in_pos;
  288. coder->pos = (coder->pos + 1) & 3;
  289. }
  290. // Stream Padding must be a multiple of four bytes (empty
  291. // Stream Padding is OK).
  292. if (coder->pos != 0) {
  293. ++*in_pos;
  294. return LZMA_DATA_ERROR;
  295. }
  296. // Prepare to decode the next Stream.
  297. return_if_error(stream_decoder_reset(coder, allocator));
  298. break;
  299. default:
  300. assert(0);
  301. return LZMA_PROG_ERROR;
  302. }
  303. // Never reached
  304. }
  305. static void
  306. stream_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  307. {
  308. lzma_stream_coder *coder = coder_ptr;
  309. lzma_next_end(&coder->block_decoder, allocator);
  310. lzma_index_hash_end(coder->index_hash, allocator);
  311. lzma_free(coder, allocator);
  312. return;
  313. }
  314. static lzma_check
  315. stream_decoder_get_check(const void *coder_ptr)
  316. {
  317. const lzma_stream_coder *coder = coder_ptr;
  318. return coder->stream_flags.check;
  319. }
  320. static lzma_ret
  321. stream_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
  322. uint64_t *old_memlimit, uint64_t new_memlimit)
  323. {
  324. lzma_stream_coder *coder = coder_ptr;
  325. *memusage = coder->memusage;
  326. *old_memlimit = coder->memlimit;
  327. if (new_memlimit != 0) {
  328. if (new_memlimit < coder->memusage)
  329. return LZMA_MEMLIMIT_ERROR;
  330. coder->memlimit = new_memlimit;
  331. }
  332. return LZMA_OK;
  333. }
  334. extern lzma_ret
  335. lzma_stream_decoder_init(
  336. lzma_next_coder *next, const lzma_allocator *allocator,
  337. uint64_t memlimit, uint32_t flags)
  338. {
  339. lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator);
  340. if (flags & ~LZMA_SUPPORTED_FLAGS)
  341. return LZMA_OPTIONS_ERROR;
  342. lzma_stream_coder *coder = next->coder;
  343. if (coder == NULL) {
  344. coder = lzma_alloc(sizeof(lzma_stream_coder), allocator);
  345. if (coder == NULL)
  346. return LZMA_MEM_ERROR;
  347. next->coder = coder;
  348. next->code = &stream_decode;
  349. next->end = &stream_decoder_end;
  350. next->get_check = &stream_decoder_get_check;
  351. next->memconfig = &stream_decoder_memconfig;
  352. coder->block_decoder = LZMA_NEXT_CODER_INIT;
  353. coder->index_hash = NULL;
  354. }
  355. coder->memlimit = my_max(1, memlimit);
  356. coder->memusage = LZMA_MEMUSAGE_BASE;
  357. coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
  358. coder->tell_unsupported_check
  359. = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
  360. coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
  361. coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0;
  362. coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
  363. coder->first_stream = true;
  364. return stream_decoder_reset(coder, allocator);
  365. }
  366. extern LZMA_API(lzma_ret)
  367. lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  368. {
  369. lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
  370. strm->internal->supported_actions[LZMA_RUN] = true;
  371. strm->internal->supported_actions[LZMA_FINISH] = true;
  372. return LZMA_OK;
  373. }