Documentation, comments and code formatting improvements
Small documentation corrections.
Improve comments in codes, spelling corrections, comment formatting
Wrap long source code lines so there are fewer lines over 80 columns and very few over 120 columns
NO changes to actual source or interface
diff --git a/README.md b/README.md
index bbac803..e3953aa 100644
--- a/README.md
+++ b/README.md
@@ -48,7 +48,7 @@
**Comprehensive test suite** – Easy to verify on a new platform
or OS with the test suite. The test suite dependencies are also
- minimal, only additionally requiring <math.h> for floating point
+ minimal, only additionally requiring <math.h> for floating-point
tests.
## Code Status
@@ -93,8 +93,8 @@
this work straight away, whatever your development environment is.
The files ieee754.c and ieee754.h are support for half-precision
-floating point. The encoding side of the floating point functionality
-is about 500 bytes. If it is never called because no floating point
+floating-point. The encoding side of the floating-point functionality
+is about 500 bytes. If it is never called because no floating-point
numbers are ever encoded, all 500 bytes will be dead stripped and not
impact code size. The decoding side is about 150 bytes of object
code. It is never dead stripped because it directly referenced by the
diff --git a/cmd_line_main.c b/cmd_line_main.c
index f40c961..9c93703 100644
--- a/cmd_line_main.c
+++ b/cmd_line_main.c
@@ -1,14 +1,14 @@
/*==============================================================================
cmd_line_mainc.c -- Runs tests for QCBOR encoder / decoder
- Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
See BSD-3-Clause license in README.md
Created on 9/13/18
- ==============================================================================*/
+ =============================================================================*/
#include <stdio.h>
#include "run_tests.h"
diff --git a/inc/qcbor.h b/inc/qcbor.h
index 4d8f8f7..18fd49e 100644
--- a/inc/qcbor.h
+++ b/inc/qcbor.h
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -28,10 +28,10 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ =============================================================================*/
-/*===================================================================================
+/*=============================================================================
FILE: qcbor.h
DESCRIPTION: This is the full public API and data structures for QCBOR
@@ -41,31 +41,32 @@
This section contains comments describing changes made to the module.
Notice that changes are listed in reverse chronological order.
- when who what, where, why
- -------- ---- ---------------------------------------------------
- 12/30/19 llundblade Add support for decimal fractions and bigfloats.
- 08/7/19 llundblade Better handling of not well-formed encode and decode.
- 07/31/19 llundblade New error code for better end of data handling.
- 7/25/19 janjongboom Add indefinite length encoding for maps and arrays.
- 05/26/19 llundblade Add QCBOREncode_GetErrorState() and _IsBufferNULL().
- 04/26/19 llundblade Big documentation & style update. No interface change.
- 02/16/19 llundblade Redesign MemPool to fix memory access alignment bug.
- 12/18/18 llundblade Move decode malloc optional code to separate repository.
- 12/13/18 llundblade Documentatation improvements.
- 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.
- 10/15/18 llundblade indefinite-length maps and arrays supported
- 10/8/18 llundblade indefinite-length strings supported
- 09/28/18 llundblade Added bstr wrapping feature for COSE implementation.
- 07/05/17 llundbla Add bstr wrapping of maps/arrays for COSE.
- 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.
+ when who what, where, why
+ -------- ---- ---------------------------------------------------
+ 01/08/2020 llundblade Documentation corrections & improved code formatting.
+ 12/30/19 llundblade Add support for decimal fractions and bigfloats.
+ 08/7/19 llundblade Better handling of not well-formed encode and decode.
+ 07/31/19 llundblade New error code for better end of data handling.
+ 7/25/19 janjongboom Add indefinite length encoding for maps and arrays.
+ 05/26/19 llundblade Add QCBOREncode_GetErrorState() and _IsBufferNULL().
+ 04/26/19 llundblade Big documentation & style update. No interface change.
+ 02/16/19 llundblade Redesign MemPool to fix memory access alignment bug.
+ 12/18/18 llundblade Move decode malloc optional code to separate repo.
+ 12/13/18 llundblade Documentatation improvements.
+ 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.
+ 10/15/18 llundblade indefinite-length maps and arrays supported
+ 10/8/18 llundblade indefinite-length strings supported
+ 09/28/18 llundblade Added bstr wrapping feature for COSE implementation.
+ 07/05/17 llundbla Add bstr wrapping of maps/arrays for COSE.
+ 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.
- =====================================================================================*/
+ =============================================================================*/
#ifndef __QCBOR__qcbor__
#define __QCBOR__qcbor__
@@ -76,7 +77,7 @@
Caller of QCBOR should not reference any of the details below up until
the start of the public part.
- =========================================================================== */
+ ========================================================================== */
/*
Standard integer types are used in the interface to be precise about
@@ -110,8 +111,8 @@
/*
PRIVATE DATA STRUCTURE
- Holds the data for tracking array and map nesting during encoding. Pairs up with
- the Nesting_xxx functions to make an "object" to handle nesting encoding.
+ Holds the data for tracking array and map nesting during encoding. Pairs up
+ with the Nesting_xxx functions to make an "object" to handle nesting encoding.
uStart is a uint32_t instead of a size_t to keep the size of this
struct down so it can be on the stack without any concern. It would be about
@@ -124,10 +125,10 @@
typedef struct __QCBORTrackNesting {
// PRIVATE DATA STRUCTURE
struct {
- // See function OpenArrayInternal() for detailed comments on how this works
- uint32_t uStart; // uStart is the byte position where the array starts
- uint16_t uCount; // Number of items in the arrary or map; counts items
- // in a map, not pairs of items
+ // See function QCBOREncode_OpenMapOrArray() for details on how this works
+ uint32_t uStart; // uStart is the byte position where the array starts
+ uint16_t uCount; // Number of items in the arrary or map; counts items
+ // in a map, not pairs of items
uint8_t uMajorType; // Indicates if item is a map or an array
} pArrays[QCBOR_MAX_ARRAY_NESTING1+1], // stored state for the nesting levels
*pCurrentNesting; // the current nesting level
@@ -225,20 +226,20 @@
#define CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN 13
-/* ===========================================================================
+/* ==========================================================================
END OF PRIVATE PART OF THIS FILE
BEGINNING OF PUBLIC PART OF THIS FILE
- =========================================================================== */
+ ========================================================================== */
-/* ===========================================================================
+/* ==========================================================================
BEGINNING OF CONSTANTS THAT COME FROM THE CBOR STANDARD, RFC 7049
It is not necessary to use these directly when encoding or decoding
CBOR with this implementation.
- =========================================================================== */
+ ========================================================================== */
/* Standard CBOR Major type for positive integers of various lengths */
#define CBOR_MAJOR_TYPE_POSITIVE_INT 0
@@ -821,7 +822,7 @@
/** Type for [RFC 3339] (https://tools.ietf.org/html/rfc3339) date
string, possibly with time zone. Data is in @c val.dateString */
#define QCBOR_TYPE_DATE_STRING 11
-/** Type for integer seconds since Jan 1970 + floating point
+/** Type for integer seconds since Jan 1970 + floating-point
fraction. Data is in @c val.epochDate */
#define QCBOR_TYPE_DATE_EPOCH 12
/** A simple type that this CBOR implementation doesn't know about;
@@ -844,7 +845,7 @@
/** A floating-point number made of base-2 exponent and integer
mantissa. See @ref expAndMantissa and
- CBOREncode_AddBigFloat(). */
+ QCBOREncode_AddBigFloat(). */
#define QCBOR_TYPE_BIGFLOAT 17
/** A floating-point number made of base-2 exponent and positive big
@@ -1212,7 +1213,7 @@
/**
@brief Add a UTF-8 text string to the encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the text to.
@param[in] Text Pointer and length of text to add.
The text passed in must be unencoded UTF-8 according to [RFC 3629]
@@ -1239,7 +1240,7 @@
/**
@brief Add a UTF-8 text string to the encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the text to.
@param[in] szString Null-terminated text to add.
This works the same as QCBOREncode_AddText().
@@ -1254,8 +1255,8 @@
/**
@brief Add a floating-point number to the encoded output.
- @param[in] pCtx The encoding context to add the float to.
- @param[in] dNum The double precision number to add.
+ @param[in] pCtx The encoding context to add the double to.
+ @param[in] dNum The double-precision number to add.
This outputs a floating-point number with CBOR major type 7.
@@ -1288,7 +1289,7 @@
/**
@brief Add an optional tag.
- @param[in] pCtx The encoding context to add the integer to.
+ @param[in] pCtx The encoding context to add the tag to.
@param[in] uTag The tag to add
This outputs a CBOR major type 6 item that tags the next data item
@@ -1314,7 +1315,7 @@
/**
@brief Add an epoch-based date.
- @param[in] pCtx The encoding context to add the simple value to.
+ @param[in] pCtx The encoding context to add the date to.
@param[in] date Number of seconds since 1970-01-01T00:00Z in UTC time.
As per RFC 7049 this is similar to UNIX/Linux/POSIX dates. This is
@@ -1350,7 +1351,7 @@
/**
@brief Add a byte string to the encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the bytes to.
@param[in] Bytes Pointer and length of the input data.
Simply adds the bytes to the encoded output as CBOR major type 2.
@@ -1371,7 +1372,7 @@
/**
@brief Add a binary UUID to the encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the UUID to.
@param[in] Bytes Pointer and length of the binary UUID.
A binary UUID as defined in [RFC 4122]
@@ -1390,7 +1391,7 @@
/**
@brief Add a positive big number to the encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the big number to.
@param[in] Bytes Pointer and length of the big number.
Big numbers are integers larger than 64-bits. Their format is
@@ -1414,7 +1415,7 @@
/**
@brief Add a negative big number to the encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the big number to.
@param[in] Bytes Pointer and length of the big number.
Big numbers are integers larger than 64-bits. Their format is
@@ -1448,7 +1449,7 @@
A decimal fraction is good for exact representation of some values
that can't be represented exactly with standard C (IEEE 754)
floating-point numbers. Much larger and much smaller numbers can
- also be represented than floating point because of the larger number
+ also be represented than floating-point because of the larger number
of bits in the exponent.
The decimal fraction is conveyed as two integers, a mantissa and a
@@ -1520,7 +1521,7 @@
/**
@brief Add a big floating-point number to the encoded output.
- @param[in] pCtx The encoding context to add the decimal fraction to.
+ @param[in] pCtx The encoding context to add the bigfloat to.
@param[in] nMantissa The mantissa.
@param[in] nBase2Exponent The exponent.
@@ -1570,7 +1571,7 @@
@brief Add a big floating-point number with a big number mantissa to
the encoded output.
- @param[in] pCtx The encoding context to add the decimal fraction to.
+ @param[in] pCtx The encoding context to add the bigfloat to.
@param[in] Mantissa The mantissa.
@param[in] bIsNegative false if mantissa is positive, true if negative.
@param[in] nBase2Exponent The exponent.
@@ -1603,7 +1604,7 @@
/**
@brief Add a text URI to the encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the URI to.
@param[in] URI Pointer and length of the URI.
The format of URI must be per [RFC 3986]
@@ -1627,7 +1628,7 @@
/**
@brief Add Base64-encoded text to encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the base-64 text to.
@param[in] B64Text Pointer and length of the base-64 encoded text.
The text content is Base64 encoded data per [RFC 4648]
@@ -1646,7 +1647,7 @@
/**
@brief Add base64url encoded data to encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the base64url to.
@param[in] B64Text Pointer and length of the base64url encoded text.
The text content is base64URL encoded text as per [RFC 4648]
@@ -1665,7 +1666,7 @@
/**
@brief Add Perl Compatible Regular Expression.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the regular expression to.
@param[in] Regex Pointer and length of the regular expression.
The text content is Perl Compatible Regular
@@ -1684,7 +1685,7 @@
/**
@brief MIME encoded text to the encoded output.
- @param[in] pCtx The context to initialize.
+ @param[in] pCtx The encoding context to add the MIME data to.
@param[in] MIMEData Pointer and length of the regular expression.
The text content is in MIME format per [RFC 2045]
@@ -1705,7 +1706,7 @@
/**
@brief Add an RFC 3339 date string
- @param[in] pCtx The encoding context to add the simple value to.
+ @param[in] pCtx The encoding context to add the date to.
@param[in] szDate Null-terminated string with date to add.
The string szDate should be in the form of [RFC 3339]
@@ -1730,8 +1731,8 @@
/**
@brief Add a standard Boolean.
- @param[in] pCtx The encoding context to add the simple value to.
- @param[in] b true or false from @c <stdbool.h>. Anything will result in an error.
+ @param[in] pCtx The encoding context to add the Boolean to.
+ @param[in] b true or false from @c <stdbool.h>.
Adds a Boolean value as CBOR major type 7.
@@ -1748,7 +1749,7 @@
/**
@brief Add a NULL to the encoded output.
- @param[in] pCtx The encoding context to add the simple value to.
+ @param[in] pCtx The encoding context to add the NULL to.
Adds the NULL value as CBOR major type 7.
@@ -1767,7 +1768,7 @@
/**
@brief Add an "undef" to the encoded output.
- @param[in] pCtx The encoding context to add the simple value to.
+ @param[in] pCtx The encoding context to add the "undef" to.
Adds the undef value as CBOR major type 7.
@@ -1826,7 +1827,7 @@
/**
@brief Close an open array.
- @param[in] pCtx The context to add to.
+ @param[in] pCtx The encoding context to close the array in.
The closes an array opened by QCBOREncode_OpenArray(). It reduces
nesting level by one. All arrays (and maps) must be closed before
@@ -1850,7 +1851,7 @@
/**
@brief Indicates that the next items added are in a map.
- @param[in] pCtx The context to add to.
+ @param[in] pCtx The encoding context to open the map in.
See QCBOREncode_OpenArray() for more information, particularly error
handling.
@@ -1888,7 +1889,7 @@
/**
@brief Close an open map.
- @param[in] pCtx The context to add to.
+ @param[in] pCtx The encoding context to close the map in .
This closes a map opened by QCBOREncode_OpenMap(). It reduces nesting
level by one.
@@ -1911,7 +1912,7 @@
/**
@brief Indicate start of encoded CBOR to be wrapped in a bstr.
- @param[in] pCtx The context to add to.
+ @param[in] pCtx The encoding context to open the bstr-wrapped CBOR in.
All added encoded items between this call and a call to
QCBOREncode_CloseBstrWrap() will be wrapped in a bstr. They will
@@ -1950,7 +1951,7 @@
/**
@brief Close a wrapping bstr.
- @param[in] pCtx The context to add to.
+ @param[in] pCtx The encoding context to close of bstr wrapping in.
@param[out] pWrappedCBOR A @ref UsefulBufC containing wrapped bytes.
The closes a wrapping bstr opened by QCBOREncode_CloseBstrWrap(). It reduces
@@ -1983,7 +1984,7 @@
/**
@brief Add some already-encoded CBOR bytes.
- @param[in] pCtx The context to add to.
+ @param[in] pCtx The encoding context to add the already-encode CBOR to.
@param[in] Encoded The already-encoded CBOR to add to the context.
The encoded CBOR being added must be fully conforming CBOR. It must
@@ -2087,7 +2088,7 @@
/**
@brief Indicate whether output buffer is NULL or not.
- @param[in] pCtx The encoding ontext.
+ @param[in] pCtx The encoding context.
@return 1 if the output buffer is @c NULL.
@@ -2101,7 +2102,7 @@
/**
@brief Get the encoding error state.
- @param[in] pCtx The encoding ontext.
+ @param[in] pCtx The encoding context.
@return One of \ref QCBORError. See return values from
QCBOREncode_Finish()
@@ -2711,7 +2712,7 @@
/**
- @brief Semi-private method to open a map, array or bstr wrapped CBOR
+ @brief Semi-private method to open a map, array or bstr-wrapped CBOR
@param[in] pCtx The context to add to.
@param[in] uMajorType The major CBOR type to close
@@ -2723,13 +2724,13 @@
/**
- @brief Semi-private method to open a map, array or bstr wrapped CBOR with indefinite length
+ @brief Semi-private method to open a map, array with indefinite length
@param[in] pCtx The context to add to.
@param[in] uMajorType The major CBOR type to close
- Call QCBOREncode_OpenArrayIndefiniteLength() or QCBOREncode_OpenMapIndefiniteLength()
- instead of this.
+ Call QCBOREncode_OpenArrayIndefiniteLength() or
+ QCBOREncode_OpenMapIndefiniteLength() instead of this.
*/
void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, uint8_t uMajorType);
@@ -2744,19 +2745,23 @@
Call QCBOREncode_CloseArray(), QCBOREncode_CloseMap() or
QCBOREncode_CloseBstrWrap() instead of this.
*/
-void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC *pWrappedCBOR);
+void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *pCtx,
+ uint8_t uMajorType,
+ UsefulBufC *pWrappedCBOR);
/**
- @brief Semi-private method to close a map, array or bstr wrapped CBOR with indefinite length
+ @brief Semi-private method to close a map, array with indefinite length
@param[in] pCtx The context to add to.
@param[in] uMajorType The major CBOR type to close.
@param[out] pWrappedCBOR Pointer to @ref UsefulBufC containing wrapped bytes.
- Call QCBOREncode_CloseArrayIndefiniteLength() or QCBOREncode_CloseMapIndefiniteLength()
- instead of this.
+ Call QCBOREncode_CloseArrayIndefiniteLength() or
+ QCBOREncode_CloseMapIndefiniteLength() instead of this.
*/
-void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC *pWrappedCBOR);
+void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
+ uint8_t uMajorType,
+ UsefulBufC *pWrappedCBOR);
/**
@brief Semi-private method to add simple types.
@@ -3036,7 +3041,12 @@
int64_t nMantissa,
int64_t nBase10Exponent)
{
- QCBOREncode_AddExponentAndMantissa(pCtx, CBOR_TAG_DECIMAL_FRACTION, NULLUsefulBufC, false, nMantissa, nBase10Exponent);
+ QCBOREncode_AddExponentAndMantissa(pCtx,
+ CBOR_TAG_DECIMAL_FRACTION,
+ NULLUsefulBufC,
+ false,
+ nMantissa,
+ nBase10Exponent);
}
static inline void QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
@@ -3062,7 +3072,11 @@
bool bIsNegative,
int64_t nBase10Exponent)
{
- QCBOREncode_AddExponentAndMantissa(pCtx, CBOR_TAG_DECIMAL_FRACTION, Mantissa, bIsNegative, 0, nBase10Exponent);
+ QCBOREncode_AddExponentAndMantissa(pCtx,
+ CBOR_TAG_DECIMAL_FRACTION,
+ Mantissa, bIsNegative,
+ 0,
+ nBase10Exponent);
}
static inline void QCBOREncode_AddDecimalFractionBigNumToMap(QCBOREncodeContext *pCtx,
diff --git a/src/UsefulBuf.c b/src/UsefulBuf.c
index 0c336b8..1d13f10 100644
--- a/src/UsefulBuf.c
+++ b/src/UsefulBuf.c
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
@@ -27,9 +27,9 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ =============================================================================*/
-/*===================================================================================
+/*=============================================================================
FILE: UsefulBuf.c
DESCRIPTION: General purpose input and output buffers
@@ -39,24 +39,26 @@
This section contains comments describing changes made to the module.
Notice that changes are listed in reverse chronological order.
- when who what, where, why
- -------- ---- ---------------------------------------------------
- 11/08/2019 llundblade Re check pointer math and update comments
- 3/6/2019 llundblade Add UsefulBuf_IsValue()
- 09/07/17 llundbla Fix critical bug in UsefulBuf_Find() -- a read off
- the end of memory when the bytes to find is longer
- than the bytes to search.
- 06/27/17 llundbla Fix UsefulBuf_Compare() bug. Only affected comparison
- for < or > for unequal length buffers. Added
- UsefulBuf_Set() function.
- 05/30/17 llundbla Functions for NULL UsefulBufs and const / unconst
- 11/13/16 llundbla Initial Version.
+ when who what, where, why
+ -------- ---- ---------------------------------------------------
+ 01/08/2020 llundblade Documentation corrections & improved code formatting.
+ 11/08/2019 llundblade Re check pointer math and update comments
+ 3/6/2019 llundblade Add UsefulBuf_IsValue()
+ 09/07/17 llundbla Fix critical bug in UsefulBuf_Find() -- a read off
+ the end of memory when the bytes to find is longer
+ than the bytes to search.
+ 06/27/17 llundbla Fix UsefulBuf_Compare() bug. Only affected comparison
+ for < or > for unequal length buffers. Added
+ UsefulBuf_Set() function.
+ 05/30/17 llundbla Functions for NULL UsefulBufs and const / unconst
+ 11/13/16 llundbla Initial Version.
- =====================================================================================*/
+ ============================================================================*/
#include "UsefulBuf.h"
-#define USEFUL_OUT_BUF_MAGIC (0x0B0F) // used to catch use of uninitialized or corrupted UOBs
+// used to catch use of uninitialized or corrupted UsefulOutBuf
+#define USEFUL_OUT_BUF_MAGIC (0x0B0F)
/*
@@ -64,7 +66,8 @@
*/
UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src)
{
- // Do this with subtraction so it doesn't give erroneous result if uOffset + Src.len overflows
+ // Do this with subtraction so it doesn't give erroneous
+ // result if uOffset + Src.len overflows
if(uOffset > Dest.len || Src.len > Dest.len - uOffset) { // uOffset + Src.len > Dest.len
return NULLUsefulBufC;
}
@@ -166,11 +169,13 @@
/*
Public function -- see UsefulBuf.h
- The core of UsefulOutBuf -- put some bytes in the buffer without writing off the end of it.
+ The core of UsefulOutBuf -- put some bytes in the buffer without writing off
+ the end of it.
Code Reviewers: THIS FUNCTION DOES POINTER MATH
- This function inserts the source buffer, NewData, into the destination buffer, me->UB.ptr.
+ This function inserts the source buffer, NewData, into the destination
+ buffer, me->UB.ptr.
Destination is represented as:
me->UB.ptr -- start of the buffer
@@ -192,7 +197,8 @@
2. Is insertion position in the range of valid data?
- 3. If insertion point is not at the end, slide data to the right of the insertion point to the right
+ 3. If insertion point is not at the end, slide data to the right of the
+ insertion point to the right
4. Put the new data in at the insertion position.
@@ -219,7 +225,9 @@
// be sure there is no pointer arithmatic under/overflow.
if(pMe->data_len > pMe->UB.len) { // Check #1
pMe->err = 1;
- return; // Offset of valid data is off the end of the UsefulOutBuf due to uninitialization or corruption
+ // Offset of valid data is off the end of the UsefulOutBuf due to
+ // uninitialization or corruption
+ return;
}
/* 1. Will it fit? */
@@ -330,7 +338,7 @@
/*
Public function -- see UsefulBuf.h
- The core of UsefulInputBuf -- consume some bytes without going off the end of the buffer.
+ The core of UsefulInputBuf -- consume bytes without going off end of buffer.
Code Reviewers: THIS FUNCTION DOES POINTER MATH
*/
@@ -342,14 +350,15 @@
}
if(!UsefulInputBuf_BytesAvailable(pMe, uAmount)) {
- // The number of bytes asked for at current position are more than available
+ // Number of bytes asked for at current position are more than available
pMe->err = 1;
return NULL;
}
// This is going to succeed
const void * const result = ((uint8_t *)pMe->UB.ptr) + pMe->cursor;
- pMe->cursor += uAmount; // this will not overflow because of check using UsefulInputBuf_BytesAvailable()
+ // Will not overflow because of check using UsefulInputBuf_BytesAvailable()
+ pMe->cursor += uAmount;
return result;
}
diff --git a/src/ieee754.c b/src/ieee754.c
index 6fdfda8..ea44e1d 100644
--- a/src/ieee754.c
+++ b/src/ieee754.c
@@ -1,35 +1,42 @@
/*==============================================================================
- ieee754.c -- floating point conversion between half, double and single precision
+ ieee754.c -- floating-point conversion between half, double & single-precision
- Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
See BSD-3-Clause license in README.md
Created on 7/23/18
- ==============================================================================*/
+ =============================================================================*/
#include "ieee754.h"
#include <string.h> // For memcpy()
/*
- This code is written for clarity and verifiability, not for size, on the assumption
- that the optimizer will do a good job. The LLVM optimizer, -Os, does seem to do the
- job and the resulting object code is smaller from combining code for the many different
- cases (normal, subnormal, infinity, zero...) for the conversions.
+ This code is written for clarity and verifiability, not for size, on
+ the assumption that the optimizer will do a good job. The LLVM
+ optimizer, -Os, does seem to do the job and the resulting object code
+ is smaller from combining code for the many different cases (normal,
+ subnormal, infinity, zero...) for the conversions.
- Dead stripping is also really helpful to get code size down when floating point
- encoding is not needed.
+ This code has really long lines and is much easier to read because of
+ them. Some coding guidelines prefer 80 column lines (can they not afford
+ big displays?). It would make this code much worse even to wrap at 120
+ columns.
- This code works solely using shifts and masks and thus has no dependency on
- any math libraries. It can even work if the CPU doesn't have any floating
- point support, though that isn't the most useful thing to do.
+ Dead stripping is also really helpful to get code size down when
+ floating-point encoding is not needed.
- The memcpy() dependency is only for CopyFloatToUint32() and friends which only
- is needed to avoid type punning when converting the actual float bits to
- an unsigned value so the bit shifts and masks can work.
+ This code works solely using shifts and masks and thus has no
+ dependency on any math libraries. It can even work if the CPU doesn't
+ have any floating-point support, though that isn't the most useful
+ thing to do.
+
+ The memcpy() dependency is only for CopyFloatToUint32() and friends
+ which only is needed to avoid type punning when converting the actual
+ float bits to an unsigned value so the bit shifts and masks can work.
*/
/*
@@ -69,7 +76,7 @@
#define HALF_EXPONENT_INF_OR_NAN (HALF_EXPONENT_BIAS+1) // 16 Unbiased
-// ------ Single Precision --------
+// ------ Single-Precision --------
#define SINGLE_NUM_SIGNIFICAND_BITS (23)
#define SINGLE_NUM_EXPONENT_BITS (8)
#define SINGLE_NUM_SIGN_BITS (1)
@@ -96,7 +103,7 @@
#define SINGLE_EXPONENT_INF_OR_NAN (SINGLE_EXPONENT_BIAS+1) // 128 unbiased
-// --------- Double Precision ----------
+// --------- Double-Precision ----------
#define DOUBLE_NUM_SIGNIFICAND_BITS (52)
#define DOUBLE_NUM_EXPONENT_BITS (11)
#define DOUBLE_NUM_SIGN_BITS (1)
@@ -349,7 +356,7 @@
uSingleSign = uHalfSign;
- // Shift the three parts of the single precision into place
+ // Shift the three parts of the single-precision into place
const uint32_t uSinglePrecision = uSingleSignificand |
(uSingleBiasedExponent << SINGLE_EXPONENT_SHIFT) |
(uSingleSign << SINGLE_SIGN_SHIFT);
diff --git a/src/ieee754.h b/src/ieee754.h
index 2530f98..d2c1224 100644
--- a/src/ieee754.h
+++ b/src/ieee754.h
@@ -1,14 +1,14 @@
/*==============================================================================
- ieee754.c -- floating point conversion between half, double and single precision
+ ieee754.c -- floating-point conversion between half, double & single-precision
- Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
See BSD-3-Clause license in README.md
Created on 7/23/18
- ==============================================================================*/
+ =============================================================================*/
#ifndef ieee754_h
#define ieee754_h
@@ -20,94 +20,77 @@
/*
General comments
- This is a complete in that it handles all conversion cases
- including +/- infinity, +/- zero, subnormal numbers, qNaN, sNaN
- and NaN payloads.
+ This is a complete in that it handles all conversion cases including
+ +/- infinity, +/- zero, subnormal numbers, qNaN, sNaN and NaN
+ payloads.
- This confirms to IEEE 754-2008, but note that this doesn't
- specify conversions, just the encodings.
+ This confirms to IEEE 754-2008, but note that this doesn't specify
+ conversions, just the encodings.
- NaN payloads are preserved with alignment on the LSB. The
- qNaN bit is handled differently and explicity copied. It
- is always the MSB of the significand. The NaN payload MSBs
- (except the qNaN bit) are truncated when going from
- double or single to half.
+ NaN payloads are preserved with alignment on the LSB. The qNaN bit is
+ handled differently and explicity copied. It is always the MSB of the
+ significand. The NaN payload MSBs (except the qNaN bit) are truncated
+ when going from double or single to half.
TODO: what does the C cast do with NaN payloads from
- double to single?
-
-
+ double to single? It probably depends entirely on the
+ CPU.
*/
/*
- Most simply just explicilty encode the type you want, single or double.
- This works easily everywhere since standard C supports both
- these types and so does qcbor. This encoder also supports
- half precision and there's a few ways to use it to encode
- floating point numbers in less space.
+ Most simply just explicilty encode the type you want, single or
+ double. This works easily everywhere since standard C supports both
+ these types and so does qcbor. This encoder also supports half
+ precision and there's a few ways to use it to encode floating-point
+ numbers in less space.
- Without losing precision, you can encode a single or double
- such that the special values of 0, NaN and Infinity encode
- as half-precision. This CBOR decodoer and most others
- should handle this properly.
+ Without losing precision, you can encode a single or double such that
+ the special values of 0, NaN and Infinity encode as half-precision.
+ This CBOR decodoer and most others should handle this properly.
If you don't mind losing precision, then you can use half-precision.
One way to do this is to set up your environment to use
___fp_16. Some compilers and CPUs support it even though it is not
- standard C. What is nice about this is that your program
- will use less memory and floating point operations like
- multiplying, adding and such will be faster.
+ standard C. What is nice about this is that your program will use
+ less memory and floating-point operations like multiplying, adding
+ and such will be faster.
- Another way to make use of half-precision is to represent
- the values in your program as single or double, but encode
- them in CBOR as half-precision. This cuts the size
- of the encoded messages by 2 or 4, but doesn't reduce
- memory needs or speed because you are still using
+ Another way to make use of half-precision is to represent the values
+ in your program as single or double, but encode them in CBOR as
+ half-precision. This cuts the size of the encoded messages by 2 or 4,
+ but doesn't reduce memory needs or speed because you are still using
single or double in your code.
-
- encode:
- - float as float
- - double as double
- - half as half
- - float as half_precision, for environments that don't support a half-precision type
- - double as half_precision, for environments that don't support a half-precision type
- - float with NaN, Infinity and 0 as half
- - double with NaN, Infinity and 0 as half
-
-
-
-
*/
/*
- Convert single precision float to half-precision float.
- Precision and NaN payload bits will be lost. Too large
- values will round up to infinity and too small to zero.
+ Convert single-precision float to half-precision float. Precision
+ and NaN payload bits will be lost. Too-large values will round up to
+ infinity and too small to zero.
*/
uint16_t IEEE754_FloatToHalf(float f);
/*
- Convert half precision float to single precision float.
- This is a loss-less conversion.
+ Convert half-precision float to single-precision float. This is a
+ loss-less conversion.
*/
float IEEE754_HalfToFloat(uint16_t uHalfPrecision);
/*
- Convert double precision float to half-precision float.
- Precision and NaN payload bits will be lost. Too large
- values will round up to infinity and too small to zero.
+ Convert double-precision float to half-precision float. Precision
+ and NaN payload bits will be lost. Too-large values will round up to
+ infinity and too small to zero.
*/
uint16_t IEEE754_DoubleToHalf(double d);
/*
- Convert half precision float to double precision float.
+ Convert half-precision float to double-precision float.
This is a loss-less conversion.
*/
double IEEE754_HalfToDouble(uint16_t uHalfPrecision);
@@ -126,9 +109,10 @@
/*
- Converts double-precision to single-precision or half-precision if possible without
- loss of precisions. If not, leaves it as a double. Only converts to single-precision
- unless bAllowHalfPrecision is set.
+ Converts double-precision to single-precision or half-precision if
+ possible without loss of precisions. If not, leaves it as a
+ double. Only converts to single-precision unless bAllowHalfPrecision
+ is set.
*/
IEEE754_union IEEE754_DoubleToSmallestInternal(double d, int bAllowHalfPrecision);
@@ -143,17 +127,18 @@
/*
- Converts double-precision to single-precision or half-precision if possible without
- loss of precisions. If not, leaves it as a double.
+ Converts double-precision to single-precision or half-precision if
+ possible without loss of precisions. If not, leaves it as a double.
*/
static inline IEEE754_union IEEE754_DoubleToSmallest(double d)
{
return IEEE754_DoubleToSmallestInternal(d, 1);
}
+
/*
- Converts single-precision to half-precision if possible without
- loss of precision. If not leaves as single-precision.
+ Converts single-precision to half-precision if possible without loss
+ of precision. If not leaves as single-precision.
*/
IEEE754_union IEEE754_FloatToSmallest(float f);
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index a51c597..26bc3f6 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -28,9 +28,9 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ =============================================================================*/
-/*===================================================================================
+/*==============================================================================
FILE: qcbor_decode.c
DESCRIPTION: This file contains the implementation of QCBOR.
@@ -40,31 +40,33 @@
This section contains comments describing changes made to the module.
Notice that changes are listed in reverse chronological order.
- when who what, where, why
- -------- ---- ---------------------------------------------------
- 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 is 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.
+ when who what, where, why
+ -------- ---- ---------------------------------------------------
+ 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 "ieee754.h"
@@ -77,31 +79,39 @@
#define UNCONST_POINTER(ptr) ((void *)(ptr))
-/*
- Collection of functions to track the map/array nesting for decoding
- */
-inline static int IsMapOrArray(uint8_t uDataType)
+/*===========================================================================
+ DecodeNesting -- Functions for tracking array/map nesting when decoding
+
+ See qcbor.h for definition of the object used here: QCBORDecodeNesting
+ ===========================================================================*/
+
+inline static int
+IsMapOrArray(uint8_t uDataType)
{
return uDataType == QCBOR_TYPE_MAP || uDataType == QCBOR_TYPE_ARRAY;
}
-inline static int DecodeNesting_IsNested(const QCBORDecodeNesting *pNesting)
+inline static int
+DecodeNesting_IsNested(const QCBORDecodeNesting *pNesting)
{
return pNesting->pCurrent != &(pNesting->pMapsAndArrays[0]);
}
-inline static int DecodeNesting_IsIndefiniteLength(const QCBORDecodeNesting *pNesting)
+inline static int
+DecodeNesting_IsIndefiniteLength(const QCBORDecodeNesting *pNesting)
{
return pNesting->pCurrent->uCount == UINT16_MAX;
}
-inline static uint8_t DecodeNesting_GetLevel(QCBORDecodeNesting *pNesting)
+inline static uint8_t
+DecodeNesting_GetLevel(QCBORDecodeNesting *pNesting)
{
return pNesting->pCurrent - &(pNesting->pMapsAndArrays[0]);
}
-inline static int DecodeNesting_TypeIsMap(const QCBORDecodeNesting *pNesting)
+inline static int
+DecodeNesting_TypeIsMap(const QCBORDecodeNesting *pNesting)
{
if(!DecodeNesting_IsNested(pNesting)) {
return 0;
@@ -111,7 +121,8 @@
}
// Process a break. This will either ascend the nesting or error out
-inline static QCBORError DecodeNesting_BreakAscend(QCBORDecodeNesting *pNesting)
+inline static QCBORError
+DecodeNesting_BreakAscend(QCBORDecodeNesting *pNesting)
{
// breaks must always occur when there is nesting
if(!DecodeNesting_IsNested(pNesting)) {
@@ -129,8 +140,9 @@
return QCBOR_SUCCESS;
}
-// Called on every single item except breaks including the opening of a map/array
-inline static void DecodeNesting_DecrementCount(QCBORDecodeNesting *pNesting)
+// Called on every single item except breaks including open of a map/array
+inline static void
+DecodeNesting_DecrementCount(QCBORDecodeNesting *pNesting)
{
while(DecodeNesting_IsNested(pNesting)) {
// Not at the top level, so there is decrementing to be done.
@@ -152,9 +164,9 @@
}
}
-
// Called on every map/array
-inline static QCBORError DecodeNesting_Descend(QCBORDecodeNesting *pNesting, QCBORItem *pItem)
+inline static QCBORError
+DecodeNesting_Descend(QCBORDecodeNesting *pNesting, QCBORItem *pItem)
{
QCBORError nReturn = QCBOR_SUCCESS;
@@ -188,7 +200,8 @@
return nReturn;;
}
-inline static void DecodeNesting_Init(QCBORDecodeNesting *pNesting)
+inline static void
+DecodeNesting_Init(QCBORDecodeNesting *pNesting)
{
pNesting->pCurrent = &(pNesting->pMapsAndArrays[0]);
}
@@ -260,10 +273,12 @@
static inline int TagMapper_LookupBuiltIn(uint64_t uTag)
{
if(sizeof(spBuiltInTagMap)/sizeof(uint16_t) > TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS) {
- // This is a cross-check to make sure the above array doesn't
- // accidentally get made too big.
- // In normal conditions the above test should optimize out
- // as all the values are known at compile time.
+ /*
+ This is a cross-check to make sure the above array doesn't
+ accidentally get made too big. In normal conditions the above
+ test should optimize out as all the values are known at compile
+ time.
+ */
return -1;
}
@@ -297,7 +312,10 @@
This and the above functions could probably be optimized and made
clearer and neater.
*/
-static QCBORError TagMapper_Lookup(const QCBORTagListIn *pCallerConfiguredTagMap, uint64_t uTag, uint8_t *puTagBitIndex)
+static QCBORError
+TagMapper_Lookup(const QCBORTagListIn *pCallerConfiguredTagMap,
+ uint64_t uTag,
+ uint8_t *puTagBitIndex)
{
int nTagBitIndex = TagMapper_LookupBuiltIn(uTag);
if(nTagBitIndex >= 0) {
@@ -324,31 +342,38 @@
-/* ===========================================================================
+/*===========================================================================
QCBORStringAllocate -- STRING ALLOCATOR INVOCATION
The following four functions are pretty wrappers for invocation of
the string allocator supplied by the caller.
- ==============================================================================*/
+ ===========================================================================*/
-static inline void StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem)
+static inline void
+StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem)
{
(pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0);
}
-// StringAllocator_Reallocate called with pMem NULL is equal to StringAllocator_Allocate()
-static inline UsefulBuf StringAllocator_Reallocate(const QCORInternalAllocator *pMe, void *pMem, size_t uSize)
+// StringAllocator_Reallocate called with pMem NULL is
+// equal to StringAllocator_Allocate()
+static inline UsefulBuf
+StringAllocator_Reallocate(const QCORInternalAllocator *pMe,
+ void *pMem,
+ size_t uSize)
{
return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize);
}
-static inline UsefulBuf StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize)
+static inline UsefulBuf
+StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize)
{
return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize);
}
-static inline void StringAllocator_Destruct(const QCORInternalAllocator *pMe)
+static inline void
+StringAllocator_Destruct(const QCORInternalAllocator *pMe)
{
if(pMe->pfAllocator) {
(pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0);
@@ -357,16 +382,22 @@
+/*===========================================================================
+ QCBORDecode -- The main implementation of CBOR decoding
+ See qcbor.h for definition of the object used here: QCBORDecodeContext
+ ===========================================================================*/
/*
Public function, see header file
*/
-void QCBORDecode_Init(QCBORDecodeContext *me, UsefulBufC EncodedCBOR, QCBORDecodeMode nDecodeMode)
+void QCBORDecode_Init(QCBORDecodeContext *me,
+ UsefulBufC EncodedCBOR,
+ QCBORDecodeMode nDecodeMode)
{
memset(me, 0, sizeof(QCBORDecodeContext));
UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR);
- // Don't bother with error check on decode mode. If a bad value is passed it will just act as
- // if the default normal mode of 0 was set.
+ // Don't bother with error check on decode mode. If a bad value is
+ // passed it will just act as if the default normal mode of 0 was set.
me->uDecodeMode = nDecodeMode;
DecodeNesting_Init(&(me->nesting));
}
@@ -389,25 +420,31 @@
/*
Public function, see header file
*/
-void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *me, const QCBORTagListIn *pTagList)
+void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *me,
+ const QCBORTagListIn *pTagList)
{
me->pCallerConfiguredTagList = pTagList;
}
/*
- This decodes the fundamental part of a CBOR data item, the type and number
+ This decodes the fundamental part of a CBOR data item, the type and
+ number
This is the Counterpart to InsertEncodedTypeAndNumber().
- This does the network->host byte order conversion. The conversion here
- also results in the conversion for floats in addition to that for
- lengths, tags and integer values.
+ This does the network->host byte order conversion. The conversion
+ here also results in the conversion for floats in addition to that
+ for lengths, tags and integer values.
This returns:
pnMajorType -- the major type for the item
- puNumber -- the "number" which is used a the value for integers, tags and floats and length for strings and arrays
- puAdditionalInfo -- Pass this along to know what kind of float or if length is indefinite
+
+ puNumber -- the "number" which is used a the value for integers,
+ tags and floats and length for strings and arrays
+
+ puAdditionalInfo -- Pass this along to know what kind of float or
+ if length is indefinite
*/
inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf,
@@ -428,8 +465,8 @@
uint64_t uArgument;
if(uAdditionalInfo >= LEN_IS_ONE_BYTE && uAdditionalInfo <= LEN_IS_EIGHT_BYTES) {
- // Need to get 1,2,4 or 8 additional argument bytes
- // Map LEN_IS_ONE_BYTE.. LEN_IS_EIGHT_BYTES to actual length
+ // Need to get 1,2,4 or 8 additional argument bytes Map
+ // LEN_IS_ONE_BYTE.. LEN_IS_EIGHT_BYTES to actual length
static const uint8_t aIterate[] = {1,2,4,8};
// Loop getting all the bytes in the argument
@@ -464,19 +501,20 @@
}
/*
- CBOR doesn't explicitly specify two's compliment for integers but all CPUs
- use it these days and the test vectors in the RFC are so. All integers in the CBOR
- structure are positive and the major type indicates positive or negative.
- CBOR can express positive integers up to 2^x - 1 where x is the number of bits
- and negative integers down to 2^x. Note that negative numbers can be one
- more away from zero than positive.
- Stdint, as far as I can tell, uses two's compliment to represent
- negative integers.
+ CBOR doesn't explicitly specify two's compliment for integers but all
+ CPUs use it these days and the test vectors in the RFC are so. All
+ integers in the CBOR structure are positive and the major type
+ indicates positive or negative. CBOR can express positive integers
+ up to 2^x - 1 where x is the number of bits and negative integers
+ down to 2^x. Note that negative numbers can be one more away from
+ zero than positive. Stdint, as far as I can tell, uses two's
+ compliment to represent negative integers.
See http://www.unix.org/whitepapers/64bit.html for reasons int isn't
used here in any way including in the interface
*/
-inline static QCBORError DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem)
+inline static QCBORError
+DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem)
{
// Stack usage: int/ptr 1 -- 8
QCBORError nReturn = QCBOR_SUCCESS;
@@ -538,18 +576,19 @@
/*
Decode true, false, floats, break...
*/
-
-inline static QCBORError DecodeSimple(uint8_t uAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem)
+inline static QCBORError
+DecodeSimple(uint8_t uAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem)
{
// Stack usage: 0
QCBORError nReturn = QCBOR_SUCCESS;
- // uAdditionalInfo is 5 bits from the initial byte
- // compile time checks above make sure uAdditionalInfo values line up with uDataType values
+ // uAdditionalInfo is 5 bits from the initial byte compile time checks
+ // above make sure uAdditionalInfo values line up with uDataType values
pDecodedItem->uDataType = uAdditionalInfo;
switch(uAdditionalInfo) {
- // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as it is caught before this is called.
+ // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they are
+ // caught before this is called.
case HALF_PREC_FLOAT:
pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber);
@@ -582,8 +621,12 @@
default: // 0-19
pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE;
- // DecodeTypeAndNumber will make uNumber equal to uAdditionalInfo when uAdditionalInfo is < 24
- // This cast is safe because the 2, 4 and 8 byte lengths of uNumber are in the double/float cases above
+ /*
+ DecodeTypeAndNumber will make uNumber equal to
+ uAdditionalInfo when uAdditionalInfo is < 24 This cast is
+ safe because the 2, 4 and 8 byte lengths of uNumber are in
+ the double/float cases above
+ */
pDecodedItem->val.uSimple = (uint8_t)uNumber;
break;
}
@@ -626,7 +669,9 @@
// Normal case with no string allocator
pDecodedItem->val.string = Bytes;
}
- pDecodedItem->uDataType = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING) ? QCBOR_TYPE_BYTE_STRING : QCBOR_TYPE_TEXT_STRING;
+ const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING);
+ pDecodedItem->uDataType = bIsBstr ? QCBOR_TYPE_BYTE_STRING
+ : QCBOR_TYPE_TEXT_STRING;
Done:
return nReturn;
@@ -638,7 +683,8 @@
-// Make sure the constants align as this is assumed by the GetAnItem() implementation
+// Make sure the constants align as this is assumed by
+// the GetAnItem() implementation
#if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY
#error QCBOR_TYPE_ARRAY value not lined up with major type
#endif
@@ -647,12 +693,13 @@
#endif
/*
- This gets a single data item and decodes it including preceding optional tagging. This does not
- deal with arrays and maps and nesting except to decode the data item introducing them. Arrays and
- maps are handled at the next level up in GetNext().
+ This gets a single data item and decodes it including preceding
+ optional tagging. This does not deal with arrays and maps and nesting
+ except to decode the data item introducing them. Arrays and maps are
+ handled at the next level up in GetNext().
- Errors detected here include: an array that is too long to decode, hit end of buffer unexpectedly,
- a few forms of invalid encoded CBOR
+ Errors detected here include: an array that is too long to decode,
+ hit end of buffer unexpectedly, a few forms of invalid encoded CBOR
*/
static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf,
QCBORItem *pDecodedItem,
@@ -661,8 +708,12 @@
// Stack usage: int/ptr 3 -- 24
QCBORError nReturn;
- // Get the major type and the number. Number could be length of more bytes or the value depending on the major type
- // nAdditionalInfo is an encoding of the length of the uNumber and is needed to decode floats and doubles
+ /*
+ Get the major type and the number. Number could be length of more
+ bytes or the value depending on the major type nAdditionalInfo is
+ an encoding of the length of the uNumber and is needed to decode
+ floats and doubles
+ */
int uMajorType;
uint64_t uNumber;
uint8_t uAdditionalInfo;
@@ -671,14 +722,14 @@
nReturn = DecodeTypeAndNumber(pUInBuf, &uMajorType, &uNumber, &uAdditionalInfo);
- // Error out here if we got into trouble on the type and number.
- // The code after this will not work if the type and number is not good.
+ // Error out here if we got into trouble on the type and number. The
+ // code after this will not work if the type and number is not good.
if(nReturn) {
goto Done;
}
- // At this point the major type and the value are valid. We've got the type and the number that
- // starts every CBOR data item.
+ // At this point the major type and the value are valid. We've got
+ // the type and the number that starts every CBOR data item.
switch (uMajorType) {
case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0
case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1
@@ -692,7 +743,9 @@
case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2
case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3
if(uAdditionalInfo == LEN_IS_INDEFINITE) {
- pDecodedItem->uDataType = (uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING) ? QCBOR_TYPE_BYTE_STRING : QCBOR_TYPE_TEXT_STRING;
+ const bool bIsBstr = (uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING);
+ pDecodedItem->uDataType = bIsBstr ? QCBOR_TYPE_BYTE_STRING
+ : QCBOR_TYPE_TEXT_STRING;
pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX};
} else {
nReturn = DecodeBytes(pAllocator, uMajorType, uNumber, pUInBuf, pDecodedItem);
@@ -709,9 +762,11 @@
if(uAdditionalInfo == LEN_IS_INDEFINITE) {
pDecodedItem->val.uCount = UINT16_MAX; // Indicate indefinite length
} else {
- pDecodedItem->val.uCount = (uint16_t)uNumber; // type conversion OK because of check above
+ // type conversion OK because of check above
+ pDecodedItem->val.uCount = (uint16_t)uNumber;
}
- pDecodedItem->uDataType = uMajorType; // C preproc #if above makes sure constants align
+ // C preproc #if above makes sure constants for major types align
+ pDecodedItem->uDataType = uMajorType;
break;
case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags
@@ -723,11 +778,13 @@
}
break;
- case CBOR_MAJOR_TYPE_SIMPLE: // Major type 7, float, double, true, false, null...
+ case CBOR_MAJOR_TYPE_SIMPLE:
+ // Major type 7, float, double, true, false, null...
nReturn = DecodeSimple(uAdditionalInfo, uNumber, pDecodedItem);
break;
- default: // Should never happen because DecodeTypeAndNumber() should never return > 7
+ default:
+ // Never happens because DecodeTypeAndNumber() should never return > 7
nReturn = QCBOR_ERR_UNSUPPORTED;
break;
}
@@ -740,12 +797,13 @@
/*
This layer deals with indefinite length strings. It pulls all the
- individual chunk items together into one QCBORItem using the
- string allocator.
+ individual chunk items together into one QCBORItem using the string
+ allocator.
Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH
*/
-static inline QCBORError GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
+static inline QCBORError
+GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
{
// Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96
QCBORError nReturn;
@@ -842,7 +900,10 @@
Gets all optional tag data items preceding a data item that is not an
optional tag and records them as bits in the tag map.
*/
-static QCBORError GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags)
+static QCBORError
+GetNext_TaggedItem(QCBORDecodeContext *me,
+ QCBORItem *pDecodedItem,
+ QCBORTagListOut *pTags)
{
// Stack usage: int/ptr: 3 -- 24
QCBORError nReturn;
@@ -899,9 +960,13 @@
/*
- This layer takes care of map entries. It combines the label and data items into one QCBORItem.
+ This layer takes care of map entries. It combines the label and data
+ items into one QCBORItem.
*/
-static inline QCBORError GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags)
+static inline QCBORError
+GetNext_MapEntry(QCBORDecodeContext *me,
+ QCBORItem *pDecodedItem,
+ QCBORTagListOut *pTags)
{
// Stack use: int/ptr 1, QCBORItem -- 56
QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags);
@@ -919,7 +984,8 @@
if(DecodeNesting_TypeIsMap(&(me->nesting))) {
// If in a map and the right decoding mode, get the label
- // Get the next item which will be the real data; Item will be the label
+ // Save label in pDecodedItem and get the next which will
+ // be the real data
QCBORItem LabelItem = *pDecodedItem;
nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags);
if(nReturn)
@@ -932,7 +998,7 @@
pDecodedItem->label.string = LabelItem.val.string;
pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING;
} else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) {
- // It's not a string and we only want strings, probably for easy translation to JSON
+ // It's not a string and we only want strings
nReturn = QCBOR_ERR_MAP_LABEL_TYPE;
goto Done;
} else if(LabelItem.uDataType == QCBOR_TYPE_INT64) {
@@ -969,7 +1035,9 @@
/*
Public function, see header qcbor.h file
*/
-QCBORError QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags)
+QCBORError QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me,
+ QCBORItem *pDecodedItem,
+ QCBORTagListOut *pTags)
{
// Stack ptr/int: 2, QCBORItem : 64
@@ -1092,13 +1160,15 @@
}
const UsefulBufC Temp = pDecodedItem->val.string;
pDecodedItem->val.bigNum = Temp;
- pDecodedItem->uDataType = pDecodedItem->uTagBits & QCBOR_TAGFLAG_POS_BIGNUM ? QCBOR_TYPE_POSBIGNUM : QCBOR_TYPE_NEGBIGNUM;
+ const bool bIsPosBigNum = (bool)(pDecodedItem->uTagBits & QCBOR_TAGFLAG_POS_BIGNUM);
+ pDecodedItem->uDataType = bIsPosBigNum ? QCBOR_TYPE_POSBIGNUM : QCBOR_TYPE_NEGBIGNUM;
return QCBOR_SUCCESS;
}
/*
- The epoch formatted date. Turns lots of different forms of encoding date into uniform one
+ The epoch formatted date. Turns lots of different forms of encoding
+ date into uniform one
*/
static int DecodeDateEpoch(QCBORItem *pDecodedItem)
{
@@ -1187,9 +1257,9 @@
}
// A check for pDecodedItem->val.uCount == 2 would work for
- // definite length arrays, but not for indefnite. Instead remember
- // the nesting level the two integers must be at, which is one
- // deeper than that of the array.
+ // definite length arrays, but not for indefnite. 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;
// --- Is it a decimal fraction or a bigfloat? ---
@@ -1266,7 +1336,10 @@
/*
Public function, see header qcbor.h file
*/
-QCBORError QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags)
+QCBORError
+QCBORDecode_GetNextWithTags(QCBORDecodeContext *me,
+ QCBORItem *pDecodedItem,
+ QCBORTagListOut *pTags)
{
QCBORError nReturn;
@@ -1383,7 +1456,9 @@
/*
Public function, see header qcbor.h file
*/
-int QCBORDecode_IsTagged(QCBORDecodeContext *me, const QCBORItem *pItem, uint64_t uTag)
+int QCBORDecode_IsTagged(QCBORDecodeContext *me,
+ const QCBORItem *pItem,
+ uint64_t uTag)
{
const QCBORTagListIn *pCallerConfiguredTagMap = me->pCallerConfiguredTagList;
@@ -1431,21 +1506,28 @@
Decoder errors handled in this file
- - Hit end of input before it was expected while decoding type and number QCBOR_ERR_HIT_END
+ - Hit end of input before it was expected while decoding type and
+ number QCBOR_ERR_HIT_END
- negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW
- - Hit end of input while decoding a text or byte string QCBOR_ERR_HIT_END
+ - Hit end of input while decoding a text or byte string
+ QCBOR_ERR_HIT_END
- - Encountered conflicting tags -- e.g., an item is tagged both a date string and an epoch date QCBOR_ERR_UNSUPPORTED
+ - Encountered conflicting tags -- e.g., an item is tagged both a date
+ string and an epoch date QCBOR_ERR_UNSUPPORTED
- - Encontered an array or mapp that has too many items QCBOR_ERR_ARRAY_TOO_LONG
+ - Encontered an array or mapp that has too many items
+ QCBOR_ERR_ARRAY_TOO_LONG
- - Encountered array/map nesting that is too deep QCBOR_ERR_ARRAY_NESTING_TOO_DEEP
+ - Encountered array/map nesting that is too deep
+ QCBOR_ERR_ARRAY_NESTING_TOO_DEEP
- - An epoch date > INT64_MAX or < INT64_MIN was encountered QCBOR_ERR_DATE_OVERFLOW
+ - An epoch date > INT64_MAX or < INT64_MIN was encountered
+ QCBOR_ERR_DATE_OVERFLOW
- - The type of a map label is not a string or int QCBOR_ERR_MAP_LABEL_TYPE
+ - The type of a map label is not a string or int
+ QCBOR_ERR_MAP_LABEL_TYPE
- Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES
@@ -1478,15 +1560,17 @@
The sizes packed in are uint32_t to be the same on all CPU types
and simplify the code.
- =========================================================================== */
+ ========================================================================== */
-static inline int MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset)
+static inline int
+MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset)
{
// Use of UsefulInputBuf is overkill, but it is convenient.
UsefulInputBuf UIB;
- // Just assume the size here. It was checked during SetUp so the assumption is safe.
+ // Just assume the size here. It was checked during SetUp so
+ // the assumption is safe.
UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem, QCBOR_DECODE_MIN_MEM_POOL_SIZE});
*puPoolSize = UsefulInputBuf_GetUint32(&UIB);
*puFreeOffset = UsefulInputBuf_GetUint32(&UIB);
@@ -1494,7 +1578,8 @@
}
-static inline int MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset)
+static inline int
+MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset)
{
// Use of UsefulOutBuf is overkill, but convenient. The
// length check performed here is useful.
@@ -1515,7 +1600,8 @@
Code Reviewers: THIS FUNCTION DOES POINTER MATH
*/
-static UsefulBuf MemPool_Function(void *pPool, void *pMem, size_t uNewSize)
+static UsefulBuf
+MemPool_Function(void *pPool, void *pMem, size_t uNewSize)
{
UsefulBuf ReturnValue = NULLUsefulBuf;
@@ -1599,7 +1685,9 @@
/*
Public function, see header qcbor.h file
*/
-QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, UsefulBuf Pool, bool bAllStrings)
+QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe,
+ UsefulBuf Pool,
+ bool bAllStrings)
{
// The pool size and free mem offset are packed into the beginning
// of the pool memory. This compile time check make sure the
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 56114e1..cb6670d 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -28,9 +28,9 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ =============================================================================*/
-/*===================================================================================
+/*=============================================================================
FILE: qcbor_encode.c
DESCRIPTION: This file contains the implementation of QCBOR.
@@ -40,54 +40,64 @@
This section contains comments describing changes made to the module.
Notice that changes are listed in reverse chronological order.
- when who what, where, why
- -------- ---- ---------------------------------------------------
- 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.
+ when who what, where, why
+ -------- ---- ---------------------------------------------------
+ 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 "ieee754.h"
-/*...... This is a ruler that is 80 characters long...........................*/
-
/*
- CBOR's two nesting types, arrays and maps, are tracked here. There is a
- limit of QCBOR_MAX_ARRAY_NESTING to the number of arrays and maps
+ Nesting -- This tracks the nesting of maps and arrays.
+
+ The following functions and data type QCBORTrackNesting implement the
+ nesting management for encoding.
+
+ CBOR's two nesting types, arrays and maps, are tracked here. There is
+ a limit of QCBOR_MAX_ARRAY_NESTING to the number of arrays and maps
that can be nested in one encoding so the encoding context stays
small enough to fit on the stack.
When an array / map is opened, pCurrentNesting points to the element
- in pArrays that records the type, start position and accumluates a
+ in pArrays that records the type, start position and accumulates a
count of the number of items added. When closed the start position is
used to go back and fill in the type and number of items in the array
/ map.
- Encoded output be just items like ints and strings that are
- not part of any array / map. That is, the first thing encoded
- does not have to be an array or a map.
+ Encoded output can be just items like ints and strings that are not
+ part of any array / map. That is, the first thing encoded does not
+ have to be an array or a map.
+
+ QCBOR has a special feature to allow constructing bstr-wrapped CBOR
+ directly into the output buffer, so an extra buffer for it is not
+ needed. This is implemented as nesting with type
+ CBOR_MAJOR_TYPE_BYTE_STRING and uses this code. Bstr-wrapped CBOR is
+ used by COSE for data that is to be hashed.
*/
inline static void Nesting_Init(QCBORTrackNesting *pNesting)
{
- // assumes pNesting has been zeroed
+ // Assumes pNesting has been zeroed
pNesting->pCurrentNesting = &pNesting->pArrays[0];
// Implied CBOR array at the top nesting level. This is never returned,
// but makes the item count work correctly.
@@ -101,7 +111,7 @@
QCBORError nReturn = QCBOR_SUCCESS;
if(pNesting->pCurrentNesting == &pNesting->pArrays[QCBOR_MAX_ARRAY_NESTING]) {
- // trying to open one too many
+ // Trying to open one too many
nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
} else {
pNesting->pCurrentNesting++;
@@ -152,17 +162,35 @@
return pNesting->pCurrentNesting->uMajorType;
}
-inline static int Nesting_IsInNest(QCBORTrackNesting *pNesting)
+inline static bool Nesting_IsInNest(QCBORTrackNesting *pNesting)
{
- return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? 0 : 1;
+ return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? false : true;
}
/*
+ Encoding of the major CBOR types is by these functions:
+
+ CBOR Major Type Public Function
+ 0 QCBOREncode_AddUInt64()
+ 0, 1 QCBOREncode_AddUInt64(), QCBOREncode_AddInt64()
+ 2, 3 QCBOREncode_AddBuffer(), Also QCBOREncode_OpenMapOrArray(),
+ QCBOREncode_CloseMapOrArray()
+ 4, 5 QCBOREncode_OpenMapOrArray(), QCBOREncode_CloseMapOrArray(),
+ QCBOREncode_OpenMapOrArrayIndefiniteLength(),
+ QCBOREncode_CloseMapOrArrayIndefiniteLength()
+ 6 QCBOREncode_AddTag()
+ 7 QCBOREncode_AddDouble(), QCBOREncode_AddType7()
+
+ Additionally, encoding of decimal fractions and bigfloats is by
+ QCBOREncode_AddExponentAndMantissa()
+*/
+
+/*
Error tracking plan -- Errors are tracked internally and not returned
- until Finish is called. The CBOR errors are in me->uError.
+ until QCBOREncode_Finish is called. The CBOR errors are in me->uError.
UsefulOutBuf also tracks whether the buffer is full or not in its
context. Once either of these errors is set they are never
cleared. Only QCBOREncode_Init() resets them. Or said another way, they must
@@ -191,27 +219,24 @@
structures like array/map nesting resulting in some stack memory
savings.
- Errors returned here fall into three categories:
+ The 8 errors returned here fall into three categories:
Sizes
- QCBOR_ERR_BUFFER_TOO_LARGE -- Encoded output exceeded UINT32_MAX
- QCBOR_ERR_BUFFER_TOO_SMALL -- output buffer too small
-
- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -- Array/map nesting > QCBOR_MAX_ARRAY_NESTING1
- QCBOR_ERR_ARRAY_TOO_LONG -- Too many things added to an array/map
+ QCBOR_ERR_BUFFER_TOO_LARGE -- Encoded output exceeded UINT32_MAX
+ QCBOR_ERR_BUFFER_TOO_SMALL -- Output buffer too small
+ QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -- Nesting > QCBOR_MAX_ARRAY_NESTING1
+ QCBOR_ERR_ARRAY_TOO_LONG -- Too many things added to an array/map
Nesting constructed incorrectly
- QCBOR_ERR_TOO_MANY_CLOSES -- more close calls than opens
- QCBOR_ERR_CLOSE_MISMATCH -- Type of close does not match open
+ QCBOR_ERR_TOO_MANY_CLOSES -- More close calls than opens
+ QCBOR_ERR_CLOSE_MISMATCH -- Type of close does not match open
QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN -- Finish called without enough closes
Would generate not-well-formed CBOR
- QCBOR_ERR_UNSUPPORTED -- Simple type between 24 and 31
+ QCBOR_ERR_UNSUPPORTED -- Simple type between 24 and 31
*/
-
-
/*
Public function for initialization. See header qcbor.h
*/
@@ -223,9 +248,18 @@
}
+/**
+ @brief Encode a data item, the most atomic part of CBOR
+ @param[in,out] me Encoding context including output buffer
+ @param[in] uMajorType One of CBOR_MAJOR_TYPE_XX
+ @param[in] nMinLen Include zero bytes up to this length. If 0 include
+ no zero bytes. Non-zero to encode floats and doubles.
+ @param[in] uNumber The number to encode, the argument.
+ @param[in] uPos The position in the output buffer (which is inside
+ the encoding context) to insert the result. This is
+ usually at the end, an append.
-/*
All CBOR data items have a type and an "argument". The argument is
either the value of the item for integer types, the length of the
content for string, byte, array and map types, a tag for major type
@@ -264,17 +298,15 @@
This function is also used to insert floats and doubles. Before this
function is called the float or double must be copied into a
uint64_t. That is how they are passed in. They are then converted to
- network byte order correctly. The uMinLen param makes sure that even
-
- if all the digits of a half, float or double are 0 it is still
+ network byte order correctly. The uMinLen parameter makes sure that
+ even if all the digits of a half, float or double are 0 it is still
correctly encoded in 2, 4 or 8 bytes.
*/
-
static void InsertEncodedTypeAndNumber(QCBOREncodeContext *me,
- uint8_t uMajorType,
- int nMinLen,
- uint64_t uNumber,
- size_t uPos)
+ uint8_t uMajorType,
+ int nMinLen,
+ uint64_t uNumber,
+ size_t uPos)
{
/*
This code does endian conversion without hton or knowing the
@@ -297,9 +329,9 @@
Code Reviewers: THIS FUNCTION DOES POINTER MATH
*/
- // Holds up to 9 bytes of type and argument
- // plus one extra so pointer always points to
- // valid bytes.
+
+ // Holds up to 9 bytes of type and argument plus one extra so pointer
+ // always points to valid bytes.
uint8_t bytes[sizeof(uint64_t)+2];
// Point to the last bytes and work backwards
uint8_t *pByte = &bytes[sizeof(bytes)-1];
@@ -320,15 +352,13 @@
uAdditionalInfo = uNumber;
} else {
/*
- Encode argument in 1,2,4 or 8 bytes. Outer loop
- runs once for 1 byte and 4 times for 8 bytes.
- Inner loop runs 1, 2 or 4 times depending on
- outer loop counter. This works backwards taking
- 8 bits off the argument being encoded at a time
- until all bits from uNumber have been encoded
- and the minimum encoding size is reached.
- Minimum encoding size is for floating point
- numbers with zero bytes.
+ Encode argument in 1,2,4 or 8 bytes. Outer loop runs once for 1
+ byte and 4 times for 8 bytes. Inner loop runs 1, 2 or 4 times
+ depending on outer loop counter. This works backwards taking 8
+ bits off the argument being encoded at a time until all bits
+ from uNumber have been encoded and the minimum encoding size is
+ reached. Minimum encoding size is for floating-point numbers
+ with zero bytes.
*/
static const uint8_t aIterate[] = {1,1,2,4};
uint8_t i;
@@ -340,9 +370,8 @@
}
nMinLen -= uIterations;
}
- // Additional info is the encoding of the
- // number of additional bytes to encode
- // argument.
+ // Additional info is the encoding of the number of additional
+ // bytes to encode argument.
uAdditionalInfo = LEN_IS_ONE_BYTE-1 + i;
}
*--pByte = (uMajorType << 5) + uAdditionalInfo;
@@ -354,7 +383,7 @@
/*
Append the type and number info to the end of the buffer.
- See InsertEncodedTypeAndNumber() function above for details
+ See InsertEncodedTypeAndNumber() function above for details.
*/
inline static void AppendEncodedTypeAndNumber(QCBOREncodeContext *me,
uint8_t uMajorType,
@@ -372,7 +401,7 @@
/*
- Public functions for closing arrays and maps. See header qcbor.h
+ Public functions for closing arrays and maps. See qcbor.h
*/
void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue)
{
@@ -384,7 +413,7 @@
/*
- Public functions for closing arrays and maps. See header qcbor.h
+ Public functions for closing arrays and maps. See qcbor.h
*/
void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum)
{
@@ -411,7 +440,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 qcbor.h
Does the work of adding actual strings bytes to the CBOR output (as
opposed to numbers and opening / closing aggregate types).
@@ -451,7 +480,7 @@
/*
- Public functions for closing arrays and maps. See header qcbor.h
+ Public functions for closing arrays and maps. See qcbor.h
*/
void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag)
{
@@ -471,13 +500,14 @@
if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) {
me->uError = QCBOR_ERR_UNSUPPORTED;
} else {
- // This function call takes care of endian swapping for the float / double
+ // This call takes care of endian swapping for the float / double
InsertEncodedTypeAndNumber(me,
// The major type for floats and doubles
CBOR_MAJOR_TYPE_SIMPLE,
- // size makes sure floats with zeros encode correctly
+ // Must pass size to ensure floats
+ // with zero bytes encode correctly
(int)uSize,
- // Bytes of the floating point number as a uint
+ // The floating-point number as a uint
uNum,
// end position because this is append
UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
@@ -489,7 +519,7 @@
/*
- Public functions for closing arrays and maps. See header qcbor.h
+ Public functions for closing arrays and maps. See qcbor.h
*/
void QCBOREncode_AddDouble(QCBOREncodeContext *me, double dNum)
{
@@ -513,12 +543,13 @@
int64_t nMantissa,
int64_t nExponent)
{
-
- // This is for encoding either a big float or a decimal fraction,
- // both of which are an array of two items, an exponent and a
- // mantissa. The difference between the two is that the exponent is
- // base-2 for big floats and base-10 for decimal fractions, but that
- // has no effect on the code here.
+ /*
+ This is for encoding either a big float or a decimal fraction,
+ both of which are an array of two items, an exponent and a
+ mantissa. The difference between the two is that the exponent is
+ base-2 for big floats and base-10 for decimal fractions, but that
+ has no effect on the code here.
+ */
QCBOREncode_AddTag(pMe, uTag);
QCBOREncode_OpenArray(pMe);
QCBOREncode_AddInt64(pMe, nExponent);
@@ -547,25 +578,30 @@
// Add one item to the nesting level we are in for the new map or array
me->uError = Nesting_Increment(&(me->nesting));
if(me->uError == QCBOR_SUCCESS) {
- // The offset where the length of an array or map will get written
- // is stored in a uint32_t, not a size_t to keep stack usage smaller. This
- // checks to be sure there is no wrap around when recording the offset.
- // Note that on 64-bit machines CBOR larger than 4GB can be encoded as long as no
- // array / map offsets occur past the 4GB mark, but the public interface
- // says that the maximum is 4GB to keep the discussion simpler.
+ /*
+ The offset where the length of an array or map will get written
+ is stored in a uint32_t, not a size_t to keep stack usage
+ smaller. This checks to be sure there is no wrap around when
+ recording the offset. Note that on 64-bit machines CBOR larger
+ than 4GB can be encoded as long as no array / map offsets occur
+ past the 4GB mark, but the public interface says that the
+ maximum is 4GB to keep the discussion simpler.
+ */
size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
- // QCBOR_MAX_ARRAY_OFFSET is slightly less than UINT32_MAX so this
- // code can run on a 32-bit machine and tests can pass on a 32-bit
- // machine. If it was exactly UINT32_MAX, then this code would
- // not compile or run on a 32-bit machine and an #ifdef or some
- // machine size detection would be needed reducing portability.
+ /*
+ QCBOR_MAX_ARRAY_OFFSET is slightly less than UINT32_MAX so this
+ code can run on a 32-bit machine and tests can pass on a 32-bit
+ machine. If it was exactly UINT32_MAX, then this code would not
+ compile or run on a 32-bit machine and an #ifdef or some
+ machine size detection would be needed reducing portability.
+ */
if(uEndPosition >= QCBOR_MAX_ARRAY_OFFSET) {
me->uError = QCBOR_ERR_BUFFER_TOO_LARGE;
} else {
- // Increase nesting level because this is a map or array.
- // Cast from size_t to uin32_t is safe because of check above
+ // Increase nesting level because this is a map or array. Cast
+ // from size_t to uin32_t is safe because of check above
me->uError = Nesting_Increase(&(me->nesting), uMajorType, (uint32_t)uEndPosition);
}
}
@@ -576,7 +612,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.h
*/
void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType)
{
@@ -586,8 +622,9 @@
QCBOREncode_OpenMapOrArray(me, uMajorType);
}
+
/*
- Public functions for closing arrays and maps. See header qcbor.h
+ Public functions for closing arrays and maps. See qcbor.h
*/
void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *me,
uint8_t uMajorType,
@@ -599,36 +636,46 @@
} else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) {
me->uError = QCBOR_ERR_CLOSE_MISMATCH;
} else {
- // When the array, map or bstr wrap was started, nothing was done
- // except note the position of the start of it. This code goes back
- // and inserts the actual CBOR array, map or bstr and its length.
- // That means all the data that is in the array, map or wrapped
- // needs to be slid to the right. This is done by UsefulOutBuf's
- // insert function that is called from inside
- // InsertEncodedTypeAndNumber()
- const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting));
- const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
- // This can't go negative because the UsefulOutBuf always only grows
- // and never shrinks. UsefulOutBut itself also has defenses such that
- // it won't write were it should not even if given hostile input lengths
+ /*
+ When the array, map or bstr wrap was started, nothing was
+ gone except note the position of the start of it. This code
+ goes back and inserts the actual CBOR array, map or bstr and
+ its length. That means all the data that is in the array,
+ map or wrapped needs to be slid to the right. This is done
+ by UsefulOutBuf's insert function that is called from inside
+ InsertEncodedTypeAndNumber()
+ */
+ const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting));
+ const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
+ /*
+ This can't go negative because the UsefulOutBuf always only
+ grows and never shrinks. UsefulOutBut itself also has
+ defenses such that it won't write were it should not even if
+ given hostile input lengths
+ */
const size_t uLenOfEncodedMapOrArray = uEndPosition - uInsertPosition;
- // Length is number of bytes for a bstr and number of items a for map & array
- const size_t uLength = uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING ?
- uLenOfEncodedMapOrArray : Nesting_GetCount(&(me->nesting));
+ // Number of bytes for a bstr or number of items a for map & array
+ const bool bIsBstr = uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING;
+ const size_t uLength = bIsBstr ? uLenOfEncodedMapOrArray
+ : Nesting_GetCount(&(me->nesting));
// Actually insert
InsertEncodedTypeAndNumber(me,
- uMajorType, // major type bstr, array or map
- 0, // no minimum length for encoding
- uLength, // either len of bstr or num map / array items
+ uMajorType, // bstr, array or map
+ 0, // no minimum length
+ uLength, // either len of bstr or
+ // num map / array items
uInsertPosition); // position in out buffer
- // Return pointer and length to the enclosed encoded CBOR. The intended
- // use is for it to be hashed (e.g., SHA-256) in a COSE implementation.
- // This must be used right away, as the pointer and length go invalid
- // on any subsequent calls to this function because there might be calls to
- // InsertEncodedTypeAndNumber() that slides data to the right.
+ /*
+ Return pointer and length to the enclosed encoded CBOR. The
+ intended use is for it to be hashed (e.g., SHA-256) in a
+ COSE implementation. This must be used right away, as the
+ pointer and length go invalid on any subsequent calls to
+ this function because there might be calls to
+ InsertEncodedTypeAndNumber() that slides data to the right.
+ */
if(pWrappedCBOR) {
const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
*pWrappedCBOR = UsefulBuf_Tail(PartialResult, uInsertPosition);
@@ -638,10 +685,13 @@
}
}
+
/*
- Public functions for closing arrays and maps. See header qcbor.h
+ Public functions for closing arrays and maps. See qcbor.h
*/
-void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC *pWrappedCBOR)
+void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *me,
+ uint8_t uMajorType,
+ UsefulBufC *pWrappedCBOR)
{
if(me->uError == QCBOR_SUCCESS) {
if(!Nesting_IsInNest(&(me->nesting))) {
@@ -650,13 +700,20 @@
me->uError = QCBOR_ERR_CLOSE_MISMATCH;
} else {
// insert the break marker (0xff for both arrays and maps)
- InsertEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_SIMPLE, 0, CBOR_SIMPLE_BREAK, UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
+ InsertEncodedTypeAndNumber(me,
+ CBOR_MAJOR_TYPE_SIMPLE,
+ 0,
+ CBOR_SIMPLE_BREAK,
+ UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
- // Return pointer and length to the enclosed encoded CBOR. The intended
- // use is for it to be hashed (e.g., SHA-256) in a COSE implementation.
- // This must be used right away, as the pointer and length go invalid
- // on any subsequent calls to this function because there might be calls to
- // InsertEncodedTypeAndNumber() that slides data to the right.
+ /*
+ Return pointer and length to the enclosed encoded CBOR. The
+ intended use is for it to be hashed (e.g., SHA-256) in a
+ COSE implementation. This must be used right away, as the
+ pointer and length go invalid on any subsequent calls to
+ this function because there might be calls to
+ InsertEncodedTypeAndNumber() that slides data to the right.
+ */
if(pWrappedCBOR) {
const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
*pWrappedCBOR = UsefulBuf_Tail(PartialResult, UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
@@ -670,7 +727,7 @@
/*
- Public functions to finish and get the encoded result. See header qcbor.h
+ Public functions to finish and get the encoded result. See qcbor.h
*/
QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR)
{
@@ -694,7 +751,7 @@
/*
- Public functions to finish and get the encoded result. See header qcbor.h
+ Public functions to finish and get the encoded result. See qcbor.h
*/
QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *me, size_t *puEncodedLen)
{
@@ -713,16 +770,6 @@
/*
- Notes on the code
-
- CBOR Major Type Public Function
- 0 QCBOREncode_AddUInt64
- 0, 1 QCBOREncode_AddUInt64, QCBOREncode_AddInt64
- 2, 3 QCBOREncode_AddBuffer, Also QCBOREncode_OpenMapOrArray
- 4, 5 QCBOREncode_OpenMapOrArray
- 6 QCBOREncode_AddTag
- 7 QCBOREncode_AddDouble, QCBOREncode_AddType7
-
Object code sizes on X86 with LLVM compiler and -Os (Dec 30, 2018)
_QCBOREncode_Init 69
diff --git a/test/UsefulBuf_Tests.c b/test/UsefulBuf_Tests.c
index f53693a..d8ccbac 100644
--- a/test/UsefulBuf_Tests.c
+++ b/test/UsefulBuf_Tests.c
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,7 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ =============================================================================*/
#include "UsefulBuf.h"
@@ -79,7 +79,8 @@
UsefulBufC UBC2 = {"unbounce ", 9};
UsefulOutBuf_InsertUsefulBuf(&UOB, UBC2, 10);
- // Make it a null terminated string (because all the appends and inserts above not strcpy !)
+ // Make it a null terminated string (because all the appends and
+ // inserts above not strcpy !)
UsefulOutBuf_AppendByte(&UOB, '\0');
@@ -160,7 +161,8 @@
- around MAX size_t
- Test these for the buffer size and the cursor, the insert amount, the append amount and the insert position
+ Test these for the buffer size and the cursor, the insert amount, the
+ append amount and the insert position
*/
diff --git a/test/float_tests.c b/test/float_tests.c
index eaf75aa..10ac012 100644
--- a/test/float_tests.c
+++ b/test/float_tests.c
@@ -1,14 +1,14 @@
/*==============================================================================
float_tests.c -- tests for float and conversion to/from half-precision
- Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
See BSD-3-Clause license in README.md
Created on 9/19/18
- ==============================================================================*/
+ =============================================================================*/
#include "float_tests.h"
#include "qcbor.h"
@@ -26,7 +26,8 @@
0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79,
0xF9, 0x7C, 0x00, // Infinity
0x73,
- 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79,
+ 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x69, 0x6E,
+ 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79,
0xF9, 0xFC, 0x00, // -Inifinity
0x63,
0x4E, 0x61, 0x4E,
@@ -38,21 +39,29 @@
0x6F, 0x6E, 0x65, 0x20, 0x74, 0x68, 0x69, 0x72, 0x64,
0xF9, 0x35, 0x55, // 0.333251953125
0x76,
- 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E,
+ 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, 0x6C,
+ 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E,
0xF9, 0x7B, 0xFF, // 65504.0
- 0x78, 0x18, 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x68, 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E,
+ 0x78, 0x18,
+ 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x68,
+ 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69,
+ 0x6F, 0x6E,
0xF9, 0x7C, 0x00, // Infinity
0x72,
- 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
+ 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75,
+ 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
0xF9, 0x00, 0x01, // 0.000000059604
0x6F,
- 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
+ 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F,
+ 0x72, 0x6D, 0x61, 0x6C,
0xF9, 0x03, 0xFF, // 0.0000609755516
0x71,
- 0x62, 0x69, 0x67, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
+ 0x62, 0x69, 0x67, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, 0x62,
+ 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
0xF9, 0x04, 0x00, // 0.000061988
0x70,
- 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65,
+ 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x20, 0x73,
+ 0x69, 0x6E, 0x67, 0x6C, 0x65,
0xF9, 0x00, 0x00,
0x03,
0xF9, 0xC0, 0x00, // -2
@@ -97,7 +106,9 @@
return -4;
}
- QCBORDecode_GetNext(&DC, &Item); // TODO, is this really converting right? It is carrying payload, but this confuses things.
+ // TODO, is this really converting right? It is carrying payload, but
+ // this confuses things.
+ QCBORDecode_GetNext(&DC, &Item);
if(Item.uDataType != QCBOR_TYPE_DOUBLE || !isnan(Item.val.dfnum)) {
return -5;
}
@@ -149,19 +160,23 @@
// TODO: double check these four tests
QCBORDecode_GetNext(&DC, &Item); // qNaN
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff8000000000000ULL) {
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
+ UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff8000000000000ULL) {
return -15;
}
QCBORDecode_GetNext(&DC, &Item); // sNaN
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff0000000000001ULL) {
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
+ UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff0000000000001ULL) {
return -16;
}
QCBORDecode_GetNext(&DC, &Item); // qNaN with payload 0x0f
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff800000000000fULL) {
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
+ UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff800000000000fULL) {
return -17;
}
QCBORDecode_GetNext(&DC, &Item); // sNaN with payload 0x0f
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff000000000000fULL) {
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
+ UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff000000000000fULL) {
return -18;
}
@@ -192,7 +207,8 @@
UsefulOutBuf_AppendByte(&UOB, uHalfPrecInitialByte); // The initial byte for a half-precision float
UsefulOutBuf_AppendUint16(&UOB, (uint16_t)uHalfP);
- // Now parse the hand-constructed CBOR. This will invoke the conversion to a float
+ // Now parse the hand-constructed CBOR. This will invoke the
+ // conversion to a float
QCBORDecodeContext DC;
QCBORDecode_Init(&DC, UsefulOutBuf_OutUBuf(&UOB), 0);
@@ -203,7 +219,8 @@
return -1;
}
- //printf("%04x QCBOR:%15.15f RFC: %15.15f (%8x)\n", uHalfP,Item.val.fnum, d , UsefulBufUtil_CopyFloatToUint32(d));
+ //printf("%04x QCBOR:%15.15f RFC: %15.15f (%8x)\n",
+ // uHalfP, Item.val.fnum, d , UsefulBufUtil_CopyFloatToUint32(d));
if(isnan(d)) {
// The RFC code uses the native instructions which may or may not
@@ -340,16 +357,17 @@
QCBOREncode_AddDoubleAsSmallestToMap(&EC, "one third", 0.333251953125);
// 76 # text(22)
- // 6C6172676573742068616C662D707265636973696F6E # "largest half-precision"
+ // 6C6172676573742068616C662D707265636973696F6E # "largest half-precision"
// F9 7BFF # primitive(31743)
QCBOREncode_AddDoubleAsSmallestToMap(&EC, "largest half-precision",65504.0);
// 76 # text(22)
- // 6C6172676573742068616C662D707265636973696F6E # "largest half-precision"
+ // 6C6172676573742068616C662D707265636973696F6E # "largest half-precision"
// F9 7BFF # primitive(31743)
QCBOREncode_AddDoubleAsSmallestToMap(&EC, "largest half-precision point one",65504.1);
- // Float 65536.0F is 0x47800000 in hex. It has an exponent of 16, which is larger than 15, the largest half-precision exponent
+ // Float 65536.0F is 0x47800000 in hex. It has an exponent of 16, which
+ // is larger than 15, the largest half-precision exponent
// 78 18 # text(24)
// 746F6F2D6C617267652068616C662D707265636973696F6E # "too-large half-precision"
// FA 47800000 # primitive(31743)
@@ -395,17 +413,20 @@
// 746F6F2D6C617267652073696E676C6520657870 # "too-large single exp"
// FB 47F8000000000000 # primitive(5185894970917126144)
// (0x01LL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) | ((128LL + DOUBLE_EXPONENT_BIAS) << DOUBLE_EXPONENT_SHIFT);
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "too-large single exp", 5.104235503814077E+38); // Exponent too large for single
+ // Exponent too large for single
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "too-large single exp", 5.104235503814077E+38);
// 66 # text(6)
// 646664666465 # "dfdfde"
// FA 4B800000 # primitive(1266679808)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "biggest single with prec",16777216); // Single with no precision loss
+ // Single with no precision loss
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "biggest single with prec", 16777216);
// 78 18 # text(24)
// 626967676573742073696E676C6520776974682070726563 # "biggest single with prec"
// FA 4B800000 # primitive(1266679808)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "first single with prec loss",16777217); // Double becuase of precision loss
+ // Double becuase of precision loss
+ QCBOREncode_AddDoubleAsSmallestToMap(&EC, "first single with prec loss", 16777217);
// Just a convenient marker when cutting and pasting encoded CBOR
QCBOREncode_AddSZStringToMapN(&EC, 1, "fin");
diff --git a/test/float_tests.h b/test/float_tests.h
index b7174c8..f3acb71 100644
--- a/test/float_tests.h
+++ b/test/float_tests.h
@@ -1,14 +1,14 @@
/*==============================================================================
float_tests.h -- tests for float and conversion to/from half-precision
- Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
See BSD-3-Clause license in README.md
Created on 9/19/18
- ==============================================================================*/
+ =============================================================================*/
#ifndef float_tests_h
#define float_tests_h
diff --git a/test/half_to_double_from_rfc7049.c b/test/half_to_double_from_rfc7049.c
index 6380e51..d1e2f39 100644
--- a/test/half_to_double_from_rfc7049.c
+++ b/test/half_to_double_from_rfc7049.c
@@ -22,7 +22,7 @@
b) the license may be an issue
QCBOR does support half-precision, but rather than using
- floating point math like this, it does it with bit shifting
+ floating-point math like this, it does it with bit shifting
and masking.
This code is here to test that code.
diff --git a/test/half_to_double_from_rfc7049.h b/test/half_to_double_from_rfc7049.h
index 9f69e35..4642f04 100644
--- a/test/half_to_double_from_rfc7049.h
+++ b/test/half_to_double_from_rfc7049.h
@@ -1,14 +1,14 @@
/*==============================================================================
half_to_double_from_rfc7049.h -- interface to IETF float conversion code.
- Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
See BSD-3-Clause license in README.md
Created on 9/23/18
- ==============================================================================*/
+ ============================================================================*/
#ifndef half_to_double_from_rfc7049_h
#define half_to_double_from_rfc7049_h
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index f0a614d..8c2f093 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,7 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ =============================================================================*/
#include "qcbor_decode_tests.h"
#include "qcbor.h"
@@ -55,11 +55,6 @@
fflush(stdout);
}
-
-/*static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
-{
- PrintUsefulBufC(szLabel, (UsefulBufC){pEncoded, nLen});
-}*/
#endif
@@ -104,7 +99,7 @@
if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
return nCBORError;
- if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
+ if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix for 32-bit machines
Item.val.int64 != -9223372036854775807LL - 1)
return -1;
@@ -460,8 +455,8 @@
/*
- Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
- and needs to be freed. This is used by several tests.
+ Creates a simple CBOR array and returns it in *pEncoded. The array is
+ malloced and needs to be freed. This is used by several tests.
Two of the inputs can be set. Two other items in the array are fixed.
@@ -542,13 +537,22 @@
0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
0x73 } ;
-static int ParseOrderedArray(const uint8_t *pEncoded, size_t nLen, int64_t *pInt1, int64_t *pInt2, const uint8_t **pBuf3, size_t *pBuf3Len, const uint8_t **pBuf4, size_t *pBuf4Len)
+static int ParseOrderedArray(const uint8_t *pEncoded,
+ size_t nLen,
+ int64_t *pInt1,
+ int64_t *pInt2,
+ const uint8_t **pBuf3,
+ size_t *pBuf3Len,
+ const uint8_t **pBuf4,
+ size_t *pBuf4Len)
{
QCBORDecodeContext DCtx;
QCBORItem Item;
int nReturn = -1; // assume error until success
- QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ (UsefulBufC){pEncoded, nLen},
+ QCBOR_DECODE_MODE_NORMAL);
// Make sure the first thing is a map
if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
@@ -641,7 +645,9 @@
QCBORDecodeContext DCtx;
QCBORItem Item;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
+ QCBOR_DECODE_MODE_NORMAL);
// Array with 3 items
if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
@@ -759,7 +765,8 @@
}
-static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
+static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
+ 0x81, 0x81, 0x81, 0x80};
int ParseDeepArrayTest()
{
@@ -767,7 +774,9 @@
int nReturn = 0;
int i;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
+ QCBOR_DECODE_MODE_NORMAL);
for(i = 0; i < 10; i++) {
QCBORItem Item;
@@ -784,10 +793,10 @@
}
// Big enough to test nesting to the depth of 24
-static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
- 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
- 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
- 0x80};
+static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
+ 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
+ 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
+ 0x81, 0x81, 0x81, 0x80};
int ParseTooDeepArrayTest()
{
@@ -797,7 +806,9 @@
QCBORItem Item;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
+ QCBOR_DECODE_MODE_NORMAL);
for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
@@ -825,7 +836,9 @@
for(int nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
QCBORDecodeContext DCtx;
- QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, nNum}, QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ (UsefulBufC){spExpectedEncodedInts, nNum},
+ QCBOR_DECODE_MODE_NORMAL);
const QCBORError nErr = IntegerValuesParseTestInternal(&DCtx);
@@ -876,7 +889,9 @@
QCBORItem Item;
int nCBORError;
- QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
+ QCBORDecode_Init(&DCtx,
+ (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
+ nMode);
if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
return nCBORError;
@@ -1001,7 +1016,9 @@
QCBORItem Item;
int nCBORError;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
+ QCBOR_DECODE_MODE_MAP_AS_ARRAY);
if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
return nCBORError;
@@ -1208,7 +1225,9 @@
QCBORItem Item;
int nCBORError;
- QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
+ QCBOR_DECODE_MODE_NORMAL);
if(nLevel < 1) {
if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
@@ -1415,7 +1434,9 @@
}
-static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
+static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
+ 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
+ 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
int ParseSimpleTest()
{
@@ -1424,7 +1445,9 @@
int nCBORError;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
+ QCBOR_DECODE_MODE_NORMAL);
if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
@@ -1518,7 +1541,8 @@
const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
- // Set up decoder context. String allocator needed for indefinite string test cases
+ // Set up decoder context. String allocator needed for indefinite
+ // string test cases
QCBORDecodeContext DCtx;
QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
UsefulBuf_MAKE_STACK_UB(Pool, 100);
@@ -1653,9 +1677,11 @@
// Deeply nested indefinite length arrays with deepest one unclosed
{ {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
// Mixed nesting with indefinite unclosed
- { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK }, // TODO: think through this one
+ // TODO: think through this one
+ { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK },
// Mixed nesting with definite unclosed
- { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, // TODO: think through this one
+ // TODO: think through this one
+ { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
// The "argument" for the data item is incomplete
@@ -1852,10 +1878,14 @@
// In addition to not-well-formed, some invalid CBOR
- { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
- { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
- { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
- { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // big num tagged an int, not a byte string
+ // Text-based date, with an integer
+ { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
+ // Epoch date, with an byte string
+ { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
+ // tagged as both epoch and string dates
+ { {(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 },
};
int DecodeFailureTests()
@@ -2014,7 +2044,9 @@
QCBORItem Item;
int nCBORError;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
+ QCBOR_DECODE_MODE_NORMAL);
const uint64_t uTags[] = {15};
QCBORTagListIn TagList = {1, uTags};
@@ -2080,7 +2112,8 @@
Item.val.epochDate.nSeconds == 0) {
return -12;
}
- // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
+ // TODO: could use a few more tests with float, double, and half precsion
+ // and negative (but coverage is still pretty good)
return 0;
}
@@ -2096,7 +2129,8 @@
DB 9192939495969798 # tag(10489608748473423768)
80 # array(0)
*/
-static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
+static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
+ 0x96, 0x97, 0x98, 0x80};
/*
DB 9192939495969798 # tag(10489608748473423768)
@@ -2105,7 +2139,8 @@
C7 # tag(7)
80 # array(0)
*/
-static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
+static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
+ 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
/*
The cbor.me parse of this.
@@ -2158,7 +2193,9 @@
QCBORDecodeContext DCtx;
QCBORItem Item;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
+ QCBOR_DECODE_MODE_NORMAL);
//-------------------------
// This text matches the magic number tag and the fraction tag
@@ -2189,7 +2226,9 @@
// --------------------------------
// This test decodes the very large tag, but it is not in
// any list so it is ignored.
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
+ QCBOR_DECODE_MODE_NORMAL);
if(QCBORDecode_GetNext(&DCtx, &Item)) {
return -6;
}
@@ -2198,8 +2237,11 @@
}
// ----------------------------------
- // This test sets up a caller-config list that includes the very large tage and then matches it.
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
+ // This test sets up a caller-config list that includes the very large
+ // tage and then matches it.
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
+ QCBOR_DECODE_MODE_NORMAL);
const uint64_t puList[] = {0x9192939495969798, 257};
const QCBORTagListIn TL = {2, puList};
QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
@@ -2216,10 +2258,12 @@
}
//------------------------
- // This test sets up a caller-configured list, and looks up something not in it
+ // Sets up a caller-configured list and look up something not in it
const uint64_t puLongList[17] = {1,2,1};
const QCBORTagListIn TLLong = {17, puLongList};
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
+ QCBOR_DECODE_MODE_NORMAL);
QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
if(QCBORDecode_GetNext(&DCtx, &Item)) {
return -11;
@@ -2227,7 +2271,9 @@
// -----------------------
// This tests retrievel of the full tag list
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
+ QCBOR_DECODE_MODE_NORMAL);
uint64_t puTags[16];
QCBORTagListOut Out = {0, 4, puTags};
if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
@@ -2242,7 +2288,9 @@
// ----------------------
// This text if too small of an out list
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
+ QCBOR_DECODE_MODE_NORMAL);
QCBORTagListOut OutSmall = {0, 3, puTags};
if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
return -14;
@@ -2250,14 +2298,18 @@
// ---------------
// Parse a version of the "CSR" that has had a ton of tags randomly inserted
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
+ QCBOR_DECODE_MODE_NORMAL);
int n = CheckCSRMaps(&DCtx);
if(n) {
return n-2000;
}
Out = (QCBORTagListOut){0, 16, puTags};
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
+ QCBOR_DECODE_MODE_NORMAL);
const uint64_t puTagList[] = {773, 1, 90599561};
const QCBORTagListIn TagList = {3, puTagList};
@@ -2462,7 +2514,9 @@
QCBORItem Item;
int nCBORError;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
+ QCBOR_DECODE_MODE_NORMAL);
//
@@ -2534,7 +2588,12 @@
-static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx, uint8_t uDataType, uint8_t uNestingLevel, uint8_t uNextNest, int64_t nLabel, QCBORItem *pItem)
+static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
+ uint8_t uDataType,
+ uint8_t uNestingLevel,
+ uint8_t uNextNest,
+ int64_t nLabel,
+ QCBORItem *pItem)
{
QCBORItem Item;
int nCBORError;
@@ -2542,7 +2601,10 @@
if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
if(Item.uDataType != uDataType) return -1;
if(uNestingLevel > 0) {
- if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
+ if(Item.uLabelType != QCBOR_TYPE_INT64 &&
+ Item.uLabelType != QCBOR_TYPE_UINT64) {
+ return -1;
+ }
if(Item.uLabelType == QCBOR_TYPE_INT64) {
if(Item.label.int64 != nLabel) return -1;
} else {
@@ -2629,7 +2691,9 @@
{
QCBORDecodeContext DCtx;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
+ QCBOR_DECODE_MODE_NORMAL);
return CheckCSRMaps(&DCtx);
}
@@ -2640,7 +2704,9 @@
{
QCBORDecodeContext DCtx;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
+ QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
QCBORItem Item;
QCBORError nCBORError;
@@ -2678,7 +2744,9 @@
{
QCBORDecodeContext DCtx;
- QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DCtx,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
+ QCBOR_DECODE_MODE_NORMAL);
return CheckCSRMaps(&DCtx);
}
@@ -2751,13 +2819,18 @@
}
-
-static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
-static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
-static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
-static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
-static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
-static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
+// [1, [2, 3]]
+static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
+// No closing break
+static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
+// Not enough closing breaks
+static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
+// Too many closing breaks
+static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
+// Unclosed indeflen inside def len
+static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
+// confused tag
+static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
int IndefiniteLengthArrayMapTest()
{
@@ -3030,7 +3103,9 @@
}
// ----- types mismatch ---
- QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DC,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
+ QCBOR_DECODE_MODE_NORMAL);
if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
return -7;
@@ -3048,7 +3123,9 @@
}
// ----- not a string ---
- QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DC,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
+ QCBOR_DECODE_MODE_NORMAL);
if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
return -11;
@@ -3066,7 +3143,9 @@
}
// ----- no end -----
- QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DC,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
+ QCBOR_DECODE_MODE_NORMAL);
if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
return -15;
@@ -3107,7 +3186,8 @@
UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
- UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
+ // 80 is big enough for MemPool overhead, but not BigIndefBStr
+ UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
@@ -3164,7 +3244,8 @@
if(QCBORDecode_GetNext(&DC, &Item)){
return -32;
}
- if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
+ if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
+ Item.uDataType != QCBOR_TYPE_INT64 ||
Item.uDataAlloc || !Item.uLabelAlloc ||
UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
return -33;
@@ -3185,7 +3266,9 @@
// First test, use the "CSRMap" as easy input and checking
- QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DC,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
+ QCBOR_DECODE_MODE_NORMAL);
UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
@@ -3257,7 +3340,9 @@
// Next parse with a pool that is too small
UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
- QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
+ QCBORDecode_Init(&DC,
+ UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
+ QCBOR_DECODE_MODE_NORMAL);
QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
return -8;
diff --git a/test/qcbor_decode_tests.h b/test/qcbor_decode_tests.h
index f0d1d9a..2c556b8 100644
--- a/test/qcbor_decode_tests.h
+++ b/test/qcbor_decode_tests.h
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,7 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ ============================================================================*/
#ifndef __QCBOR__qcbort_decode_tests__
#define __QCBOR__qcbort_decode_tests__
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index ab4fd7a..9a7aecf 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,7 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ =============================================================================*/
#include "qcbor.h"
#include "qcbor_encode_tests.h"
@@ -47,11 +47,12 @@
*/
-//#define PRINT_FUNCTIONS_FOR_DEBUGGINGXX
+//#define PRINT_FUNCTIONS_FOR_DEBUGGING
-#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
+#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
#include <stdio.h>
+#if 0
// ifdef these out to not have compiler warnings
static void printencoded(const uint8_t *pEncoded, size_t nLen)
{
@@ -64,6 +65,7 @@
fflush(stdout);
}
+#endif
// Do the comparison and print out where it fails
@@ -71,7 +73,10 @@
size_t i;
for(i = 0; i < U1.len; i++) {
if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
- printf("Position: %d Actual: 0x%x Expected: 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
+ printf("Position: %d Actual: 0x%x Expected: 0x%x\n",
+ (uint32_t)i,
+ ((uint8_t *)U1.ptr)[i],
+ ((uint8_t *)U2.ptr)[i]);
return 1;
}
}
@@ -104,7 +109,10 @@
size_t uOffset;
};
-static int32_t UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual, UsefulBufC Expected, struct UBCompareDiagnostic *pDiag) {
+static int32_t
+UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual,
+ UsefulBufC Expected,
+ struct UBCompareDiagnostic *pDiag) {
size_t i;
for(i = 0; i < Actual.len; i++) {
if(((uint8_t *)Actual.ptr)[i] != ((uint8_t *)Expected.ptr)[i]) {
@@ -113,7 +121,9 @@
pDiag->uExpected = ((uint8_t *)Expected.ptr)[i];
pDiag->uOffset = i;
}
- return (int32_t)i + 1000000; // Cast to int is OK as this is only a diagnostic and the sizes here are never over a few KB.
+ // Cast to int is OK as this is only a diagnostic and the sizes
+ // here are never over a few KB.
+ return (int32_t)i + 1000000;
}
}
return 0;
@@ -169,7 +179,8 @@
}
- // Make another encoded message with the CBOR from the previous put into this one
+ // Make another encoded message with the CBOR from the previous
+ // put into this one
UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
QCBOREncode_Init(&EC, MemoryForEncoded2);
QCBOREncode_OpenArray(&EC);
@@ -246,7 +257,10 @@
// 2 1:1 2:1 3:1
QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1 || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != -70000) {
+ if(Item.uDataType != QCBOR_TYPE_MAP ||
+ Item.val.uCount != 1 ||
+ Item.uLabelType != QCBOR_TYPE_INT64 ||
+ Item.label.int64 != -70000) {
return -11;
}
@@ -499,7 +513,8 @@
int AllAddMethodsTest()
{
- // TODO: this test should be broken down into several so it is more managable. Tags and labels could be more sensible
+ // TODO: this test should be broken down into several so it is more
+ // managable. Tags and labels could be more sensible
QCBOREncodeContext ECtx;
int nReturn = 0;
@@ -507,7 +522,8 @@
QCBOREncode_OpenArray(&ECtx);
- // Some ints that are tagged and have strings preceeding them (not labels becase it is not a map)
+ // Some ints that are tagged and have strings preceeding them
+ // (not labels becase it is not a map)
QCBOREncode_AddSZString(&ECtx, "UINT62");
QCBOREncode_AddTag(&ECtx, 100);
QCBOREncode_AddUInt64(&ECtx, 89989909);
@@ -550,7 +566,9 @@
// text blobs
QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
QCBOREncode_AddSZString(&ECtx, "oof\n");
- QCBOREncode_AddURI(&ECtx, UsefulBuf_FROM_SZ_LITERAL("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"));
+ const char *szURL =
+ "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
+ QCBOREncode_AddURI(&ECtx, UsefulBuf_FromSZ(szURL));
QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
@@ -565,7 +583,9 @@
QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
- QCBOREncode_AddURIToMap(&ECtx, "RFC", UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
+ QCBOREncode_AddURIToMap(&ECtx,
+ "RFC",
+ UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
@@ -639,7 +659,9 @@
QCBOREncode_CloseMap(&ECtx);
// UUIDs
- static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
+ static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
+ 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
+ 0x49, 0x43, 0x41, 0x32};
const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
QCBOREncode_OpenMap(&ECtx);
@@ -1520,11 +1542,13 @@
/*
@brief Encode the RTIC results
- @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
- @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
+ @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
+ CBOR_SIMPLEV_NULL
+ @param[in] time Time stamp in UNIX epoch time or 0 for none
@param[in] szAlexString Diagnostic code.
@param[in[ pOut Buffer to put the result in
- @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
+ @param[in/out] pnLen Size of pOut buffer when called; length of data
+ output in buffer on return
@return
One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
@@ -1538,11 +1562,17 @@
*/
-static UsefulBufC FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, UsefulBuf Storage)
+static UsefulBufC
+FormatRTICResults(int nRResult,
+ uint64_t time,
+ const char *szType,
+ const char *szAlexString,
+ UsefulBuf Storage)
{
// Buffer that the result will be written in to
// It is fixed size and small that a stack variable will be fine
- // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
+ // QCBOREncode will never write off the end of this buffer. If it won't
+ // fit QCBOREncode_Finish will return an error.
// Context for the encoder
QCBOREncodeContext ECtx;
@@ -1554,7 +1584,8 @@
{ // Brace / indention just to show CBOR encoding nesting
- // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
+ // The result: 0 if scan happened and found nothing; 1 if it happened and
+ // found something wrong; 2 if it didn't happen
QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
// Add the diagnostic code
@@ -1721,7 +1752,8 @@
QCBOREncode_CloseArray(&EC);
UsefulBufC BStr;
QCBOREncode_CloseBstrWrap(&EC, &BStr);
- // 3 is one byte for the wrapping bstr, 1 for an array of length 1, and 1 byte for a NULL
+ // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
+ // and 1 byte for a NULL
if(BStr.ptr != NULL || BStr.len != 3) {
return -5;
}
@@ -1747,7 +1779,7 @@
int BstrWrapErrorTest()
{
- // -------------- Test closing a bstrwrap when it is an array that is open -----------
+ // ---- Test closing a bstrwrap when it is an array that is open ---------
QCBOREncodeContext EC;
QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
@@ -1769,7 +1801,7 @@
return -1;
}
- // ----------- test closing a bstrwrap when nothing is open ---------------------
+ // -------- test closing a bstrwrap when nothing is open ----------------
QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
@@ -2204,7 +2236,8 @@
// ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
- // Do all of these tests with NULL buffers so no actual large allocations are neccesary
+ // Do all of these tests with NULL buffers so no actual
+ // large allocations are neccesary
const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
// First verify no error from a big buffer
diff --git a/test/qcbor_encode_tests.h b/test/qcbor_encode_tests.h
index 1cd8e7b..3d11883 100644
--- a/test/qcbor_encode_tests.h
+++ b/test/qcbor_encode_tests.h
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2019, Laurence Lundblade.
+ Copyright (c) 2018-2020, Laurence Lundblade.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,7 @@
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ==============================================================================*/
+ =============================================================================*/
#ifndef __QCBOR__qcbor_encode_tests__
#define __QCBOR__qcbor_encode_tests__
@@ -37,7 +37,8 @@
/*
Notes:
- - All the functions in qcbor.h are called once in the aggregation of all the tests below.
+ - All the functions in qcbor.h are called once in the aggregation of all
+ the tests below.
- All the types that are supported are given as input and parsed by these tests
@@ -54,43 +55,45 @@
/*
- Encode lots of integer values, particularly around the boundary and make sure they
- Match the expected binary output. Primarily an encoding test.
+ Encode lots of integer values, particularly around the boundary and
+ make sure they Match the expected binary output. Primarily an
+ encoding test.
*/
int IntegerValuesTest1(void);
/*
- Create nested arrays to the max depth allowed and make sure it succeeds.
- This is an encoding test.
+ Create nested arrays to the max depth allowed and make sure it
+ succeeds. This is an encoding test.
*/
int ArrayNestingTest1(void);
/*
- Create nested arrays to one more than the meax depth and make sure it fails.
- This is an encoding test.
+ Create nested arrays to one more than the meax depth and make sure it
+ fails. This is an encoding test.
*/
int ArrayNestingTest2(void);
/*
- Encoding test.
- Create arrays to max depth and close one extra time and look for correct error code
+ Encoding test. Create arrays to max depth and close one extra time
+ and look for correct error code
*/
int ArrayNestingTest3(void);
/*
- This tests the QCBOREncode_AddRaw() function by adding two chunks or RAWCBOR to an
- array and comparing with expected values. This is an encoding test.
+ This tests the QCBOREncode_AddRaw() function by adding two chunks or
+ RAWCBOR to an array and comparing with expected values. This is an
+ encoding test.
*/
int EncodeRawTest(void);
/*
- This creates a somewhat complicated CBOR MAP and verifies it against expected
- data. This is an encoding test.
+ This creates a somewhat complicated CBOR MAP and verifies it against
+ expected data. This is an encoding test.
*/
int MapEncodeTest(void);
@@ -112,9 +115,10 @@
*/
int SimpleValuesIndefiniteLengthTest1(void);
+
/*
- Indefinite length arrays and maps use the 'magic' number 31, verify that
- everything with length 31 still works properly
+ Indefinite length arrays and maps use the 'magic' number 31, verify
+ that everything with length 31 still works properly
*/
int EncodeLengthThirtyoneTest(void);
diff --git a/test/run_tests.c b/test/run_tests.c
index aba536d..192e319 100644
--- a/test/run_tests.c
+++ b/test/run_tests.c
@@ -1,14 +1,14 @@
/*==============================================================================
run_tests.c -- test aggregator and results reporting
- Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
See BSD-3-Clause license in README.md
Created on 9/30/18
- ==============================================================================*/
+ =============================================================================*/
#include "run_tests.h"
#include "UsefulBuf.h"
@@ -157,7 +157,10 @@
/*
Public function. See run_test.h.
*/
-int RunTests(const char *szTestNames[], OutputStringCB pfOutput, void *poutCtx, int *pNumTestsRun)
+int RunTests(const char *szTestNames[],
+ OutputStringCB pfOutput,
+ void *poutCtx,
+ int *pNumTestsRun)
{
int nTestsFailed = 0;
int nTestsRun = 0;
@@ -276,7 +279,10 @@
/*
Public function. See run_test.h.
*/
-static void PrintSize(const char *szWhat, uint32_t uSize, OutputStringCB pfOutput, void *pOutCtx)
+static void PrintSize(const char *szWhat,
+ uint32_t uSize,
+ OutputStringCB pfOutput,
+ void *pOutCtx)
{
UsefulBuf_MAKE_STACK_UB(buffer, 20);
@@ -292,13 +298,13 @@
*/
void PrintSizes(OutputStringCB pfOutput, void *pOutCtx)
{
- // Type and size of return from sizeof() varies. These will never be large so cast is safe
- PrintSize("sizeof(QCBORTrackNesting)", (uint32_t)sizeof(QCBORTrackNesting), pfOutput, pOutCtx);
+ // These will never be large so cast is safe
+ PrintSize("sizeof(QCBORTrackNesting)", (uint32_t)sizeof(QCBORTrackNesting), pfOutput, pOutCtx);
PrintSize("sizeof(QCBOREncodeContext)", (uint32_t)sizeof(QCBOREncodeContext), pfOutput, pOutCtx);
PrintSize("sizeof(QCBORDecodeNesting)", (uint32_t)sizeof(QCBORDecodeNesting), pfOutput, pOutCtx);
PrintSize("sizeof(QCBORDecodeContext)", (uint32_t)sizeof(QCBORDecodeContext), pfOutput, pOutCtx);
- PrintSize("sizeof(QCBORItem)", (uint32_t)sizeof(QCBORItem), pfOutput, pOutCtx);
- PrintSize("sizeof(QCBORTagListIn)", (uint32_t)sizeof(QCBORTagListIn), pfOutput, pOutCtx);
- PrintSize("sizeof(QCBORTagListOut)", (uint32_t)sizeof(QCBORTagListOut), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORItem)", (uint32_t)sizeof(QCBORItem), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORTagListIn)", (uint32_t)sizeof(QCBORTagListIn), pfOutput, pOutCtx);
+ PrintSize("sizeof(QCBORTagListOut)", (uint32_t)sizeof(QCBORTagListOut), pfOutput, pOutCtx);
(*pfOutput)("", pOutCtx, 1);
}
diff --git a/test/run_tests.h b/test/run_tests.h
index 734d4f8..97f2946 100644
--- a/test/run_tests.h
+++ b/test/run_tests.h
@@ -1,14 +1,14 @@
/*==============================================================================
run_tests.h -- test aggregator and results reporting
- Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved.
+ Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
See BSD-3-Clause license in README.md
Created 9/30/18
- ==============================================================================*/
+ =============================================================================*/
/**
@file run_tests.h
@@ -53,7 +53,10 @@
@return The number of tests that failed. Zero means overall success.
*/
-int RunTests(const char *szTestNames[], OutputStringCB pfOutput, void *pOutCtx, int *pNumTestsRun);
+int RunTests(const char *szTestNames[],
+ OutputStringCB pfOutput,
+ void *pOutCtx,
+ int *pNumTestsRun);
/**