Skip to content

Commit ea51617

Browse files
committed
Clarifications.
1 parent e2a986b commit ea51617

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

draft-mcnally-deterministic-cbor.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,29 @@ dCBOR codecs MUST validate and return errors for any CBOR that is not conformant
115115

116116
## Reduction of Floating Point Values to Integers
117117

118-
While there is no requirement that dCBOR codecs implement support for floating point numbers, dCBOR codecs that do support them MUST reduce floating point values with no fractional part to the smallest integer value that can accurately represent it. If a numeric value has a fractional part or an exponent that takes it out of the range of representable integers, then it SHALL be encoded as a floating point value.
118+
While there is no requirement that dCBOR codecs implement support for floating point numbers, dCBOR codecs that do support them MUST reduce floating point values with no fractional part to the integer value that can accurately represent it in the fewest bits. If a numeric value has a fractional part or an exponent that takes it out of the range of representable integers, then it SHALL be encoded as a floating point value. If it cannot be represented as a floating point value, then it SHALL be encoded as a BIGNUM by encoders that support them.
119+
120+
For the unsigned integers, from most to least preferred:
121+
122+
~~~
123+
UInt8: [0 ... 2^8 - 1] [0 ... 255]
124+
UInt16: [2^8 ... 2^16 - 1] [256 ... 65535]
125+
UInt32: [2^16 ... 2^32 - 1] [65536 ... 4294967295]
126+
UInt64: [2^32 ... 2^64 - 1] [4294967296 ... 18446744073709551615]
127+
Float: [2^64 ...] [18446744073709551616 ...]
128+
BIGNUM: [2^64 ...] [18446744073709551616 ...]
129+
~~~
130+
131+
For the signed integers, from most to least preferred:
132+
133+
~~~
134+
Int8: [-2^7 … 2^7 - 1] [-128, 127]
135+
Int16: [-2^15 … 2^15 - 1] [-32768, 32767]
136+
Int32: [-2^31 … 2^31 - 1] [-2147483648, 2147483647]
137+
Int64: [-2^63 … 2^63 - 1] [-9223372036854775808, 9223372036854775807]
138+
Float: [… -2^63 - 1 U 2^63 …] [… -9223372036854775809 U 9223372036854775808 …]
139+
BIGNUM: [… -2^63 - 1 U 2^63 …] [… -9223372036854775809 U 9223372036854775808 …]
140+
~~~
119141

120142
This practice still produces well-formed CBOR according to the standard, and all existing implementations will be able to read it. It does exclude a map such as the following from being validated as dCBOR, as it would have a duplicate key:
121143

@@ -138,15 +160,15 @@ Similarly, encoders that support floating point MUST reduce all `+INF` values to
138160

139161
While there is no requirement that dCBOR codecs implement support for BigNums ≥ 2^64 (tags 2 and 3), codecs that do support them MUST use regular integer encodings where integers can represent the value.
140162

141-
## CBOR_NEGATIVE_INT_MAX disallowed
163+
## 65-bit negative integers disallowed
142164

143-
The largest negative integer that can be represented in 64 bits two's complement (STANDARD_NEGATIVE_INT_MAX) is -2^63 (0x8000000000000000).
165+
The largest negative integer that can be represented in 64-bit two's complement (STANDARD_NEGATIVE_INT_MAX) is -2^63 (0x8000000000000000).
144166

145-
However, the largest negative integer that can be represented in CBOR (CBOR_NEGATIVE_INT_MAX) is -2^64 (0x10000000000000000), which requires 65 bits. The CBOR encoding for CBOR_NEGATIVE_INT_MAX is 0x3BFFFFFFFFFFFFFFFF.
167+
However, CBOR can encode negative integers as low as CBOR_NEGATIVE_INT_MAX, which is -2^64 (two's complement: 0x10000000000000000, CBOR: 0x3BFFFFFFFFFFFFFFFF). Negative integers in the range [CBOR_NEGATIVE_INT_MAX ... STANDARD_NEGATIVE_INT_MAX - 1] would require 65 bits, and are thus not representable in machine-sized integers.
146168

147-
Because of this incompatibility between the CBOR and standard representations, dCBOR disallows CBOR_NEGATIVE_INT_MAX: conformant encoders MUST never encode this sequence and conformant decoders MUST reject CBOR_NEGATIVE_INT_MAX as not well-formed.
169+
Because of this incompatibility between the CBOR and standard representations, dCBOR disallows encoding negative integer values in the range [CBOR_NEGATIVE_INT_MAX ... STANDARD_NEGATIVE_INT_MAX - 1]: conformant encoders MUST never encode these values and conformant decoders MUST reject these values as invalid.
148170

149-
Implementations that support BIGNUM are able to encode and decode this value as BIGNUM.
171+
Implementations that support BIGNUM are able to encode and decode these values as BIGNUM.
150172

151173
# Application Level
152174

0 commit comments

Comments
 (0)