core/encoding/cbor

encoding_cbor

Types

37

Add

Add :: enum u8 { False = 20, True = 21, Nil = 22, Undefined = 23, One_Byte = 24, Two_Bytes = 25, Four_Bytes = 26, Eight_Bytes = 27, Length_Unknown = 31, Break = Length_Unknown, }Source

The lower 3 bits of the header which denotes additional information for the type of value.

Decode_Data_Error

Decode_Data_Error :: enum int { None = 0, Bad_Major = 1, // An invalid major type was encountered. Bad_Argument = 2, // A general unexpected value (most likely invalid additional info in header). Bad_Tag_Value = 3, // When the type of value for the given tag is not valid. Nested_Indefinite_Length = 4, // When an streamed/indefinite length container nests another, this is not allowed. Nested_Tag = 5, // When a tag's value is another tag, this is not allowed. Length_Too_Big = 6, // When the length of a container (map, array, bytes, string) is more than `max(int)`. Disallowed_Streaming = 7, // When the `.Disallow_Streaming` flag is set and a streaming header is encountered. Break = 8, // When the `break` header was found without any stream to break off. }Source

Decoder_Flag

Decoder_Flag :: enum int { // Rejects (with an error `.Disallowed_Streaming`) when a streaming CBOR header is encountered. Disallow_Streaming = 0, // Pre-allocates buffers and containers with the size that was set in the CBOR header. // This should only be enabled when you control both ends of the encoding, if you don't, // attackers can craft input that causes massive (`max(u64)`) byte allocations for a few bytes of // CBOR. Trusted_Input = 1, // Makes the decoder shrink of excess capacity from allocated buffers/containers before returning. Shrink_Excess = 2, }Source

Encode_Data_Error

Encode_Data_Error :: enum int { None = 0, Invalid_Simple = 1, // When a simple is being encoded that is out of the range `0..=19` and `32..=max(u8)`. Int_Too_Big = 2, // When an int is being encoded that is larger than `max(u64)` or smaller than `min(u64)`. Bad_Tag_Value = 3, // When the type of value is not supported by the tag implementation. }Source

Encoder_Flag

Encoder_Flag :: enum int { // CBOR defines a tag header that also acts as a file/binary header, // this way decoders can check the first header of the binary and see if it is CBOR. Self_Described_CBOR = 0, // Integers are stored in the smallest integer type it fits. // This involves checking each int against the max of all its smaller types. Deterministic_Int_Size = 1, // Floats are stored in the smallest size float type without losing precision. // This involves casting each float down to its smaller types and checking if it changed. Deterministic_Float_Size = 2, // Sort maps by their keys in bytewise lexicographic order of their deterministic encoding. // NOTE: In order to do this, all keys of a map have to be pre-computed, sorted, and // then written, this involves temporary allocations for the keys and a copy of the map itself. Deterministic_Map_Sorting = 3, }Source

Major

Major :: enum u8 { Unsigned = 0, Negative = 1, Bytes = 2, Text = 3, Array = 4, Map = 5, Tag = 6, Other = 7, }Source

The higher 3 bits of the header which denotes what type of value it is.

Constants

20

TAG_BASE64_NR

TAG_BASE64_NR :: 34Source

The contents of this tag are base64 encoded during marshal and decoded during unmarshal. Use the struct tag cbor_tag:"34" or cbor_tag:"base64" to have your field string or bytes field en/decoded as base64.

TAG_CBOR_NR

TAG_CBOR_NR :: 24Source

TAG_DECIMAL_FRACTION :: 4 // NOTE: We could probably implement this with math/fixed. 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 decoded. Tag number 24 (CBOR data item) can be used to tag the embedded byte string as a single data item encoded in CBOR format. Use the struct tag cbor_tag:"24" or cbor_tag:"cbor" to keep a non-decoded field (string or bytes) of raw CBOR.

TAG_EPOCH_TIME_NR

TAG_EPOCH_TIME_NR :: 1Source

Tags defined in RFC 7049 that we provide implementations for. UTC time in seconds, unmarshalled into a core:time time.Time or integer. Use the struct tag cbor_tag:"1" or cbor_tag:"epoch" to have your time.Time field en/decoded as epoch time.

TAG_OBJECT_TYPE

TAG_OBJECT_TYPE :: 1010Source

A tag that is used to assign a textual type to the object following it. The tag's value must be an array of 2 items, where the first is text (describing the following type) and the second is any valid CBOR value.

See the registration: https://datatracker.ietf.org/doc/draft-rundgren-cotx/05/

We use this in Odin to marshal and unmarshal unions.

TAG_SELF_DESCRIBED_CBOR

TAG_SELF_DESCRIBED_CBOR :: 55799Source

A tag that is used to detect the contents of a binary buffer (like a file) are CBOR. This tag would wrap everything else, decoders can then check for this header and see if the given content is definitely CBOR. Added by the encoder if it has the flag .Self_Described_CBOR, decoded by default.

Variables

3

Procedures

109

destroy

destroy :: proc(val: Value, allocator: mem.Allocator = context.allocator)Source

Recursively frees all memory allocated when decoding the passed value.

from_json

from_json :: proc(val: json.Value, allocator: mem.Allocator = context.allocator) -> (mem.Allocator_Error, Value)Source

Converts from JSON to CBOR.

Everything is copied to the given allocator, the passed in JSON value can be deleted after.

to_json

to_json :: proc(val: Value, allocator: mem.Allocator = context.allocator) -> (json.Value, mem.Allocator_Error)Source

Converts from CBOR to JSON.

NOTE: overflow on integers or floats is not handled.

Everything is copied to the given allocator, the passed in CBOR value can be destroy'ed after.

If a CBOR map with non-string keys is encountered it is turned into an array of tuples.

Procedure Groups

11

decode_from

decode_from :: proc{decode_from_string, decode_from_reader, decode_from_decoder}Source

Decodes both deterministic and non-deterministic CBOR into a Value variant.

Text and Bytes can safely be cast to cstrings because of an added 0 byte.

Allocations are done using the given allocator, no allocations are done on the context.temp_allocator.

A value can be (fully and recursively) deallocated using the destroy proc in this package.

Disable streaming/indeterminate lengths with the .Disallow_Streaming flag.

Shrink excess bytes in buffers and containers with the .Shrink_Excess flag.

Mark the input as trusted input with the .Trusted_Input flag, this turns off the safety feature of not pre-allocating more than max_pre_alloc bytes before reading into the bytes. You should only do this when you own both sides of the encoding and are sure there can't be malicious bytes used as an input.

encode_into

encode_into :: proc{encode_into_bytes, encode_into_builder, encode_into_writer, encode_into_encoder}Source

Encodes the CBOR value into a binary CBOR.

Flags can be used to control the output (mainly determinism, which coincidently affects size).

The default flags ENCODE_SMALL (.Deterministic_Int_Size, .Deterministic_Float_Size) will try to put ints and floats into their smallest possible byte size without losing equality.

Adding the .Self_Described_CBOR flag will wrap the value in a tag that lets generic decoders know the contents are CBOR from just reading the first byte.

Adding the .Deterministic_Map_Sorting flag will sort the encoded maps by the byte content of the encoded key. This flag has a cost on performance and memory efficiency because all keys in a map have to be precomputed, sorted and only then written to the output.

Empty flags will do nothing extra to the value.

The allocations for the .Deterministic_Map_Sorting flag are done using the given temp_allocator. but are followed by the necessary delete and free calls if the allocator supports them. This is helpful when the CBOR size is so big that you don't want to collect all the temporary allocations until the end.

marshal_into

marshal_into :: proc{marshal_into_bytes, marshal_into_builder, marshal_into_writer, marshal_into_encoder}Source

Marshal a value into binary CBOR.

Flags can be used to control the output (mainly determinism, which coincidently affects size).

The default flags ENCODE_SMALL (.Deterministic_Int_Size, .Deterministic_Float_Size) will try to put ints and floats into their smallest possible byte size without losing equality.

Adding the .Self_Described_CBOR flag will wrap the value in a tag that lets generic decoders know the contents are CBOR from just reading the first byte.

Adding the .Deterministic_Map_Sorting flag will sort the encoded maps by the byte content of the encoded key. This flag has a cost on performance and memory efficiency because all keys in a map have to be precomputed, sorted and only then written to the output.

Empty flags will do nothing extra to the value.

The allocations for the .Deterministic_Map_Sorting flag are done using the given temp_allocator. but are followed by the necessary delete and free calls if the allocator supports them. This is helpful when the CBOR size is so big that you don't want to collect all the temporary allocations until the end.

unmarshal

unmarshal :: proc{unmarshal_from_reader, unmarshal_from_string, unmarshal_from_bytes}Source

Unmarshals the given CBOR into the given pointer using reflection. Types that require allocation are allocated using the given allocator.

Some temporary allocations are done on the given temp_allocator, but, if you want to, this can be set to a "normal" allocator, because the necessary delete and free calls are still made. This is helpful when the CBOR size is so big that you don't want to collect all the temporary allocations until the end.

Disable streaming/indeterminate lengths with the .Disallow_Streaming flag.

Shrink excess bytes in buffers and containers with the .Shrink_Excess flag.

Mark the input as trusted input with the .Trusted_Input flag, this turns off the safety feature of not pre-allocating more than max_pre_alloc bytes before reading into the bytes. You should only do this when you own both sides of the encoding and are sure there can't be malicious bytes used as an input.

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.