-
Notifications
You must be signed in to change notification settings - Fork 3
/
rfc7049.xml
2849 lines (2443 loc) · 113 KB
/
rfc7049.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet type='text/xsl' href='rfc2629.xslt' ?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
<!ENTITY RFC2045 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2045.xml">
<!ENTITY RFC2119 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2119.xml">
<!ENTITY RFC3339 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.3339.xml">
<!ENTITY RFC3629 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.3629.xml">
<!ENTITY RFC3986 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.3986.xml">
<!ENTITY RFC4287 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.4287.xml">
<!ENTITY RFC4648 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.4648.xml">
<!ENTITY RFC5226 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5226.xml">
<!ENTITY RFC0713 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.0713.xml">
<!ENTITY RFC4627 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.4627.xml">
<!ENTITY RFC6838 SYSTEM "https://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6838.xml">
]>
<rfc submissionType="IETF" number="7049" category="std" consensus="yes">
<!-- Generated by id2xml 1.4.4 on 2019-05-13T05:48:44Z -->
<?rfc compact="yes"?>
<?rfc text-list-symbols="o*+-"?>
<?rfc subcompact="no"?>
<?rfc sortrefs="yes"?>
<?rfc symrefs="yes"?>
<?rfc strict="yes"?>
<?rfc toc="yes"?>
<front>
<title abbrev="CBOR">Concise Binary Object Representation (CBOR)</title>
<author fullname="Carsten Bormann" initials="C." surname="Bormann">
<organization>Universitaet Bremen TZI</organization>
<address><postal><street>Postfach 330440</street>
<street>D-28359 Bremen</street>
<street>Germany</street>
</postal>
<phone>+49-421-218-63921</phone>
<email>[email protected]</email>
</address>
</author>
<author fullname="Paul Hoffman" initials="P." surname="Hoffman">
<organization>VPN Consortium</organization>
<address><email>[email protected]</email>
</address>
</author>
<date month="October" year="2013"/>
<abstract><t>
The Concise Binary Object Representation (CBOR) is a data format
whose design goals include the possibility of extremely small code
size, fairly small message size, and extensibility without the need
for version negotiation. These design goals make it different from
earlier binary serializations such as ASN.1 and MessagePack.</t>
</abstract>
</front>
<middle>
<section title="Introduction" anchor="section-1"><t>
There are hundreds of standardized formats for binary representation
of structured data (also known as binary serialization formats). Of
those, some are for specific domains of information, while others are
generalized for arbitrary data. In the IETF, probably the best-known
formats in the latter category are ASN.1's BER and DER <xref target="ASN.1"/>.</t>
<t>
The format defined here follows some specific design goals that are
not well met by current formats. The underlying data model is an
extended version of the JSON data model <xref target="RFC4627"/>. It is important
to note that this is not a proposal that the grammar in RFC 4627 be
extended in general, since doing so would cause a significant
backwards incompatibility with already deployed JSON documents.
Instead, this document simply defines its own data model that starts
from JSON.</t>
<t>
Appendix E lists some existing binary formats and discusses how well
they do or do not fit the design objectives of the Concise Binary
Object Representation (CBOR).</t>
<section title="Objectives" anchor="section-1.1"><t>
The objectives of CBOR, roughly in decreasing order of importance,
are:</t>
<t><list style="numbers"><t>The representation must be able to unambiguously encode most
common data formats used in Internet standards.<list style="symbols"><t>It must represent a reasonable set of basic data types and
structures using binary encoding. "Reasonable" here is
largely influenced by the capabilities of JSON, with the major
addition of binary byte strings. The structures supported are
limited to arrays and trees; loops and lattice-style graphs
are not supported.</t>
<t>There is no requirement that all data formats be uniquely
encoded; that is, it is acceptable that the number "7" might
be encoded in multiple different ways.</t>
</list>
</t>
<t>The code for an encoder or decoder must be able to be compact in
order to support systems with very limited memory, processor
power, and instruction sets.<list style="symbols"><t>An encoder and a decoder need to be implementable in a very
small amount of code (for example, in class 1 constrained
nodes as defined in <xref target="CNN-TERMS"/>).</t>
<t>The format should use contemporary machine representations of
data (for example, not requiring binary-to-decimal
conversion).</t>
</list>
</t>
<t>Data must be able to be decoded without a schema description.<list style="symbols"><t>Similar to JSON, encoded data should be self-describing so
that a generic decoder can be written.</t>
</list>
</t>
<t>The serialization must be reasonably compact, but data
compactness is secondary to code compactness for the encoder and
decoder.<list style="symbols"><t>"Reasonable" here is bounded by JSON as an upper bound in
size, and by implementation complexity maintaining a lower
bound. Using either general compression schemes or extensive
bit-fiddling violates the complexity goals.</t>
</list>
</t>
<t>The format must be applicable to both constrained nodes and high-
volume applications.<list style="symbols"><t>This means it must be reasonably frugal in CPU usage for both
encoding and decoding. This is relevant both for constrained
nodes and for potential usage in applications with a very high
volume of data.</t>
</list>
</t>
<t>The format must support all JSON data types for conversion to and
from JSON.<list style="symbols"><t>It must support a reasonable level of conversion as long as
the data represented is within the capabilities of JSON. It
must be possible to define a unidirectional mapping towards
JSON for all types of data.</t>
</list>
</t>
<t>The format must be extensible, and the extended data must be
decodable by earlier decoders.<list style="symbols"><t>The format is designed for decades of use.</t>
<t>The format must support a form of extensibility that allows
fallback so that a decoder that does not understand an
extension can still decode the message.</t>
<t>The format must be able to be extended in the future by later
IETF standards.</t>
</list>
</t>
</list>
</t>
</section>
<section title="Terminology" anchor="section-1.2"><t>
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in RFC 2119, BCP 14
<xref target="RFC2119"/> and indicate requirement levels for compliant CBOR
implementations.</t>
<t>
The term "byte" is used in its now-customary sense as a synonym for
"octet". All multi-byte values are encoded in network byte order
(that is, most significant byte first, also known as "big-endian").</t>
<t>
This specification makes use of the following terminology:</t>
<t><list style="hanging" hangIndent="3"><t hangText="Data item:">
A single piece of CBOR data. The structure of a data
<vspace blankLines="0"/>
item may contain zero, one, or more nested data items. The term
is used both for the data item in representation format and for
the abstract idea that can be derived from that by a decoder.
</t>
<t hangText="Decoder:">
A process that decodes a CBOR data item and makes it
<vspace blankLines="0"/>
available to an application. Formally speaking, a decoder
contains a parser to break up the input using the syntax rules of
CBOR, as well as a semantic processor to prepare the data in a
form suitable to the application.
</t>
<t hangText="Encoder:">
A process that generates the representation format of a
<vspace blankLines="0"/>
CBOR data item from application information.
</t>
<t hangText="Data Stream:">
A sequence of zero or more data items, not further
<vspace blankLines="0"/>
assembled into a larger containing data item. The independent
data items that make up a data stream are sometimes also referred
to as "top-level data items".
</t>
<t hangText="Well-formed:">
A data item that follows the syntactic structure of
<vspace blankLines="0"/>
CBOR. A well-formed data item uses the initial bytes and the byte
strings and/or data items that are implied by their values as
defined in CBOR and is not followed by extraneous data.
</t>
<t hangText="Valid:">
A data item that is well-formed and also follows the semantic
<vspace blankLines="0"/>
restrictions that apply to CBOR data items.
</t>
<t hangText="Stream decoder:">
A process that decodes a data stream and makes each
<vspace blankLines="0"/>
of the data items in the sequence available to an application as
they are received.
</t>
</list>
</t>
<t>
Where bit arithmetic or data types are explained, this document uses
the notation familiar from the programming language C, except that
"**" denotes exponentiation. Similar to the "0x" notation for
hexadecimal numbers, numbers in binary notation are prefixed with
"0b". Underscores can be added to such a number solely for
readability, so 0b00100001 (0x21) might be written 0b001_00001 to
emphasize the desired interpretation of the bits in the byte; in this
case, it is split into three bits and five bits.</t>
</section>
</section>
<section title="Specification of the CBOR Encoding" anchor="section-2"><t>
A CBOR-encoded data item is structured and encoded as described in
this section. The encoding is summarized in Table 5.</t>
<t>
The initial byte of each data item contains both information about
the major type (the high-order 3 bits, described in <xref target="section-2.1"/>) and
additional information (the low-order 5 bits). When the value of the
additional information is less than 24, it is directly used as a
small unsigned integer. When it is 24 to 27, the additional bytes
for a variable-length integer immediately follow; the values 24 to 27
of the additional information specify that its length is a 1-, 2-,
4-, or 8-byte unsigned integer, respectively. Additional information
value 31 is used for indefinite-length items, described in
<xref target="section-2.2"/>. Additional information values 28 to 30 are reserved for
future expansion.</t>
<t>
In all additional information values, the resulting integer is
interpreted depending on the major type. It may represent the actual
data: for example, in integer types, the resulting integer is used
for the value itself. It may instead supply length information: for
example, in byte strings it gives the length of the byte string data
that follows.</t>
<t>
A CBOR decoder implementation can be based on a jump table with all
256 defined values for the initial byte (Table 5). A decoder in a
constrained implementation can instead use the structure of the
initial byte and following bytes for more compact code (see
Appendix C for a rough impression of how this could look).</t>
<section title="Major Types" anchor="section-2.1"><t>
The following lists the major types and the additional information
and other bytes associated with the type.</t>
<t><list style="hanging" hangIndent="3"><t hangText="Major type 0:">
an unsigned integer. The 5-bit additional information
<vspace blankLines="0"/>
is either the integer itself (for additional information values 0
through 23) or the length of additional data. Additional
information 24 means the value is represented in an additional
uint8_t, 25 means a uint16_t, 26 means a uint32_t, and 27 means a
uint64_t. For example, the integer 10 is denoted as the one byte
0b000_01010 (major type 0, additional information 10). The
integer 500 would be 0b000_11001 (major type 0, additional
information 25) followed by the two bytes 0x01f4, which is 500 in
decimal.
</t>
<t hangText="Major type 1:">
a negative integer. The encoding follows the rules
<vspace blankLines="0"/>
for unsigned integers (major type 0), except that the value is
then -1 minus the encoded unsigned integer. For example, the
integer -500 would be 0b001_11001 (major type 1, additional
information 25) followed by the two bytes 0x01f3, which is 499 in
decimal.
</t>
<t hangText="Major type 2:">
a byte string. The string's length in bytes is
<vspace blankLines="0"/>
represented following the rules for positive integers (major type
0). For example, a byte string whose length is 5 would have an
initial byte of 0b010_00101 (major type 2, additional information
5 for the length), followed by 5 bytes of binary content. A byte
string whose length is 500 would have 3 initial bytes of
<vspace blankLines="1"/>
0b010_11001 (major type 2, additional information 25 to indicate a
two-byte length) followed by the two bytes 0x01f4 for a length of
500, followed by 500 bytes of binary content.
</t>
<t hangText="Major type 3:">
a text string, specifically a string of Unicode
<vspace blankLines="0"/>
characters that is encoded as UTF-8 <xref target="RFC3629"/>. The format of this
type is identical to that of byte strings (major type 2), that is,
as with major type 2, the length gives the number of bytes. This
type is provided for systems that need to interpret or display
human-readable text, and allows the differentiation between
unstructured bytes and text that has a specified repertoire and
encoding. In contrast to formats such as JSON, the Unicode
characters in this type are never escaped. Thus, a newline
character (U+000A) is always represented in a string as the byte
0x0a, and never as the bytes 0x5c6e (the characters "\" and "n")
or as 0x5c7530303061 (the characters "\", "u", "0", "0", "0", and
"a").
</t>
<t hangText="Major type 4:">
an array of data items. Arrays are also called lists,
<vspace blankLines="0"/>
sequences, or tuples. The array's length follows the rules for
byte strings (major type 2), except that the length denotes the
number of data items, not the length in bytes that the array takes
up. Items in an array do not need to all be of the same type.
For example, an array that contains 10 items of any type would
have an initial byte of 0b100_01010 (major type of 4, additional
information of 10 for the length) followed by the 10 remaining
items.
</t>
<t hangText="Major type 5:">
a map of pairs of data items. Maps are also called
<vspace blankLines="0"/>
tables, dictionaries, hashes, or objects (in JSON). A map is
comprised of pairs of data items, each pair consisting of a key
that is immediately followed by a value. The map's length follows
the rules for byte strings (major type 2), except that the length
denotes the number of pairs, not the length in bytes that the map
takes up. For example, a map that contains 9 pairs would have an
initial byte of 0b101_01001 (major type of 5, additional
information of 9 for the number of pairs) followed by the 18
remaining items. The first item is the first key, the second item
is the first value, the third item is the second key, and so on.
A map that has duplicate keys may be well-formed, but it is not
valid, and thus it causes indeterminate decoding; see also
<xref target="section-3.7"/>.
</t>
<t hangText="Major type 6:">
optional semantic tagging of other major types. See
<vspace blankLines="0"/>
<xref target="section-2.4"/>.
</t>
<t hangText="Major type 7:">
floating-point numbers and simple data types that need
<vspace blankLines="0"/>
no content, as well as the "break" stop code. See <xref target="section-2.3"/>.
</t>
</list>
</t>
<t>
These eight major types lead to a simple table showing which of the
256 possible values for the initial byte of a data item are used
(Table 5).</t>
<t>
In major types 6 and 7, many of the possible values are reserved for
future specification. See <xref target="section-7"/> for more information on these
values.</t>
</section>
<section title="Indefinite Lengths for Some Major Types" anchor="section-2.2"><t>
Four CBOR items (arrays, maps, byte strings, and text strings) can be
encoded with an indefinite length using additional information value
31. This is useful if the encoding of the item needs to begin before
the number of items inside the array or map, or the total length of
the string, is known. (The application of this is often referred to
as "streaming" within a data item.)</t>
<t>
Indefinite-length arrays and maps are dealt with differently than
indefinite-length byte strings and text strings.</t>
<section title="Indefinite-Length Arrays and Maps" anchor="section-2.2.1"><t>
Indefinite-length arrays and maps are simply opened without
indicating the number of data items that will be included in the
array or map, using the additional information value of 31. The
initial major type and additional information byte is followed by the
elements of the array or map, just as they would be in other arrays
or maps. The end of the array or map is indicated by encoding a
"break" stop code in a place where the next data item would normally
have been included. The "break" is encoded with major type 7 and
additional information value 31 (0b111_11111) but is not itself a
data item: it is just a syntactic feature to close the array or map.
That is, the "break" stop code comes after the last item in the array
or map, and it cannot occur anywhere else in place of a data item.
In this way, indefinite-length arrays and maps look identical to
other arrays and maps except for beginning with the additional
information value 31 and ending with the "break" stop code.</t>
<t>
Arrays and maps with indefinite lengths allow any number of items
(for arrays) and key/value pairs (for maps) to be given before the
"break" stop code. There is no restriction against nesting
indefinite-length array or map items. A "break" only terminates a
single item, so nested indefinite-length items need exactly as many
"break" stop codes as there are type bytes starting an indefinite-
length item.</t>
<t>
For example, assume an encoder wants to represent the abstract array
[1, [2, 3], [4, 5]]. The definite-length encoding would be
0x8301820203820405:</t>
<figure><artwork><![CDATA[
83 -- Array of length 3
01 -- 1
82 -- Array of length 2
02 -- 2
03 -- 3
82 -- Array of length 2
04 -- 4
05 -- 5
]]></artwork>
</figure>
<t>
Indefinite-length encoding could be applied independently to each of
the three arrays encoded in this data item, as required, leading to
representations such as:</t>
<figure><artwork><![CDATA[
0x9f018202039f0405ffff
9F -- Start indefinite-length array
01 -- 1
82 -- Array of length 2
02 -- 2
03 -- 3
9F -- Start indefinite-length array
04 -- 4
05 -- 5
FF -- "break" (inner array)
FF -- "break" (outer array)
0x9f01820203820405ff
9F -- Start indefinite-length array
01 -- 1
82 -- Array of length 2
02 -- 2
03 -- 3
82 -- Array of length 2
04 -- 4
05 -- 5
FF -- "break"
0x83018202039f0405ff
83 -- Array of length 3
01 -- 1
82 -- Array of length 2
02 -- 2
03 -- 3
9F -- Start indefinite-length array
04 -- 4
05 -- 5
FF -- "break"
0x83019f0203ff820405
83 -- Array of length 3
01 -- 1
9F -- Start indefinite-length array
02 -- 2
03 -- 3
FF -- "break"
82 -- Array of length 2
04 -- 4
05 -- 5
]]></artwork>
</figure>
<t>
An example of an indefinite-length map (that happens to have two
key/value pairs) might be:</t>
<figure><artwork><![CDATA[
0xbf6346756ef563416d7421ff
BF -- Start indefinite-length map
63 -- First key, UTF-8 string length 3
46756e -- "Fun"
F5 -- First value, true
63 -- Second key, UTF-8 string length 3
416d74 -- "Amt"
21 -- -2
FF -- "break"
]]></artwork>
</figure>
</section>
<section title="Indefinite-Length Byte Strings and Text Strings" anchor="section-2.2.2"><t>
Indefinite-length byte strings and text strings are actually a
concatenation of zero or more definite-length byte or text strings
("chunks") that are together treated as one contiguous string.
Indefinite-length strings are opened with the major type and
additional information value of 31, but what follows are a series of
byte or text strings that have definite lengths (the chunks). The
end of the series of chunks is indicated by encoding the "break" stop
code (0b111_11111) in a place where the next chunk in the series
would occur. The contents of the chunks are concatenated together,
and the overall length of the indefinite-length string will be the
sum of the lengths of all of the chunks. In summary, an indefinite-
length string is encoded similarly to how an indefinite-length array
of its chunks would be encoded, except that the major type of the
indefinite-length string is that of a (text or byte) string and
matches the major types of its chunks.</t>
<t>
For indefinite-length byte strings, every data item (chunk) between
the indefinite-length indicator and the "break" MUST be a definite-
length byte string item; if the parser sees any item type other than
a byte string before it sees the "break", it is an error.</t>
<t>
For example, assume the sequence:</t>
<t>
0b010_11111 0b010_00100 0xaabbccdd 0b010_00011 0xeeff99 0b111_11111</t>
<figure><artwork><![CDATA[
5F -- Start indefinite-length byte string
44 -- Byte string of length 4
aabbccdd -- Bytes content
43 -- Byte string of length 3
eeff99 -- Bytes content
FF -- "break"
]]></artwork>
</figure>
<t>
After decoding, this results in a single byte string with seven
bytes: 0xaabbccddeeff99.</t>
<t>
Text strings with indefinite lengths act the same as byte strings
with indefinite lengths, except that all their chunks MUST be
definite-length text strings. Note that this implies that the bytes
of a single UTF-8 character cannot be spread between chunks: a new
chunk can only be started at a character boundary.</t>
</section>
</section>
<section title="Floating-Point Numbers and Values with No Content" anchor="section-2.3"><t>
Major type 7 is for two types of data: floating-point numbers and
"simple values" that do not need any content. Each value of the
5-bit additional information in the initial byte has its own separate
meaning, as defined in Table 1. Like the major types for integers,
items of this major type do not carry content data; all the
information is in the initial bytes.</t>
<texttable title="Values for Additional Information in Major Type 7" anchor="ref-values-for-additional-information-in-major-type-7" style="full"><ttcol> 5-Bit Value</ttcol>
<ttcol> Semantics</ttcol>
<c>0..23</c>
<c>Simple value (value 0..23)</c>
<c>24</c>
<c>Simple value (value 32..255 in following byte)</c>
<c>25</c>
<c>IEEE 754 Half-Precision Float (16 bits follow)</c>
<c>26</c>
<c>IEEE 754 Single-Precision Float (32 bits follow)</c>
<c>27</c>
<c>IEEE 754 Double-Precision Float (64 bits follow)</c>
<c>28-30</c>
<c>(Unassigned)</c>
<c>31</c>
<c>"break" stop code for indefinite-length items</c>
</texttable>
<t>
As with all other major types, the 5-bit value 24 signifies a single-
byte extension: it is followed by an additional byte to represent the
simple value. (To minimize confusion, only the values 32 to 255 are
used.) This maintains the structure of the initial bytes: as for the
other major types, the length of these always depends on the
additional information in the first byte. Table 2 lists the values
assigned and available for simple types.</t>
<texttable title="Simple Values" anchor="ref-simple-values" style="full"><ttcol> Value</ttcol>
<ttcol> Semantics</ttcol>
<c>0..19</c>
<c>(Unassigned)</c>
<c>20</c>
<c>False</c>
<c>21</c>
<c>True</c>
<c>22</c>
<c>Null</c>
<c>23</c>
<c>Undefined value</c>
<c>24..31</c>
<c>(Reserved)</c>
<c>32..255</c>
<c>(Unassigned)</c>
</texttable>
<t>
The 5-bit values of 25, 26, and 27 are for 16-bit, 32-bit, and 64-bit
IEEE 754 binary floating-point values. These floating-point values
are encoded in the additional bytes of the appropriate size. (See
Appendix D for some information about 16-bit floating point.)</t>
</section>
<section title="Optional Tagging of Items" anchor="section-2.4"><t>
In CBOR, a data item can optionally be preceded by a tag to give it
additional semantics while retaining its structure. The tag is major
type 6, and represents an integer number as indicated by the tag's
integer value; the (sole) data item is carried as content data. If a
tag requires structured data, this structure is encoded into the
nested data item. The definition of a tag usually restricts what
kinds of nested data item or items can be carried by a tag.</t>
<t>
The initial bytes of the tag follow the rules for positive integers
(major type 0). The tag is followed by a single data item of any
type. For example, assume that a byte string of length 12 is marked
with a tag to indicate it is a positive bignum (<xref target="section-2.4.2"/>). This
would be marked as 0b110_00010 (major type 6, additional information
2 for the tag) followed by 0b010_01100 (major type 2, additional
information of 12 for the length) followed by the 12 bytes of the
bignum.</t>
<t>
Decoders do not need to understand tags, and thus tags may be of
little value in applications where the implementation creating a
particular CBOR data item and the implementation decoding that stream
know the semantic meaning of each item in the data flow. Their
primary purpose in this specification is to define common data types
such as dates. A secondary purpose is to allow optional tagging when
the decoder is a generic CBOR decoder that might be able to benefit
from hints about the content of items. Understanding the semantic
tags is optional for a decoder; it can just jump over the initial
bytes of the tag and interpret the tagged data item itself.</t>
<t>
A tag always applies to the item that is directly followed by it.
Thus, if tag A is followed by tag B, which is followed by data item
C, tag A applies to the result of applying tag B on data item C.
That is, a tagged item is a data item consisting of a tag and a
value. The content of the tagged item is the data item (the value)
that is being tagged.</t>
<t>
IANA maintains a registry of tag values as described in <xref target="section-7.2"/>.
Table 3 provides a list of initial values, with definitions in the
rest of this section.</t>
<texttable title="Values for Tags" anchor="ref-values-for-tags" style="full"><ttcol> Tag</ttcol>
<ttcol> Data Item</ttcol>
<ttcol> Semantics</ttcol>
<c>0</c>
<c>UTF-8 string</c>
<c>Standard date/time string; see</c>
<c></c>
<c></c>
<c>Section 2.4.1</c>
<c>1</c>
<c>multiple</c>
<c>Epoch-based date/time; see</c>
<c></c>
<c></c>
<c>Section 2.4.1</c>
<c>2</c>
<c>byte string</c>
<c>Positive bignum; see Section</c>
<c></c>
<c></c>
<c>2.4.2</c>
<c>3</c>
<c>byte string</c>
<c>Negative bignum; see Section</c>
<c></c>
<c></c>
<c>2.4.2</c>
<c>4</c>
<c>array</c>
<c>Decimal fraction; see Section</c>
<c></c>
<c></c>
<c>2.4.3</c>
<c>5</c>
<c>array</c>
<c>Bigfloat; see Section 2.4.3</c>
<c>6..20</c>
<c>(Unassigned)</c>
<c>(Unassigned)</c>
<c>21</c>
<c>multiple</c>
<c>Expected conversion to</c>
<c></c>
<c></c>
<c>base64url encoding; see</c>
<c></c>
<c></c>
<c>Section 2.4.4.2</c>
<c>22</c>
<c>multiple</c>
<c>Expected conversion to base64</c>
<c></c>
<c></c>
<c>encoding; see Section 2.4.4.2</c>
<c>23</c>
<c>multiple</c>
<c>Expected conversion to base16</c>
<c></c>
<c></c>
<c>encoding; see Section 2.4.4.2</c>
<c>24</c>
<c>byte string</c>
<c>Encoded CBOR data item; see</c>
<c></c>
<c></c>
<c>Section 2.4.4.1</c>
<c>25..31</c>
<c>(Unassigned)</c>
<c>(Unassigned)</c>
<c>32</c>
<c>UTF-8 string</c>
<c>URI; see Section 2.4.4.3</c>
<c>33</c>
<c>UTF-8 string</c>
<c>base64url; see Section 2.4.4.3</c>
<c>34</c>
<c>UTF-8 string</c>
<c>base64; see Section 2.4.4.3</c>
<c>35</c>
<c>UTF-8 string</c>
<c>Regular expression; see</c>
<c></c>
<c></c>
<c>Section 2.4.4.3</c>
<c>36</c>
<c>UTF-8 string</c>
<c>MIME message; see Section</c>
<c></c>
<c></c>
<c>2.4.4.3</c>
<c>37..55798</c>
<c>(Unassigned)</c>
<c>(Unassigned)</c>
<c>55799</c>
<c>multiple</c>
<c>Self-describe CBOR; see</c>
<c></c>
<c></c>
<c>Section 2.4.5</c>
<c>55800+</c>
<c>(Unassigned)</c>
<c>(Unassigned)</c>
</texttable>
<section title="Date and Time" anchor="section-2.4.1"><t>
Tag value 0 is for date/time strings that follow the standard format
described in <xref target="RFC3339"/>, as refined by Section 3.3 of <xref target="RFC4287"/>.</t>
<t>
Tag value 1 is for numerical representation of seconds relative to
1970-01-01T00:00Z in UTC time. (For the non-negative values that the
Portable Operating System Interface (POSIX) defines, the number of
seconds is counted in the same way as for POSIX "seconds since the epoch" [TIME_T].) The tagged item can be a positive or negative
integer (major types 0 and 1), or a floating-point number (major type
7 with additional information 25, 26, or 27). Note that the number
can be negative (time before 1970-01-01T00:00Z) and, if a floating-
point number, indicate fractional seconds.</t>
</section>
<section title="Bignums" anchor="section-2.4.2"><t>
Bignums are integers that do not fit into the basic integer
representations provided by major types 0 and 1. They are encoded as
a byte string data item, which is interpreted as an unsigned integer
n in network byte order. For tag value 2, the value of the bignum is
n. For tag value 3, the value of the bignum is -1 - n. Decoders
that understand these tags MUST be able to decode bignums that have
leading zeroes.</t>
<t>
For example, the number 18446744073709551616 (2**64) is represented
as 0b110_00010 (major type 6, tag 2), followed by 0b010_01001 (major
type 2, length 9), followed by 0x010000000000000000 (one byte 0x01
and eight bytes 0x00). In hexadecimal:</t>
<t><list style="hanging" hangIndent="3"><t hangText="C2">
-- Tag 2
<vspace blankLines="0"/>
29 -- Byte string of length 9
010000000000000000 -- Bytes content
</t>
</list>
</t>
</section>
<section title="Decimal Fractions and Bigfloats" anchor="section-2.4.3"><t>
Decimal fractions combine an integer mantissa with a base-10 scaling
factor. They are most useful if an application needs the exact
representation of a decimal fraction such as 1.1 because there is no
exact representation for many decimal fractions in binary floating
point.</t>
<t>
Bigfloats combine an integer mantissa with a base-2 scaling factor.
They are binary floating-point values that can exceed the range or
the precision of the three IEEE 754 formats supported by CBOR
(<xref target="section-2.3"/>). Bigfloats may also be used by constrained
applications that need some basic binary floating-point capability
without the need for supporting IEEE 754.</t>
<t>
A decimal fraction or a bigfloat is represented as a tagged array
that contains exactly two integer numbers: an exponent e and a
mantissa m. Decimal fractions (tag 4) use base-10 exponents; the
value of a decimal fraction data item is m*(10**e). Bigfloats (tag
5) use base-2 exponents; the value of a bigfloat data item is
m*(2**e). The exponent e MUST be represented in an integer of major
type 0 or 1, while the mantissa also can be a bignum (<xref target="section-2.4.2"/>).</t>
<t>
An example of a decimal fraction is that the number 273.15 could be
represented as 0b110_00100 (major type of 6 for the tag, additional
information of 4 for the type of tag), followed by 0b100_00010 (major
type of 4 for the array, additional information of 2 for the length
of the array), followed by 0b001_00001 (major type of 1 for the first
integer, additional information of 1 for the value of -2), followed
by 0b000_11001 (major type of 0 for the second integer, additional
information of 25 for a two-byte value), followed by
0b0110101010110011 (27315 in two bytes). In hexadecimal:</t>
<figure><artwork><![CDATA[
C4 -- Tag 4
82 -- Array of length 2
21 -- -2
19 6ab3 -- 27315
]]></artwork>
</figure>
<t>
An example of a bigfloat is that the number 1.5 could be represented
as 0b110_00101 (major type of 6 for the tag, additional information
of 5 for the type of tag), followed by 0b100_00010 (major type of 4
for the array, additional information of 2 for the length of the
array), followed by 0b001_00000 (major type of 1 for the first
integer, additional information of 0 for the value of -1), followed
by 0b000_00011 (major type of 0 for the second integer, additional
information of 3 for the value of 3). In hexadecimal:</t>
<figure><artwork><![CDATA[
C5 -- Tag 5
82 -- Array of length 2
20 -- -1
03 -- 3
]]></artwork>
</figure>
<t>
Decimal fractions and bigfloats provide no representation of
Infinity, -Infinity, or NaN; if these are needed in place of a
decimal fraction or bigfloat, the IEEE 754 half-precision
representations from <xref target="section-2.3"/> can be used. For constrained
applications, where there is a choice between representing a specific
number as an integer and as a decimal fraction or bigfloat (such as
when the exponent is small and non-negative), there is a quality-of-
implementation expectation that the integer representation is used
directly.</t>
</section>
<section title="Content Hints" anchor="section-2.4.4"><t>
The tags in this section are for content hints that might be used by
generic CBOR processors.</t>
<section title="Encoded CBOR Data Item" anchor="section-2.4.4.1"><t>
Sometimes it is beneficial to carry an embedded CBOR data item that
is not meant to be decoded immediately at the time the enclosing data
item is being parsed. Tag 24 (CBOR data item) can be used to tag the
embedded byte string as a data item encoded in CBOR format.</t>
</section>
<section title="Expected Later Encoding for CBOR-to-JSON Converters" anchor="section-2.4.4.2"><t>
Tags 21 to 23 indicate that a byte string might require a specific
encoding when interoperating with a text-based representation. These
tags are useful when an encoder knows that the byte string data it is
writing is likely to be later converted to a particular JSON-based
usage. That usage specifies that some strings are encoded as base64,
base64url, and so on. The encoder uses byte strings instead of doing
the encoding itself to reduce the message size, to reduce the code
size of the encoder, or both. The encoder does not know whether or
not the converter will be generic, and therefore wants to say what it
believes is the proper way to convert binary strings to JSON.</t>
<t>
The data item tagged can be a byte string or any other data item. In
the latter case, the tag applies to all of the byte string data items
contained in the data item, except for those contained in a nested
data item tagged with an expected conversion.</t>
<t>
These three tag types suggest conversions to three of the base data
encodings defined in <xref target="RFC4648"/>. For base64url encoding, padding is
not used (see Section 3.2 of RFC 4648); that is, all trailing equals
signs ("=") are removed from the base64url-encoded string. Later
tags might be defined for other data encodings of RFC 4648 or for
other ways to encode binary data in strings.</t>
</section>
<section title="Encoded Text" anchor="section-2.4.4.3"><t>
Some text strings hold data that have formats widely used on the
Internet, and sometimes those formats can be validated and presented
to the application in appropriate form by the decoder. There are
tags for some of these formats.</t>
<t><list style="symbols"><t>Tag 32 is for URIs, as defined in <xref target="RFC3986"/>;</t>
<t>Tags 33 and 34 are for base64url- and base64-encoded text strings,
as defined in <xref target="RFC4648"/>;</t>
<t>Tag 35 is for regular expressions in Perl Compatible Regular
Expressions (PCRE) / JavaScript syntax <xref target="ECMA262"/>.</t>
<t>Tag 36 is for MIME messages (including all headers), as defined in
<xref target="RFC2045"/>;</t>
</list>
</t>
<t>
Note that tags 33 and 34 differ from 21 and 22 in that the data is
transported in base-encoded form for the former and in raw byte
string form for the latter.</t>
</section>
</section>
<section title="Self-Describe CBOR" anchor="section-2.4.5"><t>
In many applications, it will be clear from the context that CBOR is
being employed for encoding a data item. For instance, a specific
protocol might specify the use of CBOR, or a media type is indicated
that specifies its use. However, there may be applications where
such context information is not available, such as when CBOR data is
stored in a file and disambiguating metadata is not in use. Here, it
may help to have some distinguishing characteristics for the data
itself.</t>
<t>
Tag 55799 is defined for this purpose. It does not impart any
special semantics on the data item that follows; that is, the
semantics of a data item tagged with tag 55799 is exactly identical
to the semantics of the data item itself.</t>
<t>
The serialization of this tag is 0xd9d9f7, which appears not to be in
use as a distinguishing mark for frequently used file types. In
particular, it is not a valid start of a Unicode text in any Unicode
encoding if followed by a valid CBOR data item.</t>
<t>
For instance, a decoder might be able to parse both CBOR and JSON.
Such a decoder would need to mechanically distinguish the two
formats. An easy way for an encoder to help the decoder would be to
tag the entire CBOR item with tag 55799, the serialization of which
will never be found at the beginning of a JSON text.</t>
</section>
</section>
</section>
<section title="Creating CBOR-Based Protocols" anchor="section-3"><t>
Data formats such as CBOR are often used in environments where there
is no format negotiation. A specific design goal of CBOR is to not
need any included or assumed schema: a decoder can take a CBOR item