enter / exit code working and somewhat orderly
diff --git a/QCBOR.xcodeproj/project.pbxproj b/QCBOR.xcodeproj/project.pbxproj
index a193355..a720857 100644
--- a/QCBOR.xcodeproj/project.pbxproj
+++ b/QCBOR.xcodeproj/project.pbxproj
@@ -78,7 +78,7 @@
E776E096214AE0C700E67947 /* cmd_line_main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = cmd_line_main.c; sourceTree = "<group>"; };
E776E161214EE19C00E67947 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
E788315B243BFDF0001893CD /* qcbor_decode_map.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = qcbor_decode_map.h; path = inc/qcbor/qcbor_decode_map.h; sourceTree = "<group>"; };
- E78C91DE240C90C100F4CECE /* qcbor_decode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = qcbor_decode.h; path = inc/qcbor/qcbor_decode.h; sourceTree = "<group>"; };
+ E78C91DE240C90C100F4CECE /* qcbor_decode.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.h; name = qcbor_decode.h; path = inc/qcbor/qcbor_decode.h; sourceTree = "<group>"; tabWidth = 3; };
E78C91DF240C90C100F4CECE /* qcbor_common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = qcbor_common.h; path = inc/qcbor/qcbor_common.h; sourceTree = "<group>"; };
E78C91E0240C90C100F4CECE /* qcbor_private.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.h; name = qcbor_private.h; path = inc/qcbor/qcbor_private.h; sourceTree = "<group>"; tabWidth = 3; };
E78C91E1240C90C100F4CECE /* qcbor_encode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = qcbor_encode.h; path = inc/qcbor/qcbor_encode.h; sourceTree = "<group>"; };
diff --git a/inc/qcbor/qcbor_decode.h b/inc/qcbor/qcbor_decode.h
index 3688a59..ac6df25 100644
--- a/inc/qcbor/qcbor_decode.h
+++ b/inc/qcbor/qcbor_decode.h
@@ -1084,10 +1084,25 @@
Entering and Exiting map mode consumes the whole
map and its contents as a GetNext after exiting
will return the item after the map. */
-static QCBORError QCBORDecode_EnterMap(QCBORDecodeContext *pCtx);
+static void QCBORDecode_EnterMap(QCBORDecodeContext *pCtx);
-void QCBORDecode_ExitMap(QCBORDecodeContext *pCtx);
+/*
+ Find a map in a map by integer label and enter it.
+
+ This will do duplicate detection on the particular label.
+
+ Call QCBORDecode_ExitMap() to return to the mode / level
+ from before this was called.
+
+ Seek to to the beginning of the map.
+ Consume items looking for nLabel
+ */
+void QCBORDecode_EnterMapFromMap(QCBORDecodeContext *pCtx, int64_t nLabel);
+
+void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pCtx, const char *szLabel);
+
+static void QCBORDecode_ExitMap(QCBORDecodeContext *pCtx);
/*
Indicate if decoding is in map mode more not.
@@ -1103,18 +1118,15 @@
void QCBORDecode_RewindMap(QCBORDecodeContext *pCtxt);
-QCBORError QCBORDecode_EnterArray(QCBORDecodeContext *pCtx);
+static void QCBORDecode_EnterArray(QCBORDecodeContext *pCtx);
+
+void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t uLabel);
+
+void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel);
+
+static void QCBORDecode_ExitArray(QCBORDecodeContext *pCtx);
-void QCBORDecode_ExitArray(QCBORDecodeContext *pCtx);
-
-QCBORError QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t uLabel);
-
-
-QCBORError QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel);
-
-
-//QCBORError QCBORDecode_EnterMapX(QCBORDecodeContext *pCtx, MapDecode *pMap);
@@ -1183,20 +1195,7 @@
void QCBORDecode_GetTextInMapSZ(QCBORDecodeContext *pCtx, const char *szLabel, UsefulBufC *pBstr);
-/*
- Find a map in a map by integer label and enter it.
-
- This will do duplicate detection on the particular label.
-
- Call QCBORDecode_ExitMap() to return to the mode / level
- from before this was called.
-
- Seek to to the beginning of the map.
- Consume items looking for nLabel
- */
-QCBORError QCBORDecode_EnterMapFromMap(QCBORDecodeContext *pCtx, int64_t nLabel);
-QCBORError QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pCtx, const char *szLabel);
@@ -1374,15 +1373,33 @@
}
-
-QCBORError QCBORDecode_EnterMapMode(QCBORDecodeContext *pMe, uint8_t uType);
+// Semi-private
+void QCBORDecode_EnterMapMode(QCBORDecodeContext *pMe, uint8_t uType);
-inline static QCBORError QCBORDecode_EnterMap(QCBORDecodeContext *pMe) {
- return QCBORDecode_EnterMapMode(pMe, QCBOR_TYPE_MAP);
+inline static void QCBORDecode_EnterMap(QCBORDecodeContext *pMe) {
+ QCBORDecode_EnterMapMode(pMe, QCBOR_TYPE_MAP);
}
+inline static void QCBORDecode_EnterArray(QCBORDecodeContext *pMe) {
+ QCBORDecode_EnterMapMode(pMe, QCBOR_TYPE_ARRAY);
+}
+
+// Semi-private
+void QCBORDecode_ExitMapMode(QCBORDecodeContext *pMe, uint8_t uType);
+
+
+static inline void QCBORDecode_ExitArray(QCBORDecodeContext *pMe)
+{
+ QCBORDecode_ExitMapMode(pMe, QCBOR_TYPE_ARRAY);
+}
+
+static inline void QCBORDecode_ExitMap(QCBORDecodeContext *pMe)
+{
+ QCBORDecode_ExitMapMode(pMe, QCBOR_TYPE_MAP);
+}
+
// Semi-private
void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe, uint32_t uOptions, int64_t *pValue, QCBORItem *pItem);
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index f5995fe..0fdb242 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -2129,10 +2129,12 @@
}
-void QCBORDecode_ExitMap(QCBORDecodeContext *pMe)
+void QCBORDecode_ExitMapMode(QCBORDecodeContext *pMe, uint8_t uType)
{
size_t uEndOffset;
+ (void)uType; // TODO: error check
+
/*
if(pMe->uMapEndOffset) {
uEndOffset = pMe->uMapEndOffset;
@@ -2213,8 +2215,19 @@
-static int FinishEnter(QCBORDecodeContext *pMe, size_t uOffset)
+static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[])
{
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ // Already in error state; do nothing.
+ return;
+ }
+
+ size_t uOffset;
+ pMe->uLastError = MapSearch(pMe, pSearch, &uOffset, NULL);
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return;
+ }
+
/* Need to get the current pre-order nesting level and cursor to be
at the first item in the map/array just entered.
@@ -2234,104 +2247,58 @@
pMe->nesting.pCurrent = pMe->nesting.pCurrentMap; // TODO: part of DecodeNesting
// TODO: check error?
- QCBORDecode_EnterMapMode(pMe, QCBOR_TYPE_MAP);
+ QCBORDecode_EnterMapMode(pMe, pSearch->uDataType);
- printdecode(pMe, "Entered Map in Map");
-
- return 0;
+ printdecode(pMe, "FinishEnter");
}
-QCBORError QCBORDecode_EnterMapInMapN(QCBORDecodeContext *pMe, int64_t nLabel)
+void QCBORDecode_EnterMapInMapN(QCBORDecodeContext *pMe, int64_t nLabel)
{
- /* Use GetItemsInMap to find the map by label, including the
- byte offset of it. */
QCBORItem One[2];
One[0].uLabelType = QCBOR_TYPE_INT64;
One[0].label.int64 = nLabel;
One[0].uDataType = QCBOR_TYPE_MAP;
One[1].uLabelType = QCBOR_TYPE_NONE;
- size_t uOffset;
- QCBORError nReturn = MapSearch(pMe, One, &uOffset, NULL);
- if(nReturn) {
- return nReturn;
- }
-
/* The map to enter was found, now finish of entering it. */
- FinishEnter(pMe, uOffset);
-
- // TODO: error code?
- return 0;
+ SearchAndEnter(pMe, One);
}
-QCBORError QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
+void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
{
QCBORItem One[2];
-
One[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
One[0].label.string = UsefulBuf_FromSZ(szLabel);
One[0].uDataType = QCBOR_TYPE_MAP;
One[1].uLabelType = QCBOR_TYPE_NONE;
-
- size_t uOffset;
-
- QCBORError nReturn = MapSearch(pMe, One, &uOffset, NULL);
-
- if(nReturn) {
- return nReturn;
- }
- FinishEnter(pMe, uOffset);
-
- return 0;
+ SearchAndEnter(pMe, One);
}
-QCBORError QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel)
-{
- QCBORItem One[2];
-
- One[0].uLabelType = QCBOR_TYPE_INT64;
- One[0].label.int64 = nLabel;
- One[0].uDataType = QCBOR_TYPE_ARRAY;
- One[1].uLabelType = QCBOR_TYPE_NONE;
-
- size_t uOffset;
-
- QCBORError nReturn = MapSearch(pMe, One, &uOffset, NULL);
-
- if(nReturn != QCBOR_SUCCESS) {
- return nReturn;
- }
-
- FinishEnter(pMe, uOffset);
-
- return 0;
-}
-
-
-QCBORError QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
+void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel)
{
QCBORItem One[2];
+ One[0].uLabelType = QCBOR_TYPE_INT64;
+ One[0].label.int64 = nLabel;
+ One[0].uDataType = QCBOR_TYPE_ARRAY;
+ One[1].uLabelType = QCBOR_TYPE_NONE;
+ SearchAndEnter(pMe, One);
+}
+
+
+void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
+{
+ QCBORItem One[2];
One[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
One[0].label.string = UsefulBuf_FromSZ(szLabel);
One[0].uDataType = QCBOR_TYPE_ARRAY;
One[1].uLabelType = QCBOR_TYPE_NONE;
- size_t uOffset;
-
- QCBORError nReturn = MapSearch(pMe, One, &uOffset, NULL);
-
- if(nReturn != QCBOR_SUCCESS) {
- return nReturn;
- }
-
- FinishEnter(pMe, uOffset);
-
- return 0;
+ SearchAndEnter(pMe, One);
}
@@ -2339,25 +2306,27 @@
/* Next item must be map or this generates an error */
-QCBORError QCBORDecode_EnterMapMode(QCBORDecodeContext *pMe, uint8_t uType)
+void QCBORDecode_EnterMapMode(QCBORDecodeContext *pMe, uint8_t uType)
{
- QCBORItem Item;
- QCBORError nReturn;
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ // Already in error state; do nothing.
+ return;
+ }
/* Get the data item that is the map that is being searched */
- nReturn = QCBORDecode_GetNext(pMe, &Item);
- if(nReturn != QCBOR_SUCCESS) {
- return nReturn;
+ QCBORItem Item;
+ pMe->uLastError = QCBORDecode_GetNext(pMe, &Item);
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return;
}
if(Item.uDataType != uType) {
- return QCBOR_ERR_UNEXPECTED_TYPE;
+ pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
+ return;
}
DecodeNesting_EnterMapMode(&(pMe->nesting), UsefulInputBuf_Tell(&(pMe->InBuf)));
- printdecode(pMe, "EnterMapDone");
-
- return QCBOR_SUCCESS;
+ printdecode(pMe, "EnterMapModeDone");
}
@@ -2379,54 +2348,7 @@
}
-QCBORError QCBORDecode_EnterArray(QCBORDecodeContext *pMe)
-{
- QCBORItem Item;
- QCBORError nReturn;
- /* Get the data item that is the map that is being searched */
- nReturn = QCBORDecode_GetNext(pMe, &Item);
- if(nReturn != QCBOR_SUCCESS) {
- return nReturn;
- }
- if(Item.uDataType != QCBOR_TYPE_ARRAY) {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
-
- printdecode(pMe, "EnterArray");
-
- DecodeNesting_EnterMapMode(&(pMe->nesting), UsefulInputBuf_Tell(&(pMe->InBuf)));
-
- return QCBOR_SUCCESS;
-}
-
-
-void QCBORDecode_ExitArray(QCBORDecodeContext *pMe)
-{
- // TODO: make sure we have entered an array
- // TODO: combine with code for map? It is the same so far.
- size_t uEndOffset;
-
- /* if(pMe->uMapEndOffset) {
- uEndOffset = pMe->uMapEndOffset;
- // It is only valid once.
- pMe->uMapEndOffset = 0;
- } else {*/
- QCBORItem Dummy;
-
- Dummy.uLabelType = QCBOR_TYPE_NONE;
-
- QCBORError nReturn = MapSearch(pMe, &Dummy, NULL, &uEndOffset);
-
- (void)nReturn; // TODO:
- //}
-
- printdecode(pMe, "start exit");
- UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset);
-
- DecodeNesting_Exit(&(pMe->nesting));
- printdecode(pMe, "end exit");
-}
void QCBORDecode_GetIntInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, int64_t *pInt)