-
Notifications
You must be signed in to change notification settings - Fork 0
C++ deserialization #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
native/V2StyxLib/modules/l5/include/serialization/BufferReaderImpl.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
| #include "serialization/IBufferReader.h" | ||
|
|
||
| class BufferReaderImpl : public IBufferReader { | ||
| private: | ||
| const StyxBuffer buffer; | ||
| Styx::Size position; | ||
| Styx::Size limit; | ||
| public: | ||
| BufferReaderImpl(const StyxBuffer &buf, Styx::Size size); | ||
| virtual ~BufferReaderImpl(); | ||
| uint8_t readUInt8() override; | ||
| uint16_t readUInt16() override; | ||
| uint32_t readUInt32() override; | ||
| uint64_t readUInt64() override; | ||
| StyxString readUTFString() override; | ||
| Styx::Size read(uint8_t* data, Styx::Size count) override; | ||
| }; | ||
14 changes: 14 additions & 0 deletions
14
native/V2StyxLib/modules/l5/include/serialization/IBufferReader.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #pragma once | ||
| #include <cstdint> | ||
| #include "data.h" | ||
|
|
||
| class IBufferReader { | ||
| public: | ||
| virtual ~IBufferReader() = default; | ||
| virtual uint8_t readUInt8() = 0; | ||
| virtual uint16_t readUInt16() = 0; | ||
| virtual uint32_t readUInt32() = 0; | ||
| virtual uint64_t readUInt64() = 0; | ||
| virtual StyxString readUTFString() = 0; | ||
| virtual Styx::Size read(uint8_t* data, Styx::Size count) = 0; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,92 @@ | ||||||||||||
| #include "serialization/BufferReaderImpl.h" | ||||||||||||
| #include <stdexcept> | ||||||||||||
|
|
||||||||||||
| BufferReaderImpl::BufferReaderImpl(const StyxBuffer &buf, Styx::Size size) | ||||||||||||
| : buffer(buf), position(0), limit(size) | ||||||||||||
| { | ||||||||||||
| if (buf == nullptr) { | ||||||||||||
| throw std::invalid_argument("Buffer cannot be null"); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| BufferReaderImpl::~BufferReaderImpl() | ||||||||||||
| { | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| uint8_t BufferReaderImpl::readUInt8() | ||||||||||||
| { | ||||||||||||
| if (position + sizeof(uint8_t) > limit) { | ||||||||||||
| throw std::out_of_range("Buffer overflow"); | ||||||||||||
| } | ||||||||||||
| return buffer[position++]; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| uint16_t BufferReaderImpl::readUInt16() | ||||||||||||
| { | ||||||||||||
| if (position + sizeof(uint16_t) > limit) { | ||||||||||||
| throw std::out_of_range("Buffer overflow"); | ||||||||||||
| } | ||||||||||||
| uint16_t value = static_cast<uint16_t>(buffer[position]) | | ||||||||||||
| (static_cast<uint16_t>(buffer[position + 1]) << 8); | ||||||||||||
| position += sizeof(uint16_t); | ||||||||||||
| return value; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| uint32_t BufferReaderImpl::readUInt32() | ||||||||||||
| { | ||||||||||||
| if (position + sizeof(uint32_t) > limit) { | ||||||||||||
| throw std::out_of_range("Buffer overflow"); | ||||||||||||
| } | ||||||||||||
| uint32_t value = static_cast<uint32_t>(buffer[position]) | | ||||||||||||
| (static_cast<uint32_t>(buffer[position + 1]) << 8) | | ||||||||||||
| (static_cast<uint32_t>(buffer[position + 2]) << 16) | | ||||||||||||
| (static_cast<uint32_t>(buffer[position + 3]) << 24); | ||||||||||||
| position += sizeof(uint32_t); | ||||||||||||
| return value; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| uint64_t BufferReaderImpl::readUInt64() | ||||||||||||
| { | ||||||||||||
| if (position + sizeof(uint64_t) > limit) { | ||||||||||||
| throw std::out_of_range("Buffer overflow"); | ||||||||||||
| } | ||||||||||||
| uint64_t value = static_cast<uint64_t>(buffer[position]) | | ||||||||||||
| (static_cast<uint64_t>(buffer[position + 1]) << 8) | | ||||||||||||
| (static_cast<uint64_t>(buffer[position + 2]) << 16) | | ||||||||||||
| (static_cast<uint64_t>(buffer[position + 3]) << 24) | | ||||||||||||
| (static_cast<uint64_t>(buffer[position + 4]) << 32) | | ||||||||||||
| (static_cast<uint64_t>(buffer[position + 5]) << 40) | | ||||||||||||
| (static_cast<uint64_t>(buffer[position + 6]) << 48) | | ||||||||||||
| (static_cast<uint64_t>(buffer[position + 7]) << 56); | ||||||||||||
| position += sizeof(uint64_t); | ||||||||||||
| return value; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| StyxString BufferReaderImpl::readUTFString() | ||||||||||||
| { | ||||||||||||
| if (position + sizeof(uint16_t) > limit) { | ||||||||||||
| throw std::out_of_range("Buffer overflow"); | ||||||||||||
| } | ||||||||||||
| uint16_t length = readUInt16(); | ||||||||||||
| if (position + length > limit) { | ||||||||||||
| throw std::out_of_range("Buffer overflow"); | ||||||||||||
| } | ||||||||||||
| StyxString result; | ||||||||||||
| result.reserve(length); | ||||||||||||
| for (uint16_t i = 0; i < length; ++i) { | ||||||||||||
| result += static_cast<char>(buffer[position++]); | ||||||||||||
| } | ||||||||||||
| return result; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Styx::Size BufferReaderImpl::read(uint8_t* data, Styx::Size count) | ||||||||||||
| { | ||||||||||||
| if (position + count > limit) { | ||||||||||||
| throw std::out_of_range("Buffer overflow"); | ||||||||||||
| } | ||||||||||||
| for (Styx::Size i = 0; i < count; ++i) { | ||||||||||||
| data[i] = buffer[position++]; | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+88
to
+90
|
||||||||||||
| for (Styx::Size i = 0; i < count; ++i) { | |
| data[i] = buffer[position++]; | |
| } | |
| std::memcpy(data, buffer + position, count); | |
| position += count; |
37 changes: 37 additions & 0 deletions
37
native/V2StyxLib/modules/l5/test/test_BufferReaderImpl.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #include <catch2/catch_test_macros.hpp> | ||
| #include "serialization/BufferReaderImpl.h" | ||
|
|
||
| TEST_CASE("readUTFString", "[BufferReaderImpl]") { | ||
| uint8_t testData[] = {0x05, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G'}; | ||
| BufferReaderImpl impl(testData, sizeof(testData)); | ||
| REQUIRE("ABCDE" == impl.readUTFString()); | ||
| } | ||
|
|
||
| TEST_CASE("readUInt8", "[BufferReaderImpl]") { | ||
| uint8_t testData[] = {0x05, 0x06}; | ||
| BufferReaderImpl impl(testData, sizeof(testData)); | ||
| REQUIRE(0x05 == impl.readUInt8()); | ||
| REQUIRE(0x06 == impl.readUInt8()); | ||
| } | ||
|
|
||
| TEST_CASE("readUInt16", "[BufferReaderImpl]") { | ||
| uint8_t testData[] = {0x05, 0x06, 0x07, 0x08}; | ||
| BufferReaderImpl impl(testData, sizeof(testData)); | ||
| REQUIRE(0x0605 == impl.readUInt16()); | ||
| REQUIRE(0x0807 == impl.readUInt16()); | ||
| } | ||
|
|
||
| TEST_CASE("readUInt32", "[BufferReaderImpl]") { | ||
| uint8_t testData[] = {0x05, 0x06, 0x07, 0x08, 0x10, 0x11, 0x12, 0x13}; | ||
| BufferReaderImpl impl(testData, sizeof(testData)); | ||
| REQUIRE(0x08070605 == impl.readUInt32()); | ||
| REQUIRE(0x13121110 == impl.readUInt32()); | ||
| } | ||
|
|
||
| TEST_CASE("readUInt64", "[BufferReaderImpl]") { | ||
| uint8_t testData[] = {0x05, 0x06, 0x07, 0x08, 0x10, 0x11, 0x12, 0x13, | ||
| 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27}; | ||
| BufferReaderImpl impl(testData, sizeof(testData)); | ||
| REQUIRE(0x1312111008070605ULL == impl.readUInt64()); | ||
| REQUIRE(0x2726252423222120ULL == impl.readUInt64()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The destructor is empty; you can simplify by defaulting it (
~BufferReaderImpl() override = default;) to reduce boilerplate.