-
Notifications
You must be signed in to change notification settings - Fork 428
Reorg rlp parse*_metadata functions
#2222
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
|
|
||
| #include <boost/outcome/try.hpp> | ||
|
|
||
| #include <utility> | ||
|
|
||
| MONAD_RLP_NAMESPACE_BEGIN | ||
|
|
||
| template <unsigned_integral T> | ||
|
|
@@ -56,90 +58,116 @@ constexpr Result<size_t> decode_length(byte_string_view const enc) | |
| return decode_raw_num<size_t>(enc); | ||
| } | ||
|
|
||
| constexpr Result<byte_string_view> parse_string_metadata(byte_string_view &enc) | ||
| enum class RlpType | ||
| { | ||
| size_t i = 0; | ||
| size_t end = 0; | ||
| String, | ||
| List | ||
| }; | ||
|
|
||
| if (MONAD_UNLIKELY(enc.empty())) { | ||
| return DecodeError::InputTooShort; | ||
| } | ||
| namespace detail | ||
| { | ||
| [[gnu::always_inline]] inline constexpr Result< | ||
| std::pair<RlpType, byte_string_view>> | ||
| parse_metadata_raw(byte_string_view &enc) | ||
| { | ||
| size_t i = 0; | ||
| size_t length; | ||
| RlpType type; | ||
|
|
||
| if (MONAD_UNLIKELY(enc[0] >= 0xc0)) { | ||
| return DecodeError::TypeUnexpected; | ||
| } | ||
| MONAD_DEBUG_ASSERT(!enc.empty()) | ||
|
|
||
| if (enc[0] < 0x80) // [0x00, 0x7f] | ||
| { | ||
| end = i + 1; | ||
| } | ||
| else if (enc[0] < 0xb8) // [0x80, 0xb7] | ||
| { | ||
| ++i; | ||
| uint8_t const length = enc[0] - 0x80; | ||
| end = i + length; | ||
| } | ||
| else // [0xb8, 0xbf] | ||
| { | ||
| ++i; | ||
| uint8_t const length_of_length = enc[0] - 0xb7; | ||
| if (enc[0] < 0x80) // [0x00, 0x7f] | ||
| { | ||
| type = RlpType::String; | ||
| length = 1; | ||
| } | ||
| else if (enc[0] < 0xb8) // [0x80, 0xb7] | ||
| { | ||
| type = RlpType::String; | ||
| length = enc[0] - 0x80; | ||
| ++i; | ||
| } | ||
| else if (enc[0] < 0xc0) // [0xb8, 0xbf] | ||
| { | ||
| type = RlpType::String; | ||
| uint8_t const length_of_length = enc[0] - 0xb7; | ||
| ++i; | ||
| if (MONAD_UNLIKELY(i + length_of_length >= enc.size())) { | ||
| return DecodeError::InputTooShort; | ||
| } | ||
| BOOST_OUTCOME_TRY( | ||
| length, decode_length(enc.substr(i, length_of_length))); | ||
| i += length_of_length; | ||
| } | ||
| else if (enc[0] < 0xf8) // [0xc0, 0xf7] | ||
| { | ||
| type = RlpType::List; | ||
| length = enc[0] - 0xc0; | ||
| ++i; | ||
| } | ||
| else // [0xf8, 0xff] | ||
| { | ||
| type = RlpType::List; | ||
| size_t const length_of_length = enc[0] - 0xf7; | ||
| ++i; | ||
| if (MONAD_UNLIKELY(i + length_of_length >= enc.size())) { | ||
| return DecodeError::InputTooShort; | ||
| } | ||
| BOOST_OUTCOME_TRY( | ||
| length, decode_length(enc.substr(i, length_of_length))); | ||
| i += length_of_length; | ||
| } | ||
|
|
||
| auto const end = i + length; | ||
|
|
||
| if (MONAD_UNLIKELY(i + length_of_length >= enc.size())) { | ||
| if (MONAD_UNLIKELY(end > enc.size() || end < i)) { | ||
| return DecodeError::InputTooShort; | ||
| } | ||
|
|
||
| BOOST_OUTCOME_TRY( | ||
| auto const length, decode_length(enc.substr(i, length_of_length))); | ||
| i += length_of_length; | ||
| end = i + length; | ||
| auto const payload = enc.substr(i, length); | ||
| enc = enc.substr(end); | ||
| return std::pair{type, payload}; | ||
| } | ||
| } | ||
|
|
||
| if (MONAD_UNLIKELY(end > enc.size() || end < i)) { | ||
| constexpr Result<std::pair<RlpType, byte_string_view>> | ||
| parse_metadata(byte_string_view &enc) | ||
|
Comment on lines
+133
to
+134
|
||
| { | ||
| if (MONAD_UNLIKELY(enc.empty())) { | ||
| return DecodeError::InputTooShort; | ||
| } | ||
|
|
||
| auto const payload = enc.substr(i, end - i); | ||
| enc = enc.substr(end); | ||
| return payload; | ||
| return detail::parse_metadata_raw(enc); | ||
| } | ||
|
goodlyrottenapple marked this conversation as resolved.
|
||
|
|
||
| constexpr Result<byte_string_view> parse_list_metadata(byte_string_view &enc) | ||
| constexpr Result<byte_string_view> parse_string_metadata(byte_string_view &enc) | ||
| { | ||
| size_t i = 0; | ||
| size_t length; | ||
| ++i; | ||
|
|
||
| if (MONAD_UNLIKELY(enc.empty())) { | ||
| return DecodeError::InputTooShort; | ||
| } | ||
|
|
||
| if (MONAD_UNLIKELY(enc[0] < 0xc0)) { | ||
| if (MONAD_UNLIKELY(enc[0] >= 0xc0)) { | ||
| return DecodeError::TypeUnexpected; | ||
| } | ||
|
|
||
| if (enc[0] < 0xf8) { | ||
| length = enc[0] - 0xc0; | ||
| } | ||
| else { | ||
| size_t const length_of_length = enc[0] - 0xf7; | ||
|
|
||
| if (MONAD_UNLIKELY(i + length_of_length >= enc.size())) { | ||
| return DecodeError::InputTooShort; | ||
| } | ||
| BOOST_OUTCOME_TRY(auto const result, detail::parse_metadata_raw(enc)); | ||
| MONAD_DEBUG_ASSERT(result.first == RlpType::String); | ||
| return result.second; | ||
| } | ||
|
|
||
| BOOST_OUTCOME_TRY( | ||
| length, decode_length(enc.substr(i, length_of_length))); | ||
| i += length_of_length; | ||
| constexpr Result<byte_string_view> parse_list_metadata(byte_string_view &enc) | ||
| { | ||
| if (MONAD_UNLIKELY(enc.empty())) { | ||
| return DecodeError::InputTooShort; | ||
| } | ||
| auto const end = i + length; | ||
|
|
||
| if (MONAD_UNLIKELY(end > enc.size() || end < i)) { | ||
| return DecodeError::InputTooShort; | ||
| if (MONAD_UNLIKELY(enc[0] < 0xc0)) { | ||
| return DecodeError::TypeUnexpected; | ||
| } | ||
|
|
||
| auto const payload = enc.substr(i, end - i); | ||
| enc = enc.substr(end); | ||
| return payload; | ||
| BOOST_OUTCOME_TRY(auto const result, detail::parse_metadata_raw(enc)); | ||
| MONAD_DEBUG_ASSERT(result.first == RlpType::List); | ||
| return result.second; | ||
| } | ||
|
|
||
| constexpr Result<byte_string_view> decode_string(byte_string_view &enc) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.