From f43009a3e21d84b477529b509c349c7d22e7b178 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Fri, 5 Mar 2021 16:53:17 -0500 Subject: [PATCH] Add unsigned to streambuffer --- luprex/core/cpp/streambuffer.cpp | 12 +++++++----- luprex/core/cpp/streambuffer.hpp | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/luprex/core/cpp/streambuffer.cpp b/luprex/core/cpp/streambuffer.cpp index d3551627..677aac64 100644 --- a/luprex/core/cpp/streambuffer.cpp +++ b/luprex/core/cpp/streambuffer.cpp @@ -19,11 +19,12 @@ StreamBuffer::StreamBuffer() { } StreamBuffer::StreamBuffer(int64_t size, bool fixed) { - assert(size > 0); + assert(size >= 0); init(fixed, true, (char*)malloc(size), size); } StreamBuffer::StreamBuffer(const char *s, int64_t size) { + assert(size >= 0); init(true, false, const_cast(s), size); } @@ -150,11 +151,11 @@ void StreamBuffer::write_ztbytes(const char *s) { void StreamBuffer::write_string(const std::string &s) { if (s.size() >= 255) { - write_int8(0xFF); - write_int64(s.size()); + write_uint8(0xFF); + write_uint64(s.size()); write_bytes(s.c_str(), s.size()); } else { - write_int8(s.size()); + write_uint8(s.size()); write_bytes(s.c_str(), s.size()); } } @@ -219,10 +220,11 @@ const char *StreamBuffer::read_bytes(int64_t bytes) { } std::string StreamBuffer::read_string(int64_t max_allowed) { - int64_t len = read_int8(); + int64_t len = read_uint8(); if (len == 255) { len = read_int64(); } + if (len < 0) throw StreamCorruption(); if (len > max_allowed) throw StreamCorruption(); const char *bytes = read_bytes(len); return std::string(bytes, len); diff --git a/luprex/core/cpp/streambuffer.hpp b/luprex/core/cpp/streambuffer.hpp index 2c671995..616a50f5 100644 --- a/luprex/core/cpp/streambuffer.hpp +++ b/luprex/core/cpp/streambuffer.hpp @@ -313,21 +313,34 @@ public: void write_int16(int16_t v); void write_int32(int32_t v); void write_int64(int64_t v); + void write_uint8(uint8_t v) { write_int8(v); } + void write_uint16(uint16_t v) { write_int16(v); } + void write_uint32(uint32_t v) { write_int32(v); } + void write_uint64(uint64_t v) { write_int64(v); } + void write_bytes(const char *bytes, int64_t len); void write_string(const std::string &s); void write_ztbytes(const char *bytes); // Overwrite values previously written to the buffer. - void overwrite_int8(int64_t write_count_after, int8_t value); - void overwrite_int16(int64_t write_count_after, int16_t value); - void overwrite_int32(int64_t write_count_after, int32_t value); - void overwrite_int64(int64_t write_count_after, int64_t value); + void overwrite_int8(int64_t write_count_after, int8_t v); + void overwrite_int16(int64_t write_count_after, int16_t v); + void overwrite_int32(int64_t write_count_after, int32_t v); + void overwrite_int64(int64_t write_count_after, int64_t v); + void overwrite_uint8(int64_t write_count_after, uint8_t v) { overwrite_int8(write_count_after, v); } + void overwrite_uint16(int64_t write_count_after, uint16_t v) { overwrite_int16(write_count_after, v); } + void overwrite_uint32(int64_t write_count_after, uint32_t v) { overwrite_int32(write_count_after, v); } + void overwrite_uint64(int64_t write_count_after, uint64_t v) { overwrite_int64(write_count_after, v); } // Read integers from the buffer. May throw StreamEof. int8_t read_int8(); int16_t read_int16(); int32_t read_int32(); int64_t read_int64(); + uint8_t read_uint8() { return read_int8(); } + uint16_t read_uint16() { return read_int16(); } + uint32_t read_uint32() { return read_int32(); } + uint64_t read_uint64() { return read_int64(); } // Read a string of no more than the specified length. May throw StreamEof // or StreamCorruption.