Lzma.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCompress/Lzma.h>
  7. namespace Compress {
  8. u32 LzmaHeader::dictionary_size() const
  9. {
  10. // "If the value of dictionary size in properties is smaller than (1 << 12),
  11. // the LZMA decoder must set the dictionary size variable to (1 << 12)."
  12. constexpr u32 minimum_dictionary_size = (1 << 12);
  13. if (m_dictionary_size < minimum_dictionary_size)
  14. return minimum_dictionary_size;
  15. return m_dictionary_size;
  16. }
  17. Optional<u64> LzmaHeader::uncompressed_size() const
  18. {
  19. // We are making a copy of the packed field here because we would otherwise
  20. // pass an unaligned reference to the constructor of Optional, which is
  21. // undefined behavior.
  22. auto uncompressed_size = m_uncompressed_size;
  23. // "If "Uncompressed size" field contains ones in all 64 bits, it means that
  24. // uncompressed size is unknown and there is the "end marker" in stream,
  25. // that indicates the end of decoding point."
  26. if (uncompressed_size == UINT64_MAX)
  27. return {};
  28. // "In opposite case, if the value from "Uncompressed size" field is not
  29. // equal to ((2^64) - 1), the LZMA stream decoding must be finished after
  30. // specified number of bytes (Uncompressed size) is decoded. And if there
  31. // is the "end marker", the LZMA decoder must read that marker also."
  32. return uncompressed_size;
  33. }
  34. ErrorOr<void> LzmaHeader::decode_model_properties(u8& literal_context_bits, u8& literal_pos_bits, u8& pos_bits) const
  35. {
  36. // "Decodes the following values from the encoded model properties field:
  37. //
  38. // name Range Description
  39. // lc [0, 8] the number of "literal context" bits
  40. // lp [0, 4] the number of "literal pos" bits
  41. // pb [0, 4] the number of "pos" bits
  42. //
  43. // Encoded using `((pb * 5 + lp) * 9 + lc)`."
  44. u8 input_bits = m_model_properties;
  45. if (input_bits >= (9 * 5 * 5))
  46. return Error::from_string_literal("Encoded model properties value is larger than the highest possible value");
  47. literal_context_bits = input_bits % 9;
  48. input_bits /= 9;
  49. literal_pos_bits = input_bits % 5;
  50. input_bits /= 5;
  51. pos_bits = input_bits;
  52. return {};
  53. }
  54. ErrorOr<LzmaDecompressorOptions> LzmaHeader::as_decompressor_options() const
  55. {
  56. u8 literal_context_bits { 0 };
  57. u8 literal_position_bits { 0 };
  58. u8 position_bits { 0 };
  59. TRY(decode_model_properties(literal_context_bits, literal_position_bits, position_bits));
  60. return LzmaDecompressorOptions {
  61. .literal_context_bits = literal_context_bits,
  62. .literal_position_bits = literal_position_bits,
  63. .position_bits = position_bits,
  64. .dictionary_size = dictionary_size(),
  65. .uncompressed_size = uncompressed_size(),
  66. };
  67. }
  68. void LzmaDecompressor::initialize_to_default_probability(Span<Probability> span)
  69. {
  70. for (auto& entry : span)
  71. entry = default_probability;
  72. }
  73. ErrorOr<NonnullOwnPtr<LzmaDecompressor>> LzmaDecompressor::create_from_container(MaybeOwned<Stream> stream)
  74. {
  75. auto header = TRY(stream->read_value<LzmaHeader>());
  76. return TRY(LzmaDecompressor::create_from_raw_stream(move(stream), TRY(header.as_decompressor_options())));
  77. }
  78. ErrorOr<NonnullOwnPtr<LzmaDecompressor>> LzmaDecompressor::create_from_raw_stream(MaybeOwned<Stream> stream, LzmaDecompressorOptions const& options)
  79. {
  80. // "The LZMA Encoder always writes ZERO in initial byte of compressed stream.
  81. // That scheme allows to simplify the code of the Range Encoder in the
  82. // LZMA Encoder. If initial byte is not equal to ZERO, the LZMA Decoder must
  83. // stop decoding and report error."
  84. {
  85. auto byte = TRY(stream->read_value<u8>());
  86. if (byte != 0)
  87. return Error::from_string_literal("Initial byte of data stream is not zero");
  88. }
  89. auto output_buffer = TRY(CircularBuffer::create_empty(options.dictionary_size));
  90. // "The LZMA Decoder uses (1 << (lc + lp)) tables with CProb values, where each table contains 0x300 CProb values."
  91. auto literal_probabilities = TRY(FixedArray<Probability>::create(literal_probability_table_size * (1 << (options.literal_context_bits + options.literal_position_bits))));
  92. auto decompressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) LzmaDecompressor(move(stream), options, move(output_buffer), move(literal_probabilities))));
  93. // Read the initial bytes into the range decoder.
  94. for (size_t i = 0; i < 4; i++) {
  95. auto byte = TRY(decompressor->m_stream->read_value<u8>());
  96. decompressor->m_range_decoder_code = decompressor->m_range_decoder_code << 8 | byte;
  97. }
  98. return decompressor;
  99. }
  100. LzmaDecompressor::LzmaDecompressor(MaybeOwned<Stream> stream, LzmaDecompressorOptions options, CircularBuffer output_buffer, FixedArray<Probability> literal_probabilities)
  101. : m_stream(move(stream))
  102. , m_options(move(options))
  103. , m_output_buffer(move(output_buffer))
  104. , m_literal_probabilities(move(literal_probabilities))
  105. {
  106. initialize_to_default_probability(m_literal_probabilities.span());
  107. for (auto& array : m_length_to_position_states)
  108. initialize_to_default_probability(array);
  109. for (auto& array : m_binary_tree_distance_probabilities)
  110. initialize_to_default_probability(array);
  111. initialize_to_default_probability(m_alignment_bit_probabilities);
  112. initialize_to_default_probability(m_is_match_probabilities);
  113. initialize_to_default_probability(m_is_rep_probabilities);
  114. initialize_to_default_probability(m_is_rep_g0_probabilities);
  115. initialize_to_default_probability(m_is_rep_g1_probabilities);
  116. initialize_to_default_probability(m_is_rep_g2_probabilities);
  117. initialize_to_default_probability(m_is_rep0_long_probabilities);
  118. }
  119. ErrorOr<void> LzmaDecompressor::normalize_range_decoder()
  120. {
  121. // "The value of the "Range" variable before each bit decoding can not be smaller
  122. // than ((UInt32)1 << 24). The Normalize() function keeps the "Range" value in
  123. // described range."
  124. constexpr u32 minimum_range_value = 1 << 24;
  125. if (m_range_decoder_range >= minimum_range_value)
  126. return {};
  127. m_range_decoder_range <<= 8;
  128. m_range_decoder_code <<= 8;
  129. m_range_decoder_code |= TRY(m_stream->read_value<u8>());
  130. VERIFY(m_range_decoder_range >= minimum_range_value);
  131. return {};
  132. }
  133. ErrorOr<u8> LzmaDecompressor::decode_direct_bit()
  134. {
  135. m_range_decoder_range >>= 1;
  136. m_range_decoder_code -= m_range_decoder_range;
  137. u32 temp = 0 - (m_range_decoder_code >> 31);
  138. m_range_decoder_code += m_range_decoder_range & temp;
  139. if (m_range_decoder_code == m_range_decoder_range)
  140. return Error::from_string_literal("Reached an invalid state while decoding LZMA stream");
  141. TRY(normalize_range_decoder());
  142. return temp + 1;
  143. }
  144. ErrorOr<u8> LzmaDecompressor::decode_bit_with_probability(Probability& probability)
  145. {
  146. // "The LZMA decoder provides the pointer to CProb variable that contains
  147. // information about estimated probability for symbol 0 and the Range Decoder
  148. // updates that CProb variable after decoding."
  149. // The significance of the shift width is not explained and appears to be a magic constant.
  150. constexpr size_t probability_shift_width = 5;
  151. u32 bound = (m_range_decoder_range >> probability_bit_count) * probability;
  152. if (m_range_decoder_code < bound) {
  153. probability += ((1 << probability_bit_count) - probability) >> probability_shift_width;
  154. m_range_decoder_range = bound;
  155. TRY(normalize_range_decoder());
  156. return 0;
  157. } else {
  158. probability -= probability >> probability_shift_width;
  159. m_range_decoder_code -= bound;
  160. m_range_decoder_range -= bound;
  161. TRY(normalize_range_decoder());
  162. return 1;
  163. }
  164. }
  165. ErrorOr<u16> LzmaDecompressor::decode_symbol_using_bit_tree(size_t bit_count, Span<Probability> probability_tree)
  166. {
  167. VERIFY(bit_count <= sizeof(u16) * 8);
  168. VERIFY(probability_tree.size() >= 1ul << bit_count);
  169. // This has been modified from the reference implementation to unlink the result and the tree index,
  170. // which should allow for better readability.
  171. u16 result = 0;
  172. size_t tree_index = 1;
  173. for (size_t i = 0; i < bit_count; i++) {
  174. u16 next_bit = TRY(decode_bit_with_probability(probability_tree[tree_index]));
  175. result = (result << 1) | next_bit;
  176. tree_index = (tree_index << 1) | next_bit;
  177. }
  178. return result;
  179. }
  180. ErrorOr<u16> LzmaDecompressor::decode_symbol_using_reverse_bit_tree(size_t bit_count, Span<Probability> probability_tree)
  181. {
  182. VERIFY(bit_count <= sizeof(u16) * 8);
  183. VERIFY(probability_tree.size() >= 1ul << bit_count);
  184. u16 result = 0;
  185. size_t tree_index = 1;
  186. for (size_t i = 0; i < bit_count; i++) {
  187. u16 next_bit = TRY(decode_bit_with_probability(probability_tree[tree_index]));
  188. result |= next_bit << i;
  189. tree_index = (tree_index << 1) | next_bit;
  190. }
  191. return result;
  192. }
  193. ErrorOr<void> LzmaDecompressor::decode_literal_to_output_buffer()
  194. {
  195. u8 previous_byte = 0;
  196. if (m_total_decoded_bytes > 0) {
  197. auto read_bytes = MUST(m_output_buffer.read_with_seekback({ &previous_byte, sizeof(previous_byte) }, 1));
  198. VERIFY(read_bytes.size() == sizeof(previous_byte));
  199. }
  200. // "To select the table for decoding it uses the context that consists of
  201. // (lc) high bits from previous literal and (lp) low bits from value that
  202. // represents current position in outputStream."
  203. u16 literal_state_bits_from_position = m_total_decoded_bytes & ((1 << m_options.literal_position_bits) - 1);
  204. u16 literal_state_bits_from_output = previous_byte >> (8 - m_options.literal_context_bits);
  205. u16 literal_state = literal_state_bits_from_position << m_options.literal_context_bits | literal_state_bits_from_output;
  206. Span<Probability> selected_probability_table = m_literal_probabilities.span().slice(literal_probability_table_size * literal_state, literal_probability_table_size);
  207. // The result is defined as u16 here and initialized to 1, but we will cut off the top bits before queueing them into the output buffer.
  208. // The top bit is only used to track how much we have decoded already, and to select the correct probability table.
  209. u16 result = 1;
  210. // "If (State > 7), the Literal Decoder also uses "matchByte" that represents
  211. // the byte in OutputStream at position the is the DISTANCE bytes before
  212. // current position, where the DISTANCE is the distance in DISTANCE-LENGTH pair
  213. // of latest decoded match."
  214. // Note: The specification says `(State > 7)`, but the reference implementation does `(State >= 7)`, which is a mismatch.
  215. // Testing `(State > 7)` with actual test files yields errors, so the reference implementation appears to be the correct one.
  216. if (m_state >= 7) {
  217. u8 matched_byte = 0;
  218. auto read_bytes = TRY(m_output_buffer.read_with_seekback({ &matched_byte, sizeof(matched_byte) }, m_rep0 + 1));
  219. VERIFY(read_bytes.size() == sizeof(matched_byte));
  220. do {
  221. u8 match_bit = (matched_byte >> 7) & 1;
  222. matched_byte <<= 1;
  223. u8 decoded_bit = TRY(decode_bit_with_probability(selected_probability_table[((1 + match_bit) << 8) + result]));
  224. result = result << 1 | decoded_bit;
  225. if (match_bit != decoded_bit)
  226. break;
  227. } while (result < 0x100);
  228. }
  229. while (result < 0x100)
  230. result = (result << 1) | TRY(decode_bit_with_probability(selected_probability_table[result]));
  231. u8 actual_result = result - 0x100;
  232. size_t written_bytes = m_output_buffer.write({ &actual_result, sizeof(actual_result) });
  233. VERIFY(written_bytes == sizeof(actual_result));
  234. m_total_decoded_bytes += sizeof(actual_result);
  235. return {};
  236. }
  237. LzmaDecompressor::LzmaLengthDecoderState::LzmaLengthDecoderState()
  238. {
  239. for (auto& array : m_low_length_probabilities)
  240. initialize_to_default_probability(array);
  241. for (auto& array : m_medium_length_probabilities)
  242. initialize_to_default_probability(array);
  243. initialize_to_default_probability(m_high_length_probabilities);
  244. }
  245. ErrorOr<u16> LzmaDecompressor::decode_normalized_match_length(LzmaLengthDecoderState& length_decoder_state)
  246. {
  247. // "LZMA uses "posState" value as context to select the binary tree
  248. // from LowCoder and MidCoder binary tree arrays:"
  249. u16 position_state = m_total_decoded_bytes & ((1 << m_options.position_bits) - 1);
  250. // "The following scheme is used for the match length encoding:
  251. //
  252. // Binary encoding Binary Tree structure Zero-based match length
  253. // sequence (binary + decimal):
  254. //
  255. // 0 xxx LowCoder[posState] xxx
  256. if (TRY(decode_bit_with_probability(length_decoder_state.m_first_choice_probability)) == 0)
  257. return TRY(decode_symbol_using_bit_tree(3, length_decoder_state.m_low_length_probabilities[position_state].span()));
  258. // 1 0 yyy MidCoder[posState] yyy + 8
  259. if (TRY(decode_bit_with_probability(length_decoder_state.m_second_choice_probability)) == 0)
  260. return TRY(decode_symbol_using_bit_tree(3, length_decoder_state.m_medium_length_probabilities[position_state].span())) + 8;
  261. // 1 1 zzzzzzzz HighCoder zzzzzzzz + 16"
  262. return TRY(decode_symbol_using_bit_tree(8, length_decoder_state.m_high_length_probabilities.span())) + 16;
  263. }
  264. ErrorOr<u32> LzmaDecompressor::decode_normalized_match_distance(u16 normalized_match_length)
  265. {
  266. // "LZMA uses normalized match length (zero-based length)
  267. // to calculate the context state "lenState" do decode the distance value."
  268. u16 length_state = min(normalized_match_length, number_of_length_to_position_states - 1);
  269. // "At first stage the distance decoder decodes 6-bit "posSlot" value with bit
  270. // tree decoder from PosSlotDecoder array."
  271. u16 position_slot = TRY(decode_symbol_using_bit_tree(6, m_length_to_position_states[length_state].span()));
  272. // "The encoding scheme for distance value is shown in the following table:
  273. //
  274. // posSlot (decimal) /
  275. // zero-based distance (binary)
  276. // 0 0
  277. // 1 1
  278. // 2 10
  279. // 3 11
  280. //
  281. // 4 10 x
  282. // 5 11 x
  283. // 6 10 xx
  284. // 7 11 xx
  285. // 8 10 xxx
  286. // 9 11 xxx
  287. // 10 10 xxxx
  288. // 11 11 xxxx
  289. // 12 10 xxxxx
  290. // 13 11 xxxxx
  291. //
  292. // 14 10 yy zzzz
  293. // 15 11 yy zzzz
  294. // 16 10 yyy zzzz
  295. // 17 11 yyy zzzz
  296. // ...
  297. // 62 10 yyyyyyyyyyyyyyyyyyyyyyyyyy zzzz
  298. // 63 11 yyyyyyyyyyyyyyyyyyyyyyyyyy zzzz
  299. //
  300. // where
  301. // "x ... x" means the sequence of binary symbols encoded with binary tree and
  302. // "Reverse" scheme. It uses separated binary tree for each posSlot from 4 to 13.
  303. // "y" means direct bit encoded with range coder.
  304. // "zzzz" means the sequence of four binary symbols encoded with binary
  305. // tree with "Reverse" scheme, where one common binary tree "AlignDecoder"
  306. // is used for all posSlot values."
  307. // "If (posSlot < 4), the "dist" value is equal to posSlot value."
  308. if (position_slot < first_position_slot_with_binary_tree_bits)
  309. return position_slot;
  310. // From here on, the first bit of the distance is always set and the second bit is set if the last bit of the position slot is set.
  311. u32 distance_prefix = ((1 << 1) | ((position_slot & 1) << 0));
  312. // "If (posSlot >= 4), the decoder uses "posSlot" value to calculate the value of
  313. // the high bits of "dist" value and the number of the low bits.
  314. // If (4 <= posSlot < kEndPosModelIndex), the decoder uses bit tree decoders.
  315. // (one separated bit tree decoder per one posSlot value) and "Reverse" scheme."
  316. if (position_slot < first_position_slot_with_direct_encoded_bits) {
  317. size_t number_of_bits_to_decode = (position_slot / 2) - 1;
  318. auto& selected_probability_tree = m_binary_tree_distance_probabilities[position_slot - first_position_slot_with_binary_tree_bits];
  319. return (distance_prefix << number_of_bits_to_decode) | TRY(decode_symbol_using_reverse_bit_tree(number_of_bits_to_decode, selected_probability_tree));
  320. }
  321. // " if (posSlot >= kEndPosModelIndex), the middle bits are decoded as direct
  322. // bits from RangeDecoder and the low 4 bits are decoded with a bit tree
  323. // decoder "AlignDecoder" with "Reverse" scheme."
  324. size_t number_of_direct_bits_to_decode = ((position_slot - first_position_slot_with_direct_encoded_bits) / 2) + 2;
  325. for (size_t i = 0; i < number_of_direct_bits_to_decode; i++) {
  326. distance_prefix = (distance_prefix << 1) | TRY(decode_direct_bit());
  327. }
  328. return (distance_prefix << number_of_alignment_bits) | TRY(decode_symbol_using_reverse_bit_tree(number_of_alignment_bits, m_alignment_bit_probabilities));
  329. }
  330. ErrorOr<Bytes> LzmaDecompressor::read_some(Bytes bytes)
  331. {
  332. while (m_output_buffer.used_space() < bytes.size() && m_output_buffer.empty_space() != 0) {
  333. if (m_found_end_of_stream_marker) {
  334. if (m_options.uncompressed_size.has_value() && m_total_decoded_bytes < m_options.uncompressed_size.value())
  335. return Error::from_string_literal("Found end-of-stream marker earlier than expected");
  336. break;
  337. }
  338. if (m_options.uncompressed_size.has_value() && m_total_decoded_bytes >= m_options.uncompressed_size.value()) {
  339. // FIXME: This should validate that either EOF or the 'end of stream' marker follow immediately.
  340. // Both of those cases count as the 'end of stream' marker being found and should check for a clean decoder state.
  341. break;
  342. }
  343. // "The decoder calculates "state2" variable value to select exact variable from
  344. // "IsMatch" and "IsRep0Long" arrays."
  345. u16 position_state = m_total_decoded_bytes & ((1 << m_options.position_bits) - 1);
  346. u16 state2 = (m_state << maximum_number_of_position_bits) + position_state;
  347. auto update_state_after_literal = [&] {
  348. if (m_state < 4)
  349. m_state = 0;
  350. else if (m_state < 10)
  351. m_state -= 3;
  352. else
  353. m_state -= 6;
  354. };
  355. auto update_state_after_match = [&] {
  356. if (m_state < 7)
  357. m_state = 7;
  358. else
  359. m_state = 10;
  360. };
  361. auto update_state_after_rep = [&] {
  362. if (m_state < 7)
  363. m_state = 8;
  364. else
  365. m_state = 11;
  366. };
  367. auto update_state_after_short_rep = [&] {
  368. if (m_state < 7)
  369. m_state = 9;
  370. else
  371. m_state = 11;
  372. };
  373. auto copy_match_to_buffer = [&](u16 real_length) -> ErrorOr<void> {
  374. VERIFY(!m_leftover_match_length.has_value());
  375. if (m_options.uncompressed_size.has_value() && m_options.uncompressed_size.value() < m_total_decoded_bytes + real_length)
  376. return Error::from_string_literal("Tried to copy match beyond expected uncompressed file size");
  377. while (real_length > 0) {
  378. if (m_output_buffer.empty_space() == 0) {
  379. m_leftover_match_length = real_length;
  380. break;
  381. }
  382. u8 byte;
  383. auto read_bytes = TRY(m_output_buffer.read_with_seekback({ &byte, sizeof(byte) }, m_rep0 + 1));
  384. VERIFY(read_bytes.size() == sizeof(byte));
  385. auto written_bytes = m_output_buffer.write({ &byte, sizeof(byte) });
  386. VERIFY(written_bytes == sizeof(byte));
  387. m_total_decoded_bytes++;
  388. real_length--;
  389. }
  390. return {};
  391. };
  392. // If we have a leftover part of a repeating match, we should finish that first.
  393. if (m_leftover_match_length.has_value()) {
  394. TRY(copy_match_to_buffer(m_leftover_match_length.release_value()));
  395. continue;
  396. }
  397. // "The decoder uses the following code flow scheme to select exact
  398. // type of LITERAL or MATCH:
  399. //
  400. // IsMatch[state2] decode
  401. // 0 - the Literal"
  402. if (TRY(decode_bit_with_probability(m_is_match_probabilities[state2])) == 0) {
  403. // "At first the LZMA decoder must check that it doesn't exceed
  404. // specified uncompressed size."
  405. // This is already checked for at the beginning of the loop.
  406. // "Then it decodes literal value and puts it to sliding window."
  407. TRY(decode_literal_to_output_buffer());
  408. // "Then the decoder must update the "state" value."
  409. update_state_after_literal();
  410. continue;
  411. }
  412. // " 1 - the Match
  413. // IsRep[state] decode
  414. // 0 - Simple Match"
  415. if (TRY(decode_bit_with_probability(m_is_rep_probabilities[m_state])) == 0) {
  416. // "The distance history table is updated with the following scheme:"
  417. m_rep3 = m_rep2;
  418. m_rep2 = m_rep1;
  419. m_rep1 = m_rep0;
  420. // "The zero-based length is decoded with "LenDecoder"."
  421. u16 normalized_length = TRY(decode_normalized_match_length(m_length_decoder));
  422. // "The state is update with UpdateState_Match function."
  423. update_state_after_match();
  424. // "and the new "rep0" value is decoded with DecodeDistance."
  425. m_rep0 = TRY(decode_normalized_match_distance(normalized_length));
  426. // "If the value of "rep0" is equal to 0xFFFFFFFF, it means that we have
  427. // "End of stream" marker, so we can stop decoding and check finishing
  428. // condition in Range Decoder"
  429. if (m_rep0 == 0xFFFFFFFF) {
  430. // The range decoder condition is checked after breaking out of the loop.
  431. m_found_end_of_stream_marker = true;
  432. continue;
  433. }
  434. // "If uncompressed size is defined, LZMA decoder must check that it doesn't
  435. // exceed that specified uncompressed size."
  436. // This is being checked for in the common "copy to buffer" implementation.
  437. // "Also the decoder must check that "rep0" value is not larger than dictionary size
  438. // and is not larger than the number of already decoded bytes."
  439. if (m_rep0 > min(m_options.dictionary_size, m_total_decoded_bytes))
  440. return Error::from_string_literal("rep0 value is larger than the possible lookback size");
  441. // "Then the decoder must copy match bytes as described in
  442. // "The match symbols copying" section."
  443. TRY(copy_match_to_buffer(normalized_length + normalized_to_real_match_length_offset));
  444. continue;
  445. }
  446. // " 1 - Rep Match
  447. // IsRepG0[state] decode
  448. // 0 - the distance is rep0"
  449. if (TRY(decode_bit_with_probability(m_is_rep_g0_probabilities[m_state])) == 0) {
  450. // "LZMA doesn't update the distance history."
  451. // " IsRep0Long[state2] decode
  452. // 0 - Short Rep Match"
  453. if (TRY(decode_bit_with_probability(m_is_rep0_long_probabilities[state2])) == 0) {
  454. // "If the subtype is "Short Rep Match", the decoder updates the state, puts
  455. // the one byte from window to current position in window and goes to next
  456. // MATCH/LITERAL symbol."
  457. update_state_after_short_rep();
  458. u8 byte;
  459. auto read_bytes = TRY(m_output_buffer.read_with_seekback({ &byte, sizeof(byte) }, m_rep0 + 1));
  460. VERIFY(read_bytes.size() == sizeof(byte));
  461. auto written_bytes = m_output_buffer.write({ &byte, sizeof(byte) });
  462. VERIFY(written_bytes == sizeof(byte));
  463. m_total_decoded_bytes++;
  464. continue;
  465. }
  466. // " 1 - Rep Match 0"
  467. // Intentional fallthrough, we just need to make sure to not run the detection for other match types and to not switch around the distance history.
  468. } else {
  469. // " 1 -
  470. // IsRepG1[state] decode
  471. // 0 - Rep Match 1"
  472. if (TRY(decode_bit_with_probability(m_is_rep_g1_probabilities[m_state])) == 0) {
  473. u32 distance = m_rep1;
  474. m_rep1 = m_rep0;
  475. m_rep0 = distance;
  476. }
  477. // " 1 -
  478. // IsRepG2[state] decode
  479. // 0 - Rep Match 2"
  480. else if (TRY(decode_bit_with_probability(m_is_rep_g2_probabilities[m_state])) == 0) {
  481. u32 distance = m_rep2;
  482. m_rep2 = m_rep1;
  483. m_rep1 = m_rep0;
  484. m_rep0 = distance;
  485. }
  486. // " 1 - Rep Match 3"
  487. else {
  488. u32 distance = m_rep3;
  489. m_rep3 = m_rep2;
  490. m_rep2 = m_rep1;
  491. m_rep1 = m_rep0;
  492. m_rep0 = distance;
  493. }
  494. }
  495. // "In other cases (Rep Match 0/1/2/3), it decodes the zero-based
  496. // length of match with "RepLenDecoder" decoder."
  497. u16 normalized_length = TRY(decode_normalized_match_length(m_rep_length_decoder));
  498. // "Then it updates the state."
  499. update_state_after_rep();
  500. // "Then the decoder must copy match bytes as described in
  501. // "The Match symbols copying" section."
  502. TRY(copy_match_to_buffer(normalized_length + normalized_to_real_match_length_offset));
  503. }
  504. if (m_found_end_of_stream_marker) {
  505. if (m_range_decoder_code != 0)
  506. return Error::from_string_literal("LZMA stream ends in an unclean state");
  507. }
  508. return m_output_buffer.read(bytes);
  509. }
  510. ErrorOr<size_t> LzmaDecompressor::write_some(ReadonlyBytes)
  511. {
  512. return Error::from_errno(EBADF);
  513. }
  514. bool LzmaDecompressor::is_eof() const
  515. {
  516. if (m_output_buffer.used_space() > 0)
  517. return false;
  518. if (m_options.uncompressed_size.has_value())
  519. return m_total_decoded_bytes >= m_options.uncompressed_size.value();
  520. return m_found_end_of_stream_marker;
  521. }
  522. bool LzmaDecompressor::is_open() const
  523. {
  524. return true;
  525. }
  526. void LzmaDecompressor::close()
  527. {
  528. }
  529. }