aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt3
-rw-r--r--src/cn-cbor.c17
-rw-r--r--src/cn-create.c28
-rw-r--r--src/cn-encoder.c5
4 files changed, 52 insertions, 1 deletions
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;