63 #ifndef __QCBOR__qcbor__ 64 #define __QCBOR__qcbor__ 90 #define QCBOR_MAX_ARRAY_NESTING1 15 // Do not increase this over 255 99 #define QCBOR_MAX_ARRAY_OFFSET (UINT32_MAX - 100) 122 } pArrays[QCBOR_MAX_ARRAY_NESTING1+1],
161 } pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING1+1],
181 uint8_t bStringAllocateAll;
189 void *pStringAllocator;
193 const void *pCallerConfiguredTagList;
198 #define CBOR_MAJOR_NONE_TYPE_RAW 9 199 #define CBOR_MAJOR_NONE_TAG_LABEL_REORDER 10 218 #define CBOR_MAJOR_TYPE_POSITIVE_INT 0 221 #define CBOR_MAJOR_TYPE_NEGATIVE_INT 1 224 #define CBOR_MAJOR_TYPE_BYTE_STRING 2 228 #define CBOR_MAJOR_TYPE_TEXT_STRING 3 231 #define CBOR_MAJOR_TYPE_ARRAY 4 236 #define CBOR_MAJOR_TYPE_MAP 5 239 #define CBOR_MAJOR_TYPE_OPTIONAL 6 242 #define CBOR_MAJOR_TYPE_SIMPLE 7 249 #define LEN_IS_ONE_BYTE 24 250 #define LEN_IS_TWO_BYTES 25 251 #define LEN_IS_FOUR_BYTES 26 252 #define LEN_IS_EIGHT_BYTES 27 253 #define ADDINFO_RESERVED1 28 254 #define ADDINFO_RESERVED2 29 255 #define ADDINFO_RESERVED3 30 256 #define LEN_IS_INDEFINITE 31 263 #define CBOR_TWENTY_FOUR 24 271 #define CBOR_TAG_DATE_STRING 0 273 #define CBOR_TAG_DATE_EPOCH 1 274 #define CBOR_TAG_POS_BIGNUM 2 275 #define CBOR_TAG_NEG_BIGNUM 3 276 #define CBOR_TAG_FRACTION 4 277 #define CBOR_TAG_BIGFLOAT 5 279 #define CBOR_TAG_COSE_ENCRYPTO 16 280 #define CBOR_TAG_COSE_MAC0 17 281 #define CBOR_TAG_COSE_SIGN1 18 284 #define CBOR_TAG_ENC_AS_B64URL 21 286 #define CBOR_TAG_ENC_AS_B64 22 288 #define CBOR_TAG_ENC_AS_B16 23 289 #define CBOR_TAG_CBOR 24 291 #define CBOR_TAG_URI 32 293 #define CBOR_TAG_B64URL 33 295 #define CBOR_TAG_B64 34 297 #define CBOR_TAG_REGEX 35 299 #define CBOR_TAG_MIME 36 301 #define CBOR_TAG_BIN_UUID 37 303 #define CBOR_TAG_CWT 61 305 #define CBOR_TAG_ENCRYPT 96 306 #define CBOR_TAG_MAC 97 307 #define CBOR_TAG_SIGN 98 309 #define CBOR_TAG_GEO_COORD 103 313 #define CBOR_TAG_CBOR_MAGIC 55799 314 #define CBOR_TAG_NONE UINT64_MAX 320 #define CBOR_SIMPLEV_FALSE 20 321 #define CBOR_SIMPLEV_TRUE 21 322 #define CBOR_SIMPLEV_NULL 22 323 #define CBOR_SIMPLEV_UNDEF 23 324 #define CBOR_SIMPLEV_ONEBYTE 24 325 #define HALF_PREC_FLOAT 25 326 #define SINGLE_PREC_FLOAT 26 327 #define DOUBLE_PREC_FLOAT 27 328 #define CBOR_SIMPLE_BREAK 31 536 #define QCBOR_MAX_ITEMS_IN_ARRAY (UINT16_MAX-1) 543 #define QCBOR_MAX_ARRAY_NESTING QCBOR_MAX_ARRAY_NESTING1 549 #define QCBOR_MAX_CUSTOM_TAGS 16 668 #define QCBOR_TYPE_NONE 0 670 #define QCBOR_TYPE_INT64 2 672 #define QCBOR_TYPE_UINT64 3 674 #define QCBOR_TYPE_ARRAY 4 676 #define QCBOR_TYPE_MAP 5 678 #define QCBOR_TYPE_BYTE_STRING 6 680 #define QCBOR_TYPE_TEXT_STRING 7 682 #define QCBOR_TYPE_POSBIGNUM 9 684 #define QCBOR_TYPE_NEGBIGNUM 10 686 #define QCBOR_TYPE_DATE_STRING 11 688 #define QCBOR_TYPE_DATE_EPOCH 12 690 #define QCBOR_TYPE_UKNOWN_SIMPLE 13 692 #define QCBOR_TYPE_FALSE 20 694 #define QCBOR_TYPE_TRUE 21 696 #define QCBOR_TYPE_NULL 22 698 #define QCBOR_TYPE_UNDEF 23 700 #define QCBOR_TYPE_FLOAT 26 702 #define QCBOR_TYPE_DOUBLE 27 704 #define QCBOR_TYPE_MAP_AS_ARRAY 32 706 #define QCBOR_TYPE_BREAK 31 // Used internally; never returned 708 #define QCBOR_TYPE_OPTTAG 254 // Used internally; never returned 741 double fSecondsFraction;
784 void *pAllocaterContext;
785 UsefulBuf (*fAllocate)(
void *pAllocaterContext,
void *pOldMem,
size_t uNewSize);
786 void (*fFree)(
void *pAllocaterContext,
void *pMem);
787 void (*fDestructor)(
void *pAllocaterContext);
799 #define QCBOR_DECODE_MIN_MEM_POOL_SIZE 72 813 const uint64_t *puTags;
832 uint8_t uNumAllocated;
916 static void QCBOREncode_AddInt64ToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, int64_t uNum);
918 static void QCBOREncode_AddInt64ToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum);
937 static void QCBOREncode_AddUInt64ToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, uint64_t uNum);
939 static void QCBOREncode_AddUInt64ToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
980 static void QCBOREncode_AddSZStringToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szString);
982 static void QCBOREncode_AddSZStringToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
const char *szString);
1014 static void QCBOREncode_AddDoubleToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
double dNum);
1016 static void QCBOREncode_AddDoubleToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
double dNum);
1069 static void QCBOREncode_AddDateEpochToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, int64_t date);
1071 static void QCBOREncode_AddDateEpochToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, int64_t date);
1269 static void QCBOREncode_AddDateStringToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szDate);
1271 static void QCBOREncode_AddDateStringToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
const char *szDate);
1286 static void QCBOREncode_AddBoolToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
bool b);
1288 static void QCBOREncode_AddBoolToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
bool b);
1327 static void QCBOREncode_AddUndefToMap(
QCBOREncodeContext *pCtx,
const char *szLabel);
1363 static void QCBOREncode_OpenArrayInMap(
QCBOREncodeContext *pCtx,
const char *szLabel);
1485 static void QCBOREncode_BstrWrapInMap(
QCBOREncodeContext *pCtx,
const char *szLabel);
1981 if(src > INT32_MAX || src < INT32_MIN) {
1984 *dest = (int32_t) src;
1989 static inline int QCBOR_Int64ToInt16(int64_t src, int16_t *dest)
1991 if(src > INT16_MAX || src < INT16_MIN) {
1994 *dest = (int16_t) src;
1999 static inline int QCBOR_Int64ToInt8(int64_t src, int8_t *dest)
2001 if(src > INT8_MAX || src < INT8_MIN) {
2004 *dest = (int8_t) src;
2009 static inline int QCBOR_Int64ToUInt32(int64_t src, uint32_t *dest)
2011 if(src > UINT32_MAX || src < 0) {
2014 *dest = (uint32_t) src;
2019 static inline int QCBOR_Int64UToInt16(int64_t src, uint16_t *dest)
2021 if(src > UINT16_MAX || src < 0) {
2024 *dest = (uint16_t) src;
2029 static inline int QCBOR_Int64ToUInt8(int64_t src, uint8_t *dest)
2031 if(src > UINT8_MAX || src < 0) {
2034 *dest = (uint8_t) src;
2039 static inline int QCBOR_Int64ToUInt64(int64_t src, uint64_t *dest)
2044 *dest = (uint64_t) src;
2124 static inline void QCBOREncode_AddInt64ToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, int64_t uNum)
2130 static inline void QCBOREncode_AddInt64ToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum)
2137 static inline void QCBOREncode_AddUInt64ToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, uint64_t uNum)
2143 static inline void QCBOREncode_AddUInt64ToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum)
2173 static inline void QCBOREncode_AddSZStringToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szString)
2179 static inline void QCBOREncode_AddSZStringToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
const char *szString)
2186 static inline void QCBOREncode_AddDoubleToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
double dNum)
2192 static inline void QCBOREncode_AddDoubleToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
double dNum)
2205 static inline void QCBOREncode_AddDateEpochToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, int64_t date)
2212 static inline void QCBOREncode_AddDateEpochToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, int64_t date)
2413 static inline void QCBOREncode_AddDateStringToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szDate)
2420 static inline void QCBOREncode_AddDateStringToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
const char *szDate)
2433 static inline void QCBOREncode_AddSimpleToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, uint8_t uSimple)
2436 QCBOREncode_AddSimple(pCtx, uSimple);
2439 static inline void QCBOREncode_AddSimpleToMapN(
QCBOREncodeContext *pCtx,
int nLabel, uint8_t uSimple)
2442 QCBOREncode_AddSimple(pCtx, uSimple);
2448 uint8_t uSimple = CBOR_SIMPLEV_FALSE;
2450 uSimple = CBOR_SIMPLEV_TRUE;
2452 QCBOREncode_AddSimple(pCtx, uSimple);
2455 static inline void QCBOREncode_AddBoolToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
bool b)
2461 static inline void QCBOREncode_AddBoolToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
bool b)
2470 QCBOREncode_AddSimple(pCtx, CBOR_SIMPLEV_NULL);
2473 static inline void QCBOREncode_AddNULLToMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2479 static inline void QCBOREncode_AddNULLToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
2488 QCBOREncode_AddSimple(pCtx, CBOR_SIMPLEV_UNDEF);
2491 static inline void QCBOREncode_AddUndefToMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2497 static inline void QCBOREncode_AddUndefToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
2509 static inline void QCBOREncode_OpenArrayInMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2515 static inline void QCBOREncode_OpenArrayInMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
2532 static inline void QCBOREncode_OpenMapInMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2538 static inline void QCBOREncode_OpenMapInMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
2555 static inline void QCBOREncode_BstrWrapInMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2561 static inline void QCBOREncode_BstrWrapInMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pCtx, const QCBORTagListIn *pTagList)
Configure list of caller selected tags to be recognized.
During encoding, the length of the encoded CBOR exceeded UINT32_MAX.
void QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag)
[in] Add an optional tag
QCBORError QCBORDecode_GetNextWithTags(QCBORDecodeContext *pCtx, QCBORItem *pDecodedItem, QCBORTagListOut *pTagList)
Gets the next item including full list of tags for item.
static void QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx, UsefulBufC B64Text)
Add base 64-encoded text to encoded output.
#define CBOR_TAG_URI
The data in the string is a URIs, as defined in RFC3986.
#define CBOR_TAG_REGEX
regular expressions in Perl Compatible Regular Expressions (PCRE) / JavaScript syntax ECMA262...
During encoding or decoding, the number of array or map opens was not matched by the number of closes...
During decoding, hit the end of the given data to decode.
#define CBOR_TAG_DATE_EPOCH
See QCBOREncode_AddDateEpoch_2()
UsefulBufC dateString
The value for uDataType QCBOR_TYPE_DATE_EPOCH.
static void QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx, UsefulBufC Bytes)
Add a positive big number to the encoded output.
static void QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx, int64_t date)
Add an epoch-based date.
static int QCBOR_Int64ToInt32(int64_t src, int32_t *dest)
Convert int64_t to smaller int's safely.
struct _QCBORItem QCBORItem
QCBORItem holds the type, value and other info for a decoded item returned by GetNextItem().
#define CBOR_TAG_MIME
MIME messages (including all headers), as defined in RFC2045.
During decoding, the CBOR is not valid, primarily a simple type is encoded in a prohibited way...
struct useful_buf UsefulBuf
The non-const UsefulBuf typically used for some allocated memory that is to be filled in...
During encoding, more arrays or maps were closed than opened.
void QCBOREncode_AddBuffer(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC Bytes)
Semi-private method to add a buffer full of bytes to encoded output.
uint8_t uLabelAlloc
1 if allocated with string allocator, 0 if not.
uint64_t uTagV
The integer value for unknown simple types.
static void QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx, UsefulBufC Bytes)
Add a negative big number to the encoded output.
Error allocating space for a string, usually for an indefinite length string.
During encoding or decoding, the array or map nesting was deeper than this implementation can handle...
The non-const UsefulBuf typically used for some allocated memory that is to be filled in...
void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType)
Semi-private method to open a map, array or bstr wrapped CBOR.
Unable to decode an indefinite length string because no string allocator was configured.
During decoding, some CBOR construct was encountered that this decoder doesn't support, primarily this is the reserved additional info values, 28 through 30.
Optional tagging that doesn't make sense (an int is tagged as a date string) or can't be handled...
uint8_t uDataAlloc
Tells what element of the label union to use.
static void QCBOREncode_OpenArray(QCBOREncodeContext *pCtx)
Indicates that the next items added are in an array.
static void QCBOREncode_AddRegex(QCBOREncodeContext *pCtx, UsefulBufC Regex)
Add Perl Compatible Regular Expression.
During decoding, an integer smaller than INT64_MIN was received (CBOR can represent integers smaller ...
UsefulBufC and UsefulBuf are simple data structures to hold a pointer and length for a binary data...
int64_t int64
The label for uLabelType QCBOR_TYPE_BYTE_STRING and QCBOR_TYPE_TEXT_STRING.
This is used to tell the decoder about tags that it should record in uTagBits in QCBORItem beyond the...
QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen)
Get the encoded CBOR and error status.
QCBORError QCBORDecode_Finish(QCBORDecodeContext *pCtx)
Check whether all the bytes have been decoded and maps and arrays closed.
QCBORError QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR)
Get the encoded result.
static void QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text)
Add a UTF-8 text string to the encoded output.
static void QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx, UsefulBufC MIMEData)
MIME encoded text to the encoded output.
static void QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx)
Indicate start of encoded CBOR to be wrapped in a bstr.
int QCBORDecode_IsTagged(QCBORDecodeContext *pCtx, const QCBORItem *pItem, uint64_t uTag)
Determine if a CBOR item was tagged with a particular tag.
QCBORItem holds the type, value and other info for a decoded item returned by GetNextItem().
The goal of this code is to make buffer and pointer manipulation easier and safer when working with b...
static void QCBOREncode_CloseMap(QCBOREncodeContext *pCtx)
Close an open map.
UsefulBufC string
The value for uDataType QCBOR_TYPE_UINT64.
static void QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes)
Add a byte string to the encoded output.
static void QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded)
Add some already-encoded CBOR bytes.
During decoding, a date greater than +- 292 billion years from Jan 1 1970 encountered during parsing...
void QCBORDecode_Init(QCBORDecodeContext *pCtx, UsefulBufC EncodedCBOR, QCBORDecodeMode nMode)
Initialize the CBOR decoder context.
During encoding, QCBOREncode_Close() call with a different type than is currently open...
During encoding, the simple value is not between CBOR_SIMPLEV_FALSE and CBOR_SIMPLEV_UNDEF.
void QCBOREncode_AddType7(QCBOREncodeContext *pCtx, size_t uSize, uint64_t uNum)
Semi-private method to add simple types.
static void QCBOREncode_AddNULL(QCBOREncodeContext *pCtx)
Add a NULL to the encoded output.
void QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum)
Add an unsigned 64-bit integer to the encoded output.
During decoding, too many tags in the caller-configured tag list, or not enough space in QCBORTagList...
#define CBOR_TAG_B64URL
The data in the string is a base 64'd URL.
Returned by QCBORDecode_Finish() if all the inputs bytes have not been consumed.
static void QCBOREncode_AddUndef(QCBOREncodeContext *pCtx)
Add an "undef" to the encoded output.
During decoding, the label for a map entry is bad.
static void QCBOREncode_AddDateString(QCBOREncodeContext *pCtx, const char *szDate)
Add an RFC 3339 date string.
QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pCtx, UsefulBuf MemPool, bool bAllStrings)
Set up the MemPool string allocator for indefinite length strings.
uint8_t uLabelType
How deep the nesting from arrays and maps are.
static void QCBOREncode_OpenMap(QCBOREncodeContext *pCtx)
Indicates that the next items added are in a map.
uint8_t uSimple
The value for uDataType QCBOR_TYPE_BIGNUM.
void QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum)
Add a floating-point number to the encoded output.
uint8_t uNextNestLevel
Like uDataAlloc, but for label.
This is a set of functions and pointer context (in object-oriented parlance, an "object") used to all...
UsefulOutBuf is a structure and functions (an object) that are good for serializing data into a buffe...
This is for QCBORDecode_GetNextWithTags() to be able to return the full list of tags on an item...
One of the chunks in an indefinite length string is not of the type of the string.
UsefulBufC bigNum
The value for uDataType QCBOR_TYPE_DATE_STRING.
void QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum)
Add a signed 64-bit integer to the encoded output.
static void QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx, UsefulBufC B64Text)
Add base 64URL -encoded URL to encoded output.
void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC *pWrappedCBOR)
Semi-private method to close a map, array or bstr wrapped CBOR.
The encode or decode completely correctly.
static void QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes)
Add a binary UUID to the encoded output.
static UsefulBufC UsefulBuf_FromSZ(const char *szString)
Convert a NULL terminated string to a UsefulBufC.
static void QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString)
Add a UTF-8 text string to the encoded output.
The buffer provided for the encoded output when doing encoding was too small and the encoded output w...
Returned by QCBORDecode_SetMemPool() when xx is too small.
void QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage)
Initialize the the encoder to prepare to encode some CBOR.
uint16_t uCount
The value for uDataType QCBOR_TYPE_BYTE_STRING and QCBOR_TYPE_TEXT_STRING.
static void QCBOREncode_CloseArray(QCBOREncodeContext *pCtx)
Close an open array.
double dfnum
The "value" for uDataType QCBOR_TYPE_ARRAY or QCBOR_TYPE_MAP – the number of items in the array or m...
QCBORError QCBORDecode_GetNext(QCBORDecodeContext *pCtx, QCBORItem *pDecodedItem)
Gets the next item (integer, byte string, array...) in pre order traversal of CBOR tree...
#define CBOR_TAG_BIN_UUID
Binary UUID.
uint8_t uNestingLevel
Tells what element of the val union to use.
#define CBOR_TAG_B64
The data in the string is base 64'd.
uint64_t uTagBits
Union holding the different label types selected based on uLabelType.
uint64_t uint64
The value for uDataType QCBOR_TYPE_INT64.
static void QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR)
Close a wrapping bstr.
During decoding or encoding, the array or map had too many items in it.
static void QCBOREncode_AddURI(QCBOREncodeContext *pCtx, UsefulBufC URI)
Add a text URI to the encoded output.
#define CBOR_TAG_DATE_STRING
See QCBOREncode_AddDateString() below.
static void QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b)
Add a standard boolean.
During decoding, a break occurred outside an indefinite length item.
void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pCtx, const QCBORStringAllocator *pAllocator, bool bAllStrings)
Sets up a custom string allocator for indefinite length strings.