more tweaks to bstr decoding
diff --git a/inc/qcbor/qcbor_private.h b/inc/qcbor/qcbor_private.h
index 66dbb8e..b0f8285 100644
--- a/inc/qcbor/qcbor_private.h
+++ b/inc/qcbor/qcbor_private.h
@@ -177,10 +177,16 @@
is no actual tracking.
This also records whether a level is bounded or not.
- All byte-count tracked levels are bounded. Maps and
+ All byte-count tracked levels (the top-level sequence
+ and bstr-wrapped CBOR) are bounded. Maps and
arrays may or may not be bounded. They are bounded if
- uStartOffset is not 0xffffffff.
-
+ they were Entered() and not if they were traversed with
+ GetNext(). They are recoded as bounded if uStartOffset is not UINT32_MAX.
+ */
+ /*
+ If uLevelType can put in a separatly indexed array,
+ the unnion / struct will be 8 bytes and a lot of wasted
+ padding for alignment can be saved.
*/
uint8_t uLevelType;
union {
@@ -198,43 +204,23 @@
*pCurrent,
*pCurrentBounded;
/*
- bounded or not with one bit in a uint16_t
+ pCurrent is for item-by-item pre-order traversal.
- bIsBounded = bits & (0x01 << index)
- bits = bits | (0x01 << index)
- bits = bits & ~(0x01 << index)
+ pCurrentBounded points to the current bounding level
+ or is NULL if there isn't one.
- byte[index] = type;
- type = bytes[index]
+ pCurrent must always be below pCurrentBounded as the
+ pre-order traversal is always bounded by the bounding
+ level.
- or...
-
- bIsBounded = 0x80 & bytes[index] // check
- bytes[index] = bytes[index] | 0x80; // set
- bytes[index] = bytes[index] & ~0x80; // clear
-
- type = byte[index] & ~0x80;
- byte[index] = (byte[index] & 0x80) + type
-
-
-
+ When a bounded level is entered, the pre-order traversal
+ is set to the first item in the bounded level. When
+ a bounded level is exited, the pre-order traversl is set
+ to the next item after the map, array or bstr. This may
+ be more than one level up, or even the end of the
+ input CBOR.
*/
- /*
- b = xxx & (0x01 << index)
-
- xxx = xxx | (0x01 << index)
-
- xxx = xxx & ~(0x01 << index)
-
-
- t = (ttt[index] & 0x3) + 4;
- b = ttt[index] & 0x04;
-
- ttt[index] = (t - 4) + (b * 4);
-
- */
- //uint8_t uNestType[QCBOR_MAX_ARRAY_NESTING1+1];
} QCBORDecodeNesting;
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 2637f3d..ebbca4e 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -181,7 +181,7 @@
return true;
}
-inline static bool DecodeNesting_InBoundedMode(const QCBORDecodeNesting *pNesting)
+inline static bool DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting)
{
if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) {
return true;
@@ -215,7 +215,7 @@
/* Not a map or array */
return false;
}
- if(!DecodeNesting_InBoundedMode(pNesting)) { // TODO: pCurrent vs pCurrentBounded
+ if(!DecodeNesting_IsCurrentBounded(pNesting)) { // TODO: pCurrent vs pCurrentBounded
/* Not in bounded mode. */
return false;
}
@@ -265,26 +265,20 @@
}
}
-
-// return 1 if closed out an array or map
inline static void
-DecodeNesting_DecrementX(QCBORDecodeNesting *pNesting)
+DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting)
{
/* Only call on array / map; TODO: add check?*/
pNesting->pCurrent->u.ma.uCountCursor--;
}
-
inline static void
DecodeNesting_Ascend(QCBORDecodeNesting *pNesting)
{
pNesting->pCurrent--;
}
-
-
-
inline static void
DecodeNesting_EnterBoundedMode(QCBORDecodeNesting *pNesting, size_t uOffset)
{
@@ -326,9 +320,9 @@
pNesting->pCurrent++;
// Fill in the new level fully
- pNesting->pCurrent->uLevelType = uQCBORType;
- pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount;
- pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount;
+ pNesting->pCurrent->uLevelType = uQCBORType;
+ pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount;
+ pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount;
DecodeNesting_ClearBoundedMode(pNesting);
@@ -1212,7 +1206,7 @@
// TODO: check this.
*/
if(DecodeNesting_IsDefiniteLength(&(pMe->nesting))) {
- DecodeNesting_DecrementX(&(pMe->nesting));
+ DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting));
if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) {
/* Didn't close out map or array; all work here is done */
break;
@@ -1251,7 +1245,7 @@
/* All items in the level have been consumed. */
/* But ascent in bounded mode is only by explicit call to QCBORDecode_ExitBoundedMode() */
- if(DecodeNesting_InBoundedMode(&(pMe->nesting))) {
+ if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) {
/* Set the count to zero for indefinite length arrays to indicate cursor is at end of bounded map / array */
pMe->nesting.pCurrent->u.ma.uCountCursor = 0;
break;
@@ -1420,7 +1414,7 @@
// were closed out and makes it possible for them to reconstruct
// the tree with just the information returned by GetNext
// TODO: pull this into DecodeNesting_GetLevel
- if(DecodeNesting_InBoundedMode(&(me->nesting)) && me->nesting.pCurrent->u.ma.uCountCursor == 0) {
+ if(DecodeNesting_IsCurrentBounded(&(me->nesting)) && me->nesting.pCurrent->u.ma.uCountCursor == 0) {
// At end of a map / array in map mode, so next nest is 0 to
// indicate this end.
pDecodedItem->uNextNestLevel = 0;
@@ -2794,7 +2788,7 @@
there is one. */
while(pMe->nesting.pCurrentBounded != &(pMe->nesting.pMapsAndArrays[0])) {
pMe->nesting.pCurrentBounded--;
- if(DecodeNesting_InBoundedMode(&(pMe->nesting))) {
+ if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) {
break;
}
}
@@ -2873,9 +2867,11 @@
goto Done;
}
- //if(pMe->nesting.pCurrent)
- // TODO: will this work for indefinite lengths?
- pMe->nesting.pCurrent->u.ma.uCountCursor++; // Don't count the bstr yet
+ if(DecodeNesting_IsDefiniteLength(&(pMe->nesting))) {
+ /* Reverse the decrement done by GetNext() for the bstr as
+ so the increment in ExitExit()->Ascender() will work right. */
+ pMe->nesting.pCurrent->u.ma.uCountCursor++;
+ }
if(pBstr) {
*pBstr = pItem->val.string;
@@ -2896,11 +2892,9 @@
UsefulInputBuf_SetBufferLen(&(pMe->InBuf), uEndOfBstr);
- // TODO: comment on cast
uError = DecodeNesting_DescendWrappedBstr(&(pMe->nesting),
uPreviousLength,
uEndOfBstr);
-
Done:
printdecode(pMe, "Entered Bstr");