Add GetSimple
diff --git a/inc/qcbor/qcbor_encode.h b/inc/qcbor/qcbor_encode.h
index daea469..83c167e 100644
--- a/inc/qcbor/qcbor_encode.h
+++ b/inc/qcbor/qcbor_encode.h
@@ -1800,15 +1800,20 @@
* @param[in] pMe The encode context.
* @param[in] uNum The simple value.
*
- * Use QCBOREncode_AddBool(), QCBOREncode_AddUndef()... instead of this.
+ * QCBOREncode_AddBool(), QCBOREncode_AddUndef() and
+ * QCBOREncode_AddNull() are preferred to this for the simple values
+ * defined in RFC 8949, but this can be used for them too.
*
- * Use this to add simple values beyond those in defined RFC
- * 8949. Simple values must be registered with IANA. There is no range
- * of values for proprietary use.
+ * The main purpose of this is to add simple values beyond those in
+ * defined RFC 8949. Note that simple values must be registered with
+ * IANA. Those in the range of 0 to 19 must be standardized. Those in
+ * the range of 32 to 255 do not require a standard, but must be
+ * publically specified. There is no range of values for proprietary
+ * use. See
* https://www.iana.org/assignments/cbor-simple-values/cbor-simple-values.xhtml
*/
static void
-QCBOREncode_AddSimple(QCBOREncodeContext *pMe, const uint64_t uNum);
+QCBOREncode_AddSimple(QCBOREncodeContext *pMe, const uint8_t uNum);
static void
QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe,
@@ -3751,7 +3756,7 @@
static inline void
-QCBOREncode_AddSimple(QCBOREncodeContext *pMe, const uint64_t uNum)
+QCBOREncode_AddSimple(QCBOREncodeContext *pMe, const uint8_t uNum)
{
/* This check often is optimized out because uNum is known at compile time. */
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
@@ -3766,8 +3771,8 @@
static inline void
QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe,
- const char *szLabel,
- const uint8_t uSimple)
+ const char *szLabel,
+ const uint8_t uSimple)
{
QCBOREncode_AddSZString(pMe, szLabel);
QCBOREncode_AddSimple(pMe, uSimple);
@@ -3775,8 +3780,8 @@
static inline void
QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uSimple)
+ const int64_t nLabel,
+ const uint8_t uSimple)
{
QCBOREncode_AddInt64(pMe, nLabel);
QCBOREncode_AddSimple(pMe, uSimple);
diff --git a/inc/qcbor/qcbor_spiffy_decode.h b/inc/qcbor/qcbor_spiffy_decode.h
index ebb4b65..b3e9e8d 100644
--- a/inc/qcbor/qcbor_spiffy_decode.h
+++ b/inc/qcbor/qcbor_spiffy_decode.h
@@ -1170,6 +1170,37 @@
/**
+ * @brief Decode the next item as a CBOR simple value.
+ *
+ * @param[in] pCtx The decode context.
+ * @param[out] puSimpleValue The simplle value returned.
+ *
+ * The purpose of this is to get a CBOR simple value other than a
+ * Boolean, NULL or "undefined", but this works on all simple
+ * values. See QCBOREncode_AddSimple() for more details on simple
+ * values in general.
+ *
+ * See QCBORDecode_GetBool(), QCBORDecode_GetNull(),
+ * QCBORDecode_GetUndefined() for the preferred way of getting those
+ * simple values.
+ */
+void
+QCBORDecode_GetSimple(QCBORDecodeContext *pCtx, uint8_t *puSimpleValue);
+
+static void
+QCBORDecode_GetSimpleInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t *puSimpleValue);
+
+static void
+QCBORDecode_GetSimpleInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t *puSimpleValue);
+
+
+
+
+/**
* @brief Decode the next item as a date string.
*
* @param[in] pCtx The decode context.
@@ -2573,6 +2604,34 @@
static inline void
+QCBORDecode_GetSimpleInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ uint8_t *puSimple)
+{
+ QCBORItem Item;
+
+ QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_UKNOWN_SIMPLE, &Item);
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ *puSimple = Item.val.uSimple;
+ }
+}
+
+static inline void
+QCBORDecode_GetSimpleInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ uint8_t *puSimple)
+{
+ QCBORItem Item;
+
+ QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_UKNOWN_SIMPLE, &Item);
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ *puSimple = Item.val.uSimple;
+ }
+}
+
+
+
+static inline void
QCBORDecode_GetDateString(QCBORDecodeContext *pMe,
const uint8_t uTagRequirement,
UsefulBufC *pValue)
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index fc34304..282efdd 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -4503,6 +4503,47 @@
+void
+QCBORDecode_GetSimple(QCBORDecodeContext *pMe, uint8_t *puSimple)
+{
+ QCBORItem Item;
+
+ QCBORDecode_VGetNext(pMe, &Item);
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return;
+ }
+
+ /* It's kind of lame to remap true...undef back to simple values, but
+ * this function isn't used much.
+ */
+ switch(Item.uDataType) {
+ case QCBOR_TYPE_UKNOWN_SIMPLE:
+ *puSimple = Item.val.uSimple;
+ break;
+
+ case QCBOR_TYPE_TRUE:
+ *puSimple = CBOR_SIMPLEV_TRUE;
+ break;
+
+ case QCBOR_TYPE_FALSE:
+ *puSimple = CBOR_SIMPLEV_FALSE;
+ break;
+
+ case QCBOR_TYPE_NULL:
+ *puSimple = CBOR_SIMPLEV_NULL;
+ break;
+
+ case QCBOR_TYPE_UNDEF:
+ *puSimple = CBOR_SIMPLEV_UNDEF;
+ break;
+
+ default:
+ pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
+ return;
+ }
+}
+
+
/**
* @brief Common processing for an epoch date.
*
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index c09b4a9..4c4ecf7 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -2332,79 +2332,127 @@
0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
0xf8, 0xff};
-int32_t ParseSimpleTest(void)
+
+static const uint8_t spGoodSimpleValues[] = {
+ 0x88, 0xf4, 0xf5, 0xf6, 0xf7, 0xe0, 0xf3,
+ 0xf8, 0x20,
+ 0xf8, 0xff};
+
+int32_t SimpleValueDecodeTests(void)
{
QCBORDecodeContext DCtx;
- QCBORItem Item;
- QCBORError nCBORError;
-
+ QCBORItem Item;
+ QCBORError uErr;
QCBORDecode_Init(&DCtx,
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
QCBOR_DECODE_MODE_NORMAL);
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Item.val.uCount != 10)
- return -1;
+ return 1;
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_FALSE)
- return -1;
+ return 2;
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_TRUE)
- return -1;
+ return 3;
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_NULL)
- return -1;
+ return 4;
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_UNDEF)
- return -1;
+ return 5;
// A break
if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
- return -1;
+ return 6;
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
- return -1;
+ return 7;
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
- return -1;
+ return 8;
if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
- return -1;
+ return 9;
if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
- return -1;
+ return 10;
if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
- return -1;
+ return 11;
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
- return -1;
+ return 12;
- if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
- return (int32_t)nCBORError;
+ if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
+ return (int32_t)uErr;
if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
- return -1;
+ return 13;
+
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spGoodSimpleValues),
+ QCBOR_DECODE_MODE_NORMAL);
+
+ uint8_t uSimple;
+
+ QCBORDecode_EnterArray(&DCtx, &Item);
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_FALSE) {
+ return 20;
+ }
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_TRUE) {
+ return 21;
+ }
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_NULL) {
+ return 22;
+ }
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_UNDEF) {
+ return 23;
+ }
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) || uSimple != 0) {
+ return 24;
+ }
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
+ return 25;
+ }
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) || uSimple != 32) {
+ return 26;
+ }
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
+ return 27;
+ }
+ QCBORDecode_GetSimple(&DCtx, &uSimple);
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
+ return 28;
+ }
return 0;
-
}
diff --git a/test/qcbor_decode_tests.h b/test/qcbor_decode_tests.h
index b23149a..a08a3af 100644
--- a/test/qcbor_decode_tests.h
+++ b/test/qcbor_decode_tests.h
@@ -126,7 +126,7 @@
/*
Test parsing of some simple values like true, false, null...
*/
-int32_t ParseSimpleTest(void);
+int32_t SimpleValueDecodeTests(void);
/*
diff --git a/test/run_tests.c b/test/run_tests.c
index e4a431b..765d54f 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -82,7 +82,7 @@
TEST_ENTRY(IndefiniteLengthArrayMapTest),
TEST_ENTRY(NestedMapTestIndefLen),
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
- TEST_ENTRY(ParseSimpleTest),
+ TEST_ENTRY(SimpleValueDecodeTests),
TEST_ENTRY(DecodeFailureTests),
TEST_ENTRY(EncodeRawTest),
TEST_ENTRY(RTICResultsTest),