Skip to content

Commit

Permalink
Parser: remove the cbor_value_get_xxxx_string_chunk() API
Browse files Browse the repository at this point in the history
It's really good, but I need to redesign it in order to support the
chained buffers found in Zephyr[1][2] and Mynewt. I'll bring it back in
0.6 when I introduce support for parsing directly from chained buffers
too.

[1] http://docs.zephyrproject.org/api/networking.html#network-buffers
[2] https://github.com/zephyrproject-rtos/zephyr/blob/master/include/net/buf.h

Signed-off-by: Thiago Macieira <[email protected]>
  • Loading branch information
thiagomacieira committed Jan 30, 2018
1 parent 2a38a95 commit 5515a99
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 139 deletions.
15 changes: 0 additions & 15 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,21 +452,6 @@ CBOR_INLINE_API CborError cbor_value_dup_byte_string(const CborValue *value, uin
return _cbor_value_dup_string(value, (void **)buffer, buflen, next);
}

CBOR_PRIVATE_API CborError _cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
size_t *len, CborValue *next);
CBOR_INLINE_API CborError cbor_value_get_text_string_chunk(const CborValue *value, const char **bufferptr,
size_t *len, CborValue *next)
{
assert(cbor_value_is_text_string(value));
return _cbor_value_get_string_chunk(value, (const void **)bufferptr, len, next);
}
CBOR_INLINE_API CborError cbor_value_get_byte_string_chunk(const CborValue *value, const uint8_t **bufferptr,
size_t *len, CborValue *next)
{
assert(cbor_value_is_byte_string(value));
return _cbor_value_get_string_chunk(value, (const void **)bufferptr, len, next);
}

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

/* Maps and arrays */
Expand Down
3 changes: 3 additions & 0 deletions src/cborinternal_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,8 @@ enum {

CBOR_INTERNAL_API CBOR_INTERNAL_API_CC CborError _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len);
CBOR_INTERNAL_API CBOR_INTERNAL_API_CC CborError _cbor_value_prepare_string_iteration(CborValue *it);
CBOR_INTERNAL_API CBOR_INTERNAL_API_CC CborError _cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
size_t *len, CborValue *next);


#endif /* CBORINTERNAL_P_H */
82 changes: 1 addition & 81 deletions src/cborparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1038,87 +1038,7 @@ static CborError get_string_chunk(CborValue *it, const void **bufferptr, size_t
return CborNoError;
}

/**
* \fn CborError cbor_value_get_text_string_chunk(const CborValue *value, const char **bufferptr, size_t *len, CborValue *next)
*
* Extracts one text string chunk pointed to by \a value and stores a pointer
* to the data in \a buffer and the size in \a len, which must not be null. If
* no more chunks are available, then \a bufferptr will be set to null. This
* function may be used to iterate over any string without causing its contents
* to be copied to a separate buffer, like the convenience function
* cbor_value_copy_text_string() does.
*
* It is designed to be used in code like:
*
* \code
* if (cbor_value_is_text_string(value)) {
* char *ptr;
* size_t len;
* while (1) {
* err = cbor_value_get_text_string_chunk(value, &ptr, &len, &value));
* if (err) return err;
* if (ptr == NULL) return CborNoError;
* consume(ptr, len);
* }
* }
* \endcode
*
* If the iterator \a value does not point to a text string, the behaviour is
* undefined, so checking with \ref cbor_value_get_type or \ref
* cbor_value_is_text_string is recommended.
*
* The \a next pointer, if not null, will be updated to point to the next item
* after this string. During iteration, the pointer must only be passed back
* again to this function; passing it to any other function in this library
* results in undefined behavior. If there are no more chunks to be read from
* \a value, then \a next will be set to the next item after this string; if \a
* value points to the last item, then \a next will be invalid.
*
* \note This function does not perform UTF-8 validation on the incoming text
* string.
*
* \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_caculate_string_length(), cbor_value_get_byte_string_chunk()
*/

/**
* \fn CborError cbor_value_get_byte_string_chunk(const CborValue *value, const char **bufferptr, size_t *len, CborValue *next)
*
* Extracts one byte string chunk pointed to by \a value and stores a pointer
* to the data in \a buffer and the size in \a len, which must not be null. If
* no more chunks are available, then \a bufferptr will be set to null. This
* function may be used to iterate over any string without causing its contents
* to be copied to a separate buffer, like the convenience function
* cbor_value_copy_byte_string() does.
*
* It is designed to be used in code like:
*
* \code
* if (cbor_value_is_byte_string(value)) {
* char *ptr;
* size_t len;
* while (1) {
* err = cbor_value_get_byte_string_chunk(value, &ptr, &len, &value));
* if (err) return err;
* if (ptr == NULL) return CborNoError;
* consume(ptr, len);
* }
* }
* \endcode
*
* If the iterator \a value does not point to a byte string, the behaviour is
* undefined, so checking with \ref cbor_value_get_type or \ref
* cbor_value_is_byte_string is recommended.
*
* The \a next pointer, if not null, will be updated to point to the next item
* after this string. During iteration, the pointer must only be passed back
* again to this function; passing it to any other function in this library
* results in undefined behavior. If there are no more chunks to be read from
* \a value, then \a next will be set to the next item after this string; if \a
* value points to the last item, then \a next will be invalid.
*
* \sa cbor_value_dup_byte_string(), cbor_value_copy_byte_string(), cbor_value_caculate_string_length(), cbor_value_get_text_string_chunk()
*/

CBOR_INTERNAL_API_CC
CborError _cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
size_t *len, CborValue *next)
{
Expand Down
45 changes: 2 additions & 43 deletions tests/parser/tst_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,36 +120,6 @@ CborError parseOne(CborValue *it, QString *parsed)
return cbor_value_to_pretty_stream(qstring_printf, parsed, it, flags);
}

CborError parseOneChunk(CborValue *it, QString *parsed)
{
CborError err;
CborType ourType = cbor_value_get_type(it);
if (ourType == CborByteStringType) {
const uint8_t *bytes;
size_t len;
err = cbor_value_get_byte_string_chunk(it, &bytes, &len, it);
if (err)
return err;

if (bytes)
*parsed = QString::fromLatin1("h'" +
QByteArray::fromRawData(reinterpret_cast<const char *>(bytes), len).toHex() +
'\'');
} else if (ourType == CborTextStringType) {
const char *text;
size_t len;
err = cbor_value_get_text_string_chunk(it, &text, &len, it);
if (err)
return err;

if (text)
*parsed = '"' + QString::fromUtf8(text, len) + '"';
} else {
Q_ASSERT(false);
}
return err;
}

template <size_t N> QByteArray raw(const char (&data)[N])
{
return QByteArray::fromRawData(data, N - 1);
Expand Down Expand Up @@ -909,19 +879,8 @@ static void chunkedStringTest(const QByteArray &data, const QString &concatenate

CborValue copy = value;

forever {
QString decoded;
err = parseOneChunk(&value, &decoded);
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");

if (decoded.isEmpty())
break; // last chunk

QVERIFY2(!chunks.isEmpty(), "Too many chunks");
QString expected = chunks.takeFirst();
QCOMPARE(decoded, expected);
}
QVERIFY2(chunks.isEmpty(), "Too few chunks");
Q_UNUSED(chunks); // for future API
QCOMPARE(cbor_value_advance(&value), CborNoError);

// compare to the concatenated data
{
Expand Down

0 comments on commit 5515a99

Please sign in to comment.