Debian Codegraph @

crt/aws-c-common/include/aws/common/cbor.h

raw symbols SCIP
1 #ifndef AWS_COMMON_CBOR_H
2 #define AWS_COMMON_CBOR_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/byte_buf.h>
10 #include <aws/common/common.h>
11
12 AWS_PUSH_SANE_WARNING_LEVEL
13 AWS_EXTERN_C_BEGIN
14
15 /**
16 * The types use by APIs, not 1:1 with major type.
17 * It's an extension for cbor major type in RFC8949 section 3.1
18 * Major type 0 - AWS_CBOR_TYPE_UINT
19 * Major type 1 - AWS_CBOR_TYPE_NEGINT
20 * Major type 2 - AWS_CBOR_TYPE_BYTES/AWS_CBOR_TYPE_INDEF_BYTES_START
21 * Major type 3 - AWS_CBOR_TYPE_TEXT/AWS_CBOR_TYPE_INDEF_TEXT_START
22 * Major type 4 - AWS_CBOR_TYPE_ARRAY_START/AWS_CBOR_TYPE_INDEF_ARRAY_START
23 * Major type 5 - AWS_CBOR_TYPE_MAP_START/AWS_CBOR_TYPE_INDEF_MAP_START
24 * Major type 6 - AWS_CBOR_TYPE_TAG
25 * Major type 7
26 * - 20/21 - AWS_CBOR_TYPE_BOOL
27 * - 22 - AWS_CBOR_TYPE_NULL
28 * - 23 - AWS_CBOR_TYPE_UNDEFINED
29 * - 25/26/27 - AWS_CBOR_TYPE_FLOAT
30 * - 31 - AWS_CBOR_TYPE_BREAK
31 * - rest of value are not supported.
32 */
33 enum aws_cbor_type {
34 AWS_CBOR_TYPE_UNKNOWN = 0,
35
36 AWS_CBOR_TYPE_UINT,
37 AWS_CBOR_TYPE_NEGINT,
38 AWS_CBOR_TYPE_FLOAT,
39 AWS_CBOR_TYPE_BYTES,
40 AWS_CBOR_TYPE_TEXT,
41
42 AWS_CBOR_TYPE_ARRAY_START,
43 AWS_CBOR_TYPE_MAP_START,
44
45 AWS_CBOR_TYPE_TAG,
46
47 AWS_CBOR_TYPE_BOOL,
48 AWS_CBOR_TYPE_NULL,
49 AWS_CBOR_TYPE_UNDEFINED,
50 AWS_CBOR_TYPE_BREAK,
51
52 AWS_CBOR_TYPE_INDEF_BYTES_START,
53 AWS_CBOR_TYPE_INDEF_TEXT_START,
54 AWS_CBOR_TYPE_INDEF_ARRAY_START,
55 AWS_CBOR_TYPE_INDEF_MAP_START,
56 };
57
58 /**
59 * The common tags, refer to RFC8949 section 3.4
60 * Expected value type followed by the tag:
61 * AWS_CBOR_TAG_STANDARD_TIME - AWS_CBOR_TYPE_TEXT
62 * AWS_CBOR_TAG_EPOCH_TIME - AWS_CBOR_TYPE_UINT/AWS_CBOR_TYPE_NEGINT/AWS_CBOR_TYPE_FLOAT
63 * AWS_CBOR_TAG_UNSIGNED_BIGNUM - AWS_CBOR_TYPE_BYTES
64 * AWS_CBOR_TAG_NEGATIVE_BIGNUM - AWS_CBOR_TYPE_BYTES
65 * AWS_CBOR_TAG_DECIMAL_FRACTION - AWS_CBOR_TYPE_ARRAY_START/AWS_CBOR_TYPE_INDEF_ARRAY_START
66 **/
67 #define AWS_CBOR_TAG_STANDARD_TIME 0
68 #define AWS_CBOR_TAG_EPOCH_TIME 1
69 #define AWS_CBOR_TAG_UNSIGNED_BIGNUM 2
70 #define AWS_CBOR_TAG_NEGATIVE_BIGNUM 3
71 #define AWS_CBOR_TAG_DECIMAL_FRACTION 4
72
73 struct aws_cbor_encoder;
74 struct aws_cbor_decoder;
75
76 /*******************************************************************************
77 * ENCODE
78 ******************************************************************************/
79
80 /* Return c-string for aws_cbor_type */
81 AWS_COMMON_API
82 const char *aws_cbor_type_cstr(enum aws_cbor_type type);
83
84 /**
85 * @brief Create a new cbor encoder. Creating a encoder with a temporay buffer.
86 * Every aws_cbor_encoder_write_* will encode directly into the buffer to follow the encoded data.
87 *
88 * @param allocator
89 * @return aws_cbor_encoder
90 */
91 AWS_COMMON_API
92 struct aws_cbor_encoder *aws_cbor_encoder_new(struct aws_allocator *allocator);
93
94 AWS_COMMON_API
95 struct aws_cbor_encoder *aws_cbor_encoder_destroy(struct aws_cbor_encoder *encoder);
96
97 /**
98 * @brief Get the current encoded data from encoder. The encoded data has the same lifetime as the encoder, and once
99 * any other function call invoked for the encoder, the encoded data is no longer valid.
100 *
101 * @param encoder
102 * @return struct aws_byte_cursor from the encoder buffer.
103 */
104 AWS_COMMON_API
105 struct aws_byte_cursor aws_cbor_encoder_get_encoded_data(const struct aws_cbor_encoder *encoder);
106
107 /**
108 * @brief Clear the current encoded buffer from encoder.
109 *
110 * @param encoder
111 */
112 AWS_COMMON_API
113 void aws_cbor_encoder_reset(struct aws_cbor_encoder *encoder);
114
115 /**
116 * @brief Encode a AWS_CBOR_TYPE_UINT value to "smallest possible" in encoder's buffer.
117 * Referring to RFC8949 section 4.2.1
118 *
119 * TODO: maybe add a width of the encoded value.
120 *
121 * @param encoder
122 * @param value value to encode.
123 */
124 AWS_COMMON_API
125 void aws_cbor_encoder_write_uint(struct aws_cbor_encoder *encoder, uint64_t value);
126
127 /**
128 * @brief Encode a AWS_CBOR_TYPE_NEGINT value to "smallest possible" in encoder's buffer.
129 * It represents (-1 - value).
130 * Referring to RFC8949 section 4.2.1
131 *
132 *
133 * @param encoder
134 * @param value The argument to encode to negative integer, which is (-1 - expected_val)
135 */
136 AWS_COMMON_API
137 void aws_cbor_encoder_write_negint(struct aws_cbor_encoder *encoder, uint64_t value);
138
139 /**
140 * @brief Encode a AWS_CBOR_TYPE_FLOAT value to "smallest possible", but will not be encoded into half-precision float,
141 * as it's not well supported cross languages.
142 *
143 * To be more specific, it will be encoded into integer/negative/float
144 * (Order with priority) when the conversation will not cause precision loss.
145 *
146 * @param encoder
147 * @param value value to encode.
148 */
149 AWS_COMMON_API
150 void aws_cbor_encoder_write_float(struct aws_cbor_encoder *encoder, double value);
151
152 /**
153 * @brief Encode a AWS_CBOR_TYPE_BYTES value to "smallest possible" in encoder's buffer.
154 * Referring to RFC8949 section 4.2.1, the length of "from" will be encoded first and then the value of "from" will
155 * be followed.
156 *
157 * @param encoder
158 * @param from value to encode.
159 */
160 AWS_COMMON_API
161 void aws_cbor_encoder_write_bytes(struct aws_cbor_encoder *encoder, struct aws_byte_cursor from);
162
163 /**
164 * @brief Encode a AWS_CBOR_TYPE_TEXT value to "smallest possible" in encoder's buffer.
165 * Referring to RFC8949 section 4.2.1, the length of "from" will be encoded first and then the value of "from" will
166 * be followed.
167 *
168 * @param encoder
169 * @param from value to encode.
170 */
171 AWS_COMMON_API
172 void aws_cbor_encoder_write_text(struct aws_cbor_encoder *encoder, struct aws_byte_cursor from);
173
174 /**
175 * @brief Encode a AWS_CBOR_TYPE_ARRAY_START value to "smallest possible" in encoder's buffer.
176 * Referring to RFC8949 section 4.2.1
177 * The "number_entries" is the cbor data items should be followed as the content of the array.
178 * Notes: it's user's responsibility to keep the integrity of the array to be encoded.
179 *
180 * @param encoder
181 * @param number_entries The number of data item in array.
182 */
183 AWS_COMMON_API
184 void aws_cbor_encoder_write_array_start(struct aws_cbor_encoder *encoder, size_t number_entries);
185
186 /**
187 * @brief Encode a AWS_CBOR_TYPE_MAP_START value to "smallest possible" in encoder's buffer.
188 * Referring to RFC8949 section 4.2.1
189 * The "number_entries" is the number of pair of cbor data items as key and value should be followed as the content of
190 * the map.
191 *
192 * Notes: it's user's responsibility to keep the integrity of the map to be encoded.
193 *
194 * @param encoder
195 * @param number_entries The number of data item in map.
196 */
197 AWS_COMMON_API
198 void aws_cbor_encoder_write_map_start(struct aws_cbor_encoder *encoder, size_t number_entries);
199
200 /**
201 * @brief Encode a AWS_CBOR_TYPE_TAG value to "smallest possible" in encoder's buffer.
202 * Referring to RFC8949 section 4.2.1
203 * The following cbor data item will be the content of the tagged value.
204 * Notes: it's user's responsibility to keep the integrity of the tagged value to follow the RFC8949 section 3.4
205 *
206 * @param encoder
207 * @param tag_number The tag value to encode.
208 */
209 AWS_COMMON_API
210 void aws_cbor_encoder_write_tag(struct aws_cbor_encoder *encoder, uint64_t tag_number);
211
212 /**
213 * @brief Encode a simple value AWS_CBOR_TYPE_NULL
214 *
215 * @param encoder
216 */
217 AWS_COMMON_API
218 void aws_cbor_encoder_write_null(struct aws_cbor_encoder *encoder);
219
220 /**
221 * @brief Encode a simple value AWS_CBOR_TYPE_UNDEFINED
222 *
223 * @param encoder
224 */
225 AWS_COMMON_API
226 void aws_cbor_encoder_write_undefined(struct aws_cbor_encoder *encoder);
227
228 /**
229 * @brief Encode a simple value AWS_CBOR_TYPE_BOOL
230 *
231 * @param encoder
232 */
233 AWS_COMMON_API
234 void aws_cbor_encoder_write_bool(struct aws_cbor_encoder *encoder, bool value);
235
236 /**
237 * @brief Encode a simple value AWS_CBOR_TYPE_BREAK
238 *
239 * Notes: no error checking, it's user's responsibility to track the break
240 * to close the corresponding indef_start
241 */
242 AWS_COMMON_API
243 void aws_cbor_encoder_write_break(struct aws_cbor_encoder *encoder);
244
245 /**
246 * @brief Encode a AWS_CBOR_TYPE_INDEF_BYTES_START
247 *
248 * Notes: no error checking, it's user's responsibility to add corresponding data and the break
249 * to close the indef_start
250 */
251 AWS_COMMON_API
252 void aws_cbor_encoder_write_indef_bytes_start(struct aws_cbor_encoder *encoder);
253 /**
254 * @brief Encode a AWS_CBOR_TYPE_INDEF_TEXT_START
255 *
256 * Notes: no error checking, it's user's responsibility to add corresponding data
257 * and the break to close the indef_start
258 */
259 AWS_COMMON_API
260 void aws_cbor_encoder_write_indef_text_start(struct aws_cbor_encoder *encoder);
261 /**
262 * @brief Encode a AWS_CBOR_TYPE_INDEF_ARRAY_START
263 *
264 * Notes: no error checking, it's user's responsibility to add corresponding data
265 * and the break to close the indef_start
266 */
267 AWS_COMMON_API
268 void aws_cbor_encoder_write_indef_array_start(struct aws_cbor_encoder *encoder);
269 /**
270 * @brief Encode a AWS_CBOR_TYPE_INDEF_MAP_START
271 *
272 * Notes: no error checking, it's user's responsibility to add corresponding data
273 * and the break to close the indef_start
274 */
275 AWS_COMMON_API
276 void aws_cbor_encoder_write_indef_map_start(struct aws_cbor_encoder *encoder);
277
278 /*******************************************************************************
279 * DECODE
280 ******************************************************************************/
281
282 /**
283 * @brief Create a cbor decoder to take src to decode.
284 * The typical usage of decoder will be:
285 * - If the next element type only accept what expected, `aws_cbor_decoder_pop_next_*`
286 * - If the next element type accept different type, invoke `aws_cbor_decoder_peek_type` first, then based on the type
287 * to invoke corresponding `aws_cbor_decoder_pop_next_*`
288 * - If the next element type doesn't have corrsponding value, specifically: AWS_CBOR_TYPE_NULL,
289 * AWS_CBOR_TYPE_UNDEFINED, AWS_CBOR_TYPE_INF_*_START, AWS_CBOR_TYPE_BREAK, call
290 * `aws_cbor_decoder_consume_next_single_element` to consume it and continues for further decoding.
291 * - To ignore the next data item (the element and the content of it), `aws_cbor_decoder_consume_next_whole_data_item`
292 *
293 * Note: it's caller's responsibilty to keep the src outlive the decoder.
294 *
295 * @param allocator
296 * @param src The src data to decode from.
297 * @return decoder
298 */
299 AWS_COMMON_API
300 struct aws_cbor_decoder *aws_cbor_decoder_new(struct aws_allocator *allocator, struct aws_byte_cursor src);
301
302 AWS_COMMON_API
303 struct aws_cbor_decoder *aws_cbor_decoder_destroy(struct aws_cbor_decoder *decoder);
304
305 /**
306 * @brief Get the length of the remaining bytes of the source. Once the source was decoded, it will be consumed,
307 * and result in decrease of the remaining length of bytes.
308 * Note: aws_cbor_decoder_peek_type will also decrease the remaining length, as it decodes the next element
309 * internally.
310 *
311 * @param decoder
312 * @return The length of bytes remaining of the decoder source.
313 */
314 AWS_COMMON_API
315 size_t aws_cbor_decoder_get_remaining_length(const struct aws_cbor_decoder *decoder);
316
317 /**
318 * @brief Get the number of bytes that have not yet been consumed (popped) by the caller.
319 * Unlike aws_cbor_decoder_get_remaining_length, aws_cbor_decoder_peek_type does NOT affect this value.
320 * Only pop/consume operations reduce the unconsumed length.
321 *
322 * @param decoder
323 * @return The number of bytes not yet consumed from the decoder source.
324 */
325 AWS_COMMON_API
326 size_t aws_cbor_decoder_get_unconsumed_length(const struct aws_cbor_decoder *decoder);
327
328 /**
329 * @brief Reset the decoder source to a new src.
330 * The previous src will be discarded regardless of the unconsumed bytes.
331 * The decoder will clear its cache if any.
332 *
333 * @param decoder
334 * @param src The src data to decode from..
335 */
336 AWS_COMMON_API
337 void aws_cbor_decoder_reset_src(struct aws_cbor_decoder *decoder, struct aws_byte_cursor src);
338
339 /**
340 * @brief Decode the next element and store it in the decoder cache if there was no element cached.
341 * If there was element cached, just return the type of the cached element.
342 *
343 * @param decoder
344 * @param out_type
345 * @return AWS_OP_SUCCESS if succeed, AWS_OP_ERR for any decoding error and corresponding error code will be raised.
346 */
347 AWS_COMMON_API
348 int aws_cbor_decoder_peek_type(struct aws_cbor_decoder *decoder, enum aws_cbor_type *out_type);
349
350 /**
351 * @brief Consume the next data item, includes all the content within the data item.
352 *
353 * As an example for the following cbor, this function will consume all the data
354 * as it's only one cbor data item, an indefinite map with 2 <key, value> pair:
355 * 0xbf6346756ef563416d7421ff
356 * BF -- Start indefinite-length map
357 * 63 -- First key, UTF-8 string length 3
358 * 46756e -- "Fun"
359 * F5 -- First value, true
360 * 63 -- Second key, UTF-8 string length 3
361 * 416d74 -- "Amt"
362 * 21 -- Second value, -2
363 * FF -- "break"
364 *
365 * Notes: this function will not ensure the data item is well-formed.
366 *
367 * @param src The src to parse data from
368 * @return AWS_OP_SUCCESS successfully consumed the next data item, otherwise AWS_OP_ERR.
369 */
370 AWS_COMMON_API
371 int aws_cbor_decoder_consume_next_whole_data_item(struct aws_cbor_decoder *decoder);
372
373 /**
374 * @brief Consume the next single element, without the content followed by the element.
375 *
376 * As an example for the following cbor, this function will only consume the
377 * 0xBF, "Start indefinite-length map", not any content of the map represented.
378 * The next element to decode will start from 0x63
379 * 0xbf6346756ef563416d7421ff
380 * BF -- Start indefinite-length map
381 * 63 -- First key, UTF-8 string length 3
382 * 46756e -- "Fun"
383 * F5 -- First value, true
384 * 63 -- Second key, UTF-8 string length 3
385 * 416d74 -- "Amt"
386 * 21 -- Second value, -2
387 * FF -- "break"
388 *
389 * @param decoder The decoder to parse data from
390 * @return AWS_OP_SUCCESS successfully consumed the next element, otherwise AWS_OP_ERR.
391 */
392 AWS_COMMON_API
393 int aws_cbor_decoder_consume_next_single_element(struct aws_cbor_decoder *decoder);
394
395 /**
396 * @brief Get the next element based on the type. If the next element doesn't match the expected type. Error will be
397 * raised. If the next element already been cached, it will consume the cached item when no error was returned.
398 * Specifically:
399 * AWS_CBOR_TYPE_UINT - aws_cbor_decoder_pop_next_unsigned_int_val
400 * AWS_CBOR_TYPE_NEGINT - aws_cbor_decoder_pop_next_negative_int_val, it represents (-1 - *out)
401 * AWS_CBOR_TYPE_FLOAT - aws_cbor_decoder_pop_next_float_val
402 * AWS_CBOR_TYPE_BYTES - aws_cbor_decoder_pop_next_bytes_val
403 * AWS_CBOR_TYPE_TEXT - aws_cbor_decoder_pop_next_text_val
404 *
405 * @param decoder
406 * @param out
407 * @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR.
408 */
409 AWS_COMMON_API
410 int aws_cbor_decoder_pop_next_unsigned_int_val(struct aws_cbor_decoder *decoder, uint64_t *out);
411 AWS_COMMON_API
412 int aws_cbor_decoder_pop_next_negative_int_val(struct aws_cbor_decoder *decoder, uint64_t *out);
413 AWS_COMMON_API
414 int aws_cbor_decoder_pop_next_float_val(struct aws_cbor_decoder *decoder, double *out);
415 AWS_COMMON_API
416 int aws_cbor_decoder_pop_next_boolean_val(struct aws_cbor_decoder *decoder, bool *out);
417 AWS_COMMON_API
418 int aws_cbor_decoder_pop_next_bytes_val(struct aws_cbor_decoder *decoder, struct aws_byte_cursor *out);
419 AWS_COMMON_API
420 int aws_cbor_decoder_pop_next_text_val(struct aws_cbor_decoder *decoder, struct aws_byte_cursor *out);
421
422 /**
423 * @brief Get the next AWS_CBOR_TYPE_ARRAY_START element. Only consume the AWS_CBOR_TYPE_ARRAY_START element and set the
424 * size of array to *out_size, not the content of the array. The next *out_size cbor data items will be the content of
425 * the array for a valid cbor data,
426 *
427 * Notes: For indefinite-length, this function will fail with "AWS_ERROR_CBOR_UNEXPECTED_TYPE". The designed way to
428 * handle indefinite-length is:
429 * - Get AWS_CBOR_TYPE_INDEF_ARRAY_START from _peek_type
430 * - call `aws_cbor_decoder_consume_next_single_element` to pop the indefinite-length start.
431 * - Decode the next data item until AWS_CBOR_TYPE_BREAK read.
432 *
433 * @param decoder
434 * @param out_size store the size of array if succeed.
435 * @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR.
436 */
437 AWS_COMMON_API
438 int aws_cbor_decoder_pop_next_array_start(struct aws_cbor_decoder *decoder, uint64_t *out_size);
439
440 /**
441 * @brief Get the next AWS_CBOR_TYPE_MAP_START element.
442 * Only consume the AWS_CBOR_TYPE_MAP_START element and set the size of array to *out_size, not the content of the map.
443 * The next *out_size pair of cbor data items as key and value will be the content of the array for a valid cbor data,
444 *
445 * Notes: For indefinite-length, this function will fail with "AWS_ERROR_CBOR_UNEXPECTED_TYPE". The designed way to
446 * handle indefinite-length is:
447 * - Get AWS_CBOR_TYPE_INDEF_MAP_START from _peek_type
448 * - call `aws_cbor_decoder_consume_next_single_element` to pop the indefinite-length start.
449 * - Decode the next data item until AWS_CBOR_TYPE_BREAK read.
450 *
451 * @param decoder
452 * @param out_size store the size of map if succeed.
453 * @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR.
454 */
455 AWS_COMMON_API
456 int aws_cbor_decoder_pop_next_map_start(struct aws_cbor_decoder *decoder, uint64_t *out_size);
457
458 /**
459 * @brief Get the next AWS_CBOR_TYPE_TAG element.
460 * Only consume the AWS_CBOR_TYPE_TAG element and set the tag ID value to *out_tag_val, not the content of the tagged.
461 * The next cbor data item will be the content of the tagged value for a valid cbor data.
462 *
463 * @param decoder
464 * @param out_tag_val store the value of tag ID.
465 * @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR.
466 */
467 AWS_COMMON_API
468 int aws_cbor_decoder_pop_next_tag_val(struct aws_cbor_decoder *decoder, uint64_t *out_tag_val);
469
470 AWS_EXTERN_C_END
471 AWS_POP_SANE_WARNING_LEVEL
472
473 #endif // AWS_COMMON_CBOR_H