diff --git a/common.go b/common.go index f004a74b..4e11f0f5 100644 --- a/common.go +++ b/common.go @@ -47,11 +47,21 @@ func (t cborType) String() string { type additionalInformation uint8 const ( - maxAdditionalInformationWithoutArgument = 23 - additionalInformationWith1ByteArgument = 24 - additionalInformationWith2ByteArgument = 25 - additionalInformationWith4ByteArgument = 26 - additionalInformationWith8ByteArgument = 27 + maxAdditionalInformationWithoutArgument = 23 + additionalInformationWith1ByteArgument = 24 + additionalInformationWith2ByteArgument = 25 + additionalInformationWith4ByteArgument = 26 + additionalInformationWith8ByteArgument = 27 + + // additional information with major type 7 + additionalInformationAsFalse = 20 + additionalInformationAsTrue = 21 + additionalInformationAsNull = 22 + additionalInformationAsUndefined = 23 + additionalInformationAsFloat16 = 25 + additionalInformationAsFloat32 = 26 + additionalInformationAsFloat64 = 27 + additionalInformationAsIndefiniteLengthFlag = 31 ) diff --git a/decode.go b/decode.go index 1d92ddb5..ea943977 100644 --- a/decode.go +++ b/decode.go @@ -1444,15 +1444,18 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin case cborTypePrimitives: _, ai, val := d.getHead() switch ai { - case 25: + case additionalInformationAsFloat16: f := float64(float16.Frombits(uint16(val)).Float32()) return fillFloat(t, f, v) - case 26: + + case additionalInformationAsFloat32: f := float64(math.Float32frombits(uint32(val))) return fillFloat(t, f, v) - case 27: + + case additionalInformationAsFloat64: f := math.Float64frombits(val) return fillFloat(t, f, v) + default: // ai <= 24 if d.dm.simpleValues.rejected[SimpleValue(val)] { return &UnacceptableDataItemError{ @@ -1462,10 +1465,14 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin } switch ai { - case 20, 21: - return fillBool(t, ai == 21, v) - case 22, 23: + case additionalInformationAsFalse, + additionalInformationAsTrue: + return fillBool(t, ai == additionalInformationAsTrue, v) + + case additionalInformationAsNull, + additionalInformationAsUndefined: return fillNil(t, v) + default: return fillPositiveInt(t, val, v) } @@ -1664,12 +1671,15 @@ func (d *decoder) parseToTime() (time.Time, bool, error) { _, ai, val := d.getHead() var f float64 switch ai { - case 25: + case additionalInformationAsFloat16: f = float64(float16.Frombits(uint16(val)).Float32()) - case 26: + + case additionalInformationAsFloat32: f = float64(math.Float32frombits(uint32(val))) - case 27: + + case additionalInformationAsFloat64: f = math.Float64frombits(val) + default: return time.Time{}, false, &UnmarshalTypeError{CBORType: t.String(), GoType: typeTime.String()} } @@ -1931,6 +1941,7 @@ func (d *decoder) parse(skipSelfDescribedTag bool) (interface{}, error) { //noli return content, nil } return Tag{tagNum, content}, nil + case cborTypePrimitives: _, ai, val := d.getHead() if ai <= 24 && d.dm.simpleValues.rejected[SimpleValue(val)] { @@ -1943,20 +1954,27 @@ func (d *decoder) parse(skipSelfDescribedTag bool) (interface{}, error) { //noli return SimpleValue(val), nil } switch ai { - case 20, 21: - return (ai == 21), nil - case 22, 23: + case additionalInformationAsFalse, + additionalInformationAsTrue: + return (ai == additionalInformationAsTrue), nil + + case additionalInformationAsNull, + additionalInformationAsUndefined: return nil, nil - case 25: + + case additionalInformationAsFloat16: f := float64(float16.Frombits(uint16(val)).Float32()) return f, nil - case 26: + + case additionalInformationAsFloat32: f := float64(math.Float32frombits(uint32(val))) return f, nil - case 27: + + case additionalInformationAsFloat64: f := math.Float64frombits(val) return f, nil } + case cborTypeArray: return d.parseArray() case cborTypeMap: diff --git a/diagnose.go b/diagnose.go index 800f25c9..05850f1a 100644 --- a/diagnose.go +++ b/diagnose.go @@ -441,23 +441,25 @@ func (di *diagnose) item() error { //nolint:gocyclo case cborTypePrimitives: _, ai, val := di.d.getHead() switch ai { - case 20: + case additionalInformationAsFalse: di.w.WriteString("false") return nil - case 21: + case additionalInformationAsTrue: di.w.WriteString("true") return nil - case 22: + case additionalInformationAsNull: di.w.WriteString("null") return nil - case 23: + case additionalInformationAsUndefined: di.w.WriteString("undefined") return nil - case 25, 26, 27: + case additionalInformationAsFloat16, + additionalInformationAsFloat32, + additionalInformationAsFloat64: return di.encodeFloat(ai, val) default: @@ -628,7 +630,7 @@ func (di *diagnose) encodeTextString(val string, quote byte) error { func (di *diagnose) encodeFloat(ai byte, val uint64) error { f64 := float64(0) switch ai { - case 25: + case additionalInformationAsFloat16: f16 := float16.Frombits(uint16(val)) switch { case f16.IsNaN(): @@ -644,7 +646,7 @@ func (di *diagnose) encodeFloat(ai byte, val uint64) error { f64 = float64(f16.Float32()) } - case 26: + case additionalInformationAsFloat32: f32 := math.Float32frombits(uint32(val)) switch { case f32 != f32: @@ -660,7 +662,7 @@ func (di *diagnose) encodeFloat(ai byte, val uint64) error { f64 = float64(f32) } - case 27: + case additionalInformationAsFloat64: f64 = math.Float64frombits(val) switch { case f64 != f64: @@ -703,13 +705,15 @@ func (di *diagnose) encodeFloat(ai byte, val uint64) error { if di.dm.floatPrecisionIndicator { switch ai { - case 25: + case additionalInformationAsFloat16: di.w.WriteString("_1") return nil - case 26: + + case additionalInformationAsFloat32: di.w.WriteString("_2") return nil - case 27: + + case additionalInformationAsFloat64: di.w.WriteString("_3") return nil }