From 73d400f7b3ef618c819ad4c603ea3b4b7abb61bd Mon Sep 17 00:00:00 2001 From: Luc Talatinian Date: Fri, 12 Jan 2024 15:42:57 -0500 Subject: [PATCH] decode major7 floats --- encoding/cbor/decode.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/encoding/cbor/decode.go b/encoding/cbor/decode.go index 71e1f84b2..5e174e68b 100644 --- a/encoding/cbor/decode.go +++ b/encoding/cbor/decode.go @@ -3,6 +3,7 @@ package cbor import ( "encoding/binary" "fmt" + "math" ) const minorIndefinite = 31 @@ -254,11 +255,23 @@ func decodeMajor7(p []byte) (Value, int, error) { case major7Undefined: return &Major7Undefined{}, 1, nil case major7Float16: - return nil, 0, fmt.Errorf("TODO") + if len(p) < 3 { + return nil, 0, fmt.Errorf("encoded float16 at end of buffer") + } + b := binary.BigEndian.Uint16(p[1:]) + return Major7Float32(math.Float32frombits(float16to32(b))), 3, nil case major7Float32: - return nil, 0, fmt.Errorf("TODO") + if len(p) < 5 { + return nil, 0, fmt.Errorf("encoded float32 at end of buffer") + } + b := binary.BigEndian.Uint32(p[1:]) + return Major7Float32(math.Float32frombits(b)), 5, nil case major7Float64: - return nil, 0, fmt.Errorf("TODO") + if len(p) < 9 { + return nil, 0, fmt.Errorf("encoded float64 at end of buffer") + } + b := binary.BigEndian.Uint64(p[1:]) + return Major7Float64(math.Float64frombits(b)), 9, nil default: return nil, 0, fmt.Errorf("unexpected minor value %d", m) }