Skip to content

Commit

Permalink
decoder: Make skip_simple work with (b|t)str types
Browse files Browse the repository at this point in the history
  • Loading branch information
bergzand committed Oct 18, 2023
1 parent ae01e39 commit f3929cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,16 @@ void nanocbor_leave_container(nanocbor_value_t *it, nanocbor_value_t *container)

static int _skip_simple(nanocbor_value_t *it)
{
int type = nanocbor_get_type(it);
uint64_t tmp = 0;
int res = _get_uint64(it, &tmp, NANOCBOR_SIZE_LONG, nanocbor_get_type(it));
return _advance_if(it, res);
if (type == NANOCBOR_TYPE_BSTR || type == NANOCBOR_TYPE_TSTR) {
const uint8_t *tmp = NULL;
size_t len = 0;
return _get_str(it, &tmp, &len, (uint8_t)type);
}
int res = _get_uint64(it, &tmp, NANOCBOR_SIZE_LONG, type);
res = _advance_if(it, res);
return res > 0 ? NANOCBOR_OK : res;
}

int nanocbor_get_subcbor(nanocbor_value_t *it, const uint8_t **start,
Expand All @@ -556,13 +563,8 @@ static int _skip_limited(nanocbor_value_t *it, uint8_t limit)
int type = nanocbor_get_type(it);
int res = type;

if (type == NANOCBOR_TYPE_BSTR || type == NANOCBOR_TYPE_TSTR) {
const uint8_t *tmp = NULL;
size_t len = 0;
res = _get_str(it, &tmp, &len, (uint8_t)type);
}
/* map or array */
else if (type == NANOCBOR_TYPE_ARR || type == NANOCBOR_TYPE_MAP) {
if (type == NANOCBOR_TYPE_ARR || type == NANOCBOR_TYPE_MAP) {
nanocbor_value_t recurse;
res = (type == NANOCBOR_TYPE_MAP ? nanocbor_enter_map(it, &recurse)
: nanocbor_enter_array(it, &recurse));
Expand Down
34 changes: 34 additions & 0 deletions tests/automated/test_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,36 @@ static void test_decode_basic(void)
CU_ASSERT_EQUAL(mantissa, 27315);
}

static void _decode_skip_simple(const uint8_t *test_case, size_t test_case_len)
{
nanocbor_value_t decoder;
nanocbor_decoder_init(&decoder, test_case, test_case_len);
CU_ASSERT_EQUAL(nanocbor_at_end(&decoder), false);
int res = nanocbor_skip_simple(&decoder);
printf("skip result = %d", res);
CU_ASSERT_EQUAL(res, NANOCBOR_OK);
CU_ASSERT_EQUAL(nanocbor_at_end(&decoder), true);
}

static void test_decode_skip(void)
{
static const uint8_t test_uint[] = { 0x00 };
_decode_skip_simple(test_uint, sizeof(test_uint));
static const uint8_t test_nint[] = { 0x20 };
_decode_skip_simple(test_nint, sizeof(test_nint));
static const uint8_t test_bstr_empty[] = { 0x40 };
_decode_skip_simple(test_bstr_empty, sizeof(test_bstr_empty));
static const uint8_t test_bstr[] = { 0x42, 0xAA, 0xBB };
_decode_skip_simple(test_bstr, sizeof(test_bstr));
static const uint8_t test_tstr[] = { 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F };
_decode_skip_simple(test_tstr, sizeof(test_tstr));

static const uint8_t test_float[] = { 0xF9, 0x42, 0x00 };
_decode_skip_simple(test_float, sizeof(test_float));
static const uint8_t test_simple[] = { 0xF4 };
_decode_skip_simple(test_simple, sizeof(test_simple));
}

const test_t tests_decoder[] = {
{
.f = test_decode_none,
Expand All @@ -243,6 +273,10 @@ const test_t tests_decoder[] = {
.f = test_double_tag,
.n = "CBOR double tag decode test",
},
{
.f = test_decode_skip,
.n = "CBOR simple skip test",
},
{
.f = NULL,
.n = NULL,
Expand Down

0 comments on commit f3929cd

Please sign in to comment.