1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:27:45 +00:00

LibCore: Use the new Handle type for the BitStream types

This allows us to either pass a reference, which keeps compatibility
with old code, or to pass a NonnullOwnPtr, which allows us to
comfortably chain streams as usual.
This commit is contained in:
Tim Schumacher 2022-12-02 19:26:49 +01:00 committed by Linus Groh
parent 8b5df161af
commit f909cfbe75
4 changed files with 33 additions and 35 deletions

View file

@ -60,7 +60,7 @@ MaybeLoaderError FlacLoaderPlugin::initialize()
// 11.5 STREAM // 11.5 STREAM
MaybeLoaderError FlacLoaderPlugin::parse_header() MaybeLoaderError FlacLoaderPlugin::parse_header()
{ {
auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(*m_stream)); auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*m_stream)));
// A mixture of VERIFY and the non-crashing TRY(). // A mixture of VERIFY and the non-crashing TRY().
#define FLAC_VERIFY(check, category, msg) \ #define FLAC_VERIFY(check, category, msg) \
@ -79,7 +79,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
auto streaminfo = TRY(next_meta_block(*bit_input)); auto streaminfo = TRY(next_meta_block(*bit_input));
FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO"); FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
auto streaminfo_data_memory = LOADER_TRY(Core::Stream::MemoryStream::construct(streaminfo.data.bytes())); auto streaminfo_data_memory = LOADER_TRY(Core::Stream::MemoryStream::construct(streaminfo.data.bytes()));
auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(*streaminfo_data_memory)); auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*streaminfo_data_memory)));
// 11.10 METADATA_BLOCK_STREAMINFO // 11.10 METADATA_BLOCK_STREAMINFO
m_min_block_size = LOADER_TRY(streaminfo_data->read_bits<u16>(16)); m_min_block_size = LOADER_TRY(streaminfo_data->read_bits<u16>(16));
@ -150,7 +150,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block) MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
{ {
auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes())); auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes()));
auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(*memory_stream)); auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
PictureData picture {}; PictureData picture {};
@ -187,7 +187,7 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block) MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
{ {
auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes())); auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes()));
auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(*memory_stream)); auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
for (size_t i = 0; i < block.length / 18; ++i) { for (size_t i = 0; i < block.length / 18; ++i) {
// 11.14. SEEKPOINT // 11.14. SEEKPOINT
FlacSeekPoint seekpoint { FlacSeekPoint seekpoint {
@ -333,7 +333,7 @@ MaybeLoaderError FlacLoaderPlugin::next_frame(Span<Sample> target_vector)
} \ } \
} while (0) } while (0)
auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(*m_stream)); auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*m_stream)));
// TODO: Check the CRC-16 checksum (and others) by keeping track of read data // TODO: Check the CRC-16 checksum (and others) by keeping track of read data

View file

@ -41,7 +41,7 @@ Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::try_create(
MaybeLoaderError MP3LoaderPlugin::initialize() MaybeLoaderError MP3LoaderPlugin::initialize()
{ {
m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(*m_stream)); m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*m_stream)));
TRY(synchronize()); TRY(synchronize());

View file

@ -29,7 +29,7 @@ ErrorOr<size_t> BrotliDecompressionStream::CanonicalCode::read_symbol(LittleEndi
} }
BrotliDecompressionStream::BrotliDecompressionStream(Stream& stream) BrotliDecompressionStream::BrotliDecompressionStream(Stream& stream)
: m_input_stream(stream) : m_input_stream(Core::Stream::Handle(stream))
{ {
} }

View file

@ -21,12 +21,11 @@ namespace Core::Stream {
/// A stream wrapper class that allows you to read arbitrary amounts of bits /// A stream wrapper class that allows you to read arbitrary amounts of bits
/// in big-endian order from another stream. /// in big-endian order from another stream.
/// Note that this stream does not own its underlying stream, it merely takes a reference.
class BigEndianInputBitStream : public Stream { class BigEndianInputBitStream : public Stream {
public: public:
static ErrorOr<NonnullOwnPtr<BigEndianInputBitStream>> construct(Stream& stream) static ErrorOr<NonnullOwnPtr<BigEndianInputBitStream>> construct(Handle<Stream> stream)
{ {
return adopt_nonnull_own_or_enomem<BigEndianInputBitStream>(new BigEndianInputBitStream(stream)); return adopt_nonnull_own_or_enomem<BigEndianInputBitStream>(new BigEndianInputBitStream(move(stream)));
} }
// ^Stream // ^Stream
@ -34,18 +33,18 @@ public:
{ {
if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) { if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) {
bytes[0] = m_current_byte.release_value(); bytes[0] = m_current_byte.release_value();
return m_stream.read(bytes.slice(1)); return m_stream->read(bytes.slice(1));
} }
align_to_byte_boundary(); align_to_byte_boundary();
return m_stream.read(bytes); return m_stream->read(bytes);
} }
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream.write(bytes); } virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream->write(bytes); }
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override { return m_stream.write_entire_buffer(bytes); } virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override { return m_stream->write_entire_buffer(bytes); }
virtual bool is_eof() const override { return m_stream.is_eof() && !m_current_byte.has_value(); } virtual bool is_eof() const override { return m_stream->is_eof() && !m_current_byte.has_value(); }
virtual bool is_open() const override { return m_stream.is_open(); } virtual bool is_open() const override { return m_stream->is_open(); }
virtual void close() override virtual void close() override
{ {
m_stream.close(); m_stream->close();
align_to_byte_boundary(); align_to_byte_boundary();
} }
@ -98,7 +97,7 @@ public:
} }
} else { } else {
auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1)); auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1));
TRY(m_stream.read(temp_buffer.bytes())); TRY(m_stream->read(temp_buffer.bytes()));
m_current_byte = temp_buffer[0]; m_current_byte = temp_buffer[0];
m_bit_offset = 0; m_bit_offset = 0;
} }
@ -119,28 +118,27 @@ public:
ALWAYS_INLINE bool is_aligned_to_byte_boundary() const { return m_bit_offset == 0; } ALWAYS_INLINE bool is_aligned_to_byte_boundary() const { return m_bit_offset == 0; }
private: private:
BigEndianInputBitStream(Stream& stream) BigEndianInputBitStream(Handle<Stream> stream)
: m_stream(stream) : m_stream(move(stream))
{ {
} }
Optional<u8> m_current_byte; Optional<u8> m_current_byte;
size_t m_bit_offset { 0 }; size_t m_bit_offset { 0 };
Stream& m_stream; Handle<Stream> m_stream;
}; };
/// A stream wrapper class that allows you to read arbitrary amounts of bits /// A stream wrapper class that allows you to read arbitrary amounts of bits
/// in little-endian order from another stream. /// in little-endian order from another stream.
/// Note that this stream does not own its underlying stream, it merely takes a reference.
class LittleEndianInputBitStream : public Stream { class LittleEndianInputBitStream : public Stream {
public: public:
static ErrorOr<NonnullOwnPtr<LittleEndianInputBitStream>> construct(Stream& stream) static ErrorOr<NonnullOwnPtr<LittleEndianInputBitStream>> construct(Handle<Stream> stream)
{ {
return adopt_nonnull_own_or_enomem<LittleEndianInputBitStream>(new LittleEndianInputBitStream(stream)); return adopt_nonnull_own_or_enomem<LittleEndianInputBitStream>(new LittleEndianInputBitStream(move(stream)));
} }
LittleEndianInputBitStream(Stream& stream) LittleEndianInputBitStream(Handle<Stream> stream)
: m_stream(stream) : m_stream(move(stream))
{ {
} }
@ -149,18 +147,18 @@ public:
{ {
if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) { if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) {
bytes[0] = m_current_byte.release_value(); bytes[0] = m_current_byte.release_value();
return m_stream.read(bytes.slice(1)); return m_stream->read(bytes.slice(1));
} }
align_to_byte_boundary(); align_to_byte_boundary();
return m_stream.read(bytes); return m_stream->read(bytes);
} }
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream.write(bytes); } virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream->write(bytes); }
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override { return m_stream.write_entire_buffer(bytes); } virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override { return m_stream->write_entire_buffer(bytes); }
virtual bool is_eof() const override { return m_stream.is_eof() && !m_current_byte.has_value(); } virtual bool is_eof() const override { return m_stream->is_eof() && !m_current_byte.has_value(); }
virtual bool is_open() const override { return m_stream.is_open(); } virtual bool is_open() const override { return m_stream->is_open(); }
virtual void close() override virtual void close() override
{ {
m_stream.close(); m_stream->close();
align_to_byte_boundary(); align_to_byte_boundary();
} }
@ -209,7 +207,7 @@ public:
} }
} else { } else {
auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1)); auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1));
auto read_bytes = TRY(m_stream.read(temp_buffer.bytes())); auto read_bytes = TRY(m_stream->read(temp_buffer.bytes()));
if (read_bytes.is_empty()) if (read_bytes.is_empty())
return Error::from_string_literal("eof"); return Error::from_string_literal("eof");
m_current_byte = temp_buffer[0]; m_current_byte = temp_buffer[0];
@ -236,7 +234,7 @@ public:
private: private:
Optional<u8> m_current_byte; Optional<u8> m_current_byte;
size_t m_bit_offset { 0 }; size_t m_bit_offset { 0 };
Stream& m_stream; Handle<Stream> m_stream;
}; };
} }