Apply formatting updates.
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..5e6bc74
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,14 @@
+---
+BasedOnStyle: Chromium
+IndentWidth: 4
+TabWidth: 4
+UseTab: Always
+DerivePointerAlignment: true
+BreakBeforeBraces: Custom
+BraceWrapping:
+ AfterFunction: true
+ BeforeCatch: false
+ BeforeElse: true
+AlignAfterOpenBracket: false
+SortIncludes: false
+ColumnLimit: 120
diff --git a/.clang-tidy b/.clang-tidy
new file mode 100644
index 0000000..d3e1aa5
--- /dev/null
+++ b/.clang-tidy
@@ -0,0 +1,35 @@
+Checks: "*, \
+###################, \
+# ENABLED CHECKS , \
+###################, \
+modernize-*, \
+cppcoreguidelines-*, \
+hicpp-*, \
+google-*, \
+bugprone-*, \
+llvm-*, \
+performance-*, \
+readability-*, \
+clang-analyzer-*, \
+zircon-*, \
+portability-*, \
+misc-*, \
+boost-*, \
+abseil-*, \
+###################, \
+# DISABLED CHECKS , \
+###################, \
+-fuchsia-default-arguments, \
+-readability-container-size-empty, \
+-google-readability-namespace-comments, \
+-llvm-namespace-comment, \
+-modernize-use-trailing-return-type, \
+-fuchsia-default-arguments-calls, \
+# TODO remove following lines, \
+-readability-braces-around-statements, \
+-cppcoreguidelines-avoid-magic-numbers, \
+-hicpp-braces-around-statements, \
+-readability-magic-numbers, \
+-cppcoreguidelines-avoid-magic-numbers, \
+-cppcoreguidelines-init-variables, \
+"
diff --git a/cmake-format.yaml b/cmake-format.yaml
new file mode 100644
index 0000000..8086dd4
--- /dev/null
+++ b/cmake-format.yaml
@@ -0,0 +1,20 @@
+# https://github.com/cheshirekow/cmake_format
+
+# usage:
+
+# pip install cmake_format --upgrade
+# ./scripts/format_cmake.sh
+
+# How wide to allow formatted cmake files
+line_width: 120
+
+# How many spaces to tab for indent
+tab_size: 2
+
+# Format command names consistently as 'lower' or 'upper' case
+command_case: "lower"
+
+first_comment_is_literal: False
+
+# enable comment markup parsing and reflow
+enable_markup: False
diff --git a/src/cbor.h b/src/cbor.h
index 6329477..c392286 100644
--- a/src/cbor.h
+++ b/src/cbor.h
@@ -8,53 +8,53 @@
/* The 8 major types */
#define MT_UNSIGNED 0
#define MT_NEGATIVE 1
-#define MT_BYTES 2
-#define MT_TEXT 3
-#define MT_ARRAY 4
-#define MT_MAP 5
-#define MT_TAG 6
-#define MT_PRIM 7
+#define MT_BYTES 2
+#define MT_TEXT 3
+#define MT_ARRAY 4
+#define MT_MAP 5
+#define MT_TAG 6
+#define MT_PRIM 7
/* The initial bytes resulting from those */
#define IB_UNSIGNED (MT_UNSIGNED << 5)
#define IB_NEGATIVE (MT_NEGATIVE << 5)
-#define IB_BYTES (MT_BYTES << 5)
-#define IB_TEXT (MT_TEXT << 5)
-#define IB_ARRAY (MT_ARRAY << 5)
-#define IB_MAP (MT_MAP << 5)
-#define IB_TAG (MT_TAG << 5)
-#define IB_PRIM (MT_PRIM << 5)
+#define IB_BYTES (MT_BYTES << 5)
+#define IB_TEXT (MT_TEXT << 5)
+#define IB_ARRAY (MT_ARRAY << 5)
+#define IB_MAP (MT_MAP << 5)
+#define IB_TAG (MT_TAG << 5)
+#define IB_PRIM (MT_PRIM << 5)
#define IB_NEGFLAG (IB_NEGATIVE - IB_UNSIGNED)
#define IB_NEGFLAG_AS_BIT(ib) ((ib) >> 5)
#define IB_TEXTFLAG (IB_TEXT - IB_BYTES)
-#define IB_AI(ib) ((ib) & 0x1F)
+#define IB_AI(ib) ((ib)&0x1F)
#define IB_MT(ib) ((ib) >> 5)
/* Tag numbers handled by this implementation */
#define TAG_TIME_EPOCH 1
-#define TAG_BIGNUM 2
+#define TAG_BIGNUM 2
#define TAG_BIGNUM_NEG 3
-#define TAG_URI 32
-#define TAG_RE 35
+#define TAG_URI 32
+#define TAG_RE 35
/* Initial bytes of those tag numbers */
#define IB_TIME_EPOCH (IB_TAG | TAG_TIME_EPOCH)
-#define IB_BIGNUM (IB_TAG | TAG_BIGNUM)
+#define IB_BIGNUM (IB_TAG | TAG_BIGNUM)
#define IB_BIGNUM_NEG (IB_TAG | TAG_BIGNUM_NEG)
/* TAG_URI and TAG_RE are non-immediate tags */
/* Simple values handled by this implementation */
#define VAL_FALSE 20
-#define VAL_TRUE 21
-#define VAL_NIL 22
+#define VAL_TRUE 21
+#define VAL_NIL 22
#define VAL_UNDEF 23
/* Initial bytes of those simple values */
#define IB_FALSE (IB_PRIM | VAL_FALSE)
-#define IB_TRUE (IB_PRIM | VAL_TRUE)
-#define IB_NIL (IB_PRIM | VAL_NIL)
+#define IB_TRUE (IB_PRIM | VAL_TRUE)
+#define IB_NIL (IB_PRIM | VAL_NIL)
#define IB_UNDEF (IB_PRIM | VAL_UNDEF)
/* AI values with more data in head */
@@ -82,18 +82,15 @@
* @param[in] ctx The allocation context, or NULL for calloc.
* @return A pointer to a `cn_cbor` or NULL on failure
*/
-#define CN_CALLOC(ctx) ((ctx) && (ctx)->calloc_func) ? \
- (ctx)->calloc_func(1, sizeof(cn_cbor), (ctx)->context) : \
- calloc(1, sizeof(cn_cbor));
+#define CN_CALLOC(ctx) \
+ ((ctx) && (ctx)->calloc_func) ? (ctx)->calloc_func(1, sizeof(cn_cbor), (ctx)->context) : calloc(1, sizeof(cn_cbor));
/**
* Free a
* @param free_func [description]
* @return [description]
*/
-#define CN_FREE(ptr, ctx) ((ctx) && (ctx)->free_func) ? \
- (ctx)->free_func((ptr), (ctx)->context) : \
- free((ptr));
+#define CN_FREE(ptr, ctx) ((ctx) && (ctx)->free_func) ? (ctx)->free_func((ptr), (ctx)->context) : free((ptr));
#define CBOR_CONTEXT_PARAM , context
#define CN_CALLOC_CONTEXT() CN_CALLOC(context)
@@ -113,10 +110,10 @@
#define CN_FREE free
#endif
-#endif // USE_CBOR_CONTEXT
+#endif // USE_CBOR_CONTEXT
#ifndef UNUSED_PARAM
#define UNUSED_PARAM(p) ((void)&(p))
#endif
-#endif // CBOR_PROTOCOL_H__
+#endif // CBOR_PROTOCOL_H__
diff --git a/src/cn-cbor.c b/src/cn-cbor.c
index d837606..aca6b97 100644
--- a/src/cn-cbor.c
+++ b/src/cn-cbor.c
@@ -1,7 +1,7 @@
#ifndef CN_CBOR_C
#define CN_CBOR_C
-#ifdef __cplusplus
+#ifdef __cplusplus
extern "C" {
#endif
#ifdef EMACS_INDENTATION_HELPER
@@ -17,281 +17,324 @@
#include <WinSock2.h> // needed for ntohl on Windows
#else
#ifndef __MBED__
-#include <arpa/inet.h> // needed for ntohl (e.g.) on Linux
+#include <arpa/inet.h> // needed for ntohl (e.g.) on Linux
#endif
#include "dll-export.h"
-#endif // _MSC_VER
+#endif // _MSC_VER
#include "cn-cbor/cn-cbor.h"
#include "cbor.h"
-#define CN_CBOR_FAIL(code) do { pb->err = code; goto fail; } while(0)
+#define CN_CBOR_FAIL(code) \
+ do { \
+ pb->err = code; \
+ goto fail; \
+ } while (0)
MYLIB_EXPORT
-void cn_cbor_free(cn_cbor* cb CBOR_CONTEXT) {
- cn_cbor* p = (cn_cbor*) cb;
- assert(!p || !p->parent);
- while (p) {
- cn_cbor* p1;
- while ((p1 = p->first_child)) { /* go down */
- p = p1;
- }
- if (!(p1 = p->next)) { /* go up next */
- if ((p1 = p->parent))
- p1->first_child = 0;
- }
- CN_CBOR_FREE_CONTEXT(p);
- p = p1;
- }
+void cn_cbor_free(cn_cbor *cb CBOR_CONTEXT)
+{
+ cn_cbor *p = (cn_cbor *)cb;
+ assert(!p || !p->parent);
+ while (p) {
+ cn_cbor *p1;
+ while ((p1 = p->first_child)) { /* go down */
+ p = p1;
+ }
+ if (!(p1 = p->next)) { /* go up next */
+ if ((p1 = p->parent))
+ p1->first_child = 0;
+ }
+ CN_CBOR_FREE_CONTEXT(p);
+ p = p1;
+ }
}
#ifndef CBOR_NO_FLOAT
-static double decode_half(int half) {
- int exp = (half >> 10) & 0x1f;
- int mant = half & 0x3ff;
- double val;
- if (exp == 0) val = ldexp(mant, -24);
- else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
- else val = mant == 0 ? INFINITY : NAN;
- return half & 0x8000 ? -val : val;
+static double decode_half(int half)
+{
+ int exp = (half >> 10) & 0x1f;
+ int mant = half & 0x3ff;
+ double val;
+ if (exp == 0)
+ val = ldexp(mant, -24);
+ else if (exp != 31)
+ val = ldexp(mant + 1024, exp - 25);
+ else
+ val = mant == 0 ? INFINITY : NAN;
+ return half & 0x8000 ? -val : val;
}
#endif /* CBOR_NO_FLOAT */
-#define ntoh8p(p) (*(unsigned char*)(p))
+#define ntoh8p(p) (*(unsigned char *)(p))
#ifndef CBOR_ALIGN_READS
-#define ntoh16p(p) (ntohs(*(unsigned short*)(p)))
-#define ntoh32p(p) (ntohl(*(unsigned long*)(p)))
+#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 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 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;
- ret += ntoh32p(p+4);
- return ret;
+static uint64_t ntoh64p(unsigned char *p)
+{
+ uint64_t ret = ntoh32p(p);
+ ret <<= 32;
+ ret += ntoh32p(p + 4);
+ return ret;
}
static cn_cbor_type mt_trans[] = {
- CN_CBOR_UINT, CN_CBOR_INT,
- CN_CBOR_BYTES, CN_CBOR_TEXT,
- CN_CBOR_ARRAY, CN_CBOR_MAP,
- CN_CBOR_TAG, CN_CBOR_SIMPLE,
+ CN_CBOR_UINT,
+ CN_CBOR_INT,
+ CN_CBOR_BYTES,
+ CN_CBOR_TEXT,
+ CN_CBOR_ARRAY,
+ CN_CBOR_MAP,
+ CN_CBOR_TAG,
+ CN_CBOR_SIMPLE,
};
struct parse_buf {
- unsigned char *buf;
- unsigned char *ebuf;
- cn_cbor_error err;
+ unsigned char *buf;
+ unsigned char *ebuf;
+ cn_cbor_error err;
};
-#define TAKE(pos, ebuf, n, stmt) \
- if (n > (size_t)(ebuf - pos)) \
- CN_CBOR_FAIL(CN_CBOR_ERR_OUT_OF_DATA); \
- stmt; \
- pos += n;
+#define TAKE(pos, ebuf, n, stmt) \
+ if (n > (size_t)(ebuf - pos)) \
+ CN_CBOR_FAIL(CN_CBOR_ERR_OUT_OF_DATA); \
+ stmt; \
+ pos += n;
-static cn_cbor *decode_item (struct parse_buf *pb CBOR_CONTEXT, cn_cbor* top_parent) {
- unsigned char *pos = pb->buf;
- unsigned char *ebuf = pb->ebuf;
- cn_cbor* parent = top_parent;
- int ib;
- unsigned int mt;
- int ai;
- uint64_t val;
- cn_cbor* cb = NULL;
+static cn_cbor *decode_item(struct parse_buf *pb CBOR_CONTEXT, cn_cbor *top_parent)
+{
+ unsigned char *pos = pb->buf;
+ unsigned char *ebuf = pb->ebuf;
+ cn_cbor *parent = top_parent;
+ int ib;
+ unsigned int mt;
+ int ai;
+ uint64_t val;
+ cn_cbor *cb = NULL;
#ifndef CBOR_NO_FLOAT
- union {
- float f;
- uint32_t u;
- } u32;
- union {
- double d;
- uint64_t u;
- } u64;
+ union {
+ float f;
+ uint32_t u;
+ } u32;
+ union {
+ double d;
+ uint64_t u;
+ } u64;
#endif /* CBOR_NO_FLOAT */
again:
- TAKE(pos, ebuf, 1, ib = ntoh8p(pos) );
- if (ib == IB_BREAK) {
- if (!(parent->flags & CN_CBOR_FL_INDEF))
- CN_CBOR_FAIL(CN_CBOR_ERR_BREAK_OUTSIDE_INDEF);
- switch (parent->type) {
- case CN_CBOR_BYTES: case CN_CBOR_TEXT:
- parent->type += 2; /* CN_CBOR_* -> CN_CBOR_*_CHUNKED */
- break;
- case CN_CBOR_MAP:
- if (parent->length & 1)
- CN_CBOR_FAIL(CN_CBOR_ERR_ODD_SIZE_INDEF_MAP);
- default:;
- }
- goto complete;
- }
- mt = ib >> 5;
- ai = ib & 0x1f;
- val = ai;
+ TAKE(pos, ebuf, 1, ib = ntoh8p(pos));
+ if (ib == IB_BREAK) {
+ if (!(parent->flags & CN_CBOR_FL_INDEF))
+ CN_CBOR_FAIL(CN_CBOR_ERR_BREAK_OUTSIDE_INDEF);
+ switch (parent->type) {
+ case CN_CBOR_BYTES:
+ case CN_CBOR_TEXT:
+ parent->type += 2; /* CN_CBOR_* -> CN_CBOR_*_CHUNKED */
+ break;
+ case CN_CBOR_MAP:
+ if (parent->length & 1)
+ CN_CBOR_FAIL(CN_CBOR_ERR_ODD_SIZE_INDEF_MAP);
+ default:;
+ }
+ goto complete;
+ }
+ mt = ib >> 5;
+ ai = ib & 0x1f;
+ val = ai;
- cb = CN_CALLOC_CONTEXT();
- if (!cb)
- CN_CBOR_FAIL(CN_CBOR_ERR_OUT_OF_MEMORY);
+ cb = CN_CALLOC_CONTEXT();
+ if (!cb)
+ CN_CBOR_FAIL(CN_CBOR_ERR_OUT_OF_MEMORY);
- cb->type = mt_trans[mt];
+ cb->type = mt_trans[mt];
- cb->parent = parent;
- if (parent->last_child) {
- parent->last_child->next = cb;
- } else {
- parent->first_child = cb;
- }
- parent->last_child = cb;
- parent->length++;
+ cb->parent = parent;
+ if (parent->last_child) {
+ parent->last_child->next = cb;
+ }
+ else {
+ parent->first_child = cb;
+ }
+ parent->last_child = cb;
+ parent->length++;
- switch (ai) {
- case AI_1: TAKE(pos, ebuf, 1, val = ntoh8p(pos)) ; break;
- case AI_2: TAKE(pos, ebuf, 2, val = ntoh16p(pos)) ; break;
- case AI_4: TAKE(pos, ebuf, 4, val = ntoh32p(pos)) ; break;
- case AI_8: TAKE(pos, ebuf, 8, val = ntoh64p(pos)) ; break;
- case 28: case 29: case 30: CN_CBOR_FAIL(CN_CBOR_ERR_RESERVED_AI);
- case AI_INDEF:
- if ((mt - MT_BYTES) <= MT_MAP) {
- cb->flags |= CN_CBOR_FL_INDEF;
- goto push;
- } else {
- CN_CBOR_FAIL(CN_CBOR_ERR_MT_UNDEF_FOR_INDEF);
- }
- }
- // process content
- switch (mt) {
- case MT_UNSIGNED:
- cb->v.uint = val; /* to do: Overflow check */
- break;
- case MT_NEGATIVE:
- cb->v.sint = ~val; /* to do: Overflow check */
- break;
- case MT_BYTES: case MT_TEXT:
- cb->v.str = (char *) pos;
- cb->length = (size_t) val;
- TAKE(pos, ebuf, val, ;);
- break;
- case MT_MAP:
- val <<= 1;
- /* fall through */
- case MT_ARRAY:
- if ((cb->v.count = val)) {
- cb->flags |= CN_CBOR_FL_COUNT;
- goto push;
- }
- break;
- case MT_TAG:
- cb->v.uint = val;
- goto push;
- case MT_PRIM:
- switch (ai) {
- case VAL_FALSE: cb->type = CN_CBOR_FALSE; break;
- case VAL_TRUE: cb->type = CN_CBOR_TRUE; break;
- case VAL_NIL: cb->type = CN_CBOR_NULL; break;
- case VAL_UNDEF: cb->type = CN_CBOR_UNDEF; break;
- case AI_2:
+ switch (ai) {
+ case AI_1:
+ TAKE(pos, ebuf, 1, val = ntoh8p(pos));
+ break;
+ case AI_2:
+ TAKE(pos, ebuf, 2, val = ntoh16p(pos));
+ break;
+ case AI_4:
+ TAKE(pos, ebuf, 4, val = ntoh32p(pos));
+ break;
+ case AI_8:
+ TAKE(pos, ebuf, 8, val = ntoh64p(pos));
+ break;
+ case 28:
+ case 29:
+ case 30:
+ CN_CBOR_FAIL(CN_CBOR_ERR_RESERVED_AI);
+ case AI_INDEF:
+ if ((mt - MT_BYTES) <= MT_MAP) {
+ cb->flags |= CN_CBOR_FL_INDEF;
+ goto push;
+ }
+ else {
+ CN_CBOR_FAIL(CN_CBOR_ERR_MT_UNDEF_FOR_INDEF);
+ }
+ }
+ // process content
+ switch (mt) {
+ case MT_UNSIGNED:
+ cb->v.uint = val; /* to do: Overflow check */
+ break;
+ case MT_NEGATIVE:
+ cb->v.sint = ~val; /* to do: Overflow check */
+ break;
+ case MT_BYTES:
+ case MT_TEXT:
+ cb->v.str = (char *)pos;
+ cb->length = (size_t)val;
+ TAKE(pos, ebuf, val, ;);
+ break;
+ case MT_MAP:
+ val <<= 1;
+ /* fall through */
+ case MT_ARRAY:
+ if ((cb->v.count = val)) {
+ cb->flags |= CN_CBOR_FL_COUNT;
+ goto push;
+ }
+ break;
+ case MT_TAG:
+ cb->v.uint = val;
+ goto push;
+ case MT_PRIM:
+ switch (ai) {
+ case VAL_FALSE:
+ cb->type = CN_CBOR_FALSE;
+ break;
+ case VAL_TRUE:
+ cb->type = CN_CBOR_TRUE;
+ break;
+ case VAL_NIL:
+ cb->type = CN_CBOR_NULL;
+ break;
+ case VAL_UNDEF:
+ cb->type = CN_CBOR_UNDEF;
+ break;
+ case AI_2:
#ifndef CBOR_NO_FLOAT
- cb->type = CN_CBOR_DOUBLE;
- cb->v.dbl = decode_half(val);
-#else /* CBOR_NO_FLOAT */
- CN_CBOR_FAIL(CN_CBOR_ERR_FLOAT_NOT_SUPPORTED);
+ cb->type = CN_CBOR_DOUBLE;
+ cb->v.dbl = decode_half(val);
+#else /* CBOR_NO_FLOAT */
+ CN_CBOR_FAIL(CN_CBOR_ERR_FLOAT_NOT_SUPPORTED);
#endif /* CBOR_NO_FLOAT */
- break;
- case AI_4:
+ break;
+ case AI_4:
#ifndef CBOR_NO_FLOAT
- cb->type = CN_CBOR_DOUBLE;
- u32.u = (uint32_t) val;
- cb->v.dbl = u32.f;
-#else /* CBOR_NO_FLOAT */
- CN_CBOR_FAIL(CN_CBOR_ERR_FLOAT_NOT_SUPPORTED);
+ cb->type = CN_CBOR_DOUBLE;
+ u32.u = (uint32_t)val;
+ cb->v.dbl = u32.f;
+#else /* CBOR_NO_FLOAT */
+ CN_CBOR_FAIL(CN_CBOR_ERR_FLOAT_NOT_SUPPORTED);
#endif /* CBOR_NO_FLOAT */
- break;
- case AI_8:
+ break;
+ case AI_8:
#ifndef CBOR_NO_FLOAT
- cb->type = CN_CBOR_DOUBLE;
- u64.u = val;
- cb->v.dbl = u64.d;
-#else /* CBOR_NO_FLOAT */
- CN_CBOR_FAIL(CN_CBOR_ERR_FLOAT_NOT_SUPPORTED);
+ cb->type = CN_CBOR_DOUBLE;
+ u64.u = val;
+ cb->v.dbl = u64.d;
+#else /* CBOR_NO_FLOAT */
+ CN_CBOR_FAIL(CN_CBOR_ERR_FLOAT_NOT_SUPPORTED);
#endif /* CBOR_NO_FLOAT */
- break;
- default: cb->v.uint = val;
- }
- }
-fill: /* emulate loops */
- if (parent->flags & CN_CBOR_FL_INDEF) {
- if (parent->type == CN_CBOR_BYTES || parent->type == CN_CBOR_TEXT)
- if (cb->type != parent->type)
- CN_CBOR_FAIL(CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING);
- goto again;
- }
- if (parent->flags & CN_CBOR_FL_COUNT) {
- if (--parent->v.count)
- goto again;
- }
- /* so we are done filling parent. */
-complete: /* emulate return from call */
- if (parent == top_parent) {
- if (pos != ebuf) /* XXX do this outside */
- CN_CBOR_FAIL(CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED);
- pb->buf = pos;
- return cb;
- }
- cb = parent;
- parent = parent->parent;
- goto fill;
-push: /* emulate recursive call */
- parent = cb;
- goto again;
+ break;
+ default:
+ cb->v.uint = val;
+ }
+ }
+fill: /* emulate loops */
+ if (parent->flags & CN_CBOR_FL_INDEF) {
+ if (parent->type == CN_CBOR_BYTES || parent->type == CN_CBOR_TEXT)
+ if (cb->type != parent->type)
+ CN_CBOR_FAIL(CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING);
+ goto again;
+ }
+ if (parent->flags & CN_CBOR_FL_COUNT) {
+ if (--parent->v.count)
+ goto again;
+ }
+ /* so we are done filling parent. */
+complete: /* emulate return from call */
+ if (parent == top_parent) {
+ if (pos != ebuf) /* XXX do this outside */
+ CN_CBOR_FAIL(CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED);
+ pb->buf = pos;
+ return cb;
+ }
+ cb = parent;
+ parent = parent->parent;
+ goto fill;
+push: /* emulate recursive call */
+ parent = cb;
+ goto again;
fail:
- pb->buf = pos;
- return 0;
+ pb->buf = pos;
+ return 0;
}
MYLIB_EXPORT
-cn_cbor* cn_cbor_decode(const unsigned char* buf, size_t len CBOR_CONTEXT, cn_cbor_errback *errp) {
- cn_cbor catcher = {CN_CBOR_INVALID, 0, {0}, 0, NULL, NULL, NULL, NULL};
- struct parse_buf pb;
- cn_cbor* ret;
+cn_cbor *cn_cbor_decode(const unsigned char *buf, size_t len CBOR_CONTEXT, cn_cbor_errback *errp)
+{
+ cn_cbor catcher = {CN_CBOR_INVALID, 0, {0}, 0, NULL, NULL, NULL, NULL};
+ struct parse_buf pb;
+ cn_cbor *ret;
- pb.buf = (unsigned char *)buf;
- pb.ebuf = (unsigned char *)buf+len;
- pb.err = CN_CBOR_NO_ERROR;
- ret = decode_item(&pb CBOR_CONTEXT_PARAM, &catcher);
- if (ret != NULL) {
- /* mark as top node */
- ret->parent = NULL;
- } else {
- if (catcher.first_child) {
- catcher.first_child->parent = 0;
- cn_cbor_free(catcher.first_child CBOR_CONTEXT_PARAM);
- }
-//fail:
- if (errp) {
- errp->err = pb.err;
- errp->pos = pb.buf - (unsigned char *)buf;
- }
- return NULL;
- }
- return ret;
+ pb.buf = (unsigned char *)buf;
+ pb.ebuf = (unsigned char *)buf + len;
+ pb.err = CN_CBOR_NO_ERROR;
+ ret = decode_item(&pb CBOR_CONTEXT_PARAM, &catcher);
+ if (ret != NULL) {
+ /* mark as top node */
+ ret->parent = NULL;
+ }
+ else {
+ if (catcher.first_child) {
+ catcher.first_child->parent = 0;
+ cn_cbor_free(catcher.first_child CBOR_CONTEXT_PARAM);
+ }
+ // fail:
+ if (errp) {
+ errp->err = pb.err;
+ errp->pos = pb.buf - (unsigned char *)buf;
+ }
+ return NULL;
+ }
+ return ret;
}
-#ifdef __cplusplus
+#ifdef __cplusplus
}
#endif
-#endif /* CN_CBOR_C */
+#endif /* CN_CBOR_C */
diff --git a/src/cn-create.c b/src/cn-create.c
index c9ab816..d292ed3 100644
--- a/src/cn-create.c
+++ b/src/cn-create.c
@@ -1,7 +1,7 @@
#ifndef CN_CREATE_C
#define CN_CREATE_C
-#ifdef __cplusplus
+#ifdef __cplusplus
extern "C" {
#endif
@@ -12,211 +12,209 @@
#include "cn-cbor/cn-cbor.h"
#include "cbor.h"
-#define INIT_CB(v) \
- if (errp) {errp->err = CN_CBOR_NO_ERROR;} \
- (v) = CN_CALLOC_CONTEXT(); \
- if (!(v)) { if (errp) {errp->err = CN_CBOR_ERR_OUT_OF_MEMORY;} return NULL; }
+#define INIT_CB(v) \
+ if (errp) { \
+ errp->err = CN_CBOR_NO_ERROR; \
+ } \
+ (v) = CN_CALLOC_CONTEXT(); \
+ if (!(v)) { \
+ if (errp) { \
+ errp->err = CN_CBOR_ERR_OUT_OF_MEMORY; \
+ } \
+ return NULL; \
+ }
MYLIB_EXPORT
-cn_cbor* cn_cbor_map_create(CBOR_CONTEXT_COMMA cn_cbor_errback *errp)
+cn_cbor* cn_cbor_map_create(CBOR_CONTEXT_COMMA cn_cbor_errback* errp)
{
- cn_cbor* ret;
- INIT_CB(ret);
+ cn_cbor* ret;
+ INIT_CB(ret);
- ret->type = CN_CBOR_MAP;
- ret->flags |= CN_CBOR_FL_COUNT;
+ ret->type = CN_CBOR_MAP;
+ ret->flags |= CN_CBOR_FL_COUNT;
- return ret;
+ return ret;
}
MYLIB_EXPORT
-cn_cbor* cn_cbor_data_create(const uint8_t* data, int len
- CBOR_CONTEXT,
- cn_cbor_errback *errp)
+cn_cbor* cn_cbor_data_create(const uint8_t* data, int len CBOR_CONTEXT, cn_cbor_errback* errp)
{
- cn_cbor* ret;
- INIT_CB(ret);
+ cn_cbor* ret;
+ INIT_CB(ret);
- ret->type = CN_CBOR_BYTES;
- ret->length = len;
- ret->v.str = (const char*) data; // TODO: add v.ustr to the union?
+ ret->type = CN_CBOR_BYTES;
+ ret->length = len;
+ ret->v.str = (const char*)data; // TODO: add v.ustr to the union?
- return ret;
+ return ret;
}
MYLIB_EXPORT
-cn_cbor* cn_cbor_string_create(const char* data
- CBOR_CONTEXT,
- cn_cbor_errback *errp)
+cn_cbor* cn_cbor_string_create(const char* data CBOR_CONTEXT, cn_cbor_errback* errp)
{
- cn_cbor* ret;
- INIT_CB(ret);
+ cn_cbor* ret;
+ INIT_CB(ret);
- ret->type = CN_CBOR_TEXT;
- ret->length = strlen(data);
- ret->v.str = data;
+ ret->type = CN_CBOR_TEXT;
+ ret->length = strlen(data);
+ ret->v.str = data;
- return ret;
+ return ret;
}
MYLIB_EXPORT
-cn_cbor* cn_cbor_int_create(int64_t value
- CBOR_CONTEXT,
- cn_cbor_errback *errp)
+cn_cbor* cn_cbor_int_create(int64_t value CBOR_CONTEXT, cn_cbor_errback* errp)
{
- cn_cbor* ret;
- INIT_CB(ret);
+ cn_cbor* ret;
+ INIT_CB(ret);
- if (value<0) {
- ret->type = CN_CBOR_INT;
- ret->v.sint = value;
- } else {
- ret->type = CN_CBOR_UINT;
- ret->v.uint = value;
- }
+ if (value < 0) {
+ ret->type = CN_CBOR_INT;
+ ret->v.sint = value;
+ }
+ else {
+ ret->type = CN_CBOR_UINT;
+ ret->v.uint = value;
+ }
- return ret;
+ return ret;
}
#ifndef CBOR_NO_FLOAT
-cn_cbor* cn_cbor_float_create(float value
- CBOR_CONTEXT,
- cn_cbor_errback *errp)
+cn_cbor* cn_cbor_float_create(float value CBOR_CONTEXT, cn_cbor_errback* errp)
{
- cn_cbor* ret;
- INIT_CB(ret);
+ cn_cbor* ret;
+ INIT_CB(ret);
- ret->type = CN_CBOR_FLOAT;
- ret->v.f = value;
+ ret->type = CN_CBOR_FLOAT;
+ ret->v.f = value;
- return ret;
+ return ret;
}
-cn_cbor* cn_cbor_double_create(double value
- CBOR_CONTEXT,
- cn_cbor_errback *errp)
+cn_cbor* cn_cbor_double_create(double value CBOR_CONTEXT, cn_cbor_errback* errp)
{
- cn_cbor* ret;
- INIT_CB(ret);
+ cn_cbor* ret;
+ INIT_CB(ret);
- ret->type = CN_CBOR_DOUBLE;
- ret->v.dbl = value;
+ ret->type = CN_CBOR_DOUBLE;
+ ret->v.dbl = value;
- return ret;
+ return ret;
}
#endif /* CBOR_NO_FLOAT */
-static bool _append_kv(cn_cbor *cb_map, cn_cbor *key, cn_cbor *val)
+static bool _append_kv(cn_cbor* cb_map, cn_cbor* key, cn_cbor* val)
{
- //Connect key and value and insert them into the map.
- key->parent = cb_map;
- key->next = val;
- val->parent = cb_map;
- val->next = NULL;
+ // Connect key and value and insert them into the map.
+ key->parent = cb_map;
+ key->next = val;
+ val->parent = cb_map;
+ val->next = NULL;
- if(cb_map->last_child) {
- cb_map->last_child->next = key;
- } else {
- cb_map->first_child = key;
- }
- cb_map->last_child = val;
- cb_map->length += 2;
- return true;
+ if (cb_map->last_child) {
+ cb_map->last_child->next = key;
+ }
+ else {
+ cb_map->first_child = key;
+ }
+ cb_map->last_child = val;
+ cb_map->length += 2;
+ return true;
}
MYLIB_EXPORT
-bool cn_cbor_map_put(cn_cbor* cb_map,
- cn_cbor *cb_key, cn_cbor *cb_value,
- cn_cbor_errback *errp)
+bool cn_cbor_map_put(cn_cbor* cb_map, cn_cbor* cb_key, cn_cbor* cb_value, cn_cbor_errback* errp)
{
- //Make sure input is a map. Otherwise
- if(!cb_map || !cb_key || !cb_value || cb_map->type != CN_CBOR_MAP)
- {
- if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;}
- return false;
- }
+ // Make sure input is a map. Otherwise
+ if (!cb_map || !cb_key || !cb_value || cb_map->type != CN_CBOR_MAP) {
+ if (errp) {
+ errp->err = CN_CBOR_ERR_INVALID_PARAMETER;
+ }
+ return false;
+ }
- return _append_kv(cb_map, cb_key, cb_value);
+ return _append_kv(cb_map, cb_key, cb_value);
}
MYLIB_EXPORT
-bool cn_cbor_mapput_int(cn_cbor* cb_map,
- int64_t key, cn_cbor* cb_value
- CBOR_CONTEXT,
- cn_cbor_errback *errp)
+bool cn_cbor_mapput_int(cn_cbor* cb_map, int64_t key, cn_cbor* cb_value CBOR_CONTEXT, cn_cbor_errback* errp)
{
- cn_cbor* cb_key;
+ cn_cbor* cb_key;
- //Make sure input is a map. Otherwise
- if(!cb_map || !cb_value || cb_map->type != CN_CBOR_MAP)
- {
- if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;}
- return false;
- }
+ // Make sure input is a map. Otherwise
+ if (!cb_map || !cb_value || cb_map->type != CN_CBOR_MAP) {
+ if (errp) {
+ errp->err = CN_CBOR_ERR_INVALID_PARAMETER;
+ }
+ return false;
+ }
- cb_key = cn_cbor_int_create(key CBOR_CONTEXT_PARAM, errp);
- if (!cb_key) { return false; }
- return _append_kv(cb_map, cb_key, cb_value);
+ cb_key = cn_cbor_int_create(key CBOR_CONTEXT_PARAM, errp);
+ if (!cb_key) {
+ return false;
+ }
+ return _append_kv(cb_map, cb_key, cb_value);
}
MYLIB_EXPORT
-bool cn_cbor_mapput_string(cn_cbor* cb_map,
- const char* key, cn_cbor* cb_value
- CBOR_CONTEXT,
- cn_cbor_errback *errp)
+bool cn_cbor_mapput_string(cn_cbor* cb_map, const char* key, cn_cbor* cb_value CBOR_CONTEXT, cn_cbor_errback* errp)
{
- cn_cbor* cb_key;
+ cn_cbor* cb_key;
- //Make sure input is a map. Otherwise
- if(!cb_map || !cb_value || cb_map->type != CN_CBOR_MAP)
- {
- if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;}
- return false;
- }
+ // Make sure input is a map. Otherwise
+ if (!cb_map || !cb_value || cb_map->type != CN_CBOR_MAP) {
+ if (errp) {
+ errp->err = CN_CBOR_ERR_INVALID_PARAMETER;
+ }
+ return false;
+ }
- cb_key = cn_cbor_string_create(key CBOR_CONTEXT_PARAM, errp);
- if (!cb_key) { return false; }
- return _append_kv(cb_map, cb_key, cb_value);
+ cb_key = cn_cbor_string_create(key CBOR_CONTEXT_PARAM, errp);
+ if (!cb_key) {
+ return false;
+ }
+ return _append_kv(cb_map, cb_key, cb_value);
}
MYLIB_EXPORT
-cn_cbor* cn_cbor_array_create(CBOR_CONTEXT_COMMA cn_cbor_errback *errp)
+cn_cbor* cn_cbor_array_create(CBOR_CONTEXT_COMMA cn_cbor_errback* errp)
{
- cn_cbor* ret;
- INIT_CB(ret);
+ cn_cbor* ret;
+ INIT_CB(ret);
- ret->type = CN_CBOR_ARRAY;
- ret->flags |= CN_CBOR_FL_COUNT;
+ ret->type = CN_CBOR_ARRAY;
+ ret->flags |= CN_CBOR_FL_COUNT;
- return ret;
+ return ret;
}
MYLIB_EXPORT
-bool cn_cbor_array_append(cn_cbor* cb_array,
- cn_cbor* cb_value,
- cn_cbor_errback *errp)
+bool cn_cbor_array_append(cn_cbor* cb_array, cn_cbor* cb_value, cn_cbor_errback* errp)
{
- //Make sure input is an array.
- if(!cb_array || !cb_value || cb_array->type != CN_CBOR_ARRAY)
- {
- if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;}
- return false;
- }
+ // Make sure input is an array.
+ if (!cb_array || !cb_value || cb_array->type != CN_CBOR_ARRAY) {
+ if (errp) {
+ errp->err = CN_CBOR_ERR_INVALID_PARAMETER;
+ }
+ return false;
+ }
- cb_value->parent = cb_array;
- cb_value->next = NULL;
- if(cb_array->last_child) {
- cb_array->last_child->next = cb_value;
- } else {
- cb_array->first_child = cb_value;
- }
- cb_array->last_child = cb_value;
- cb_array->length++;
- return true;
+ cb_value->parent = cb_array;
+ cb_value->next = NULL;
+ if (cb_array->last_child) {
+ cb_array->last_child->next = cb_value;
+ }
+ else {
+ cb_array->first_child = cb_value;
+ }
+ cb_array->last_child = cb_value;
+ cb_array->length++;
+ return true;
}
-#ifdef __cplusplus
+#ifdef __cplusplus
}
#endif
-#endif /* CN_CBOR_C */
+#endif /* CN_CBOR_C */
diff --git a/src/cn-encoder.c b/src/cn-encoder.c
index d5b5bf0..51c82f0 100644
--- a/src/cn-encoder.c
+++ b/src/cn-encoder.c
@@ -1,7 +1,7 @@
#ifndef CN_ENCODER_C
#define CN_ENCODER_C
-#ifdef __cplusplus
+#ifdef __cplusplus
extern "C" {
#endif
#ifdef EMACS_INDENTATION_HELPER
@@ -29,323 +29,337 @@
#include "cn-cbor/cn-cbor.h"
#include "cbor.h"
-#define hton8p(p) (*(uint8_t*)(p))
-#define hton16p(p) (htons(*(uint16_t*)(p)))
-#define hton32p(p) (htonl(*(uint32_t*)(p)))
-static uint64_t hton64p(const uint8_t *p) {
- /* TODO: does this work on both BE and LE systems? */
- uint64_t ret = hton32p(p);
- ret <<= 32;
- ret |= hton32p(p+4);
- return ret;
+#define hton8p(p) (*(uint8_t *)(p))
+#define hton16p(p) (htons(*(uint16_t *)(p)))
+#define hton32p(p) (htonl(*(uint32_t *)(p)))
+static uint64_t hton64p(const uint8_t *p)
+{
+ /* TODO: does this work on both BE and LE systems? */
+ uint64_t ret = hton32p(p);
+ ret <<= 32;
+ ret |= hton32p(p + 4);
+ return ret;
}
-typedef struct _write_state
-{
- uint8_t *buf;
- ssize_t offset;
- ssize_t size;
+typedef struct _write_state {
+ uint8_t *buf;
+ ssize_t offset;
+ ssize_t size;
} cn_write_state;
-#define ensure_writable(sz) if ((ws->buf != NULL) && ((ws->offset<0) || (ws->offset + (sz) > ws->size))) { \
- ws->offset = -1; \
- return; \
-}
+#define ensure_writable(sz) \
+ if ((ws->buf != NULL) && ((ws->offset < 0) || (ws->offset + (sz) > ws->size))) { \
+ ws->offset = -1; \
+ return; \
+ }
-#define write_byte_and_data(b, data, sz) \
-if(ws->buf) { \
- ws->buf[ws->offset++] = (b); \
- memcpy(ws->buf+ws->offset, (data), (sz)); \
-} else { \
- ws->offset++; \
-} \
-ws->offset += sz;
+#define write_byte_and_data(b, data, sz) \
+ if (ws->buf) { \
+ ws->buf[ws->offset++] = (b); \
+ memcpy(ws->buf + ws->offset, (data), (sz)); \
+ } \
+ else { \
+ ws->offset++; \
+ } \
+ ws->offset += sz;
-#define write_byte(b) \
-if(ws->buf) { \
- ws->buf[ws->offset++] = (b); \
-} else { \
- ws->offset++; \
-}
+#define write_byte(b) \
+ if (ws->buf) { \
+ ws->buf[ws->offset++] = (b); \
+ } \
+ else { \
+ ws->offset++; \
+ }
#define write_byte_ensured(b) \
-ensure_writable(1); \
-write_byte(b);
+ ensure_writable(1); \
+ write_byte(b);
static uint8_t _xlate[] = {
- IB_FALSE, /* CN_CBOR_FALSE */
- IB_TRUE, /* CN_CBOR_TRUE */
- IB_NIL, /* CN_CBOR_NULL */
- IB_UNDEF, /* CN_CBOR_UNDEF */
- IB_UNSIGNED, /* CN_CBOR_UINT */
- IB_NEGATIVE, /* CN_CBOR_INT */
- IB_BYTES, /* CN_CBOR_BYTES */
- IB_TEXT, /* CN_CBOR_TEXT */
- IB_BYTES, /* CN_CBOR_BYTES_CHUNKED */
- IB_TEXT, /* CN_CBOR_TEXT_CHUNKED */
- IB_ARRAY, /* CN_CBOR_ARRAY */
- IB_MAP, /* CN_CBOR_MAP */
- IB_TAG, /* CN_CBOR_TAG */
- IB_PRIM, /* CN_CBOR_SIMPLE */
- 0xFF, /* CN_CBOR_DOUBLE */
- 0xFF /* CN_CBOR_INVALID */
+ IB_FALSE, /* CN_CBOR_FALSE */
+ IB_TRUE, /* CN_CBOR_TRUE */
+ IB_NIL, /* CN_CBOR_NULL */
+ IB_UNDEF, /* CN_CBOR_UNDEF */
+ IB_UNSIGNED, /* CN_CBOR_UINT */
+ IB_NEGATIVE, /* CN_CBOR_INT */
+ IB_BYTES, /* CN_CBOR_BYTES */
+ IB_TEXT, /* CN_CBOR_TEXT */
+ IB_BYTES, /* CN_CBOR_BYTES_CHUNKED */
+ IB_TEXT, /* CN_CBOR_TEXT_CHUNKED */
+ IB_ARRAY, /* CN_CBOR_ARRAY */
+ IB_MAP, /* CN_CBOR_MAP */
+ IB_TAG, /* CN_CBOR_TAG */
+ IB_PRIM, /* CN_CBOR_SIMPLE */
+ 0xFF, /* CN_CBOR_DOUBLE */
+ 0xFF /* CN_CBOR_INVALID */
};
static inline bool is_indefinite(const cn_cbor *cb)
{
- return (cb->flags & CN_CBOR_FL_INDEF) != 0;
+ return (cb->flags & CN_CBOR_FL_INDEF) != 0;
}
-static void _write_positive(cn_write_state *ws, cn_cbor_type typ, uint64_t val) {
- uint8_t ib;
+static void _write_positive(cn_write_state *ws, cn_cbor_type typ, uint64_t val)
+{
+ uint8_t ib;
- assert((size_t)typ < sizeof(_xlate));
+ assert((size_t)typ < sizeof(_xlate));
- ib = _xlate[typ];
- if (ib == 0xFF) {
- ws->offset = -1;
- return;
- }
+ ib = _xlate[typ];
+ if (ib == 0xFF) {
+ ws->offset = -1;
+ return;
+ }
- if (val < 24) {
- ensure_writable(1);
- write_byte(ib | (uint8_t) val);
- } else if (val < 256) {
- ensure_writable(2);
- write_byte(ib | 24);
- write_byte((uint8_t)val);
- } else if (val < 65536) {
- uint16_t be16 = (uint16_t)val;
- ensure_writable(3);
- be16 = hton16p(&be16);
- write_byte_and_data(ib | 25, (const void*)&be16, 2);
- } else if (val < 0x100000000L) {
- uint32_t be32 = (uint32_t)val;
- ensure_writable(5);
- // be32 = hton32p(&be32);
- be32 = htonl(be32);
- write_byte_and_data(ib | 26, (const void*)&be32, 4);
- } else {
- uint64_t be64;
- ensure_writable(9);
- be64 = hton64p((const uint8_t*)&val);
- write_byte_and_data(ib | 27, (const void*)&be64, 8);
- }
+ if (val < 24) {
+ ensure_writable(1);
+ write_byte(ib | (uint8_t)val);
+ }
+ else if (val < 256) {
+ ensure_writable(2);
+ write_byte(ib | 24);
+ write_byte((uint8_t)val);
+ }
+ else if (val < 65536) {
+ uint16_t be16 = (uint16_t)val;
+ ensure_writable(3);
+ be16 = hton16p(&be16);
+ write_byte_and_data(ib | 25, (const void *)&be16, 2);
+ }
+ else if (val < 0x100000000L) {
+ uint32_t be32 = (uint32_t)val;
+ ensure_writable(5);
+ // be32 = hton32p(&be32);
+ be32 = htonl(be32);
+ write_byte_and_data(ib | 26, (const void *)&be32, 4);
+ }
+ else {
+ uint64_t be64;
+ ensure_writable(9);
+ be64 = hton64p((const uint8_t *)&val);
+ write_byte_and_data(ib | 27, (const void *)&be64, 8);
+ }
}
#ifndef CBOR_NO_FLOAT
static void _write_double(cn_write_state *ws, double val)
{
- float float_val = val;
- if (float_val == val) { /* 32 bits is enough and we aren't NaN */
- uint32_t be32;
- uint16_t be16, u16;
- union {
- float f;
- uint32_t u;
- } u32;
- u32.f = float_val;
- if ((u32.u & 0x1FFF) == 0) { /* worth trying half */
- int s16 = (u32.u >> 16) & 0x8000;
- int exp = (u32.u >> 23) & 0xff;
- int mant = u32.u & 0x7fffff;
- if (exp == 0 && mant == 0)
- ; /* 0.0, -0.0 */
- else if (exp >= 113 && exp <= 142) /* normalized */
- s16 += ((exp - 112) << 10) + (mant >> 13);
- else if (exp >= 103 && exp < 113) { /* denorm, exp16 = 0 */
- if (mant & ((1 << (126 - exp)) - 1))
- goto float32; /* loss of precision */
- s16 += ((mant + 0x800000) >> (126 - exp));
- } else if (exp == 255 && mant == 0) { /* Inf */
- s16 += 0x7c00;
- } else
- goto float32; /* loss of range */
+ float float_val = val;
+ if (float_val == val) { /* 32 bits is enough and we aren't NaN */
+ uint32_t be32;
+ uint16_t be16, u16;
+ union {
+ float f;
+ uint32_t u;
+ } u32;
+ u32.f = float_val;
+ if ((u32.u & 0x1FFF) == 0) { /* worth trying half */
+ int s16 = (u32.u >> 16) & 0x8000;
+ int exp = (u32.u >> 23) & 0xff;
+ int mant = u32.u & 0x7fffff;
+ if (exp == 0 && mant == 0)
+ ; /* 0.0, -0.0 */
+ else if (exp >= 113 && exp <= 142) /* normalized */
+ s16 += ((exp - 112) << 10) + (mant >> 13);
+ else if (exp >= 103 && exp < 113) { /* denorm, exp16 = 0 */
+ if (mant & ((1 << (126 - exp)) - 1))
+ goto float32; /* loss of precision */
+ s16 += ((mant + 0x800000) >> (126 - exp));
+ }
+ else if (exp == 255 && mant == 0) { /* Inf */
+ s16 += 0x7c00;
+ }
+ else
+ goto float32; /* loss of range */
- ensure_writable(3);
- u16 = s16;
- be16 = hton16p((const uint8_t*)&u16);
+ ensure_writable(3);
+ u16 = s16;
+ be16 = hton16p((const uint8_t *)&u16);
- write_byte_and_data(IB_PRIM | 25, (const void*)&be16, 2);
- return;
- }
- float32:
- ensure_writable(5);
- be32 = hton32p((const uint8_t*)&u32.u);
+ write_byte_and_data(IB_PRIM | 25, (const void *)&be16, 2);
+ return;
+ }
+ float32:
+ ensure_writable(5);
+ be32 = hton32p((const uint8_t *)&u32.u);
- write_byte_and_data(IB_PRIM | 26, (const void*)&be32, 4);
+ write_byte_and_data(IB_PRIM | 26, (const void *)&be32, 4);
+ }
+ else if (val != val) { /* NaN -- we always write a half NaN*/
+ ensure_writable(3);
+ write_byte_and_data(IB_PRIM | 25, (const void *)"\x7e\x00", 2);
+ }
+ else {
+ uint64_t be64;
+ /* Copy the same problematic implementation from the decoder. */
+ union {
+ double d;
+ uint64_t u;
+ } u64;
- } else if (val != val) { /* NaN -- we always write a half NaN*/
- ensure_writable(3);
- write_byte_and_data(IB_PRIM | 25, (const void*)"\x7e\x00", 2);
- } else {
- uint64_t be64;
- /* Copy the same problematic implementation from the decoder. */
- union {
- double d;
- uint64_t u;
- } u64;
+ u64.d = val;
- u64.d = val;
+ ensure_writable(9);
+ be64 = hton64p((const uint8_t *)&u64.u);
- ensure_writable(9);
- be64 = hton64p((const uint8_t*)&u64.u);
-
- write_byte_and_data(IB_PRIM | 27, (const void*)&be64, 8);
-
- }
+ write_byte_and_data(IB_PRIM | 27, (const void *)&be64, 8);
+ }
}
#endif /* CBOR_NO_FLOAT */
// TODO: make public?
typedef void (*cn_visit_func)(const cn_cbor *cb, int depth, void *context);
-void _visit(const cn_cbor *cb,
- cn_visit_func visitor,
- cn_visit_func breaker,
- void *context)
+void _visit(const cn_cbor *cb, cn_visit_func visitor, cn_visit_func breaker, void *context)
{
- const cn_cbor *p = cb;
- int depth = 0;
- while (p)
- {
-visit:
- visitor(p, depth, context);
- if (p->first_child) {
- p = p->first_child;
- depth++;
- } else{
- // Empty indefinite
+ const cn_cbor *p = cb;
+ int depth = 0;
+ while (p) {
+ visit:
+ visitor(p, depth, context);
+ if (p->first_child) {
+ p = p->first_child;
+ depth++;
+ }
+ else {
+ // Empty indefinite
#ifdef CN_INCLUDE_DUMPER
- breaker(p, depth, context);
+ breaker(p, depth, context);
#else
- if (is_indefinite(p)) {
- breaker(p, depth, context);
- }
+ if (is_indefinite(p)) {
+ breaker(p, depth, context);
+ }
#endif
- if (p->next) {
- p = p->next;
- } else {
- while (p->parent) {
- depth--;
+ if (p->next) {
+ p = p->next;
+ }
+ else {
+ while (p->parent) {
+ depth--;
#ifdef CN_INCLUDE_DUMPER
- breaker(p->parent, depth, context);
+ breaker(p->parent, depth, context);
#else
- if (is_indefinite(p->parent)) {
- breaker(p->parent, depth, context);
- }
+ if (is_indefinite(p->parent)) {
+ breaker(p->parent, depth, context);
+ }
#endif
- if (p->parent->next) {
- p = p->parent->next;
- goto visit;
- }
- p = p->parent;
- }
- return;
- }
- }
- }
+ if (p->parent->next) {
+ p = p->parent->next;
+ goto visit;
+ }
+ p = p->parent;
+ }
+ return;
+ }
+ }
+ }
}
-#define CHECK(st) (st); \
-if (ws->offset < 0) { return; }
+#define CHECK(st) \
+ (st); \
+ if (ws->offset < 0) { \
+ return; \
+ }
void _encoder_visitor(const cn_cbor *cb, int depth, void *context)
{
- cn_write_state *ws = context;
- UNUSED_PARAM(depth);
+ cn_write_state *ws = context;
+ UNUSED_PARAM(depth);
- switch (cb->type) {
- case CN_CBOR_ARRAY:
- if (is_indefinite(cb)) {
- write_byte_ensured(IB_ARRAY | AI_INDEF);
- } else {
- CHECK(_write_positive(ws, CN_CBOR_ARRAY, cb->length));
- }
- break;
- case CN_CBOR_MAP:
- if (is_indefinite(cb)) {
- write_byte_ensured(IB_MAP | AI_INDEF);
- } else {
- CHECK(_write_positive(ws, CN_CBOR_MAP, cb->length/2));
- }
- break;
- case CN_CBOR_BYTES_CHUNKED:
- case CN_CBOR_TEXT_CHUNKED:
- write_byte_ensured(_xlate[cb->type] | AI_INDEF);
- break;
+ switch (cb->type) {
+ case CN_CBOR_ARRAY:
+ if (is_indefinite(cb)) {
+ write_byte_ensured(IB_ARRAY | AI_INDEF);
+ }
+ else {
+ CHECK(_write_positive(ws, CN_CBOR_ARRAY, cb->length));
+ }
+ break;
+ case CN_CBOR_MAP:
+ if (is_indefinite(cb)) {
+ write_byte_ensured(IB_MAP | AI_INDEF);
+ }
+ else {
+ CHECK(_write_positive(ws, CN_CBOR_MAP, cb->length / 2));
+ }
+ break;
+ case CN_CBOR_BYTES_CHUNKED:
+ case CN_CBOR_TEXT_CHUNKED:
+ write_byte_ensured(_xlate[cb->type] | AI_INDEF);
+ break;
- case CN_CBOR_TEXT:
- case CN_CBOR_BYTES:
- CHECK(_write_positive(ws, cb->type, cb->length));
- ensure_writable(cb->length);
- if (ws->buf) {
- memcpy(ws->buf+ws->offset, cb->v.str, cb->length);
- }
- ws->offset += cb->length;
- break;
+ case CN_CBOR_TEXT:
+ case CN_CBOR_BYTES:
+ CHECK(_write_positive(ws, cb->type, cb->length));
+ ensure_writable(cb->length);
+ if (ws->buf) {
+ memcpy(ws->buf + ws->offset, cb->v.str, cb->length);
+ }
+ ws->offset += cb->length;
+ break;
- case CN_CBOR_FALSE:
- case CN_CBOR_TRUE:
- case CN_CBOR_NULL:
- case CN_CBOR_UNDEF:
- write_byte_ensured(_xlate[cb->type]);
- break;
+ case CN_CBOR_FALSE:
+ case CN_CBOR_TRUE:
+ case CN_CBOR_NULL:
+ case CN_CBOR_UNDEF:
+ write_byte_ensured(_xlate[cb->type]);
+ break;
- case CN_CBOR_TAG:
- case CN_CBOR_UINT:
- case CN_CBOR_SIMPLE:
- CHECK(_write_positive(ws, cb->type, cb->v.uint));
- break;
+ case CN_CBOR_TAG:
+ case CN_CBOR_UINT:
+ case CN_CBOR_SIMPLE:
+ CHECK(_write_positive(ws, cb->type, cb->v.uint));
+ break;
- case CN_CBOR_INT:
- assert(cb->v.sint < 0);
- CHECK(_write_positive(ws, CN_CBOR_INT, ~(cb->v.sint)));
- break;
+ case CN_CBOR_INT:
+ assert(cb->v.sint < 0);
+ CHECK(_write_positive(ws, CN_CBOR_INT, ~(cb->v.sint)));
+ break;
#ifndef CBOR_NO_FLOAT
- case CN_CBOR_DOUBLE:
- CHECK(_write_double(ws, cb->v.dbl));
- break;
+ case CN_CBOR_DOUBLE:
+ CHECK(_write_double(ws, cb->v.dbl));
+ break;
- case CN_CBOR_FLOAT:
- CHECK(_write_double(ws, cb->v.f));
+ case CN_CBOR_FLOAT:
+ CHECK(_write_double(ws, cb->v.f));
#endif /* CBOR_NO_FLOAT */
- break;
+ break;
- case CN_CBOR_INVALID:
- ws->offset = -1;
- break;
- }
+ case CN_CBOR_INVALID:
+ ws->offset = -1;
+ break;
+ }
}
void _encoder_breaker(const cn_cbor *cb, int depth, void *context)
{
- cn_write_state *ws = context;
- UNUSED_PARAM(cb);
- UNUSED_PARAM(depth);
+ cn_write_state *ws = context;
+ UNUSED_PARAM(cb);
+ UNUSED_PARAM(depth);
#ifdef CN_INCLUDE_DUMPER
- if (is_indefinite(cb)) {
+ if (is_indefinite(cb)) {
#endif
- write_byte_ensured(IB_BREAK);
+ write_byte_ensured(IB_BREAK);
#ifdef CN_INCLUDE_DUMPER
- }
+ }
#endif
}
-ssize_t cn_cbor_encoder_write(uint8_t *buf,
- size_t buf_offset,
- size_t buf_size,
- const cn_cbor *cb)
+ssize_t cn_cbor_encoder_write(uint8_t *buf, size_t buf_offset, size_t buf_size, const cn_cbor *cb)
{
- cn_write_state ws = { buf, buf_offset, buf_size };
- if (!ws.buf && ws.size <= 0) { ws.size = (ssize_t)(((size_t)-1) / 2); }
- _visit(cb, _encoder_visitor, _encoder_breaker, &ws);
- if (ws.offset < 0) { return -1; }
- return ws.offset - buf_offset;
+ cn_write_state ws = {buf, buf_offset, buf_size};
+ if (!ws.buf && ws.size <= 0) {
+ ws.size = (ssize_t)(((size_t)-1) / 2);
+ }
+ _visit(cb, _encoder_visitor, _encoder_breaker, &ws);
+ if (ws.offset < 0) {
+ return -1;
+ }
+ return ws.offset - buf_offset;
}
-#ifdef __cplusplus
+#ifdef __cplusplus
}
#endif
-#endif /* CN_CBOR_C */
+#endif /* CN_CBOR_C */
diff --git a/src/cn-error.c b/src/cn-error.c
index ad48746..6aae83e 100644
--- a/src/cn-error.c
+++ b/src/cn-error.c
@@ -1,16 +1,7 @@
#include "dll-export.h"
MYLIB_EXPORT
-const char *cn_cbor_error_str[] = {
- "CN_CBOR_NO_ERROR",
- "CN_CBOR_ERR_OUT_OF_DATA",
- "CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED",
- "CN_CBOR_ERR_ODD_SIZE_INDEF_MAP",
- "CN_CBOR_ERR_BREAK_OUTSIDE_INDEF",
- "CN_CBOR_ERR_MT_UNDEF_FOR_INDEF",
- "CN_CBOR_ERR_RESERVED_AI",
- "CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING",
- "CN_CBOR_ERR_INVALID_PARAMETER",
- "CN_CBOR_ERR_OUT_OF_MEMORY",
- "CN_CBOR_ERR_FLOAT_NOT_SUPPORTED"
-};
+const char *cn_cbor_error_str[] = {"CN_CBOR_NO_ERROR", "CN_CBOR_ERR_OUT_OF_DATA", "CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED",
+ "CN_CBOR_ERR_ODD_SIZE_INDEF_MAP", "CN_CBOR_ERR_BREAK_OUTSIDE_INDEF", "CN_CBOR_ERR_MT_UNDEF_FOR_INDEF",
+ "CN_CBOR_ERR_RESERVED_AI", "CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING", "CN_CBOR_ERR_INVALID_PARAMETER",
+ "CN_CBOR_ERR_OUT_OF_MEMORY", "CN_CBOR_ERR_FLOAT_NOT_SUPPORTED"};
diff --git a/src/cn-get.c b/src/cn-get.c
index 4b2ee13..fa7163f 100644
--- a/src/cn-get.c
+++ b/src/cn-get.c
@@ -7,62 +7,63 @@
#include "cn-cbor/cn-cbor.h"
MYLIB_EXPORT
-cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key) {
- cn_cbor* cp;
- assert(cb);
- for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
- switch(cp->type) {
- case CN_CBOR_UINT:
- if (cp->v.uint == (unsigned long)key) {
- return cp->next;
- }
- break;
- case CN_CBOR_INT:
- if (cp->v.sint == (long)key) {
- return cp->next;
- }
- break;
- default:
- ; // skip non-integer keys
- }
- }
- return NULL;
+cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key)
+{
+ cn_cbor* cp;
+ assert(cb);
+ for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
+ switch (cp->type) {
+ case CN_CBOR_UINT:
+ if (cp->v.uint == (unsigned long)key) {
+ return cp->next;
+ }
+ break;
+ case CN_CBOR_INT:
+ if (cp->v.sint == (long)key) {
+ return cp->next;
+ }
+ break;
+ default:; // skip non-integer keys
+ }
+ }
+ return NULL;
}
MYLIB_EXPORT
-cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key) {
- cn_cbor *cp;
- int keylen;
- assert(cb);
- assert(key);
- keylen = strlen(key);
- for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
- switch(cp->type) {
- case CN_CBOR_TEXT: // fall-through
- case CN_CBOR_BYTES:
- if (keylen != cp->length) {
- continue;
- }
- if (memcmp(key, cp->v.str, keylen) == 0) {
- return cp->next;
- }
- default:
- ; // skip non-string keys
- }
- }
- return NULL;
+cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key)
+{
+ cn_cbor* cp;
+ int keylen;
+ assert(cb);
+ assert(key);
+ keylen = strlen(key);
+ for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
+ switch (cp->type) {
+ case CN_CBOR_TEXT: // fall-through
+ case CN_CBOR_BYTES:
+ if (keylen != cp->length) {
+ continue;
+ }
+ if (memcmp(key, cp->v.str, keylen) == 0) {
+ return cp->next;
+ }
+ default:; // skip non-string keys
+ }
+ }
+ return NULL;
}
MYLIB_EXPORT
-cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx) {
- cn_cbor *cp;
- unsigned int i = 0;
- assert(cb);
- for (cp = cb->first_child; cp; cp = cp->next) {
- if (i == idx) {
- return cp;
- }
- i++;
- }
- return NULL;
+cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx)
+{
+ cn_cbor* cp;
+ unsigned int i = 0;
+ assert(cb);
+ for (cp = cb->first_child; cp; cp = cp->next) {
+ if (i == idx) {
+ return cp;
+ }
+ i++;
+ }
+ return NULL;
}
diff --git a/src/cn-print.c b/src/cn-print.c
index 077b0a4..621b334 100644
--- a/src/cn-print.c
+++ b/src/cn-print.c
@@ -7,7 +7,7 @@
#include <stdio.h>
#include <stdio.h>
-#ifdef __cplusplus
+#ifdef __cplusplus
extern "C" {
#endif
#ifdef EMACS_INDENTATION_HELPER
@@ -27,28 +27,24 @@
#include "cn-cbor/cn-cbor.h"
#include "cbor.h"
-typedef struct _write_state
-{
- char * rgbOutput;
+typedef struct _write_state {
+ char *rgbOutput;
ssize_t ib;
size_t cbLeft;
- uint8_t * rgFlags;
- const char * szIndentWith;
- const char * szEndOfLine;
+ uint8_t *rgFlags;
+ const char *szIndentWith;
+ const char *szEndOfLine;
} cn_write_state;
-typedef void(*cn_visit_func)(const cn_cbor *cb, int depth, void *context);
-extern void _visit(const cn_cbor *cb,
- cn_visit_func visitor,
- cn_visit_func breaker,
- void *context);
+typedef void (*cn_visit_func)(const cn_cbor *cb, int depth, void *context);
+extern void _visit(const cn_cbor *cb, cn_visit_func visitor, cn_visit_func breaker, void *context);
-const char RgchHex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
-'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+const char RgchHex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
-bool _isWritable(cn_write_state * ws, size_t cb)
+bool _isWritable(cn_write_state *ws, size_t cb)
{
- if (ws->rgbOutput == NULL) return true;
+ if (ws->rgbOutput == NULL)
+ return true;
if ((ws->ib < 0) || (ws->ib + cb > ws->cbLeft)) {
ws->ib = -1;
return false;
@@ -56,22 +52,22 @@
return true;
}
-void write_data(cn_write_state * ws, const char * sz, size_t cb)
+void write_data(cn_write_state *ws, const char *sz, size_t cb)
{
if (_isWritable(ws, cb)) {
- if (ws->rgbOutput != NULL) memcpy(ws->rgbOutput + ws->ib, sz, cb);
+ if (ws->rgbOutput != NULL)
+ memcpy(ws->rgbOutput + ws->ib, sz, cb);
ws->ib += cb;
}
}
-void _doIndent(cn_write_state * ws, int depth)
+void _doIndent(cn_write_state *ws, int depth)
{
int i;
- char * sz = ws->rgbOutput + ws->ib;
+ char *sz = ws->rgbOutput + ws->ib;
size_t cbIndentWith = strlen(ws->szIndentWith);
int cbIndent = depth * cbIndentWith;
-
if (ws->rgbOutput == NULL) {
ws->ib += cbIndent;
return;
@@ -89,12 +85,12 @@
return;
}
-void _print_encoder(const cn_cbor * cb, int depth, void * context)
+void _print_encoder(const cn_cbor *cb, int depth, void *context)
{
int i;
char rgchT[256];
int cch;
- cn_write_state * ws = (cn_write_state *)context;
+ cn_write_state *ws = (cn_write_state *)context;
uint8_t flags = ws->rgFlags[depth];
if (flags & 1) {
@@ -113,136 +109,142 @@
}
switch (cb->type) {
- case CN_CBOR_BYTES_CHUNKED:
- case CN_CBOR_TEXT_CHUNKED:
- break;
+ case CN_CBOR_BYTES_CHUNKED:
+ case CN_CBOR_TEXT_CHUNKED:
+ break;
- case CN_CBOR_ARRAY:
- write_data(ws, "[", 1);
- ws->rgFlags[depth] |= 4;
+ case CN_CBOR_ARRAY:
+ write_data(ws, "[", 1);
+ ws->rgFlags[depth] |= 4;
- if (ws->szIndentWith) {
- write_data(ws, ws->szEndOfLine, strlen(ws->szEndOfLine));
- _doIndent(ws, depth + 1);
- }
- break;
+ if (ws->szIndentWith) {
+ write_data(ws, ws->szEndOfLine, strlen(ws->szEndOfLine));
+ _doIndent(ws, depth + 1);
+ }
+ break;
- case CN_CBOR_MAP:
- write_data(ws, "{", 1);
- ws->rgFlags[depth] |= 8;
+ case CN_CBOR_MAP:
+ write_data(ws, "{", 1);
+ ws->rgFlags[depth] |= 8;
- if (ws->szIndentWith) {
- write_data(ws, ws->szEndOfLine, strlen(ws->szEndOfLine));
- _doIndent(ws, depth + 1);
- }
- break;
+ if (ws->szIndentWith) {
+ write_data(ws, ws->szEndOfLine, strlen(ws->szEndOfLine));
+ _doIndent(ws, depth + 1);
+ }
+ break;
- case CN_CBOR_TAG:
- case CN_CBOR_UINT:
- case CN_CBOR_SIMPLE:
- cch = _snprintf(rgchT, sizeof(rgchT), "%u", (unsigned int) cb->v.uint);
- write_data(ws, rgchT, cch);
- break;
+ case CN_CBOR_TAG:
+ case CN_CBOR_UINT:
+ case CN_CBOR_SIMPLE:
+ cch = _snprintf(rgchT, sizeof(rgchT), "%u", (unsigned int)cb->v.uint);
+ write_data(ws, rgchT, cch);
+ break;
- case CN_CBOR_FALSE:
- write_data(ws, "false", 5);
- break;
+ case CN_CBOR_FALSE:
+ write_data(ws, "false", 5);
+ break;
- case CN_CBOR_TRUE:
- write_data(ws, "true", 4);
- break;
+ case CN_CBOR_TRUE:
+ write_data(ws, "true", 4);
+ break;
- case CN_CBOR_NULL:
- write_data(ws, "null", 4);
- break;
+ case CN_CBOR_NULL:
+ write_data(ws, "null", 4);
+ break;
- case CN_CBOR_UNDEF:
- write_data(ws, "undef", 5);
- break;
+ case CN_CBOR_UNDEF:
+ write_data(ws, "undef", 5);
+ break;
- case CN_CBOR_INT:
- cch = _snprintf(rgchT, sizeof(rgchT), "%d", (unsigned int) cb->v.sint);
- write_data(ws, rgchT, cch);
- break;
+ case CN_CBOR_INT:
+ cch = _snprintf(rgchT, sizeof(rgchT), "%d", (unsigned int)cb->v.sint);
+ write_data(ws, rgchT, cch);
+ break;
#ifndef CBOR_NO_FLOAT
- case CN_CBOR_FLOAT:
- cch = _snprintf(rgchT, sizeof(rgchT), "%f", cb->v.f);
- write_data(ws, rgchT, cch);
- break;
+ case CN_CBOR_FLOAT:
+ cch = _snprintf(rgchT, sizeof(rgchT), "%f", cb->v.f);
+ write_data(ws, rgchT, cch);
+ break;
- case CN_CBOR_DOUBLE:
- cch = _snprintf(rgchT, sizeof(rgchT), "%f", cb->v.dbl);
- write_data(ws, rgchT, cch);
- break;
+ case CN_CBOR_DOUBLE:
+ cch = _snprintf(rgchT, sizeof(rgchT), "%f", cb->v.dbl);
+ write_data(ws, rgchT, cch);
+ break;
#endif
- case CN_CBOR_INVALID:
- write_data(ws, "invalid", 7);
- break;
+ case CN_CBOR_INVALID:
+ write_data(ws, "invalid", 7);
+ break;
- case CN_CBOR_TEXT:
- write_data(ws, "\"", 1);
- write_data(ws, cb->v.str, cb->length);
- write_data(ws, "\"", 1);
- break;
+ case CN_CBOR_TEXT:
+ write_data(ws, "\"", 1);
+ write_data(ws, cb->v.str, cb->length);
+ write_data(ws, "\"", 1);
+ break;
- case CN_CBOR_BYTES:
- write_data(ws, "h'", 2);
- for (i = 0; i < cb->length; i++) {
- write_data(ws, &RgchHex[(cb->v.str[i] / 16) & 0xf], 1);
- write_data(ws, &RgchHex[cb->v.str[i] & 0xf], 1);
- }
- write_data(ws, "\'", 1);
- break;
-
+ case CN_CBOR_BYTES:
+ write_data(ws, "h'", 2);
+ for (i = 0; i < cb->length; i++) {
+ write_data(ws, &RgchHex[(cb->v.str[i] / 16) & 0xf], 1);
+ write_data(ws, &RgchHex[cb->v.str[i] & 0xf], 1);
+ }
+ write_data(ws, "\'", 1);
+ break;
}
if (depth > 0) {
- if (ws->rgFlags[depth - 1] & 4) ws->rgFlags[depth] |= 1;
+ if (ws->rgFlags[depth - 1] & 4)
+ ws->rgFlags[depth] |= 1;
else if (ws->rgFlags[depth - 1] & 8) {
- if (flags & 2) ws->rgFlags[depth] |= 1;
- else ws->rgFlags[depth] |= 2;
+ if (flags & 2)
+ ws->rgFlags[depth] |= 1;
+ else
+ ws->rgFlags[depth] |= 2;
}
}
}
-void _print_breaker(const cn_cbor * cb, int depth, void * context)
+void _print_breaker(const cn_cbor *cb, int depth, void *context)
{
- cn_write_state * ws = (cn_write_state *)context;
+ cn_write_state *ws = (cn_write_state *)context;
switch (cb->type) {
- case CN_CBOR_ARRAY:
- if (ws->szIndentWith) {
- write_data(ws, ws->szEndOfLine, strlen(ws->szEndOfLine));
- _doIndent(ws, depth);
- }
+ case CN_CBOR_ARRAY:
+ if (ws->szIndentWith) {
+ write_data(ws, ws->szEndOfLine, strlen(ws->szEndOfLine));
+ _doIndent(ws, depth);
+ }
- write_data(ws, "]", 1);
- ws->rgFlags[depth + 1] = 0;
- break;
+ write_data(ws, "]", 1);
+ ws->rgFlags[depth + 1] = 0;
+ break;
- case CN_CBOR_MAP:
- if (ws->szIndentWith) {
- write_data(ws, ws->szEndOfLine, strlen(ws->szEndOfLine));
- _doIndent(ws, depth);
- }
+ case CN_CBOR_MAP:
+ if (ws->szIndentWith) {
+ write_data(ws, ws->szEndOfLine, strlen(ws->szEndOfLine));
+ _doIndent(ws, depth);
+ }
- write_data(ws, "}", 1);
- ws->rgFlags[depth + 1] = 0;
- break;
+ write_data(ws, "}", 1);
+ ws->rgFlags[depth + 1] = 0;
+ break;
- default:
- break;
+ default:
+ break;
}
}
-ssize_t cn_cbor_printer_write(char * rgbBuffer, size_t cbBuffer, const cn_cbor * cb, const char * szIndentWith, const char * szEndOfLine)
+ssize_t cn_cbor_printer_write(char *rgbBuffer,
+ size_t cbBuffer,
+ const cn_cbor *cb,
+ const char *szIndentWith,
+ const char *szEndOfLine)
{
- uint8_t flags[128] = { 0 };
- char rgchZero[1] = { 0 };
+ uint8_t flags[128] = {0};
+ char rgchZero[1] = {0};
- cn_write_state ws = { rgbBuffer, 0, cbBuffer, flags, szIndentWith, szEndOfLine };
+ cn_write_state ws = {rgbBuffer, 0, cbBuffer, flags, szIndentWith, szEndOfLine};
_visit(cb, _print_encoder, _print_breaker, &ws);
write_data(&ws, rgchZero, 1);
@@ -253,9 +255,8 @@
{ /* Duh. */
#endif
#ifdef _cplusplus
-} /* extern "C" */
+} /* extern "C" */
#endif
-#endif // CN_INCLUDE_DUMPER
-#endif // CN_PRINT_C
-
+#endif // CN_INCLUDE_DUMPER
+#endif // CN_PRINT_C
diff --git a/src/dll-export.h b/src/dll-export.h
index 3d0c928..0457951 100644
--- a/src/dll-export.h
+++ b/src/dll-export.h
@@ -10,14 +10,14 @@
*/
// We are using the Visual Studio Compiler and building Shared libraries
-#if defined (_WIN32)
+#if defined(_WIN32)
#if defined(CN_CBOR_IS_DLL)
- #define MYLIB_EXPORT __declspec(dllexport)
+#define MYLIB_EXPORT __declspec(dllexport)
#else
- #define MYLIB_EXPORT
+#define MYLIB_EXPORT
#endif /* cn_cbor_EXPORTS */
-#else /* defined (_WIN32) */
- #define MYLIB_EXPORT
+#else /* defined (_WIN32) */
+#define MYLIB_EXPORT
#endif
#endif /* _cn_cbor_DLLDEFINES_H_ */
diff --git a/test/cbor_test.c b/test/cbor_test.c
index 3888723..3678e61 100644
--- a/test/cbor_test.c
+++ b/test/cbor_test.c
@@ -25,7 +25,7 @@
int main(int argc, const char *argv[])
{
- return ctest_main(argc, argv);
+ return ctest_main(argc, argv);
}
#ifdef USE_CBOR_CONTEXT
@@ -37,444 +37,451 @@
#endif
typedef struct _buffer {
- size_t sz;
- unsigned char *ptr;
+ size_t sz;
+ unsigned char *ptr;
} buffer;
static bool parse_hex(char *inp, buffer *b)
{
- int len = strlen(inp);
- size_t i;
- if (len%2 != 0) {
- b->sz = -1;
- b->ptr = NULL;
- return false;
- }
- b->sz = len / 2;
- b->ptr = malloc(b->sz);
- for (i=0; i<b->sz; i++) {
+ int len = strlen(inp);
+ size_t i;
+ if (len % 2 != 0) {
+ b->sz = -1;
+ b->ptr = NULL;
+ return false;
+ }
+ b->sz = len / 2;
+ b->ptr = malloc(b->sz);
+ for (i = 0; i < b->sz; i++) {
#ifdef _MSC_VER
unsigned int iX;
- sscanf(inp+(2*i), "%02hx", &iX);
+ sscanf(inp + (2 * i), "%02hx", &iX);
b->ptr[i] = (byte)iX;
#else
- sscanf(inp+(2*i), "%02hhx", &b->ptr[i]);
+ sscanf(inp + (2 * i), "%02hhx", &b->ptr[i]);
#endif
- }
- return true;
+ }
+ return true;
}
CTEST(cbor, error)
{
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_NO_ERROR], "CN_CBOR_NO_ERROR");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_OUT_OF_DATA], "CN_CBOR_ERR_OUT_OF_DATA");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED], "CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_ODD_SIZE_INDEF_MAP], "CN_CBOR_ERR_ODD_SIZE_INDEF_MAP");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_BREAK_OUTSIDE_INDEF], "CN_CBOR_ERR_BREAK_OUTSIDE_INDEF");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_MT_UNDEF_FOR_INDEF], "CN_CBOR_ERR_MT_UNDEF_FOR_INDEF");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_RESERVED_AI], "CN_CBOR_ERR_RESERVED_AI");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING], "CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_INVALID_PARAMETER], "CN_CBOR_ERR_INVALID_PARAMETER");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_OUT_OF_MEMORY], "CN_CBOR_ERR_OUT_OF_MEMORY");
- ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_FLOAT_NOT_SUPPORTED], "CN_CBOR_ERR_FLOAT_NOT_SUPPORTED");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_NO_ERROR], "CN_CBOR_NO_ERROR");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_OUT_OF_DATA], "CN_CBOR_ERR_OUT_OF_DATA");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED], "CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_ODD_SIZE_INDEF_MAP], "CN_CBOR_ERR_ODD_SIZE_INDEF_MAP");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_BREAK_OUTSIDE_INDEF], "CN_CBOR_ERR_BREAK_OUTSIDE_INDEF");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_MT_UNDEF_FOR_INDEF], "CN_CBOR_ERR_MT_UNDEF_FOR_INDEF");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_RESERVED_AI], "CN_CBOR_ERR_RESERVED_AI");
+ ASSERT_STR(
+ cn_cbor_error_str[CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING], "CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_INVALID_PARAMETER], "CN_CBOR_ERR_INVALID_PARAMETER");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_OUT_OF_MEMORY], "CN_CBOR_ERR_OUT_OF_MEMORY");
+ ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_FLOAT_NOT_SUPPORTED], "CN_CBOR_ERR_FLOAT_NOT_SUPPORTED");
}
CTEST(cbor, parse)
{
- cn_cbor_errback err;
- char *tests[] = {
- "00", // 0
- "01", // 1
- "17", // 23
- "1818", // 24
- "190100", // 256
- "1a00010000", // 65536
- "1b0000000100000000", // 4294967296
- "20", // -1
- "37", // -24
- "3818", // -25
- "390100", // -257
- "3a00010000", // -65537
- "3b0000000100000000", // -4294967297
- "4161", // h"a"
- "6161", // "a"
- "80", // []
- "8100", // [0]
- "820102", // [1,2]
- "818100", // [[0]]
- "a1616100", // {"a":0}
- "d8184100", // tag
- "f4", // false
- "f5", // true
- "f6", // null
- "f7", // undefined
- "f8ff", // simple(255)
+ cn_cbor_errback err;
+ char *tests[] = {
+ "00", // 0
+ "01", // 1
+ "17", // 23
+ "1818", // 24
+ "190100", // 256
+ "1a00010000", // 65536
+ "1b0000000100000000", // 4294967296
+ "20", // -1
+ "37", // -24
+ "3818", // -25
+ "390100", // -257
+ "3a00010000", // -65537
+ "3b0000000100000000", // -4294967297
+ "4161", // h"a"
+ "6161", // "a"
+ "80", // []
+ "8100", // [0]
+ "820102", // [1,2]
+ "818100", // [[0]]
+ "a1616100", // {"a":0}
+ "d8184100", // tag
+ "f4", // false
+ "f5", // true
+ "f6", // null
+ "f7", // undefined
+ "f8ff", // simple(255)
#ifndef CBOR_NO_FLOAT
- "f93c00", // 1.0
- "f9bc00", // -1.0
- "f903ff", // 6.097555160522461e-05
- "f90400", // 6.103515625e-05
- "f907ff", // 0.00012201070785522461
- "f90800", // 0.0001220703125
- "fa47800000", // 65536.0
- "fb3ff199999999999a", // 1.1
- "f97e00", // NaN
-#endif /* CBOR_NO_FLOAT */
- "5f42010243030405ff", // (_ h'0102', h'030405')
- "7f61616161ff", // (_ "a", "a")
- "9fff", // [_ ]
- "9f9f9fffffff", // [_ [_ [_ ]]]
- "9f009f00ff00ff", // [_ 0, [_ 0], 0]
- "bf61610161629f0203ffff", // {_ "a": 1, "b": [_ 2, 3]}
- };
- cn_cbor *cb;
- buffer b;
- size_t i;
- unsigned char encoded[1024];
- ssize_t enc_sz;
- ssize_t enc_sz2;
+ "f93c00", // 1.0
+ "f9bc00", // -1.0
+ "f903ff", // 6.097555160522461e-05
+ "f90400", // 6.103515625e-05
+ "f907ff", // 0.00012201070785522461
+ "f90800", // 0.0001220703125
+ "fa47800000", // 65536.0
+ "fb3ff199999999999a", // 1.1
+ "f97e00", // NaN
+#endif /* CBOR_NO_FLOAT */
+ "5f42010243030405ff", // (_ h'0102', h'030405')
+ "7f61616161ff", // (_ "a", "a")
+ "9fff", // [_ ]
+ "9f9f9fffffff", // [_ [_ [_ ]]]
+ "9f009f00ff00ff", // [_ 0, [_ 0], 0]
+ "bf61610161629f0203ffff", // {_ "a": 1, "b": [_ 2, 3]}
+ };
+ cn_cbor *cb;
+ buffer b;
+ size_t i;
+ unsigned char encoded[1024];
+ ssize_t enc_sz;
+ ssize_t enc_sz2;
- for (i=0; i<sizeof(tests)/sizeof(char*); i++) {
- ASSERT_TRUE(parse_hex(tests[i], &b));
- err.err = CN_CBOR_NO_ERROR;
- cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
- //CTEST_LOG("%s: %s", tests[i], cn_cbor_error_str[err.err]);
- ASSERT_EQUAL(err.err, CN_CBOR_NO_ERROR);
- ASSERT_NOT_NULL(cb);
+ for (i = 0; i < sizeof(tests) / sizeof(char *); i++) {
+ ASSERT_TRUE(parse_hex(tests[i], &b));
+ err.err = CN_CBOR_NO_ERROR;
+ cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
+ // CTEST_LOG("%s: %s", tests[i], cn_cbor_error_str[err.err]);
+ ASSERT_EQUAL(err.err, CN_CBOR_NO_ERROR);
+ ASSERT_NOT_NULL(cb);
- enc_sz2 = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), cb);
- enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), cb);
- ASSERT_EQUAL(enc_sz, enc_sz2);
- ASSERT_DATA(b.ptr, b.sz, encoded, enc_sz);
- free(b.ptr);
- cn_cbor_free(cb CONTEXT_NULL);
- }
+ enc_sz2 = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), cb);
+ enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), cb);
+ ASSERT_EQUAL(enc_sz, enc_sz2);
+ ASSERT_DATA(b.ptr, b.sz, encoded, enc_sz);
+ free(b.ptr);
+ cn_cbor_free(cb CONTEXT_NULL);
+ }
}
-
CTEST(cbor, parse_normalize)
{
- cn_cbor_errback err;
- char *basic_tests[] = {
- "00", "00", // 0
- "1800", "00",
- "1818", "1818",
- "190000", "00",
- "190018", "1818",
- "1a00000000", "00",
- "1b0000000000000000", "00",
- "20", "20", // -1
- "3800", "20",
- "c600", "c600", // 6(0) (undefined tag)
- "d80600", "c600",
- "d9000600", "c600",
- };
- char *float_tests[] = {
- "fb3ff0000000000000", "f93c00", // 1.0
- "fbbff0000000000000", "f9bc00", // -1.0
- "fb40f86a0000000000", "fa47c35000", // 100000.0
- "fb7ff8000000000000", "f97e00", // NaN
- "fb3e70000000000000", "f90001", // 5.960464477539063e-08
- "fb3e78000000000000", "fa33c00000", // 8.940696716308594e-08
- "fb3e80000000000000", "f90002", // 1.1920928955078125e-07
- };
- cn_cbor *cb;
- buffer b, b2;
- size_t i;
- unsigned char encoded[1024];
- ssize_t enc_sz;
- ssize_t enc_sz2;
+ cn_cbor_errback err;
+ char *basic_tests[] = {
+ "00",
+ "00", // 0
+ "1800",
+ "00",
+ "1818",
+ "1818",
+ "190000",
+ "00",
+ "190018",
+ "1818",
+ "1a00000000",
+ "00",
+ "1b0000000000000000",
+ "00",
+ "20",
+ "20", // -1
+ "3800",
+ "20",
+ "c600",
+ "c600", // 6(0) (undefined tag)
+ "d80600",
+ "c600",
+ "d9000600",
+ "c600",
+ };
+ char *float_tests[] = {
+ "fb3ff0000000000000", "f93c00", // 1.0
+ "fbbff0000000000000", "f9bc00", // -1.0
+ "fb40f86a0000000000", "fa47c35000", // 100000.0
+ "fb7ff8000000000000", "f97e00", // NaN
+ "fb3e70000000000000", "f90001", // 5.960464477539063e-08
+ "fb3e78000000000000", "fa33c00000", // 8.940696716308594e-08
+ "fb3e80000000000000", "f90002", // 1.1920928955078125e-07
+ };
+ cn_cbor *cb;
+ buffer b, b2;
+ size_t i;
+ unsigned char encoded[1024];
+ ssize_t enc_sz;
+ ssize_t enc_sz2;
- for (i=0; i<sizeof(basic_tests)/sizeof(char*); i+=2) {
- ASSERT_TRUE(parse_hex(basic_tests[i], &b));
- ASSERT_TRUE(parse_hex(basic_tests[i+1], &b2));
- err.err = CN_CBOR_NO_ERROR;
- cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
- CTEST_LOG("%s: %s", basic_tests[i], cn_cbor_error_str[err.err]);
- ASSERT_EQUAL(err.err, CN_CBOR_NO_ERROR);
- ASSERT_NOT_NULL(cb);
+ for (i = 0; i < sizeof(basic_tests) / sizeof(char *); i += 2) {
+ ASSERT_TRUE(parse_hex(basic_tests[i], &b));
+ ASSERT_TRUE(parse_hex(basic_tests[i + 1], &b2));
+ err.err = CN_CBOR_NO_ERROR;
+ cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
+ CTEST_LOG("%s: %s", basic_tests[i], cn_cbor_error_str[err.err]);
+ ASSERT_EQUAL(err.err, CN_CBOR_NO_ERROR);
+ ASSERT_NOT_NULL(cb);
- enc_sz2 = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), cb);
- enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), cb);
- ASSERT_EQUAL(enc_sz, enc_sz2);
- ASSERT_DATA(b2.ptr, b2.sz, encoded, enc_sz);
- free(b.ptr);
- free(b2.ptr);
- cn_cbor_free(cb CONTEXT_NULL);
- }
+ enc_sz2 = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), cb);
+ enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), cb);
+ ASSERT_EQUAL(enc_sz, enc_sz2);
+ ASSERT_DATA(b2.ptr, b2.sz, encoded, enc_sz);
+ free(b.ptr);
+ free(b2.ptr);
+ cn_cbor_free(cb CONTEXT_NULL);
+ }
- for (i=0; i<sizeof(float_tests)/sizeof(char*); i+=2) {
- ASSERT_TRUE(parse_hex(float_tests[i], &b));
- ASSERT_TRUE(parse_hex(float_tests[i+1], &b2));
- err.err = CN_CBOR_NO_ERROR;
- cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
- CTEST_LOG("%s: %s", float_tests[i], cn_cbor_error_str[err.err]);
+ for (i = 0; i < sizeof(float_tests) / sizeof(char *); i += 2) {
+ ASSERT_TRUE(parse_hex(float_tests[i], &b));
+ ASSERT_TRUE(parse_hex(float_tests[i + 1], &b2));
+ err.err = CN_CBOR_NO_ERROR;
+ cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
+ CTEST_LOG("%s: %s", float_tests[i], cn_cbor_error_str[err.err]);
#ifndef CBOR_NO_FLOAT
- ASSERT_EQUAL(err.err, CN_CBOR_NO_ERROR);
- ASSERT_NOT_NULL(cb);
-#else /* CBOR_NO_FLOAT */
- ASSERT_EQUAL(err.err, CN_CBOR_ERR_FLOAT_NOT_SUPPORTED);
- ASSERT_NULL(cb);
+ ASSERT_EQUAL(err.err, CN_CBOR_NO_ERROR);
+ ASSERT_NOT_NULL(cb);
+#else /* CBOR_NO_FLOAT */
+ ASSERT_EQUAL(err.err, CN_CBOR_ERR_FLOAT_NOT_SUPPORTED);
+ ASSERT_NULL(cb);
#endif /* CBOR_NO_FLOAT */
- /* enc_sz2 = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), cb); */
- /* enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), cb); */
- /* ASSERT_EQUAL(enc_sz, enc_sz2); */
- /* ASSERT_DATA(b2.ptr, b2.sz, encoded, enc_sz); */
- free(b.ptr);
- free(b2.ptr);
- cn_cbor_free(cb CONTEXT_NULL);
- }
+ /* enc_sz2 = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), cb); */
+ /* enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), cb); */
+ /* ASSERT_EQUAL(enc_sz, enc_sz2); */
+ /* ASSERT_DATA(b2.ptr, b2.sz, encoded, enc_sz); */
+ free(b.ptr);
+ free(b2.ptr);
+ cn_cbor_free(cb CONTEXT_NULL);
+ }
}
-typedef struct _cbor_failure
-{
- char *hex;
- cn_cbor_error err;
+typedef struct _cbor_failure {
+ char *hex;
+ cn_cbor_error err;
} cbor_failure;
CTEST(cbor, fail)
{
- cn_cbor_errback err;
- cbor_failure tests[] = {
- {"81", CN_CBOR_ERR_OUT_OF_DATA},
- {"0000", CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED},
- {"bf00ff", CN_CBOR_ERR_ODD_SIZE_INDEF_MAP},
- {"ff", CN_CBOR_ERR_BREAK_OUTSIDE_INDEF},
- {"1f", CN_CBOR_ERR_MT_UNDEF_FOR_INDEF},
- {"1c", CN_CBOR_ERR_RESERVED_AI},
- {"7f4100", CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING},
- };
- cn_cbor *cb;
- buffer b;
- size_t i;
- uint8_t buf[10];
- cn_cbor inv = {CN_CBOR_INVALID, 0, {0}, 0, NULL, NULL, NULL, NULL};
+ cn_cbor_errback err;
+ cbor_failure tests[] = {
+ {"81", CN_CBOR_ERR_OUT_OF_DATA},
+ {"0000", CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED},
+ {"bf00ff", CN_CBOR_ERR_ODD_SIZE_INDEF_MAP},
+ {"ff", CN_CBOR_ERR_BREAK_OUTSIDE_INDEF},
+ {"1f", CN_CBOR_ERR_MT_UNDEF_FOR_INDEF},
+ {"1c", CN_CBOR_ERR_RESERVED_AI},
+ {"7f4100", CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING},
+ };
+ cn_cbor *cb;
+ buffer b;
+ size_t i;
+ uint8_t buf[10];
+ cn_cbor inv = {CN_CBOR_INVALID, 0, {0}, 0, NULL, NULL, NULL, NULL};
- ASSERT_EQUAL(-1, cn_cbor_encoder_write(buf, 0, sizeof(buf), &inv));
+ ASSERT_EQUAL(-1, cn_cbor_encoder_write(buf, 0, sizeof(buf), &inv));
- for (i=0; i<sizeof(tests)/sizeof(cbor_failure); i++) {
- ASSERT_TRUE(parse_hex(tests[i].hex, &b));
- cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
- ASSERT_NULL(cb);
- ASSERT_EQUAL(err.err, tests[i].err);
+ for (i = 0; i < sizeof(tests) / sizeof(cbor_failure); i++) {
+ ASSERT_TRUE(parse_hex(tests[i].hex, &b));
+ cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
+ ASSERT_NULL(cb);
+ ASSERT_EQUAL(err.err, tests[i].err);
- free(b.ptr);
- cn_cbor_free(cb CONTEXT_NULL);
- }
+ free(b.ptr);
+ cn_cbor_free(cb CONTEXT_NULL);
+ }
}
// Decoder loses float size information
CTEST(cbor, float)
{
#ifndef CBOR_NO_FLOAT
- cn_cbor_errback err;
- char *tests[] = {
- "f90001", // 5.960464477539063e-08
- "f9c400", // -4.0
- "fa47c35000", // 100000.0
- "f97e00", // Half NaN, half beast
- "f9fc00", // -Inf
- "f97c00", // Inf
- };
- cn_cbor *cb;
- buffer b;
- size_t i;
- unsigned char encoded[1024];
- ssize_t enc_sz;
- ssize_t enc_sz2;
+ cn_cbor_errback err;
+ char *tests[] = {
+ "f90001", // 5.960464477539063e-08
+ "f9c400", // -4.0
+ "fa47c35000", // 100000.0
+ "f97e00", // Half NaN, half beast
+ "f9fc00", // -Inf
+ "f97c00", // Inf
+ };
+ cn_cbor *cb;
+ buffer b;
+ size_t i;
+ unsigned char encoded[1024];
+ ssize_t enc_sz;
+ ssize_t enc_sz2;
- for (i=0; i<sizeof(tests)/sizeof(char*); i++) {
- ASSERT_TRUE(parse_hex(tests[i], &b));
- cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
- ASSERT_NOT_NULL(cb);
+ for (i = 0; i < sizeof(tests) / sizeof(char *); i++) {
+ ASSERT_TRUE(parse_hex(tests[i], &b));
+ cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
+ ASSERT_NOT_NULL(cb);
- enc_sz2 = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), cb);
- enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), cb);
- ASSERT_EQUAL(enc_sz, enc_sz2);
- ASSERT_DATA(b.ptr, b.sz, encoded, enc_sz);
+ enc_sz2 = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), cb);
+ enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), cb);
+ ASSERT_EQUAL(enc_sz, enc_sz2);
+ ASSERT_DATA(b.ptr, b.sz, encoded, enc_sz);
- free(b.ptr);
- cn_cbor_free(cb CONTEXT_NULL);
- }
+ free(b.ptr);
+ cn_cbor_free(cb CONTEXT_NULL);
+ }
#endif /* CBOR_NO_FLOAT */
}
CTEST(cbor, getset)
{
- buffer b;
- cn_cbor *cb;
- cn_cbor *val;
- cn_cbor_errback err;
+ buffer b;
+ cn_cbor *cb;
+ cn_cbor *val;
+ cn_cbor_errback err;
- ASSERT_TRUE(parse_hex("a40000436363630262626201616100", &b));
- cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
- ASSERT_NOT_NULL(cb);
- val = cn_cbor_mapget_string(cb, "a");
- ASSERT_NOT_NULL(val);
- val = cn_cbor_mapget_string(cb, "bb");
- ASSERT_NOT_NULL(val);
- val = cn_cbor_mapget_string(cb, "ccc");
- ASSERT_NOT_NULL(val);
- val = cn_cbor_mapget_string(cb, "b");
- ASSERT_NULL(val);
- free(b.ptr);
- cn_cbor_free(cb CONTEXT_NULL);
+ ASSERT_TRUE(parse_hex("a40000436363630262626201616100", &b));
+ cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
+ ASSERT_NOT_NULL(cb);
+ val = cn_cbor_mapget_string(cb, "a");
+ ASSERT_NOT_NULL(val);
+ val = cn_cbor_mapget_string(cb, "bb");
+ ASSERT_NOT_NULL(val);
+ val = cn_cbor_mapget_string(cb, "ccc");
+ ASSERT_NOT_NULL(val);
+ val = cn_cbor_mapget_string(cb, "b");
+ ASSERT_NULL(val);
+ free(b.ptr);
+ cn_cbor_free(cb CONTEXT_NULL);
- ASSERT_TRUE(parse_hex("a3616100006161206162", &b));
- cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
- ASSERT_NOT_NULL(cb);
- val = cn_cbor_mapget_int(cb, 0);
- ASSERT_NOT_NULL(val);
- val = cn_cbor_mapget_int(cb, -1);
- ASSERT_NOT_NULL(val);
- val = cn_cbor_mapget_int(cb, 1);
- ASSERT_NULL(val);
- free(b.ptr);
- cn_cbor_free(cb CONTEXT_NULL);
+ ASSERT_TRUE(parse_hex("a3616100006161206162", &b));
+ cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
+ ASSERT_NOT_NULL(cb);
+ val = cn_cbor_mapget_int(cb, 0);
+ ASSERT_NOT_NULL(val);
+ val = cn_cbor_mapget_int(cb, -1);
+ ASSERT_NOT_NULL(val);
+ val = cn_cbor_mapget_int(cb, 1);
+ ASSERT_NULL(val);
+ free(b.ptr);
+ cn_cbor_free(cb CONTEXT_NULL);
- ASSERT_TRUE(parse_hex("8100", &b));
- cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
- ASSERT_NOT_NULL(cb);
- val = cn_cbor_index(cb, 0);
- ASSERT_NOT_NULL(val);
- val = cn_cbor_index(cb, 1);
- ASSERT_NULL(val);
- val = cn_cbor_index(cb, -1);
- ASSERT_NULL(val);
- free(b.ptr);
- cn_cbor_free(cb CONTEXT_NULL);
+ ASSERT_TRUE(parse_hex("8100", &b));
+ cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err);
+ ASSERT_NOT_NULL(cb);
+ val = cn_cbor_index(cb, 0);
+ ASSERT_NOT_NULL(val);
+ val = cn_cbor_index(cb, 1);
+ ASSERT_NULL(val);
+ val = cn_cbor_index(cb, -1);
+ ASSERT_NULL(val);
+ free(b.ptr);
+ cn_cbor_free(cb CONTEXT_NULL);
}
CTEST(cbor, create)
{
- cn_cbor_errback err;
- const cn_cbor* val;
- const char* data = "abc";
- cn_cbor *cb_map = cn_cbor_map_create(CONTEXT_NULL_COMMA &err);
- cn_cbor *cb_int;
- cn_cbor *cb_data;
+ cn_cbor_errback err;
+ const cn_cbor *val;
+ const char *data = "abc";
+ 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;
+ cn_cbor *cb_dbl;
#endif
- ASSERT_NOT_NULL(cb_map);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
+ ASSERT_NOT_NULL(cb_map);
+ ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
- cb_int = cn_cbor_int_create(256 CONTEXT_NULL, &err);
- ASSERT_NOT_NULL(cb_int);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
+ cb_int = cn_cbor_int_create(256 CONTEXT_NULL, &err);
+ ASSERT_NOT_NULL(cb_int);
+ ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
- cb_data = cn_cbor_data_create((const uint8_t*)data, 4 CONTEXT_NULL, &err);
- ASSERT_NOT_NULL(cb_data);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
+ cb_data = cn_cbor_data_create((const uint8_t *)data, 4 CONTEXT_NULL, &err);
+ 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);
+ 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);
+ 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);
- cn_cbor_mapput_int(cb_map, -7, cb_data CONTEXT_NULL, &err);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
- ASSERT_TRUE(cb_map->length == 4);
+ cn_cbor_mapput_int(cb_map, -7, cb_data CONTEXT_NULL, &err);
+ ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
+ ASSERT_TRUE(cb_map->length == 4);
- cn_cbor_mapput_string(cb_map, "foo",
- cn_cbor_string_create(data CONTEXT_NULL, &err)
- CONTEXT_NULL, &err);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
- ASSERT_TRUE(cb_map->length == 6);
+ cn_cbor_mapput_string(cb_map, "foo", cn_cbor_string_create(data CONTEXT_NULL, &err) CONTEXT_NULL, &err);
+ ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
+ ASSERT_TRUE(cb_map->length == 6);
- cn_cbor_map_put(cb_map,
- cn_cbor_string_create("bar" CONTEXT_NULL, &err),
- cn_cbor_string_create("qux" CONTEXT_NULL, &err),
- &err);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
- ASSERT_TRUE(cb_map->length == 8);
+ cn_cbor_map_put(
+ cb_map, cn_cbor_string_create("bar" CONTEXT_NULL, &err), cn_cbor_string_create("qux" CONTEXT_NULL, &err), &err);
+ 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);
+ 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);
+ val = cn_cbor_mapget_int(cb_map, 5);
+ ASSERT_NOT_NULL(val);
+ ASSERT_TRUE(val->v.sint == 256);
- val = cn_cbor_mapget_int(cb_map, -7);
- ASSERT_NOT_NULL(val);
- ASSERT_STR(val->v.str, "abc");
+ val = cn_cbor_mapget_int(cb_map, -7);
+ 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);
+ 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);
+ cn_cbor_free(cb_map CONTEXT_NULL);
}
CTEST(cbor, map_errors)
{
- cn_cbor_errback err;
- cn_cbor *ci;
- ci = cn_cbor_int_create(65536, CONTEXT_NULL_COMMA &err);
- cn_cbor_mapput_int(ci, -5, NULL, CONTEXT_NULL_COMMA &err);
- ASSERT_EQUAL(err.err, CN_CBOR_ERR_INVALID_PARAMETER);
- cn_cbor_mapput_string(ci, "foo", NULL, CONTEXT_NULL_COMMA &err);
- ASSERT_EQUAL(err.err, CN_CBOR_ERR_INVALID_PARAMETER);
- cn_cbor_map_put(ci, NULL, NULL, &err);
+ cn_cbor_errback err;
+ cn_cbor *ci;
+ ci = cn_cbor_int_create(65536, CONTEXT_NULL_COMMA & err);
+ cn_cbor_mapput_int(ci, -5, NULL, CONTEXT_NULL_COMMA & err);
+ ASSERT_EQUAL(err.err, CN_CBOR_ERR_INVALID_PARAMETER);
+ cn_cbor_mapput_string(ci, "foo", NULL, CONTEXT_NULL_COMMA & err);
+ ASSERT_EQUAL(err.err, CN_CBOR_ERR_INVALID_PARAMETER);
+ cn_cbor_map_put(ci, NULL, NULL, &err);
}
CTEST(cbor, array)
{
- cn_cbor_errback err;
- cn_cbor *a = cn_cbor_array_create(CONTEXT_NULL_COMMA &err);
- ASSERT_NOT_NULL(a);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
- ASSERT_EQUAL(a->length, 0);
+ cn_cbor_errback err;
+ cn_cbor *a = cn_cbor_array_create(CONTEXT_NULL_COMMA & err);
+ ASSERT_NOT_NULL(a);
+ ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
+ ASSERT_EQUAL(a->length, 0);
- cn_cbor_array_append(a, cn_cbor_int_create(256, CONTEXT_NULL_COMMA &err), &err);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
- ASSERT_EQUAL(a->length, 1);
+ cn_cbor_array_append(a, cn_cbor_int_create(256, CONTEXT_NULL_COMMA & err), &err);
+ ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
+ ASSERT_EQUAL(a->length, 1);
- cn_cbor_array_append(a, cn_cbor_string_create("five", CONTEXT_NULL_COMMA &err), &err);
- ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
- ASSERT_EQUAL(a->length, 2);
+ cn_cbor_array_append(a, cn_cbor_string_create("five", CONTEXT_NULL_COMMA & err), &err);
+ ASSERT_TRUE(err.err == CN_CBOR_NO_ERROR);
+ ASSERT_EQUAL(a->length, 2);
}
CTEST(cbor, array_errors)
{
- cn_cbor_errback err;
- cn_cbor *ci = cn_cbor_int_create(12, CONTEXT_NULL_COMMA &err);
- cn_cbor_array_append(NULL, ci, &err);
- ASSERT_EQUAL(err.err, CN_CBOR_ERR_INVALID_PARAMETER);
- cn_cbor_array_append(ci, NULL, &err);
- ASSERT_EQUAL(err.err, CN_CBOR_ERR_INVALID_PARAMETER);
+ cn_cbor_errback err;
+ cn_cbor *ci = cn_cbor_int_create(12, CONTEXT_NULL_COMMA & err);
+ cn_cbor_array_append(NULL, ci, &err);
+ ASSERT_EQUAL(err.err, CN_CBOR_ERR_INVALID_PARAMETER);
+ cn_cbor_array_append(ci, NULL, &err);
+ ASSERT_EQUAL(err.err, CN_CBOR_ERR_INVALID_PARAMETER);
}
CTEST(cbor, create_encode)
{
- cn_cbor *map;
- cn_cbor *cdata;
- char data[] = "data";
- unsigned char encoded[1024];
- ssize_t enc_sz;
+ cn_cbor *map;
+ cn_cbor *cdata;
+ char data[] = "data";
+ unsigned char encoded[1024];
+ ssize_t enc_sz;
- map = cn_cbor_map_create(CONTEXT_NULL_COMMA NULL);
- ASSERT_NOT_NULL(map);
+ map = cn_cbor_map_create(CONTEXT_NULL_COMMA NULL);
+ ASSERT_NOT_NULL(map);
- cdata = cn_cbor_data_create((uint8_t*)data, sizeof(data)-1, CONTEXT_NULL_COMMA NULL);
- ASSERT_NOT_NULL(cdata);
+ cdata = cn_cbor_data_create((uint8_t *)data, sizeof(data) - 1, CONTEXT_NULL_COMMA NULL);
+ ASSERT_NOT_NULL(cdata);
- ASSERT_TRUE(cn_cbor_mapput_int(map, 0, cdata, CONTEXT_NULL_COMMA NULL));
- enc_sz = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), map);
- ASSERT_EQUAL(7, enc_sz);
- enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), map);
- ASSERT_EQUAL(7, enc_sz);
+ ASSERT_TRUE(cn_cbor_mapput_int(map, 0, cdata, CONTEXT_NULL_COMMA NULL));
+ enc_sz = cn_cbor_encoder_write(NULL, 0, sizeof(encoded), map);
+ ASSERT_EQUAL(7, enc_sz);
+ enc_sz = cn_cbor_encoder_write(encoded, 0, sizeof(encoded), map);
+ ASSERT_EQUAL(7, enc_sz);
}
diff --git a/test/ctest.h b/test/ctest.h
index c971c55..cc11925 100644
--- a/test/ctest.h
+++ b/test/ctest.h
@@ -25,28 +25,28 @@
#endif
#ifndef UNUSED_PARAM
- /**
- * \def UNUSED_PARAM(p);
- *
- * A macro for quelling compiler warnings about unused variables.
- */
-# define UNUSED_PARAM(p) ((void)&(p))
+/**
+ * \def UNUSED_PARAM(p);
+ *
+ * A macro for quelling compiler warnings about unused variables.
+ */
+#define UNUSED_PARAM(p) ((void)&(p))
#endif /* UNUSED_PARM */
typedef void (*SetupFunc)(void*);
typedef void (*TearDownFunc)(void*);
struct ctest {
- const char* ssname; // suite name
- const char* ttname; // test name
- void (*run)();
- int skip;
+ const char* ssname; // suite name
+ const char* ttname; // test name
+ void (*run)();
+ int skip;
- void* data;
- SetupFunc setup;
- TearDownFunc teardown;
+ void* data;
+ SetupFunc setup;
+ TearDownFunc teardown;
- unsigned int magic;
+ unsigned int magic;
};
#define __FNAME(sname, tname) __ctest_##sname##_##tname##_run
@@ -54,42 +54,38 @@
#define __CTEST_MAGIC (0xdeadbeef)
#ifdef __APPLE__
-#define __Test_Section __attribute__ ((unused,section ("__DATA, .ctest")))
+#define __Test_Section __attribute__((unused, section("__DATA, .ctest")))
#define MS__Test_Section
#else
#ifdef _MSC_VER
#define __Test_Section
#define MS__Test_Section __declspec(allocate(".ctest"))
#else
-#define __Test_Section __attribute__ ((unused,section (".ctest")))
+#define __Test_Section __attribute__((unused, section(".ctest")))
#define MS__Test_Section
#endif
#endif
-#define __CTEST_STRUCT(sname, tname, _skip, __data, __setup, __teardown) \
- MS__Test_Section \
- struct ctest __TNAME(sname, tname) __Test_Section = { \
- .ssname=#sname, \
- .ttname=#tname, \
- .run = __FNAME(sname, tname), \
- .skip = _skip, \
- .data = __data, \
- .setup = (SetupFunc)__setup, \
- .teardown = (TearDownFunc)__teardown, \
- .magic = __CTEST_MAGIC };
+#define __CTEST_STRUCT(sname, tname, _skip, __data, __setup, __teardown) \
+ MS__Test_Section struct ctest __TNAME(sname, tname) __Test_Section = {.ssname = #sname, \
+ .ttname = #tname, \
+ .run = __FNAME(sname, tname), \
+ .skip = _skip, \
+ .data = __data, \
+ .setup = (SetupFunc)__setup, \
+ .teardown = (TearDownFunc)__teardown, \
+ .magic = __CTEST_MAGIC};
#define CTEST_DATA(sname) struct sname##_data
-#define CTEST_SETUP(sname) \
- void __attribute__ ((weak)) sname##_setup(struct sname##_data* data)
+#define CTEST_SETUP(sname) void __attribute__((weak)) sname##_setup(struct sname##_data* data)
-#define CTEST_TEARDOWN(sname) \
- void __attribute__ ((weak)) sname##_teardown(struct sname##_data* data)
+#define CTEST_TEARDOWN(sname) void __attribute__((weak)) sname##_teardown(struct sname##_data* data)
-#define __CTEST_INTERNAL(sname, tname, _skip) \
- void __FNAME(sname, tname)(); \
- __CTEST_STRUCT(sname, tname, _skip, NULL, NULL, NULL) \
- void __FNAME(sname, tname)()
+#define __CTEST_INTERNAL(sname, tname, _skip) \
+ void __FNAME(sname, tname)(); \
+ __CTEST_STRUCT(sname, tname, _skip, NULL, NULL, NULL) \
+ void __FNAME(sname, tname)()
#ifdef __APPLE__
#define SETUP_FNAME(sname) NULL
@@ -99,17 +95,16 @@
#define TEARDOWN_FNAME(sname) sname##_teardown
#endif
-#define __CTEST2_INTERNAL(sname, tname, _skip) \
- static struct sname##_data __ctest_##sname##_data; \
- CTEST_SETUP(sname); \
- CTEST_TEARDOWN(sname); \
- void __FNAME(sname, tname)(struct sname##_data* data); \
- __CTEST_STRUCT(sname, tname, _skip, &__ctest_##sname##_data, SETUP_FNAME(sname), TEARDOWN_FNAME(sname)) \
- void __FNAME(sname, tname)(struct sname##_data* data)
+#define __CTEST2_INTERNAL(sname, tname, _skip) \
+ static struct sname##_data __ctest_##sname##_data; \
+ CTEST_SETUP(sname); \
+ CTEST_TEARDOWN(sname); \
+ void __FNAME(sname, tname)(struct sname##_data * data); \
+ __CTEST_STRUCT(sname, tname, _skip, &__ctest_##sname##_data, SETUP_FNAME(sname), TEARDOWN_FNAME(sname)) \
+ void __FNAME(sname, tname)(struct sname##_data * data)
-
-void CTEST_LOG(char *fmt, ...);
-void CTEST_ERR(char *fmt, ...); // doesn't return
+void CTEST_LOG(char* fmt, ...);
+void CTEST_ERR(char* fmt, ...); // doesn't return
#define CTEST(sname, tname) __CTEST_INTERNAL(sname, tname, 0)
#define CTEST_SKIP(sname, tname) __CTEST_INTERNAL(sname, tname, 1)
@@ -117,15 +112,16 @@
#define CTEST2(sname, tname) __CTEST2_INTERNAL(sname, tname, 0)
#define CTEST2_SKIP(sname, tname) __CTEST2_INTERNAL(sname, tname, 1)
-
void assert_str(const char* exp, const char* real, const char* caller, int line);
#define ASSERT_STR(exp, real) assert_str(exp, real, __FILE__, __LINE__)
-void assert_data(const unsigned char* exp, int expsize,
- const unsigned char* real, int realsize,
- const char* caller, int line);
-#define ASSERT_DATA(exp, expsize, real, realsize) \
- assert_data(exp, expsize, real, realsize, __FILE__, __LINE__)
+void assert_data(const unsigned char* exp,
+ int expsize,
+ const unsigned char* real,
+ int realsize,
+ const char* caller,
+ int line);
+#define ASSERT_DATA(exp, expsize, real, realsize) assert_data(exp, expsize, real, realsize, __FILE__, __LINE__)
void assert_equal(long exp, long real, const char* caller, int line);
#define ASSERT_EQUAL(exp, real) assert_equal(exp, real, __FILE__, __LINE__)
@@ -180,161 +176,173 @@
typedef int (*filter_func)(struct ctest*);
-#define ANSI_BLACK "\033[0;30m"
-#define ANSI_RED "\033[0;31m"
-#define ANSI_GREEN "\033[0;32m"
-#define ANSI_YELLOW "\033[0;33m"
-#define ANSI_BLUE "\033[0;34m"
-#define ANSI_MAGENTA "\033[0;35m"
-#define ANSI_CYAN "\033[0;36m"
-#define ANSI_GREY "\033[0;37m"
+#define ANSI_BLACK "\033[0;30m"
+#define ANSI_RED "\033[0;31m"
+#define ANSI_GREEN "\033[0;32m"
+#define ANSI_YELLOW "\033[0;33m"
+#define ANSI_BLUE "\033[0;34m"
+#define ANSI_MAGENTA "\033[0;35m"
+#define ANSI_CYAN "\033[0;36m"
+#define ANSI_GREY "\033[0;37m"
#define ANSI_DARKGREY "\033[01;30m"
-#define ANSI_BRED "\033[01;31m"
-#define ANSI_BGREEN "\033[01;32m"
-#define ANSI_BYELLOW "\033[01;33m"
-#define ANSI_BBLUE "\033[01;34m"
+#define ANSI_BRED "\033[01;31m"
+#define ANSI_BGREEN "\033[01;32m"
+#define ANSI_BYELLOW "\033[01;33m"
+#define ANSI_BBLUE "\033[01;34m"
#define ANSI_BMAGENTA "\033[01;35m"
-#define ANSI_BCYAN "\033[01;36m"
-#define ANSI_WHITE "\033[01;37m"
-#define ANSI_NORMAL "\033[0m"
+#define ANSI_BCYAN "\033[01;36m"
+#define ANSI_WHITE "\033[01;37m"
+#define ANSI_NORMAL "\033[0m"
-static CTEST(suite, test) { }
+static CTEST(suite, test) {}
-static void msg_start(const char* color, const char* title) {
- int size;
- if (color_output) {
- size = snprintf(ctest_errormsg, ctest_errorsize, "%s", color);
- ctest_errorsize -= size;
- ctest_errormsg += size;
- }
- size = snprintf(ctest_errormsg, ctest_errorsize, " %s: ", title);
- ctest_errorsize -= size;
- ctest_errormsg += size;
-}
-
-static void msg_end() {
- int size;
- if (color_output) {
- size = snprintf(ctest_errormsg, ctest_errorsize, ANSI_NORMAL);
- ctest_errorsize -= size;
- ctest_errormsg += size;
- }
- size = snprintf(ctest_errormsg, ctest_errorsize, "\n");
- ctest_errorsize -= size;
- ctest_errormsg += size;
-}
-
-void CTEST_LOG(char *fmt, ...)
+static void msg_start(const char* color, const char* title)
{
- va_list argp;
- msg_start(ANSI_BLUE, "LOG");
-
- va_start(argp, fmt);
- int size = vsnprintf(ctest_errormsg, ctest_errorsize, fmt, argp);
- ctest_errorsize -= size;
- ctest_errormsg += size;
- va_end(argp);
-
- msg_end();
+ int size;
+ if (color_output) {
+ size = snprintf(ctest_errormsg, ctest_errorsize, "%s", color);
+ ctest_errorsize -= size;
+ ctest_errormsg += size;
+ }
+ size = snprintf(ctest_errormsg, ctest_errorsize, " %s: ", title);
+ ctest_errorsize -= size;
+ ctest_errormsg += size;
}
-void CTEST_ERR(char *fmt, ...)
+static void msg_end()
{
- va_list argp;
- msg_start(ANSI_YELLOW, "ERR");
-
- va_start(argp, fmt);
- int size = vsnprintf(ctest_errormsg, ctest_errorsize, fmt, argp);
- ctest_errorsize -= size;
- ctest_errormsg += size;
- va_end(argp);
-
- msg_end();
- longjmp(ctest_err, 1);
+ int size;
+ if (color_output) {
+ size = snprintf(ctest_errormsg, ctest_errorsize, ANSI_NORMAL);
+ ctest_errorsize -= size;
+ ctest_errormsg += size;
+ }
+ size = snprintf(ctest_errormsg, ctest_errorsize, "\n");
+ ctest_errorsize -= size;
+ ctest_errormsg += size;
}
-void assert_str(const char* exp, const char* real, const char* caller, int line) {
- if ((exp == NULL && real != NULL) ||
- (exp != NULL && real == NULL) ||
- (exp && real && strcmp(exp, real) != 0)) {
- CTEST_ERR("%s:%d expected '%s', got '%s'", caller, line, exp, real);
- }
+void CTEST_LOG(char* fmt, ...)
+{
+ va_list argp;
+ msg_start(ANSI_BLUE, "LOG");
+
+ va_start(argp, fmt);
+ int size = vsnprintf(ctest_errormsg, ctest_errorsize, fmt, argp);
+ ctest_errorsize -= size;
+ ctest_errormsg += size;
+ va_end(argp);
+
+ msg_end();
}
-void assert_data(const unsigned char* exp, int expsize,
- const unsigned char* real, int realsize,
- const char* caller, int line) {
- int i;
- if (expsize != realsize) {
- CTEST_ERR("%s:%d expected %d bytes, got %d", caller, line, expsize, realsize);
- }
- for (i=0; i<expsize; i++) {
- if (exp[i] != real[i]) {
- CTEST_ERR("%s:%d expected 0x%02x at offset %d got 0x%02x",
- caller, line, exp[i], i, real[i]);
- }
- }
+void CTEST_ERR(char* fmt, ...)
+{
+ va_list argp;
+ msg_start(ANSI_YELLOW, "ERR");
+
+ va_start(argp, fmt);
+ int size = vsnprintf(ctest_errormsg, ctest_errorsize, fmt, argp);
+ ctest_errorsize -= size;
+ ctest_errormsg += size;
+ va_end(argp);
+
+ msg_end();
+ longjmp(ctest_err, 1);
}
-void assert_equal(long exp, long real, const char* caller, int line) {
- if (exp != real) {
- CTEST_ERR("%s:%d expected %ld, got %ld", caller, line, exp, real);
- }
+void assert_str(const char* exp, const char* real, const char* caller, int line)
+{
+ if ((exp == NULL && real != NULL) || (exp != NULL && real == NULL) || (exp && real && strcmp(exp, real) != 0)) {
+ CTEST_ERR("%s:%d expected '%s', got '%s'", caller, line, exp, real);
+ }
}
-void assert_not_equal(long exp, long real, const char* caller, int line) {
- if ((exp) == (real)) {
- CTEST_ERR("%s:%d should not be %ld", caller, line, real);
- }
+void assert_data(const unsigned char* exp,
+ int expsize,
+ const unsigned char* real,
+ int realsize,
+ const char* caller,
+ int line)
+{
+ int i;
+ if (expsize != realsize) {
+ CTEST_ERR("%s:%d expected %d bytes, got %d", caller, line, expsize, realsize);
+ }
+ for (i = 0; i < expsize; i++) {
+ if (exp[i] != real[i]) {
+ CTEST_ERR("%s:%d expected 0x%02x at offset %d got 0x%02x", caller, line, exp[i], i, real[i]);
+ }
+ }
}
-void assert_null(void* real, const char* caller, int line) {
- if ((real) != NULL) {
- CTEST_ERR("%s:%d should be NULL", caller, line);
- }
+void assert_equal(long exp, long real, const char* caller, int line)
+{
+ if (exp != real) {
+ CTEST_ERR("%s:%d expected %ld, got %ld", caller, line, exp, real);
+ }
}
-void assert_not_null(const void* real, const char* caller, int line) {
- if (real == NULL) {
- CTEST_ERR("%s:%d should not be NULL", caller, line);
- }
+void assert_not_equal(long exp, long real, const char* caller, int line)
+{
+ if ((exp) == (real)) {
+ CTEST_ERR("%s:%d should not be %ld", caller, line, real);
+ }
}
-void assert_true(int real, const char* caller, int line) {
- if ((real) == 0) {
- CTEST_ERR("%s:%d should be true", caller, line);
- }
+void assert_null(void* real, const char* caller, int line)
+{
+ if ((real) != NULL) {
+ CTEST_ERR("%s:%d should be NULL", caller, line);
+ }
}
-void assert_false(int real, const char* caller, int line) {
- if ((real) != 0) {
- CTEST_ERR("%s:%d should be false", caller, line);
- }
+void assert_not_null(const void* real, const char* caller, int line)
+{
+ if (real == NULL) {
+ CTEST_ERR("%s:%d should not be NULL", caller, line);
+ }
}
-void assert_fail(const char* caller, int line) {
- CTEST_ERR("%s:%d shouldn't come here", caller, line);
+void assert_true(int real, const char* caller, int line)
+{
+ if ((real) == 0) {
+ CTEST_ERR("%s:%d should be true", caller, line);
+ }
}
-
-static int suite_all(struct ctest* t) {
- UNUSED_PARAM(t);
- return 1;
+void assert_false(int real, const char* caller, int line)
+{
+ if ((real) != 0) {
+ CTEST_ERR("%s:%d should be false", caller, line);
+ }
}
-static int suite_filter(struct ctest* t) {
- return strncmp(suite_name, t->ssname, strlen(suite_name)) == 0;
+void assert_fail(const char* caller, int line)
+{
+ CTEST_ERR("%s:%d shouldn't come here", caller, line);
+}
+
+static int suite_all(struct ctest* t)
+{
+ UNUSED_PARAM(t);
+ return 1;
+}
+
+static int suite_filter(struct ctest* t)
+{
+ return strncmp(suite_name, t->ssname, strlen(suite_name)) == 0;
}
#ifdef _MSC_VER
-int gettimeofday(struct timeval * tp, struct timezone * tzp)
+int gettimeofday(struct timeval* tp, struct timezone* tzp)
{
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
- SYSTEMTIME system_time;
- FILETIME file_time;
- uint64_t time;
+ SYSTEMTIME system_time;
+ FILETIME file_time;
+ uint64_t time;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
@@ -347,39 +355,41 @@
}
#endif
-static uint64_t getCurrentTime() {
- struct timeval now;
- gettimeofday(&now, NULL);
- uint64_t now64 = now.tv_sec;
- now64 *= 1000000;
- now64 += (now.tv_usec);
- return now64;
+static uint64_t getCurrentTime()
+{
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ uint64_t now64 = now.tv_sec;
+ now64 *= 1000000;
+ now64 += (now.tv_usec);
+ return now64;
}
-static void color_print(const char* color, const char* text) {
- if (color_output)
- printf("%s%s"ANSI_NORMAL"\n", color, text);
- else
- printf("%s\n", text);
+static void color_print(const char* color, const char* text)
+{
+ if (color_output)
+ printf("%s%s" ANSI_NORMAL "\n", color, text);
+ else
+ printf("%s\n", text);
}
#ifdef __APPLE__
-static void *find_symbol(struct ctest *test, const char *fname)
+static void* find_symbol(struct ctest* test, const char* fname)
{
- size_t len = strlen(test->ssname) + 1 + strlen(fname);
- char *symbol_name = (char *) malloc(len + 1);
- memset(symbol_name, 0, len + 1);
- snprintf(symbol_name, len + 1, "%s_%s", test->ssname, fname);
+ size_t len = strlen(test->ssname) + 1 + strlen(fname);
+ char* symbol_name = (char*)malloc(len + 1);
+ memset(symbol_name, 0, len + 1);
+ snprintf(symbol_name, len + 1, "%s_%s", test->ssname, fname);
- //fprintf(stderr, ">>>> dlsym: loading %s\n", symbol_name);
- void *symbol = dlsym(RTLD_DEFAULT, symbol_name);
- if (!symbol) {
- //fprintf(stderr, ">>>> ERROR: %s\n", dlerror());
- }
- // returns NULL on error
+ // fprintf(stderr, ">>>> dlsym: loading %s\n", symbol_name);
+ void* symbol = dlsym(RTLD_DEFAULT, symbol_name);
+ if (!symbol) {
+ // fprintf(stderr, ">>>> ERROR: %s\n", dlerror());
+ }
+ // returns NULL on error
- free(symbol_name);
- return symbol;
+ free(symbol_name);
+ return symbol;
}
#endif
@@ -387,113 +397,124 @@
#include <signal.h>
static void sighandler(int signum)
{
- char msg[128];
- sprintf(msg, "[SIGNAL %d: %s]", signum, sys_siglist[signum]);
- color_print(ANSI_BRED, msg);
- fflush(stdout);
+ char msg[128];
+ sprintf(msg, "[SIGNAL %d: %s]", signum, sys_siglist[signum]);
+ color_print(ANSI_BRED, msg);
+ fflush(stdout);
- /* "Unregister" the signal handler and send the signal back to the process
- * so it can terminate as expected */
- signal(signum, SIG_DFL);
- kill(getpid(), signum);
+ /* "Unregister" the signal handler and send the signal back to the process
+ * so it can terminate as expected */
+ signal(signum, SIG_DFL);
+ kill(getpid(), signum);
}
#endif
-int ctest_main(int argc, const char *argv[])
+int ctest_main(int argc, const char* argv[])
{
- static int total = 0;
- static int num_ok = 0;
- static int num_fail = 0;
- static int num_skip = 0;
- static int index = 1;
- static filter_func filter = suite_all;
+ static int total = 0;
+ static int num_ok = 0;
+ static int num_fail = 0;
+ static int num_skip = 0;
+ static int index = 1;
+ static filter_func filter = suite_all;
#ifdef CTEST_SEGFAULT
- signal(SIGSEGV, sighandler);
+ signal(SIGSEGV, sighandler);
#endif
- if (argc == 2) {
- suite_name = argv[1];
- filter = suite_filter;
- }
+ if (argc == 2) {
+ suite_name = argv[1];
+ filter = suite_filter;
+ }
- color_output = isatty(1);
+ color_output = isatty(1);
- uint64_t t1 = getCurrentTime();
+ uint64_t t1 = getCurrentTime();
- struct ctest* ctest_begin = &__TNAME(suite, test);
- struct ctest* ctest_end = &__TNAME(suite, test);
- // find begin and end of section by comparing magics
- while (1) {
- struct ctest* t = ctest_begin-1;
- if (t->magic != __CTEST_MAGIC) break;
- ctest_begin--;
- }
- while (1) {
- struct ctest* t = ctest_end+1;
- if (t->magic != __CTEST_MAGIC) break;
- ctest_end++;
- }
- ctest_end++; // end after last one
+ struct ctest* ctest_begin = &__TNAME(suite, test);
+ struct ctest* ctest_end = &__TNAME(suite, test);
+ // find begin and end of section by comparing magics
+ while (1) {
+ struct ctest* t = ctest_begin - 1;
+ if (t->magic != __CTEST_MAGIC)
+ break;
+ ctest_begin--;
+ }
+ while (1) {
+ struct ctest* t = ctest_end + 1;
+ if (t->magic != __CTEST_MAGIC)
+ break;
+ ctest_end++;
+ }
+ ctest_end++; // end after last one
- static struct ctest* test;
- for (test = ctest_begin; test != ctest_end; test++) {
- if (test == &__ctest_suite_test) continue;
- if (filter(test)) total++;
- }
+ static struct ctest* test;
+ for (test = ctest_begin; test != ctest_end; test++) {
+ if (test == &__ctest_suite_test)
+ continue;
+ if (filter(test))
+ total++;
+ }
- for (test = ctest_begin; test != ctest_end; test++) {
- if (test == &__ctest_suite_test) continue;
- if (filter(test)) {
- ctest_errorbuffer[0] = 0;
- ctest_errorsize = MSG_SIZE-1;
- ctest_errormsg = ctest_errorbuffer;
- printf("TEST %d/%d %s:%s ", index, total, test->ssname, test->ttname);
- fflush(stdout);
- if (test->skip) {
- color_print(ANSI_BYELLOW, "[SKIPPED]");
- num_skip++;
- } else {
- int result = setjmp(ctest_err);
- if (result == 0) {
+ for (test = ctest_begin; test != ctest_end; test++) {
+ if (test == &__ctest_suite_test)
+ continue;
+ if (filter(test)) {
+ ctest_errorbuffer[0] = 0;
+ ctest_errorsize = MSG_SIZE - 1;
+ ctest_errormsg = ctest_errorbuffer;
+ printf("TEST %d/%d %s:%s ", index, total, test->ssname, test->ttname);
+ fflush(stdout);
+ if (test->skip) {
+ color_print(ANSI_BYELLOW, "[SKIPPED]");
+ num_skip++;
+ }
+ else {
+ int result = setjmp(ctest_err);
+ if (result == 0) {
#ifdef __APPLE__
- if (!test->setup) {
- test->setup = (SetupFunc)find_symbol(test, "setup");
- }
- if (!test->teardown) {
- test->teardown = (SetupFunc)find_symbol(test, "teardown");
- }
+ if (!test->setup) {
+ test->setup = (SetupFunc)find_symbol(test, "setup");
+ }
+ if (!test->teardown) {
+ test->teardown = (SetupFunc)find_symbol(test, "teardown");
+ }
#endif
- if (test->setup) test->setup(test->data);
- if (test->data)
- test->run(test->data);
- else
- test->run();
- if (test->teardown) test->teardown(test->data);
- // if we got here it's ok
+ if (test->setup)
+ test->setup(test->data);
+ if (test->data)
+ test->run(test->data);
+ else
+ test->run();
+ if (test->teardown)
+ test->teardown(test->data);
+ // if we got here it's ok
#ifdef COLOR_OK
- color_print(ANSI_BGREEN, "[OK]");
+ color_print(ANSI_BGREEN, "[OK]");
#else
- printf("[OK]\n");
+ printf("[OK]\n");
#endif
- num_ok++;
- } else {
- color_print(ANSI_BRED, "[FAIL]");
- num_fail++;
- }
- if (ctest_errorsize != MSG_SIZE-1) printf("%s", ctest_errorbuffer);
- }
- index++;
- }
- }
- uint64_t t2 = getCurrentTime();
+ num_ok++;
+ }
+ else {
+ color_print(ANSI_BRED, "[FAIL]");
+ num_fail++;
+ }
+ if (ctest_errorsize != MSG_SIZE - 1)
+ printf("%s", ctest_errorbuffer);
+ }
+ index++;
+ }
+ }
+ uint64_t t2 = getCurrentTime();
- const char* color = (num_fail) ? ANSI_BRED : ANSI_GREEN;
- char results[80];
- sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped) ran in %"PRIu64" ms", total, num_ok, num_fail, num_skip, (t2 - t1)/1000);
- color_print(color, results);
- return num_fail;
+ const char* color = (num_fail) ? ANSI_BRED : ANSI_GREEN;
+ char results[80];
+ sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped) ran in %" PRIu64 " ms", total, num_ok, num_fail,
+ num_skip, (t2 - t1) / 1000);
+ color_print(color, results);
+ return num_fail;
}
#endif
diff --git a/test/test.c b/test/test.c
index b7d85af..8cee485 100644
--- a/test/test.c
+++ b/test/test.c
@@ -15,122 +15,161 @@
#define ERROR(msg, p) fprintf(stderr, "ERROR: " msg " %s\n", (p));
-static unsigned char* load_file(const char* filepath, unsigned char **end) {
- struct stat st;
- if (stat(filepath, &st)==-1) {
- ERROR("can't find file", filepath);
- return 0;
- }
- int fd=open(filepath, O_RDONLY);
- if (fd==-1) {
- ERROR("can't open file", filepath);
- return 0;
- }
- unsigned char* text=malloc(st.st_size+1); // this is not going to be freed
- if (st.st_size!=read(fd, text, st.st_size)) {
- ERROR("can't read file", filepath);
- close(fd);
- return 0;
- }
- close(fd);
- text[st.st_size]='\0';
- *end = text + st.st_size;
- return text;
+static unsigned char *load_file(const char *filepath, unsigned char **end)
+{
+ struct stat st;
+ if (stat(filepath, &st) == -1) {
+ ERROR("can't find file", filepath);
+ return 0;
+ }
+ int fd = open(filepath, O_RDONLY);
+ if (fd == -1) {
+ ERROR("can't open file", filepath);
+ return 0;
+ }
+ unsigned char *text = malloc(st.st_size + 1); // this is not going to be freed
+ if (st.st_size != read(fd, text, st.st_size)) {
+ ERROR("can't read file", filepath);
+ close(fd);
+ return 0;
+ }
+ close(fd);
+ text[st.st_size] = '\0';
+ *end = text + st.st_size;
+ return text;
}
-static void dump(const cn_cbor* cb, char* out, char** end, int indent) {
- if (!cb)
- goto done;
- int i;
- cn_cbor* cp;
- char finchar = ')'; /* most likely */
+static void dump(const cn_cbor *cb, char *out, char **end, int indent)
+{
+ if (!cb)
+ goto done;
+ int i;
+ cn_cbor *cp;
+ char finchar = ')'; /* most likely */
-#define CPY(s, l) memcpy(out, s, l); out += l;
-#define OUT(s) CPY(s, sizeof(s)-1)
+#define CPY(s, l) \
+ memcpy(out, s, l); \
+ out += l;
+#define OUT(s) CPY(s, sizeof(s) - 1)
#define PRF(f, a) out += sprintf(out, f, a)
- for (i = 0; i < indent; i++) *out++ = ' ';
- switch (cb->type) {
- case CN_CBOR_TEXT_CHUNKED: OUT("(_\n"); goto sequence;
- case CN_CBOR_BYTES_CHUNKED: OUT("(_\n\n"); goto sequence;
- case CN_CBOR_TAG: PRF("%ld(\n", cb->v.sint); goto sequence;
- case CN_CBOR_ARRAY: finchar = ']'; OUT("[\n"); goto sequence;
- case CN_CBOR_MAP: finchar = '}'; OUT("{\n"); goto sequence;
- sequence:
- for (cp = cb->first_child; cp; cp = cp->next) {
- dump(cp, out, &out, indent+2);
- }
- for (i=0; i<indent; i++) *out++ = ' ';
- *out++ = finchar;
- break;
- case CN_CBOR_BYTES: OUT("h'");
- for (i=0; i<cb->length; i++)
- PRF("%02x", cb->v.str[i] & 0xff);
- *out++ = '\'';
- break;
- case CN_CBOR_TEXT: *out++ = '"';
- CPY(cb->v.str, cb->length); /* should escape stuff */
- *out++ = '"';
- break;
- case CN_CBOR_NULL: OUT("null"); break;
- case CN_CBOR_TRUE: OUT("true"); break;
- case CN_CBOR_FALSE: OUT("false"); break;
- case CN_CBOR_UNDEF: OUT("simple(23)"); break;
- case CN_CBOR_INT: PRF("%ld", cb->v.sint); break;
- case CN_CBOR_UINT: PRF("%lu", cb->v.uint); break;
- case CN_CBOR_DOUBLE: PRF("%e", cb->v.dbl); break;
- case CN_CBOR_SIMPLE: PRF("simple(%ld)", cb->v.sint); break;
- default: PRF("???%d???", cb->type); break;
- }
- *out++ = '\n';
+ for (i = 0; i < indent; i++)
+ *out++ = ' ';
+ switch (cb->type) {
+ case CN_CBOR_TEXT_CHUNKED:
+ OUT("(_\n");
+ goto sequence;
+ case CN_CBOR_BYTES_CHUNKED:
+ OUT("(_\n\n");
+ goto sequence;
+ case CN_CBOR_TAG:
+ PRF("%ld(\n", cb->v.sint);
+ goto sequence;
+ case CN_CBOR_ARRAY:
+ finchar = ']';
+ OUT("[\n");
+ goto sequence;
+ case CN_CBOR_MAP:
+ finchar = '}';
+ OUT("{\n");
+ goto sequence;
+ sequence:
+ for (cp = cb->first_child; cp; cp = cp->next) {
+ dump(cp, out, &out, indent + 2);
+ }
+ for (i = 0; i < indent; i++)
+ *out++ = ' ';
+ *out++ = finchar;
+ break;
+ case CN_CBOR_BYTES:
+ OUT("h'");
+ for (i = 0; i < cb->length; i++)
+ PRF("%02x", cb->v.str[i] & 0xff);
+ *out++ = '\'';
+ break;
+ case CN_CBOR_TEXT:
+ *out++ = '"';
+ CPY(cb->v.str, cb->length); /* should escape stuff */
+ *out++ = '"';
+ break;
+ case CN_CBOR_NULL:
+ OUT("null");
+ break;
+ case CN_CBOR_TRUE:
+ OUT("true");
+ break;
+ case CN_CBOR_FALSE:
+ OUT("false");
+ break;
+ case CN_CBOR_UNDEF:
+ OUT("simple(23)");
+ break;
+ case CN_CBOR_INT:
+ PRF("%ld", cb->v.sint);
+ break;
+ case CN_CBOR_UINT:
+ PRF("%lu", cb->v.uint);
+ break;
+ case CN_CBOR_DOUBLE:
+ PRF("%e", cb->v.dbl);
+ break;
+ case CN_CBOR_SIMPLE:
+ PRF("simple(%ld)", cb->v.sint);
+ break;
+ default:
+ PRF("???%d???", cb->type);
+ break;
+ }
+ *out++ = '\n';
done:
- *end = out;
+ *end = out;
}
-
const char *err_name[] = {
- "CN_CBOR_NO_ERROR",
- "CN_CBOR_ERR_OUT_OF_DATA",
- "CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED",
- "CN_CBOR_ERR_ODD_SIZE_INDEF_MAP",
- "CN_CBOR_ERR_BREAK_OUTSIDE_INDEF",
- "CN_CBOR_ERR_MT_UNDEF_FOR_INDEF",
- "CN_CBOR_ERR_RESERVED_AI",
- "CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING",
- "CN_CBOR_ERR_OUT_OF_MEMORY",
- "CN_CBOR_ERR_FLOAT_NOT_SUPPORTED",
+ "CN_CBOR_NO_ERROR",
+ "CN_CBOR_ERR_OUT_OF_DATA",
+ "CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED",
+ "CN_CBOR_ERR_ODD_SIZE_INDEF_MAP",
+ "CN_CBOR_ERR_BREAK_OUTSIDE_INDEF",
+ "CN_CBOR_ERR_MT_UNDEF_FOR_INDEF",
+ "CN_CBOR_ERR_RESERVED_AI",
+ "CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING",
+ "CN_CBOR_ERR_OUT_OF_MEMORY",
+ "CN_CBOR_ERR_FLOAT_NOT_SUPPORTED",
};
-static void cn_cbor_decode_test(const unsigned char *buf, int len) {
- struct cn_cbor_errback back;
- const cn_cbor *ret = cn_cbor_decode(buf, len CBOR_CONTEXT_PARAM, &back);
- if (ret)
- printf("oops 1");
- printf("%s at %d\n", err_name[back.err], back.pos);
+static void cn_cbor_decode_test(const unsigned char *buf, int len)
+{
+ struct cn_cbor_errback back;
+ const cn_cbor *ret = cn_cbor_decode(buf, len CBOR_CONTEXT_PARAM, &back);
+ if (ret)
+ printf("oops 1");
+ printf("%s at %d\n", err_name[back.err], back.pos);
}
-int main(void) {
- char buf[100000];
- unsigned char *end;
- char *bufend;
- unsigned char *s = load_file("cases.cbor", &end);
- printf("%zd\n", end-s);
- cn_cbor *cb = cn_cbor_decode(s, end-s CBOR_CONTEXT_PARAM, 0);
- if (cb) {
- dump(cb, buf, &bufend, 0);
- *bufend = 0;
- printf("%s\n", buf);
- cn_cbor_free(cb CBOR_CONTEXT_PARAM);
- cb = 0; /* for leaks testing */
- }
- cn_cbor_decode_test((const unsigned char*)"\xff", 1); /* break outside indef */
- cn_cbor_decode_test((const unsigned char*)"\x1f", 1); /* mt undef for indef */
- cn_cbor_decode_test((const unsigned char*)"\x00\x00", 2); /* not all data consumed */
- cn_cbor_decode_test((const unsigned char*)"\x81", 1); /* out of data */
- cn_cbor_decode_test((const unsigned char*)"\x1c", 1); /* reserved ai */
- cn_cbor_decode_test((const unsigned char*)"\xbf\x00\xff", 3); /* odd size indef map */
- cn_cbor_decode_test((const unsigned char*)"\x7f\x40\xff", 3); /* wrong nesting in indef string */
- system("leaks test");
+int main(void)
+{
+ char buf[100000];
+ unsigned char *end;
+ char *bufend;
+ unsigned char *s = load_file("cases.cbor", &end);
+ printf("%zd\n", end - s);
+ cn_cbor *cb = cn_cbor_decode(s, end - s CBOR_CONTEXT_PARAM, 0);
+ if (cb) {
+ dump(cb, buf, &bufend, 0);
+ *bufend = 0;
+ printf("%s\n", buf);
+ cn_cbor_free(cb CBOR_CONTEXT_PARAM);
+ cb = 0; /* for leaks testing */
+ }
+ cn_cbor_decode_test((const unsigned char *)"\xff", 1); /* break outside indef */
+ cn_cbor_decode_test((const unsigned char *)"\x1f", 1); /* mt undef for indef */
+ cn_cbor_decode_test((const unsigned char *)"\x00\x00", 2); /* not all data consumed */
+ cn_cbor_decode_test((const unsigned char *)"\x81", 1); /* out of data */
+ cn_cbor_decode_test((const unsigned char *)"\x1c", 1); /* reserved ai */
+ cn_cbor_decode_test((const unsigned char *)"\xbf\x00\xff", 3); /* odd size indef map */
+ cn_cbor_decode_test((const unsigned char *)"\x7f\x40\xff", 3); /* wrong nesting in indef string */
+ system("leaks test");
}
/* cn-cbor.c:112: CN_CBOR_FAIL("out of memory"); */