Improve test and doc for GetString
diff --git a/inc/qcbor/qcbor_spiffy_decode.h b/inc/qcbor/qcbor_spiffy_decode.h
index ba92891..b3839c4 100644
--- a/inc/qcbor/qcbor_spiffy_decode.h
+++ b/inc/qcbor/qcbor_spiffy_decode.h
@@ -821,8 +821,8 @@
/** @private Semi-private function. See qcbor_spiffy_decode.c */
void
QCBORDecode_Private_GetString(QCBORDecodeContext *pMe,
- UsefulBufC *pText,
- uint8_t uType);
+ uint8_t uType,
+ UsefulBufC *pText);
/** @private Semi-private function. See qcbor_spiffy_decode.c */
void
@@ -1013,7 +1013,7 @@
static inline void
QCBORDecode_GetByteString(QCBORDecodeContext *pMe, UsefulBufC *pBytes)
{
- QCBORDecode_Private_GetString(pMe, pBytes, QCBOR_TYPE_BYTE_STRING);
+ QCBORDecode_Private_GetString(pMe, QCBOR_TYPE_BYTE_STRING, pBytes);
}
static inline void
@@ -1052,7 +1052,7 @@
static inline void
QCBORDecode_GetTextString(QCBORDecodeContext *pMe, UsefulBufC *pText)
{
- QCBORDecode_Private_GetString(pMe, pText, QCBOR_TYPE_TEXT_STRING);
+ QCBORDecode_Private_GetString(pMe, QCBOR_TYPE_TEXT_STRING, pText);
}
static inline void
diff --git a/src/qcbor_spiffy_decode.c b/src/qcbor_spiffy_decode.c
index cd2e4e0..badfc86 100644
--- a/src/qcbor_spiffy_decode.c
+++ b/src/qcbor_spiffy_decode.c
@@ -66,18 +66,27 @@
#endif
-/* Public function, see qcbor/qcbor_spiffy_decode.h */
+/**
+ * @brief Spiffy decode get a byte string.
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] uType The CBOR qcbor type requested.
+ * @param[out] pString The returned string.
+ *
+ * This sets the spiffy decode last error if there is a problem
+ * deocing or the string is not of the requested type.
+ */
void
-QCBORDecode_Private_GetString(QCBORDecodeContext *pMe, UsefulBufC *pText, uint8_t uType)
+QCBORDecode_Private_GetString(QCBORDecodeContext *pMe, const uint8_t uType, UsefulBufC *pString)
{
QCBORItem Item;
QCBORDecode_VGetNext(pMe, &Item);
- *pText = NULLUsefulBufC;
+ *pString = NULLUsefulBufC;
if(pMe->uLastError == QCBOR_SUCCESS) {
if(Item.uDataType == uType) {
- *pText = Item.val.string;
+ *pString = Item.val.string;
} else {
pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
}
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 305489c..8ae0e3d 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -9323,7 +9323,7 @@
/*
An array of three map entries
- 1) Indefinite length string label for indefinite lenght byte string
+ 1) Indefinite length string label for indefinite length byte string
2) Indefinite length string label for an integer
3) Indefinite length string label for an indefinite-length negative big num
*/
@@ -9416,6 +9416,58 @@
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
+static const uint8_t spStringsTest[] = {
+ 0x63, 'a', 'b', 'c',
+ 0x43, 'd', 'e', 'f'
+};
+
+static const uint8_t spNotWellFormed[] = {
+ 0xff
+};
+
+int32_t SpiffyStringTest(void)
+{
+ QCBORDecodeContext DC;
+ UsefulBufC String;
+
+ QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spStringsTest), 0);
+
+ QCBORDecode_GetTextString(&DC, &String);
+ if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS &&
+ UsefulBuf_Compare(String, SZLiteralToUsefulBufC("abc"))) {
+ return 1;
+ }
+
+ QCBORDecode_GetByteString(&DC, &String);
+ if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS &&
+ UsefulBuf_Compare(String, SZLiteralToUsefulBufC("def"))) {
+ return 2;
+ }
+
+ QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spStringsTest), 0);
+ QCBORDecode_GetByteString(&DC, &String);
+ if(QCBORDecode_GetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE &&
+ !UsefulBuf_IsNULLC(String)) {
+ return 3;
+ }
+
+ QCBORDecode_Init(&DC, ByteArrayLiteralToUsefulBufC(spNotWellFormed), 0);
+ QCBORDecode_GetByteString(&DC, &String);
+ if(QCBORDecode_GetError(&DC) != QCBOR_ERR_BAD_BREAK &&
+ !UsefulBuf_IsNULLC(String)) {
+ return 4;
+ }
+
+#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
+ return SpiffyIndefiniteLengthStringsTests();
+#else
+ return 0;
+#endif
+
+}
+
+
+
#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
/*
* An array of an integer and an array. The second array contains
diff --git a/test/qcbor_decode_tests.h b/test/qcbor_decode_tests.h
index b542896..65020ff 100644
--- a/test/qcbor_decode_tests.h
+++ b/test/qcbor_decode_tests.h
@@ -299,7 +299,7 @@
/*
Test spiffy decoding of indefinite length strings.
*/
-int32_t SpiffyIndefiniteLengthStringsTests(void);
+int32_t SpiffyStringTest(void);
/*
diff --git a/test/run_tests.c b/test/run_tests.c
index bcb4540..f186f57 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -131,7 +131,7 @@
TEST_ENTRY(MemPoolTest),
TEST_ENTRY(IndefiniteLengthStringTest),
#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
- TEST_ENTRY(SpiffyIndefiniteLengthStringsTests),
+ TEST_ENTRY(SpiffyStringTest),
#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
TEST_ENTRY(SetUpAllocatorTest),
TEST_ENTRY(CBORTestIssue134),