Update TinyCBOR to 1b233087a6e6b6be297e69bfcce5ed36f338c91d

From the fork at https://github.com/thiagomacieira/tinycbor

Change-Id: I117816bf0f5e469b8d34fffd153dc88683051208
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
bb10
Thiago Macieira 2018-07-02 23:38:48 -07:00 committed by Edward Welbourne
parent 678b0cf6c3
commit 354842fbc1
6 changed files with 49 additions and 45 deletions

View File

@ -234,6 +234,7 @@ typedef struct CborEncoder CborEncoder;
static const size_t CborIndefiniteLength = SIZE_MAX;
#ifndef CBOR_NO_ENCODER_API
CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
CBOR_API void cbor_encoder_init_writer(CborEncoder *encoder, CborEncoderWriteFunction writer, void *);
CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value);
@ -280,6 +281,7 @@ CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *en
{
return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
}
#endif /* CBOR_NO_ENCODER_API */
/* Parser API */
@ -344,7 +346,8 @@ struct CborValue
};
typedef struct CborValue CborValue;
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
#ifndef CBOR_NO_PARSER_API
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, CborParser *parser, CborValue *it);
CBOR_API CborError cbor_parser_init_reader(const struct CborParserOperations *ops, CborParser *parser, CborValue *it, void *token);
CBOR_API CborError cbor_value_validate_basic(const CborValue *it);
@ -464,10 +467,11 @@ CBOR_INLINE_API bool cbor_value_is_text_string(const CborValue *value)
CBOR_INLINE_API CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
{
uint64_t v;
assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
if (!cbor_value_is_length_known(value))
return CborErrorUnknownLength;
uint64_t v = _cbor_value_extract_int64_helper(value);
v = _cbor_value_extract_int64_helper(value);
*length = (size_t)v;
if (*length != v)
return CborErrorDataTooLarge;
@ -560,10 +564,11 @@ CBOR_INLINE_API bool cbor_value_is_map(const CborValue *value)
CBOR_INLINE_API CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
{
uint64_t v;
assert(cbor_value_is_array(value));
if (!cbor_value_is_length_known(value))
return CborErrorUnknownLength;
uint64_t v = _cbor_value_extract_int64_helper(value);
v = _cbor_value_extract_int64_helper(value);
*length = (size_t)v;
if (*length != v)
return CborErrorDataTooLarge;
@ -572,10 +577,11 @@ CBOR_INLINE_API CborError cbor_value_get_array_length(const CborValue *value, si
CBOR_INLINE_API CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
{
uint64_t v;
assert(cbor_value_is_map(value));
if (!cbor_value_is_length_known(value))
return CborErrorUnknownLength;
uint64_t v = _cbor_value_extract_int64_helper(value);
v = _cbor_value_extract_int64_helper(value);
*length = (size_t)v;
if (*length != v)
return CborErrorDataTooLarge;
@ -601,9 +607,10 @@ CBOR_INLINE_API bool cbor_value_is_float(const CborValue *value)
{ return value->type == CborFloatType; }
CBOR_INLINE_API CborError cbor_value_get_float(const CborValue *value, float *result)
{
uint32_t data;
assert(cbor_value_is_float(value));
assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
uint32_t data = (uint32_t)_cbor_value_decode_int64_internal(value);
data = (uint32_t)_cbor_value_decode_int64_internal(value);
memcpy(result, &data, sizeof(*result));
return CborNoError;
}
@ -612,14 +619,16 @@ CBOR_INLINE_API bool cbor_value_is_double(const CborValue *value)
{ return value->type == CborDoubleType; }
CBOR_INLINE_API CborError cbor_value_get_double(const CborValue *value, double *result)
{
uint64_t data;
assert(cbor_value_is_double(value));
assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
uint64_t data = _cbor_value_decode_int64_internal(value);
data = _cbor_value_decode_int64_internal(value);
memcpy(result, &data, sizeof(*result));
return CborNoError;
}
/* Validation API */
#ifndef CBOR_NO_VALIDATION_API
enum CborValidationFlags {
/* Bit mapping:
@ -662,9 +671,11 @@ enum CborValidationFlags {
CborValidateBasic = 0
};
CBOR_API CborError cbor_value_validate(const CborValue *it, int flags);
CBOR_API CborError cbor_value_validate(const CborValue *it, uint32_t flags);
#endif /* CBOR_NO_VALIDATION_API */
/* Human-readable (dump) API */
#ifndef CBOR_NO_PRETTY_API
enum CborPrettyFlags {
CborPrettyNumericEncodingIndicators = 0x01,
@ -699,6 +710,10 @@ CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value
}
#endif /* __STDC_HOSTED__ check */
#endif /* CBOR_NO_PRETTY_API */
#endif /* CBOR_NO_PARSER_API */
#ifdef __cplusplus
}
#endif

View File

@ -415,11 +415,12 @@ CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value)
*/
CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value)
{
unsigned size;
uint8_t buf[1 + sizeof(uint64_t)];
cbor_assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType);
buf[0] = fpType;
unsigned size = 2U << (fpType - CborHalfFloatType);
size = 2U << (fpType - CborHalfFloatType);
if (size == 8)
put64(buf + 1, *(const uint64_t*)value);
else if (size == 4)

View File

@ -92,8 +92,8 @@ enum {
static inline void copy_current_position(CborValue *dst, const CborValue *src)
{
// This "if" is here for pedantry only: the two branches should perform
// the same memory operation.
/* This "if" is here for pedantry only: the two branches should perform
* the same memory operation. */
if (src->parser->flags & CborParserFlag_ExternalSource)
dst->source.token = src->source.token;
else

View File

@ -340,7 +340,7 @@ uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
* threads iterating at the same time, but the object can be copied so multiple
* threads can iterate.
*/
CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, CborParser *parser, CborValue *it)
{
memset(parser, 0, sizeof(*parser));
parser->source.end = buffer + size;
@ -471,6 +471,9 @@ CborError cbor_value_advance_fixed(CborValue *it)
static CborError advance_recursive(CborValue *it, int nestingLevel)
{
CborError err;
CborValue recursed;
if (is_fixed_type(it->type))
return advance_internal(it);
@ -483,8 +486,6 @@ static CborError advance_recursive(CborValue *it, int nestingLevel)
if (nestingLevel == 0)
return CborErrorNestingTooDeep;
CborError err;
CborValue recursed;
err = cbor_value_enter_container(it, &recursed);
if (err)
return err;
@ -810,8 +811,9 @@ CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
*/
CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
{
uint64_t v;
cbor_assert(cbor_value_is_integer(value));
uint64_t v = _cbor_value_extract_int64_helper(value);
v = _cbor_value_extract_int64_helper(value);
/* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
* "[if] the new type is signed and the value cannot be represented in it; either the
@ -849,8 +851,9 @@ CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
*/
CborError cbor_value_get_int_checked(const CborValue *value, int *result)
{
uint64_t v;
cbor_assert(cbor_value_is_integer(value));
uint64_t v = _cbor_value_extract_int64_helper(value);
v = _cbor_value_extract_int64_helper(value);
/* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
* "[if] the new type is signed and the value cannot be represented in it; either the
@ -1177,13 +1180,12 @@ static uintptr_t iterate_memcpy(char *dest, const uint8_t *src, size_t len)
static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
bool *result, CborValue *next, IterateFunction func)
{
cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
CborError err;
CborValue tmp;
size_t total = 0;
const void *ptr;
cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
if (!next)
next = &tmp;
*next = *value;
@ -1320,6 +1322,7 @@ CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
*/
CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
{
size_t len;
CborValue copy = *value;
CborError err = cbor_value_skip_tag(&copy);
if (err)
@ -1329,7 +1332,7 @@ CborError cbor_value_text_string_equals(const CborValue *value, const char *stri
return CborNoError;
}
size_t len = strlen(string);
len = strlen(string);
return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
}
@ -1407,9 +1410,10 @@ CborError cbor_value_text_string_equals(const CborValue *value, const char *stri
*/
CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
{
cbor_assert(cbor_value_is_map(map));
CborError err;
size_t len = strlen(string);
CborError err = cbor_value_enter_container(map, element);
cbor_assert(cbor_value_is_map(map));
err = cbor_value_enter_container(map, element);
if (err)
goto error;

View File

@ -213,10 +213,11 @@ static inline unsigned short encode_half(double val)
return _cvtss_sh((float)val, 3);
#else
uint64_t v;
int sign, exp, mant;
memcpy(&v, &val, sizeof(v));
int sign = v >> 63 << 15;
int exp = (v >> 52) & 0x7ff;
int mant = v << 12 >> 12 >> (53-11); /* keep only the 11 most significant bits of the mantissa */
sign = v >> 63 << 15;
exp = (v >> 52) & 0x7ff;
mant = v << 12 >> 12 >> (53-11); /* keep only the 11 most significant bits of the mantissa */
exp -= 1023;
if (exp == 1024) {
/* infinity or NaN */

View File

@ -58,12 +58,15 @@ QT_WARNING_DISABLE_GCC("-Wunused-function")
QT_WARNING_DISABLE_CLANG("-Wunused-function")
QT_WARNING_DISABLE_CLANG("-Wundefined-internal")
QT_WARNING_DISABLE_MSVC(4334) // '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
#define CBOR_ENCODER_NO_CHECK_USER
#define CBOR_NO_VALIDATION_API 1
#define CBOR_NO_PRETTY_API 1
#define CBOR_API static inline
#define CBOR_PRIVATE_API static inline
#define CBOR_INLINE_API static inline
#define CBOR_ENCODER_NO_CHECK_USER
#include <cbor.h>
static CborError qt_cbor_encoder_write_callback(void *token, const void *data, size_t len, CborEncoderAppendType);
@ -97,26 +100,6 @@ static CborError _cbor_value_dup_string(const CborValue *, void **, size_t *, Cb
Q_UNREACHABLE();
return CborErrorInternalError;
}
static CborError cbor_value_to_pretty_stream(CborStreamFunction, void*, CborValue*, int)
{
Q_UNREACHABLE();
return CborErrorInternalError;
}
static CborError cbor_value_to_pretty_advance(FILE*, CborValue*)
{
Q_UNREACHABLE();
return CborErrorInternalError;
}
static CborError cbor_value_to_pretty_advance_flags(FILE *, CborValue *, int)
{
Q_UNREACHABLE();
return CborErrorInternalError;
}
static CborError cbor_value_validate(const CborValue *, int)
{
Q_UNREACHABLE();
return CborErrorInternalError;
}
QT_WARNING_POP
Q_DECLARE_TYPEINFO(CborEncoder, Q_PRIMITIVE_TYPE);