LibCore: Add support for ReadonlyBytes to MemoryStream

This commit is contained in:
Tim Schumacher 2022-11-30 10:03:19 +01:00 committed by Sam Atkins
parent 38a882ddfa
commit d402f6cdb3
Notes: sideshowbarker 2024-07-17 03:51:58 +09:00
4 changed files with 22 additions and 6 deletions

View file

@ -10,7 +10,7 @@
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{
auto bufstream_result = Core::Stream::MemoryStream::construct({ const_cast<uint8_t*>(data), size });
auto bufstream_result = Core::Stream::MemoryStream::construct({ data, size });
if (bufstream_result.is_error()) {
dbgln("MemoryStream::construct() failed.");
return 1;

View file

@ -12,8 +12,7 @@
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{
// FIXME: Create a ReadonlyBytes variant of Core::Stream::MemoryStream.
auto input_stream_or_error = Core::Stream::MemoryStream::construct(Bytes { const_cast<uint8_t*>(data), size });
auto input_stream_or_error = Core::Stream::MemoryStream::construct({ data, size });
if (input_stream_or_error.is_error())
return 0;

View file

@ -22,6 +22,11 @@ public:
return adopt_nonnull_own_or_enomem<MemoryStream>(new (nothrow) MemoryStream(bytes));
}
static ErrorOr<NonnullOwnPtr<MemoryStream>> construct(ReadonlyBytes bytes)
{
return adopt_nonnull_own_or_enomem<MemoryStream>(new (nothrow) MemoryStream(bytes));
}
virtual bool is_eof() const override { return m_offset >= m_bytes.size(); }
virtual bool is_open() const override { return true; }
// FIXME: It doesn't make sense to close an memory stream. Therefore, we don't do anything here. Is that fine?
@ -67,6 +72,8 @@ public:
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override
{
VERIFY(m_writing_enabled);
// FIXME: Can this not error?
auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
m_offset += nwritten;
@ -81,7 +88,11 @@ public:
return true;
}
Bytes bytes() { return m_bytes; }
Bytes bytes()
{
VERIFY(m_writing_enabled);
return m_bytes;
}
ReadonlyBytes bytes() const { return m_bytes; }
size_t offset() const { return m_offset; }
size_t remaining() const { return m_bytes.size() - m_offset; }
@ -92,9 +103,16 @@ protected:
{
}
explicit MemoryStream(ReadonlyBytes bytes)
: m_bytes({ const_cast<u8*>(bytes.data()), bytes.size() })
, m_writing_enabled(false)
{
}
private:
Bytes m_bytes;
size_t m_offset { 0 };
bool m_writing_enabled { true };
};
}

View file

@ -78,8 +78,7 @@ static Optional<ByteBuffer> handle_content_encoding(ByteBuffer const& buf, Strin
} else if (content_encoding == "br") {
dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: buf is brotli compressed!");
// FIXME: MemoryStream is both read and write, however we only need the read part here
auto bufstream_result = Core::Stream::MemoryStream::construct({ const_cast<u8*>(buf.data()), buf.size() });
auto bufstream_result = Core::Stream::MemoryStream::construct({ buf.data(), buf.size() });
if (bufstream_result.is_error()) {
dbgln("Job::handle_content_encoding: MemoryStream::construct() failed.");
return {};