Option to disable all tag processing; Minor bug fix to QCBORDecode_GetDecimalFraction()
This allows disabling of all tag decoding saving about 400 bytes of object code (a lot!).
This also fixes a bug where QCBORDecode_GetDecimalFraction() and
QCBORDecode_GetDecimalFractionBig() would fail on input CBOR that is not a tag when it should succeed.
* Check point work on disabling tags
* Check point tag disable
* Checkpoint work on disabling tags
* check point
* More tests are passing
* Check point ; down to only 3 tests not passing
* all tests passing
* documentation fixes
* minor tidying
* more tidiness
* More documentation
* Check point -- more tidiness
* Check point new mant/exp tests
* main source is probably done; more testing to do
* Documentation fix
* Improve testing of mantissa and exponent a lot
* Final clean up
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/README.md b/README.md
index d26c56c..28e3b14 100644
--- a/README.md
+++ b/README.md
@@ -324,9 +324,9 @@
| | smallest | largest |
|---------------|----------|---------|
- | encode only | 850 | 2100 |
- | decode only | 2000 | 13300 |
- | combined | 2850 | 15500 |
+ | encode only | 900 | 2100 |
+ | decode only | 1550 | 13300 |
+ | combined | 2450 | 15500 |
From the table above, one can see that the amount of code pulled in
from the QCBOR library varies a lot, ranging from 1KB to 15KB. The
@@ -381,6 +381,7 @@
| QCBOR_DISABLE_EXP_AND_MANTISSA | 400 |
| QCBOR_DISABLE_PREFERRED_FLOAT | 900 |
| QCBOR_DISABLE_FLOAT_HW_USE | 50 |
+ | QCBOR_DISABLE_TAGS | 400 |
| USEFULBUF_DISABLE_ALL_FLOAT | 950 |
QCBOR_DISABLE_ENCODE_USAGE_GUARDS affects encoding only. It doesn't
@@ -409,12 +410,17 @@
when an indefinite-length map or array arrives for decoding.
QCBOR_DISABLE_UNCOMMON_TAGS disables the decoding of explicit tags for
-base 64, regex, UUID and MIME data. This just disabled the automatic
+base 64, regex, UUID and MIME data. This just disables the automatic
recognition of these from a major type 6 tag.
QCBOR_DISABLE_EXP_AND_MANTISSA disables the decoding of decimal
fractions and big floats.
+QCBOR_DISABLE_TAGS disables all decoding of CBOR tags. If the input has
+a single tag, the error is unrecoverable so it is suitable only for protocols that
+have no tags. "Borrowed" tag content formats (e.g. an epoch-based date
+without the tag number), can still be processed.
+
See the discussion above on floating-point.
### Size of spiffy decode
diff --git a/inc/qcbor/qcbor_common.h b/inc/qcbor/qcbor_common.h
index e9f9a7c..127537d 100644
--- a/inc/qcbor/qcbor_common.h
+++ b/inc/qcbor/qcbor_common.h
@@ -468,6 +468,10 @@
indefinite length map or array in the input CBOR. */
QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED = 50,
+ /** All decoding of tags (major type 6) has been disabled and a tag
+ occurred in the decode input. */
+ QCBOR_ERR_TAGS_DISABLED = 51,
+
#define QCBOR_END_OF_UNRECOVERABLE_DECODE_ERRORS 59
/** More than @ref QCBOR_MAX_TAGS_PER_ITEM tags encountered for a
diff --git a/inc/qcbor/qcbor_decode.h b/inc/qcbor/qcbor_decode.h
index 65eff4c..bf30e6d 100644
--- a/inc/qcbor/qcbor_decode.h
+++ b/inc/qcbor/qcbor_decode.h
@@ -162,6 +162,23 @@
* item being sought, in which case the unrecoverable error will be
* returned. Unrecoverable errors are those indicated by
* QCBORDecode_IsUnrecoverableError().
+ *
+ * @anchor Disabilng-Tag-Decoding
+ * # Disabilng Tag Decoding
+ *
+ * If QCBOR_DISABLE_TAGS is defined, all code for decoding tags will
+ * be omitted reducing the core decoder, QCBORDecode_VGetNext(), by
+ * about 400 bytes. If a tag number is encountered in the decoder
+ * input the unrecoverable error @ref QCBOR_ERR_TAGS_DISABLED will be
+ * returned. No input with tags can be decoded.
+ *
+ * Decode functions like QCBORDecode_GetEpochDate() and
+ * QCBORDecode_GetDecimalFraction() that can decode the tag content
+ * even if the tag number is absent are still available. Typically
+ * they won't be linked in because of dead stripping. The
+ * @c uTagRequirement parameter has no effect, but if it is
+ * @ref QCBOR_TAG_REQUIREMENT_TAG, @ref QCBOR_ERR_TAGS_DISABLED
+ * will be set.
*/
/**
@@ -481,6 +498,7 @@
uint64_t uint64;
} label;
+#ifndef QCBOR_DISABLE_TAGS
/**
* The tags numbers for which the item is the tag content. Tags
* nest, so index 0 in the array is the tag on the data item
@@ -502,6 +520,7 @@
* having to reference this array. Also see @ref Tags-Overview.
*/
uint16_t uTags[QCBOR_MAX_TAGS_PER_ITEM];
+#endif
} QCBORItem;
diff --git a/inc/qcbor/qcbor_spiffy_decode.h b/inc/qcbor/qcbor_spiffy_decode.h
index 9103782..0faddc3 100644
--- a/inc/qcbor/qcbor_spiffy_decode.h
+++ b/inc/qcbor/qcbor_spiffy_decode.h
@@ -88,7 +88,7 @@
## Tag Usage
Data types beyond the basic CBOR types of numbers, strings, maps and
- arrays are called tags. The main registry of these new types is in in
+ arrays are called tags. The main registry of these new types is in
the IANA CBOR tags registry. These new types may be simple such a
number that is to be interpreted as a date, or of moderate complexity
such as defining a decimal fraction that is an array containing a
@@ -296,6 +296,12 @@
See also QCBORDecode_GetInt64ConvertAll() which does some of these
conversions, but links in much less object code. See also
QCBORDecode_GetUInt64ConvertAll().
+
+ This relies on CBOR tags to identify big numbers, decimal fractions
+ and big floats. It will not attempt to decode non-tag CBOR that might
+ be one of these. (If QCBOR_DISABLE_TAGS is set, this is effectively
+ the same as QCBORDecode_GetInt64Convert() because all the additional
+ number types this decodes are tags).
*/
void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pCtx,
uint32_t uConvertTypes,
@@ -1210,6 +1216,10 @@
See also @ref CBOR_TAG_DECIMAL_FRACTION,
QCBOREncode_AddDecimalFraction(), @ref QCBOR_TYPE_DECIMAL_FRACTION
and QCBORDecode_GetDecimalFractionBig().
+
+ If QCBOR_DISABLE_TAGS is set, the only input this will decode is
+ an array of two integers. It will set an error if the the array is preceded
+ by by a tag number or if the mantissa is a big number.
*/
void QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pCtx,
uint8_t uTagRequirement,
@@ -1297,6 +1307,10 @@
mantissa * ( 2 ** exponent )
+ If the mantissa is a tag that is a positive or negative big number,
+ this will attempt to fit it into the int64_t that @c pnMantissa is
+ and set an overflow error if it doesn't fit.
+
See also QCBORDecode_GetInt64ConvertAll(),
QCBORDecode_GetUInt64ConvertAll() and
QCBORDecode_GetDoubleConvertAll() which can convert big floats.
@@ -1979,21 +1993,28 @@
-// Semi private (this may change in the future)
#define QCBOR_TAGSPEC_NUM_TYPES 4
-/* Improvement: Carefully understand what compilers do with this,
-particularly initialization and see if it can be optimized so
-there is less code and maybe so it can be smaller. */
+/* Semi-private data structure (which might change).
+ *
+ * See CheckTagRequirement() which uses this to check the type of a
+ * item to be decoded as a tag or tag content.
+ *
+ * Improvement: Carefully understand what compilers do with this,
+ * particularly initialization and see if it can be optimized so there
+ * is less code and maybe so it can be smaller.
+ */
typedef struct {
/* One of QCBOR_TAGSPEC_MATCH_xxx */
uint8_t uTagRequirement;
- /* The tagged type translated into QCBOR_TYPE_XXX. Used to match explicit
- tagging */
+ /* The tagged type translated into QCBOR_TYPE_XXX. Used to match
+ * explicit tagging */
uint8_t uTaggedTypes[QCBOR_TAGSPEC_NUM_TYPES];
- /* The types of the content, which are used to match implicit tagging */
+ /* The types of the content, which are used to match implicit
+ * tagging */
uint8_t uAllowedContentTypes[QCBOR_TAGSPEC_NUM_TYPES];
} TagSpecification;
+
// Semi private
void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe,
TagSpecification TagSpec,
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 46c4f12..841f49a 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -1163,12 +1163,16 @@
break;
case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */
+#ifndef QCBOR_DISABLE_TAGS
if(nAdditionalInfo == LEN_IS_INDEFINITE) {
uReturn = QCBOR_ERR_BAD_INT;
} else {
pDecodedItem->val.uTagV = uArgument;
pDecodedItem->uDataType = QCBOR_TYPE_TAG;
}
+#else /* QCBOR_DISABLE_TAGS */
+ uReturn = QCBOR_ERR_TAGS_DISABLED;
+#endif /* QCBOR_DISABLE_TAGS */
break;
case CBOR_MAJOR_TYPE_SIMPLE:
@@ -1347,6 +1351,7 @@
}
+#ifndef QCBOR_DISABLE_TAGS
/**
* @brief This converts a tag number to a shorter mapped value for storage.
*
@@ -1421,6 +1426,7 @@
return pMe->auMappedTags[uIndex];
}
}
+#endif /* QCBOR_DISABLE_TAGS */
/**
@@ -1450,6 +1456,7 @@
static QCBORError
QCBORDecode_GetNextTagNumber(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem)
{
+#ifndef QCBOR_DISABLE_TAGS
/* Accummulate the tags from multiple items here and then copy them
* into the last item, the non-tag item.
*/
@@ -1508,8 +1515,13 @@
Done:
return uReturn;
-}
+#else /* QCBOR_DISABLE_TAGS */
+
+ return QCBORDecode_GetNextFullString(pMe, pDecodedItem);
+
+#endif /* QCBOR_DISABLE_TAGS */
+}
/**
* @brief Combine a map entry label and value into one item (decode layer 3).
@@ -1900,6 +1912,7 @@
}
+#ifndef QCBOR_DISABLE_TAGS
/**
* @brief Shift 0th tag out of the tag list.
*
@@ -1916,6 +1929,7 @@
pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16;
}
+#endif /* QCBOR_DISABLE_TAGS */
/**
* @brief Convert different epoch date formats in to the QCBOR epoch date format
@@ -2074,6 +2088,18 @@
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
+
+/* Forward declaration is necessary for
+ * QCBORDecode_MantissaAndExponent(). to be able to decode bignum
+ * tags in the mantissa. If the mantissa is a decimal fraction or big
+ * float in error, this will result in a recurive call to
+ * QCBORDecode_MantissaAndExponent(), but the recursion will unwined
+ * correctly and the correct error is returned.
+ */
+static QCBORError
+QCBORDecode_GetNextTagContent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem);
+
+
/**
* @brief Decode decimal fractions and big floats.
*
@@ -2086,14 +2112,19 @@
* @returns Decoding errors from getting primitive data items or
* \ref QCBOR_ERR_BAD_EXP_AND_MANTISSA.
*
- * When called pDecodedItem must be the array that is tagged as a big
- * float or decimal fraction, the array that has the two members, the
+ * When called pDecodedItem must be the array with two members, the
* exponent and mantissa.
*
* This will fetch and decode the exponent and mantissa and put the
* result back into pDecodedItem.
+ *
+ * This does no checking or processing of tag numbers. That is to be
+ * done by the code that calls this.
+ *
+ * This stuffs the type of the mantissa into pDecodedItem with the expectation
+ * the caller will process it.
*/
-static inline QCBORError
+static QCBORError
QCBORDecode_MantissaAndExponent(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem)
{
QCBORError uReturn;
@@ -2105,16 +2136,12 @@
}
/* A check for pDecodedItem->val.uCount == 2 would work for
- * definite-length arrays, but not for indefnite. Instead remember
+ * definite-length arrays, but not for indefinite. Instead remember
* the nesting level the two integers must be at, which is one
* deeper than that of the array.
*/
const int nNestLevel = pDecodedItem->uNestingLevel + 1;
- /* --- Which is it, decimal fraction or a bigfloat? --- */
- const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(pMe, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION);
- pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT;
-
/* --- Get the exponent --- */
QCBORItem exponentItem;
uReturn = QCBORDecode_GetNextMapOrArray(pMe, &exponentItem);
@@ -2142,7 +2169,7 @@
/* --- Get the mantissa --- */
QCBORItem mantissaItem;
- uReturn = QCBORDecode_GetNextWithTags(pMe, &mantissaItem, NULL);
+ uReturn = QCBORDecode_GetNextTagContent(pMe, &mantissaItem);
if(uReturn != QCBOR_SUCCESS) {
goto Done;
}
@@ -2151,6 +2178,9 @@
uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
goto Done;
}
+ /* Stuff the mantissa data type into the item to send it up to the
+ * the next level. */
+ pDecodedItem->uDataType = mantissaItem.uDataType;
if(mantissaItem.uDataType == QCBOR_TYPE_INT64) {
/* Data arriving as an unsigned int < INT64_MAX has been
* converted to QCBOR_TYPE_INT64 and thus handled here. This is
@@ -2159,14 +2189,16 @@
* and thus an error that will get handled in an else below.
*/
pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64;
+#ifndef QCBOR_DISABLE_TAGS
+ /* With tags fully disabled a big number mantissa will error out
+ * in the call to QCBORDecode_GetNextWithTags() because it has
+ * a tag number.
+ */
} else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM ||
mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) {
/* Got a good big num mantissa */
pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum;
- /* Depends on numbering of QCBOR_TYPE_XXX */
- pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType +
- mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM +
- 1);
+#endif /* QCBOR_DISABLE_TAGS */
} else {
/* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */
uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
@@ -2176,6 +2208,7 @@
/* --- Check that array only has the two numbers --- */
if(mantissaItem.uNextNestLevel == nNestLevel) {
/* Extra items in the decimal fraction / big float */
+ /* Improvement: this should probably be an unrecoverable error. */
uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
goto Done;
}
@@ -2187,6 +2220,8 @@
#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
+#ifndef QCBOR_DISABLE_TAGS
+
#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
/**
* @brief Decode the MIME type tag
@@ -2215,7 +2250,6 @@
}
#endif /* QCBOR_DISABLE_UNCOMMON_TAGS */
-
/**
* Table of CBOR tags whose content is either a text string or a byte
* string. The table maps the CBOR tag to the QCBOR type. The high-bit
@@ -2303,6 +2337,31 @@
pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK);
return QCBOR_SUCCESS;
}
+#endif /* QCBOR_DISABLE_TAGS */
+
+
+#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
+/*
+ * This returns the QCBOR_TYPE for a mantissa and exponent.
+
+Called in one context where there is always a tag
+
+ Called in another context where there might be a tag or the caller might say what they are expecting.
+
+ 6 possible outputs
+ */
+static inline uint8_t
+MantissaExponentDataType(const uint16_t uTagToProcess, const QCBORItem *pDecodedItem)
+{
+ uint8_t uBase = uTagToProcess == CBOR_TAG_DECIMAL_FRACTION ?
+ QCBOR_TYPE_DECIMAL_FRACTION :
+ QCBOR_TYPE_BIGFLOAT;
+ if(pDecodedItem->uDataType != QCBOR_TYPE_INT64) {
+ uBase = (uint8_t)(uBase + pDecodedItem->uDataType - QCBOR_TYPE_POSBIGNUM + 1);
+ }
+ return uBase;
+}
+#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
/**
@@ -2328,6 +2387,7 @@
goto Done;
}
+#ifndef QCBOR_DISABLE_TAGS
/* When there are no tag numbers for the item, this exits first
* thing and effectively does nothing.
*
@@ -2357,6 +2417,9 @@
} else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION ||
uTagToProcess == CBOR_TAG_BIGFLOAT) {
uReturn = QCBORDecode_MantissaAndExponent(pMe, pDecodedItem);
+ /* --- Which is it, decimal fraction or a bigfloat? --- */
+ pDecodedItem->uDataType = MantissaExponentDataType(uTagToProcess, pDecodedItem);
+
#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
} else if(uTagToProcess == CBOR_TAG_MIME ||
@@ -2388,6 +2451,7 @@
*/
ShiftTags(pDecodedItem);
}
+#endif /* QCBOR_DISABLE_TAGS */
Done:
return uReturn;
@@ -2463,6 +2527,8 @@
QCBORItem *pDecodedItem,
QCBORTagListOut *pTags)
{
+#ifndef QCBOR_DISABLE_TAGS
+
QCBORError uReturn;
uReturn = QCBORDecode_GetNext(pMe, pDecodedItem);
@@ -2486,6 +2552,13 @@
}
return QCBOR_SUCCESS;
+
+#else /* QCBOR_DISABLE_TAGS */
+ (void)pMe;
+ (void)pDecodedItem;
+ (void)pTags;
+ return QCBOR_ERR_TAGS_DISABLED;
+#endif /* QCBOR_DISABLE_TAGS */
}
@@ -2496,6 +2569,7 @@
const QCBORItem *pItem,
uint64_t uTag)
{
+#ifndef QCBOR_DISABLE_TAGS
for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) {
if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) {
break;
@@ -2504,6 +2578,11 @@
return true;
}
}
+#else /* QCBOR_TAGS_DISABLED */
+ (void)pMe;
+ (void)pItem;
+ (void)uTag;
+#endif /* QCBOR_TAGS_DISABLED */
return false;
}
@@ -2564,6 +2643,7 @@
const QCBORItem *pItem,
uint32_t uIndex)
{
+#ifndef QCBOR_DISABLE_TAGS
if(pItem->uDataType == QCBOR_TYPE_NONE) {
return CBOR_TAG_INVALID64;
}
@@ -2572,6 +2652,13 @@
} else {
return UnMapTagNumber(pMe, pItem->uTags[uIndex]);
}
+#else /* QCBOR_DISABLE_TAGS */
+ (void)pMe;
+ (void)pItem;
+ (void)uIndex;
+
+ return CBOR_TAG_INVALID64;
+#endif /* QCBOR_DISABLE_TAGS */
}
@@ -2581,6 +2668,8 @@
uint64_t QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe,
uint32_t uIndex)
{
+#ifndef QCBOR_DISABLE_TAGS
+
if(pMe->uLastError != QCBOR_SUCCESS) {
return CBOR_TAG_INVALID64;
}
@@ -2589,6 +2678,12 @@
} else {
return UnMapTagNumber(pMe, pMe->uLastTags[uIndex]);
}
+#else /* QCBOR_DISABLE_TAGS */
+ (void)pMe;
+ (void)uIndex;
+
+ return CBOR_TAG_INVALID64;
+#endif /* QCBOR_DISABLE_TAGS */
}
@@ -2784,9 +2879,15 @@
-static inline void CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem)
+static inline void
+CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem)
{
+#ifndef QCBOR_DISABLE_TAGS
memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags));
+#else
+ (void)pMe;
+ (void)pItem;
+#endif
}
@@ -3224,7 +3325,6 @@
}
-
static QCBORError
CheckTypeList(int uDataType, const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES])
{
@@ -3238,30 +3338,47 @@
/**
- @param[in] TagSpec Specification for matching tags.
- @param[in] pItem The item to check.
-
- @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec
- @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec
-
- The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered
- tag value.
+ * Match a tag/type specification against the type of the item.
+ *
+ * @param[in] TagSpec Specification for matching tags.
+ * @param[in] pItem The item to check.
+ *
+ * @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec
+ * @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec
+ *
+ * This checks the item data type of untagged items as well as of
+ * tagged items against a specification to see if decoding should
+ * proceed.
+ *
+ * This relies on the automatic tag decoding done by QCBOR that turns
+ * tag numbers into particular QCBOR_TYPEs so there is no actual
+ * comparsion of tag numbers, just of QCBOR_TYPEs.
+ *
+ * This checks the data item type as possibly representing the tag
+ * number or as the tag content type.
+ *
+ * If QCBOR_DISABLE_TAGS is #defined, this primarily checks the item
+ * data type against the allowed tag content types. It will also error out
+ * if the caller tries to require a tag because there is no way that can
+ * ever be fulfilled.
*/
static QCBORError
CheckTagRequirement(const TagSpecification TagSpec, const QCBORItem *pItem)
{
+ const int nItemType = pItem->uDataType;
+ const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS;
+
+#ifndef QCBOR_DISABLE_TAGS
if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) &&
pItem->uTags[0] != CBOR_TAG_INVALID16) {
/* There are tags that QCBOR couldn't process on this item and
- the caller has told us there should not be. */
+ * the caller has told us there should not be.
+ */
return QCBOR_ERR_UNEXPECTED_TYPE;
}
- const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS;
- const int nItemType = pItem->uDataType;
-
if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) {
- // Must match the tag and only the tag
+ /* Must match the tag number and only the tag */
return CheckTypeList(nItemType, TagSpec.uTaggedTypes);
}
@@ -3272,26 +3389,35 @@
if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) {
/* Must match the content type and only the content type.
- There was no match just above so it is a fail. */
+ * There was no match just above so it is a fail. */
return QCBOR_ERR_UNEXPECTED_TYPE;
}
- /* If here it can match either the tag or the content
- and it hasn't matched the content, so the end
- result is whether it matches the tag. This is
- also the case that the CBOR standard discourages. */
+ /* QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG: If here it can match either the tag or the content
+ * and it hasn't matched the content, so the end
+ * result is whether it matches the tag. This is
+ * the tag optional case that the CBOR standard discourages.
+ */
return CheckTypeList(nItemType, TagSpec.uTaggedTypes);
-}
+#else /* QCBOR_DISABLE_TAGS */
+ if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) {
+ return QCBOR_ERR_UNEXPECTED_TYPE;
+ }
+
+ return CheckTypeList(nItemType, TagSpec.uAllowedContentTypes);
+
+#endif /* QCBOR_DISABLE_TAGS */
+}
// This could be semi-private if need be
static inline
-void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe,
- int64_t nLabel,
- TagSpecification TagSpec,
- QCBORItem *pItem)
+void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe,
+ const int64_t nLabel,
+ const TagSpecification TagSpec,
+ QCBORItem *pItem)
{
QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
if(pMe->uLastError != QCBOR_SUCCESS) {
@@ -3304,10 +3430,10 @@
// This could be semi-private if need be
static inline
-void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe,
- const char *szLabel,
- TagSpecification TagSpec,
- QCBORItem *pItem)
+void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe,
+ const char *szLabel,
+ const TagSpecification TagSpec,
+ QCBORItem *pItem)
{
QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
if(pMe->uLastError != QCBOR_SUCCESS) {
@@ -3321,7 +3447,7 @@
void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe,
int64_t nLabel,
TagSpecification TagSpec,
- UsefulBufC *pString)
+ UsefulBufC *pString)
{
QCBORItem Item;
QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item);
@@ -3631,10 +3757,11 @@
-static QCBORError InternalEnterBstrWrapped(QCBORDecodeContext *pMe,
- const QCBORItem *pItem,
- uint8_t uTagRequirement,
- UsefulBufC *pBstr)
+static QCBORError
+InternalEnterBstrWrapped(QCBORDecodeContext *pMe,
+ const QCBORItem *pItem,
+ const uint8_t uTagRequirement,
+ UsefulBufC *pBstr)
{
if(pBstr) {
*pBstr = NULLUsefulBufC;
@@ -3875,7 +4002,7 @@
static void ProcessEpochDate(QCBORDecodeContext *pMe,
QCBORItem *pItem,
- uint8_t uTagRequirement,
+ const uint8_t uTagRequirement,
int64_t *pnTime)
{
if(pMe->uLastError != QCBOR_SUCCESS) {
@@ -4054,13 +4181,16 @@
-
-void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe,
- TagSpecification TagSpec,
- UsefulBufC *pBstr)
+/*
+ * @brief Get a string that matches the type/tag specification.
+ */
+void
+QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe,
+ const TagSpecification TagSpec,
+ UsefulBufC *pBstr)
{
if(pMe->uLastError != QCBOR_SUCCESS) {
- // Already in error state, do nothing
+ /* Already in error state, do nothing */
return;
}
@@ -4085,10 +4215,11 @@
-static QCBORError ProcessBigNum(uint8_t uTagRequirement,
- const QCBORItem *pItem,
- UsefulBufC *pValue,
- bool *pbIsNegative)
+static QCBORError
+ProcessBigNum(const uint8_t uTagRequirement,
+ const QCBORItem *pItem,
+ UsefulBufC *pValue,
+ bool *pbIsNegative)
{
const TagSpecification TagSpec =
{
@@ -4179,10 +4310,11 @@
// Semi private
-QCBORError QCBORDecode_GetMIMEInternal(uint8_t uTagRequirement,
- const QCBORItem *pItem,
- UsefulBufC *pMessage,
- bool *pbIsTag257)
+QCBORError
+QCBORDecode_GetMIMEInternal(const uint8_t uTagRequirement,
+ const QCBORItem *pItem,
+ UsefulBufC *pMessage,
+ bool *pbIsTag257)
{
const TagSpecification TagSpecText =
{
@@ -5469,38 +5601,105 @@
}
-static QCBORError MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe,
- TagSpecification TagSpec,
- QCBORItem *pItem)
+/**
+ * @brief Check and/or complete mantissa and exponent item.
+ *
+ * @param[in] pMe The decoder context
+ * @param[in] TagSpec Expected type(s)
+ * @param[in,out] pItem See below
+ *
+ * This is for decimal fractions and big floats, both of which are a
+ * mantissa and exponent.
+ *
+ * The input item is either a fully decoded decimal faction or big
+ * float, or a just the decoded first item of a decimal fraction or
+ * big float.
+ *
+ * On output, the item is always a fully decoded decimal fraction or
+ * big float.
+ *
+ * This errors out if the input type does not meet the TagSpec.
+ */
+// TODO: document and see tests for the bug that was fixed by this rewrite
+static QCBORError
+MantissaAndExponentTypeHandler(QCBORDecodeContext *pMe,
+ const TagSpecification TagSpec,
+ QCBORItem *pItem)
{
QCBORError uErr;
- // Loops runs at most 1.5 times. Making it a loop saves object code.
- while(1) {
- uErr = CheckTagRequirement(TagSpec, pItem);
- if(uErr != QCBOR_SUCCESS) {
- goto Done;
- }
- if(pItem->uDataType != QCBOR_TYPE_ARRAY) {
- break; // Successful exit. Moving on to finish decoding.
- }
+ /* pItem could either be an auto-decoded mantissa and exponent or
+ * the opening array of an undecoded mantissa and exponent. This
+ * check will succeed on either, but doesn't say which it was.
+ */
+ uErr = CheckTagRequirement(TagSpec, pItem);
+ if(uErr != QCBOR_SUCCESS) {
+ goto Done;
+ }
- // The item is an array, which means an undecoded
- // mantissa and exponent, so decode it. It will then
- // have a different type and exit the loop if.
+ if(pItem->uDataType == QCBOR_TYPE_ARRAY) {
+ /* The item is an array, which means is is an undecoded mantissa
+ * and exponent. This call consumes the items in the array and
+ * results in a decoded mantissa and exponent in pItem. This is
+ * the case where there was no tag.
+ */
uErr = QCBORDecode_MantissaAndExponent(pMe, pItem);
if(uErr != QCBOR_SUCCESS) {
goto Done;
}
- // Second time around, the type must match.
- TagSpec.uTagRequirement = QCBOR_TAG_REQUIREMENT_TAG;
+ /* The above decode didn't determine whether it is a decimal
+ * fraction or big num. Which of these two depends on what the
+ * caller wants it decoded as since there is no tag, so fish the
+ * type out of the TagSpec. */
+ pItem->uDataType = MantissaExponentDataType(TagSpec.uTaggedTypes[0], pItem);
+
+ /* No need to check the type again. All that we need to know was
+ * that it decoded correctly as a mantissa and exponent. The
+ * QCBOR type is set out by what was requested.
+ */
}
+
+ /* If the item was not an array and the check passed, then
+ * it is a fully decoded big float or decimal fraction and
+ * matches what is requested.
+ */
+
Done:
return uErr;
}
+/* Some notes from the work to disable tags.
+ *
+ * The API for big floats and decimal fractions seems good.
+ * If there's any issue with it it's that the code size to
+ * implement is a bit large because of the conversion
+ * to/from int and bignum that is required. There is no API
+ * that doesn't do the conversion so dead stripping will never
+ * leave that code out.
+ *
+ * The implementation itself seems correct, but not as clean
+ * and neat as it could be. It could probably be smaller too.
+ *
+ * The implementation has three main parts / functions
+ * - The decoding of the array of two
+ * - All the tag and type checking for the various API functions
+ * - Conversion to/from bignum and int
+ *
+ * The type checking seems like it wastes the most code for
+ * what it needs to do.
+ *
+ * The inlining for the conversion is probably making the
+ * overall code base larger.
+ *
+ * The tests cases could be organized a lot better and be
+ * more thorough.
+ *
+ * Seems also like there could be more common code in the
+ * first tier part of the public API. Some functions only
+ * vary by a TagSpec.
+ */
static void ProcessMantissaAndExponent(QCBORDecodeContext *pMe,
TagSpecification TagSpec,
QCBORItem *pItem,
@@ -5518,10 +5717,12 @@
case QCBOR_TYPE_DECIMAL_FRACTION:
case QCBOR_TYPE_BIGFLOAT:
- *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt;
*pnExponent = pItem->val.expAndMantissa.nExponent;
+ *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt;
break;
+#ifndef QCBOR_DISABLE_TAGS
+ /* If tags are disabled, mantissas can never be big nums */
case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
*pnExponent = pItem->val.expAndMantissa.nExponent;
@@ -5533,6 +5734,7 @@
*pnExponent = pItem->val.expAndMantissa.nExponent;
uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa);
break;
+#endif /* QCBOR_DISABLE_TAGS */
default:
uErr = QCBOR_ERR_UNEXPECTED_TYPE;
@@ -5575,6 +5777,8 @@
*pnExponent = pItem->val.expAndMantissa.nExponent;
break;
+#ifndef QCBOR_DISABLE_TAGS
+ /* If tags are disabled, mantissas can never be big nums */
case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
*pnExponent = pItem->val.expAndMantissa.nExponent;
@@ -5588,6 +5792,7 @@
*pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum;
*pbIsNegative = true;
break;
+#endif /* QCBOR_DISABLE_TAGS */
default:
uErr = QCBOR_ERR_UNEXPECTED_TYPE;
@@ -5690,7 +5895,7 @@
*/
void QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe,
uint8_t uTagRequirement,
- UsefulBuf MantissaBuffer,
+ UsefulBuf MantissaBuffer,
UsefulBufC *pMantissa,
bool *pbMantissaIsNegative,
int64_t *pnExponent)
diff --git a/test/not_well_formed_cbor.h b/test/not_well_formed_cbor.h
index 6734e93..7cbd3c3 100644
--- a/test/not_well_formed_cbor.h
+++ b/test/not_well_formed_cbor.h
@@ -50,8 +50,10 @@
{(uint8_t[]){0x5f, 0x80, 0xff}, 3},
// indefinite length byte string with an map chunk
{(uint8_t[]){0x5f, 0xa0, 0xff}, 3},
+#ifndef QCBOR_DISABLE_TAGS
// indefinite length byte string with tagged integer chunk
{(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4},
+#endif /* QCBOR_DISABLE_TAGS */
// indefinite length byte string with an simple type chunk
{(uint8_t[]){0x5f, 0xe0, 0xff}, 3},
// indefinite length byte string with indefinite string inside
@@ -246,11 +248,12 @@
{(uint8_t[]){0x1f}, 1},
// Negative integer with "argument" an indefinite length
{(uint8_t[]){0x3f}, 1},
+#ifndef QCBOR_DISABLE_TAGS
// CBOR tag with "argument" an indefinite length
{(uint8_t[]){0xdf, 0x00}, 2},
// CBOR tag with "argument" an indefinite length alternate vector
{(uint8_t[]){0xdf}, 1},
-
+#endif /* QCBOR_DISABLE_TAGS */
// Missing content bytes from a definite length string
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 431836c..742a01b 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -1758,6 +1758,10 @@
const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
+ if(nIterate == 86) {
+ nIterate = 86;
+ }
+
// Set up decoder context. String allocator needed for indefinite
// string test cases
QCBORDecodeContext DCtx;
@@ -1814,6 +1818,11 @@
}
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
+ const size_t nIndexx = (size_t)(pF - pFailInputs);
+ if(nIndexx == 8) {
+ uCBORError = 9;
+ }
+
// Iterate until there is an error of some sort error
QCBORItem Item;
@@ -1867,8 +1876,13 @@
{ {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
// indefinite length byte string with an map chunk
{ {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
+#ifndef QCBOR_DISABLE_TAGS
// indefinite length byte string with tagged integer chunk
{ {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
+#else
+ // indefinite length byte string with tagged integer chunk
+ { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_TAGS_DISABLED },
+#endif /* QCBOR_DISABLE_TAGS */
// indefinite length byte string with an simple type chunk
{ {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
{ {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
@@ -1999,10 +2013,12 @@
// double-precision with 3 byte argument
{ {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
-
+#ifndef QCBOR_DISABLE_TAGS
// Tag with no content
{ {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
-
+#else /* QCBOR_DISABLE_TAGS */
+ { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_TAGS_DISABLED },
+#endif /* QCBOR_DISABLE_TAGS */
// Breaks must not occur in definite length arrays and maps
// Array of length 1 with sole member replaced by a break
@@ -2094,11 +2110,16 @@
{ {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
// Negative integer with additional info indefinite length
{ {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
+
+#ifndef QCBOR_DISABLE_TAGS
// CBOR tag with "argument" an indefinite length
{ {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
// CBOR tag with "argument" an indefinite length alternate vector
{ {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
-
+#else /* QCBOR_DISABLE_TAGS */
+ { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
+ { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_TAGS_DISABLED },
+#endif /* QCBOR_DISABLE_TAGS */
// Missing bytes from a deterministic length string
// A byte string is of length 1 without the 1 byte
@@ -2117,7 +2138,7 @@
// Text string should have 2^64 bytes, but has 3
{ {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
-#else
+#else
// Byte string should have 2^32-15 bytes, but has one
{ {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
// Byte string should have 2^32-15 bytes, but has one
@@ -2191,7 +2212,7 @@
{ {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
-
+#ifndef QCBOR_DISABLE_TAGS
// In addition to not-well-formed, some invalid CBOR
// Text-based date, with an integer
{ {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
@@ -2201,6 +2222,17 @@
{ {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
// big num tagged an int, not a byte string
{ {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
+#else /* QCBOR_DISABLE_TAGS */
+ // Text-based date, with an integer
+ { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
+ // Epoch date, with an byte string
+ { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_TAGS_DISABLED },
+ // tagged as both epoch and string dates
+ { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_TAGS_DISABLED },
+ // big num tagged an int, not a byte string
+ { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
+#endif /* QCBOR_DISABLE_TAGS */
+
};
int32_t DecodeFailureTests()
@@ -2401,7 +2433,7 @@
#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
-
+/* Test date decoding using GetNext() */
int32_t DateParseTest()
{
QCBORDecodeContext DCtx;
@@ -2532,6 +2564,7 @@
return 0;
}
+
/*
Test cases covered here. Some items cover more than one of these.
positive integer (zero counts as a positive integer)
@@ -2561,7 +2594,73 @@
Untagged values
*/
static const uint8_t spSpiffyDateTestInput[] = {
- 0x86, // array of 6 items
+ 0x87, // array of 7 items
+
+ 0xa6, // Open a map for tests involving untagged items with labels.
+
+ // Untagged integer 0
+ 0x08,
+ 0x00,
+
+ // Utagged date string with string label y
+ 0x61, 0x79,
+ 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
+
+ // Untagged single-precision float with value 3.14 with string label x
+ 0x61, 0x78,
+ 0xFA, 0x40, 0x48, 0xF5, 0xC3,
+
+ // Untagged half-precision float with value -2
+ 0x09,
+ 0xF9, 0xC0, 0x00,
+
+ /* Untagged date-only date string */
+ 0x18, 0x63,
+ 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
+
+ /* Untagged days-count epoch date */
+ 0x11,
+ 0x19, 0x0F, 0x9A, /* 3994 */
+
+ // End of map, back to array
+
+ 0xa7, // Open map of tagged items with labels
+
+ 0x00,
+ 0xc0, // tag for string date
+ 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
+
+
+ 0x01,
+ 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
+ 0xc1, // tag for epoch date
+ 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
+
+ 0x05,
+ 0xc1,
+ 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
+
+
+ 0x07,
+ 0xc1, // tag for epoch date
+ 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
+
+ /* Tagged days-count epoch date */
+ 0x63, 0x53, 0x44, 0x45,
+ 0xD8, 0x64, /* tag(100) */
+ 0x39, 0x29, 0xB3, /* -10676 */
+
+ // Untagged -1000 with label z
+ 0x61, 0x7a,
+ 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
+ 0x39, 0x03, 0xe7,
+
+ /* Tagged date-only date string */
+ 0x63, 0x53, 0x44, 0x53,
+ 0xD9, 0x03, 0xEC,
+ 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
+
+ // End of map of tagged items
0xc1,
0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
@@ -2572,144 +2671,34 @@
0xc1, // tag for epoch date
0xf9, 0xfc, 0x00, // Half-precision -Infinity
- 0xad, // Open a map for tests involving labels.
-
- 0x00,
- 0xc0, // tag for string date
- 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
-
- 0x01,
- 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
- 0xc1, // tag for epoch date
- 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
-
- // Untagged integer 0
- 0x08,
- 0x00,
-
- // Utagged date string with string label y
- 0x61, 0x79,
- 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
-
- // Untagged -1000 with label z
- 0x61, 0x7a,
- 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
- 0x39, 0x03, 0xe7,
-
- 0x07,
- 0xc1, // tag for epoch date
- 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
-
- 0x05,
- 0xc1,
- 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
-
- // Untagged single-precision float with value 3.14 with string label x
- 0x61, 0x78,
- 0xFA, 0x40, 0x48, 0xF5, 0xC3,
-
- // Untagged half-precision float with value -2
- 0x09,
- 0xF9, 0xC0, 0x00,
-
- /* Tagged date-only date string */
- 0x63, 0x53, 0x44, 0x53,
- 0xD9, 0x03, 0xEC,
- 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
-
- /* Untagged date-only date string */
- 0x18, 0x63,
- 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
-
- /* Tagged days-count epoch date */
- 0x63, 0x53, 0x44, 0x45,
- 0xD8, 0x64, /* tag(100) */
- 0x39, 0x29, 0xB3, /* -10676 */
-
- /* Untagged days-count epoch date */
- 0x11,
- 0x19, 0x0F, 0x9A, /* 3994 */
-
- // End of map, back to array
-
// These two at the end because they are unrecoverable errors
0xc1, // tag for epoch date
0x80, // Erroneous empty array as content for date
0xc0, // tag for string date
0xa0 // Erroneous empty map as content for date
-
};
int32_t SpiffyDateDecodeTest()
{
QCBORDecodeContext DC;
QCBORError uError;
- int64_t nEpochDate2, nEpochDate3, nEpochDate5,
- nEpochDate4, nEpochDate6, nEpochDateFail,
- nEpochDate1400000000, nEpochDays1, nEpochDays2;
- UsefulBufC StringDate1, StringDate2, StringDays1, StringDays2;
- uint64_t uTag1, uTag2;
+ int64_t nEpochDate3, nEpochDate5,
+ nEpochDate4, nEpochDate6,
+ nEpochDays2;
+ UsefulBufC StringDate1, StringDate2, StringDays2;
QCBORDecode_Init(&DC,
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
QCBOR_DECODE_MODE_NORMAL);
+
+ /* Items are in an array or map to test look up by label and other
+ * that might not occur in isolated items. But it does make the
+ * test a bit messy. */
QCBORDecode_EnterArray(&DC, NULL);
- // Too-negative float, -9.2233720368547748E+18
- QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
- uError = QCBORDecode_GetAndResetError(&DC);
- if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
- return 1111;
- }
-
- // Too-large integer
- QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
- uError = QCBORDecode_GetAndResetError(&DC);
- if(uError != QCBOR_ERR_DATE_OVERFLOW) {
- return 1;
- }
-
- // Half-precision minus infinity
- QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
- uError = QCBORDecode_GetAndResetError(&DC);
- if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
- return 2;
- }
-
-
-
QCBORDecode_EnterMap(&DC, NULL);
- // Get largest negative double precision epoch date allowed
- QCBORDecode_GetEpochDateInMapN(&DC,
- 5,
- QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
- QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
- &nEpochDate2);
- uError = QCBORDecode_GetAndResetError(&DC);
- if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
- return 102;
- }
- if(uError == QCBOR_SUCCESS) {
- if(nEpochDate2 != -9223372036854773760LL) {
- return 101;
- }
- }
-
- // Get largest double precision epoch date allowed
- QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
- &nEpochDate2);
- uError = QCBORDecode_GetAndResetError(&DC);
- if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
- return 112;
- }
- if(uError == QCBOR_SUCCESS) {
- if(nEpochDate2 != 9223372036854773760ULL) {
- return 111;
- }
- }
-
// A single-precision date
QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
&nEpochDate5);
@@ -2772,6 +2761,39 @@
// The rest of these succeed even if float features are disabled
+
+ // Untagged integer 0
+ QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
+ &nEpochDate3);
+ // Untagged date string
+ QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
+ &StringDate2);
+
+ QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
+ &StringDays2);
+
+ QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
+ &nEpochDays2);
+
+ QCBORDecode_ExitMap(&DC);
+ if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
+ return 3001;
+ }
+
+ // The map of tagged items
+ QCBORDecode_EnterMap(&DC, NULL);
+
+#ifndef QCBOR_DISABLE_TAGS
+ int64_t nEpochDate2,
+ nEpochDateFail,
+ nEpochDate1400000000, nEpochDays1;
+ UsefulBufC StringDays1;
+ uint64_t uTag1, uTag2;
+
+ // Tagged date string
+ QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ &StringDate1);
+
// Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
QCBORDecode_GetEpochDateInMapN(&DC,
1,
@@ -2779,15 +2801,23 @@
QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
&nEpochDate1400000000);
uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
- // Tagged date string
- QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
- &StringDate1);
- // Untagged integer 0
- QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
- &nEpochDate3);
- // Untagged date string
- QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
- &StringDate2);
+
+ // Get largest negative double precision epoch date allowed
+ QCBORDecode_GetEpochDateInMapN(&DC,
+ 5,
+ QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
+ QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
+ &nEpochDate2);
+ uError = QCBORDecode_GetAndResetError(&DC);
+ if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
+ return 102;
+ }
+ if(uError == QCBOR_SUCCESS) {
+ if(nEpochDate2 != -9223372036854773760LL) {
+ return 101;
+ }
+ }
+
// Untagged -1000 with label z
QCBORDecode_GetEpochDateInMapSZ(&DC,
"z",
@@ -2796,6 +2826,20 @@
&nEpochDate6);
uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
+
+ // Get largest double precision epoch date allowed
+ QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ &nEpochDate2);
+ uError = QCBORDecode_GetAndResetError(&DC);
+ if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
+ return 112;
+ }
+ if(uError == QCBOR_SUCCESS) {
+ if(nEpochDate2 != 9223372036854773760ULL) {
+ return 111;
+ }
+ }
+
/* The days format is much simpler than the date format
* because it can't be a floating point value. The test
* of the spiffy decode functions sufficiently covers
@@ -2807,20 +2851,36 @@
QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
&StringDays1);
- QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
- &StringDays2);
-
QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
&nEpochDays1);
- QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
- &nEpochDays2);
-
QCBORDecode_ExitMap(&DC);
if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
return 3001;
}
+ // Too-negative float, -9.2233720368547748E+18
+ QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
+ uError = QCBORDecode_GetAndResetError(&DC);
+ if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
+ return 1111;
+ }
+
+ // Too-large integer
+ QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
+ uError = QCBORDecode_GetAndResetError(&DC);
+ if(uError != QCBOR_ERR_DATE_OVERFLOW) {
+ return 1;
+ }
+
+ // Half-precision minus infinity
+ QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
+ uError = QCBORDecode_GetAndResetError(&DC);
+ if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
+ return 2;
+ }
+
+
// Bad content for epoch date
QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
uError = QCBORDecode_GetAndResetError(&DC);
@@ -2840,6 +2900,17 @@
if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
return 1000 + (int32_t)uError;
}
+#else /* QCBOR_DISABLE_TAGS */
+ QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ &StringDate1);
+ uError = QCBORDecode_GetAndResetError(&DC);
+ if(uError != QCBOR_ERR_TAGS_DISABLED) {
+ return 4;
+ }
+#endif /* QCBOR_DISABLE_TAGS */
+
+
+#ifndef QCBOR_DISABLE_TAGS
if(nEpochDate1400000000 != 1400000000) {
return 200;
@@ -2849,36 +2920,38 @@
return 201;
}
- if(nEpochDate3 != 0) {
- return 202;
+ if(nEpochDays1 != -10676) {
+ return 205;
}
- if(nEpochDate6 != -1000) {
- return 203;
+ if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
+ return 207;
}
if(uTag2 != 0x01010101) {
return 204;
}
- if(nEpochDays1 != -10676) {
- return 205;
- }
-
- if(nEpochDays2 != 3994) {
- return 206;
+ if(nEpochDate6 != -1000) {
+ return 203;
}
if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
return 205;
}
- if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
+#endif /* QCBOR_DISABLE_TAGS */
+
+ if(nEpochDate3 != 0) {
+ return 202;
+ }
+
+ if(nEpochDays2 != 3994) {
return 206;
}
- if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
- return 207;
+ if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
+ return 206;
}
if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
@@ -2889,7 +2962,6 @@
}
-
// Input for one of the tagging tests
static const uint8_t spTagInput[] = {
0xd9, 0xd9, 0xf7, // CBOR magic number
@@ -3530,8 +3602,19 @@
return 0;
}
-
-
+/*
+ * These are showing the big numbers converted to integers.
+ * The tag numbers are not shown.
+ *
+ * [ 18446744073709551616,
+ * -18446744073709551617,
+ * {"BN+": 18446744073709551616,
+ * 64: 18446744073709551616,
+ * "BN-": -18446744073709551617,
+ * -64: -18446744073709551617
+ * }
+ * ]
+ */
static const uint8_t spBigNumInput[] = {
0x83,
@@ -3547,10 +3630,12 @@
0x38, 0x3F,
0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+#ifndef QCBOR_DISABLE_TAGS
/* The expected big num */
static const uint8_t spBigNum[] = {
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00};
+#endif /* QCBOR_DISABLE_TAGS */
int32_t BignumParseTest()
@@ -3571,6 +3656,7 @@
return -2;
}
+#ifndef QCBOR_DISABLE_TAGS
//
if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
return -3;
@@ -3627,6 +3713,12 @@
UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
return -16;
}
+#else
+
+ if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
+ return -100;
+ }
+#endif /* QCBOR_DISABLE_TAGS */
return 0;
}
@@ -4015,6 +4107,8 @@
QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
nResult = QCBORDecode_GetNext(&DC, &Item);
+
+#ifndef QCBOR_DISABLE_TAGS
if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
return -18;
}
@@ -4023,6 +4117,11 @@
if(nResult != QCBOR_ERR_BAD_BREAK) {
return -19;
}
+#else /* QCBOR_DISABLE_TAGS */
+ if(nResult != QCBOR_ERR_TAGS_DISABLED) {
+ return -20;
+ }
+#endif /* QCBOR_DISABLE_TAGS */
return 0;
}
@@ -4549,41 +4648,525 @@
#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
-/* exponent, mantissa
- [
- 4([-1, 3]),
- 4([-20, 4759477275222530853136]),
- 4([9223372036854775807, -4759477275222530853137]),
- 5([300, 100]),
- 5([-20, 4759477275222530853136]),
- 5([-9223372036854775807, -4759477275222530853137])
- 5([ 9223372036854775806, -4759477275222530853137])
- 5([ 9223372036854775806, 9223372036854775806])]
- ]
- */
-static const uint8_t spExpectedExponentsAndMantissas[] = {
- 0x88,
- 0xC4, 0x82, 0x20,
- 0x03,
- 0xC4, 0x82, 0x33,
- 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
- 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
- 0xC5, 0x82, 0x19, 0x01, 0x2C,
- 0x18, 0x64,
- 0xC5, 0x82, 0x33,
- 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
- 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
- 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
- 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
- 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
- 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
- 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
+struct EaMTest {
+ const char *szName;
+ UsefulBufC Input;
+ uint8_t uTagRequirement;
+ bool bHasTags;
+
+ /* Expected values for GetNext */
+ QCBORError uExpectedErrorGN;
+ uint8_t uQCBORTypeGN;
+ int64_t nExponentGN;
+ int64_t nMantissaGN;
+ UsefulBufC MantissaGN;
+
+ /* Expected values for GetDecimalFraction */
+ QCBORError uExpectedErrorGDF;
+ int64_t nExponentGDF;
+ int64_t nMantissaGDF;
+
+ /* Expected values for GetDecimalFractionBig */
+ QCBORError uExpectedErrorGDFB;
+ int64_t nExponentGDFB;
+ UsefulBufC MantissaGDFB;
+ bool IsNegativeGDFB;
+
+ /* Expected values for GetBigFloat */
+ QCBORError uExpectedErrorGBF;
+ int64_t nExponentGBF;
+ int64_t nMantissaGBF;
+
+ /* Expected values for GetBigFloatBig */
+ QCBORError uExpectedErrorGBFB;
+ int64_t nExponentGBFB;
+ UsefulBufC MantissaGBFB;
+ bool IsNegativeGBFB;
};
-int32_t ExponentAndMantissaDecodeTests(void)
+
+static const struct EaMTest pEaMTests[] = {
+ {
+ "1. Untagged pair (big float or decimal fraction), no tag required",
+ {(const uint8_t []){0x82, 0x20, 0x03}, 3},
+ QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
+ false,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_ARRAY,
+ 0,
+ 0,
+ {(const uint8_t []){0x00}, 1},
+
+ QCBOR_SUCCESS, /* GetDecimalFraction */
+ -1,
+ 3,
+
+ QCBOR_SUCCESS, /* for GetDecimalFractionBig */
+ -1,
+ {(const uint8_t []){0x03}, 1},
+ false,
+
+ QCBOR_SUCCESS, /* for GetBigFloat */
+ -1,
+ 3,
+
+ QCBOR_SUCCESS, /* for GetBigFloatBig */
+ -1,
+ {(const uint8_t []){0x03}, 1},
+ false
+ },
+
+ {
+ "2. Untagged pair (big float or decimal fraction), tag required",
+ {(const uint8_t []){0x82, 0x20, 0x03}, 3},
+ QCBOR_TAG_REQUIREMENT_TAG,
+ false,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_ARRAY,
+ 0,
+ 0,
+ {(const uint8_t []){0x00}, 1},
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
+ 0,
+ {(const uint8_t []){0x00}, 1},
+ false,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
+ 0,
+ {(const uint8_t []){0x00}, 1},
+ false
+
+ },
+
+ {
+ "3. Tagged 1.5 decimal fraction, tag 4 optional",
+ {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
+ QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_DECIMAL_FRACTION,
+ -1,
+ 3,
+ {(const uint8_t []){0x00}, 1},
+
+
+ QCBOR_SUCCESS, /* for GetDecimalFraction */
+ -1,
+ 3,
+
+ QCBOR_SUCCESS, /* for GetDecimalFractionBig */
+ -1,
+ {(const uint8_t []){0x03}, 1},
+ false,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
+ 0,
+ {(const uint8_t []){0x00}, 1},
+ false
+ },
+ {
+ "4. Tagged 100 * 2^300 big float, tag 5 optional",
+ {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
+ QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_BIGFLOAT,
+ 300,
+ 100,
+ {(const uint8_t []){0x00}, 1},
+
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
+ 0,
+ {(const uint8_t []){0x03}, 1},
+ false,
+
+ QCBOR_SUCCESS, /* for GetBigFloat */
+ 300,
+ 100,
+
+ QCBOR_SUCCESS, /* for GetBigFloatBig */
+ 300,
+ {(const uint8_t []){0x64}, 1},
+ false
+ },
+
+ {
+ "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
+ {(const uint8_t []){0xC4, 0x82, 0x33,
+ 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
+ QCBOR_TAG_REQUIREMENT_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
+ -20,
+ 0,
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
+
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_SUCCESS, /* for GetDecimalFractionBig */
+ -20,
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
+ false,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
+ 0,
+ {(const uint8_t []){0x00}, 0},
+ false
+ },
+
+ {
+ "6. Error: Mantissa and exponent inside a Mantissa and exponent",
+ {(const uint8_t []){0xC4, 0x82, 0x33,
+ 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
+ QCBOR_TAG_REQUIREMENT_TAG,
+ true,
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
+ QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
+ 0,
+ 0,
+ {(const uint8_t []){0x00}, 0},
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
+ 0,
+ {(const uint8_t []){0x00}, 0},
+ false,
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
+ 0,
+ {(const uint8_t []){0x00}, 0},
+ false
+ },
+ {
+ "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
+ {(const uint8_t []){0xC5, 0x82, 0x33,
+ 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
+ QCBOR_TAG_REQUIREMENT_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
+ -20,
+ 0,
+ {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
+ -20,
+ {(const uint8_t []){0x00}, 1},
+ false,
+
+ QCBOR_SUCCESS, /* for GetBigFloat */
+ -20,
+ 4294967295,
+
+ QCBOR_SUCCESS, /* for GetBigFloatBig */
+ -20,
+ {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
+ false
+ },
+
+ {
+ /* Special case for test 8. Don't renumber it. */
+ "8. Untagged pair with big num (big float or decimal fraction), tag optional",
+ {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
+ QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_ARRAY,
+ 0,
+ 0,
+ {(const uint8_t []){0x00}, 1},
+
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_SUCCESS, /* for GetDecimalFractionBig */
+ -20,
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
+ false,
+
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_SUCCESS, /* for GetBigFloatBig */
+ -20,
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
+ false
+ },
+
+ {
+ "9. decimal fraction with large exponent and negative big num mantissa",
+ {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
+ QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
+ true,
+
+ QCBOR_SUCCESS, /* for GetNext */
+ QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
+ 9223372036854775807,
+ 0,
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
+
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
+ 0,
+ 0,
+
+ QCBOR_SUCCESS, /* for GetDecimalFractionBig */
+ 9223372036854775807,
+ {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
+ true,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
+ 0,
+ 0,
+
+ QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
+ 0,
+ {(const uint8_t []){0x00}, 1},
+ false
+ },
+};
+
+
+
+int32_t ProcessEaMTests(void)
{
+ size_t uIndex;
+ QCBORDecodeContext DCtx;
+ QCBORItem Item;
+ QCBORError uError;
+ int64_t nMantissa, nExponent;
+ MakeUsefulBufOnStack( MantissaBuf, 200);
+ UsefulBufC Mantissa;
+ bool bMantissaIsNegative;
+
+ for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
+ const struct EaMTest *pT = &pEaMTests[uIndex];
+ /* Decode with GetNext */
+ QCBORDecode_Init(&DCtx, pT->Input, 0);
+
+ if(uIndex + 1 == 9) {
+ nExponent = 99; // just to set a break point
+ }
+
+ uError = QCBORDecode_GetNext(&DCtx, &Item);
+#ifdef QCBOR_DISABLE_TAGS
+ /* Test 8 is a special case when tags are disabled */
+ if(pT->bHasTags && uIndex + 1 != 8) {
+ if(uError != QCBOR_ERR_TAGS_DISABLED) {
+ return (int32_t)(1+uIndex) * 1000 + 9;
+ }
+ } else {
+#endif
+ /* Now check return code, data type, mantissa and exponent */
+ if(pT->uExpectedErrorGN != uError) {
+ return (int32_t)(1+uIndex) * 1000 + 1;
+ }
+ if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
+ if(pT->uQCBORTypeGN != Item.uDataType) {
+ return (int32_t)(1+uIndex) * 1000 + 2;
+ }
+ if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
+ return (int32_t)(1+uIndex) * 1000 + 3;
+ }
+ if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
+ if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
+ return (int32_t)(1+uIndex) * 1000 + 4;
+ }
+ } else {
+ if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
+ return (int32_t)(1+uIndex) * 1000 + 5;
+ }
+ }
+ }
+#ifdef QCBOR_DISABLE_TAGS
+ }
+#endif
+
+ /* Decode with GetDecimalFraction */
+ QCBORDecode_Init(&DCtx, pT->Input, 0);
+ QCBORDecode_GetDecimalFraction(&DCtx,
+ pT->uTagRequirement,
+ &nMantissa,
+ &nExponent);
+ uError = QCBORDecode_GetAndResetError(&DCtx);
+#ifdef QCBOR_DISABLE_TAGS
+ if(pT->bHasTags) {
+ if(uError != QCBOR_ERR_TAGS_DISABLED) {
+ return (int32_t)(1+uIndex) * 1000 + 39;
+ }
+ } else {
+#endif
+ /* Now check return code, mantissa and exponent */
+ if(pT->uExpectedErrorGDF != uError) {
+ return (int32_t)(1+uIndex) * 1000 + 31;
+ }
+ if(uError == QCBOR_SUCCESS) {
+ if(pT->nExponentGDF != nExponent) {
+ return (int32_t)(1+uIndex) * 1000 + 32;
+ }
+ if(pT->nMantissaGDF != nMantissa) {
+ return (int32_t)(1+uIndex) * 1000 + 33;
+ }
+ }
+#ifdef QCBOR_DISABLE_TAGS
+ }
+#endif
+
+ /* Decode with GetDecimalFractionBig */
+ QCBORDecode_Init(&DCtx, pT->Input, 0);
+ QCBORDecode_GetDecimalFractionBig(&DCtx,
+ pT->uTagRequirement,
+ MantissaBuf,
+ &Mantissa,
+ &bMantissaIsNegative,
+ &nExponent);
+ uError = QCBORDecode_GetAndResetError(&DCtx);
+#ifdef QCBOR_DISABLE_TAGS
+ if(pT->bHasTags) {
+ if(uError != QCBOR_ERR_TAGS_DISABLED) {
+ return (int32_t)(1+uIndex) * 1000 + 49;
+ }
+ } else {
+#endif
+ /* Now check return code, mantissa (bytes and sign) and exponent */
+ if(pT->uExpectedErrorGDFB != uError) {
+ return (int32_t)(1+uIndex) * 1000 + 41;
+ }
+ if(uError == QCBOR_SUCCESS) {
+ if(pT->nExponentGDFB != nExponent) {
+ return (int32_t)(1+uIndex) * 1000 + 42;
+ }
+ if(pT->IsNegativeGDFB != bMantissaIsNegative) {
+ return (int32_t)(1+uIndex) * 1000 + 43;
+ }
+ if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
+ return (int32_t)(1+uIndex) * 1000 + 44;
+ }
+ }
+#ifdef QCBOR_DISABLE_TAGS
+ }
+#endif
+
+ /* Decode with GetBigFloat */
+ QCBORDecode_Init(&DCtx, pT->Input, 0);
+ QCBORDecode_GetBigFloat(&DCtx,
+ pT->uTagRequirement,
+ &nMantissa,
+ &nExponent);
+ uError = QCBORDecode_GetAndResetError(&DCtx);
+#ifdef QCBOR_DISABLE_TAGS
+ if(pT->bHasTags) {
+ if(uError != QCBOR_ERR_TAGS_DISABLED) {
+ return (int32_t)(1+uIndex) * 1000 + 19;
+ }
+ } else {
+#endif
+ /* Now check return code, mantissa and exponent */
+ if(pT->uExpectedErrorGBF != uError) {
+ return (int32_t)(1+uIndex) * 1000 + 11;
+ }
+ if(uError == QCBOR_SUCCESS) {
+ if(pT->nExponentGBF != nExponent) {
+ return (int32_t)(1+uIndex) * 1000 + 12;
+ }
+ if(pT->nMantissaGBF != nMantissa) {
+ return (int32_t)(1+uIndex) * 1000 + 13;
+ }
+ }
+#ifdef QCBOR_DISABLE_TAGS
+ }
+#endif
+
+ /* Decode with GetBigFloatBig */
+ QCBORDecode_Init(&DCtx, pT->Input, 0);
+ QCBORDecode_GetBigFloatBig(&DCtx,
+ pT->uTagRequirement,
+ MantissaBuf,
+ &Mantissa,
+ &bMantissaIsNegative,
+ &nExponent);
+ uError = QCBORDecode_GetAndResetError(&DCtx);
+#ifdef QCBOR_DISABLE_TAGS
+ if(pT->bHasTags) {
+ if(uError != QCBOR_ERR_TAGS_DISABLED) {
+ return (int32_t)(1+uIndex) * 1000 + 29;
+ }
+ } else {
+#endif
+ /* Now check return code, mantissa (bytes and sign) and exponent */
+ if(pT->uExpectedErrorGBFB != uError) {
+ return (int32_t)(1+uIndex) * 1000 + 21;
+ }
+ if(uError == QCBOR_SUCCESS) {
+ if(pT->nExponentGBFB != nExponent) {
+ return (int32_t)(1+uIndex) * 1000 + 22;
+ }
+ if(pT->IsNegativeGBFB != bMantissaIsNegative) {
+ return (int32_t)(1+uIndex) * 1000 + 23;
+ }
+ if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
+ return (int32_t)(1+uIndex) * 1000 + 24;
+ }
+ }
+#ifdef QCBOR_DISABLE_TAGS
+ }
+#endif
+ }
+
+ return 0;
+}
+
+
+int32_t ExponentAndMantissaDecodeTestsSecondary(void)
+{
+#ifndef QCBOR_DISABLE_TAGS
QCBORDecodeContext DC;
QCBORError uErr;
QCBORItem item;
@@ -4593,111 +5176,6 @@
UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
- QCBORDecode_Init(&DC,
- UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
- QCBOR_DECODE_MODE_NORMAL);
-
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 1;
- }
-
- if(item.uDataType != QCBOR_TYPE_ARRAY) {
- return 2;
- }
-
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 3;
- }
-
- if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
- item.val.expAndMantissa.Mantissa.nInt != 3 ||
- item.val.expAndMantissa.nExponent != -1) {
- return 4;
- }
-
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 5;
- }
-
- if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
- item.val.expAndMantissa.nExponent != -20 ||
- UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
- return 6;
- }
-
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 7;
- }
-
- if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
- item.val.expAndMantissa.nExponent != 9223372036854775807 ||
- UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
- return 8;
- }
-
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 9;
- }
-
- if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
- item.val.expAndMantissa.Mantissa.nInt != 100 ||
- item.val.expAndMantissa.nExponent != 300) {
- return 10;
- }
-
- // 5([-20, 4759477275222530853136]),
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 11;
- }
- if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
- item.val.expAndMantissa.nExponent != -20 ||
- UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
- return 12;
- }
-
- // 5([-9223372036854775807, -4759477275222530853137])
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 13;
- }
- if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
- item.val.expAndMantissa.nExponent != -9223372036854775807 ||
- UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
- return 14;
- }
-
- // 5([ 9223372036854775806, -4759477275222530853137])
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 15;
- }
- if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
- item.val.expAndMantissa.nExponent != 9223372036854775806 ||
- UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
- return 16;
- }
-
- // 5([ 9223372036854775806, 9223372036854775806])]
- uErr = QCBORDecode_GetNext(&DC, &item);
- if(uErr != QCBOR_SUCCESS) {
- return 17;
- }
- if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
- item.val.expAndMantissa.nExponent != 9223372036854775806 ||
- item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
- return 18;
- }
-
- uErr = QCBORDecode_Finish(&DC);
- if(uErr != QCBOR_SUCCESS) {
- return 18;
- }
/* Now encode some stuff and then decode it */
uint8_t pBuf[40];
@@ -4752,58 +5230,23 @@
return 106;
}
-
- int64_t nExp, nMant;
- UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
- UsefulBufC Mant;
- bool bIsNeg;
-
- QCBORDecode_Init(&DC,
- UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
- QCBOR_DECODE_MODE_NORMAL);
- QCBORDecode_EnterArray(&DC, NULL);
-
- // 4([-1, 3]),
- QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
-
- // 4([-20, 4759477275222530853136]),
- QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
- &Mant, &bIsNeg, &nExp);
-
- // 4([9223372036854775807, -4759477275222530853137]),
- QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
- MantBuf, &Mant, &bIsNeg, &nExp);
-
- // 5([300, 100]),
- QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
-
- // 5([-20, 4759477275222530853136]),
- QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
- &bIsNeg, &nExp);
-
- // 5([-9223372036854775807, -4759477275222530853137])
- QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
- &bIsNeg, &nExp);
-
- // 5([ 9223372036854775806, -4759477275222530853137])
- QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
- &bIsNeg, &nExp);
-
- // 5([ 9223372036854775806, 9223372036854775806])]
- QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
- &bIsNeg, &nExp);
-
- QCBORDecode_ExitArray(&DC);
-
- uErr = QCBORDecode_Finish(&DC);
- if(uErr != QCBOR_SUCCESS) {
- return 200;
- }
+#endif /* QCBOR_TAGS_DISABLED */
return 0;
}
+int32_t ExponentAndMantissaDecodeTests(void)
+{
+ int32_t rv = ProcessEaMTests();
+ if(rv) {
+ return rv;
+ }
+
+ return ExponentAndMantissaDecodeTestsSecondary();
+}
+
+
static const struct FailInput ExponentAndMantissaFailures[] = {
// Exponent > INT64_MAX
{ {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
@@ -5211,10 +5654,14 @@
}
*/
static const uint8_t spRecoverableMapErrors[] = {
+#ifndef QCBOR_DISABLE_TAGS
0xa6,
- 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
- 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
+ 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
+#else
+ 0xa4,
+#endif
+ 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x05, 0x00,
0x05, 0x00,
0x08, 0x08,
@@ -5487,6 +5934,7 @@
int64_t nInt;
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
QCBORDecode_EnterMap(&DCtx, NULL);
+#ifndef QCBOR_DISABLE_TAGS
QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
uErr = QCBORDecode_GetError(&DCtx);
if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
@@ -5496,6 +5944,7 @@
return 2121;
}
(void)QCBORDecode_GetAndResetError(&DCtx);
+#endif
QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
@@ -5504,11 +5953,13 @@
return 2023;
}
+#ifndef QCBOR_DISABLE_TAGS
QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
uErr = QCBORDecode_GetAndResetError(&DCtx);
if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
return 2024;
}
+#endif
QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
uErr = QCBORDecode_GetAndResetError(&DCtx);
@@ -5623,6 +6074,7 @@
return 2600;
}
+#ifndef QCBOR_DISABLE_TAGS
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
QCBORDecode_VGetNextConsume(&DCtx, &Item1);
if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
@@ -5634,6 +6086,8 @@
if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
return 2800;
}
+#endif
+
QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
QCBORDecode_VGetNextConsume(&DCtx, &Item1);
@@ -5676,6 +6130,7 @@
static const struct NumberConversion NumberConversions[] = {
+#ifndef QCBOR_DISABLE_TAGS
{
"too large to fit into int64_t",
{(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
@@ -5794,77 +6249,6 @@
FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
},
{
- "Positive integer 18446744073709551615",
- {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
- 0,
- QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
- 18446744073709551615ULL,
- QCBOR_SUCCESS,
- 18446744073709551615.0,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
- },
- {
- "Positive bignum 0xffff",
- {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
- 65536-1,
- QCBOR_SUCCESS,
- 0xffff,
- QCBOR_SUCCESS,
- 65535.0,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
- },
- {
- "Postive integer 0",
- {(uint8_t[]){0x0}, 1},
- 0LL,
- QCBOR_SUCCESS,
- 0ULL,
- QCBOR_SUCCESS,
- 0.0,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
- },
- {
- "Negative integer -18446744073709551616",
- {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
- -9223372036854775807-1, // INT64_MIN
- QCBOR_SUCCESS,
- 0ULL,
- QCBOR_ERR_NUMBER_SIGN_CONVERSION,
- -9223372036854775808.0,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
- },
- {
- "Double Floating point value 100.3",
- {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
- 100L,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
- 100ULL,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
- 100.3,
- FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
- },
- {
- "Floating point value NaN 0xfa7fc00000",
- {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
- 0,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
- 0,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
- NAN,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
- },
- {
- "half-precision Floating point value -4",
- {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
- // Normal case with all enabled.
- -4,
- FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
- 0,
- FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
- -4.0,
- FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
- },
- {
"Decimal fraction 3/10",
{(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
0,
@@ -5875,17 +6259,6 @@
FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
},
{
- "+inifinity single precision",
- {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
- 0,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
- 0,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
- INFINITY,
- FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
- },
-
- {
"extreme pos bignum",
{(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
// 50 rows of 8 is 400 digits.
@@ -6038,6 +6411,90 @@
-INFINITY,
FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
},
+ {
+ "Positive bignum 0xffff",
+ {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
+ 65536-1,
+ QCBOR_SUCCESS,
+ 0xffff,
+ QCBOR_SUCCESS,
+ 65535.0,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
+ },
+#endif /* QCBOR_DISABLE_TAGS */
+ {
+ "Positive integer 18446744073709551615",
+ {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
+ 0,
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
+ 18446744073709551615ULL,
+ QCBOR_SUCCESS,
+ 18446744073709551615.0,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
+ },
+
+ {
+ "Postive integer 0",
+ {(uint8_t[]){0x0}, 1},
+ 0LL,
+ QCBOR_SUCCESS,
+ 0ULL,
+ QCBOR_SUCCESS,
+ 0.0,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
+ },
+ {
+ "Negative integer -18446744073709551616",
+ {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
+ -9223372036854775807-1, // INT64_MIN
+ QCBOR_SUCCESS,
+ 0ULL,
+ QCBOR_ERR_NUMBER_SIGN_CONVERSION,
+ -9223372036854775808.0,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
+ },
+ {
+ "Double Floating point value 100.3",
+ {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
+ 100L,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
+ 100ULL,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
+ 100.3,
+ FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
+ },
+ {
+ "Floating point value NaN 0xfa7fc00000",
+ {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
+ 0,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
+ 0,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
+ NAN,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
+ },
+ {
+ "half-precision Floating point value -4",
+ {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
+ // Normal case with all enabled.
+ -4,
+ FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
+ 0,
+ FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
+ -4.0,
+ FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
+ },
+ {
+ "+inifinity single precision",
+ {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
+ 0,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
+ 0,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
+ INFINITY,
+ FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
+ },
+
};
@@ -6155,6 +6612,23 @@
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
+
+
+static const uint8_t spSequenceTestInput[] = {
+ /* 1. The valid date string "1985-04-12" */
+ 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
+
+ /* 2. */
+ 0x00,
+
+ /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
+ 0x1a, 0x53, 0x72, 0x4E, 0x00,
+
+ /* 4. */
+ 0x62, 'h', 'i',
+};
+
+
int32_t CBORSequenceDecodeTests(void)
{
QCBORDecodeContext DCtx;
@@ -6164,46 +6638,43 @@
// --- Test a sequence with extra bytes ---
- // The input for the date test happens to be a sequence so it
- // is reused. It is a sequence because it doesn't start as
- // an array or map.
QCBORDecode_Init(&DCtx,
- UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
QCBOR_DECODE_MODE_NORMAL);
- // Get the first item
+ // Get 1.
uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
if(uCBORError != QCBOR_SUCCESS) {
return 1;
}
- if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
+ if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
return 2;
}
uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
+ uConsumed != 11) {
+ return 102;
+ }
+
+ // Get 2.
+ uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
+ if(uCBORError != QCBOR_SUCCESS) {
+ return 66;
+ }
+
+ uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
+ if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
uConsumed != 12) {
return 102;
}
- // Get a second item
- uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
- if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
- return 66;
- }
-
- uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
- if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
- uConsumed != 14) {
- return 102;
- }
-
- // Get a third item
+ // Get 3.
uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
if(uCBORError != QCBOR_SUCCESS) {
return 2;
}
- if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
+ if(Item.uDataType != QCBOR_TYPE_INT64) {
return 3;
}
@@ -6220,7 +6691,6 @@
return 4;
}
-
// --- Test an empty input ----
uint8_t empty[1];
UsefulBufC Empty = {empty, 0};
@@ -7034,6 +7504,12 @@
#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
+/*
+ An array of three map entries
+ 1) Indefinite length string label for indefinite lenght byte string
+ 2) Indefinite length string label for an integer
+ 3) Indefinite length string label for an indefinite-length negative big num
+ */
static const uint8_t spMapWithIndefLenStrings[] = {
0xa3,
0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
@@ -7059,6 +7535,8 @@
UsefulBufC ByteString;
QCBORDecode_EnterMap(&DCtx, NULL);
QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
+
+#ifndef QCBOR_DISABLE_TAGS
if(QCBORDecode_GetAndResetError(&DCtx)) {
return 1;
}
@@ -7083,6 +7561,7 @@
"label2",
0xff,
&uDouble);
+
#ifndef QCBOR_DISABLE_FLOAT_HW_USE
if(QCBORDecode_GetAndResetError(&DCtx)) {
return 5;
@@ -7097,13 +7576,23 @@
#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
-
QCBORDecode_ExitMap(&DCtx);
if(QCBORDecode_Finish(&DCtx)) {
return 99;
}
+#else /* QCBOR_DISABLE_TAGS */
+ /* The big num in the input is a CBOR tag and you can't do
+ * map lookups in a map with a tag so this test does very little
+ * when tags are disabled. That is OK, the test coverage is still
+ * good when they are not.
+ */
+ if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
+ return 1002;
+ }
+#endif /*QCBOR_DISABLE_TAGS */
+
return 0;
}
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
@@ -7179,6 +7668,20 @@
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
+
+/*
+An array of one that contains
+ a byte string that is tagged 24 which means CBOR-encoded data
+ the byte string is an indefinite length string
+ the wrapped byte string is an array of three numbers
+ [42, 43, 44]
+
+[
+ 24(
+ (_ h'83', h'18', h'2A182B', h'182C')
+ )
+]
+ */
static const uint8_t pWrappedByIndefiniteLength[] = {
0x81,
0xd8, 0x18,
@@ -7779,6 +8282,7 @@
// Rewind an indefnite length byte-string wrapped sequence
#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
+ // TODO: rewrite this test to not use tags
QCBORDecode_Init(&DCtx,
UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
0);
@@ -7787,11 +8291,13 @@
QCBORDecode_EnterArray(&DCtx, NULL);
QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
+#ifndef QCBOR_DISABLE_TAGS
if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
- /* this is what happens when trying to enter
- indefinite-length byte string
- wrapped CBOR. Tolerate for now. Eventually it needs
- to be fixed so this works, but that is not simple. */
+ /* TODO: This is what happens when trying to enter
+ * indefinite-length byte string wrapped CBOR. Tolerate for
+ * now. Eventually it needs to be fixed so this works, but that
+ * is not simple.
+ */
return 7300;
}
@@ -7805,6 +8311,13 @@
if(i != 42) {
return 7220;
}*/
+
+#else /* QCBOR_DISABLE_TAGS */
+ if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
+ return 7301;
+ }
+#endif /* QCBOR_DISABLE_TAGS */
+
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
diff --git a/test/run_tests.c b/test/run_tests.c
index f28f526..f2baaf1 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -99,8 +99,11 @@
TEST_ENTRY(BasicEncodeTest),
TEST_ENTRY(NestedMapTest),
TEST_ENTRY(BignumParseTest),
+#ifndef QCBOR_DISABLE_TAGS
TEST_ENTRY(OptTagParseTest),
TEST_ENTRY(DateParseTest),
+ TEST_ENTRY(DecodeTaggedTypeTests),
+#endif /* QCBOR_DISABLE_TAGS */
TEST_ENTRY(SpiffyDateDecodeTest),
TEST_ENTRY(ShortBufferParseTest2),
TEST_ENTRY(ShortBufferParseTest),
@@ -136,12 +139,12 @@
TEST_ENTRY(EncodeLengthThirtyoneTest),
TEST_ENTRY(CBORSequenceDecodeTests),
TEST_ENTRY(IntToTests),
- TEST_ENTRY(DecodeTaggedTypeTests),
TEST_ENTRY(PeekAndRewindTest),
-#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
- TEST_ENTRY(EncodeLengthThirtyoneTest),
+#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
TEST_ENTRY(ExponentAndMantissaDecodeTests),
+#ifndef QCBOR_DISABLE_TAGS
TEST_ENTRY(ExponentAndMantissaDecodeFailTests),
+#endif /* QCBOR_DISABLE_TAGS */
TEST_ENTRY(ExponentAndMantissaEncodeTests),
#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
TEST_ENTRY(ParseEmptyMapInMapTest),