Add QCBORDecoder_Tell(); traversal cursor position bug fix (#223)
Adds QCBORDecode_Tell() to return traversal cursor position.
Fixes a traversal cursor position bug. The position was not correct after a QCBORDecode_GetXXXInX() of a non-aggregate item by label (e.g., QCBORDecode_GetInt64InMapN()) in an array that was entered.
Fixes to documentation for traversal cursor.
Addresses #213
* Add QCBORDecoder_Tell()
* Testing near complete and passing
* More tests and documentation
* Add some more documentation
* typo
---------
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 2188211..799fff2 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -3132,6 +3132,29 @@
}
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+uint32_t
+QCBORDecode_Tell(QCBORDecodeContext *pMe)
+{
+ size_t uCursorOffset;
+
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return UINT32_MAX;
+ }
+
+ uCursorOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
+
+ if(uCursorOffset == UsefulInputBuf_GetBufferLength(&(pMe->InBuf))) {
+ return UINT32_MAX;
+ } else {
+ /* Cast is safe because decoder input size is restricted. */
+ return (uint32_t)uCursorOffset;
+ }
+}
+
+
/**
* @brief Rewind cursor to start as if map or array were just entered.
*
@@ -3262,6 +3285,7 @@
}
QCBORDecodeNesting SaveNesting;
+ size_t uSavePos = UsefulInputBuf_Tell(&(pMe->InBuf));
DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting);
/* Reposition to search from the start of the map / array */
@@ -3378,6 +3402,7 @@
Done:
DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting);
+ UsefulInputBuf_Seek(&(pMe->InBuf), uSavePos);
Done2:
/* For all items not found, set the data and label type to QCBOR_TYPE_NONE */