Lzma.cpp 26 KB

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