2020-09-01 12:43:32 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-01 12:43:32 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2021-04-26 22:39:02 +03:00
|
|
|
#include <AK/LEB128.h>
|
2020-09-01 12:43:32 +03:00
|
|
|
#include <AK/MemMem.h>
|
|
|
|
#include <AK/Stream.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class InputMemoryStream final : public InputStream {
|
|
|
|
public:
|
2020-09-15 13:08:00 +03:00
|
|
|
explicit InputMemoryStream(ReadonlyBytes bytes)
|
2020-09-01 12:43:32 +03:00
|
|
|
: m_bytes(bytes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-13 13:24:17 +03:00
|
|
|
bool unreliable_eof() const override { return eof(); }
|
|
|
|
bool eof() const { return m_offset >= m_bytes.size(); }
|
2020-09-01 12:43:32 +03:00
|
|
|
|
|
|
|
size_t read(Bytes bytes) override
|
|
|
|
{
|
2020-09-05 17:39:56 +03:00
|
|
|
if (has_any_error())
|
|
|
|
return 0;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const count = min(bytes.size(), remaining());
|
2020-09-01 12:43:32 +03:00
|
|
|
__builtin_memcpy(bytes.data(), m_bytes.data() + m_offset, count);
|
|
|
|
m_offset += count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool read_or_error(Bytes bytes) override
|
|
|
|
{
|
|
|
|
if (remaining() < bytes.size()) {
|
|
|
|
set_recoverable_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
__builtin_memcpy(bytes.data(), m_bytes.data() + m_offset, bytes.size());
|
|
|
|
m_offset += bytes.size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool discard_or_error(size_t count) override
|
|
|
|
{
|
|
|
|
if (remaining() < count) {
|
|
|
|
set_recoverable_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_offset += count;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void seek(size_t offset)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(offset < m_bytes.size());
|
2020-09-01 12:43:32 +03:00
|
|
|
m_offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 peek_or_error() const
|
|
|
|
{
|
|
|
|
if (remaining() == 0) {
|
|
|
|
set_recoverable_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_bytes[m_offset];
|
|
|
|
}
|
|
|
|
|
2021-07-27 10:42:57 +03:00
|
|
|
template<typename ValueType>
|
|
|
|
bool read_LEB128_unsigned(ValueType& result) { return LEB128::read_unsigned(*this, result); }
|
2020-09-01 12:43:32 +03:00
|
|
|
|
2021-07-27 10:42:57 +03:00
|
|
|
template<typename ValueType>
|
|
|
|
bool read_LEB128_signed(ValueType& result) { return LEB128::read_signed(*this, result); }
|
2020-09-01 12:43:32 +03:00
|
|
|
|
|
|
|
ReadonlyBytes bytes() const { return m_bytes; }
|
|
|
|
size_t offset() const { return m_offset; }
|
|
|
|
size_t remaining() const { return m_bytes.size() - m_offset; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ReadonlyBytes m_bytes;
|
|
|
|
size_t m_offset { 0 };
|
|
|
|
};
|
|
|
|
|
2020-09-15 13:08:00 +03:00
|
|
|
class OutputMemoryStream final : public OutputStream {
|
|
|
|
public:
|
|
|
|
explicit OutputMemoryStream(Bytes bytes)
|
|
|
|
: m_bytes(bytes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t write(ReadonlyBytes bytes) override
|
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
|
2020-09-15 13:08:00 +03:00
|
|
|
m_offset += nwritten;
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_or_error(ReadonlyBytes bytes) override
|
|
|
|
{
|
|
|
|
if (remaining() < bytes.size()) {
|
|
|
|
set_recoverable_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
write(bytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:18:31 +03:00
|
|
|
size_t fill_to_end(u8 value)
|
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const nwritten = m_bytes.slice(m_offset).fill(value);
|
2020-09-15 13:18:31 +03:00
|
|
|
m_offset += nwritten;
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2020-09-20 13:32:30 +03:00
|
|
|
bool is_end() const { return remaining() == 0; }
|
|
|
|
|
2020-09-15 13:08:00 +03:00
|
|
|
ReadonlyBytes bytes() const { return { data(), size() }; }
|
|
|
|
Bytes bytes() { return { data(), size() }; }
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
u8 const* data() const { return m_bytes.data(); }
|
2020-09-15 13:08:00 +03:00
|
|
|
u8* data() { return m_bytes.data(); }
|
|
|
|
|
|
|
|
size_t size() const { return m_offset; }
|
|
|
|
size_t remaining() const { return m_bytes.size() - m_offset; }
|
2021-05-11 19:35:36 +03:00
|
|
|
void reset() { m_offset = 0; }
|
2020-09-15 13:08:00 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
size_t m_offset { 0 };
|
|
|
|
Bytes m_bytes;
|
|
|
|
};
|
|
|
|
|
2020-09-01 12:43:32 +03:00
|
|
|
class DuplexMemoryStream final : public DuplexStream {
|
|
|
|
public:
|
|
|
|
static constexpr size_t chunk_size = 4 * 1024;
|
|
|
|
|
2020-09-13 13:24:17 +03:00
|
|
|
bool unreliable_eof() const override { return eof(); }
|
|
|
|
bool eof() const { return m_write_offset == m_read_offset; }
|
2020-09-01 12:43:32 +03:00
|
|
|
|
|
|
|
bool discard_or_error(size_t count) override
|
|
|
|
{
|
|
|
|
if (m_write_offset - m_read_offset < count) {
|
|
|
|
set_recoverable_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_read_offset += count;
|
|
|
|
try_discard_chunks();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<size_t> offset_of(ReadonlyBytes value) const
|
|
|
|
{
|
2020-12-05 04:01:56 +03:00
|
|
|
// We can't directly pass m_chunks to memmem since we have a limited read/write range we want to search in.
|
|
|
|
Vector<ReadonlyBytes> spans;
|
|
|
|
auto chunk_index = (m_read_offset - m_base_offset) / chunk_size;
|
|
|
|
auto chunk_read_offset = (m_read_offset - m_base_offset) % chunk_size;
|
|
|
|
auto bytes_to_search = m_write_offset - m_read_offset;
|
|
|
|
for (; bytes_to_search > 0;) {
|
|
|
|
ReadonlyBytes span = m_chunks[chunk_index];
|
|
|
|
if (chunk_read_offset) {
|
|
|
|
span = span.slice(chunk_read_offset);
|
|
|
|
chunk_read_offset = 0;
|
2020-09-01 12:43:32 +03:00
|
|
|
}
|
2020-12-05 04:01:56 +03:00
|
|
|
if (bytes_to_search < span.size()) {
|
|
|
|
spans.append(span.slice(0, bytes_to_search));
|
|
|
|
break;
|
2020-09-01 12:43:32 +03:00
|
|
|
}
|
2020-12-05 04:01:56 +03:00
|
|
|
bytes_to_search -= span.size();
|
|
|
|
spans.append(move(span));
|
|
|
|
++chunk_index;
|
2020-09-01 12:43:32 +03:00
|
|
|
}
|
|
|
|
|
2020-12-05 04:01:56 +03:00
|
|
|
return memmem(spans.begin(), spans.end(), value);
|
2020-09-01 12:43:32 +03:00
|
|
|
}
|
|
|
|
|
2020-09-01 12:55:04 +03:00
|
|
|
size_t read_without_consuming(Bytes bytes) const
|
2020-09-01 12:43:32 +03:00
|
|
|
{
|
|
|
|
size_t nread = 0;
|
|
|
|
while (bytes.size() - nread > 0 && m_write_offset - m_read_offset - nread > 0) {
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const chunk_index = (m_read_offset - m_base_offset + nread) / chunk_size;
|
|
|
|
auto const chunk_bytes = m_chunks[chunk_index].bytes().slice((m_read_offset + nread) % chunk_size).trim(m_write_offset - m_read_offset - nread);
|
2020-09-01 12:43:32 +03:00
|
|
|
nread += chunk_bytes.copy_trimmed_to(bytes.slice(nread));
|
|
|
|
}
|
|
|
|
|
2020-09-01 12:55:04 +03:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t read(Bytes bytes) override
|
|
|
|
{
|
2020-09-05 17:39:56 +03:00
|
|
|
if (has_any_error())
|
|
|
|
return 0;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const nread = read_without_consuming(bytes);
|
2020-09-01 12:43:32 +03:00
|
|
|
|
2020-09-01 12:55:04 +03:00
|
|
|
m_read_offset += nread;
|
2020-09-01 12:43:32 +03:00
|
|
|
try_discard_chunks();
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool read_or_error(Bytes bytes) override
|
|
|
|
{
|
|
|
|
if (m_write_offset - m_read_offset < bytes.size()) {
|
|
|
|
set_recoverable_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:06:04 +03:00
|
|
|
return read(bytes) == bytes.size();
|
2020-09-01 12:43:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t write(ReadonlyBytes bytes) override
|
|
|
|
{
|
2020-12-09 17:11:09 +03:00
|
|
|
// FIXME: This doesn't write around chunk borders correctly?
|
|
|
|
|
2020-09-01 12:43:32 +03:00
|
|
|
size_t nwritten = 0;
|
|
|
|
while (bytes.size() - nwritten > 0) {
|
|
|
|
if ((m_write_offset + nwritten) % chunk_size == 0)
|
2022-01-20 20:47:39 +03:00
|
|
|
m_chunks.append(ByteBuffer::create_uninitialized(chunk_size).release_value_but_fixme_should_propagate_errors()); // FIXME: Handle possible OOM situation.
|
2020-09-01 12:43:32 +03:00
|
|
|
|
2020-12-09 17:11:09 +03:00
|
|
|
nwritten += bytes.slice(nwritten).copy_trimmed_to(m_chunks.last().bytes().slice((m_write_offset + nwritten) % chunk_size));
|
2020-09-01 12:43:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m_write_offset += nwritten;
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_or_error(ReadonlyBytes bytes) override
|
|
|
|
{
|
|
|
|
write(bytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-01 12:55:04 +03:00
|
|
|
ByteBuffer copy_into_contiguous_buffer() const
|
|
|
|
{
|
2021-09-06 01:59:52 +03:00
|
|
|
// FIXME: Handle possible OOM situation.
|
2022-01-20 20:47:39 +03:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(size()).release_value_but_fixme_should_propagate_errors();
|
2020-09-01 12:55:04 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const nread = read_without_consuming(buffer);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(nread == buffer.size());
|
2020-09-01 12:55:04 +03:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2020-09-01 12:43:32 +03:00
|
|
|
size_t roffset() const { return m_read_offset; }
|
|
|
|
size_t woffset() const { return m_write_offset; }
|
|
|
|
|
2020-09-15 12:48:54 +03:00
|
|
|
size_t size() const { return m_write_offset - m_read_offset; }
|
2020-09-01 12:43:32 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
void try_discard_chunks()
|
|
|
|
{
|
|
|
|
while (m_read_offset - m_base_offset >= chunk_size) {
|
|
|
|
m_chunks.take_first();
|
|
|
|
m_base_offset += chunk_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<ByteBuffer> m_chunks;
|
|
|
|
size_t m_write_offset { 0 };
|
|
|
|
size_t m_read_offset { 0 };
|
|
|
|
size_t m_base_offset { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::DuplexMemoryStream;
|
|
|
|
using AK::InputMemoryStream;
|
|
|
|
using AK::InputStream;
|
2020-09-15 13:08:00 +03:00
|
|
|
using AK::OutputMemoryStream;
|