Xz.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/MemoryStream.h>
  8. #include <LibCompress/Lzma2.h>
  9. #include <LibCompress/Xz.h>
  10. #include <LibCrypto/Checksum/CRC32.h>
  11. namespace Compress {
  12. ErrorOr<XzMultibyteInteger> XzMultibyteInteger::read_from_stream(Stream& stream)
  13. {
  14. // 1.2. Multibyte Integers:
  15. // "When smaller values are more likely than bigger values (for
  16. // example file sizes), multibyte integers are encoded in a
  17. // variable-length representation:
  18. // - Numbers in the range [0, 127] are copied as is, and take
  19. // one byte of space.
  20. // - Bigger numbers will occupy two or more bytes. All but the
  21. // last byte of the multibyte representation have the highest
  22. // (eighth) bit set."
  23. // 9 * 7 bits is 63 bits, which is the largest that will fit into an u64.
  24. constexpr size_t maximum_number_of_bytes = 9;
  25. u64 result = 0;
  26. for (size_t i = 0; i < maximum_number_of_bytes; i++) {
  27. u64 const next_byte = TRY(stream.read_value<u8>());
  28. result |= (next_byte & 0x7F) << (i * 7);
  29. // We should reject numbers that are encoded in too many bytes.
  30. if (next_byte == 0x00 && i != 0)
  31. return Error::from_string_literal("XZ multibyte integer has a larger encoding than necessary");
  32. if ((next_byte & 0x80) == 0)
  33. break;
  34. }
  35. return XzMultibyteInteger { result };
  36. }
  37. ErrorOr<void> XzStreamHeader::validate() const
  38. {
  39. // 2.1.1.1. Header Magic Bytes:
  40. // "The first six (6) bytes of the Stream are so called Header
  41. // Magic Bytes. They can be used to identify the file type.
  42. //
  43. // Using a C array and ASCII:
  44. // const uint8_t HEADER_MAGIC[6]
  45. // = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
  46. //
  47. // In plain hexadecimal:
  48. // FD 37 7A 58 5A 00
  49. //
  50. // If the Header Magic Bytes don't match, the decoder MUST
  51. // indicate an error."
  52. if (magic[0] != 0xFD || magic[1] != '7' || magic[2] != 'z' || magic[3] != 'X' || magic[4] != 'Z' || magic[5] != 0x00)
  53. return Error::from_string_literal("XZ stream header has an invalid magic");
  54. // 2.1.1.2. Stream Flags:
  55. // "If any reserved bit is set, the decoder MUST indicate an error.
  56. // It is possible that there is a new field present which the
  57. // decoder is not aware of, and can thus parse the Stream Header
  58. // incorrectly."
  59. if (flags.reserved != 0 || flags.reserved_bits != 0)
  60. return Error::from_string_literal("XZ stream header has reserved non-null stream flag bits");
  61. // 2.1.1.3. CRC32:
  62. // "The CRC32 is calculated from the Stream Flags field. It is
  63. // stored as an unsigned 32-bit little endian integer. If the
  64. // calculated value does not match the stored one, the decoder
  65. // MUST indicate an error."
  66. if (Crypto::Checksum::CRC32({ &flags, sizeof(flags) }).digest() != flags_crc32)
  67. return Error::from_string_literal("XZ stream header has an invalid CRC32 checksum");
  68. return {};
  69. }
  70. ErrorOr<void> XzStreamFooter::validate() const
  71. {
  72. // 2.1.2.1. CRC32:
  73. // "The CRC32 is calculated from the Backward Size and Stream Flags
  74. // fields. It is stored as an unsigned 32-bit little endian
  75. // integer. If the calculated value does not match the stored one,
  76. // the decoder MUST indicate an error."
  77. Crypto::Checksum::CRC32 calculated_crc32;
  78. calculated_crc32.update({ &encoded_backward_size, sizeof(encoded_backward_size) });
  79. calculated_crc32.update({ &flags, sizeof(flags) });
  80. if (calculated_crc32.digest() != size_and_flags_crc32)
  81. return Error::from_string_literal("XZ stream footer has an invalid CRC32 checksum");
  82. // 2.1.2.4. Footer Magic Bytes:
  83. // "As the last step of the decoding process, the decoder MUST
  84. // verify the existence of Footer Magic Bytes. If they don't
  85. // match, an error MUST be indicated.
  86. //
  87. // Using a C array and ASCII:
  88. // const uint8_t FOOTER_MAGIC[2] = { 'Y', 'Z' };
  89. //
  90. // In hexadecimal:
  91. // 59 5A"
  92. if (magic[0] != 'Y' || magic[1] != 'Z')
  93. return Error::from_string_literal("XZ stream footer has an invalid magic");
  94. return {};
  95. }
  96. u32 XzStreamFooter::backward_size() const
  97. {
  98. // 2.1.2.2. Backward Size:
  99. // "Backward Size is stored as a 32-bit little endian integer,
  100. // which indicates the size of the Index field as multiple of
  101. // four bytes, minimum value being four bytes:
  102. //
  103. // real_backward_size = (stored_backward_size + 1) * 4;"
  104. return (encoded_backward_size + 1) * 4;
  105. }
  106. u8 XzBlockFlags::number_of_filters() const
  107. {
  108. // 3.1.2. Block Flags:
  109. // "Bit(s) Mask Description
  110. // 0-1 0x03 Number of filters (1-4)"
  111. return encoded_number_of_filters + 1;
  112. }
  113. ErrorOr<void> XzFilterLzma2Properties::validate() const
  114. {
  115. // 5.3.1. LZMA2:
  116. // "Bits Mask Description
  117. // 6-7 0xC0 Reserved for future use; MUST be zero for now."
  118. if (reserved != 0)
  119. return Error::from_string_literal("XZ LZMA2 filter properties contains non-null reserved bits");
  120. // " const uint8_t bits = get_dictionary_flags() & 0x3F;
  121. // if (bits > 40)
  122. // return DICTIONARY_TOO_BIG; // Bigger than 4 GiB"
  123. if (encoded_dictionary_size > 40)
  124. return Error::from_string_literal("XZ LZMA2 filter properties contains larger-than-allowed dictionary size");
  125. return {};
  126. }
  127. u32 XzFilterLzma2Properties::dictionary_size() const
  128. {
  129. // "Dictionary Size is encoded with one-bit mantissa and five-bit
  130. // exponent. The smallest dictionary size is 4 KiB and the biggest
  131. // is 4 GiB.
  132. // Instead of having a table in the decoder, the dictionary size
  133. // can be decoded using the following C code:"
  134. if (encoded_dictionary_size == 40)
  135. return NumericLimits<u32>::max();
  136. u32 dictionary_size = 2 | (encoded_dictionary_size & 1);
  137. dictionary_size <<= encoded_dictionary_size / 2 + 11;
  138. return dictionary_size;
  139. }
  140. ErrorOr<NonnullOwnPtr<XzDecompressor>> XzDecompressor::create(MaybeOwned<Stream> stream)
  141. {
  142. auto counting_stream = TRY(try_make<CountingStream>(move(stream)));
  143. auto decompressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) XzDecompressor(move(counting_stream))));
  144. return decompressor;
  145. }
  146. XzDecompressor::XzDecompressor(NonnullOwnPtr<CountingStream> stream)
  147. : m_stream(move(stream))
  148. {
  149. }
  150. static Optional<size_t> size_for_check_type(XzStreamCheckType check_type)
  151. {
  152. switch (check_type) {
  153. case XzStreamCheckType::None:
  154. return 0;
  155. case XzStreamCheckType::CRC32:
  156. return 4;
  157. case XzStreamCheckType::CRC64:
  158. return 8;
  159. case XzStreamCheckType::SHA256:
  160. return 32;
  161. default:
  162. return {};
  163. }
  164. }
  165. ErrorOr<bool> XzDecompressor::load_next_stream()
  166. {
  167. // If we already determined to have found the last stream footer, there is nothing more to do.
  168. if (m_found_last_stream_footer)
  169. return false;
  170. // This assumes that we can just read the Stream Header into memory as-is. Check that this still holds up for good measure.
  171. static_assert(AK::Traits<XzStreamHeader>::is_trivially_serializable());
  172. XzStreamHeader stream_header {};
  173. Bytes stream_header_bytes { &stream_header, sizeof(stream_header) };
  174. if (m_found_first_stream_header) {
  175. // 2.2. Stream Padding:
  176. // "Stream Padding MUST contain only null bytes. To preserve the
  177. // four-byte alignment of consecutive Streams, the size of Stream
  178. // Padding MUST be a multiple of four bytes. Empty Stream Padding
  179. // is allowed. If these requirements are not met, the decoder MUST
  180. // indicate an error."
  181. VERIFY(m_stream->read_bytes() % 4 == 0);
  182. while (true) {
  183. // Read the first byte until we either get a non-null byte or reach EOF.
  184. auto byte_or_error = m_stream->read_value<u8>();
  185. if (byte_or_error.is_error() && m_stream->is_eof())
  186. break;
  187. auto byte = TRY(byte_or_error);
  188. if (byte != 0) {
  189. stream_header_bytes[0] = byte;
  190. stream_header_bytes = stream_header_bytes.slice(1);
  191. break;
  192. }
  193. }
  194. // If we aren't at EOF we already read the potential first byte of the header, so we need to subtract that.
  195. auto end_of_padding_offset = m_stream->read_bytes();
  196. if (!m_stream->is_eof())
  197. end_of_padding_offset -= 1;
  198. if (end_of_padding_offset % 4 != 0)
  199. return Error::from_string_literal("XZ Stream Padding is not aligned to 4 bytes");
  200. if (m_stream->is_eof()) {
  201. m_found_last_stream_footer = true;
  202. return false;
  203. }
  204. }
  205. TRY(m_stream->read_until_filled(stream_header_bytes));
  206. TRY(stream_header.validate());
  207. m_stream_flags = stream_header.flags;
  208. m_found_first_stream_header = true;
  209. return true;
  210. }
  211. ErrorOr<void> XzDecompressor::load_next_block(u8 encoded_block_header_size)
  212. {
  213. // We already read the encoded Block Header size (one byte) to determine that this is not an Index.
  214. m_current_block_start_offset = m_stream->read_bytes() - 1;
  215. // Ensure that the start of the block is aligned to a multiple of four (in theory, everything in XZ is).
  216. VERIFY(m_current_block_start_offset % 4 == 0);
  217. // 3.1.1. Block Header Size:
  218. // "This field contains the size of the Block Header field,
  219. // including the Block Header Size field itself. Valid values are
  220. // in the range [0x01, 0xFF], which indicate the size of the Block
  221. // Header as multiples of four bytes, minimum size being eight
  222. // bytes:
  223. //
  224. // real_header_size = (encoded_header_size + 1) * 4;"
  225. u64 const block_header_size = (encoded_block_header_size + 1) * 4;
  226. // Read the whole header into a buffer to allow calculating the CRC32 later (3.1.7. CRC32).
  227. auto header = TRY(ByteBuffer::create_uninitialized(block_header_size));
  228. header[0] = encoded_block_header_size;
  229. TRY(m_stream->read_until_filled(header.span().slice(1)));
  230. FixedMemoryStream header_stream { header.span().slice(1) };
  231. // 3.1.2. Block Flags:
  232. // "If any reserved bit is set, the decoder MUST indicate an error.
  233. // It is possible that there is a new field present which the
  234. // decoder is not aware of, and can thus parse the Block Header
  235. // incorrectly."
  236. auto const flags = TRY(header_stream.read_value<XzBlockFlags>());
  237. if (flags.reserved != 0)
  238. return Error::from_string_literal("XZ block header has reserved non-null block flag bits");
  239. MaybeOwned<Stream> new_block_stream { *m_stream };
  240. // 3.1.3. Compressed Size:
  241. // "This field is present only if the appropriate bit is set in
  242. // the Block Flags field (see Section 3.1.2)."
  243. if (flags.compressed_size_present) {
  244. // "Compressed Size is stored using the encoding described in Section 1.2."
  245. u64 const compressed_size = TRY(header_stream.read_value<XzMultibyteInteger>());
  246. // "The Compressed Size field contains the size of the Compressed
  247. // Data field, which MUST be non-zero."
  248. if (compressed_size == 0)
  249. return Error::from_string_literal("XZ block header contains a compressed size of zero");
  250. new_block_stream = TRY(try_make<ConstrainedStream>(move(new_block_stream), compressed_size));
  251. }
  252. // 3.1.4. Uncompressed Size:
  253. // "This field is present only if the appropriate bit is set in
  254. // the Block Flags field (see Section 3.1.2)."
  255. if (flags.uncompressed_size_present) {
  256. // "Uncompressed Size is stored using the encoding described in Section 1.2."
  257. u64 const uncompressed_size = TRY(header_stream.read_value<XzMultibyteInteger>());
  258. m_current_block_expected_uncompressed_size = uncompressed_size;
  259. } else {
  260. m_current_block_expected_uncompressed_size.clear();
  261. }
  262. // 3.1.5. List of Filter Flags:
  263. // "The number of Filter Flags fields is stored in the Block Flags
  264. // field (see Section 3.1.2)."
  265. for (size_t i = 0; i < flags.number_of_filters(); i++) {
  266. // "The format of each Filter Flags field is as follows:
  267. // Both Filter ID and Size of Properties are stored using the
  268. // encoding described in Section 1.2."
  269. u64 const filter_id = TRY(header_stream.read_value<XzMultibyteInteger>());
  270. u64 const size_of_properties = TRY(header_stream.read_value<XzMultibyteInteger>());
  271. // "Size of Properties indicates the size of the Filter Properties field as bytes."
  272. auto filter_properties = TRY(ByteBuffer::create_uninitialized(size_of_properties));
  273. TRY(header_stream.read_until_filled(filter_properties));
  274. // 5.3.1. LZMA2
  275. if (filter_id == 0x21) {
  276. if (size_of_properties < sizeof(XzFilterLzma2Properties))
  277. return Error::from_string_literal("XZ LZMA2 filter has a smaller-than-needed properties size");
  278. auto const* properties = reinterpret_cast<XzFilterLzma2Properties*>(filter_properties.data());
  279. TRY(properties->validate());
  280. new_block_stream = TRY(Lzma2Decompressor::create_from_raw_stream(move(new_block_stream), properties->dictionary_size()));
  281. continue;
  282. }
  283. return Error::from_string_literal("XZ block header contains unknown filter ID");
  284. }
  285. // 3.1.6. Header Padding:
  286. // "This field contains as many null byte as it is needed to make
  287. // the Block Header have the size specified in Block Header Size."
  288. constexpr size_t size_of_block_header_size = 1;
  289. constexpr size_t size_of_crc32 = 4;
  290. while (MUST(header_stream.tell()) < block_header_size - size_of_block_header_size - size_of_crc32) {
  291. auto const padding_byte = TRY(header_stream.read_value<u8>());
  292. // "If any of the bytes are not null bytes, the decoder MUST
  293. // indicate an error."
  294. if (padding_byte != 0)
  295. return Error::from_string_literal("XZ block header padding contains non-null bytes");
  296. }
  297. // 3.1.7. CRC32:
  298. // "The CRC32 is calculated over everything in the Block Header
  299. // field except the CRC32 field itself.
  300. Crypto::Checksum::CRC32 calculated_header_crc32 { header.span().trim(block_header_size - size_of_crc32) };
  301. // It is stored as an unsigned 32-bit little endian integer.
  302. u32 const stored_header_crc32 = TRY(header_stream.read_value<LittleEndian<u32>>());
  303. // If the calculated value does not match the stored one, the decoder MUST indicate
  304. // an error."
  305. if (calculated_header_crc32.digest() != stored_header_crc32)
  306. return Error::from_string_literal("Stored XZ block header CRC32 does not match the stored CRC32");
  307. m_current_block_stream = move(new_block_stream);
  308. m_current_block_uncompressed_size = 0;
  309. return {};
  310. }
  311. ErrorOr<void> XzDecompressor::finish_current_block()
  312. {
  313. auto unpadded_size = m_stream->read_bytes() - m_current_block_start_offset;
  314. // 3.3. Block Padding:
  315. // "Block Padding MUST contain 0-3 null bytes to make the size of
  316. // the Block a multiple of four bytes. This can be needed when
  317. // the size of Compressed Data is not a multiple of four."
  318. for (size_t i = 0; (unpadded_size + i) % 4 != 0; i++) {
  319. auto const padding_byte = TRY(m_stream->read_value<u8>());
  320. // "If any of the bytes in Block Padding are not null bytes, the decoder
  321. // MUST indicate an error."
  322. if (padding_byte != 0)
  323. return Error::from_string_literal("XZ block contains a non-null padding byte");
  324. }
  325. // 3.4. Check:
  326. // "The type and size of the Check field depends on which bits
  327. // are set in the Stream Flags field (see Section 2.1.1.2).
  328. //
  329. // The Check, when used, is calculated from the original
  330. // uncompressed data. If the calculated Check does not match the
  331. // stored one, the decoder MUST indicate an error. If the selected
  332. // type of Check is not supported by the decoder, it SHOULD
  333. // indicate a warning or error."
  334. auto const maybe_check_size = size_for_check_type(m_stream_flags->check_type);
  335. if (!maybe_check_size.has_value())
  336. return Error::from_string_literal("XZ stream has an unknown check type");
  337. // TODO: Block content checks are currently unimplemented as a whole, independent of the check type.
  338. // For now, we only make sure to remove the correct amount of bytes from the stream.
  339. TRY(m_stream->discard(*maybe_check_size));
  340. unpadded_size += *maybe_check_size;
  341. if (m_current_block_expected_uncompressed_size.has_value()) {
  342. if (*m_current_block_expected_uncompressed_size != m_current_block_uncompressed_size)
  343. return Error::from_string_literal("Uncompressed size of XZ block does not match the expected value");
  344. }
  345. TRY(m_processed_blocks.try_append({
  346. .uncompressed_size = m_current_block_uncompressed_size,
  347. .unpadded_size = unpadded_size,
  348. }));
  349. return {};
  350. }
  351. ErrorOr<void> XzDecompressor::finish_current_stream()
  352. {
  353. // We already read the Index Indicator (one byte) to determine that this is an Index.
  354. auto const start_of_current_block = m_stream->read_bytes() - 1;
  355. // 4.2. Number of Records:
  356. // "This field indicates how many Records there are in the List
  357. // of Records field, and thus how many Blocks there are in the
  358. // Stream. The value is stored using the encoding described in
  359. // Section 1.2."
  360. u64 const number_of_records = TRY(m_stream->read_value<XzMultibyteInteger>());
  361. if (m_processed_blocks.size() != number_of_records)
  362. return Error::from_string_literal("Number of Records in XZ Index does not match the number of processed Blocks");
  363. // 4.3. List of Records:
  364. // "List of Records consists of as many Records as indicated by the
  365. // Number of Records field:"
  366. for (u64 i = 0; i < number_of_records; i++) {
  367. // "Each Record contains information about one Block:
  368. //
  369. // +===============+===================+
  370. // | Unpadded Size | Uncompressed Size |
  371. // +===============+===================+"
  372. // 4.3.1. Unpadded Size:
  373. // "This field indicates the size of the Block excluding the Block
  374. // Padding field. That is, Unpadded Size is the size of the Block
  375. // Header, Compressed Data, and Check fields. Unpadded Size is
  376. // stored using the encoding described in Section 1.2."
  377. u64 const unpadded_size = TRY(m_stream->read_value<XzMultibyteInteger>());
  378. // "The value MUST never be zero; with the current structure of Blocks, the
  379. // actual minimum value for Unpadded Size is five."
  380. if (unpadded_size < 5)
  381. return Error::from_string_literal("XZ index contains a record with an unpadded size of less than five");
  382. // 4.3.2. Uncompressed Size:
  383. // "This field indicates the Uncompressed Size of the respective
  384. // Block as bytes. The value is stored using the encoding
  385. // described in Section 1.2."
  386. u64 const uncompressed_size = TRY(m_stream->read_value<XzMultibyteInteger>());
  387. // 4.3. List of Records:
  388. // "If the decoder has decoded all the Blocks of the Stream, it
  389. // MUST verify that the contents of the Records match the real
  390. // Unpadded Size and Uncompressed Size of the respective Blocks."
  391. if (m_processed_blocks[i].uncompressed_size != uncompressed_size)
  392. return Error::from_string_literal("Uncompressed size of XZ Block does not match the Index");
  393. if (m_processed_blocks[i].unpadded_size != unpadded_size)
  394. return Error::from_string_literal("Unpadded size of XZ Block does not match the Index");
  395. }
  396. // 4.4. Index Padding:
  397. // "This field MUST contain 0-3 null bytes to pad the Index to
  398. // a multiple of four bytes. If any of the bytes are not null
  399. // bytes, the decoder MUST indicate an error."
  400. while ((m_stream->read_bytes() - start_of_current_block) % 4 != 0) {
  401. auto padding_byte = TRY(m_stream->read_value<u8>());
  402. if (padding_byte != 0)
  403. return Error::from_string_literal("XZ index contains a non-null padding byte");
  404. }
  405. // 4.5. CRC32:
  406. // "The CRC32 is calculated over everything in the Index field
  407. // except the CRC32 field itself. The CRC32 is stored as an
  408. // unsigned 32-bit little endian integer."
  409. u32 const index_crc32 = TRY(m_stream->read_value<LittleEndian<u32>>());
  410. // "If the calculated value does not match the stored one, the decoder MUST indicate
  411. // an error."
  412. // TODO: Validation of the index CRC32 is currently unimplemented.
  413. (void)index_crc32;
  414. auto const size_of_index = m_stream->read_bytes() - start_of_current_block;
  415. // According to the specification of a stream (2.1. Stream), the index is the last element in a stream,
  416. // followed by the stream footer (2.1.2. Stream Footer).
  417. auto const stream_footer = TRY(m_stream->read_value<XzStreamFooter>());
  418. // This handles verifying the CRC32 (2.1.2.1. CRC32) and the magic bytes (2.1.2.4. Footer Magic Bytes).
  419. TRY(stream_footer.validate());
  420. // 2.1.2.2. Backward Size:
  421. // "If the stored value does not match the real size of the Index
  422. // field, the decoder MUST indicate an error."
  423. if (stream_footer.backward_size() != size_of_index)
  424. return Error::from_string_literal("XZ index size does not match the stored size in the stream footer");
  425. // 2.1.2.3. Stream Flags:
  426. // "This is a copy of the Stream Flags field from the Stream
  427. // Header. The information stored to Stream Flags is needed
  428. // when parsing the Stream backwards. The decoder MUST compare
  429. // the Stream Flags fields in both Stream Header and Stream
  430. // Footer, and indicate an error if they are not identical."
  431. if (ReadonlyBytes { &*m_stream_flags, sizeof(XzStreamFlags) } != ReadonlyBytes { &stream_footer.flags, sizeof(stream_footer.flags) })
  432. return Error::from_string_literal("XZ stream header flags don't match the stream footer");
  433. return {};
  434. }
  435. ErrorOr<Bytes> XzDecompressor::read_some(Bytes bytes)
  436. {
  437. if (!m_stream_flags.has_value()) {
  438. if (!TRY(load_next_stream()))
  439. return bytes.trim(0);
  440. }
  441. if (!m_current_block_stream.has_value() || (*m_current_block_stream)->is_eof()) {
  442. if (m_current_block_stream.has_value()) {
  443. // We have already processed a block, so we weed to clean up trailing data before the next block starts.
  444. TRY(finish_current_block());
  445. }
  446. // The first byte between Block Header (3.1.1. Block Header Size) and Index (4.1. Index Indicator) overlap.
  447. // Block header sizes have valid values in the range of [0x01, 0xFF], the only valid value for an Index Indicator is therefore 0x00.
  448. auto const encoded_block_header_size_or_index_indicator = TRY(m_stream->read_value<u8>());
  449. if (encoded_block_header_size_or_index_indicator == 0x00) {
  450. // This is an Index, which is the last element before the stream footer.
  451. TRY(finish_current_stream());
  452. // Another XZ Stream might follow, so we just unset the current information and continue on the next read.
  453. m_stream_flags.clear();
  454. m_processed_blocks.clear();
  455. return bytes.trim(0);
  456. }
  457. TRY(load_next_block(encoded_block_header_size_or_index_indicator));
  458. }
  459. auto result = TRY((*m_current_block_stream)->read_some(bytes));
  460. m_current_block_uncompressed_size += result.size();
  461. return result;
  462. }
  463. ErrorOr<size_t> XzDecompressor::write_some(ReadonlyBytes)
  464. {
  465. return Error::from_errno(EBADF);
  466. }
  467. bool XzDecompressor::is_eof() const
  468. {
  469. return m_found_last_stream_footer;
  470. }
  471. bool XzDecompressor::is_open() const
  472. {
  473. return true;
  474. }
  475. void XzDecompressor::close()
  476. {
  477. }
  478. }