From 1502b61d080102e27a477b017aed2f089d36f121 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Sat, 17 Oct 2020 02:12:05 +0200 Subject: [PATCH] Parsing uint and nint --- include/gp/enveloppe/cbor.hpp | 90 +++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/include/gp/enveloppe/cbor.hpp b/include/gp/enveloppe/cbor.hpp index 0b3dc45..9289258 100644 --- a/include/gp/enveloppe/cbor.hpp +++ b/include/gp/enveloppe/cbor.hpp @@ -68,6 +68,11 @@ namespace gp { : sign{v < 0} , value{uint64_t((sign ? -1 : 1) * v)} {} + + cbor_number(bool s, uint64_t v) + : sign{s} + , value{v} + {} }; struct ieee754_hf final { @@ -112,6 +117,11 @@ namespace gp { , alloc(alloc_v) {} + cbor_value(cbor_composite val, allocator& alloc_v) + : contents(val) + , alloc(alloc_v) + {} + cbor_value(const cbor_value& oth) : contents(oth.contents) , alloc(oth.alloc) @@ -159,6 +169,86 @@ namespace gp { return gp::vector>{alloc}; } + static gp::pair::associated_iterator> decode(allocator& alloc, gp::buffer src) { + #define ERROR return {cbor_value{undefined_t{}, alloc}, src.begin()} + if(src.size()==0) ERROR; + auto discriminant = (cbor_type)(((uint8_t)*src.begin()) >> 5); + auto local = uint8_t(((uint8_t)*src.begin()) & 0b00011111); + switch(discriminant) { + case cbor_type::uint: + case cbor_type::nint: { + if(local <= 23) { + return {cbor_value{cbor_number{(bool)discriminant, local}, alloc}, src.begin()+1}; + } else { + switch((cbor_oths)local) { + case cbor_oths::byte: { + if(src.size()<2) ERROR; + return { + cbor_value{ + cbor_number{(bool)discriminant, (uint8_t)*(src.begin()+1)}, + alloc + }, + src.begin()+2 + }; + } + case cbor_oths::word: { + if(src.size()<3) ERROR; + return { + cbor_value{ + cbor_number{(bool)discriminant, uint16_t(*(gp::endian_wrapper*)(src.begin().data))}, + alloc + }, + src.begin()+3 + }; + } + case cbor_oths::dword: { + if(src.size()<5) ERROR; + return { + cbor_value{ + cbor_number{(bool)discriminant, uint32_t(*(gp::endian_wrapper*)(src.begin().data))}, + alloc + }, + src.begin()+5 + }; + } + case cbor_oths::qword: { + if(src.size()<9) ERROR; + return { + cbor_value{ + cbor_number{(bool)discriminant, uint64_t(*(gp::endian_wrapper*)(src.begin().data))}, + alloc + }, + src.begin()+9 + }; + } + default: { + ERROR; + } + } + } + } + case cbor_type::bstr: { + + } + case cbor_type::tstr: { + ERROR; + } + case cbor_type::list: { + + } + case cbor_type::hmap: { + + } + case cbor_type::tags: { + + } + case cbor_type::oths: { + + } + } + ERROR; + } + static auto encode_float(buffer dest, cbor_floating_point& value) { switch(value.type()) { case cbor_floating_point::alt():{