Skip to content

Commit 239ee1d

Browse files
committed
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.
1 parent 7c349db commit 239ee1d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/cbor.h

+15
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ CBOR_PRIVATE_API CborError _cbor_value_copy_string(const CborValue *value, void
425425
size_t *buflen, CborValue *next);
426426
CBOR_PRIVATE_API CborError _cbor_value_dup_string(const CborValue *value, void **buffer,
427427
size_t *buflen, CborValue *next);
428+
CBOR_PRIVATE_API CborError _cbor_value_ptr_string(const CborValue *value, void **ptr,
429+
size_t *len, CborValue *next);
428430

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

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

459+
CBOR_INLINE_API CborError cbor_value_ptr_text_string(const CborValue *value, char **ptr,
460+
size_t *len, CborValue *next)
461+
{
462+
assert(cbor_value_is_text_string(value));
463+
return _cbor_value_ptr_string(value, (void **)ptr, len, next);
464+
}
465+
CBOR_INLINE_API CborError cbor_value_ptr_byte_string(const CborValue *value, uint8_t **ptr,
466+
size_t *len, CborValue *next)
467+
{
468+
assert(cbor_value_is_byte_string(value));
469+
return _cbor_value_ptr_string(value, (void **)ptr, len, next);
470+
}
471+
457472
CBOR_API CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result);
458473

459474
/* Maps and arrays */

src/cborparser.c

+23
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,29 @@ CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
12171217
copied_all ? CborNoError : CborErrorOutOfMemory;
12181218
}
12191219

1220+
CborError _cbor_value_ptr_string(const CborValue *value, void **ptr,
1221+
size_t *len, CborValue *next)
1222+
{
1223+
CborError err;
1224+
CborValue tmp;
1225+
1226+
cbor_assert(cbor_value_is_length_known(value));
1227+
if (!next)
1228+
next = &tmp;
1229+
*next = *value;
1230+
1231+
err = extract_length(next->parser, &next->ptr, len);
1232+
if (err)
1233+
return err;
1234+
if (*len > (size_t)(next->parser->end - next->ptr))
1235+
return CborErrorUnexpectedEOF;
1236+
1237+
*ptr = (void *)next->ptr;
1238+
next->ptr += *len;
1239+
1240+
return CborNoError;
1241+
}
1242+
12201243
/**
12211244
* Compares the entry \a value with the string \a string and stores the result
12221245
* in \a result. If the value is different from \a string \a result will

0 commit comments

Comments
 (0)