Skip to content

Commit

Permalink
Handle CTRLs using a simple uint
Browse files Browse the repository at this point in the history
  • Loading branch information
PJK committed Apr 27, 2015
1 parent e480432 commit 2e3f818
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
18 changes: 9 additions & 9 deletions src/cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ cbor_item_t *cbor_new_ctrl()
.type = CBOR_TYPE_FLOAT_CTRL,
.data = NULL,
.refcount = 1,
.metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_0, .type = 0}}
.metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_0, .ctrl = CBOR_CTRL_NONE}}
};
return item;
}
Expand Down Expand Up @@ -1423,17 +1423,17 @@ inline bool cbor_is_uint(const cbor_item_t *item)
inline bool cbor_is_bool(const cbor_item_t *item)
{
return cbor_isa_float_ctrl(item) &&
(cbor_ctrl_code(item) == CBOR_CTRL_FALSE || cbor_ctrl_code(item) == CBOR_CTRL_TRUE);
(cbor_ctrl_value(item) == CBOR_CTRL_FALSE || cbor_ctrl_value(item) == CBOR_CTRL_TRUE);
}

inline bool cbor_is_null(const cbor_item_t *item)
{
return cbor_isa_float_ctrl(item) && cbor_ctrl_code(item) == CBOR_CTRL_NULL;
return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_NULL;
}

inline bool cbor_is_undef(const cbor_item_t *item)
{
return cbor_isa_float_ctrl(item) && cbor_ctrl_code(item) == CBOR_CTRL_UNDEF;
return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_UNDEF;
}

bool cbor_is_float(const cbor_item_t *item)
Expand Down Expand Up @@ -1494,11 +1494,11 @@ cbor_float_width cbor_float_get_width(const cbor_item_t *item)
return item->metadata.float_ctrl_metadata.width;
}

cbor_ctrl cbor_ctrl_code(const cbor_item_t *item)
uint8_t cbor_ctrl_value(const cbor_item_t *item)
{
assert(cbor_isa_float_ctrl(item));
assert(cbor_float_get_width(item) == CBOR_FLOAT_0);
return item->metadata.float_ctrl_metadata.type;
return item->metadata.float_ctrl_metadata.ctrl;
}

bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item)
Expand Down Expand Up @@ -1550,15 +1550,15 @@ void cbor_set_float8(cbor_item_t *item, double value)
*((double *) item->data) = value;
}

void cbor_set_ctrl(cbor_item_t *item, cbor_ctrl value)
void cbor_set_ctrl(cbor_item_t *item, uint8_t value)
{
assert(cbor_isa_float_ctrl(item));
assert(cbor_float_get_width(item) == CBOR_FLOAT_0);
item->metadata.float_ctrl_metadata.type = value;
item->metadata.float_ctrl_metadata.ctrl = value;
}

bool cbor_ctrl_bool(const cbor_item_t *item)
{
assert(cbor_is_bool(item));
return item->metadata.float_ctrl_metadata.type == CBOR_CTRL_TRUE;
return item->metadata.float_ctrl_metadata.ctrl == CBOR_CTRL_TRUE;
}
20 changes: 10 additions & 10 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ typedef enum {
CBOR_FLOAT_64
} cbor_float_width;

typedef enum {
CBOR_CTRL_FALSE,
CBOR_CTRL_TRUE,
CBOR_CTRL_NULL,
CBOR_CTRL_UNDEF
} cbor_ctrl;

typedef enum {
_CBOR_METADATA_DEFINITE,
_CBOR_METADATA_INDEFINITE
} _cbor_dst_metadata;

typedef enum {
CBOR_CTRL_NONE = 0,
CBOR_CTRL_FALSE = 20,
CBOR_CTRL_TRUE = 21,
CBOR_CTRL_NULL = 22,
CBOR_CTRL_UNDEF = 23
} _cbor_ctrl;

struct _cbor_int_metadata {
cbor_int_width width;
Expand Down Expand Up @@ -94,7 +94,7 @@ struct _cbor_tag_metadata {

struct _cbor_float_ctrl_metadata {
cbor_float_width width;
cbor_ctrl type;
uint8_t ctrl;
};

/* Raw memory casts */
Expand Down Expand Up @@ -504,12 +504,12 @@ cbor_item_t * cbor_new_null();
cbor_item_t * cbor_new_undef();
cbor_item_t * cbor_new_bool(bool value);

void cbor_set_ctrl(cbor_item_t * item, cbor_ctrl value);
void cbor_set_ctrl(cbor_item_t * item, uint8_t value);
void cbor_set_float2(cbor_item_t * item, float value);
void cbor_set_float4(cbor_item_t * item, float value);
void cbor_set_float8(cbor_item_t * item, double value);

cbor_ctrl cbor_ctrl_code(const cbor_item_t * item);
uint8_t cbor_ctrl_value(const cbor_item_t * item);
bool cbor_ctrl_bool(const cbor_item_t * item);

#ifdef DEBUG
Expand Down
11 changes: 11 additions & 0 deletions test/cbor_serialize_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ static void test_serialize_double(void **state)
}


static void test_serialize_ctrl(void **state)
{
cbor_item_t *item = cbor_new_float8();
cbor_set_float8(item, -4.1);

assert_int_equal(9, cbor_serialize(item, buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]) {0xFB, 0xC0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}), 9);
cbor_decref(&item);
}


int main(void)
{
const UnitTest tests[] = {
Expand Down

0 comments on commit 2e3f818

Please sign in to comment.