Big number code tidy and documentation update
Move the big number, big float and decimal fractions around to group them better
Clearly comment the deprecated functions
Improve documentation for big numbers, big floats and decimal fractions
Improve test coverage for big numbers a little
Optimize big number encoding
* big number and exp mantissa tidy up
* Big number documentation
* Fix commits of XCode project files
* documentation improvements
* Remove junk
---------
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/QCBOR.xcodeproj/project.pbxproj b/QCBOR.xcodeproj/project.pbxproj
index cf51075..fbe0228 100644
--- a/QCBOR.xcodeproj/project.pbxproj
+++ b/QCBOR.xcodeproj/project.pbxproj
@@ -192,6 +192,7 @@
E7C960B82800A09E00FB537C /* ub-example.c */ = {isa = PBXFileReference; indentWidth = 3; lastKnownFileType = sourcecode.c.c; path = "ub-example.c"; sourceTree = "<group>"; tabWidth = 3; };
E7CA1F152C8ACCAE0008F454 /* qcbor_tag_decode.h */ = {isa = PBXFileReference; indentWidth = 3; lastKnownFileType = sourcecode.c.h; name = qcbor_tag_decode.h; path = inc/qcbor/qcbor_tag_decode.h; sourceTree = "<group>"; tabWidth = 3; };
E7CA1F1D2C8C337E0008F454 /* qcbor_tag_decode.c */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.c; name = qcbor_tag_decode.c; path = src/qcbor_tag_decode.c; sourceTree = "<group>"; tabWidth = 3; };
+ E7E305E02CE14447000E9348 /* Numbers.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = Numbers.md; path = doc/Numbers.md; sourceTree = "<group>"; };
E7FDBF16256C969D007138A8 /* QCBOR_Disable_Indef */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = QCBOR_Disable_Indef; sourceTree = BUILT_PRODUCTS_DIR; };
E7FDBF2C257A6C1F007138A8 /* QCBOR_Disable_Indef_array */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = QCBOR_Disable_Indef_array; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
@@ -260,6 +261,7 @@
children = (
E776E161214EE19C00E67947 /* README.md */,
E743D132251014E60017899F /* Tagging.md */,
+ E7E305E02CE14447000E9348 /* Numbers.md */,
E7A7B60E2B76FB62009102C2 /* Serialization.md */,
E776E096214AE0C700E67947 /* cmd_line_main.c */,
E776E092214AE07C00E67947 /* inc */,
diff --git a/doc/Numbers.md b/doc/Numbers.md
new file mode 100644
index 0000000..d47ffb8
--- /dev/null
+++ b/doc/Numbers.md
@@ -0,0 +1,190 @@
+
+@file Numbers.md
+
+@anchor BigNumbers
+
+# Big Numbers
+
+## Basics
+
+Big numbers are integers that can be as long and as large as desired,
+limited only by the amount of memory allocated for them.
+
+They are represented as a byte string plus an indication of sign. The
+most significant byte is first (network byte order) both in the
+encoded form and as input and output by QCBOR APIs.
+
+CBOR has only one encoded representation of zero for integers, big
+numbers included. That means the whole negative number space is
+offset by one. Since big numbers are encoded as a sign and some bytes,
+the steps to encode a negative value are to take the absolute value
+and then adjust by one. On encoding, the adjustment is to subtract one
+from the absolute value, and on decoding to add one to the absolute
+value.
+
+Take -256 as an example. It is represented in memory with two bytes,
+0x01 0x00, plus indication that it is negative, perhaps a boolean.
+When it is CBOR-encoded it becomes 0xff plus an indication of the
+sign.
+
+Notice that the length changed. This is one place in the design of
+CBOR where the bytes off the wire cannot be simply used as they
+are. This has an effect on the big number encoding and decoding
+APIs.
+
+The tag numbers 2 and 3 indicate a big number. The tag content is a
+byte string with the absolute value of the big number, offset by one
+when negative. Tag number 2 indicates positive and 3 negative.
+
+For example, 256 encoded as a positive big number is 0xc2 0x42 0x01
+0x00 and -256 encodes as 0xc3 0x41 0xff (non-preferred; see preferred
+example in next section).
+
+
+## Preferred Serialization
+
+The integer number space encodable by big numbers overlaps with the
+number space encodable by regular integers, by CBOR type 0 and type 1,
+the range from -(2^64) to (2^64)-1.
+
+RFC 8949 big number preferred serialization requires that numbers in
+the type 0 and 1 integer range must not be encoded as big
+numbers. They must be encoded as type 0 and type 1 integers.
+
+For example, 256 must be encoded as 0x19 0x01 0x00 and -256 as
+0x38 0xff.
+
+One purpose of this is to save some bytes, but typically only one byte
+is saved. The more important reason for this is determinism. There is
+only one way to represent a particular value.
+
+Technically speaking, this reduction of the big number space to the
+integer space is not about serialization. It doesn't change the way
+big numbers and integers are serialized by themselves.
+
+Note, also that CBOR can encode what can be called "65-big negative
+numbers", 64 bits of precision and one bit to indicate a negative
+one. These can't be represented by 64-bit two's compliment signed
+integers or 64-bit unsigned integers. While many programming
+environments, like Ruby and Python that have big number support built
+in will support these, some environments like C and the CPU
+instructions do not. QCBOR big number decoding supports them with
+explicit code to carry the negative number in as a uint64_t and a sign
+indicator. Big number specific preferred serialization requires
+encoding of 65-bit negative integers. Other than big numbers QCBOR
+avoids 65-big negative integers.
+
+## QCBOR APIs
+
+The negative number offset of one and the reduction required by
+big number preferred serialization have a large effect on the QCBOR
+big number API.
+
+QCBOREncode_AddTBigNumber() and QCBORDecode_GetTBigNumber() are the
+most comprehensive and easiest to use APIs. They automatically take
+care of the offset of one for negative numbers and big number
+preferred serialization. Unlike most other decode APIs,
+QCBORDecode_GetTBigNumber() requires an output buffer of sufficient
+size.
+
+If the protocol being implemented does not use preferred
+serialization, then QCBOREncode_AddTBigNumberNoPreferred() and
+QCBORDecode_GetTBigNumberNoPreferred() can be used. Only tags 2 and 3
+will be output when encoding. Type 0 and 1 integers will never be
+output. Decoding will error if type 0 and type 1 integers are
+encountered.
+
+It is likely that any implementation for a protocol that uses big
+numbers will link in a big number library. When that's so, it can be
+used to offset the negative numbers by one rather than the internal
+implementation that QCBOR has. If both the offset by one and the
+preferred serialization are side-stepped, the big numbers APIs become
+simple pass throughs for encoding and decoding byte strings. This
+reduces the amount of object linked from the QCBOR library by about
+1KB.
+
+This is what QCBOREncode_AddTBigNumberRaw() and
+QCBORDecode_GetTBigNumberRaw() do. Note that decoding doesn't require
+an output buffer be supplied because the bytes off the wire can simply
+be returned. When these are used, the big number library should be
+used to add one to negative numbers before encoding and subtract one
+from negative numbers after decoding.
+
+Last, QCBORDecode_StringsTagCB(), @ref QCBOR_TYPE_POSBIGNUM,
+@ref QCBOR_TYPE_NEGBIGNUM, and QCBORDecode_ProcessBigNumber() need to
+be described. QCBOR's tag processing callbacks can be used to handle
+big numbers by installing QCBORDecode_StringsTagCB() for tag numbers 2
+and 3. This will result in positive and negative big numbers being
+returned in a @ref QCBORItem as QCBOR types @ref QCBOR_TYPE_POSBIGNUM and
+@ref QCBOR_TYPE_NEGBIGNUM. QCBORDecode_StringsTagCB() is very simple,
+and not much object code. Neither the offset of one for negative
+values nor preferred serialization number reduction is preformed. It is
+equivalent to QCBORDecode_GetTBigNumberRaw().
+
+QCBORDecode_ProcessBigNumber() may be used to fully process a
+@ref QCBORItem that is expected to be a big number. It will perform the
+negative offset and handle the preferred serialization number
+reduction. QCBORDecode_ProcessBigNumber() is what is used internally
+to implement QCBORDecode_GetTBigNumber() and links in a lot of
+object code. Note that QCBORDecode_ProcessBigNumber() requires an
+output buffer be supplied.
+
+QCBORDecode_ProcessBigNumbernoPreferred() is the same as
+QCBORDecode_ProcessBigNumber() but will error out if the @ref QCBORItem
+contains anything but @ref QCBOR_TYPE_POSBIGNUM, @ref QCBOR_TYPE_NEGBIGNUM.
+
+Note that if QCBORDecode_StringsTagCB() is installed,
+QCBORDecode_GetTBigNumber(), QCBORDecode_GetTBigNumberNoPreferred()
+and QCBORDecode_GetTBigNumberRaw() all still work as documented.
+
+## "Borrowed" Big Number Tag Content
+
+Like all other tag decoding functions in QCBOR, the big number tag
+decoders can also work with borrowed tag content for big numbers. For
+example, the value of a map item might be specified that it be decoded
+as a big number even though there is no tag number indicating so.
+
+The main thing to mention here is that without a tag number, there's
+nothing in the encoded CBOR to indicate the sign. That must be
+indicated some other way. For example, it might be that the map item
+is defined to always be a positive big number.
+
+On the encode side, that means the sign boolean is ignored when
+QCBOREncode_AddTBigNumber() is called with @c uTagRequirement other
+than @ref QCBOR_ENCODE_AS_TAG.
+
+On the decode side, the sign boolean becomes an input parameter rather
+than an output parameter so that QCBORDecode_GetTBigNumber() can know
+whether to apply the offset of one in case the value is negative.
+
+## Big Number Conversions
+
+QCBOR provides a number of decode functions that can convert big
+numbers to other representations like floating point. For example, see
+QCBORDecode_GetDoubleConvertAll().
+
+Note also that QCBORDecode_ProcessBigNumber() can convert integers to
+big numbers. It will work even if the protocol item is never supposed
+to be a big number. It will just happily convert any type 0 or type 1
+CBOR integer to a big number.
+
+## Backwards Compatibility with QCBOR v1
+
+QCBOR v1 supports only the minimal pass through processing of big
+numbers, not the offset of one for negative values or the big number
+specific preferred serialization. The main functions in v1 are
+QCBOREncode_AddTPositiveBignum(), QCBOREncode_AddTNegativeBignum() and
+QCBORDecode_GetBignum(). These are equivalent to
+QCBOREncode_AddTBigNumberRaw() and QCBORDecode_GetTBigNumberRaw().
+
+Also, in v1 @ref CBOR_TAG_POS_BIGNUM and @ref CBOR_TAG_NEG_BIGNUM are
+always processed into a @ref QCBORItem of type @ref
+QCBOR_TYPE_POSBIGNUM and @ref QCBOR_TYPE_NEGBIGNUM by an equivalent of
+QCBORDecode_StringsTagCB.
+
+All the v1 methods for big numbers such as
+QCBOREncode_AddTPositiveBignum() and QCBORDecode_GetBignum() are fully
+supported in v2. When QCBORDecode_StringsTagCB() is installed for tags
+2 and 3 such as by calling QCBORDecode_CompatibilityV1(), QCBOR v2 big
+number behavior is 100% backwards compatible.
+
diff --git a/inc/qcbor/qcbor_common.h b/inc/qcbor/qcbor_common.h
index d986742..4826769 100644
--- a/inc/qcbor/qcbor_common.h
+++ b/inc/qcbor/qcbor_common.h
@@ -33,6 +33,7 @@
#ifndef qcbor_common_h
#define qcbor_common_h
+
#ifdef __cplusplus
extern "C" {
#if 0
@@ -121,9 +122,11 @@
#define CBOR_TAG_DATE_STRING 0
/** See QCBOREncode_AddTDateEpoch(). */
#define CBOR_TAG_DATE_EPOCH 1
-/** See QCBOREncode_AddTBigNumber(). */
+/** A postive big number. See QCBOREncode_AddTBigNumber(),
+ * QCBORDecode_GetTBigNumber() and QCBORDecode_StringsTagCB(). */
#define CBOR_TAG_POS_BIGNUM 2
-/** See QCBOREncode_AddTBigNumber(). */
+/** A negative big number. See QCBOREncode_AddTBigNumber(),
+ * QCBORDecode_GetTBigNumber() and QCBORDecode_StringsTagCB(). */
#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 e120bd2..075b8f5 100644
--- a/inc/qcbor/qcbor_decode.h
+++ b/inc/qcbor/qcbor_decode.h
@@ -276,19 +276,21 @@
#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_ProcessBigNumber(). */
+ * pointer and a length. See QCBORDecode_StringsTagCB()
+ * and @ref CBOR_TAG_POS_BIGNUM. */
#define QCBOR_TYPE_POSBIGNUM 9
/** Type for a negative big number. Data is in @c val.bignum, a
* pointer and a length. Type 1 integers in the range of [-2^64,
- * -2^63 - 1] are returned in this type. 1 MUST be subtracted from
+ * -2^63 - 1] are returned in this type. One must be subtracted from
* what is returned to get the actual value. This is because of the
* way CBOR negative numbers are represented. QCBOR doesn't do this
* because it can't be done without storage allocation and QCBOR
* 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_ProcessBigNumber(). */
+ * what was received. See QCBORDecode_StringsTagCB(). and
+ * @ref CBOR_TAG_NEG_BIGNUM. */
#define QCBOR_TYPE_NEGBIGNUM 10
/** Type for [RFC 3339] (https://tools.ietf.org/html/rfc3339) date
@@ -593,9 +595,11 @@
/** See @ref QCBOR_TYPE_UKNOWN_SIMPLE */
uint8_t uSimple;
+
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
QCBORExpAndMantissa expAndMantissa;
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
+
uint64_t uTagNumber; /* Used internally during decoding */
/* For use by user-defined tag content handlers */
@@ -1482,63 +1486,50 @@
/**
* @brief Decode a preferred serialization 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[in] Item The number to process.
+ * @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
- * encoded with preferred serialization. This processing is not part
- * of the main decoding because of the number of CBOR types it
- * involves, because it needs a buffer to output to, and to keep code
- * size of the core decoding small.
+ * This exists to process a @ref QCBORItem that is expected to be a
+ * big number encoded with preferred serialization. This will turn the
+ * types listed in the table below into a big number. In particular it
+ * will apply the offset of one needed to get the actual value for
+ * @ref QCBOR_TYPE_NEGBIGNUM. Leading zeros are removed. The value 0
+ * is always returned as a one-byte big number with the value 0x00.
*
- * This can also be used to do the subtraction of 1 for negative big
- * numbers even if preferred serialization of big numbers is not in
- * use.
+ *| Type |
+ * | ---- |
+ * | @ref QCBOR_TYPE_INT64 |
+ * | @ref QCBOR_TYPE_UINT64 |
+ * | @ref QCBOR_TYPE_65BIT_NEG_INT |
+ * | @ref QCBOR_TYPE_POSBIGNUM |
+ * | @ref QCBOR_TYPE_NEGBIGNUM |
+ * | @ref QCBOR_TYPE_BYTE_STRING |
+ * | ---- |
*
- * This works on all CBOR type 0 and 1 integers and all tag 2 and 3
- * big numbers. In terms of QCBOR types, this works on
- * \ref QCBOR_TYPE_INT64, \ref QCBOR_TYPE_UINT64,
- * \ref QCBOR_TYPE_65BIT_NEG, \ref QCBOR_TYPE_POSBIGNUM and
- * \ref QCBOR_TYPE_NEGBIGNUM. This also works on
- * \ref QCBOR_TYPE_BYTES in which case pIsNegative
- * becomes an in parameter indicating the sign.
+ * For the type @ref QCBOR_TYPE_BYTES, @c pIsNegative becomes an in
+ * parameter indicating the sign.
*
- * 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 BigNumberBuf is too small, @c pBigNum.ptr will be @c NULL and
+ * @c pBigNum.len reports the required length. Note that the size of
+ * the output buffer, @c *pBigNumberBuf, should be 1 byte larger than
+ * the size of the @c Item.val.bignum when the input @c Item is @ref
+ * QCBOR_TYPE_NEGBIGNUM because the application of the offset of one
+ * for negative numbers may have an arithmetic carry. A way to size
+ * the output buffer is MIN(9, Item.val.bignum.len + 1). 9 comes from
+ * the length of they type @ref QCBOR_TYPE_65BIT_NEG plus the
+ * possibility of an arithmetic carry.
*
- * 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
- * example, an encoded tag 3 that is 0xff, is returned here as 0x01
- * 0x00. The other case is a empty tag 2 which is returned as a
- * one-byte big number with the value 0x00. (This is the only place
- * in all of RFC 8949 except for indefinite length strings where the
- * encoded buffer off the wire can't be returned directly, the only
- * place some storage allocation is required.)
+ * The object code for this is surprisingly large at about 1KB. This
+ * is to apply the offset of one for the negative values and to
+ * operate all the data types used by big number specific preferred
+ * serialization.
*
- * This is the decode-side implementation of preferred serialization
- * of big numbers described in section 3.4.3 of RFC 8949. It
- * implements the decode-side unification of big numbers and regular
- * integers.
- *
- * 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.
- *
- * Finally, the object code for this function is suprisingly large,
- * almost 1KB. This is due to the number of CBOR data types, and the
- * big number math required to subtract one and the buffer sizing
- * issue it brings.
+ * See @ref BigNumbers for a useful overview of CBOR big numbers and
+ * QCBOR's support for them. See also
+ * QCBORDecode_ProcessBigNumberNoPreferred(),
+ * QCBORDecode_GetTBigNumber() and QCBOREncode_AddTBigNumber().
*/
QCBORError
QCBORDecode_ProcessBigNumber(const QCBORItem Item,
@@ -1556,9 +1547,8 @@
* @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.
+ * allow @ref QCBOR_TYPE_INT64, @ref QCBOR_TYPE_UINT64 and @ref
+ * QCBOR_TYPE_65BIT_NEG_INT.
*/
QCBORError
QCBORDecode_ProcessBigNumberNoPreferred(const QCBORItem Item,
diff --git a/inc/qcbor/qcbor_encode.h b/inc/qcbor/qcbor_encode.h
index 2ceb999..25b67e7 100644
--- a/inc/qcbor/qcbor_encode.h
+++ b/inc/qcbor/qcbor_encode.h
@@ -741,7 +741,7 @@
* this range. If you need 65-bit negative integers, you likely need
* negative 66, 67 and 68-bit negative integers so it is likely better
* to use CBOR big numbers where you can have any number of bits. See
- * QCBOREncode_AddTNegativeBignum() and @ref Serialization.
+ * QCBOREncode_AddTBigNumber() and @ref Serialization.
*/
static void
QCBOREncode_AddNegativeUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
@@ -1188,42 +1188,37 @@
/**
* @brief Add a big number to encoded output using preferred serialization.
*
- * @param[in] pCtx The encoding context.
+ * @param[in] pCtx The encoding context to add 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.
+ * @param[in] bNegative If true, @c BigNumber is negative.
+ * @param[in] BigNumber Pointer and length of the big number,
+ * most significant byte first (network
+ * byte order).
*
- * @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]
+ * This encodes CBOR tag numbers 2 and 3, positive and negative 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.
+ * This performs the offset of one required when encoding negative
+ * numbers.
*
- * Leading zeros are removed.
+ * Leading zeros are not encoded.
*
- * See also QCBOREncode_AddTBigNumberNoPreferred().
+ * This uses preferred serialization described specifically for big
+ * numbers. Positive values between 0 and (2^64)-1 are encoded as
+ * common type 0 integers. Negative values between -(2^64) and -1 are
+ * encoded as common type 1 integers.
*
- * 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.
+ * See @ref BigNumbers for a useful overview of CBOR big numbers and
+ * QCBOR's support for them. See
+ * QCBOREncode_AddTBigNumberNoPreferred() to encode without conversion
+ * to common integer types 0 and 1. See QCBOREncode_AddTBigNumberRaw()
+ * for encoding that is simple pass through as a byte string that
+ * links in much less object code. See QCBORDecode_GetTBigNumber() for
+ * the decoder counter part.
*/
-void
+static void
QCBOREncode_AddTBigNumber(QCBOREncodeContext *pCtx,
uint8_t uTagRequirement,
bool bNegative,
@@ -1247,26 +1242,26 @@
/**
* @brief Add a big number to encoded output without preferred serialization.
*
- * @param[in] pCtx The encoding context to add the UUID to.
+ * @param[in] pCtx The encoding context to add 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.
+ * @param[in] bNegative If true, @c BigNumber is negative.
+ * @param[in] BigNumber Pointer and length of the big number,
+ * most significant byte first (network
+ * byte order).
*
* 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.
+ * Leading zeros are removed before encoding.
*
- * 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.
+ * See @ref BigNumbers for a useful overview of CBOR big numbers and
+ * QCBOR's support for them. See also QCBOREncode_AddTBigNumber().
+ * See QCBORDecode_GetTBigNumberNoPreferred(), the decode counter part
+ * for this.
*/
-void
+static void
QCBOREncode_AddTBigNumberNoPreferred(QCBOREncodeContext *pCtx,
uint8_t uTagRequirement,
bool bNegative,
@@ -1290,28 +1285,33 @@
/**
* @brief Add a big number to encoded output with no processing.
*
- * @param[in] pCtx The encoding context to add the UUID to.
+ * @param[in] pCtx The encoding context to add 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.
+ * @param[in] BigNumber Pointer and length of the big number,
+ * most significant byte first (network
+ * byte order).
*
- * 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.
+ * All this does is output tag number 2 or 3 depending on @c bNegative
+ * and then output @c BigNumber as a byte string. If @c
+ * uTagRequirement is @ref QCBOR_ENCODE_AS_BORROWED, the tag number is
+ * not even output and this equivalent to QCBOREncode_AddBytes().
*
- * See QCBOREncode_AddTBigNumber().
+ * No leading zeros are removed. No offset of one is performed for
+ * negative numbers. There is no conversion to type 0 and type 1
+ * integers.
*
- * @c BigNum makes up an aribtrary precision integer in
- * network/big-endian byte order. The first byte is the most
- * significant.
+ * This is mostly an inline implementation that links in no additional
+ * object from the QCBOR library.
*
- * 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.
+ * This is most useful when a big number library has been linked, and
+ * it can be (trivially) used to perform the offset of one for
+ * negative numbers.
+ *
+ * See @ref BigNumbers for a useful overview of CBOR big numbers and
+ * QCBOR's support for them. See QCBORDecode_GetTBigNumberRaw(), the
+ * decode counter part for this. See also QCBOREncode_AddTBigNumber().
*/
static void
QCBOREncode_AddTBigNumberRaw(QCBOREncodeContext *pCtx,
@@ -2592,217 +2592,10 @@
uint64_t uNumber);
-/* ================ Deprecated ============================ */
-
-
-/* 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_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_DISABLE_EXP_AND_MANTISSA */
/* ========================================================================= *
- * BEGINNING OF DEPRECATED FUNCTIONS *
+ * BEGINNING OF DEPRECATED FUNCTION DECLARATIONS *
* *
* There is no plan to remove these in future versions. *
* They just have been replaced by something better. *
@@ -2870,65 +2663,144 @@
static void
QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
-/* Use QCBOREncode_AddTPositiveBignum() instead */
+/* Deprecated. Use QCBOREncode_AddTagNumber() instead */
static void
-QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx, UsefulBufC BigNumber);
+QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag);
-/* QCBOREncode_AddTPositiveBignumToMapSZ() instead */
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRaw() instead */
+static void
+QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRawToMapSZ() instead */
+static void
+QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRawToMapN() instead */
+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);
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRawToMapSZ() instead */
static void
QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
UsefulBufC BigNumber);
-/* Use QCBOREncode_AddTPositiveBignumToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTBigNumberRawToMapN() instead */
static void
QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
UsefulBufC BigNumber);
-/* Use QCBOREncode_AddTNegativeBignum() instead */
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRaw() instead */
static void
-QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx, UsefulBufC BigNumber);
+QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
-/* Use QCBOREncode_AddTNegativeBignumToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTBigNumberRawToMapSZ() instead */
static void
-QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC BigNumber);
+QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC BigNumber);
-/* Use QCBOREncode_AddTNegativeBignumToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTBigNumberRawToMapN() instead */
static void
-QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC BigNumber);
+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);
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRawToMapSZ() instead */
+static void
+QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ UsefulBufC BigNumber);
+
+/* Deprecated. Use QCBOREncode_AddTBigNumberRawToMapN() instead */
+static void
+QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ UsefulBufC BigNumber);
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
+/* Deprecated. Use QCBOREncode_AddTDecimalFraction() instead */
-/* Use QCBOREncode_AddTDecimalFraction() instead */
static void
QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
int64_t nMantissa,
int64_t nBase10Exponent);
-/* Use QCBOREncode_AddTDecimalFractionToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionToMapSZ() instead */
static void
QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
int64_t nMantissa,
int64_t nBase10Exponent);
-/* Use QCBOREncode_AddTDecimalFractionToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionToMapN() instead */
static void
QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
int64_t nMantissa,
int64_t nBase10Exponent);
-/* Use QCBOREncode_AddTDecimalFractionBigNum() instead */
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigMantissaRaw() instead */
+static void
+QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ() instead */
+static void
+QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN() instead */
+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);
-/* Use QCBOREncode_AddTDecimalFractionBigNumToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ() instead */
static void
QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
const char *szLabel,
@@ -2936,7 +2808,7 @@
bool bIsNegative,
int64_t nBase10Exponent);
-/* Use QCBOREncode_AddTDecimalFractionBigNumToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN() instead */
static void
QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
@@ -2944,34 +2816,61 @@
bool bIsNegative,
int64_t nBase10Exponent);
-/* Use QCBOREncode_AddTBigFloat() instead */
+
+/* Deprecated. Use QCBOREncode_AddTBigFloat() instead. */
static void
QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx,
int64_t nMantissa,
int64_t nBase2Exponent);
-/* Use QCBOREncode_AddTBigFloatToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTBigFloatToMapSZ() instead. */
static void
QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
int64_t nMantissa,
int64_t nBase2Exponent);
-/* Use QCBOREncode_AddTBigFloatToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTBigFloatToMapN() instead. */
static void
QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
int64_t nMantissa,
int64_t nBase2Exponent);
-/* Use QCBOREncode_AddTBigFloatBigNum() instead */
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigMantissaRaw() instead. */
+static void
+QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ() instead. */
+static void
+QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ uint8_t uTagRequirement,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigMantissaRawToMapN() instead. */
+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);
-/* Use QCBOREncode_AddTBigFloatBigNumToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ() instead. */
static void
QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
@@ -2979,7 +2878,94 @@
bool bIsNegative,
int64_t nBase2Exponent);
-/* Use QCBOREncode_AddTBigFloatBigNumToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigMantissaRawToMapN() instead. */
+static void
+QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFraction() instead. */
+static void
+QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
+ int64_t nMantissa,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionToMapSZ() instead. */
+static void
+QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ int64_t nMantissa,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionToMapN() instead. */
+static void
+QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ int64_t nMantissa,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigNum() instead. */
+static void
+QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigNumToMapSZ() instead. */
+static void
+QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase10Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTDecimalFractionBigNumToMapN() instead. */
+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);
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatToMapSZ() instead. */
+static void
+QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ int64_t nMantissa,
+ int64_t nBase2Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatToMapN() instead. */
+static void
+QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
+ int64_t nLabel,
+ int64_t nMantissa,
+ int64_t nBase2Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigNum() instead. */
+static void
+QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigNumToMapSZ() instead. */
+static void
+QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
+ const char *szLabel,
+ UsefulBufC Mantissa,
+ bool bIsNegative,
+ int64_t nBase2Exponent);
+
+/* Deprecated. Use QCBOREncode_AddTBigFloatBigNumToMapN() instead. */
static void
QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
@@ -2988,141 +2974,141 @@
int64_t nBase2Exponent);
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
-/* Use QCBOREncode_AddTURI() instead */
+/* Deprecated. Use QCBOREncode_AddTURI() instead. */
static void
QCBOREncode_AddURI(QCBOREncodeContext *pCtx, UsefulBufC URI);
-/* Use QCBOREncode_AddTURIToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTURIToMapSZ() instead. */
static void
QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC URI);
-/* Use QCBOREncode_AddTURIToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTURIToMapN() instead */
static void
QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC URI);
-/* Use QCBOREncode_AddTB64Text() instead */
+/* Deprecated. Use QCBOREncode_AddTB64Text() instead. */
static void
QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx, UsefulBufC B64Text);
-/* Use QCBOREncode_AddTB64TextToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTB64TextToMapSZ() instead. */
static void
QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC B64Text);
-/* Use QCBOREncode_AddTB64TextToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTB64TextToMapN() instead. */
static void
QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC B64Text);
-/* Use QCBOREncode_AddTB64URLText() instead */
+/* Deprecated. Use QCBOREncode_AddTB64URLText() instead. */
static void
QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx, UsefulBufC B64Text);
-/* Use QCBOREncode_AddTB64URLTextToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTB64URLTextToMapSZ() instead. */
static void
QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
UsefulBufC B64Text);
-/* Use QCBOREncode_AddTB64URLTextToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTB64URLTextToMapN() instead. */
static void
QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
UsefulBufC B64Text);
-/* Use QCBOREncode_AddTRegex() instead */
+/* Deprecated. Use QCBOREncode_AddTRegex() instead. */
static void
QCBOREncode_AddRegex(QCBOREncodeContext *pCtx, UsefulBufC Regex);
-/* Use QCBOREncode_AddTRegexToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTRegexToMapSZ() instead. */
static void
QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
UsefulBufC Regex);
-/* Use QCBOREncode_AddTRegexToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTRegexToMapN() instead. */
static void
QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
UsefulBufC Regex);
-/* Use QCBOREncode_AddTMIMEData() instead */
+/* Deprecated. Use QCBOREncode_AddTMIMEData() instead. */
static void
QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx, UsefulBufC MIMEData);
-/* Use QCBOREncode_AddTMIMEDataToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTMIMEDataToMapSZ() instead. */
static void
QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
UsefulBufC MIMEData);
-/* Use QCBOREncode_AddTMIMEDataToMapN() instead */
+/* Deprecated. Use QCBOREncode_AddTMIMEDataToMapN() instead. */
static void
QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
UsefulBufC MIMEData);
-/* Use QCBOREncode_AddTDateString() instead */
+/* Deprecated. Use QCBOREncode_AddTDateString() instead. */
static void
QCBOREncode_AddDateString(QCBOREncodeContext *pCtx, const char *szDate);
-/* Use QCBOREncode_AddTDateStringToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddTDateStringToMapSZ() instead. */
static void
QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szDate);
-/* Use QCBOREncode_AddTDateStringToMapN instead */
+/* Deprecated. Use QCBOREncode_AddTDateStringToMapN instead. */
static void
QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx,
int64_t nLabel,
const char *szDate);
-/* Use QCBOREncode_AddBoolToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddBoolToMapSZ() instead. */
static void
QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b);
-/* Use QCBOREncode_AddNULLToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddNULLToMapSZ() instead. */
static void
QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel);
-/* Use QCBOREncode_AddUndefToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddUndefToMapSZ() instead. */
static void
QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel);
-/* Use QCBOREncode_AddSimpleToMapSZ instead */
+/* Deprecated. Use QCBOREncode_AddSimpleToMapSZ instead. */
static void
QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pCtx,
const char *szLabel,
const uint8_t uSimple);
-/* Use QCBOREncode_OpenArrayInMapSZ() instead */
+/* Deprecated. Use QCBOREncode_OpenArrayInMapSZ() instead. */
static void
QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel);
-/* Use QCBOREncode_OpenMapInMapSZ() instead */
+/* Deprecated. Use QCBOREncode_OpenMapInMapSZ() instead. */
static void
QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
-/* Use QCBOREncode_OpenArrayIndefiniteLengthInMapSZ() instead */
+/* Deprecated. Use QCBOREncode_OpenArrayIndefiniteLengthInMapSZ() instead. */
static void
QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pCtx,
const char *szLabel);
-/* Use QCBOREncode_OpenMapIndefiniteLengthInMapSZ() instead */
+/* Deprecated. Use QCBOREncode_OpenMapIndefiniteLengthInMapSZ() instead. */
static void
QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pCtx,
const char *szLabel);
-/* Use QCBOREncode_BstrWrapInMapSZ() instead */
+/* Deprecated. Use QCBOREncode_BstrWrapInMapSZ() instead. */
static void
QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
-/* Use QCBOREncode_AddEncodedToMapSZ() instead */
+/* Deprecated. Use QCBOREncode_AddEncodedToMapSZ() instead. */
static void
QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded);
/* ========================================================================= *
- * END OF DEPRECATED FUNCTIONS *
+ * END OF DEPRECATED FUNCTION DECLARATIONS *
* ========================================================================= */
@@ -3180,6 +3166,13 @@
QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
uint8_t uMajorType);
+/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
+void
+QCBOREncode_Private_AddTBigNumberMain(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ bool bPreferred,
+ const bool bNegative,
+ const UsefulBufC BigNumber);
/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
void
@@ -3778,6 +3771,18 @@
}
+
+
+static inline void
+QCBOREncode_AddTBigNumber(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const bool bNegative,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_Private_AddTBigNumberMain(pMe, uTagRequirement, true, bNegative, BigNumber);
+}
+
+
static inline void
QCBOREncode_AddTBigNumberToMapSZ(QCBOREncodeContext *pMe,
const char *szLabel,
@@ -3801,6 +3806,15 @@
}
static inline void
+QCBOREncode_AddTBigNumberNoPreferred(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const bool bNegative,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_Private_AddTBigNumberMain(pMe, uTagRequirement, false, bNegative, BigNumber);
+}
+
+static inline void
QCBOREncode_AddTBigNumberNoPreferredToMapSZ(QCBOREncodeContext *pMe,
const char *szLabel,
uint8_t uTagRequirement,
@@ -3835,7 +3849,7 @@
bool bNegative)
{
if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- QCBOREncode_AddTag(pMe, bNegative ? CBOR_TAG_NEG_BIGNUM : CBOR_TAG_POS_BIGNUM);
+ QCBOREncode_AddTagNumber(pMe, bNegative ? CBOR_TAG_NEG_BIGNUM : CBOR_TAG_POS_BIGNUM);
}
}
@@ -3873,116 +3887,11 @@
}
-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 BigNumber)
-{
- QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, uTagRequirement, false, BigNumber);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNumber)
-{
- QCBOREncode_AddTBigNumberRawToMapN(pMe, nLabel, uTagRequirement, false, BigNumber);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, const UsefulBufC BigNumber)
-{
- QCBOREncode_AddTBigNumberRaw(pMe, QCBOR_ENCODE_AS_TAG, false, BigNumber);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe,
- const char *szLabel,
- const UsefulBufC BigNumber)
-{
- QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, false, BigNumber);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const UsefulBufC BigNumber)
-{
- 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 /* Deprecated */
-QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe,
- const char *szLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNumber)
-{
- QCBOREncode_AddTBigNumberRawToMapSZ(pMe,
- szLabel,
- uTagRequirement,
- true,
- BigNumber);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC BigNumber)
-{
- QCBOREncode_AddTBigNumberRawToMapN(pMe,
- nLabel,
- uTagRequirement,
- true,
- BigNumber);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, const UsefulBufC BigNumber)
-{
- QCBOREncode_AddTBigNumberRaw(pMe, QCBOR_ENCODE_AS_TAG, true, BigNumber);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe,
- const char *szLabel,
- const UsefulBufC BigNumber)
-{
- QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, true, BigNumber);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const UsefulBufC BigNumber)
-{
- 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,
@@ -4024,42 +3933,6 @@
nBase10Exponent);
}
-static inline void /* Deprecated */
-QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe,
- const int64_t nMantissa,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFraction(pMe,
- QCBOR_ENCODE_AS_TAG,
- nMantissa,
- nBase10Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe,
- const char *szLabel,
- const int64_t nMantissa,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFractionToMapSZ(pMe,
- szLabel,
- QCBOR_ENCODE_AS_TAG,
- nMantissa,
- nBase10Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const int64_t nMantissa,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFractionToMapN(pMe,
- nLabel,
- QCBOR_ENCODE_AS_TAG,
- nMantissa,
- nBase10Exponent);
-}
static inline void
@@ -4159,98 +4032,6 @@
}
-
-static inline void /* Deprecated */
-QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFractionBigMantissaRaw(pMe,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
-}
-
-
-static inline void /* Deprecated */
-QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
- const char *szLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ(pMe,
- szLabel,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN(pMe,
- nLabel,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFractionBigMantissaRaw(pMe,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
- const char *szLabel,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ(pMe,
- szLabel,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase10Exponent)
-{
- QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN(pMe,
- nLabel,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase10Exponent);
-}
-
-
static inline void
QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe,
const uint8_t uTagRequirement,
@@ -4286,43 +4067,6 @@
QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
}
-static inline void /* Deprecated */
-QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe,
- const int64_t nMantissa,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloat(pMe,
- QCBOR_ENCODE_AS_TAG,
- nMantissa,
- nBase2Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe,
- const char *szLabel,
- const int64_t nMantissa,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloatToMapSZ(pMe,
- szLabel,
- QCBOR_ENCODE_AS_TAG,
- nMantissa,
- nBase2Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const int64_t nMantissa,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloatToMapN(pMe,
- nLabel,
- QCBOR_ENCODE_AS_TAG,
- nMantissa,
- nBase2Exponent);
-}
-
static inline void
QCBOREncode_AddTBigFloatBigMantissa(QCBOREncodeContext *pMe,
@@ -4372,7 +4116,6 @@
}
-
static inline void
QCBOREncode_AddTBigFloatBigMantissaRaw(QCBOREncodeContext *pMe,
const uint8_t uTagRequirement,
@@ -4420,97 +4163,6 @@
nBase2Exponent);
}
-
-
-static inline void /* Deprecated */
-QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe,
- const uint8_t uTagRequirement,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloatBigMantissaRaw(pMe,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe,
- const char *szLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ(pMe,
- szLabel,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const uint8_t uTagRequirement,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloatBigMantissaRawToMapN(pMe,
- nLabel,
- uTagRequirement,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
-}
-
-
-static inline void /* Deprecated */
-QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloatBigMantissaRaw(pMe,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe,
- const char *szLabel,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ(pMe,
- szLabel,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
-}
-
-static inline void /* Deprecated */
-QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
- const int64_t nLabel,
- const UsefulBufC Mantissa,
- const bool bIsNegative,
- const int64_t nBase2Exponent)
-{
- QCBOREncode_AddTBigFloatBigMantissaRawToMapN(pMe,
- nLabel,
- QCBOR_ENCODE_AS_TAG,
- Mantissa,
- bIsNegative,
- nBase2Exponent);
-}
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
@@ -5161,7 +4813,7 @@
QCBOREncode_BstrWrap(pMe);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pMe, const char *szLabel)
{
QCBOREncode_BstrWrapInMapSZ(pMe, szLabel);
@@ -5191,7 +4843,7 @@
QCBOREncode_AddEncoded(pMe, Encoded);
}
-static inline void
+static inline void /* Deprecated */
QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Encoded)
{
QCBOREncode_AddEncodedToMapSZ(pMe, szLabel, Encoded);
@@ -5248,10 +4900,388 @@
return UsefulOutBuf_GetEndPosition(&(pMe->OutBuf));
}
+
/* ======================================================================== *
* END OF PRIVATE INLINE IMPLEMENTATION *
* ======================================================================== */
+
+
+
+/* ========================================================================= *
+ * BEGINNING OF INLINES FOR DEPRECATED FUNCTIONS *
+ * ========================================================================= */
+
+
+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 BigNumber)
+{
+ QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, uTagRequirement, false, BigNumber);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRawToMapN(pMe, nLabel, uTagRequirement, false, BigNumber);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRaw(pMe, QCBOR_ENCODE_AS_TAG, false, BigNumber);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, false, BigNumber);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const UsefulBufC BigNumber)
+{
+ 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 /* Deprecated */
+QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRawToMapSZ(pMe,
+ szLabel,
+ uTagRequirement,
+ true,
+ BigNumber);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRawToMapN(pMe,
+ nLabel,
+ uTagRequirement,
+ true,
+ BigNumber);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRaw(pMe, QCBOR_ENCODE_AS_TAG, true, BigNumber);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRawToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, true, BigNumber);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const UsefulBufC BigNumber)
+{
+ QCBOREncode_AddTBigNumberRawToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, true, BigNumber);
+
+}
+
+#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
+static inline void /* Deprecated */
+QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe,
+ const int64_t nMantissa,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFraction(pMe,
+ QCBOR_ENCODE_AS_TAG,
+ nMantissa,
+ nBase10Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const int64_t nMantissa,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFractionToMapSZ(pMe,
+ szLabel,
+ QCBOR_ENCODE_AS_TAG,
+ nMantissa,
+ nBase10Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const int64_t nMantissa,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFractionToMapN(pMe,
+ nLabel,
+ QCBOR_ENCODE_AS_TAG,
+ nMantissa,
+ nBase10Exponent);
+}
+
+
+static inline void /* Deprecated */
+QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFractionBigMantissaRaw(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+
+static inline void /* Deprecated */
+QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ(pMe,
+ szLabel,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN(pMe,
+ nLabel,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFractionBigMantissaRaw(pMe,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFractionBigMantissaRawToMapSZ(pMe,
+ szLabel,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase10Exponent)
+{
+ QCBOREncode_AddTDecimalFractionBigMantissaRawToMapN(pMe,
+ nLabel,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase10Exponent);
+}
+
+
+static inline void /* Deprecated */
+QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe,
+ const int64_t nMantissa,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloat(pMe,
+ QCBOR_ENCODE_AS_TAG,
+ nMantissa,
+ nBase2Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const int64_t nMantissa,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloatToMapSZ(pMe,
+ szLabel,
+ QCBOR_ENCODE_AS_TAG,
+ nMantissa,
+ nBase2Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const int64_t nMantissa,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloatToMapN(pMe,
+ nLabel,
+ QCBOR_ENCODE_AS_TAG,
+ nMantissa,
+ nBase2Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloatBigMantissaRaw(pMe,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ(pMe,
+ szLabel,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const uint8_t uTagRequirement,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloatBigMantissaRawToMapN(pMe,
+ nLabel,
+ uTagRequirement,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+
+static inline void /* Deprecated */
+QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloatBigMantissaRaw(pMe,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe,
+ const char *szLabel,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloatBigMantissaRawToMapSZ(pMe,
+ szLabel,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+static inline void /* Deprecated */
+QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
+ const int64_t nLabel,
+ const UsefulBufC Mantissa,
+ const bool bIsNegative,
+ const int64_t nBase2Exponent)
+{
+ QCBOREncode_AddTBigFloatBigMantissaRawToMapN(pMe,
+ nLabel,
+ QCBOR_ENCODE_AS_TAG,
+ Mantissa,
+ bIsNegative,
+ nBase2Exponent);
+}
+
+#endif /* ! QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
+
+/* ========================================================================= *
+ * END OF INLINES FOR DEPRECATED FUNCTIONS *
+ * ========================================================================= */
+
+
#ifdef __cplusplus
}
#endif
diff --git a/inc/qcbor/qcbor_spiffy_decode.h b/inc/qcbor/qcbor_spiffy_decode.h
index 16542d5..5747869 100644
--- a/inc/qcbor/qcbor_spiffy_decode.h
+++ b/inc/qcbor/qcbor_spiffy_decode.h
@@ -358,179 +358,6 @@
#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */
-/**
- * @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.
- *
- * See QCBORDecode_PreferedBigNumber() in full detail.
- *
- * The type processing rules are as follows.
- *
- * This always succeeds on type 0 and 1 integers (QCBOR_TYPE_INT64,
- * QCBOR_TYPE_UINT64 and QCBOR_TYPE_65_BIT_NEG) no matter what
- * uTagRequirement is. The rest of the rules pertain to what happens
- * if the CBOR is not type 0 or type 1.
- *
- * 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 @c uTagRequreiement is @ref QCBOR_TAG_REQUIREMENT_NOT_A_TAG then this
- * will fail on anything but a byte string.
- *
- * 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.
- */
-void
-QCBORDecode_GetTBigNumber(QCBORDecodeContext *pCtx,
- const uint8_t uTagRequirement,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative);
-
-void
-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_GetTBigNumberNoPreferredInMapN(QCBORDecodeContext *pCtx,
- int64_t nLabel,
- uint8_t uTagRequirement,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative);
-
-void
-QCBORDecode_GetTBigNumberNoPreferredInMapSZ(QCBORDecodeContext *pCtx,
- const char *szLabel,
- uint8_t uTagRequirement,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative);
-
-
/**
* @brief Decode next item into a signed 64-bit integer with conversions.
@@ -1663,6 +1490,192 @@
+/**
+ * @brief Decode next item as a big number encoded using preferred serialization.
+ *
+ * @param[in] pCtx The decode context.
+ * @param[in] uTagRequirement @ref QCBOR_TAG_REQUIREMENT_TAG or related.
+ * @param[in] BigNumberBuf The buffer to write the result into.
+ * @param[out] pBigNumber The decoded big number, most significant
+ * byte first (network byte order).
+ * @param[in,out] pbIsNegative Set to true if the resulting big number is negative.
+ *
+ * This decodes CBOR tag numbers 2 and 3, positive and negative big
+ * numbers, as defined in [RFC 8949 section 3.4.3]
+ * (https://www.rfc-editor.org/rfc/rfc8949.html#section-3.4.3). This
+ * decodes preferred serialization described specifically for big
+ * numbers.
+ *
+ * See QCBORDecode_PreferedBigNumber() which performs the bulk of this.
+ *
+ * The type processing rules are as follows.
+ *
+ * This always succeeds on type 0 and 1 integers (@ref QCBOR_TYPE_INT64,
+ * @ref QCBOR_TYPE_UINT64 and @ref QCBOR_TYPE_65BIT_NEG_INT) no matter what
+ * @c uTagRequirement is. The rest of the rules pertain to what happens
+ * if the CBOR is not type 0 or type 1.
+ *
+ * If @c uTagRequirement is @ref QCBOR_TAG_REQUIREMENT_TAG, this
+ * expects a full tag 2 or tag 3 big number.
+ *
+ * If @c uTagRequreiement is @ref QCBOR_TAG_REQUIREMENT_NOT_A_TAG then
+ * this expects a byte string.
+ *
+ * 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
+ * @c 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.
+ *
+ * This works whether or not QCBORDecode_StringsTagCB() is installed
+ * to process tags 2 and 3.
+ *
+ * See @ref BigNumbers for a useful overview of CBOR big numbers and
+ * QCBOR's support for them. See QCBOREncode_AddTBigNumber(), the
+ * encode counter part for this. See also
+ * QCBORDecode_GetTBigNumberNoPreferred() and
+ * QCBORDecode_GetTBigNumberRaw().
+ *
+ * Please see @ref Decode-Errors-Overview "Decode Errors Overview".
+ *
+ * See @ref Tag-Usage for discussion on tag requirements.
+ */
+void
+QCBORDecode_GetTBigNumber(QCBORDecodeContext *pCtx,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+void
+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 next item as a big number without preferred serialization.
+ *
+ * @param[in] pCtx The decode context.
+ * @param[in] uTagRequirement @ref QCBOR_TAG_REQUIREMENT_TAG or related.
+ * @param[in] BigNumberBuf The buffer to write the result into.
+ * @param[out] pBigNumber The decoded big number, most significant
+ * byte first (network byte order).
+ * @param[in,out] pbIsNegative Set to true if the returned big number is negative.
+ *
+ * This is the same as QCBORDecode_GetTBigNumber(), but will error out
+ * on type 0 and 1 integers as it doesn't support the preferred
+ * serialization specific for big numbers.
+ *
+ * See @ref BigNumbers for a useful overview of CBOR big numbers and
+ * QCBOR's support for them. See QCBOREncode_AddTBigNumberNoPreferred(),
+ * the encode counter part for this. See also QCBORDecode_GetTBigNumber()
+ * and QCBORDecode_GetTBigNumberRaw().
+ */
+void
+QCBORDecode_GetTBigNumberNoPreferred(QCBORDecodeContext *pCtx,
+ const uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+void
+QCBORDecode_GetTBigNumberNoPreferredInMapN(QCBORDecodeContext *pCtx,
+ int64_t nLabel,
+ uint8_t uTagRequirement,
+ UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative);
+
+void
+QCBORDecode_GetTBigNumberNoPreferredInMapSZ(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 @ref QCBOR_TAG_REQUIREMENT_TAG or related.
+ * @param[out] pBigNumber The decoded big number, most significant
+ * byte first (network byte order).
+ * @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 CBOR tag numbers 2 and 3, positive and negative big
+ * numbers, as defined in [RFC 8949 section 3.4.3]
+ * (https://www.rfc-editor.org/rfc/rfc8949.html#section-3.4.3).
+ *
+ * This returns the byte string representing the big number
+ * directly. It does not apply the required the offset of one for
+ * negative big numbers. It will error out big numbers that have been
+ * encoded as type 0 and 1 integer because of big number preferred
+ * serialization.
+ *
+ * This is most useful when a big number library has been linked, and
+ * it can be (trivially) used to perform the offset of one for
+ * negative numbers.
+ *
+ * This links in much less object code than QCBORDecode_GetTBigNumber() and
+ * QCBORDecode_GetTBigNumberNoPreferred().
+ *
+ * This does the same minimal processing as installing QCBORDecode_StringsTagCB()
+ * installed to handle @ref CBOR_TAG_POS_BIGNUM and @ref CBOR_TAG_NEG_BIGNUM
+ * so QCBORDecode_VGetNext() returns a @ref QCBORItem of type
+ * @ref QCBOR_TYPE_POSBIGNUM or @ref QCBOR_TYPE_POSBIGNUM
+ *
+ * See @ref BigNumbers for a useful overview of CBOR big numbers and
+ * QCBOR's support for them. See QCBOREncode_AddTBigNumberRaw() for
+ * the encoding counter part. See QCBORDecode_GetTBigNumber() which
+ * does perform the offset for negative numbers and handles preferred
+ * serialization big numbers.
+ *
+ * Please see @ref Decode-Errors-Overview "Decode Errors Overview".
+ *
+ * See @ref Tag-Usage for discussion on tag requirements.
+ */
+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);
+
+
+
+
+
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
/**
* @brief Decode the next item as a decimal fraction.
@@ -1734,7 +1747,6 @@
int64_t *pnMantissa,
int64_t *pnExponent);
-
/**
* @brief Decode the next item as a decimal fraction with a big number mantissa.
*
@@ -1845,8 +1857,6 @@
int64_t *pnExponent);
-
-
/**
* @brief Decode the next item as a big float.
*
@@ -2343,9 +2353,13 @@
-/* ===========================================================================
- DEPRECATED methods
- ========================================================================== */
+/* ========================================================================= *
+ * BEGINNING OF DEPRECATED FUNCTION DECLARATIONS *
+ * *
+ * There is no plan to remove these in future versions. *
+ * They just have been replaced by something better. *
+ * ========================================================================= */
+
/* Deprecated. Use QCBORDecode_GetTBigNumberRaw() instead. */
static void
@@ -2354,6 +2368,7 @@
UsefulBufC *pValue,
bool *pbIsNegative);
+/* Deprecated. Use QCBORDecode_GetTBigNumberRawInMapN() instead. */
static void
QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pCtx,
int64_t nLabel,
@@ -2361,6 +2376,7 @@
UsefulBufC *pValue,
bool *pbIsNegative);
+/* Deprecated. Use QCBORDecode_GetTBigNumberRawInMapSZ() instead. */
static void
QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pCtx,
const char *szLabel,
@@ -2368,22 +2384,23 @@
UsefulBufC *pValue,
bool *pbIsNegative);
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
-/* Use QCBORDecode_GetTDecimalFraction() instead */
+#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
+/* Deprecated. Use QCBORDecode_GetTDecimalFraction() instead. */
static void
QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pCtx,
uint8_t uTagRequirement,
int64_t *pnMantissa,
int64_t *pnExponent);
-/* Use QCBORDecode_GetTDecimalFractionInMapN() instead */
+/* Deprecated. 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 */
+
+/* Deprecated. Use QCBORDecode_GetTDecimalFractionInMapSZ() instead. */
static void
QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
@@ -2391,8 +2408,7 @@
int64_t *pnMantissa,
int64_t *pnExponent);
-/* Use QCBORDecode_GetTDecimalFractionBigMantissaRaw() instead */
-
+/* Deprecated. Use QCBORDecode_GetTDecimalFractionBigMantissaRaw() instead. */
/*
TODO: integrate this comment better
* For QCBOR before v1.5, this function had a bug where
@@ -2412,7 +2428,7 @@
bool *pbMantissaIsNegative,
int64_t *pnExponent);
-/* Use QCBORDecode_GetTDecimalFractionBigMantissaRawInMapN() instead */
+/* Deprecated. Use QCBORDecode_GetTDecimalFractionBigMantissaRawInMapN() instead */
static void
QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pCtx,
int64_t nLabel,
@@ -2422,7 +2438,7 @@
bool *pbIsNegative,
int64_t *pnExponent);
-/* Use QCBORDecode_GetTDecimalFractionBigMantissaRawInMapSZ() instead */
+/* Deprecated. Use QCBORDecode_GetTDecimalFractionBigMantissaRawInMapSZ() instead. */
static void
QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pCtx,
const char *szLabel,
@@ -2432,14 +2448,14 @@
bool *pbMantissaIsNegative,
int64_t *pnExponent);
-/* Deprecated. Use QCBORDecode_GetTBigFloat() instead */
+/* 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 */
+/* Deprecated. Use QCBORDecode_GetTBigFloatInMapN() instead. */
static void
QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pCtx,
int64_t nLabel,
@@ -2447,7 +2463,7 @@
int64_t *pnMantissa,
int64_t *pnExponent);
-/* Deprecated. Use QCBORDecode_GetTBigFloatInMapSZ() instead */
+/* Deprecated. Use QCBORDecode_GetTBigFloatInMapSZ() instead. */
static void
QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pCtx,
const char *szLabel,
@@ -2455,7 +2471,7 @@
int64_t *pnMantissa,
int64_t *pnExponent);
-/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRaw() instead */
+/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRaw() instead. */
static void
QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pCtx,
uint8_t uTagRequirement,
@@ -2464,7 +2480,7 @@
bool *pbMantissaIsNegative,
int64_t *pnExponent);
-/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRawInMapN() instead */
+/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRawInMapN() instead. */
static void
QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pCtx,
int64_t nLabel,
@@ -2474,7 +2490,7 @@
bool *pbMantissaIsNegative,
int64_t *pnExponent);
-/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRawInMapSZ() instead */
+/* Deprecated. Use QCBORDecode_GetTBigFloatBigMantissaRawInMapSZ() instead. */
static void
QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pCtx,
const char *szLabel,
@@ -2486,6 +2502,11 @@
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
+/* ========================================================================= *
+ * END OF DEPRECATED FUNCTION DECLARATIONS *
+ * ========================================================================= */
+
+
/* ========================================================================= *
@@ -2577,19 +2598,10 @@
uint32_t uConvertTypes,
double *pdValue,
QCBORItem *pItem);
-#endif /* !USEFULBUF_DISABLE_ALL_FLOAT */
-
-#define QCBOR_TAGSPEC_NUM_TYPES 4
-
-
-
-
-
-
+#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT */
/* Semi-private funcion used by public inline functions. See qcbor_decode.c */
-
void
QCBORDecode_Private_GetTaggedString(QCBORDecodeContext *pMe,
uint8_t uTagRequirement,
@@ -2891,8 +2903,6 @@
-
-
#ifndef USEFULBUF_DISABLE_ALL_FLOAT
static inline void
QCBORDecode_GetDoubleConvert(QCBORDecodeContext *pMe,
@@ -3332,8 +3342,6 @@
pRegex);
}
-
-
static inline void
QCBORDecode_GetRegexInMapN(QCBORDecodeContext *pMe,
const int64_t nLabel,
@@ -3364,12 +3372,6 @@
}
-
-
-
-
-
-
static inline void
QCBORDecode_GetBinaryUUID(QCBORDecodeContext *pMe,
const uint8_t uTagRequirement,
@@ -3414,7 +3416,15 @@
* END OF PRIVATE INLINE IMPLEMENTATION *
* ======================================================================== */
-static inline void
+
+
+
+/* ========================================================================= *
+ * BEGINNING OF INLINES FOR DEPRECATED FUNCTIONS *
+ * ========================================================================= */
+
+
+static inline void /* Deprecated */
QCBORDecode_GetBignum(QCBORDecodeContext *pMe,
uint8_t uTagRequirement,
UsefulBufC *pBigNumber,
@@ -3423,7 +3433,7 @@
QCBORDecode_GetTBigNumberRaw(pMe, uTagRequirement, pBigNumber, pbIsNegative);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe,
int64_t nLabel,
uint8_t uTagRequirement,
@@ -3433,7 +3443,7 @@
QCBORDecode_GetTBigNumberRawInMapN(pMe, nLabel, uTagRequirement, pBigNumber, pbIsNegative);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
uint8_t uTagRequirement,
@@ -3443,8 +3453,8 @@
QCBORDecode_GetTBigNumberRawInMapSZ(pMe, szLabel, uTagRequirement, pBigNumber, pbIsNegative);
}
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
-static inline void
+#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
+static inline void /* Deprecated */
QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe,
uint8_t uTagRequirement,
int64_t *pnMantissa,
@@ -3453,7 +3463,7 @@
QCBORDecode_GetTDecimalFraction(pMe, uTagRequirement, pnMantissa, pnExponent);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe,
int64_t nLabel,
uint8_t uTagRequirement,
@@ -3463,7 +3473,7 @@
QCBORDecode_GetTDecimalFractionInMapN(pMe, nLabel, uTagRequirement, pnMantissa, pnExponent);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
uint8_t uTagRequirement,
@@ -3474,7 +3484,7 @@
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe,
uint8_t uTagRequirement,
UsefulBuf MantissaBuffer,
@@ -3485,7 +3495,7 @@
QCBORDecode_GetTDecimalFractionBigMantissaRaw(pMe, uTagRequirement, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe,
int64_t nLabel,
uint8_t uTagRequirement,
@@ -3497,7 +3507,7 @@
QCBORDecode_GetTDecimalFractionBigMantissaRawInMapN(pMe, nLabel, uTagRequirement, MantissaBuffer, pMantissa, pbMantissaIsNegative, pnExponent);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
uint8_t uTagRequirement,
@@ -3510,7 +3520,7 @@
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe,
uint8_t uTagRequirement,
int64_t *pnMantissa,
@@ -3519,7 +3529,7 @@
QCBORDecode_GetTBigFloat(pMe, uTagRequirement, pnMantissa, pnExponent);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe,
int64_t nLabel,
uint8_t uTagRequirement,
@@ -3529,7 +3539,7 @@
QCBORDecode_GetTBigFloatInMapN(pMe, nLabel, uTagRequirement, pnMantissa, pnExponent);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
uint8_t uTagRequirement,
@@ -3539,7 +3549,7 @@
QCBORDecode_GetTBigFloatInMapSZ(pMe, szLabel, uTagRequirement, pnMantissa, pnExponent);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe,
uint8_t uTagRequirement,
UsefulBuf MantissaBuffer,
@@ -3555,8 +3565,7 @@
pnExponent);
}
-
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe,
int64_t nLabel,
uint8_t uTagRequirement,
@@ -3574,7 +3583,7 @@
pnExponent);
}
-static inline void
+static inline void /* Deprecated */
QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
uint8_t uTagRequirement,
@@ -3593,6 +3602,12 @@
}
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
+
+/* ========================================================================= *
+ * END OF INLINES FOR DEPRECATED FUNCTIONS *
+ * ========================================================================= */
+
+
#ifdef __cplusplus
}
#endif
diff --git a/inc/qcbor/qcbor_tag_decode.h b/inc/qcbor/qcbor_tag_decode.h
index 7efd9e2..d952387 100644
--- a/inc/qcbor/qcbor_tag_decode.h
+++ b/inc/qcbor/qcbor_tag_decode.h
@@ -213,6 +213,8 @@
*
* This converts all the date formats into one format of an unsigned
* integer plus a floating-point fraction.
+ *
+ * This is a call back to be installed by QCBORDecode_InstallTagDecoders().
*/
QCBORError
QCBORDecode_DateEpochTagCB(QCBORDecodeContext *pDecodeCtx,
@@ -242,6 +244,8 @@
*
* This is much simpler than the other epoch date format because
* floating-porint is not allowed. This is mostly a simple type check.
+ *
+ * This is a call back to be installed by QCBORDecode_InstallTagDecoders().
*/
QCBORError
QCBORDecode_DaysEpochTagCB(QCBORDecodeContext *pDecodeCtx,
@@ -278,10 +282,10 @@
* @ref CBOR_TAG_BIN_UUID,
* @ref CBOR_TAG_CBOR_SEQUENCE
*
- * This maps the CBOR tag to the QCBOR type and checks the content
- * type. Nothing more. It may not be the most important
- * functionality, but it part of implementing as much of RFC 8949 as
- * possible.
+ * This maps the CBOR tag to the QCBOR type and checks the tag content
+ * type. Nothing more.
+ *
+ * This is a call back to be installed by QCBORDecode_InstallTagDecoders().
*/
QCBORError
QCBORDecode_StringsTagCB(QCBORDecodeContext *pDecodeCtx,
@@ -305,6 +309,8 @@
* This works for :
* @ref CBOR_TAG_BINARY_MIME,
* @ref CBOR_TAG_MIME
+ *
+ * This is a call back to be installed by QCBORDecode_InstallTagDecoders().
*/
QCBORError
QCBORDecode_MIMETagCB(QCBORDecodeContext *pDecodeCtx,
@@ -336,6 +342,8 @@
* This works for:
* @ref CBOR_TAG_DECIMAL_FRACTION,
* @ref CBOR_TAG_BIGFLOAT
+ *
+ * This is a call back to be installed by QCBORDecode_InstallTagDecoders().
*/
QCBORError
QCBORDecode_ExpMantissaTagCB(QCBORDecodeContext *pDecodeCtx,
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 73ae425..84bf70f 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -94,13 +94,6 @@
#endif
-
-
-#define SIZEOF_C_ARRAY(array,type) (sizeof(array)/sizeof(type))
-
-
-
-
static bool
QCBORItem_IsMapOrArray(const QCBORItem Item)
{
@@ -5167,53 +5160,6 @@
-
-/**
- * @brief Common processing for a big number tag.
- *
- * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
- * @param[in] pItem The item with the date.
- * @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
- * the tag content is correct and copy forward any further other tag
- * numbers.
- */
-static void
-QCBORDecode_Private_BigNumberRawMain(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- QCBORItem *pItem,
- UsefulBufC *pBignumber,
- bool *pbIsNegative,
- size_t uOffset)
-{
- // TODO: refer to the static const ones instead
-
- 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;
- }
-
- if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) {
- *pbIsNegative = false;
- } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) {
- *pbIsNegative = true;
- }
- *pBignumber = pItem->val.bigNum;
-}
-
-
static void
QCBORDecode_Private_GetMIME(QCBORDecodeContext *pMe,
const uint8_t uTagRequirement,
@@ -5310,79 +5256,11 @@
}
-/*
- * Public function, see header qcbor/qcbor_spiffy_decode.h
- */
-void
-QCBORDecode_GetTBigNumberRaw(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- 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);
- QCBORDecode_Private_BigNumberRawMain(pMe,
- uTagRequirement,
- &Item,
- pBigNumber,
- pbIsNegative,
- uOffset);
-}
-
-
// Improvement: add methods for wrapped CBOR, a simple alternate
// to EnterBstrWrapped
-
-
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
/**
@@ -5645,26 +5523,29 @@
/**
* @brief Convert a CBOR big number to a uint64_t.
*
- * @param[in] BigNum Bytes of the big number to convert.
- * @param[in] uMax Maximum value allowed for the result.
- * @param[out] pResult Place to put the unsigned integer result.
+ * @param[in] BigNumber Bytes of the big number to convert.
+ * @param[in] uMax Maximum value allowed for the result.
+ * @param[out] pResult Place to put the unsigned integer result.
*
- * @returns Error code
+ * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW When the bignumber is
+ * too large to fit
+ * @retval QCBOR_SUCCESS The conversion succeeded.
*
- * Many values will overflow because a big num can represent a much
+ * Many values will overflow because a big number can represent a much
* larger range than uint64_t.
*/
static QCBORError
-QCBOR_Private_ConvertBigNumToUnsigned(const UsefulBufC BigNum,
- const uint64_t uMax,
- uint64_t *pResult)
+QCBORDecode_Private_BigNumberToUInt(const UsefulBufC BigNumber,
+ const uint64_t uMax,
+ uint64_t *pResult)
{
uint64_t uResult;
+ size_t uLen;
+
+ const uint8_t *pByte = BigNumber.ptr;
uResult = 0;
- const uint8_t *pByte = BigNum.ptr;
- size_t uLen = BigNum.len;
- while(uLen--) {
+ for(uLen = BigNumber.len; uLen > 0; uLen--) {
if(uResult > (uMax >> 8)) {
return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
}
@@ -5679,45 +5560,49 @@
/**
* @brief Convert a CBOR postive big number to a uint64_t.
*
- * @param[in] BigNum Bytes of the big number to convert.
- * @param[out] pResult Place to put the unsigned integer result.
+ * @param[in] BigNumber Bytes of the big number to convert.
+ * @param[out] pResult Place to put the unsigned integer result.
*
- * @returns Error code
+ * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW When the bignumber is
+ * too large to fit
+ * @retval QCBOR_SUCCESS The conversion succeeded.
*
- * Many values will overflow because a big num can represent a much
+ * Many values will overflow because a big num can represent a much
* larger range than uint64_t.
*/
static QCBORError
-QCBOR_Private_ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum,
- uint64_t *pResult)
+QCBORDecode_Private_PositiveBigNumberToUInt(const UsefulBufC BigNumber,
+ uint64_t *pResult)
{
- return QCBOR_Private_ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult);
+ return QCBORDecode_Private_BigNumberToUInt(BigNumber, UINT64_MAX, pResult);
}
/**
* @brief Convert a CBOR positive big number to an int64_t.
*
- * @param[in] BigNum Bytes of the big number to convert.
- * @param[out] pResult Place to put the signed integer result.
+ * @param[in] BigNumber Bytes of the big number to convert.
+ * @param[out] pResult Place to put the signed integer result.
*
- * @returns Error code
+ * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW When the bignumber is
+ * too large to fit
+ * @retval QCBOR_SUCCESS The conversion succeeded.
*
- * Many values will overflow because a big num can represent a much
+ * Many values will overflow because a big num can represent a much
* larger range than int64_t.
*/
static QCBORError
-QCBOR_Private_ConvertPositiveBigNumToSigned(const UsefulBufC BigNum,
- int64_t *pResult)
+QCBORDecode_Private_PositiveBigNumberToInt(const UsefulBufC BigNumber,
+ int64_t *pResult)
{
- uint64_t uResult;
- QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum,
- INT64_MAX,
- &uResult);
- if(uError) {
+ uint64_t uResult;
+ QCBORError uError;
+
+ uError = QCBORDecode_Private_BigNumberToUInt(BigNumber, INT64_MAX, &uResult);
+ if(uError != QCBOR_SUCCESS) {
return uError;
}
- /* Cast is safe because ConvertBigNumToUnsigned limits to INT64_MAX */
+ /* Cast safe because QCBORDecode_Private_BigNumberToUInt() limits to INT64_MAX */
*pResult = (int64_t)uResult;
return QCBOR_SUCCESS;
}
@@ -5726,20 +5611,22 @@
/**
* @brief Convert a CBOR negative big number to an int64_t.
*
- * @param[in] BigNum Bytes of the big number to convert.
+ * @param[in] BigNumber Bytes of the big number to convert.
* @param[out] pnResult Place to put the signed integer result.
*
- * @returns Error code
+ * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW When the bignumber is
+ * too large to fit
+ * @retval QCBOR_SUCCESS The conversion succeeded.
*
- * Many values will overflow because a big num can represent a much
+ * Many values will overflow because a big num can represent a much
* larger range than int64_t.
*/
static QCBORError
-QCBOR_Private_ConvertNegativeBigNumToSigned(const UsefulBufC BigNum,
- int64_t *pnResult)
+QCBORDecode_Private_NegativeBigNumberToInt(const UsefulBufC BigNumber,
+ int64_t *pnResult)
{
- uint64_t uResult;
- QCBORError uError;
+ 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
@@ -5753,7 +5640,7 @@
* -n - 1 <= -INT64_MAX - 1
* n <= INT64_MAX.
*/
- uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult);
+ uError = QCBORDecode_Private_BigNumberToUInt(BigNumber, INT64_MAX, &uResult);
if(uError != QCBOR_SUCCESS) {
return uError;
}
@@ -5767,7 +5654,66 @@
return QCBOR_SUCCESS;
}
+/**
+ * @brief Convert an integer to a big number.
+ *
+ * @param[in] uNum The integer to convert.
+ * @param[in] BigNumberBuf The buffer to output the big number to.
+ *
+ * @returns The big number or NULLUsefulBufC is the buffer is to small.
+ *
+ * This always succeeds unless the buffer is too small.
+ */
+static UsefulBufC
+QCBORDecode_Private_UIntToBigNumber(uint64_t uNum, const UsefulBuf BigNumberBuf)
+{
+ UsefulOutBuf UOB;
+ /* With a UsefulOutBuf, there's no pointer math */
+ UsefulOutBuf_Init(&UOB, BigNumberBuf);
+
+ /* Must copy one byte even if zero. The loop, mask and shift
+ * algorithm provides endian conversion.
+ */
+ do {
+ UsefulOutBuf_InsertByte(&UOB, uNum & 0xff, 0);
+ uNum >>= 8;
+ } while(uNum);
+
+ return UsefulOutBuf_OutUBuf(&UOB);
+}
+
+#ifndef QCBOR_DISABLE_FLOAT_HW_USE
+/**
+ * @brief Convert a big number to double-precision float.
+ *
+ * @param[in] BigNumber The big number to convert.
+ *
+ * @returns The double value.
+ *
+ * This will always succeed. It will lose precision for larger
+ * numbers. If the big number is too large to fit (more than
+ * 1.7976931348623157E+308) infinity will be returned. NaN is never
+ * returned.
+ */
+static double
+QCBORDecode_Private_BigNumberToDouble(const UsefulBufC BigNumber)
+{
+ double dResult;
+ size_t uLen;
+
+ const uint8_t *pByte = BigNumber.ptr;
+
+ dResult = 0.0;
+ /* This will overflow and become the float value INFINITY if the number
+ * is too large to fit. */
+ for(uLen = BigNumber.len; uLen > 0; uLen--){
+ dResult = (dResult * 256.0) + (double)*pByte++;
+ }
+
+ return dResult;
+}
+#endif /* ! QCBOR_DISABLE_FLOAT_HW_USE */
/**
@@ -5851,1171 +5797,613 @@
}
-/**
- * @brief Almost-public method to decode a number and convert to int64_t (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] pnValue Result of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetInt64Convert().
+
+#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
+/*
+ * Public function, see header qcbor/qcbor_spiffy_decode.h file
*/
void
-QCBORDecode_Private_GetInt64Convert(QCBORDecodeContext *pMe,
- uint32_t uConvertTypes,
- int64_t *pnValue,
- QCBORItem *pItem)
+QCBORDecode_GetNumberConvertPrecisely(QCBORDecodeContext *pMe,
+ QCBORItem *pNumber)
{
- QCBORDecode_VGetNext(pMe, pItem);
- if(pMe->uLastError) {
- return;
- }
+ QCBORItem Item;
+ struct IEEE754_ToInt ToInt;
+ double dNum;
+ QCBORError uError;
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem,
- uConvertTypes,
- pnValue);
-}
-
-/**
- * @brief Almost-public method to decode a number and convert to int64_t (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] nLabel Label to find in map.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] pnValue Result of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetInt64ConvertInMapN().
- */
-void
-QCBORDecode_Private_GetInt64ConvertInMapN(QCBORDecodeContext *pMe,
- int64_t nLabel,
- uint32_t uConvertTypes,
- int64_t *pnValue,
- QCBORItem *pItem)
-{
- QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
if(pMe->uLastError != QCBOR_SUCCESS) {
return;
}
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem,
- uConvertTypes,
- pnValue);
-}
-
-/**
- * @brief Almost-public method to decode a number and convert to int64_t (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] szLabel Label to find in map.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] pnValue Result of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetInt64ConvertInMapSZ().
- */
-void
-QCBORDecode_Private_GetInt64ConvertInMapSZ(QCBORDecodeContext *pMe,
- const char * szLabel,
- uint32_t uConvertTypes,
- int64_t *pnValue,
- QCBORItem *pItem)
-{
- QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
- if(pMe->uLastError != QCBOR_SUCCESS) {
+ // TODO:VGetNext?
+ uError = QCBORDecode_GetNext(pMe, &Item);
+ if(uError != QCBOR_SUCCESS) {
+ *pNumber = Item;
+ pMe->uLastError = (uint8_t)uError;
return;
}
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem,
- uConvertTypes,
- pnValue);
-}
-
-
-/**
- * @brief Convert many number types to an int64_t.
- *
- * @param[in] pItem The item to convert.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] pnValue The resulting converted value.
- *
- * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested
- * in uConvertTypes.
- * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
- * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large
- * or too small.
- */
-static QCBORError
-QCBOR_Private_Int64ConvertAll(const QCBORItem *pItem,
- const uint32_t uConvertTypes,
- int64_t *pnValue)
-{
- switch(pItem->uDataType) {
-
- case QCBOR_TYPE_POSBIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
- return QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_NEGBIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
- return QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
- case QCBOR_TYPE_DECIMAL_FRACTION:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
- pItem->val.expAndMantissa.nExponent,
- pnValue,
- &QCBOR_Private_Exponentitate10);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_BIGFLOAT:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) {
- return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
- pItem->val.expAndMantissa.nExponent,
- pnValue,
- QCBOR_Private_Exponentitate2);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- int64_t nMantissa;
- QCBORError uErr;
- uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
- if(uErr) {
- return uErr;
- }
- return QCBOR_Private_ExponentiateNN(nMantissa,
- pItem->val.expAndMantissa.nExponent,
- pnValue,
- QCBOR_Private_Exponentitate10);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- int64_t nMantissa;
- QCBORError uErr;
- uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
- if(uErr) {
- return uErr;
- }
- return QCBOR_Private_ExponentiateNN(nMantissa,
- pItem->val.expAndMantissa.nExponent,
- pnValue,
- QCBOR_Private_Exponentitate10);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- int64_t nMantissa;
- QCBORError uErr;
- uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
- if(uErr) {
- return uErr;
- }
- return QCBOR_Private_ExponentiateNN(nMantissa,
- pItem->val.expAndMantissa.nExponent,
- pnValue,
- QCBOR_Private_Exponentitate2);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- int64_t nMantissa;
- QCBORError uErr;
- uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
- if(uErr) {
- return uErr;
- }
- return QCBOR_Private_ExponentiateNN(nMantissa,
- pItem->val.expAndMantissa.nExponent,
- pnValue,
- QCBOR_Private_Exponentitate2);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
-
-
- default:
- return QCBOR_ERR_UNEXPECTED_TYPE; }
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe,
- const uint32_t uConvertTypes,
- int64_t *pnValue)
-{
- QCBORItem Item;
-
- QCBORDecode_Private_GetInt64Convert(pMe, uConvertTypes, pnValue, &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item,
- uConvertTypes,
- pnValue);
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint32_t uConvertTypes,
- int64_t *pnValue)
-{
- QCBORItem Item;
-
- QCBORDecode_Private_GetInt64ConvertInMapN(pMe,
- nLabel,
- uConvertTypes,
- pnValue,
- &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item,
- uConvertTypes,
- pnValue);
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- const uint32_t uConvertTypes,
- int64_t *pnValue)
-{
- QCBORItem Item;
- QCBORDecode_Private_GetInt64ConvertInMapSZ(pMe,
- szLabel,
- uConvertTypes,
- pnValue,
- &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item,
- uConvertTypes,
- pnValue);
-}
-
-
-/**
- * @brief Convert many number types to an uint64_t.
- *
- * @param[in] pItem The item to convert.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] puValue The resulting converted value.
- *
- * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested
- * in uConvertTypes.
- * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
- * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large
- * or too small.
- */
-static QCBORError
-QCBOR_Private_ConvertUInt64(const QCBORItem *pItem,
- const uint32_t uConvertTypes,
- uint64_t *puValue)
-{
- switch(pItem->uDataType) {
- case QCBOR_TYPE_DOUBLE:
- case QCBOR_TYPE_FLOAT:
-#ifndef QCBOR_DISABLE_FLOAT_HW_USE
- if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
- // Can't use llround here because it will not convert values
- // greater than INT64_MAX and less than UINT64_MAX that
- // need to be converted so it is more complicated.
- feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO);
- if(pItem->uDataType == QCBOR_TYPE_DOUBLE) {
- if(isnan(pItem->val.dfnum)) {
- return QCBOR_ERR_FLOAT_EXCEPTION;
- } else if(pItem->val.dfnum < 0) {
- return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
- } else {
- double dRounded = round(pItem->val.dfnum);
- // See discussion in DecodeDateEpoch() for
- // explanation of - 0x7ff
- if(dRounded > (double)(UINT64_MAX- 0x7ff)) {
- return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
- }
- *puValue = (uint64_t)dRounded;
- }
- } else {
- if(isnan(pItem->val.fnum)) {
- return QCBOR_ERR_FLOAT_EXCEPTION;
- } else if(pItem->val.fnum < 0) {
- return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
- } else {
- float fRounded = roundf(pItem->val.fnum);
- // See discussion in DecodeDateEpoch() for
- // explanation of - 0x7ff
- if(fRounded > (float)(UINT64_MAX- 0x7ff)) {
- return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
- }
- *puValue = (uint64_t)fRounded;
- }
- }
- if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) {
- // round() and roundf() shouldn't result in exceptions here, but
- // catch them to be robust and thorough. Don't try to
- // distinguish between the various exceptions because it seems
- // they vary by CPU, compiler and OS.
- return QCBOR_ERR_FLOAT_EXCEPTION;
- }
-
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
-#else
- return QCBOR_ERR_HW_FLOAT_DISABLED;
-#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
- break;
-
+ switch(Item.uDataType) {
case QCBOR_TYPE_INT64:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) {
- if(pItem->val.int64 >= 0) {
- *puValue = (uint64_t)pItem->val.int64;
+ case QCBOR_TYPE_UINT64:
+ *pNumber = Item;
+ break;
+
+ case QCBOR_TYPE_DOUBLE:
+ ToInt = IEEE754_DoubleToInt(Item.val.dfnum);
+ if(ToInt.type == IEEE754_ToInt_IS_INT) {
+ pNumber->uDataType = QCBOR_TYPE_INT64;
+ pNumber->val.int64 = ToInt.integer.is_signed;
+ } else if(ToInt.type == IEEE754_ToInt_IS_UINT) {
+ if(ToInt.integer.un_signed <= INT64_MAX) {
+ /* Do the same as base QCBOR integer decoding */
+ pNumber->uDataType = QCBOR_TYPE_INT64;
+ pNumber->val.int64 = (int64_t)ToInt.integer.un_signed;
} else {
- return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
+ pNumber->uDataType = QCBOR_TYPE_UINT64;
+ pNumber->val.uint64 = ToInt.integer.un_signed;
}
} else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
+ *pNumber = Item;
}
break;
- case QCBOR_TYPE_UINT64:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) {
- *puValue = pItem->val.uint64;
+ case QCBOR_TYPE_FLOAT:
+ ToInt = IEEE754_SingleToInt(Item.val.fnum);
+ if(ToInt.type == IEEE754_ToInt_IS_INT) {
+ pNumber->uDataType = QCBOR_TYPE_INT64;
+ pNumber->val.int64 = ToInt.integer.is_signed;
+ } else if(ToInt.type == IEEE754_ToInt_IS_UINT) {
+ if(ToInt.integer.un_signed <= INT64_MAX) {
+ /* Do the same as base QCBOR integer decoding */
+ pNumber->uDataType = QCBOR_TYPE_INT64;
+ pNumber->val.int64 = (int64_t)ToInt.integer.un_signed;
+ } else {
+ pNumber->uDataType = QCBOR_TYPE_UINT64;
+ pNumber->val.uint64 = ToInt.integer.un_signed;
+ }
} else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
+ *pNumber = Item;
}
break;
case QCBOR_TYPE_65BIT_NEG_INT:
- return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
-
- default:
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
-
- return QCBOR_SUCCESS;
-}
-
-
-/**
- * @brief Almost-public method to decode a number and convert to uint64_t (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] puValue Result of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetUInt64Convert().
- */
-void
-QCBORDecode_Private_GetUInt64Convert(QCBORDecodeContext *pMe,
- const uint32_t uConvertTypes,
- uint64_t *puValue,
- QCBORItem *pItem)
-{
- QCBORDecode_VGetNext(pMe, pItem);
- if(pMe->uLastError) {
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem,
- uConvertTypes,
- puValue);
-}
-
-
-/**
- * @brief Almost-public method to decode a number and convert to uint64_t (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] nLabel Label to find in map.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] puValue Result of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetUInt64ConvertInMapN().
- */
-void
-QCBORDecode_Private_GetUInt64ConvertInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint32_t uConvertTypes,
- uint64_t *puValue,
- QCBORItem *pItem)
-{
- QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
- if(pMe->uLastError != QCBOR_SUCCESS) {
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem,
- uConvertTypes,
- puValue);
-}
-
-
-/**
- * @brief Almost-public method to decode a number and convert to uint64_t (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] szLabel Label to find in map.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] puValue Result of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetUInt64ConvertInMapSZ().
- */
-void
-QCBORDecode_Private_GetUInt64ConvertInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- const uint32_t uConvertTypes,
- uint64_t *puValue,
- QCBORItem *pItem)
-{
- QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
- if(pMe->uLastError != QCBOR_SUCCESS) {
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem,
- uConvertTypes,
- puValue);
-}
-
-
-/**
- * @brief Convert many number types to an unt64_t.
- *
- * @param[in] pItem The item to convert.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] puValue The resulting converted value.
- *
- * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested
- * in uConvertTypes.
- * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
- * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large
- * or too small.
- */
-static QCBORError
-QCBOR_Private_UInt64ConvertAll(const QCBORItem *pItem,
- const uint32_t uConvertTypes,
- uint64_t *puValue)
-{
- switch(pItem->uDataType) { /* -Wmaybe-uninitialized falsly warns here */
-
- case QCBOR_TYPE_POSBIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
- return QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue);
+ if(Item.val.uint64 == UINT64_MAX) {
+ /* The value -18446744073709551616 is encoded as an
+ * unsigned 18446744073709551615. It's a whole number that
+ * needs to be returned as a double. It can't be handled
+ * by IEEE754_UintToDouble because 18446744073709551616
+ * doesn't fit into a uint64_t. You can't get it by adding
+ * 1 to 18446744073709551615.
+ */
+ pNumber->val.dfnum = -18446744073709551616.0;
+ pNumber->uDataType = QCBOR_TYPE_DOUBLE;
} else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_NEGBIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
- return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
-
- case QCBOR_TYPE_DECIMAL_FRACTION:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
- pItem->val.expAndMantissa.nExponent,
- puValue,
- QCBOR_Private_Exponentitate10);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_BIGFLOAT:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) {
- return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
- pItem->val.expAndMantissa.nExponent,
- puValue,
- QCBOR_Private_Exponentitate2);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- uint64_t uMantissa;
- QCBORError uErr;
- uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa);
- if(uErr != QCBOR_SUCCESS) {
- return uErr;
- }
- return QCBOR_Private_ExponentitateUU(uMantissa,
- pItem->val.expAndMantissa.nExponent,
- puValue,
- QCBOR_Private_Exponentitate10);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- uint64_t uMantissa;
- QCBORError uErr;
- uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum,
- &uMantissa);
- if(uErr != QCBOR_SUCCESS) {
- return uErr;
- }
- return QCBOR_Private_ExponentitateUU(uMantissa,
- pItem->val.expAndMantissa.nExponent,
- puValue,
- QCBOR_Private_Exponentitate2);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
- default:
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe,
- const uint32_t uConvertTypes,
- uint64_t *puValue)
-{
- QCBORItem Item;
-
- QCBORDecode_Private_GetUInt64Convert(pMe, uConvertTypes, puValue, &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item,
- uConvertTypes,
- puValue);
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint32_t uConvertTypes,
- uint64_t *puValue)
-{
- QCBORItem Item;
-
- QCBORDecode_Private_GetUInt64ConvertInMapN(pMe,
- nLabel,
- uConvertTypes,
- puValue,
- &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item,
- uConvertTypes,
- puValue);
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- const uint32_t uConvertTypes,
- uint64_t *puValue)
-{
- QCBORItem Item;
- QCBORDecode_Private_GetUInt64ConvertInMapSZ(pMe,
- szLabel,
- uConvertTypes,
- puValue,
- &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item,
- uConvertTypes,
- puValue);
-}
-
-
-
-
-#ifndef USEFULBUF_DISABLE_ALL_FLOAT
-/**
- * @brief Basic conversions to a double.
- *
- * @param[in] pItem The item to convert
- * @param[in] uConvertTypes Bit flags indicating source types for conversion
- * @param[out] pdValue The value converted to a double
- *
- * This does the conversions that don't need much object code,
- * the conversions from int, uint and float to double.
- *
- * See QCBOR_Private_DoubleConvertAll() for the full set
- * of conversions.
- */
-static QCBORError
-QCBOR_Private_ConvertDouble(const QCBORItem *pItem,
- const uint32_t uConvertTypes,
- double *pdValue)
-{
- switch(pItem->uDataType) {
- case QCBOR_TYPE_FLOAT:
-#ifndef QCBOR_DISABLE_FLOAT_HW_USE
- if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
- if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
- // Simple cast does the job.
- *pdValue = (double)pItem->val.fnum;
+ dNum = IEEE754_UintToDouble(Item.val.uint64 + 1, 1);
+ if(dNum == IEEE754_UINT_TO_DOUBLE_OOB) {
+ *pNumber = Item;
} else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- }
-#else /* QCBOR_DISABLE_FLOAT_HW_USE */
- return QCBOR_ERR_HW_FLOAT_DISABLED;
-#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
- break;
-
- case QCBOR_TYPE_DOUBLE:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
- if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
- *pdValue = pItem->val.dfnum;
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
+ pNumber->val.dfnum = dNum;
+ pNumber->uDataType = QCBOR_TYPE_DOUBLE;
}
}
break;
- case QCBOR_TYPE_INT64:
-#ifndef QCBOR_DISABLE_FLOAT_HW_USE
- if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) {
- // A simple cast seems to do the job with no worry of exceptions.
- // There will be precision loss for some values.
- *pdValue = (double)pItem->val.int64;
-
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
-#else
- return QCBOR_ERR_HW_FLOAT_DISABLED;
-#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
- break;
-
- case QCBOR_TYPE_UINT64:
-#ifndef QCBOR_DISABLE_FLOAT_HW_USE
- if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) {
- // A simple cast seems to do the job with no worry of exceptions.
- // There will be precision loss for some values.
- *pdValue = (double)pItem->val.uint64;
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-#else
- return QCBOR_ERR_HW_FLOAT_DISABLED;
-#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
-
- case QCBOR_TYPE_65BIT_NEG_INT:
-#ifndef QCBOR_DISABLE_FLOAT_HW_USE
- // TODO: don't use float HW. We have the function to do it.
- *pdValue = -(double)pItem->val.uint64 - 1;
- break;
-#else
- return QCBOR_ERR_HW_FLOAT_DISABLED;
-#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
-
default:
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
-
- return QCBOR_SUCCESS;
-}
-
-
-/**
- * @brief Almost-public method to decode a number and convert to double (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] uConvertTypes Bit mask list of conversion options
- * @param[out] pdValue The output of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetDoubleConvert().
- */
-void
-QCBORDecode_Private_GetDoubleConvert(QCBORDecodeContext *pMe,
- const uint32_t uConvertTypes,
- double *pdValue,
- QCBORItem *pItem)
-{
- QCBORDecode_VGetNext(pMe, pItem);
- if(pMe->uLastError) {
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem,
- uConvertTypes,
- pdValue);
-}
-
-
-/**
- * @brief Almost-public method to decode a number and convert to double (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] nLabel Label to find in map.
- * @param[in] uConvertTypes Bit mask list of conversion options
- * @param[out] pdValue The output of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetDoubleConvertInMapN().
- */
-void
-QCBORDecode_Private_GetDoubleConvertInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint32_t uConvertTypes,
- double *pdValue,
- QCBORItem *pItem)
-{
- QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
- if(pMe->uLastError != QCBOR_SUCCESS) {
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem,
- uConvertTypes,
- pdValue);
-}
-
-
-/**
- * @brief Almost-public method to decode a number and convert to double (semi-private).
- *
- * @param[in] pMe The decode context.
- * @param[in] szLabel Label to find in map.
- * @param[in] uConvertTypes Bit mask list of conversion options
- * @param[out] pdValue The output of the conversion.
- * @param[in,out] pItem Temporary space to store Item, returned item.
- *
- * See QCBORDecode_GetDoubleConvertInMapSZ().
- */
-void
-QCBORDecode_Private_GetDoubleConvertInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- const uint32_t uConvertTypes,
- double *pdValue,
- QCBORItem *pItem)
-{
- QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
- if(pMe->uLastError != QCBOR_SUCCESS) {
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem,
- uConvertTypes,
- pdValue);
-}
-
-
-#ifndef QCBOR_DISABLE_FLOAT_HW_USE
-/**
- * @brief Convert a big number to double-precision float.
- *
- * @param[in] BigNum The big number to convert
- *
- * @returns The double value.
- *
- * This will always succeed. It will lose precision for larger
- * numbers. If the big number is too large to fit (more than
- * 1.7976931348623157E+308) infinity will be returned. NaN is never
- * returned.
- */
-static double
-QCBOR_Private_ConvertBigNumToDouble(const UsefulBufC BigNum)
-{
- double dResult;
-
- dResult = 0.0;
- const uint8_t *pByte = BigNum.ptr;
- size_t uLen = BigNum.len;
- /* This will overflow and become the float value INFINITY if the number
- * is too large to fit. */
- while(uLen--) {
- dResult = (dResult * 256.0) + (double)*pByte++;
- }
-
- return dResult;
-}
-#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
-
-
-
-
-/**
- * @brief Convert many number types to a double.
- *
- * @param[in] pItem The item to convert.
- * @param[in] uConvertTypes Bit mask list of conversion options.
- * @param[out] pdValue The resulting converted value.
- *
- * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested
- * in uConvertTypes.
- * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
- * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large
- * or too small.
- */
-static QCBORError
-QCBOR_Private_DoubleConvertAll(const QCBORItem *pItem,
- const uint32_t uConvertTypes,
- double *pdValue)
-{
-#ifndef QCBOR_DISABLE_FLOAT_HW_USE
- /*
- * What Every Computer Scientist Should Know About Floating-Point Arithmetic
- * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
- */
- switch(pItem->uDataType) {
-
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
- case QCBOR_TYPE_DECIMAL_FRACTION:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- // Underflow gives 0, overflow gives infinity
- *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
- pow(10.0, (double)pItem->val.expAndMantissa.nExponent);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
+ pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
+ pNumber->uDataType = QCBOR_TYPE_NONE;
break;
-
- case QCBOR_TYPE_BIGFLOAT:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) {
- // Underflow gives 0, overflow gives infinity
- *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
- exp2((double)pItem->val.expAndMantissa.nExponent);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
-
- case QCBOR_TYPE_POSBIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
- *pdValue = QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_NEGBIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
- *pdValue = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
- case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
- *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- /* Must subtract 1 for CBOR negative integer offset */
- double dMantissa = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
- *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) {
- double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
- *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-
- case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
- if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) {
- double dMantissa = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
- *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
- } else {
- return QCBOR_ERR_UNEXPECTED_TYPE;
- }
- break;
-#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
-
- default:
- return QCBOR_ERR_UNEXPECTED_TYPE;
}
-
- return QCBOR_SUCCESS;
-
-#else
- (void)pItem;
- (void)uConvertTypes;
- (void)pdValue;
- return QCBOR_ERR_HW_FLOAT_DISABLED;
-#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
-
}
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe,
- const uint32_t uConvertTypes,
- double *pdValue)
-{
-
- QCBORItem Item;
-
- QCBORDecode_Private_GetDoubleConvert(pMe, uConvertTypes, pdValue, &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item,
- uConvertTypes,
- pdValue);
-}
+#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe,
- const int64_t nLabel,
- const uint32_t uConvertTypes,
- double *pdValue)
-{
- QCBORItem Item;
-
- QCBORDecode_Private_GetDoubleConvertInMapN(pMe,
- nLabel,
- uConvertTypes,
- pdValue,
- &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item,
- uConvertTypes,
- pdValue);
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h file
- */
-void
-QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- const uint32_t uConvertTypes,
- double *pdValue)
-{
- QCBORItem Item;
- QCBORDecode_Private_GetDoubleConvertInMapSZ(pMe,
- szLabel,
- uConvertTypes,
- pdValue,
- &Item);
-
- if(pMe->uLastError == QCBOR_SUCCESS) {
- // The above conversion succeeded
- return;
- }
-
- if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
- // The above conversion failed in a way that code below can't correct
- return;
- }
-
- pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item,
- uConvertTypes,
- pdValue);
-}
-#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
-
-
-
-
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
-/**
- * @brief Convert an integer to a big number
+/* Add one to the big number and put the result in a new UsefulBufC
+ * from storage in UsefulBuf.
*
- * @param[in] uInt The integer to convert.
- * @param[in] Buffer The buffer to output the big number to.
+ * Leading zeros must be removed before calling this.
*
- * @returns The big number or NULLUsefulBufC is the buffer is to small.
- *
- * This always succeeds unless the buffer is too small.
+ * Code Reviewers: THIS FUNCTION DOES POINTER MATH
*/
static UsefulBufC
-QCBOR_Private_ConvertIntToBigNum(uint64_t uInt, const UsefulBuf Buffer)
+QCBORDecode_BigNumberCopyPlusOne(UsefulBufC BigNumber, UsefulBuf BigNumberBuf)
{
- while((uInt & 0xff00000000000000UL) == 0) {
- uInt = uInt << 8;
- };
+ uint8_t uCarry;
+ uint8_t uSourceValue;
+ const uint8_t *pSource;
+ uint8_t *pDest;
+ ptrdiff_t uDestBytesLeft;
- UsefulOutBuf UOB;
+ /* Start adding at the LSB */
+ pSource = &((const uint8_t *)BigNumber.ptr)[BigNumber.len-1];
+ pDest = &((uint8_t *)BigNumberBuf.ptr)[BigNumberBuf.len-1];
- UsefulOutBuf_Init(&UOB, Buffer);
+ uCarry = 1; /* Gets set back to zero if add the next line doesn't wrap */
+ *pDest = *pSource + 1;
+ while(1) {
+ /* Wrap around from 0xff to 0 is a defined operation for
+ * unsigned addition in C.*/
+ if(*pDest != 0) {
+ /* The add operation didn't wrap so no more carry. This
+ * funciton only adds one, so when there is no more carry,
+ * carrying is over to the end.
+ */
+ uCarry = 0;
+ }
- while(uInt) {
- UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56));
- uInt = uInt << 8;
+ uDestBytesLeft = pDest - (uint8_t *)BigNumberBuf.ptr;
+ if(pSource <= (const uint8_t *)BigNumber.ptr && uCarry == 0) {
+ break; /* Successful exit */
+ }
+ if(pSource > (const uint8_t *)BigNumber.ptr) {
+ uSourceValue = *--pSource;
+ } else {
+ /* All source bytes processed, but not the last carry */
+ uSourceValue = 0;
+ }
+
+ pDest--;
+ if(uDestBytesLeft < 0) {
+ return NULLUsefulBufC; /* Not enough space in destination buffer */
+ }
+
+ *pDest = uSourceValue + uCarry;
}
- return UsefulOutBuf_OutUBuf(&UOB);
+ return (UsefulBufC){pDest, BigNumberBuf.len - (size_t)uDestBytesLeft};
}
+
+/* This returns 1 when uNum is 0 */
+static size_t
+QCBORDecode_Private_CountNonZeroBytes(uint64_t uNum)
+{
+ size_t uCount = 0;
+ do {
+ uCount++;
+ uNum >>= 8;
+ } while(uNum);
+
+ return uCount;
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h
+ */
+QCBORError
+QCBORDecode_ProcessBigNumberNoPreferred(const QCBORItem Item,
+ const UsefulBuf BigNumberBuf,
+ UsefulBufC *pBigNumber,
+ bool *pbIsNegative)
+{
+ size_t uLen;
+ UsefulBufC BigNumber;
+ int uType;
+
+ uType = Item.uDataType;
+ if(uType == QCBOR_TYPE_BYTE_STRING) {
+ uType = *pbIsNegative ? QCBOR_TYPE_NEGBIGNUM : QCBOR_TYPE_POSBIGNUM;
+ }
+
+ static const uint8_t Zero[] = {0x00};
+ 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;
+
+ case QCBOR_TYPE_UINT64:
+ uLen = QCBORDecode_Private_CountNonZeroBytes(Item.val.uint64);
+ break;
+
+ case QCBOR_TYPE_65BIT_NEG_INT:
+ uLen = Item.val.uint64 == UINT64_MAX ? 9 : QCBORDecode_Private_CountNonZeroBytes(Item.val.uint64);
+ 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 */
+ }
+
+ uResult = QCBOR_SUCCESS;
+
+ if(uType == QCBOR_TYPE_UINT64) {
+ *pBigNumber = QCBORDecode_Private_UIntToBigNumber(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;
+ *pBigNumber = QCBORDecode_Private_UIntToBigNumber((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 */
+ static const uint8_t TwoToThe64[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ *pBigNumber = UsefulBuf_Copy(BigNumberBuf, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(TwoToThe64));
+ } else {
+ /* +1 because negative big numbers are encoded one less than actual */
+ *pBigNumber = QCBORDecode_Private_UIntToBigNumber(Item.val.uint64 + 1, BigNumberBuf);
+ }
+ }
+
+ 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]
+
+/**
+ * @brief Common processing for a big number tag.
+ *
+ * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX.
+ * @param[in] pItem The item with the date.
+ * @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
+ * the tag content is correct and copy forward any further other tag
+ * numbers.
+ */
+static void
+QCBORDecode_Private_BigNumberRawMain(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ QCBORItem *pItem,
+ UsefulBufC *pBignumber,
+ bool *pbIsNegative,
+ size_t uOffset)
+{
+ QCBORDecode_Private_ProcessTagItemMulti(pMe,
+ pItem,
+ uTagRequirement,
+ QCBORDecode_Private_BigNumberTypesNoPreferred,
+ QCBORDecode_Private_BigNumberTagNumbers,
+ QCBORDecode_StringsTagCB,
+ uOffset);
+ if(pMe->uLastError) {
+ return;
+ }
+
+ if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) {
+ *pbIsNegative = false;
+ } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) {
+ *pbIsNegative = true;
+ }
+ *pBignumber = pItem->val.bigNum;
+}
+
+
+static void
+QCBORDecode_Private_BigNumberNoPreferredMain(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_BigNumberTypesNoPreferred,
+ QCBORDecode_Private_BigNumberTagNumbers,
+ QCBORDecode_StringsTagCB,
+ uOffset);
+ if(pMe->uLastError) {
+ return;
+ }
+
+ 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);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h
+ */
+void
+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);
+ QCBORDecode_Private_BigNumberMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative);
+}
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h
+ */
+void
+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);
+ QCBORDecode_Private_BigNumberMain(pMe,
+ uTagRequirement,
+ &Item,
+ uOffset,
+ BigNumberBuf,
+ pBigNumber,
+ pbIsNegative);
+}
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h
+ */
+void
+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);
+ 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);
+}
+
+
+
+/*
+ * Public function, see header qcbor/qcbor_spiffy_decode.h
+ */
+void
+QCBORDecode_GetTBigNumberRaw(QCBORDecodeContext *pMe,
+ const uint8_t uTagRequirement,
+ 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);
+ QCBORDecode_Private_BigNumberRawMain(pMe,
+ uTagRequirement,
+ &Item,
+ pBigNumber,
+ pbIsNegative,
+ uOffset);
+}
+
+
+#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+
+
+// TODO: relocate these notes?
/* Some notes from the work to disable tags.
* Some are out of date since tag refactoring.
*
@@ -7137,13 +6525,13 @@
case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
*pnExponent = pItem->val.expAndMantissa.nExponent;
- uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa);
+ uErr = QCBORDecode_Private_PositiveBigNumberToInt(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa);
break;
case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
*pnExponent = pItem->val.expAndMantissa.nExponent;
- uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa);
+ uErr = QCBORDecode_Private_NegativeBigNumberToInt(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa);
break;
#endif /* QCBOR_DISABLE_TAGS */
@@ -7222,7 +6610,7 @@
* big number arithmetic. This is a bug fix for QCBOR v1.5.
*/
uMantissa--;
- *pMantissa = QCBOR_Private_ConvertIntToBigNum(uMantissa, BufferForMantissa);
+ *pMantissa = QCBORDecode_Private_UIntToBigNumber(uMantissa, BufferForMantissa);
*pnExponent = pItem->val.expAndMantissa.nExponent;
break;
@@ -7745,6 +7133,9 @@
}
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
void
QCBORDecode_GetTBigFloatBigMantissaRaw(QCBORDecodeContext *pMe,
const uint8_t uTagRequirement,
@@ -7768,9 +7159,6 @@
pnExponent);
}
-
-
-
/*
* Public function, see header qcbor/qcbor_decode.h file
*/
@@ -7826,529 +7214,1108 @@
pnExponent);
}
-
#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
-#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
-/*
- * Public function, see header qcbor/qcbor_spiffy_decode.h file
+
+
+/**
+ * @brief Almost-public method to decode a number and convert to int64_t (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] pnValue Result of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetInt64Convert().
*/
void
-QCBORDecode_GetNumberConvertPrecisely(QCBORDecodeContext *pMe,
- QCBORItem *pNumber)
+QCBORDecode_Private_GetInt64Convert(QCBORDecodeContext *pMe,
+ uint32_t uConvertTypes,
+ int64_t *pnValue,
+ QCBORItem *pItem)
{
- QCBORItem Item;
- struct IEEE754_ToInt ToInt;
- double dNum;
- QCBORError uError;
+ QCBORDecode_VGetNext(pMe, pItem);
+ if(pMe->uLastError) {
+ return;
+ }
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem,
+ uConvertTypes,
+ pnValue);
+}
+
+/**
+ * @brief Almost-public method to decode a number and convert to int64_t (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] nLabel Label to find in map.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] pnValue Result of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetInt64ConvertInMapN().
+ */
+void
+QCBORDecode_Private_GetInt64ConvertInMapN(QCBORDecodeContext *pMe,
+ int64_t nLabel,
+ uint32_t uConvertTypes,
+ int64_t *pnValue,
+ QCBORItem *pItem)
+{
+ QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
if(pMe->uLastError != QCBOR_SUCCESS) {
return;
}
- // TODO:VGetNext?
- uError = QCBORDecode_GetNext(pMe, &Item);
- if(uError != QCBOR_SUCCESS) {
- *pNumber = Item;
- pMe->uLastError = (uint8_t)uError;
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem,
+ uConvertTypes,
+ pnValue);
+}
+
+/**
+ * @brief Almost-public method to decode a number and convert to int64_t (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] szLabel Label to find in map.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] pnValue Result of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetInt64ConvertInMapSZ().
+ */
+void
+QCBORDecode_Private_GetInt64ConvertInMapSZ(QCBORDecodeContext *pMe,
+ const char * szLabel,
+ uint32_t uConvertTypes,
+ int64_t *pnValue,
+ QCBORItem *pItem)
+{
+ QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
+ if(pMe->uLastError != QCBOR_SUCCESS) {
return;
}
- switch(Item.uDataType) {
- case QCBOR_TYPE_INT64:
- case QCBOR_TYPE_UINT64:
- *pNumber = Item;
- break;
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem,
+ uConvertTypes,
+ pnValue);
+}
- case QCBOR_TYPE_DOUBLE:
- ToInt = IEEE754_DoubleToInt(Item.val.dfnum);
- if(ToInt.type == IEEE754_ToInt_IS_INT) {
- pNumber->uDataType = QCBOR_TYPE_INT64;
- pNumber->val.int64 = ToInt.integer.is_signed;
- } else if(ToInt.type == IEEE754_ToInt_IS_UINT) {
- if(ToInt.integer.un_signed <= INT64_MAX) {
- /* Do the same as base QCBOR integer decoding */
- pNumber->uDataType = QCBOR_TYPE_INT64;
- pNumber->val.int64 = (int64_t)ToInt.integer.un_signed;
- } else {
- pNumber->uDataType = QCBOR_TYPE_UINT64;
- pNumber->val.uint64 = ToInt.integer.un_signed;
- }
+
+/**
+ * @brief Convert many number types to an int64_t.
+ *
+ * @param[in] pItem The item to convert.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] pnValue The resulting converted value.
+ *
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested
+ * in uConvertTypes.
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
+ * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large
+ * or too small.
+ */
+static QCBORError
+QCBOR_Private_Int64ConvertAll(const QCBORItem *pItem,
+ const uint32_t uConvertTypes,
+ int64_t *pnValue)
+{
+ switch(pItem->uDataType) {
+
+ case QCBOR_TYPE_POSBIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
+ return QCBORDecode_Private_PositiveBigNumberToInt(pItem->val.bigNum, pnValue);
} else {
- *pNumber = Item;
+ return QCBOR_ERR_UNEXPECTED_TYPE;
}
break;
+ case QCBOR_TYPE_NEGBIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
+ return QCBORDecode_Private_NegativeBigNumberToInt(pItem->val.bigNum, pnValue);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+ case QCBOR_TYPE_DECIMAL_FRACTION:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
+ pItem->val.expAndMantissa.nExponent,
+ pnValue,
+ &QCBOR_Private_Exponentitate10);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) {
+ return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
+ pItem->val.expAndMantissa.nExponent,
+ pnValue,
+ QCBOR_Private_Exponentitate2);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ int64_t nMantissa;
+ QCBORError uErr;
+ uErr = QCBORDecode_Private_PositiveBigNumberToInt(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
+ if(uErr) {
+ return uErr;
+ }
+ return QCBOR_Private_ExponentiateNN(nMantissa,
+ pItem->val.expAndMantissa.nExponent,
+ pnValue,
+ QCBOR_Private_Exponentitate10);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ int64_t nMantissa;
+ QCBORError uErr;
+ uErr = QCBORDecode_Private_NegativeBigNumberToInt(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
+ if(uErr) {
+ return uErr;
+ }
+ return QCBOR_Private_ExponentiateNN(nMantissa,
+ pItem->val.expAndMantissa.nExponent,
+ pnValue,
+ QCBOR_Private_Exponentitate10);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ int64_t nMantissa;
+ QCBORError uErr;
+ uErr = QCBORDecode_Private_PositiveBigNumberToInt(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
+ if(uErr) {
+ return uErr;
+ }
+ return QCBOR_Private_ExponentiateNN(nMantissa,
+ pItem->val.expAndMantissa.nExponent,
+ pnValue,
+ QCBOR_Private_Exponentitate2);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ int64_t nMantissa;
+ QCBORError uErr;
+ uErr = QCBORDecode_Private_NegativeBigNumberToInt(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
+ if(uErr) {
+ return uErr;
+ }
+ return QCBOR_Private_ExponentiateNN(nMantissa,
+ pItem->val.expAndMantissa.nExponent,
+ pnValue,
+ QCBOR_Private_Exponentitate2);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
+
+
+ default:
+ return QCBOR_ERR_UNEXPECTED_TYPE; }
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe,
+ const uint32_t uConvertTypes,
+ int64_t *pnValue)
+{
+ QCBORItem Item;
+
+ QCBORDecode_Private_GetInt64Convert(pMe, uConvertTypes, pnValue, &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item,
+ uConvertTypes,
+ pnValue);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint32_t uConvertTypes,
+ int64_t *pnValue)
+{
+ QCBORItem Item;
+
+ QCBORDecode_Private_GetInt64ConvertInMapN(pMe,
+ nLabel,
+ uConvertTypes,
+ pnValue,
+ &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item,
+ uConvertTypes,
+ pnValue);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint32_t uConvertTypes,
+ int64_t *pnValue)
+{
+ QCBORItem Item;
+ QCBORDecode_Private_GetInt64ConvertInMapSZ(pMe,
+ szLabel,
+ uConvertTypes,
+ pnValue,
+ &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item,
+ uConvertTypes,
+ pnValue);
+}
+
+
+/**
+ * @brief Convert many number types to an uint64_t.
+ *
+ * @param[in] pItem The item to convert.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] puValue The resulting converted value.
+ *
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested
+ * in uConvertTypes.
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
+ * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large
+ * or too small.
+ */
+static QCBORError
+QCBOR_Private_ConvertUInt64(const QCBORItem *pItem,
+ const uint32_t uConvertTypes,
+ uint64_t *puValue)
+{
+ switch(pItem->uDataType) {
+ case QCBOR_TYPE_DOUBLE:
case QCBOR_TYPE_FLOAT:
- ToInt = IEEE754_SingleToInt(Item.val.fnum);
- if(ToInt.type == IEEE754_ToInt_IS_INT) {
- pNumber->uDataType = QCBOR_TYPE_INT64;
- pNumber->val.int64 = ToInt.integer.is_signed;
- } else if(ToInt.type == IEEE754_ToInt_IS_UINT) {
- if(ToInt.integer.un_signed <= INT64_MAX) {
- /* Do the same as base QCBOR integer decoding */
- pNumber->uDataType = QCBOR_TYPE_INT64;
- pNumber->val.int64 = (int64_t)ToInt.integer.un_signed;
+#ifndef QCBOR_DISABLE_FLOAT_HW_USE
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
+ // Can't use llround here because it will not convert values
+ // greater than INT64_MAX and less than UINT64_MAX that
+ // need to be converted so it is more complicated.
+ feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO);
+ if(pItem->uDataType == QCBOR_TYPE_DOUBLE) {
+ if(isnan(pItem->val.dfnum)) {
+ return QCBOR_ERR_FLOAT_EXCEPTION;
+ } else if(pItem->val.dfnum < 0) {
+ return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
+ } else {
+ double dRounded = round(pItem->val.dfnum);
+ // See discussion in DecodeDateEpoch() for
+ // explanation of - 0x7ff
+ if(dRounded > (double)(UINT64_MAX- 0x7ff)) {
+ return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
+ }
+ *puValue = (uint64_t)dRounded;
+ }
} else {
- pNumber->uDataType = QCBOR_TYPE_UINT64;
- pNumber->val.uint64 = ToInt.integer.un_signed;
+ if(isnan(pItem->val.fnum)) {
+ return QCBOR_ERR_FLOAT_EXCEPTION;
+ } else if(pItem->val.fnum < 0) {
+ return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
+ } else {
+ float fRounded = roundf(pItem->val.fnum);
+ // See discussion in DecodeDateEpoch() for
+ // explanation of - 0x7ff
+ if(fRounded > (float)(UINT64_MAX- 0x7ff)) {
+ return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
+ }
+ *puValue = (uint64_t)fRounded;
+ }
+ }
+ if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) {
+ // round() and roundf() shouldn't result in exceptions here, but
+ // catch them to be robust and thorough. Don't try to
+ // distinguish between the various exceptions because it seems
+ // they vary by CPU, compiler and OS.
+ return QCBOR_ERR_FLOAT_EXCEPTION;
+ }
+
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+#else
+ return QCBOR_ERR_HW_FLOAT_DISABLED;
+#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
+ break;
+
+ case QCBOR_TYPE_INT64:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) {
+ if(pItem->val.int64 >= 0) {
+ *puValue = (uint64_t)pItem->val.int64;
+ } else {
+ return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
}
} else {
- *pNumber = Item;
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_UINT64:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) {
+ *puValue = pItem->val.uint64;
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
}
break;
case QCBOR_TYPE_65BIT_NEG_INT:
- if(Item.val.uint64 == UINT64_MAX) {
- /* The value -18446744073709551616 is encoded as an
- * unsigned 18446744073709551615. It's a whole number that
- * needs to be returned as a double. It can't be handled
- * by IEEE754_UintToDouble because 18446744073709551616
- * doesn't fit into a uint64_t. You can't get it by adding
- * 1 to 18446744073709551615.
- */
- pNumber->val.dfnum = -18446744073709551616.0;
- pNumber->uDataType = QCBOR_TYPE_DOUBLE;
- } else {
- dNum = IEEE754_UintToDouble(Item.val.uint64 + 1, 1);
- if(dNum == IEEE754_UINT_TO_DOUBLE_OOB) {
- *pNumber = Item;
- } else {
- pNumber->val.dfnum = dNum;
- pNumber->uDataType = QCBOR_TYPE_DOUBLE;
- }
- }
- break;
-
- default:
- pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
- pNumber->uDataType = QCBOR_TYPE_NONE;
- break;
- }
-}
-
-#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */
-
-
-
-
-static UsefulBufC
-QCBORDecode_IntToBigNumber(uint64_t uNum,
- const UsefulBuf BigNumberBuf)
-{
- UsefulOutBuf OB;
-
- /* With a UsefulOutBuf, there's no pointer math here. */
- UsefulOutBuf_Init(&OB, BigNumberBuf);
-
- /* Must copy one byte even if zero. The loop, mask and shift
- * algorithm provides endian conversion.
- */
- do {
- UsefulOutBuf_InsertByte(&OB, uNum & 0xff, 0);
- uNum >>= 8;
- } while(uNum);
-
- return UsefulOutBuf_OutUBuf(&OB);
-}
-
-
-/* Add one to the big number and put the result in a new UsefulBufC
- * from storage in UsefulBuf.
- *
- * Leading zeros must be removed before calling this.
- *
- * Code Reviewers: THIS FUNCTION DOES POINTER MATH
- */
-static UsefulBufC
-QCBORDecode_BigNumberCopyPlusOne(UsefulBufC BigNumber,
- UsefulBuf BigNumberBuf)
-{
- uint8_t uCarry;
- uint8_t uSourceValue;
- const uint8_t *pSource;
- uint8_t *pDest;
- ptrdiff_t uDestBytesLeft;
-
- /* Start adding at the LSB */
- 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;
- while(1) {
- /* Wrap around from 0xff to 0 is a defined operation for
- * unsigned addition in C.*/
- if(*pDest != 0) {
- /* The add operation didn't wrap so no more carry. This
- * funciton only adds one, so when there is no more carry,
- * carrying is over to the end.
- */
- 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 *)BigNumber.ptr) {
- uSourceValue = *--pSource;
- } else {
- /* All source bytes processed, but not the last carry */
- uSourceValue = 0;
- }
-
- pDest--;
- if(uDestBytesLeft < 0) {
- return NULLUsefulBufC; /* Not enough space in destination buffer */
- }
-
- *pDest = uSourceValue + uCarry;
- }
-
- return (UsefulBufC){pDest, BigNumberBuf.len - (size_t)uDestBytesLeft};
-}
-
-
-/* This returns 1 when uNum is 0 */
-static size_t
-QCBORDecode_Private_CountNonZeroBytes(uint64_t uNum)
-{
- size_t uCount = 0;
- do {
- uCount++;
- uNum >>= 8;
- } while(uNum);
-
- return uCount;
-}
-
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h
- */
-QCBORError
-QCBORDecode_ProcessBigNumberNoPreferred(const QCBORItem Item,
- const UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative)
-{
- size_t uLen;
- UsefulBufC BigNumber;
- int uType;
-
- uType = Item.uDataType;
- if(uType == QCBOR_TYPE_BYTE_STRING) {
- uType = *pbIsNegative ? QCBOR_TYPE_NEGBIGNUM : QCBOR_TYPE_POSBIGNUM;
- }
-
- static const uint8_t Zero[] = {0x00};
- 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;
+ return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
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
+/**
+ * @brief Almost-public method to decode a number and convert to uint64_t (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] puValue Result of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetUInt64Convert().
*/
-QCBORError
-QCBORDecode_ProcessBigNumber(const QCBORItem Item,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative)
+void
+QCBORDecode_Private_GetUInt64Convert(QCBORDecodeContext *pMe,
+ const uint32_t uConvertTypes,
+ uint64_t *puValue,
+ QCBORItem *pItem)
{
- QCBORError uResult;
- size_t uLen;
- int uType;
+ QCBORDecode_VGetNext(pMe, pItem);
+ if(pMe->uLastError) {
+ return;
+ }
- uType = Item.uDataType;
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem,
+ uConvertTypes,
+ puValue);
+}
- switch(uType) {
+
+/**
+ * @brief Almost-public method to decode a number and convert to uint64_t (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] nLabel Label to find in map.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] puValue Result of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetUInt64ConvertInMapN().
+ */
+void
+QCBORDecode_Private_GetUInt64ConvertInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint32_t uConvertTypes,
+ uint64_t *puValue,
+ QCBORItem *pItem)
+{
+ QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem,
+ uConvertTypes,
+ puValue);
+}
+
+
+/**
+ * @brief Almost-public method to decode a number and convert to uint64_t (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] szLabel Label to find in map.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] puValue Result of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetUInt64ConvertInMapSZ().
+ */
+void
+QCBORDecode_Private_GetUInt64ConvertInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint32_t uConvertTypes,
+ uint64_t *puValue,
+ QCBORItem *pItem)
+{
+ QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem,
+ uConvertTypes,
+ puValue);
+}
+
+
+/**
+ * @brief Convert many number types to an unt64_t.
+ *
+ * @param[in] pItem The item to convert.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] puValue The resulting converted value.
+ *
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested
+ * in uConvertTypes.
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
+ * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large
+ * or too small.
+ */
+static QCBORError
+QCBOR_Private_UInt64ConvertAll(const QCBORItem *pItem,
+ const uint32_t uConvertTypes,
+ uint64_t *puValue)
+{
+ switch(pItem->uDataType) { /* -Wmaybe-uninitialized falsly warns here */
+
case QCBOR_TYPE_POSBIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
+ return QCBORDecode_Private_PositiveBigNumberToUInt(pItem->val.bigNum, puValue);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
case QCBOR_TYPE_NEGBIGNUM:
- case QCBOR_TYPE_BYTE_STRING:
- return QCBORDecode_ProcessBigNumberNoPreferred(Item, BigNumberBuf, pBigNumber, pbIsNegative);
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
+ return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+
+ case QCBOR_TYPE_DECIMAL_FRACTION:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
+ pItem->val.expAndMantissa.nExponent,
+ puValue,
+ QCBOR_Private_Exponentitate10);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) {
+ return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
+ pItem->val.expAndMantissa.nExponent,
+ puValue,
+ QCBOR_Private_Exponentitate2);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ uint64_t uMantissa;
+ QCBORError uErr;
+ uErr = QCBORDecode_Private_PositiveBigNumberToUInt(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa);
+ if(uErr != QCBOR_SUCCESS) {
+ return uErr;
+ }
+ return QCBOR_Private_ExponentitateUU(uMantissa,
+ pItem->val.expAndMantissa.nExponent,
+ puValue,
+ QCBOR_Private_Exponentitate10);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ uint64_t uMantissa;
+ QCBORError uErr;
+ uErr = QCBORDecode_Private_PositiveBigNumberToUInt(pItem->val.expAndMantissa.Mantissa.bigNum,
+ &uMantissa);
+ if(uErr != QCBOR_SUCCESS) {
+ return uErr;
+ }
+ return QCBOR_Private_ExponentitateUU(uMantissa,
+ pItem->val.expAndMantissa.nExponent,
+ puValue,
+ QCBOR_Private_Exponentitate2);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
+ default:
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe,
+ const uint32_t uConvertTypes,
+ uint64_t *puValue)
+{
+ QCBORItem Item;
+
+ QCBORDecode_Private_GetUInt64Convert(pMe, uConvertTypes, puValue, &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item,
+ uConvertTypes,
+ puValue);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint32_t uConvertTypes,
+ uint64_t *puValue)
+{
+ QCBORItem Item;
+
+ QCBORDecode_Private_GetUInt64ConvertInMapN(pMe,
+ nLabel,
+ uConvertTypes,
+ puValue,
+ &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item,
+ uConvertTypes,
+ puValue);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint32_t uConvertTypes,
+ uint64_t *puValue)
+{
+ QCBORItem Item;
+ QCBORDecode_Private_GetUInt64ConvertInMapSZ(pMe,
+ szLabel,
+ uConvertTypes,
+ puValue,
+ &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item,
+ uConvertTypes,
+ puValue);
+}
+
+
+
+
+#ifndef USEFULBUF_DISABLE_ALL_FLOAT
+/**
+ * @brief Basic conversions to a double.
+ *
+ * @param[in] pItem The item to convert
+ * @param[in] uConvertTypes Bit flags indicating source types for conversion
+ * @param[out] pdValue The value converted to a double
+ *
+ * This does the conversions that don't need much object code,
+ * the conversions from int, uint and float to double.
+ *
+ * See QCBOR_Private_DoubleConvertAll() for the full set
+ * of conversions.
+ */
+static QCBORError
+QCBOR_Private_ConvertDouble(const QCBORItem *pItem,
+ const uint32_t uConvertTypes,
+ double *pdValue)
+{
+ switch(pItem->uDataType) {
+ case QCBOR_TYPE_FLOAT:
+#ifndef QCBOR_DISABLE_FLOAT_HW_USE
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
+ // Simple cast does the job.
+ *pdValue = (double)pItem->val.fnum;
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ }
+#else /* QCBOR_DISABLE_FLOAT_HW_USE */
+ return QCBOR_ERR_HW_FLOAT_DISABLED;
+#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
+ break;
+
+ case QCBOR_TYPE_DOUBLE:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) {
+ *pdValue = pItem->val.dfnum;
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ }
break;
case QCBOR_TYPE_INT64:
- uLen = QCBORDecode_Private_CountNonZeroBytes((uint64_t)ABSOLUTE_VALUE(Item.val.int64));
+#ifndef QCBOR_DISABLE_FLOAT_HW_USE
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) {
+ // A simple cast seems to do the job with no worry of exceptions.
+ // There will be precision loss for some values.
+ *pdValue = (double)pItem->val.int64;
+
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+#else
+ return QCBOR_ERR_HW_FLOAT_DISABLED;
+#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
break;
case QCBOR_TYPE_UINT64:
- uLen = QCBORDecode_Private_CountNonZeroBytes(Item.val.uint64);
+#ifndef QCBOR_DISABLE_FLOAT_HW_USE
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) {
+ // A simple cast seems to do the job with no worry of exceptions.
+ // There will be precision loss for some values.
+ *pdValue = (double)pItem->val.uint64;
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
break;
+#else
+ return QCBOR_ERR_HW_FLOAT_DISABLED;
+#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
case QCBOR_TYPE_65BIT_NEG_INT:
- uLen = Item.val.uint64 == UINT64_MAX ? 9 : QCBORDecode_Private_CountNonZeroBytes(Item.val.uint64);
+#ifndef QCBOR_DISABLE_FLOAT_HW_USE
+ // TODO: don't use float HW. We have the function to do it.
+ *pdValue = -(double)pItem->val.uint64 - 1;
break;
+#else
+ return QCBOR_ERR_HW_FLOAT_DISABLED;
+#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
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 */
- }
-
- uResult = QCBOR_SUCCESS;
-
- 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;
- *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 */
- static const uint8_t TwoToThe64[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- *pBigNumber = UsefulBuf_Copy(BigNumberBuf, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(TwoToThe64));
- } else {
- // TODO: why + 1; test it; document it
- *pBigNumber = QCBORDecode_IntToBigNumber(Item.val.uint64 + 1, BigNumberBuf);
- }
- }
-
- return uResult;
+ return QCBOR_SUCCESS;
}
-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
-QCBORDecode_Private_BigNumberNoPreferredMain(QCBORDecodeContext *pMe,
- const uint8_t uTagRequirement,
- QCBORItem *pItem,
- const size_t uOffset,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative)
+/**
+ * @brief Almost-public method to decode a number and convert to double (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] uConvertTypes Bit mask list of conversion options
+ * @param[out] pdValue The output of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetDoubleConvert().
+ */
+void
+QCBORDecode_Private_GetDoubleConvert(QCBORDecodeContext *pMe,
+ const uint32_t uConvertTypes,
+ double *pdValue,
+ QCBORItem *pItem)
{
- QCBORDecode_Private_ProcessTagItemMulti(pMe,
- pItem,
- uTagRequirement,
- QCBORDecode_Private_BigNumberTypesNoPreferred,
- QCBORDecode_Private_BigNumberTagNumbers,
- QCBORDecode_StringsTagCB,
- uOffset);
+ QCBORDecode_VGetNext(pMe, pItem);
if(pMe->uLastError) {
return;
}
- pMe->uLastError = (uint8_t)QCBORDecode_ProcessBigNumberNoPreferred(*pItem, BigNumberBuf, pBigNumber, pbIsNegative);
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem,
+ uConvertTypes,
+ pdValue);
}
-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);
-}
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h
+/**
+ * @brief Almost-public method to decode a number and convert to double (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] nLabel Label to find in map.
+ * @param[in] uConvertTypes Bit mask list of conversion options
+ * @param[out] pdValue The output of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetDoubleConvertInMapN().
*/
void
-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);
- QCBORDecode_Private_BigNumberMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative);
-}
-
-
-
-/*
- * Public function, see header qcbor/qcbor_decode.h
- */
-void
-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);
- QCBORDecode_Private_BigNumberMain(pMe,
- uTagRequirement,
- &Item,
- uOffset,
- BigNumberBuf,
- pBigNumber,
- pbIsNegative);
-}
-
-/*
- * Public function, see header qcbor/qcbor_decode.h
- */
-void
-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);
- 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,
+QCBORDecode_Private_GetDoubleConvertInMapN(QCBORDecodeContext *pMe,
const int64_t nLabel,
- const uint8_t uTagRequirement,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative)
+ const uint32_t uConvertTypes,
+ double *pdValue,
+ QCBORItem *pItem)
{
- QCBORItem Item;
- size_t uOffset;
+ QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return;
+ }
- QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBORDecode_Private_BigNumberNoPreferredMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative);
-
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem,
+ uConvertTypes,
+ pdValue);
}
-/*
- * Public function, see header qcbor/qcbor_decode.h
+
+/**
+ * @brief Almost-public method to decode a number and convert to double (semi-private).
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] szLabel Label to find in map.
+ * @param[in] uConvertTypes Bit mask list of conversion options
+ * @param[out] pdValue The output of the conversion.
+ * @param[in,out] pItem Temporary space to store Item, returned item.
+ *
+ * See QCBORDecode_GetDoubleConvertInMapSZ().
*/
void
-QCBORDecode_GetTBigNumberNoPreferredInMapSZ(QCBORDecodeContext *pMe,
+QCBORDecode_Private_GetDoubleConvertInMapSZ(QCBORDecodeContext *pMe,
const char *szLabel,
- const uint8_t uTagRequirement,
- UsefulBuf BigNumberBuf,
- UsefulBufC *pBigNumber,
- bool *pbIsNegative)
+ const uint32_t uConvertTypes,
+ double *pdValue,
+ QCBORItem *pItem)
{
- QCBORItem Item;
- size_t uOffset;
+ QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
+ if(pMe->uLastError != QCBOR_SUCCESS) {
+ return;
+ }
- QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset);
- QCBORDecode_Private_BigNumberNoPreferredMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative);
+ pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem,
+ uConvertTypes,
+ pdValue);
}
+
+/**
+ * @brief Convert many number types to a double.
+ *
+ * @param[in] pItem The item to convert.
+ * @param[in] uConvertTypes Bit mask list of conversion options.
+ * @param[out] pdValue The resulting converted value.
+ *
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested
+ * in uConvertTypes.
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
+ * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large
+ * or too small.
+ */
+static QCBORError
+QCBOR_Private_DoubleConvertAll(const QCBORItem *pItem,
+ const uint32_t uConvertTypes,
+ double *pdValue)
+{
+#ifndef QCBOR_DISABLE_FLOAT_HW_USE
+ /*
+ * What Every Computer Scientist Should Know About Floating-Point Arithmetic
+ * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
+ */
+ switch(pItem->uDataType) {
+
+#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+ case QCBOR_TYPE_DECIMAL_FRACTION:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ // Underflow gives 0, overflow gives infinity
+ *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
+ pow(10.0, (double)pItem->val.expAndMantissa.nExponent);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) {
+ // Underflow gives 0, overflow gives infinity
+ *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
+ exp2((double)pItem->val.expAndMantissa.nExponent);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
+
+ case QCBOR_TYPE_POSBIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
+ *pdValue = QCBORDecode_Private_BigNumberToDouble(pItem->val.bigNum);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_NEGBIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) {
+ *pdValue = -1-QCBORDecode_Private_BigNumberToDouble(pItem->val.bigNum);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+ case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ double dMantissa = QCBORDecode_Private_BigNumberToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
+ *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
+ /* Must subtract 1 for CBOR negative integer offset */
+ double dMantissa = -1-QCBORDecode_Private_BigNumberToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
+ *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) {
+ double dMantissa = QCBORDecode_Private_BigNumberToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
+ *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+
+ case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
+ if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) {
+ double dMantissa = -1-QCBORDecode_Private_BigNumberToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
+ *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
+ } else {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+ break;
+#endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */
+
+ default:
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+
+ return QCBOR_SUCCESS;
+
+#else
+ (void)pItem;
+ (void)uConvertTypes;
+ (void)pdValue;
+ return QCBOR_ERR_HW_FLOAT_DISABLED;
+#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
+
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe,
+ const uint32_t uConvertTypes,
+ double *pdValue)
+{
+
+ QCBORItem Item;
+
+ QCBORDecode_Private_GetDoubleConvert(pMe, uConvertTypes, pdValue, &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item,
+ uConvertTypes,
+ pdValue);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const uint32_t uConvertTypes,
+ double *pdValue)
+{
+ QCBORItem Item;
+
+ QCBORDecode_Private_GetDoubleConvertInMapN(pMe,
+ nLabel,
+ uConvertTypes,
+ pdValue,
+ &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item,
+ uConvertTypes,
+ pdValue);
+}
+
+
+/*
+ * Public function, see header qcbor/qcbor_decode.h file
+ */
+void
+QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const uint32_t uConvertTypes,
+ double *pdValue)
+{
+ QCBORItem Item;
+ QCBORDecode_Private_GetDoubleConvertInMapSZ(pMe,
+ szLabel,
+ uConvertTypes,
+ pdValue,
+ &Item);
+
+ if(pMe->uLastError == QCBOR_SUCCESS) {
+ // The above conversion succeeded
+ return;
+ }
+
+ if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
+ // The above conversion failed in a way that code below can't correct
+ return;
+ }
+
+ pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item,
+ uConvertTypes,
+ pdValue);
+}
+#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
+
+
// TODO: re order above functions in tag number order
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 47208a8..a8f01fe 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -795,8 +795,8 @@
*
* @return If there is a carry, \c true.
*
- * If this returns @c true, then @c BigNumber - 1 is
- * one byte shorter than @c BigNumber.
+ * If this returns @c true, then @c BigNumber - 1 is one byte shorter
+ * than @c BigNumber.
**/
static bool
QCBOREncode_Private_BigNumberCarry(const UsefulBufC BigNumber)
@@ -821,7 +821,7 @@
}
-/*
+/**
* @brief Output negative bignum bytes with subtraction of 1.
*
* @param[in] pMe The decode context.
@@ -864,7 +864,7 @@
if(bCarry && BigNumber.len > 1 && UsefulBufC_NTH_BYTE(BigNumber, 0) >= 1) {
uLen--;
}
- QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, uLen, 0);
+ QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, uLen,0);
SubString = BigNumber;
bCopiedSomething = false;
@@ -887,61 +887,19 @@
/**
- * @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.
- */
-static bool
-QCBOREncode_Private_NegativeBigNumberToUInt(const UsefulBufC BigNumber, uint64_t *puInt)
-{
- bool bIs2exp64;
-
- static const uint8_t twoExp64[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
-
- bIs2exp64 = ! UsefulBuf_Compare(BigNumber, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(twoExp64));
-
- if(BigNumber.len > 8 && !bIs2exp64) {
- return false;
- }
-
- /* 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.
+ * @param[in] BigNumber The 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.
+ * 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.
+ * RFC 8949 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)
@@ -950,63 +908,82 @@
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++;
+ if(NLZ.len == 0 &&
+ BigNumber.len > 0 &&
+ UsefulBufC_NTH_BYTE(BigNumber, 0) == 0x00) {
+ NLZ.len = 1;
}
return NLZ;
}
-/*
- * Public functions for adding a big number. See qcbor/qcbor_encode.h
+/**
+ * @brief Output a big number, preferred or not, with negative offset
+ *
+ * @param[in] pMe The decode context.
+ * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
+ * @ref QCBOR_ENCODE_AS_BORROWED.
+ * @param[in] bPreferred Uses preferred serialization if true
+ * @param[in] bNegative Indicates big number is negative or postive.
+ * @param[in] BigNumber The big number.
+ *
+ * Regardless of whether preferred serialization is used, if the big
+ * number is negative, one is subtracted before is output per CBOR
+ * convetion for big numbers. This requires a little big number
+ * arithmetic and adds some object code.
+ *
+ * If preferred serialization is used, then if the number is smaller
+ * than UINT64_MAX and postive it is output as type 0 and if it is
+ * equal to or smaller than UINT64_MAX it is output as a type 1
+ * integer minus one.
+ *
+ * See QCBOREncode_AddTBigNumberRaw() for simple copy through.
*/
void
-QCBOREncode_AddTBigNumber(QCBOREncodeContext *pMe,
- const uint8_t uTagRequirement,
- const bool bNegative,
- const UsefulBufC BigNumber)
+QCBOREncode_Private_AddTBigNumberMain(QCBOREncodeContext *pMe,
+ const uint8_t uTagRequirement,
+ const bool bPreferred,
+ const bool bNegative,
+ const UsefulBufC BigNumber)
{
- uint64_t uInt;
+ uint64_t uInt;
+ bool bIs2exp64;
+ uint8_t uMajorType;
+ UsefulBufC BigNumberNLZ;
- const UsefulBufC BigNumberNLZ = QCBOREncode_Private_SkipLeadingZeros(BigNumber);
+ 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);
+ static const uint8_t twoExp64[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ bIs2exp64 = ! UsefulBuf_Compare(BigNumber, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(twoExp64));
+
+ if(bPreferred && (BigNumberNLZ.len <= 8 || (bNegative && bIs2exp64))) {
+ 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_BigNumberToUInt(BigNumberNLZ);
+ if(bNegative) {
+ uInt--;
+ }
+ }
+ uMajorType = bNegative ? CBOR_MAJOR_TYPE_NEGATIVE_INT :
+ CBOR_MAJOR_TYPE_POSITIVE_INT;
+ QCBOREncode_Private_AppendCBORHead(pMe, uMajorType, uInt, 0);
+ } else {
+ if(bNegative) {
QCBOREncode_Private_AddTNegativeBigNumber(pMe, uTagRequirement, BigNumberNLZ);
- }
-
- } else {
- if(BigNumberNLZ.len <= sizeof(uint64_t)) {
- QCBOREncode_AddUInt64(pMe, QCBOREncode_Private_BigNumberToUInt(BigNumberNLZ));
} else {
- QCBOREncode_AddTBigNumberRaw(pMe, bNegative, uTagRequirement, BigNumberNLZ);
+ QCBOREncode_AddTBigNumberRaw(pMe, false, 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
@@ -1078,7 +1055,7 @@
* that has no effect on the code here.
*/
if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- QCBOREncode_AddTag(pMe, uTagNumber);
+ QCBOREncode_AddTagNumber(pMe, uTagNumber);
}
QCBOREncode_OpenArray(pMe);
QCBOREncode_AddInt64(pMe, nExponent);
@@ -1107,7 +1084,7 @@
* for CBOR negative numbers.
*/
if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
- QCBOREncode_AddTag(pMe, uTagNumber);
+ QCBOREncode_AddTagNumber(pMe, uTagNumber);
}
QCBOREncode_OpenArray(pMe);
QCBOREncode_AddInt64(pMe, nExponent);
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 2355fe0..9360da2 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -4790,6 +4790,13 @@
true
},
{
+ "-18446744073709551615 preferred",
+ {"\x3B\xff\xff\xff\xff\xff\xff\xff\xfe", 9},
+ QCBOR_SUCCESS,
+ {"\xff\xff\xff\xff\xff\xff\xff\xff", 8},
+ true
+ },
+ {
"-18446744073709551616 as big num",
{"\xC3\x48\xff\xff\xff\xff\xff\xff\xff\xff", 10},
QCBOR_SUCCESS,
@@ -8741,7 +8748,7 @@
QCBOREncode_Init(&EC, OutputBuffer);
#ifndef QCBOR_DISABLE_TAGS
- QCBOREncode_AddTag(&EC, CBOR_TAG_CBOR);
+ QCBOREncode_AddTagNumber(&EC, CBOR_TAG_CBOR);
#endif /* ! QCBOR_DISABLE_TAGS */
QCBOREncode_BstrWrap(&EC);
QCBOREncode_OpenMap(&EC);