file_info.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file file_info.c
  5. /// \brief Decode .xz file information into a lzma_index structure
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "index_decoder.h"
  11. typedef struct {
  12. enum {
  13. SEQ_MAGIC_BYTES,
  14. SEQ_PADDING_SEEK,
  15. SEQ_PADDING_DECODE,
  16. SEQ_FOOTER,
  17. SEQ_INDEX_INIT,
  18. SEQ_INDEX_DECODE,
  19. SEQ_HEADER_DECODE,
  20. SEQ_HEADER_COMPARE,
  21. } sequence;
  22. /// Absolute position of in[*in_pos] in the file. All code that
  23. /// modifies *in_pos also updates this. seek_to_pos() needs this
  24. /// to determine if we need to request the application to seek for
  25. /// us or if we can do the seeking internally by adjusting *in_pos.
  26. uint64_t file_cur_pos;
  27. /// This refers to absolute positions of interesting parts of the
  28. /// input file. Sometimes it points to the *beginning* of a specific
  29. /// field and sometimes to the *end* of a field. The current target
  30. /// position at each moment is explained in the comments.
  31. uint64_t file_target_pos;
  32. /// Size of the .xz file (from the application).
  33. uint64_t file_size;
  34. /// Index decoder
  35. lzma_next_coder index_decoder;
  36. /// Number of bytes remaining in the Index field that is currently
  37. /// being decoded.
  38. lzma_vli index_remaining;
  39. /// The Index decoder will store the decoded Index in this pointer.
  40. lzma_index *this_index;
  41. /// Amount of Stream Padding in the current Stream.
  42. lzma_vli stream_padding;
  43. /// The final combined index is collected here.
  44. lzma_index *combined_index;
  45. /// Pointer from the application where to store the index information
  46. /// after successful decoding.
  47. lzma_index **dest_index;
  48. /// Pointer to lzma_stream.seek_pos to be used when returning
  49. /// LZMA_SEEK_NEEDED. This is set by seek_to_pos() when needed.
  50. uint64_t *external_seek_pos;
  51. /// Memory usage limit
  52. uint64_t memlimit;
  53. /// Stream Flags from the very beginning of the file.
  54. lzma_stream_flags first_header_flags;
  55. /// Stream Flags from Stream Header of the current Stream.
  56. lzma_stream_flags header_flags;
  57. /// Stream Flags from Stream Footer of the current Stream.
  58. lzma_stream_flags footer_flags;
  59. size_t temp_pos;
  60. size_t temp_size;
  61. uint8_t temp[8192];
  62. } lzma_file_info_coder;
  63. /// Copies data from in[*in_pos] into coder->temp until
  64. /// coder->temp_pos == coder->temp_size. This also keeps coder->file_cur_pos
  65. /// in sync with *in_pos. Returns true if more input is needed.
  66. static bool
  67. fill_temp(lzma_file_info_coder *coder, const uint8_t *restrict in,
  68. size_t *restrict in_pos, size_t in_size)
  69. {
  70. coder->file_cur_pos += lzma_bufcpy(in, in_pos, in_size,
  71. coder->temp, &coder->temp_pos, coder->temp_size);
  72. return coder->temp_pos < coder->temp_size;
  73. }
  74. /// Seeks to the absolute file position specified by target_pos.
  75. /// This tries to do the seeking by only modifying *in_pos, if possible.
  76. /// The main benefit of this is that if one passes the whole file at once
  77. /// to lzma_code(), the decoder will never need to return LZMA_SEEK_NEEDED
  78. /// as all the seeking can be done by adjusting *in_pos in this function.
  79. ///
  80. /// Returns true if an external seek is needed and the caller must return
  81. /// LZMA_SEEK_NEEDED.
  82. static bool
  83. seek_to_pos(lzma_file_info_coder *coder, uint64_t target_pos,
  84. size_t in_start, size_t *in_pos, size_t in_size)
  85. {
  86. // The input buffer doesn't extend beyond the end of the file.
  87. // This has been checked by file_info_decode() already.
  88. assert(coder->file_size - coder->file_cur_pos >= in_size - *in_pos);
  89. const uint64_t pos_min = coder->file_cur_pos - (*in_pos - in_start);
  90. const uint64_t pos_max = coder->file_cur_pos + (in_size - *in_pos);
  91. bool external_seek_needed;
  92. if (target_pos >= pos_min && target_pos <= pos_max) {
  93. // The requested position is available in the current input
  94. // buffer or right after it. That is, in a corner case we
  95. // end up setting *in_pos == in_size and thus will immediately
  96. // need new input bytes from the application.
  97. *in_pos += (size_t)(target_pos - coder->file_cur_pos);
  98. external_seek_needed = false;
  99. } else {
  100. // Ask the application to seek the input file.
  101. *coder->external_seek_pos = target_pos;
  102. external_seek_needed = true;
  103. // Mark the whole input buffer as used. This way
  104. // lzma_stream.total_in will have a better estimate
  105. // of the amount of data read. It still won't be perfect
  106. // as the value will depend on the input buffer size that
  107. // the application uses, but it should be good enough for
  108. // those few who want an estimate.
  109. *in_pos = in_size;
  110. }
  111. // After seeking (internal or external) the current position
  112. // will match the requested target position.
  113. coder->file_cur_pos = target_pos;
  114. return external_seek_needed;
  115. }
  116. /// The caller sets coder->file_target_pos so that it points to the *end*
  117. /// of the desired file position. This function then determines how far
  118. /// backwards from that position we can seek. After seeking fill_temp()
  119. /// can be used to read data into coder->temp. When fill_temp() has finished,
  120. /// coder->temp[coder->temp_size] will match coder->file_target_pos.
  121. ///
  122. /// This also validates that coder->target_file_pos is sane in sense that
  123. /// we aren't trying to seek too far backwards (too close or beyond the
  124. /// beginning of the file).
  125. static lzma_ret
  126. reverse_seek(lzma_file_info_coder *coder,
  127. size_t in_start, size_t *in_pos, size_t in_size)
  128. {
  129. // Check that there is enough data before the target position
  130. // to contain at least Stream Header and Stream Footer. If there
  131. // isn't, the file cannot be valid.
  132. if (coder->file_target_pos < 2 * LZMA_STREAM_HEADER_SIZE)
  133. return LZMA_DATA_ERROR;
  134. coder->temp_pos = 0;
  135. // The Stream Header at the very beginning of the file gets handled
  136. // specially in SEQ_MAGIC_BYTES and thus we will never need to seek
  137. // there. By not seeking to the first LZMA_STREAM_HEADER_SIZE bytes
  138. // we avoid a useless external seek after SEQ_MAGIC_BYTES if the
  139. // application uses an extremely small input buffer and the input
  140. // file is very small.
  141. if (coder->file_target_pos - LZMA_STREAM_HEADER_SIZE
  142. < sizeof(coder->temp))
  143. coder->temp_size = (size_t)(coder->file_target_pos
  144. - LZMA_STREAM_HEADER_SIZE);
  145. else
  146. coder->temp_size = sizeof(coder->temp);
  147. // The above if-statements guarantee this. This is important because
  148. // the Stream Header/Footer decoders assume that there's at least
  149. // LZMA_STREAM_HEADER_SIZE bytes in coder->temp.
  150. assert(coder->temp_size >= LZMA_STREAM_HEADER_SIZE);
  151. if (seek_to_pos(coder, coder->file_target_pos - coder->temp_size,
  152. in_start, in_pos, in_size))
  153. return LZMA_SEEK_NEEDED;
  154. return LZMA_OK;
  155. }
  156. /// Gets the number of zero-bytes at the end of the buffer.
  157. static size_t
  158. get_padding_size(const uint8_t *buf, size_t buf_size)
  159. {
  160. size_t padding = 0;
  161. while (buf_size > 0 && buf[--buf_size] == 0x00)
  162. ++padding;
  163. return padding;
  164. }
  165. /// With the Stream Header at the very beginning of the file, LZMA_FORMAT_ERROR
  166. /// is used to tell the application that Magic Bytes didn't match. In other
  167. /// Stream Header/Footer fields (in the middle/end of the file) it could be
  168. /// a bit confusing to return LZMA_FORMAT_ERROR as we already know that there
  169. /// is a valid Stream Header at the beginning of the file. For those cases
  170. /// this function is used to convert LZMA_FORMAT_ERROR to LZMA_DATA_ERROR.
  171. static lzma_ret
  172. hide_format_error(lzma_ret ret)
  173. {
  174. if (ret == LZMA_FORMAT_ERROR)
  175. ret = LZMA_DATA_ERROR;
  176. return ret;
  177. }
  178. /// Calls the Index decoder and updates coder->index_remaining.
  179. /// This is a separate function because the input can be either directly
  180. /// from the application or from coder->temp.
  181. static lzma_ret
  182. decode_index(lzma_file_info_coder *coder, const lzma_allocator *allocator,
  183. const uint8_t *restrict in, size_t *restrict in_pos,
  184. size_t in_size, bool update_file_cur_pos)
  185. {
  186. const size_t in_start = *in_pos;
  187. const lzma_ret ret = coder->index_decoder.code(
  188. coder->index_decoder.coder,
  189. allocator, in, in_pos, in_size,
  190. NULL, NULL, 0, LZMA_RUN);
  191. coder->index_remaining -= *in_pos - in_start;
  192. if (update_file_cur_pos)
  193. coder->file_cur_pos += *in_pos - in_start;
  194. return ret;
  195. }
  196. static lzma_ret
  197. file_info_decode(void *coder_ptr, const lzma_allocator *allocator,
  198. const uint8_t *restrict in, size_t *restrict in_pos,
  199. size_t in_size,
  200. uint8_t *restrict out lzma_attribute((__unused__)),
  201. size_t *restrict out_pos lzma_attribute((__unused__)),
  202. size_t out_size lzma_attribute((__unused__)),
  203. lzma_action action lzma_attribute((__unused__)))
  204. {
  205. lzma_file_info_coder *coder = coder_ptr;
  206. const size_t in_start = *in_pos;
  207. // If the caller provides input past the end of the file, trim
  208. // the extra bytes from the buffer so that we won't read too far.
  209. assert(coder->file_size >= coder->file_cur_pos);
  210. if (coder->file_size - coder->file_cur_pos < in_size - in_start)
  211. in_size = in_start
  212. + (size_t)(coder->file_size - coder->file_cur_pos);
  213. while (true)
  214. switch (coder->sequence) {
  215. case SEQ_MAGIC_BYTES:
  216. // Decode the Stream Header at the beginning of the file
  217. // first to check if the Magic Bytes match. The flags
  218. // are stored in coder->first_header_flags so that we
  219. // don't need to seek to it again.
  220. //
  221. // Check that the file is big enough to contain at least
  222. // Stream Header.
  223. if (coder->file_size < LZMA_STREAM_HEADER_SIZE)
  224. return LZMA_FORMAT_ERROR;
  225. // Read the Stream Header field into coder->temp.
  226. if (fill_temp(coder, in, in_pos, in_size))
  227. return LZMA_OK;
  228. // This is the only Stream Header/Footer decoding where we
  229. // want to return LZMA_FORMAT_ERROR if the Magic Bytes don't
  230. // match. Elsewhere it will be converted to LZMA_DATA_ERROR.
  231. return_if_error(lzma_stream_header_decode(
  232. &coder->first_header_flags, coder->temp));
  233. // Now that we know that the Magic Bytes match, check the
  234. // file size. It's better to do this here after checking the
  235. // Magic Bytes since this way we can give LZMA_FORMAT_ERROR
  236. // instead of LZMA_DATA_ERROR when the Magic Bytes don't
  237. // match in a file that is too big or isn't a multiple of
  238. // four bytes.
  239. if (coder->file_size > LZMA_VLI_MAX || (coder->file_size & 3))
  240. return LZMA_DATA_ERROR;
  241. // Start looking for Stream Padding and Stream Footer
  242. // at the end of the file.
  243. coder->file_target_pos = coder->file_size;
  244. // Fall through
  245. case SEQ_PADDING_SEEK:
  246. coder->sequence = SEQ_PADDING_DECODE;
  247. return_if_error(reverse_seek(
  248. coder, in_start, in_pos, in_size));
  249. // Fall through
  250. case SEQ_PADDING_DECODE: {
  251. // Copy to coder->temp first. This keeps the code simpler if
  252. // the application only provides input a few bytes at a time.
  253. if (fill_temp(coder, in, in_pos, in_size))
  254. return LZMA_OK;
  255. // Scan the buffer backwards to get the size of the
  256. // Stream Padding field (if any).
  257. const size_t new_padding = get_padding_size(
  258. coder->temp, coder->temp_size);
  259. coder->stream_padding += new_padding;
  260. // Set the target position to the beginning of Stream Padding
  261. // that has been observed so far. If all Stream Padding has
  262. // been seen, then the target position will be at the end
  263. // of the Stream Footer field.
  264. coder->file_target_pos -= new_padding;
  265. if (new_padding == coder->temp_size) {
  266. // The whole buffer was padding. Seek backwards in
  267. // the file to get more input.
  268. coder->sequence = SEQ_PADDING_SEEK;
  269. break;
  270. }
  271. // Size of Stream Padding must be a multiple of 4 bytes.
  272. if (coder->stream_padding & 3)
  273. return LZMA_DATA_ERROR;
  274. coder->sequence = SEQ_FOOTER;
  275. // Calculate the amount of non-padding data in coder->temp.
  276. coder->temp_size -= new_padding;
  277. coder->temp_pos = coder->temp_size;
  278. // We can avoid an external seek if the whole Stream Footer
  279. // is already in coder->temp. In that case SEQ_FOOTER won't
  280. // read more input and will find the Stream Footer from
  281. // coder->temp[coder->temp_size - LZMA_STREAM_HEADER_SIZE].
  282. //
  283. // Otherwise we will need to seek. The seeking is done so
  284. // that Stream Footer will be at the end of coder->temp.
  285. // This way it's likely that we also get a complete Index
  286. // field into coder->temp without needing a separate seek
  287. // for that (unless the Index field is big).
  288. if (coder->temp_size < LZMA_STREAM_HEADER_SIZE)
  289. return_if_error(reverse_seek(
  290. coder, in_start, in_pos, in_size));
  291. }
  292. // Fall through
  293. case SEQ_FOOTER:
  294. // Copy the Stream Footer field into coder->temp.
  295. // If Stream Footer was already available in coder->temp
  296. // in SEQ_PADDING_DECODE, then this does nothing.
  297. if (fill_temp(coder, in, in_pos, in_size))
  298. return LZMA_OK;
  299. // Make coder->file_target_pos and coder->temp_size point
  300. // to the beginning of Stream Footer and thus to the end
  301. // of the Index field. coder->temp_pos will be updated
  302. // a bit later.
  303. coder->file_target_pos -= LZMA_STREAM_HEADER_SIZE;
  304. coder->temp_size -= LZMA_STREAM_HEADER_SIZE;
  305. // Decode Stream Footer.
  306. return_if_error(hide_format_error(lzma_stream_footer_decode(
  307. &coder->footer_flags,
  308. coder->temp + coder->temp_size)));
  309. // Check that we won't seek past the beginning of the file.
  310. //
  311. // LZMA_STREAM_HEADER_SIZE is added because there must be
  312. // space for Stream Header too even though we won't seek
  313. // there before decoding the Index field.
  314. //
  315. // There's no risk of integer overflow here because
  316. // Backward Size cannot be greater than 2^34.
  317. if (coder->file_target_pos < coder->footer_flags.backward_size
  318. + LZMA_STREAM_HEADER_SIZE)
  319. return LZMA_DATA_ERROR;
  320. // Set the target position to the beginning of the Index field.
  321. coder->file_target_pos -= coder->footer_flags.backward_size;
  322. coder->sequence = SEQ_INDEX_INIT;
  323. // We can avoid an external seek if the whole Index field is
  324. // already available in coder->temp.
  325. if (coder->temp_size >= coder->footer_flags.backward_size) {
  326. // Set coder->temp_pos to point to the beginning
  327. // of the Index.
  328. coder->temp_pos = coder->temp_size
  329. - coder->footer_flags.backward_size;
  330. } else {
  331. // These are set to zero to indicate that there's no
  332. // useful data (Index or anything else) in coder->temp.
  333. coder->temp_pos = 0;
  334. coder->temp_size = 0;
  335. // Seek to the beginning of the Index field.
  336. if (seek_to_pos(coder, coder->file_target_pos,
  337. in_start, in_pos, in_size))
  338. return LZMA_SEEK_NEEDED;
  339. }
  340. // Fall through
  341. case SEQ_INDEX_INIT: {
  342. // Calculate the amount of memory already used by the earlier
  343. // Indexes so that we know how big memory limit to pass to
  344. // the Index decoder.
  345. //
  346. // NOTE: When there are multiple Streams, the separate
  347. // lzma_index structures can use more RAM (as measured by
  348. // lzma_index_memused()) than the final combined lzma_index.
  349. // Thus memlimit may need to be slightly higher than the final
  350. // calculated memory usage will be. This is perhaps a bit
  351. // confusing to the application, but I think it shouldn't
  352. // cause problems in practice.
  353. uint64_t memused = 0;
  354. if (coder->combined_index != NULL) {
  355. memused = lzma_index_memused(coder->combined_index);
  356. assert(memused <= coder->memlimit);
  357. if (memused > coder->memlimit) // Extra sanity check
  358. return LZMA_PROG_ERROR;
  359. }
  360. // Initialize the Index decoder.
  361. return_if_error(lzma_index_decoder_init(
  362. &coder->index_decoder, allocator,
  363. &coder->this_index,
  364. coder->memlimit - memused));
  365. coder->index_remaining = coder->footer_flags.backward_size;
  366. coder->sequence = SEQ_INDEX_DECODE;
  367. }
  368. // Fall through
  369. case SEQ_INDEX_DECODE: {
  370. // Decode (a part of) the Index. If the whole Index is already
  371. // in coder->temp, read it from there. Otherwise read from
  372. // in[*in_pos] onwards. Note that index_decode() updates
  373. // coder->index_remaining and optionally coder->file_cur_pos.
  374. lzma_ret ret;
  375. if (coder->temp_size != 0) {
  376. assert(coder->temp_size - coder->temp_pos
  377. == coder->index_remaining);
  378. ret = decode_index(coder, allocator, coder->temp,
  379. &coder->temp_pos, coder->temp_size,
  380. false);
  381. } else {
  382. // Don't give the decoder more input than the known
  383. // remaining size of the Index field.
  384. size_t in_stop = in_size;
  385. if (in_size - *in_pos > coder->index_remaining)
  386. in_stop = *in_pos
  387. + (size_t)(coder->index_remaining);
  388. ret = decode_index(coder, allocator,
  389. in, in_pos, in_stop, true);
  390. }
  391. switch (ret) {
  392. case LZMA_OK:
  393. // If the Index docoder asks for more input when we
  394. // have already given it as much input as Backward Size
  395. // indicated, the file is invalid.
  396. if (coder->index_remaining == 0)
  397. return LZMA_DATA_ERROR;
  398. // We cannot get here if we were reading Index from
  399. // coder->temp because when reading from coder->temp
  400. // we give the Index decoder exactly
  401. // coder->index_remaining bytes of input.
  402. assert(coder->temp_size == 0);
  403. return LZMA_OK;
  404. case LZMA_STREAM_END:
  405. // If the decoding seems to be successful, check also
  406. // that the Index decoder consumed as much input as
  407. // indicated by the Backward Size field.
  408. if (coder->index_remaining != 0)
  409. return LZMA_DATA_ERROR;
  410. break;
  411. default:
  412. return ret;
  413. }
  414. // Calculate how much the Index tells us to seek backwards
  415. // (relative to the beginning of the Index): Total size of
  416. // all Blocks plus the size of the Stream Header field.
  417. // No integer overflow here because lzma_index_total_size()
  418. // cannot return a value greater than LZMA_VLI_MAX.
  419. const uint64_t seek_amount
  420. = lzma_index_total_size(coder->this_index)
  421. + LZMA_STREAM_HEADER_SIZE;
  422. // Check that Index is sane in sense that seek_amount won't
  423. // make us seek past the beginning of the file when locating
  424. // the Stream Header.
  425. //
  426. // coder->file_target_pos still points to the beginning of
  427. // the Index field.
  428. if (coder->file_target_pos < seek_amount)
  429. return LZMA_DATA_ERROR;
  430. // Set the target to the beginning of Stream Header.
  431. coder->file_target_pos -= seek_amount;
  432. if (coder->file_target_pos == 0) {
  433. // We would seek to the beginning of the file, but
  434. // since we already decoded that Stream Header in
  435. // SEQ_MAGIC_BYTES, we can use the cached value from
  436. // coder->first_header_flags to avoid the seek.
  437. coder->header_flags = coder->first_header_flags;
  438. coder->sequence = SEQ_HEADER_COMPARE;
  439. break;
  440. }
  441. coder->sequence = SEQ_HEADER_DECODE;
  442. // Make coder->file_target_pos point to the end of
  443. // the Stream Header field.
  444. coder->file_target_pos += LZMA_STREAM_HEADER_SIZE;
  445. // If coder->temp_size is non-zero, it points to the end
  446. // of the Index field. Then the beginning of the Index
  447. // field is at coder->temp[coder->temp_size
  448. // - coder->footer_flags.backward_size].
  449. assert(coder->temp_size == 0 || coder->temp_size
  450. >= coder->footer_flags.backward_size);
  451. // If coder->temp contained the whole Index, see if it has
  452. // enough data to contain also the Stream Header. If so,
  453. // we avoid an external seek.
  454. //
  455. // NOTE: This can happen only with small .xz files and only
  456. // for the non-first Stream as the Stream Flags of the first
  457. // Stream are cached and already handled a few lines above.
  458. // So this isn't as useful as the other seek-avoidance cases.
  459. if (coder->temp_size != 0 && coder->temp_size
  460. - coder->footer_flags.backward_size
  461. >= seek_amount) {
  462. // Make temp_pos and temp_size point to the *end* of
  463. // Stream Header so that SEQ_HEADER_DECODE will find
  464. // the start of Stream Header from coder->temp[
  465. // coder->temp_size - LZMA_STREAM_HEADER_SIZE].
  466. coder->temp_pos = coder->temp_size
  467. - coder->footer_flags.backward_size
  468. - seek_amount
  469. + LZMA_STREAM_HEADER_SIZE;
  470. coder->temp_size = coder->temp_pos;
  471. } else {
  472. // Seek so that Stream Header will be at the end of
  473. // coder->temp. With typical multi-Stream files we
  474. // will usually also get the Stream Footer and Index
  475. // of the *previous* Stream in coder->temp and thus
  476. // won't need a separate seek for them.
  477. return_if_error(reverse_seek(coder,
  478. in_start, in_pos, in_size));
  479. }
  480. }
  481. // Fall through
  482. case SEQ_HEADER_DECODE:
  483. // Copy the Stream Header field into coder->temp.
  484. // If Stream Header was already available in coder->temp
  485. // in SEQ_INDEX_DECODE, then this does nothing.
  486. if (fill_temp(coder, in, in_pos, in_size))
  487. return LZMA_OK;
  488. // Make all these point to the beginning of Stream Header.
  489. coder->file_target_pos -= LZMA_STREAM_HEADER_SIZE;
  490. coder->temp_size -= LZMA_STREAM_HEADER_SIZE;
  491. coder->temp_pos = coder->temp_size;
  492. // Decode the Stream Header.
  493. return_if_error(hide_format_error(lzma_stream_header_decode(
  494. &coder->header_flags,
  495. coder->temp + coder->temp_size)));
  496. coder->sequence = SEQ_HEADER_COMPARE;
  497. // Fall through
  498. case SEQ_HEADER_COMPARE:
  499. // Compare Stream Header against Stream Footer. They must
  500. // match.
  501. return_if_error(lzma_stream_flags_compare(
  502. &coder->header_flags, &coder->footer_flags));
  503. // Store the decoded Stream Flags into the Index. Use the
  504. // Footer Flags because it contains Backward Size, although
  505. // it shouldn't matter in practice.
  506. if (lzma_index_stream_flags(coder->this_index,
  507. &coder->footer_flags) != LZMA_OK)
  508. return LZMA_PROG_ERROR;
  509. // Store also the size of the Stream Padding field. It is
  510. // needed to calculate the offsets of the Streams correctly.
  511. if (lzma_index_stream_padding(coder->this_index,
  512. coder->stream_padding) != LZMA_OK)
  513. return LZMA_PROG_ERROR;
  514. // Reset it so that it's ready for the next Stream.
  515. coder->stream_padding = 0;
  516. // Append the earlier decoded Indexes after this_index.
  517. if (coder->combined_index != NULL)
  518. return_if_error(lzma_index_cat(coder->this_index,
  519. coder->combined_index, allocator));
  520. coder->combined_index = coder->this_index;
  521. coder->this_index = NULL;
  522. // If the whole file was decoded, tell the caller that we
  523. // are finished.
  524. if (coder->file_target_pos == 0) {
  525. // The combined index must indicate the same file
  526. // size as was told to us at initialization.
  527. assert(lzma_index_file_size(coder->combined_index)
  528. == coder->file_size);
  529. // Make the combined index available to
  530. // the application.
  531. *coder->dest_index = coder->combined_index;
  532. coder->combined_index = NULL;
  533. // Mark the input buffer as used since we may have
  534. // done internal seeking and thus don't know how
  535. // many input bytes were actually used. This way
  536. // lzma_stream.total_in gets a slightly better
  537. // estimate of the amount of input used.
  538. *in_pos = in_size;
  539. return LZMA_STREAM_END;
  540. }
  541. // We didn't hit the beginning of the file yet, so continue
  542. // reading backwards in the file. If we have unprocessed
  543. // data in coder->temp, use it before requesting more data
  544. // from the application.
  545. //
  546. // coder->file_target_pos, coder->temp_size, and
  547. // coder->temp_pos all point to the beginning of Stream Header
  548. // and thus the end of the previous Stream in the file.
  549. coder->sequence = coder->temp_size > 0
  550. ? SEQ_PADDING_DECODE : SEQ_PADDING_SEEK;
  551. break;
  552. default:
  553. assert(0);
  554. return LZMA_PROG_ERROR;
  555. }
  556. }
  557. static lzma_ret
  558. file_info_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
  559. uint64_t *old_memlimit, uint64_t new_memlimit)
  560. {
  561. lzma_file_info_coder *coder = coder_ptr;
  562. // The memory usage calculation comes from three things:
  563. //
  564. // (1) The Indexes that have already been decoded and processed into
  565. // coder->combined_index.
  566. //
  567. // (2) The latest Index in coder->this_index that has been decoded but
  568. // not yet put into coder->combined_index.
  569. //
  570. // (3) The latest Index that we have started decoding but haven't
  571. // finished and thus isn't available in coder->this_index yet.
  572. // Memory usage and limit information needs to be communicated
  573. // from/to coder->index_decoder.
  574. //
  575. // Care has to be taken to not do both (2) and (3) when calculating
  576. // the memory usage.
  577. uint64_t combined_index_memusage = 0;
  578. uint64_t this_index_memusage = 0;
  579. // (1) If we have already successfully decoded one or more Indexes,
  580. // get their memory usage.
  581. if (coder->combined_index != NULL)
  582. combined_index_memusage = lzma_index_memused(
  583. coder->combined_index);
  584. // Choose between (2), (3), or neither.
  585. if (coder->this_index != NULL) {
  586. // (2) The latest Index is available. Use its memory usage.
  587. this_index_memusage = lzma_index_memused(coder->this_index);
  588. } else if (coder->sequence == SEQ_INDEX_DECODE) {
  589. // (3) The Index decoder is activate and hasn't yet stored
  590. // the new index in coder->this_index. Get the memory usage
  591. // information from the Index decoder.
  592. //
  593. // NOTE: If the Index decoder doesn't yet know how much memory
  594. // it will eventually need, it will return a tiny value here.
  595. uint64_t dummy;
  596. if (coder->index_decoder.memconfig(coder->index_decoder.coder,
  597. &this_index_memusage, &dummy, 0)
  598. != LZMA_OK) {
  599. assert(0);
  600. return LZMA_PROG_ERROR;
  601. }
  602. }
  603. // Now we know the total memory usage/requirement. If we had neither
  604. // old Indexes nor a new Index, this will be zero which isn't
  605. // acceptable as lzma_memusage() has to return non-zero on success
  606. // and even with an empty .xz file we will end up with a lzma_index
  607. // that takes some memory.
  608. *memusage = combined_index_memusage + this_index_memusage;
  609. if (*memusage == 0)
  610. *memusage = lzma_index_memusage(1, 0);
  611. *old_memlimit = coder->memlimit;
  612. // If requested, set a new memory usage limit.
  613. if (new_memlimit != 0) {
  614. if (new_memlimit < *memusage)
  615. return LZMA_MEMLIMIT_ERROR;
  616. // In the condition (3) we need to tell the Index decoder
  617. // its new memory usage limit.
  618. if (coder->this_index == NULL
  619. && coder->sequence == SEQ_INDEX_DECODE) {
  620. const uint64_t idec_new_memlimit = new_memlimit
  621. - combined_index_memusage;
  622. assert(this_index_memusage > 0);
  623. assert(idec_new_memlimit > 0);
  624. uint64_t dummy1;
  625. uint64_t dummy2;
  626. if (coder->index_decoder.memconfig(
  627. coder->index_decoder.coder,
  628. &dummy1, &dummy2, idec_new_memlimit)
  629. != LZMA_OK) {
  630. assert(0);
  631. return LZMA_PROG_ERROR;
  632. }
  633. }
  634. coder->memlimit = new_memlimit;
  635. }
  636. return LZMA_OK;
  637. }
  638. static void
  639. file_info_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  640. {
  641. lzma_file_info_coder *coder = coder_ptr;
  642. lzma_next_end(&coder->index_decoder, allocator);
  643. lzma_index_end(coder->this_index, allocator);
  644. lzma_index_end(coder->combined_index, allocator);
  645. lzma_free(coder, allocator);
  646. return;
  647. }
  648. static lzma_ret
  649. lzma_file_info_decoder_init(lzma_next_coder *next,
  650. const lzma_allocator *allocator, uint64_t *seek_pos,
  651. lzma_index **dest_index,
  652. uint64_t memlimit, uint64_t file_size)
  653. {
  654. lzma_next_coder_init(&lzma_file_info_decoder_init, next, allocator);
  655. if (dest_index == NULL)
  656. return LZMA_PROG_ERROR;
  657. lzma_file_info_coder *coder = next->coder;
  658. if (coder == NULL) {
  659. coder = lzma_alloc(sizeof(lzma_file_info_coder), allocator);
  660. if (coder == NULL)
  661. return LZMA_MEM_ERROR;
  662. next->coder = coder;
  663. next->code = &file_info_decode;
  664. next->end = &file_info_decoder_end;
  665. next->memconfig = &file_info_decoder_memconfig;
  666. coder->index_decoder = LZMA_NEXT_CODER_INIT;
  667. coder->this_index = NULL;
  668. coder->combined_index = NULL;
  669. }
  670. coder->sequence = SEQ_MAGIC_BYTES;
  671. coder->file_cur_pos = 0;
  672. coder->file_target_pos = 0;
  673. coder->file_size = file_size;
  674. lzma_index_end(coder->this_index, allocator);
  675. coder->this_index = NULL;
  676. lzma_index_end(coder->combined_index, allocator);
  677. coder->combined_index = NULL;
  678. coder->stream_padding = 0;
  679. coder->dest_index = dest_index;
  680. coder->external_seek_pos = seek_pos;
  681. // If memlimit is 0, make it 1 to ensure that lzma_memlimit_get()
  682. // won't return 0 (which would indicate an error).
  683. coder->memlimit = my_max(1, memlimit);
  684. // Prepare these for reading the first Stream Header into coder->temp.
  685. coder->temp_pos = 0;
  686. coder->temp_size = LZMA_STREAM_HEADER_SIZE;
  687. return LZMA_OK;
  688. }
  689. extern LZMA_API(lzma_ret)
  690. lzma_file_info_decoder(lzma_stream *strm, lzma_index **dest_index,
  691. uint64_t memlimit, uint64_t file_size)
  692. {
  693. lzma_next_strm_init(lzma_file_info_decoder_init, strm, &strm->seek_pos,
  694. dest_index, memlimit, file_size);
  695. // We allow LZMA_FINISH in addition to LZMA_RUN for convenience.
  696. // lzma_code() is able to handle the LZMA_FINISH + LZMA_SEEK_NEEDED
  697. // combination in a sane way. Applications still need to be careful
  698. // if they use LZMA_FINISH so that they remember to reset it back
  699. // to LZMA_RUN after seeking if needed.
  700. strm->internal->supported_actions[LZMA_RUN] = true;
  701. strm->internal->supported_actions[LZMA_FINISH] = true;
  702. return LZMA_OK;
  703. }