Xz.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 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()
  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()
  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()
  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()
  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()
  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()
  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::finish_current_block()
  212. {
  213. auto unpadded_size = m_stream->read_bytes() - m_current_block_start_offset;
  214. // 3.3. Block Padding:
  215. // "Block Padding MUST contain 0-3 null bytes to make the size of
  216. // the Block a multiple of four bytes. This can be needed when
  217. // the size of Compressed Data is not a multiple of four."
  218. for (size_t i = 0; (unpadded_size + i) % 4 != 0; i++) {
  219. auto padding_byte = TRY(m_stream->read_value<u8>());
  220. // "If any of the bytes in Block Padding are not null bytes, the decoder
  221. // MUST indicate an error."
  222. if (padding_byte != 0)
  223. return Error::from_string_literal("XZ block contains a non-null padding byte");
  224. }
  225. // 3.4. Check:
  226. // "The type and size of the Check field depends on which bits
  227. // are set in the Stream Flags field (see Section 2.1.1.2).
  228. //
  229. // The Check, when used, is calculated from the original
  230. // uncompressed data. If the calculated Check does not match the
  231. // stored one, the decoder MUST indicate an error. If the selected
  232. // type of Check is not supported by the decoder, it SHOULD
  233. // indicate a warning or error."
  234. auto maybe_check_size = size_for_check_type(m_stream_flags->check_type);
  235. if (!maybe_check_size.has_value())
  236. return Error::from_string_literal("XZ stream has an unknown check type");
  237. // TODO: Block content checks are currently unimplemented as a whole, independent of the check type.
  238. // For now, we only make sure to remove the correct amount of bytes from the stream.
  239. TRY(m_stream->discard(*maybe_check_size));
  240. unpadded_size += *maybe_check_size;
  241. if (m_current_block_expected_uncompressed_size.has_value()) {
  242. if (*m_current_block_expected_uncompressed_size != m_current_block_uncompressed_size)
  243. return Error::from_string_literal("Uncompressed size of XZ block does not match the expected value");
  244. }
  245. TRY(m_processed_blocks.try_append({
  246. .uncompressed_size = m_current_block_uncompressed_size,
  247. .unpadded_size = unpadded_size,
  248. }));
  249. return {};
  250. }
  251. ErrorOr<void> XzDecompressor::finish_current_stream()
  252. {
  253. // We already read the Index Indicator (one byte) to determine that this is an Index.
  254. auto start_of_current_block = m_stream->read_bytes() - 1;
  255. // 4.2. Number of Records:
  256. // "This field indicates how many Records there are in the List
  257. // of Records field, and thus how many Blocks there are in the
  258. // Stream. The value is stored using the encoding described in
  259. // Section 1.2."
  260. u64 number_of_records = TRY(m_stream->read_value<XzMultibyteInteger>());
  261. if (m_processed_blocks.size() != number_of_records)
  262. return Error::from_string_literal("Number of Records in XZ Index does not match the number of processed Blocks");
  263. // 4.3. List of Records:
  264. // "List of Records consists of as many Records as indicated by the
  265. // Number of Records field:"
  266. for (u64 i = 0; i < number_of_records; i++) {
  267. // "Each Record contains information about one Block:
  268. //
  269. // +===============+===================+
  270. // | Unpadded Size | Uncompressed Size |
  271. // +===============+===================+"
  272. // 4.3.1. Unpadded Size:
  273. // "This field indicates the size of the Block excluding the Block
  274. // Padding field. That is, Unpadded Size is the size of the Block
  275. // Header, Compressed Data, and Check fields. Unpadded Size is
  276. // stored using the encoding described in Section 1.2."
  277. u64 unpadded_size = TRY(m_stream->read_value<XzMultibyteInteger>());
  278. // "The value MUST never be zero; with the current structure of Blocks, the
  279. // actual minimum value for Unpadded Size is five."
  280. if (unpadded_size < 5)
  281. return Error::from_string_literal("XZ index contains a record with an unpadded size of less than five");
  282. // 4.3.2. Uncompressed Size:
  283. // "This field indicates the Uncompressed Size of the respective
  284. // Block as bytes. The value is stored using the encoding
  285. // described in Section 1.2."
  286. u64 uncompressed_size = TRY(m_stream->read_value<XzMultibyteInteger>());
  287. // 4.3. List of Records:
  288. // "If the decoder has decoded all the Blocks of the Stream, it
  289. // MUST verify that the contents of the Records match the real
  290. // Unpadded Size and Uncompressed Size of the respective Blocks."
  291. if (m_processed_blocks[i].uncompressed_size != uncompressed_size)
  292. return Error::from_string_literal("Uncompressed size of XZ Block does not match the Index");
  293. if (m_processed_blocks[i].unpadded_size != unpadded_size)
  294. return Error::from_string_literal("Unpadded size of XZ Block does not match the Index");
  295. }
  296. // 4.4. Index Padding:
  297. // "This field MUST contain 0-3 null bytes to pad the Index to
  298. // a multiple of four bytes. If any of the bytes are not null
  299. // bytes, the decoder MUST indicate an error."
  300. while ((m_stream->read_bytes() - start_of_current_block) % 4 != 0) {
  301. auto padding_byte = TRY(m_stream->read_value<u8>());
  302. if (padding_byte != 0)
  303. return Error::from_string_literal("XZ index contains a non-null padding byte");
  304. }
  305. // 4.5. CRC32:
  306. // "The CRC32 is calculated over everything in the Index field
  307. // except the CRC32 field itself. The CRC32 is stored as an
  308. // unsigned 32-bit little endian integer."
  309. u32 index_crc32 = TRY(m_stream->read_value<LittleEndian<u32>>());
  310. // "If the calculated value does not match the stored one, the decoder MUST indicate
  311. // an error."
  312. // TODO: Validation of the index CRC32 is currently unimplemented.
  313. (void)index_crc32;
  314. auto size_of_index = m_stream->read_bytes() - start_of_current_block;
  315. // According to the specification of a stream (2.1. Stream), the index is the last element in a stream,
  316. // followed by the stream footer (2.1.2. Stream Footer).
  317. auto stream_footer = TRY(m_stream->read_value<XzStreamFooter>());
  318. // This handles verifying the CRC32 (2.1.2.1. CRC32) and the magic bytes (2.1.2.4. Footer Magic Bytes).
  319. TRY(stream_footer.validate());
  320. // 2.1.2.2. Backward Size:
  321. // "If the stored value does not match the real size of the Index
  322. // field, the decoder MUST indicate an error."
  323. if (stream_footer.backward_size() != size_of_index)
  324. return Error::from_string_literal("XZ index size does not match the stored size in the stream footer");
  325. // 2.1.2.3. Stream Flags:
  326. // "This is a copy of the Stream Flags field from the Stream
  327. // Header. The information stored to Stream Flags is needed
  328. // when parsing the Stream backwards. The decoder MUST compare
  329. // the Stream Flags fields in both Stream Header and Stream
  330. // Footer, and indicate an error if they are not identical."
  331. if (Bytes { &*m_stream_flags, sizeof(XzStreamFlags) } != Bytes { &stream_footer.flags, sizeof(stream_footer.flags) })
  332. return Error::from_string_literal("XZ stream header flags don't match the stream footer");
  333. return {};
  334. }
  335. ErrorOr<Bytes> XzDecompressor::read_some(Bytes bytes)
  336. {
  337. if (!m_stream_flags.has_value()) {
  338. if (!TRY(load_next_stream()))
  339. return bytes.trim(0);
  340. }
  341. if (!m_current_block_stream.has_value() || (*m_current_block_stream)->is_eof()) {
  342. if (m_current_block_stream.has_value()) {
  343. // We have already processed a block, so we weed to clean up trailing data before the next block starts.
  344. TRY(finish_current_block());
  345. }
  346. auto start_of_current_block = m_stream->read_bytes();
  347. // Ensure that the start of the block is aligned to a multiple of four (in theory, everything in XZ is).
  348. VERIFY(start_of_current_block % 4 == 0);
  349. // The first byte between Block Header (3.1.1. Block Header Size) and Index (4.1. Index Indicator) overlap.
  350. // Block header sizes have valid values in the range of [0x01, 0xFF], the only valid value for an Index Indicator is therefore 0x00.
  351. auto encoded_block_header_size_or_index_indicator = TRY(m_stream->read_value<u8>());
  352. if (encoded_block_header_size_or_index_indicator == 0x00) {
  353. // This is an Index, which is the last element before the stream footer.
  354. TRY(finish_current_stream());
  355. // Another XZ Stream might follow, so we just unset the current information and continue on the next read.
  356. m_stream_flags.clear();
  357. m_processed_blocks.clear();
  358. return bytes.trim(0);
  359. }
  360. m_current_block_start_offset = start_of_current_block;
  361. // 3.1.1. Block Header Size:
  362. // "This field contains the size of the Block Header field,
  363. // including the Block Header Size field itself. Valid values are
  364. // in the range [0x01, 0xFF], which indicate the size of the Block
  365. // Header as multiples of four bytes, minimum size being eight
  366. // bytes:
  367. //
  368. // real_header_size = (encoded_header_size + 1) * 4;"
  369. u64 block_header_size = (encoded_block_header_size_or_index_indicator + 1) * 4;
  370. // Read the whole header into a buffer to allow calculating the CRC32 later (3.1.7. CRC32).
  371. auto header = TRY(ByteBuffer::create_uninitialized(block_header_size));
  372. header[0] = encoded_block_header_size_or_index_indicator;
  373. TRY(m_stream->read_until_filled(header.span().slice(1)));
  374. FixedMemoryStream header_stream { header.span().slice(1) };
  375. // 3.1.2. Block Flags:
  376. // "If any reserved bit is set, the decoder MUST indicate an error.
  377. // It is possible that there is a new field present which the
  378. // decoder is not aware of, and can thus parse the Block Header
  379. // incorrectly."
  380. auto flags = TRY(header_stream.read_value<XzBlockFlags>());
  381. if (flags.reserved != 0)
  382. return Error::from_string_literal("XZ block header has reserved non-null block flag bits");
  383. MaybeOwned<Stream> new_block_stream { *m_stream };
  384. // 3.1.3. Compressed Size:
  385. // "This field is present only if the appropriate bit is set in
  386. // the Block Flags field (see Section 3.1.2)."
  387. if (flags.compressed_size_present) {
  388. // "Compressed Size is stored using the encoding described in Section 1.2."
  389. u64 compressed_size = TRY(header_stream.read_value<XzMultibyteInteger>());
  390. // "The Compressed Size field contains the size of the Compressed
  391. // Data field, which MUST be non-zero."
  392. if (compressed_size == 0)
  393. return Error::from_string_literal("XZ block header contains a compressed size of zero");
  394. new_block_stream = TRY(try_make<ConstrainedStream>(move(new_block_stream), compressed_size));
  395. }
  396. // 3.1.4. Uncompressed Size:
  397. // "This field is present only if the appropriate bit is set in
  398. // the Block Flags field (see Section 3.1.2)."
  399. if (flags.uncompressed_size_present) {
  400. // "Uncompressed Size is stored using the encoding described in Section 1.2."
  401. u64 uncompressed_size = TRY(header_stream.read_value<XzMultibyteInteger>());
  402. m_current_block_expected_uncompressed_size = uncompressed_size;
  403. } else {
  404. m_current_block_expected_uncompressed_size.clear();
  405. }
  406. // 3.1.5. List of Filter Flags:
  407. // "The number of Filter Flags fields is stored in the Block Flags
  408. // field (see Section 3.1.2)."
  409. for (size_t i = 0; i < flags.number_of_filters(); i++) {
  410. // "The format of each Filter Flags field is as follows:
  411. // Both Filter ID and Size of Properties are stored using the
  412. // encoding described in Section 1.2."
  413. u64 filter_id = TRY(header_stream.read_value<XzMultibyteInteger>());
  414. u64 size_of_properties = TRY(header_stream.read_value<XzMultibyteInteger>());
  415. // "Size of Properties indicates the size of the Filter Properties field as bytes."
  416. auto filter_properties = TRY(ByteBuffer::create_uninitialized(size_of_properties));
  417. TRY(header_stream.read_until_filled(filter_properties));
  418. // 5.3.1. LZMA2
  419. if (filter_id == 0x21) {
  420. if (size_of_properties < sizeof(XzFilterLzma2Properties))
  421. return Error::from_string_literal("XZ LZMA2 filter has a smaller-than-needed properties size");
  422. auto properties = reinterpret_cast<XzFilterLzma2Properties*>(filter_properties.data());
  423. TRY(properties->validate());
  424. new_block_stream = TRY(Lzma2Decompressor::create_from_raw_stream(move(new_block_stream), properties->dictionary_size()));
  425. continue;
  426. }
  427. return Error::from_string_literal("XZ block header contains unknown filter ID");
  428. }
  429. // 3.1.6. Header Padding:
  430. // "This field contains as many null byte as it is needed to make
  431. // the Block Header have the size specified in Block Header Size."
  432. constexpr size_t size_of_block_header_size = 1;
  433. constexpr size_t size_of_crc32 = 4;
  434. while (MUST(header_stream.tell()) < block_header_size - size_of_block_header_size - size_of_crc32) {
  435. auto padding_byte = TRY(header_stream.read_value<u8>());
  436. // "If any of the bytes are not null bytes, the decoder MUST
  437. // indicate an error."
  438. if (padding_byte != 0)
  439. return Error::from_string_literal("XZ block header padding contains non-null bytes");
  440. }
  441. // 3.1.7. CRC32:
  442. // "The CRC32 is calculated over everything in the Block Header
  443. // field except the CRC32 field itself.
  444. Crypto::Checksum::CRC32 calculated_header_crc32 { header.span().trim(block_header_size - size_of_crc32) };
  445. // It is stored as an unsigned 32-bit little endian integer.
  446. u32 stored_header_crc32 = TRY(header_stream.read_value<LittleEndian<u32>>());
  447. // If the calculated value does not match the stored one, the decoder MUST indicate
  448. // an error."
  449. if (calculated_header_crc32.digest() != stored_header_crc32)
  450. return Error::from_string_literal("Stored XZ block header CRC32 does not match the stored CRC32");
  451. m_current_block_stream = move(new_block_stream);
  452. m_current_block_uncompressed_size = 0;
  453. }
  454. auto result = TRY((*m_current_block_stream)->read_some(bytes));
  455. m_current_block_uncompressed_size += result.size();
  456. return result;
  457. }
  458. ErrorOr<size_t> XzDecompressor::write_some(ReadonlyBytes)
  459. {
  460. return Error::from_errno(EBADF);
  461. }
  462. bool XzDecompressor::is_eof() const
  463. {
  464. return m_found_last_stream_footer;
  465. }
  466. bool XzDecompressor::is_open() const
  467. {
  468. return true;
  469. }
  470. void XzDecompressor::close()
  471. {
  472. }
  473. }