From 239ee1dc361a4421942621432691aa392333f359 Mon Sep 17 00:00:00 2001 From: Peter Mutsaers Date: Tue, 16 Mar 2021 12:42:44 +0100 Subject: [PATCH] Add cbor_value_ptr_{text,byte}_string Add functions to directly obtain a pointer to the data of a text or byte string, without the need to copy. Otherwise these are analogous to the corresponding _copy_ functions. --- src/cbor.h | 15 +++++++++++++++ src/cborparser.c | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/cbor.h b/src/cbor.h index d6360d82..123b5111 100644 --- a/src/cbor.h +++ b/src/cbor.h @@ -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_ptr_string(const CborValue *value, void **ptr, + size_t *len, CborValue *next); CBOR_API CborError cbor_value_calculate_string_length(const CborValue *value, size_t *length); @@ -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_ptr_text_string(const CborValue *value, char **ptr, + size_t *len, CborValue *next) +{ + assert(cbor_value_is_text_string(value)); + return _cbor_value_ptr_string(value, (void **)ptr, len, next); +} +CBOR_INLINE_API CborError cbor_value_ptr_byte_string(const CborValue *value, uint8_t **ptr, + size_t *len, CborValue *next) +{ + assert(cbor_value_is_byte_string(value)); + return _cbor_value_ptr_string(value, (void **)ptr, len, next); +} + CBOR_API CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result); /* Maps and arrays */ diff --git a/src/cborparser.c b/src/cborparser.c index 60ce5748..bc2adc37 100644 --- a/src/cborparser.c +++ b/src/cborparser.c @@ -1217,6 +1217,29 @@ CborError _cbor_value_copy_string(const CborValue *value, void *buffer, copied_all ? CborNoError : CborErrorOutOfMemory; } +CborError _cbor_value_ptr_string(const CborValue *value, void **ptr, + 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; + + *ptr = (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