Skip to content

Commit 8c9200c

Browse files
authored
Add aws_cbor_decoder_reset_src api for aws_cbor_decoder (#1202)
1 parent 32f5927 commit 8c9200c

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

include/aws/common/cbor.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ struct aws_cbor_decoder *aws_cbor_decoder_destroy(struct aws_cbor_decoder *decod
312312
AWS_COMMON_API
313313
size_t aws_cbor_decoder_get_remaining_length(const struct aws_cbor_decoder *decoder);
314314

315+
/**
316+
* @brief Reset the decoder source to a new src.
317+
* The previous src will be discarded regardless of the unconsumed bytes.
318+
* The decoder will clear its cache if any.
319+
*
320+
* @param decoder
321+
* @param src The src data to decode from..
322+
*/
323+
AWS_COMMON_API
324+
void aws_cbor_decoder_reset_src(struct aws_cbor_decoder *decoder, struct aws_byte_cursor src);
325+
315326
/**
316327
* @brief Decode the next element and store it in the decoder cache if there was no element cached.
317328
* If there was element cached, just return the type of the cached element.
@@ -414,9 +425,9 @@ AWS_COMMON_API
414425
int aws_cbor_decoder_pop_next_array_start(struct aws_cbor_decoder *decoder, uint64_t *out_size);
415426

416427
/**
417-
* @brief Get the next AWS_CBOR_TYPE_MAP_START element. Only consume the AWS_CBOR_TYPE_MAP_START element and set the
418-
* size of array to *out_size, not the content of the map. The next *out_size pair of cbor data items as key and value
419-
* will be the content of the array for a valid cbor data,
428+
* @brief Get the next AWS_CBOR_TYPE_MAP_START element.
429+
* Only consume the AWS_CBOR_TYPE_MAP_START element and set the size of array to *out_size, not the content of the map.
430+
* 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,
420431
*
421432
* Notes: For indefinite-length, this function will fail with "AWS_ERROR_CBOR_UNEXPECTED_TYPE". The designed way to
422433
* handle indefinite-length is:
@@ -432,12 +443,12 @@ AWS_COMMON_API
432443
int aws_cbor_decoder_pop_next_map_start(struct aws_cbor_decoder *decoder, uint64_t *out_size);
433444

434445
/**
435-
* @brief Get the next AWS_CBOR_TYPE_TAG element. Only consume the AWS_CBOR_TYPE_TAG element and set the
436-
* tag value to *out_tag_val, not the content of the tagged. The next cbor data item will be the content of the tagged
437-
* value for a valid cbor data.
446+
* @brief Get the next AWS_CBOR_TYPE_TAG element.
447+
* Only consume the AWS_CBOR_TYPE_TAG element and set the tag ID value to *out_tag_val, not the content of the tagged.
448+
* The next cbor data item will be the content of the tagged value for a valid cbor data.
438449
*
439450
* @param decoder
440-
* @param out_size store the size of map if succeed.
451+
* @param out_tag_val store the value of tag ID.
441452
* @return AWS_OP_SUCCESS successfully consumed the next element and get the result, otherwise AWS_OP_ERR.
442453
*/
443454
AWS_COMMON_API

source/cbor.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,13 @@ size_t aws_cbor_decoder_get_remaining_length(const struct aws_cbor_decoder *deco
320320
return decoder->src.len;
321321
}
322322

323+
void aws_cbor_decoder_reset_src(struct aws_cbor_decoder *decoder, struct aws_byte_cursor src) {
324+
decoder->src = src;
325+
/* Reset all cached state */
326+
decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
327+
decoder->error_code = AWS_ERROR_SUCCESS;
328+
}
329+
323330
#define LIBCBOR_VALUE_CALLBACK(field, callback_type, cbor_type) \
324331
static void s_##field##_callback(void *ctx, callback_type val) { \
325332
struct aws_cbor_decoder *decoder = ctx; \

tests/cbor_test.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,21 +438,19 @@ CBOR_TEST_CASE(cbor_decode_error_handling_test) {
438438
struct aws_cbor_decoder *decoder = aws_cbor_decoder_new(allocator, invalid_cbor);
439439
ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type));
440440
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error());
441-
aws_cbor_decoder_destroy(decoder);
442441

443442
/* 2. Empty cursor */
444443
struct aws_byte_cursor empty = {0};
445-
decoder = aws_cbor_decoder_new(allocator, empty);
444+
aws_cbor_decoder_reset_src(decoder, empty);
446445
ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type));
447446
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error());
448-
aws_cbor_decoder_destroy(decoder);
449447

450448
/* 3. Try get wrong type */
451449
struct aws_cbor_encoder *encoder = aws_cbor_encoder_new(allocator);
452450
uint64_t val = 1;
453451
aws_cbor_encoder_write_uint(encoder, val);
454452
struct aws_byte_cursor final_cursor = aws_cbor_encoder_get_encoded_data(encoder);
455-
decoder = aws_cbor_decoder_new(allocator, final_cursor);
453+
aws_cbor_decoder_reset_src(decoder, final_cursor);
456454
uint64_t out = 0;
457455
ASSERT_FAILS(aws_cbor_decoder_pop_next_array_start(decoder, &out));
458456
ASSERT_UINT_EQUALS(AWS_ERROR_CBOR_UNEXPECTED_TYPE, aws_last_error());
@@ -463,7 +461,6 @@ CBOR_TEST_CASE(cbor_decode_error_handling_test) {
463461
ASSERT_FAILS(aws_cbor_decoder_consume_next_whole_data_item(decoder));
464462
ASSERT_FAILS(aws_cbor_decoder_peek_type(decoder, &out_type));
465463
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_CBOR, aws_last_error());
466-
aws_cbor_decoder_destroy(decoder);
467464

468465
/* 4. Consume data items with size */
469466
struct aws_byte_cursor val_1 = aws_byte_cursor_from_c_str("my test");
@@ -476,13 +473,13 @@ CBOR_TEST_CASE(cbor_decode_error_handling_test) {
476473
aws_cbor_encoder_write_tag(encoder, AWS_CBOR_TAG_NEGATIVE_BIGNUM);
477474
aws_cbor_encoder_write_bytes(encoder, val_1);
478475
final_cursor = aws_cbor_encoder_get_encoded_data(encoder);
479-
decoder = aws_cbor_decoder_new(allocator, final_cursor);
476+
aws_cbor_decoder_reset_src(decoder, final_cursor);
480477
ASSERT_SUCCESS(aws_cbor_decoder_peek_type(decoder, &out_type));
481478
ASSERT_UINT_EQUALS(AWS_CBOR_TYPE_MAP_START, out_type);
482479
ASSERT_SUCCESS(aws_cbor_decoder_consume_next_whole_data_item(decoder));
483480
ASSERT_UINT_EQUALS(0, aws_cbor_decoder_get_remaining_length(decoder));
484-
aws_cbor_decoder_destroy(decoder);
485481

482+
aws_cbor_decoder_destroy(decoder);
486483
aws_cbor_encoder_destroy(encoder);
487484
aws_common_library_clean_up();
488485
return AWS_OP_SUCCESS;

0 commit comments

Comments
 (0)