Skip to content

Truly independent web browser

LibCompress: Remove CanonicalCode

Shannon Booth5c5f8506099d
diff --git a/Libraries/LibCompress/CMakeLists.txt b/Libraries/LibCompress/CMakeLists.txt
index 40a5a9a63ed..bacb6d662c2 100644
--- a/Libraries/LibCompress/CMakeLists.txt
+++ b/Libraries/LibCompress/CMakeLists.txt
@@ -1,5 +1,4 @@
set(SOURCES
- Deflate.cpp
GenericZlib.cpp
Zlib.cpp
)
diff --git a/Libraries/LibCompress/Deflate.cpp b/Libraries/LibCompress/Deflate.cpp
deleted file mode 100644
index 54263373181..00000000000
--- a/Libraries/LibCompress/Deflate.cpp
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (c) 2020, the SerenityOS developers.
- * Copyright (c) 2021, Idan Horowitz <[email protected]>
- * Copyright (c) 2025, Altomani Gianluca <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibCompress/Deflate.h>
-#include <LibCompress/DeflateTables.h>
-
-namespace Compress {
-
-ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
-{
- CanonicalCode code;
-
- auto non_zero_symbols = 0;
- auto last_non_zero = -1;
- for (size_t i = 0; i < bytes.size(); i++) {
- if (bytes[i] != 0) {
- non_zero_symbols++;
- last_non_zero = i;
- }
- }
-
- if (non_zero_symbols == 1) { // special case - only 1 symbol
- 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));
- }
- code.m_bit_codes[last_non_zero] = 0;
- code.m_bit_code_lengths[last_non_zero] = 1;
-
- return code;
- }
-
- auto next_code = 0;
- for (size_t code_length = 1; code_length <= 15; ++code_length) {
- next_code <<= 1;
- auto start_bit = 1 << code_length;
-
- for (size_t symbol = 0; symbol < bytes.size(); ++symbol) {
- if (bytes[symbol] != code_length)
- continue;
-
- if (next_code > start_bit)
- return Error::from_string_literal("Failed to decode code lengths");
-
- 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));
- }
- code.m_bit_codes[symbol] = fast_reverse16(start_bit | next_code, code_length); // DEFLATE writes huffman encoded symbols as lsb-first
- code.m_bit_code_lengths[symbol] = code_length;
-
- next_code++;
- }
- }
-
- if (next_code != (1 << 15))
- return Error::from_string_literal("Failed to decode code lengths");
-
- return code;
-}
-
-}
diff --git a/Libraries/LibCompress/Deflate.h b/Libraries/LibCompress/Deflate.h
deleted file mode 100644
index ca425e2c140..00000000000
--- a/Libraries/LibCompress/Deflate.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2020, the SerenityOS developers.
- * Copyright (c) 2021, Idan Horowitz <[email protected]>
- * Copyright (c) 2025, Altomani Gianluca <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/BitStream.h>
-#include <AK/Stream.h>
-#include <AK/Vector.h>
-
-namespace Compress {
-
-class CanonicalCode {
-public:
- CanonicalCode() = default;
- ErrorOr<void> write_symbol(LittleEndianOutputBitStream&, u32) const;
-
- static ErrorOr<CanonicalCode> from_bytes(ReadonlyBytes);
-
-private:
- // 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.
- Vector<u16, 288> m_bit_codes {};
- Vector<u16, 288> m_bit_code_lengths {};
-};
-
-ALWAYS_INLINE ErrorOr<void> CanonicalCode::write_symbol(LittleEndianOutputBitStream& stream, u32 symbol) const
-{
- auto code = symbol < m_bit_codes.size() ? m_bit_codes[symbol] : 0u;
- auto length = symbol < m_bit_code_lengths.size() ? m_bit_code_lengths[symbol] : 0u;
- TRY(stream.write_bits(code, length));
- return {};
-}
-
-}
diff --git a/Libraries/LibCompress/DeflateTables.h b/Libraries/LibCompress/DeflateTables.h
deleted file mode 100644
index 6f8922b8057..00000000000
--- a/Libraries/LibCompress/DeflateTables.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2021, Idan Horowitz <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Array.h>
-#include <stdint.h>
-
-namespace Compress {
-
-static consteval u8 reverse8(u8 value)
-{
- u8 result = 0;
- for (size_t i = 0; i < 8; i++) {
- if (value & (1 << i))
- result |= 1 << (7 - i);
- }
- return result;
-}
-static consteval Array<u8, UINT8_MAX + 1> generate_reverse8_lookup_table()
-{
- Array<u8, UINT8_MAX + 1> array;
- for (size_t i = 0; i <= UINT8_MAX; i++) {
- array[i] = reverse8(i);
- }
- return array;
-}
-static constexpr auto reverse8_lookup_table = generate_reverse8_lookup_table();
-
-// Lookup-table based bit swap
-ALWAYS_INLINE static u16 fast_reverse16(u16 value, size_t bits)
-{
- VERIFY(bits <= 16);
-
- u16 lo = value & 0xff;
- u16 hi = value >> 8;
-
- u16 reversed = (u16)((reverse8_lookup_table[lo] << 8) | reverse8_lookup_table[hi]);
-
- return reversed >> (16 - bits);
-}
-
-}
diff --git a/Libraries/LibCompress/Huffman.h b/Libraries/LibCompress/Huffman.h
deleted file mode 100644
index eae26e3909c..00000000000
--- a/Libraries/LibCompress/Huffman.h
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Copyright (c) 2024, the SerenityOS developers.
- * Copyright (c) 2021, Idan Horowitz <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Array.h>
-#include <AK/BinaryHeap.h>
-
-namespace Compress {
-
-template<size_t Size>
-void generate_huffman_lengths(Array<u8, Size>& lengths, Array<u16, Size> const& frequencies, size_t max_bit_length, u16 shift = 0)
-{
- VERIFY((1u << max_bit_length) >= Size);
- u16 heap_keys[Size]; // Used for O(n) heap construction
- u16 heap_values[Size];
-
- u16 huffman_links[Size * 2] = { 0 };
- size_t non_zero_freqs = 0;
- for (size_t i = 0; i < Size; i++) {
- auto frequency = frequencies[i];
- if (frequency == 0)
- continue;
-
- frequency = max(1, frequency >> shift);
-
- heap_keys[non_zero_freqs] = frequency; // sort symbols by frequency
- heap_values[non_zero_freqs] = Size + non_zero_freqs; // huffman_links "links"
- non_zero_freqs++;
- }
-
- // special case for only 1 used symbol
- if (non_zero_freqs < 2) {
- for (size_t i = 0; i < Size; i++)
- lengths[i] = (frequencies[i] == 0) ? 0 : 1;
- return;
- }
-
- BinaryHeap<u16, u16, Size> heap { heap_keys, heap_values, non_zero_freqs };
-
- // build the huffman tree - binary heap is used for efficient frequency comparisons
- while (heap.size() > 1) {
- u16 lowest_frequency = heap.peek_min_key();
- u16 lowest_link = heap.pop_min();
- u16 second_lowest_frequency = heap.peek_min_key();
- u16 second_lowest_link = heap.pop_min();
-
- u16 new_link = heap.size() + 1;
-
- u32 sum = lowest_frequency + second_lowest_frequency;
- sum = min(sum, UINT16_MAX);
- heap.insert(sum, new_link);
-
- huffman_links[lowest_link] = new_link;
- huffman_links[second_lowest_link] = new_link;
- }
-
- non_zero_freqs = 0;
- for (size_t i = 0; i < Size; i++) {
- if (frequencies[i] == 0) {
- lengths[i] = 0;
- continue;
- }
-
- u16 link = huffman_links[Size + non_zero_freqs];
- non_zero_freqs++;
-
- size_t bit_length = 1;
- while (link != 1) {
- bit_length++;
- link = huffman_links[link];
- }
-
- if (bit_length > max_bit_length) {
- VERIFY(shift < 15);
- return generate_huffman_lengths(lengths, frequencies, max_bit_length, shift + 1);
- }
-
- lengths[i] = bit_length;
- }
-}
-
-}
diff --git a/Tests/LibCompress/CMakeLists.txt b/Tests/LibCompress/CMakeLists.txt
index 82c3d28b422..0ba8e606ca9 100644
--- a/Tests/LibCompress/CMakeLists.txt
+++ b/Tests/LibCompress/CMakeLists.txt
@@ -1,7 +1,6 @@
ladybird_test_suite(LibCompress)
set(TEST_SOURCES
- TestDeflate.cpp
TestZlib.cpp
)
diff --git a/Tests/LibCompress/TestDeflate.cpp b/Tests/LibCompress/TestDeflate.cpp
deleted file mode 100644
index b370860f3c1..00000000000
--- a/Tests/LibCompress/TestDeflate.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- * Copyright (c) 2020-2021, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <AK/Array.h>
-#include <LibCompress/Deflate.h>
-#include <LibTest/TestCase.h>
-
-TEST_CASE(invalid_canonical_code)
-{
- Array<u8, 257> code;
- code.fill(0x08);
- EXPECT(Compress::CanonicalCode::from_bytes(code).is_error());
-}