Skip to content

Commit

Permalink
add cbor_value_ref_{text,byte}_string
Browse files Browse the repository at this point in the history
add functions to directly obtain a pointer to the data
of a text or byte string, without the need to copy.

otherwise these are analog to the corresponding _copy_ functions.
  • Loading branch information
petm-dec committed Mar 16, 2021
1 parent 7c349db commit 21e445b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ CBOR_PRIVATE_API CborError _cbor_value_copy_string(const CborValue *value, void
size_t *buflen, CborValue *next);
CBOR_PRIVATE_API CborError _cbor_value_dup_string(const CborValue *value, void **buffer,
size_t *buflen, CborValue *next);
CBOR_PRIVATE_API CborError _cbor_value_ref_string(const CborValue *value, void **buffer,
size_t *buflen, CborValue *next);

CBOR_API CborError cbor_value_calculate_string_length(const CborValue *value, size_t *length);

Expand Down Expand Up @@ -454,6 +456,19 @@ CBOR_INLINE_API CborError cbor_value_dup_byte_string(const CborValue *value, uin
return _cbor_value_dup_string(value, (void **)buffer, buflen, next);
}

CBOR_INLINE_API CborError cbor_value_ref_text_string(const CborValue *value, char **buffer,
size_t *buflen, CborValue *next)
{
assert(cbor_value_is_text_string(value));
return _cbor_value_ref_string(value, (void **)buffer, buflen, next);
}
CBOR_INLINE_API CborError cbor_value_ref_byte_string(const CborValue *value, uint8_t **buffer,
size_t *buflen, CborValue *next)
{
assert(cbor_value_is_byte_string(value));
return _cbor_value_ref_string(value, (void **)buffer, buflen, next);
}

CBOR_API CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result);

/* Maps and arrays */
Expand Down
23 changes: 23 additions & 0 deletions src/cborparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,29 @@ CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
copied_all ? CborNoError : CborErrorOutOfMemory;
}

CborError _cbor_value_ref_string(const CborValue *value, void **ref,
size_t *len, CborValue *next)
{
CborError err;
CborValue tmp;

cbor_assert(cbor_value_is_length_known(value));
if (!next)
next = &tmp;
*next = *value;

err = extract_length(next->parser, &next->ptr, len);
if (err)
return err;
if (*len > (size_t)(next->parser->end - next->ptr))
return CborErrorUnexpectedEOF;

*ref = (void *)next->ptr;
next->ptr += *len;

return CborNoError;
}

/**
* Compares the entry \a value with the string \a string and stores the result
* in \a result. If the value is different from \a string \a result will
Expand Down

0 comments on commit 21e445b

Please sign in to comment.