2018-10-24 10:43:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2019-05-30 11:39:17 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2018-10-24 10:43:52 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class BufferStream {
|
|
|
|
public:
|
|
|
|
explicit BufferStream(ByteBuffer& buffer)
|
|
|
|
: m_buffer(buffer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:28:30 +00:00
|
|
|
~BufferStream()
|
|
|
|
{
|
|
|
|
ASSERT(!m_read_failure);
|
|
|
|
}
|
|
|
|
|
2019-07-16 07:28:05 +00:00
|
|
|
BufferStream& operator<<(i8 value)
|
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator>>(i8& value)
|
|
|
|
{
|
2019-08-03 17:20:05 +00:00
|
|
|
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
|
2019-07-16 07:28:05 +00:00
|
|
|
m_read_failure = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
value = m_buffer[m_offset++];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:28:30 +00:00
|
|
|
BufferStream& operator<<(u8 value)
|
2018-10-24 10:43:52 +00:00
|
|
|
{
|
2019-07-13 22:12:54 +00:00
|
|
|
m_buffer[m_offset++] = value;
|
2019-07-13 22:28:30 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator>>(u8& value )
|
|
|
|
{
|
2019-08-03 17:20:05 +00:00
|
|
|
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
|
2019-07-13 22:28:30 +00:00
|
|
|
m_read_failure = true;
|
|
|
|
return *this;
|
2019-08-03 15:02:42 +00:00
|
|
|
}
|
|
|
|
value = m_buffer[m_offset++];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferStream& operator<<(bool value)
|
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator>>(bool& value )
|
|
|
|
{
|
2019-08-03 17:20:05 +00:00
|
|
|
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
|
2019-08-03 15:02:42 +00:00
|
|
|
m_read_failure = true;
|
|
|
|
return *this;
|
2019-07-13 22:28:30 +00:00
|
|
|
}
|
|
|
|
value = m_buffer[m_offset++];
|
|
|
|
return *this;
|
2018-10-24 10:43:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 22:28:30 +00:00
|
|
|
BufferStream& operator<<(char value)
|
2019-02-02 22:13:12 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
m_buffer[m_offset++] = (u8)value;
|
2019-07-13 22:28:30 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator>>(char& value)
|
|
|
|
{
|
2019-08-03 17:20:05 +00:00
|
|
|
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
|
2019-07-13 22:28:30 +00:00
|
|
|
m_read_failure = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
value = (u8)m_buffer[m_offset++];
|
|
|
|
return *this;
|
2019-02-02 22:13:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 22:28:30 +00:00
|
|
|
BufferStream& operator<<(u16 value)
|
2018-10-24 10:43:52 +00:00
|
|
|
{
|
2019-07-13 22:12:54 +00:00
|
|
|
m_buffer[m_offset++] = value;
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 8);
|
2019-07-13 22:28:30 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator>>(u16& value)
|
|
|
|
{
|
2019-08-03 17:20:05 +00:00
|
|
|
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
|
2019-07-13 22:28:30 +00:00
|
|
|
m_read_failure = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
value = 0;
|
|
|
|
u8 b0 = m_buffer[m_offset++];
|
|
|
|
u8 b1 = m_buffer[m_offset++];
|
|
|
|
value |= (u8(b1) << 8);
|
|
|
|
value |= (u8(b0));
|
|
|
|
return *this;
|
2018-10-24 10:43:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 07:28:05 +00:00
|
|
|
BufferStream& operator<<(i16 value)
|
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value;
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 8);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator>>(i16& value)
|
|
|
|
{
|
2019-08-03 17:20:05 +00:00
|
|
|
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
|
2019-07-16 07:28:05 +00:00
|
|
|
m_read_failure = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
value = 0;
|
|
|
|
u8 b0 = m_buffer[m_offset++];
|
|
|
|
u8 b1 = m_buffer[m_offset++];
|
|
|
|
value |= (u8(b1) << 8);
|
|
|
|
value |= (u8(b0));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:28:30 +00:00
|
|
|
BufferStream& operator<<(u32 value)
|
2018-10-24 10:43:52 +00:00
|
|
|
{
|
2019-07-13 22:12:54 +00:00
|
|
|
m_buffer[m_offset++] = value;
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 8);
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 16);
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 24);
|
2019-07-13 22:28:30 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator>>(u32& value)
|
|
|
|
{
|
2019-08-03 17:20:05 +00:00
|
|
|
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
|
2019-07-13 22:28:30 +00:00
|
|
|
m_read_failure = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
u8 b0 = m_buffer[m_offset++];
|
|
|
|
u8 b1 = m_buffer[m_offset++];
|
|
|
|
u8 b2 = m_buffer[m_offset++];
|
|
|
|
u8 b3 = m_buffer[m_offset++];
|
|
|
|
|
|
|
|
value = 0;
|
|
|
|
value |= (u8(b3) << 24);
|
|
|
|
value |= (u8(b2) << 16);
|
2019-07-16 07:28:05 +00:00
|
|
|
value |= (u8(b1) << 8);
|
|
|
|
value |= (u8(b0));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator<<(i32 value)
|
|
|
|
{
|
|
|
|
m_buffer[m_offset++] = value;
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 8);
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 16);
|
|
|
|
m_buffer[m_offset++] = (u8)(value >> 24);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
BufferStream& operator>>(i32& value)
|
|
|
|
{
|
2019-08-03 17:20:05 +00:00
|
|
|
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
|
2019-07-16 07:28:05 +00:00
|
|
|
m_read_failure = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
u8 b0 = m_buffer[m_offset++];
|
|
|
|
u8 b1 = m_buffer[m_offset++];
|
|
|
|
u8 b2 = m_buffer[m_offset++];
|
|
|
|
u8 b3 = m_buffer[m_offset++];
|
|
|
|
|
|
|
|
value = 0;
|
|
|
|
value |= (u8(b3) << 24);
|
|
|
|
value |= (u8(b2) << 16);
|
2019-07-13 22:28:30 +00:00
|
|
|
value |= (u8(b1) << 8);
|
|
|
|
value |= (u8(b0));
|
|
|
|
return *this;
|
2018-10-24 10:43:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 17:02:48 +00:00
|
|
|
BufferStream& operator<<(const char* value)
|
|
|
|
{
|
|
|
|
return *this << StringView(value);
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:28:30 +00:00
|
|
|
BufferStream& operator<<(const StringView& value)
|
2018-10-24 10:43:52 +00:00
|
|
|
{
|
2019-02-25 21:06:55 +00:00
|
|
|
for (ssize_t i = 0; i < value.length(); ++i)
|
2018-10-24 10:43:52 +00:00
|
|
|
m_buffer[m_offset++] = value[i];
|
2019-07-13 22:28:30 +00:00
|
|
|
return *this;
|
2018-10-24 10:43:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 22:28:30 +00:00
|
|
|
BufferStream& operator<<(const ByteBuffer& value)
|
2019-02-02 22:13:12 +00:00
|
|
|
{
|
2019-02-25 21:06:55 +00:00
|
|
|
for (ssize_t i = 0; i < value.size(); ++i)
|
2019-02-02 22:13:12 +00:00
|
|
|
m_buffer[m_offset++] = value[i];
|
2019-07-13 22:28:30 +00:00
|
|
|
return *this;
|
2019-02-02 22:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool at_end() const
|
|
|
|
{
|
|
|
|
return m_offset == m_buffer.size();
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
void fill_to_end(u8 ch)
|
2018-10-24 10:43:52 +00:00
|
|
|
{
|
2019-02-02 22:13:12 +00:00
|
|
|
while (!at_end())
|
2018-10-24 10:43:52 +00:00
|
|
|
m_buffer[m_offset++] = ch;
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:06:55 +00:00
|
|
|
ssize_t offset() const { return m_offset; }
|
2018-10-24 10:43:52 +00:00
|
|
|
|
2019-03-19 15:29:06 +00:00
|
|
|
void snip()
|
|
|
|
{
|
|
|
|
m_buffer.trim(m_offset);
|
|
|
|
}
|
|
|
|
|
2019-07-16 07:32:21 +00:00
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_offset = 0;
|
|
|
|
m_read_failure = false;
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:28:30 +00:00
|
|
|
bool handle_read_failure() {
|
|
|
|
bool old = m_read_failure;
|
|
|
|
m_read_failure = false;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:43:52 +00:00
|
|
|
private:
|
|
|
|
ByteBuffer& m_buffer;
|
2019-02-25 21:06:55 +00:00
|
|
|
ssize_t m_offset { 0 };
|
2019-07-13 22:28:30 +00:00
|
|
|
bool m_read_failure { false };
|
2018-10-24 10:43:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::BufferStream;
|