Merge recent changes from master
diff --git a/inc/qcbor/qcbor_encode.h b/inc/qcbor/qcbor_encode.h
index 008ee91..cf32f3c 100644
--- a/inc/qcbor/qcbor_encode.h
+++ b/inc/qcbor/qcbor_encode.h
@@ -673,7 +673,7 @@
*
* Error handling is the same as for QCBOREncode_AddInt64().
*/
-void
+static void
QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
static void
@@ -839,7 +839,7 @@
* By default, this will error out on an attempt to encode a NaN with
* a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD.
*/
-void
+static void
QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum);
static void
@@ -861,7 +861,7 @@
* See also QCBOREncode_AddDouble(), QCBOREncode_AddDoubleNoPreferred(),
* and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
*/
-void
+static void
QCBOREncode_AddFloat(QCBOREncodeContext *pCtx, float fNum);
static void
@@ -877,18 +877,15 @@
* @param[in] pCtx The encoding context to add the double to.
* @param[in] dNum The double-precision number to add.
*
- * This always outputs the number as a 64-bit double-precision.
- * Preferred serialization is not used.
+ * Output a double-precision float straight-through with no checking or
+ * processing for preferred serializtion, dCBOR or other.
*
* Error handling is the same as QCBOREncode_AddInt64().
*
- * By default, this will error out on an attempt to encode a NaN with
- * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD.
- *
* See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
* QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
*/
-void
+static void
QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pCtx, double dNum);
static void
@@ -904,18 +901,15 @@
* @param[in] pCtx The encoding context to add the double to.
* @param[in] fNum The single-precision number to add.
*
- * This always outputs the number as a 32-bit single-precision.
- * Preferred serialization is not used.
+ * Output a single-precision float straight-through with no checking or
+ * processing for preferred serializtion, dCBOR or other.
*
* Error handling is the same as QCBOREncode_AddInt64().
*
- * By default, this will error out on an attempt to encode a NaN with
- * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD.
- *
* See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
* QCBOREncode_AddDoubleNoPreferred() and @ref Floating-Point.
*/
-void
+static void
QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pCtx, float fNum);
static void
@@ -949,7 +943,7 @@
* tags. See QCBORDecode_GetNext() for discussion of decoding custom
* tags.
*/
-void
+static void
QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag);
@@ -1944,7 +1938,6 @@
QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b);
-
/**
* @brief Add a NULL to the encoded output.
*
@@ -1992,6 +1985,33 @@
/**
+ * @brief Add a simple value.
+ *
+ * @param[in] pMe The encode context.
+ * @param[in] uNum The simple value.
+ *
+ * Use QCBOREncode_AddBool(), QCBOREncode_AddUndef()... instead of this.
+ *
+ * Use this to add simple values beyond those in defined RFC
+ * 8949. Simple values must be registered with IANA. There is no range
+ * of values for proprietary use.
+ * https://www.iana.org/assignments/cbor-simple-values/cbor-simple-values.xhtml
+ */
+static void
+QCBOREncode_AddSimple(QCBOREncodeContext *pMe, const uint64_t uNum);
+
+static void
+QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uSimple);
+
+static void
+QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uSimple);
+
+
+/**
* @brief Indicates that the next items added are in an array.
*
* @param[in] pCtx The encoding context to open the array in.
@@ -2343,7 +2363,7 @@
* must be enclosed in a map or array. At the top level the raw
* CBOR must be a single data item.
*/
-static void
+void
QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded);
static void
@@ -2523,12 +2543,29 @@
========================================================================= */
/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
+void QCBOREncode_Private_AppendCBORHead(QCBOREncodeContext *pMe,
+ const uint8_t uMajorType,
+ const uint64_t uArgument,
+ const uint8_t uMinLen);
+
+
+/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
void
QCBOREncode_Private_AddBuffer(QCBOREncodeContext *pCtx,
uint8_t uMajorType,
UsefulBufC Bytes);
+/* Semi-private function for adding a double with preferred encoding. See qcbor_encode.c */
+void
+QCBOREncode_Private_AddPreferredDouble(QCBOREncodeContext *pMe, const double dNum);
+
+
+/* Semi-private function for adding a float with preferred encoding. See qcbor_encode.c */
+void
+QCBOREncode_Private_AddPreferredFloat(QCBOREncodeContext *pMe, const float fNum);
+
+
/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
void
QCBOREncode_Private_OpenMapOrArray(QCBOREncodeContext *pCtx,
@@ -2555,13 +2592,6 @@
/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
void
-QCBOREncode_Private_AddType7(QCBOREncodeContext *pCtx,
- uint8_t uMinLen,
- uint64_t uNum);
-
-
-/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
-void
QCBOREncode_Private_AddExpMantissa(QCBOREncodeContext *pCtx,
uint64_t uTag,
UsefulBufC BigNumMantissa,
@@ -2571,6 +2601,73 @@
+/**
+ * @brief Semi-private method to add simple items and floating-point.
+ *
+ * @param[in] pMe The encoding context.
+ * @param[in] uMinLen Minimum encoding size for uNum. Usually 0.
+ * @param[in] uArgument The value to add.
+ *
+ * This is used to add simple types like true and false and float-point
+ * values, both of which are type 7.
+ *
+ * Call QCBOREncode_AddBool(), QCBOREncode_AddNULL(),
+ * QCBOREncode_AddUndef() QCBOREncode_AddDouble() instead of this.
+ *
+ * Error handling is the same as QCBOREncode_AddInt64().
+ */
+static inline void
+QCBOREncode_Private_AddType7(QCBOREncodeContext *pMe,
+ const uint8_t uMinLen,
+ const uint64_t uArgument)
+{
+ QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_SIMPLE, uArgument, uMinLen);
+}
+
+
+/**
+ * @brief Semi-private method to add only the type and length of a byte string.
+ *
+ * @param[in] pCtx The context to initialize.
+ * @param[in] Bytes Pointer and length of the input data.
+ *
+ * This will be removed in QCBOR 2.0. It was never a public function.
+ *
+ * This is the same as QCBOREncode_AddBytes() except it only adds the
+ * CBOR encoding for the type and the length. It doesn't actually add
+ * the bytes. You can't actually produce correct CBOR with this and
+ * the rest of this API. It is only used for a special case where the
+ * valid CBOR is created manually by putting this type and length in
+ * and then adding the actual bytes. In particular, when only a hash
+ * of the encoded CBOR is needed, where the type and header are hashed
+ * separately and then the bytes is hashed. This makes it possible to
+ * implement COSE Sign1 with only one copy of the payload in the
+ * output buffer, rather than two, roughly cutting memory use in half.
+ *
+ * This is only used for this odd case, but this is a supported
+ * tested function for QCBOR 1.0.
+ *
+ * See also QCBOREncode_EncodeHead().
+ */
+static void
+QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pCtx,
+ UsefulBufC Bytes);
+
+static void
+QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ UsefulBufC Bytes);
+
+static void
+QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ UsefulBufC Bytes);
+
+
+/* Forward declaration */
+static void
+QCBOREncode_AddSZString(QCBOREncodeContext *pMe, const char *szString);
+
static inline void
QCBOREncode_SerializationCDE(QCBOREncodeContext *pMe)
@@ -2616,10 +2713,7 @@
const char *szLabel,
const int64_t uNum)
{
- /* Use _AddBuffer() because _AddSZString() is defined below, not above */
- QCBOREncode_Private_AddBuffer(pMe,
- CBOR_MAJOR_TYPE_TEXT_STRING,
- UsefulBuf_FromSZ(szLabel));
+ QCBOREncode_AddSZString(pMe, szLabel);
QCBOREncode_AddInt64(pMe, uNum);
}
@@ -2634,14 +2728,18 @@
static inline void
+QCBOREncode_AddUInt64(QCBOREncodeContext *pMe, const uint64_t uValue)
+{
+ QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue, 0);
+}
+
+
+static inline void
QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pMe,
const char *szLabel,
const uint64_t uNum)
{
- /* Use _AddBuffer() because _AddSZString() is defined below, not above */
- QCBOREncode_Private_AddBuffer(pMe,
- CBOR_MAJOR_TYPE_TEXT_STRING,
- UsefulBuf_FromSZ(szLabel));
+ QCBOREncode_AddSZString(pMe, szLabel);
QCBOREncode_AddUInt64(pMe, uNum);
}
@@ -2723,7 +2821,47 @@
}
+
+/*
+ * Public functions for adding a tag. See qcbor/qcbor_encode.h
+ */
+static inline void
+QCBOREncode_AddTag(QCBOREncodeContext *pMe, const uint64_t uTag)
+{
+ QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_TAG, uTag, 0);
+}
+
+
+
#ifndef USEFULBUF_DISABLE_ALL_FLOAT
+
+static inline void
+QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pMe, const double dNum)
+{
+ QCBOREncode_Private_AddType7(pMe,
+ sizeof(uint64_t),
+ UsefulBufUtil_CopyDoubleToUint64(dNum));
+}
+
+static inline void
+QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pMe, const float fNum)
+{
+ QCBOREncode_Private_AddType7(pMe,
+ sizeof(uint32_t),
+ UsefulBufUtil_CopyFloatToUint32(fNum));
+}
+
+
+static inline void
+QCBOREncode_AddDouble(QCBOREncodeContext *pMe, const double dNum)
+{
+#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
+ QCBOREncode_Private_AddPreferredDouble(pMe, dNum);
+#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
+ QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
+#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
+}
+
static inline void
QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pMe,
const char *szLabel,
@@ -2742,6 +2880,17 @@
QCBOREncode_AddDouble(pMe, dNum);
}
+
+static inline void
+QCBOREncode_AddFloat(QCBOREncodeContext *pMe, const float fNum)
+{
+#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
+ QCBOREncode_Private_AddPreferredFloat(pMe, fNum);
+#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
+ QCBOREncode_AddFloatNoPreferred(pMe, fNum);
+#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
+}
+
static inline void
QCBOREncode_AddFloatToMap(QCBOREncodeContext *pMe,
const char *szLabel,
@@ -2799,6 +2948,8 @@
+
+
static inline void
QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pMe,
const uint8_t uTag,
@@ -2932,6 +3083,35 @@
}
+/*
+ * Public functions for adding only a byte string length. See qcbor/qcbor_encode.h
+ */
+static inline void
+QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
+{
+ QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes.len, 0);
+}
+
+
+static inline void
+QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const UsefulBufC Bytes)
+{
+ QCBOREncode_AddSZString(pMe, szLabel);
+ QCBOREncode_AddBytesLenOnly(pMe, Bytes);
+}
+
+static inline void
+QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const UsefulBufC Bytes)
+{
+ QCBOREncode_AddInt64(pMe, nLabel);
+ QCBOREncode_AddBytesLenOnly(pMe, Bytes);
+}
+
+
static inline void
QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pMe,
const uint8_t uTagRequirement,
@@ -3841,9 +4021,8 @@
}
-
static inline void
-QCBOREncode_Private_AddSimple(QCBOREncodeContext *pMe, const uint64_t uNum)
+QCBOREncode_AddSimple(QCBOREncodeContext *pMe, const uint64_t uNum)
{
#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
if(pMe->uMode >= QCBOR_ENCODE_MODE_DCBOR) {
@@ -3853,27 +4032,32 @@
return;
}
}
-#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+ /* This check often is optimized out because uNum is known at compile time. */
+ if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) {
+ pMe->uError = QCBOR_ERR_ENCODE_UNSUPPORTED;
+ return;
+ }
+#endif /* !QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
QCBOREncode_Private_AddType7(pMe, 0, uNum);
}
static inline void
-QCBOREncode_Private_AddSimpleToMap(QCBOREncodeContext *pMe,
+QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe,
const char *szLabel,
const uint8_t uSimple)
{
QCBOREncode_AddSZString(pMe, szLabel);
- QCBOREncode_Private_AddSimple(pMe, uSimple);
+ QCBOREncode_AddSimple(pMe, uSimple);
}
static inline void
-QCBOREncode_Private_AddSimpleToMapN(QCBOREncodeContext *pMe,
+QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const uint8_t uSimple)
{
QCBOREncode_AddInt64(pMe, nLabel);
- QCBOREncode_Private_AddSimple(pMe, uSimple);
+ QCBOREncode_AddSimple(pMe, uSimple);
}
@@ -3884,7 +4068,7 @@
if(b) {
uSimple = CBOR_SIMPLEV_TRUE;
}
- QCBOREncode_Private_AddSimple(pMe, uSimple);
+ QCBOREncode_AddSimple(pMe, uSimple);
}
static inline void
@@ -3905,7 +4089,7 @@
static inline void
QCBOREncode_AddNULL(QCBOREncodeContext *pMe)
{
- QCBOREncode_Private_AddSimple(pMe, CBOR_SIMPLEV_NULL);
+ QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_NULL);
}
static inline void
@@ -3926,7 +4110,7 @@
static inline void
QCBOREncode_AddUndef(QCBOREncodeContext *pMe)
{
- QCBOREncode_Private_AddSimple(pMe, CBOR_SIMPLEV_UNDEF);
+ QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_UNDEF);
}
static inline void
@@ -4083,11 +4267,6 @@
}
-static inline void
-QCBOREncode_AddEncoded(QCBOREncodeContext *pMe, const UsefulBufC Encoded)
-{
- QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_RAW, Encoded);
-}
static inline void
QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pMe,