clean up use of const with UsefulBuf. May solve compiler errors/warnings for some compilers / compiler options
diff --git a/inc/UsefulBuf.h b/inc/UsefulBuf.h
index 2b808dc..b9bdf2f 100644
--- a/inc/UsefulBuf.h
+++ b/inc/UsefulBuf.h
@@ -146,7 +146,7 @@
*/
typedef struct {
const void *ptr;
- const size_t len;
+ size_t len;
} UsefulBufC;
@@ -157,7 +157,7 @@
*/
typedef struct {
void *ptr;
- const size_t len;
+ size_t len;
} UsefulBuf;
@@ -647,11 +647,6 @@
*/
void UsefulOutBuf_Init(UsefulOutBuf *me, UsefulBuf Storage);
-static inline void UsefulOutBuf_Realloc(UsefulOutBuf *me, UsefulBuf Storage)
-{
- me->UB = Storage;
-}
-
diff --git a/src/UsefulBuf.c b/src/UsefulBuf.c
index 8cb8c49..282b6ad 100644
--- a/src/UsefulBuf.c
+++ b/src/UsefulBuf.c
@@ -70,7 +70,7 @@
memcpy((uint8_t *)Dest.ptr + uOffset, Src.ptr, Src.len);
- return((UsefulBufC){Dest.ptr, Src.len + uOffset});
+ return (UsefulBufC){Dest.ptr, Src.len + uOffset};
}
@@ -277,7 +277,7 @@
return NULLUsefulBufC;
}
- return(UsefulBufC){me->UB.ptr,me->data_len};
+ return (UsefulBufC){me->UB.ptr,me->data_len};
}
@@ -288,7 +288,7 @@
*/
UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *me, UsefulBuf pDest)
{
- UsefulBufC Tmp = UsefulOutBuf_OutUBuf(me);
+ const UsefulBufC Tmp = UsefulOutBuf_OutUBuf(me);
if(UsefulBuf_IsNULLC(Tmp)) {
return NULLUsefulBufC;
}
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 1a923f7..8a1b151 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -560,7 +560,7 @@
// Stack usage: UsefulBuf 2, int/ptr 1 40
QCBORError nReturn = QCBOR_SUCCESS;
- UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, uStrLen);
+ const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, uStrLen);
if(UsefulBuf_IsNULLC(Bytes)) {
// Failed to get the bytes for this string item
nReturn = QCBOR_ERR_HIT_END;
@@ -596,7 +596,7 @@
return QCBOR_ERR_BAD_OPT_TAG;
}
- UsefulBufC Temp = pDecodedItem->val.string;
+ const UsefulBufC Temp = pDecodedItem->val.string;
pDecodedItem->val.dateString = Temp;
pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING;
return QCBOR_SUCCESS;
@@ -612,7 +612,7 @@
if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) {
return QCBOR_ERR_BAD_OPT_TAG;
}
- UsefulBufC Temp = pDecodedItem->val.string;
+ const UsefulBufC Temp = pDecodedItem->val.string;
pDecodedItem->val.bigNum = Temp;
pDecodedItem->uDataType = pDecodedItem->uTagBits & QCBOR_TAGFLAG_POS_BIGNUM ? QCBOR_TYPE_POSBIGNUM : QCBOR_TYPE_NEGBIGNUM;
return QCBOR_SUCCESS;
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 99eaafd..a99da1f 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -495,8 +495,8 @@
// on any subsequent calls to this function because of the
// InsertEncodedTypeAndNumber() call that slides data to the right.
if(pWrappedCBOR) {
- UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
- size_t uBstrLen = UsefulOutBuf_GetEndPosition(&(me->OutBuf)) - uEndPosition;
+ const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
+ const size_t uBstrLen = UsefulOutBuf_GetEndPosition(&(me->OutBuf)) - uEndPosition;
*pWrappedCBOR = UsefulBuf_Tail(PartialResult, uInsertPosition+uBstrLen);
}
Nesting_Decrease(&(me->nesting));
diff --git a/test/UsefulBuf_Tests.c b/test/UsefulBuf_Tests.c
index fc99ee6..82f2da6 100644
--- a/test/UsefulBuf_Tests.c
+++ b/test/UsefulBuf_Tests.c
@@ -335,7 +335,7 @@
return "IsNULLOrEmpty failed";
}
- UsefulBufC UBC = UsefulBuf_Const(UB);
+ const UsefulBufC UBC = UsefulBuf_Const(UB);
if(!UsefulBuf_IsNULLC(UBC)){
return "IsNull const failed";
@@ -349,7 +349,7 @@
return "IsNULLOrEmptyC failed";
}
- UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
+ const UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
if(!UsefulBuf_IsEmpty(UB2)) {
return "Back to UB is Empty failed";
}
@@ -371,7 +371,7 @@
// Set 100 bytes of '+'; validated a few tests later
UsefulBuf_MAKE_STACK_UB(Temp, 100);
- UsefulBufC TempC = UsefulBuf_Set(Temp, '+');
+ const UsefulBufC TempC = UsefulBuf_Set(Temp, '+');
// Try to copy into a buf that is too small and see failure
UsefulBuf_MAKE_STACK_UB(Temp2, 99);
@@ -409,7 +409,7 @@
}
// Try to copy into a NULL/empty buf and see failure
- UsefulBuf UBNull = NULLUsefulBuf;
+ const UsefulBuf UBNull = NULLUsefulBuf;
if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, TempC))) {
return "Copy to NULL should have failed";
}
@@ -465,7 +465,7 @@
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
'+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
};
- UsefulBufC ExpectedBigger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedBigger);
+ const UsefulBufC ExpectedBigger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedBigger);
// Expect -1 when the first arg is smaller
if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
@@ -485,7 +485,7 @@
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
'+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
};
- UsefulBufC ExpectedSmaller = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedSmaller);
+ const UsefulBufC ExpectedSmaller = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedSmaller);
// Expect +1 when the first arg is larger
if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
return "Compare with smaller";
@@ -504,7 +504,7 @@
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
};
- UsefulBufC ExpectedLonger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedLonger);
+ const UsefulBufC ExpectedLonger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedLonger);
// Expect -1 when the first arg is smaller
if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
@@ -524,7 +524,7 @@
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
'+', '+', '+', '+', '+', '+', '+', '+', '+',
};
- UsefulBufC ExpectedShorter = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedShorter);
+ const UsefulBufC ExpectedShorter = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedShorter);
// Expect +1 with the first arg is larger
if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
return "Compare with shorter";
@@ -542,7 +542,7 @@
// look for ++* in ....++* and find it at the end
static const uint8_t pToFind[] = {'+', '+', '*'};
- UsefulBufC ToBeFound = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pToFind);
+ const UsefulBufC ToBeFound = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pToFind);
if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){
return "Failed to find 2";
@@ -581,7 +581,7 @@
UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat
UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble
- UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
+ const UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
if(UsefulBuf_IsNULLC(O))
return "Couldn't output integers";
@@ -621,7 +621,7 @@
// Reset and go again for a few more tests
UsefulInputBuf_Init(&UIB, O);
- UsefulBufC Four = UsefulInputBuf_GetUsefulBuf(&UIB, 4);
+ const UsefulBufC Four = UsefulInputBuf_GetUsefulBuf(&UIB, 4);
if(UsefulBuf_IsNULLC(Four)) {
return "Four is NULL";
}
diff --git a/test/qcbor_decode_malloc_tests.c b/test/qcbor_decode_malloc_tests.c
index ea6be04..ea184b5 100644
--- a/test/qcbor_decode_malloc_tests.c
+++ b/test/qcbor_decode_malloc_tests.c
@@ -66,7 +66,7 @@
// Next parse, save pointers to a few strings, destroy original and see all is OK.
UsefulBuf_MAKE_STACK_UB(CopyOfStorage, 160);
- UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
+ const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
QCBORStringAllocator *pAlloc = QCBORDecode_MakeMallocStringAllocator();
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 08cf947..6c201e8 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -1422,7 +1422,7 @@
QCBORItem Item;
int nCBORError;
- UsefulBufC Input = {pBuf, nLen+1};
+ const UsefulBufC Input = {pBuf, nLen+1};
QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
@@ -2197,7 +2197,7 @@
UsefulBuf_MAKE_STACK_UB(Storage, 50);
int i;
for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
- UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
+ const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
int nReturn = parse_indeflen_nested(Nested, i);
if(nReturn) {
return nReturn;
@@ -2561,7 +2561,7 @@
// ----- Mempool is way too small -----
UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
- UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
+ const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
@@ -2651,7 +2651,7 @@
// Next parse, save pointers to a few strings, destroy original and see all is OK.
UsefulBuf_MAKE_STACK_UB(CopyOfStorage, 160);
- UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
+ const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index dd56791..97cee3d 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -609,7 +609,7 @@
// UUIDs
static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
- UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
+ const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
QCBOREncode_OpenMap(&ECtx);
QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
@@ -626,7 +626,7 @@
static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
+ const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
QCBOREncode_OpenMap(&ECtx);
@@ -1286,7 +1286,7 @@
// Add a few fake integers and buffers for now.
static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
- UsefulBufC WSPV = {pPV, sizeof(pPV)};
+ const UsefulBufC WSPV = {pPV, sizeof(pPV)};
QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
}
@@ -1354,7 +1354,7 @@
int RTICResultsTest()
{
- UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
+ const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
"recent", "0xA1eC5001",
UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
if(UsefulBuf_IsNULLC(Encoded)) {
@@ -1793,16 +1793,16 @@
{
// All of this is from RFC 8152 C.2.1
const char *szKid = "11";
- UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
+ const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
const char *szPayload = "This is the content.";
- UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
+ const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
- UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
+ const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
// It would be good to compare this to the output from
// a COSE implementation like COSE-C. It has been checked
// against the CBOR playground.
- UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSignature);
+ const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSignature);
QCBOREncodeContext EC;
QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));