Merge branch 'master' into complete
diff --git a/include/cn-cbor/cn-cbor.h b/include/cn-cbor/cn-cbor.h
index 9a36ac5..0416bd2 100644
--- a/include/cn-cbor/cn-cbor.h
+++ b/include/cn-cbor/cn-cbor.h
@@ -1,3 +1,4 @@
+
/**
* \file
* \brief
@@ -69,6 +70,8 @@
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;
@@ -116,6 +119,8 @@
#endif
/** CN_CBOR_DOUBLE */
double dbl;
+ /** CN_CBOR_FLOAT */
+ float f;
/** for use during parsing */
#ifdef _MSC_VER
uint64_t count;
@@ -370,6 +375,32 @@
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/cn-cbor.c b/src/cn-cbor.c
index 35f7e0c..7da5f88 100644
--- a/src/cn-cbor.c
+++ b/src/cn-cbor.c
@@ -56,10 +56,25 @@
}
#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(*(unsigned long*)(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 a6f6b5b..c9ab816 100644
--- a/src/cn-create.c
+++ b/src/cn-create.c
@@ -78,6 +78,34 @@
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 76ff50b..18de686 100644
--- a/src/cn-encoder.c
+++ b/src/cn-encoder.c
@@ -302,6 +302,11 @@
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 857363b..3888723 100644
--- a/test/cbor_test.c
+++ b/test/cbor_test.c
@@ -354,6 +354,9 @@
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);
@@ -366,6 +369,12 @@
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);
@@ -387,6 +396,12 @@
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);
@@ -395,6 +410,12 @@
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 @@
printf("%s at %d\n", err_name[back.err], back.pos);
}
-int main() {
+int main(void) {
char buf[100000];
unsigned char *end;
char *bufend;