diff options
author | asynts <asynts@gmail.com> | 2020-09-05 17:38:46 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-06 12:54:45 +0200 |
commit | 4c317a94c7c90a4e60b84d7837a2b73ad8011f1f (patch) | |
tree | 37be4bf57b1d3febe43a4232a324a814391224fb | |
parent | 6de63782c79ea7975df0c23c3ee6673b2bd5a3f7 (diff) | |
download | serenity-4c317a94c7c90a4e60b84d7837a2b73ad8011f1f.zip |
LibCompress: Simplify logic in deflate implementation.
-rw-r--r-- | AK/Tests/TestCircularDuplexStream.cpp | 30 | ||||
-rw-r--r-- | Libraries/LibCompress/Deflate.cpp | 15 |
2 files changed, 36 insertions, 9 deletions
diff --git a/AK/Tests/TestCircularDuplexStream.cpp b/AK/Tests/TestCircularDuplexStream.cpp index 13bd4d405c..f784205db2 100644 --- a/AK/Tests/TestCircularDuplexStream.cpp +++ b/AK/Tests/TestCircularDuplexStream.cpp @@ -64,9 +64,8 @@ TEST_CASE(overwritting_is_well_defined) stream >> Bytes { bytes, sizeof(bytes) }; - for (size_t idx = 0; idx < half_capacity; ++idx) { + for (size_t idx = 0; idx < half_capacity; ++idx) EXPECT_EQ(bytes[idx], idx % 256); - } for (size_t idx = 0; idx < half_capacity; ++idx) stream << static_cast<u8>(idx % 256); @@ -84,4 +83,31 @@ TEST_CASE(overwritting_is_well_defined) EXPECT(stream.eof()); } +TEST_CASE(of_by_one) +{ + constexpr size_t half_capacity = 32; + constexpr size_t capacity = half_capacity * 2; + + CircularDuplexStream<capacity> stream; + + for (size_t idx = 0; idx < half_capacity; ++idx) + stream << static_cast<u8>(0); + + for (size_t idx = 0; idx < half_capacity; ++idx) + stream << static_cast<u8>(1); + + stream.discard_or_error(capacity); + + for (size_t idx = 0; idx < capacity; ++idx) { + u8 byte; + stream.read({ &byte, sizeof(byte) }, capacity); + stream << byte; + + if (idx < half_capacity) + EXPECT_EQ(byte, 0); + else + EXPECT_EQ(byte, 1); + } +} + TEST_MAIN(CircularDuplexStream) diff --git a/Libraries/LibCompress/Deflate.cpp b/Libraries/LibCompress/Deflate.cpp index b1f89b5f4c..25f69c737e 100644 --- a/Libraries/LibCompress/Deflate.cpp +++ b/Libraries/LibCompress/Deflate.cpp @@ -138,17 +138,18 @@ bool DeflateDecompressor::CompressedBlock::try_read_more() m_eof = true; return false; } else { - // FIXME: This assertion depends on user input. - ASSERT(m_distance_codes.has_value()); + if (!m_distance_codes.has_value()) { + m_decompressor.set_fatal_error(); + return false; + } const auto run_length = m_decompressor.decode_run_length(symbol); const auto distance = m_decompressor.decode_distance(m_distance_codes.value().read_symbol(m_decompressor.m_input_stream)); - size_t nread = 0; - while (nread < run_length) { - const auto nreserved = min(run_length - nread, m_decompressor.m_output_stream.remaining_contigous_space()); - auto bytes = m_decompressor.m_output_stream.reserve_contigous_space(nreserved); - nread += m_decompressor.m_output_stream.read(bytes, distance + nread + nreserved); + for (size_t idx = 0; idx < run_length; ++idx) { + u8 byte = 0; + m_decompressor.m_output_stream.read({ &byte, sizeof(byte) }, distance); + m_decompressor.m_output_stream << byte; } return true; |