diff options
author | Tim Schumacher <timschumi@gmx.de> | 2023-03-14 02:36:03 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-21 10:25:13 +0100 |
commit | 04f69de7f1c12c7ac8de46ba7a77c70bd7dcc178 (patch) | |
tree | 9b6d9b5bdc87df594ce811db43321f9088de5cc2 /Userland | |
parent | 52d9fc92f1d30e32a9213efc48124457da73ce6c (diff) | |
download | serenity-04f69de7f1c12c7ac8de46ba7a77c70bd7dcc178.zip |
LibCompress: Refactor LZMA model property decoding into a static helper
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibCompress/Lzma.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibCompress/Lzma.h | 12 |
2 files changed, 26 insertions, 19 deletions
diff --git a/Userland/Libraries/LibCompress/Lzma.cpp b/Userland/Libraries/LibCompress/Lzma.cpp index ccd3440f85..e933dbe286 100644 --- a/Userland/Libraries/LibCompress/Lzma.cpp +++ b/Userland/Libraries/LibCompress/Lzma.cpp @@ -39,7 +39,7 @@ Optional<u64> LzmaHeader::uncompressed_size() const return uncompressed_size; } -ErrorOr<void> LzmaHeader::decode_model_properties(u8& literal_context_bits, u8& literal_pos_bits, u8& pos_bits) const +ErrorOr<LzmaModelProperties> LzmaHeader::decode_model_properties(u8 input_bits) { // "Decodes the following values from the encoded model properties field: // @@ -50,34 +50,35 @@ ErrorOr<void> LzmaHeader::decode_model_properties(u8& literal_context_bits, u8& // // Encoded using `((pb * 5 + lp) * 9 + lc)`." - u8 input_bits = m_model_properties; - if (input_bits >= (9 * 5 * 5)) return Error::from_string_literal("Encoded model properties value is larger than the highest possible value"); - literal_context_bits = input_bits % 9; + u8 literal_context_bits = input_bits % 9; input_bits /= 9; + VERIFY(literal_context_bits >= 0 && literal_context_bits <= 8); - literal_pos_bits = input_bits % 5; + u8 literal_position_bits = input_bits % 5; input_bits /= 5; + VERIFY(literal_position_bits >= 0 && literal_position_bits <= 4); - pos_bits = input_bits; + u8 position_bits = input_bits; + VERIFY(position_bits >= 0 && position_bits <= 4); - return {}; + return LzmaModelProperties { + .literal_context_bits = literal_context_bits, + .literal_position_bits = literal_position_bits, + .position_bits = position_bits, + }; } ErrorOr<LzmaDecompressorOptions> LzmaHeader::as_decompressor_options() const { - u8 literal_context_bits { 0 }; - u8 literal_position_bits { 0 }; - u8 position_bits { 0 }; - - TRY(decode_model_properties(literal_context_bits, literal_position_bits, position_bits)); + auto model_properties = TRY(decode_model_properties(m_encoded_model_properties)); - return LzmaDecompressorOptions { - .literal_context_bits = literal_context_bits, - .literal_position_bits = literal_position_bits, - .position_bits = position_bits, + return Compress::LzmaDecompressorOptions { + .literal_context_bits = model_properties.literal_context_bits, + .literal_position_bits = model_properties.literal_position_bits, + .position_bits = model_properties.position_bits, .dictionary_size = dictionary_size(), .uncompressed_size = uncompressed_size(), }; diff --git a/Userland/Libraries/LibCompress/Lzma.h b/Userland/Libraries/LibCompress/Lzma.h index ff9b83afd9..347e07b705 100644 --- a/Userland/Libraries/LibCompress/Lzma.h +++ b/Userland/Libraries/LibCompress/Lzma.h @@ -17,6 +17,12 @@ namespace Compress { // This implementation is mostly based on the LZMA specification contained in the 7-Zip SDK, which has been placed in the public domain. // LZMA Specification Draft (2015): https://www.7-zip.org/a/lzma-specification.7z +struct LzmaModelProperties { + u8 literal_context_bits; + u8 literal_position_bits; + u8 position_bits; +}; + struct LzmaDecompressorOptions { u8 literal_context_bits { 0 }; u8 literal_position_bits { 0 }; @@ -32,10 +38,10 @@ struct [[gnu::packed]] LzmaHeader { ErrorOr<LzmaDecompressorOptions> as_decompressor_options() const; -private: - ErrorOr<void> decode_model_properties(u8& literal_context_bits, u8& literal_pos_bits, u8& pos_bits) const; + static ErrorOr<LzmaModelProperties> decode_model_properties(u8 input_bits); - u8 m_model_properties; +private: + u8 m_encoded_model_properties; u32 m_dictionary_size; u64 m_uncompressed_size; }; |