entering maps and arrays returns the map / array item
diff --git a/example.c b/example.c
index b0ea925..042c3b3 100644
--- a/example.c
+++ b/example.c
@@ -294,7 +294,7 @@
QCBORDecodeContext DecodeCtx;
QCBORDecode_Init(&DecodeCtx, EncodedEngine, QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterMap(&DecodeCtx);
+ QCBORDecode_EnterMap(&DecodeCtx, NULL);
QCBORDecode_GetTextStringInMapSZ(&DecodeCtx, "Manufacturer", &(pE->Manufacturer));
QCBORDecode_GetInt64InMapSZ(&DecodeCtx, "Displacement", &(pE->uDisplacement));
QCBORDecode_GetInt64InMapSZ(&DecodeCtx, "Horsepower", &(pE->uHorsePower));
@@ -359,7 +359,7 @@
QCBORDecodeContext DecodeCtx;
QCBORDecode_Init(&DecodeCtx, EncodedEngine, QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterMap(&DecodeCtx);
+ QCBORDecode_EnterMap(&DecodeCtx, NULL);
QCBORItem EngineItems[7];
EngineItems[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
diff --git a/inc/qcbor/qcbor_spiffy_decode.h b/inc/qcbor/qcbor_spiffy_decode.h
index 13a0608..04fe204 100644
--- a/inc/qcbor/qcbor_spiffy_decode.h
+++ b/inc/qcbor/qcbor_spiffy_decode.h
@@ -615,7 +615,10 @@
/**
@brief Enter an array for decoding in bounded mode.
- @param[in] pCtx The decode context.
+ @param[in] pCtx The decode context.
+ @param[out] pItem The optionally returned QCBORItem that has the
+ label and tags for the array. May be @c NULL (and
+ usually is).
This enters an array for decoding in bounded mode. The items in array
are decoded in order the same as when not in bounded mode, but the
@@ -638,7 +641,7 @@
See also QCBORDecode_ExitArray(), QCBORDecode_EnterMap() and
QCBORDecode_EnterBstrWrapped().
*/
-static void QCBORDecode_EnterArray(QCBORDecodeContext *pCtx);
+static void QCBORDecode_EnterArray(QCBORDecodeContext *pCtx, QCBORItem *pItem);
void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t uLabel);
@@ -666,7 +669,10 @@
/**
@brief Enter a map for decoding and searching.
- @param[in] pCtx The decode context.
+ @param[in] pCtx The decode context.
+ @param[out] pItem The optionally returned QCBORItem that has the
+ label and tags for the map. May be @c NULL (and
+ usually is).
The next item in the CBOR input must be map or this sets an error.
@@ -707,7 +713,7 @@
bstr-wrapped CBOR is supported up to the maximum of @ref
QCBOR_MAX_ARRAY_NESTING.
*/
-static void QCBORDecode_EnterMap(QCBORDecodeContext *pCtx);
+static void QCBORDecode_EnterMap(QCBORDecodeContext *pCtx, QCBORItem *pItem);
void QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pCtx, int64_t nLabel);
@@ -1654,16 +1660,16 @@
// Semi-private
-void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType);
+void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem);
// Semi-private
-inline static void QCBORDecode_EnterMap(QCBORDecodeContext *pMe) {
- QCBORDecode_EnterBoundedMapOrArray(pMe, QCBOR_TYPE_MAP);
+inline static void QCBORDecode_EnterMap(QCBORDecodeContext *pMe, QCBORItem *pItem) {
+ QCBORDecode_EnterBoundedMapOrArray(pMe, QCBOR_TYPE_MAP, pItem);
}
// Semi-private
-inline static void QCBORDecode_EnterArray(QCBORDecodeContext *pMe) {
- QCBORDecode_EnterBoundedMapOrArray(pMe, QCBOR_TYPE_ARRAY);
+inline static void QCBORDecode_EnterArray(QCBORDecodeContext *pMe, QCBORItem *pItem) {
+ QCBORDecode_EnterBoundedMapOrArray(pMe, QCBOR_TYPE_ARRAY, pItem);
}
// Semi-private
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 029e4e5..aee1581 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -2975,7 +2975,7 @@
DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting));
- QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType);
+ QCBORDecode_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL);
}
@@ -3039,7 +3039,7 @@
// Semi-private function
-void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType)
+void QCBORDecode_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, uint8_t uType, QCBORItem *pItem)
{
QCBORError uErr;
@@ -3080,6 +3080,10 @@
uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty,
UsefulInputBuf_Tell(&(pMe->InBuf)));
+ if(pItem != NULL) {
+ *pItem = Item;
+ }
+
Done:
pMe->uLastError = (uint8_t)uErr;
}
diff --git a/test/float_tests.c b/test/float_tests.c
index 981e897..3f8ec1a 100644
--- a/test/float_tests.c
+++ b/test/float_tests.c
@@ -891,7 +891,7 @@
TestData = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedFloats);
double d;
QCBORDecode_Init(&DC, TestData, 0);
- QCBORDecode_EnterArray(&DC);
+ QCBORDecode_EnterArray(&DC, NULL);
#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
#ifndef QCBOR_DISABLE_FLOAT_HW_USE
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 6d58749..e5585af 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -2455,7 +2455,7 @@
QCBORDecode_Init(&DC,
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterArray(&DC);
+ QCBORDecode_EnterArray(&DC, NULL);
// Too-negative float, -9.2233720368547748E+18
QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
@@ -2507,7 +2507,7 @@
return 4;
}
- QCBORDecode_EnterMap(&DC);
+ QCBORDecode_EnterMap(&DC, NULL);
// Get largest negative double precision epoch date allowed
QCBORDecode_GetEpochDateInMapN(&DC,
@@ -3198,7 +3198,7 @@
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
// tagged date string
QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
// untagged date string
@@ -3231,7 +3231,7 @@
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
// tagged date string
QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
// untagged date string
@@ -3263,7 +3263,7 @@
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
// tagged date string
QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
@@ -4539,7 +4539,7 @@
QCBORDecode_Init(&DC,
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterArray(&DC);
+ QCBORDecode_EnterArray(&DC, NULL);
// 4([-1, 3]),
QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
@@ -4657,7 +4657,7 @@
QCBORDecode_Init(&DCtx, input, 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
@@ -4841,44 +4841,45 @@
int32_t EnterMapTest()
{
QCBORItem Item1;
+ QCBORItem ArrayItem;
QCBORDecodeContext DCtx;
int32_t nReturn;
QCBORError uErr;
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
- QCBORDecode_EnterArray(&DCtx); // Label 0
+ QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
QCBORDecode_ExitArray(&DCtx);
- QCBORDecode_EnterArray(&DCtx); // Label 9
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
+ QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_ExitArray(&DCtx);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_ExitArray(&DCtx);
QCBORDecode_ExitArray(&DCtx);
- QCBORDecode_EnterMap(&DCtx); // Label 8
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
+ QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_ExitArray(&DCtx);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_ExitMap(&DCtx);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_ExitArray(&DCtx);
QCBORDecode_ExitMap(&DCtx);
- QCBORDecode_EnterMap(&DCtx); // Label4
+ QCBORDecode_EnterMap(&DCtx, NULL); // Label4
QCBORDecode_ExitMap(&DCtx);
- QCBORDecode_EnterArray(&DCtx); // Label 5
+ QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
QCBORDecode_ExitArray(&DCtx);
- QCBORDecode_EnterArray(&DCtx); // Label 6
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
+ QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_ExitArray(&DCtx);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_ExitArray(&DCtx);
QCBORDecode_ExitArray(&DCtx);
@@ -4897,9 +4898,9 @@
}
nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
- if(nReturn) {
- return nReturn;
- }
+ if(nReturn) {
+ return nReturn;
+ }
@@ -4909,7 +4910,7 @@
// Confirm cursor is at right place
QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetNext(&DCtx, &Item1);
if(Item1.uDataType != QCBOR_TYPE_INT64) {
return 2001;
@@ -4919,14 +4920,27 @@
QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
QCBORDecode_VGetNext(&DCtx, &Item1);
QCBORDecode_VGetNext(&DCtx, &Item1);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, &ArrayItem);
+ if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(ArrayItem.label.string,
+ UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
+ return 2051;
+ }
QCBORDecode_GetNext(&DCtx, &Item1);
if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
return 2002;
}
+ QCBORDecode_ExitArray(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, &ArrayItem);
+ if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ UsefulBuf_Compare(ArrayItem.label.string,
+ UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
+ return 2052;
+ }
+
QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetNext(&DCtx, &Item1);
QCBORDecode_GetNext(&DCtx, &Item1);
QCBORDecode_GetNext(&DCtx, &Item1);
@@ -4937,7 +4951,7 @@
}
QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetNext(&DCtx, &Item1);
QCBORDecode_GetNext(&DCtx, &Item1);
QCBORDecode_GetNext(&DCtx, &Item1);
@@ -4952,7 +4966,7 @@
}
QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
QCBORDecode_ExitArray(&DCtx);
QCBORDecode_GetNext(&DCtx, &Item1);
@@ -4965,7 +4979,7 @@
}
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
int64_t nDecodedInt2;
QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
uErr = QCBORDecode_GetAndResetError(&DCtx);
@@ -4980,7 +4994,7 @@
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
// This will fail because the map is empty.
QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
uErr = QCBORDecode_GetAndResetError(&DCtx);
@@ -4995,7 +5009,7 @@
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
// This will fail because the map is empty.
QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
uErr = QCBORDecode_GetAndResetError(&DCtx);
@@ -5010,11 +5024,11 @@
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_GetByteString(&DCtx, &String);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_ExitMap(&DCtx);
- QCBORDecode_EnterArray(&DCtx);
+ QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_ExitArray(&DCtx);
QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
QCBORDecode_ExitArray(&DCtx);
@@ -5025,7 +5039,7 @@
int64_t nInt;
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
uErr = QCBORDecode_GetAndResetError(&DCtx);
if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
@@ -5071,7 +5085,7 @@
}
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
uErr = QCBORDecode_GetAndResetError(&DCtx);
if(uErr != QCBOR_ERR_BAD_BREAK) {
@@ -5079,7 +5093,7 @@
}
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
uErr = QCBORDecode_GetAndResetError(&DCtx);
if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
@@ -5087,7 +5101,7 @@
}
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
uErr = QCBORDecode_GetAndResetError(&DCtx);
if(uErr != QCBOR_ERR_HIT_END) {
@@ -5095,7 +5109,7 @@
}
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
uErr = QCBORDecode_GetAndResetError(&DCtx);
if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
@@ -6089,12 +6103,12 @@
QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
- QCBORDecode_EnterMap(&DC);
+ QCBORDecode_EnterMap(&DC, NULL);
QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
QCBORDecode_ExitMap(&DC);
QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
- QCBORDecode_EnterArray(&DC);
+ QCBORDecode_EnterArray(&DC, NULL);
QCBORDecode_GetInt64(&DC, &n3);
QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
QCBORDecode_GetInt64(&DC, &n4);
@@ -6104,7 +6118,7 @@
QCBORDecode_ExitBstrWrapped(&DC);
QCBORDecode_GetInt64(&DC, &n6);
QCBORDecode_ExitBstrWrapped(&DC);
- QCBORDecode_EnterArray(&DC);
+ QCBORDecode_EnterArray(&DC, NULL);
QCBORDecode_GetInt64(&DC, &n7);
QCBORDecode_GetInt64(&DC, &n8);
QCBORDecode_ExitArray(&DC);
@@ -6214,7 +6228,7 @@
UsefulBufC String;
bool bNeg;
- QCBORDecode_EnterMap(&DC);
+ QCBORDecode_EnterMap(&DC, NULL);
QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
@@ -6454,13 +6468,13 @@
// Tests the start of a map being too large
QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterArray(&DC);
+ QCBORDecode_EnterArray(&DC, NULL);
QCBORDecode_GetTextString(&DC, &String);
uErr = QCBORDecode_GetError(&DC);
if(uErr != QCBOR_SUCCESS) {
return 1;
}
- QCBORDecode_EnterMap(&DC);
+ QCBORDecode_EnterMap(&DC, NULL);
uErr = QCBORDecode_GetError(&DC);
if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
return 2;
@@ -6468,8 +6482,8 @@
// Tests the end of a map being too large
QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterArray(&DC);
- QCBORDecode_EnterMap(&DC);
+ QCBORDecode_EnterArray(&DC, NULL);
+ QCBORDecode_EnterMap(&DC, NULL);
uErr = QCBORDecode_GetError(&DC);
if(uErr != QCBOR_SUCCESS) {
return 3;
@@ -6516,7 +6530,7 @@
QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
UsefulBufC ByteString;
- QCBORDecode_EnterMap(&DCtx);
+ QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
if(QCBORDecode_GetAndResetError(&DCtx)) {
return 1;