idnits 2.17.1 draft-bormann-cbor-00.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- == There are 2 instances of lines with non-RFC6890-compliant IPv4 addresses in the document. If these are example addresses, they should be changed. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (May 21, 2013) is 3994 days in the past. Is this intentional? -- Found something which looks like a code comment -- if you have code sections in the document, please surround them with '' and '' lines. Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) -- Looks like a reference, but probably isn't: '1' on line 1218 -- Looks like a reference, but probably isn't: '2' on line 1220 -- Looks like a reference, but probably isn't: '3' on line 1220 -- Looks like a reference, but probably isn't: '0' on line 1482 == Outdated reference: A later version (-07) exists of draft-ietf-lwig-terminology-04 -- Obsolete informational reference (is this intentional?): RFC 4627 (Obsoleted by RFC 7158, RFC 7159) -- Obsolete informational reference (is this intentional?): RFC 5226 (Obsoleted by RFC 8126) Summary: 0 errors (**), 0 flaws (~~), 3 warnings (==), 8 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 not yet C. Bormann 3 Internet-Draft Universitaet Bremen TZI 4 Intended status: Standards Track P. Hoffman 5 Expires: November 22, 2013 VPN Consortium 6 May 21, 2013 8 Concise Binary Object Representation (CBOR) 9 draft-bormann-cbor-00 11 Abstract 13 The Concise Binary Object Representation (CBOR) is a data format 14 whose design goals include the possibility of extremely small code 15 size, fairly small message size, and extensibility without the need 16 for version negotiation. These design goals make it different from 17 earlier binary serializations such as ASN.1 and MessagePack. 19 Status of This Memo 21 This Internet-Draft is submitted in full conformance with the 22 provisions of BCP 78 and BCP 79. 24 Internet-Drafts are working documents of the Internet Engineering 25 Task Force (IETF). Note that other groups may also distribute 26 working documents as Internet-Drafts. The list of current Internet- 27 Drafts is at http://datatracker.ietf.org/drafts/current/. 29 Internet-Drafts are draft documents valid for a maximum of six months 30 and may be updated, replaced, or obsoleted by other documents at any 31 time. It is inappropriate to use Internet-Drafts as reference 32 material or to cite them other than as "work in progress." 34 This Internet-Draft will expire on November 22, 2013. 36 Copyright Notice 38 Copyright (c) 2013 IETF Trust and the persons identified as the 39 document authors. All rights reserved. 41 This document is subject to BCP 78 and the IETF Trust's Legal 42 Provisions Relating to IETF Documents 43 (http://trustee.ietf.org/license-info) in effect on the date of 44 publication of this document. Please review these documents 45 carefully, as they describe your rights and restrictions with respect 46 to this document. Code Components extracted from this document must 47 include Simplified BSD License text as described in Section 4.e of 48 the Trust Legal Provisions and are provided without warranty as 49 described in the Simplified BSD License. 51 Table of Contents 53 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 3 54 1.1. Objectives . . . . . . . . . . . . . . . . . . . . . . . 3 55 1.2. Terminology . . . . . . . . . . . . . . . . . . . . . . . 5 56 2. Specification of the CBOR Encoding . . . . . . . . . . . . . 5 57 2.1. Major Types . . . . . . . . . . . . . . . . . . . . . . . 6 58 2.2. Floating Point Numbers and Values with No Content . . . . 8 59 2.3. Optional Tagging of Items . . . . . . . . . . . . . . . . 9 60 2.3.1. Date and Time . . . . . . . . . . . . . . . . . . . . 11 61 2.3.2. Bignums . . . . . . . . . . . . . . . . . . . . . . . 11 62 2.3.3. Decimal Fractions . . . . . . . . . . . . . . . . . . 11 63 2.3.4. Content Hints . . . . . . . . . . . . . . . . . . . . 11 64 2.3.4.1. Encoded CBOR data item . . . . . . . . . . . . . 12 65 2.3.4.2. Expected Later Encoding for CBOR to JSON 66 Converters . . . . . . . . . . . . . . . . . . . 12 67 2.3.4.3. Encoded Text . . . . . . . . . . . . . . . . . . 12 68 3. Creating CBOR-Based Protocols . . . . . . . . . . . . . . . . 13 69 3.1. CBOR in Streaming Applications . . . . . . . . . . . . . 13 70 3.2. Parsing Errors . . . . . . . . . . . . . . . . . . . . . 14 71 3.2.1. Enforcing Restrictions on the Value Following a Tag . 14 72 3.2.2. Handling Unknown Simple Values and Tags . . . . . . . 14 73 3.2.3. UTF-8 Strings . . . . . . . . . . . . . . . . . . . . 14 74 3.2.4. Incomplete CBOR data items . . . . . . . . . . . . . 14 75 3.3. Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 15 76 3.4. Specifying Keys for Maps . . . . . . . . . . . . . . . . 15 77 3.5. Undefined Values . . . . . . . . . . . . . . . . . . . . 16 78 3.6. Generic Encoders and Parsers . . . . . . . . . . . . . . 16 79 4. Converting Data Between CBOR and JSON . . . . . . . . . . . . 16 80 4.1. Converting From CBOR to JSON . . . . . . . . . . . . . . 17 81 4.2. Converting From JSON to CBOR . . . . . . . . . . . . . . 18 82 5. Diagnostic Notation . . . . . . . . . . . . . . . . . . . . . 19 83 6. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 20 84 6.1. Simple Values Registry . . . . . . . . . . . . . . . . . 20 85 6.2. Tags Registry . . . . . . . . . . . . . . . . . . . . . . 20 86 6.3. Media Type ("MIME Type") . . . . . . . . . . . . . . . . 20 87 7. Security Considerations . . . . . . . . . . . . . . . . . . . 21 88 8. Acknowledgements . . . . . . . . . . . . . . . . . . . . . . 21 89 9. References . . . . . . . . . . . . . . . . . . . . . . . . . 22 90 9.1. Normative References . . . . . . . . . . . . . . . . . . 22 91 9.2. Informative References . . . . . . . . . . . . . . . . . 22 92 Appendix A. Examples . . . . . . . . . . . . . . . . . . . . . . 23 93 Appendix B. Jump Table . . . . . . . . . . . . . . . . . . . . . 27 94 Appendix C. Pseudocode . . . . . . . . . . . . . . . . . . . . . 30 95 Appendix D. Half-precision . . . . . . . . . . . . . . . . . . . 31 96 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 32 98 1. Introduction 100 There are hundreds of standardized formats for binary representation 101 of structured data. Of those, some are for specific domains of 102 information, while others are generalized for arbitrary data. In the 103 IETF, probably the best-known formats in the latter category are 104 ASN.1's BER and DER [ASN.1]. 106 The format defined here follows some specific design goals that are 107 not well met by current formats. The serialization is for an 108 extended version of the JSON grammar [RFC4627]. It is important to 109 note that this is not a proposal that the grammar in RFC 4627 be 110 extended in general, since doing so would cause a significant 111 backwards incompatibility with already-deployed JSON documents. 112 Instead, this document simply defines its own grammar which starts 113 from JSON. 115 1.1. Objectives 117 The objectives of the Concise Binary Object Representation (CBOR), 118 roughly in decreasing order of importance, are: 120 1. The representation must be able to unambiguously encode most 121 common data formats used in Internet standards. 123 * Representing a reasonable set of basic data types and 124 structures using binary encoding. "Reasonable" here is 125 largely influenced by the capabilities of JSON, with the major 126 addition of binary byte strings. The structures supported are 127 limited to arrays and trees; loops and lattice-style graphs 128 are not supported. 130 * There is no requirement that all data formats be uniquely 131 encoded; that is, it is acceptable that the number "7" might 132 be encoded in multiple different ways. 134 2. The code for an encoder or parser must be able to be compact in 135 order to support systems with very limited memory and processor 136 power and instruction sets. 138 * An encoder and a parser need to be implementable in a very 139 small amount of code, thus being applicable to class 1 140 constrained nodes as defined in [I-D.ietf-lwig-terminology]. 142 * The format should use contemporary machine representations of 143 data (for example, not requiring binary-to-decimal 144 conversion). 146 3. Data must be able to be parsed without a schema description. 148 * Similar to JSON, encoded data should be self-describing so 149 that a generic parser can be written. 151 4. The serialization must be reasonably compact, but data 152 compactness is secondary to code compactness for the encoder and 153 parser. 155 * "Reasonable" here is bounded by JSON as an upper bound in 156 size, and by implementation complexity maintaining a lower 157 bound. Using either general compression schemes or extensive 158 bit-fiddling violates the complexity goals. 160 5. The format must be applicable to both constrained nodes and high- 161 volume applications. 163 * This means it must be reasonably frugal in CPU usage for both 164 encoding and parsing. This is relevant both for constrained 165 nodes and for potential usage in applications with a very high 166 volume of data. 168 6. The format must support all JSON data types for conversion to and 169 from JSON. 171 * It must support a reasonable level of conversion as long as 172 the data represented are within the capabilities of JSON. It 173 must be possible to define a unidirectional mapping towards 174 JSON for all types of data. 176 7. The format must be extensible, with the extended data being able 177 to be parsed by earlier parsers. 179 * The format is designed for decades of use. 181 * The format must support a form of extensibility that allows 182 fallback so that a parser that does not understand an 183 extension can still parse the message. 185 * The format must be able to be extended in the future by later 186 IETF standards. 188 1.2. Terminology 190 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 191 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 192 document are to be interpreted as described in RFC 2119, BCP 14 193 [RFC2119] and indicate requirement levels for compliant CBOR 194 implementations. 196 The term "byte" is used in its now-customary sense as a synonym for 197 "octet". All multi-byte values are encoded in network byte order. 199 This specification makes use of the following terminology: 201 Data item: A single piece of CBOR data. The structure of data item 202 may contain zero, one or more nested data items. The term is used 203 both for the data item in representation format and for the 204 abstract idea that can be derived from that by a parser. 206 Parser: A process that decodes a CBOR data item and makes it 207 available to an application. This is also sometimes called a 208 decoder. 210 Encoder: A process that generates the representation format of a 211 CBOR data item from application information. 213 Data Stream: A sequence of zero or more data items, not further 214 assembled into a larger containing data item. The independent 215 data items that make up a data stream are sometimes also referred 216 to as "top-level data items". 218 Stream parser: A process that decodes a data stream and makes each 219 of the data items in the sequence available to an application. 221 Where bit arithmetic or data types are explained, this document uses 222 the notation familiar from the programming language C, except that ** 223 denotes exponentiation. Similar to the "0x" notation for hexadecimal 224 numbers, numbers in binary notation are prefixed with "0b". 225 Underscores can be added to such a number solely for readability, so 226 0b00100001 (0x21) might be written 0b001_00001 to emphasize the 227 desired interpretation of the bits in the byte. 229 2. Specification of the CBOR Encoding 230 A CBOR encoded data item is structured and encoded as described in 231 this section. For the impatient reader, the encoding is summarized 232 in Table 4 in Appendix B. 234 The initial byte of each data item contains both information about 235 the major type (the high-order 3 bits) and additional information 236 (the low-order 5 bits). When the value of the additional information 237 is less than 28, it is directly used as a small integer. When it is 238 28 to 31, the additional bytes for a variable-length integer 239 immediately follow; the values 28 to 31 of the additional information 240 specify that its length is a 1-, 2-, 4- or 8-byte unsigned integer, 241 respectively. 243 In all additional information values, the resulting integer is 244 interpreted depending on the major type. It may represent the actual 245 data: for example, in integer types the resulting integer is used for 246 the value itself. It may instead supply length information: for 247 example, in byte strings it gives the length of the byte string data 248 that follows. 250 A CBOR parser implementation can be based on the jump table with all 251 256 defined values for the initial byte (Table 4). A parser in a 252 constrained implementation can instead use the structure of the 253 initial byte and following bytes for more compact code (see 254 Appendix C for a rough impression of how this could look like). 256 2.1. Major Types 258 The following lists the major types and the additional information 259 and other bytes associated with the type. 261 Major type 0: an unsigned integer. The 5-bit additional information 262 is either the integer itself (for additional information values 0 263 through 27), or the length of additional data. Additional 264 information 28 means the value is represented in an additional 265 uint8_t, 29 means a uint16_t, 30 means a uint32_t, and 31 means a 266 uint64_t. For example, the integer 10 is denoted as the one byte 267 0b000_01010 (major type 0, additional information 10). The 268 integer 500 would be 0b000_11101 (major type 0, additional 269 information 29) followed by the two bytes 0x01f4, which is 500 in 270 decimal. 272 Major type 1: a negative integer. The encoding follows the rules 273 for unsigned integers (major type 0), except that the value is 274 then -1 minus the encoded unsigned integer. For example, the 275 integer -500 would be 0b001_11101 (major type 1, additional 276 information 29) followed by the two bytes 0x01f3, which is 499 in 277 decimal. 279 Major type 2: a byte string. The string's length in bytes is 280 represented following the rules for positive integers (major type 281 0). For example, a byte string whose length is 5 would have an 282 initial byte of 0b010_00101 (major type 2, additional information 283 5 for the length), followed by 5 bytes of binary content. A byte 284 string whose length is 500 would have 3 initial bytes of 285 0b010_11101 (major type 2, additional information 29 to indicate a 286 two-byte length) followed by the two bytes 0x01f4 for a length of 287 500, followed by 500 bytes of binary content. 289 Major type 3: string of Unicode characters that is encoded as UTF-8 290 [RFC3629]. The format of this type is identical to that of byte 291 strings (major type 2), that is, as with major type 2, the length 292 gives the number of bytes. This type is provided for systems that 293 need to interpret or display human-readable text. The Unicode 294 characters in this type are never escaped. Thus, a newline 295 character (U+000A) is always represented in a string as the byte 296 0x0a, and never as the bytes 0x5c6e (the characters "\" and "n") 297 or as 0x5c7530303061 (the characters "\", "u", "0", "0", "0", and 298 "a"). 300 Major type 4: an array of data items. Arrays are also called 301 sequences or tuples. The array's length follows the rules for 302 byte strings (major type 2), except that the length denotes the 303 number of data items, not the length in bytes that the array takes 304 up. Items in an array do not need to all be of the same type. 305 For example, an array that contains 10 items of any type would 306 have an initial byte of 0b100_01010 (major type of 4, additional 307 information of 10 for the length) followed by the 10 remaining 308 items. 310 Major type 5: a map of pairs of data items. Maps are often also 311 called tables, dictionaries, hashes, or objects (in JSON). A map 312 is comprised of pairs of data items, the even-numbered ones 313 serving as keys and the following odd-numbered ones serving as 314 values for the key that comes immediately before it. The map's 315 length follows the rules for byte strings (major type 2), except 316 that the length denotes the number of pairs, not the length in 317 bytes that the map takes up. For example, a map that contains 9 318 pairs would have an initial byte of 0b101_01001 (major type of 5, 319 additional information of 9 for the number of pairs) followed by 320 the 18 remaining items. The first item is the first key, the 321 second item is the first value, the third item is the second key, 322 and so on. 324 Major type 6: floating point numbers and simple data types that need 325 no content. See Section 2.2. 327 Major type 7: optional semantic tagging of other major types. See 328 Section 2.3. 330 These eight major types lead to a simple table showing which of the 331 256 possible values for the initial byte of a data item are used for 332 (Table 4). 334 In major types 6 and 7, many of the possible values are reserved for 335 future specification. See Section 6 for more information on these 336 values. 338 2.2. Floating Point Numbers and Values with No Content 340 Major type 6 is for two types of data: floating point numbers and 341 "simple values" that do not need any content. Each value of the 342 5-bit additional information in the initial byte has its own separate 343 meaning, as defined in Table 1. Like the major types for integers, 344 items of this major type do not carry content data; all the 345 information is in the initial bytes. 347 +-------------+--------------------------------------------------+ 348 | 5-bit value | semantics | 349 +-------------+--------------------------------------------------+ 350 | 0..27 | Simple value (value 0..27) | 351 | | | 352 | 28 | Simple value (value 28..255 in following byte) | 353 | | | 354 | 29 | IEEE 754 Half-Precision Float (16 bits follow) | 355 | | | 356 | 30 | IEEE 754 Single-Precision Float (32 bits follow) | 357 | | | 358 | 31 | IEEE 754 Double-Precision Float (64 bits follow) | 359 +-------------+--------------------------------------------------+ 361 Table 1: Values for Additional Information in Major Type 6 363 The 5-bit values of 29, 30, and 31 are for 16-bit, 32-bit, and 64-bit 364 IEEE 754 binary floating point values. These floating point values 365 are encoded in the additional bytes of the appropriate size. (See 366 Appendix D for some information about 16-bit floating point.) 368 As with all other major types, the 5-bit value 28 signifies a single- 369 byte extension: it is followed by an additional byte to represent the 370 simple value (to minimize confusion, only the values 28 to 255 are 371 used). This maintains the structure of the initial bytes: as for the 372 other major types, the length of these always depends on the 373 additional information in the first byte. Table 2 lists the values 374 allocated and available for simple types. 376 +---------+-----------------+ 377 | value | semantics | 378 +---------+-----------------+ 379 | 0..23 | (unallocated) | 380 | | | 381 | 24 | False | 382 | | | 383 | 25 | True | 384 | | | 385 | 26 | Null | 386 | | | 387 | 27 | Undefined value | 388 | | | 389 | 28..255 | (unallocated) | 390 +---------+-----------------+ 392 Table 2: Simple Values 394 2.3. Optional Tagging of Items 396 In CBOR, a data item can optionally be preceded by (enclosed by) a 397 tag to give it additional semantics while retaining its structure. 398 The tag is major type 7, and represents an integer number as 399 indicated by the tag's integer value; the (sole) data item is carried 400 as content data. If a tag requires structured data, this structure 401 is encoded into the nested data item. The definition of a tag 402 usually restricts what kinds of nested data item or items can be 403 carried by a tag. 405 The initial bytes of the tag follow the rules for positive integers 406 (major type 0). The tag is followed by a single data item of any 407 type. For example, assume that a byte string of length 12 is marked 408 with a tag to indicate it is a positive bignum. This would be marked 409 as 0b111_00010 (major type 7, additional information 2 for the tag) 410 followed by 0b010_01100 (major type 2, additional information of 12 411 for the length) followed by the 12 bytes of the bignum. 413 CBOR tags are truly optional, and are probably of little value in 414 applications where the implementation creating a particular CBOR data 415 stream and the implementation parsing that stream know the semantic 416 meaning of each item in the stream. Their primary purpose in this 417 specification is to define common data types such as dates. A 418 secondary purpose it to allow optional tagging when the parser is a 419 generic CBOR parser that might be able to benefit from hints about 420 the content of items. Understanding the semantic tags is optional 421 for a parser; it can just jump over the initial bytes of the tag and 422 interpret the tagged data item itself. 424 Applications may use specific tags defined in the following list and/ 425 or defined by standard action or in the registry. 427 +---------+-------------------+-------------------------------------+ 428 | tag | data item | semantics | 429 +---------+-------------------+-------------------------------------+ 430 | 0 | UTF-8 string | Standard date/time string; see | 431 | | | Section 2.3.1 | 432 | | | | 433 | 1 | multiple | Epoch-based date/time; see Section | 434 | | | 2.3.1 | 435 | | | | 436 | 2 | byte string | Positive bignum; see Section 2.3.2 | 437 | | | | 438 | 3 | byte string | Negative bignum; see Section 2.3.2 | 439 | | | | 440 | 4 | array | Decimal fraction; see Section 2.3.3 | 441 | | | | 442 | 5..24 | (unallocated) | (unallocated) | 443 | | | | 444 | 25 | multiple | Expected conversion to base64url | 445 | | | encoding; see Section 2.3.4.2 | 446 | | | | 447 | 26 | multiple | Expected conversion to base64 | 448 | | | encoding; see Section 2.3.4.2 | 449 | | | | 450 | 27 | multiple | Expected conversion to base16 | 451 | | | encoding; see Section 2.3.4.2 | 452 | | | | 453 | 28 | byte string | Encoded CBOR data item; see Section | 454 | | | 2.3.4.1 | 455 | | | | 456 | 32 | UTF-8 string | URI; see Section 2.3.4.3 | 457 | | | | 458 | 33 | UTF-8 string | Base64url; see Section 2.3.4.3 | 459 | | | | 460 | 34 | UTF-8 string | Base64; see Section 2.3.4.3 | 461 | | | | 462 | 35 | UTF-8 string | Regular expression; see Section | 463 | | | 2.3.4.3 | 464 | | | | 465 | 36 | UTF-8 string | MIME message; see Section 2.3.4.3 | 466 | | | | 467 | 37+ | (unallocated) | (unallocated) | 468 +---------+-------------------+-------------------------------------+ 470 Table 3: Values for tags 472 2.3.1. Date and Time 474 Tag type 0 is for date/time strings that follow the standard format 475 described in [RFC3339], as refined by Section 3.3 of [RFC4287]. 477 Tag type 1 is for numerical representation of seconds relative to 478 1970-01-01T00:00Z in UTC time. The tagged item can be a positive or 479 negative integer (major types 0 and 1), or a floating point number 480 (major type 6 with additional information 29, 30 or 31). Note that 481 the number can be negative (time before 1970-01-01T00:00Z) and, if a 482 floating point number, indicate fractional seconds. 484 2.3.2. Bignums 486 Bignums are integers that do not fit into the basic integer 487 representations provided by major types 0 and 1. They are encoded as 488 a byte string data item, which is interpreted as an unsigned integer 489 n in network byte order. For tag value 2, the value of the bignum is 490 n. For tag value 3, the value of the bignum is -1 - n. Parsers that 491 understand these tags MUST be able to decode bignums that have 492 leading zeroes. 494 For example, the number 18446744073709551616 (2**64) is represented 495 as 0b111_00010 (major type 7, tag 2), followed by 0b010_01001 (major 496 type 2, length 9), followed by 0x010000000000000000 (one byte 0x01 497 and eight bytes 0x00). 499 2.3.3. Decimal Fractions 501 [RFC6020] defines a decimal fraction format called decimal64, which 502 can be used for an exact representation of decimal fractions by 503 combining a 64-bit integer with a small negative decimal (base-10) 504 exponent. CBOR supports a slight generalization, by allowing the use 505 of other integer lengths than 64 bit. In CBOR this is represented as 506 an array that contains exactly two integers: the (negative, base-10) 507 exponent and the mantissa. For example, the number 273.15 could be 508 represented as 0b111_00100 (major type of 7 for the tag, additional 509 information of 4 for the type of tag), followed by 0b100_00010 (major 510 type of 4 for the array, additional information of 2 for the length 511 of the array), followed by 0b001_00001 (major type of 1 for the first 512 integer, additional information of 1 for the value of -2), followed 513 by 0b000_11101 (major type of 0 for the second integer, additional 514 information of 29 for a two-byte value), followed by 515 0b0110101010110011 (27315 in two bytes). 517 2.3.4. Content Hints 518 The tags in this section are for content hints that might be used by 519 generic CBOR processors. 521 2.3.4.1. Encoded CBOR data item 523 Sometimes it is beneficial to carry an embedded CBOR data item that 524 is not meant to be parsed immediately at the time the enclosing data 525 item is being parsed. Tag 28 (CBOR data item) can be used to tag the 526 embedded byte string as a data item encoded in CBOR format. 528 2.3.4.2. Expected Later Encoding for CBOR to JSON Converters 530 Tags 25 to 27 indicate that a byte string might require a specific 531 encoding when interoperating with a text-based representation. These 532 tags are useful when an encoder knows that the byte string data it is 533 writing is likely to be later converted to a particular JSON-based 534 usage. That usage specifies that some strings are encoded as Base64, 535 Base64url, and so on. The encoder uses byte strings instead of doing 536 the encoding itself to reduce the message size, to reduce the code 537 size of the encoder, or both. The encoder does not know whether or 538 not the converter will be generic, and therefore wants to say what it 539 believes is the proper way to convert binary strings to JSON. 541 The data item following this tag can be a byte string, an array, or a 542 map. In the latter two cases, the tag applies to all of the byte 543 strings in the data object. 545 These three tag types suggest conversions to three of the base data 546 encodings defined in [RFC4648]. Where the encoding allows the use of 547 padding ("="), this is not used. Later tags might be defined for 548 other data encodings of RFC 4648, or of other ways to encode binary 549 data in strings. 551 2.3.4.3. Encoded Text 553 Some text strings hold data that have formats widely-used on the 554 Internet, and sometimes those formats can be validated and presented 555 to the application in appropriate form by the parser. There are tags 556 for some of these formats. 558 o Tag 32 is for URIs, as defined in [RFC3986]; 560 o Tags 33 and 34 are for base64url and base64 encoded text strings, 561 as defined in [RFC4648]; 563 o Tag 35 is for regular expressions in PCRE/JavaScript syntax 564 [ECMA262]. 566 o Tag 36 is for MIME messages, as defined in [RFC2045]; 568 Note that tag 33 and 34 differ from 25 and 26 in that the data is 569 transported in base-encoded form for the former and in raw byte 570 string form in the latter case. 572 3. Creating CBOR-Based Protocols 574 Data formats such as CBOR are often used in environments where there 575 is no format negotiation. A specific design goal of CBOR is to not 576 need any included or assumed schema: a parser can take a CBOR item 577 and parse it with no other knowledge. 579 Of course, in real-world implementations, the encoder and the parser 580 will have a shared view of what should be in a CBOR data item. For 581 example, an agreed-to format might be "the item is an array whose 582 first value is a UTF-8 string, the second value is an integer, 583 followed by zero or more floating point numbers" or "a map whose keys 584 are byte strings that has to contain at least one pair whose key is 585 0xab01". 587 This specification puts no restrictions on CBOR-based protocols. An 588 encoder can be capable of encoding as many or as few types of values 589 as is required by the protocol in which it is used; a parser can be 590 capable of understanding as many or as few types of values as is 591 required by the protocols in which it is used. This lack or 592 restrictions allows CBOR to be used in extremely constrained 593 environments. 595 This section discusses some considerations in creating CBOR-based 596 protocols. It is advisory only, and explicitly excludes any language 597 from RFC 2119 other than words that could be interpreted as "MAY" in 598 the RFC 2119 sense. 600 3.1. CBOR in Streaming Applications 602 In a streaming application, a data stream may be composed of a 603 sequence of CBOR data items concatenated back-to-back. In such an 604 environment, the parser immediately begins decoding a new data item 605 if data is found after the end of a previous data item. 607 Not all of the bytes making up a data item may be immediately 608 available to the parser; some parsers will buffer additional data 609 until a complete data item can be presented to the application. 610 Other parsers can present partial information about a top-level data 611 item to an application, such as the nested data items that could 612 already be decoded, or even parts of a byte string that hasn't 613 completely arrived yet. 615 3.2. Parsing Errors 617 3.2.1. Enforcing Restrictions on the Value Following a Tag 619 Tags (Section 2.3) specify what type of data item is supposed to 620 follow the tag; for example, the tags for positive or negative 621 bignums are supposed to be followed by byte strings. A parser that 622 finds a data item of the wrong type after a tag might issue a 623 warning, might stop processing altogether, might handle the error and 624 make the incorrectly-typed value available to the application as 625 such, or take some other type of action. 627 3.2.2. Handling Unknown Simple Values and Tags 629 A parser that comes across a simple value Section 2.2 that it does 630 not recognize, such as a value that was added to the IANA registry 631 after the parser was deployed or a value that the parser chose not to 632 implement, might issue a warning, might stop processing altogether, 633 might handle the error by making the unknown value available to the 634 application as such, or take some other type of action. 636 A parser that comes across a tag Section 2.3 that it does not 637 recognize, such as a tag that was added to the IANA registry after 638 the parser was deployed or a tag that the parser chose not to 639 implement, might issue a warning, might stop processing altogether, 640 might handle the error and present the unknown tag value together 641 with the contained data item to the application, might ignore the tag 642 and simply present the contained data item only to the application, 643 or take some other type of action. 645 3.2.3. UTF-8 Strings 647 A parser might or might not want to verify that the octets in an 648 UTF-8 string (major type 3) is actually valid UTF-8. If a parser 649 attempts to validate the UTF-8 and fails, it might issue a warning, 650 might stop processing altogether, might handle the error and present 651 the invalid string to the application as such, or take some other 652 type of action. 654 3.2.4. Incomplete CBOR data items 656 The representation of a CBOR data item has a specific length, 657 determined by its initial bytes and by the structure of any data 658 items enclosed in the data items. If less data is available in the 659 input byte string, a parser may completely fail the decoding, or 660 substitute the missing data and data items using an decoder-specific 661 convention. A decoder may also implement incremental parsing, that 662 is, parse the data item as far as it is available and present the 663 data found so far, (such as in an event-based interface) with the 664 option of continuing the decoding once further data are available. 666 For instance, if a parser is expecting a certain number of array or 667 map entries, but it instead encounters the end of the data, it should 668 probably issue an error and/or stop processing altogether, but it 669 might take some other action. The same is true if it is processing 670 what it expects to be the last pair in a map and it comes to the end 671 of the data. 673 Similarly, if a parser has just seen a tag and then encounters the 674 end of the data, it should probably issue an error and/or stop 675 processing altogether, but it might take some other action. 677 3.3. Numbers 679 For the purposes of this specification, all number representations 680 are equivalent. This means that an encoder can encode a floating 681 point value of 0.0 as the integer 0. It, however, also means that an 682 application that expects to find integer values only might find 683 floating point values if the encoder decides these are desirable, 684 e.g., where the floating point value is more compact than a 64-bit 685 integer. 687 A CBOR-based protocol that includes floating point numbers can 688 restrict which of the three formats (half-precision, single- 689 precision, and double-precision) are to be supported. For an 690 integer-only application, a protocol may want to completely exclude 691 the use of floating point values. 693 A CBOR-based protocol designed for compactness may want to exclude 694 specific integer encodings that are longer than necessary for the 695 application, such as to save the need to implement 64-bit integers. 696 There is an expectation that encoders will use the most compact 697 integer representation that can represent a given value. However, a 698 compact application should accept values that use a longer-than 699 needed encoding (such as encoding "0" as 0b000_11101 followed by two 700 bytes of 0x00) as long as the application can parse an integer of the 701 given size. 703 3.4. Specifying Keys for Maps 705 The encoding and parsing applications need to agree on what types of 706 keys are going to be used in maps. In applications that need to 707 interwork with JSON-based applications, keys probably should be 708 limited to UTF-8 strings only; otherwise, there has to be a specified 709 mapping from the other CBOR types to Unicode characters, and this 710 often leads to implementation errors. 712 If multiple types of keys are to used, consideration should be given 713 to how these types would be represented in the specific programming 714 environments that are to be used. For example, in JavaScript 715 objects, a key of integer 1 cannot be distinguished from a key of 716 string "1". This means that, if integer keys are used, the 717 simultaneous use of string keys that look like numbers needs to be 718 avoided. Again, this leads to the conclusion that keys should be of 719 a single CBOR type. 721 Applications for constrained devices that have maps with fewer than 722 28 known keys should consider using integers because the keys can 723 then be encoded in a single byte. 725 3.5. Undefined Values 727 In some CBOR-based protocols, the simple value of Undefined might be 728 used by an encoder as a substitute for a data item with an encoding 729 problem, in order to allow the rest of the enclosing data items to be 730 encoded without harm. 732 3.6. Generic Encoders and Parsers 734 A generic CBOR decoder can parse all well-formed CBOR data and 735 present them to an application. CBOR data are well-formed if the 736 structure of the initial bytes and the byte strings/data items 737 implied by their values is followed and no extraneous data follows 738 (Appendix C). 740 Even though CBOR attempts to minimize these cases, not all well- 741 formed CBOR data are valid: E.g., the format excludes simple values 742 below 32 that are encoded with an extension byte. Also, specific 743 tags may make semantic constraints that may be violated, e.g. by 744 including a tag in a tag or by enclosing a byte string within a date 745 tag. Finally, the data may be invalid, such as invalid UTF-8 strings 746 or date strings that do not conform to [RFC3339]. 748 Generic decoders provide ways to present well-formed CBOR values, 749 both valid and invalid, to an application. The diagnostic notation 750 (Section 5) may be used to present well-formed CBOR values to humans. 752 Generic encoders provide an application interface that allows the 753 application to specify any well-formed value, including simple values 754 and tags unknown to the encoder. 756 4. Converting Data Between CBOR and JSON 757 This section gives non-normative advice about converting between CBOR 758 and JSON. Implementations of converters are free to use whichever 759 advice here they want. 761 It is worth noting that a JSON text is a string of characters, not an 762 encoded string of bytes, while a CBOR data item consist of bytes, not 763 characters. 765 4.1. Converting From CBOR to JSON 767 Most of the types in CBOR have direct analogs in JSON. However, some 768 do not, and someone implementing a CBOR-to-JSON converter has to 769 consider what to do in those cases. The following non-normative 770 suggestion deals with these by converting them to a single substitute 771 value, such as a JSON null. 773 o An Integer (major type 0 or 1) becomes a JSON number. 775 o A byte string (major type 2) that is not embedded in a tag that 776 specifies a proposed encoding is encoded in Base64url without 777 padding and becomes a JSON string. 779 o A UTF-8 string (major type 3) becomes a JSON string. Note that 780 JSON requires escaping certain characters (RFC 4627, section 2.5): 781 quotation mark (U+0022), reverse solidus (U+005C), and the "C0 782 control characters" (U+0000 through U+001F). All other characters 783 are copied unchanged into the JSON UTF-8 string. 785 o An array (major type 4) becomes a JSON array. 787 o A map (major type 5) becomes a JSON object. This is possible 788 directly only if all keys are UTF-8 strings. A converter might 789 also convert other keys into UTF-8 strings (such as by converting 790 integers into strings containing their decimal representation); 791 however, doing so introduces a danger of key collision. 793 o False (major type 6, additional information 24) becomes a JSON 794 false. 796 o True (major type 6, additional information 25) becomes a JSON 797 true. 799 o Null (major type 6, additional information 26) becomes a JSON 800 null. 802 o A floating point value (major type 6, additional information 29 803 through 31) becomes a JSON number if it is finite (i.e., can be 804 represented in a JSON number); if the value is non-finite (i.e., 805 (positive) Infinity, -Infinity, or NaN), it is represented by the 806 substitute value. 808 o Any other simple value (Major type 6, any additional information 809 value not yet discussed) is represented by the substitute value. 811 o A bignum (major type 7, tag value 8 or 9) becomes a JSON number. 813 o A byte string with an encoding hint (major type 7, tag value 12 814 through 14) is encoded as described and becomes a JSON string. 816 o For all other tags (major type 7, any other tag value), the 817 embedded CBOR item is represented as a JSON value; the tag value 818 is ignored. 820 4.2. Converting From JSON to CBOR 822 All JSON values, once decoded, directly map into one or more CBOR 823 values. As with any kind of CBOR generation, decisions have to be 824 made with respect to number representation. In a suggested 825 conversion: 827 o JSON numbers without fractional parts (integer numbers) are 828 represented as integers (major types 0 and 1, possibly major type 829 7 tag value 8 and 9), choosing the shortest form; integers longer 830 than an implementation-defined threshold (which is usually either 831 32 or 64 bits) may instead be represented as floating point 832 values. (If the JSON was generated from a JavaScript 833 implementation, its precision is already limited to 53 bits 834 maximum.) 836 o Numbers with fractional parts are represented as floating point 837 values. The shortest exact floating point representation is used; 838 for instance, 1.5 is represented in a 16-bit floating point value 839 (not all implementations will be efficiently capable of finding 840 the minimum form, though). There may be an implementation-defined 841 limit to the precision that will affect the precision of the 842 represented values. Decimal representation should only be used if 843 that is specified in a protocol. 845 CBOR has been designed to generally provide a more compact encoding 846 than JSON. One implementation strategy that comes to mind is to 847 perform a JSON to CBOR encoding in place in a single buffer. This 848 strategy would need to consider the pathological case that some 849 strings represented with no or very few escapes and longer (or much 850 longer) than 255 may expand when encoded as UTF-8 strings in CBOR. 851 Similarly, a few of the binary floating point representations might 852 cause expansion from some short decimal representations in JSON. 854 5. Diagnostic Notation 856 CBOR is a binary interchange format. To facilitate documentation and 857 debugging, and in particular to facilitate communication between 858 entities cooperating in debugging, this section defines a simple 859 human-readable diagnostic notation. All actual interchange always 860 happen in the binary format. 862 Note that this truly is a diagnostic format; it is not meant to be 863 parsed. 865 The diagnostic notation is based on JSON as it is defined in RFC 866 4627. The notation borrows the JSON syntax for numbers (integer and 867 floating point), True, False, Null, UTF-8 strings, arrays and maps 868 (maps are called objects in JSON; the diagnostic notation extends 869 JSON here by allowing any data item in the key position). Undefined 870 is written >undefined< as in JavaScript. The non-finite floating 871 point numbers Infinity, -Infinity, and NaN are written exactly as in 872 this sentence (this is also a way they can be written in JavaScript, 873 although JSON does not allow them). A tagged item is written as an 874 integer number for the tag followed by the item in parentheses, e.g. 875 an RFC 3339 (ISO 8601) date could be notated as: 877 0("2013-03-21T20:04:00Z") 879 or the equivalent relative time as 881 1(1363896240) 883 Byte strings are notated in one of the base encodings, without 884 padding, enclosed in single quotes, prefixed by >h< for base16, >b32< 885 for base32, >h32< for base32hex, >b64< for base64 or base64url (the 886 actual encodings do not overlap, so the string remains unambiguous). 887 For example, the byte string 0x12345678 could be written h'12345678', 888 b32'CI2FM6A', or b64'EjRWeA'. 890 Unassigned simple values are given as "simple()" with the appropriate 891 integer in the parentheses. For example, "simple(42)" indicates 892 major type 6, value 42. 894 There is no way to indicate in the diagnostic notation which of 895 several alternative representations were actually used, so a data 896 item written >1.5< be a diagnostic decoder might have been encoded as 897 a half-, single-, or double-precision float. (Future versions of the 898 diagnostic notation might want to add some annotations for those fine 899 differences.) 901 6. IANA Considerations 903 IANA will create two registries for new CBOR values. The registries 904 will follow the rules in [RFC5226]. IANA will also allocate a new 905 MIME media type. 907 6.1. Simple Values Registry 909 A registry called "CBOR Simple Values" will be created. The initial 910 values are shown in Table 2. 912 New entries in the range 0 to 23 will be allocated by Standards 913 Action, starting with the number 16. New entries in the range 32 to 914 255 will be allocated by Specification Required. 916 6.2. Tags Registry 918 A registry called "CBOR Tags" will be created. The initial values 919 are shown in Table 3. 921 New entries in the range 0 to 27 will be allocated by Standards 922 Action. New entries in the range 28 to 255 will be allocated by 923 Specification Required. New entries in the range 256 to 924 18446744073709551615 will be allocated by First Come First Served. 925 The template for First Come First Served will include point of 926 contact and an optional field for URL to a description of the 927 semantics of the tag; the latter can be something like an Internet- 928 Draft or a web page. 930 6.3. Media Type ("MIME Type") 932 The Internet media type [RFC6838] for CBOR data is application/cbor. 934 Type name: application 936 Subtype name: cbor 938 Required parameters: n/a 940 Optional parameters: n/a 942 Encoding considerations: none; CBOR is a binary format 944 Security considerations: Same as for the base document 946 Interoperability considerations: n/a 948 Published specification: This document 949 Applications that use this media type: None yet, but it is expected 950 that this format will be deployed in many protocols and 951 applications. 953 Additional information: 954 Magic number(s): n/a 955 File extension(s): .cbor 956 Macintosh file type code(s): n/a 958 Person & email address to contact for further information: 959 Carsten Bormann 960 cabo@tzi.org 962 Intended usage: COMMON 964 Restrictions on usage: none 966 Author: 967 Carsten Bormann 968 cabo@tzi.org 970 Change controller: 971 Carsten Bormann 972 cabo@tzi.org 974 TBD: Maybe add application/mmmmm+cbor for specific protocols? 976 7. Security Considerations 978 A network-facing application can exhibit vulnerabilities in its 979 processing logic for incoming data. Complex parsers are well known 980 as a likely source of such vulnerabilities, such as the ability to 981 remotely crash a node, or even remotely execute arbitrary code on it. 982 CBOR attempts to narrow the opportunities for introducing such 983 vulnerabilities by reducing parser complexity, by giving the entire 984 range of encodable values a meaning where possible. 986 8. Acknowledgements 988 CBOR was inspired by MessagePack. MessagePack was developed and 989 promoted by Sadayuki Furuhashi ("frsyuki"). This reference to 990 MessagePack is solely for attribution; CBOR is not intended as a 991 version of or replacement for MessagePack, as it has different design 992 goals and requirements. 994 The need for functionality beyond the original MessagePack 995 Specification became obvious to many people at about the same time 996 around the year 2012. BinaryPack is a minor derivation of 997 MessagePack that was developed by Eric Zhang for the binaryjs 998 project. A similar, but different extension was made by Tim Caswell 999 for his msgpack-js and msgpack-js-browser projects. Many people have 1000 contributed to the recent discussion about extending MessagePack to 1001 separate text string representation from byte string representation. 1003 The encoding of the argument information in CBOR was inspired by the 1004 encoding of length information designed by Klaus Hartke for CoAP. 1006 9. References 1008 9.1. Normative References 1010 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1011 Requirement Levels", BCP 14, RFC 2119, March 1997. 1013 9.2. Informative References 1015 [ASN.1] International Telecommunications Union, "Information 1016 Technology -- ASN.1 encoding rules: Specification of Basic 1017 Encoding Rules (BER), Canonical Encoding Rules (CER) and 1018 Distinguished Encoding Rules (DER)", ITU-T Recommendation 1019 X.690, 1994. 1021 [ECMA262] European Computer Manufacturers Association, "ECMAScript 1022 Language Specification 5.1 Edition", ECMA Standard 1023 ECMA-262, June 2011, . 1026 [I-D.ietf-lwig-terminology] 1027 Bormann, C., Ersue, M., and A. Keraenen, "Terminology for 1028 Constrained Node Networks", draft-ietf-lwig-terminology-04 1029 (work in progress), April 2013. 1031 [RFC2045] Freed, N. and N.S. Borenstein, "Multipurpose Internet Mail 1032 Extensions (MIME) Part One: Format of Internet Message 1033 Bodies", RFC 2045, November 1996. 1035 [RFC3339] Klyne, G., Ed. and C. Newman, "Date and Time on the 1036 Internet: Timestamps", RFC 3339, July 2002. 1038 [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO 1039 10646", STD 63, RFC 3629, November 2003. 1041 [RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, "Uniform 1042 Resource Identifier (URI): Generic Syntax", STD 66, RFC 1043 3986, January 2005. 1045 [RFC4287] Nottingham, M., Ed. and R. Sayre, Ed., "The Atom 1046 Syndication Format", RFC 4287, December 2005. 1048 [RFC4627] Crockford, D., "The application/json Media Type for 1049 JavaScript Object Notation (JSON)", RFC 4627, July 2006. 1051 [RFC4648] Josefsson, S., "The Base16, Base32, and Base64 Data 1052 Encodings", RFC 4648, October 2006. 1054 [RFC5226] Narten, T. and H. Alvestrand, "Guidelines for Writing an 1055 IANA Considerations Section in RFCs", BCP 26, RFC 5226, 1056 May 2008. 1058 [RFC6020] Bjorklund, M., "YANG - A Data Modeling Language for the 1059 Network Configuration Protocol (NETCONF)", RFC 6020, 1060 October 2010. 1062 [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type 1063 Specifications and Registration Procedures", BCP 13, RFC 1064 6838, January 2013. 1066 Appendix A. Examples 1068 The following table provides some CBOR encoded values in hexadecimal 1069 (right column), together with diagnostic notation for these values 1070 (left column). Note that the string "\u00fc" is one form of 1071 diagnostic notation for a UTF-8 string containing the single Unicode 1072 character U+00FC, LATIN SMALL LETTER U WITH DIAERESIS (u umlaut). 1073 Similarly, "\u6c34" is a UTF-8 string in diagnostic notation with a 1074 single character U+6C34 (CJK UNIFIED IDEOGRAPH-6C34, often 1075 representing "water"), and "\ud800\udd51" is a UTF-8 string in 1076 diagnostic notation with a single character U+10151 (GREEK ACROPHONIC 1077 ATTIC FIFTY STATERS). (Note that all these single-character strings 1078 could also be represented in native UTF-8 in diagnostic notation, 1079 just not in an ASCII-only specification like the present one.) 1081 +--------------------+----------------------------------------------+ 1082 | Diagnostic | Encoded | 1083 +--------------------+----------------------------------------------+ 1084 | 0 | 0x00 | 1085 | | | 1086 | 1 | 0x01 | 1087 | | | 1088 | 10 | 0x0a | 1089 | | | 1090 | 27 | 0x1b | 1091 | | | 1092 | 28 | 0x1c1c | 1093 | | | 1094 | 29 | 0x1c1d | 1095 | | | 1096 | 100 | 0x1c64 | 1097 | | | 1098 | 1000 | 0x1d03e8 | 1099 | | | 1100 | 1000000 | 0x1e000f4240 | 1101 | | | 1102 | 1000000000000 | 0x1f000000e8d4a51000 | 1103 | | | 1104 | 184467440737095516 | 0x1fffffffffffffffff | 1105 | 15 | | 1106 | | | 1107 | 184467440737095516 | 0xe249010000000000000000 | 1108 | 16 | | 1109 | | | 1110 | -18446744073709551 | 0x3fffffffffffffffff | 1111 | 616 | | 1112 | | | 1113 | -18446744073709551 | 0xe349010000000000000000 | 1114 | 617 | | 1115 | | | 1116 | -1 | 0x20 | 1117 | | | 1118 | -10 | 0x29 | 1119 | | | 1120 | -100 | 0x3c63 | 1121 | | | 1122 | -1000 | 0x3d03e7 | 1123 | | | 1124 | 0.0 | 0xdd0000 | 1125 | | | 1126 | -0.0 | 0xdd8000 | 1127 | | | 1128 | 1.0 | 0xdd3c00 | 1129 | | | 1130 | 1.1 | 0xdf3ff199999999999a | 1131 | | | 1132 | 1.5 | 0xdd3e00 | 1133 | | | 1134 | 65504.0 | 0xdd7bff | 1135 | | | 1136 | 100000.0 | 0xde47c35000 | 1137 | | | 1138 | 3.4028234663852886 | 0xde7f7fffff | 1139 | e+38 | | 1140 | | | 1141 | 1.0e+300 | 0xdf7e37e43c8800759c | 1142 | | | 1143 | 5.960464477539063e | 0xdd0001 | 1144 | -08 | | 1145 | | | 1146 | 6.103515625e-05 | 0xdd0400 | 1147 | | | 1148 | -4.0 | 0xddc400 | 1149 | | | 1150 | -4.1 | 0xdfc010666666666666 | 1151 | | | 1152 | Infinity | 0xdd7c00 | 1153 | | | 1154 | NaN | 0xdd7e00 | 1155 | | | 1156 | -Infinity | 0xddfc00 | 1157 | | | 1158 | Infinity | 0xde7f800000 | 1159 | | | 1160 | NaN | 0xde7fc00000 | 1161 | | | 1162 | -Infinity | 0xdeff800000 | 1163 | | | 1164 | Infinity | 0xdf7ff0000000000000 | 1165 | | | 1166 | NaN | 0xdf7ff8000000000000 | 1167 | | | 1168 | -Infinity | 0xdffff0000000000000 | 1169 | | | 1170 | false | 0xd8 | 1171 | | | 1172 | true | 0xd9 | 1173 | | | 1174 | nil | 0xda | 1175 | | | 1176 | undefined | 0xdb | 1177 | | | 1178 | simple(16) | 0xd0 | 1179 | | | 1180 | simple(28) | 0xdc1c | 1181 | | | 1182 | simple(255) | 0xdcff | 1183 | | | 1184 | 0("2013-03-21T20:0 | 0xe074323031332d30332d32315432303a30343a3030 | 1185 | 4:00Z") | 5a | 1186 | | | 1187 | 1(1363896240) | 0xe11e514b67b0 | 1188 | | | 1189 | 1(1363896240.5) | 0xe1df41d452d9ec200000 | 1190 | | | 1191 | 27(h'01020304') | 0xfb4401020304 | 1192 | | | 1193 | 28(h'6449455446') | 0xfc1c456449455446 | 1194 | | | 1195 | 32("http://www.exa | 0xfc2076687474703a2f2f7777772e6578616d706c65 | 1196 | mple.com") | 2e636f6d | 1197 | | | 1198 | h'' | 0x40 | 1199 | | | 1200 | h'01020304' | 0x4401020304 | 1201 | | | 1202 | "" | 0x60 | 1203 | | | 1204 | "a" | 0x6161 | 1205 | | | 1206 | "IETF" | 0x6449455446 | 1207 | | | 1208 | "\"\\" | 0x62225c | 1209 | | | 1210 | "\u00fc" | 0x62c3bc | 1211 | | | 1212 | "\u6c34" | 0x63e6b0b4 | 1213 | | | 1214 | "\ud800\udd51" | 0x64f0908591 | 1215 | | | 1216 | [] | 0x80 | 1217 | | | 1218 | [1, 2, 3] | 0x83010203 | 1219 | | | 1220 | [1, [2, 3], [4, | 0x8301820203820405 | 1221 | 5]] | | 1222 | | | 1223 | [1, 2, 3, 4, 5, 6, | 0x9c1e0102030405060708090a0b0c0d0e0f10111213 | 1224 | 7, 8, 9, 10, 11, | 1415161718191a1b1c1c1c1d1c1e | 1225 | 12, 13, 14, 15, | | 1226 | 16, 17, 18, 19, | | 1227 | 20, 21, 22, 23, | | 1228 | 24, 25, 26, 27, | | 1229 | 28, 29, 30] | | 1230 | | | 1231 | {} | 0xa0 | 1232 | | | 1233 | {1: 2, 3: 4} | 0xa201020304 | 1234 | | | 1235 | {"a": 1, "b": [2, | 0xa26161016162820203 | 1236 | 3]} | | 1237 | | | 1238 | ["a", {"b": "c"}] | 0x826161a161626163 | 1239 | | | 1240 | {"a": "A", "b": | 0xa56161614161626142616361436164614461656145 | 1241 | "B", "c": "C", | | 1242 | "d": "D", "e": | | 1243 | "E"} | | 1244 +--------------------+----------------------------------------------+ 1246 TBD: add more examples? 1248 Appendix B. Jump Table 1250 For brevity, this jump table does not show initial bytes that are 1251 reserved for future extension. It also only shows a selection of the 1252 initial bytes that can be used for optional features. (All unsigned 1253 integers are in network byte order.) 1255 TBD: check again that we have all the single-byte tags represented in 1256 the table 1258 +-----------------+-------------------------------------------------+ 1259 | Byte | Structure/Semantics | 1260 +-----------------+-------------------------------------------------+ 1261 | 0x00..0x1b | Integer 0x00..0x1b (0..27) | 1262 | | | 1263 | 0x1c | Unsigned integer (one-byte uint8_t follows) | 1264 | | | 1265 | 0x1d | Unsigned integer (two-byte uint16_t follows) | 1266 | | | 1267 | 0x1e | Unsigned integer (four-byte uint32_t follows) | 1268 | | | 1269 | 0x1f | Unsigned integer (eight-byte uint64_t follows) | 1270 | | | 1271 | 0x20..0x3b | Negative Integer -1-0x00..-1-0x1b (-1..-28) | 1272 | | | 1273 | 0x3c | Negative Integer -1-n (one-byte uint8_t for n | 1274 | | follows) | 1275 | | | 1276 | 0x3d | Negative integer -1-n (two-byte uint16_t for n | 1277 | | follows) | 1278 | | | 1279 | 0x3e | Negative integer -1-n (four-byte uint32_t for n | 1280 | | follows) | 1281 | | | 1282 | 0x3f | Negative integer -1-n (eight-byte uint64_t for | 1283 | | n follows) | 1284 | | | 1285 | 0x40..0x5b | byte string (0x00..0x1b bytes follow) | 1286 | | | 1287 | 0x5c | byte string (one-byte uint8_t for n, and then n | 1288 | | bytes follow) | 1289 | | | 1290 | 0x5d | byte string (two-byte uint16_t for n, and then | 1291 | | n bytes follow) | 1292 | | | 1293 | 0x5e | byte string (four-byte uint32_t for n, and then | 1294 | | n bytes follow) | 1295 | | | 1296 | 0x5f | byte string (eight-byte uint64_t for n, and | 1297 | | then n bytes follow) | 1298 | | | 1299 | 0x60..0x7b | UTF-8 string (0x00..0x1b bytes follow) | 1300 | | | 1301 | 0x7c | UTF-8 string (one-byte uint8_t for n, and then | 1302 | | n bytes follow) | 1303 | | | 1304 | 0x7d | UTF-8 string (two-byte uint16_t for n, and then | 1305 | | n bytes follow) | 1306 | | | 1307 | 0x7e | UTF-8 string (four-byte uint32_t for n, and | 1308 | | then n bytes follow) | 1309 | | | 1310 | 0x7f | UTF-8 string (eight-byte uint64_t for n, and | 1311 | | then n bytes follow) | 1312 | | | 1313 | 0x80..0x9b | array (0x00..0x1b data items follow) | 1314 | | | 1315 | 0x9c | array (one-byte uint8_t for n, and then n data | 1316 | | items follow) | 1317 | | | 1318 | 0x9d | array (two-byte uint16_t for n, and then n data | 1319 | | items follow) | 1320 | | | 1321 | 0x9e | array (four-byte uint32_t for n, and then n | 1322 | | data items follow) | 1323 | | | 1324 | 0x9f | array (eight-byte uint64_t for n, and then n | 1325 | | data items follow) | 1326 | | | 1327 | 0xa0..0xbb | map (0x00..0x1b pairs of data items follow) | 1328 | | | 1329 | 0xbc | map (one-byte uint8_t for n, and then n pairs | 1330 | | of data items follow) | 1331 | | | 1332 | 0xbd | map (two-byte uint16_t for n, and then n pairs | 1333 | | of data items follow) | 1334 | | | 1335 | 0xbe | map (four-byte uint32_t for n, and then n pairs | 1336 | | of data items follow) | 1337 | | | 1338 | 0xbf | map (eight-byte uint64_t for n, and then n | 1339 | | pairs of data items follow) | 1340 | | | 1341 | 0xd8 | False | 1342 | | | 1343 | 0xd9 | True | 1344 | | | 1345 | 0xda | Null | 1346 | | | 1347 | 0xdb | Undefined | 1348 | | | 1349 | 0xdd | Half-Precision Float (two-byte IEEE 754) | 1350 | | | 1351 | 0xde | Single-Precision Float (four-byte IEEE 754) | 1352 | | | 1353 | 0xdf | Double-Precision Float (eight-byte IEEE 754) | 1354 | | | 1355 | 0xe0 | Text-based date/time (data item follows, see | 1356 | | Section 2.3.1) | 1357 | | | 1358 | 0xe1 | Epoch-based date/time (data item follows, see | 1359 | | Section 2.3.1) | 1360 | | | 1361 | 0xe2 | Positive bignum (data item "byte string" | 1362 | | follows) | 1363 | | | 1364 | 0xe3 | Negative bignum (data item "byte string" | 1365 | | follows) | 1366 | | | 1367 | 0xe4 | Decimal Fraction (data item "array" follows, | 1368 | | see Section 2.3.3 | 1369 | | | 1370 | 0xf9..0xfb | Expected Conversion (data item follows, see | 1371 | | Section 2.3.4.2) | 1372 | | | 1373 | 0xfc | (more tagged items, one byte and then a data | 1374 | | item follow) | 1375 +-----------------+-------------------------------------------------+ 1377 Table 4: Jump Table for Initial Byte 1379 Appendix C. Pseudocode 1381 The well-formedness of a CBOR item can be checked by the pseudo-code 1382 in Figure 1. The data is well-formed, iff: 1384 o the pseudo-code does not "fail"; 1386 o after execution of the pseudo-code, no bytes are left in the input 1387 (except in streaming applications) 1389 The pseudo-code has the following prerequisites: 1391 o take(n) reads n bytes from the input data and returns them as a 1392 byte string. If n bytes are no longer available, take(n) fails. 1394 o uint() converts a byte string into an unsigned integer by 1395 interpreting the byte string in network byte order. 1397 o Arithmetic works as in C. 1399 o All variables are unsigned integers of sufficient range. 1401 well_formed () { 1402 // process initial bytes 1403 ib = uint(take(1)); 1404 mt = ib >> 5; 1405 val = ai = ib & 0x1f; 1406 switch (ai) { 1407 case 28: val = uint(take(1)); break; 1408 case 29: val = uint(take(2)); break; 1409 case 30: val = uint(take(4)); break; 1410 case 31: val = uint(take(8)); break; 1411 } 1412 // process content 1413 switch (mt) { 1414 // case 0, 1, 6 do not have content; use val 1415 case 2: case 3: take(val); break; // bytes/UTF-8 1416 case 4: for (i = 0; i < val; i++) well_formed(); break; 1417 case 5: for (i = 0; i < val*2; i++) well_formed(); break; 1418 case 7: well_formed(); break; // 1 embedded data item 1419 } 1420 } 1422 Figure 1: Pseudo-Code for well-formedness check 1424 Note that the remaining complexity of a complete CBOR decoder is 1425 about presenting data that has been parsed to the application in an 1426 appropriate form. 1428 Major types 0 and 1 are designed in such a way that they can be 1429 encoded in C from a signed integer without actually doing an if-then- 1430 else for positive/negative (Figure 2). This uses the fact that 1431 (-1-n), the transformation for major type 1, is the same as ~n 1432 (bitwise complement) in C unsigned arithmetic, ~n can then be 1433 expressed as (-1)^n for the negative case, while 0^n leaves n 1434 unchanged for non-negative. The sign of a number can be converted to 1435 -1 for negative and 0 for non-negative (0 or positive) by arithmetic- 1436 shifting the number by one bit less than the bit length of the number 1437 (e.g., by 63 for 64-bit numbers). 1439 void encode_sint(int64_t n) { 1440 uint64t ui = n >> 63; // extend sign to whole length 1441 mt = ui & 0x20; // extract major type 1442 ui ^= n; // complement negatives 1443 if (ui < 28) 1444 *p++ = mt + ui; 1445 else if (ui < 256) { 1446 *p++ = mt + 28; 1447 *p++ = ui; 1448 } else 1449 ... 1451 Figure 2: Pseudo-code for encoding a signed integer 1453 Appendix D. Half-precision 1455 As half-precision floating point numbers were only added to IEEE 754 1456 in 2008, today's programming platforms often still only have limited 1457 support for them. It is very easy to include at least decoding 1458 support for them even without such support. An example of a small 1459 decoder for half-precision floating point numbers in the C language 1460 is shown in Figure 3. This code assumes that the 2-byte value has 1461 already been parsed as an unsigned integer in network byte order (as 1462 would be done by the pseudocode in Appendix C). A similar program 1463 for Python is in Figure 4. 1465 #include 1467 double decode_half(int half) { 1468 int exp = (half >> 10) & 0x1f; 1469 int mant = half & 0x3ff; 1470 double val; 1471 if (exp == 0) val = ldexp(mant, -24); 1472 else if (exp != 31) val = ldexp(mant + 1024, exp - 25); 1473 else val = mant == 0 ? INFINITY : NAN; 1474 return half & 0x8000 ? -val : val; 1475 } 1476 Figure 3: C code for a half-precision decoder 1478 import struct 1479 from math import ldexp 1481 def decode_single(single): 1482 return struct.unpack("!f", struct.pack("!I", single))[0] 1484 def decode_half(half): 1485 valu = (half & 0x7fff) << 13 | (half & 0x8000) << 16 1486 if ((half & 0x7c00) != 0x7c00): 1487 return ldexp(decode_single(valu), 112) 1488 return decode_single(valu | 0x7f800000) 1490 Figure 4: Python code for a half-precision decoder 1492 Authors' Addresses 1494 Carsten Bormann 1495 Universitaet Bremen TZI 1496 Postfach 330440 1497 D-28359 Bremen 1498 Germany 1500 Phone: +49-421-218-63921 1501 Email: cabo@tzi.org 1503 Paul Hoffman 1504 VPN Consortium 1506 Email: paul.hoffman@vpnc.org