Skip to content

Commit 0724f34

Browse files
authored
Change reader and parser initializer prototype (#8)
1 parent ba450e2 commit 0724f34

File tree

11 files changed

+159
-202
lines changed

11 files changed

+159
-202
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
## v0.1.0 - August 24, 2022
1+
## v0.2.0 - August 24, 2022
2+
- **Breaking Change**: Change the reader and the parser initializer prototypes
3+
- `cbor_reader_init()` and `cbor_parse()`
4+
- Now a reader can be reusable to different messages
25

6+
## v0.1.0 - August 24, 2022
37
The initial release.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ cbor_reader_t reader;
6161
cbor_item_t items[MAX_ITEMS];
6262
size_t n;
6363

64-
cbor_reader_init(&reader, cbor_message, sizeof(cbor_message));
65-
cbor_parse(&reader, items, MAX_ITEMS, &n);
64+
cbor_reader_init(&reader, items, sizeof(items) / sizeof(*items));
65+
cbor_parse(&reader, cbor_message, cbor_message_len, &n);
6666

6767
for (i = 0; i < n; i++) {
6868
printf("item: %s, size: %zu\n",

examples/complex.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ void complex_example(void const *data, size_t datasize, void *udt)
162162
cbor_item_t items[32];
163163
size_t n;
164164

165-
cbor_reader_init(&reader, data, datasize);
166-
cbor_error_t err = cbor_parse(&reader, items, sizeof(items) / sizeof(items[0]), &n);
165+
cbor_reader_init(&reader, items, sizeof(items) / sizeof(*items));
166+
cbor_error_t err = cbor_parse(&reader, data, datasize, &n);
167167
if (err == CBOR_SUCCESS || err == CBOR_BREAK) {
168168
cbor_iterate(&reader, items, n, 0, convert, udt);
169169
}

examples/example.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ struct udt {
2828
} data;
2929
};
3030

31-
typedef void (*example_writer)(void const *data, size_t datasize);
31+
typedef void (*example_printer_t)(void const *data, size_t datasize);
3232

33-
void simple_example(example_writer print);
33+
void simple_example(example_printer_t print);
3434
void complex_example(void const *data, size_t datasize, void *udt);
3535

3636
#if defined(__cplusplus)

examples/simple.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ union cbor_value {
2020
uint8_t str_copy[16];
2121
};
2222

23-
static void print_cbor(cbor_reader_t const *reader, cbor_item_t const *item, example_writer print)
23+
static void print_cbor(cbor_reader_t const *reader, cbor_item_t const *item, example_printer_t print)
2424
{
2525
union cbor_value val;
2626

@@ -55,21 +55,21 @@ static size_t encode_simple_data(void *buf, size_t bufsize)
5555
return cbor_writer_len(&writer);
5656
}
5757

58-
static void decode_simple_data(void const *data, size_t datasize, example_writer print)
58+
static void decode_simple_data(void const *data, size_t datasize, example_printer_t print)
5959
{
6060
cbor_reader_t reader;
6161
cbor_item_t items[16];
6262
size_t n;
6363

64-
cbor_reader_init(&reader, data, datasize);
65-
cbor_parse(&reader, items, sizeof(items) / sizeof(items[0]), &n);
64+
cbor_reader_init(&reader, items, sizeof(items) / sizeof(items[0]));
65+
cbor_parse(&reader, data, datasize, &n);
6666

6767
for (size_t i = 0; i < n; i++) {
6868
print_cbor(&reader, &items[i], print);
6969
}
7070
}
7171

72-
void simple_example(example_writer print)
72+
void simple_example(example_printer_t print)
7373
{
7474
uint8_t buf[32];
7575
size_t len = encode_simple_data(buf, sizeof(buf));

include/cbor/cbor.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ typedef struct {
5151
uint8_t const *msg;
5252
size_t msgsize;
5353
size_t msgidx;
54+
55+
cbor_item_t *items;
56+
size_t itemidx;
57+
size_t maxitems;
5458
} cbor_reader_t;
5559

5660
typedef struct {
@@ -59,7 +63,14 @@ typedef struct {
5963
size_t bufidx;
6064
} cbor_writer_t;
6165

62-
void cbor_reader_init(cbor_reader_t *reader, void const *msg, size_t msgsize);
66+
/**
67+
* Initialize the reader for CBOR encoded messages.
68+
*
69+
* @param[in,out] reader reader context for the actual encoded message
70+
* @param[out] items a pointer to item buffers
71+
* @param[in] maxitems the maximum number of items to be stored in @p items
72+
*/
73+
void cbor_reader_init(cbor_reader_t *reader, cbor_item_t *items, size_t maxitems);
6374
void cbor_writer_init(cbor_writer_t *writer, void *buf, size_t bufsize);
6475
size_t cbor_writer_len(cbor_writer_t const *writer);
6576
uint8_t const *cbor_writer_get_encoded(cbor_writer_t const *writer);

include/cbor/parser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ extern "C" {
1111
* Parse the encoded CBOR messages into items.
1212
*
1313
* @param[in,out] reader reader context for the actual encoded message
14-
* @param[out] items a pointer to item buffers
15-
* @param[in] maxitems the maximum number of items to be stored in @p items
14+
* @param[in] msg CBOR encoded message
15+
* @param[in] msgsize the @p msg size in bytes
1616
* @param[out] nitems_parsed the number of items parsed gets stored if not null
1717
*
1818
* @return a code of @ref cbor_error_t
1919
*/
20-
cbor_error_t cbor_parse(cbor_reader_t *reader,
21-
cbor_item_t *items, size_t maxitems, size_t *nitems_parsed);
20+
cbor_error_t cbor_parse(cbor_reader_t *reader, void const *msg, size_t msgsize,
21+
size_t *nitems_parsed);
2222

2323
#if defined(__cplusplus)
2424
}

src/common.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,19 @@ size_t cbor_get_item_size(cbor_item_t const *item)
5959
return item->size;
6060
}
6161

62-
void cbor_reader_init(cbor_reader_t *reader, void const *msg, size_t msgsize)
62+
void cbor_reader_init(cbor_reader_t *reader, cbor_item_t *items, size_t maxitems)
6363
{
64-
assert(parser != NULL);
65-
assert(msg != NULL);
64+
assert(reader != NULL);
6665

67-
reader->msg = (uint8_t const *)msg;
68-
reader->msgsize = msgsize;
69-
reader->msgidx = 0;
66+
reader->items = items;
67+
reader->maxitems = maxitems;
68+
reader->itemidx = 0;
7069
}
7170

7271
void cbor_writer_init(cbor_writer_t *writer, void *buf, size_t bufsize)
7372
{
74-
assert(parser != NULL);
75-
assert(msg != NULL);
73+
assert(writer != NULL);
74+
assert(buf != NULL);
7675

7776
writer->buf = (uint8_t *)buf;
7877
writer->bufsize = bufsize;

src/parser.c

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
struct parser_context {
1212
cbor_reader_t *reader;
1313

14-
cbor_item_t *items;
15-
size_t itemidx;
16-
size_t maxitems;
17-
1814
uint8_t major_type;
1915
uint8_t additional_info;
2016
uint8_t following_bytes;
@@ -93,8 +89,9 @@ static cbor_error_t parse(struct parser_context *ctx, size_t maxitems)
9389
return CBOR_EXCESSIVE;
9490
}
9591

96-
for (size_t i = 0; i < maxitems && ctx->itemidx < ctx->maxitems
97-
&& ctx->reader->msgidx < ctx->reader->msgsize; i++) {
92+
for (size_t i = 0; i < maxitems &&
93+
ctx->reader->itemidx < ctx->reader->maxitems &&
94+
ctx->reader->msgidx < ctx->reader->msgsize; i++) {
9895
uint8_t val = ctx->reader->msg[ctx->reader->msgidx];
9996
ctx->major_type = get_cbor_major_type(val);
10097
ctx->additional_info = get_cbor_additional_info(val);
@@ -131,44 +128,44 @@ static cbor_error_t do_integer(struct parser_context *ctx)
131128
return CBOR_ILLEGAL;
132129
}
133130

134-
cbor_item_t *item = &ctx->items[ctx->itemidx];
131+
cbor_item_t *item = &ctx->reader->items[ctx->reader->itemidx];
135132
item->type = CBOR_ITEM_INTEGER;
136133
item->size = (size_t)ctx->following_bytes;
137134
item->offset = ctx->reader->msgidx;
138135

139136
ctx->reader->msgidx += (size_t)(ctx->following_bytes + 1);
140-
ctx->itemidx++;
137+
ctx->reader->itemidx++;
141138

142139
return CBOR_SUCCESS;
143140
}
144141

145142
static cbor_error_t do_string(struct parser_context *ctx)
146143
{
147-
cbor_item_t *item = &ctx->items[ctx->itemidx];
144+
cbor_item_t *item = &ctx->reader->items[ctx->reader->itemidx];
148145
size_t len = go_get_item_length(ctx);
149146

150147
item->type = CBOR_ITEM_STRING;
151148
item->size = len;
152149
item->offset = ctx->reader->msgidx;
153150

154151
if (len == (size_t)CBOR_INDEFINITE_VALUE) {
155-
ctx->itemidx++;
156-
return parse(ctx, ctx->maxitems - ctx->itemidx);
152+
ctx->reader->itemidx++;
153+
return parse(ctx, ctx->reader->maxitems - ctx->reader->itemidx);
157154
}
158155
if (len > ctx->reader->msgsize - ctx->reader->msgidx) {
159156
return CBOR_ILLEGAL;
160157
}
161158

162159
ctx->reader->msgidx += len;
163-
ctx->itemidx++;
160+
ctx->reader->itemidx++;
164161

165162
return CBOR_SUCCESS;
166163
}
167164

168165
static cbor_error_t do_recursive(struct parser_context *ctx)
169166
{
170-
size_t current_item_index = ctx->itemidx;
171-
cbor_item_t *item = &ctx->items[current_item_index];
167+
size_t current_item_index = ctx->reader->itemidx;
168+
cbor_item_t *item = &ctx->reader->items[current_item_index];
172169
size_t len = go_get_item_length(ctx);
173170

174171
item->type = (cbor_item_data_t)(ctx->major_type - 1);
@@ -179,9 +176,9 @@ static cbor_error_t do_recursive(struct parser_context *ctx)
179176
return CBOR_ILLEGAL;
180177
}
181178

182-
ctx->itemidx++;
179+
ctx->reader->itemidx++;
183180

184-
return parse(ctx, MIN(len, ctx->maxitems - ctx->itemidx));
181+
return parse(ctx, MIN(len, ctx->reader->maxitems - ctx->reader->itemidx));
185182
}
186183

187184
/* TODO: Implement tag */
@@ -193,7 +190,7 @@ static cbor_error_t do_tag(struct parser_context *ctx)
193190

194191
static cbor_error_t do_float_and_other(struct parser_context *ctx)
195192
{
196-
cbor_item_t *item = &ctx->items[ctx->itemidx];
193+
cbor_item_t *item = &ctx->reader->items[ctx->reader->itemidx];
197194
cbor_error_t err = CBOR_SUCCESS;
198195

199196
item->type = CBOR_ITEM_FLOAT;
@@ -202,7 +199,7 @@ static cbor_error_t do_float_and_other(struct parser_context *ctx)
202199

203200
if (ctx->following_bytes == (uint8_t)CBOR_INDEFINITE_VALUE) {
204201
ctx->reader->msgidx++;
205-
ctx->itemidx++;
202+
ctx->reader->itemidx++;
206203
return CBOR_BREAK;
207204
} else if (!has_valid_following_bytes(ctx, &err)) {
208205
return err;
@@ -211,29 +208,33 @@ static cbor_error_t do_float_and_other(struct parser_context *ctx)
211208
}
212209

213210
ctx->reader->msgidx += item->size + 1;
214-
ctx->itemidx++;
211+
ctx->reader->itemidx++;
215212

216213
return err;
217214
}
218215

219-
cbor_error_t cbor_parse(cbor_reader_t *reader,
220-
cbor_item_t *items, size_t maxitems, size_t *items_parsed)
216+
cbor_error_t cbor_parse(cbor_reader_t *reader, void const *msg, size_t msgsize,
217+
size_t *nitems_parsed)
221218
{
219+
assert(reader->items != NULL);
220+
reader->itemidx = 0;
221+
222+
reader->msg = (uint8_t const *)msg;
223+
reader->msgsize = msgsize;
224+
reader->msgidx = 0;
225+
222226
struct parser_context ctx = {
223227
.reader = reader,
224-
.items = items,
225-
.itemidx = 0,
226-
.maxitems = maxitems,
227228
};
228229

229-
cbor_error_t err = parse(&ctx, maxitems);
230+
cbor_error_t err = parse(&ctx, reader->maxitems);
230231

231232
if (err == CBOR_SUCCESS && reader->msgidx < reader->msgsize) {
232233
err = CBOR_OVERRUN;
233234
}
234235

235-
if (items_parsed != NULL) {
236-
*items_parsed = ctx.itemidx;
236+
if (nitems_parsed != NULL) {
237+
*nitems_parsed = reader->itemidx;
237238
}
238239

239240
return err;

0 commit comments

Comments
 (0)