diff --git a/src/cbor.h b/src/cbor.h index 24c6df69..31d19713 100644 --- a/src/cbor.h +++ b/src/cbor.h @@ -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 */ diff --git a/src/cborinternal_p.h b/src/cborinternal_p.h index 06fa6a2e..faa76829 100644 --- a/src/cborinternal_p.h +++ b/src/cborinternal_p.h @@ -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 */ diff --git a/src/cborparser.c b/src/cborparser.c index 099cdc8f..9cdc6849 100644 --- a/src/cborparser.c +++ b/src/cborparser.c @@ -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) { diff --git a/tests/parser/tst_parser.cpp b/tests/parser/tst_parser.cpp index 6dd4a353..740e8ea4 100644 --- a/tests/parser/tst_parser.cpp +++ b/tests/parser/tst_parser.cpp @@ -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(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 QByteArray raw(const char (&data)[N]) { return QByteArray::fromRawData(data, N - 1); @@ -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 {