all repos — circe-vpn @ 556b655293e9ce71509a72ef3c98ea334b5aa85b

Circe VPN

common/cbor.h (view raw)

 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
#ifndef CIRCE_CBOR_H
#define CIRCE_CBOR_H

#include <stdbool.h>
#include <stdint.h>

/// 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