Skip to content

Commit

Permalink
Checkpoint: mostly working
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurence Lundblade committed May 10, 2024
1 parent 5adc043 commit c892908
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 39 deletions.
23 changes: 22 additions & 1 deletion inc/qcbor/UsefulBuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,10 @@ static int UsefulInputBuf_BytesAvailable(UsefulInputBuf *pUInBuf, size_t uLen);
*
* @return SIZE_MAX if @c p is out of range, the byte offset if not.
*/
static inline size_t UsefulInputBuf_PointerToOffset(UsefulInputBuf *pUInBuf, const void *p);
static size_t UsefulInputBuf_PointerToOffset(UsefulInputBuf *pUInBuf, const void *p);


static const void *UsefulInputBuf_OffsetToPointer(UsefulInputBuf *pUInBuf, size_t uOffset);


/**
Expand Down Expand Up @@ -2263,6 +2266,24 @@ static inline size_t UsefulInputBuf_PointerToOffset(UsefulInputBuf *pUInBuf, con
}


// TODO: move and test this
static inline const void *UsefulBuf_OffsetToPointer(UsefulBufC UB, size_t uOffset)
{
if(uOffset > UB.len) {
return NULL;
}

return (const uint8_t *)UB.ptr + uOffset;
}


static inline const void *UsefulInputBuf_OffsetToPointer(UsefulInputBuf *pUInBuf, size_t uOffset)
{
return UsefulBuf_OffsetToPointer(pUInBuf->UB, uOffset);
}



static inline UsefulBufC UsefulInputBuf_GetUsefulBuf(UsefulInputBuf *pMe, size_t uNum)
{
const void *pResult = UsefulInputBuf_GetBytes(pMe, uNum);
Expand Down
66 changes: 41 additions & 25 deletions inc/qcbor/qcbor_spiffy_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,19 @@ QCBORDecode_ExitArray(QCBORDecodeContext *pCtx);
@brief Get the bytes that make up an array.
@param[in] pCtx The decode context.
@param[out] puNumItems Place to return the number of items in the array.
@param[out] pItem Place to return the array item.
@param[out] pEncodedCBOR Place to return pointer and length of the array.
The next item to decode must be an array.
TODO: return the array Item, not just the number of items in it
That will give the label and tags of it too.
@c pItem will have the label and tags for the array. It is filled
in the same as if GetNext() were called on the array item. In
particular the array count will be filled in for definite-length
arrays and set to XXXX for indefinite-length arrays.
This works on both definite and indefinite length arrays (unless
indefinite length array decoding has been disabled).
Expand All @@ -730,6 +738,14 @@ QCBORDecode_ExitArray(QCBORDecodeContext *pCtx);
occurs in another map and thus has a label, the label is not included
in what is returned.
You can pass pEncodedCBOR directly to QCBORDecode_Init() if you want to
create a separate decoder instance.
QCBORDecode_GetArray() consumes the entire array and leaves
the traversal cursor at the item after the array. QCBORDecode_GetArrayFromMapN()
and QCBORDecode_GetArrayFromMapSZ() don't affect the traversal
cursor.
This traverses the whole array and every subordinate array or map in
it. This is necessary to determine the length of the array. The
traversal cursor is left at the first item after the array.
Expand All @@ -739,17 +755,17 @@ QCBORDecode_ExitArray(QCBORDecodeContext *pCtx);
See also QCBORDecode_EnterArray().
*/
static void QCBORDecode_GetArray(QCBORDecodeContext *pCtx,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR);

static void QCBORDecode_GetArrayFromMapN(QCBORDecodeContext *pCtx,
int64_t nLabel,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR);

static void QCBORDecode_GetArrayFromMapSZ(QCBORDecodeContext *pCtx,
const char *szLabel,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR);


Expand Down Expand Up @@ -842,7 +858,7 @@ QCBORDecode_ExitMap(QCBORDecodeContext *pCtx);
@brief Get the bytes that make up a map.
@param[in] pCtx The decode context.
@param[out] puNumItems Place to return the number of items in the map.
@param[out] pItem TODO:.
@param[out] pEncodedCBOR Place to return pointer and length of the map.
The next item to decode must be a map.
Expand All @@ -864,17 +880,17 @@ QCBORDecode_ExitMap(QCBORDecodeContext *pCtx);
See also QCBORDecode_EnterMap().
*/
static void QCBORDecode_GetMap(QCBORDecodeContext *pCtx,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR);

static void QCBORDecode_GetMapFromMapN(QCBORDecodeContext *pCtx,
int64_t nLabel,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR);

static void QCBORDecode_GetMapFromMapSZ(QCBORDecodeContext *pCtx,
const char *szLabel,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR);


Expand Down Expand Up @@ -2155,8 +2171,8 @@ QCBORDecode_ExitMap(QCBORDecodeContext *pMe)
* @brief Semi-private. Get pointer, length and item count of a map or array.
*
* @param[in] pCtx The decode context.
* @param[in] uType CBOR major type, either array or map.
* @param[out] puNumItems The number of data items in the map or array.
* @param[in] uType CBOR major type, either array/map.
* @param[out] pItem The item for the array/map.
* @param[out] pEncodedCBOR Pointer and length of the encoded map or array.
*
* The next item to be decoded must be a map or array as specified by \c uType.
Expand All @@ -2166,7 +2182,7 @@ QCBORDecode_ExitMap(QCBORDecodeContext *pMe)
void
QCBORDecode_Private_GetMapOrArray(QCBORDecodeContext *pCtx,
uint8_t uType,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR);


Expand All @@ -2175,7 +2191,7 @@ QCBORDecode_Private_GetMapOrArray(QCBORDecodeContext *pCtx,
*
* @param[in] pCtx The decode context.
* @param[in] pTarget The label and type of the array or map to retrieve.
* @param[out] puNumItems The number of data items in the map or array.
* @param[out] pItem TODO:
* @param[out] pEncodedCBOR Pointer and length of the encoded map or array.
*
* The next item to be decoded must be a map or array as specified by \c uType.
Expand All @@ -2184,23 +2200,23 @@ QCBORDecode_Private_GetMapOrArray(QCBORDecodeContext *pCtx,
*/
void QCBORDecode_Private_SearchAndGetMapOrArray(QCBORDecodeContext *pCtx,
QCBORItem *pTarget,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR);


static inline void
QCBORDecode_GetArray(QCBORDecodeContext *pMe,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR)
{
QCBORDecode_Private_GetMapOrArray(pMe, QCBOR_TYPE_ARRAY, puNumItems, pEncodedCBOR);
QCBORDecode_Private_GetMapOrArray(pMe, QCBOR_TYPE_ARRAY, pItem, pEncodedCBOR);
}


static inline void
QCBORDecode_GetArrayFromMapN(QCBORDecodeContext *pMe,
int64_t nLabel,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR)
{
QCBORItem OneItemSeach[2];
Expand All @@ -2209,14 +2225,14 @@ QCBORDecode_GetArrayFromMapN(QCBORDecodeContext *pMe,
OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY;
OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;

QCBORDecode_Private_SearchAndGetMapOrArray(pMe, OneItemSeach, puNumItems, pEncodedCBOR);
QCBORDecode_Private_SearchAndGetMapOrArray(pMe, OneItemSeach, pItem, pEncodedCBOR);
}


static inline void
QCBORDecode_GetArrayFromMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR)
{
QCBORItem OneItemSeach[2];
Expand All @@ -2225,22 +2241,22 @@ QCBORDecode_GetArrayFromMapSZ(QCBORDecodeContext *pMe,
OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY;
OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;

QCBORDecode_Private_SearchAndGetMapOrArray(pMe, OneItemSeach, puNumItems, pEncodedCBOR);
QCBORDecode_Private_SearchAndGetMapOrArray(pMe, OneItemSeach, pItem, pEncodedCBOR);
}

static inline void
QCBORDecode_GetMap(QCBORDecodeContext *pMe,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR)
{
QCBORDecode_Private_GetMapOrArray(pMe, QCBOR_TYPE_MAP, puNumItems, pEncodedCBOR);
QCBORDecode_Private_GetMapOrArray(pMe, QCBOR_TYPE_MAP, pItem, pEncodedCBOR);
}


static inline void
QCBORDecode_GetMapFromMapN(QCBORDecodeContext *pMe,
int64_t nLabel,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR)
{
QCBORItem OneItemSeach[2];
Expand All @@ -2249,14 +2265,14 @@ QCBORDecode_GetMapFromMapN(QCBORDecodeContext *pMe,
OneItemSeach[0].uDataType = QCBOR_TYPE_MAP;
OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;

QCBORDecode_Private_SearchAndGetMapOrArray(pMe, OneItemSeach, puNumItems, pEncodedCBOR);
QCBORDecode_Private_SearchAndGetMapOrArray(pMe, OneItemSeach, pItem, pEncodedCBOR);
}


static inline void
QCBORDecode_GetMapFromMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
uint16_t *puNumItems,
QCBORItem *pItem,
UsefulBufC *pEncodedCBOR)
{
QCBORItem OneItemSeach[2];
Expand All @@ -2265,7 +2281,7 @@ QCBORDecode_GetMapFromMapSZ(QCBORDecodeContext *pMe,
OneItemSeach[0].uDataType = QCBOR_TYPE_MAP;
OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;

QCBORDecode_Private_SearchAndGetMapOrArray(pMe, OneItemSeach, puNumItems, pEncodedCBOR);
QCBORDecode_Private_SearchAndGetMapOrArray(pMe, OneItemSeach, pItem, pEncodedCBOR);
}


Expand Down
Loading

0 comments on commit c892908

Please sign in to comment.