mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 05:27:45 +00:00
AK: Move ConstrainedStream from LibWasm and limit discarding
This commit is contained in:
parent
92800cff4a
commit
d1f6a28ffd
6 changed files with 99 additions and 51 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ConstrainedStream.h>
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/Endian.h>
|
||||
#include <AK/LEB128.h>
|
||||
|
@ -1186,7 +1187,7 @@ ParseResult<CodeSection::Code> CodeSection::Code::parse(Stream& stream)
|
|||
return with_eof_check(stream, ParseError::InvalidSize);
|
||||
size_t size = size_or_error.release_value();
|
||||
|
||||
auto constrained_stream = ConstrainedStream { stream, size };
|
||||
auto constrained_stream = ConstrainedStream { MaybeOwned<Stream>(stream), size };
|
||||
|
||||
auto func = Func::parse(constrained_stream);
|
||||
if (func.is_error())
|
||||
|
@ -1301,7 +1302,7 @@ ParseResult<Module> Module::parse(Stream& stream)
|
|||
return with_eof_check(stream, ParseError::ExpectedSize);
|
||||
size_t section_size = section_size_or_error.release_value();
|
||||
|
||||
auto section_stream = ConstrainedStream { stream, section_size };
|
||||
auto section_stream = ConstrainedStream { MaybeOwned<Stream>(stream), section_size };
|
||||
|
||||
switch (section_id) {
|
||||
case CustomSection::section_id:
|
||||
|
|
|
@ -135,55 +135,6 @@ private:
|
|||
Vector<u8, 8> m_buffer;
|
||||
};
|
||||
|
||||
class ConstrainedStream : public Stream {
|
||||
public:
|
||||
explicit ConstrainedStream(Stream& stream, size_t size)
|
||||
: m_stream(stream)
|
||||
, m_bytes_left(size)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
ErrorOr<Bytes> read_some(Bytes bytes) override
|
||||
{
|
||||
auto to_read = min(m_bytes_left, bytes.size());
|
||||
auto read_bytes = TRY(m_stream.read_some(bytes.slice(0, to_read)));
|
||||
m_bytes_left -= read_bytes.size();
|
||||
return read_bytes;
|
||||
}
|
||||
|
||||
bool is_eof() const override
|
||||
{
|
||||
return m_bytes_left == 0 || m_stream.is_eof();
|
||||
}
|
||||
|
||||
ErrorOr<void> discard(size_t count) override
|
||||
{
|
||||
if (count > m_bytes_left)
|
||||
return Error::from_string_literal("Trying to discard more bytes than allowed");
|
||||
|
||||
return m_stream.discard(count);
|
||||
}
|
||||
|
||||
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override
|
||||
{
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
virtual bool is_open() const override
|
||||
{
|
||||
return m_stream.is_open();
|
||||
}
|
||||
|
||||
virtual void close() override
|
||||
{
|
||||
m_stream.close();
|
||||
}
|
||||
|
||||
Stream& m_stream;
|
||||
size_t m_bytes_left { 0 };
|
||||
};
|
||||
|
||||
// https://webassembly.github.io/spec/core/bikeshed/#value-types%E2%91%A2
|
||||
class ValueType {
|
||||
public:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue