Skip to content

Commit

Permalink
Better simple values encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
PJK committed Apr 27, 2015
1 parent 2e3f818 commit 51a0e6b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ struct cbor_decoder_result cbor_stream_decode(cbor_data source, size_t source_si
}
case 0x78:
/* One byte length string */
// TODO template this?
{
if (_cbor_claim_bytes(1, source_size, &result)) {
size_t length = (size_t) _cbor_load_uint8(source + 1);
Expand Down Expand Up @@ -834,7 +833,7 @@ cbor_int_width cbor_int_get_width(const cbor_item_t *item)

uint8_t cbor_get_uint8(const cbor_item_t *item)
{
return *(uint8_t *) item->data;
return *item->data;
}

uint16_t cbor_get_uint16(const cbor_item_t *item)
Expand All @@ -856,7 +855,7 @@ void cbor_set_uint8(cbor_item_t *item, uint8_t value)
{
assert(cbor_is_int(item));
assert(cbor_int_get_width(item) == CBOR_INT_8);
*(uint8_t *) item->data = value;
*item->data = value;
}

void cbor_set_uint16(cbor_item_t *item, uint16_t value)
Expand Down
3 changes: 1 addition & 2 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ size_t cbor_encode_half(float, unsigned char *, size_t);
size_t cbor_encode_single(float, unsigned char *, size_t);
size_t cbor_encode_double(double, unsigned char *, size_t);
size_t cbor_encode_break(unsigned char *, size_t);
size_t cbor_encode_float(unsigned char *, size_t);
size_t cbor_encode_ctrl(unsigned char *, size_t);
size_t cbor_encode_ctrl(uint8_t, unsigned char *, size_t);


/*
Expand Down
5 changes: 5 additions & 0 deletions src/cbor_encoders.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,8 @@ size_t cbor_encode_break(unsigned char *buffer, size_t buffer_size)
{
return _cbor_encode_byte(0xFF, buffer, buffer_size);
}

size_t cbor_encode_ctrl(uint8_t value, unsigned char * buffer, size_t buffer_size)
{
return _cbor_encode_uint8(value, buffer, buffer_size, 0xE0);
}
5 changes: 2 additions & 3 deletions src/cbor_serializers.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,9 @@ size_t cbor_serialize_float_ctrl(const cbor_item_t *item, unsigned char *buffer,
{
assert(cbor_isa_float_ctrl(item));
switch (cbor_float_get_width(item)) {
case CBOR_FLOAT_0: {
case CBOR_FLOAT_0:
/* CTRL - special treatment */
break;
}
return cbor_encode_ctrl(cbor_ctrl_value(item), buffer, buffer_size);
case CBOR_FLOAT_16:
return cbor_encode_half(cbor_float_get_float2(item), buffer, buffer_size);
case CBOR_FLOAT_32:
Expand Down
21 changes: 16 additions & 5 deletions test/cbor_serialize_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,20 @@ 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);
cbor_item_t *item = cbor_new_undef();

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);
assert_int_equal(1, cbor_serialize(item, buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]) {0xF7}), 1);
cbor_decref(&item);
}

static void test_serialize_long_ctrl(void **state)
{
cbor_item_t *item = cbor_new_ctrl();
cbor_set_ctrl(item, 254);

assert_int_equal(2, cbor_serialize(item, buffer, 512));
assert_memory_equal(buffer, ((unsigned char[]) {0xF8, 0xFE}), 2);
cbor_decref(&item);
}

Expand All @@ -288,7 +297,9 @@ int main(void)
unit_test(test_serialize_tags),
unit_test(test_serialize_half),
unit_test(test_serialize_single),
unit_test(test_serialize_double)
unit_test(test_serialize_double),
unit_test(test_serialize_ctrl),
unit_test(test_serialize_long_ctrl)
};
return run_tests(tests);
}

0 comments on commit 51a0e6b

Please sign in to comment.