Skip to content

Commit

Permalink
OffsetToPtr documentation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurence Lundblade committed May 11, 2024
1 parent f6872ac commit c7668eb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
64 changes: 42 additions & 22 deletions inc/qcbor/UsefulBuf.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* =========================================================================
* Copyright (c) 2016-2018, The Linux Foundation.
* Copyright (c) 2018-2022, Laurence Lundblade.
* Copyright (c) 2018-2024, Laurence Lundblade.
* Copyright (c) 2021, Arm Limited. All rights reserved.
* All rights reserved.
*
Expand Down Expand Up @@ -43,6 +43,7 @@
when who what, where, why
-------- ---- --------------------------------------------------
10/05/2024 llundblade Add Xxx_OffsetToPointer.
19/12/2022 llundblade Document that adding empty data is allowed.
4/11/2022 llundblade Add GetOutPlace and Advance to UsefulOutBuf.
9/21/2021 llundbla Clarify UsefulOutBuf size calculation mode
Expand Down Expand Up @@ -647,16 +648,27 @@ size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind);


/**
@brief Convert a pointer to an offset with bounds checking.
@param[in] UB Pointer to the UsefulInputBuf.
@param[in] p Pointer to convert to offset.
@return SIZE_MAX if @c p is out of range, the byte offset if not.
* @brief Convert a pointer to an offset with bounds checking.
*
* @param[in] UB A UsefulBuf.
* @param[in] p Pointer to convert to offset.
*
* @return SIZE_MAX if @c p is out of range, the byte offset if not.
*/
static inline size_t UsefulBuf_PointerToOffset(UsefulBufC UB, const void *p);


/**
* @brief Convert an offset to a pointer with bounds checking.
*
* @param[in] UB A UsefulBuf.
* @param[in] uOffset Offset in @c pUInBuf.
*
* @return @c NULL if @c uOffset is out of range, a pointer into the buffer if not.
*/
static inline const void *UsefulBuf_OffsetToPointer(UsefulBufC UB, size_t uOffset);


#ifndef USEFULBUF_DISABLE_DEPRECATED
/** Deprecated macro; use @ref UsefulBuf_FROM_SZ_LITERAL instead */
#define SZLiteralToUsefulBufC(szString) UsefulBuf_FROM_SZ_LITERAL(szString)
Expand Down Expand Up @@ -1494,6 +1506,14 @@ static int UsefulInputBuf_BytesAvailable(UsefulInputBuf *pUInBuf, size_t uLen);
static size_t UsefulInputBuf_PointerToOffset(UsefulInputBuf *pUInBuf, const void *p);


/**
* @brief Convert an offset to a pointer with bounds checking.
*
* @param[in] pUInBuf Pointer to the @ref UsefulInputBuf.
* @param[in] uOffset Offset in @c pUInBuf.
*
* @return @c NULL if @c uOffset is out of range, a pointer into the buffer if not.
*/
static const void *UsefulInputBuf_OffsetToPointer(UsefulInputBuf *pUInBuf, size_t uOffset);


Expand Down Expand Up @@ -1860,6 +1880,18 @@ static inline size_t UsefulBuf_PointerToOffset(UsefulBufC UB, const void *p)
}


static inline const void *UsefulBuf_OffsetToPointer(UsefulBufC UB, size_t uOffset)
{
if(UsefulBuf_IsNULLC(UB) || uOffset >= UB.len) {
return NULL;
}

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




#ifndef USEFULBUF_DISABLE_ALL_FLOAT
static inline uint32_t UsefulBufUtil_CopyFloatToUint32(float f)
{
Expand Down Expand Up @@ -2266,22 +2298,10 @@ 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);
}

{
return UsefulBuf_OffsetToPointer(pUInBuf->UB, uOffset);
}


static inline UsefulBufC UsefulInputBuf_GetUsefulBuf(UsefulInputBuf *pMe, size_t uNum)
Expand Down
30 changes: 30 additions & 0 deletions test/UsefulBuf_Tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,22 @@ const char *UBUtilTests(void)
return "Incorrect pointer offset for start";
}

if(UsefulBuf_OffsetToPointer(Boo, 0) != &pB[0]) {
return "Wrong OffsetToPointer";
}

if(UsefulBuf_OffsetToPointer(Boo, 3) != NULL) {
return "Didn't validate offset correctly";
}

if(UsefulBuf_OffsetToPointer(Boo, 2) != &pB[2]) {
return "Wrong OffsetToPointer 2";
}

if(UsefulBuf_OffsetToPointer(NULLUsefulBufC, 2) != NULL) {
return "Failed OffsetToPtr on NULLUsefulBufC";
}

return NULL;
}

Expand Down Expand Up @@ -800,6 +816,20 @@ const char * UIBTest_IntegerFormat(void)
return "PointerToOffset not working";
}


const uint8_t pB[] = {0x01, 0x02, 0x03};
UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);

UsefulInputBuf_Init(&UIB, Boo);

if(UsefulInputBuf_OffsetToPointer(&UIB, 0) != &pB[0]) {
return "OffsetToPointer fail";
}

if(UsefulInputBuf_OffsetToPointer(&UIB, SIZE_MAX) != NULL) {
return "OffsetToPointer SIZE_MAX fail";
}

return NULL;
}

Expand Down

0 comments on commit c7668eb

Please sign in to comment.