# API

The data API is centered around `cbor_item_t`, a generic handle for any CBOR item. There are functions to

> - create items,

> - set items’ data,

> - parse serialized data into items,

> - manage, move, and links item together.

The single most important thing to keep in mind is: `cbor_item_t` is an opaque type and should only be manipulated using the appropriate functions! Think of it as an object.

The libcbor API closely follows the semantics outlined by CBOR standard. This part of the documentation provides a short overview of the CBOR constructs, as well as a general introduction to the libcbor API. Remaining reference can be found in the following files structured by data types.

The API is designed to allow both very tight control & flexibility and general convenience with sane defaults. [1] For example, client with very specific requirements (constrained environment, custom application protocol built on top of CBOR, etc.) may choose to take full control (and responsibility) of memory and data structures management by interacting directly with the decoder. Other clients might want to take control of specific aspects (streamed collections, hash maps storage), but leave other responsibilities to libcbor. More general clients might prefer to be abstracted away from all aforementioned details and only be presented complete data structures.

libcbor provides

  - stateless encoders and decoders

  - encoding and decoding drivers, routines that coordinate encoding and decoding of complex structures

  - data structures to represent and transform CBOR structures

  - routines for building and manipulating these structures

  - utilities for inspection and debugging

- Types of items
  - `cbor_type`
    - `CBOR_TYPE_UINT`
    - `CBOR_TYPE_NEGINT`
    - `CBOR_TYPE_BYTESTRING`
    - `CBOR_TYPE_STRING`
    - `CBOR_TYPE_ARRAY`
    - `CBOR_TYPE_MAP`
    - `CBOR_TYPE_TAG`
    - `CBOR_TYPE_FLOAT_CTRL`
  - `cbor_typeof()`
  - Binary queries
    - `cbor_isa_uint()`
    - `cbor_isa_negint()`
    - `cbor_isa_bytestring()`
    - `cbor_isa_string()`
    - `cbor_isa_array()`
    - `cbor_isa_map()`
    - `cbor_isa_tag()`
    - `cbor_isa_float_ctrl()`
  - Logical queries
    - `cbor_is_int()`
    - `cbor_is_float()`
    - `cbor_is_bool()`
    - `cbor_is_null()`
    - `cbor_is_undef()`
- Memory management and reference counting
  - Using custom allocator
    - `cbor_set_allocs()`
  - Reference counting
    - `cbor_incref()`
    - `cbor_decref()`
    - `cbor_intermediate_decref()`
    - `cbor_refcount()`
    - `cbor_move()`
    - `cbor_copy()`
- Decoding
  - `cbor_load()`
  - Associated data structures
    - `cbor_error_code`
      - `CBOR_ERR_NONE`
      - `CBOR_ERR_NOTENOUGHDATA`
      - `CBOR_ERR_NODATA`
      - `CBOR_ERR_MALFORMATED`
      - `CBOR_ERR_MEMERROR`
      - `CBOR_ERR_SYNTAXERROR`
    - `cbor_load_result`
      - `error`
      - `read`
    - `cbor_error`
      - `position`
      - `code`
- Encoding
  - `cbor_serialize()`
  - `cbor_serialize_alloc()`
  - `cbor_serialized_size()`
  - Type-specific serializers
    - `cbor_serialize_uint()`
    - `cbor_serialize_negint()`
    - `cbor_serialize_bytestring()`
    - `cbor_serialize_string()`
    - `cbor_serialize_array()`
    - `cbor_serialize_map()`
    - `cbor_serialize_tag()`
    - `cbor_serialize_float_ctrl()`
- Streaming Decoding
  - `cbor_stream_decode()`
  - `cbor_callbacks`
    - `uint8`
    - `uint16`
    - `uint32`
    - `uint64`
    - `negint64`
    - `negint32`
    - `negint16`
    - `negint8`
    - `byte_string_start`
    - `byte_string`
    - `string`
    - `string_start`
    - `indef_array_start`
    - `array_start`
    - `indef_map_start`
    - `map_start`
    - `tag`
    - `float2`
    - `float4`
    - `float8`
    - `undefined`
    - `null`
    - `boolean`
    - `indef_break`
  - `cbor_empty_callbacks`
  - Callback types definition
    - `cbor_int8_callback`
    - `cbor_int16_callback`
    - `cbor_int32_callback`
    - `cbor_int64_callback`
    - `cbor_simple_callback`
    - `cbor_string_callback`
    - `cbor_collection_callback`
    - `cbor_float_callback`
    - `cbor_double_callback`
    - `cbor_bool_callback`
- Streaming Encoding
  - `cbor_encode_uint8()`
  - `cbor_encode_uint16()`
  - `cbor_encode_uint32()`
  - `cbor_encode_uint64()`
  - `cbor_encode_uint()`
  - `cbor_encode_negint8()`
  - `cbor_encode_negint16()`
  - `cbor_encode_negint32()`
  - `cbor_encode_negint64()`
  - `cbor_encode_negint()`
  - `cbor_encode_bytestring_start()`
  - `cbor_encode_indef_bytestring_start()`
  - `cbor_encode_string_start()`
  - `cbor_encode_indef_string_start()`
  - `cbor_encode_array_start()`
  - `cbor_encode_indef_array_start()`
  - `cbor_encode_map_start()`
  - `cbor_encode_indef_map_start()`
  - `cbor_encode_tag()`
  - `cbor_encode_bool()`
  - `cbor_encode_null()`
  - `cbor_encode_undef()`
  - `cbor_encode_half()`
  - `cbor_encode_single()`
  - `cbor_encode_double()`
  - `cbor_encode_break()`
  - `cbor_encode_ctrl()`
- Types 0 & 1 – Positive and negative integers
  - `cbor_int_width`
    - `CBOR_INT_8`
    - `CBOR_INT_16`
    - `CBOR_INT_32`
    - `CBOR_INT_64`
  - Type 0 - positive integers
  - Type 1 - negative integers
  - Type 0 & 1
  - Building new items
    - `cbor_build_uint8()`
    - `cbor_build_uint16()`
    - `cbor_build_uint32()`
    - `cbor_build_uint64()`
  - Retrieving values
    - `cbor_get_uint8()`
    - `cbor_get_uint16()`
    - `cbor_get_uint32()`
    - `cbor_get_uint64()`
  - Setting values
    - `cbor_set_uint8()`
    - `cbor_set_uint16()`
    - `cbor_set_uint32()`
    - `cbor_set_uint64()`
  - Dealing with width
    - `cbor_int_get_width()`
  - Dealing with signedness
    - `cbor_mark_uint()`
    - `cbor_mark_negint()`
  - Creating new items
    - `cbor_new_int8()`
    - `cbor_new_int16()`
    - `cbor_new_int32()`
    - `cbor_new_int64()`
- Type 2 – Byte strings
  - Getting metadata
    - `cbor_bytestring_length()`
    - `cbor_bytestring_is_definite()`
    - `cbor_bytestring_is_indefinite()`
    - `cbor_bytestring_chunk_count()`
  - Reading data
    - `cbor_bytestring_handle()`
    - `cbor_bytestring_chunks_handle()`
  - Creating new items
    - `cbor_new_definite_bytestring()`
    - `cbor_new_indefinite_bytestring()`
  - Building items
    - `cbor_build_bytestring()`
  - Manipulating existing items
    - `cbor_bytestring_set_handle()`
    - `cbor_bytestring_add_chunk()`
- Type 3 – UTF-8 strings
  - UTF-8 encoding validation
  - Getting metadata
    - `cbor_string_length()`
    - `cbor_string_is_definite()`
    - `cbor_string_is_indefinite()`
    - `cbor_string_chunk_count()`
  - Reading data
    - `cbor_string_handle()`
    - `cbor_string_chunks_handle()`
  - Creating new items
    - `cbor_new_definite_string()`
    - `cbor_new_indefinite_string()`
  - Building items
    - `cbor_build_string()`
  - Manipulating existing items
    - `cbor_string_set_handle()`
    - `cbor_string_add_chunk()`
- Type 4 – Arrays
  - Examples
  - Getting metadata
    - `cbor_array_size()`
    - `cbor_array_allocated()`
    - `cbor_array_is_definite()`
    - `cbor_array_is_indefinite()`
  - Reading data
    - `cbor_array_handle()`
    - `cbor_array_get()`
  - Creating new items
    - `cbor_new_definite_array()`
    - `cbor_new_indefinite_array()`
  - Modifying items
    - `cbor_array_push()`
    - `cbor_array_replace()`
    - `cbor_array_set()`
- Type 5 – Maps
  - Examples
  - Getting metadata
    - `cbor_map_size()`
    - `cbor_map_allocated()`
    - `cbor_map_is_definite()`
    - `cbor_map_is_indefinite()`
  - Reading data
    - `cbor_map_handle()`
  - Creating new items
    - `cbor_new_definite_map()`
    - `cbor_new_indefinite_map()`
  - Modifying items
    - `cbor_map_add()`
- Type 6 – Semantic tags
  - `cbor_new_tag()`
  - `cbor_tag_item()`
  - `cbor_tag_value()`
  - `cbor_tag_set_item()`
- Type 7 – Floats & control tokens
  - `cbor_float_width`
    - `CBOR_FLOAT_0`
    - `CBOR_FLOAT_16`
    - `CBOR_FLOAT_32`
    - `CBOR_FLOAT_64`
  - Getting metadata
    - `cbor_float_ctrl_is_ctrl()`
    - `cbor_float_get_width()`
  - Reading data
    - `cbor_float_get_float2()`
    - `cbor_float_get_float4()`
    - `cbor_float_get_float8()`
    - `cbor_float_get_float()`
    - `cbor_ctrl_value()`
    - `cbor_get_bool()`
  - Creating new items
    - `cbor_new_ctrl()`
    - `cbor_new_float2()`
    - `cbor_new_float4()`
    - `cbor_new_float8()`
    - `cbor_new_null()`
    - `cbor_new_undef()`
  - Building items
    - `cbor_build_bool()`
    - `cbor_build_ctrl()`
    - `cbor_build_float2()`
    - `cbor_build_float4()`
    - `cbor_build_float8()`
  - Manipulating existing items
    - `cbor_set_ctrl()`
    - `cbor_set_bool()`
    - `cbor_set_float2()`
    - `cbor_set_float4()`
    - `cbor_set_float8()`
  - Half floats

[1]

http://softwareengineering.vazexqi.com/files/pattern.html