From cdd360209514cc181e73266bf55e31408853a9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Tempel?= Date: Fri, 2 Feb 2018 11:31:16 +0100 Subject: test: Fix declaration of main method --- test/test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; -- cgit v1.2.3 From 2703d2d101deae1815d26fb26fb676902d27a58e Mon Sep 17 00:00:00 2001 From: Vincent Dupont Date: Thu, 22 Feb 2018 12:37:20 +0100 Subject: cn-create: add cn_cbor_double_create --- include/cn-cbor/cn-cbor.h | 15 +++++++++++++++ src/cn-create.c | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/cn-cbor/cn-cbor.h b/include/cn-cbor/cn-cbor.h index bf71af8..cde9dfc 100644 --- a/include/cn-cbor/cn-cbor.h +++ b/include/cn-cbor/cn-cbor.h @@ -1,3 +1,4 @@ + /** * \file * \brief @@ -324,6 +325,20 @@ cn_cbor* cn_cbor_int_create(int64_t value CBOR_CONTEXT, cn_cbor_errback *errp); +#ifndef CBOR_NO_FLOAT +/** + * 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/cn-create.c b/src/cn-create.c index bc448e9..84a6ee9 100644 --- a/src/cn-create.c +++ b/src/cn-create.c @@ -73,6 +73,21 @@ cn_cbor* cn_cbor_int_create(int64_t value return ret; } +#ifndef CBOR_NO_FLOAT +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. -- cgit v1.2.3 From 7bb482d7032dae3588ecd13f270d525b38e9bb21 Mon Sep 17 00:00:00 2001 From: Vincent Dupont Date: Mon, 26 Feb 2018 15:43:39 +0100 Subject: test: add create double test --- test/cbor_test.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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); } -- cgit v1.2.3 From 0301ef69c312cc663a6d3014c8831cfb2cc790ff Mon Sep 17 00:00:00 2001 From: Vincent Dupont Date: Mon, 26 Feb 2018 16:28:27 +0100 Subject: cn-create: add cn_cbor_float_create --- include/cn-cbor/cn-cbor.h | 16 ++++++++++++++++ src/cn-create.c | 13 +++++++++++++ src/cn-encoder.c | 5 +++++ 3 files changed, 34 insertions(+) diff --git a/include/cn-cbor/cn-cbor.h b/include/cn-cbor/cn-cbor.h index cde9dfc..187a55c 100644 --- a/include/cn-cbor/cn-cbor.h +++ b/include/cn-cbor/cn-cbor.h @@ -53,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; @@ -92,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 */ @@ -326,6 +330,18 @@ cn_cbor* cn_cbor_int_create(int64_t value 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. * diff --git a/src/cn-create.c b/src/cn-create.c index 84a6ee9..4ddce3b 100644 --- a/src/cn-create.c +++ b/src/cn-create.c @@ -74,6 +74,19 @@ cn_cbor* cn_cbor_int_create(int64_t value } #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) 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 @@ -274,6 +274,11 @@ void _encoder_visitor(const cn_cbor *cb, int depth, void *context) case CN_CBOR_DOUBLE: #ifndef CBOR_NO_FLOAT 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; -- cgit v1.2.3 From 894e0b468dffc0c2f8f9380337304e5fd8cf1ea4 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 16 Mar 2018 15:33:00 +0100 Subject: implement alignment-safe ntoh16p() && ntoh32p() --- src/cn-cbor.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/cn-cbor.c b/src/cn-cbor.c index a7677ae..400de5f 100644 --- a/src/cn-cbor.c +++ b/src/cn-cbor.c @@ -51,8 +51,18 @@ static double decode_half(int half) { /* Fix these if you can't do non-aligned reads */ #define ntoh8p(p) (*(unsigned char*)(p)) -#define ntoh16p(p) (ntohs(*(unsigned short*)(p))) -#define ntoh32p(p) (ntohl(*(unsigned long*)(p))) +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); +} + static uint64_t ntoh64p(unsigned char *p) { uint64_t ret = ntoh32p(p); ret <<= 32; -- cgit v1.2.3 From 94e6919a5eba61706f1808c78304b9cdfc06b88f Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 23 Mar 2018 22:08:26 +0100 Subject: cmake: add align_reads -> -DCBOR_ALIGN_READS --- CMakeLists.txt | 1 + src/CMakeLists.txt | 3 +++ 2 files changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 195c780..31ef545 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_memreads "Use memcpy in ntoh*p()" OFF ) set ( dist_dir ${CMAKE_BINARY_DIR}/dist ) set ( prefix ${CMAKE_INSTALL_PREFIX} ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ceb0608..4826889 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,6 +10,9 @@ set ( cbor_srcs cn-get.c ) +if (align_memreads) + add_definitions(-DCBOR_ALIGN_READS) +endif() if (use_context) add_definitions(-DUSE_CBOR_CONTEXT) endif() -- cgit v1.2.3 From 6cd105dc93122d343e197bfac1d266ecab101a4f Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 23 Mar 2018 22:10:27 +0100 Subject: fixup! cmake: add align_reads -> -DCBOR_ALIGN_READS --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4826889..babe95c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,7 +10,7 @@ set ( cbor_srcs cn-get.c ) -if (align_memreads) +if (align_reads) add_definitions(-DCBOR_ALIGN_READS) endif() if (use_context) -- cgit v1.2.3 From 3d8c1e859e170cdca1a66bc4124fccba9a47c853 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 23 Mar 2018 22:10:27 +0100 Subject: fixup! cmake: add align_reads -> -DCBOR_ALIGN_READS --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 31ef545..e076dc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +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_memreads "Use memcpy in ntoh*p()" OFF ) +option ( align_reads "Use memcpy in ntoh*p()" OFF ) set ( dist_dir ${CMAKE_BINARY_DIR}/dist ) set ( prefix ${CMAKE_INSTALL_PREFIX} ) -- cgit v1.2.3 From 6afc222e0b4373b04fde5b94742a224a72c57cca Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 23 Mar 2018 22:10:41 +0100 Subject: fixup! implement alignment-safe ntoh16p() && ntoh32p() --- src/cn-cbor.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cn-cbor.c b/src/cn-cbor.c index 400de5f..b5759e6 100644 --- a/src/cn-cbor.c +++ b/src/cn-cbor.c @@ -49,8 +49,12 @@ 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_MEMREADS +#define ntoh16p(p) (ntohs(*(unsigned short*)(p))) +#define ntoh32p(p) (ntohl(*(unsigned long*)(p))) +#else static uint16_t ntoh16p(unsigned char *p) { uint16_t tmp; memcpy(&tmp, p, sizeof(tmp)); @@ -62,6 +66,7 @@ static uint32_t ntoh32p(unsigned char *p) { memcpy(&tmp, p, sizeof(tmp)); return ntohl(tmp); } +#endif static uint64_t ntoh64p(unsigned char *p) { uint64_t ret = ntoh32p(p); -- cgit v1.2.3 From d458c34ab356b860ca8700023c76a402694dd3d0 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 23 Mar 2018 22:14:36 +0100 Subject: fixup! fixup! implement alignment-safe ntoh16p() && ntoh32p() --- src/cn-cbor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cn-cbor.c b/src/cn-cbor.c index b5759e6..9093537 100644 --- a/src/cn-cbor.c +++ b/src/cn-cbor.c @@ -51,7 +51,7 @@ static double decode_half(int half) { #define ntoh8p(p) (*(unsigned char*)(p)) -#ifndef CBOR_ALIGN_MEMREADS +#ifndef CBOR_ALIGN_READS #define ntoh16p(p) (ntohs(*(unsigned short*)(p))) #define ntoh32p(p) (ntohl(*(unsigned long*)(p))) #else @@ -66,7 +66,7 @@ static uint32_t ntoh32p(unsigned char *p) { memcpy(&tmp, p, sizeof(tmp)); return ntohl(tmp); } -#endif +#endif /* CBOR_ALIGN_READS */ static uint64_t ntoh64p(unsigned char *p) { uint64_t ret = ntoh32p(p); -- cgit v1.2.3