Skip to content

Commit ece8b46

Browse files
committed
Bring tinycbor up to date with mynewt tinycbor
- Bring tinycbor up to date with mynewt tinycbor - Changing implementation of cbor_encoder_get_extra_bytes_needed() and cbor_encoder_get_buffer_size() as part of cbor_buf_writer APIs - Move bytes_needed field from CborEncoder to the buf writer, this is needed by the tests mainly. - Fix cbor_buf_cmp to do memcmp and return complemented result - iterate_string_chunks(): fixing NULL compare at the end of string and moving it out of the iterate_string_chunks(). This is to avoid buffer specific parser calls in the function - cbor_value_get_next_byte() is removed in mynewt version of tinycbor, so, we track offsets of the buffer which can be used for comparison in the parser tests instead of calculating the offset - Move cbor_encoder_get_extra_bytes_needed() and cbor_encoder_get_buffer_size() to be part of cbor_buf_writer APIs - Add bytes_needed field to the buf writer - Adding encoder writer and parser reader as part of the encoder and parser structure. This is to make the encoder and parser use new function of encoder_writer and decoder_reader without breaking backwards compatibility. - Making the old API use flat buffers by default - Adding APIs for initializing encoder and parser with custom writer and reader - Make the default writer and reader conditional based on NO_DFLT_READER/WRITER define. This is because we want a default reader/writer to avoid API changes. - Move enums to cbor_defs.h - Use it->offset instead of it->ptr to track buffer offsets - Update resolve_indicator() static api paramaters to use cbor value and access offsets instead of taking pointers as input parameters - In validate_container() do a byte by byte comparison instead of memcmp since we no longer have access to teh buffer directly Also, use offets instead of pointers to validate sorted maps - Added a new dfine for conditionally compiling in float support (NO_FLOAT_SUPPORT). This is because we want the float support to be compiled in by default. - Use static_assert macro instead of Static_assert. Changed to avoid build failures. - Add api to get string chunk, this is a callback which can be used by buffer implementations to grab a string that is divided in chunks which spans across multiple chained buffers
1 parent 497066e commit ece8b46

26 files changed

+838
-218
lines changed

Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ RMDIR = rmdir
2020
SED = sed
2121

2222
# Our sources
23-
TINYCBOR_HEADERS = src/cbor.h src/cborjson.h
23+
TINYCBOR_HEADERS = \
24+
src/cbor.h \
25+
src/cborjson.h \
26+
src/cbor_enocoder_writer.h \
27+
src/cbor_decoder_reader.h
2428
TINYCBOR_SOURCES = \
2529
src/cborerrorstrings.c \
2630
src/cborencoder.c \
@@ -31,6 +35,8 @@ TINYCBOR_SOURCES = \
3135
src/cborpretty_stdio.c \
3236
src/cbortojson.c \
3337
src/cborvalidation.c \
38+
src/cbor_buf_reader.c \
39+
src/cbor_buf_writer.c
3440
#
3541
CBORDUMP_SOURCES = tools/cbordump/cbordump.c
3642

Makefile.nmake

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CFLAGS = -W3
22

3-
TINYCBOR_HEADERS = src\cbor.h src\cborjson.h
3+
TINYCBOR_HEADERS = src
44
TINYCBOR_SOURCES = \
55
src\cborerrorstrings.c \
66
src\cborencoder.c \
@@ -9,7 +9,9 @@ TINYCBOR_SOURCES = \
99
src\cborparser_dup_string.c \
1010
src\cborpretty.c \
1111
src\cborpretty_stdio.c \
12-
src\cborvalidation.c
12+
src\cborvalidation.c \
13+
src\cbor_buf_reader.c \
14+
src\cbor_buf_writer.c
1315
TINYCBOR_OBJS = \
1416
src\cborerrorstrings.obj \
1517
src\cborencoder.obj \
@@ -18,7 +20,9 @@ TINYCBOR_OBJS = \
1820
src\cborparser_dup_string.obj \
1921
src\cborpretty.obj \
2022
src\cborpretty_stdio.obj \
21-
src\cborvalidation.obj
23+
src\cborvalidation.obj \
24+
src\cbor_buf_writer.obj \
25+
src\cbor_buf_reader.obj
2226

2327
all: lib\tinycbor.lib
2428
check: tests\Makefile lib\tinycbor.lib

src/cbor.h

+23-25
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <string.h>
3535
#include <stdio.h>
3636

37+
#include "cbor_buf_writer.h"
38+
#include "cbor_buf_reader.h"
3739
#include "tinycbor-version.h"
3840

3941
#define TINYCBOR_VERSION ((TINYCBOR_VERSION_MAJOR << 16) | (TINYCBOR_VERSION_MINOR << 8) | TINYCBOR_VERSION_PATCH)
@@ -204,19 +206,23 @@ CBOR_API const char *cbor_error_string(CborError error);
204206
/* Encoder API */
205207
struct CborEncoder
206208
{
207-
union {
208-
uint8_t *ptr;
209-
ptrdiff_t bytes_needed;
210-
} data;
211-
const uint8_t *end;
209+
cbor_encoder_writer *writer;
210+
void *writer_arg;
211+
#ifndef NO_DFLT_WRITER
212+
struct cbor_buf_writer wr;
213+
#endif
212214
size_t remaining;
213215
int flags;
214216
};
217+
215218
typedef struct CborEncoder CborEncoder;
216219

217220
static const size_t CborIndefiniteLength = SIZE_MAX;
218221

222+
#ifndef NO_DFLT_WRITER
219223
CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
224+
#endif
225+
CBOR_API void cbor_encoder_cust_writer_init(CborEncoder *encoder, struct cbor_encoder_writer *w, int flags);
220226
CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value);
221227
CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value);
222228
CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value);
@@ -227,7 +233,8 @@ CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const c
227233
{ return cbor_encode_text_string(encoder, string, strlen(string)); }
228234
CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length);
229235
CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value);
230-
236+
CBOR_INLINE_API int cbor_encode_bytes_written(CborEncoder *encoder)
237+
{ return encoder->writer->bytes_written; }
231238
CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
232239
{ return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); }
233240
CBOR_INLINE_API CborError cbor_encode_null(CborEncoder *encoder)
@@ -247,21 +254,6 @@ CBOR_API CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *ma
247254
CBOR_API CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder);
248255
CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder);
249256

250-
CBOR_INLINE_API uint8_t *_cbor_encoder_get_buffer_pointer(const CborEncoder *encoder)
251-
{
252-
return encoder->data.ptr;
253-
}
254-
255-
CBOR_INLINE_API size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
256-
{
257-
return (size_t)(encoder->data.ptr - buffer);
258-
}
259-
260-
CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
261-
{
262-
return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
263-
}
264-
265257
/* Parser API */
266258

267259
enum CborParserIteratorFlags
@@ -275,30 +267,36 @@ enum CborParserIteratorFlags
275267

276268
struct CborParser
277269
{
278-
const uint8_t *end;
270+
#ifndef NO_DFLT_READER
271+
struct cbor_buf_reader br;
272+
#endif
273+
struct cbor_decoder_reader *d;
274+
int end;
279275
int flags;
280276
};
281277
typedef struct CborParser CborParser;
282278

283279
struct CborValue
284280
{
285281
const CborParser *parser;
286-
const uint8_t *ptr;
282+
int offset;
287283
uint32_t remaining;
284+
uint32_t remainingclen;
288285
uint16_t extra;
289286
uint8_t type;
290287
uint8_t flags;
291288
};
292289
typedef struct CborValue CborValue;
293290

291+
#ifndef NO_DFLT_READER
294292
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
293+
#endif
294+
CBOR_API CborError cbor_parser_cust_reader_init(struct cbor_decoder_reader *r, int flags, CborParser *parser, CborValue *it);
295295

296296
CBOR_API CborError cbor_value_validate_basic(const CborValue *it);
297297

298298
CBOR_INLINE_API bool cbor_value_at_end(const CborValue *it)
299299
{ return it->remaining == 0; }
300-
CBOR_INLINE_API const uint8_t *cbor_value_get_next_byte(const CborValue *it)
301-
{ return it->ptr; }
302300
CBOR_API CborError cbor_value_advance_fixed(CborValue *it);
303301
CBOR_API CborError cbor_value_advance(CborValue *it);
304302
CBOR_INLINE_API bool cbor_value_is_container(const CborValue *it)

src/cbor_buf_reader.c

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2016 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#include "cbor_buf_reader.h"
26+
#include "compilersupport_p.h"
27+
28+
/**
29+
* \addtogroup CborParsing
30+
* @{
31+
*/
32+
33+
/**
34+
* Gets 16 bit unsigned value from the passed in ptr location, it also
35+
* converts it to host byte order
36+
*/
37+
CBOR_INLINE_API uint16_t get16(const uint8_t *ptr)
38+
{
39+
uint16_t result;
40+
memcpy(&result, ptr, sizeof(result));
41+
return cbor_ntohs(result);
42+
}
43+
44+
/**
45+
* Gets 32 bit unsigned value from the passed in ptr location, it also
46+
* converts it to host byte order
47+
*/
48+
CBOR_INLINE_API uint32_t get32(const uint8_t *ptr)
49+
{
50+
uint32_t result;
51+
memcpy(&result, ptr, sizeof(result));
52+
return cbor_ntohl(result);
53+
}
54+
55+
/**
56+
* Gets 64 bit unsigned value from the passed in ptr location, it also
57+
* converts it to host byte order
58+
*/
59+
CBOR_INLINE_API uint64_t get64(const uint8_t *ptr)
60+
{
61+
uint64_t result;
62+
memcpy(&result, ptr, sizeof(result));
63+
return cbor_ntohll(result);
64+
}
65+
66+
/**
67+
* Gets a string chunk from the passed in ptr location
68+
*/
69+
CBOR_INLINE_API uintptr_t get_string_chunk(const uint8_t *ptr)
70+
{
71+
return (uintptr_t)ptr;
72+
}
73+
74+
/**
75+
* Gets 8 bit unsigned value using the buffer pointed to by the
76+
* decoder reader from passed in offset
77+
*/
78+
static uint8_t
79+
cbuf_buf_reader_get8(struct cbor_decoder_reader *d, int offset)
80+
{
81+
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
82+
return cb->buffer[offset];
83+
}
84+
85+
/**
86+
* Gets 16 bit unsigned value using the buffer pointed to by the
87+
* decoder reader from passed in offset
88+
*/
89+
static uint16_t
90+
cbuf_buf_reader_get16(struct cbor_decoder_reader *d, int offset)
91+
{
92+
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
93+
return get16(cb->buffer + offset);
94+
}
95+
96+
/**
97+
* Gets 32 bit unsigned value using the buffer pointed to by the
98+
* decoder reader from passed in offset
99+
*/
100+
static uint32_t
101+
cbuf_buf_reader_get32(struct cbor_decoder_reader *d, int offset)
102+
{
103+
uint32_t val;
104+
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
105+
val = get32(cb->buffer + offset);
106+
return val;
107+
}
108+
109+
/**
110+
* Gets 64 bit unsigned value using the buffer pointed to by the
111+
* decoder reader from passed in offset
112+
*/
113+
static uint64_t
114+
cbuf_buf_reader_get64(struct cbor_decoder_reader *d, int offset)
115+
{
116+
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
117+
return get64(cb->buffer + offset);
118+
}
119+
120+
static uintptr_t
121+
cbor_buf_reader_get_string_chunk(struct cbor_decoder_reader *d,
122+
int offset, size_t *len)
123+
{
124+
struct cbor_buf_reader *cb = (struct cbor_buf_reader *)d;
125+
126+
(void)*len;
127+
128+
return get_string_chunk(cb->buffer + offset);
129+
}
130+
131+
static uintptr_t
132+
cbor_buf_reader_cmp(struct cbor_decoder_reader *d, char *dst, int src_offset,
133+
size_t len)
134+
{
135+
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
136+
137+
return !memcmp(dst, cb->buffer + src_offset, len);
138+
}
139+
140+
static uintptr_t
141+
cbor_buf_reader_cpy(struct cbor_decoder_reader *d, char *dst, int src_offset,
142+
size_t len)
143+
{
144+
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
145+
return (uintptr_t) memcpy(dst, cb->buffer + src_offset, len);
146+
}
147+
148+
void
149+
cbor_buf_reader_init(struct cbor_buf_reader *cb, const uint8_t *buffer,
150+
size_t data)
151+
{
152+
cb->buffer = buffer;
153+
cb->r.get8 = &cbuf_buf_reader_get8;
154+
cb->r.get16 = &cbuf_buf_reader_get16;
155+
cb->r.get32 = &cbuf_buf_reader_get32;
156+
cb->r.get64 = &cbuf_buf_reader_get64;
157+
cb->r.cmp = &cbor_buf_reader_cmp;
158+
cb->r.cpy = &cbor_buf_reader_cpy;
159+
cb->r.get_string_chunk = &cbor_buf_reader_get_string_chunk;
160+
cb->r.message_size = data;
161+
}
162+
163+
/** @} */

src/cbor_buf_reader.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2016 Intel Corporation
4+
**
5+
** Permission is hereby granted, free of charge, to any person obtaining a copy
6+
** of this software and associated documentation files (the "Software"), to deal
7+
** in the Software without restriction, including without limitation the rights
8+
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
** copies of the Software, and to permit persons to whom the Software is
10+
** furnished to do so, subject to the following conditions:
11+
**
12+
** The above copyright notice and this permission notice shall be included in
13+
** all copies or substantial portions of the Software.
14+
**
15+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
** THE SOFTWARE.
22+
**
23+
****************************************************************************/
24+
25+
#ifndef CBOR_BUF_READER_H
26+
#define CBOR_BUF_READER_H
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
#include "cbor_decoder_reader.h"
33+
34+
struct cbor_buf_reader {
35+
struct cbor_decoder_reader r;
36+
const uint8_t *buffer;
37+
};
38+
39+
void cbor_buf_reader_init(struct cbor_buf_reader *cb, const uint8_t *buffer,
40+
size_t data);
41+
42+
#ifdef __cplusplus
43+
}
44+
#endif
45+
46+
#endif /* CBOR_BUF_READER_H */
47+

0 commit comments

Comments
 (0)