Skip to content

Commit

Permalink
Revert cbor_ecode_text_string() to its original version (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
onkwon authored Sep 23, 2022
1 parent 6ba3a6f commit 340edcb
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 66 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.3.0 - September 23, 2022
- **Breaking Change**: Revert `cbor_encode_text_string()` to its original version
- Add `cbor_encode_null_terminated_string()`
- Fix to conform to C99 standard removing `strnlen()`
- Increase the maximum recursion level from 4 to 8

## v0.2.0 - August 24, 2022
- **Breaking Change**: Change the reader and the parser initializer prototypes
- `cbor_reader_init()` and `cbor_parse()`
Expand Down
20 changes: 10 additions & 10 deletions examples/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,25 @@ static void convert(cbor_reader_t const *reader, cbor_item_t const *item,
cbor_encode_simple(&writer, 1);
cbor_encode_byte_string(&writer, uuid, sizeof(uuid));
cbor_encode_map_indefinite(&writer);
cbor_encode_text_string(&writer, "t");
cbor_encode_null_terminated_text_string(&writer, "t");
cbor_encode_unsigned_integer(&writer, 1661225893);
cbor_encode_text_string(&writer, "data");
cbor_encode_null_terminated_text_string(&writer, "data");
cbor_encode_map(&writer, 2);
cbor_encode_text_string(&writer, "acc");
cbor_encode_null_terminated_text_string(&writer, "acc");
cbor_encode_map(&writer, 3);
cbor_encode_text_string(&writer, "x");
cbor_encode_null_terminated_text_string(&writer, "x");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "y");
cbor_encode_null_terminated_text_string(&writer, "y");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "z");
cbor_encode_null_terminated_text_string(&writer, "z");
cbor_encode_float(&writer, 9.81f);
cbor_encode_text_string(&writer, "gyro");
cbor_encode_null_terminated_text_string(&writer, "gyro");
cbor_encode_map(&writer, 3);
cbor_encode_text_string(&writer, "x");
cbor_encode_null_terminated_text_string(&writer, "x");
cbor_encode_float(&writer, 6.3f);
cbor_encode_text_string(&writer, "y");
cbor_encode_null_terminated_text_string(&writer, "y");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "z");
cbor_encode_null_terminated_text_string(&writer, "z");
cbor_encode_float(&writer, 0.f);
cbor_encode_break(&writer);
cbor_encode_break(&writer);
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static size_t encode_simple_data(void *buf, size_t bufsize)
cbor_writer_t writer;
cbor_writer_init(&writer, buf, bufsize);

cbor_encode_text_string(&writer, "Hello, World!");
cbor_encode_null_terminated_text_string(&writer, "Hello, World!");
cbor_encode_unsigned_integer(&writer, 1661225893);
cbor_encode_bool(&writer, true);

Expand Down
2 changes: 1 addition & 1 deletion include/cbor/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "C" {
#include <stdint.h>

#if !defined(CBOR_RECURSION_MAX_LEVEL)
#define CBOR_RECURSION_MAX_LEVEL 4
#define CBOR_RECURSION_MAX_LEVEL 8
#endif

#define CBOR_INDEFINITE_VALUE (-1)
Expand Down
5 changes: 4 additions & 1 deletion include/cbor/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ cbor_error_t cbor_encode_byte_string(cbor_writer_t *writer,
uint8_t const *data, size_t datasize);
cbor_error_t cbor_encode_byte_string_indefinite(cbor_writer_t *writer);

cbor_error_t cbor_encode_text_string(cbor_writer_t *writer, char const *text);
cbor_error_t cbor_encode_text_string(cbor_writer_t *writer,
char const *text, size_t textsize);
cbor_error_t cbor_encode_null_terminated_text_string(cbor_writer_t *writer,
char const *text);
cbor_error_t cbor_encode_text_string_indefinite(cbor_writer_t *writer);

cbor_error_t cbor_encode_array(cbor_writer_t *writer, size_t length);
Expand Down
41 changes: 25 additions & 16 deletions src/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
*/

#include "cbor/encoder.h"
#include <string.h>
#include "cbor/ieee754.h"

#define MAJOR_TYPE_BIT 5

#if !defined(MIN)
#define MIN(a, b) (((a) > (b))? (b) : (a))
#endif

static uint8_t get_additional_info(uint64_t value)
{
uint8_t additional_info = 0;
Expand Down Expand Up @@ -88,6 +93,26 @@ cbor_error_t cbor_encode_byte_string_indefinite(cbor_writer_t *writer)
return encode_core(writer, 2, NULL, 0, true);
}


cbor_error_t cbor_encode_text_string(cbor_writer_t *writer,
char const *text, size_t textsize)
{
return encode_core(writer, 3,
(uint8_t const *)text, textsize, false);
}

cbor_error_t cbor_encode_null_terminated_text_string(cbor_writer_t *writer,
char const *text)
{
size_t len = 0;

if (text != NULL) {
len = MIN(strlen(text), writer->bufsize - writer->bufidx);
}

return encode_core(writer, 3, (uint8_t const *)text, len, false);
}

cbor_error_t cbor_encode_array(cbor_writer_t *writer, size_t length)
{
return encode_core(writer, 4, NULL, length, false);
Expand Down Expand Up @@ -174,19 +199,3 @@ cbor_error_t cbor_encode_text_string_indefinite(cbor_writer_t *writer)
{
return encode_core(writer, 3, NULL, 0, true);
}

#if !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200809L
#endif
#include <string.h>

cbor_error_t cbor_encode_text_string(cbor_writer_t *writer, char const *text)
{
size_t len = 0;

if (text != NULL) {
len = (size_t)strnlen(text, writer->bufsize - writer->bufidx);
}

return encode_core(writer, 3, (uint8_t const *)text, len, false);
}
49 changes: 32 additions & 17 deletions tests/src/encoder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ TEST(Encoder, WhenTwoByteLengthByteStringGiven) {

TEST(Encoder, WhenIndefiniteTextStringGiven) {
cbor_encode_text_string_indefinite(&writer);
cbor_encode_text_string(&writer, "strea");
cbor_encode_text_string(&writer, "ming");
cbor_encode_text_string(&writer, "strea", 5);
cbor_encode_text_string(&writer, "ming", 4);
LONGS_EQUAL(12, writer.bufidx);
LONGS_EQUAL(0x7F, writer.buf[0]);
LONGS_EQUAL(0x65, writer.buf[1]);
Expand Down Expand Up @@ -258,20 +258,20 @@ TEST(Encoder, WhenEncodedLengthMapGiven3) {
0x44,0x61,0x65,0x61,0x45 };
cbor_encode_map(&writer, 5);
// 1st item
cbor_encode_text_string(&writer, "a");
cbor_encode_text_string(&writer, "A");
cbor_encode_text_string(&writer, "a", 1);
cbor_encode_text_string(&writer, "A", 1);
// 2nd
cbor_encode_text_string(&writer, "b");
cbor_encode_text_string(&writer, "B");
cbor_encode_text_string(&writer, "b", 1);
cbor_encode_text_string(&writer, "B", 1);
// 3
cbor_encode_text_string(&writer, "c");
cbor_encode_text_string(&writer, "C");
cbor_encode_text_string(&writer, "c", 1);
cbor_encode_text_string(&writer, "C", 1);
// 4
cbor_encode_text_string(&writer, "d");
cbor_encode_text_string(&writer, "D");
cbor_encode_text_string(&writer, "d", 1);
cbor_encode_text_string(&writer, "D", 1);
// 5
cbor_encode_text_string(&writer, "e");
cbor_encode_text_string(&writer, "E");
cbor_encode_text_string(&writer, "e", 1);
cbor_encode_text_string(&writer, "E", 1);
LONGS_EQUAL(21, writer.bufidx);
MEMCMP_EQUAL(expected, writer.buf, sizeof(expected));
}
Expand All @@ -280,9 +280,9 @@ TEST(Encoder, WhenIndefiniteLengthMapGiven) {
const uint8_t expected[] = { 0xbf,0x61,0x61,0x01,0x61,0x62,0x9f,0x02,
0x03,0xff,0xff };
cbor_encode_map_indefinite(&writer);
cbor_encode_text_string(&writer, "a");
cbor_encode_text_string(&writer, "a", 1);
cbor_encode_unsigned_integer(&writer, 1);
cbor_encode_text_string(&writer, "b");
cbor_encode_text_string(&writer, "b", 1);
cbor_encode_array_indefinite(&writer);
cbor_encode_unsigned_integer(&writer, 2);
cbor_encode_unsigned_integer(&writer, 3);
Expand All @@ -295,10 +295,10 @@ TEST(Encoder, WhenIndefiniteLengthMapInArrayGiven) {
const uint8_t expected[] = { 0x82,0x61,0x61,0xbf,0x61,0x62,0x61,0x63,
0xff };
cbor_encode_array(&writer, 2);
cbor_encode_text_string(&writer, "a");
cbor_encode_text_string(&writer, "a", 1);
cbor_encode_map_indefinite(&writer);
cbor_encode_text_string(&writer, "b");
cbor_encode_text_string(&writer, "c");
cbor_encode_text_string(&writer, "b", 1);
cbor_encode_text_string(&writer, "c", 1);
cbor_encode_break(&writer);
LONGS_EQUAL(9, writer.bufidx);
MEMCMP_EQUAL(expected, writer.buf, sizeof(expected));
Expand Down Expand Up @@ -462,3 +462,18 @@ TEST(Encoder, WhenDoublePrecisionFloatGiven3) {
LONGS_EQUAL(9, writer.bufidx);
MEMCMP_EQUAL(expected, writer.buf, sizeof(expected));
}

TEST(Encoder, WhenNullTerminatedTextStringGiven) {
char const *expected = "A null terminated string";
cbor_encode_text_string_indefinite(&writer);
cbor_encode_null_terminated_text_string(&writer, expected);
MEMCMP_EQUAL(expected, &writer.buf[3], sizeof(expected));
}

TEST(Encoder, When_null_terminated_text_string_WithNullParameterGiven) {
cbor_encode_text_string_indefinite(&writer);
cbor_encode_null_terminated_text_string(&writer, NULL);
LONGS_EQUAL(2, writer.bufidx);
LONGS_EQUAL(0x7F, writer.buf[0]);
LONGS_EQUAL(0x60, writer.buf[1]);
}
40 changes: 20 additions & 20 deletions tests/src/example_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ TEST_GROUP(Example) {

TEST(Example, ShouldConvertToUserDefinedDataType) {
cbor_encode_map_indefinite(&writer);
cbor_encode_text_string(&writer, "t");
cbor_encode_null_terminated_text_string(&writer, "t");
cbor_encode_unsigned_integer(&writer, 1661225893);
cbor_encode_text_string(&writer, "data");
cbor_encode_null_terminated_text_string(&writer, "data");
cbor_encode_map(&writer, 2);
cbor_encode_text_string(&writer, "acc");
cbor_encode_null_terminated_text_string(&writer, "acc");
cbor_encode_map(&writer, 3);
cbor_encode_text_string(&writer, "x");
cbor_encode_null_terminated_text_string(&writer, "x");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "y");
cbor_encode_null_terminated_text_string(&writer, "y");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "z");
cbor_encode_null_terminated_text_string(&writer, "z");
cbor_encode_float(&writer, 9.81f);
cbor_encode_text_string(&writer, "gyro");
cbor_encode_null_terminated_text_string(&writer, "gyro");
cbor_encode_map(&writer, 3);
cbor_encode_text_string(&writer, "x");
cbor_encode_null_terminated_text_string(&writer, "x");
cbor_encode_float(&writer, 6.3f);
cbor_encode_text_string(&writer, "y");
cbor_encode_null_terminated_text_string(&writer, "y");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "z");
cbor_encode_null_terminated_text_string(&writer, "z");
cbor_encode_float(&writer, 0.f);
cbor_encode_break(&writer);

Expand All @@ -94,25 +94,25 @@ TEST(Example, ShouldConvertToUserDefinedDataType2) {
cbor_encode_simple(&writer, 1);
cbor_encode_byte_string(&writer, uuid, sizeof(uuid));
cbor_encode_map_indefinite(&writer);
cbor_encode_text_string(&writer, "t");
cbor_encode_null_terminated_text_string(&writer, "t");
cbor_encode_unsigned_integer(&writer, 1661225893);
cbor_encode_text_string(&writer, "data");
cbor_encode_null_terminated_text_string(&writer, "data");
cbor_encode_map(&writer, 2);
cbor_encode_text_string(&writer, "acc");
cbor_encode_null_terminated_text_string(&writer, "acc");
cbor_encode_map(&writer, 3);
cbor_encode_text_string(&writer, "x");
cbor_encode_null_terminated_text_string(&writer, "x");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "y");
cbor_encode_null_terminated_text_string(&writer, "y");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "z");
cbor_encode_null_terminated_text_string(&writer, "z");
cbor_encode_float(&writer, 9.81f);
cbor_encode_text_string(&writer, "gyro");
cbor_encode_null_terminated_text_string(&writer, "gyro");
cbor_encode_map(&writer, 3);
cbor_encode_text_string(&writer, "x");
cbor_encode_null_terminated_text_string(&writer, "x");
cbor_encode_float(&writer, 6.3f);
cbor_encode_text_string(&writer, "y");
cbor_encode_null_terminated_text_string(&writer, "y");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "z");
cbor_encode_null_terminated_text_string(&writer, "z");
cbor_encode_float(&writer, 0.f);
cbor_encode_break(&writer);
cbor_encode_break(&writer);
Expand Down

0 comments on commit 340edcb

Please sign in to comment.