36
36
37
37
#include "cbor_buf_writer.h"
38
38
#include "cbor_buf_reader.h"
39
- #include "cbor_defs.h"
40
39
#include "tinycbor-version.h"
41
40
42
41
#define TINYCBOR_VERSION ((TINYCBOR_VERSION_MAJOR << 16) | (TINYCBOR_VERSION_MINOR << 8) | TINYCBOR_VERSION_PATCH)
@@ -47,6 +46,161 @@ extern "C" {
47
46
#include <stdbool.h>
48
47
#endif
49
48
49
+ #ifndef SIZE_MAX
50
+ /* Some systems fail to define SIZE_MAX in <stdint.h>, even though C99 requires it...
51
+ * Conversion from signed to unsigned is defined in 6.3.1.3 (Signed and unsigned integers) p2,
52
+ * which says: "the value is converted by repeatedly adding or subtracting one more than the
53
+ * maximum value that can be represented in the new type until the value is in the range of the
54
+ * new type."
55
+ * So -1 gets converted to size_t by adding SIZE_MAX + 1, which results in SIZE_MAX.
56
+ */
57
+ # define SIZE_MAX ((size_t)-1)
58
+ #endif
59
+
60
+ #ifndef CBOR_API
61
+ # define CBOR_API
62
+ #endif
63
+ #ifndef CBOR_PRIVATE_API
64
+ # define CBOR_PRIVATE_API
65
+ #endif
66
+ #ifndef CBOR_INLINE_API
67
+ # if defined(__cplusplus )
68
+ # define CBOR_INLINE inline
69
+ # define CBOR_INLINE_API inline
70
+ # else
71
+ # define CBOR_INLINE_API static CBOR_INLINE
72
+ # if defined(_MSC_VER )
73
+ # define CBOR_INLINE __inline
74
+ # elif defined(__GNUC__ )
75
+ # define CBOR_INLINE __inline__
76
+ # elif defined(__STDC_VERSION__ ) && __STDC_VERSION__ >= 199901L
77
+ # define CBOR_INLINE inline
78
+ # else
79
+ # define CBOR_INLINE
80
+ # endif
81
+ # endif
82
+ #endif
83
+
84
+ typedef enum CborType {
85
+ CborIntegerType = 0x00 ,
86
+ CborByteStringType = 0x40 ,
87
+ CborTextStringType = 0x60 ,
88
+ CborArrayType = 0x80 ,
89
+ CborMapType = 0xa0 ,
90
+ CborTagType = 0xc0 ,
91
+ CborSimpleType = 0xe0 ,
92
+ CborBooleanType = 0xf5 ,
93
+ CborNullType = 0xf6 ,
94
+ CborUndefinedType = 0xf7 ,
95
+ CborHalfFloatType = 0xf9 ,
96
+ CborFloatType = 0xfa ,
97
+ CborDoubleType = 0xfb ,
98
+
99
+ CborInvalidType = 0xff /* equivalent to the break byte, so it will never be used */
100
+ } CborType ;
101
+
102
+ typedef uint64_t CborTag ;
103
+ typedef enum CborKnownTags {
104
+ CborDateTimeStringTag = 0 ,
105
+ CborUnixTime_tTag = 1 ,
106
+ CborPositiveBignumTag = 2 ,
107
+ CborNegativeBignumTag = 3 ,
108
+ CborDecimalTag = 4 ,
109
+ CborBigfloatTag = 5 ,
110
+ CborCOSE_Encrypt0Tag = 16 ,
111
+ CborCOSE_Mac0Tag = 17 ,
112
+ CborCOSE_Sign1Tag = 18 ,
113
+ CborExpectedBase64urlTag = 21 ,
114
+ CborExpectedBase64Tag = 22 ,
115
+ CborExpectedBase16Tag = 23 ,
116
+ CborEncodedCborTag = 24 ,
117
+ CborUrlTag = 32 ,
118
+ CborBase64urlTag = 33 ,
119
+ CborBase64Tag = 34 ,
120
+ CborRegularExpressionTag = 35 ,
121
+ CborMimeMessageTag = 36 ,
122
+ CborCOSE_EncryptTag = 96 ,
123
+ CborCOSE_MacTag = 97 ,
124
+ CborCOSE_SignTag = 98 ,
125
+ CborSignatureTag = 55799
126
+ } CborKnownTags ;
127
+
128
+ /* #define the constants so we can check with #ifdef */
129
+ #define CborDateTimeStringTag CborDateTimeStringTag
130
+ #define CborUnixTime_tTag CborUnixTime_tTag
131
+ #define CborPositiveBignumTag CborPositiveBignumTag
132
+ #define CborNegativeBignumTag CborNegativeBignumTag
133
+ #define CborDecimalTag CborDecimalTag
134
+ #define CborBigfloatTag CborBigfloatTag
135
+ #define CborCOSE_Encrypt0Tag CborCOSE_Encrypt0Tag
136
+ #define CborCOSE_Mac0Tag CborCOSE_Mac0Tag
137
+ #define CborCOSE_Sign1Tag CborCOSE_Sign1Tag
138
+ #define CborExpectedBase64urlTag CborExpectedBase64urlTag
139
+ #define CborExpectedBase64Tag CborExpectedBase64Tag
140
+ #define CborExpectedBase16Tag CborExpectedBase16Tag
141
+ #define CborEncodedCborTag CborEncodedCborTag
142
+ #define CborUrlTag CborUrlTag
143
+ #define CborBase64urlTag CborBase64urlTag
144
+ #define CborBase64Tag CborBase64Tag
145
+ #define CborRegularExpressionTag CborRegularExpressionTag
146
+ #define CborMimeMessageTag CborMimeMessageTag
147
+ #define CborCOSE_EncryptTag CborCOSE_EncryptTag
148
+ #define CborCOSE_MacTag CborCOSE_MacTag
149
+ #define CborCOSE_SignTag CborCOSE_SignTag
150
+ #define CborSignatureTag CborSignatureTag
151
+
152
+ /* Error API */
153
+
154
+ typedef enum CborError {
155
+ CborNoError = 0 ,
156
+
157
+ /* errors in all modes */
158
+ CborUnknownError ,
159
+ CborErrorUnknownLength , /* request for length in array, map, or string with indeterminate length */
160
+ CborErrorAdvancePastEOF ,
161
+ CborErrorIO ,
162
+
163
+ /* parser errors streaming errors */
164
+ CborErrorGarbageAtEnd = 256 ,
165
+ CborErrorUnexpectedEOF ,
166
+ CborErrorUnexpectedBreak ,
167
+ CborErrorUnknownType , /* can only heppen in major type 7 */
168
+ CborErrorIllegalType , /* type not allowed here */
169
+ CborErrorIllegalNumber ,
170
+ CborErrorIllegalSimpleType , /* types of value less than 32 encoded in two bytes */
171
+
172
+ /* parser errors in strict mode parsing only */
173
+ CborErrorUnknownSimpleType = 512 ,
174
+ CborErrorUnknownTag ,
175
+ CborErrorInappropriateTagForType ,
176
+ CborErrorDuplicateObjectKeys ,
177
+ CborErrorInvalidUtf8TextString ,
178
+ CborErrorExcludedType ,
179
+ CborErrorExcludedValue ,
180
+ CborErrorImproperValue ,
181
+ CborErrorOverlongEncoding ,
182
+ CborErrorMapKeyNotString ,
183
+ CborErrorMapNotSorted ,
184
+ CborErrorMapKeysNotUnique ,
185
+
186
+ /* encoder errors */
187
+ CborErrorTooManyItems = 768 ,
188
+ CborErrorTooFewItems ,
189
+
190
+ /* internal implementation errors */
191
+ CborErrorDataTooLarge = 1024 ,
192
+ CborErrorNestingTooDeep ,
193
+ CborErrorUnsupportedType ,
194
+
195
+ /* errors in converting to JSON */
196
+ CborErrorJsonObjectKeyIsAggregate = 1280 ,
197
+ CborErrorJsonObjectKeyNotString ,
198
+ CborErrorJsonNotImplemented ,
199
+
200
+ CborErrorOutOfMemory = (int ) (~0U / 2 + 1 ),
201
+ CborErrorInternalError = (int ) (~0U / 2 ) /* INT_MAX on two's complement machines */
202
+ } CborError ;
203
+
50
204
CBOR_API const char * cbor_error_string (CborError error );
51
205
52
206
/* Encoder API */
@@ -57,13 +211,14 @@ struct CborEncoder
57
211
#ifndef NO_DFLT_WRITER
58
212
struct cbor_buf_writer wr ;
59
213
#endif
60
- size_t added ;
61
- size_t container_size ;
214
+ size_t remaining ;
62
215
int flags ;
63
216
};
64
217
65
218
typedef struct CborEncoder CborEncoder ;
66
219
220
+ static const size_t CborIndefiniteLength = SIZE_MAX ;
221
+
67
222
#ifndef NO_DFLT_WRITER
68
223
CBOR_API void cbor_encoder_init (CborEncoder * encoder , uint8_t * buffer , size_t size , int flags );
69
224
#endif
@@ -100,6 +255,16 @@ CBOR_API CborError cbor_encoder_close_container(CborEncoder *encoder, const Cbor
100
255
CBOR_API CborError cbor_encoder_close_container_checked (CborEncoder * encoder , const CborEncoder * containerEncoder );
101
256
102
257
/* Parser API */
258
+
259
+ enum CborParserIteratorFlags
260
+ {
261
+ CborIteratorFlag_IntegerValueTooLarge = 0x01 ,
262
+ CborIteratorFlag_NegativeInteger = 0x02 ,
263
+ CborIteratorFlag_IteratingStringChunks = 0x02 ,
264
+ CborIteratorFlag_UnknownLength = 0x04 ,
265
+ CborIteratorFlag_ContainerIsMap = 0x20
266
+ };
267
+
103
268
struct CborParser
104
269
{
105
270
#ifndef NO_DFLT_READER
@@ -362,10 +527,65 @@ CBOR_INLINE_API CborError cbor_value_get_double(const CborValue *value, double *
362
527
}
363
528
364
529
/* Validation API */
530
+
531
+ enum CborValidationFlags {
532
+ /* Bit mapping:
533
+ * bits 0-7 (8 bits): canonical format
534
+ * bits 8-11 (4 bits): canonical format & strict mode
535
+ * bits 12-20 (8 bits): strict mode
536
+ * bits 21-31 (10 bits): other
537
+ */
538
+
539
+ CborValidateShortestIntegrals = 0x0001 ,
540
+ CborValidateShortestFloatingPoint = 0x0002 ,
541
+ CborValidateShortestNumbers = CborValidateShortestIntegrals | CborValidateShortestFloatingPoint ,
542
+ CborValidateNoIndeterminateLength = 0x0100 ,
543
+ CborValidateMapIsSorted = 0x0200 | CborValidateNoIndeterminateLength ,
544
+
545
+ CborValidateCanonicalFormat = 0x0fff ,
546
+
547
+ CborValidateMapKeysAreUnique = 0x1000 | CborValidateMapIsSorted ,
548
+ CborValidateTagUse = 0x2000 ,
549
+ CborValidateUtf8 = 0x4000 ,
550
+
551
+ CborValidateStrictMode = 0xfff00 ,
552
+
553
+ CborValidateMapKeysAreString = 0x100000 ,
554
+ CborValidateNoUndefined = 0x200000 ,
555
+ CborValidateNoTags = 0x400000 ,
556
+ CborValidateFiniteFloatingPoint = 0x800000 ,
557
+ /* unused = 0x1000000, */
558
+ /* unused = 0x2000000, */
559
+
560
+ CborValidateNoUnknownSimpleTypesSA = 0x4000000 ,
561
+ CborValidateNoUnknownSimpleTypes = 0x8000000 | CborValidateNoUnknownSimpleTypesSA ,
562
+ CborValidateNoUnknownTagsSA = 0x10000000 ,
563
+ CborValidateNoUnknownTagsSR = 0x20000000 | CborValidateNoUnknownTagsSA ,
564
+ CborValidateNoUnknownTags = 0x40000000 | CborValidateNoUnknownTagsSR ,
565
+
566
+ CborValidateCompleteData = (int )0x80000000 ,
567
+
568
+ CborValidateStrictest = (int )~0U ,
569
+ CborValidateBasic = 0
570
+ };
571
+
365
572
CBOR_API CborError cbor_value_validate (const CborValue * it , int flags );
366
573
367
574
/* Human-readable (dump) API */
368
575
576
+ enum CborPrettyFlags {
577
+ CborPrettyNumericEncodingIndicators = 0x01 ,
578
+ CborPrettyTextualEncodingIndicators = 0 ,
579
+
580
+ CborPrettyIndicateIndetermineLength = 0x02 ,
581
+ CborPrettyIndicateOverlongNumbers = 0x04 ,
582
+
583
+ CborPrettyShowStringFragments = 0x100 ,
584
+ CborPrettyMergeStringFragments = 0 ,
585
+
586
+ CborPrettyDefaultFlags = CborPrettyIndicateIndetermineLength
587
+ };
588
+
369
589
typedef CborError (* CborStreamFunction )(void * token , const char * fmt , ...)
370
590
#ifdef __GNUC__
371
591
__attribute__((__format__ (printf , 2 , 3 )))
@@ -390,3 +610,4 @@ CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value
390
610
#endif
391
611
392
612
#endif /* CBOR_H */
613
+
0 commit comments