Skip to content

Commit 4c53ee1

Browse files
committed
Bring tinycbor up to date with mynewt tinycbor
- Adding include directory for header files - Changing Makefiles and .pri and .pro files for the new include directory and source files - Adding mynewt pkg.yml - Removing cborencoder_close_container_checked.c since cborencoder_close_container() checks the number of elements now. - Changing tools and tests to adhere to the latest api - Comment out some tests to finish builds - Re-adding some bug fixes including mynewt changes
1 parent aa692b7 commit 4c53ee1

34 files changed

+1011
-285
lines changed

Makefile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ libdir = $(exec_prefix)/lib
66
includedir = $(prefix)/include
77
pkgconfigdir = $(libdir)/pkgconfig
88

9-
CFLAGS = -Wall -Wextra
9+
CFLAGS = -ggdb -Wall -Wextra -DFLOAT_SUPPORT
1010
LDFLAGS_GCSECTIONS = -Wl,--gc-sections
1111
LDFLAGS = $(if $(gc_sections-pass),$(LDFLAGS_GCSECTIONS))
1212

@@ -20,16 +20,17 @@ RMDIR = rmdir
2020
SED = sed
2121

2222
# Our sources
23-
TINYCBOR_HEADERS = src/cbor.h src/cborjson.h
23+
TINYCBOR_HEADERS = include
2424
TINYCBOR_SOURCES = \
2525
src/cborerrorstrings.c \
2626
src/cborencoder.c \
27-
src/cborencoder_close_container_checked.c \
2827
src/cborparser.c \
2928
src/cborparser_dup_string.c \
3029
src/cborpretty.c \
3130
src/cbortojson.c \
3231
src/cborvalidation.c \
32+
src/cbor_buf_reader.c\
33+
src/cbor_buf_writer.c
3334
#
3435
CBORDUMP_SOURCES = tools/cbordump/cbordump.c
3536

@@ -43,7 +44,7 @@ INSTALL_TARGETS += $(libdir)/libtinycbor.so.0
4344
INSTALL_TARGETS += $(libdir)/libtinycbor.so.$(VERSION)
4445
endif
4546
INSTALL_TARGETS += $(pkgconfigdir)/tinycbor.pc
46-
INSTALL_TARGETS += $(TINYCBOR_HEADERS:src/%=$(includedir)/tinycbor/%)
47+
INSTALL_TARGETS += $(TINYCBOR_HEADERS:./%=$(includedir)/tinycbor/%)
4748

4849
# setup VPATH
4950
MAKEFILE := $(lastword $(MAKEFILE_LIST))
@@ -156,7 +157,7 @@ $(DESTDIR)$(bindir)/%: bin/%
156157
$(DESTDIR)$(pkgconfigdir)/%: %
157158
$(INSTALL) -d $(@D)
158159
$(INSTALL_DATA) $< $@
159-
$(DESTDIR)$(includedir)/tinycbor/%: src/%
160+
$(DESTDIR)$(includedir)/%: include/%
160161
$(INSTALL) -d $(@D)
161162
$(INSTALL_DATA) $< $@
162163

@@ -200,9 +201,9 @@ tag: distcheck
200201
.PHONY: docs dist distcheck release
201202
.SECONDARY:
202203

203-
cflags := $(CPPFLAGS) -I$(SRCDIR)src
204+
cflags := $(CPPFLAGS) -I$(SRCDIR)include
204205
cflags += -DTINYCBOR_VERSION_SUFFIX=\"$(DIRTYSRC)\"
205-
cflags += -std=c99 $(CFLAGS)
206+
cflags += -ggdb -std=c99 $(CFLAGS)
206207
%.o: %.c
207208
@test -d $(@D) || $(MKDIR) $(@D)
208209
$(CC) $(cflags) $($(basename $(notdir $@))_CCFLAGS) -c -o $@ $<

Makefile.nmake

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
CFLAGS = -W3
22

3-
TINYCBOR_HEADERS = src\cbor.h src\cborjson.h
3+
TINYCBOR_HEADERS = include
44
TINYCBOR_SOURCES = \
55
src\cborerrorstrings.c \
66
src\cborencoder.c \
7-
src\cborencoder_close_container_checked.c \
87
src\cborparser.c \
98
src\cborparser_dup_string.c \
109
src\cborpretty.c \
1110
src\cborvalidation.c
11+
src\cbor_buf_reader.c \
12+
src\cbor_buf_writer.c
1213
TINYCBOR_OBJS = \
1314
src\cborerrorstrings.obj \
1415
src\cborencoder.obj \
15-
src\cborencoder_close_container_checked.obj \
1616
src\cborparser.obj \
1717
src\cborparser_dup_string.obj \
1818
src\cborpretty.obj \
1919
src\cborvalidation.obj
20+
src\cbor_buf_writer.obj \
21+
src\cbor_buf_reader.obj
2022

2123
all: lib\tinycbor.lib
2224
check: tests\Makefile lib\tinycbor.lib

include/tinycbor/assert_p.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2015 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 <assert.h>
26+
#ifdef NDEBUG
27+
# undef assert
28+
# define assert(cond) do { if (!(cond)) unreachable(); } while (0)
29+
#endif

src/cbor.h renamed to include/tinycbor/cbor.h

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,31 @@ typedef enum CborError {
199199

200200
CBOR_API const char *cbor_error_string(CborError error);
201201

202+
struct cbor_encoder_writer;
203+
204+
typedef int (cbor_encoder_write)(struct cbor_encoder_writer *, const char *data, int len);
205+
206+
typedef struct cbor_encoder_writer {
207+
cbor_encoder_write *write;
208+
int bytes_written;
209+
} cbor_encoder_writer;
210+
211+
202212
/* Encoder API */
203213
struct CborEncoder
204214
{
205-
union {
206-
uint8_t *ptr;
207-
ptrdiff_t bytes_needed;
208-
} data;
209-
const uint8_t *end;
215+
cbor_encoder_writer *writer;
216+
void *writer_arg;
210217
size_t added;
218+
size_t container_size;
211219
int flags;
212220
};
213221
typedef struct CborEncoder CborEncoder;
214222

215223
static const size_t CborIndefiniteLength = SIZE_MAX;
216224

217-
CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
225+
226+
CBOR_API void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *pwriter, int flags);
218227
CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value);
219228
CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value);
220229
CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value);
@@ -225,7 +234,8 @@ CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const c
225234
{ return cbor_encode_text_string(encoder, string, strlen(string)); }
226235
CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length);
227236
CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value);
228-
237+
CBOR_INLINE_API int cbor_encode_bytes_written(CborEncoder *encoder)
238+
{ return encoder->writer->bytes_written; }
229239
CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
230240
{ return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); }
231241
CBOR_INLINE_API CborError cbor_encode_null(CborEncoder *encoder)
@@ -250,16 +260,6 @@ CBOR_INLINE_API uint8_t *_cbor_encoder_get_buffer_pointer(const CborEncoder *enc
250260
return encoder->data.ptr;
251261
}
252262

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

265265
enum CborParserIteratorFlags
@@ -271,32 +271,50 @@ enum CborParserIteratorFlags
271271
CborIteratorFlag_ContainerIsMap = 0x20
272272
};
273273

274+
struct cbor_decoder_reader;
275+
276+
typedef uint8_t (cbor_reader_get8)(struct cbor_decoder_reader *d, int offset);
277+
typedef uint16_t (cbor_reader_get16)(struct cbor_decoder_reader *d, int offset);
278+
typedef uint32_t (cbor_reader_get32)(struct cbor_decoder_reader *d, int offset);
279+
typedef uint64_t (cbor_reader_get64)(struct cbor_decoder_reader *d, int offset);
280+
typedef uintptr_t (cbor_memcmp)(struct cbor_decoder_reader *d, char *buf, int offset, size_t len);
281+
typedef uintptr_t (cbor_memcpy)(struct cbor_decoder_reader *d, char *buf, int offset, size_t len);
282+
283+
struct cbor_decoder_reader {
284+
cbor_reader_get8 *get8;
285+
cbor_reader_get16 *get16;
286+
cbor_reader_get32 *get32;
287+
cbor_reader_get64 *get64;
288+
cbor_memcmp *cmp;
289+
cbor_memcpy *cpy;
290+
size_t message_size;
291+
};
292+
274293
struct CborParser
275294
{
276-
const uint8_t *end;
295+
struct cbor_decoder_reader *d;
296+
int end;
277297
int flags;
278298
};
279299
typedef struct CborParser CborParser;
280300

281301
struct CborValue
282302
{
283303
const CborParser *parser;
284-
const uint8_t *ptr;
304+
int offset;
285305
uint32_t remaining;
286306
uint16_t extra;
287307
uint8_t type;
288308
uint8_t flags;
289309
};
290310
typedef struct CborValue CborValue;
291311

292-
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
312+
CBOR_API CborError cbor_parser_init(struct cbor_decoder_reader *d, int flags, CborParser *parser, CborValue *it);
293313

294314
CBOR_API CborError cbor_value_validate_basic(const CborValue *it);
295315

296316
CBOR_INLINE_API bool cbor_value_at_end(const CborValue *it)
297317
{ return it->remaining == 0; }
298-
CBOR_INLINE_API const uint8_t *cbor_value_get_next_byte(const CborValue *it)
299-
{ return it->ptr; }
300318
CBOR_API CborError cbor_value_advance_fixed(CborValue *it);
301319
CBOR_API CborError cbor_value_advance(CborValue *it);
302320
CBOR_INLINE_API bool cbor_value_is_container(const CborValue *it)

include/tinycbor/cbor_buf_reader.h

Lines changed: 47 additions & 0 deletions
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 <tinycbor/cbor.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+

include/tinycbor/cbor_buf_writer.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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_WRITER_H
26+
#define CBOR_BUF_WRITER_H
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
struct cbor_buf_writer {
33+
struct cbor_encoder_writer enc;
34+
uint8_t *ptr;
35+
const uint8_t *end;
36+
};
37+
38+
void cbor_buf_writer_init(struct cbor_buf_writer *cb, uint8_t *buffer,
39+
size_t data);
40+
size_t cbor_buf_writer_buffer_size(struct cbor_buf_writer *cb,
41+
const uint8_t *buffer);
42+
#ifdef __cplusplus
43+
}
44+
#endif
45+
46+
#endif /* CBOR_BUF_WRITER_H */

include/tinycbor/cbor_cnt_writer.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_CNT_WRITER_H
26+
#define CBOR_CNT_WRITER_H
27+
28+
#include "cbor.h"
29+
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
/* use this count writer if you want to try out a cbor encoding to see
36+
* how long it would be (before allocating memory). This replaced the
37+
* code in tinycbor.h that would try to do this once the encoding failed
38+
* in a buffer. Its much easier to understand this way (for me)
39+
*/
40+
41+
struct CborCntWriter {
42+
struct cbor_encoder_writer enc;
43+
};
44+
45+
static inline int
46+
cbor_cnt_writer(struct cbor_encoder_writer *arg, const char *data, int len) {
47+
struct CborCntWriter *cb = (struct CborCntWriter *) arg;
48+
cb->enc.bytes_written += len;
49+
return CborNoError;
50+
}
51+
52+
static inline void
53+
cbor_cnt_writer_init(struct CborCntWriter *cb) {
54+
cb->enc.bytes_written = 0;
55+
cb->enc.write = &cbor_cnt_writer;
56+
}
57+
58+
#ifdef __cplusplus
59+
}
60+
#endif
61+
62+
#endif /* CBOR_CNT_WRITER_H */
63+

0 commit comments

Comments
 (0)