Update TinyCBOR

Updated to commit e6a4fa4862bcc3a6f6b07cf9d9b784d0ab6068b4.

Pick-to: 6.0
Fixes: QTBUG-89650
Task-number: QTBUG-90214
Change-Id: I6edce5101800424a8093fffd15cdf6591cbf809d
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Thiago Macieira 2019-10-15 16:38:27 -07:00
parent e425a2a4b8
commit cbdf2ba46a
11 changed files with 114 additions and 48 deletions

View File

@ -5,11 +5,11 @@
"QtUsage": "Used for QCborStreamReader and QCborStreamWriter.", "QtUsage": "Used for QCborStreamReader and QCborStreamWriter.",
"Description": "Concise Binary Object Representation (CBOR) Library", "Description": "Concise Binary Object Representation (CBOR) Library",
"Version": "0.6.0",
"Homepage": "https://github.com/intel/tinycbor", "Homepage": "https://github.com/intel/tinycbor",
"License": "MIT License", "License": "MIT License",
"LicenseId": "MIT", "LicenseId": "MIT",
"LicenseFile": "LICENSE", "LicenseFile": "LICENSE",
"Url": "https://github.com/thiagomacieira/tinycbor/archive/e6a4fa4862bcc3a6f6b07cf9d9b784d0ab6068b4.tar.gz",
"Version": "0.6+patches", "Version": "0.6+patches",
"Copyright": "Copyright (C) 2015-2019 Intel Corporation" "Copyright": "Copyright (C) 2015-2019 Intel Corporation"
} }

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2018 Intel Corporation ** Copyright (C) 2019 Intel Corporation
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy ** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal ** of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2018 Intel Corporation ** Copyright (C) 2019 Intel Corporation
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy ** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal ** of this software and associated documentation files (the "Software"), to deal
@ -65,7 +65,7 @@
* \code * \code
* uint8_t buf[16]; * uint8_t buf[16];
* CborEncoder encoder; * CborEncoder encoder;
* cbor_encoder_init(&encoder, &buf, sizeof(buf), 0); * cbor_encoder_init(&encoder, buf, sizeof(buf), 0);
* cbor_encode_int(&encoder, some_value); * cbor_encode_int(&encoder, some_value);
* \endcode * \endcode
* *
@ -117,16 +117,16 @@
* CborEncoder encoder, mapEncoder; * CborEncoder encoder, mapEncoder;
* cbor_encoder_init(&encoder, buf, sizeof(buf), 0); * cbor_encoder_init(&encoder, buf, sizeof(buf), 0);
* err = cbor_encoder_create_map(&encoder, &mapEncoder, 1); * err = cbor_encoder_create_map(&encoder, &mapEncoder, 1);
* if (!err) * if (err)
* return err; * return err;
* err = cbor_encode_text_stringz(&mapEncoder, "foo"); * err = cbor_encode_text_stringz(&mapEncoder, "foo");
* if (!err) * if (err)
* return err; * return err;
* err = cbor_encode_boolean(&mapEncoder, some_value); * err = cbor_encode_boolean(&mapEncoder, some_value);
* if (!err) * if (err)
* return err; * return err;
* err = cbor_encoder_close_container_checked(&encoder, &mapEncoder); * err = cbor_encoder_close_container_checked(&encoder, &mapEncoder);
* if (!err) * if (err)
* return err; * return err;
* *
* size_t len = cbor_encoder_get_buffer_size(&encoder, buf); * size_t len = cbor_encoder_get_buffer_size(&encoder, buf);
@ -157,7 +157,7 @@
* *
* cbor_encoder_init(&encoder, buf, size, 0); * cbor_encoder_init(&encoder, buf, size, 0);
* err = cbor_encoder_create_array(&encoder, &arrayEncoder, n); * err = cbor_encoder_create_array(&encoder, &arrayEncoder, n);
* cbor_assert(err); // can't fail, the buffer is always big enough * cbor_assert(!err); // can't fail, the buffer is always big enough
* *
* for (i = 0; i < n; ++i) { * for (i = 0; i < n; ++i) {
* err = cbor_encode_text_stringz(&arrayEncoder, strings[i]); * err = cbor_encode_text_stringz(&arrayEncoder, strings[i]);
@ -166,7 +166,7 @@
* } * }
* *
* err = cbor_encoder_close_container_checked(&encoder, &arrayEncoder); * err = cbor_encoder_close_container_checked(&encoder, &arrayEncoder);
* cbor_assert(err); // shouldn't fail! * cbor_assert(!err); // shouldn't fail!
* *
* more_bytes = cbor_encoder_get_extra_bytes_needed(encoder); * more_bytes = cbor_encoder_get_extra_bytes_needed(encoder);
* if (more_size) { * if (more_size) {
@ -222,8 +222,8 @@ void cbor_encoder_init_writer(CborEncoder *encoder, CborEncoderWriteFunction wri
static inline void put16(void *where, uint16_t v) static inline void put16(void *where, uint16_t v)
{ {
v = cbor_htons(v); uint16_t v_be = cbor_htons(v);
memcpy(where, &v, sizeof(v)); memcpy(where, &v_be, sizeof(v_be));
} }
/* Note: Since this is currently only used in situations where OOM is the only /* Note: Since this is currently only used in situations where OOM is the only
@ -243,14 +243,14 @@ static inline bool isOomError(CborError err)
static inline void put32(void *where, uint32_t v) static inline void put32(void *where, uint32_t v)
{ {
v = cbor_htonl(v); uint32_t v_be = cbor_htonl(v);
memcpy(where, &v, sizeof(v)); memcpy(where, &v_be, sizeof(v_be));
} }
static inline void put64(void *where, uint64_t v) static inline void put64(void *where, uint64_t v)
{ {
v = cbor_htonll(v); uint64_t v_be = cbor_htonll(v);
memcpy(where, &v, sizeof(v)); memcpy(where, &v_be, sizeof(v_be));
} }
static inline bool would_overflow(CborEncoder *encoder, size_t len) static inline bool would_overflow(CborEncoder *encoder, size_t len)
@ -462,11 +462,10 @@ static CborError encode_string(CborEncoder *encoder, size_t length, uint8_t shif
*/ */
/** /**
* Appends the text string \a string of length \a length to the CBOR stream * Appends the byte string \a string of length \a length to the CBOR stream
* provided by \a encoder. CBOR requires that \a string be valid UTF-8, but * provided by \a encoder. CBOR byte strings are arbitrary raw data.
* TinyCBOR makes no verification of correctness.
* *
* \sa CborError cbor_encode_text_stringz, cbor_encode_byte_string * \sa cbor_encode_text_stringz, cbor_encode_text_string
*/ */
CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length) CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length)
{ {
@ -474,10 +473,11 @@ CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, s
} }
/** /**
* Appends the byte string \a string of length \a length to the CBOR stream * Appends the text string \a string of length \a length to the CBOR stream
* provided by \a encoder. CBOR byte strings are arbitrary raw data. * provided by \a encoder. CBOR requires that \a string be valid UTF-8, but
* TinyCBOR makes no verification of correctness.
* *
* \sa cbor_encode_text_stringz, cbor_encode_text_string * \sa CborError cbor_encode_text_stringz, cbor_encode_byte_string
*/ */
CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length) CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length)
{ {
@ -572,11 +572,10 @@ CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder,
*/ */
CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder) CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder)
{ {
if (encoder->end && !(encoder->flags & CborIteratorFlag_WriterFunction)) // synchronise buffer state with that of the container
encoder->data.ptr = containerEncoder->data.ptr;
else
encoder->data.bytes_needed = containerEncoder->data.bytes_needed;
encoder->end = containerEncoder->end; encoder->end = containerEncoder->end;
encoder->data = containerEncoder->data;
if (containerEncoder->flags & CborIteratorFlag_UnknownLength) if (containerEncoder->flags & CborIteratorFlag_UnknownLength)
return append_byte_to_buffer(encoder, BreakByte); return append_byte_to_buffer(encoder, BreakByte);
@ -585,6 +584,7 @@ CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *
if (!encoder->end) if (!encoder->end)
return CborErrorOutOfMemory; /* keep the state */ return CborErrorOutOfMemory; /* keep the state */
return CborNoError; return CborNoError;
} }

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2018 Intel Corporation ** Copyright (C) 2019 Intel Corporation
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy ** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal ** of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2018 Intel Corporation ** Copyright (C) 2019 Intel Corporation
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy ** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal ** of this software and associated documentation files (the "Software"), to deal
@ -37,15 +37,17 @@
#endif #endif
#ifndef CBOR_NO_HALF_FLOAT_TYPE #ifndef CBOR_NO_HALF_FLOAT_TYPE
# ifdef __F16C__ # if defined(__F16C__) || defined(__AVX2__)
# include <immintrin.h> # include <immintrin.h>
static inline unsigned short encode_half(double val) static inline unsigned short encode_half(float val)
{ {
return _cvtss_sh((float)val, 3); __m128i m = _mm_cvtps_ph(_mm_set_ss(val), _MM_FROUND_CUR_DIRECTION);
return _mm_extract_epi16(m, 0);
} }
static inline double decode_half(unsigned short half) static inline float decode_half(unsigned short half)
{ {
return _cvtsh_ss(half); __m128i m = _mm_cvtsi32_si128(half);
return _mm_cvtss_f32(_mm_cvtph_ps(m));
} }
# else # else
/* software implementation of float-to-fp16 conversions */ /* software implementation of float-to-fp16 conversions */

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2018 Intel Corporation ** Copyright (C) 2019 Intel Corporation
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy ** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal ** of this software and associated documentation files (the "Software"), to deal
@ -913,7 +913,7 @@ CborError cbor_value_get_int_checked(const CborValue *value, int *result)
/** /**
* \fn bool cbor_value_is_byte_string(const CborValue *value) * \fn bool cbor_value_is_byte_string(const CborValue *value)
* *
* Returns true if the iterator \a value is valid and points to a CBOR text * Returns true if the iterator \a value is valid and points to a CBOR byte
* string. CBOR byte strings are binary data with no specified encoding or * string. CBOR byte strings are binary data with no specified encoding or
* format. * format.
* *

View File

@ -44,7 +44,7 @@
# include <stdbool.h> # include <stdbool.h>
#endif #endif
#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L || __cpp_static_assert >= 200410 #if __STDC_VERSION__ >= 201112L || (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(__cpp_static_assert) && __cpp_static_assert >= 200410)
# define cbor_static_assert(x) static_assert(x, #x) # define cbor_static_assert(x) static_assert(x, #x)
#elif !defined(__cplusplus) && defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) && (__STDC_VERSION__ > 199901L) #elif !defined(__cplusplus) && defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) && (__STDC_VERSION__ > 199901L)
# define cbor_static_assert(x) _Static_assert(x, #x) # define cbor_static_assert(x) _Static_assert(x, #x)

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2018 Intel Corporation ** Copyright (C) 2019 Intel Corporation
** **
** Permission is hereby granted, free of charge, to any person obtaining a copy ** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal ** of this software and associated documentation files (the "Software"), to deal
@ -22,8 +22,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QTest> #include <QtTest>
#include <QFloat16>
static float myNaNf() static float myNaNf()
{ {

View File

@ -22,7 +22,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QTest> #include <QtTest>
#include "cbor.h" #include "cbor.h"
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)

View File

@ -1,4 +1,28 @@
#include <QTest> /****************************************************************************
**
** Copyright (C) 2019 Intel Corporation
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
** THE SOFTWARE.
**
****************************************************************************/
#include <QtTest>
#include <limits> #include <limits>
#include <cbor.h> #include <cbor.h>
@ -484,6 +508,7 @@ void addValidationData(size_t minInvalid = ~size_t(0))
QTest::newRow("map-no-break1") << raw("\x81\xbf") << 0 << CborErrorUnexpectedEOF; QTest::newRow("map-no-break1") << raw("\x81\xbf") << 0 << CborErrorUnexpectedEOF;
QTest::newRow("map-no-break2") << raw("\x81\xbf\0\0") << 0 << CborErrorUnexpectedEOF; QTest::newRow("map-no-break2") << raw("\x81\xbf\0\0") << 0 << CborErrorUnexpectedEOF;
QTest::newRow("map-break-after-key") << raw("\x81\xbf\0\xff") << 0 << CborErrorUnexpectedBreak; QTest::newRow("map-break-after-key") << raw("\x81\xbf\0\xff") << 0 << CborErrorUnexpectedBreak;
QTest::newRow("map-break-after-second-key") << raw("\x81\xbf\x64xyzw\x04\x00\xff") << 0 << CborErrorUnexpectedBreak;
QTest::newRow("map-break-after-value-tag") << raw("\x81\xbf\0\xc0\xff") << 0 << CborErrorUnexpectedBreak; QTest::newRow("map-break-after-value-tag") << raw("\x81\xbf\0\xc0\xff") << 0 << CborErrorUnexpectedBreak;
QTest::newRow("map-break-after-value-tag2") << raw("\x81\xbf\0\xd8\x20\xff") << 0 << CborErrorUnexpectedBreak; QTest::newRow("map-break-after-value-tag2") << raw("\x81\xbf\0\xd8\x20\xff") << 0 << CborErrorUnexpectedBreak;

View File

@ -24,7 +24,7 @@
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
#define _DARWIN_C_SOURCE 1 /* need MAP_ANON */ #define _DARWIN_C_SOURCE 1 /* need MAP_ANON */
#include <QTest> #include <QtTest>
#include "cbor.h" #include "cbor.h"
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
@ -894,22 +894,44 @@ static void chunkedStringTest(const QByteArray &data, const QString &concatenate
err = cbor_value_calculate_string_length(&copy, &n); err = cbor_value_calculate_string_length(&copy, &n);
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\""); QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
QByteArray buffer(n, Qt::Uninitialized); size_t nn = n;
QByteArray buffer(n + 1, Qt::Uninitialized);
QByteArray buffer2(n + 1, Qt::Uninitialized);
buffer[int(n)] = 0xff;
buffer2[int(n)] = 0xff;
QString formatted; QString formatted;
if (cbor_value_is_byte_string(&copy)) { if (cbor_value_is_byte_string(&copy)) {
err = cbor_value_copy_byte_string(&copy, (uint8_t *)buffer.data(), &n, nullptr); err = cbor_value_copy_byte_string(&copy, (uint8_t *)buffer.data(), &nn, nullptr);
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\""); QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
QCOMPARE(int(n), buffer.size()); QCOMPARE(nn, n);
formatted = QString::fromLatin1("h'" + buffer.toHex() + '\''); formatted = QString::fromLatin1("h'" + QByteArray::fromRawData(buffer.data(), n).toHex() + '\'');
// repeat by allowing the null termination
nn = n + 1;
err = cbor_value_copy_byte_string(&copy, (uint8_t *)buffer2.data(), &nn, nullptr);
} else { } else {
err = cbor_value_copy_text_string(&copy, buffer.data(), &n, nullptr); err = cbor_value_copy_text_string(&copy, buffer.data(), &n, nullptr);
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\""); QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
QCOMPARE(int(n), buffer.size()); QCOMPARE(nn, n);
formatted = '"' + QString::fromUtf8(buffer.data(), n) + '"'; formatted = '"' + QString::fromUtf8(buffer.data(), n) + '"';
// repeat by allowing the null termination
nn = n + 1;
err = cbor_value_copy_text_string(&copy, buffer2.data(), &nn, nullptr);
} }
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
QCOMPARE(formatted, concatenated); QCOMPARE(formatted, concatenated);
// verify terminators
QCOMPARE(buffer.at(n), char(0xff));
QCOMPARE(buffer2.at(n), '\0');
QCOMPARE(nn, n);
buffer.truncate(n);
buffer2.truncate(n);
QCOMPARE(buffer2, buffer);
} }
// confirm that the extra string we appended is still here // confirm that the extra string we appended is still here
@ -1340,6 +1362,24 @@ void tst_Parser::validation()
QCOMPARE(err2, expectedError); QCOMPARE(err2, expectedError);
QCOMPARE(err3, expectedError); QCOMPARE(err3, expectedError);
} }
// see if we've got a map
if (QByteArray(QTest::currentDataTag()).startsWith("map")) {
w.init(data, uint32_t(flags)); // reinit
QVERIFY(cbor_value_is_array(&w.first));
CborValue map;
CborError err = cbor_value_enter_container(&w.first, &map);
if (err == CborNoError) {
QVERIFY(cbor_value_is_map(&map));
CborValue element;
err = cbor_value_map_find_value(&map, "foobar", &element);
if (err == CborNoError)
QVERIFY(!cbor_value_is_valid(&element));
}
QCOMPARE(err, expectedError);
}
} }
void tst_Parser::strictValidation_data() void tst_Parser::strictValidation_data()