Big number, big float and decimal fraction rework
New set of functions for big numbers, big floats and decimal fractions to have naming consistency. The old ones are still supported.
This is backwards compatible with QBOR v1. Previous work on v2 big numbers were not. This regains compatibility.
Offset of one for negative numbers is consistency supported for big numbers, big floats and decimal fractions.
Support for preferred serialization of big numbers including big floats and decimal fraction. No-preferred and raw options are provided too.
Prefer "BigNumber" to "bignum" and variants in function and variable naming
Add UsefulBuf_SkipLeading()
Separate definition of QCBORExpAndMantissa for readability of code.
* Big number rework (check point; not tested yet)
* Lots more reworking; tests passing (checkpoint)
* More big number rework
* Big number documentation and code tidy
* documentation and tidy
* GetNext handles 65 bit negs in exp and float...
* Fixes -- tests are passing now
* Fix disabled tags
* Check point; big function rename and fan out
* Lots of cross checking and small fixes
* Fix full test fan out
* Remove some redundant/left-over commas
* relocate expAndMantissa definition for clarity
---------
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/inc/qcbor/UsefulBuf.h b/inc/qcbor/UsefulBuf.h
index 819a0d5..24b087b 100644
--- a/inc/qcbor/UsefulBuf.h
+++ b/inc/qcbor/UsefulBuf.h
@@ -43,8 +43,10 @@
when who what, where, why
-------- ---- --------------------------------------------------
+ 08/31/2024 llundblade Add UsefulBufC_NTH_BYTE().
08/14/2024 llundblade Add UsefulOutBuf_RetrieveOutputStorage().
08/13/2024 llundblade Add UsefulInputBuf_RetrieveUndecodedInput().
+ 8/10/2024 llundblade Add UsefulBuf_SkipLeading().
08/08/2024 llundblade Add UsefulOutBuf_SubString().
10/05/2024 llundblade Add Xxx_OffsetToPointer.
28/02/2024 llundblade Rearrange UsefulOutBuf_Compare().
@@ -496,6 +498,10 @@
static inline UsefulBufC UsefulBuf_FromSZ(const char *szString);
+/* Get the nth byte from a UsefulBufC. There's no length check! */
+#define UsefulBufC_NTH_BYTE(UBC, n) (((const uint8_t *)(UBC.ptr))[n])
+
+
/**
* @brief Copy one @ref UsefulBuf into another at an offset.
*
@@ -656,6 +662,17 @@
/**
+ * @brief Skip leading bytes of a particular value in a string.
+ *
+ * @param[in] String The input string. String.ptr must not be @c NULL.
+ * @param[in] uByte The byte value.
+ *
+ * @return Substring with leading bytes with value @c uByte removed.
+ */
+UsefulBufC UsefulBuf_SkipLeading(UsefulBufC String, uint8_t uByte);
+
+
+/**
* @brief Convert a pointer to an offset with bounds checking.
*
* @param[in] UB A UsefulBuf.
diff --git a/inc/qcbor/qcbor_common.h b/inc/qcbor/qcbor_common.h
index b74a97d..b4ac5d5 100644
--- a/inc/qcbor/qcbor_common.h
+++ b/inc/qcbor/qcbor_common.h
@@ -123,9 +123,9 @@
#define CBOR_TAG_DATE_STRING 0
/** See QCBOREncode_AddTDateEpoch(). */
#define CBOR_TAG_DATE_EPOCH 1
-/** See QCBOREncode_AddTPositiveBignum(). */
+/** See QCBOREncode_AddTBigNumber(). */
#define CBOR_TAG_POS_BIGNUM 2
-/** See QCBOREncode_AddTNegativeBignum(). */
+/** See QCBOREncode_AddTBigNumber(). */
#define CBOR_TAG_NEG_BIGNUM 3
/** CBOR tag for a two-element array representing a fraction with a
* mantissa and base-10 scaling factor. See
diff --git a/inc/qcbor/qcbor_decode.h b/inc/qcbor/qcbor_decode.h
index e0781a9..6ae57c4 100644
--- a/inc/qcbor/qcbor_decode.h
+++ b/inc/qcbor/qcbor_decode.h
@@ -276,7 +276,7 @@
#define QCBOR_TYPE_TEXT_STRING 7
/** Type for a positive big number. Data is in @c val.bignum, a
- * pointer and a length. See QCBORDecode_BignumPreferred(). */
+ * pointer and a length. See QCBORDecode_ProcessBigNumber(). */
#define QCBOR_TYPE_POSBIGNUM 9
/** Type for a negative big number. Data is in @c val.bignum, a
@@ -288,7 +288,7 @@
* avoids storage allocation for the most part. For example, if 1 is
* subtraced from a negative big number that is the two bytes 0xff
* 0xff, the result would be 0x01 0x00 0x00, one byte longer than
- * what was received. See QCBORDecode_BignumPreferred(). */
+ * what was received. See QCBORDecode_ProcessBigNumber(). */
#define QCBOR_TYPE_NEGBIGNUM 10
/** Type for [RFC 3339] (https://tools.ietf.org/html/rfc3339) date
@@ -312,34 +312,55 @@
#define QCBOR_TYPE_UKNOWN_SIMPLE 13
/** A decimal fraction made of decimal exponent and integer mantissa.
- * See @ref expAndMantissa and QCBOREncode_AddDecimalFraction(). */
+ * See @ref expAndMantissa and QCBOREncode_AddTDecimalFraction(). */
#define QCBOR_TYPE_DECIMAL_FRACTION 14
/** A decimal fraction made of decimal exponent and positive big
* number mantissa. See @ref expAndMantissa and
- * QCBOREncode_AddDecimalFractionBigNum(). */
+ * QCBOREncode_AddTDecimalFractionBigMantissa(). */
#define QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM 15
/** A decimal fraction made of decimal exponent and negative big
* number mantissa. See @ref expAndMantissa and
- * QCBOREncode_AddDecimalFractionBigNum(). */
+ * QCBOREncode_AddTDecimalFractionBigMantissa(). */
#define QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM 16
+/** A decimal fraction made of decimal exponent and positive
+ * uint64_t . See QCBOREncode_AddTDecimalFractionBigMantissa(). */
+#define QCBOR_TYPE_DECIMAL_FRACTION_POS_U64 79
+
+/** A decimal fraction made of decimal exponent and negative big
+ * number mantissa. See @ref expAndMantissa and
+ * QCBOREncode_AddTDecimalFractionBigMantissa(). */
+#define QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64 80
+
/** A floating-point number made of base-2 exponent and integer
* mantissa. See @ref expAndMantissa and
- * QCBOREncode_AddBigFloat(). */
+ * QCBOREncode_AddTBigFloat(). */
#define QCBOR_TYPE_BIGFLOAT 17
/** A floating-point number made of base-2 exponent and positive big
* number mantissa. See @ref expAndMantissa and
- * QCBOREncode_AddBigFloatBigNum(). */
+ * QCBOREncode_AddTBigFloatBigMantissa(). */
+// TODO: rename to BIGMANTISSA?
#define QCBOR_TYPE_BIGFLOAT_POS_BIGNUM 18
/** A floating-point number made of base-2 exponent and negative big
* number mantissa. See @ref expAndMantissa and
- * QCBOREncode_AddBigFloatBigNum(). */
+ * QCBOREncode_AddTBigFloatBigMantissa(). */
#define QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM 19
+/** A floating-point number made of base-2 exponent and positive big
+ * number mantissa. See @ref expAndMantissa and
+ * QCBOREncode_AddTBigFloatBigMantissa(). */
+// TODO: rename to U64MANTISSA
+#define QCBOR_TYPE_BIGFLOAT_POS_U64 82
+
+/** A floating-point number made of base-2 exponent and negative big
+ * number mantissa. See @ref expAndMantissa and
+ * QCBOREncode_AddTBigFloatBigMantissa(). */
+#define QCBOR_TYPE_BIGFLOAT_NEG_U64 83
+
/** Type for the simple value false. */
#define QCBOR_TYPE_FALSE 20
@@ -417,6 +438,8 @@
* @c val.epochDays */
#define QCBOR_TYPE_DAYS_EPOCH 78
+/* 79, 80, 82, 83 is used above for decimal fraction and big float */
+
#define QCBOR_TYPE_TAG_NUMBER 127 /* Used internally; never returned */
@@ -436,6 +459,50 @@
/**
+ * @anchor expAndMantissa
+ *
+ * This holds the value for big floats and decimal fractions, as an
+ * exponent and mantissa. For big floats the base for exponentiation
+ * is 2. For decimal fractions it is 10. Whether an instance is a big
+ * float or decimal fraction is known by context, usually by @c uDataType
+ * in @ref QCBORItem which might be @ref QCBOR_TYPE_DECIMAL_FRACTION,
+ * @ref QCBOR_TYPE_BIGFLOAT, ...
+ *
+ * The mantissa may be an @c int64_t or a big number. This is again
+ * determined by context, usually @c uDataType in @ref QCBORItem which
+ * might be @ref QCBOR_TYPE_DECIMAL_FRACTION,
+ * @ref QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, ... The sign of the
+ * big number also comes from the context
+ * (@ref QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
+ * @ref QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,...).
+ *
+ * @c bigNum is big endian or network byte order. The most significant
+ * byte is first.
+ *
+ * When @c Mantissa is @c int64_t, it represents the true value of the
+ * mantissa with the offset of 1 for CBOR negative values
+ * applied. When it is a negative big number
+ * (@ref QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM or
+ * @ref QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM), the offset of 1 has NOT been
+ * applied (doing so requires somewhat complex big number arithmetic
+ * and may increase the length of the big number). To get the correct
+ * value @c bigNum must be incremented by one before use.
+ *
+ * Also see QCBOREncode_AddTDecimalFraction(),
+ * QCBOREncode_AddTBigFloat(), QCBOREncode_AddTDecimalFractionBigNum()
+ * and QCBOREncode_AddTBigFloatBigNum().
+ */
+typedef struct {
+ int64_t nExponent;
+ union {
+ int64_t nInt;
+ uint64_t uInt;
+ UsefulBufC bigNum;
+ } Mantissa;
+} QCBORExpAndMantissa;
+
+
+/**
* This holds a decoded data item. It is returned by the
* QCBORDecode_GetNext(), the principle decoding function.
* It holds the type, value, label, tags and other details
@@ -527,45 +594,12 @@
/** See @ref QCBOR_TYPE_UKNOWN_SIMPLE */
uint8_t uSimple;
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
- /**
- * @anchor expAndMantissa
- *
- * This holds the value for big floats and decimal fractions.
- * The use of the fields in this structure depends on @c
- * uDataType.
- *
- * When @c uDataType indicates a decimal fraction, the
- * @c nExponent is base 10. When it indicates a big float, it
- * is base 2.
- *
- * When @c uDataType indicates a big number, then the @c bigNum
- * member of @c Mantissa is valid. Otherwise the @c nInt member
- * of @c Mantissa is valid.
- *
- * See @ref QCBOR_TYPE_DECIMAL_FRACTION,
- * @ref QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
- * @ref QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
- * @ref QCBOR_TYPE_BIGFLOAT, @ref QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
- * and @ref QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM.
- *
- * Also see QCBOREncode_AddTDecimalFraction(),
- * QCBOREncode_AddTBigFloat(),
- * QCBOREncode_AddTDecimalFractionBigNum() and
- * QCBOREncode_AddTBigFloatBigNum().
- */
- struct {
- int64_t nExponent;
- union {
- int64_t nInt;
- UsefulBufC bigNum;
- } Mantissa;
- } expAndMantissa;
-#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
+ QCBORExpAndMantissa expAndMantissa;
+#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
uint64_t uTagNumber;
/* For use by user-defined tag content handlers */
uint8_t userDefined[24];
-
} val;
/** Union holding the different label types selected based on @c uLabelType */
@@ -1449,8 +1483,8 @@
* @brief Decode a preferred serialization big number.
*
* @param[in] Item The number to process.
- * @param[in] BigNumBuf The buffer to output to.
- * @param[out] pBigNum The resulting big number.
+ * @param[in] BigNumberBuf The buffer to output to.
+ * @param[out] pBigNumber The resulting big number.
* @param[in,out] pbIsNegative The sign of the resulting big number.
*
* This exists to process an item that is expected to be a big number
@@ -1474,9 +1508,9 @@
* This always returns the result as a big number. The integer types 0
* and 1 are converted. Leading zeros are removed. The value 0 is
* always returned as a one-byte big number with the value 0x00.
-
- * If \c BigNumBuf is too small, \c pBigNum.ptr will be \c NULL and \c
- * pBigNum.len reports the required length. The size of \c BigNumBuf
+ *
+ * If \c BigNumberBuf is too small, \c pBigNum.ptr will be \c NULL and \c
+ * pBigNum.len reports the required length. The size of \c BigNumberBuf
* might have to be one larger than the size of the tag 2 or 3 being
* decode because of two cases. In CBOR the value of a tag 3 big
* number is -n - 1. The subtraction of one might have a carry. For
@@ -1495,6 +1529,8 @@
* This can also be used if you happen to want type 0 and type 1
* integers converted to big numbers.
*
+ * See also QCBORDecode_ProcessBigNumberNoPreferred().
+ *
* If QCBOR is being used in an environment with a full big number
* library, it may be better (less object code) to use the big number
* library than this, particularly to subtract one for tag 3.
@@ -1505,10 +1541,31 @@
* issue it brings.
*/
QCBORError
-QCBORDecode_BignumPreferred(const QCBORItem Item,
- UsefulBuf BigNumBuf,
- UsefulBufC *pBigNum,
- bool *pbIsNegative);
+QCBORDecode_ProcessBigNumber(const QCBORItem Item,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+
+/**
+ * @brief Decode a big number.
+ *
+ * @param[in] Item The number to process.
+ * @param[in] BigNumberBuf The buffer to output to.
+ * @param[out] pBigNumber The resulting big number.
+ * @param[out] pbIsNegative The sign of the resulting big number.
+ *
+ * This is the same as QCBORDecode_ProcessBigNumber(), but doesn't
+ * allow type 0 and 1 integers. It only works on tag 2 and 3 big numbers.
+ * The main work this does is handle the offset of 1 for negative big
+ * number decoding.
+ */
+QCBORError
+QCBORDecode_ProcessBigNumberNoPreferred(const QCBORItem Item,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
/**
diff --git a/inc/qcbor/qcbor_encode.h b/inc/qcbor/qcbor_encode.h
index a770768..10f3a95 100644
--- a/inc/qcbor/qcbor_encode.h
+++ b/inc/qcbor/qcbor_encode.h
@@ -1216,224 +1216,157 @@
/**
- * @brief Add a positive big number using preferred serialization.
+ * @brief Add a big number to encoded output using preferred serialization.
*
* @param[in] pCtx The encoding context.
* @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
* @ref QCBOR_ENCODE_AS_BORROWED.
* @param[in] BigNumber Pointer and length of the big number.
*
- * @c BigNumber makes up an aribtrary precision integer in
+ * @c BigNumber is in network byte order. The most significant byte is
+ * first. There is no limit to the size of @c BigNumber.
+ *
+ * This encodes preferred serialization of big numbers as defined in
+ * [RFC 8949 section 3.4.3]
+ * (https://www.rfc-editor.org/rfc/rfc8949.html#section-3.4.3).
+ * Positive values less than 2^64 are output as CBOR type 0 integers
+ * rather than as big numbers. Negative values greater than -(2^64)
+ * are output as CBOR type 1 integers. Otherwise the output is as a
+ * CBOR tag 2 or tag 3 big number.
+ *
+ * This performs the offset of 1 for the encoding of all CBOR negative
+ * numbers. To effect this some big number arithmetic is done and the
+ * byte string output may be one byte shorter than the input. For
+ * example, the negative big number 0x01 0x00 is encoded as 0xff.
+ *
+ * Leading zeros are removed.
+ *
+ * See also QCBOREncode_AddTBigNumberNoPreferred().
+ *
+ * This is fairly complex internally because of support for preferred
+ * serialization and the offset of 1 for CBOR negative values.
+ *
+ * QCBOREncode_AddTPositiveBignum() and related methods are still
+ * available but are listed as deprecated in favor of this because
+ * this does the full preferred serialization and offest of 1 for
+ * negative numbers. The methods like
+ * QCBOREncode_AddTPositiveBignum() bring in less object code because
+ * they are mostly pass-through.
+ */
+void
+QCBOREncode_AddTBigNumber(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddTBigNumberToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddTBigNumberToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber);
+
+
+/**
+ * @brief Add a big number to encoded output without preferred serialization.
+ *
+ * @param[in] pCtx The encoding context to add the UUID to.
+ * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
+ * @ref QCBOR_ENCODE_AS_BORROWED.
+ * @param[in] bNegative If true @c BigNumber is negative.
+ * @param[in] BigNumber The big number.
+ *
+ * This is the same as QCBOREncode_AddTBigNumber(), without preferred
+ * serialization. This always outputs tag 2 or 3, never type 0 or 1
+ * integers.
+ *
+ * This removes leading zeros.
+ *
+ * See also QCBOREncode_AddTBigNumber().
+ *
+ * This performs the offset of 1 for encoded CBOR negative so the
+ * internal implementation is still complicated compared to the simple
+ * copy-through in methods like QCBOREncode_AddTPositiveBignum() from
+ * QCBOR v1.
+ */
+void
+QCBOREncode_AddTBigNumberNoPreferred(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddTBigNumberNoPreferredToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddTBigNumberNoPreferredToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber);
+
+
+/**
+ * @brief Add a big number to encoded output with no processing.
+ *
+ * @param[in] pCtx The encoding context to add the UUID to.
+ * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
+ * @ref QCBOR_ENCODE_AS_BORROWED.
+ * @param[in] bNegative If true @c BigNumber is negative.
+ * @param[in] BigNumber The big number.
+ *
+ * This does NOT offset negative numbers by 1 because it
+ * does no processing. For this to encode correctly,
+ * 1 must be subtract from BigNumber before this is called
+ * if bNegative is true.
+ *
+ * See QCBOREncode_AddTBigNumber().
+ *
+ * @c BigNum makes up an aribtrary precision integer in
* network/big-endian byte order. The first byte is the most
* significant.
*
- * If the value in @c BigNumber is greater than @c UINT64_MAX, this
- * will be encoded as the CBOR tag @ref CBOR_TAG_POS_BIGNUM. If less
- * then, it will be output as a type 0 integer. This is as required
- * for preferred serialization described in [RFC 8949 section 3.4.3]
- * (https://www.rfc-editor.org/rfc/rfc8949.html#section-3.4.3).
- *
- * See also QCBOREncode_AddTPositiveBignumNoPreferred().
- *
- * In QCBOR v2, this is NOT backwards compatible with v1 by default. It
- * can be made to be compatible with QCBOREncode_Setv1Compatibility().
- * The incompatibility is that in v1 this function was a straight through
- * copy, where for v2 this will use preferred serialization to encode
- * small integers as type 1.
- *
- * Sometimes big numbers are used to represent parts of
- * cryptographic keys, however, COSE which defines representations for
- * keys does, not use this particular type.
- */
-void
-QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-
-static void
-QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-
-static void
-QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-
-
-/**
- * @brief Add a positive big number using preferred serialization.
- *
- * @param[in] pCtx The encoding context to add the big number to.
- * @param[in] BigNumber Pointer and length of the big number.
- *
- * This operates the same as QCBOREncode_AddTPositiveBignum(),
- * but always outputs the tag number. This exists for backwards
- * compatibility with an early version of QCBOR.
- *
- * In QCBOR v2, this is NOT backwards compatible with v1 by default. It
- * can be made to be compatible with QCBOREncode_Setv1Compatibility().
+ * It is encoded as CBOR major type 2, a binary string, possibly with
+ * tag @ref CBOR_TAG_NEG_BIGNUM. See [RFC 8949 section 3.4.3]
+ * (https://www.rfc-editor.org/rfc/rfc8949.html#section-3.4.3). No
+ * processing, such as removal of leading zeros or the required offset
+ * of 1 for negative values, is perfomed.
*/
static void
-QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx,
- UsefulBufC BigNumber);
+QCBOREncode_AddTBigNumberRaw(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber);
static void
-QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx,
- const char *szLabel,
- UsefulBufC BigNumber);
-
-static void
-QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
+QCBOREncode_AddTBigNumberRawToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
UsefulBufC BigNumber);
-
-/**
- * @brief Add a positive big number without preferred serialization.
- *
- * @param[in] pCtx The encoding context.
- * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
- * @ref QCBOR_ENCODE_AS_BORROWED.
- * @param[in] BigNumber Pointer and length of the big number.
- *
- * This is mostly a straight-through output of the bytes that make up the big
- * number. There is no conversion to CBOR type 0 integers for values
- * smaller than @c UINT64_MAX.
- *
- * See also QCBOREncode_AddTPositiveBignum().
- */
-void
-QCBOREncode_AddTPositiveBignumNoPreferred(QCBOREncodeContext *pCtx,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNumber);
-
-void
-QCBOREncode_AddTPositiveBignumNoPreferredToMap(QCBOREncodeContext *pCtx,
- const char *szLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNumber);
-
-void
-QCBOREncode_AddTPositiveBignumNoPreferredToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNumber);
-
-/**
- * @brief Add a negative big number using preferred serialization.
- *
- * @param[in] pCtx The encoding context to add the big number to.
- * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
- * @ref QCBOR_ENCODE_AS_BORROWED.
- * @param[in] BigNumber Pointer and length of the big number.
- *
- * This outputs @c BigNum as a type 1 if the value is greater than or
- * equal to -(2^64), and as tag 3 if less than. This is as required
- * for preferred serialization described in RFC 8949 3.4.3.
- *
- * Note that the actual encoded value is 1 more than the value of
- * BigNum as required for all encoded negative numbers in CBOR. The
- * implementation of this function does some big number arithmatic to
- * effect this.
- *
- * See also QCBOREncode_AddTNegativeBignumNoPreferred().
- *
- * B y default, this function is not comparible with QCBOR v1. In
- * QCBOR v1 this just copied @c BigNum straight through without
- * adjusting the negative number by 1 and without preferred
- * serialization. By calling QCBOREncode_Setv1Compatibility() , this
- * will go back to the v1 behavior. This behavior in v2 is more
- * correct.
- */
-void
-QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-
static void
-QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-
-static void
-QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-
-
-/**
- * @brief Add a negative big number using preferred serialization.
- *
- * @param[in] pCtx The encoding context to add the big number to.
- * @param[in] BigNumber Pointer and length of the big number.
- *
- * This operates the same as QCBOREncode_AddTNegativeBignum(),
- * but always outputs the tag number. This exists for backwards
- * compatibility with an early version of QCBOR.
- *
- * In QCBOR v2, this is NOT backwards compatible with v1 by default. It
- * can be made to be compatible with QCBOREncode_Setv1Compatibility().
- */
-static void
-QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx,
- UsefulBufC BigNumber);
-
-static void
-QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx,
- const char *szLabel,
+QCBOREncode_AddTBigNumberRawToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
UsefulBufC BigNumber);
-static void
-QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- UsefulBufC BigNumber);
-
-
-/**
- * @brief Add a negative big number without preferred serialization.
- *
- * @param[in] pCtx The encoding context to add the big number to.
- * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
- * @ref QCBOR_ENCODE_AS_BORROWED.
- * @param[in] BigNumber Pointer and length of the big number.
- *
- * This always outputs the value as tag 3 (negative big number). It doesn't
- * output numbers greater than -UINT64_MAX as type 1 integers as would
- * be done with preferred serialization.
- *
- * This is not a straight through copy of the big number. It adds one, the
- * offset used for all negative numbers in CBOR.
- *
- * If QCBOREncode_Setv1Compatibility() is set, then this does a
- * straight-through copy without adding one.
- *
- * See also QCBOREncode_AddTNegativeBignum().
- */
-void
-QCBOREncode_AddTNegativeBignumNoPreferred(QCBOREncodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-void
-QCBOREncode_AddTNegativeBignumNoPreferredToMap(QCBOREncodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-void
-QCBOREncode_AddTNegativeBignumNoPreferredToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber);
-
-
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
/**
- * @brief Add a decimal fraction to the encoded output.
+ * @brief Add a decimal fraction.
*
* @param[in] pCtx Encoding context to add the decimal fraction to.
* @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
@@ -1463,9 +1396,9 @@
* CBOR Preferred serialization of the integers is used, thus they
* will be encoded in the smallest number of bytes possible.
*
- * See also QCBOREncode_AddDecimalFractionBigNum() for a decimal
+ * See also QCBOREncode_AddTDecimalFractionBigNumber() for a decimal
* fraction with arbitrarily large precision and
- * QCBOREncode_AddBigFloat().
+ * QCBOREncode_AddTBigFloat().
*
* There is no representation of positive or negative infinity or NaN
* (Not a Number). Use QCBOREncode_AddDouble() to encode them.
@@ -1493,84 +1426,88 @@
int64_t nBase10Exponent);
-static void
-QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
- int64_t nMantissa,
- int64_t nBase10Exponent);
-
-static void
-QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
- const char *szLabel,
- int64_t nMantissa,
- int64_t nBase10Exponent);
-
-static void
-QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- int64_t nMantissa,
- int64_t nBase10Exponent);
-
/**
- * @brief Add a decimal fraction with a big number mantissa to the encoded output.
+ * @brief Add a decimal fraction with a big number mantissa..
*
* @param[in] pCtx Encoding context to add the decimal fraction to.
* @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
* @ref QCBOR_ENCODE_AS_BORROWED.
- * @param[in] Mantissa The mantissa.
+ * @param[in] Mantissa The big number mantissa.
* @param[in] bIsNegative false if mantissa is positive, true if negative.
* @param[in] nBase10Exponent The exponent.
*
- * This is the same as QCBOREncode_AddDecimalFraction() except the
- * mantissa is a big number (See QCBOREncode_AddPositiveBignum())
+ * This is the same as QCBOREncode_AddTDecimalFraction() except the
+ * mantissa is a big number (See QCBOREncode_AddTBignumber())
* allowing for arbitrarily large precision.
*
+ * Preferred serialization of the big number is used. This means it may be converted to
+ * a type 0 or type 1 integers making the result the same as QCBOREncode_AddTDecimalFraction().
+ * This also offsets negative big numbers by one.
+ *
+ * If you want the big number to be copied straight through without the conversion to type 0
+ * and 1 integers and without the offset of 1 (and much smaller objet code) use QCBOREncode_AddTBigFloatBigMantissaRaw().
+ *
* See @ref expAndMantissa for decoded representation.
*/
static void
-QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase10Exponent);
-
-static void
-QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase10Exponent);
-
-static void
-QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase10Exponent);
-
-
-static void
-QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase10Exponent);
-
-static void
-QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
- const char *szLabel,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase10Exponent);
-
-static void
-QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
+QCBOREncode_AddTDecimalFractionBigMantissa(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
UsefulBufC Mantissa,
bool bIsNegative,
int64_t nBase10Exponent);
+static void
+QCBOREncode_AddTDecimalFractionBigMantissaToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddTDecimalFractionBigMantissaToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+/**
+ * @brief Add a decimal fraction with a raw big number mantissa.
+ *
+ * @param[in] pCtx The encoding context to add the bigfloat to.
+ * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
+ * @ref QCBOR_ENCODE_AS_BORROWED.
+ * @param[in] Mantissa The mantissa.
+ * @param[in] bIsNegative false if mantissa is positive, true if negative.
+ * @param[in] nBase10Exponent The exponent.
+ *
+ * This is the same as QCBOREncode_AddTDecimalFractionBigMantissa() except the mantissa
+ * is not corrected by one and links in much less object code.
+ */static void
+QCBOREncode_AddTDecimalFractionBigMantissaRaw(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+
/**
* @brief Add a big floating-point number to the encoded output.
*
@@ -1589,12 +1526,12 @@
* which can be 8, 16, 32 or 64 bits. With both the mantissa and
* exponent 64 bits they can express more precision and a larger range
* than an IEEE double floating-point number. See
- * QCBOREncode_AddBigFloatBigNum() for even more precision.
+ * QCBOREncode_AddTBigFloatBigMantissa() for even more precision.
*
* For example, 1.5 would be represented by a mantissa of 3 and an
* exponent of -1.
*
- * The exponent and mantissa have the range from @c INT64_MIN to
+ * The exponent has a range from @c INT64_MIN to
* @c INT64_MAX for both encoding and decoding (CBOR allows @c
* -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't
* support this range to reduce code size and interface complexity a
@@ -1629,26 +1566,8 @@
int64_t nBase2Exponent);
-static void
-QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx,
- int64_t nMantissa,
- int64_t nBase2Exponent);
-
-static void
-QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
- const char *szLabel,
- int64_t nMantissa,
- int64_t nBase2Exponent);
-
-static void
-QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- int64_t nMantissa,
- int64_t nBase2Exponent);
-
/**
- * @brief Add a big floating-point number with a big number mantissa to
- * the encoded output.
+ * @brief Add a big floating-point number with a big number mantissa.
*
* @param[in] pCtx The encoding context to add the bigfloat to.
* @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
@@ -1657,55 +1576,78 @@
* @param[in] bIsNegative false if mantissa is positive, true if negative.
* @param[in] nBase2Exponent The exponent.
*
- * This is the same as QCBOREncode_AddBigFloat() except the mantissa
- * is a big number (See QCBOREncode_AddPositiveBignum()) allowing for
+ * This is the same as QCBOREncode_AddTBigFloat() except the mantissa
+ * is a big number (See QCBOREncode_AddTBigMantissa()) allowing for
* arbitrary precision.
*
+ *The big number will be offset by 1 if negative and preferred serialization will be used (tag 0 and 1).
+ *
+ * If you want the big number to be copied straight through without the conversion to type 0
+ * and 1 integers and without the offset of 1 (and much smaller objet code) use QCBOREncode_AddTBigFloatBigMantissa().
+ *
* See @ref expAndMantissa for decoded representation.
*/
static void
-QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase2Exponent);
-
-static void
-QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase2Exponent);
-
-static void
-QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase2Exponent);
-
-
-static void
-QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase2Exponent);
-
-static void
-QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
- const char *szLabel,
- UsefulBufC Mantissa,
- bool bIsNegative,
- int64_t nBase2Exponent);
-
-static void
-QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
- int64_t nLabel,
+QCBOREncode_AddTBigFloatBigMantissa(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
UsefulBufC Mantissa,
bool bIsNegative,
int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddTBigFloatBigMantissaToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddTBigFloatBigMantissaToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+
+/**
+ * @brief Add a big floating-point number with a big number mantissa.
+ *
+ * @param[in] pCtx The encoding context to add the bigfloat to.
+ * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
+ * @ref QCBOR_ENCODE_AS_BORROWED.
+ * @param[in] Mantissa The mantissa.
+ * @param[in] bIsNegative false if mantissa is positive, true if negative.
+ * @param[in] nBase2Exponent The exponent.
+ *
+ * This is the same as QCBOREncode_AddTBigFloatBigMantissa() except the mantissa
+ * is not corrected by one and links in much less object code.
+ */
+static void
+QCBOREncode_AddTBigFloatBigMantissaRaw(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+
+static void
+QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddTBigFloatBigMantissaRawToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
@@ -2766,15 +2708,215 @@
uint64_t uNumber);
+/* ================ Deprecated ============================ */
-/* DEPRECATED */
-
-/* Use QCBOREncode_AddTagNumber() instead */
+/* Deprecated. Use QCBOREncode_AddTagNumber() instead */
static void
QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag);
+/* Deprecated. Use QCBOREncode_AddTBigNumberRaw() instead */
+static void
+QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
+
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRaw() instead */
+static void
+QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ UsefulBufC BigNumber);
+
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRaw() instead */
+static void
+QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
+
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRaw() instead */
+static void
+QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ UsefulBufC BigNumber);
+
+static void
+QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ UsefulBufC BigNumber);
+
+
+#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
+/* Deprecated. Use QCBOREncode_AddTDecimalFraction() instead */
+static void
+QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
+ int64_t nMantissa,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ int64_t nMantissa,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ int64_t nMantissa,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigMantissaRaw() instead */
+static void
+QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigMantissaRaw() instead */
+static void
+QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+static void
+QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+
+/* Deprecated. Use QCBOREncode_AddTBigFloat() instead. */
+static void
+QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx,
+ int64_t nMantissa,
+ int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ int64_t nMantissa,
+ int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ int64_t nMantissa,
+ int64_t nBase2Exponent);
+
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigMantissaRaw() instead */
+static void
+QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigMantissaRaw() instead */
+static void
+QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+static void
+QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+#endif /* ! QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
+
+
/* =========================================================================
BEGINNING OF PRIVATE IMPLEMENTATION
========================================================================= */
@@ -2829,14 +2971,31 @@
/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
void
-QCBOREncode_Private_AddExpMantissa(QCBOREncodeContext *pCtx,
- uint64_t uTag,
- int64_t nExponent,
- UsefulBufC BigNumMantissa,
- bool bBigNumIsNegative,
- int64_t nMantissa);
+QCBOREncode_Private_AddTExpIntMantissa(QCBOREncodeContext *pMe,
+ const int uTagRequirement,
+ const uint64_t uTagNumber,
+ const int64_t nExponent,
+ const int64_t nMantissa);
+/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
+void
+QCBOREncode_Private_AddTExpBigMantissa(QCBOREncodeContext *pMe,
+ const int uTagRequirement,
+ const uint64_t uTagNumber,
+ const int64_t nExponent,
+ const UsefulBufC BigNumMantissa,
+ const bool bBigNumIsNegative);
+
+
+/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
+void
+QCBOREncode_Private_AddTExpBigMantissaRaw(QCBOREncodeContext *pMe,
+ const int uTagRequirement,
+ const uint64_t uTagNumber,
+ const int64_t nExponent,
+ const UsefulBufC BigNumMantissa,
+ const bool bBigNumIsNegative);
/**
* @brief Semi-private method to add simple items and floating-point.
@@ -3359,128 +3518,222 @@
}
+static inline void
+QCBOREncode_AddTBigNumberToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber)
+{
+ QCBOREncode_AddSZString(pMe, szLabel);
+ QCBOREncode_AddTBigNumber(pMe, uTagRequirement, bNegative, BigNumber);
+}
static inline void
+QCBOREncode_AddTBigNumberToMapN(QCBOREncodeContext *pMe,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber)
+{
+ QCBOREncode_AddInt64(pMe, nLabel);
+ QCBOREncode_AddTBigNumber(pMe, uTagRequirement, bNegative, BigNumber);
+}
+
+static inline void
+QCBOREncode_AddTBigNumberNoPreferredToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber)
+{
+ QCBOREncode_AddSZString(pMe, szLabel);
+ QCBOREncode_AddTBigNumberNoPreferred(pMe, uTagRequirement, bNegative, BigNumber);
+}
+
+static inline void
+QCBOREncode_AddTBigNumberNoPreferredToMapN(QCBOREncodeContext *pMe,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ bool bNegative,
+ UsefulBufC BigNumber)
+{
+ QCBOREncode_AddInt64(pMe, nLabel);
+ QCBOREncode_AddTBigNumberNoPreferred(pMe, uTagRequirement, bNegative, BigNumber);
+}
+
+/*
+ * @brief Add the tag number for a big number (private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] uTagRequirement
+ * @param[in] bNegative If true, big number is negative.
+ */
+static inline void
+QCBOREncode_Private_BigNumberTag(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ bool bNegative)
+{
+ if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
+ QCBOREncode_AddTag(pMe, bNegative ? CBOR_TAG_NEG_BIGNUM : CBOR_TAG_POS_BIGNUM);
+ }
+}
+
+static inline void
+QCBOREncode_AddTBigNumberRaw(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ bool bNegative,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_Private_BigNumberTag(pMe, uTagRequirement, bNegative);
+ QCBOREncode_AddBytes(pMe, BigNumber);
+}
+
+static inline void
+QCBOREncode_AddTBigNumberRawToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ bool bNegative,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddSZString(pMe, szLabel);
+ QCBOREncode_AddTBigNumberRaw(pMe, uTagRequirement, bNegative, BigNumber);
+}
+
+
+static inline void
+QCBOREncode_AddTBigNumberRawToMapN(QCBOREncodeContext *pMe,
+ int64_t nLabel,
+ const uint8_t uTagRequirement,
+ bool bNegative,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddInt64(pMe, nLabel);
+ QCBOREncode_AddTBigNumberRaw(pMe, uTagRequirement, bNegative, BigNumber);
+}
+
+
+static inline void /* Deprecated */
+QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRaw(pMe, uTagRequirement, false, BigNumber);
+}
+
+static inline void /* Deprecated */
QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pMe,
const char *szLabel,
const uint8_t uTagRequirement,
- const UsefulBufC Bytes)
+ const UsefulBufC BigNumber)
{
- QCBOREncode_AddSZString(pMe, szLabel);
- QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
+ QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, uTagRequirement, false, BigNumber);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const uint8_t uTagRequirement,
- const UsefulBufC Bytes)
+ const UsefulBufC BigNumber)
{
- QCBOREncode_AddInt64(pMe, nLabel);
- QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
+ QCBOREncode_AddTBigNumberRawToMapN(pMe, nLabel, uTagRequirement, false, BigNumber);
}
-static inline void
-QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
+static inline void /* Deprecated */
+QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, const UsefulBufC BigNumber)
{
- QCBOREncode_AddTPositiveBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
+ QCBOREncode_AddTBigNumberRaw(pMe, QCBOR_ENCODE_AS_TAG, false, BigNumber);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe,
const char *szLabel,
- const UsefulBufC Bytes)
+ const UsefulBufC BigNumber)
{
- QCBOREncode_AddTPositiveBignumToMapSZ(pMe,
- szLabel,
- QCBOR_ENCODE_AS_TAG,
- Bytes);
+ QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, false, BigNumber);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
- const UsefulBufC Bytes)
+ const UsefulBufC BigNumber)
{
- QCBOREncode_AddTPositiveBignumToMapN(pMe,
- nLabel,
- QCBOR_ENCODE_AS_TAG,
- Bytes);
+ QCBOREncode_AddTBigNumberRawToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, false, BigNumber);
}
+static inline void /* Deprecated */
+QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRaw(pMe, uTagRequirement, true, BigNumber);
+}
-
-
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe,
const char *szLabel,
const uint8_t uTagRequirement,
- const UsefulBufC Bytes)
+ const UsefulBufC BigNumber)
{
- QCBOREncode_AddSZString(pMe, szLabel);
- QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
+ QCBOREncode_AddTBigNumberRawToMapSZ(pMe,
+ szLabel,
+ uTagRequirement,
+ true,
+ BigNumber);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const uint8_t uTagRequirement,
- const UsefulBufC Bytes)
+ const UsefulBufC BigNumber)
{
- QCBOREncode_AddInt64(pMe, nLabel);
- QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
+ QCBOREncode_AddTBigNumberRawToMapN(pMe,
+ nLabel,
+ uTagRequirement,
+ true,
+ BigNumber);
}
-static inline void
-QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
+static inline void /* Deprecated */
+QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, const UsefulBufC BigNumber)
{
- QCBOREncode_AddTNegativeBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
+ QCBOREncode_AddTBigNumberRaw(pMe, QCBOR_ENCODE_AS_TAG, true, BigNumber);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe,
const char *szLabel,
- const UsefulBufC Bytes)
+ const UsefulBufC BigNumber)
{
- QCBOREncode_AddTNegativeBignumToMapSZ(pMe,
- szLabel,
- QCBOR_ENCODE_AS_TAG,
- Bytes);
+ QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, true, BigNumber);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
- const UsefulBufC Bytes)
+ const UsefulBufC BigNumber)
{
- QCBOREncode_AddTNegativeBignumToMapN(pMe,
- nLabel,
- QCBOR_ENCODE_AS_TAG,
- Bytes);
+ QCBOREncode_AddTBigNumberRawToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, true, BigNumber);
+
}
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+
static inline void
QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pMe,
const uint8_t uTagRequirement,
const int64_t nMantissa,
const int64_t nBase10Exponent)
{
- uint64_t uTag;
- if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- uTag = CBOR_TAG_DECIMAL_FRACTION;
- } else {
- uTag = CBOR_TAG_INVALID64;
- }
- QCBOREncode_Private_AddExpMantissa(pMe,
- uTag,
- nBase10Exponent,
- NULLUsefulBufC,
- false,
- nMantissa);
+ QCBOREncode_Private_AddTExpIntMantissa(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ nBase10Exponent,
+ nMantissa);
}
static inline void
@@ -3511,7 +3764,7 @@
nBase10Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe,
const int64_t nMantissa,
const int64_t nBase10Exponent)
@@ -3522,7 +3775,7 @@
nBase10Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe,
const char *szLabel,
const int64_t nMantissa,
@@ -3535,7 +3788,7 @@
nBase10Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const int64_t nMantissa,
@@ -3549,29 +3802,120 @@
}
+static inline void
+QCBOREncode_AddTDecimalFractionBigMantissa(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_Private_AddTExpBigMantissa(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ nBase10Exponent,
+ Mantissa,
+ bIsNegative);
+}
+
static inline void
+QCBOREncode_AddTDecimalFractionBigMantissaToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddSZString(pMe, szLabel);
+ QCBOREncode_AddTDecimalFractionBigMantissa(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+static inline void
+QCBOREncode_AddTDecimalFractionBigMantissaToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddInt64(pMe, nLabel);
+ QCBOREncode_AddTDecimalFractionBigMantissa(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+static inline void
+QCBOREncode_AddTDecimalFractionBigMantissaRaw(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_Private_AddTExpBigMantissaRaw(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ nBase10Exponent,
+ Mantissa,
+ bIsNegative);
+}
+
+
+static inline void
+QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddSZString(pMe, szLabel);
+ QCBOREncode_AddTDecimalFractionBigMantissaRaw(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+static inline void
+QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddInt64(pMe, nLabel);
+ QCBOREncode_AddTDecimalFractionBigMantissaRaw(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+
+
+static inline void /* Deprecated */
QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe,
const uint8_t uTagRequirement,
const UsefulBufC Mantissa,
const bool bIsNegative,
const int64_t nBase10Exponent)
{
- uint64_t uTag;
- if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- uTag = CBOR_TAG_DECIMAL_FRACTION;
- } else {
- uTag = CBOR_TAG_INVALID64;
- }
- QCBOREncode_Private_AddExpMantissa(pMe,
- uTag,
- nBase10Exponent,
- Mantissa,
- bIsNegative,
- 0);
+ QCBOREncode_AddTDecimalFractionBigMantissaRaw(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
}
-static inline void
+
+static inline void /* Deprecated */
QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
const char *szLabel,
const uint8_t uTagRequirement,
@@ -3579,15 +3923,15 @@
const bool bIsNegative,
const int64_t nBase10Exponent)
{
- QCBOREncode_AddSZString(pMe, szLabel);
- QCBOREncode_AddTDecimalFractionBigNum(pMe,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
+ QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ(pMe,
+ szLabel,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const uint8_t uTagRequirement,
@@ -3595,79 +3939,69 @@
const bool bIsNegative,
const int64_t nBase10Exponent)
{
- QCBOREncode_AddInt64(pMe, nLabel);
- QCBOREncode_AddTDecimalFractionBigNum(pMe,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
+ QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN(pMe,
+ nLabel,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe,
const UsefulBufC Mantissa,
const bool bIsNegative,
const int64_t nBase10Exponent)
{
- QCBOREncode_AddTDecimalFractionBigNum(pMe,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
+ QCBOREncode_AddTDecimalFractionBigMantissaRaw(pMe,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
const char *szLabel,
const UsefulBufC Mantissa,
const bool bIsNegative,
const int64_t nBase10Exponent)
{
- QCBOREncode_AddTDecimalFractionBigNumToMapSZ(pMe,
- szLabel,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
+ QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ(pMe,
+ szLabel,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const UsefulBufC Mantissa,
const bool bIsNegative,
- const int64_t nBase2Exponent)
+ const int64_t nBase10Exponent)
{
- QCBOREncode_AddTDecimalFractionBigNumToMapN(pMe,
- nLabel,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
+ QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN(pMe,
+ nLabel,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
}
-
-
-
static inline void
QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe,
const uint8_t uTagRequirement,
const int64_t nMantissa,
const int64_t nBase2Exponent)
{
- uint64_t uTag;
- if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- uTag = CBOR_TAG_BIGFLOAT;
- } else {
- uTag = CBOR_TAG_INVALID64;
- }
- QCBOREncode_Private_AddExpMantissa(pMe,
- uTag,
- nBase2Exponent,
- NULLUsefulBufC,
- false,
- nMantissa);
+ QCBOREncode_Private_AddTExpIntMantissa(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ nBase2Exponent,
+ nMantissa);
}
static inline void
@@ -3692,7 +4026,7 @@
QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe,
const int64_t nMantissa,
const int64_t nBase2Exponent)
@@ -3703,7 +4037,7 @@
nBase2Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe,
const char *szLabel,
const int64_t nMantissa,
@@ -3716,7 +4050,7 @@
nBase2Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const int64_t nMantissa,
@@ -3730,29 +4064,119 @@
}
+static inline void
+QCBOREncode_AddTBigFloatBigMantissa(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_Private_AddTExpBigMantissa(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ nBase2Exponent,
+ Mantissa,
+ bIsNegative);
+}
static inline void
+QCBOREncode_AddTBigFloatBigMantissaToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddSZString(pMe, szLabel);
+ QCBOREncode_AddTBigFloatBigMantissa(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+static inline void
+QCBOREncode_AddTBigFloatBigMantissaToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddInt64(pMe, nLabel);
+ QCBOREncode_AddTBigFloatBigMantissa(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+
+
+static inline void
+QCBOREncode_AddTBigFloatBigMantissaRaw(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_Private_AddTExpBigMantissaRaw(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ nBase2Exponent,
+ Mantissa,
+ bIsNegative);
+}
+
+static inline void
+QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddSZString(pMe, szLabel);
+ QCBOREncode_AddTBigFloatBigMantissaRaw(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+static inline void
+QCBOREncode_AddTBigFloatBigMantissaRawToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddInt64(pMe, nLabel);
+ QCBOREncode_AddTBigFloatBigMantissaRaw(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+
+
+static inline void /* Deprecated */
QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe,
const uint8_t uTagRequirement,
const UsefulBufC Mantissa,
const bool bIsNegative,
const int64_t nBase2Exponent)
{
- uint64_t uTag;
- if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- uTag = CBOR_TAG_BIGFLOAT;
- } else {
- uTag = CBOR_TAG_INVALID64;
- }
- QCBOREncode_Private_AddExpMantissa(pMe,
- uTag,
- nBase2Exponent,
- Mantissa,
- bIsNegative,
- 0);
+ QCBOREncode_AddTBigFloatBigMantissaRaw(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe,
const char *szLabel,
const uint8_t uTagRequirement,
@@ -3760,15 +4184,15 @@
const bool bIsNegative,
const int64_t nBase2Exponent)
{
- QCBOREncode_AddSZString(pMe, szLabel);
- QCBOREncode_AddTBigFloatBigNum(pMe,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
+ QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ(pMe,
+ szLabel,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const uint8_t uTagRequirement,
@@ -3776,55 +4200,56 @@
const bool bIsNegative,
const int64_t nBase2Exponent)
{
- QCBOREncode_AddInt64(pMe, nLabel);
- QCBOREncode_AddTBigFloatBigNum(pMe,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
+ QCBOREncode_AddTBigFloatBigMantissaRawToMapN(pMe,
+ nLabel,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe,
const UsefulBufC Mantissa,
const bool bIsNegative,
const int64_t nBase2Exponent)
{
- QCBOREncode_AddTBigFloatBigNum(pMe,
- QCBOR_ENCODE_AS_TAG,
- Mantissa, bIsNegative,
- nBase2Exponent);
+ QCBOREncode_AddTBigFloatBigMantissaRaw(pMe,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe,
const char *szLabel,
const UsefulBufC Mantissa,
const bool bIsNegative,
const int64_t nBase2Exponent)
{
- QCBOREncode_AddTBigFloatBigNumToMapSZ(pMe,
- szLabel,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
+ QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ(pMe,
+ szLabel,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
const int64_t nLabel,
const UsefulBufC Mantissa,
const bool bIsNegative,
const int64_t nBase2Exponent)
{
- QCBOREncode_AddTBigFloatBigNumToMapN(pMe,
- nLabel,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
+ QCBOREncode_AddTBigFloatBigMantissaRawToMapN(pMe,
+ nLabel,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
}
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
diff --git a/inc/qcbor/qcbor_private.h b/inc/qcbor/qcbor_private.h
index 97f5088..7e3b1b7 100644
--- a/inc/qcbor/qcbor_private.h
+++ b/inc/qcbor/qcbor_private.h
@@ -231,6 +231,7 @@
#define QCBOR_ENCODE_MODE_DCBOR 3
+// TODO: probably get rid of this
/* Operate in compatibility with QCBOR 1.0
* So far the differences are:
* - AddNegativeBigNum and AddBigFloat with a negative big num
diff --git a/inc/qcbor/qcbor_spiffy_decode.h b/inc/qcbor/qcbor_spiffy_decode.h
index 8420f20..6c9963a 100644
--- a/inc/qcbor/qcbor_spiffy_decode.h
+++ b/inc/qcbor/qcbor_spiffy_decode.h
@@ -336,7 +336,7 @@
* function encounters them they are returned as \ref
* QCBOR_TYPE_65BIT_NEG_INT. See the description of this type for
* instructions to gets its value. Also see
- * QCBORDecode_BignumPreferred().
+ * QCBORDecode_ProcessBigNumber().
*
* To give an example, the value -18446744073709551616 can't be
* represented by an int64_t or uint64_t, but can be represented by a
@@ -363,11 +363,11 @@
*
* @param[in] pCtx The decode context.
* @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
- * @param[in] BigNumBuf The buffer to write the result into.
+ * @param[in] BigNumberBuf The buffer to write the result into.
+ * @param[out] pBigNumber The output big number.
* @param[in,out] pbIsNegative Set to true if the resulting big number is negative.
- * @param[out] pBigNum The output big number.
*
- * See QCBORDecode_PreferedBigNum() in full detail.
+ * See QCBORDecode_PreferedBigNumber() in full detail.
*
* The type processing rules are as follows.
*
@@ -376,59 +376,160 @@
* uTagRequirement is. The rest of the rules pertain to what happens
* if the CBOR is not type 0 or type 1.
*
- * If uTagRequirement is REQUIRE, this will fail on anything but a
+ * If @c uTagRequirement is @ref QCBOR_TAG_REQUIREMENT_TAG, this will fail on anything but a
* full and correct tag 2 or tag 3 big number.
*
- * If uTagRequreiement is QCBOR_TAG_REQUIREMENT_NOT_A_TAG then this
+ * If @c uTagRequreiement is @ref QCBOR_TAG_REQUIREMENT_NOT_A_TAG then this
* will fail on anything but a byte string.
*
- * If QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, then this will succeed on
+ * If @ref QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, then this will succeed on
* either a byte string or a tag 2 or 3.
*
* If the item is a bare byte string, not a tag 2 or 3, then
* pbIsNegative is an input parameter that determines the sign of the
* big number. The sign must be known because the decoding of a
* positive big number is different than a negative.
- *
- * TODO: reevaluate whether to call this GetBigNum after tag redesign
- * It would be more consistent to be GetBigNum with its friend named GetBigNumNoPreferred
- * but the parameters are different so backwards compat can be with a mode bit.
*/
void
-QCBORDecode_GetBigNumPreferred(QCBORDecodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBuf BigNumBuf,
- UsefulBufC *pBigNum,
- bool *pbIsNegative);
+QCBORDecode_GetTBigNumber(QCBORDecodeContext *pCtx,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
void
-QCBORDecode_GetBigNumPreferredInMapN(QCBORDecodeContext *pCtx,
- uint8_t uTagRequirement,
- int64_t nLabel,
- UsefulBuf BigNumBuf,
- UsefulBufC *pBigNum,
+QCBORDecode_GetTBigNumberInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+void
+QCBORDecode_GetTBigNumberInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+ /**
+ * @brief Decode the next item as a big number with no processing
+ *
+ * @param[in] pCtx The decode context.
+ * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
+ * @param[out] pBigNumber The returned big number.
+ * @param[out] pbIsNegative Is @c true if the big number is negative. This
+ * is only valid when @c uTagRequirement is
+ * @ref QCBOR_TAG_REQUIREMENT_TAG.
+ *
+ * This decodes a standard CBOR big number, integer tag number of 2 or
+ * 3, or encoded CBOR that is not a tag, but borrows the content
+ * format.
+ *
+ * Please see @ref Decode-Errors-Overview "Decode Errors Overview".
+ *
+ * The big number is in network byte order. The first byte in @c
+ * pValue is the most significant byte. There may be leading zeros.
+ *
+ * This does not apply the offset of 1 to negative big numbers
+ * because it does no processing. To get the correct value
+ * one must be subtracted when the value is negative. That
+ * is one must be added to pBigNumber. Note that this
+ * operation may increase the length of pBigNumber
+ * because of the arithmetic carry. See
+ *
+ * The negative value is computed as -1 - n, where n is the postive
+ * big number in @c pValue. There is no standard representation for
+ * big numbers, positive or negative in C, so this function
+ * leaves it up to the caller to apply this computation for negative
+ * big numbers, but QCBORDecode_ProcessBigNumber() can be
+ * used too.
+ *
+ * See @ref Tag-Usage for discussion on tag requirements.
+ *
+ * Determination of the sign of the big number depends on the tag
+ * requirement of the protocol using the big number. If the protocol
+ * requires tagging, @ref QCBOR_TAG_REQUIREMENT_TAG, then the sign
+ * indication is in the protocol and @c pbIsNegative indicates the
+ * sign. If the protocol doesn't use a tag,
+ * @ref QCBOR_TAG_REQUIREMENT_NOT_A_TAG, then the protocol design must
+ * have some way of indicating the sign.
+ *
+ * See also QCBORDecode_GetInt64ConvertAll(),
+ * QCBORDecode_GetUInt64ConvertAll(),
+ * QCBORDecode_GetDoubleConvertAll() and
+ * QCBORDecode_ProcessBigNumber() which can convert big numbers.
+ *
+ * See also @ref CBOR_TAG_POS_BIGNUM, @ref CBOR_TAG_NEG_BIGNUM,
+ * QCBOREncode_AddPositiveBignum(), QCBOREncode_AddNegativeBignum(),
+ * @ref QCBOR_TYPE_POSBIGNUM and @ref QCBOR_TYPE_NEGBIGNUM.
+ *
+ * To feed the output of this to QCBORDecode_ProcessBigNumber() construct
+ * a temporary QCBORItem and feed it to QCBORDecode_ProcessBigNumber().
+ * Set the data type to @ref CBOR_TAG_POS_BIGNUM or @ref CBOR_TAG_NEG_BIGNUM,
+ * and value to @c *pValue in the QCBORItem.
+ *
+ * This is the same as QCBORDecode_GetBignum() in QCBOR v1.
+ *
+ * This links in much less object code than QCBORDecode_GetTBitNumber() and
+ * QCBORDecode_GetTBitNumberNoPreferred().
+ */
+void
+QCBORDecode_GetTBigNumberRaw(QCBORDecodeContext *pCtx,
+ const uint8_t uTagRequirement,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+void
+QCBORDecode_GetTBigNumberRawInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+void
+QCBORDecode_GetTBigNumberRawInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+/**
+ * @brief Decode next item as a big number encoded using preferred serialization.
+ *
+ * @param[in] pCtx The decode context.
+ * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
+ * @param[in] BigNumberBuf The buffer to write the result into.
+ * @param[out] pBigNumber The output big number.
+ * @param[in,out] pbIsNegative Set to true if the resulting big number is negative.
+ *
+ * This is the same as QCBORDecode_GetBigNumber(), but will error
+ * out on type 0 and 1 integers. It only succeeds on tag 2 and tag 3.
+ */
+void
+QCBORDecode_GetTBigNumberNoPreferred(QCBORDecodeContext *pCtx,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
bool *pbIsNegative);
void
-QCBORDecode_GetBigNumPreferredInMapSZ(QCBORDecodeContext *pCtx,
- uint8_t uTagRequirement,
- const char *szLabel,
- UsefulBuf BigNumBuf,
- UsefulBufC *pBigNum,
- bool *pbIsNegative);
+QCBORDecode_GetTBigNumberNoPreferredInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
-
-/*
- * Decode the next as a big number.
- *
- *
- */
void
-QCBORDecode_GetBigNumber(QCBORDecodeContext *pCtx,
- const uint8_t uTagRequirement,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative);
+QCBORDecode_GetTBigNumberNoPreferredInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
/**
@@ -797,7 +898,7 @@
* The typical way to iterate over items in an array is to call
* QCBORDecode_VGetNext() until QCBORDecode_GetError() returns
* @ref QCBOR_ERR_NO_MORE_ITEMS. Other methods like QCBORDecode_GetInt64(),
- * QCBORDecode_GetBignum() and such may also called until
+ * QCBORDecode_GetBignum() and such may also be called until
* QCBORDecode_GetError() doesn't return QCBOR_SUCCESS.
*
* Another option is to get the array item count from
@@ -1562,72 +1663,6 @@
-/**
- * @brief Decode the next item as a big number.
- *
- * @param[in] pCtx The decode context.
- * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
- * @param[out] pValue The returned big number.
- * @param[out] pbIsNegative Is @c true if the big number is negative. This
- * is only valid when @c uTagRequirement is
- * @ref QCBOR_TAG_REQUIREMENT_TAG.
- *
- * This decodes a standard CBOR big number, integer tag number of 2 or
- * 3, or encoded CBOR that is not a tag, but borrows the content
- * format.
- *
- * Please see @ref Decode-Errors-Overview "Decode Errors Overview".
- *
- * The big number is in network byte order. The first byte in @c
- * pValue is the most significant byte. There may be leading zeros.
- *
- * The negative value is computed as -1 - n, where n is the postive
- * big number in @c pValue. There is no standard representation for
- * big numbers, positive or negative in C, so this function
- * leaves it up to the caller to apply this computation for negative
- * big numbers, but QCBORDecode_BignumPreferred() can be
- * used too.
- *
- * See @ref Tag-Usage for discussion on tag requirements.
- *
- * Determination of the sign of the big number depends on the tag
- * requirement of the protocol using the big number. If the protocol
- * requires tagging, @ref QCBOR_TAG_REQUIREMENT_TAG, then the sign
- * indication is in the protocol and @c pbIsNegative indicates the
- * sign. If the protocol doesn't use a tag, @ref QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
- * then the protocol design must have some way of indicating the sign.
- *
- * See also QCBORDecode_GetInt64ConvertAll(),
- * QCBORDecode_GetUInt64ConvertAll(),
- * QCBORDecode_GetDoubleConvertAll() and QCBORDecode_BignumPreferred() which can convert big numbers.
- *
- * See also @ref CBOR_TAG_POS_BIGNUM, @ref CBOR_TAG_NEG_BIGNUM,
- * QCBOREncode_AddPositiveBignum(), QCBOREncode_AddNegativeBignum(),
- * @ref QCBOR_TYPE_POSBIGNUM and @ref QCBOR_TYPE_NEGBIGNUM.
- */
-void
-QCBORDecode_GetBignum(QCBORDecodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBufC *pValue,
- bool *pbIsNegative);
-
-void
-QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBufC *pValue,
- bool *pbIsNegative);
-
-void
-QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBufC *pValue,
- bool *pbIsNegative);
-
-
-
-
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
/**
* @brief Decode the next item as a decimal fraction.
@@ -1637,64 +1672,68 @@
* @param[out] pnMantissa The mantissa.
* @param[out] pnExponent The base 10 exponent.
*
- * This decodes a standard CBOR decimal fraction, integer tag number
- * of 4, or encoded CBOR that is not a tag, but borrows the content
- * format.
+ * The input to decode must be a decimal fraction as defined in
+ * [RFC 8949 section 3.4.4]
+ * (https://www.rfc-editor.org/rfc/rfc8949.html#section-3.4.4). That
+ * is, an array of two numbers, the first of which is the exponent and
+ * the second is the mantissa.
*
- * Please see @ref Decode-Errors-Overview "Decode Errors Overview".
+ * Depending on @c uTagRequirement, the tag number
+ * @ref CBOR_TAG_DECIMAL_FRACTION (4) may or may not need to be
+ * present before the array. See @ref Tag-Usage.
*
- * The value of this is computed by:
+ * The exponent must always be an integer (CBOR type 0 or 1). The
+ * mantissa may be an integer or a big number. If it is a big number,
+ * the tag number 2 or 3 must be present.
+ *
+ * The exponent is limited to between @c INT64_MIN and
+ * @c INT64_MAX while CBOR allows the range of @c -UINT64_MAX to @c UINT64_MAX.
+ *
+ * The mantissa is always returned as an @c int64_t. If the value
+ * won't fit, @ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will be
+ * set. Use QCBORDecode_GetTDecimalFractionBigMantissa() to avoid the
+ * limit to @c int64_t.
+ *
+ * The value of this is computed by:
*
* mantissa * ( 10 ** exponent )
*
- * In the encoded CBOR, the mantissa and exponent may be of CBOR type
- * 0 (positive integer), type 1 (negative integer), type 2 tag 2
- * (positive big number) or type 2 tag 3 (negative big number). This
- * implementation will attempt to convert all of these to an @c
- * int64_t. If the value won't fit, @ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW
- * or @ref QCBOR_ERR_BAD_EXP_AND_MANTISSA will be set.
- *
- * This implementation limits the exponent to between @c INT64_MIN and
- * @c INT64_MAX while CBOR allows the range of @c -UINT64_MAX to
- * @c UINT64_MAX.
- *
- * Various format and type issues will result in
- * @ref QCBOR_ERR_BAD_EXP_AND_MANTISSA being set.
- *
- * See @ref Tag-Usage for discussion on tag requirements.
+ * Various format and type issues will result in @ref QCBOR_ERR_BAD_EXP_AND_MANTISSA
+ * being set. See @ref Decode-Errors-Overview "Decode Errors
+ * Overview".
*
* See also QCBORDecode_GetInt64ConvertAll(),
* QCBORDecode_GetUInt64ConvertAll() and
- * QCBORDecode_GetDoubleConvertAll() which can convert big numbers.
+ * QCBORDecode_GetDoubleConvertAll() which can also decode decimal
+ * fractions.
*
* See also @ref CBOR_TAG_DECIMAL_FRACTION,
- * QCBOREncode_AddDecimalFraction(), @ref QCBOR_TYPE_DECIMAL_FRACTION
- * and QCBORDecode_GetDecimalFractionBig().
+ * QCBOREncode_AddTDecimalFraction(), @ref QCBOR_TYPE_DECIMAL_FRACTION.
*
* If QCBOR_DISABLE_TAGS is set, the only input this will decode is an
* array of two integers. It will set an error if the the array is
* preceded by by a tag number or if the mantissa is a big number.
*/
void
-QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pCtx,
- uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent);
+QCBORDecode_GetTDecimalFraction(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
void
-QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent);
-
-void
-QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
+QCBORDecode_GetTDecimalFractionInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
uint8_t uTagRequirement,
int64_t *pnMantissa,
int64_t *pnExponent);
+void
+QCBORDecode_GetTDecimalFractionInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
+
/**
* @brief Decode the next item as a decimal fraction with a big number mantissa.
@@ -1706,7 +1745,7 @@
* @param[out] pbMantissaIsNegative Is @c true if @c pMantissa is negative.
* @param[out] pnExponent The base 10 exponent.
*
- * This is the same as QCBORDecode_GetDecimalFraction() except the
+ * This is the same as QCBORDecode_GetTDecimalFraction() except the
* mantissa is returned as a big number.
*
* In the encoded CBOR, the mantissa may be a type 0 (positive
@@ -1721,34 +1760,91 @@
* fractions.
*
* See also @ref CBOR_TAG_DECIMAL_FRACTION,
- * QCBOREncode_AddDecimalFraction(), @ref QCBOR_TYPE_DECIMAL_FRACTION
- * and QCBORDecode_GetDecimalFraction().
+ * QCBOREncode_AddTDecimalFractionBigMantissa(), @ref QCBOR_TYPE_DECIMAL_FRACTION
+ * and QCBORDecode_GetTDecimalFraction().
*/
void
-QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBuf MantissaBuffer,
- UsefulBufC *pMantissa,
- bool *pbMantissaIsNegative,
- int64_t *pnExponent);
+QCBORDecode_GetTDecimalFractionBigMantissa(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
void
-QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBuf MantissaBuffer,
- UsefulBufC *pbMantissaIsNegative,
- bool *pbIsNegative,
- int64_t *pnExponent);
+QCBORDecode_GetTDecimalFractionBigMantissaInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pbMantissaIsNegative,
+ bool *pbIsNegative,
+ int64_t *pnExponent);
void
-QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBuf MantissaBuffer,
- UsefulBufC *pMantissa,
- bool *pbMantissaIsNegative,
- int64_t *pnExponent);
+QCBORDecode_GetTDecimalFractionBigMantissaInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+
+/**
+ * @brief Decode the next item as a decimal fraction with a big number mantissa raw.
+ *
+ * @param[in] pCtx The decode context.
+ * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
+ * @param[in] MantissaBuffer The buffer in which to put the mantissa.
+ * @param[out] pMantissa The big num mantissa.
+ * @param[out] pbMantissaIsNegative Is @c true if @c pMantissa is negative.
+ * @param[out] pnExponent The base 10 exponent.
+ *
+ * This is the same as QCBORDecode_GetTDecimalFractionBigMantissa() except the
+ * negative mantissas are NOT offset by one and this links in less object code.
+ *
+ * In the encoded CBOR, the mantissa may be a type 0 (positive
+ * integer), type 1 (negative integer), type 2 tag 2 (positive big
+ * number) or type 2 tag 3 (negative big number). This implementation
+ * will convert all these to a big number. The limit to this
+ * conversion is the size of @c MantissaBuffer.
+ *
+ * See also QCBORDecode_GetInt64ConvertAll(),
+ * QCBORDecode_GetUInt64ConvertAll() and
+ * QCBORDecode_GetDoubleConvertAll() which can convert decimal
+ * fractions.
+ *
+ * See also @ref CBOR_TAG_DECIMAL_FRACTION,
+ * QCBOREncode_AddTDecimalFractionBigMantissaRaw(), @ref QCBOR_TYPE_DECIMAL_FRACTION
+ * and QCBORDecode_GetTDecimalFractionBigMantissa().
+ */
+void
+QCBORDecode_GetTDecimalFractionBigMantissaRaw(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+void
+QCBORDecode_GetTDecimalFractionBigMantissaRawInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pbMantissaIsNegative,
+ bool *pbIsNegative,
+ int64_t *pnExponent);
+
+void
+QCBORDecode_GetTDecimalFractionBigMantissaRawInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+
/**
@@ -1759,45 +1855,64 @@
* @param[out] pnMantissa The mantissa.
* @param[out] pnExponent The base 2 exponent.
*
- * This decodes a standard CBOR big float, integer tag number of 5, or
- * encoded CBOR that is not a tag, but borrows the content format.
+ * The input to decode must be a big float defined in [RFC 8949 section 3.4.4]
+ * (https://www.rfc-editor.org/rfc/rfc8949.html#section-3.4.4). That
+ * is, an array of two numbers, the first of which is the exponent and
+ * the second is the mantissa.
*
- * This is the same as QCBORDecode_GetDecimalFraction() with the
- * important distinction that the value is computed by:
+ * Depending on @c uTagRequirement, the tag number
+ * @ref CBOR_TAG_BIG_FLOAT (5) may or may not need to be present
+ * before the array. See @ref Tag-Usage.
+ *
+ * The exponent must always be an integer (CBOR type 0 or 1). The
+ * mantissa may be an integer or a big number. If it is a big number,
+ * the tag number 2 or 3 must be present.
+ *
+ * This implementation limits the exponent to between @c INT64_MIN and
+ * @c INT64_MAX while CBOR allows the range of @c -UINT64_MAX to @c UINT64_MAX.
+ *
+ * The mantissa is always returned as an @c int64_t. If the value
+ * won't fit, @ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will be
+ * set. Use QCBORDecode_GetBigFloastBigNumber() to avoid the
+ * limit to @c int64_t.
*
* mantissa * ( 2 ** exponent )
*
- * If the mantissa is a tag that is a positive or negative big number,
- * this will attempt to fit it into the int64_t that @c pnMantissa is
- * and set an overflow error if it doesn't fit.
+ * Various format and type issues will result in
+ * @ref QCBOR_ERR_BAD_EXP_AND_MANTISSA being set. See
+ * @ref Decode-Errors-Overview "Decode Errors Overview".
*
* See also QCBORDecode_GetInt64ConvertAll(),
* QCBORDecode_GetUInt64ConvertAll() and
* QCBORDecode_GetDoubleConvertAll() which can convert big floats.
*
- * See also @ref CBOR_TAG_BIGFLOAT, QCBOREncode_AddBigFloat(), @ref
- * QCBOR_TYPE_BIGFLOAT and QCBORDecode_GetBigFloatBig().
+ * See also @ref CBOR_TAG_BIGFLOAT, QCBOREncode_AddTBigFloat(),
+ * @ref QCBOR_TYPE_BIGFLOAT and QCBORDecode_GetTBigFloatBigMantissa().
+ *
+ * If QCBOR_DISABLE_TAGS is set, the only input this will decode is an
+ * array of two integers. It will set an error if the the array is
+ * preceded by by a tag number or if the mantissa is a big number.
*/
void
-QCBORDecode_GetBigFloat(QCBORDecodeContext *pCtx,
- uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent);
+QCBORDecode_GetTBigFloat(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
void
-QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent);
-
-void
-QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pCtx,
- const char *szLabel,
+QCBORDecode_GetTBigFloatInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
uint8_t uTagRequirement,
int64_t *pnMantissa,
int64_t *pnExponent);
+void
+QCBORDecode_GetTBigFloatInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
+
/**
* @brief Decode the next item as a big float with a big number mantissa.
@@ -1809,46 +1924,101 @@
* @param[out] pbMantissaIsNegative Is @c true if @c pMantissa is negative.
* @param[out] pnExponent The base 2 exponent.
*
- * This is the same as QCBORDecode_GetDecimalFractionBig() with the
- * important distinction that the value is computed by:
+ * This is the same as QCBORDecode_GetTBigFloat() except the mantissa
+ * is returned as a big number. The only limit to precision is the
+ * size of @c MantissaBuffer.
*
- * mantissa * ( 2 ** exponent )
+ * The encoded mantissa may be an integer or a big number. The
+ * standard CBOR offset of 1 for negative is applied, so the mantissa can be used as
+ * returned.
*
- * See also QCBORDecode_GetInt64ConvertAll(),
- * QCBORDecode_GetUInt64ConvertAll() and
- * QCBORDecode_GetDoubleConvertAll() which can convert big floats.
- *
- * See also @ref CBOR_TAG_BIGFLOAT, QCBOREncode_AddBigFloat(),
- * @ref QCBOR_TYPE_BIGFLOAT and QCBORDecode_GetBigFloat().
+ * See also @ref CBOR_TAG_BIGFLOAT,
+ * QCBOREncode_AddTBigFloatBigNumber(), @ref QCBOR_TYPE_BIGFLOAT and
+ * QCBORDecode_GetTBigFloat().
*/
void
-QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pCtx,
- uint8_t uTagRequirement,
- UsefulBuf MantissaBuffer,
- UsefulBufC *pMantissa,
- bool *pbMantissaIsNegative,
- int64_t *pnExponent);
+QCBORDecode_GetTBigFloatBigMantissa(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
void
-QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBuf MantissaBuffer,
- UsefulBufC *pMantissa,
- bool *pbMantissaIsNegative,
- int64_t *pnExponent);
+QCBORDecode_GetTBigFloatBigMantissaInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
void
-QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBuf MantissaBuffer,
- UsefulBufC *pMantissa,
- bool *pbMantissaIsNegative,
- int64_t *pnExponent);
-#endif /* #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA */
+QCBORDecode_GetTBigFloatBigMantissaInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+/**
+ * @brief Decode the next item as a big float with a big number mantissa with out offsetting the mantissa
+ *
+ * @param[in] pCtx The decode context.
+ * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
+ * @param[in] MantissaBuffer The buffer in which to put the mantissa.
+ * @param[out] pMantissa The big num mantissa.
+ * @param[out] pbMantissaIsNegative Is @c true if @c pMantissa is negative.
+ * @param[out] pnExponent The base 2 exponent.
+ *
+ * This is the same as QCBORDecode_GetBigFloat() except the mantissa
+ * is returned as a big number. The only limit to precision is the
+ * size of @c MantissaBuffer.
+ *
+ * The encoded mantissa may be an integer or a big number. The
+ * standard CBOR offset of 1 for negative is NOT applied. If the ma
+ * mantissa is negative, one must be added to get it's actual value.
+ *
+ * Because this doesn't offset the negative big numbers, this links in
+ * a lot less object code. It is suitable for uses where a big number
+ * library is already linked in for other purposes as it can trivially
+ * do the increment by one.
+ *
+ * See also @ref CBOR_TAG_BIGFLOAT,
+ * QCBOREncode_AddTBigFloatBigNumber(), @ref QCBOR_TYPE_BIGFLOAT and
+ * QCBORDecode_GetTBigFloat().
+ */
+void
+QCBORDecode_GetTBigFloatBigMantissaRaw(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+
+void
+QCBORDecode_GetTBigFloatBigMantissaRawInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+void
+QCBORDecode_GetTBigFloatBigMantissaRawInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
/**
@@ -2173,7 +2343,135 @@
+/* ===========================================================================
+ DEPRECATED methods
+ ========================================================================== */
+/* Deprecated. Use QCBORDecode_GetTBigNumberRaw() instead. */
+static void
+QCBORDecode_GetBignum(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC *pValue,
+ bool *pbIsNegative);
+
+static void
+QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC *pValue,
+ bool *pbIsNegative);
+
+static void
+QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC *pValue,
+ bool *pbIsNegative);
+
+#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
+/* Use QCBORDecode_GetTDecimalFraction() instead */
+static void
+QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
+
+/* Use QCBORDecode_GetTDecimalFractionInMapN() instead */
+static void
+QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
+/* Use QCBORDecode_GetTDecimalFractionInMapSZ() instead */
+static void
+QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
+
+/* Use QCBORDecode_GetTDecimalFractionBigMantissaRaw() instead */
+static void
+QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+/* Use QCBORDecode_GetTDecimalFractionBigMantissaRawInMapN() instead */
+static void
+QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pbMantissaIsNegative,
+ bool *pbIsNegative,
+ int64_t *pnExponent);
+
+/* Use QCBORDecode_GetTDecimalFractionBigMantissaRawInMapSZ() instead */
+static void
+QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+/* Deprecated. Use QCBORDecode_GetTBigFloat() instead */
+static void
+QCBORDecode_GetBigFloat(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
+
+/* Deprecated. Use QCBORDecode_GetTBigFloatInMapN() instead */
+static void
+QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
+
+/* Deprecated. Use QCBORDecode_GetTBigFloatInMapSZ() instead */
+static void
+QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent);
+
+/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRaw() instead */
+static void
+QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRawInMapN() instead */
+static void
+QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+
+/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRawInMapSZ() instead */
+static void
+QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent);
+#endif /* ! QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
/* ===========================================================================
BEGINNING OF PRIVATE INLINE IMPLEMENTATION
========================================================================== */
@@ -3097,6 +3395,185 @@
}
+static inline void
+QCBORDecode_GetBignum(QCBORDecodeContext *pMe,
+ uint8_t uTagRequirement,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORDecode_GetTBigNumberRaw(pMe, uTagRequirement, pBigNumber, pbIsNegative);
+}
+
+static inline void
+QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORDecode_GetTBigNumberRawInMapN(pMe, nLabel, uTagRequirement, pBigNumber, pbIsNegative);
+}
+
+static inline void
+QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORDecode_GetTBigNumberRawInMapSZ(pMe, szLabel, uTagRequirement, pBigNumber, pbIsNegative);
+}
+
+#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
+static inline void
+QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTDecimalFraction(pMe, uTagRequirement, pnMantissa, pnExponent);
+}
+
+static inline void
+QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTDecimalFractionInMapN(pMe, nLabel, uTagRequirement, pnMantissa, pnExponent);
+}
+
+static inline void
+QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTDecimalFractionInMapSZ(pMe, szLabel, uTagRequirement, pnMantissa, pnExponent);
+}
+
+
+static inline void
+QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTDecimalFractionBigMantissaRaw(pMe, uTagRequirement, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent);
+}
+
+static inline void
+QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTDecimalFractionBigMantissaRawInMapN(pMe, nLabel, uTagRequirement, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent);
+}
+
+static inline void
+QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTDecimalFractionBigMantissaRawInMapSZ(pMe, szLabel, uTagRequirement, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent);
+
+}
+
+static inline void
+QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTBigFloat(pMe, uTagRequirement, pnMantissa, pnExponent);
+}
+
+static inline void
+QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTBigFloatInMapN(pMe, nLabel, uTagRequirement, pnMantissa, pnExponent);
+}
+
+static inline void
+QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTBigFloatInMapSZ(pMe, szLabel, uTagRequirement, pnMantissa, pnExponent);
+}
+
+static inline void
+QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTBigFloatBigMantissaRaw(pMe,
+ uTagRequirement,
+ MantissaBuffer,
+ pMantissa,
+ pbMantissaIsNegative,
+ pnExponent);
+}
+
+
+static inline void
+QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTBigFloatBigMantissaRawInMapN(pMe,
+ nLabel,
+ uTagRequirement,
+ MantissaBuffer,
+ pMantissa,
+ pbMantissaIsNegative,
+ pnExponent);
+}
+
+static inline void
+QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORDecode_GetTBigFloatBigMantissaRawInMapSZ(pMe,
+ szLabel,
+ uTagRequirement,
+ MantissaBuffer,
+ pMantissa,
+ pbMantissaIsNegative,
+ pnExponent);
+}
+#endif /* ! QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
+
#ifdef __cplusplus
}
#endif
diff --git a/src/UsefulBuf.c b/src/UsefulBuf.c
index 25a92f4..46b66c6 100644
--- a/src/UsefulBuf.c
+++ b/src/UsefulBuf.c
@@ -161,6 +161,24 @@
/*
* Public function -- see UsefulBuf.h
+ */
+UsefulBufC
+UsefulBuf_SkipLeading(UsefulBufC String, uint8_t uByte)
+{
+ for(;String.len; String.len--) {
+ if(*(const uint8_t *)String.ptr != uByte) {
+ break;
+ }
+ String.ptr = (const uint8_t *)String.ptr + 1;
+ }
+
+ return String;
+}
+
+
+
+/*
+ * Public function -- see UsefulBuf.h
*
* Code Reviewers: THIS FUNCTION DOES POINTER MATH
*/
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 5edd2d6..d08298b 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -4903,7 +4903,7 @@
}
-void
+static void
QCBORDecode_Private_ProcessTagItemMulti(QCBORDecodeContext *pMe,
QCBORItem *pItem,
const uint8_t uTagRequirement,
@@ -4945,7 +4945,7 @@
/*
**/
- void
+static void
QCBORDecode_Private_ProcessTagItem(QCBORDecodeContext *pMe,
QCBORItem *pItem,
const uint8_t uTagRequirement,
@@ -4970,13 +4970,13 @@
static void
-QCBORDecode_Private_ProcessTagOne(QCBORDecodeContext *pMe,
- QCBORItem *pItem,
- const uint8_t uTagRequirement,
- const uint8_t uQCBORType,
- const uint64_t uTagNumber,
+QCBORDecode_Private_ProcessTagOne(QCBORDecodeContext *pMe,
+ QCBORItem *pItem,
+ const uint8_t uTagRequirement,
+ const uint8_t uQCBORType,
+ const uint64_t uTagNumber,
QCBORTagContentCallBack *pfCB,
- const size_t uOffset)
+ const size_t uOffset)
{
uint8_t auQCBORType[2];
@@ -5173,7 +5173,7 @@
*
* @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
* @param[in] pItem The item with the date.
- * @param[out] pValue The returned big number
+ * @param[out] pBignumber The returned big number
* @param[out] pbIsNegative The returned sign of the big number.
*
* Common processing for the big number tag. Mostly make sure
@@ -5181,14 +5181,14 @@
* numbers.
*/
static void
-QCBOR_Private_ProcessBigNum(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- QCBORItem *pItem,
- UsefulBufC *pValue,
- bool *pbIsNegative,
- size_t uOffset)
+QCBORDecode_Private_BigNumberRawMain(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ QCBORItem *pItem,
+ UsefulBufC *pBignumber,
+ bool *pbIsNegative,
+ size_t uOffset)
{
- QCBORError uErr;
+ // TODO: refer to the static const ones instead
const uint8_t puTypes[] = {QCBOR_TYPE_POSBIGNUM,QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE};
@@ -5198,24 +5198,19 @@
pItem,
uTagRequirement,
puTypes,
- puTNs, QCBORDecode_StringsTagCB, uOffset);
+ puTNs,
+ QCBORDecode_StringsTagCB,
+ uOffset);
if(pMe->uLastError) {
return;
}
-
- // TODO: is this right? Tests are passing. Fix after merges.
- // TODO: make this work for GetBigNum and GetBigNumber(). They are different.
if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) {
*pbIsNegative = false;
} else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) {
*pbIsNegative = true;
}
- *pValue = pItem->val.bigNum;
-
- uErr = QCBOR_SUCCESS;
-
- pMe->uLastError = (uint8_t)uErr;
+ *pBignumber = pItem->val.bigNum;
}
@@ -5319,65 +5314,65 @@
* Public function, see header qcbor/qcbor_spiffy_decode.h
*/
void
-QCBORDecode_GetBignum(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- UsefulBufC *pValue,
- bool *pbIsNegative)
-{
- QCBORItem Item;
- size_t uOffset;
-
- QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
- QCBOR_Private_ProcessBigNum(pMe,
- uTagRequirement,
- &Item,
- pValue,
- pbIsNegative,
- uOffset);
-}
-
-/*
- * Public function, see header qcbor/qcbor_spiffy_decode.h
- */
-void
-QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- UsefulBufC *pValue,
- bool *pbIsNegative)
-{
- QCBORItem Item;
- size_t uOffset;
-
- QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBOR_Private_ProcessBigNum(pMe,
- uTagRequirement,
- &Item,
- pValue,
- pbIsNegative,
- uOffset);
-}
-
-/*
- * Public function, see header qcbor/qcbor_spiffy_decode.h
- */
-void
-QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
+QCBORDecode_GetTBigNumberRaw(QCBORDecodeContext *pMe,
const uint8_t uTagRequirement,
- UsefulBufC *pValue,
+ UsefulBufC *pBignumber,
bool *pbIsNegative)
{
QCBORItem Item;
size_t uOffset;
+ QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
+ QCBORDecode_Private_BigNumberRawMain(pMe,
+ uTagRequirement,
+ &Item,
+ pBignumber,
+ pbIsNegative,
+ uOffset);
+}
+
+/*
+ * Public function, see header qcbor/qcbor_spiffy_decode.h
+ */
+void
+QCBORDecode_GetTBigNumberRawInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_BigNumberRawMain(pMe,
+ uTagRequirement,
+ &Item,
+ pBigNumber,
+ pbIsNegative,
+ uOffset);
+}
+
+/*
+ * Public function, see header qcbor/qcbor_spiffy_decode.h
+ */
+void
+QCBORDecode_GetTBigNumberRawInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBOR_Private_ProcessBigNum(pMe,
- uTagRequirement,
- &Item,
- pValue,
- pbIsNegative,
- uOffset);
+ QCBORDecode_Private_BigNumberRawMain(pMe,
+ uTagRequirement,
+ &Item,
+ pBigNumber,
+ pbIsNegative,
+ uOffset);
}
@@ -5744,6 +5739,8 @@
int64_t *pnResult)
{
uint64_t uResult;
+ QCBORError uError;
+
/* The negative integer furthest from zero for a C int64_t is
* INT64_MIN which is expressed as -INT64_MAX - 1. The value of a
* negative number in CBOR is computed as -n - 1 where n is the
@@ -5756,9 +5753,7 @@
* -n - 1 <= -INT64_MAX - 1
* n <= INT64_MAX.
*/
- QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum,
- INT64_MAX,
- &uResult);
+ uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult);
if(uError != QCBOR_SUCCESS) {
return uError;
}
@@ -7020,9 +7015,6 @@
return UsefulOutBuf_OutUBuf(&UOB);
}
-
-
-
/* Some notes from the work to disable tags.
* Some are out of date since tag refactoring.
*
@@ -7060,16 +7052,20 @@
QCBOR_TYPE_DECIMAL_FRACTION,
QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
+ QCBOR_TYPE_DECIMAL_FRACTION_POS_U64,
+ QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64,
QCBOR_TYPE_NONE};
static const uint8_t QCBORDecode_Private_BigFloatTypes[] = {
QCBOR_TYPE_BIGFLOAT,
QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM,
+ QCBOR_TYPE_BIGFLOAT_POS_U64,
+ QCBOR_TYPE_BIGFLOAT_NEG_U64,
QCBOR_TYPE_NONE};
/**
- * @brief Common processor for exponent and mantissa.
+ * @brief Common processor for exponent and int64_t mantissa.
*
* @param[in] pMe The decode context.
* @param[in] uTagRequirement Whether tag number must be present or not.
@@ -7086,18 +7082,22 @@
* On output, the item is always a fully decoded decimal fraction or
* big float.
*
- * This errors out if the input type does not meet the TagSpec.
+ * This errors out if the input tag and type aren't as required.
+ *
+ * This always provides the correctly offset mantissa, even when the
+ * input CBOR is a negative big number. This works the
+ * same in QCBOR v1 and v2.
*/
static void
-QCBOR_Private_ProcessExpMantissa(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- const uint64_t uTagNumber,
- const size_t uOffset,
- QCBORItem *pItem,
- int64_t *pnMantissa,
- int64_t *pnExponent)
+QCBORDecode_Private_ExpIntMantissaMain(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const uint64_t uTagNumber,
+ const size_t uOffset,
+ QCBORItem *pItem,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
{
- QCBORError uErr = QCBOR_SUCCESS;
+ QCBORError uErr;
const uint8_t *qTypes;
if(pMe->uLastError) {
@@ -7122,6 +7122,7 @@
return;
}
+ uErr = QCBOR_SUCCESS;
switch (pItem->uDataType) {
case QCBOR_TYPE_DECIMAL_FRACTION:
@@ -7145,32 +7146,22 @@
break;
#endif /* QCBOR_DISABLE_TAGS */
+ case QCBOR_TYPE_BIGFLOAT_NEG_U64:
+ case QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64:
+ case QCBOR_TYPE_BIGFLOAT_POS_U64:
+ case QCBOR_TYPE_DECIMAL_FRACTION_POS_U64:
+ uErr = QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
+ break;
+
default:
uErr = QCBOR_ERR_UNEXPECTED_TYPE;
}
- // Done:
- pMe->uLastError = (uint8_t)uErr;
+ pMe->uLastError = (uint8_t)uErr;
}
-
-/*
- * @brief Decode exponent and mantissa into a big number.
- *
- * @param[in] pMe The decode context.
- * @param[in] TagSpec The expected/allowed tags.
- * @param[in] pItem Item to decode and convert.
- * @param[in] BufferForMantissa Buffer to output mantissa into.
- * @param[out] pMantissa The output mantissa.
- * @param[out] pbIsNegative The sign of the output.
- * @param[out] pnExponent The mantissa of the output.
- *
- * This is the common processing of a decimal fraction or a big float
- * into a big number. This will decode and consume all the CBOR items
- * that make up the decimal fraction or big float.
- */
static void
-QCBORDecode_Private_ProcessExpMantissaBig(QCBORDecodeContext *pMe,
+QCBORDecode_Private_ExpBigMantissaRawMain(QCBORDecodeContext *pMe,
const uint8_t uTagRequirement,
const uint64_t uTagNumber,
const size_t uOffset,
@@ -7180,7 +7171,8 @@
bool *pbIsNegative,
int64_t *pnExponent)
{
- QCBORError uErr = QCBOR_SUCCESS;
+ QCBORError uErr;
+ uint64_t uMantissa;
const uint8_t *qTypes;
if(pMe->uLastError) {
@@ -7205,25 +7197,31 @@
return;
}
- uint64_t uMantissa;
+ uErr = QCBOR_SUCCESS;
switch (pItem->uDataType) {
case QCBOR_TYPE_DECIMAL_FRACTION:
case QCBOR_TYPE_BIGFLOAT:
- /* See comments in ExponentiateNN() on handling INT64_MIN */
if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) {
uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt;
*pbIsNegative = false;
- } else if(pItem->val.expAndMantissa.Mantissa.nInt != INT64_MIN) {
- uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt;
- *pbIsNegative = true;
} else {
- uMantissa = (uint64_t)INT64_MAX+1;
+ if(pItem->val.expAndMantissa.Mantissa.nInt != INT64_MIN) {
+ uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt;
+ } else {
+ /* Can't negate like above when int64_t is INT64_MIN because it
+ * will overflow. See ExponentNN() */
+ uMantissa = (uint64_t)INT64_MAX+1;
+ }
*pbIsNegative = true;
}
- *pMantissa = QCBOR_Private_ConvertIntToBigNum(uMantissa,
- BufferForMantissa);
+ /* Reverse the offset by 1 for type 1 negative value to be consistent
+ * with big num case below which don't offset because it requires
+ * big number arithmetic. This is a bug fix for QCBOR v1.5.
+ */
+ uMantissa--;
+ *pMantissa = QCBOR_Private_ConvertIntToBigNum(uMantissa, BufferForMantissa);
*pnExponent = pItem->val.expAndMantissa.nExponent;
break;
@@ -7252,26 +7250,105 @@
}
-/*
- * Public function, see header qcbor/qcbor_decode.h file
+/**
+ * @brief Decode exponent and mantissa into a big number with negative offset of 1.
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] uTagRequirement Whether a tag number must be present or not.
+ * @param[in] pItem Item to decode and convert.
+ * @param[in] BufferForMantissa Buffer to output mantissa into.
+ * @param[out] pMantissa The output mantissa.
+ * @param[out] pbIsNegative The sign of the output.
+ * @param[out] pnExponent The mantissa of the output.
+ *
+ * This is the common processing of a decimal fraction or a big float
+ * into a big number. This will decode and consume all the CBOR items
+ * that make up the decimal fraction or big float.
+ *
+ * This performs the subtraction of 1 from the negative value so the
+ * caller doesn't need to. This links more object code than QCBORDecode_Private_ProcessExpMantissaBig().
*/
-void
-QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent)
+static void
+QCBORDecode_Private_ExpBigMantissaMain(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const uint64_t uTagNumber,
+ const size_t uOffset,
+ QCBORItem *pItem,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
{
- QCBORItem Item;
- size_t uOffset;
+ QCBORError uErr;
+ QCBORItem TempMantissa;
+ const uint8_t *qTypes;
- QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
- QCBOR_Private_ProcessExpMantissa(pMe,
- uTagRequirement,
- CBOR_TAG_DECIMAL_FRACTION,
- uOffset,
- &Item,
- pnMantissa,
- pnExponent);
+ if(pMe->uLastError) {
+ return;
+ }
+
+ if(uTagNumber == CBOR_TAG_BIGFLOAT) {
+ qTypes = QCBORDecode_Private_BigFloatTypes;
+ } else {
+ qTypes = QCBORDecode_Private_DecimalFractionTypes;
+ }
+
+ QCBORDecode_Private_ProcessTagItem(pMe,
+ pItem,
+ uTagRequirement,
+ qTypes,
+ uTagNumber,
+ QCBORDecode_ExpMantissaTagCB,
+ uOffset);
+
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return;
+ }
+
+ memset(&TempMantissa, 0, sizeof(TempMantissa));
+
+ switch (pItem->uDataType) {
+
+ case QCBOR_TYPE_DECIMAL_FRACTION:
+ case QCBOR_TYPE_BIGFLOAT:
+ TempMantissa.uDataType = QCBOR_TYPE_INT64;
+ TempMantissa.val.int64 = pItem->val.expAndMantissa.Mantissa.nInt;
+ break;
+
+ case QCBOR_TYPE_DECIMAL_FRACTION_POS_U64:
+ case QCBOR_TYPE_BIGFLOAT_POS_U64:
+ TempMantissa.uDataType = QCBOR_TYPE_UINT64;
+ TempMantissa.val.uint64 = pItem->val.expAndMantissa.Mantissa.uInt;
+ break;
+
+ case QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64:
+ case QCBOR_TYPE_BIGFLOAT_NEG_U64:
+ TempMantissa.uDataType = QCBOR_TYPE_65BIT_NEG_INT;
+ TempMantissa.val.uint64 = pItem->val.expAndMantissa.Mantissa.uInt;
+ break;
+
+#ifndef QCBOR_DISABLE_TAGS
+ /* If tags are disabled, mantissas can never be big nums */
+ case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
+ case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
+ TempMantissa.uDataType = QCBOR_TYPE_BYTE_STRING;
+ TempMantissa.val.bigNum = pItem->val.expAndMantissa.Mantissa.bigNum;
+ *pbIsNegative = false;
+ break;
+
+ case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
+ case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
+ TempMantissa.uDataType = QCBOR_TYPE_BYTE_STRING;
+ TempMantissa.val.bigNum = pItem->val.expAndMantissa.Mantissa.bigNum;
+ *pbIsNegative = true;
+ break;
+#endif /* ! QCBOR_DISABLE_TAGS */
+ }
+
+ *pnExponent = pItem->val.expAndMantissa.nExponent;
+ uErr = QCBORDecode_ProcessBigNumber(TempMantissa, BufferForMantissa, pMantissa, pbIsNegative);
+
+ pMe->uLastError = (uint8_t)uErr;
}
@@ -7279,23 +7356,46 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent)
+QCBORDecode_GetTDecimalFraction(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
+ QCBORDecode_Private_ExpIntMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ uOffset,
+ &Item,
+ pnMantissa,
+ pnExponent);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetTDecimalFractionInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBOR_Private_ProcessExpMantissa(pMe,
- uTagRequirement,
- CBOR_TAG_DECIMAL_FRACTION,
- uOffset,
- &Item,
- pnMantissa,
- pnExponent);
+ QCBORDecode_Private_ExpIntMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ uOffset,
+ &Item,
+ pnMantissa,
+ pnExponent);
}
@@ -7304,23 +7404,23 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- const uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent)
+QCBORDecode_GetTDecimalFractionInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBOR_Private_ProcessExpMantissa(pMe,
- uTagRequirement,
- CBOR_TAG_DECIMAL_FRACTION,
- uOffset,
- &Item,
- pnMantissa,
- pnExponent);
+ QCBORDecode_Private_ExpIntMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ uOffset,
+ &Item,
+ pnMantissa,
+ pnExponent);
}
@@ -7328,18 +7428,100 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBuf MantissaBuffer,
- UsefulBufC *pMantissa,
- bool *pbMantissaIsNegative,
- int64_t *pnExponent)
+QCBORDecode_GetTDecimalFractionBigMantissa(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
- QCBORDecode_Private_ProcessExpMantissaBig(pMe,
+ QCBORDecode_Private_ExpBigMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ uOffset,
+ &Item,
+ MantissaBuffer,
+ pMantissa,
+ pbMantissaIsNegative,
+ pnExponent);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetTDecimalFractionBigMantissaInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_ExpBigMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ uOffset,
+ &Item,
+ BufferForMantissa,
+ pMantissa,
+ pbIsNegative,
+ pnExponent);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetTDecimalFractionBigMantissaInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_ExpBigMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_DECIMAL_FRACTION,
+ uOffset,
+ &Item,
+ BufferForMantissa,
+ pMantissa,
+ pbIsNegative,
+ pnExponent);
+}
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetTDecimalFractionBigMantissaRaw(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
+ QCBORDecode_Private_ExpBigMantissaRawMain(pMe,
uTagRequirement,
CBOR_TAG_DECIMAL_FRACTION,
uOffset,
@@ -7355,19 +7537,19 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- const UsefulBuf BufferForMantissa,
- UsefulBufC *pMantissa,
- bool *pbIsNegative,
- int64_t *pnExponent)
+QCBORDecode_GetTDecimalFractionBigMantissaRawInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBORDecode_Private_ProcessExpMantissaBig(pMe,
+ QCBORDecode_Private_ExpBigMantissaRawMain(pMe,
uTagRequirement,
CBOR_TAG_DECIMAL_FRACTION,
uOffset,
@@ -7383,19 +7565,19 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- const uint8_t uTagRequirement,
- const UsefulBuf BufferForMantissa,
- UsefulBufC *pMantissa,
- bool *pbIsNegative,
- int64_t *pnExponent)
+QCBORDecode_GetTDecimalFractionBigMantissaRawInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBORDecode_Private_ProcessExpMantissaBig(pMe,
+ QCBORDecode_Private_ExpBigMantissaRawMain(pMe,
uTagRequirement,
CBOR_TAG_DECIMAL_FRACTION,
uOffset,
@@ -7411,22 +7593,22 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent)
+QCBORDecode_GetTBigFloat(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
{
- QCBORItem Item;
- size_t uOffset;
+ QCBORItem Item;
+ size_t uOffset;
QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
- QCBOR_Private_ProcessExpMantissa(pMe,
- uTagRequirement,
- CBOR_TAG_BIGFLOAT,
- uOffset,
- &Item,
- pnMantissa,
- pnExponent);
+ QCBORDecode_Private_ExpIntMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ uOffset,
+ &Item,
+ pnMantissa,
+ pnExponent);
}
@@ -7434,32 +7616,8 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- int64_t *pnMantissa,
- int64_t *pnExponent)
-{
- QCBORItem Item;
- size_t uOffset;
-
- QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBOR_Private_ProcessExpMantissa(pMe,
- uTagRequirement,
- CBOR_TAG_BIGFLOAT,
- uOffset,
- &Item,
- pnMantissa,
- pnExponent);
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
+QCBORDecode_GetTBigFloatInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
const uint8_t uTagRequirement,
int64_t *pnMantissa,
int64_t *pnExponent)
@@ -7467,14 +7625,14 @@
QCBORItem Item;
size_t uOffset;
- QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBOR_Private_ProcessExpMantissa(pMe,
- uTagRequirement,
- CBOR_TAG_BIGFLOAT,
- uOffset,
- &Item,
- pnMantissa,
- pnExponent);
+ QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_ExpIntMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ uOffset,
+ &Item,
+ pnMantissa,
+ pnExponent);
}
@@ -7482,18 +7640,123 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBuf MantissaBuffer,
- UsefulBufC *pMantissa,
- bool *pbMantissaIsNegative,
- int64_t *pnExponent)
+QCBORDecode_GetTBigFloatInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ int64_t *pnMantissa,
+ int64_t *pnExponent)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_ExpIntMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ uOffset,
+ &Item,
+ pnMantissa,
+ pnExponent);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetTBigFloatBigMantissa(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
- QCBORDecode_Private_ProcessExpMantissaBig(pMe,
+ QCBORDecode_Private_ExpBigMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ uOffset,
+ &Item,
+ MantissaBuffer,
+ pMantissa,
+ pbMantissaIsNegative,
+ pnExponent);
+}
+
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetTBigFloatBigMantissaInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_ExpBigMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ uOffset,
+ &Item,
+ BufferForMantissa,
+ pMantissa,
+ pbIsNegative,
+ pnExponent);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetTBigFloatBigMantissaInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_ExpBigMantissaMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ uOffset,
+ &Item,
+ BufferForMantissa,
+ pMantissa,
+ pbIsNegative,
+ pnExponent);
+}
+
+
+void
+QCBORDecode_GetTBigFloatBigMantissaRaw(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBuf MantissaBuffer,
+ UsefulBufC *pMantissa,
+ bool *pbMantissaIsNegative,
+ int64_t *pnExponent)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
+ QCBORDecode_Private_ExpBigMantissaRawMain(pMe,
uTagRequirement,
CBOR_TAG_BIGFLOAT,
uOffset,
@@ -7505,31 +7768,33 @@
}
+
+
/*
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- const UsefulBuf BufferForMantissa,
- UsefulBufC *pMantissa,
- bool *pbIsNegative,
- int64_t *pnExponent)
+QCBORDecode_GetTBigFloatBigMantissaRawInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBORDecode_Private_ProcessExpMantissaBig(pMe,
- uTagRequirement,
- CBOR_TAG_BIGFLOAT,
- uOffset,
- &Item,
- BufferForMantissa,
- pMantissa,
- pbIsNegative,
- pnExponent);
+ QCBORDecode_Private_ExpBigMantissaRawMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ uOffset,
+ &Item,
+ BufferForMantissa,
+ pMantissa,
+ pbIsNegative,
+ pnExponent);
}
@@ -7537,30 +7802,31 @@
* Public function, see header qcbor/qcbor_decode.h file
*/
void
-QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- const uint8_t uTagRequirement,
- const UsefulBuf BufferForMantissa,
- UsefulBufC *pMantissa,
- bool *pbIsNegative,
- int64_t *pnExponent)
+QCBORDecode_GetTBigFloatBigMantissaRawInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBuf BufferForMantissa,
+ UsefulBufC *pMantissa,
+ bool *pbIsNegative,
+ int64_t *pnExponent)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBORDecode_Private_ProcessExpMantissaBig(pMe,
- uTagRequirement,
- CBOR_TAG_BIGFLOAT,
- uOffset,
- &Item,
- BufferForMantissa,
- pMantissa,
- pbIsNegative,
- pnExponent);
+ QCBORDecode_Private_ExpBigMantissaRawMain(pMe,
+ uTagRequirement,
+ CBOR_TAG_BIGFLOAT,
+ uOffset,
+ &Item,
+ BufferForMantissa,
+ pMantissa,
+ pbIsNegative,
+ pnExponent);
}
-#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
+
+#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
@@ -7664,14 +7930,16 @@
#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */
+
+
static UsefulBufC
-QCBORDecode_IntToBigNum(uint64_t uNum,
- const UsefulBuf BigNumBuf)
+QCBORDecode_IntToBigNumber(uint64_t uNum,
+ const UsefulBuf BigNumberBuf)
{
UsefulOutBuf OB;
/* With a UsefulOutBuf, there's no pointer math here. */
- UsefulOutBuf_Init(&OB, BigNumBuf);
+ UsefulOutBuf_Init(&OB, BigNumberBuf);
/* Must copy one byte even if zero. The loop, mask and shift
* algorithm provides endian conversion.
@@ -7685,21 +7953,6 @@
}
-static UsefulBufC
-QCBORDecode_Private_RemoveLeadingZeros(UsefulBufC String)
-{
- while(String.len > 1) {
- if(*(const uint8_t *)String.ptr) {
- break;
- }
- String.len--;
- String.ptr = (const uint8_t *)String.ptr + 1;
- }
-
- return String;
-}
-
-
/* Add one to the big number and put the result in a new UsefulBufC
* from storage in UsefulBuf.
*
@@ -7708,8 +7961,8 @@
* Code Reviewers: THIS FUNCTION DOES POINTER MATH
*/
static UsefulBufC
-QCBORDecode_BigNumCopyPlusOne(UsefulBufC BigNum,
- UsefulBuf BigNumBuf)
+QCBORDecode_BigNumberCopyPlusOne(UsefulBufC BigNumber,
+ UsefulBuf BigNumberBuf)
{
uint8_t uCarry;
uint8_t uSourceValue;
@@ -7718,8 +7971,8 @@
ptrdiff_t uDestBytesLeft;
/* Start adding at the LSB */
- pSource = &((const uint8_t *)BigNum.ptr)[BigNum.len-1];
- pDest = &((uint8_t *)BigNumBuf.ptr)[BigNumBuf.len-1];
+ pSource = &((const uint8_t *)BigNumber.ptr)[BigNumber.len-1];
+ pDest = &((uint8_t *)BigNumberBuf.ptr)[BigNumberBuf.len-1];
uCarry = 1; /* Gets set back to zero if add the next line doesn't wrap */
*pDest = *pSource + 1;
@@ -7734,11 +7987,11 @@
uCarry = 0;
}
- uDestBytesLeft = pDest - (uint8_t *)BigNumBuf.ptr;
- if(pSource <= (const uint8_t *)BigNum.ptr && uCarry == 0) {
+ uDestBytesLeft = pDest - (uint8_t *)BigNumberBuf.ptr;
+ if(pSource <= (const uint8_t *)BigNumber.ptr && uCarry == 0) {
break; /* Successful exit */
}
- if(pSource > (const uint8_t *)BigNum.ptr) {
+ if(pSource > (const uint8_t *)BigNumber.ptr) {
uSourceValue = *--pSource;
} else {
/* All source bytes processed, but not the last carry */
@@ -7753,7 +8006,7 @@
*pDest = uSourceValue + uCarry;
}
- return (UsefulBufC){pDest, BigNumBuf.len - (size_t)uDestBytesLeft};
+ return (UsefulBufC){pDest, BigNumberBuf.len - (size_t)uDestBytesLeft};
}
@@ -7776,30 +8029,90 @@
* Public function, see header qcbor/qcbor_decode.h
*/
QCBORError
-QCBORDecode_BignumPreferred(const QCBORItem Item,
- UsefulBuf BigNumBuf,
- UsefulBufC *pBigNum,
- bool *pbIsNegative)
+QCBORDecode_ProcessBigNumberNoPreferred(const QCBORItem Item,
+ const UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
{
- QCBORError uResult;
size_t uLen;
- UsefulBufC BigNum;
+ UsefulBufC BigNumber;
int uType;
uType = Item.uDataType;
if(uType == QCBOR_TYPE_BYTE_STRING) {
- uType = *pbIsNegative ? QCBOR_TYPE_POSBIGNUM : QCBOR_TYPE_NEGBIGNUM;
+ uType = *pbIsNegative ? QCBOR_TYPE_NEGBIGNUM : QCBOR_TYPE_POSBIGNUM;
}
static const uint8_t Zero[] = {0x00};
- BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(Zero);
- if((uType == QCBOR_TYPE_POSBIGNUM || uType == QCBOR_TYPE_NEGBIGNUM) &&
- Item.val.bigNum.len) {
- BigNum = QCBORDecode_Private_RemoveLeadingZeros(Item.val.bigNum);
+ BigNumber = UsefulBuf_SkipLeading(Item.val.bigNum, 0);
+ if(BigNumber.len == 0) {
+ BigNumber = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(Zero);
}
/* Compute required length so it can be returned if buffer is too small */
switch(uType) {
+
+ case QCBOR_TYPE_POSBIGNUM:
+ uLen = BigNumber.len;
+ break;
+
+ case QCBOR_TYPE_NEGBIGNUM:
+ uLen = BigNumber.len;
+ if(UsefulBuf_IsValue(UsefulBuf_SkipLeading(BigNumber, 0), 0xff) == SIZE_MAX) {
+ uLen++;
+ }
+ break;
+
+ default:
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+
+ *pBigNumber = (UsefulBufC){NULL, uLen};
+
+ if(BigNumberBuf.len < uLen || uLen == 0 || BigNumberBuf.ptr == NULL) {
+ return BigNumberBuf.ptr == NULL ? QCBOR_SUCCESS : QCBOR_ERR_BUFFER_TOO_SMALL;
+ /* Buffer is too short or type is wrong */
+ }
+
+
+ if(uType == QCBOR_TYPE_POSBIGNUM) {
+ *pBigNumber = UsefulBuf_Copy(BigNumberBuf, BigNumber);
+ *pbIsNegative = false;
+ } else if(uType == QCBOR_TYPE_NEGBIGNUM) {
+ /* The messy one. Take the stuff in the buffer and copy it to
+ * the new buffer, adding one to it. This might be one byte
+ * bigger than the original because of the carry from adding
+ * one.*/
+ *pbIsNegative = true;
+ *pBigNumber = QCBORDecode_BigNumberCopyPlusOne(BigNumber, BigNumberBuf);
+ }
+
+ return QCBOR_SUCCESS;
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h
+ */
+QCBORError
+QCBORDecode_ProcessBigNumber(const QCBORItem Item,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORError uResult;
+ size_t uLen;
+ int uType;
+
+ uType = Item.uDataType;
+
+ switch(uType) {
+ case QCBOR_TYPE_POSBIGNUM:
+ case QCBOR_TYPE_NEGBIGNUM:
+ case QCBOR_TYPE_BYTE_STRING:
+ return QCBORDecode_ProcessBigNumberNoPreferred(Item, BigNumberBuf, pBigNumber, pbIsNegative);
+ break;
+
case QCBOR_TYPE_INT64:
uLen = QCBORDecode_Private_CountNonZeroBytes((uint64_t)ABSOLUTE_VALUE(Item.val.int64));
break;
@@ -7809,132 +8122,109 @@
break;
case QCBOR_TYPE_65BIT_NEG_INT:
- uLen = Item.val.uint64 == UINT64_MAX ? 9 : QCBORDecode_Private_CountNonZeroBytes(Item.val.uint64);
- break;
-
- case QCBOR_TYPE_POSBIGNUM:
- uLen = BigNum.len;
- break;
-
- case QCBOR_TYPE_NEGBIGNUM:
- uLen = BigNum.len;
- if(UsefulBuf_IsValue(BigNum, 0xff) == SIZE_MAX) {
- uLen++;
- }
+ uLen = Item.val.uint64 == UINT64_MAX ? 9 : QCBORDecode_Private_CountNonZeroBytes(Item.val.uint64);
break;
default:
- uLen = 0;
+ return QCBOR_ERR_UNEXPECTED_TYPE;
}
- *pBigNum = (UsefulBufC){NULL, uLen};
- if(BigNumBuf.len < uLen || uLen == 0 || BigNumBuf.ptr == NULL) {
- return BigNumBuf.ptr == NULL ? QCBOR_SUCCESS : QCBOR_ERR_BUFFER_TOO_SMALL;
+ *pBigNumber = (UsefulBufC){NULL, uLen};
+
+ if(BigNumberBuf.len < uLen || uLen == 0 || BigNumberBuf.ptr == NULL) {
+ return BigNumberBuf.ptr == NULL ? QCBOR_SUCCESS : QCBOR_ERR_BUFFER_TOO_SMALL;
/* Buffer is too short or type is wrong */
}
uResult = QCBOR_SUCCESS;
- if(uType == QCBOR_TYPE_POSBIGNUM) {
- *pBigNum = UsefulBuf_Copy(BigNumBuf, BigNum);
- *pbIsNegative = false;
- } else if(uType == QCBOR_TYPE_UINT64) {
- *pBigNum = QCBORDecode_IntToBigNum(Item.val.uint64, BigNumBuf);
+ if(uType == QCBOR_TYPE_UINT64) {
+ *pBigNumber = QCBORDecode_IntToBigNumber(Item.val.uint64, BigNumberBuf);
*pbIsNegative = false;
} else if(uType == QCBOR_TYPE_INT64) {
+ /* Offset of 1 for negative numbers already performed */
*pbIsNegative = Item.val.int64 < 0;
- *pBigNum = QCBORDecode_IntToBigNum((uint64_t)(*pbIsNegative ? -Item.val.int64 : Item.val.int64), BigNumBuf);
+ *pBigNumber = QCBORDecode_IntToBigNumber((uint64_t)(*pbIsNegative ? -Item.val.int64 : Item.val.int64), BigNumberBuf);
} else if(uType == QCBOR_TYPE_65BIT_NEG_INT) {
+ /* Offset of 1 for negative numbers NOT already performed */
*pbIsNegative = true;
if(Item.val.uint64 == UINT64_MAX) {
/* The one value that can't be done with a computation
- * because it would overflow a uint64_t*/
+ * because it would overflow a uint64_t */
static const uint8_t TwoToThe64[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- *pBigNum = UsefulBuf_Copy(BigNumBuf, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(TwoToThe64));
+ *pBigNumber = UsefulBuf_Copy(BigNumberBuf, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(TwoToThe64));
} else {
- *pBigNum = QCBORDecode_IntToBigNum(Item.val.uint64 + 1, BigNumBuf);
+ // TODO: why + 1; test it; document it
+ *pBigNumber = QCBORDecode_IntToBigNumber(Item.val.uint64 + 1, BigNumberBuf);
}
- } else if(uType == QCBOR_TYPE_NEGBIGNUM) {
- /* The messy one. Take the stuff in the buffer and copy it to
- * the new buffer, adding one to it. This might be one byte
- * bigger than the original because of the carry from adding
- * one.*/
- *pbIsNegative = true;
- *pBigNum = QCBORDecode_BigNumCopyPlusOne(BigNum, BigNumBuf);
-
- } else {
- uResult = QCBOR_ERR_UNEXPECTED_TYPE;
}
return uResult;
}
+static const uint64_t QCBORDecode_Private_BigNumberTagNumbers[] = {
+ CBOR_TAG_POS_BIGNUM,
+ CBOR_TAG_NEG_BIGNUM,
+ CBOR_TAG_INVALID64};
+
+static const uint8_t QCBORDecode_Private_BigNumberTypes[] = {
+ QCBOR_TYPE_INT64,
+ QCBOR_TYPE_UINT64,
+ QCBOR_TYPE_65BIT_NEG_INT,
+ QCBOR_TYPE_POSBIGNUM,
+ QCBOR_TYPE_NEGBIGNUM,
+ QCBOR_TYPE_NONE};
+
+#define QCBORDecode_Private_BigNumberTypesNoPreferred &QCBORDecode_Private_BigNumberTypes[3]
static void
-QCBOR_Private_ProcessPreferredBigNum(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- QCBORItem *pItem,
- const size_t uOffset,
- UsefulBuf BigNumBuf,
- UsefulBufC *pValue,
- bool *pbIsNegative)
+QCBORDecode_Private_BigNumberNoPreferredMain(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ QCBORItem *pItem,
+ const size_t uOffset,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
{
- if(pItem->uDataType != QCBOR_TYPE_INT64 &&
- pItem->uDataType != QCBOR_TYPE_UINT64 &&
- pItem->uDataType != QCBOR_TYPE_65BIT_NEG_INT) {
-
- /* Two stage processing because big numbers are handled like that */
-
- const uint8_t puTypes[] = {QCBOR_TYPE_POSBIGNUM,QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE};
- const uint64_t puTNs[] = {CBOR_TAG_POS_BIGNUM, CBOR_TAG_NEG_BIGNUM, CBOR_TAG_INVALID64};
-
- QCBORDecode_Private_ProcessTagItemMulti(pMe,
- pItem,
- uTagRequirement,
- puTypes,
- puTNs,
- QCBORDecode_StringsTagCB,
- uOffset);
-
- if(pMe->uLastError) {
- return;
- }
- }
-
- pMe->uLastError = (uint8_t)QCBORDecode_BignumPreferred(*pItem, BigNumBuf, pValue, pbIsNegative);
-}
-
-
-static void
-QCBOR_Private_ProcessBigNumber(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- QCBORItem *pItem,
- const size_t uOffset,
- UsefulBuf BigNumBuf,
- UsefulBufC *pValue,
- bool *pbIsNegative)
-{
- /* Two stage processing because big numbers are handled like that */
-
- const uint8_t puTypes[] = {QCBOR_TYPE_POSBIGNUM,QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE};
- const uint64_t puTNs[] = {CBOR_TAG_POS_BIGNUM, CBOR_TAG_NEG_BIGNUM, CBOR_TAG_INVALID64};
-
QCBORDecode_Private_ProcessTagItemMulti(pMe,
pItem,
uTagRequirement,
- puTypes,
- puTNs,
+ QCBORDecode_Private_BigNumberTypesNoPreferred,
+ QCBORDecode_Private_BigNumberTagNumbers,
QCBORDecode_StringsTagCB,
uOffset);
-
if(pMe->uLastError) {
return;
}
- pMe->uLastError = (uint8_t)QCBORDecode_BignumPreferred(*pItem, BigNumBuf, pValue, pbIsNegative);
+ pMe->uLastError = (uint8_t)QCBORDecode_ProcessBigNumberNoPreferred(*pItem, BigNumberBuf, pBigNumber, pbIsNegative);
+}
+
+
+static void
+QCBORDecode_Private_BigNumberMain(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ QCBORItem *pItem,
+ const size_t uOffset,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORDecode_Private_ProcessTagItemMulti(pMe,
+ pItem,
+ uTagRequirement,
+ QCBORDecode_Private_BigNumberTypes,
+ QCBORDecode_Private_BigNumberTagNumbers,
+ QCBORDecode_StringsTagCB,
+ uOffset);
+ if(pMe->uLastError) {
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBORDecode_ProcessBigNumber(*pItem, BigNumberBuf, pBigNumber, pbIsNegative);
}
@@ -7942,88 +8232,122 @@
* Public function, see header qcbor/qcbor_decode.h
*/
void
-QCBORDecode_GetBigNumPreferred(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- UsefulBuf BigNumBuf,
- UsefulBufC *pValue,
- bool *pbIsNegative)
-{
- QCBORItem Item;
- size_t uOffset;
-
- QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
- QCBOR_Private_ProcessPreferredBigNum(pMe, uTagRequirement, &Item, uOffset, BigNumBuf, pValue, pbIsNegative);
-}
-
-
-
-void
-QCBORDecode_GetBigNumber(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative)
+QCBORDecode_GetTBigNumber(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
- QCBOR_Private_ProcessBigNumber(pMe,
- uTagRequirement,
- &Item,
- uOffset,
- BigNumberBuf,
- pBigNumber,
- pbIsNegative);
+ QCBORDecode_Private_BigNumberMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative);
}
+
/*
* Public function, see header qcbor/qcbor_decode.h
*/
void
-QCBORDecode_GetPreferredBignumInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- UsefulBuf BigNumBuf,
- UsefulBufC *pValue,
- bool *pbIsNegative)
+QCBORDecode_GetTBigNumberInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBOR_Private_ProcessPreferredBigNum(pMe,
- uTagRequirement,
- &Item,
- uOffset,
- BigNumBuf,
- pValue,
- pbIsNegative);
+ QCBORDecode_Private_BigNumberMain(pMe,
+ uTagRequirement,
+ &Item,
+ uOffset,
+ BigNumberBuf,
+ pBigNumber,
+ pbIsNegative);
}
/*
* Public function, see header qcbor/qcbor_decode.h
*/
void
-QCBORDecode_GetPreferredBignumInMapSZ(QCBORDecodeContext *pMe,
- const char * szLabel,
- const uint8_t uTagRequirement,
- UsefulBuf BigNumBuf,
- UsefulBufC *pValue,
- bool *pbIsNegative)
+QCBORDecode_GetTBigNumberInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
{
QCBORItem Item;
size_t uOffset;
QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBOR_Private_ProcessPreferredBigNum(pMe,
- uTagRequirement,
- &Item,
- uOffset,
- BigNumBuf,
- pValue,
- pbIsNegative);
+ QCBORDecode_Private_BigNumberMain(pMe,
+ uTagRequirement,
+ &Item,
+ uOffset,
+ BigNumberBuf,
+ pBigNumber,
+ pbIsNegative);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h
+ */
+void
+QCBORDecode_GetTBigNumberNoPreferred(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset);
+ QCBORDecode_Private_BigNumberNoPreferredMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative);
+}
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h
+ */
+void
+QCBORDecode_GetTBigNumberNoPreferredInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_BigNumberNoPreferredMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative);
+
+}
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h
+ */
+void
+QCBORDecode_GetTBigNumberNoPreferredInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ QCBORItem Item;
+ size_t uOffset;
+
+ QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
+ QCBORDecode_Private_BigNumberNoPreferredMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative);
}
// TODO: re order above functions in tag number order
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 28ff84d..49dfa2a 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -764,51 +764,55 @@
-/* Actual addition of a positive/negative big num tag */
-static void
-QCBOREncode_Private_AddTBignum(QCBOREncodeContext *pMe,
- const uint64_t uTag,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
+/**
+ * @brief Convert a big number to unsigned integer.
+ *
+ * @param[in] BigNumber Big number to convert.
+ *
+ * @return Converted unsigned.
+ *
+ * The big number must be less than 8 bytes long.
+ **/
+static uint64_t
+QCBOREncode_Private_BigNumberToUInt(const UsefulBufC BigNumber)
{
- if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- QCBOREncode_AddTagNumber(pMe, uTag);
+ uint64_t uInt;
+ size_t uIndex;
+
+ uInt = 0;
+ for(uIndex = 0; uIndex < BigNumber.len; uIndex++) {
+ uInt = (uInt << 8) + UsefulBufC_NTH_BYTE(BigNumber, uIndex);
}
- QCBOREncode_AddBytes(pMe, BigNum);
+
+ return uInt;
}
-/* Add a positive/negative big num, non-preferred */
-static void
-QCBOREncode_Private_AddTBignumNoPreferred(QCBOREncodeContext *pMe,
- const uint64_t uTag,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
-{
-#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
- if(pMe->uMode >= QCBOR_ENCODE_MODE_PREFERRED) {
- pMe->uError = QCBOR_ERR_NOT_PREFERRED;
- return;
- }
-#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
-
- QCBOREncode_Private_AddTBignum(pMe, uTag, uTagRequirement, BigNum);
-}
-
-
-/* Is there a carry when you add 1 to the BigNum? */
+/**
+ * @brief Is there a carry when you subtract 1 from the BigNumber.
+ *
+ * @param[in] BigNumber Big number to check for carry.
+ *
+ * @return If there is a carry, \c true.
+ *
+ * If this returns @c true, then @c BigNumber - 1 is
+ * one byte shorter than @c BigNumber.
+ **/
static bool
-QCBOREncode_Private_BigNumCarry(UsefulBufC BigNum)
+QCBOREncode_Private_BigNumberCarry(const UsefulBufC BigNumber)
{
bool bCarry;
UsefulBufC SubBigNum;
- if(BigNum.len == 0) {
- return true; /* Adding one to zero-length string gives a carry */
+ // Improvement: rework without recursion?
+
+ if(BigNumber.len == 0) {
+ return true; /* Subtracting one from zero-length string gives a carry */
} else {
- SubBigNum = UsefulBuf_Tail(BigNum, 1);
- bCarry = QCBOREncode_Private_BigNumCarry(SubBigNum);
- if(*(const uint8_t *)BigNum.ptr == 0x00 && bCarry) {
+ SubBigNum = UsefulBuf_Tail(BigNumber, 1);
+ bCarry = QCBOREncode_Private_BigNumberCarry(SubBigNum);
+ if(UsefulBufC_NTH_BYTE(BigNumber, 0) == 0x00 && bCarry) {
+ /* Subtracting one from 0 gives a carry */
return true;
} else {
return false;
@@ -818,12 +822,17 @@
/*
- * Output negative bignum bytes with subtraction of 1
+ * @brief Output negative bignum bytes with subtraction of 1.
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
+ * @ref QCBOR_ENCODE_AS_BORROWED.
+ * @param[in] BigNumber The negative big number.
*/
-void
-QCBOREncode_Private_AddTNegativeBignum(QCBOREncodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
+static void
+QCBOREncode_Private_AddTNegativeBigNumber(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC BigNumber)
{
size_t uLen;
bool bCarry;
@@ -832,9 +841,7 @@
UsefulBufC SubString;
UsefulBufC NextSubString;
- if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- QCBOREncode_AddTagNumber(pMe, CBOR_TAG_NEG_BIGNUM);
- }
+ QCBOREncode_Private_BigNumberTag(pMe, uTagRequirement, true);
/* This works on any length without the need of an additional buffer */
@@ -846,26 +853,31 @@
* 0xff -> 0xfe
* 0xff 0x00 -> 0xfe 0xff
* 0x01 0x00 0x00 -> 0xff 0xff
+ *
+ * This outputs the big number a byte at a time to be able to operate on
+ * a big number of any length without memory allocation.
*/
- /* Compute the length up front because it goes in the head */
- bCarry = QCBOREncode_Private_BigNumCarry(UsefulBuf_Tail(BigNum, 1));
- uLen = BigNum.len;
- if(bCarry && *(const uint8_t *)BigNum.ptr >= 1 && BigNum.len > 1) {
+ /* Compute the length up front because it goes in the encoded head */
+ bCarry = QCBOREncode_Private_BigNumberCarry(UsefulBuf_Tail(BigNumber, 1));
+ uLen = BigNumber.len;
+ if(bCarry && BigNumber.len > 1 && UsefulBufC_NTH_BYTE(BigNumber, 0) >= 1) {
uLen--;
}
QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, uLen, 0);
- SubString = BigNum;
+ SubString = BigNumber;
bCopiedSomething = false;
while(SubString.len) {
- uByte = *((const uint8_t *)SubString.ptr);
+ uByte = UsefulBufC_NTH_BYTE(SubString, 0);
NextSubString = UsefulBuf_Tail(SubString, 1);
- bCarry = QCBOREncode_Private_BigNumCarry(NextSubString);
+ bCarry = QCBOREncode_Private_BigNumberCarry(NextSubString);
if(bCarry) {
uByte--;
}
- if(bCopiedSomething || NextSubString.len == 0 || uByte != 0) { /* No leading zeros, but one zero is OK */
+ /* This avoids all but the last leading zero. See
+ * QCBOREncode_Private_SkipLeadingZeros() */
+ if(bCopiedSomething || NextSubString.len == 0 || uByte != 0) {
UsefulOutBuf_AppendByte(&(pMe->OutBuf), uByte);
bCopiedSomething = true;
}
@@ -874,201 +886,135 @@
}
-static UsefulBufC
-QCBOREncode_Private_RemoveLeadingZeros(UsefulBufC String)
-{
- while(String.len > 1) {
- if(*(const uint8_t *)String.ptr) {
- break;
- }
- String.len--;
- String.ptr = (const uint8_t *)String.ptr + 1;
- }
-
- return String;
-}
-
-
-/*
- * Public function. See qcbor/qcbor_encode.h
+/**
+ * @brief Convert a negative big number to unsigned int if possible.
+ *
+ * @param[in] BigNumber The negative big number.
+ * @param[out] puInt The converted negative big number.
+ *
+ * @return If conversion was possible, returns @c true.
+ *
+ * The parameters here are unsigned integers, but they always
+ * represent negative numbers.
+ *
+ * Conversion is possible if the big number is greater than -(2^64).
+ * Conversion include offset of 1 for encoding CBOR negative numbers.
*/
-void
-QCBOREncode_AddTNegativeBignumNoPreferred(QCBOREncodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
+static bool
+QCBOREncode_Private_NegativeBigNumberToUInt(const UsefulBufC BigNumber, uint64_t *puInt)
{
-#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
- if(pMe->uMode >= QCBOR_ENCODE_MODE_PREFERRED) {
- pMe->uError = QCBOR_ERR_NOT_PREFERRED;
- return;
- }
-#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+ bool bIs2exp64;
- if(UsefulBuf_IsValue(BigNum, 0) == SIZE_MAX) {
- pMe->uError = QCBOR_ERR_NO_NEGATIVE_ZERO;
- return;
- }
-
- if(pMe->uConfig & QCBOR_ENCODE_CONFIG_V1_COMPAT) {
- QCBOREncode_Private_AddTBignum(pMe, CBOR_TAG_NEG_BIGNUM, uTagRequirement, BigNum);
- } else {
- QCBOREncode_Private_AddTNegativeBignum(pMe, uTagRequirement, QCBOREncode_Private_RemoveLeadingZeros(BigNum));
- }
-}
-
-
-void
-QCBOREncode_AddTNegativeBignumNoPreferredToMap(QCBOREncodeContext *pMe,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber)
-{
- QCBOREncode_AddSZString(pMe, szLabel);
- QCBOREncode_AddTNegativeBignumNoPreferred(pMe, uTagRequirement, BigNumber);
-}
-
-
-void
-QCBOREncode_AddTNegativeBignumNoPreferredToMapN(QCBOREncodeContext *pMe,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBufC BigNumber)
-{
- QCBOREncode_AddInt64(pMe, nLabel);
- QCBOREncode_AddTNegativeBignumNoPreferred(pMe, uTagRequirement, BigNumber);
-}
-
-/*
- * Public function. See qcbor/qcbor_encode.h
- */
-void
-QCBOREncode_AddTPositiveBignumNoPreferred(QCBOREncodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
-{
- QCBOREncode_Private_AddTBignumNoPreferred(pMe, CBOR_TAG_POS_BIGNUM, uTagRequirement, BigNum);
-}
-
-void
-QCBOREncode_AddTPositiveBignumNoPreferredToMap(QCBOREncodeContext *pMe,
- const char *szLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
-{
- QCBOREncode_AddSZString(pMe, szLabel);
- QCBOREncode_Private_AddTBignumNoPreferred(pMe, CBOR_TAG_POS_BIGNUM, uTagRequirement, BigNum);
-}
-
-void
-QCBOREncode_AddTPositiveBignumNoPreferredToMapN(QCBOREncodeContext *pMe,
- int64_t nLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
-{
- QCBOREncode_AddInt64(pMe, nLabel);
- QCBOREncode_Private_AddTBignumNoPreferred(pMe, CBOR_TAG_POS_BIGNUM, uTagRequirement, BigNum);
-}
-
-
-
-
-/* This will return an erroneous value if BigNum.len > 8 */
-/* Convert from bignum to uint with endianess conversion */
-static uint64_t
-QCBOREncode_Private_BigNumToUInt(const UsefulBufC BigNum)
-{
- uint64_t uInt;
- size_t uIndex;
-
- uInt = 0;
- for(uIndex = 0; uIndex < BigNum.len; uIndex++) {
- uInt = (uInt << 8) + ((const uint8_t *)BigNum.ptr)[uIndex];
- }
-
- return uInt;
-}
-
-
-/*
- * Public function. See qcbor/qcbor_encode.h
- */
-void
-QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
-{
- if(pMe->uConfig & QCBOR_ENCODE_CONFIG_V1_COMPAT) {
- QCBOREncode_AddTPositiveBignumNoPreferred(pMe, uTagRequirement, BigNum);
- } else {
- const UsefulBufC BigNumNLZ = QCBOREncode_Private_RemoveLeadingZeros(BigNum);
- if(BigNumNLZ.len <= 8) {
- /* Preferred serialization requires conversion to type 0 */
- QCBOREncode_AddUInt64(pMe, QCBOREncode_Private_BigNumToUInt(BigNumNLZ));
- } else {
- QCBOREncode_Private_AddTBignum(pMe, CBOR_TAG_POS_BIGNUM, uTagRequirement, BigNumNLZ);
- }
- }
-}
-
-
-/*
- * Public function. See qcbor/qcbor_encode.h
- */
-void
-QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNum)
-{
- uint64_t uInt;
- bool bIs2exp64;
static const uint8_t twoExp64[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- if(UsefulBuf_IsValue(BigNum, 0) == SIZE_MAX) {
- pMe->uError = QCBOR_ERR_NO_NEGATIVE_ZERO;
- return;
+ bIs2exp64 = ! UsefulBuf_Compare(BigNumber, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(twoExp64));
+
+ if(BigNumber.len > 8 && !bIs2exp64) {
+ return false;
}
- if(pMe->uConfig & QCBOR_ENCODE_CONFIG_V1_COMPAT) {
- QCBOREncode_AddTNegativeBignumNoPreferred(pMe, uTagRequirement, BigNum);
+ /* Must convert to CBOR type 1, a negative integer */
+ if(bIs2exp64) {
+ /* 2^64 is a 9 byte big number. Since negative numbers are offset
+ * by one in CBOR, it can be encoded as a type 1 negative. The
+ * conversion below won't work because the uInt will overflow
+ * before the subtraction of 1.
+ */
+ *puInt = UINT64_MAX;
+ } else {
+ *puInt = QCBOREncode_Private_BigNumberToUInt(BigNumber);
+ (*puInt)--; /* CBOR's negative offset of 1 */
+ }
+ return true;
+}
+
+
+/**
+ * @brief Remove leading zeros.
+ *
+ * @param[in] BigNumber The negative big number.
+ *
+ * @return Big number with no leading zeros.
+ *
+ * If the big number is all zeros, this returns a big number
+ * that is one zero rather than the empty string.
+ *
+ * 3.4.3 does not explicitly decoders MUST handle the empty string,
+ * but does say decoders MUST handle leading zeros. So Postel's Law
+ * is applied here and 0 is not encoded as an empty string.
+ */
+static UsefulBufC
+QCBOREncode_Private_SkipLeadingZeros(const UsefulBufC BigNumber)
+{
+ UsefulBufC NLZ;
+ NLZ = UsefulBuf_SkipLeading(BigNumber, 0x00);
+
+ /* An all-zero string reduces to one 0, not an empty string. */
+ if(NLZ.len == 0 && BigNumber.len > 0 && UsefulBufC_NTH_BYTE(BigNumber, 0) == 0x00) {
+ NLZ.len++;
+ }
+
+ return NLZ;
+}
+
+
+/*
+ * Public functions for adding a big number. See qcbor/qcbor_encode.h
+ */
+void
+QCBOREncode_AddTBigNumber(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const bool bNegative,
+ const UsefulBufC BigNumber)
+{
+ uint64_t uInt;
+
+ const UsefulBufC BigNumberNLZ = QCBOREncode_Private_SkipLeadingZeros(BigNumber);
+
+ /* Preferred serialization requires reduction to type 0 and 1 integers */
+ if(bNegative) {
+ if(QCBOREncode_Private_NegativeBigNumberToUInt(BigNumberNLZ, &uInt)) {
+ /* Might be a 65-bit negative; use special add method for such */
+ QCBOREncode_AddNegativeUInt64(pMe, uInt);
+ } else {
+ QCBOREncode_Private_AddTNegativeBigNumber(pMe, uTagRequirement, BigNumberNLZ);
+ }
} else {
- /* Here we do preferred serialization. That requires removal of leading zeros */
- const UsefulBufC BigNumNLZ = QCBOREncode_Private_RemoveLeadingZeros(BigNum);
-
- bIs2exp64 = ! UsefulBuf_Compare(BigNumNLZ, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(twoExp64));
-
- if(BigNumNLZ.len <= 8 || bIs2exp64) {
- /* Must convert to CBOR type 1, a negative integer */
- if(bIs2exp64) {
- /* 2^64 is a 9 byte big number. Since negative numbers are offset
- * by one in CBOR, it can be encoded as a type 1 negative. The
- * conversion below won't work because the uInt will overflow
- * before the subtraction of 1.
- */
- uInt = UINT64_MAX;
- } else {
- uInt = QCBOREncode_Private_BigNumToUInt(BigNumNLZ);
- uInt--; /* CBOR's negative offset of 1 */
- }
- QCBOREncode_AddNegativeUInt64(pMe, uInt);
-
+ if(BigNumberNLZ.len <= sizeof(uint64_t)) {
+ QCBOREncode_AddUInt64(pMe, QCBOREncode_Private_BigNumberToUInt(BigNumberNLZ));
} else {
- QCBOREncode_Private_AddTNegativeBignum(pMe, uTagRequirement, BigNumNLZ);
+ QCBOREncode_AddTBigNumberRaw(pMe, bNegative, uTagRequirement, BigNumberNLZ);
}
}
}
+/*
+ * Public functions for adding a big number. See qcbor/qcbor_encode.h
+ */
+void
+QCBOREncode_AddTBigNumberNoPreferred(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const bool bNegative,
+ const UsefulBufC BigNumber)
+{
+ const UsefulBufC BigNumberNLZ = QCBOREncode_Private_SkipLeadingZeros(BigNumber);
+
+ if(bNegative) {
+ QCBOREncode_Private_AddTNegativeBigNumber(pMe, uTagRequirement, BigNumberNLZ);
+ } else {
+ QCBOREncode_AddTBigNumberRaw(pMe, false, uTagRequirement, BigNumberNLZ);
+ }
+}
+
+
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
/**
* @brief Semi-private method to add bigfloats and decimal fractions.
*
* @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] uTagNumber The type 6 tag indicating what this is to be.
* @param[in] nMantissa The @c int64_t mantissa if it is not a big number.
* @param[in] nExponent The exponent.
*
@@ -1091,12 +1037,11 @@
* is called instead of this.
*/
void
-QCBOREncode_Private_AddExpMantissa(QCBOREncodeContext *pMe,
- const uint64_t uTag,
- const int64_t nExponent,
- const UsefulBufC BigNumMantissa,
- const bool bBigNumIsNegative,
- const int64_t nMantissa)
+QCBOREncode_Private_AddTExpIntMantissa(QCBOREncodeContext *pMe,
+ const int uTagRequirement,
+ const uint64_t uTagNumber,
+ const int64_t nExponent,
+ const int64_t nMantissa)
{
/* 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
@@ -1104,22 +1049,72 @@
* is base-2 for big floats and base-10 for decimal fractions, but
* that has no effect on the code here.
*/
- if(uTag != CBOR_TAG_INVALID64) {
- QCBOREncode_AddTagNumber(pMe, uTag);
+ /* Separate from QCBOREncode_Private_AddTExpBigMantissa() because
+ * linking QCBOREncode_AddTBigNumber() adds a lot because it
+ * does preferred serialization of big numbers and the offset of 1
+ * for CBOR negative numbers.
+ */
+ if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
+ QCBOREncode_AddTagNumber(pMe, uTagNumber);
}
QCBOREncode_OpenArray(pMe);
QCBOREncode_AddInt64(pMe, nExponent);
- if(!UsefulBuf_IsNULLC(BigNumMantissa)) {
- if(bBigNumIsNegative) {
- QCBOREncode_AddNegativeBignum(pMe, BigNumMantissa);
- } else {
- QCBOREncode_AddPositiveBignum(pMe, BigNumMantissa);
- }
- } else {
- QCBOREncode_AddInt64(pMe, nMantissa);
- }
+ QCBOREncode_AddInt64(pMe, nMantissa);
QCBOREncode_CloseArray(pMe);
}
+
+void
+QCBOREncode_Private_AddTExpBigMantissa(QCBOREncodeContext *pMe,
+ const int uTagRequirement,
+ const uint64_t uTagNumber,
+ const int64_t nExponent,
+ const UsefulBufC BigNumMantissa,
+ const bool bBigNumIsNegative)
+{
+ /* 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
+ * mantissa. The difference between the two is that the exponent
+ * is base-2 for big floats and base-10 for decimal fractions, but
+ * that has no effect on the code here.
+ */
+ if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
+ QCBOREncode_AddTag(pMe, uTagNumber);
+ }
+ QCBOREncode_OpenArray(pMe);
+ QCBOREncode_AddInt64(pMe, nExponent);
+ QCBOREncode_AddTBigNumber(pMe, QCBOR_ENCODE_AS_TAG, bBigNumIsNegative, BigNumMantissa);
+ QCBOREncode_CloseArray(pMe);
+}
+
+
+void
+QCBOREncode_Private_AddTExpBigMantissaRaw(QCBOREncodeContext *pMe,
+ const int uTagRequirement,
+ const uint64_t uTagNumber,
+ const int64_t nExponent,
+ const UsefulBufC BigNumMantissa,
+ const bool bBigNumIsNegative)
+{
+ /* 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
+ * mantissa. The difference between the two is that the exponent
+ * is base-2 for big floats and base-10 for decimal fractions, but
+ * that has no effect on the code here.
+ */
+ /* Separate from QCBOREncode_Private_AddTExpBigMantissa() because
+ * linking QCBOREncode_AddTBigNumber() adds a lot because it
+ * does preferred serialization of big numbers and the offset of 1
+ * for CBOR negative numbers.
+ */
+ if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
+ QCBOREncode_AddTag(pMe, uTagNumber);
+ }
+ QCBOREncode_OpenArray(pMe);
+ QCBOREncode_AddInt64(pMe, nExponent);
+ QCBOREncode_AddTBigNumberRaw(pMe, QCBOR_ENCODE_AS_TAG, bBigNumIsNegative, BigNumMantissa);
+ QCBOREncode_CloseArray(pMe);
+}
+
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
diff --git a/src/qcbor_tag_decode.c b/src/qcbor_tag_decode.c
index 6479fa2..36e8dae 100644
--- a/src/qcbor_tag_decode.c
+++ b/src/qcbor_tag_decode.c
@@ -158,20 +158,16 @@
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
/**
- * @brief Figures out data type for exponent mantissa tags.
+ * @brief Figures out QCBOR data type for exponent and mantissa tags.
*
* @param[in] uTagToProcess Either @ref CBOR_TAG_DECIMAL_FRACTION or
* @ref CBOR_TAG_BIG_FLOAT.
* @param[in] pDecodedItem Item being decoded.
*
- * @returns One of the 6 values between \ref QCBOR_TYPE_DECIMAL_FRACTION
- * and @ref QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM.
+ * @returns One of the ten values related to @ref QCBOR_TYPE_DECIMAL_FRACTION and @ref QCBOR_TYPE_BIGFLOAT
*
- * Does mapping between a CBOR tag number and a QCBOR type. with a
- * little bit of logic and arithmatic.
- *
- * Used in serveral contexts. Does the work where sometimes the data
- * item is explicitly tagged and sometimes not.
+ * Does mapping between a CBOR tag number and a QCBOR type with a
+ * little logic and arithmetic.
*/
static uint8_t
QCBOR_Private_ExpMantissaDataType(const uint64_t uTagToProcess,
@@ -180,10 +176,20 @@
uint8_t uBase = uTagToProcess == CBOR_TAG_DECIMAL_FRACTION ?
QCBOR_TYPE_DECIMAL_FRACTION :
QCBOR_TYPE_BIGFLOAT;
- if(pDecodedItem->uDataType != QCBOR_TYPE_INT64) {
- uBase = (uint8_t)(uBase + pDecodedItem->uDataType - QCBOR_TYPE_POSBIGNUM + 1);
+
+ switch(pDecodedItem->uDataType) {
+ case QCBOR_TYPE_INT64:
+ return uBase;
+
+ case QCBOR_TYPE_UINT64:
+ return uBase + (QCBOR_TYPE_DECIMAL_FRACTION_POS_U64 - QCBOR_TYPE_DECIMAL_FRACTION); // TODO: test this
+
+ case QCBOR_TYPE_65BIT_NEG_INT:
+ return uBase + (QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64 - QCBOR_TYPE_DECIMAL_FRACTION);
+
+ default:
+ return (uint8_t)(uBase + pDecodedItem->uDataType - QCBOR_TYPE_POSBIGNUM + 1);
}
- return uBase;
}
@@ -197,6 +203,8 @@
(void)pTagDecodersContext;
QCBORError uReturn;
+ QCBORItem ExponentItem;
+ QCBORItem MantissaItem;
/* --- Make sure it is an array; track nesting level of members --- */
if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) {
@@ -211,7 +219,6 @@
const uint8_t uNestLevel = pDecodedItem->uNestingLevel + 1;
/* --- Get the exponent --- */
- QCBORItem ExponentItem;
uReturn = QCBORDecode_GetNext(pDecodeCtx, &ExponentItem);
if(uReturn != QCBOR_SUCCESS) {
goto Done;
@@ -235,7 +242,6 @@
}
/* --- Get the mantissa --- */
- QCBORItem MantissaItem;
uReturn = QCBORDecode_GetNext(pDecodeCtx, &MantissaItem);
if(uReturn != QCBOR_SUCCESS) {
goto Done;
@@ -263,8 +269,12 @@
/* Got a good big num mantissa */
pDecodedItem->val.expAndMantissa.Mantissa.bigNum = MantissaItem.val.bigNum;
#endif /* QCBOR_DISABLE_TAGS */
+ } else if(MantissaItem.uDataType == QCBOR_TYPE_UINT64) {
+ pDecodedItem->val.expAndMantissa.Mantissa.uInt = MantissaItem.val.uint64;
+ } else if(MantissaItem.uDataType == QCBOR_TYPE_65BIT_NEG_INT) {
+ pDecodedItem->val.expAndMantissa.Mantissa.uInt = MantissaItem.val.uint64;
} else {
- /* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */
+ /* Wrong type of mantissa */
uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
goto Done;
}
diff --git a/test/UsefulBuf_Tests.c b/test/UsefulBuf_Tests.c
index 338e6db..ae3862c 100644
--- a/test/UsefulBuf_Tests.c
+++ b/test/UsefulBuf_Tests.c
@@ -704,6 +704,31 @@
return "Failed to find 4";
}
+ UsefulBufC Substring;
+ Substring = UsefulBuf_SkipLeading(UsefulBuf_FromSZ("xxyyzz"), 'x');
+ if(UsefulBuf_Compare(Substring, UsefulBuf_FromSZ("yyzz"))) {
+ return "SkipLeading didn't skip";
+ }
+
+ Substring = UsefulBuf_SkipLeading(UsefulBuf_FromSZ("xxyyzz"), 'y');
+ if(UsefulBuf_Compare(Substring, UsefulBuf_FromSZ("xxyyzz"))) {
+ return "SkipLeading skipped";
+ }
+
+ Substring = UsefulBuf_SkipLeading(UsefulBuf_FromSZ("qqq"), 'q');
+ if(UsefulBuf_Compare(Substring, UsefulBuf_FromSZ(""))) {
+ return "SkipLeading didn't return empty";
+ }
+
+ Substring = UsefulBuf_SkipLeading(UsefulBuf_FromSZ("x"), 'x');
+ if(UsefulBuf_Compare(Substring, UsefulBuf_FromSZ(""))) {
+ return "SkipLeading didn't return empty";
+ }
+
+ Substring = UsefulBuf_SkipLeading(UsefulBuf_FromSZ("xxxxxxxxxxxx"), 'x');
+ if(UsefulBuf_Compare(Substring, UsefulBuf_FromSZ(""))) {
+ return "SkipLeading didn't return empty";
+ }
const uint8_t pB[] = {0x01, 0x02, 0x03};
UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index f5c95e1..adcf9b7 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -2610,7 +2610,7 @@
}
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
- if(nIndex == 52) {
+ if(nIndex == 0) {
uCBORError = 9; /* For setting break points */
}
@@ -4753,7 +4753,6 @@
{"\x80\x00\x00\x00\x00\x00\x00\x00", 8},
true
},
-
{
"Preferred -1",
{"\x20", 1},
@@ -4955,7 +4954,7 @@
for(uTestIndex = 0; uTestIndex < uTestCount; uTestIndex++) {
pTest = &BignumDecodeTests[uTestIndex];
- if(uTestIndex == 13) {
+ if(uTestIndex == 9) {
bIsNeg = false; /* Line of code so a break point can be set. */
}
@@ -4967,7 +4966,7 @@
return MakeTestResultCode(uTestIndex, 1, uErr);
}
- uErr = QCBORDecode_BignumPreferred(Item, BignumBuf, &ResultBigNum, &bIsNeg);
+ uErr = QCBORDecode_ProcessBigNumber(Item, BignumBuf, &ResultBigNum, &bIsNeg);
if(uErr != pTest->uErr) {
return MakeTestResultCode(uTestIndex, 2, uErr);
}
@@ -4984,15 +4983,14 @@
return MakeTestResultCode(uTestIndex, 4, 0);
}
- uErr = QCBORDecode_BignumPreferred(Item, (UsefulBuf){NULL, 200}, &ResultBigNum, &bIsNeg);
+ uErr = QCBORDecode_ProcessBigNumber(Item, (UsefulBuf){NULL, 200}, &ResultBigNum, &bIsNeg);
if(ResultBigNum.len != pTest->ExpectedBigNum.len) {
return MakeTestResultCode(uTestIndex, 5, uErr);
}
QCBORDecode_Init(&DCtx, pTest->Encoded, 0);
QCBORDecode_CompatibilityV1(&DCtx);
-
- QCBORDecode_GetBigNumPreferred(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, BignumBuf, &ResultBigNum, &bIsNeg);
+ QCBORDecode_GetTBigNumber(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, BignumBuf, &ResultBigNum, &bIsNeg);
uErr = QCBORDecode_GetError(&DCtx);
if(uErr != QCBOR_SUCCESS) {
return MakeTestResultCode(uTestIndex, 6, uErr);
@@ -5960,6 +5958,7 @@
uint8_t uQCBORTypeGN;
int64_t nExponentGN;
int64_t nMantissaGN;
+ uint64_t uMantissaGU;
UsefulBufC MantissaGN;
/* Expected values for GetDecimalFraction */
@@ -5998,6 +5997,7 @@
QCBOR_TYPE_ARRAY,
0,
0,
+ 0UL,
{(const uint8_t []){0x00}, 1},
QCBOR_SUCCESS, /* GetDecimalFraction */
@@ -6018,7 +6018,6 @@
{(const uint8_t []){0x03}, 1},
false
},
-
{
"2. Untagged pair (big float or decimal fraction), tag required",
{(const uint8_t []){0x82, 0x20, 0x03}, 3},
@@ -6029,6 +6028,7 @@
QCBOR_TYPE_ARRAY,
0,
0,
+ 0UL,
{(const uint8_t []){0x00}, 1},
QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
@@ -6048,9 +6048,7 @@
0,
{(const uint8_t []){0x00}, 1},
false
-
},
-
{
"3. Tagged 1.5 decimal fraction, tag 4 optional",
{(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
@@ -6061,6 +6059,7 @@
QCBOR_TYPE_DECIMAL_FRACTION,
-1,
3,
+ 0UL,
{(const uint8_t []){0x00}, 1},
@@ -6077,7 +6076,7 @@
0,
0,
- QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */ // TODO: think about error code here
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
0,
{(const uint8_t []){0x00}, 1},
false
@@ -6092,9 +6091,9 @@
QCBOR_TYPE_BIGFLOAT,
300,
100,
+ 0UL,
{(const uint8_t []){0x00}, 1},
-
QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */ // TODO: think about error code
0,
0,
@@ -6124,6 +6123,7 @@
QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
-20,
0,
+ 0UL,
{(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
@@ -6144,7 +6144,6 @@
{(const uint8_t []){0x00}, 0},
false
},
-
{
"6. Error: Mantissa and exponent inside a Mantissa and exponent",
{(const uint8_t []){0xC4, 0x82, 0x33,
@@ -6156,6 +6155,7 @@
QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
0,
0,
+ 0UL,
{(const uint8_t []){0x00}, 0},
QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
@@ -6176,7 +6176,6 @@
{(const uint8_t []){0x00}, 0},
false
},
-
{
"7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
{(const uint8_t []){0xC5, 0x82, 0x33,
@@ -6188,6 +6187,7 @@
QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
-20,
0,
+ 0UL,
{(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
@@ -6208,7 +6208,6 @@
{(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
false
},
-
{
/* Special case for test 8. Don't renumber it. */
"8. Untagged pair with big num (big float or decimal fraction), tag optional",
@@ -6220,6 +6219,7 @@
QCBOR_TYPE_ARRAY,
0,
0,
+ 0UL,
{(const uint8_t []){0x00}, 1},
QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
@@ -6240,7 +6240,6 @@
{(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
false
},
-
{
"9. decimal fraction with large exponent and negative big num mantissa",
{(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
@@ -6252,6 +6251,7 @@
QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
9223372036854775807,
0,
+ 0UL,
{(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
@@ -6260,7 +6260,7 @@
QCBOR_SUCCESS, /* for GetDecimalFractionBig */
9223372036854775807,
- {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x11}, 10},
true,
QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
@@ -6272,6 +6272,102 @@
{(const uint8_t []){0x00}, 1},
false
},
+ {
+ "10. big float with large exponent and negative big num mantissa",
+ {(const uint8_t []){0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
+ QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM,
+ 9223372036854775807,
+ 0,
+ 0UL,
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
+ 0,
+ {(const uint8_t []){0x00}, 1},
+ false,
+
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_SUCCESS, /* for GetBigFloatBig */
+ 9223372036854775807,
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x11}, 10},
+ true
+ },
+ {
+ "11. big float with large exponent and negative big num mantissa",
+ {(const uint8_t []){0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0x3B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 20},
+ QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_BIGFLOAT_NEG_U64,
+ 9223372036854775807,
+ 0,
+ 0xffffffffffffffff,
+ {(const uint8_t []){0x00}, 0},
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
+ 0,
+ {(const uint8_t []){0x00}, 1},
+ false,
+
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_SUCCESS, /* for GetBigFloatBig */
+ 9223372036854775807,
+ {(const uint8_t []){0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 9},
+ true
+ },
+ {
+ "12. big float with large exponent and positive unsigned mantissa",
+ {(const uint8_t []){0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 20},
+ QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_BIGFLOAT_POS_U64,
+ 9223372036854775807,
+ 0,
+ 0xffffffffffffffff,
+ {(const uint8_t []){0x00}, 0},
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
+ 0,
+ {(const uint8_t []){0x00}, 1},
+ false,
+
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_SUCCESS, /* for GetBigFloatBig */
+ 9223372036854775807,
+ {(const uint8_t []){0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8},
+ false
+ }
};
@@ -6289,14 +6385,14 @@
for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
const struct EaMTest *pT = &pEaMTests[uIndex];
- /* Decode with GetNext */
- QCBORDecode_Init(&DCtx, pT->Input, 0);
- QCBORDecode_CompatibilityV1(&DCtx);
- if(uIndex + 1 == 9) {
+ if(uIndex + 1 == 11) {
nExponent = 99; // just to set a break point
}
+ /* --- Decode with GetNext --- */
+ QCBORDecode_Init(&DCtx, pT->Input, 0);
+ QCBORDecode_CompatibilityV1(&DCtx);
uError = QCBORDecode_GetNext(&DCtx, &Item);
#ifdef QCBOR_DISABLE_TAGS
/* Test 8 is a special case when tags are disabled */
@@ -6321,6 +6417,11 @@
if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
return (int32_t)(1+uIndex) * 1000 + 4;
}
+ } else if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64 || Item.uDataType == QCBOR_TYPE_BIGFLOAT_NEG_U64 ||
+ Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION_POS_U64 || Item.uDataType == QCBOR_TYPE_BIGFLOAT_POS_U64) {
+ if(pT->uMantissaGU != Item.val.expAndMantissa.Mantissa.uInt) {
+ return (int32_t)(1+uIndex) * 1000 + 4;
+ }
} else {
if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
return (int32_t)(1+uIndex) * 1000 + 5;
@@ -6331,14 +6432,13 @@
}
#endif
- /* Decode with GetDecimalFraction */
+ /* --- Decode with GetDecimalFraction --- */
QCBORDecode_Init(&DCtx, pT->Input, 0);
QCBORDecode_CompatibilityV1(&DCtx);
-
- QCBORDecode_GetDecimalFraction(&DCtx,
- pT->uTagRequirement,
- &nMantissa,
- &nExponent);
+ QCBORDecode_GetTDecimalFraction(&DCtx,
+ pT->uTagRequirement,
+ &nMantissa,
+ &nExponent);
uError = QCBORDecode_GetAndResetError(&DCtx);
#ifdef QCBOR_DISABLE_TAGS
if(pT->bHasTags) {
@@ -6363,16 +6463,17 @@
}
#endif
- /* Decode with GetDecimalFractionBig */
+
+
+ /* --- Decode with GetDecimalFractionBig ---v*/
QCBORDecode_Init(&DCtx, pT->Input, 0);
QCBORDecode_CompatibilityV1(&DCtx);
-
- QCBORDecode_GetDecimalFractionBig(&DCtx,
- pT->uTagRequirement,
- MantissaBuf,
- &Mantissa,
- &bMantissaIsNegative,
- &nExponent);
+ QCBORDecode_GetTDecimalFractionBigMantissa(&DCtx,
+ pT->uTagRequirement,
+ MantissaBuf,
+ &Mantissa,
+ &bMantissaIsNegative,
+ &nExponent);
uError = QCBORDecode_GetAndResetError(&DCtx);
#ifdef QCBOR_DISABLE_TAGS
if(pT->bHasTags) {
@@ -6400,11 +6501,11 @@
}
#endif
- /* Decode with GetBigFloat */
+
+ /* --- Decode with GetBigFloat --- */
QCBORDecode_Init(&DCtx, pT->Input, 0);
QCBORDecode_CompatibilityV1(&DCtx);
-
- QCBORDecode_GetBigFloat(&DCtx,
+ QCBORDecode_GetTBigFloat(&DCtx,
pT->uTagRequirement,
&nMantissa,
&nExponent);
@@ -6432,11 +6533,12 @@
}
#endif
- /* Decode with GetBigFloatBig */
+
+
+ /* --- Decode with GetBigFloatBig --- */
QCBORDecode_Init(&DCtx, pT->Input, 0);
QCBORDecode_CompatibilityV1(&DCtx);
-
- QCBORDecode_GetBigFloatBig(&DCtx,
+ QCBORDecode_GetTBigFloatBigMantissa(&DCtx,
pT->uTagRequirement,
MantissaBuf,
&Mantissa,
@@ -6468,6 +6570,7 @@
#ifdef QCBOR_DISABLE_TAGS
}
#endif
+
}
return 0;
@@ -6560,16 +6663,6 @@
static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
- { "Exponent > INT64_MAX",
- QCBOR_DECODE_MODE_NORMAL,
- {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
- QCBOR_ERR_BAD_EXP_AND_MANTISSA
- },
- { "Mantissa > INT64_MAX",
- QCBOR_DECODE_MODE_NORMAL,
- {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
- QCBOR_ERR_BAD_EXP_AND_MANTISSA
- },
{
"End of input",
QCBOR_DECODE_MODE_NORMAL,
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index 76572c6..e724767 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -293,7 +293,8 @@
}
-
+/* Don't change this, make a new test instead. Keep this
+ * as it was in v1 for full regression. */
static const uint8_t spExpectedEncodedAll[] = {
0x98, 0x23, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
@@ -495,17 +496,16 @@
0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x01, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x01, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
+ 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00
};
-
static const char *szMIME = "\
MIME-Version: 1.0\n\
Content-Type: multipart/mixed;\n\
@@ -689,8 +689,9 @@
QCBOREncode_AddBoolToMapN(pECtx, 010101, true);
QCBOREncode_CloseMap(pECtx);
+
/* Big numbers */
- static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
+ static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
QCBOREncode_AddPositiveBignum(pECtx, BIGNUM);
QCBOREncode_AddNegativeBignum(pECtx, BIGNUM);
@@ -969,7 +970,7 @@
{
"2^96 -1 or 79228162514264337593543950335 pos and neg with leading zeros",
{"\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", 15},
- {"\xC2\x4F\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", 17},
+ {"\xC2\x4C\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", 14},
{"\xC2\x4C\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", 14},
{"\xC3\x4C\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe", 14},
{"\xC3\x4C\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe", 14},
@@ -1008,7 +1009,7 @@
{"\x20", 1},
},
{
- "0 and error",
+ "0 and error for no negative 0",
{"\x00", 1},
{"\xC2\x41\x00", 3},
{"\x00", 1},
@@ -1016,9 +1017,9 @@
NULLUsefulBufC,
},
{
- "leading zeros -- 0 and error",
+ "leading zeros -- 0 and error for no negative 0",
{"\x00\x00\x00\x00", 4},
- {"\xC2\x44\x00\x00\x00\x00", 6},
+ {"\xC2\x41\x00", 3},
{"\x00", 1},
NULLUsefulBufC,
NULLUsefulBufC,
@@ -1032,46 +1033,45 @@
unsigned uTestIndex;
unsigned uTestCount;
QCBOREncodeContext Enc;
- UsefulBufC B;
- const struct BigNumEncodeTest *pTest;
+ UsefulBufC EncodedBigNumber;
uTestCount = (int)C_ARRAY_COUNT(BigNumEncodeTestCases, struct BigNumEncodeTest);
for(uTestIndex = 0; uTestIndex < uTestCount; uTestIndex++) {
- pTest = &BigNumEncodeTestCases[uTestIndex];
+ const struct BigNumEncodeTest *pTest = &BigNumEncodeTestCases[uTestIndex];
- if(uTestIndex == 3) {
- B.len = 0; /* Line of code so a break point can be set. */
+ if(uTestIndex == 6) {
+ EncodedBigNumber.len = 0; /* Line of code so a break point can be set. */
}
QCBOREncode_Init(&Enc, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
- QCBOREncode_AddTPositiveBignumNoPreferred(&Enc, QCBOR_ENCODE_AS_TAG, pTest->BigNum);
- QCBOREncode_Finish(&Enc, &B);
- if(UsefulBuf_Compare(B, pTest->PositiveNoPreferred)) {
+ QCBOREncode_AddTBigNumberNoPreferred(&Enc, QCBOR_ENCODE_AS_TAG, false, pTest->BigNum);
+ QCBOREncode_Finish(&Enc, &EncodedBigNumber);
+ if(UsefulBuf_Compare(EncodedBigNumber, pTest->PositiveNoPreferred)) {
return MakeTestResultCode(uTestIndex, 1, QCBOR_SUCCESS);
}
QCBOREncode_Init(&Enc, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
- QCBOREncode_AddPositiveBignum (&Enc, pTest->BigNum);
- QCBOREncode_Finish(&Enc, &B);
- if(UsefulBuf_Compare(B, pTest->PositivePreferred)) {
+ QCBOREncode_AddTBigNumber(&Enc, QCBOR_ENCODE_AS_TAG, false, pTest->BigNum);
+ QCBOREncode_Finish(&Enc, &EncodedBigNumber);
+ if(UsefulBuf_Compare(EncodedBigNumber, pTest->PositivePreferred)) {
return MakeTestResultCode(uTestIndex, 2, QCBOR_SUCCESS);
}
if(!UsefulBuf_IsNULLC(pTest->NegativeNoPreferred)){
QCBOREncode_Init(&Enc, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
- QCBOREncode_AddTNegativeBignumNoPreferred(&Enc, QCBOR_ENCODE_AS_TAG, pTest->BigNum);
- QCBOREncode_Finish(&Enc, &B);
- if(UsefulBuf_Compare(B, pTest->NegativeNoPreferred)) {
+ QCBOREncode_AddTBigNumberNoPreferred(&Enc, QCBOR_ENCODE_AS_TAG, true, pTest->BigNum);
+ QCBOREncode_Finish(&Enc, &EncodedBigNumber);
+ if(UsefulBuf_Compare(EncodedBigNumber, pTest->NegativeNoPreferred)) {
return MakeTestResultCode(uTestIndex, 3, QCBOR_SUCCESS);
}
}
if(!UsefulBuf_IsNULLC(pTest->NegativePreferred)){
QCBOREncode_Init(&Enc, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
- QCBOREncode_AddNegativeBignum(&Enc, pTest->BigNum);
- QCBOREncode_Finish(&Enc, &B);
- if(UsefulBuf_Compare(B, pTest->NegativePreferred)) {
+ QCBOREncode_AddTBigNumber(&Enc, QCBOR_ENCODE_AS_TAG, true, pTest->BigNum);
+ QCBOREncode_Finish(&Enc, &EncodedBigNumber);
+ if(UsefulBuf_Compare(EncodedBigNumber, pTest->NegativePreferred)) {
return MakeTestResultCode(uTestIndex, 4, QCBOR_SUCCESS);
}
}
@@ -2964,6 +2964,7 @@
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+
/*
[
4([-1, 3]),
@@ -2978,7 +2979,8 @@
5([-9223372036854775808, -4759477275222530853137])]
]
*/
-static const uint8_t spExpectedExponentAndMantissaArray[] = {
+static const uint8_t spExpectedExponentAndMantissaArrayv1[] = {
+
0x8A, 0xC4, 0x82, 0x20, 0x03, 0x82, 0x20, 0x04,
0xC4, 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x82,
@@ -2986,7 +2988,8 @@
0x06, 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B,
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
- 0x07, 0x08, 0x09, 0x0F, 0xC5, 0x82, 0x19, 0x01, // TODO: adjust this back to value from master when better big num is ...
+ 0x07, 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01,
+
0x2C, 0x18, 0x64, 0x82, 0x19, 0x02, 0x58, 0x18,
0xC8, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02,
0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
@@ -2994,7 +2997,7 @@
0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0xC5, 0x82,
0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
- 0x06, 0x07, 0x08, 0x09, 0x0F}; // TODO: adjust this back to value from master when better big num is ...
+ 0x06, 0x07, 0x08, 0x09, 0x10};
/*
{
@@ -3012,7 +3015,7 @@
800: 5([-9223372036854775808, -4759477275222530853137])
}
*/
-static const uint8_t spExpectedExponentAndMantissaMap[] = {
+static const uint8_t spExpectedExponentAndMantissaMapv1[] = {
0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
@@ -3032,10 +3035,10 @@
0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
- 0x06, 0x07, 0x08, 0x09, 0x0F, 0x19, 0x01, 0xF4,
+ 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
- 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0F, 0x69,
+ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
@@ -3052,11 +3055,11 @@
0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
- 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0F,
+ 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
- 0x09, 0x0F
+ 0x09, 0x10
};
@@ -3133,72 +3136,11 @@
}
-
-int32_t
-ExponentAndMantissaEncodeTests(void)
+/* Test QCBOR v1 compatible functions */
+int32_t ExponentAndMantissaEncodeTestsv1(void)
{
QCBOREncodeContext EC;
UsefulBufC EncodedExponentAndMantissa;
- int nIndex;
- QCBORError uErr;
-
- const int nNumberOfTests = C_ARRAY_COUNT(EET, struct EAMEncodeTest);
-
- for(nIndex = 0; nIndex < nNumberOfTests; nIndex++) {
- struct EAMEncodeTest *pTest = &EET[nIndex];
-
-
- if(UsefulBuf_IsNULLC(pTest->BigNumMantissa)) {
- EAMTestSetup(pTest, &EC);
-
- QCBOREncode_AddDecimalFraction(&EC, pTest->nMantissa, pTest->nExponent);
- uErr = QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa);
- if(uErr) {
- return MakeTestResultCode((uint32_t)nIndex, 1, uErr);
- }
-
- if(UsefulBuf_Compare(EncodedExponentAndMantissa, pTest->DecFrac)) {
- return MakeTestResultCode((uint32_t)nIndex, 2, 0);
- }
-
- EAMTestSetup(pTest, &EC);
- QCBOREncode_AddBigFloat(&EC, pTest->nMantissa, pTest->nExponent);
- uErr = QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa);
- if(uErr) {
- return MakeTestResultCode((uint32_t)nIndex, 11, uErr);
- }
-
- if(UsefulBuf_Compare(EncodedExponentAndMantissa, pTest->BigFloat)) {
- return MakeTestResultCode((uint32_t)nIndex, 12, 0);
- }
-
- } else {
- EAMTestSetup(pTest, &EC);
-
- QCBOREncode_AddDecimalFractionBigNum(&EC, pTest->BigNumMantissa, pTest->bSign, pTest->nExponent);
- uErr = QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa);
- if(uErr) {
- return MakeTestResultCode((uint32_t)nIndex, 11, uErr);
- }
-
- if(UsefulBuf_Compare(EncodedExponentAndMantissa, pTest->DecFracBig)) {
- return MakeTestResultCode((uint32_t)nIndex, 12, 0);
- }
-
- EAMTestSetup(pTest, &EC);
-
- QCBOREncode_AddBigFloatBigNum(&EC, pTest->BigNumMantissa, pTest->bSign, pTest->nExponent);
- uErr = QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa);
- if(uErr) {
- return MakeTestResultCode((uint32_t)nIndex, 11, uErr);
- }
-
- if(UsefulBuf_Compare(EncodedExponentAndMantissa, pTest->BigFloatBig)) {
- return MakeTestResultCode((uint32_t)nIndex, 12, 0);
- }
- }
- }
-
// Constant for the big number used in all the tests.
static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
@@ -3226,7 +3168,7 @@
struct UBCompareDiagnostic Foo;
int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
- UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArrayv1),
&Foo);
if(nReturn) {
return nReturn;
@@ -3297,9 +3239,11 @@
return -3;
}
+
struct UBCompareDiagnostic Diag;
+
nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
- UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMapv1),
&Diag);
if(nReturn) {
return nReturn + 1000000; // +1000000 to distinguish from first test above
@@ -3308,6 +3252,84 @@
return 0;
}
+
+
+int32_t
+ExponentAndMantissaEncodeTests(void)
+{
+ QCBOREncodeContext EC;
+ UsefulBufC EncodedExponentAndMantissa;
+ int nIndex;
+ QCBORError uErr;
+
+ int32_t uReturn = ExponentAndMantissaEncodeTestsv1();
+ if(uReturn) {
+ return uReturn;
+ }
+
+ const int nNumberOfTests = C_ARRAY_COUNT(EET, struct EAMEncodeTest);
+
+ for(nIndex = 0; nIndex < nNumberOfTests; nIndex++) {
+ struct EAMEncodeTest *pTest = &EET[nIndex];
+
+
+ if(UsefulBuf_IsNULLC(pTest->BigNumMantissa)) {
+ EAMTestSetup(pTest, &EC);
+
+ QCBOREncode_AddDecimalFraction(&EC, pTest->nMantissa, pTest->nExponent);
+ uErr = QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa);
+ if(uErr) {
+ return MakeTestResultCode((uint32_t)nIndex, 1, uErr);
+ }
+
+ if(UsefulBuf_Compare(EncodedExponentAndMantissa, pTest->DecFrac)) {
+ return MakeTestResultCode((uint32_t)nIndex, 2, 0);
+ }
+
+ EAMTestSetup(pTest, &EC);
+ QCBOREncode_AddBigFloat(&EC, pTest->nMantissa, pTest->nExponent);
+ uErr = QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa);
+ if(uErr) {
+ return MakeTestResultCode((uint32_t)nIndex, 11, uErr);
+ }
+
+ if(UsefulBuf_Compare(EncodedExponentAndMantissa, pTest->BigFloat)) {
+ return MakeTestResultCode((uint32_t)nIndex, 12, 0);
+ }
+
+ } else {
+ EAMTestSetup(pTest, &EC);
+
+ //QCBOREncode_AddDecimalFractionBigNum(&EC, pTest->BigNumMantissa, pTest->bSign, pTest->nExponent);
+ QCBOREncode_AddTDecimalFractionBigMantissa(&EC, QCBOR_ENCODE_AS_TAG, pTest->BigNumMantissa, pTest->bSign, pTest->nExponent);
+ uErr = QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa);
+ if(uErr) {
+ return MakeTestResultCode((uint32_t)nIndex, 11, uErr);
+ }
+
+ if(UsefulBuf_Compare(EncodedExponentAndMantissa, pTest->DecFracBig)) {
+ return MakeTestResultCode((uint32_t)nIndex, 12, 0);
+ }
+
+ EAMTestSetup(pTest, &EC);
+
+ //QCBOREncode_AddBigFloatBigNum(&EC, pTest->BigNumMantissa, pTest->bSign, pTest->nExponent);
+ QCBOREncode_AddTBigFloatBigMantissa(&EC, QCBOR_ENCODE_AS_TAG, pTest->BigNumMantissa, pTest->bSign, pTest->nExponent);
+ uErr = QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa);
+ if(uErr) {
+ return MakeTestResultCode((uint32_t)nIndex, 11, uErr);
+ }
+
+ if(UsefulBuf_Compare(EncodedExponentAndMantissa, pTest->BigFloatBig)) {
+ return MakeTestResultCode((uint32_t)nIndex, 12, 0);
+ }
+ }
+ }
+
+ return 0;
+}
+
+
#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */