Skip to content

Commit 9650bde

Browse files
committed
Making API backwards compatible
- Making the decoder and parser APIs backwards compatible - 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 - cpp test now uses tinycbor lib - updating tools and tests to uset he new API
1 parent a4850cd commit 9650bde

File tree

15 files changed

+351
-116
lines changed

15 files changed

+351
-116
lines changed

Makefile

Lines changed: 2 additions & 2 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 -DFLOAT_SUPPORT
9+
CFLAGS = -ggdb -O0 -Wall -Wextra -DFLOAT_SUPPORT
1010
LDFLAGS_GCSECTIONS = -Wl,--gc-sections
1111
LDFLAGS = $(if $(gc_sections-pass),$(LDFLAGS_GCSECTIONS))
1212

@@ -203,7 +203,7 @@ tag: distcheck
203203

204204
cflags := $(CPPFLAGS) -I$(SRCDIR)include
205205
cflags += -DTINYCBOR_VERSION_SUFFIX=\"$(DIRTYSRC)\"
206-
cflags += -std=c99 $(CFLAGS)
206+
cflags += -ggdb -std=c99 $(CFLAGS)
207207
%.o: %.c
208208
@test -d $(@D) || $(MKDIR) $(@D)
209209
$(CC) $(cflags) $($(basename $(notdir $@))_CCFLAGS) -c -o $@ $<

include/tinycbor/cbor.h

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
#define CBOR_H
2727

2828
#include <assert.h>
29-
#include <limits.h>
3029
#include <stddef.h>
3130
#include <stdint.h>
3231
#include <string.h>
3332
#include <stdio.h>
3433

34+
#include <tinycbor/cbor_buf_writer.h>
35+
#include <tinycbor/cbor_buf_reader.h>
36+
#include <tinycbor/cbor_defs.h>
3537
#include "tinycbor-version.h"
3638

3739
#define TINYCBOR_VERSION ((TINYCBOR_VERSION_MAJOR << 16) | (TINYCBOR_VERSION_MINOR << 8) | TINYCBOR_VERSION_PATCH)
@@ -209,21 +211,24 @@ typedef struct cbor_encoder_writer {
209211
} cbor_encoder_writer;
210212

211213

214+
=======
215+
>>>>>>> Making API backwards compatible
212216
/* Encoder API */
213217
struct CborEncoder
214218
{
215219
cbor_encoder_writer *writer;
216220
void *writer_arg;
221+
struct cbor_buf_writer wr;
217222
size_t added;
218223
size_t container_size;
219224
int flags;
220225
};
221-
typedef struct CborEncoder CborEncoder;
222226

223-
static const size_t CborIndefiniteLength = SIZE_MAX;
227+
typedef struct CborEncoder CborEncoder;
224228

225229

226-
CBOR_API void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *pwriter, int flags);
230+
CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
231+
CBOR_API void cbor_encoder_cust_writer_init(CborEncoder *encoder, struct cbor_encoder_writer *w, int flags);
227232
CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value);
228233
CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value);
229234
CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value);
@@ -271,27 +276,9 @@ enum CborParserIteratorFlags
271276
CborIteratorFlag_ContainerIsMap = 0x20
272277
};
273278

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-
293279
struct CborParser
294280
{
281+
struct cbor_buf_reader br;
295282
struct cbor_decoder_reader *d;
296283
int end;
297284
int flags;
@@ -309,7 +296,8 @@ struct CborValue
309296
};
310297
typedef struct CborValue CborValue;
311298

312-
CBOR_API CborError cbor_parser_init(struct cbor_decoder_reader *d, int flags, CborParser *parser, CborValue *it);
299+
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
300+
CBOR_API CborError cbor_parser_cust_reader_init(struct cbor_decoder_reader *r, int flags, CborParser *parser, CborValue *it);
313301

314302
CBOR_API CborError cbor_value_validate_basic(const CborValue *it);
315303

@@ -615,11 +603,12 @@ CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value
615603
return cbor_value_to_pretty_advance_flags(out, &copy, CborPrettyDefaultFlags);
616604
}
617605

606+
CBOR_API const char *cbor_error_string(CborError error);
607+
618608
#endif /* __STDC_HOSTED__ check */
619609

620610
#ifdef __cplusplus
621611
}
622612
#endif
623613

624614
#endif /* CBOR_H */
625-

include/tinycbor/cbor_buf_reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
extern "C" {
3030
#endif
3131

32-
#include <tinycbor/cbor.h>
32+
#include <tinycbor/cbor_decoder_reader.h>
3333

3434
struct cbor_buf_reader {
3535
struct cbor_decoder_reader r;

include/tinycbor/cbor_buf_writer.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#ifndef CBOR_BUF_WRITER_H
2626
#define CBOR_BUF_WRITER_H
2727

28+
#include <tinycbor/cbor_encoder_writer.h>
29+
2830
#ifdef __cplusplus
2931
extern "C" {
3032
#endif
@@ -36,24 +38,15 @@ struct cbor_buf_writer {
3638
int bytes_needed;
3739
};
3840

41+
struct CborEncoder;
42+
3943
void cbor_buf_writer_init(struct cbor_buf_writer *cb, uint8_t *buffer,
4044
size_t data);
4145
size_t cbor_buf_writer_buffer_size(struct cbor_buf_writer *cb,
4246
const uint8_t *buffer);
43-
44-
CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
45-
{
46-
struct cbor_buf_writer *wr = (struct cbor_buf_writer *)encoder->writer;
47-
48-
return wr->end ? 0 : (size_t)wr->bytes_needed;
49-
}
50-
51-
CBOR_INLINE_API size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
52-
{
53-
struct cbor_buf_writer *wr = (struct cbor_buf_writer *)encoder->writer;
54-
55-
return (size_t)(wr->ptr - buffer);
56-
}
47+
size_t cbor_encoder_get_extra_bytes_needed(const struct CborEncoder *encoder);
48+
size_t cbor_encoder_get_buffer_size(const struct CborEncoder *encoder,
49+
const uint8_t *buffer);
5750

5851
#ifdef __cplusplus
5952
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_DECODER_WRITER_H
26+
#define CBOR_DECODER_WRITER_H
27+
28+
#include <stdint.h>
29+
#include <stddef.h>
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
struct cbor_decoder_reader;
36+
37+
typedef uint8_t (cbor_reader_get8)(struct cbor_decoder_reader *d, int offset);
38+
typedef uint16_t (cbor_reader_get16)(struct cbor_decoder_reader *d, int offset);
39+
typedef uint32_t (cbor_reader_get32)(struct cbor_decoder_reader *d, int offset);
40+
typedef uint64_t (cbor_reader_get64)(struct cbor_decoder_reader *d, int offset);
41+
typedef uintptr_t (cbor_memcmp)(struct cbor_decoder_reader *d, char *buf, int offset, size_t len);
42+
typedef uintptr_t (cbor_memcpy)(struct cbor_decoder_reader *d, char *buf, int offset, size_t len);
43+
44+
struct cbor_decoder_reader {
45+
cbor_reader_get8 *get8;
46+
cbor_reader_get16 *get16;
47+
cbor_reader_get32 *get32;
48+
cbor_reader_get64 *get64;
49+
cbor_memcmp *cmp;
50+
cbor_memcpy *cpy;
51+
size_t message_size;
52+
};
53+
54+
#ifdef __cplusplus
55+
}
56+
#endif
57+
58+
#endif

include/tinycbor/cbor_defs.h

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
#ifndef CBOR_DEFS_H
26+
#define CBOR_DEFS_H
27+
28+
#include <limits.h>
29+
30+
#ifndef SIZE_MAX
31+
/* Some systems fail to define SIZE_MAX in <stdint.h>, even though C99 requires it...
32+
* Conversion from signed to unsigned is defined in 6.3.1.3 (Signed and unsigned integers) p2,
33+
* which says: "the value is converted by repeatedly adding or subtracting one more than the
34+
* maximum value that can be represented in the new type until the value is in the range of the
35+
* new type."
36+
* So -1 gets converted to size_t by adding SIZE_MAX + 1, which results in SIZE_MAX.
37+
*/
38+
# define SIZE_MAX ((size_t)-1)
39+
#endif
40+
41+
#ifndef CBOR_API
42+
# define CBOR_API
43+
#endif
44+
#ifndef CBOR_PRIVATE_API
45+
# define CBOR_PRIVATE_API
46+
#endif
47+
#ifndef CBOR_INLINE_API
48+
# if defined(__cplusplus)
49+
# define CBOR_INLINE inline
50+
# define CBOR_INLINE_API inline
51+
# else
52+
# define CBOR_INLINE_API static CBOR_INLINE
53+
# if defined(_MSC_VER)
54+
# define CBOR_INLINE __inline
55+
# elif defined(__GNUC__)
56+
# define CBOR_INLINE __inline__
57+
# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
58+
# define CBOR_INLINE inline
59+
# else
60+
# define CBOR_INLINE
61+
# endif
62+
# endif
63+
#endif
64+
65+
typedef enum CborType {
66+
CborIntegerType = 0x00,
67+
CborByteStringType = 0x40,
68+
CborTextStringType = 0x60,
69+
CborArrayType = 0x80,
70+
CborMapType = 0xa0,
71+
CborTagType = 0xc0,
72+
CborSimpleType = 0xe0,
73+
CborBooleanType = 0xf5,
74+
CborNullType = 0xf6,
75+
CborUndefinedType = 0xf7,
76+
CborHalfFloatType = 0xf9,
77+
CborFloatType = 0xfa,
78+
CborDoubleType = 0xfb,
79+
80+
CborInvalidType = 0xff /* equivalent to the break byte, so it will never be used */
81+
} CborType;
82+
83+
typedef uint64_t CborTag;
84+
typedef enum CborKnownTags {
85+
CborDateTimeStringTag = 0, /* RFC 3339 format: YYYY-MM-DD hh:mm:ss+zzzz */
86+
CborUnixTime_tTag = 1,
87+
CborPositiveBignumTag = 2,
88+
CborNegativeBignumTag = 3,
89+
CborDecimalTag = 4,
90+
CborBigfloatTag = 5,
91+
CborExpectedBase64urlTag = 21,
92+
CborExpectedBase64Tag = 22,
93+
CborExpectedBase16Tag = 23,
94+
CborUriTag = 32,
95+
CborBase64urlTag = 33,
96+
CborBase64Tag = 34,
97+
CborRegularExpressionTag = 35,
98+
CborMimeMessageTag = 36, /* RFC 2045-2047 */
99+
CborSignatureTag = 55799
100+
} CborKnownTags;
101+
102+
/* Error API */
103+
104+
typedef enum CborError {
105+
CborNoError = 0,
106+
107+
/* errors in all modes */
108+
CborUnknownError,
109+
CborErrorUnknownLength, /* request for length in array, map, or string with indeterminate length */
110+
CborErrorAdvancePastEOF,
111+
CborErrorIO,
112+
113+
/* parser errors streaming errors */
114+
CborErrorGarbageAtEnd = 256,
115+
CborErrorUnexpectedEOF,
116+
CborErrorUnexpectedBreak,
117+
CborErrorUnknownType, /* can only heppen in major type 7 */
118+
CborErrorIllegalType, /* type not allowed here */
119+
CborErrorIllegalNumber,
120+
CborErrorIllegalSimpleType, /* types of value less than 32 encoded in two bytes */
121+
122+
/* parser errors in strict mode parsing only */
123+
CborErrorUnknownSimpleType = 512,
124+
CborErrorUnknownTag,
125+
CborErrorInappropriateTagForType,
126+
CborErrorDuplicateObjectKeys,
127+
CborErrorInvalidUtf8TextString,
128+
129+
/* encoder errors */
130+
CborErrorTooManyItems = 768,
131+
CborErrorTooFewItems,
132+
133+
/* internal implementation errors */
134+
CborErrorDataTooLarge = 1024,
135+
CborErrorNestingTooDeep,
136+
CborErrorUnsupportedType,
137+
138+
/* errors in converting to JSON */
139+
CborErrorJsonObjectKeyIsAggregate,
140+
CborErrorJsonObjectKeyNotString,
141+
CborErrorJsonNotImplemented,
142+
143+
CborErrorOutOfMemory = (int) (~0U / 2 + 1),
144+
CborErrorInternalError = (int) ~0U
145+
} CborError;
146+
147+
static const size_t CborIndefiniteLength = SIZE_MAX;
148+
149+
#endif

0 commit comments

Comments
 (0)