Skip to content

Commit 15e4d71

Browse files
authored
Remove the length parameter of cbor_encode_text_string() (#5)
1 parent 5ebeee2 commit 15e4d71

File tree

6 files changed

+50
-36
lines changed

6 files changed

+50
-36
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ cbor_writer_t writer;
9494
9595
cbor_writer_init(&reader, buf, sizeof(buf));
9696
97-
cbor_encode_map(&writer, 2);
97+
cbor_encode_map_indefinite(&writer);
9898
/* 1st */
99-
cbor_encode_text_string(&writer, "key", 3);
100-
cbor_encode_text_string(&writer, "value", 5);
99+
cbor_encode_text_string(&writer, "key");
100+
cbor_encode_text_string(&writer, "value");
101101
/* 2nd */
102-
cbor_encode_text_string(&writer, "age", 1);
102+
cbor_encode_text_string(&writer, "age");
103103
cbor_encode_negative_integer(&writer, -1);
104+
cbor_encode_break(&writer);
104105
```
105106

106107
## Limitation

include/cbor/cbor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typedef struct {
6262
void cbor_reader_init(cbor_reader_t *reader, void const *msg, size_t msgsize);
6363
void cbor_writer_init(cbor_writer_t *writer, void *buf, size_t bufsize);
6464
size_t cbor_writer_len(cbor_writer_t const *writer);
65+
uint8_t const *cbor_writer_get_encoded(cbor_writer_t const *writer);
6566

6667
cbor_item_data_t cbor_get_item_type(cbor_item_t const *item);
6768
size_t cbor_get_item_size(cbor_item_t const *item);

include/cbor/encoder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ cbor_error_t cbor_encode_byte_string(cbor_writer_t *writer,
1515
uint8_t const *data, size_t datasize);
1616
cbor_error_t cbor_encode_byte_string_indefinite(cbor_writer_t *writer);
1717

18-
cbor_error_t cbor_encode_text_string(cbor_writer_t *writer,
19-
char const *text, size_t textsize);
18+
cbor_error_t cbor_encode_text_string(cbor_writer_t *writer, char const *text);
2019
cbor_error_t cbor_encode_text_string_indefinite(cbor_writer_t *writer);
2120

2221
cbor_error_t cbor_encode_array(cbor_writer_t *writer, size_t length);

src/common.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,8 @@ size_t cbor_writer_len(cbor_writer_t const *writer)
8383
{
8484
return writer->bufidx;
8585
}
86+
87+
uint8_t const *cbor_writer_get_encoded(cbor_writer_t const *writer)
88+
{
89+
return (uint8_t const *)writer->buf;
90+
}

src/encoder.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,6 @@ cbor_error_t cbor_encode_byte_string_indefinite(cbor_writer_t *writer)
8282
return encode_core(writer, 2, NULL, 0, true);
8383
}
8484

85-
cbor_error_t cbor_encode_text_string(cbor_writer_t *writer,
86-
char const *text, size_t textsize)
87-
{
88-
return encode_core(writer, 3,
89-
(uint8_t const *)text, textsize, false);
90-
}
91-
92-
cbor_error_t cbor_encode_text_string_indefinite(cbor_writer_t *writer)
93-
{
94-
return encode_core(writer, 3, NULL, 0, true);
95-
}
96-
9785
cbor_error_t cbor_encode_array(cbor_writer_t *writer, size_t length)
9886
{
9987
return encode_core(writer, 4, NULL, length, false);
@@ -175,3 +163,24 @@ cbor_error_t cbor_encode_double(cbor_writer_t *writer, double value)
175163

176164
return CBOR_SUCCESS;
177165
}
166+
167+
cbor_error_t cbor_encode_text_string_indefinite(cbor_writer_t *writer)
168+
{
169+
return encode_core(writer, 3, NULL, 0, true);
170+
}
171+
172+
#if !defined(_POSIX_C_SOURCE)
173+
#define _POSIX_C_SOURCE 200809L
174+
#endif
175+
#include <string.h>
176+
177+
cbor_error_t cbor_encode_text_string(cbor_writer_t *writer, char const *text)
178+
{
179+
size_t len = 0;
180+
181+
if (text != NULL) {
182+
len = (size_t)strnlen(text, writer->bufsize - writer->bufidx);
183+
}
184+
185+
return encode_core(writer, 3, (uint8_t const *)text, len, false);
186+
}

tests/src/encoder_test.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,9 @@ TEST(Encoder, WhenTwoByteLengthByteStringGiven) {
132132
}
133133

134134
TEST(Encoder, WhenIndefiniteTextStringGiven) {
135-
const char *fixed_text = "streaming";
136135
cbor_encode_text_string_indefinite(&writer);
137-
cbor_encode_text_string(&writer, fixed_text, 5);
138-
cbor_encode_text_string(&writer, &fixed_text[5], 4);
136+
cbor_encode_text_string(&writer, "strea");
137+
cbor_encode_text_string(&writer, "ming");
139138
LONGS_EQUAL(12, writer.bufidx);
140139
LONGS_EQUAL(0x7F, writer.buf[0]);
141140
LONGS_EQUAL(0x65, writer.buf[1]);
@@ -253,20 +252,20 @@ TEST(Encoder, WhenEncodedLengthMapGiven3) {
253252
0x44,0x61,0x65,0x61,0x45 };
254253
cbor_encode_map(&writer, 5);
255254
// 1st item
256-
cbor_encode_text_string(&writer, "a", 1);
257-
cbor_encode_text_string(&writer, "A", 1);
255+
cbor_encode_text_string(&writer, "a");
256+
cbor_encode_text_string(&writer, "A");
258257
// 2nd
259-
cbor_encode_text_string(&writer, "b", 1);
260-
cbor_encode_text_string(&writer, "B", 1);
258+
cbor_encode_text_string(&writer, "b");
259+
cbor_encode_text_string(&writer, "B");
261260
// 3
262-
cbor_encode_text_string(&writer, "c", 1);
263-
cbor_encode_text_string(&writer, "C", 1);
261+
cbor_encode_text_string(&writer, "c");
262+
cbor_encode_text_string(&writer, "C");
264263
// 4
265-
cbor_encode_text_string(&writer, "d", 1);
266-
cbor_encode_text_string(&writer, "D", 1);
264+
cbor_encode_text_string(&writer, "d");
265+
cbor_encode_text_string(&writer, "D");
267266
// 5
268-
cbor_encode_text_string(&writer, "e", 1);
269-
cbor_encode_text_string(&writer, "E", 1);
267+
cbor_encode_text_string(&writer, "e");
268+
cbor_encode_text_string(&writer, "E");
270269
LONGS_EQUAL(21, writer.bufidx);
271270
MEMCMP_EQUAL(expected, writer.buf, sizeof(expected));
272271
}
@@ -275,9 +274,9 @@ TEST(Encoder, WhenIndefiniteLengthMapGiven) {
275274
const uint8_t expected[] = { 0xbf,0x61,0x61,0x01,0x61,0x62,0x9f,0x02,
276275
0x03,0xff,0xff };
277276
cbor_encode_map_indefinite(&writer);
278-
cbor_encode_text_string(&writer, "a", 1);
277+
cbor_encode_text_string(&writer, "a");
279278
cbor_encode_unsigned_integer(&writer, 1);
280-
cbor_encode_text_string(&writer, "b", 1);
279+
cbor_encode_text_string(&writer, "b");
281280
cbor_encode_array_indefinite(&writer);
282281
cbor_encode_unsigned_integer(&writer, 2);
283282
cbor_encode_unsigned_integer(&writer, 3);
@@ -290,10 +289,10 @@ TEST(Encoder, WhenIndefiniteLengthMapInArrayGiven) {
290289
const uint8_t expected[] = { 0x82,0x61,0x61,0xbf,0x61,0x62,0x61,0x63,
291290
0xff };
292291
cbor_encode_array(&writer, 2);
293-
cbor_encode_text_string(&writer, "a", 1);
292+
cbor_encode_text_string(&writer, "a");
294293
cbor_encode_map_indefinite(&writer);
295-
cbor_encode_text_string(&writer, "b", 1);
296-
cbor_encode_text_string(&writer, "c", 1);
294+
cbor_encode_text_string(&writer, "b");
295+
cbor_encode_text_string(&writer, "c");
297296
cbor_encode_break(&writer);
298297
LONGS_EQUAL(9, writer.bufidx);
299298
MEMCMP_EQUAL(expected, writer.buf, sizeof(expected));

0 commit comments

Comments
 (0)