Skip to content

Truly independent web browser

LibCompress+LibGfx: Remove CanonicalCode's decoding path

Shannon Booth967783bb9230
diff --git a/Libraries/LibCompress/Deflate.cpp b/Libraries/LibCompress/Deflate.cpp
index 4268af7e44e..039f626a3ad 100644
--- a/Libraries/LibCompress/Deflate.cpp
+++ b/Libraries/LibCompress/Deflate.cpp
@@ -6,7 +6,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/BinarySearch.h>
#include <AK/NeverDestroyed.h>
#include <LibCompress/Deflate.h>
#include <LibCompress/DeflateTables.h>
@@ -27,10 +26,6 @@ ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
}
if (non_zero_symbols == 1) { // special case - only 1 symbol
- code.m_prefix_table[0] = PrefixTableEntry { static_cast<u16>(last_non_zero), 1u };
- code.m_prefix_table[1] = code.m_prefix_table[0];
- code.m_max_prefixed_code_length = 1;
-
if (code.m_bit_codes.size() < static_cast<size_t>(last_non_zero + 1)) {
TRY(code.m_bit_codes.try_resize(last_non_zero + 1));
TRY(code.m_bit_code_lengths.try_resize(last_non_zero + 1));
@@ -41,14 +36,6 @@ ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
return code;
}
- struct PrefixCode {
- u16 symbol_code { 0 };
- u16 symbol_value { 0 };
- u16 code_length { 0 };
- };
- Array<PrefixCode, 1 << CanonicalCode::max_allowed_prefixed_code_length> prefix_codes;
- size_t number_of_prefix_codes = 0;
-
auto next_code = 0;
for (size_t code_length = 1; code_length <= 15; ++code_length) {
next_code <<= 1;
@@ -61,21 +48,6 @@ ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
if (next_code > start_bit)
return Error::from_string_literal("Failed to decode code lengths");
- if (code_length <= CanonicalCode::max_allowed_prefixed_code_length) {
- if (number_of_prefix_codes >= prefix_codes.size())
- return Error::from_string_literal("Invalid canonical Huffman code");
-
- auto& prefix_code = prefix_codes[number_of_prefix_codes++];
- prefix_code.symbol_code = next_code;
- prefix_code.symbol_value = symbol;
- prefix_code.code_length = code_length;
-
- code.m_max_prefixed_code_length = code_length;
- } else {
- code.m_symbol_codes.append(start_bit | next_code);
- code.m_symbol_values.append(symbol);
- }
-
if (code.m_bit_codes.size() < symbol + 1) {
TRY(code.m_bit_codes.try_resize(symbol + 1));
TRY(code.m_bit_code_lengths.try_resize(symbol + 1));
@@ -90,44 +62,7 @@ ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
if (next_code != (1 << 15))
return Error::from_string_literal("Failed to decode code lengths");
- for (auto [symbol_code, symbol_value, code_length] : prefix_codes) {
- if (code_length == 0 || code_length > CanonicalCode::max_allowed_prefixed_code_length)
- break;
-
- auto shift = code.m_max_prefixed_code_length - code_length;
- symbol_code <<= shift;
-
- for (size_t j = 0; j < (1u << shift); ++j) {
- auto index = fast_reverse16(symbol_code + j, code.m_max_prefixed_code_length);
- code.m_prefix_table[index] = PrefixTableEntry { symbol_value, code_length };
- }
- }
-
return code;
}
-ErrorOr<u32> CanonicalCode::read_symbol(LittleEndianInputBitStream& stream) const
-{
- auto prefix = TRY(stream.peek_bits<size_t>(m_max_prefixed_code_length));
-
- if (auto [symbol_value, code_length] = m_prefix_table[prefix]; code_length != 0) {
- stream.discard_previously_peeked_bits(code_length);
- return symbol_value;
- }
-
- auto code_bits = TRY(stream.read_bits<u16>(m_max_prefixed_code_length));
- code_bits = fast_reverse16(code_bits, m_max_prefixed_code_length);
- code_bits |= 1 << m_max_prefixed_code_length;
-
- for (size_t i = m_max_prefixed_code_length; i < 16; ++i) {
- size_t index;
- if (binary_search(m_symbol_codes.span(), code_bits, &index))
- return m_symbol_values[index];
-
- code_bits = code_bits << 1 | TRY(stream.read_bit());
- }
-
- return Error::from_string_literal("Symbol exceeds maximum symbol number");
-}
-
}
diff --git a/Libraries/LibCompress/Deflate.h b/Libraries/LibCompress/Deflate.h
index b9672aef1bb..ca425e2c140 100644
--- a/Libraries/LibCompress/Deflate.h
+++ b/Libraries/LibCompress/Deflate.h
@@ -8,7 +8,6 @@
#pragma once
-#include <AK/Array.h>
#include <AK/BitStream.h>
#include <AK/Stream.h>
#include <AK/Vector.h>
@@ -18,26 +17,11 @@ namespace Compress {
class CanonicalCode {
public:
CanonicalCode() = default;
- ErrorOr<u32> read_symbol(LittleEndianInputBitStream&) const;
ErrorOr<void> write_symbol(LittleEndianOutputBitStream&, u32) const;
static ErrorOr<CanonicalCode> from_bytes(ReadonlyBytes);
private:
- static constexpr size_t max_allowed_prefixed_code_length = 8;
-
- struct PrefixTableEntry {
- u16 symbol_value { 0 };
- u16 code_length { 0 };
- };
-
- // Decompression - indexed by code
- Vector<u16, 286> m_symbol_codes;
- Vector<u16, 286> m_symbol_values;
-
- Array<PrefixTableEntry, 1 << max_allowed_prefixed_code_length> m_prefix_table {};
- size_t m_max_prefixed_code_length { 0 };
-
// Compression - indexed by symbol
// Deflate uses a maximum of 288 symbols (maximum of 32 for distances),
// but this is also used by webp, which can use up to 256 + 24 + (1 << 11) == 2328 symbols.
diff --git a/Libraries/LibGfx/ImageFormats/WebPSharedLossless.cpp b/Libraries/LibGfx/ImageFormats/WebPSharedLossless.cpp
index 3b5b4928e0e..331b4202cfe 100644
--- a/Libraries/LibGfx/ImageFormats/WebPSharedLossless.cpp
+++ b/Libraries/LibGfx/ImageFormats/WebPSharedLossless.cpp
@@ -25,11 +25,4 @@ ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
return CanonicalCode(TRY(Compress::CanonicalCode::from_bytes(bytes)));
}
-ErrorOr<u32> CanonicalCode::read_symbol(LittleEndianInputBitStream& bit_stream) const
-{
- return TRY(m_code.visit(
- [](u32 single_code) -> ErrorOr<u32> { return single_code; },
- [&bit_stream](Compress::CanonicalCode const& code) { return code.read_symbol(bit_stream); }));
-}
-
}
diff --git a/Libraries/LibGfx/ImageFormats/WebPSharedLossless.h b/Libraries/LibGfx/ImageFormats/WebPSharedLossless.h
index 25ec33f0ca7..4bf88874ec1 100644
--- a/Libraries/LibGfx/ImageFormats/WebPSharedLossless.h
+++ b/Libraries/LibGfx/ImageFormats/WebPSharedLossless.h
@@ -21,7 +21,6 @@ public:
CanonicalCode() = default;
static ErrorOr<CanonicalCode> from_bytes(ReadonlyBytes);
- ErrorOr<u32> read_symbol(LittleEndianInputBitStream&) const;
ErrorOr<void> write_symbol(LittleEndianOutputBitStream&, u32) const;
private:
diff --git a/Tests/LibCompress/TestDeflate.cpp b/Tests/LibCompress/TestDeflate.cpp
index 48273bb89d3..b370860f3c1 100644
--- a/Tests/LibCompress/TestDeflate.cpp
+++ b/Tests/LibCompress/TestDeflate.cpp
@@ -5,53 +5,9 @@
*/
#include <AK/Array.h>
-#include <AK/BitStream.h>
-#include <AK/MemoryStream.h>
#include <LibCompress/Deflate.h>
#include <LibTest/TestCase.h>
-TEST_CASE(canonical_code_simple)
-{
- Array<u8, 32> const code {
- 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
- 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
- 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05
- };
- Array<u8, 6> const input {
- 0x00, 0x42, 0x84, 0xa9, 0xb0, 0x15
- };
- Array<u32, 9> const output {
- 0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15
- };
-
- auto const huffman = TRY_OR_FAIL(Compress::CanonicalCode::from_bytes(code));
- auto memory_stream = MUST(try_make<FixedMemoryStream>(input));
- LittleEndianInputBitStream bit_stream { move(memory_stream) };
-
- for (size_t idx = 0; idx < 9; ++idx)
- EXPECT_EQ(MUST(huffman.read_symbol(bit_stream)), output[idx]);
-}
-
-TEST_CASE(canonical_code_complex)
-{
- Array<u8, 6> const code {
- 0x03, 0x02, 0x03, 0x03, 0x02, 0x03
- };
- Array<u8, 4> const input {
- 0xa1, 0xf3, 0xa1, 0xf3
- };
- Array<u32, 12> const output {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05
- };
-
- auto const huffman = TRY_OR_FAIL(Compress::CanonicalCode::from_bytes(code));
- auto memory_stream = MUST(try_make<FixedMemoryStream>(input));
- LittleEndianInputBitStream bit_stream { move(memory_stream) };
-
- for (size_t idx = 0; idx < 12; ++idx)
- EXPECT_EQ(MUST(huffman.read_symbol(bit_stream)), output[idx]);
-}
-
TEST_CASE(invalid_canonical_code)
{
Array<u8, 257> code;