summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorjoshua stein <jcs@jcs.org>2020-02-02 00:56:03 -0600
committerAndreas Kling <kling@serenityos.org>2020-02-05 18:39:45 +0100
commit0c4c5b5eb74aa40cecd7fa58c7de3ca0076c1565 (patch)
treea5d5eef12af8720c8df68630be9e65f36faf19bc /AK
parentb4568b1422c05ae1b50e6c96d262bb44b8a2f3d8 (diff)
downloadserenity-0c4c5b5eb74aa40cecd7fa58c7de3ca0076c1565.zip
AK: Support 64-bit integers in BufferStream
Diffstat (limited to 'AK')
-rw-r--r--AK/BufferStream.h121
1 files changed, 115 insertions, 6 deletions
diff --git a/AK/BufferStream.h b/AK/BufferStream.h
index a1f3eda21e..653e36dc86 100644
--- a/AK/BufferStream.h
+++ b/AK/BufferStream.h
@@ -174,12 +174,6 @@ public:
m_buffer[m_offset++] = (u8)(value >> 24);
return *this;
}
-
- BufferStream& operator<<(size_t value)
- {
- return *this << (u32)value;
- }
-
BufferStream& operator>>(u32& value)
{
if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
@@ -198,6 +192,7 @@ public:
value |= (u8(b0));
return *this;
}
+
BufferStream& operator<<(i32 value)
{
m_buffer[m_offset++] = value;
@@ -225,6 +220,120 @@ public:
return *this;
}
+ BufferStream& operator<<(u64 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);
+ m_buffer[m_offset++] = (u8)(value >> 32);
+ m_buffer[m_offset++] = (u8)(value >> 40);
+ m_buffer[m_offset++] = (u8)(value >> 48);
+ m_buffer[m_offset++] = (u8)(value >> 56);
+ return *this;
+ }
+ BufferStream& operator>>(u64& value)
+ {
+ if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
+ 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++];
+ u8 b4 = m_buffer[m_offset++];
+ u8 b5 = m_buffer[m_offset++];
+ u8 b6 = m_buffer[m_offset++];
+ u8 b7 = m_buffer[m_offset++];
+
+ value = 0;
+ value |= ((long long)b7 << 56);
+ value |= ((long long)b6 << 48);
+ value |= ((long long)b5 << 40);
+ value |= ((long long)b4 << 32);
+ value |= ((long long)b3 << 24);
+ value |= ((long long)b2 << 16);
+ value |= ((long long)b1 << 8);
+ value |= ((long long)b0);
+ return *this;
+ }
+ BufferStream& operator<<(i64 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);
+ m_buffer[m_offset++] = (u8)(value >> 32);
+ m_buffer[m_offset++] = (u8)(value >> 40);
+ m_buffer[m_offset++] = (u8)(value >> 48);
+ m_buffer[m_offset++] = (u8)(value >> 56);
+ return *this;
+ }
+ BufferStream& operator>>(i64& value)
+ {
+ if (m_offset + sizeof(value) > unsigned(m_buffer.size())) {
+ 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++];
+ u8 b4 = m_buffer[m_offset++];
+ u8 b5 = m_buffer[m_offset++];
+ u8 b6 = m_buffer[m_offset++];
+ u8 b7 = m_buffer[m_offset++];
+
+ value = 0;
+ value |= ((long long)b7 << 56);
+ value |= ((long long)b6 << 48);
+ value |= ((long long)b5 << 40);
+ value |= ((long long)b4 << 32);
+ value |= ((long long)b3 << 24);
+ value |= ((long long)b2 << 16);
+ value |= ((long long)b1 << 8);
+ value |= ((long long)b0);
+ return *this;
+ }
+
+ BufferStream& operator<<(size_t value)
+ {
+ if constexpr(sizeof(size_t) == 4)
+ return *this << (u32)value;
+ else if constexpr(sizeof(size_t) == 8)
+ return *this << (u64)value;
+ ASSERT_NOT_REACHED();
+ }
+ BufferStream& operator>>(size_t& value)
+ {
+ if constexpr(sizeof(size_t) == 4)
+ return *this >> (u32&)value;
+ else if constexpr(sizeof(size_t) == 8)
+ return *this >> (u64&)value;
+ ASSERT_NOT_REACHED();
+ }
+
+#ifndef __i386__
+ BufferStream& operator<<(ssize_t value)
+ {
+ if constexpr(sizeof(ssize_t) == 4)
+ return *this << (i32)value;
+ else if constexpr(sizeof(ssize_t) == 8)
+ return *this << (i64)value;
+ ASSERT_NOT_REACHED();
+ }
+ BufferStream& operator>>(ssize_t& value)
+ {
+ if constexpr(sizeof(ssize_t) == 4)
+ return *this >> (i32&)value;
+ else if constexpr(sizeof(ssize_t) == 8)
+ return *this >> (i64&)value;
+ ASSERT_NOT_REACHED();
+ }
+#endif
+
+
BufferStream& operator<<(const char* value)
{
return *this << StringView(value);