summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrimio <vasi.vilvoiu@gmail.com>2018-03-01 23:33:16 +0200
committerrimio <vasi.vilvoiu@gmail.com>2018-03-01 23:33:16 +0200
commit3d821056f53edcf6fc8786f0d156fa56720a0c5a (patch)
treec44fa080dfeaef8458519219bb9db0786c125d3e
parent85e583715c02dcd9edb0875943327957e53227f8 (diff)
Refactor major_type->type; hide major type 7 from user
-rw-r--r--include/ecbor.h71
-rw-r--r--src/ecbor.c46
-rw-r--r--src/ecbor_decoder.c69
-rw-r--r--src/ecbor_describe.c20
-rw-r--r--src/ecbor_internal.h5
5 files changed, 111 insertions, 100 deletions
diff --git a/include/ecbor.h b/include/ecbor.h
index 730d786..db6786b 100644
--- a/include/ecbor.h
+++ b/include/ecbor.h
@@ -58,38 +58,41 @@ typedef enum {
*/
/*
- * CBOR major types
+ * CBOR types
*/
typedef enum {
- /* first type, used for bounds checking */
- ECBOR_MT_FIRST = 0,
-
- /* Major types as defined in the RFC */
- ECBOR_MT_UINT = 0,
- ECBOR_MT_NINT = 1,
- ECBOR_MT_BSTR = 2,
- ECBOR_MT_STR = 3,
- ECBOR_MT_ARRAY = 4,
- ECBOR_MT_MAP = 5,
- ECBOR_MT_TAG = 6,
- ECBOR_MT_SPECIAL = 7, /* this will be translated to a custom type */
+ /* Unknown type */
+ ECBOR_TYPE_UNDEFINED = -1,
- /* Custom types */
- ECBOR_MT_UNDEFINED = -1,
- ECBOR_MT_FP16 = 8,
- ECBOR_MT_FP32 = 9,
- ECBOR_MT_FP64 = 10,
+ /* First type, used for bounds checking */
+ ECBOR_TYPE_FIRST = 0,
+
+ /* Following types have a correspondent as a major type, as per RFC */
+ ECBOR_TYPE_UINT = 0,
+ ECBOR_TYPE_NINT = 1,
+ ECBOR_TYPE_BSTR = 2,
+ ECBOR_TYPE_STR = 3,
+ ECBOR_TYPE_ARRAY = 4,
+ ECBOR_TYPE_MAP = 5,
+ ECBOR_TYPE_TAG = 6,
+ /* Major type #7 is not exposed, but translated in one of the following.
+ * We keep value 7 reserved so there is no confusion. */
+
+ /* Following types are translated from major type #7 */
+ ECBOR_TYPE_FP16 = 8,
+ ECBOR_TYPE_FP32 = 9,
+ ECBOR_TYPE_FP64 = 10,
- /* last code, used for bounds checking */
- ECBOR_MT_LAST = 10
-} ecbor_major_type_t;
+ /* Last type, used for bounds checking */
+ ECBOR_TYPE_LAST = 10
+} ecbor_type_t;
/*
* CBOR Item
*/
typedef struct {
- /* major type and additional info of item */
- ecbor_major_type_t major_type;
+ /* item type */
+ ecbor_type_t type;
/* value */
union {
@@ -223,7 +226,7 @@ ecbor_get_root (ecbor_decode_context_t *context, ecbor_node_t **root);
/*
* Strict API
*/
-extern ecbor_major_type_t
+extern ecbor_type_t
ecbor_get_type (ecbor_item_t *item);
extern ecbor_error_t
@@ -248,33 +251,33 @@ ecbor_get_tag_item (ecbor_item_t *tag, ecbor_item_t *arr_item);
* Inline API
*/
#define ECBOR_GET_TYPE(i) \
- ((i).major_type)
+ ((i).type)
#define ECBOR_IS_INDEFINITE(i) \
((i).is_indefinite)
#define ECBOR_IS_NINT(i) \
- ((i).major_type == ECBOR_MT_NINT)
+ ((i).type == ECBOR_TYPE_NINT)
#define ECBOR_IS_UINT(i) \
- ((i).major_type == ECBOR_MT_UINT)
+ ((i).type == ECBOR_TYPE_UINT)
#define ECBOR_IS_INTEGER(i) \
(ECBOR_IS_NINT(i) || ECBOR_IS_UINT(i))
#define ECBOR_IS_BSTR(i) \
- ((i).major_type == ECBOR_MT_BSTR)
+ ((i).type == ECBOR_TYPE_BSTR)
#define ECBOR_IS_STR(i) \
- ((i).major_type == ECBOR_MT_STR)
+ ((i).type == ECBOR_TYPE_STR)
#define ECBOR_IS_ARRAY(i) \
- ((i).major_type == ECBOR_MT_ARRAY)
+ ((i).type == ECBOR_TYPE_ARRAY)
#define ECBOR_IS_MAP(i) \
- ((i).major_type == ECBOR_MT_MAP)
+ ((i).type == ECBOR_TYPE_MAP)
#define ECBOR_IS_TAG(i) \
- ((i).major_type == ECBOR_MT_TAG)
+ ((i).type == ECBOR_TYPE_TAG)
#define ECBOR_IS_FP32(i) \
- ((i).major_type == ECBOR_MT_FP32)
+ ((i).type == ECBOR_TYPE_FP32)
#define ECBOR_IS_FLOAT(i) \
ECBOR_IS_FP32(i)
#define ECBOR_IS_FP64(i) \
- ((i).major_type == ECBOR_MT_FP64)
+ ((i).type == ECBOR_TYPE_FP64)
#define ECBOR_IS_DOUBLE(i) \
ECBOR_IS_FP64(i)
diff --git a/src/ecbor.c b/src/ecbor.c
index 0a5e3fb..b2580d8 100644
--- a/src/ecbor.c
+++ b/src/ecbor.c
@@ -84,17 +84,17 @@ ecbor_fp64_from_big_endian (double value)
#endif
}
-extern ecbor_major_type_t
+extern ecbor_type_t
ecbor_get_type (ecbor_item_t *item)
{
if (!item) {
- return ECBOR_MT_UNDEFINED;
+ return ECBOR_TYPE_UNDEFINED;
}
- if (item->major_type < ECBOR_MT_FIRST
- || item->major_type > ECBOR_MT_LAST) {
- return ECBOR_MT_UNDEFINED;
+ if (item->type < ECBOR_TYPE_FIRST
+ || item->type > ECBOR_TYPE_LAST) {
+ return ECBOR_TYPE_UNDEFINED;
}
- return item->major_type;
+ return item->type;
}
extern ecbor_error_t
@@ -107,35 +107,35 @@ ecbor_get_value (ecbor_item_t *item, void *value)
return ECBOR_ERR_NULL_VALUE;
}
- switch (item->major_type) {
- case ECBOR_MT_NINT:
+ switch (item->type) {
+ case ECBOR_TYPE_NINT:
*((int64_t *)value) = item->value.integer;
break;
- case ECBOR_MT_UINT:
+ case ECBOR_TYPE_UINT:
*((uint64_t *)value) = item->value.uinteger;
break;
- case ECBOR_MT_BSTR:
- case ECBOR_MT_STR:
+ case ECBOR_TYPE_BSTR:
+ case ECBOR_TYPE_STR:
if (item->is_indefinite) {
return ECBOR_ERR_WONT_RETURN_INDEFINITE;
}
*((uint8_t **)value) = (uint8_t *) item->value.string.str;
break;
- case ECBOR_MT_TAG:
+ case ECBOR_TYPE_TAG:
*((uint64_t *)value) = item->value.tag.tag_value;
break;
- case ECBOR_MT_FP16:
+ case ECBOR_TYPE_FP16:
return ECBOR_ERR_CURRENTLY_NOT_SUPPORTED;
- case ECBOR_MT_FP32:
+ case ECBOR_TYPE_FP32:
*((float *)value) = item->value.fp32;
break;
- case ECBOR_MT_FP64:
+ case ECBOR_TYPE_FP64:
*((double *)value) = item->value.fp64;
break;
@@ -156,14 +156,14 @@ ecbor_get_length (ecbor_item_t *item, uint64_t *length)
return ECBOR_ERR_NULL_VALUE;
}
- switch (item->major_type) {
- case ECBOR_MT_BSTR:
- case ECBOR_MT_STR:
- case ECBOR_MT_ARRAY:
+ switch (item->type) {
+ case ECBOR_TYPE_BSTR:
+ case ECBOR_TYPE_STR:
+ case ECBOR_TYPE_ARRAY:
*length = item->length;
break;
- case ECBOR_MT_MAP:
+ case ECBOR_TYPE_MAP:
*length = item->length / 2;
break;
@@ -185,7 +185,7 @@ ecbor_get_array_item (ecbor_item_t *array, uint64_t index,
if (!array) {
return ECBOR_ERR_NULL_ARRAY;
}
- if (array->major_type != ECBOR_MT_ARRAY) {
+ if (array->type != ECBOR_TYPE_ARRAY) {
return ECBOR_ERR_INVALID_TYPE;
}
if (array->length <= index) {
@@ -225,7 +225,7 @@ ecbor_get_map_item (ecbor_item_t *map, uint64_t index, ecbor_item_t *key,
if (!map) {
return ECBOR_ERR_NULL_MAP;
}
- if (map->major_type != ECBOR_MT_MAP) {
+ if (map->type != ECBOR_TYPE_MAP) {
return ECBOR_ERR_INVALID_TYPE;
}
if (map->length <= (index * 2)) {
@@ -272,7 +272,7 @@ ecbor_get_tag_item (ecbor_item_t *tag, ecbor_item_t *item)
if (!tag) {
return ECBOR_ERR_NULL_ITEM;
}
- if (tag->major_type != ECBOR_MT_TAG) {
+ if (tag->type != ECBOR_TYPE_TAG) {
return ECBOR_ERR_INVALID_TYPE;
}
if (!item) {
diff --git a/src/ecbor_decoder.c b/src/ecbor_decoder.c
index 1c065d8..8d114dd 100644
--- a/src/ecbor_decoder.c
+++ b/src/ecbor_decoder.c
@@ -199,7 +199,7 @@ static ecbor_error_t
ecbor_decode_next_internal (ecbor_decode_context_t *context,
ecbor_item_t *item,
int8_t is_chunk,
- ecbor_major_type_t chunk_mtype)
+ ecbor_type_t chunk_mtype)
{
unsigned int additional;
@@ -214,21 +214,21 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
}
/* clear item, just so we do not leave garbage on partial read */
- item->major_type = ECBOR_MT_UNDEFINED;
+ item->type = ECBOR_TYPE_UNDEFINED;
item->size = 0;
item->length = 0;
item->is_indefinite = false;
/* extract major type (most significant three bits) and additional info */
- item->major_type = (*context->in_position >> 5) & 0x07;
+ item->type = (*context->in_position >> 5) & 0x07;
additional = (*context->in_position & 0x1f);
context->in_position ++; context->bytes_left --;
/* check mandatory major type (in case we are reading string chunks);
we do not want to continue parsing a malformed indefinite string and
potentially explode the stack with subsequent calls */
- if (is_chunk && chunk_mtype != item->major_type) {
- if (item->major_type == ECBOR_MT_SPECIAL
+ if (is_chunk && chunk_mtype != item->type) {
+ if (item->type == (ecbor_type_t) ECBOR_TYPE_SPECIAL
&& additional == ECBOR_ADDITIONAL_INDEFINITE) {
/* this is a valid stop code, pass it directly; note that this branch is
only taken when inside an indefinite string */
@@ -240,16 +240,16 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
}
/* handle type */
- switch (item->major_type) {
+ switch (item->type) {
/*
* Integer types
*/
- case ECBOR_MT_UINT:
+ case ECBOR_TYPE_UINT:
return ecbor_decode_uint (context, &item->value.uinteger, &item->size,
additional);
- case ECBOR_MT_NINT:
+ case ECBOR_TYPE_NINT:
{
/* read negative value as unsigned */
ecbor_error_t rc = ecbor_decode_uint (context, &item->value.uinteger,
@@ -266,8 +266,8 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
/*
* String types
*/
- case ECBOR_MT_BSTR:
- case ECBOR_MT_STR:
+ case ECBOR_TYPE_BSTR:
+ case ECBOR_TYPE_STR:
/* keep buffer pointer from current pointer */
item->value.string.str = context->in_position;
item->value.string.n_chunks = 0;
@@ -295,7 +295,7 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
while (true) {
/* read next chunk */
rc = ecbor_decode_next_internal (context, &chunk, true,
- item->major_type);
+ item->type);
if (rc != ECBOR_OK) {
if (rc == ECBOR_END_OF_INDEFINITE) {
/* stop code found, break from loop */
@@ -339,8 +339,8 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
/*
* Arrays and maps
*/
- case ECBOR_MT_ARRAY:
- case ECBOR_MT_MAP:
+ case ECBOR_TYPE_ARRAY:
+ case ECBOR_TYPE_MAP:
/* keep buffer pointer from current pointer */
item->value.items = context->in_position;
@@ -359,7 +359,7 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
while (true) {
/* read next chunk */
rc = ecbor_decode_next_internal (context, &child, false,
- ECBOR_MT_UNDEFINED);
+ ECBOR_TYPE_UNDEFINED);
if (rc != ECBOR_OK) {
if (rc == ECBOR_END_OF_INDEFINITE) {
/* stop code found, break from loop */
@@ -380,7 +380,7 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
item->length ++;
}
- if ((item->major_type == ECBOR_MT_MAP) && (item->length % 2 != 0)) {
+ if ((item->type == ECBOR_TYPE_MAP) && (item->length % 2 != 0)) {
/* incomplete key-value pair; we expect maps to have even number of
items */
return ECBOR_ERR_INVALID_KEY_VALUE_PAIR;
@@ -394,7 +394,7 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
return rc;
}
- if (item->major_type == ECBOR_MT_MAP) {
+ if (item->type == ECBOR_TYPE_MAP) {
/* we keep the total number of items in length, yet the map has the
number of key-value pairs encoded in the length */
item->length *= 2;
@@ -409,7 +409,7 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
for (child_no = 0; child_no < item->length; child_no ++) {
/* read next child */
rc = ecbor_decode_next_internal (context, &child, false,
- ECBOR_MT_UNDEFINED);
+ ECBOR_TYPE_UNDEFINED);
if (rc != ECBOR_OK) {
if (rc == ECBOR_END_OF_INDEFINITE) {
/* stop code found, but none is expected */
@@ -431,7 +431,7 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
}
break;
- case ECBOR_MT_TAG:
+ case ECBOR_TYPE_TAG:
{
ecbor_error_t rc;
@@ -451,7 +451,7 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
/* not in streamed mode; compute size so we can advance */
rc = ecbor_decode_next_internal (context, &child, false,
- ECBOR_MT_UNDEFINED);
+ ECBOR_TYPE_UNDEFINED);
if (rc != ECBOR_OK) {
return rc;
}
@@ -462,7 +462,10 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
}
break;
- case ECBOR_MT_SPECIAL:
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wswitch"
+ case ECBOR_TYPE_SPECIAL:
+#pragma GCC diagnostic pop
if (additional == ECBOR_ADDITIONAL_INDEFINITE) {
/* stop code */
item->size = 1;
@@ -475,11 +478,11 @@ ecbor_decode_next_internal (ecbor_decode_context_t *context,
return ECBOR_ERR_CURRENTLY_NOT_SUPPORTED;
} else if (additional == ECBOR_ADDITIONAL_4BYTE) {
/* floating point 32bit */
- item->major_type = ECBOR_MT_FP32;
+ item->type = ECBOR_TYPE_FP32;
return ecbor_decode_fp32 (context, &item->value.fp32, &item->size);
} else if (additional == ECBOR_ADDITIONAL_8BYTE) {
/* floating point 64bit */
- item->major_type = ECBOR_MT_FP64;
+ item->type = ECBOR_TYPE_FP64;
return ecbor_decode_fp64 (context, &item->value.fp64, &item->size);
} else {
/* currently unassigned according to RFC */
@@ -514,7 +517,7 @@ ecbor_decode (ecbor_decode_context_t *context, ecbor_item_t *item)
}
/* we just get the next item */
- return ecbor_decode_next_internal (context, item, false, ECBOR_MT_UNDEFINED);
+ return ecbor_decode_next_internal (context, item, false, ECBOR_TYPE_UNDEFINED);
}
extern ecbor_error_t
@@ -551,20 +554,20 @@ ecbor_decode_tree (ecbor_decode_context_t *context)
/* consume next item */
rc = ecbor_decode_next_internal (context, &curr_node->item, false,
- ECBOR_MT_UNDEFINED);
+ ECBOR_TYPE_UNDEFINED);
/* handle end of indefinite */
if (rc == ECBOR_END_OF_INDEFINITE) {
if ((!par_node)
- || (par_node->item.major_type != ECBOR_MT_MAP
- && par_node->item.major_type != ECBOR_MT_ARRAY)
+ || (par_node->item.type != ECBOR_TYPE_MAP
+ && par_node->item.type != ECBOR_TYPE_ARRAY)
|| (!par_node->item.is_indefinite)) {
/* we are not in an indefinite map or array */
rc = ECBOR_ERR_INVALID_STOP_CODE;
goto end;
}
- if (par_node && par_node->item.major_type == ECBOR_MT_MAP
+ if (par_node && par_node->item.type == ECBOR_TYPE_MAP
&& prev_node && (prev_node->index % 2 == 0)) {
/* map must have even number of children */
rc = ECBOR_ERR_INVALID_KEY_VALUE_PAIR;
@@ -608,9 +611,9 @@ ecbor_decode_tree (ecbor_decode_context_t *context)
}
/* handle new children */
- if (curr_node->item.major_type == ECBOR_MT_MAP
- || curr_node->item.major_type == ECBOR_MT_ARRAY
- || curr_node->item.major_type == ECBOR_MT_TAG) {
+ if (curr_node->item.type == ECBOR_TYPE_MAP
+ || curr_node->item.type == ECBOR_TYPE_ARRAY
+ || curr_node->item.type == ECBOR_TYPE_TAG) {
/* jump a level down */
par_node = curr_node;
prev_node = NULL;
@@ -619,9 +622,9 @@ ecbor_decode_tree (ecbor_decode_context_t *context)
/* handle end of definite maps and arrays, and tags */
while (par_node
- && (par_node->item.major_type == ECBOR_MT_MAP
- || par_node->item.major_type == ECBOR_MT_ARRAY
- || par_node->item.major_type == ECBOR_MT_TAG)
+ && (par_node->item.type == ECBOR_TYPE_MAP
+ || par_node->item.type == ECBOR_TYPE_ARRAY
+ || par_node->item.type == ECBOR_TYPE_TAG)
&& !par_node->item.is_indefinite
&& par_node->item.length == (curr_node->index + 1)) {
/* parent has filled all items, jump a level up */
diff --git a/src/ecbor_describe.c b/src/ecbor_describe.c
index 1cd9917..a301367 100644
--- a/src/ecbor_describe.c
+++ b/src/ecbor_describe.c
@@ -49,7 +49,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
{
static const unsigned int max_str_print_len = 64;
static const char *msg_too_large = "<too_large>";
- ecbor_major_type_t mt;
+ ecbor_type_t mt;
ecbor_error_t rc;
unsigned int i;
@@ -58,7 +58,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
mt = ecbor_get_type (item);
switch (mt) {
- case ECBOR_MT_NINT:
+ case ECBOR_TYPE_NINT:
{
int64_t val;
@@ -71,7 +71,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
}
break;
- case ECBOR_MT_UINT:
+ case ECBOR_TYPE_UINT:
{
uint64_t val;
@@ -84,7 +84,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
}
break;
- case ECBOR_MT_STR:
+ case ECBOR_TYPE_STR:
{
uint64_t len;
char *val;
@@ -106,7 +106,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
}
break;
- case ECBOR_MT_BSTR:
+ case ECBOR_TYPE_BSTR:
{
uint64_t len;
char *val;
@@ -135,7 +135,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
}
break;
- case ECBOR_MT_ARRAY:
+ case ECBOR_TYPE_ARRAY:
{
uint64_t len, i;
@@ -162,7 +162,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
}
break;
- case ECBOR_MT_MAP:
+ case ECBOR_TYPE_MAP:
{
uint64_t len, i;
char kv_msg[100] = { 0 };
@@ -197,7 +197,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
}
break;
- case ECBOR_MT_TAG:
+ case ECBOR_TYPE_TAG:
{
int64_t val;
ecbor_item_t child;
@@ -221,7 +221,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
}
break;
- case ECBOR_MT_FP32:
+ case ECBOR_TYPE_FP32:
{
float val;
@@ -234,7 +234,7 @@ print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
}
break;
- case ECBOR_MT_FP64:
+ case ECBOR_TYPE_FP64:
{
double val;
diff --git a/src/ecbor_internal.h b/src/ecbor_internal.h
index e5e4179..7b29546 100644
--- a/src/ecbor_internal.h
+++ b/src/ecbor_internal.h
@@ -15,6 +15,11 @@
#define false 0
#define true 1
+/* CBOR major type 7 must not be exposed to user, but translated to other types */
+enum {
+ ECBOR_TYPE_SPECIAL = 7
+};
+
/* Additional value meanings */
enum {
ECBOR_ADDITIONAL_1BYTE = 24,