|
@ -68,6 +68,11 @@ namespace gp { |
|
|
: sign{v < 0} |
|
|
: sign{v < 0} |
|
|
, value{uint64_t((sign ? -1 : 1) * v)} |
|
|
, value{uint64_t((sign ? -1 : 1) * v)} |
|
|
{} |
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
cbor_number(bool s, uint64_t v) |
|
|
|
|
|
: sign{s} |
|
|
|
|
|
, value{v} |
|
|
|
|
|
{} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
struct ieee754_hf final { |
|
|
struct ieee754_hf final { |
|
@ -112,6 +117,11 @@ namespace gp { |
|
|
, alloc(alloc_v) |
|
|
, alloc(alloc_v) |
|
|
{} |
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
cbor_value(cbor_composite<cbor_value> val, allocator& alloc_v) |
|
|
|
|
|
: contents(val) |
|
|
|
|
|
, alloc(alloc_v) |
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
cbor_value(const cbor_value& oth) |
|
|
cbor_value(const cbor_value& oth) |
|
|
: contents(oth.contents) |
|
|
: contents(oth.contents) |
|
|
, alloc(oth.alloc) |
|
|
, alloc(oth.alloc) |
|
@ -159,6 +169,86 @@ namespace gp { |
|
|
return gp::vector<gp::pair<cbor_value, cbor_value>>{alloc}; |
|
|
return gp::vector<gp::pair<cbor_value, cbor_value>>{alloc}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static gp::pair<cbor_value, gp::buffer<std::byte>::associated_iterator> decode(allocator& alloc, gp::buffer<std::byte> 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<uint16_t, endian::big>*)(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<uint32_t, endian::big>*)(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<uint64_t, endian::big>*)(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<std::byte> dest, cbor_floating_point& value) { |
|
|
static auto encode_float(buffer<std::byte> dest, cbor_floating_point& value) { |
|
|
switch(value.type()) { |
|
|
switch(value.type()) { |
|
|
case cbor_floating_point::alt<ieee754_hf>():{ |
|
|
case cbor_floating_point::alt<ieee754_hf>():{ |
|
|