From d4b0e64825c1863a1fc621267c9a56d41fd5d89f Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Fri, 19 May 2023 14:13:13 +0200 Subject: [PATCH] LibCompress: Move two shared LZMA magic numbers into a common place --- Userland/Libraries/LibCompress/Lzma.cpp | 12 +----------- Userland/Libraries/LibCompress/Lzma.h | 6 ++++++ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibCompress/Lzma.cpp b/Userland/Libraries/LibCompress/Lzma.cpp index 97a65e595a7..4c94e070ffd 100644 --- a/Userland/Libraries/LibCompress/Lzma.cpp +++ b/Userland/Libraries/LibCompress/Lzma.cpp @@ -231,10 +231,7 @@ ErrorOr LzmaDecompressor::append_input_stream(MaybeOwned stream, O ErrorOr LzmaDecompressor::normalize_range_decoder() { - // "The value of the "Range" variable before each bit decoding can not be smaller - // than ((UInt32)1 << 24). The Normalize() function keeps the "Range" value in - // described range." - constexpr u32 minimum_range_value = 1 << 24; + // "The Normalize() function keeps the "Range" value in described range." if (m_range_decoder_range >= minimum_range_value) return {}; @@ -290,8 +287,6 @@ ErrorOr LzmaCompressor::normalize_range_encoder() // Logically, we should only ever build up an overflow that is smaller than or equal to 0x01. VERIFY((maximum_range_value >> 32) <= 0x01); - constexpr u32 minimum_range_value = 1 << 24; - if (m_range_encoder_range >= minimum_range_value) return {}; @@ -341,9 +336,6 @@ ErrorOr LzmaDecompressor::decode_bit_with_probability(Probability& probabili // information about estimated probability for symbol 0 and the Range Decoder // updates that CProb variable after decoding." - // The significance of the shift width is not explained and appears to be a magic constant. - constexpr size_t probability_shift_width = 5; - u32 bound = (m_range_decoder_range >> probability_bit_count) * probability; dbgln_if(LZMA_DEBUG, "Decoding bit {} with probability = {:#x}, bound = {:#x}, code = {:#x}, range = {:#x}", m_range_decoder_code < bound ? 0 : 1, probability, bound, m_range_decoder_code, m_range_decoder_range); @@ -364,8 +356,6 @@ ErrorOr LzmaDecompressor::decode_bit_with_probability(Probability& probabili ErrorOr LzmaCompressor::encode_bit_with_probability(Probability& probability, u8 value) { - constexpr size_t probability_shift_width = 5; - u32 bound = (m_range_encoder_range >> probability_bit_count) * probability; dbgln_if(LZMA_DEBUG, "Encoding bit {} with probability = {:#x}, bound = {:#x}, code = {:#x}, range = {:#x}", value, probability, bound, m_range_encoder_code, m_range_encoder_range); diff --git a/Userland/Libraries/LibCompress/Lzma.h b/Userland/Libraries/LibCompress/Lzma.h index e9b811b179e..3a1886cebb9 100644 --- a/Userland/Libraries/LibCompress/Lzma.h +++ b/Userland/Libraries/LibCompress/Lzma.h @@ -70,6 +70,12 @@ protected: static constexpr Probability default_probability = (1 << probability_bit_count) / 2; static void initialize_to_default_probability(Span); + // The significance of the shift width is not explained and appears to be a magic constant. + static constexpr size_t probability_shift_width = 5; + + // "The value of the "Range" variable before each bit decoding can not be smaller than ((UInt32)1 << 24)." + static constexpr u32 minimum_range_value = 1 << 24; + LzmaState(FixedArray literal_probabilities); u64 m_total_processed_bytes { 0 };