AK: Remove the fallible constructor from LittleEndianInputBitStream

This commit is contained in:
Tim Schumacher 2023-01-30 11:04:23 +01:00 committed by Linus Groh
parent fa09152e23
commit 261d62438f
Notes: sideshowbarker 2024-07-17 00:41:54 +09:00
4 changed files with 18 additions and 23 deletions

View file

@ -121,12 +121,7 @@ private:
/// in little-endian order from another stream.
class LittleEndianInputBitStream : public Stream {
public:
static ErrorOr<NonnullOwnPtr<LittleEndianInputBitStream>> construct(MaybeOwned<Stream> stream)
{
return adopt_nonnull_own_or_enomem<LittleEndianInputBitStream>(new LittleEndianInputBitStream(move(stream)));
}
LittleEndianInputBitStream(MaybeOwned<Stream> stream)
explicit LittleEndianInputBitStream(MaybeOwned<Stream> stream)
: m_stream(move(stream))
{
}

View file

@ -16,23 +16,23 @@ TEST_CASE(little_endian_bit_stream_input_output_match)
// Note: The bit stream only ever reads from/writes to the underlying stream in one byte chunks,
// so testing with sizes that will not trigger a write will yield unexpected results.
auto bit_write_stream = MUST(LittleEndianOutputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
auto bit_read_stream = MUST(LittleEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
LittleEndianInputBitStream bit_read_stream { MaybeOwned<AK::Stream>(*memory_stream) };
// Test two mirrored chunks of a fully mirrored pattern to check that we are not dropping bits.
{
MUST(bit_write_stream->write_bits(0b1111u, 4));
MUST(bit_write_stream->write_bits(0b1111u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
auto result = MUST(bit_read_stream.read_bits(4));
EXPECT_EQ(0b1111u, result);
result = MUST(bit_read_stream->read_bits(4));
result = MUST(bit_read_stream.read_bits(4));
EXPECT_EQ(0b1111u, result);
}
{
MUST(bit_write_stream->write_bits(0b0000u, 4));
MUST(bit_write_stream->write_bits(0b0000u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
auto result = MUST(bit_read_stream.read_bits(4));
EXPECT_EQ(0b0000u, result);
result = MUST(bit_read_stream->read_bits(4));
result = MUST(bit_read_stream.read_bits(4));
EXPECT_EQ(0b0000u, result);
}
@ -40,9 +40,9 @@ TEST_CASE(little_endian_bit_stream_input_output_match)
{
MUST(bit_write_stream->write_bits(0b1000u, 4));
MUST(bit_write_stream->write_bits(0b1000u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
auto result = MUST(bit_read_stream.read_bits(4));
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream->read_bits(4));
result = MUST(bit_read_stream.read_bits(4));
EXPECT_EQ(0b1000u, result);
}
@ -50,16 +50,16 @@ TEST_CASE(little_endian_bit_stream_input_output_match)
{
MUST(bit_write_stream->write_bits(0b1000u, 4));
MUST(bit_write_stream->write_bits(0b0100u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
auto result = MUST(bit_read_stream.read_bits(4));
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream->read_bits(4));
result = MUST(bit_read_stream.read_bits(4));
EXPECT_EQ(0b0100u, result);
}
// Test a pattern that spans multiple bytes.
{
MUST(bit_write_stream->write_bits(0b1101001000100001u, 16));
auto result = MUST(bit_read_stream->read_bits(16));
auto result = MUST(bit_read_stream.read_bits(16));
EXPECT_EQ(0b1101001000100001u, result);
}
}

View file

@ -29,10 +29,10 @@ TEST_CASE(canonical_code_simple)
auto const huffman = Compress::CanonicalCode::from_bytes(code).value();
auto memory_stream = MUST(FixedMemoryStream::construct(input));
auto bit_stream = MUST(LittleEndianInputBitStream::construct(move(memory_stream)));
LittleEndianInputBitStream bit_stream { move(memory_stream) };
for (size_t idx = 0; idx < 9; ++idx)
EXPECT_EQ(MUST(huffman.read_symbol(*bit_stream)), output[idx]);
EXPECT_EQ(MUST(huffman.read_symbol(bit_stream)), output[idx]);
}
TEST_CASE(canonical_code_complex)
@ -49,10 +49,10 @@ TEST_CASE(canonical_code_complex)
auto const huffman = Compress::CanonicalCode::from_bytes(code).value();
auto memory_stream = MUST(FixedMemoryStream::construct(input));
auto bit_stream = MUST(LittleEndianInputBitStream::construct(move(memory_stream)));
LittleEndianInputBitStream bit_stream { move(memory_stream) };
for (size_t idx = 0; idx < 12; ++idx)
EXPECT_EQ(MUST(huffman.read_symbol(*bit_stream)), output[idx]);
EXPECT_EQ(MUST(huffman.read_symbol(bit_stream)), output[idx]);
}
TEST_CASE(deflate_decompress_compressed_block)

View file

@ -599,7 +599,7 @@ PDFErrorOr<Vector<DocumentParser::PageOffsetHintTableEntry>> DocumentParser::par
auto input_stream = TRY(FixedMemoryStream::construct(hint_stream_bytes));
TRY(input_stream->seek(sizeof(PageOffsetHintTable)));
auto bit_stream = TRY(LittleEndianInputBitStream::construct(move(input_stream)));
LittleEndianInputBitStream bit_stream { move(input_stream) };
auto number_of_pages = m_linearization_dictionary.value().number_of_pages;
Vector<PageOffsetHintTableEntry> entries;
@ -620,7 +620,7 @@ PDFErrorOr<Vector<DocumentParser::PageOffsetHintTableEntry>> DocumentParser::par
for (int i = 0; i < number_of_pages; i++) {
auto& entry = entries[i];
entry.*field = TRY(bit_stream->read_bits(bit_size));
entry.*field = TRY(bit_stream.read_bits(bit_size));
}
return {};
@ -636,7 +636,7 @@ PDFErrorOr<Vector<DocumentParser::PageOffsetHintTableEntry>> DocumentParser::par
items.ensure_capacity(number_of_shared_objects);
for (size_t i = 0; i < number_of_shared_objects; i++)
items.unchecked_append(TRY(bit_stream->read_bits(bit_size)));
items.unchecked_append(TRY(bit_stream.read_bits(bit_size)));
entries[page].*field = move(items);
}