more number conversion working
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 03e8b28..203e000 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -2684,7 +2684,7 @@
* UINT64_MAX < 10 ^^ 19. More than that will cause
* exit with the overflow error
*/
- while(nExponent > 0) {
+ while(nExponent > 1) {
if(uResult > UINT64_MAX / 10) {
return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow
}
@@ -2851,7 +2851,7 @@
-static inline QCBORError ConvertPositiveBigNumToUnSigned(const UsefulBufC BigNum, uint64_t *pResult)
+static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult)
{
return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult);
}
@@ -2877,6 +2877,7 @@
return uError;
}
/* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */
+ // TODO: this code is incorrect. See RFC 7049
*pResult = -(int64_t)uResult;
return QCBOR_SUCCESS;
}
@@ -3016,10 +3017,15 @@
int64_t nMantissa;
pMe->uLastError = (uint8_t)ConvertPositiveBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa);
if(!pMe->uLastError) {
- pMe->uLastError = (uint8_t)ExponentiateNN(nMantissa,
- Item.val.expAndMantissa.nExponent,
- pValue,
- &Exponentitate10UU);
+ int64_t nMultiplier;
+ pMe->uLastError = (uint8_t)ExponentiateNN(10,
+ Item.val.expAndMantissa.nExponent,
+ &nMultiplier,
+ &Exponentitate10UU);
+ if(!pMe->uLastError) {
+ // TODO: overflow
+ *pValue = nMantissa * nMultiplier;
+ }
}
} else {
pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED;
@@ -3172,7 +3178,7 @@
case QCBOR_TYPE_POSBIGNUM:
if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
- pMe->uLastError = (uint8_t)ConvertPositiveBigNumToUnSigned(Item.val.bigNum, pValue);
+ pMe->uLastError = (uint8_t)ConvertPositiveBigNumToUnsigned(Item.val.bigNum, pValue);
} else {
pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED;
}
@@ -3214,13 +3220,17 @@
case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
- int64_t nMantissa;
- pMe->uLastError = (uint8_t)ConvertPositiveBigNumToSigned(Item.val.expAndMantissa.Mantissa.bigNum, &nMantissa);
+ uint64_t uMantissa;
+ pMe->uLastError = (uint8_t)ConvertPositiveBigNumToUnsigned(Item.val.expAndMantissa.Mantissa.bigNum, &uMantissa);
if(!pMe->uLastError) {
- pMe->uLastError = (uint8_t)ExponentitateNU(nMantissa,
+ uint64_t uMultiplier;
+ pMe->uLastError = (uint8_t)ExponentitateNU(10,
Item.val.expAndMantissa.nExponent,
- pValue,
+ &uMultiplier,
Exponentitate10UU);
+ if(!pMe->uLastError) {
+ *pValue = uMantissa * uMultiplier;
+ }
}
} else {
pMe->uLastError = QCBOR_ERR_CONVERSION_NOT_REQUESTED;
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index d96c8a8..86e75e7 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -4002,6 +4002,49 @@
static const struct NumberConversion NumberConversions[] = {
{
+ "Decimal Fraction with negative bignum [3, 257]",
+ {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
+ 0xC2, 0x42, 0x01, 0x01}, 15},
+ 257000,
+ QCBOR_SUCCESS,
+ 257000,
+ QCBOR_SUCCESS,
+ 257000.0,
+ QCBOR_SUCCESS
+ },
+ {
+ "bigfloat with negative bignum [3, 257]",
+ {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
+ 0xC3, 0x42, 0x01, 0x01}, 15},
+ -2056,
+ QCBOR_SUCCESS,
+ 0,
+ QCBOR_ERR_NUMBER_SIGN_CONVERSION,
+ -2056.0,
+ QCBOR_SUCCESS
+ },
+ {
+ "bigfloat with positive bignum [3, 257]",
+ {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
+ 0xC2, 0x42, 0x01, 0x01}, 15},
+ 2056,
+ QCBOR_SUCCESS,
+ 2056,
+ QCBOR_SUCCESS,
+ 2056.0,
+ QCBOR_SUCCESS
+ },
+ {
+ "negative bignum 0xc349010000000000000000",
+ {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
+ 0,
+ QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
+ 0,
+ QCBOR_ERR_NUMBER_SIGN_CONVERSION,
+ -18446744073709551617.0,
+ QCBOR_SUCCESS
+ },
+ {
"Positive bignum 0x01020304 indefinite length string",
{(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
0x01020304,
@@ -4012,7 +4055,7 @@
QCBOR_SUCCESS
},
{
- "Decimal Fraction [9223372036854775807, -4759477275222530853137]",
+ "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
{(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},
0,
@@ -4053,17 +4096,6 @@
18446744073709551615.0,
QCBOR_SUCCESS
},
-
- /* {
- "negative bignum 0xc349010000000000000000",
- {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
- 0,
- QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
- 0,
- QCBOR_ERR_NUMBER_SIGN_CONVERSION,
- -18446744073709551617.0,
- QCBOR_SUCCESS
- }, */
{
"Positive bignum 0xffff",
{(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},