Pull in updates from master
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index d5b2a9b..146253d 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2023, Laurence Lundblade.
+ Copyright (c) 2018-2024, Laurence Lundblade.
Copyright (c) 2021, Arm Limited.
All rights reserved.
@@ -72,7 +72,7 @@
* with the type CBOR_MAJOR_TYPE_BYTE_STRING and is tracked here. Byte
* string wrapped CBOR is used by COSE for data that is to be hashed.
*/
-static inline void
+static void
Nesting_Init(QCBORTrackNesting *pNesting)
{
/* Assumes pNesting has been zeroed. */
@@ -83,10 +83,10 @@
pNesting->pCurrentNesting->uMajorType = CBOR_MAJOR_TYPE_ARRAY;
}
-static inline uint8_t
+static uint8_t
Nesting_Increase(QCBORTrackNesting *pNesting,
- uint8_t uMajorType,
- uint32_t uPos)
+ const uint8_t uMajorType,
+ const uint32_t uPos)
{
if(pNesting->pCurrentNesting == &pNesting->pArrays[QCBOR_MAX_ARRAY_NESTING]) {
return QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
@@ -99,7 +99,7 @@
}
}
-static inline void
+static void
Nesting_Decrease(QCBORTrackNesting *pNesting)
{
if(pNesting->pCurrentNesting > &pNesting->pArrays[0]) {
@@ -107,7 +107,7 @@
}
}
-static inline uint8_t
+static uint8_t
Nesting_Increment(QCBORTrackNesting *pNesting)
{
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
@@ -121,7 +121,7 @@
return QCBOR_SUCCESS;
}
-static inline void
+static void
Nesting_Decrement(QCBORTrackNesting *pNesting)
{
/* No error check for going below 0 here needed because this
@@ -130,7 +130,7 @@
pNesting->pCurrentNesting->uCount--;
}
-static inline uint16_t
+static uint16_t
Nesting_GetCount(QCBORTrackNesting *pNesting)
{
/* The nesting count recorded is always the actual number of
@@ -148,20 +148,20 @@
}
}
-static inline uint32_t
+static uint32_t
Nesting_GetStartPos(QCBORTrackNesting *pNesting)
{
return pNesting->pCurrentNesting->uStart;
}
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
-static inline uint8_t
+static uint8_t
Nesting_GetMajorType(QCBORTrackNesting *pNesting)
{
return pNesting->pCurrentNesting->uMajorType;
}
-static inline bool
+static bool
Nesting_IsInNest(QCBORTrackNesting *pNesting)
{
return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? false : true;
@@ -251,21 +251,23 @@
/*
Public function for initialization. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_Init(QCBOREncodeContext *me, UsefulBuf Storage)
+void
+QCBOREncode_Init(QCBOREncodeContext *pMe, UsefulBuf Storage)
{
- memset(me, 0, sizeof(QCBOREncodeContext));
- UsefulOutBuf_Init(&(me->OutBuf), Storage);
- Nesting_Init(&(me->nesting));
+ memset(pMe, 0, sizeof(QCBOREncodeContext));
+ UsefulOutBuf_Init(&(pMe->OutBuf), Storage);
+ Nesting_Init(&(pMe->nesting));
}
/*
* Public function to encode a CBOR head. See qcbor/qcbor_encode.h
*/
-UsefulBufC QCBOREncode_EncodeHead(UsefulBuf buffer,
- uint8_t uMajorType,
- uint8_t uMinLen,
- uint64_t uArgument)
+UsefulBufC
+QCBOREncode_EncodeHead(UsefulBuf Buffer,
+ uint8_t uMajorType,
+ uint8_t uMinLen,
+ uint64_t uArgument)
{
/*
* == Description of the CBOR Head ==
@@ -412,12 +414,12 @@
* extra. The one extra is needed for this code to work as it does
* a pre-decrement.
*/
- if(buffer.len < QCBOR_HEAD_BUFFER_SIZE) {
+ if(Buffer.len < QCBOR_HEAD_BUFFER_SIZE) {
return NULLUsefulBufC;
}
/* Pointer to last valid byte in the buffer */
- uint8_t * const pBufferEnd = &((uint8_t *)buffer.ptr)[QCBOR_HEAD_BUFFER_SIZE-1];
+ uint8_t * const pBufferEnd = &((uint8_t *)Buffer.ptr)[QCBOR_HEAD_BUFFER_SIZE-1];
/* Point to the last byte and work backwards */
uint8_t *pByte = pBufferEnd;
@@ -500,14 +502,18 @@
/**
* @brief Append the CBOR head, the major type and argument
*
- * @param me Encoder context.
+ * @param pMe Encoder context.
* @param uMajorType Major type to insert.
* @param uArgument The argument (an integer value or a length).
* @param uMinLen The minimum number of bytes for encoding the CBOR argument.
*
* This formats the CBOR "head" and appends it to the output.
*/
-static void AppendCBORHead(QCBOREncodeContext *me, uint8_t uMajorType, uint64_t uArgument, uint8_t uMinLen)
+static void
+QCBOREncode_Private_AppendCBORHead(QCBOREncodeContext *pMe,
+ const uint8_t uMajorType,
+ const uint64_t uArgument,
+ const uint8_t uMinLen)
{
/* A stack buffer large enough for a CBOR head */
UsefulBuf_MAKE_STACK_UB (pBufferForEncodedHead, QCBOR_HEAD_BUFFER_SIZE);
@@ -524,7 +530,7 @@
* security hole introduced.
*/
- UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), EncodedHead);
+ UsefulOutBuf_AppendUsefulBuf(&(pMe->OutBuf), EncodedHead);
}
@@ -541,8 +547,9 @@
* This is called when closing maps, arrays, byte string wrapping and
* open/close of byte strings.
*/
-bool
-CheckDecreaseNesting(QCBOREncodeContext *pMe, uint8_t uMajorType)
+static bool
+QCBOREncode_Private_CheckDecreaseNesting(QCBOREncodeContext *pMe,
+ const uint8_t uMajorType)
{
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(pMe->uError != QCBOR_SUCCESS) {
@@ -578,7 +585,7 @@
/**
* @brief Insert the CBOR head for a map, array or wrapped bstr
*
- * @param me QCBOR encoding context.
+ * @param pMe QCBOR encoding context.
* @param uMajorType One of CBOR_MAJOR_TYPE_XXXX.
* @param uLen The length of the data item.
*
@@ -586,9 +593,12 @@
* the position. This function goes back to that position and inserts
* the CBOR Head with the major type and length.
*/
-static void InsertCBORHead(QCBOREncodeContext *me, uint8_t uMajorType, size_t uLen)
+static void
+QCBOREncode_Private_InsertCBORHead(QCBOREncodeContext *pMe,
+ uint8_t uMajorType,
+ size_t uLen)
{
- if(CheckDecreaseNesting(me, uMajorType)) {
+ if(QCBOREncode_Private_CheckDecreaseNesting(pMe, uMajorType)) {
return;
}
@@ -610,11 +620,11 @@
* UsefulOutBuf_InsertUsefulBuf() will do nothing so there is no
* security hole introduced.
*/
- UsefulOutBuf_InsertUsefulBuf(&(me->OutBuf),
+ UsefulOutBuf_InsertUsefulBuf(&(pMe->OutBuf),
EncodedHead,
- Nesting_GetStartPos(&(me->nesting)));
+ Nesting_GetStartPos(&(pMe->nesting)));
- Nesting_Decrease(&(me->nesting));
+ Nesting_Decrease(&(pMe->nesting));
}
@@ -626,7 +636,8 @@
* This is mostly a separate function to make code more readable and
* to have fewer occurrences of #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
*/
-static inline void IncrementMapOrArrayCount(QCBOREncodeContext *pMe)
+static void
+QCBOREncode_Private_IncrementMapOrArrayCount(QCBOREncodeContext *pMe)
{
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(pMe->uError == QCBOR_SUCCESS) {
@@ -641,18 +652,23 @@
/*
* Public functions for adding unsigned integers. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue)
+void
+QCBOREncode_AddUInt64(QCBOREncodeContext *pMe, const uint64_t uValue)
{
- AppendCBORHead(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue, 0);
+ QCBOREncode_Private_AppendCBORHead(pMe,
+ CBOR_MAJOR_TYPE_POSITIVE_INT,
+ uValue,
+ 0);
- IncrementMapOrArrayCount(me);
+ QCBOREncode_Private_IncrementMapOrArrayCount(pMe);
}
/*
* Public functions for adding signed integers. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum)
+void
+QCBOREncode_AddInt64(QCBOREncodeContext *pMe, const int64_t nNum)
{
uint8_t uMajorType;
uint64_t uValue;
@@ -670,17 +686,28 @@
uValue = (uint64_t)nNum;
uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT;
}
- AppendCBORHead(me, uMajorType, uValue, 0);
+ QCBOREncode_Private_AppendCBORHead(pMe, uMajorType, uValue, 0);
- IncrementMapOrArrayCount(me);
+ QCBOREncode_Private_IncrementMapOrArrayCount(pMe);
}
-/*
- * Semi-private function. It is exposed to user of the interface, but
- * one of its inline wrappers will usually be called instead of this.
+/**
+ * @brief Semi-private method to add a buffer full of bytes to encoded output.
*
- * See qcbor/qcbor_encode.h
+ * @param[in] pMe The encoding context to add the integer to.
+ * @param[in] uMajorType The CBOR major type of the bytes.
+ * @param[in] Bytes The bytes to add.
+ *
+ * Use QCBOREncode_AddText() or QCBOREncode_AddBytes() or
+ * QCBOREncode_AddEncoded() instead. They are inline functions that
+ * call this and supply the correct major type. This function is
+ * public to make the inline functions work to keep the overall code
+ * size down and because the C language has no way to make it private.
+ *
+ * If this is called the major type should be @c CBOR_MAJOR_TYPE_TEXT_STRING,
+ * @c CBOR_MAJOR_TYPE_BYTE_STRING or @c CBOR_MAJOR_NONE_TYPE_RAW. The
+ * last one is special for adding already-encoded CBOR.
*
* This does the work of adding actual strings bytes to the CBOR
* output (as opposed to adding numbers and opening / closing
@@ -697,7 +724,10 @@
* fourth just adds the head for the very special case of
* QCBOREncode_AddBytesLenOnly().
*/
-void QCBOREncode_AddBuffer(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC Bytes)
+void
+QCBOREncode_Private_AddBuffer(QCBOREncodeContext *pMe,
+ const uint8_t uMajorType,
+ const UsefulBufC Bytes)
{
/* If it is not Raw CBOR, add the type and the length */
if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) {
@@ -705,48 +735,64 @@
if(uRealMajorType == CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) {
uRealMajorType = CBOR_MAJOR_TYPE_BYTE_STRING;
}
- AppendCBORHead(me, uRealMajorType, Bytes.len, 0);
+ QCBOREncode_Private_AppendCBORHead(pMe, uRealMajorType, Bytes.len, 0);
}
if(uMajorType != CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) {
/* Actually add the bytes */
- UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes);
+ UsefulOutBuf_AppendUsefulBuf(&(pMe->OutBuf), Bytes);
}
- IncrementMapOrArrayCount(me);
+ QCBOREncode_Private_IncrementMapOrArrayCount(pMe);
}
/*
* Public functions for adding a tag. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag)
+void
+QCBOREncode_AddTag(QCBOREncodeContext *pMe, const uint64_t uTag)
{
- AppendCBORHead(me, CBOR_MAJOR_TYPE_TAG, uTag, 0);
+ QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_TAG, uTag, 0);
}
-/*
- * Semi-private function. It is exposed to user of the interface, but
- * one of its inline wrappers will usually be called instead of this.
+/**
+ * @brief Semi-private method to add simple types.
*
- * See header qcbor/qcbor_encode.h
+ * @param[in] pMe The encoding context to add the simple value to.
+ * @param[in] uMinLen Minimum encoding size for uNum. Usually 0.
+ * @param[in] uNum One of CBOR_SIMPLEV_FALSE through _UNDEF or other.
+ *
+ * This is used to add simple types like true and false.
+ *
+ * Call QCBOREncode_AddBool(), QCBOREncode_AddNULL(),
+ * QCBOREncode_AddUndef() instead of this.
+ *
+ * This function can add simple values that are not defined by CBOR
+ * yet. This expansion point in CBOR should not be used unless they are
+ * standardized.
+ *
+ * Error handling is the same as QCBOREncode_AddInt64().
*/
-void QCBOREncode_AddType7(QCBOREncodeContext *me, uint8_t uMinLen, uint64_t uNum)
+void
+QCBOREncode_Private_AddType7(QCBOREncodeContext *pMe,
+ const uint8_t uMinLen,
+ const uint64_t uNum)
{
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
- if(me->uError == QCBOR_SUCCESS) {
+ if(pMe->uError == QCBOR_SUCCESS) {
if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) {
- me->uError = QCBOR_ERR_ENCODE_UNSUPPORTED;
+ pMe->uError = QCBOR_ERR_ENCODE_UNSUPPORTED;
return;
}
}
#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
/* AppendCBORHead() does endian swapping for the float / double */
- AppendCBORHead(me, CBOR_MAJOR_TYPE_SIMPLE, uNum, uMinLen);
+ QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_SIMPLE, uNum, uMinLen);
- IncrementMapOrArrayCount(me);
+ QCBOREncode_Private_IncrementMapOrArrayCount(pMe);
}
@@ -754,25 +800,27 @@
/*
* Public functions for adding a double. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *me, double dNum)
+void
+QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pMe, const double dNum)
{
- QCBOREncode_AddType7(me,
- sizeof(uint64_t),
- UsefulBufUtil_CopyDoubleToUint64(dNum));
+ QCBOREncode_Private_AddType7(pMe,
+ sizeof(uint64_t),
+ UsefulBufUtil_CopyDoubleToUint64(dNum));
}
/*
* Public functions for adding a double. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_AddDouble(QCBOREncodeContext *me, double dNum)
+void
+QCBOREncode_AddDouble(QCBOREncodeContext *pMe, const double dNum)
{
#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
const IEEE754_union uNum = IEEE754_DoubleToSmaller(dNum, true);
- QCBOREncode_AddType7(me, (uint8_t)uNum.uSize, uNum.uValue);
+ QCBOREncode_Private_AddType7(pMe, (uint8_t)uNum.uSize, uNum.uValue);
#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
- QCBOREncode_AddDoubleNoPreferred(me, dNum);
+ QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
}
@@ -806,48 +854,70 @@
/*
* Public functions for adding a float. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *me, float fNum)
+void
+QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pMe, const float fNum)
{
- QCBOREncode_AddType7(me,
- sizeof(uint32_t),
- UsefulBufUtil_CopyFloatToUint32(fNum));
+ QCBOREncode_Private_AddType7(pMe,
+ sizeof(uint32_t),
+ UsefulBufUtil_CopyFloatToUint32(fNum));
}
/*
* Public functions for adding a float. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_AddFloat(QCBOREncodeContext *me, float fNum)
+void
+QCBOREncode_AddFloat(QCBOREncodeContext *pMe, const float fNum)
{
#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
const IEEE754_union uNum = IEEE754_SingleToHalf(fNum);
- QCBOREncode_AddType7(me, (uint8_t)uNum.uSize, uNum.uValue);
+ QCBOREncode_Private_AddType7(pMe, (uint8_t)uNum.uSize, uNum.uValue);
#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
- QCBOREncode_AddFloatNoPreferred(me, fNum);
+ QCBOREncode_AddFloatNoPreferred(pMe, fNum);
#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
}
#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
-/*
- * Semi-public function. It is exposed to the user of the interface,
- * but one of the inline wrappers will usually be called rather than
- * this.
+/**
+ * @brief Semi-private method to add bigfloats and decimal fractions.
*
- * See qcbor/qcbor_encode.h
+ * @param[in] pMe The encoding context to add the value to.
+ * @param[in] uTag The type 6 tag indicating what this is to be.
+ * @param[in] BigNumMantissa Is @ref NULLUsefulBufC if mantissa is an
+ * @c int64_t or the actual big number mantissa
+ * if not.
+ * @param[in] bBigNumIsNegative This is @c true if the big number is negative.
+ * @param[in] nMantissa The @c int64_t mantissa if it is not a big number.
+ * @param[in] nExponent The exponent.
*
- * Improvement: create another version of this that only takes a big
- * number mantissa and converts the output to a type 0 or 1 integer
- * when mantissa is small enough.
+ * This outputs either the @ref CBOR_TAG_DECIMAL_FRACTION or
+ * @ref CBOR_TAG_BIGFLOAT tag. if @c uTag is @ref CBOR_TAG_INVALID64,
+ * then this outputs the "borrowed" content format.
+ *
+ * The tag content output by this is an array with two members, the
+ * exponent and then the mantissa. The mantissa can be either a big
+ * number or an @c int64_t.
+ *
+ * This implementation cannot output an exponent further from 0 than
+ * @c INT64_MAX.
+ *
+ * To output a mantissa that is between INT64_MAX and UINT64_MAX from 0,
+ * it must be as a big number.
+ *
+ * Typically, QCBOREncode_AddDecimalFraction(), QCBOREncode_AddBigFloat(),
+ * QCBOREncode_AddDecimalFractionBigNum() or QCBOREncode_AddBigFloatBigNum()
+ * is called instead of this.
*/
-void QCBOREncode_AddExponentAndMantissa(QCBOREncodeContext *pMe,
- uint64_t uTag,
- UsefulBufC BigNumMantissa,
- bool bBigNumIsNegative,
- int64_t nMantissa,
- int64_t nExponent)
+void
+QCBOREncode_Private_AddExpMantissa(QCBOREncodeContext *pMe,
+ const uint64_t uTag,
+ const UsefulBufC BigNumMantissa,
+ const bool bBigNumIsNegative,
+ const int64_t nMantissa,
+ const int64_t nExponent)
{
/* This is for encoding either a big float or a decimal fraction,
* both of which are an array of two items, an exponent and a
@@ -874,17 +944,21 @@
#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
-/*
- * Semi-public function. It is exposed to the user of the interface,
- * but one of the inline wrappers will usually be called rather than
- * this.
+/**
+ * @brief Semi-private method to open a map, array or bstr-wrapped CBOR
*
- * See qcbor/qcbor_encode.h
+ * @param[in] pMe The context to add to.
+ * @param[in] uMajorType The major CBOR type to close
+ *
+ * Call QCBOREncode_OpenArray(), QCBOREncode_OpenMap() or
+ * QCBOREncode_BstrWrap() instead of this.
*/
-void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType)
+void
+QCBOREncode_Private_OpenMapOrArray(QCBOREncodeContext *pMe,
+ const uint8_t uMajorType)
{
/* Add one item to the nesting level we are in for the new map or array */
- IncrementMapOrArrayCount(me);
+ QCBOREncode_Private_IncrementMapOrArrayCount(pMe);
/* The offset where the length of an array or map will get written
* is stored in a uint32_t, not a size_t to keep stack usage
@@ -894,7 +968,7 @@
* past the 4GB mark, but the public interface says that the
* maximum is 4GB to keep the discussion simpler.
*/
- size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
+ size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(pMe->OutBuf));
/* QCBOR_MAX_ARRAY_OFFSET is slightly less than UINT32_MAX so this
* code can run on a 32-bit machine and tests can pass on a 32-bit
@@ -903,43 +977,54 @@
* size detection would be needed reducing portability.
*/
if(uEndPosition >= QCBOR_MAX_ARRAY_OFFSET) {
- me->uError = QCBOR_ERR_BUFFER_TOO_LARGE;
+ pMe->uError = QCBOR_ERR_BUFFER_TOO_LARGE;
} else {
/* Increase nesting level because this is a map or array. Cast
* from size_t to uin32_t is safe because of check above.
*/
- me->uError = Nesting_Increase(&(me->nesting), uMajorType, (uint32_t)uEndPosition);
+ pMe->uError = Nesting_Increase(&(pMe->nesting), uMajorType, (uint32_t)uEndPosition);
}
}
-/*
- * Semi-public function. It is exposed to the user of the interface,
- * but one of the inline wrappers will usually be called rather than
- * this.
+/**
+ * @brief Semi-private method to open a map, array with indefinite length
*
- * See qcbor/qcbor_encode.h
+ * @param[in] pMe The context to add to.
+ * @param[in] uMajorType The major CBOR type to close
+ *
+ * Call QCBOREncode_OpenArrayIndefiniteLength() or
+ * QCBOREncode_OpenMapIndefiniteLength() instead of this.
*/
-void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType)
+void
+QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pMe,
+ const uint8_t uMajorType)
{
/* Insert the indefinite length marker (0x9f for arrays, 0xbf for maps) */
- AppendCBORHead(me, uMajorType, 0, 0);
+ QCBOREncode_Private_AppendCBORHead(pMe, uMajorType, 0, 0);
/* Call the definite-length opener just to do the bookkeeping for
* nesting. It will record the position of the opening item in the
* encoded output but this is not used when closing this open.
*/
- QCBOREncode_OpenMapOrArray(me, uMajorType);
+ QCBOREncode_Private_OpenMapOrArray(pMe, uMajorType);
}
-/*
- * Public functions for closing arrays and maps. See qcbor/qcbor_encode.h
+/**
+ * @brief Semi-private method to close a map, array or bstr wrapped CBOR
+ *
+ * @param[in] pMe The context to add to.
+ * @param[in] uMajorType The major CBOR type to close.
+ *
+ * Call QCBOREncode_CloseArray() or QCBOREncode_CloseMap() instead of this.
*/
-void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType)
+void
+QCBOREncode_Private_CloseMapOrArray(QCBOREncodeContext *pMe,
+ const uint8_t uMajorType)
{
- InsertCBORHead(me, uMajorType, Nesting_GetCount(&(me->nesting)));
+ QCBOREncode_Private_InsertCBORHead(pMe, uMajorType, Nesting_GetCount(&(pMe->nesting)));
}
@@ -1032,7 +1117,7 @@
* Recursive, but stack usage is light and encoding depth limit
*/
static QCBORError
-QCBOREncodePriv_ConsumeNext(UsefulInputBuf *pInBuf)
+QCBOR_Private_ConsumeNext(UsefulInputBuf *pInBuf)
{
int nMajor;
uint64_t uArgument;
@@ -1062,13 +1147,13 @@
case CBOR_MAJOR_TYPE_TEXT_STRING:
if(nAdditional == LEN_IS_INDEFINITE) {
/* Segments of indefinite length */
- while(QCBOREncodePriv_ConsumeNext(pInBuf) == 0);
+ while(QCBOR_Private_ConsumeNext(pInBuf) == 0);
}
(void)UsefulInputBuf_GetBytes(pInBuf, uArgument);
break;
case CBOR_MAJOR_TYPE_TAG:
- QCBOREncodePriv_ConsumeNext(pInBuf);
+ QCBOR_Private_ConsumeNext(pInBuf);
break;
case CBOR_MAJOR_TYPE_MAP:
@@ -1080,7 +1165,7 @@
uItemCount = UINT16_MAX;
}
for(i = uItemCount; i > 0; i--) {
- if(QCBOREncodePriv_ConsumeNext(pInBuf)) {
+ if(QCBOR_Private_ConsumeNext(pInBuf)) {
/* End of indefinite length */
break;
}
@@ -1106,7 +1191,7 @@
* in case of bugs here, but it is OK to report end of input on error.
*/
static uint32_t
-QCBOREncodePriv_DecodeNextInMap(QCBOREncodeContext *pMe, uint32_t uStart)
+QCBOREncode_Private_DecodeNextInMap(QCBOREncodeContext *pMe, uint32_t uStart)
{
UsefulInputBuf InBuf;
UsefulBufC EncodedMapBytes;
@@ -1120,11 +1205,11 @@
UsefulInputBuf_Init(&InBuf, EncodedMapBytes);
/* This is always used on maps, so consume two, the label and the value */
- uCBORError = QCBOREncodePriv_ConsumeNext(&InBuf);
+ uCBORError = QCBOR_Private_ConsumeNext(&InBuf);
if(uCBORError) {
return 0;
}
- uCBORError = QCBOREncodePriv_ConsumeNext(&InBuf);
+ uCBORError = QCBOR_Private_ConsumeNext(&InBuf);
if(uCBORError) {
return 0;
}
@@ -1146,7 +1231,7 @@
* UsefulOutBuf.
*/
static void
-QCBOREncodePriv_SortMap(QCBOREncodeContext *pMe, uint32_t uStart)
+QCBOREncode_Private_SortMap(QCBOREncodeContext *pMe, uint32_t uStart)
{
bool bSwapped;
int nComparison;
@@ -1174,7 +1259,7 @@
* swapped.
*/
do {
- uLen1 = QCBOREncodePriv_DecodeNextInMap(pMe, uStart);
+ uLen1 = QCBOREncode_Private_DecodeNextInMap(pMe, uStart);
if(uLen1 == 0) {
/* It's an empty map. Nothing to do. */
break;
@@ -1184,7 +1269,7 @@
bSwapped = false;
while(1) {
- uLen2 = QCBOREncodePriv_DecodeNextInMap(pMe, uStart2);
+ uLen2 = QCBOREncode_Private_DecodeNextInMap(pMe, uStart2);
if(uLen2 == 0) {
break;
}
@@ -1216,9 +1301,9 @@
* items we are about to sort.
*/
uStart = Nesting_GetStartPos(&(pMe->nesting));
- QCBOREncodePriv_SortMap(pMe, uStart);
+ QCBOREncode_Private_SortMap(pMe, uStart);
- QCBOREncode_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP);
+ QCBOREncode_Private_InsertCBORHead(pMe, CBOR_MAJOR_TYPE_MAP, Nesting_GetCount(&(pMe->nesting)));
}
@@ -1230,19 +1315,22 @@
uint32_t uStart;
uStart = Nesting_GetStartPos(&(pMe->nesting));
- QCBOREncodePriv_SortMap(pMe, uStart);
+ QCBOREncode_Private_SortMap(pMe, uStart);
- QCBOREncode_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
+ QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
}
/*
* Public functions for closing bstr wrapping. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *me, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR)
+void
+QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *pMe,
+ const bool bIncludeCBORHead,
+ UsefulBufC *pWrappedCBOR)
{
- const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting));
- const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
+ const size_t uInsertPosition = Nesting_GetStartPos(&(pMe->nesting));
+ const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(pMe->OutBuf));
/* This subtraction can't go negative because the UsefulOutBuf
* always only grows and never shrinks. UsefulOutBut itself also
@@ -1252,7 +1340,7 @@
const size_t uBstrLen = uEndPosition - uInsertPosition;
/* Actually insert */
- InsertCBORHead(me, CBOR_MAJOR_TYPE_BYTE_STRING, uBstrLen);
+ QCBOREncode_Private_InsertCBORHead(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, uBstrLen);
if(pWrappedCBOR) {
/* Return pointer and length to the enclosed encoded CBOR. The
@@ -1265,10 +1353,10 @@
size_t uStartOfNew = uInsertPosition;
if(!bIncludeCBORHead) {
/* Skip over the CBOR head to just get the inserted bstr */
- const size_t uNewEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
+ const size_t uNewEndPosition = UsefulOutBuf_GetEndPosition(&(pMe->OutBuf));
uStartOfNew += uNewEndPosition - uEndPosition;
}
- const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
+ const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(pMe->OutBuf));
*pWrappedCBOR = UsefulBuf_Tail(PartialResult, uStartOfNew);
}
}
@@ -1277,9 +1365,10 @@
/*
* Public function for canceling a bstr wrap. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pMe)
+void
+QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pMe)
{
- if(CheckDecreaseNesting(pMe, CBOR_MAJOR_TYPE_BYTE_STRING)) {
+ if(QCBOREncode_Private_CheckDecreaseNesting(pMe, CBOR_MAJOR_TYPE_BYTE_STRING)) {
return;
}
@@ -1307,7 +1396,8 @@
/*
* Public function for opening a byte string. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_OpenBytes(QCBOREncodeContext *pMe, UsefulBuf *pPlace)
+void
+QCBOREncode_OpenBytes(QCBOREncodeContext *pMe, UsefulBuf *pPlace)
{
*pPlace = UsefulOutBuf_GetOutPlace(&(pMe->OutBuf));
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
@@ -1319,14 +1409,15 @@
}
#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
- QCBOREncode_OpenMapOrArray(pMe, CBOR_MAJOR_NONE_TYPE_OPEN_BSTR);
+ QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_NONE_TYPE_OPEN_BSTR);
}
/*
* Public function for closing a byte string. See qcbor/qcbor_encode.h
*/
-void QCBOREncode_CloseBytes(QCBOREncodeContext *pMe, const size_t uAmount)
+void
+QCBOREncode_CloseBytes(QCBOREncodeContext *pMe, const size_t uAmount)
{
UsefulOutBuf_Advance(&(pMe->OutBuf), uAmount);
if(UsefulOutBuf_GetError(&(pMe->OutBuf))) {
@@ -1334,21 +1425,29 @@
return;
}
- InsertCBORHead(pMe, CBOR_MAJOR_NONE_TYPE_OPEN_BSTR, uAmount);
+ QCBOREncode_Private_InsertCBORHead(pMe, CBOR_MAJOR_NONE_TYPE_OPEN_BSTR, uAmount);
}
-/*
- * Public function for closing arrays and maps. See qcbor/qcbor_encode.h
+/**
+ * @brief Semi-private method to close a map, array with indefinite length
+ *
+ * @param[in] pMe The context to add to.
+ * @param[in] uMajorType The major CBOR type to close.
+ *
+ * Call QCBOREncode_CloseArrayIndefiniteLength() or
+ * QCBOREncode_CloseMapIndefiniteLength() instead of this.
*/
-void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pMe, uint8_t uMajorType)
+void
+QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pMe,
+ const uint8_t uMajorType)
{
- if(CheckDecreaseNesting(pMe, uMajorType)) {
+ if(QCBOREncode_Private_CheckDecreaseNesting(pMe, uMajorType)) {
return;
}
/* Append the break marker (0xff for both arrays and maps) */
- AppendCBORHead(pMe, CBOR_MAJOR_NONE_TYPE_SIMPLE_BREAK, CBOR_SIMPLE_BREAK, 0);
+ QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_NONE_TYPE_SIMPLE_BREAK, CBOR_SIMPLE_BREAK, 0);
Nesting_Decrease(&(pMe->nesting));
}
@@ -1356,22 +1455,23 @@
/*
* Public function to finish and get the encoded result. See qcbor/qcbor_encode.h
*/
-QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR)
+QCBORError
+QCBOREncode_Finish(QCBOREncodeContext *pMe, UsefulBufC *pEncodedCBOR)
{
- QCBORError uReturn = QCBOREncode_GetErrorState(me);
+ QCBORError uReturn = QCBOREncode_GetErrorState(pMe);
if(uReturn != QCBOR_SUCCESS) {
goto Done;
}
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
- if(Nesting_IsInNest(&(me->nesting))) {
+ if(Nesting_IsInNest(&(pMe->nesting))) {
uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
goto Done;
}
#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
- *pEncodedCBOR = UsefulOutBuf_OutUBuf(&(me->OutBuf));
+ *pEncodedCBOR = UsefulOutBuf_OutUBuf(&(pMe->OutBuf));
Done:
return uReturn;
@@ -1381,11 +1481,12 @@
/*
* Public functions to get size of the encoded result. See qcbor/qcbor_encode.h
*/
-QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *me, size_t *puEncodedLen)
+QCBORError
+QCBOREncode_FinishGetSize(QCBOREncodeContext *pMe, size_t *puEncodedLen)
{
UsefulBufC Enc;
- QCBORError nReturn = QCBOREncode_Finish(me, &Enc);
+ QCBORError nReturn = QCBOREncode_Finish(pMe, &Enc);
if(nReturn == QCBOR_SUCCESS) {
*puEncodedLen = Enc.len;