Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cbor_value_ptr_{text,byte}_string #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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);

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_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 */
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_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
Expand Down