Skip to content

Commit

Permalink
nrf_rpc: add function for decoding string without copying
Browse files Browse the repository at this point in the history
This commit adds new helper function nrf_rpc_decode_str_ptr_and_len()
that decodes CBOR string without copying it to a buffer. It allows to
get string length before buffer allocation.

Signed-off-by: Konrad Derda <[email protected]>
  • Loading branch information
konradderda committed Dec 3, 2024
1 parent 09228a5 commit 99e4ff9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/nrf_rpc/nrf_rpc_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ int64_t nrf_rpc_decode_int64(struct nrf_rpc_cbor_ctx *ctx);
*/
char *nrf_rpc_decode_str(struct nrf_rpc_cbor_ctx *ctx, char *buffer, size_t buffer_size);

/** @brief Decode a string pointer and length. Moves CBOR pointer past string on success.
*
* @param[in,out] ctx CBOR decoding context.
* @param[out] size String length.
*
* @retval Pointer to a string within CBOR stream or NULL on error.
*/
const void *nrf_rpc_decode_str_ptr_and_len(struct nrf_rpc_cbor_ctx *ctx, size_t *len);

/** Decode a string value into a scratchpad.
*
* @param[in] scratchpad Pointer to the scratchpad.
Expand Down
26 changes: 26 additions & 0 deletions subsys/nrf_rpc/nrf_rpc_serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,32 @@ char *nrf_rpc_decode_str(struct nrf_rpc_cbor_ctx *ctx, char *buffer, size_t buff
return buffer;
}

const void *nrf_rpc_decode_str_ptr_and_len(struct nrf_rpc_cbor_ctx *ctx, size_t *size)
{
struct zcbor_string zst = {0};

if (is_decoder_invalid(ctx)) {
return NULL;
}

if (zcbor_nil_expect(ctx->zs, NULL)) {
return NULL;
}

if (ctx->zs->constant_state->error != ZCBOR_ERR_WRONG_TYPE) {
return NULL;
}

zcbor_pop_error(ctx->zs);

if (!zcbor_tstr_decode(ctx->zs, &zst)) {
return NULL;
}

*size = zst.len;
return zst.value;
}

char *nrf_rpc_decode_str_into_scratchpad(struct nrf_rpc_scratchpad *scratchpad, size_t *len)
{
struct nrf_rpc_cbor_ctx *ctx = scratchpad->ctx;
Expand Down

0 comments on commit 99e4ff9

Please sign in to comment.