diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | METADATA | 12 | ||||
-rw-r--r-- | include/cn-cbor/cn-cbor.h | 31 | ||||
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/cn-cbor.c | 17 | ||||
-rw-r--r-- | src/cn-create.c | 28 | ||||
-rw-r--r-- | src/cn-encoder.c | 5 | ||||
-rw-r--r-- | test/cbor_test.c | 21 | ||||
-rw-r--r-- | test/test.c | 2 |
9 files changed, 113 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 195c780..e076dc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ option ( coveralls "Generate coveralls data" ON ) option ( coveralls_send "Send data to coveralls site" OFF ) option ( build_docs "Create docs using Doxygen" ${DOXYGEN_FOUND} ) option ( no_floats "Build without floating point support" OFF ) +option ( align_reads "Use memcpy in ntoh*p()" OFF ) set ( dist_dir ${CMAKE_BINARY_DIR}/dist ) set ( prefix ${CMAKE_INSTALL_PREFIX} ) @@ -1,7 +1,5 @@ name: "cn-cbor" -description: - "cn-cbor: A constrained node implementation of CBOR in C" - +description: "cn-cbor: A constrained node implementation of CBOR in C" third_party { url { type: HOMEPAGE @@ -11,7 +9,11 @@ third_party { type: GIT value: "https://github.com/cabo/cn-cbor.git" } - version: "2f9c3b1931eb012909e74f3b628e6a31fd446ad1" - last_upgrade_date { year: 2017 month: 11 day: 8 } + version: "f1cf9ffdf5cfab935a45900556f9b68af925c256" license_type: NOTICE + last_upgrade_date { + year: 2019 + month: 2 + day: 1 + } } diff --git a/include/cn-cbor/cn-cbor.h b/include/cn-cbor/cn-cbor.h index bf71af8..187a55c 100644 --- a/include/cn-cbor/cn-cbor.h +++ b/include/cn-cbor/cn-cbor.h @@ -1,3 +1,4 @@ + /** * \file * \brief @@ -52,6 +53,8 @@ typedef enum cn_cbor_type { CN_CBOR_SIMPLE, /** Doubles, floats, and half-floats */ CN_CBOR_DOUBLE, + /** Floats, and half-floats */ + CN_CBOR_FLOAT, /** An error has occurred */ CN_CBOR_INVALID } cn_cbor_type; @@ -91,6 +94,8 @@ typedef struct cn_cbor { unsigned long uint; /** CN_CBOR_DOUBLE */ double dbl; + /** CN_CBOR_FLOAT */ + float f; /** for use during parsing */ unsigned long count; } v; /* TBD: optimize immediate */ @@ -324,6 +329,32 @@ cn_cbor* cn_cbor_int_create(int64_t value CBOR_CONTEXT, cn_cbor_errback *errp); +#ifndef CBOR_NO_FLOAT +/** + * Create a CBOR float. + * + * @param[in] value the value of the float + * @param[in] CBOR_CONTEXT Allocation context (only if USE_CBOR_CONTEXT is defined) + * @param[out] errp Error, if NULL is returned + * @return The created object, or NULL on error + */ +cn_cbor* cn_cbor_float_create(float value + CBOR_CONTEXT, + cn_cbor_errback *errp); + +/** + * Create a CBOR double. + * + * @param[in] value the value of the double + * @param[in] CBOR_CONTEXT Allocation context (only if USE_CBOR_CONTEXT is defined) + * @param[out] errp Error, if NULL is returned + * @return The created object, or NULL on error + */ +cn_cbor* cn_cbor_double_create(double value + CBOR_CONTEXT, + cn_cbor_errback *errp); +#endif /* CBOR_NO_FLOAT */ + /** * Put a CBOR object into a map with a CBOR object key. Duplicate checks are NOT * currently performed. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ceb0608..babe95c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,6 +10,9 @@ set ( cbor_srcs cn-get.c ) +if (align_reads) + add_definitions(-DCBOR_ALIGN_READS) +endif() if (use_context) add_definitions(-DUSE_CBOR_CONTEXT) endif() diff --git a/src/cn-cbor.c b/src/cn-cbor.c index 8fadf73..2526b92 100644 --- a/src/cn-cbor.c +++ b/src/cn-cbor.c @@ -49,10 +49,25 @@ static double decode_half(int half) { } #endif /* CBOR_NO_FLOAT */ -/* Fix these if you can't do non-aligned reads */ #define ntoh8p(p) (*(unsigned char*)(p)) + +#ifndef CBOR_ALIGN_READS #define ntoh16p(p) (ntohs(*(unsigned short*)(p))) #define ntoh32p(p) (ntohl(*(uint32_t*)(p))) +#else +static uint16_t ntoh16p(unsigned char *p) { + uint16_t tmp; + memcpy(&tmp, p, sizeof(tmp)); + return ntohs(tmp); +} + +static uint32_t ntoh32p(unsigned char *p) { + uint32_t tmp; + memcpy(&tmp, p, sizeof(tmp)); + return ntohl(tmp); +} +#endif /* CBOR_ALIGN_READS */ + static uint64_t ntoh64p(unsigned char *p) { uint64_t ret = ntoh32p(p); ret <<= 32; diff --git a/src/cn-create.c b/src/cn-create.c index bc448e9..4ddce3b 100644 --- a/src/cn-create.c +++ b/src/cn-create.c @@ -73,6 +73,34 @@ cn_cbor* cn_cbor_int_create(int64_t value return ret; } +#ifndef CBOR_NO_FLOAT +cn_cbor* cn_cbor_float_create(float value + CBOR_CONTEXT, + cn_cbor_errback *errp) +{ + cn_cbor* ret; + INIT_CB(ret); + + ret->type = CN_CBOR_FLOAT; + ret->v.f = value; + + return ret; +} + +cn_cbor* cn_cbor_double_create(double value + CBOR_CONTEXT, + cn_cbor_errback *errp) +{ + cn_cbor* ret; + INIT_CB(ret); + + ret->type = CN_CBOR_DOUBLE; + ret->v.dbl = value; + + return ret; +} +#endif /* CBOR_NO_FLOAT */ + static bool _append_kv(cn_cbor *cb_map, cn_cbor *key, cn_cbor *val) { //Connect key and value and insert them into the map. diff --git a/src/cn-encoder.c b/src/cn-encoder.c index 8593b39..d8a4d49 100644 --- a/src/cn-encoder.c +++ b/src/cn-encoder.c @@ -276,6 +276,11 @@ void _encoder_visitor(const cn_cbor *cb, int depth, void *context) CHECK(_write_double(ws, cb->v.dbl)); #endif /* CBOR_NO_FLOAT */ break; + case CN_CBOR_FLOAT: +#ifndef CBOR_NO_FLOAT + CHECK(_write_double(ws, cb->v.f)); +#endif /* CBOR_NO_FLOAT */ + break; case CN_CBOR_INVALID: ws->offset = -1; diff --git a/test/cbor_test.c b/test/cbor_test.c index 3326497..eafea5d 100644 --- a/test/cbor_test.c +++ b/test/cbor_test.c @@ -327,6 +327,9 @@ CTEST(cbor, create) cn_cbor *cb_map = cn_cbor_map_create(CONTEXT_NULL_COMMA &err); cn_cbor *cb_int; cn_cbor *cb_data; +#ifndef CBOR_NO_FLOAT + cn_cbor *cb_dbl; +#endif ASSERT_NOT_NULL(cb_map); ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR); @@ -339,6 +342,12 @@ CTEST(cbor, create) ASSERT_NOT_NULL(cb_data); ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR); +#ifndef CBOR_NO_FLOAT + cb_dbl = cn_cbor_double_create(3.14159 CONTEXT_NULL, &err); + ASSERT_NOT_NULL(cb_dbl); + ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR); +#endif + cn_cbor_mapput_int(cb_map, 5, cb_int CONTEXT_NULL, &err); ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR); ASSERT_TRUE(cb_map->length == 2); @@ -360,6 +369,12 @@ CTEST(cbor, create) ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR); ASSERT_TRUE(cb_map->length == 8); +#ifndef CBOR_NO_FLOAT + cn_cbor_mapput_int(cb_map, 42, cb_dbl CONTEXT_NULL, &err); + ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR); + ASSERT_TRUE(cb_map->length == 10); +#endif + val = cn_cbor_mapget_int(cb_map, 5); ASSERT_NOT_NULL(val); ASSERT_TRUE(val->v.sint == 256); @@ -368,6 +383,12 @@ CTEST(cbor, create) ASSERT_NOT_NULL(val); ASSERT_STR(val->v.str, "abc"); +#ifndef CBOR_NO_FLOAT + val = cn_cbor_mapget_int(cb_map, 42); + ASSERT_NOT_NULL(val); + ASSERT_TRUE(val->v.dbl > 3.14 && val->v.dbl < 3.15); +#endif + cn_cbor_free(cb_map CONTEXT_NULL); } diff --git a/test/test.c b/test/test.c index d24992f..b7d85af 100644 --- a/test/test.c +++ b/test/test.c @@ -109,7 +109,7 @@ static void cn_cbor_decode_test(const unsigned char *buf, int len) { printf("%s at %d\n", err_name[back.err], back.pos); } -int main() { +int main(void) { char buf[100000]; unsigned char *end; char *bufend; |