diff --git a/include/gp/ipc/envelope/cbor.hpp b/include/gp/ipc/envelope/cbor.hpp index 00e967f..818ce19 100644 --- a/include/gp/ipc/envelope/cbor.hpp +++ b/include/gp/ipc/envelope/cbor.hpp @@ -8,7 +8,6 @@ #include #include -#include // TODO: Rewrite in a more ORM-like way // TODO: Implement some bignum for support @@ -352,13 +351,12 @@ namespace gp { if(!state.size()) return {nullopt, state}; auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5); + switch(type) { case cbor_type::oths: { const auto[value, new_state] = pull_arbitrary_integer_from_cbor(state); if(!value.has_value()) break; - if(new_state.size() return_value{alloc}; if(value.value() == 22) { return {optional(nullptr), parsing_state(new_state.begin()+1, new_state.end())}; @@ -370,6 +368,32 @@ namespace gp { return {nullopt, state}; } + template<> + inline gp::pair, parsing_state> read_cbor(parsing_state state, gp::allocator& alloc) { + if(!state.size()) return {nullopt, state}; + auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5); + + + switch(type) { + case cbor_type::oths: + { + const auto[value, new_state] = pull_arbitrary_integer_from_cbor(state); + if(!value.has_value()) break; + if(value.value() == 20) + { + return {false, parsing_state(new_state.begin()+1, new_state.end())}; + } + else if(value.value() == 21) + { + return {true, parsing_state(new_state.begin()+1, new_state.end())}; + } + break; + } + default: break; + } + return {nullopt, state}; + } + template<> inline gp::pair, parsing_state> read_cbor(parsing_state state, gp::allocator& alloc) { if(!state.size()) return {nullopt, state}; @@ -380,8 +404,6 @@ namespace gp { { const auto[value, new_state] = pull_arbitrary_integer_from_cbor(state); if(!value.has_value()) break; - if(new_state.size() return_value{alloc}; if(value.value() == 23) { return {optional(cbor_undefined{}), parsing_state(new_state.begin()+1, new_state.end())}; diff --git a/tests/cbor_test.cpp b/tests/cbor_test.cpp index abcbd34..e74be42 100644 --- a/tests/cbor_test.cpp +++ b/tests/cbor_test.cpp @@ -384,7 +384,7 @@ struct cbor_test12 : public generic_cbor_test { auto [value, state] = gp::read_cbor(serialized.as_buffer(), alloc); - gp_config::assertion(value.has_value(), "could not encode"); + gp_config::assertion(value.has_value(), "could not decode"); gp_config::assertion(value.value() == nullptr, "data did not decode correctly"); } @@ -461,12 +461,14 @@ struct cbor_test15 : public generic_cbor_test { { gp::vector serialized(alloc); gp::array serialized_manual{ - char(0b11100000+24) + char(0b11100000+23) }; gp::push_as_cbor(serialized, gp::cbor_undefined{}); gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode"); + + log_segment("value", std::to_string((uint8_t)serialized[0]).c_str()); gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly"); auto [value, state] = gp::read_cbor(serialized.as_buffer(), alloc); @@ -505,7 +507,7 @@ struct cbor_test16 : public generic_cbor_test { gp::push_as_cbor(serialized, str.as_buffer()); - gp_config::assertion(serialized.size() != serialized_manual.size(), "could not encode"); + gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode"); for(size_t idx = 0; idx < serialized_manual.size(); idx++) { gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly"); }