summaryrefslogtreecommitdiffstats
path: root/lib/libcbor/src/cbor/data.h
blob: 7f703bc063559b020160fd247ead005cc58384be (plain) (blame)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#ifndef LIBCBOR_DATA_H
#define LIBCBOR_DATA_H

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

#ifdef __cplusplus
extern "C" {
#endif

typedef const unsigned char* cbor_data;
typedef unsigned char* cbor_mutable_data;

/** Specifies the Major type of ::cbor_item_t */
typedef enum cbor_type {
  CBOR_TYPE_UINT /** 0 - positive integers */
  ,
  CBOR_TYPE_NEGINT /** 1 - negative integers*/
  ,
  CBOR_TYPE_BYTESTRING /** 2 - byte strings */
  ,
  CBOR_TYPE_STRING /** 3 - strings */
  ,
  CBOR_TYPE_ARRAY /** 4 - arrays */
  ,
  CBOR_TYPE_MAP /** 5 - maps */
  ,
  CBOR_TYPE_TAG /** 6 - tags  */
  ,
  CBOR_TYPE_FLOAT_CTRL /** 7 - decimals and special values (true, false, nil,
                          ...) */
} cbor_type;

/** Possible decoding errors */
typedef enum {
  CBOR_ERR_NONE,
  CBOR_ERR_NOTENOUGHDATA,
  CBOR_ERR_NODATA,
  CBOR_ERR_MALFORMATED,
  CBOR_ERR_MEMERROR /** Memory error - item allocation failed. Is it too big for
                       your allocator? */
  ,
  CBOR_ERR_SYNTAXERROR /** Stack parsing algorithm failed */
} cbor_error_code;

/** Possible widths of #CBOR_TYPE_UINT items */
typedef enum {
  CBOR_INT_8,
  CBOR_INT_16,
  CBOR_INT_32,
  CBOR_INT_64
} cbor_int_width;

/** Possible widths of #CBOR_TYPE_FLOAT_CTRL items */
typedef enum {
  CBOR_FLOAT_0 /** Internal use - ctrl and special values */
  ,
  CBOR_FLOAT_16 /** Half float */
  ,
  CBOR_FLOAT_32 /** Single float */
  ,
  CBOR_FLOAT_64 /** Double */
} cbor_float_width;

/** Metadata for dynamically sized types */
typedef enum {
  _CBOR_METADATA_DEFINITE,
  _CBOR_METADATA_INDEFINITE
} _cbor_dst_metadata;

/** Semantic mapping for CTRL simple values */
typedef enum {
  CBOR_CTRL_NONE = 0,
  CBOR_CTRL_FALSE = 20,
  CBOR_CTRL_TRUE = 21,
  CBOR_CTRL_NULL = 22,
  CBOR_CTRL_UNDEF = 23
} _cbor_ctrl;

/** Integers specific metadata */
struct _cbor_int_metadata {
  cbor_int_width width;
};

/** Bytestrings specific metadata */
struct _cbor_bytestring_metadata {
  size_t length;
  _cbor_dst_metadata type;
};

/** Strings specific metadata */
struct _cbor_string_metadata {
  size_t length;
  size_t codepoint_count; /* Sum of chunks' codepoint_counts for indefinite
                             strings */
  _cbor_dst_metadata type;
};

/** Arrays specific metadata */
struct _cbor_array_metadata {
  size_t allocated;
  size_t end_ptr;
  _cbor_dst_metadata type;
};

/** Maps specific metadata */
struct _cbor_map_metadata {
  size_t allocated;
  size_t end_ptr;
  _cbor_dst_metadata type;
};

/** Arrays specific metadata
 *
 * The pointer is included - cbor_item_metadata is
 * 2 * sizeof(size_t) + sizeof(_cbor_string_type_metadata),
 * lets use the space
 */
struct _cbor_tag_metadata {
  struct cbor_item_t* tagged_item;
  uint64_t value;
};

/** Floats specific metadata - includes CTRL values */
struct _cbor_float_ctrl_metadata {
  cbor_float_width width;
  uint8_t ctrl;
};

/** Raw memory casts helper */
union _cbor_float_helper {
  float as_float;
  uint32_t as_uint;
};

/** Raw memory casts helper */
union _cbor_double_helper {
  double as_double;
  uint64_t as_uint;
};

/** Union of metadata across all possible types - discriminated in #cbor_item_t
 */
union cbor_item_metadata {
  struct _cbor_int_metadata int_metadata;
  struct _cbor_bytestring_metadata bytestring_metadata;
  struct _cbor_string_metadata string_metadata;
  struct _cbor_array_metadata array_metadata;
  struct _cbor_map_metadata map_metadata;
  struct _cbor_tag_metadata tag_metadata;
  struct _cbor_float_ctrl_metadata float_ctrl_metadata;
};

/** The item handle */
typedef struct cbor_item_t {
  /** Discriminated by type */
  union cbor_item_metadata metadata;
  /** Reference count - initialize to 0 */
  size_t refcount;
  /** Major type discriminator */
  cbor_type type;
  /** Raw data block - interpretation depends on metadata */
  unsigned char* data;
} cbor_item_t;

/** Defines cbor_item_t#data structure for indefinite strings and bytestrings
 *
 * Used to cast the raw representation for a sane manipulation
 */
struct cbor_indefinite_string_data {
  size_t chunk_count;
  size_t chunk_capacity;
  cbor_item_t** chunks;
};

/** High-level decoding error */
struct cbor_error {
  /** Aproximate position */
  size_t position;
  /** Description */
  cbor_error_code code;
};

/** Simple pair of items for use in maps */
struct cbor_pair {
  cbor_item_t *key, *value;
};

/** High-level decoding result */
struct cbor_load_result {
  /** Error indicator */
  struct cbor_error error;
  /** Number of bytes read*/
  size_t read;
};

/** Streaming decoder result - status */
enum cbor_decoder_status {
  CBOR_DECODER_FINISHED /** OK, finished */
  ,
  CBOR_DECODER_NEDATA /** Not enough data - mismatch with MTB */
  ,
  CBOR_DECODER_EBUFFER /** Buffer manipulation problem */
  ,
  CBOR_DECODER_ERROR /** Malformed or reserved MTB/value */
};

/** Streaming decoder result */
struct cbor_decoder_result {
  /** Bytes read */
  size_t read;
  /** The result */
  enum cbor_decoder_status status;
  /** When status == CBOR_DECODER_NEDATA,
   *  the minimum number of bytes required to continue parsing */
  size_t required;
};

#ifdef __cplusplus
}
#endif

#endif  // LIBCBOR_DATA_H