#ifndef CIRCE_CBOR_H #define CIRCE_CBOR_H #include #include /// Represents all major types in the standard enum circe_cbor_major_type { CIRCE_CBOR_MAJOR_UNSIGNED = 0, CIRCE_CBOR_MAJOR_NEGATIVE, CIRCE_CBOR_MAJOR_BYTESTR, CIRCE_CBOR_MAJOR_TEXTSTR, CIRCE_CBOR_MAJOR_ARRAY, CIRCE_CBOR_MAJOR_MAPPING, CIRCE_CBOR_MAJOR_TAG, /// Also includes floating-point numbers CIRCE_CBOR_MAJOR_SIMPLE, }; /// Represents most simple value types, missing IEEE 754 half-precision floats /// because those are not standard in C without stdlib access enum circe_cbor_simple_type { CIRCE_CBOR_SIMPLE_FALSE = 20, CIRCE_CBOR_SIMPLE_TRUE = 21, CIRCE_CBOR_SIMPLE_NULL = 22, CIRCE_CBOR_SIMPLE_UNDEFINED = 23, CIRCE_CBOR_SIMPLE_SINGLE = 26, CIRCE_CBOR_SIMPLE_DOUBLE = 27, CIRCE_CBOR_SIMPLE_BREAK = 31, }; /// Decoder context object struct circe_cbor_decoder { const uint8_t *buf_start; const uint64_t buf_len; const uint8_t *cursor; }; /// Used to tie together buffer attributes in a single struct struct circe_cbor_buffer { uint8_t *buf_start; uint64_t buf_len; }; bool circe_cbor_get_major_type (struct circe_cbor_decoder *dc, enum circe_cbor_major_type *out); bool circe_cbor_get_simple_type (struct circe_cbor_decoder *dc, enum circe_cbor_simple_type *out); bool circe_cbor_extract_length (struct circe_cbor_decoder *dc, uint64_t *out, bool *is_definite); bool circe_cbor_skip (struct circe_cbor_decoder *dc, uint64_t max_depth); bool circe_cbor_extract_unsigned (struct circe_cbor_decoder *dc, uint64_t *out); bool circe_cbor_extract_negative (struct circe_cbor_decoder *dc, uint64_t *magnitude_out); bool circe_cbor_extract_bytestr (struct circe_cbor_decoder *dc, struct circe_cbor_buffer *out); bool circe_cbor_extract_textstr (struct circe_cbor_decoder *dc, struct circe_cbor_buffer *out); bool circe_cbor_extract_float (struct circe_cbor_decoder *dc, float *out); bool circe_cbor_extract_double (struct circe_cbor_decoder *dc, double *out); bool circe_cbor_extract_tag (struct circe_cbor_decoder *dc, uint64_t *out); struct circe_cbor_encoder { uint8_t *buf_start; const uint64_t buf_len; uint8_t *cursor; }; bool circe_cbor_emit_unsigned (struct circe_cbor_encoder *ec, uint64_t n); bool circe_cbor_emit_negative (struct circe_cbor_encoder *ec, uint64_t magnitude); bool circe_cbor_emit_bytestr (struct circe_cbor_encoder *ec, struct circe_cbor_buffer contents); bool circe_cbor_emit_textstr (struct circe_cbor_encoder *ec, struct circe_cbor_buffer contents); bool circe_cbor_emit_simple (struct circe_cbor_encoder *ec, enum circe_cbor_simple_type t); bool circe_cbor_emit_float (struct circe_cbor_encoder *ec, float x); bool circe_cbor_emit_double (struct circe_cbor_encoder *ec, double x); bool circe_cbor_emit_tag (struct circe_cbor_encoder *ec, uint64_t tagnum); bool circe_cbor_emit_mapping_header (struct circe_cbor_encoder *ec, uint64_t len); bool circe_cbor_emit_array_header (struct circe_cbor_encoder *ec, uint64_t len); #endif