opyeb cborlab

cborlab · Basics

The shape of CBOR bytes

One first byte carries the major type and the additional information. What each of the eight kinds holds.

CBOR carries a set of values like JSON, but every value states its own kind and length in binary. The first byte tells you what kind of value starts and how long it is, so a reader can walk the whole item without a schema.

First byte = major type (top 3 bits) + additional information (low 5 bits)

0x82 has 100 (4) in the top three bits and 00010 (2) in the low five: major type 4, an array of two items. 0x64 0x61 0x62 0x63 0x64 is major type 3 with length 4 and the body "abcd".

Major typeHoldsWhat the length means
0unsigned integerthe argument is the value (0-23) or 1, 2, 4 or 8 following bytes
1negative integervalue = -1 - n
2byte stringnumber of body bytes
3text stringnumber of UTF-8 body bytes
4arraynumber of items
5mapnumber of key-value pairs
6tagthe tag number, then one value
7simple value or floatfalse, true, null, undefined, or Float16/32/64

Additional information 24-31

A value of 23 or less sits in the low five bits. 24 reads one more byte, 25 two, 26 four and 27 eight. 28 to 30 are reserved, and 31 marks an indefinite-length item that carries no length.

Definite and indefinite

Arrays, maps and strings come either with a length up front (definite) or starting with 0x9f and ending with 0xff (indefinite). Indefinite form suits streaming, but because the length is not known in advance deterministic encoding never uses it.

Integers and floats

Tags add meaning

A tag does not change how the inner value is read; it adds meaning. Tags 0 and 1 are dates and times, 2 and 3 are big integers, 18 is COSE_Sign1, and 24 says the inner byte string is itself a CBOR data item. This tool shows the name of the well-known tags and the interpreted value (date, bignum) beside them.

CBOR separates UTF-8 text from bytes. When a text string is not valid UTF-8 this tool reports an error and shows the remaining bytes with replacement characters.

Questions

Why use CBOR at all?

It is smaller than JSON, states its own length without a schema, and carries byte strings, large integers and tags directly. Security standards such as COSE and CWT rely on those properties.

Why does major type 7 hold so much?

It carries the four simple values (false, true, null, undefined), reserved simple values, and floats in three widths. The width comes from additional information 25, 26 or 27.

Why separate text from bytes?

Text can be validated as UTF-8 while bytes hold arbitrary binary data. Blending them would leave the receiver nothing to validate.

The basic-map preset is a 109-byte example holding integers, text, a Float16, an array, a nested map and a byte string at once.

Open the tool

Written against RFC 8949 (CBOR), RFC 9052 (COSE), RFC 8392 (CWT) and the IANA COSE algorithm registry. Every byte string and key here is synthetic.