64 #ifndef __QCBOR__qcbor__ 65 #define __QCBOR__qcbor__ 91 #define QCBOR_MAX_ARRAY_NESTING1 15 // Do not increase this over 255 100 #define QCBOR_MAX_ARRAY_OFFSET (UINT32_MAX - 100) 123 } pArrays[QCBOR_MAX_ARRAY_NESTING1+1],
162 } pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING1+1],
182 uint8_t bStringAllocateAll;
190 void *pStringAllocator;
194 const void *pCallerConfiguredTagList;
199 #define CBOR_MAJOR_NONE_TYPE_RAW 9 200 #define CBOR_MAJOR_NONE_TAG_LABEL_REORDER 10 219 #define CBOR_MAJOR_TYPE_POSITIVE_INT 0 222 #define CBOR_MAJOR_TYPE_NEGATIVE_INT 1 225 #define CBOR_MAJOR_TYPE_BYTE_STRING 2 229 #define CBOR_MAJOR_TYPE_TEXT_STRING 3 232 #define CBOR_MAJOR_TYPE_ARRAY 4 237 #define CBOR_MAJOR_TYPE_MAP 5 240 #define CBOR_MAJOR_TYPE_OPTIONAL 6 243 #define CBOR_MAJOR_TYPE_SIMPLE 7 250 #define LEN_IS_ONE_BYTE 24 251 #define LEN_IS_TWO_BYTES 25 252 #define LEN_IS_FOUR_BYTES 26 253 #define LEN_IS_EIGHT_BYTES 27 254 #define ADDINFO_RESERVED1 28 255 #define ADDINFO_RESERVED2 29 256 #define ADDINFO_RESERVED3 30 257 #define LEN_IS_INDEFINITE 31 264 #define CBOR_TWENTY_FOUR 24 272 #define CBOR_TAG_DATE_STRING 0 274 #define CBOR_TAG_DATE_EPOCH 1 275 #define CBOR_TAG_POS_BIGNUM 2 276 #define CBOR_TAG_NEG_BIGNUM 3 277 #define CBOR_TAG_FRACTION 4 278 #define CBOR_TAG_BIGFLOAT 5 280 #define CBOR_TAG_COSE_ENCRYPTO 16 281 #define CBOR_TAG_COSE_MAC0 17 282 #define CBOR_TAG_COSE_SIGN1 18 285 #define CBOR_TAG_ENC_AS_B64URL 21 287 #define CBOR_TAG_ENC_AS_B64 22 289 #define CBOR_TAG_ENC_AS_B16 23 290 #define CBOR_TAG_CBOR 24 292 #define CBOR_TAG_URI 32 294 #define CBOR_TAG_B64URL 33 296 #define CBOR_TAG_B64 34 298 #define CBOR_TAG_REGEX 35 300 #define CBOR_TAG_MIME 36 302 #define CBOR_TAG_BIN_UUID 37 304 #define CBOR_TAG_CWT 61 306 #define CBOR_TAG_ENCRYPT 96 307 #define CBOR_TAG_MAC 97 308 #define CBOR_TAG_SIGN 98 310 #define CBOR_TAG_GEO_COORD 103 314 #define CBOR_TAG_CBOR_MAGIC 55799 315 #define CBOR_TAG_NONE UINT64_MAX 321 #define CBOR_SIMPLEV_FALSE 20 322 #define CBOR_SIMPLEV_TRUE 21 323 #define CBOR_SIMPLEV_NULL 22 324 #define CBOR_SIMPLEV_UNDEF 23 325 #define CBOR_SIMPLEV_ONEBYTE 24 326 #define HALF_PREC_FLOAT 25 327 #define SINGLE_PREC_FLOAT 26 328 #define DOUBLE_PREC_FLOAT 27 329 #define CBOR_SIMPLE_BREAK 31 537 #define QCBOR_MAX_ITEMS_IN_ARRAY (UINT16_MAX-1) 544 #define QCBOR_MAX_ARRAY_NESTING QCBOR_MAX_ARRAY_NESTING1 550 #define QCBOR_MAX_CUSTOM_TAGS 16 669 #define QCBOR_TYPE_NONE 0 671 #define QCBOR_TYPE_INT64 2 673 #define QCBOR_TYPE_UINT64 3 675 #define QCBOR_TYPE_ARRAY 4 677 #define QCBOR_TYPE_MAP 5 679 #define QCBOR_TYPE_BYTE_STRING 6 681 #define QCBOR_TYPE_TEXT_STRING 7 683 #define QCBOR_TYPE_POSBIGNUM 9 685 #define QCBOR_TYPE_NEGBIGNUM 10 687 #define QCBOR_TYPE_DATE_STRING 11 689 #define QCBOR_TYPE_DATE_EPOCH 12 691 #define QCBOR_TYPE_UKNOWN_SIMPLE 13 693 #define QCBOR_TYPE_FALSE 20 695 #define QCBOR_TYPE_TRUE 21 697 #define QCBOR_TYPE_NULL 22 699 #define QCBOR_TYPE_UNDEF 23 701 #define QCBOR_TYPE_FLOAT 26 703 #define QCBOR_TYPE_DOUBLE 27 705 #define QCBOR_TYPE_MAP_AS_ARRAY 32 707 #define QCBOR_TYPE_BREAK 31 // Used internally; never returned 709 #define QCBOR_TYPE_OPTTAG 254 // Used internally; never returned 742 double fSecondsFraction;
785 void *pAllocaterContext;
786 UsefulBuf (*fAllocate)(
void *pAllocaterContext,
void *pOldMem,
size_t uNewSize);
787 void (*fFree)(
void *pAllocaterContext,
void *pMem);
788 void (*fDestructor)(
void *pAllocaterContext);
800 #define QCBOR_DECODE_MIN_MEM_POOL_SIZE 72 814 const uint64_t *puTags;
833 uint8_t uNumAllocated;
917 static void QCBOREncode_AddInt64ToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, int64_t uNum);
919 static void QCBOREncode_AddInt64ToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum);
938 static void QCBOREncode_AddUInt64ToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, uint64_t uNum);
940 static void QCBOREncode_AddUInt64ToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
981 static void QCBOREncode_AddSZStringToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szString);
983 static void QCBOREncode_AddSZStringToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
const char *szString);
1015 static void QCBOREncode_AddDoubleToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
double dNum);
1017 static void QCBOREncode_AddDoubleToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
double dNum);
1070 static void QCBOREncode_AddDateEpochToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, int64_t date);
1072 static void QCBOREncode_AddDateEpochToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, int64_t date);
1270 static void QCBOREncode_AddDateStringToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szDate);
1272 static void QCBOREncode_AddDateStringToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
const char *szDate);
1287 static void QCBOREncode_AddBoolToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
bool b);
1289 static void QCBOREncode_AddBoolToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
bool b);
1328 static void QCBOREncode_AddUndefToMap(
QCBOREncodeContext *pCtx,
const char *szLabel);
1364 static void QCBOREncode_OpenArrayInMap(
QCBOREncodeContext *pCtx,
const char *szLabel);
1486 static void QCBOREncode_BstrWrapInMap(
QCBOREncodeContext *pCtx,
const char *szLabel);
1982 if(src > INT32_MAX || src < INT32_MIN) {
1985 *dest = (int32_t) src;
1990 static inline int QCBOR_Int64ToInt16(int64_t src, int16_t *dest)
1992 if(src > INT16_MAX || src < INT16_MIN) {
1995 *dest = (int16_t) src;
2000 static inline int QCBOR_Int64ToInt8(int64_t src, int8_t *dest)
2002 if(src > INT8_MAX || src < INT8_MIN) {
2005 *dest = (int8_t) src;
2010 static inline int QCBOR_Int64ToUInt32(int64_t src, uint32_t *dest)
2012 if(src > UINT32_MAX || src < 0) {
2015 *dest = (uint32_t) src;
2020 static inline int QCBOR_Int64UToInt16(int64_t src, uint16_t *dest)
2022 if(src > UINT16_MAX || src < 0) {
2025 *dest = (uint16_t) src;
2030 static inline int QCBOR_Int64ToUInt8(int64_t src, uint8_t *dest)
2032 if(src > UINT8_MAX || src < 0) {
2035 *dest = (uint8_t) src;
2040 static inline int QCBOR_Int64ToUInt64(int64_t src, uint64_t *dest)
2045 *dest = (uint64_t) src;
2125 static inline void QCBOREncode_AddInt64ToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, int64_t uNum)
2131 static inline void QCBOREncode_AddInt64ToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum)
2138 static inline void QCBOREncode_AddUInt64ToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, uint64_t uNum)
2144 static inline void QCBOREncode_AddUInt64ToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum)
2174 static inline void QCBOREncode_AddSZStringToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szString)
2180 static inline void QCBOREncode_AddSZStringToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
const char *szString)
2187 static inline void QCBOREncode_AddDoubleToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
double dNum)
2193 static inline void QCBOREncode_AddDoubleToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
double dNum)
2206 static inline void QCBOREncode_AddDateEpochToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, int64_t date)
2213 static inline void QCBOREncode_AddDateEpochToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel, int64_t date)
2414 static inline void QCBOREncode_AddDateStringToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
const char *szDate)
2421 static inline void QCBOREncode_AddDateStringToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
const char *szDate)
2434 static inline void QCBOREncode_AddSimpleToMap(
QCBOREncodeContext *pCtx,
const char *szLabel, uint8_t uSimple)
2437 QCBOREncode_AddSimple(pCtx, uSimple);
2440 static inline void QCBOREncode_AddSimpleToMapN(
QCBOREncodeContext *pCtx,
int nLabel, uint8_t uSimple)
2443 QCBOREncode_AddSimple(pCtx, uSimple);
2449 uint8_t uSimple = CBOR_SIMPLEV_FALSE;
2451 uSimple = CBOR_SIMPLEV_TRUE;
2453 QCBOREncode_AddSimple(pCtx, uSimple);
2456 static inline void QCBOREncode_AddBoolToMap(
QCBOREncodeContext *pCtx,
const char *szLabel,
bool b)
2462 static inline void QCBOREncode_AddBoolToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel,
bool b)
2471 QCBOREncode_AddSimple(pCtx, CBOR_SIMPLEV_NULL);
2474 static inline void QCBOREncode_AddNULLToMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2480 static inline void QCBOREncode_AddNULLToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
2489 QCBOREncode_AddSimple(pCtx, CBOR_SIMPLEV_UNDEF);
2492 static inline void QCBOREncode_AddUndefToMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2498 static inline void QCBOREncode_AddUndefToMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
2510 static inline void QCBOREncode_OpenArrayInMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2516 static inline void QCBOREncode_OpenArrayInMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
2533 static inline void QCBOREncode_OpenMapInMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2539 static inline void QCBOREncode_OpenMapInMapN(
QCBOREncodeContext *pCtx, int64_t nLabel)
2556 static inline void QCBOREncode_BstrWrapInMap(
QCBOREncodeContext *pCtx,
const char *szLabel)
2562 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.