re sync with master; fix data parse bug
diff --git a/src/ieee754.c b/src/ieee754.c
index ac454b4..226282a 100644
--- a/src/ieee754.c
+++ b/src/ieee754.c
@@ -453,9 +453,8 @@
// Public function; see ieee754.h
-double IEEE754_FloatToDouble(float f)
+double IEEE754_FloatToDouble(uint32_t uFloat)
{
- const uint32_t uFloat = CopyFloatToUint32(f);
// Pull out the three parts of the single-precision float
// Do all the work in 64 bits because that is what the end result is.
// It may give smaller code size and will keep static analyzers happier.
diff --git a/src/ieee754.h b/src/ieee754.h
index 47bfea5..72444f1 100644
--- a/src/ieee754.h
+++ b/src/ieee754.h
@@ -103,7 +103,7 @@
floating-point HW or compiler-supplied SW.
This is a loss-less conversion.
*/
-double IEEE754_FloatToDouble(float f);
+double IEEE754_FloatToDouble(uint32_t ufloat);
// Both tags the value and gives the size
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index ab37435..99060f3 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -30,48 +30,8 @@
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=============================================================================*/
-/*==============================================================================
- FILE: qcbor_decode.c
- DESCRIPTION: This file contains the implementation of QCBOR.
-
- EDIT HISTORY FOR FILE:
-
- This section contains comments describing changes made to the module.
- Notice that changes are listed in reverse chronological order.
-
- when who what, where, why
- -------- ---- ---------------------------------------------------
- 01/28/2020 llundblade Refine integer signedness to quiet static analysis.
- 01/25/2020 llundblade Cleaner handling of too-long encoded string input.
- 01/25/2020 llundblade Refine use of integer types to quiet static analysis
- 01/08/2020 llundblade Documentation corrections & improved code formatting
- 12/30/19 llundblade Add support for decimal fractions and bigfloats.
- 11/07/19 llundblade Fix long long conversion to double compiler warning
- 09/07/19 llundblade Fix bug decoding empty arrays and maps
- 07/31/19 llundblade Decode error fixes for some not-well-formed CBOR
- 07/31/19 llundblade New error code for better end of data handling
- 02/17/19 llundblade Fixed: QCBORItem.u{Data|Label}Alloc when
- bAllStrings set
- 02/16/19 llundblade Redesign MemPool to fix memory access alignment bug
- 01/10/19 llundblade Clever type and argument decoder; 250 bytes smaller
- 11/9/18 llundblade Error codes are now enums.
- 11/2/18 llundblade Simplify float decoding and align with preferred
- float encoding
- 10/31/18 llundblade Switch to one license that is almost BSD-3.
- 10/28/18 llundblade Reworked tag decoding
- 10/15/18 llundblade Indefinite length maps and arrays supported
- 10/8/18 llundblade Indefinite length strings supported
- 02/04/17 llundbla Work on CPUs that don's require pointer alignment
- by making use of changes in UsefulBuf
- 03/01/17 llundbla More data types; decoding improvements and fixes
- 11/13/16 llundbla Integrate most TZ changes back into github version.
- 09/30/16 gkanike Porting to TZ.
- 03/15/16 llundbla Initial Version.
-
- =============================================================================*/
-
-#include "qcbor.h"
+#include "qcbor/qcbor_decode.h"
#include "ieee754.h"
@@ -86,7 +46,8 @@
/*===========================================================================
DecodeNesting -- Functions for tracking array/map nesting when decoding
- See qcbor.h for definition of the object used here: QCBORDecodeNesting
+ See qcbor/qcbor_decode.h for definition of the object
+ used here: QCBORDecodeNesting
===========================================================================*/
inline static int
@@ -390,7 +351,8 @@
/*===========================================================================
QCBORDecode -- The main implementation of CBOR decoding
- See qcbor.h for definition of the object used here: QCBORDecodeContext
+ See qcbor/qcbor_decode.h for definition of the object
+ used here: QCBORDecodeContext
===========================================================================*/
/*
Public function, see header file
@@ -620,7 +582,7 @@
case SINGLE_PREC_FLOAT:
#ifndef QCBOR_CONFIG_DISABLE_ENCODE_IEEE754
// The caast to uint32_t is safe because the encoded value
- // was 16 bits. It was widened to 64 bits to be passed in here.
+ // was 32 bits. It was widened to 64 bits to be passed in here.
pDecodedItem->val.dfnum = IEEE754_FloatToDouble((uint32_t)uNumber);
#else
pDecodedItem->val.fnum = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber);
@@ -845,12 +807,16 @@
GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
{
// Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96
- QCBORError nReturn;
+
+ // Get pointer to string allocator. First use is to pass it to
+ // GetNext_Item() when option is set to allocate for *every* string.
+ // Second use here is to allocate space to coallese indefinite
+ // length string items into one.
const QCORInternalAllocator *pAllocator = me->StringAllocator.pfAllocator ?
&(me->StringAllocator) :
NULL;
- UsefulBufC FullString = NULLUsefulBufC;
+ QCBORError nReturn;
nReturn = GetNext_Item(&(me->InBuf),
pDecodedItem,
me->bStringAllocateAll ? pAllocator: NULL);
@@ -863,8 +829,8 @@
// indefinite length string tests, to be sure all is OK if this is removed.
// Only do indefinite length processing on strings
- if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING &&
- pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
+ const uint8_t uStringType = pDecodedItem->uDataType;
+ if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) {
goto Done; // no need to do any work here on non-string types
}
@@ -879,15 +845,14 @@
goto Done;
}
- // There is an indefinite length string to work on...
- // Track which type of string it is
- const uint8_t uStringType = pDecodedItem->uDataType;
-
// Loop getting chunk of indefinite string
+ UsefulBufC FullString = NULLUsefulBufC;
+
for(;;) {
// Get item for next chunk
QCBORItem StringChunkItem;
- // NULL passed to never string alloc chunk of indefinite length strings
+ // NULL string allocator passed here. Do not need to allocate
+ // chunks even if bStringAllocateAll is set.
nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL);
if(nReturn) {
break; // Error getting the next chunk
@@ -927,12 +892,12 @@
FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string);
}
-Done:
if(nReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) {
// Getting the item failed, clean up the allocated memory
StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr));
}
+Done:
return nReturn;
}
@@ -1062,9 +1027,15 @@
}
} else {
if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) {
+ if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) {
+ nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
+ goto Done;
+ }
// Decoding a map as an array
pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY;
- pDecodedItem->val.uCount *= 2;
+ // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2
+ // Cast is needed because of integer promotion
+ pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2);
}
}
@@ -1074,7 +1045,7 @@
/*
- Public function, see header qcbor.h file
+ Public function, see header qcbor/qcbor_decode.h file
*/
QCBORError QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me,
QCBORItem *pDecodedItem,
@@ -1392,7 +1363,7 @@
/*
- Public function, see header qcbor.h file
+ Public function, see header qcbor/qcbor_decode.h file
*/
QCBORError
QCBORDecode_GetNextWithTags(QCBORDecodeContext *me,
@@ -1460,7 +1431,7 @@
/*
- Public function, see header qcbor.h file
+ Public function, see header qcbor/qcbor_decode.h file
*/
QCBORError QCBORDecode_GetNext(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
{
@@ -1512,7 +1483,7 @@
/*
- Public function, see header qcbor.h file
+ Public function, see header qcbor/qcbor_decode.h file
*/
int QCBORDecode_IsTagged(QCBORDecodeContext *me,
const QCBORItem *pItem,
@@ -1533,7 +1504,7 @@
/*
- Public function, see header qcbor.h file
+ Public function, see header qcbor/qcbor_decode.h file
*/
QCBORError QCBORDecode_Finish(QCBORDecodeContext *me)
{
@@ -1741,7 +1712,7 @@
/*
- Public function, see header qcbor.h file
+ Public function, see header qcbor/qcbor_decode.h file
*/
QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe,
UsefulBuf Pool,
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 7e16595..95cb9fa 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -30,42 +30,8 @@
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=============================================================================*/
-/*=============================================================================
- FILE: qcbor_encode.c
- DESCRIPTION: This file contains the implementation of QCBOR.
-
- EDIT HISTORY FOR FILE:
-
- This section contains comments describing changes made to the module.
- Notice that changes are listed in reverse chronological order.
-
- when who what, where, why
- -------- ---- ---------------------------------------------------
- 02/07/2020 llundblade QCBOREncode_EncodeHead() and other for bstr hashing.
- 01/25/2020 llundblade Refine use of integer types to quiet static analysis.
- 01/08/2020 llundblade Documentation corrections & improved code formatting.
- 12/30/19 llundblade Add support for decimal fractions and bigfloats.
- 8/7/19 llundblade Prevent encoding simple type reserved values 24..31
- 7/25/19 janjongboom Add indefinite length encoding for maps and arrays
- 4/6/19 llundblade Wrapped bstr returned now includes the wrapping bstr.
- 12/30/18 llundblade Small efficient clever encode of type & argument.
- 11/29/18 llundblade Rework to simpler handling of tags and labels.
- 11/9/18 llundblade Error codes are now enums.
- 11/1/18 llundblade Floating support.
- 10/31/18 llundblade Switch to one license that is almost BSD-3.
- 09/28/18 llundblade Added bstr wrapping feature for COSE implementation.
- 02/05/18 llundbla Works on CPUs which require integer alignment.
- Requires new version of UsefulBuf.
- 07/05/17 llundbla Add bstr wrapping of maps/arrays for COSE
- 03/01/17 llundbla More data types
- 11/13/16 llundbla Integrate most TZ changes back into github version.
- 09/30/16 gkanike Porting to TZ.
- 03/15/16 llundbla Initial Version.
-
- =============================================================================*/
-
-#include "qcbor.h"
+#include "qcbor/qcbor_encode.h"
#include "ieee754.h"
@@ -133,7 +99,7 @@
return QCBOR_ERR_ARRAY_TOO_LONG;
}
- pNesting->pCurrentNesting->uCount += 1;
+ pNesting->pCurrentNesting->uCount++;
return QCBOR_SUCCESS;
}
@@ -242,7 +208,7 @@
/*
- Public function for initialization. See header qcbor.h
+ Public function for initialization. See qcbor/qcbor_encode.h
*/
void QCBOREncode_Init(QCBOREncodeContext *me, UsefulBuf Storage)
{
@@ -253,7 +219,7 @@
/*
- Public function for initialization. See header qcbor.h
+ Public function to encode a CBOR head. See qcbor/qcbor_encode.h
*/
UsefulBufC QCBOREncode_EncodeHead(UsefulBuf buffer,
uint8_t uMajorType,
@@ -275,7 +241,7 @@
The top three bits of the initial byte are the major type for the
CBOR data item. The eight major types defined by the standard are
- defined as CBOR_MAJOR_TYPE_xxxx in qcbor.h.
+ defined as CBOR_MAJOR_TYPE_xxxx in qcbor/qcbor_common.h.
The remaining five bits, known as "additional information", and
possibly more bytes encode the argument. If the argument is less than
@@ -502,7 +468,7 @@
/*
- Public functions for closing arrays and maps. See qcbor.h
+ Public functions for adding integers. See qcbor/qcbor_encode.h
*/
void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue)
{
@@ -514,7 +480,7 @@
/*
- Public functions for closing arrays and maps. See qcbor.h
+ Public functions for adding unsigned. See qcbor/qcbor_encode.h
*/
void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum)
{
@@ -541,7 +507,7 @@
Semi-private function. It is exposed to user of the interface, but
they will usually call one of the inline wrappers rather than this.
- See qcbor.h
+ See qcbor/qcbor_encode.h
Does the work of adding actual strings bytes to the CBOR output (as
opposed to numbers and opening / closing aggregate types).
@@ -581,7 +547,7 @@
/*
- Public functions for closing arrays and maps. See qcbor.h
+ Public functions for adding a tag. See qcbor/qcbor_encode.h
*/
void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag)
{
@@ -593,7 +559,7 @@
Semi-private function. It is exposed to user of the interface,
but they will usually call one of the inline wrappers rather than this.
- See header qcbor.h
+ See header qcbor/qcbor_encode.h
*/
void QCBOREncode_AddType7(QCBOREncodeContext *me, uint8_t uMinLen, uint64_t uNum)
{
@@ -610,7 +576,7 @@
/*
- Public functions for closing arrays and maps. See qcbor.h
+ Public functions for adding a double. See qcbor/qcbor_encode.h
*/
void QCBOREncode_AddDouble(QCBOREncodeContext *me, double dNum)
{
@@ -645,7 +611,7 @@
Semi-public function. It is exposed to the user of the interface, but
one of the inline wrappers will usually be called rather than this.
- See qcbor.h
+ See qcbor/qcbor_encode.h
*/
void QCBOREncode_AddExponentAndMantissa(QCBOREncodeContext *pMe,
uint64_t uTag,
@@ -682,7 +648,7 @@
Semi-public function. It is exposed to user of the interface,
but they will usually call one of the inline wrappers rather than this.
- See header qcbor.h
+ See qcbor/qcbor_encode.h
*/
void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType)
{
@@ -723,7 +689,7 @@
Semi-public function. It is exposed to user of the interface,
but they will usually call one of the inline wrappers rather than this.
- See qcbor.h
+ See qcbor/qcbor_encode.h
*/
void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType)
{
@@ -737,7 +703,7 @@
/*
- Public functions for closing arrays and maps. See qcbor.h
+ Public functions for closing arrays and maps. See qcbor/qcbor_encode.h
*/
void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType)
{
@@ -746,7 +712,7 @@
/*
- Public functions for closing bstr wrapping. See qcbor.h
+ Public functions for closing bstr wrapping. See qcbor/qcbor_encode.h
*/
void QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *me, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR)
{
@@ -783,7 +749,7 @@
/*
- Public functions for closing arrays and maps. See qcbor.h
+ Public functions for closing arrays and maps. See qcbor/qcbor_encode.h
*/
void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType)
{
@@ -803,7 +769,7 @@
/*
- Public functions to finish and get the encoded result. See qcbor.h
+ Public functions to finish and get the encoded result. See qcbor/qcbor_encode.h
*/
QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR)
{
@@ -826,7 +792,7 @@
/*
- Public functions to finish and get the encoded result. See qcbor.h
+ Public functions to finish and get the encoded result. See qcbor/qcbor_encode.h
*/
QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *me, size_t *puEncodedLen)
{
diff --git a/src/qcbor_err_to_str.c b/src/qcbor_err_to_str.c
new file mode 100644
index 0000000..3f9db0a
--- /dev/null
+++ b/src/qcbor_err_to_str.c
@@ -0,0 +1,48 @@
+/*==============================================================================
+ err_to_str.c -- strings names for errors
+
+ Copyright (c) 2020, Patrick Uiterwijk. All rights reserved.
+
+ SPDX-License-Identifier: BSD-3-Clause
+
+ See BSD-3-Clause license in README.md
+
+ Created on 3/21/20
+ =============================================================================*/
+
+#include "qcbor.h"
+
+#define _ERR_TO_STR(errpart) case QCBOR_##errpart: return "QCBOR_" #errpart;
+
+const char *qcbor_err_to_str(QCBORError err) {
+ switch (err) {
+ _ERR_TO_STR(SUCCESS)
+ _ERR_TO_STR(ERR_BUFFER_TOO_SMALL)
+ _ERR_TO_STR(ERR_ARRAY_NESTING_TOO_DEEP)
+ _ERR_TO_STR(ERR_ARRAY_TOO_LONG)
+ _ERR_TO_STR(ERR_TOO_MANY_CLOSES)
+ _ERR_TO_STR(ERR_UNSUPPORTED)
+ _ERR_TO_STR(ERR_HIT_END)
+ _ERR_TO_STR(ERR_BUFFER_TOO_LARGE)
+ _ERR_TO_STR(ERR_INT_OVERFLOW)
+ _ERR_TO_STR(ERR_MAP_LABEL_TYPE)
+ _ERR_TO_STR(ERR_ARRAY_OR_MAP_STILL_OPEN)
+ _ERR_TO_STR(ERR_DATE_OVERFLOW)
+ _ERR_TO_STR(ERR_BAD_TYPE_7)
+ _ERR_TO_STR(ERR_BAD_OPT_TAG)
+ _ERR_TO_STR(ERR_EXTRA_BYTES)
+ _ERR_TO_STR(ERR_CLOSE_MISMATCH)
+ _ERR_TO_STR(ERR_NO_STRING_ALLOCATOR)
+ _ERR_TO_STR(ERR_INDEFINITE_STRING_CHUNK)
+ _ERR_TO_STR(ERR_STRING_ALLOCATE)
+ _ERR_TO_STR(ERR_BAD_BREAK)
+ _ERR_TO_STR(ERR_TOO_MANY_TAGS)
+ _ERR_TO_STR(ERR_BAD_INT)
+ _ERR_TO_STR(ERR_NO_MORE_ITEMS)
+ _ERR_TO_STR(ERR_BAD_EXP_AND_MANTISSA)
+ _ERR_TO_STR(ERR_STRING_TOO_LONG)
+
+ default:
+ return "Invalid error";
+ }
+}