Skip to content

Commit

Permalink
Replace TinyCBOR APIs with QCBOR APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
glan6388 committed Jul 17, 2024
1 parent 2097e8e commit 62a3326
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 254 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ if (IS_CFS_ARCH_BUILD)
# The CFE build system determines whether to create a shared or static object inside this routine
add_cfe_app(bplib ${BPLIB_SRC})

target_link_libraries(bplib ${TINYCBOR_LDFLAGS})
target_link_libraries(bplib ${TINYCBOR_LDFLAGS} ${QCBOR_LDFLAGS})

else()

Expand All @@ -133,7 +133,7 @@ else()
add_library(bplib ${BPLIB_SRC})

# link with the requisite dependencies
target_link_libraries(bplib ${TINYCBOR_LDFLAGS})
target_link_libraries(bplib ${TINYCBOR_LDFLAGS} ${QCBOR_LDFLAGS})

# Add in the required link libraries based on OS adapter selection
# this should preferably be in OS subdirectory, but it needs to be done
Expand Down
5 changes: 5 additions & 0 deletions v7/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ find_package(PkgConfig)
pkg_search_module(TINYCBOR tinycbor REQUIRED)
message(STATUS "Found tinycbor version ${TINYCBOR_VERSION}")

find_package(PkgConfig)
pkg_search_module(QCBOR qcbor REQUIRED)
message(STATUS "Found qcbor version ${QCBOR_VERSION}")

add_library(bplib_v7 OBJECT
src/v7_codec_common.c
src/v7_encode_api.c
Expand All @@ -37,6 +41,7 @@ target_include_directories(bplib_v7 PRIVATE
# nothing outside of here should directly call TinyCBOR, so this
# can be considered private to this submodule
${TINYCBOR_INCLUDE_DIRS}
${QCBOR_INCLUDE_DIRS}
$<TARGET_PROPERTY:bplib_common,INTERFACE_INCLUDE_DIRECTORIES>
)

Expand Down
41 changes: 9 additions & 32 deletions v7/src/v7_bp_basetypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ void v7_encode_small_int(v7_encode_state_t *enc, int val)
{
if (!enc->error)
{
if (cbor_encode_int(enc->cbor, val) != CborNoError)
{
enc->error = true;
}
QCBOREncode_AddInt64(enc->cbor, val);
}
}

Expand All @@ -48,23 +45,15 @@ int v7_decode_small_int(v7_decode_state_t *dec)
int val;

val = 0;

if (!dec->error)
{
if (cbor_value_at_end(dec->cbor) || cbor_value_get_type(dec->cbor) != CborIntegerType)
QCBORDecode_GetInt64(dec->cbor, (int64_t*)&val);
QCBORError err = QCBORDecode_GetError(dec->cbor);
if(err != QCBOR_SUCCESS)
{
dec->error = true;
}
else
{
if (cbor_value_get_int(dec->cbor, &val) != CborNoError)
{
dec->error = true;
}
else if (cbor_value_advance_fixed(dec->cbor) != CborNoError)
{
dec->error = true;
}
}
}

return val;
Expand All @@ -83,21 +72,12 @@ void v7_decode_bp_integer(v7_decode_state_t *dec, bp_integer_t *v)
val = 0;
if (!dec->error)
{
if (cbor_value_at_end(dec->cbor) || cbor_value_get_type(dec->cbor) != CborIntegerType)
QCBORDecode_GetUInt64(dec->cbor, &val);
QCBORError err = QCBORDecode_GetError(dec->cbor);
if(err != QCBOR_SUCCESS)
{
dec->error = true;
}
else
{
if (cbor_value_get_uint64(dec->cbor, &val) != CborNoError)
{
dec->error = true;
}
else if (cbor_value_advance_fixed(dec->cbor) != CborNoError)
{
dec->error = true;
}
}
}

*v = val;
Expand All @@ -107,10 +87,7 @@ void v7_encode_bp_integer(v7_encode_state_t *enc, const bp_integer_t *v)
{
if (!enc->error)
{
if (cbor_encode_uint(enc->cbor, *v) != CborNoError)
{
enc->error = true;
}
QCBOREncode_AddUInt64(enc->cbor, *v);
}
}

Expand Down
95 changes: 18 additions & 77 deletions v7/src/v7_bp_canonical_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,11 @@ void v7_encode_bp_canonical_info(v7_encode_state_t *enc, const v7_canonical_bloc
* This encodes the data and snapshots the position of the END of that CBOR byte
* string.
*/
cbor_encode_byte_string(enc->cbor, info->content_vptr, info->content_size);
UsefulBufC bytes_buf;
bytes_buf.ptr = info->content_vptr;
bytes_buf.len = info->content_size;
QCBOREncode_AddBytes(enc->cbor, bytes_buf);

if (!enc->error)
{
if (enc->total_bytes_encoded < info->content_size)
{
/* this should never happen */
enc->error = true;
}
else if (info->content_offset_out != NULL)
{
/*
* Export the location where the encoded data actually appeared in the stream.
*
* CBOR markup occurs at the beginning of each record, so by knowing
* where it ends it is easy to find the beginning of actual data, as the CBOR
* markup itself is variable size, but the content is a known size here.
*/
*info->content_offset_out = enc->total_bytes_encoded - info->content_size;
}
}
}

void v7_encode_bp_canonical_bundle_block(v7_encode_state_t *enc, const bp_canonical_bundle_block_t *v,
Expand Down Expand Up @@ -192,80 +176,37 @@ void v7_decode_bp_block_processing_flags(v7_decode_state_t *dec, bp_block_proces

void v7_decode_bp_canonical_info(v7_decode_state_t *dec, v7_canonical_block_info_t *info)
{
const uint8_t *cbor_content_start_ptr;
size_t cbor_content_length;
size_t cbor_major_size;


QCBORItem item;
QCBORError err;
UsefulBufC buff;
/*
* The content within this context must be a CBOR byte string.
* This byte string, in turn, _might_ have CBOR-encoded data within it,
* but it won't be decoded right now, just grab the location and
* make sure everything seems legit at the outer layer
*/
if (cbor_value_get_type(dec->cbor) != CborByteStringType)
err = QCBORDecode_PeekNext(dec->cbor, &item);
if (err != QCBOR_SUCCESS || item.uDataType != QCBOR_TYPE_BYTE_STRING)
{
dec->error = true;
return;
}

cbor_content_start_ptr = cbor_value_get_next_byte(dec->cbor);
if (cbor_value_advance(dec->cbor) != CborNoError)
{
dec->error = true;
return;
}

/*
* This calculated length reflects the start of this CBOR value to the
* start of the next CBOR value. Notably this includes the CBOR overhead/markup
* for this value, which will need to be removed. TinyCBOR will want to
* copy the value, so we go around it for this value and decode the major
* type locally.
*/
cbor_content_length = cbor_value_get_next_byte(dec->cbor) - cbor_content_start_ptr;

/* Advance the pointer according to the CBOR length to get to the real data. */
cbor_major_size = *cbor_content_start_ptr & 0x1F;
if (cbor_major_size < 24)
{
/* no extra bytes beyond the major type */
cbor_major_size = 0;
}
else if (cbor_major_size < 28)
{
/* 1, 2, 4, or 8 additional bytes beyond the major type */
cbor_major_size = 1 << (cbor_major_size - 24);
}
else
{
/* Value not well formed, or indefinite length (not supported here) */
cbor_major_size = cbor_content_length;
}

++cbor_major_size; /* Account for the CBOR major type octet itself (always there) */
if (cbor_major_size <= cbor_content_length)
{
cbor_content_start_ptr += cbor_major_size;
cbor_content_length -= cbor_major_size;

/* Export the position of the now-located content information */
*info->content_offset_out = cbor_content_start_ptr - dec->base;
info->content_vptr = cbor_content_start_ptr;
info->content_size = cbor_content_length;
}
else
{
/* This should not happen */
dec->error = true;
}
QCBORDecode_GetByteString(dec->cbor, &buff);
/* Export the position of the now-located content information */
*info->content_offset_out = (uint8_t*)buff.ptr - dec->base;
info->content_vptr = buff.ptr;
info->content_size = buff.len;
}

void v7_decode_bp_canonical_bundle_block(v7_decode_state_t *dec, bp_canonical_bundle_block_t *v,
v7_canonical_block_info_t *info)
{
bp_canonical_field_t field_id = bp_canonical_field_undef;

while (field_id < bp_canonical_field_done && !dec->error && !cbor_value_at_end(dec->cbor))

while (field_id < bp_canonical_field_done && !dec->error)
{
switch (field_id)
{
Expand Down Expand Up @@ -314,7 +255,7 @@ void v7_decode_bp_canonical_block_buffer(v7_decode_state_t *dec, bp_canonical_bl
info.decode_block = &v->canonical_block;
info.content_offset_out = content_encoded_offset;

v7_decode_container(dec, CborIndefiniteLength, v7_decode_bp_canonical_block_buffer_impl, &info);
v7_decode_container(dec, QCBOR_MAX_ITEMS_IN_ARRAY, v7_decode_bp_canonical_block_buffer_impl, &info);

*content_length = info.content_size;
}
70 changes: 13 additions & 57 deletions v7/src/v7_bp_container.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,80 +41,36 @@

void v7_encode_container(v7_encode_state_t *enc, size_t entries, v7_encode_func_t func, const void *arg)
{
CborEncoder content;
CborEncoder *parent;

if (!enc->error)
{
/* Save the parent encode state */
parent = enc->cbor;
QCBOREncode_OpenArray(enc->cbor);

if (cbor_encoder_create_array(parent, &content, entries) != CborNoError)
{
enc->error = true;
}
else
{
/* go into container */
enc->cbor = &content;

/* call the handler impl */
func(enc, arg);

/* return to parent */
if (cbor_encoder_close_container(parent, &content) != CborNoError)
{
enc->error = true;
}

enc->cbor = parent;
}
/* call the handler impl */
func(enc, arg);

QCBOREncode_CloseArray(enc->cbor);
}
}

void v7_decode_container(v7_decode_state_t *dec, size_t entries, v7_decode_func_t func, void *arg)
{
CborValue content;
CborValue *parent;
QCBORItem item;

if (!dec->error)
{
/* Save the parent decode state */
parent = dec->cbor;

if (cbor_value_at_end(parent) || cbor_value_get_type(parent) != CborArrayType)

QCBORDecode_EnterArray(dec->cbor, &item);
if (item.uDataType != QCBOR_TYPE_ARRAY)
{
dec->error = true;
}
else
{
if (cbor_value_enter_container(parent, &content) != CborNoError)
{
dec->error = true;
}
else
{
/* go into container */
dec->cbor = &content;

/* call the handler impl */
func(dec, arg);

/* This should have consumed every item */
if (!dec->error)
{
if (!cbor_value_at_end(&content))
{
dec->error = true;
}
else if (cbor_value_leave_container(parent, &content) != CborNoError)
{
dec->error = true;
}
}

dec->cbor = parent;
}
/* call the handler impl */
func(dec, arg);

QCBORDecode_ExitArray(dec->cbor);
}
}
}
Loading

0 comments on commit 62a3326

Please sign in to comment.