merge new tag encoding design
diff --git a/README.md b/README.md
index fca5ba9..0b4e71b 100644
--- a/README.md
+++ b/README.md
@@ -70,6 +70,9 @@
* Finish cannot be called repeatedly on a partial decode (some tests used this, but it is not really a good thing to use in the first place)
* UsefulOutBuf_OutUBuf changed to work differently
* UsefulOutBuf_Init works differently
+* The "_3" functions are replaced with "_2" functions (the macros that referenced _3, now reference _2 and work the same)
+* There is a new AddTag functon instead of the "_3" functions, making the interface simpler and saving some code
+* QCBOREncode_AddRawSimple_2 is removed (the macros that referenced still exist and work the same)
diff --git a/inc/qcbor.h b/inc/qcbor.h
index 23e38fb..fd45f1a 100644
--- a/inc/qcbor.h
+++ b/inc/qcbor.h
@@ -197,6 +197,10 @@
const void *pCallerConfiguredTagList;
};
+// Used internally in the impementation here
+// Must not conflict with any of the official CBOR types
+#define CBOR_MAJOR_NONE_TYPE_RAW 9
+#define CBOR_MAJOR_NONE_TAG_LABEL_REORDER 10
/* ===========================================================================
@@ -466,11 +470,12 @@
buffers need only to be valid during the "Add" calls. The
data is copied into the output buf during the "Add" call.
- There are several "Add" functions / macros for each type. The one
- with named ending in "_3", for example QCBOREncode_AddInt64_3(),
- takes parameters for labels and tags and is the most powerful.
+ There are several "Add" functions / macros for each type. The
+ main one is named ending in "_2", for example
+ QCBOREncode_AddInt64_2().
Generally it is better to use the macros that only take the
- parameters necessary. For example, QCBOREncode_AddInt64(),
+ parameters necessary what you are adding. For example,
+ QCBOREncode_AddInt64(),
only takes the integer value to add with no labels and tags.
The simplest aggregate type is an array, which is a simple ordered
@@ -488,19 +493,14 @@
Note that when you nest arrays or maps in a map, the nested
array or map has a label.
-
- TODO: describe decoding
-
-
- As mentioned callers of this API will generally not need tags
- and thus not need the "_3" functions, but they are available
- if need be. There is an IANA registry for new tags that are
+
+ Usually it is not necessary to add tags explcitly as most
+ tagged types have functions here, but they can be added by
+ calling QCBOREncode_AddTag(). There is an IANA registry for new tags that are
for broad use and standardization as per RFC 7049. It is also
allowed for protocols to make up new tags in the range above 256.
Note that even arrays and maps can be tagged.
- TODO: describe tagging
-
Summary Limits of this implementation:
- The entire encoded CBOR must fit into contiguous memory.
- Max size of encoded / decoded CBOR data is UINT32_MAX (4GB).
@@ -511,8 +511,8 @@
- Does not support encoding indefinite lengths (decoding is supported).
- Does not directly support some tagged types: decimal fractions, big floats
- Does not directly support labels in maps other than text strings and ints.
+ - Does not directly support int labels > INT64_MAX
- Epoch dates limited to INT64_MAX (+/- 292 billion years)
- - Only one tag per data item is supported for tag values > 62
- Tags on labels are ignored
This implementation is intended to run on 32 and 64-bit CPUs. It
@@ -853,6 +853,23 @@
void QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage);
+/**
+ @brief[in] Add an optional tag
+
+ @param[in] pCtx The encoding context to add the integer to.
+ @param[in] uTag The tag to add
+
+ The tag is applied to the next data item added to the encoded
+ output. That data item can be of any major CBOR type.
+
+ Any number of tags can be added to a data item.
+
+ When one of the Add functions is called with either a string or
+ integer label after a call to this function, the output will be
+ re ordered so that the tag comes after the label and tags the
+ value, not the label.
+ */
+void QCBOREncode_AddTag(QCBOREncodeContext *pCtx,uint64_t uTag);
/**
@@ -862,7 +879,6 @@
@param[in] pCtx The encoding context to add the integer to.
@param[in] szLabel The string map label for this integer value.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag A CBOR type 6 tag
@param[in] nNum The integer to add.
The functions and macros with a "U" add unsigned integers and those
@@ -900,26 +916,26 @@
*/
-void QCBOREncode_AddInt64_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, int64_t nNum);
-void QCBOREncode_AddUInt64_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, uint64_t uNum);
+void QCBOREncode_AddInt64_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, int64_t nNum);
+void QCBOREncode_AddUInt64_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uNum);
#define QCBOREncode_AddUInt64(pCtx, uNum) \
- QCBOREncode_AddUInt64_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (uNum))
+ QCBOREncode_AddUInt64_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (uNum))
#define QCBOREncode_AddUInt64ToMap(pCtx, szLabel, uNum) \
- QCBOREncode_AddUInt64_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (uNum))
+ QCBOREncode_AddUInt64_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (uNum))
#define QCBOREncode_AddUInt64ToMapN(pCtx, nLabel, uNum) \
- QCBOREncode_AddUInt64_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (uNum))
+ QCBOREncode_AddUInt64_2((pCtx), NULL, (nLabel), (uNum))
#define QCBOREncode_AddInt64(pCtx, nNum) \
- QCBOREncode_AddInt64_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (nNum))
+ QCBOREncode_AddInt64_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (nNum))
#define QCBOREncode_AddInt64ToMap(pCtx, szLabel, nNum) \
- QCBOREncode_AddInt64_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (nNum))
+ QCBOREncode_AddInt64_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (nNum))
#define QCBOREncode_AddInt64ToMapN(pCtx, nLabel, nNum) \
- QCBOREncode_AddInt64_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (nNum))
+ QCBOREncode_AddInt64_2((pCtx), NULL, (nLabel), (nNum))
@@ -931,32 +947,31 @@
@param[in] pCtx The encoding context to add the float to.
@param[in] szLabel The string map label for this integer value.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag A CBOR type 6 tag
@param[in] fNum The float to add.
- This works the same as QCBOREncode_AddInt64_3() except it is for floats and doubles.
+ This works the same as QCBOREncode_AddInt64_2() except it is for floats and doubles.
*/
-void QCBOREncode_AddFloat_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, float fNum);
-void QCBOREncode_AddDouble_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, double dNum);
+void QCBOREncode_AddFloat_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, float fNum);
+void QCBOREncode_AddDouble_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, double dNum);
#define QCBOREncode_AddFloat(pCtx, fNum) \
- QCBOREncode_AddFloat_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloat_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (fNum))
#define QCBOREncode_AddFloatToMap(pCtx, szLabel, fNum) \
- QCBOREncode_AddFloat_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloat_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (fNum))
#define QCBOREncode_AddFloatToMapN(pCtx, nLabel, fNum) \
- QCBOREncode_AddFloat_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloat_2((pCtx), NULL, (nLabel), (fNum))
#define QCBOREncode_AddDouble(pCtx, dNum) \
- QCBOREncode_AddDouble_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (dNum))
+ QCBOREncode_AddDouble_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (dNum))
#define QCBOREncode_AddDoubleToMap(pCtx, szLabel, dNum) \
- QCBOREncode_AddDouble_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (dNum))
+ QCBOREncode_AddDouble_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (dNum))
#define QCBOREncode_AddDoubleToMapN(pCtx, nLabel, dNum) \
- QCBOREncode_AddDouble_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (dNum))
+ QCBOREncode_AddDouble_2((pCtx), NULL, (nLabel), (dNum))
/*
@brief Add a half-precision floating point number to the encoded output
@@ -964,7 +979,6 @@
@param[in] pCtx The encoding context to add the float to.
@param[in] szLabel The string map label for this integer value.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag A CBOR type 6 tag
@param[in] fNum The float to add.
This will truncate the precision of the single precision float to half-precision.
@@ -977,20 +991,20 @@
Half-precision floating point number take up 2 bytes, half that of single-precision.
- This works the same as QCBOREncode_AddInt64_3() except it is for half-precision floats.
+ This works the same as QCBOREncode_AddInt64_2() except it is for half-precision floats.
*/
-void QCBOREncode_AddFloatAsHalf_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, float fNum);
+void QCBOREncode_AddFloatAsHalf_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, float fNum);
#define QCBOREncode_AddFloatAsHalf(pCtx, fNum) \
- QCBOREncode_AddFloatAsHalf_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloatAsHalf_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (fNum))
#define QCBOREncode_AddFloatAsHalfToMap(pCtx, szLabel, fNum) \
- QCBOREncode_AddFloatAsHalf_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloatAsHalf_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (fNum))
#define QCBOREncode_AddFloatAsHalfToMapN(pCtx, nLabel, fNum) \
- QCBOREncode_AddFloatAsHalf_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloatAsHalf_2((pCtx), NULL, (nLabel), (fNum))
/*
@@ -999,7 +1013,6 @@
@param[in] pCtx The encoding context to add the float to.
@param[in] szLabel The string map label for this integer value.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag A CBOR type 6 tag
@param[in] fNum The float to add.
This will selectively encode the single-precision floating point number as either
@@ -1015,29 +1028,28 @@
These will always be decoded into a float as standard C doesn't have a widely used
standard representation for half-precision floats yet.
- This works the same as QCBOREncode_AddInt64_3() except it is for single and half-precision floats.
+ This works the same as QCBOREncode_AddInt64_2() except it is for single and half-precision floats.
*/
-void QCBOREncode_AddFloatAsSmallest_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, float fNum);
+void QCBOREncode_AddFloatAsSmallest_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, float fNum);
#define QCBOREncode_AddFloatAsSmallest(pCtx, fNum) \
- QCBOREncode_AddFloatAsSmallest_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloatAsSmallest_2((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (fNum))
#define QCBOREncode_AddFloatAsSmallestToMap(pCtx, szLabel, fNum) \
- QCBOREncode_AddFloatAsSmallest_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloatAsSmallest_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (fNum))
#define QCBOREncode_AddFloatAsSmallestToMapN(pCtx, nLabel, fNum) \
- QCBOREncode_AddFloatAsSmallest_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (fNum))
+ QCBOREncode_AddFloatAsSmallest_2((pCtx), NULL, (nLabel), (fNum))
-/*
+/**
@brief Add a dynamically sized floating point number to the encoded output
@param[in] pCtx The encoding context to add the float to.
@param[in] szLabel The string map label for this integer value.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag A CBOR type 6 tag
@param[in] dNum The float to add.
This will selectively encode the double-precision floating point number as either
@@ -1059,20 +1071,20 @@
using this approach can / should assume that floats received actually have the precision
of double. They should probably cast the float received to double.
- This works the same as QCBOREncode_AddInt64_3() except it is for floating point types.
+ This works the same as QCBOREncode_AddInt64_2() except it is for floating point types.
*/
-void QCBOREncode_AddDoubleAsSmallest_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, double dNum);
+void QCBOREncode_AddDoubleAsSmallest_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, double dNum);
#define QCBOREncode_AddDoubleAsSmallest(pCtx, dNum) \
- QCBOREncode_AddDoubleAsSmallest_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (dNum))
+ QCBOREncode_AddDoubleAsSmallest_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (dNum))
#define QCBOREncode_AddDoubleAsSmallestToMap(pCtx, szLabel, dNum) \
- QCBOREncode_AddDoubleAsSmallest_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (dNum))
+ QCBOREncode_AddDoubleAsSmallest_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (dNum))
#define QCBOREncode_AddDoubleAsSmallestToMapN(pCtx, nLabel, dNum) \
- QCBOREncode_AddDoubleAsSmallest_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (dNum))
+ QCBOREncode_AddDoubleAsSmallest_2((pCtx), NULL, (nLabel), (dNum))
@@ -1101,15 +1113,16 @@
This implementation cannot encode fractional seconds using float or double
even though that is allowed by CBOR, but you can encode them if you
- want to by calling QCBOREncode_AddFloat_3() or QCBOREncode_AddDouble_3()
+ want to by calling QCBOREncode_AddFloat_2() or QCBOREncode_AddDouble_2()
with the right parameters.
- Error handling is the same as QCBOREncode_AddInt64_3().
+ Error handling is the same as QCBOREncode_AddInt64_2().
*/
static inline void QCBOREncode_AddDateEpoch_2(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel, int64_t date)
{
- QCBOREncode_AddInt64_3(pCtx, szLabel, nLabel, CBOR_TAG_DATE_EPOCH, date);
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_DATE_EPOCH);
+ QCBOREncode_AddInt64_2(pCtx, szLabel, nLabel, date);
}
#define QCBOREncode_AddDateEpoch(pCtx, date) \
@@ -1122,6 +1135,15 @@
QCBOREncode_AddDateEpoch_2((pCtx), NULL, (nLabel), (date))
+/*
+ Use QCBOREncode_AddBytes_2() or QCBOREncode_AddBText_2() or
+ QCBOREncode_AddEncoded_2() instead of this. Their inline
+ implementations call this to do their work.
+
+ The code is structured like this with the inline functions
+ to reduce object code.
+ */
+void QCBOREncode_AddBuffer_2(QCBOREncodeContext *me, uint8_t uMajorType, const char *szLabel, int64_t nLabel, UsefulBufC Bytes);
/**
@@ -1131,7 +1153,6 @@
@param[in] pCtx The context to initialize.
@param[in] szLabel The string map label for this integer value.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag Optional CBOR data tag or CBOR_TAG_NONE.
@param[in] Bytes Pointer and length of the input data.
Simply adds the bytes to the encoded output and CBOR major type 2.
@@ -1141,46 +1162,69 @@
*/
-void QCBOREncode_AddBytes_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, UsefulBufC Bytes);
+static inline void QCBOREncode_AddBytes_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddBuffer_2(pCtx, CBOR_MAJOR_TYPE_BYTE_STRING, szLabel, nLabel, Bytes);
+}
#define QCBOREncode_AddBytes(pCtx, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (Bytes))
+ QCBOREncode_AddBytes_2((pCtx), NULL, QCBOR_NO_INT_LABEL, Bytes)
#define QCBOREncode_AddBytesToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (Bytes))
+ QCBOREncode_AddBytes_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddBytesToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (Bytes))
+ QCBOREncode_AddBytes_2((pCtx), NULL, (nLabel), (Bytes))
+
+
+
+static inline void QCBOREncode_AddBinaryUUID_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_BIN_UUID);
+ QCBOREncode_AddBytes_2(pCtx, szLabel, nLabel, Bytes);
+}
#define QCBOREncode_AddBinaryUUID(pCtx, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_BIN_UUID, (Bytes))
+ QCBOREncode_AddBinaryUUID_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddBinaryUUIDToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_BIN_UUID, (Bytes))
+ QCBOREncode_AddBinaryUUID_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddBinaryUUIDToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), NULL, (nLabel), CBOR_TAG_BIN_UUID, (Bytes))
+ QCBOREncode_AddBinaryUUID_2((pCtx), NULL, (nLabel), (Bytes))
+static inline void QCBOREncode_AddPositiveBignum_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_POS_BIGNUM);
+ QCBOREncode_AddBytes_2(pCtx, szLabel, nLabel, Bytes);
+}
+
#define QCBOREncode_AddPositiveBignum(pCtx, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_POS_BIGNUM, (Bytes))
+ QCBOREncode_AddPositiveBignum_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddPositiveBignumToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_POS_BIGNUM, (Bytes))
+ QCBOREncode_AddPositiveBignum_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddPositiveBignumToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), NULL, (nLabel), CBOR_TAG_POS_BIGNUM, (Bytes))
+ QCBOREncode_AddPositiveBignum_2((pCtx), NULL, (nLabel), (Bytes))
+static inline void QCBOREncode_AddNegativeBignum_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_NEG_BIGNUM);
+ QCBOREncode_AddBytes_2(pCtx, szLabel, nLabel, Bytes);
+}
+
#define QCBOREncode_AddNegativeBignum(pCtx, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NEG_BIGNUM, (Bytes))
+ QCBOREncode_AddNegativeBignum_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddNegativeBignumToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NEG_BIGNUM, (Bytes))
+ QCBOREncode_AddNegativeBignum_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddNegativeBignumToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddBytes_3((pCtx), NULL, (nLabel), CBOR_TAG_NEG_BIGNUM, (Bytes))
+ QCBOREncode_AddNegativeBignum_2((pCtx), NULL, (nLabel), (Bytes))
@@ -1191,7 +1235,6 @@
@param[in] pCtx The context to initialize.
@param[in] szLabel The string map label for this integer value.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag Optional CBOR data tag or CBOR_TAG_NONE.
@param[in] Bytes Pointer and length of text to add.
The text passed in must be unencoded UTF-8 according to RFC
@@ -1206,78 +1249,122 @@
lengths greater. This limit to 4GB for a text string should not be a
problem.
- Error handling is the same as QCBOREncode_AddInt64_3().
+ Error handling is the same as QCBOREncode_AddInt64_2().
*/
-
-void QCBOREncode_AddText_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, UsefulBufC Bytes);
+static inline void QCBOREncode_AddText_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddBuffer_2(pCtx, CBOR_MAJOR_TYPE_TEXT_STRING, szLabel, nLabel, Bytes);
+}
#define QCBOREncode_AddText(pCtx, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (Bytes))
+ QCBOREncode_AddText_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddTextToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (Bytes))
+ QCBOREncode_AddText_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddTextToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (Bytes))
+ QCBOREncode_AddText_2((pCtx), NULL, (nLabel), (Bytes))
-inline static void QCBOREncode_AddSZString_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, const char *szString) {
- QCBOREncode_AddText_3(pCtx, szLabel, nLabel, uTag, UsefulBuf_FromSZ(szString));
+
+inline static void QCBOREncode_AddSZString_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, const char *szString)
+{
+ QCBOREncode_AddText_2(pCtx, szLabel, nLabel,UsefulBuf_FromSZ(szString));
}
#define QCBOREncode_AddSZString(pCtx, szString) \
- QCBOREncode_AddSZString_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (szString))
+ QCBOREncode_AddSZString_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (szString))
#define QCBOREncode_AddSZStringToMap(pCtx, szLabel, szString) \
- QCBOREncode_AddSZString_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (szString))
+ QCBOREncode_AddSZString_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (szString))
#define QCBOREncode_AddSZStringToMapN(pCtx, nLabel, szString) \
- QCBOREncode_AddSZString_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (szString))
+ QCBOREncode_AddSZString_2((pCtx), NULL, (nLabel), (szString))
+
+
+
+
+static inline void QCBOREncode_AddURI_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_URI);
+ QCBOREncode_AddText_2(pCtx, szLabel, nLabel, Bytes);
+}
#define QCBOREncode_AddURI(pCtx, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_URI, (Bytes))
+ QCBOREncode_AddURI_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddURIToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_URI, (Bytes))
+ QCBOREncode_AddURI_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddURIToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, (nLabel), CBOR_TAG_URI, (Bytes))
+ QCBOREncode_AddURI_2((pCtx), NULL, (nLabel), (Bytes))
+
+
+static inline void QCBOREncode_AddB64Text_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_B64);
+ QCBOREncode_AddText_2(pCtx, szLabel, nLabel, Bytes);
+}
#define QCBOREncode_AddB64Text(pCtx, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_B64, (Bytes))
+ QCBOREncode_AddB64Text_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddB64TextToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_B64, (Bytes))
+ QCBOREncode_AddB64Text_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddB64TextToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, (nLabel), CBOR_TAG_B64, (Bytes))
+ QCBOREncode_AddB64Text_2((pCtx), NULL, (nLabel), (Bytes))
+
+
+
+static inline void QCBOREncode_AddB64URLText_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_B64URL);
+ QCBOREncode_AddText_2(pCtx, szLabel, nLabel, Bytes);
+}
#define QCBOREncode_AddB64URLText(pCtx, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_B64URL, (Bytes))
+ QCBOREncode_AddB64URLText_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddB64URLTextToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_B64URL, (Bytes))
+ QCBOREncode_AddB64URLText_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddB64URLTextToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, (nLabel), CBOR_TAG_B64URL, (Bytes))
+ QCBOREncode_AddB64URLText_2((pCtx), NULL, (nLabel), (Bytes))
+
+
+
+static inline void QCBOREncode_AddRegex_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_REGEX);
+ QCBOREncode_AddText_2(pCtx, szLabel, nLabel, Bytes);
+}
#define QCBOREncode_AddRegex(pCtx, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_REGEX, (Bytes))
+ QCBOREncode_AddRegex_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddRegexToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_REGEX, (Bytes))
+ QCBOREncode_AddRegex_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddRegexToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, (nLabel), CBOR_TAG_REGEX, (Bytes))
+ QCBOREncode_AddRegex_2((pCtx), NULL, (nLabel), (Bytes))
+
+
+
+static inline void QCBOREncode_AddMIMEData_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_MIME);
+ QCBOREncode_AddText_2(pCtx, szLabel, nLabel, Bytes);
+}
#define QCBOREncode_AddMIMEData(pCtx, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_MIME, (Bytes))
+ QCBOREncode_AddMIMEData_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddMIMEDataToMap(pCtx, szLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_MIME, (Bytes))
+ QCBOREncode_AddMIMEData_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Bytes))
#define QCBOREncode_AddMIMEDataToMapN(pCtx, nLabel, Bytes) \
- QCBOREncode_AddText_3((pCtx), NULL, (nLabel), CBOR_TAG_MIME, (Bytes))
+ QCBOREncode_AddMIMEData_2((pCtx), NULL, (nLabel), (Bytes))
@@ -1290,7 +1377,6 @@
@param[in] szLabel A string label for the bytes to add. NULL if no label.
@param[in] nLabel The integer map label for this integer value.
- @return
None.
The string szDate should be in the form of RFC 3339 as refined by section
@@ -1300,20 +1386,34 @@
at all. If you add an incorrect format date string, the generated
CBOR will be incorrect and the receiver may not be able to handle it.
- Error handling is the same as QCBOREncode_AddInt64_3().
+ Error handling is the same as QCBOREncode_AddInt64_2().
*/
+static inline void QCBOREncode_AddDateString_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, const char *szDate)
+{
+ QCBOREncode_AddTag(pCtx, CBOR_TAG_DATE_STRING);
+ QCBOREncode_AddSZString_2(pCtx, szLabel, nLabel, szDate);
+}
#define QCBOREncode_AddDateString(pCtx, szDate) \
- QCBOREncode_AddSZString_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_DATE_STRING, (szDate))
+ QCBOREncode_AddDateString_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (szDate))
#define QCBOREncode_AddDateStringToMap(pCtx, szLabel, szDate) \
- QCBOREncode_AddSZString_3(pCtx, (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_DATE_STRING, (szDate))
+ QCBOREncode_AddDateString_2(pCtx, szLabel, QCBOR_NO_INT_LABEL, (szDate))
#define QCBOREncode_AddDateStringToMapN(pCtx, nLabel, szDate) \
- QCBOREncode_AddSZString_3(pCtx, NULL, (nLabel), CBOR_TAG_DATE_STRING, (szDate))
+ QCBOREncode_AddDateString_2(pCtx, NULL, (nLabel), (szDate))
+/*
+Use QCBOREncode_AddSimple_2() or QCBOREncode_AddBool_2()
+instead of this. Their inline
+implementations call this to do their work.
+
+The code is structured like this with the inline functions
+to reduce object code.
+ */
+void QCBOREncode_AddType7_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, size_t uSize, uint64_t uNum);
/**
@@ -1323,24 +1423,31 @@
@param[in] pCtx The encoding context to add the simple value to.
@param[in] szLabel A string label for the bytes to add. NULL if no label.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag Optional CBOR data tag or CBOR_TAG_NONE.
@param[in] uSimple One of CBOR_SIMPLEV_FALSE through _UNDEF
CBOR defines encoding for special values "true", "false", "null" and "undef". This
function can add these values.
- Error handling is the same as QCBOREncode_AddInt64_3().
+ Error handling is the same as QCBOREncode_AddInt64_2().
*/
-void QCBOREncode_AddSimple_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, uint8_t uSimple);
+static inline void QCBOREncode_AddSimple_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint8_t uSimple)
+{
+ if(uSimple < CBOR_SIMPLEV_FALSE || uSimple > CBOR_SIMPLEV_UNDEF) {
+ pCtx->uError = QCBOR_ERR_BAD_SIMPLE;
+ } else {
+ QCBOREncode_AddType7_2(pCtx, szLabel, nLabel, 0, uSimple);
+ }
+}
#define QCBOREncode_AddSimple(pCtx, uSimple) \
- QCBOREncode_AddSimple_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (uSimple))
+ QCBOREncode_AddSimple_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (uSimple))
#define QCBOREncode_AddSimpleToMap(pCtx, szLabel, uSimple) \
- QCBOREncode_AddSimple_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (uSimple))
+ QCBOREncode_AddSimple_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (uSimple))
#define QCBOREncode_AddSimpleToMapN(pCtx, nLabel, uSimple) \
- QCBOREncode_AddSimple_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (uSimple))
+ QCBOREncode_AddSimple_2((pCtx), NULL, (nLabel), (uSimple))
+
/**
@@ -1350,27 +1457,39 @@
@param[in] pCtx The encoding context to add the simple value to.
@param[in] szLabel A string label for the bytes to add. NULL if no label.
@param[in] nLabel The integer map label for this integer value.
- @param[in] uTag Optional CBOR data tag or CBOR_TAG_NONE.
@param[in] b true or false from stdbool. Anything will result in an error.
- Error handling is the same as QCBOREncode_AddInt64_3().
+ Error handling is the same as QCBOREncode_AddInt64_2().
*/
-inline static void QCBOREncode_AddBool_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, bool b) {
+inline static void QCBOREncode_AddBool_2(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, bool b)
+{
uint8_t uSimple = CBOR_SIMPLE_BREAK; // CBOR_SIMPLE_BREAK is invalid here. The point is to cause an error later
if(b == true || b == false)
uSimple = CBOR_SIMPLEV_FALSE + b;;
- QCBOREncode_AddSimple_3(pCtx, szLabel, nLabel, uTag, uSimple);
+ QCBOREncode_AddSimple_2(pCtx, szLabel, nLabel, uSimple);
}
#define QCBOREncode_AddBool(pCtx, bool) \
- QCBOREncode_AddBool_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (bool))
+ QCBOREncode_AddBool_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (bool))
#define QCBOREncode_AddBoolToMap(pCtx, szLabel, bool) \
- QCBOREncode_AddBool_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (bool))
+ QCBOREncode_AddBool_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (bool))
#define QCBOREncode_AddBoolToMapN(pCtx, nLabel, bool) \
- QCBOREncode_AddBool_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, (bool))
+ QCBOREncode_AddBool_2((pCtx), NULL, (nLabel), (bool))
+
+
+
+/*
+ Call QCBOREncode_OpenArray_2(), QCBOREncode_OpenMap_2() or
+ QCBOREncode_OpenBstrWrap_2() instead of this. Their inline
+ implementations call this to do their work.
+
+ The code is structured like this with the inline functions
+ to reduce object code.
+ */
+void QCBOREncode_OpenMapOrArray_2(QCBOREncodeContext *me, uint8_t uMajorType, const char *szLabel, uint64_t nLabel);
/**
@@ -1380,7 +1499,6 @@
@param[in] pCtx The encoding context to open the array in.
@param[in] szLabel A NULL-terminated string label for the map. May be a NULL pointer.
@param[in] nLabel An integer label for the whole map. QCBOR_NO_INT_LABEL for no integer label.
- @param[in] uTag A tag for the whole map or CBOR_TAG_NONE.
Arrays are the basic CBOR aggregate or structure type. Call this
function to start or open an array. The call the various AddXXX
@@ -1419,16 +1537,19 @@
to get to all the data needed for a signature verification.
*/
-void QCBOREncode_OpenArray_3(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel, uint64_t uTag);
+static inline void QCBOREncode_OpenArray_2(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel)
+{
+ QCBOREncode_OpenMapOrArray_2(pCtx, CBOR_MAJOR_TYPE_ARRAY, szLabel, nLabel);
+}
#define QCBOREncode_OpenArray(pCtx) \
- QCBOREncode_OpenArray_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE)
+ QCBOREncode_OpenArray_2((pCtx), NULL, QCBOR_NO_INT_LABEL)
#define QCBOREncode_OpenArrayInMap(pCtx, szLabel) \
- QCBOREncode_OpenArray_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE)
+ QCBOREncode_OpenArray_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL)
#define QCBOREncode_OpenArrayInMapN(pCtx, nLabel) \
- QCBOREncode_OpenArray_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE)
+ QCBOREncode_OpenArray_2((pCtx), NULL, (nLabel))
/**
@@ -1438,7 +1559,6 @@
@param[in] pCtx The context to add to.
@param[in] szLabel A NULL-terminated string label for the map. May be a NULL pointer.
@param[in] nLabel An integer label for the whole map. QCBOR_NO_INT_LABEL for no integer label.
- @param[in] uTag A tag for the whole map or CBOR_TAG_NONE.
See QCBOREncode_OpenArray() for more information.
@@ -1459,16 +1579,19 @@
*/
-void QCBOREncode_OpenMap_3(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel, uint64_t uTag);
+static inline void QCBOREncode_OpenMap_2(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel)
+{
+ QCBOREncode_OpenMapOrArray_2(pCtx, CBOR_MAJOR_TYPE_MAP, szLabel, nLabel);
+}
#define QCBOREncode_OpenMap(pCtx) \
- QCBOREncode_OpenMap_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE)
+ QCBOREncode_OpenMap_2((pCtx), NULL, QCBOR_NO_INT_LABEL)
#define QCBOREncode_OpenMapInMap(pCtx, szLabel) \
- QCBOREncode_OpenMap_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE)
+ QCBOREncode_OpenMap_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL)
#define QCBOREncode_OpenMapInMapN(pCtx, nLabel) \
- QCBOREncode_OpenMap_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE)
+ QCBOREncode_OpenMap_2((pCtx), NULL, (nLabel))
/**
@@ -1518,7 +1641,6 @@
@param[in] pCtx The context to add to.
@param[in] szLabel A NULL-terminated string label for the map. May be a NULL pointer.
@param[in] nLabel An integer label for the whole map. QCBOR_NO_INT_LABEL for no integer label.
- @param[in] uTag A tag for the whole map or CBOR_TAG_NONE.
All added encoded items between this call and a call to
QCBOREncode_CloseBstrWrap() will be wrapped in a bstr. They will
@@ -1533,16 +1655,19 @@
Sig_structure) potentially saving a lot of memory.
*/
-void QCBOREncode_OpenBstrWrap_3(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel, uint64_t uTag);
+static inline void QCBOREncode_OpenBstrWrap_2(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel)
+{
+ QCBOREncode_OpenMapOrArray_2(pCtx, CBOR_MAJOR_TYPE_BYTE_STRING, szLabel, nLabel);
+}
#define QCBOREncode_BstrWrap(pCtx) \
- QCBOREncode_OpenBstrWrap_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE)
+ QCBOREncode_OpenBstrWrap_2((pCtx), NULL, QCBOR_NO_INT_LABEL)
#define QCBOREncode_BstrWrapInMap(pCtx, szLabel) \
- QCBOREncode_OpenBstrWrap_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE)
+ QCBOREncode_OpenBstrWrap_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL)
#define QCBOREncode_BstrWrapMapN(pCtx, nLabel) \
- QCBOREncode_OpenBstrWrap_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE)
+ QCBOREncode_OpenBstrWrap_2((pCtx), NULL, (nLabel))
@@ -1552,7 +1677,6 @@
@param[in] pCtx The context to add to.
@param[in] szLabel A NULL-terminated string label for the map. May be a NULL pointer.
@param[in] nLabel An integer label for the whole map. QCBOR_NO_INT_LABEL for no integer label.
- @param[in] uTag A tag for the whole map or CBOR_TAG_NONE.
@param[in] Encoded The already-encoded CBOR to add to the context.
The encoded CBOR being added must be fully conforming CBOR. It must
@@ -1570,37 +1694,19 @@
*/
-void QCBOREncode_AddEncodedToMap_3(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel, uint64_t uTag, UsefulBufC Encoded);
+static inline void QCBOREncode_AddEncodedToMap_2(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t nLabel, UsefulBufC Encoded)
+{
+ QCBOREncode_AddBuffer_2(pCtx, CBOR_MAJOR_NONE_TYPE_RAW, szLabel, nLabel, Encoded);
+}
#define QCBOREncode_AddEncodedToMapN(pCtx, nLabel, Encoded) \
- QCBOREncode_AddEncodedToMap_3((pCtx), NULL, (nLabel), CBOR_TAG_NONE, Encoded)
+ QCBOREncode_AddEncodedToMap_2((pCtx), NULL, (nLabel), Encoded)
#define QCBOREncode_AddEncoded(pCtx, Encoded) \
- QCBOREncode_AddEncodedToMap_3((pCtx), NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (Encoded))
+ QCBOREncode_AddEncodedToMap_2((pCtx), NULL, QCBOR_NO_INT_LABEL, (Encoded))
#define QCBOREncode_AddEncodedToMap(pCtx, szLabel, Encoded) \
- QCBOREncode_AddEncodedToMap_3((pCtx), (szLabel), QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, (Encoded))
-
-
-/**
-
- @brief Add a simple value
-
- @param[in] pCtx The encoding context to add the simple value to.
- @param[in] szLabel A string label for the bytes to add. NULL if no label.
- @param[in] nLabel The integer map tag / label for this integer value.
- @param[in] uTag Optional CBOR data tag or CBOR_TAG_NONE.
- @param[in] uSimple One of CBOR_SIMPLEV_xxx.
-
- There should be no need to use this function directly unless some
- extensions to the CBOR standard are created and put to use. All the defined
- simple types are available via the macros for false...null
- below. Float and double are also simple types and have functions to
- add them above.
-
- Error handling is the same as QCBOREncode_AddInt64_3().
- */
-void QCBOREncode_AddRawSimple_3(QCBOREncodeContext *pCtx, const char *szLabel, int64_t nLabel, uint64_t uTag, uint8_t uSimple);
+ QCBOREncode_AddEncodedToMap_2((pCtx), (szLabel), QCBOR_NO_INT_LABEL, (Encoded))
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index ece04e4..e8401ca 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -82,14 +82,6 @@
/*...... This is a ruler that is 80 characters long...........................*/
-// Used internally in the impementation here
-// Must not conflict with any of the official CBOR types
-#define CBOR_MAJOR_NONE_TYPE_RAW 9
-
-
-
-
-
/*
CBOR's two nesting types, arrays and maps, are tracked here. There is a
limit of QCBOR_MAX_ARRAY_NESTING to the number of arrays and maps
@@ -204,7 +196,7 @@
error checks in this code if you knew the caller called it
correctly. Maybe someday CDDL or some such language will be able to
generate the code to call this and the calling code would always be
- correct. This could also make automatically size some of the data
+ correct. This could also automatically size some of the data
structures like array/map nesting resulting in some good memory
savings.
*/
@@ -280,7 +272,7 @@
// always generated internally, not by the caller, b) this is for CBOR
// _generation_, not parsing c) a mistake will result in bad CBOR generation,
// not a security vulnerability.
- uMajorType <<= 5; // TODO: make this a constant
+ uMajorType <<= 5;
if(uNumber > 0xffffffff || uMinLen >= 8) {
UsefulOutBuf_InsertByte(&(me->OutBuf), uMajorType + LEN_IS_EIGHT_BYTES, uPos);
@@ -319,26 +311,26 @@
}
-static void AddBytesInternal(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, UsefulBufC Bytes, uint8_t uMajorType);
/*
- Add an optional label and optional tag. It will go in front of a real data item.
+ Internal function for adding positive and negative integers of all different sizes
*/
-static void AddLabelAndOptionalTag(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag)
+void InsertInt64(QCBOREncodeContext *me, int64_t nNum, size_t uPos)
{
- if(szLabel) {
- UsefulBufC SZText = {szLabel, strlen(szLabel)};
- AddBytesInternal(me, NULL, nLabel, CBOR_TAG_NONE, SZText, CBOR_MAJOR_TYPE_TEXT_STRING);
- } else if (QCBOR_NO_INT_LABEL != nLabel) {
- // Add an integer label. This is just adding an integer at this point
- // This will result in a call right back to here, but the call won't do anything
- // because of the params NULL, QCBOR_NO_INT_LABEL and CBOR_TAG_NONE
- QCBOREncode_AddInt64_3(me, NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, nLabel);
+ uint8_t uMajorType;
+ uint64_t uValue;
+
+ if(nNum < 0) {
+ uValue = (uint64_t)(-nNum - 1); // This is the way negative ints work in CBOR. -1 encodes as 0x00 with major type negative int.
+ uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT;
+ } else {
+ uValue = (uint64_t)nNum;
+ uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT;
}
- if(uTag != CBOR_TAG_NONE) {
- AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_OPTIONAL, uTag);
- }
+
+ InsertEncodedTypeAndNumber(me, uMajorType, 0, uValue, uPos);
+ me->uError = Nesting_Increment(&(me->nesting), 1);
}
@@ -348,28 +340,30 @@
different major types. This is also used to insert raw
pre-encoded CBOR.
*/
-static void AddBytesInternal(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, UsefulBufC Bytes, uint8_t uMajorType)
+static void AddBufferInternal(QCBOREncodeContext *me, UsefulBufC Bytes, uint8_t uMajorType, size_t uPos)
{
if(Bytes.len >= UINT32_MAX) {
- // This implementation doesn't allow buffers larger than UINT32_MAX. This is
- // primarily because QCBORTrackNesting.pArrays[].uStart is an uint32 rather
- // than size_t to keep the stack usage down. Also it is entirely impractical
- // to create tokens bigger than 4GB in contiguous RAM
+ // This implementation doesn't allow buffers larger than UINT32_MAX.
+ // This is primarily because QCBORTrackNesting.pArrays[].uStart is
+ // an uint32 rather than size_t to keep the stack usage down. Also
+ // it is entirely impractical to create tokens bigger than 4GB in
+ // contiguous RAM
me->uError = QCBOR_ERR_BUFFER_TOO_LARGE;
} else {
-
- AddLabelAndOptionalTag(me, szLabel, nLabel, uTag);
-
if(!me->uError) {
-
// If it is not Raw CBOR, add the type and the length
if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) {
- AppendEncodedTypeAndNumber(me, uMajorType, Bytes.len);
+ const size_t uPosBeforeInsert = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
+ InsertEncodedTypeAndNumber(me, uMajorType, 0, Bytes.len, uPos);
+ // The increment in uPos is to account for bytes added for
+ // type and number so the buffer being added goes to the
+ // right place
+ uPos += UsefulOutBuf_GetEndPosition(&(me->OutBuf)) - uPosBeforeInsert;
}
// Actually add the bytes
- UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes);
+ UsefulOutBuf_InsertUsefulBuf(&(me->OutBuf), Bytes, uPos);
// Update the array counting if there is any nesting at all
me->uError = Nesting_Increment(&(me->nesting), 1);
@@ -378,38 +372,69 @@
}
+/*
+ Add an optional label. It will go in front of a real data item.
+ */
+static void AddLabel(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel)
+{
+ size_t uPos = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
+ if(Nesting_GetMajorType(&(me->nesting)) == CBOR_MAJOR_NONE_TAG_LABEL_REORDER) {
+ // Have to insert the label rather than just appen if a tag
+ // has been added. This is so the tag ends up on the value, not
+ // on the label.
+ uPos = Nesting_GetStartPos(&(me->nesting));
+ Nesting_Decrease(&(me->nesting));
+ }
+
+ if(szLabel) {
+ const UsefulBufC SZText = UsefulBuf_FromSZ(szLabel);
+ AddBufferInternal(me, SZText, CBOR_MAJOR_TYPE_TEXT_STRING, uPos);
+ } else if (QCBOR_NO_INT_LABEL != nLabel) {
+ InsertInt64(me, nLabel, uPos);
+ }
+}
/*
- Public functions for adding strings and raw encoded CBOR. See header qcbor.h
+ Public Function
*/
-void QCBOREncode_AddBytes_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, UsefulBufC Bytes)
+void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag)
{
- AddBytesInternal(me, szLabel, nLabel, uTag, Bytes, CBOR_MAJOR_TYPE_BYTE_STRING);
-}
+ uint8_t uNestingType = Nesting_GetMajorType(&(me->nesting));
+ if(uNestingType == CBOR_MAJOR_TYPE_MAP || uNestingType == CBOR_MAJOR_TYPE_ARRAY) {
+ // Remember where the first tag is for this item
+ // So we can go back and insert the label in front of it.
+ // Cast to uint32_t here OK as all inputs are limited to 4GB
+ const uint32_t uPos = (uint32_t)UsefulOutBuf_GetEndPosition(&(me->OutBuf));
+ me->uError = Nesting_Increase(&(me->nesting), CBOR_MAJOR_NONE_TAG_LABEL_REORDER, uPos);
+ }
-void QCBOREncode_AddText_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, UsefulBufC Bytes)
-{
- AddBytesInternal(me, szLabel, nLabel, uTag, Bytes, CBOR_MAJOR_TYPE_TEXT_STRING);
+ AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_OPTIONAL, uTag);
}
-void QCBOREncode_AddEncodedToMap_3(QCBOREncodeContext *me, const char *szLabel, uint64_t nLabel, uint64_t uTag, UsefulBufC Encoded)
-{
- AddBytesInternal(me, szLabel, nLabel, uTag, Encoded, CBOR_MAJOR_NONE_TYPE_RAW);
-}
-
-
/*
- Internal function common to opening an array or a map
-
- QCBOR_MAX_ARRAY_NESTING is the number of times Open can be called
- successfully. Call it one more time gives an error.
-
+ Semi-public interface. Called by inline functions to add text and byte strings
+ and already-encoded CBOR. They are the real public interface, even though this
+ is the main entry point. The code is structured like this to reduce code size.
*/
-static void OpenMapOrArrayInternal(QCBOREncodeContext *me, uint8_t uMajorType, const char *szLabel, uint64_t nLabel, uint64_t uTag)
+void QCBOREncode_AddBuffer_2(QCBOREncodeContext *me, uint8_t uMajorType, const char *szLabel, int64_t nLabel, UsefulBufC Bytes)
{
- AddLabelAndOptionalTag(me, szLabel, nLabel, uTag);
+ AddLabel(me, szLabel, nLabel);
+ if(!me->uError) {
+ AddBufferInternal(me, Bytes, uMajorType, UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
+ }
+}
+
+
+/*
+ Semi-public interfaced. Called by inline functions to open arrays, maps and
+ bstr wrapped CBOR. They are the real public interface, even though this is the
+ main entry point. This code is structured like this to reduce code size.
+ */
+void QCBOREncode_OpenMapOrArray_2(QCBOREncodeContext *me, uint8_t uMajorType, const char *szLabel, uint64_t nLabel)
+{
+ AddLabel(me, szLabel, nLabel);
if(!me->uError) {
// Add one item to the nesting level we are in for the new map or array
@@ -425,23 +450,8 @@
/*
- Public functions for opening / closing arrays and maps. See header qcbor.h
+ Public functions for closing arrays and maps. See header qcbor.h
*/
-void QCBOREncode_OpenArray_3(QCBOREncodeContext *me, const char *szLabel, uint64_t nLabel, uint64_t uTag)
-{
- OpenMapOrArrayInternal(me, CBOR_MAJOR_TYPE_ARRAY, szLabel, nLabel, uTag);
-}
-
-void QCBOREncode_OpenMap_3(QCBOREncodeContext *me, const char *szLabel, uint64_t nLabel, uint64_t uTag)
-{
- OpenMapOrArrayInternal(me, CBOR_MAJOR_TYPE_MAP, szLabel, nLabel, uTag);
-}
-
-void QCBOREncode_OpenBstrWrap_3(QCBOREncodeContext *me, const char *szLabel, uint64_t nLabel, uint64_t uTag)
-{
- OpenMapOrArrayInternal(me, CBOR_MAJOR_TYPE_BYTE_STRING, szLabel, nLabel, uTag);
-}
-
void QCBOREncode_Close(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC *pWrappedCBOR)
{
if(!me->uError) {
@@ -450,21 +460,22 @@
} else if( Nesting_GetMajorType(&(me->nesting)) != uMajorType) {
me->uError = QCBOR_ERR_CLOSE_MISMATCH;
} else {
- const uint32_t uInsertPosition = Nesting_GetStartPos(&(me->nesting));
- // When the array, map or bstr wrap was started, nothing was done except
- // note the position of the start of it. This code goes back and inserts
- // the actual CBOR array, map or bstr and its length. That means all the
- // data that is in the array, map or wrapped needs to be slid to the
- // right. This is done by UsefulOutBuf's insert function that is called
- // from inside InsertEncodedTypeAndNumber()
+ // When the array, map or bstr wrap was started, nothing was done
+ // except note the position of the start of it. This code goes back
+ // and inserts the actual CBOR array, map or bstr and its length.
+ // That means all the data that is in the array, map or wrapped
+ // needs to be slid to the right. This is done by UsefulOutBuf's
+ // insert function that is called from inside
+ // InsertEncodedTypeAndNumber()
+ const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting));
+ const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
+ // This can't go negative because the UsefulOutBuf always only grows
+ // and never shrinks. UsefulOutBut itself also has defenses such that
+ // it won't write were it should not even if given hostile input lengths
+ const size_t uLenOfEncodedMapOrArray = uEndPosition - uInsertPosition;
- // Cast from size_t to uin32_t is safe because the UsefulOutBuf
- // size is limited to UINT32_MAX in QCBOR_Init().
- const uint32_t uEndPosition = (uint32_t)UsefulOutBuf_GetEndPosition(&(me->OutBuf));
- const uint32_t uLenOfEncodedMapOrArray = uEndPosition - uInsertPosition;
-
- // Length is number of bytes for a bstr and number of items for map & array
- const uint32_t uLength = uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING ?
+ // Length is number of bytes for a bstr and number of items a for map & array
+ const size_t uLength = uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING ?
uLenOfEncodedMapOrArray : Nesting_GetCount(&(me->nesting));
// Actually insert
@@ -481,7 +492,7 @@
// InsertEncodedTypeAndNumber() call that slides data to the right.
if(pWrappedCBOR) {
UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
- uint32_t uBstrLen = (uint32_t)UsefulOutBuf_GetEndPosition(&(me->OutBuf)) - uEndPosition;
+ size_t uBstrLen = UsefulOutBuf_GetEndPosition(&(me->OutBuf)) - uEndPosition;
*pWrappedCBOR = UsefulBuf_Tail(PartialResult, uInsertPosition+uBstrLen);
}
Nesting_Decrease(&(me->nesting));
@@ -490,15 +501,14 @@
}
-
/*
- Internal function for adding positive and negative integers of all different sizes
+ Public functions for adding integers. See header qcbor.h
*/
-static void AddUInt64Internal(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, uint8_t uMajorType, uint64_t n)
+void QCBOREncode_AddUInt64_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uNum)
{
- AddLabelAndOptionalTag(me, szLabel, nLabel, uTag);
+ AddLabel(me, szLabel, nLabel);
if(!me->uError) {
- AppendEncodedTypeAndNumber(me, uMajorType, n);
+ AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uNum);
me->uError = Nesting_Increment(&(me->nesting), 1);
}
}
@@ -507,31 +517,21 @@
/*
Public functions for adding integers. See header qcbor.h
*/
-void QCBOREncode_AddUInt64_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, uint64_t uNum)
+void QCBOREncode_AddInt64_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, int64_t nNum)
{
- AddUInt64Internal(me, szLabel, nLabel, uTag, CBOR_MAJOR_TYPE_POSITIVE_INT, uNum);
-}
-
-void QCBOREncode_AddInt64_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, int64_t nNum)
-{
- uint8_t uMajorType;
- uint64_t uValue;
-
- // Handle CBOR's particular format for positive and negative integers
- if(nNum < 0) {
- uValue = (uint64_t)(-nNum - 1); // This is the way negative ints work in CBOR. -1 encodes as 0x00 with major type negative int.
- uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT;
- } else {
- uValue = (uint64_t)nNum;
- uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT;
+ AddLabel(me, szLabel, nLabel);
+ if(!me->uError) {
+ // Cast is OK here because the output buffer is limited to 4GB in Init().
+ InsertInt64(me, nNum, (uint32_t)UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
}
- AddUInt64Internal(me, szLabel, nLabel, uTag, uMajorType, uValue);
}
-
-
/*
+ Semi-public interfaced. Called by inline functions to add simple and float
+ types. They are the real public interface, even though this is the
+ main entry point. This code is structured like this to reduce code size.
+
Common code for adding floats and doubles and simple types like true and false
One way to look at simple values is that they are:
@@ -545,15 +545,18 @@
- additional integer 31 is a "break"
- additional integers 32-255 are unassigned and could be used in an update to CBOR
*/
-static void AddSimpleInternal(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, size_t uSize, uint64_t uNum)
+void QCBOREncode_AddType7_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, size_t uSize, uint64_t uNum)
{
- AddLabelAndOptionalTag(me, szLabel, nLabel, uTag);
+ AddLabel(me, szLabel, nLabel);
if(!me->uError) {
// This function call takes care of endian swapping for the float / double
InsertEncodedTypeAndNumber(me,
- CBOR_MAJOR_TYPE_SIMPLE, // The major type for floats and doubles
- uSize, // min size / tells encoder to do it right
- uNum, // Bytes of the floating point number as a uint
+ CBOR_MAJOR_TYPE_SIMPLE, // The major type for
+ // floats and doubles
+ uSize, // min size / tells
+ // encoder to do it right
+ uNum, // Bytes of the floating
+ // point number as a uint
UsefulOutBuf_GetEndPosition(&(me->OutBuf))); // end position for append
me->uError = Nesting_Increment(&(me->nesting), 1);
@@ -562,83 +565,51 @@
/*
- Public function for adding simple values. See header qcbor.h
- */
-void QCBOREncode_AddRawSimple_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, uint8_t uSimple)
-{
- AddSimpleInternal(me, szLabel, nLabel, uTag, 0, uSimple);
-}
-
-
-/*
- Public function for adding simple values. See header qcbor.h
- */
-void QCBOREncode_AddSimple_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, uint8_t uSimple)
-{
- if(uSimple < CBOR_SIMPLEV_FALSE || uSimple > CBOR_SIMPLEV_UNDEF) {
- me->uError = QCBOR_ERR_BAD_SIMPLE;
- } else {
- QCBOREncode_AddRawSimple_3(me, szLabel, nLabel, uTag, uSimple);
- }
-}
-
-
-/*
Public functions for floating point numbers. See header qcbor.h
*/
-void QCBOREncode_AddFloat_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, float fNum)
-{
- // Convert the *type* of the data from a float to a uint so the
- // standard integer encoding can work. This takes advantage
- // of CBOR's indicator for a float being the same as for a 4
- // byte integer too.
- const float *pfNum = &fNum;
- const uint32_t uNum = *(uint32_t *)pfNum;
-
- AddSimpleInternal(me, szLabel, nLabel, uTag, sizeof(float), uNum);
+void QCBOREncode_AddFloat_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, float fNum)
+{
+ QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(float), UsefulBufUtil_CopyFloatToUint32(fNum));
}
-void QCBOREncode_AddDouble_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, double dNum)
+void QCBOREncode_AddDouble_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, double dNum)
{
- // see how it is done for floats above
- const double *pdNum = &dNum;
- const uint64_t uNum = *(uint64_t *)pdNum;
-
- AddSimpleInternal(me, szLabel, nLabel, uTag, sizeof(double), uNum);
+ QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(double), UsefulBufUtil_CopyDoubleToUint64(dNum));
}
-void QCBOREncode_AddFloatAsHalf_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, float fNum)
+void QCBOREncode_AddFloatAsHalf_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, float fNum)
{
- AddSimpleInternal(me, szLabel, nLabel, uTag, sizeof(uint16_t), IEEE754_FloatToHalf(fNum));
+ QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(uint16_t), IEEE754_FloatToHalf(fNum));
}
-static void QCBOREncode_AddFUnionAsSmallest_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, IEEE754_union uNum)
+static void QCBOREncode_AddFUnionAsSmallest_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, IEEE754_union uNum)
{
switch(uNum.uTag) {
case IEEE754_UNION_IS_HALF:
- AddSimpleInternal(me, szLabel, nLabel, uTag, sizeof(uint16_t), uNum.u16);
+ QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(uint16_t), uNum.u16);
break;
case IEEE754_UNION_IS_SINGLE:
- AddSimpleInternal(me, szLabel, nLabel, uTag, sizeof(uint32_t), uNum.u32);
+ QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(uint32_t), uNum.u32);
break;
case IEEE754_UNION_IS_DOUBLE:
- AddSimpleInternal(me, szLabel, nLabel, uTag, sizeof(uint64_t), uNum.u64);
+ QCBOREncode_AddType7_2(me, szLabel, nLabel, sizeof(uint64_t), uNum.u64);
break;
}
}
-void QCBOREncode_AddFloatAsSmallest_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, float fNum)
+void QCBOREncode_AddFloatAsSmallest_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, float fNum)
{
- QCBOREncode_AddFUnionAsSmallest_3(me, szLabel, nLabel, uTag, IEEE754_FloatToSmallest(fNum));
+ QCBOREncode_AddFUnionAsSmallest_2(me, szLabel, nLabel, IEEE754_FloatToSmallest(fNum));
}
-void QCBOREncode_AddDoubleAsSmallest_3(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, uint64_t uTag, double dNum)
+void QCBOREncode_AddDoubleAsSmallest_2(QCBOREncodeContext *me, const char *szLabel, int64_t nLabel, double dNum)
{
- QCBOREncode_AddFUnionAsSmallest_3(me, szLabel, nLabel, uTag, IEEE754_DoubleToSmallest(dNum));
+ QCBOREncode_AddFUnionAsSmallest_2(me, szLabel, nLabel, IEEE754_DoubleToSmallest(dNum));
}
+
/*
Public functions to finish and get the encoded result. See header qcbor.h
*/
@@ -654,10 +625,11 @@
if(UsefulOutBuf_GetError(&(me->OutBuf))) {
// Stuff didn't fit in the buffer.
- // This check catches this condition for all the appends and inserts so checks aren't needed
- // when the appends and inserts are performed. And of course UsefulBuf will never
- // overrun the input buffer given to it. No complex analysis of the error handling
- // in this file is needed to know that is true. Just read the UsefulBuf code.
+ // This check catches this condition for all the appends and inserts
+ // so checks aren't needed when the appends and inserts are performed.
+ // And of course UsefulBuf will never overrun the input buffer given
+ // to it. No complex analysis of the error handling in this file is
+ // needed to know that is true. Just read the UsefulBuf code.
me->uError = QCBOR_ERR_BUFFER_TOO_SMALL;
goto Done;
}
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index 50531c2..8da104f 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -72,13 +72,13 @@
//#define PRINT_FUNCTIONS_FOR_DEBUGGINGXX
-#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
+#if PRINT_FUNCTIONS_FOR_DEBUGGINGXX
#include <stdio.h>
// ifdef these out to not have compiler warnings
static void printencoded(const uint8_t *pEncoded, size_t nLen)
{
- int i;
+ size_t i;
for(i = 0; i < nLen; i++) {
uint8_t Z = pEncoded[i];
printf("%02x ", Z);
@@ -91,10 +91,10 @@
// Do the comparison and print out where it fails
static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
- int i;
+ size_t i;
for(i = 0; i < U1.len; i++) {
if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
- printf("%d 0x%x 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
+ printf("Position: %d Actual: 0x%x Expected: 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
return 1;
}
}
@@ -508,8 +508,10 @@
QCBOREncode_OpenArray(&ECtx);
// Non-map ints
- QCBOREncode_AddUInt64_3(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 100, 89989909);
- QCBOREncode_AddInt64_3(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 76, 77689989909);
+ QCBOREncode_AddTag(&ECtx, 100);
+ QCBOREncode_AddUInt64_2(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 89989909);
+ QCBOREncode_AddTag(&ECtx, 76);
+ QCBOREncode_AddInt64_2(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 77689989909);
QCBOREncode_AddUInt64(&ECtx,0);
QCBOREncode_AddInt64(&ECtx, -44);
@@ -522,8 +524,10 @@
QCBOREncode_CloseMap(&ECtx);
// floats and doubles
- QCBOREncode_AddFloat_3(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 88, 3.14159);
- QCBOREncode_AddDouble_3(&ECtx, "Street", QCBOR_NO_INT_LABEL, 99, 8.654309);
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddFloat_2(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 3.14159);
+ QCBOREncode_AddTag(&ECtx, 99);
+ QCBOREncode_AddDouble_2(&ECtx, "Street", QCBOR_NO_INT_LABEL, 8.654309);
QCBOREncode_AddFloat(&ECtx, 1);
QCBOREncode_AddDouble(&ECtx, 1);
@@ -550,7 +554,8 @@
// binary blobs in maps
QCBOREncode_OpenMap(&ECtx);
- QCBOREncode_AddBytes_3(&ECtx, "binbin", QCBOR_NO_INT_LABEL, 100000, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
+ QCBOREncode_AddTag(&ECtx, 100000);
+ QCBOREncode_AddBytes_2(&ECtx, "binbin", QCBOR_NO_INT_LABEL, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3}));
QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
QCBOREncode_CloseMap(&ECtx);
@@ -566,8 +571,9 @@
// text blobs in maps
QCBOREncode_OpenMap(&ECtx);
QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
- QCBOREncode_AddText_3(&ECtx, "____", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, UsefulBuf_FROM_SZ_LITERAL("foo bar"));
- QCBOREncode_AddSZString_3(&ECtx, "()()()", QCBOR_NO_INT_LABEL, 1000, "rab rab oof");
+ QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar")); // TODO _2?
+ QCBOREncode_AddTag(&ECtx, 1000);
+ QCBOREncode_AddSZString_2(&ECtx, "()()()", QCBOR_NO_INT_LABEL, "rab rab oof");
QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
@@ -591,7 +597,8 @@
// true / false ...
QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
QCBOREncode_OpenMap(&ECtx);
- QCBOREncode_AddSimple_3(&ECtx, "dare", QCBOR_NO_INT_LABEL, 66, CBOR_SIMPLEV_TRUE);
+ QCBOREncode_AddTag(&ECtx, 66);
+ QCBOREncode_AddSimple_2(&ECtx, "dare", QCBOR_NO_INT_LABEL, CBOR_SIMPLEV_TRUE);
QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE);
QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
QCBOREncode_CloseMap(&ECtx);
@@ -602,7 +609,8 @@
// opening arrays in a map
QCBOREncode_OpenMap(&ECtx);
- QCBOREncode_OpenArray_3(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL, 1093);
+ QCBOREncode_AddTag(&ECtx, 1093);
+ QCBOREncode_OpenArray_2(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL);
QCBOREncode_CloseArray(&ECtx);
QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
QCBOREncode_CloseArray(&ECtx);
@@ -614,7 +622,8 @@
QCBOREncode_OpenMap(&ECtx);
QCBOREncode_OpenMapInMap(&ECtx, "in a map");
QCBOREncode_OpenMapInMapN(&ECtx, 5556);
- QCBOREncode_OpenMap_3(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL, 9087);
+ QCBOREncode_AddTag(&ECtx, 9087);
+ QCBOREncode_OpenMap_2(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL);
QCBOREncode_CloseMap(&ECtx);
QCBOREncode_CloseMap(&ECtx);
QCBOREncode_CloseMap(&ECtx);
@@ -622,11 +631,15 @@
// Extended simple values (these are not standard...)
QCBOREncode_OpenMap(&ECtx);
- QCBOREncode_AddRawSimple_3(&ECtx, "s1", QCBOR_NO_INT_LABEL, 88, 255);
- QCBOREncode_AddRawSimple_3(&ECtx, "s2", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 0);
- QCBOREncode_AddRawSimple_3(&ECtx, "s3", QCBOR_NO_INT_LABEL, 88, 33);
- QCBOREncode_AddRawSimple_3(&ECtx, NULL, 88378374, 88, 255);
- QCBOREncode_AddRawSimple_3(&ECtx, NULL, 89, 88, 19);
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddType7_2(&ECtx, "s1", QCBOR_NO_INT_LABEL, 0, 255);
+ QCBOREncode_AddType7_2(&ECtx, "s2", QCBOR_NO_INT_LABEL, 0, 0);
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddType7_2(&ECtx, "s3", QCBOR_NO_INT_LABEL, 0, 33);
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddType7_2(&ECtx, NULL, 88378374, 0, 255);
+ QCBOREncode_AddTag(&ECtx, 88);
+ QCBOREncode_AddType7_2(&ECtx, NULL, 89, 0, 19);
QCBOREncode_CloseMap(&ECtx);
@@ -1831,7 +1844,8 @@
QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
// top level array for cose sign1, 18 is the tag for COSE sign
- QCBOREncode_OpenArray_3(&EC, NULL, QCBOR_NO_INT_LABEL, 18);
+ QCBOREncode_AddTag(&EC, 18); // TODO: replace with constant
+ QCBOREncode_OpenArray_2(&EC, NULL, QCBOR_NO_INT_LABEL); // TODO: _2
// Add protected headers
QCBOREncode_AddBytes(&EC, ProtectedHeaders);