Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 957c084

Browse files
committedDec 3, 2024
Add tinycbor
1 parent d6d4547 commit 957c084

18 files changed

+5846
-0
lines changed
 

‎src/Arduino_TinyCBOR.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
#pragma once
11+
12+
#include "./tinycbor/cbor-lib.h"

‎src/tinycbor/cbor-lib.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* cbor implementation based on Intel tinycbor under MIT license */
2+
3+
#ifndef CBOR_LIB_H
4+
#define CBOR_LIB_H
5+
6+
/******************************************************************************
7+
INCLUDE
8+
******************************************************************************/
9+
10+
#include "src/cbor.h"
11+
12+
/******************************************************************************
13+
* DEFINE
14+
******************************************************************************/
15+
16+
#ifndef CHECK_CBOR
17+
#define CHECK_CBOR(expr) \
18+
do { \
19+
CborError error = CborNoError; \
20+
error = (expr); \
21+
if (CborNoError != error) \
22+
return error; \
23+
} while(0);
24+
#endif /* CHECK_CBOR */
25+
26+
#ifndef CHECK_CBOR_MULTI
27+
#define CHECK_CBOR_MULTI(expr) \
28+
do { \
29+
CborError error = CborNoError; \
30+
error = (expr); \
31+
if (CborErrorOutOfMemory == error) \
32+
return CborErrorSplitItems; \
33+
if (CborNoError != error) \
34+
return error; \
35+
} while(0);
36+
#endif /* CHECK_CBOR_MULTI */
37+
38+
#endif

‎src/tinycbor/src/cbor.h

Lines changed: 639 additions & 0 deletions
Large diffs are not rendered by default.

‎src/tinycbor/src/cborencoder.c

Lines changed: 650 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2015 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#define _BSD_SOURCE 1
26+
#define _DEFAULT_SOURCE 1
27+
#ifndef __STDC_LIMIT_MACROS
28+
# define __STDC_LIMIT_MACROS 1
29+
#endif
30+
31+
#include "cbor.h"
32+
33+
#pragma GCC diagnostic push
34+
#pragma GCC diagnostic ignored "-Wpedantic"
35+
36+
/**
37+
* \addtogroup CborEncoding
38+
* @{
39+
*/
40+
41+
/**
42+
* @deprecated
43+
*
44+
* Closes the CBOR container (array or map) provided by \a containerEncoder and
45+
* updates the CBOR stream provided by \a encoder. Both parameters must be the
46+
* same as were passed to cbor_encoder_create_array() or
47+
* cbor_encoder_create_map().
48+
*
49+
* Prior to version 0.5, cbor_encoder_close_container() did not check the
50+
* number of items added. Since that version, it does and now
51+
* cbor_encoder_close_container_checked() is no longer needed.
52+
*
53+
* \sa cbor_encoder_create_array(), cbor_encoder_create_map()
54+
*/
55+
CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder)
56+
{
57+
return cbor_encoder_close_container(encoder, containerEncoder);
58+
}
59+
60+
#pragma GCC diagnostic pop
61+
62+
/** @} */

‎src/tinycbor/src/cborerrorstrings.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2016 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#include "cbor.h"
26+
27+
#ifndef _
28+
# define _(msg) msg
29+
#endif
30+
31+
#pragma GCC diagnostic push
32+
#pragma GCC diagnostic ignored "-Wpedantic"
33+
34+
/**
35+
* \enum CborError
36+
* \ingroup CborGlobals
37+
* The CborError enum contains the possible error values used by the CBOR encoder and decoder.
38+
*
39+
* TinyCBOR functions report success by returning CborNoError, or one error
40+
* condition by returning one of the values below. One exception is the
41+
* out-of-memory condition (CborErrorOutOfMemory), which the functions for \ref
42+
* CborEncoding may report in bit-wise OR with other conditions.
43+
*
44+
* This technique allows code to determine whether the only error condition was
45+
* a lack of buffer space, which may not be a fatal condition if the buffer can
46+
* be resized. Additionally, the functions for \ref CborEncoding may continue
47+
* to be used even after CborErrorOutOfMemory is returned, and instead they
48+
* will simply calculate the extra space needed.
49+
*
50+
* \value CborNoError No error occurred
51+
* \omitvalue CborUnknownError
52+
* \value CborErrorUnknownLength Request for the length of an array, map or string whose length is not provided in the CBOR stream
53+
* \value CborErrorAdvancePastEOF Not enough data in the stream to decode item (decoding would advance past end of stream)
54+
* \value CborErrorIO An I/O error occurred, probably due to an out-of-memory situation
55+
* \value CborErrorGarbageAtEnd Bytes exist past the end of the CBOR stream
56+
* \value CborErrorUnexpectedEOF End of stream reached unexpectedly
57+
* \value CborErrorUnexpectedBreak A CBOR break byte was found where not expected
58+
* \value CborErrorUnknownType An unknown type (future extension to CBOR) was found in the stream
59+
* \value CborErrorIllegalType An invalid type was found while parsing a chunked CBOR string
60+
* \value CborErrorIllegalNumber An illegal initial byte (encoding unspecified additional information) was found
61+
* \value CborErrorIllegalSimpleType An illegal encoding of a CBOR Simple Type of value less than 32 was found
62+
* \omitvalue CborErrorUnknownSimpleType
63+
* \omitvalue CborErrorUnknownTag
64+
* \omitvalue CborErrorInappropriateTagForType
65+
* \omitvalue CborErrorDuplicateObjectKeys
66+
* \value CborErrorInvalidUtf8TextString Illegal UTF-8 encoding found while parsing CBOR Text String
67+
* \value CborErrorTooManyItems Too many items were added to CBOR map or array of pre-determined length
68+
* \value CborErrorTooFewItems Too few items were added to CBOR map or array of pre-determined length
69+
* \value CborErrorDataTooLarge Data item size exceeds TinyCBOR's implementation limits
70+
* \value CborErrorNestingTooDeep Data item nesting exceeds TinyCBOR's implementation limits
71+
* \omitvalue CborErrorUnsupportedType
72+
* \value CborErrorJsonObjectKeyIsAggregate Conversion to JSON failed because the key in a map is a CBOR map or array
73+
* \value CborErrorJsonObjectKeyNotString Conversion to JSON failed because the key in a map is not a text string
74+
* \value CborErrorOutOfMemory During CBOR encoding, the buffer provided is insufficient for encoding the data item;
75+
* in other situations, TinyCBOR failed to allocate memory
76+
* \value CborErrorInternalError An internal error occurred in TinyCBOR
77+
*/
78+
79+
/**
80+
* \ingroup CborGlobals
81+
* Returns the error string corresponding to the CBOR error condition \a error.
82+
*/
83+
const char *cbor_error_string(CborError error)
84+
{
85+
switch (error) {
86+
case CborNoError:
87+
return "";
88+
89+
case CborUnknownError:
90+
return _("unknown error");
91+
92+
case CborErrorOutOfMemory:
93+
return _("out of memory/need more memory");
94+
95+
case CborErrorUnknownLength:
96+
return _("unknown length (attempted to get the length of a map/array/string of indeterminate length");
97+
98+
case CborErrorAdvancePastEOF:
99+
return _("attempted to advance past EOF");
100+
101+
case CborErrorIO:
102+
return _("I/O error");
103+
104+
case CborErrorGarbageAtEnd:
105+
return _("garbage after the end of the content");
106+
107+
case CborErrorUnexpectedEOF:
108+
return _("unexpected end of data");
109+
110+
case CborErrorUnexpectedBreak:
111+
return _("unexpected 'break' byte");
112+
113+
case CborErrorUnknownType:
114+
return _("illegal byte (encodes future extension type)");
115+
116+
case CborErrorIllegalType:
117+
return _("mismatched string type in chunked string");
118+
119+
case CborErrorIllegalNumber:
120+
return _("illegal initial byte (encodes unspecified additional information)");
121+
122+
case CborErrorIllegalSimpleType:
123+
return _("illegal encoding of simple type smaller than 32");
124+
125+
case CborErrorUnknownSimpleType:
126+
return _("unknown simple type");
127+
128+
case CborErrorUnknownTag:
129+
return _("unknown tag");
130+
131+
case CborErrorInappropriateTagForType:
132+
return _("inappropriate tag for type");
133+
134+
case CborErrorDuplicateObjectKeys:
135+
return _("duplicate keys in object");
136+
137+
case CborErrorInvalidUtf8TextString:
138+
return _("invalid UTF-8 content in string");
139+
140+
case CborErrorExcludedType:
141+
return _("excluded type found");
142+
143+
case CborErrorExcludedValue:
144+
return _("excluded value found");
145+
146+
case CborErrorImproperValue:
147+
case CborErrorOverlongEncoding:
148+
return _("value encoded in non-canonical form");
149+
150+
case CborErrorMapKeyNotString:
151+
case CborErrorJsonObjectKeyNotString:
152+
return _("key in map is not a string");
153+
154+
case CborErrorMapNotSorted:
155+
return _("map is not sorted");
156+
157+
case CborErrorMapKeysNotUnique:
158+
return _("map keys are not unique");
159+
160+
case CborErrorTooManyItems:
161+
return _("too many items added to encoder");
162+
163+
case CborErrorTooFewItems:
164+
return _("too few items added to encoder");
165+
166+
case CborErrorSplitItems:
167+
return _("splitted item added to encoder");
168+
169+
case CborErrorDataTooLarge:
170+
return _("internal error: data too large");
171+
172+
case CborErrorNestingTooDeep:
173+
return _("internal error: too many nested containers found in recursive function");
174+
175+
case CborErrorUnsupportedType:
176+
return _("unsupported type");
177+
178+
case CborErrorJsonObjectKeyIsAggregate:
179+
return _("conversion to JSON failed: key in object is an array or map");
180+
181+
case CborErrorJsonNotImplemented:
182+
return _("conversion to JSON failed: open_memstream unavailable");
183+
184+
case CborErrorInternalError:
185+
return _("internal error");
186+
}
187+
return cbor_error_string(CborUnknownError);
188+
}
189+
190+
#pragma GCC diagnostic pop

‎src/tinycbor/src/cborinternal_p.h

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2017 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#ifndef CBORINTERNAL_P_H
26+
#define CBORINTERNAL_P_H
27+
28+
#include "compilersupport_p.h"
29+
30+
#ifndef CBOR_NO_FLOATING_POINT
31+
# include <float.h>
32+
# include <math.h>
33+
#else
34+
# ifndef CBOR_NO_HALF_FLOAT_TYPE
35+
# define CBOR_NO_HALF_FLOAT_TYPE 1
36+
# endif
37+
#endif
38+
39+
#ifndef CBOR_NO_HALF_FLOAT_TYPE
40+
# ifdef __F16C__
41+
# include <immintrin.h>
42+
static inline unsigned short encode_half(double val)
43+
{
44+
return _cvtss_sh((float)val, 3);
45+
}
46+
static inline double decode_half(unsigned short half)
47+
{
48+
return _cvtsh_ss(half);
49+
}
50+
# else
51+
/* software implementation of float-to-fp16 conversions */
52+
static inline unsigned short encode_half(double val)
53+
{
54+
uint64_t v;
55+
int sign, exp, mant;
56+
memcpy(&v, &val, sizeof(v));
57+
sign = v >> 63 << 15;
58+
exp = (v >> 52) & 0x7ff;
59+
mant = v << 12 >> 12 >> (53-11); /* keep only the 11 most significant bits of the mantissa */
60+
exp -= 1023;
61+
if (exp == 1024) {
62+
/* infinity or NaN */
63+
exp = 16;
64+
mant >>= 1;
65+
} else if (exp >= 16) {
66+
/* overflow, as largest number */
67+
exp = 15;
68+
mant = 1023;
69+
} else if (exp >= -14) {
70+
/* regular normal */
71+
} else if (exp >= -24) {
72+
/* subnormal */
73+
mant |= 1024;
74+
mant >>= -(exp + 14);
75+
exp = -15;
76+
} else {
77+
/* underflow, make zero */
78+
return 0;
79+
}
80+
81+
/* safe cast here as bit operations above guarantee not to overflow */
82+
return (unsigned short)(sign | ((exp + 15) << 10) | mant);
83+
}
84+
85+
/* this function was copied & adapted from RFC 7049 Appendix D */
86+
static inline double decode_half(unsigned short half)
87+
{
88+
int exp = (half >> 10) & 0x1f;
89+
int mant = half & 0x3ff;
90+
double val;
91+
if (exp == 0) val = ldexp(mant, -24);
92+
else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
93+
else val = mant == 0 ? INFINITY : NAN;
94+
return half & 0x8000 ? -val : val;
95+
}
96+
# endif
97+
#endif /* CBOR_NO_HALF_FLOAT_TYPE */
98+
99+
#ifndef CBOR_INTERNAL_API
100+
# define CBOR_INTERNAL_API
101+
#endif
102+
103+
#ifndef CBOR_PARSER_MAX_RECURSIONS
104+
# define CBOR_PARSER_MAX_RECURSIONS 1024
105+
#endif
106+
107+
/*
108+
* CBOR Major types
109+
* Encoded in the high 3 bits of the descriptor byte
110+
* See http://tools.ietf.org/html/rfc7049#section-2.1
111+
*/
112+
typedef enum CborMajorTypes {
113+
UnsignedIntegerType = 0U,
114+
NegativeIntegerType = 1U,
115+
ByteStringType = 2U,
116+
TextStringType = 3U,
117+
ArrayType = 4U,
118+
MapType = 5U, /* a.k.a. object */
119+
TagType = 6U,
120+
SimpleTypesType = 7U
121+
} CborMajorTypes;
122+
123+
/*
124+
* CBOR simple and floating point types
125+
* Encoded in the low 8 bits of the descriptor byte when the
126+
* Major Type is 7.
127+
*/
128+
typedef enum CborSimpleTypes {
129+
FalseValue = 20,
130+
TrueValue = 21,
131+
NullValue = 22,
132+
UndefinedValue = 23,
133+
SimpleTypeInNextByte = 24, /* not really a simple type */
134+
HalfPrecisionFloat = 25, /* ditto */
135+
SinglePrecisionFloat = 26, /* ditto */
136+
DoublePrecisionFloat = 27, /* ditto */
137+
Break = 31
138+
} CborSimpleTypes;
139+
140+
enum {
141+
SmallValueBitLength = 5U,
142+
SmallValueMask = (1U << SmallValueBitLength) - 1, /* 31 */
143+
Value8Bit = 24U,
144+
Value16Bit = 25U,
145+
Value32Bit = 26U,
146+
Value64Bit = 27U,
147+
IndefiniteLength = 31U,
148+
149+
MajorTypeShift = SmallValueBitLength,
150+
MajorTypeMask = (int) (~0U << MajorTypeShift),
151+
152+
BreakByte = (unsigned)Break | (SimpleTypesType << MajorTypeShift)
153+
};
154+
155+
CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len);
156+
CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC _cbor_value_prepare_string_iteration(CborValue *it);
157+
CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC _cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
158+
size_t *len, CborValue *next);
159+
160+
161+
#endif /* CBORINTERNAL_P_H */

‎src/tinycbor/src/cborjson.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2015 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#ifndef CBORJSON_H
26+
#define CBORJSON_H
27+
28+
#include "cbor.h"
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
/* Conversion to JSON */
35+
enum CborToJsonFlags
36+
{
37+
CborConvertAddMetadata = 1,
38+
CborConvertTagsToObjects = 2,
39+
CborConvertIgnoreTags = 0,
40+
41+
CborConvertObeyByteStringTags = 0,
42+
CborConvertByteStringsToBase64Url = 4,
43+
44+
CborConvertRequireMapStringKeys = 0,
45+
CborConvertStringifyMapKeys = 8,
46+
47+
CborConvertDefaultFlags = 0
48+
};
49+
50+
CBOR_API CborError cbor_value_to_json_advance(FILE *out, CborValue *value, int flags);
51+
CBOR_INLINE_API CborError cbor_value_to_json(FILE *out, const CborValue *value, int flags)
52+
{
53+
CborValue copy = *value;
54+
return cbor_value_to_json_advance(out, &copy, flags);
55+
}
56+
57+
#ifdef __cplusplus
58+
}
59+
#endif
60+
61+
#endif /* CBORJSON_H */
62+

‎src/tinycbor/src/cborparser.c

Lines changed: 1435 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2016 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#ifndef _BSD_SOURCE
26+
#define _BSD_SOURCE 1
27+
#endif
28+
#ifndef _DEFAULT_SOURCE
29+
#define _DEFAULT_SOURCE 1
30+
#endif
31+
#ifndef __STDC_LIMIT_MACROS
32+
# define __STDC_LIMIT_MACROS 1
33+
#endif
34+
35+
#include "cbor.h"
36+
#include "compilersupport_p.h"
37+
#include <stdlib.h>
38+
39+
#pragma GCC diagnostic push
40+
#pragma GCC diagnostic ignored "-Wpedantic"
41+
42+
/**
43+
* \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
44+
*
45+
* Allocates memory for the string pointed by \a value and copies it into this
46+
* buffer. The pointer to the buffer is stored in \a buffer and the number of
47+
* bytes copied is stored in \a buflen (those variables must not be NULL).
48+
*
49+
* If the iterator \a value does not point to a text string, the behaviour is
50+
* undefined, so checking with \ref cbor_value_get_type or \ref
51+
* cbor_value_is_text_string is recommended.
52+
*
53+
* If \c malloc returns a NULL pointer, this function will return error
54+
* condition \ref CborErrorOutOfMemory.
55+
*
56+
* On success, \c{*buffer} will contain a valid pointer that must be freed by
57+
* calling \c{free()}. This is the case even for zero-length strings.
58+
*
59+
* The \a next pointer, if not null, will be updated to point to the next item
60+
* after this string. If \a value points to the last item, then \a next will be
61+
* invalid.
62+
*
63+
* This function may not run in constant time (it will run in O(n) time on the
64+
* number of chunks). It requires constant memory (O(1)) in addition to the
65+
* malloc'ed block.
66+
*
67+
* \note This function does not perform UTF-8 validation on the incoming text
68+
* string.
69+
*
70+
* \sa cbor_value_get_text_string_chunk(), cbor_value_copy_text_string(), cbor_value_dup_byte_string()
71+
*/
72+
73+
/**
74+
* \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
75+
*
76+
* Allocates memory for the string pointed by \a value and copies it into this
77+
* buffer. The pointer to the buffer is stored in \a buffer and the number of
78+
* bytes copied is stored in \a buflen (those variables must not be NULL).
79+
*
80+
* If the iterator \a value does not point to a byte string, the behaviour is
81+
* undefined, so checking with \ref cbor_value_get_type or \ref
82+
* cbor_value_is_byte_string is recommended.
83+
*
84+
* If \c malloc returns a NULL pointer, this function will return error
85+
* condition \ref CborErrorOutOfMemory.
86+
*
87+
* On success, \c{*buffer} will contain a valid pointer that must be freed by
88+
* calling \c{free()}. This is the case even for zero-length strings.
89+
*
90+
* The \a next pointer, if not null, will be updated to point to the next item
91+
* after this string. If \a value points to the last item, then \a next will be
92+
* invalid.
93+
*
94+
* This function may not run in constant time (it will run in O(n) time on the
95+
* number of chunks). It requires constant memory (O(1)) in addition to the
96+
* malloc'ed block.
97+
*
98+
* \sa cbor_value_get_text_string_chunk(), cbor_value_copy_byte_string(), cbor_value_dup_text_string()
99+
*/
100+
CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
101+
{
102+
CborError err;
103+
cbor_assert(buffer);
104+
cbor_assert(buflen);
105+
*buflen = SIZE_MAX;
106+
err = _cbor_value_copy_string(value, NULL, buflen, NULL);
107+
if (err)
108+
return err;
109+
110+
++*buflen;
111+
*buffer = malloc(*buflen);
112+
if (!*buffer) {
113+
/* out of memory */
114+
return CborErrorOutOfMemory;
115+
}
116+
err = _cbor_value_copy_string(value, *buffer, buflen, next);
117+
if (err) {
118+
free(*buffer);
119+
return err;
120+
}
121+
return CborNoError;
122+
}
123+
124+
#pragma GCC diagnostic pop

‎src/tinycbor/src/cborpretty.c

Lines changed: 583 additions & 0 deletions
Large diffs are not rendered by default.

‎src/tinycbor/src/cborpretty_stdio.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2017 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#include "cbor.h"
26+
#include <stdarg.h>
27+
#include <stdio.h>
28+
29+
#pragma GCC diagnostic push
30+
#pragma GCC diagnostic ignored "-Wpedantic"
31+
32+
static CborError cbor_fprintf(void *out, const char *fmt, ...)
33+
{
34+
int n;
35+
36+
va_list list;
37+
va_start(list, fmt);
38+
n = vfprintf((FILE *)out, fmt, list);
39+
va_end(list);
40+
41+
return n < 0 ? CborErrorIO : CborNoError;
42+
}
43+
44+
/**
45+
* \fn CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
46+
*
47+
* Converts the current CBOR type pointed to by \a value to its textual
48+
* representation and writes it to the \a out stream. If an error occurs, this
49+
* function returns an error code similar to CborParsing.
50+
*
51+
* \sa cbor_value_to_pretty_advance(), cbor_value_to_json_advance()
52+
*/
53+
54+
/**
55+
* Converts the current CBOR type pointed to by \a value to its textual
56+
* representation and writes it to the \a out stream. If an error occurs, this
57+
* function returns an error code similar to CborParsing.
58+
*
59+
* If no error ocurred, this function advances \a value to the next element.
60+
* Often, concatenating the text representation of multiple elements can be
61+
* done by appending a comma to the output stream in between calls to this
62+
* function.
63+
*
64+
* \sa cbor_value_to_pretty(), cbor_value_to_pretty_stream(), cbor_value_to_json_advance()
65+
*/
66+
CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value)
67+
{
68+
return cbor_value_to_pretty_stream(cbor_fprintf, out, value, CborPrettyDefaultFlags);
69+
}
70+
71+
/**
72+
* Converts the current CBOR type pointed to by \a value to its textual
73+
* representation and writes it to the \a out stream. If an error occurs, this
74+
* function returns an error code similar to CborParsing.
75+
*
76+
* The textual representation can be controlled by the \a flags parameter (see
77+
* CborPrettyFlags for more information).
78+
*
79+
* If no error ocurred, this function advances \a value to the next element.
80+
* Often, concatenating the text representation of multiple elements can be
81+
* done by appending a comma to the output stream in between calls to this
82+
* function.
83+
*
84+
* \sa cbor_value_to_pretty_stream(), cbor_value_to_pretty(), cbor_value_to_json_advance()
85+
*/
86+
CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags)
87+
{
88+
return cbor_value_to_pretty_stream(cbor_fprintf, out, value, flags);
89+
}
90+
91+
#pragma GCC diagnostic pop

‎src/tinycbor/src/cbortojson.c

Lines changed: 704 additions & 0 deletions
Large diffs are not rendered by default.

‎src/tinycbor/src/cborvalidation.c

Lines changed: 671 additions & 0 deletions
Large diffs are not rendered by default.

‎src/tinycbor/src/compilersupport_p.h

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2017 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#ifndef COMPILERSUPPORT_H
26+
#define COMPILERSUPPORT_H
27+
28+
#include "cbor.h"
29+
30+
#ifndef _BSD_SOURCE
31+
# define _BSD_SOURCE
32+
#endif
33+
#ifndef _DEFAULT_SOURCE
34+
# define _DEFAULT_SOURCE
35+
#endif
36+
#ifndef assert
37+
# include <assert.h>
38+
#endif
39+
#include <stddef.h>
40+
#include <stdint.h>
41+
#include <string.h>
42+
43+
#ifndef __cplusplus
44+
# include <stdbool.h>
45+
#endif
46+
47+
#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L || __cpp_static_assert >= 200410
48+
# define cbor_static_assert(x) static_assert(x, #x)
49+
#elif !defined(__cplusplus) && defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) && (__STDC_VERSION__ > 199901L)
50+
# define cbor_static_assert(x) _Static_assert(x, #x)
51+
#else
52+
# define cbor_static_assert(x) ((void)sizeof(char[2*!!(x) - 1]))
53+
#endif
54+
#if __STDC_VERSION__ >= 199901L || defined(__cplusplus)
55+
/* inline is a keyword */
56+
#else
57+
/* use the definition from cbor.h */
58+
# define inline CBOR_INLINE
59+
#endif
60+
61+
#ifdef NDEBUG
62+
# define cbor_assert(cond) do { if (!(cond)) unreachable(); } while (0)
63+
#else
64+
# define cbor_assert(cond) assert(cond)
65+
#endif
66+
67+
#ifndef STRINGIFY
68+
#define STRINGIFY(x) STRINGIFY2(x)
69+
#endif
70+
#define STRINGIFY2(x) #x
71+
72+
#if !defined(UINT32_MAX) || !defined(INT64_MAX)
73+
/* C89? We can define UINT32_MAX portably, but not INT64_MAX */
74+
# error "Your system has stdint.h but that doesn't define UINT32_MAX or INT64_MAX"
75+
#endif
76+
77+
#ifndef DBL_DECIMAL_DIG
78+
/* DBL_DECIMAL_DIG is C11 */
79+
# define DBL_DECIMAL_DIG 17
80+
#endif
81+
#define DBL_DECIMAL_DIG_STR STRINGIFY(DBL_DECIMAL_DIG)
82+
83+
#if defined(__GNUC__) && defined(__i386__) && !defined(__iamcu__)
84+
# define CBOR_INTERNAL_API_CC __attribute__((regparm(3)))
85+
#elif defined(_MSC_VER) && defined(_M_IX86)
86+
# define CBOR_INTERNAL_API_CC __fastcall
87+
#else
88+
# define CBOR_INTERNAL_API_CC
89+
#endif
90+
91+
#ifndef __has_builtin
92+
# define __has_builtin(x) 0
93+
#endif
94+
95+
#if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) || \
96+
(__has_builtin(__builtin_bswap64) && __has_builtin(__builtin_bswap32))
97+
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
98+
# define cbor_ntohll __builtin_bswap64
99+
# define cbor_htonll __builtin_bswap64
100+
# define cbor_ntohl __builtin_bswap32
101+
# define cbor_htonl __builtin_bswap32
102+
# ifdef __INTEL_COMPILER
103+
# define cbor_ntohs _bswap16
104+
# define cbor_htons _bswap16
105+
# elif (__GNUC__ * 100 + __GNUC_MINOR__ >= 608) || __has_builtin(__builtin_bswap16)
106+
# define cbor_ntohs __builtin_bswap16
107+
# define cbor_htons __builtin_bswap16
108+
# else
109+
# define cbor_ntohs(x) (((uint16_t)x >> 8) | ((uint16_t)x << 8))
110+
# define cbor_htons cbor_ntohs
111+
# endif
112+
# else
113+
# define cbor_ntohll
114+
# define cbor_htonll
115+
# define cbor_ntohl
116+
# define cbor_htonl
117+
# define cbor_ntohs
118+
# define cbor_htons
119+
# endif
120+
#elif defined(__sun)
121+
# include <sys/byteorder.h>
122+
#elif defined(_MSC_VER)
123+
/* MSVC, which implies Windows, which implies little-endian and sizeof(long) == 4 */
124+
# include <stdlib.h>
125+
# define cbor_ntohll _byteswap_uint64
126+
# define cbor_htonll _byteswap_uint64
127+
# define cbor_ntohl _byteswap_ulong
128+
# define cbor_htonl _byteswap_ulong
129+
# define cbor_ntohs _byteswap_ushort
130+
# define cbor_htons _byteswap_ushort
131+
#endif
132+
#ifndef cbor_ntohs
133+
# include <arpa/inet.h>
134+
# define cbor_ntohs ntohs
135+
# define cbor_htons htons
136+
#endif
137+
#ifndef cbor_ntohl
138+
# include <arpa/inet.h>
139+
# define cbor_ntohl ntohl
140+
# define cbor_htonl htonl
141+
#endif
142+
#ifndef cbor_ntohll
143+
# define cbor_ntohll ntohll
144+
# define cbor_htonll htonll
145+
/* ntohll isn't usually defined */
146+
# ifndef ntohll
147+
# if (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
148+
(defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || \
149+
(defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) || \
150+
(defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) || (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)) || \
151+
defined(__ARMEB__) || defined(__MIPSEB__) || defined(__s390__) || defined(__sparc__)
152+
# define ntohll
153+
# define htonll
154+
# elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
155+
(defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
156+
(defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && BYTE_ORDER == LITTLE_ENDIAN) || \
157+
defined(_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
158+
defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64)
159+
# define ntohll(x) ((ntohl((uint32_t)(x)) * UINT64_C(0x100000000)) + (ntohl((x) >> 32)))
160+
# define htonll ntohll
161+
# else
162+
# error "Unable to determine byte order!"
163+
# endif
164+
# endif
165+
#endif
166+
167+
168+
#ifdef __cplusplus
169+
# define CONST_CAST(t, v) const_cast<t>(v)
170+
#else
171+
/* C-style const_cast without triggering a warning with -Wcast-qual */
172+
# define CONST_CAST(t, v) (t)(uintptr_t)(v)
173+
#endif
174+
175+
#ifdef __GNUC__
176+
#ifndef likely
177+
# define likely(x) __builtin_expect(!!(x), 1)
178+
#endif
179+
#ifndef unlikely
180+
# define unlikely(x) __builtin_expect(!!(x), 0)
181+
#endif
182+
# define unreachable() __builtin_unreachable()
183+
#elif defined(_MSC_VER)
184+
# define likely(x) (x)
185+
# define unlikely(x) (x)
186+
# define unreachable() __assume(0)
187+
#else
188+
# define likely(x) (x)
189+
# define unlikely(x) (x)
190+
# define unreachable() do {} while (0)
191+
#endif
192+
193+
static inline bool add_check_overflow(size_t v1, size_t v2, size_t *r)
194+
{
195+
#if ((defined(__GNUC__) && (__GNUC__ >= 5)) && !defined(__INTEL_COMPILER)) || __has_builtin(__builtin_add_overflow)
196+
return __builtin_add_overflow(v1, v2, r);
197+
#else
198+
/* unsigned additions are well-defined */
199+
*r = v1 + v2;
200+
return v1 > v1 + v2;
201+
#endif
202+
}
203+
204+
#endif /* COMPILERSUPPORT_H */
205+

‎src/tinycbor/src/open_memstream.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2015 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#define _BSD_SOURCE 1
26+
#define _DEFAULT_SOURCE 1
27+
#define _GNU_SOURCE 1
28+
29+
#include <sys/types.h>
30+
#include <errno.h>
31+
#include <stdio.h>
32+
#include <stdlib.h>
33+
#include <string.h>
34+
35+
#include <unistd.h>
36+
37+
typedef ssize_t RetType;
38+
typedef size_t LenType;
39+
40+
#include "compilersupport_p.h"
41+
42+
#pragma GCC diagnostic push
43+
#pragma GCC diagnostic ignored "-Wpedantic"
44+
#pragma GCC diagnostic ignored "-Wreturn-type"
45+
#pragma GCC diagnostic ignored "-Wunused-function"
46+
47+
struct Buffer
48+
{
49+
char **ptr;
50+
size_t *len;
51+
size_t alloc;
52+
};
53+
54+
static RetType write_to_buffer(void *cookie, const char *data, LenType len)
55+
{
56+
struct Buffer *b = (struct Buffer *)cookie;
57+
char *ptr = *b->ptr;
58+
size_t newsize;
59+
60+
errno = EFBIG;
61+
if (unlikely(add_check_overflow(*b->len, len, &newsize)))
62+
return -1;
63+
64+
if (newsize > b->alloc) {
65+
/* make room */
66+
size_t newalloc = newsize + newsize / 2 + 1; /* give 50% more room */
67+
ptr = realloc(ptr, newalloc);
68+
if (ptr == NULL)
69+
return -1;
70+
b->alloc = newalloc;
71+
*b->ptr = ptr;
72+
}
73+
74+
memcpy(ptr + *b->len, data, len);
75+
*b->len = newsize;
76+
return len;
77+
}
78+
79+
static int close_buffer(void *cookie)
80+
{
81+
struct Buffer *b = (struct Buffer *)cookie;
82+
if (*b->ptr)
83+
(*b->ptr)[*b->len] = '\0';
84+
free(b);
85+
return 0;
86+
}
87+
88+
FILE *open_memstream(char **bufptr, size_t *lenptr)
89+
{
90+
struct Buffer *b = (struct Buffer *)malloc(sizeof(struct Buffer));
91+
if (b == NULL)
92+
return NULL;
93+
b->alloc = 0;
94+
b->len = lenptr;
95+
b->ptr = bufptr;
96+
*bufptr = NULL;
97+
*lenptr = 0;
98+
99+
#ifdef __APPLE__
100+
return funopen(b, NULL, write_to_buffer, NULL, close_buffer);
101+
#elif __GLIBC__
102+
static const cookie_io_functions_t vtable = {
103+
NULL,
104+
write_to_buffer,
105+
NULL,
106+
close_buffer
107+
};
108+
return fopencookie(b, "w", vtable);
109+
#endif
110+
}
111+
112+
#pragma GCC diagnostic pop

‎src/tinycbor/src/tinycbor-version.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#define TINYCBOR_VERSION_MAJOR 0
2+
#define TINYCBOR_VERSION_MINOR 5
3+
#define TINYCBOR_VERSION_PATCH 2

‎src/tinycbor/src/utf8_p.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2017 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#ifndef CBOR_UTF8_H
26+
#define CBOR_UTF8_H
27+
28+
#include "compilersupport_p.h"
29+
30+
#include <stdint.h>
31+
32+
static inline uint32_t get_utf8(const uint8_t **buffer, const uint8_t *end)
33+
{
34+
int charsNeeded;
35+
uint32_t uc, min_uc;
36+
uint8_t b;
37+
ptrdiff_t n = end - *buffer;
38+
if (n == 0)
39+
return ~0U;
40+
41+
uc = *(*buffer)++;
42+
if (uc < 0x80) {
43+
/* single-byte UTF-8 */
44+
return uc;
45+
}
46+
47+
/* multi-byte UTF-8, decode it */
48+
if (unlikely(uc <= 0xC1))
49+
return ~0U;
50+
if (uc < 0xE0) {
51+
/* two-byte UTF-8 */
52+
charsNeeded = 2;
53+
min_uc = 0x80;
54+
uc &= 0x1f;
55+
} else if (uc < 0xF0) {
56+
/* three-byte UTF-8 */
57+
charsNeeded = 3;
58+
min_uc = 0x800;
59+
uc &= 0x0f;
60+
} else if (uc < 0xF5) {
61+
/* four-byte UTF-8 */
62+
charsNeeded = 4;
63+
min_uc = 0x10000;
64+
uc &= 0x07;
65+
} else {
66+
return ~0U;
67+
}
68+
69+
if (n < charsNeeded - 1)
70+
return ~0U;
71+
72+
/* first continuation character */
73+
b = *(*buffer)++;
74+
if ((b & 0xc0) != 0x80)
75+
return ~0U;
76+
uc <<= 6;
77+
uc |= b & 0x3f;
78+
79+
if (charsNeeded > 2) {
80+
/* second continuation character */
81+
b = *(*buffer)++;
82+
if ((b & 0xc0) != 0x80)
83+
return ~0U;
84+
uc <<= 6;
85+
uc |= b & 0x3f;
86+
87+
if (charsNeeded > 3) {
88+
/* third continuation character */
89+
b = *(*buffer)++;
90+
if ((b & 0xc0) != 0x80)
91+
return ~0U;
92+
uc <<= 6;
93+
uc |= b & 0x3f;
94+
}
95+
}
96+
97+
/* overlong sequence? surrogate pair? out or range? */
98+
if (uc < min_uc || uc - 0xd800U < 2048U || uc > 0x10ffff)
99+
return ~0U;
100+
101+
return uc;
102+
}
103+
104+
#endif /* CBOR_UTF8_H */

0 commit comments

Comments
 (0)
Please sign in to comment.