Skip to content

Commit

Permalink
Bring tinycbor up to date with mynewt tinycbor
Browse files Browse the repository at this point in the history
- Removing cborencoder_close_container_checked.c since
  cborencoder_close_container() checks the number of elements now.
- Add container_size for the container
- cbor_encoder_close_container(): look at isMap flag to determine
  container_size for comparison
- 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
  • Loading branch information
vrahane committed Jan 8, 2018
1 parent f39dcb8 commit 88c8d4c
Show file tree
Hide file tree
Showing 28 changed files with 889 additions and 318 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ RMDIR = rmdir
SED = sed

# Our sources
TINYCBOR_HEADERS = src/cbor.h src/cborjson.h
TINYCBOR_HEADERS = \
src/cbor.h \
src/cborjson.h \
src/cbor_enocoder_writer.h \
src/cbor_decoder_reader.h
TINYCBOR_SOURCES = \
src/cborerrorstrings.c \
src/cborencoder.c \
src/cborencoder_close_container_checked.c \
src/cborparser.c \
src/cborparser_dup_string.c \
src/cborpretty.c \
src/cborpretty_stdio.c \
src/cbortojson.c \
src/cborvalidation.c \
src/cbor_buf_reader.c \
src/cbor_buf_writer.c
#
CBORDUMP_SOURCES = tools/cbordump/cbordump.c

Expand Down
12 changes: 7 additions & 5 deletions Makefile.nmake
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
CFLAGS = -W3

TINYCBOR_HEADERS = src\cbor.h src\cborjson.h
TINYCBOR_HEADERS = src
TINYCBOR_SOURCES = \
src\cborerrorstrings.c \
src\cborencoder.c \
src\cborencoder_close_container_checked.c \
src\cborparser.c \
src\cborparser_dup_string.c \
src\cborpretty.c \
src\cborpretty_stdio.c \
src\cborvalidation.c
src\cborvalidation.c \
src\cbor_buf_reader.c \
src\cbor_buf_writer.c
TINYCBOR_OBJS = \
src\cborerrorstrings.obj \
src\cborencoder.obj \
src\cborencoder_close_container_checked.obj \
src\cborparser.obj \
src\cborparser_dup_string.obj \
src\cborpretty.obj \
src\cborpretty_stdio.obj \
src\cborvalidation.obj
src\cborvalidation.obj \
src\cbor_buf_writer.obj \
src\cbor_buf_reader.obj

all: lib\tinycbor.lib
check: tests\Makefile lib\tinycbor.lib
Expand Down
49 changes: 24 additions & 25 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <string.h>
#include <stdio.h>

#include "cbor_buf_writer.h"
#include "cbor_buf_reader.h"
#include "tinycbor-version.h"

#define TINYCBOR_VERSION ((TINYCBOR_VERSION_MAJOR << 16) | (TINYCBOR_VERSION_MINOR << 8) | TINYCBOR_VERSION_PATCH)
Expand Down Expand Up @@ -204,19 +206,24 @@ CBOR_API const char *cbor_error_string(CborError error);
/* Encoder API */
struct CborEncoder
{
union {
uint8_t *ptr;
ptrdiff_t bytes_needed;
} data;
const uint8_t *end;
cbor_encoder_writer *writer;
void *writer_arg;
#ifndef NO_DFLT_WRITER
struct cbor_buf_writer wr;
#endif
size_t added;
size_t container_size;
int flags;
};

typedef struct CborEncoder CborEncoder;

static const size_t CborIndefiniteLength = SIZE_MAX;

#ifndef NO_DFLT_WRITER
CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
#endif
CBOR_API void cbor_encoder_cust_writer_init(CborEncoder *encoder, struct cbor_encoder_writer *w, int flags);
CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value);
CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value);
CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value);
Expand All @@ -227,7 +234,8 @@ CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const c
{ return cbor_encode_text_string(encoder, string, strlen(string)); }
CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length);
CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value);

CBOR_INLINE_API int cbor_encode_bytes_written(CborEncoder *encoder)
{ return encoder->writer->bytes_written; }
CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
{ return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); }
CBOR_INLINE_API CborError cbor_encode_null(CborEncoder *encoder)
Expand All @@ -247,21 +255,6 @@ CBOR_API CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *ma
CBOR_API CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder);
CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder);

CBOR_INLINE_API uint8_t *_cbor_encoder_get_buffer_pointer(const CborEncoder *encoder)
{
return encoder->data.ptr;
}

CBOR_INLINE_API size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
{
return (size_t)(encoder->data.ptr - buffer);
}

CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
{
return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
}

/* Parser API */

enum CborParserIteratorFlags
Expand All @@ -275,30 +268,36 @@ enum CborParserIteratorFlags

struct CborParser
{
const uint8_t *end;
#ifndef NO_DFLT_READER
struct cbor_buf_reader br;
#endif
struct cbor_decoder_reader *d;
int end;
int flags;
};
typedef struct CborParser CborParser;

struct CborValue
{
const CborParser *parser;
const uint8_t *ptr;
int offset;
uint32_t remaining;
uint32_t remainingclen;
uint16_t extra;
uint8_t type;
uint8_t flags;
};
typedef struct CborValue CborValue;

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

CBOR_API CborError cbor_value_validate_basic(const CborValue *it);

CBOR_INLINE_API bool cbor_value_at_end(const CborValue *it)
{ return it->remaining == 0; }
CBOR_INLINE_API const uint8_t *cbor_value_get_next_byte(const CborValue *it)
{ return it->ptr; }
CBOR_API CborError cbor_value_advance_fixed(CborValue *it);
CBOR_API CborError cbor_value_advance(CborValue *it);
CBOR_INLINE_API bool cbor_value_is_container(const CborValue *it)
Expand Down
163 changes: 163 additions & 0 deletions src/cbor_buf_reader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/****************************************************************************
**
** Copyright (C) 2016 Intel Corporation
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
** THE SOFTWARE.
**
****************************************************************************/

#include "cbor_buf_reader.h"
#include "compilersupport_p.h"

/**
* \addtogroup CborParsing
* @{
*/

/**
* Gets 16 bit unsigned value from the passed in ptr location, it also
* converts it to host byte order
*/
CBOR_INLINE_API uint16_t get16(const uint8_t *ptr)
{
uint16_t result;
memcpy(&result, ptr, sizeof(result));
return cbor_ntohs(result);
}

/**
* Gets 32 bit unsigned value from the passed in ptr location, it also
* converts it to host byte order
*/
CBOR_INLINE_API uint32_t get32(const uint8_t *ptr)
{
uint32_t result;
memcpy(&result, ptr, sizeof(result));
return cbor_ntohl(result);
}

/**
* Gets 64 bit unsigned value from the passed in ptr location, it also
* converts it to host byte order
*/
CBOR_INLINE_API uint64_t get64(const uint8_t *ptr)
{
uint64_t result;
memcpy(&result, ptr, sizeof(result));
return cbor_ntohll(result);
}

/**
* Gets a string chunk from the passed in ptr location
*/
CBOR_INLINE_API uintptr_t get_string_chunk(const uint8_t *ptr)
{
return (uintptr_t)ptr;
}

/**
* Gets 8 bit unsigned value using the buffer pointed to by the
* decoder reader from passed in offset
*/
static uint8_t
cbuf_buf_reader_get8(struct cbor_decoder_reader *d, int offset)
{
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
return cb->buffer[offset];
}

/**
* Gets 16 bit unsigned value using the buffer pointed to by the
* decoder reader from passed in offset
*/
static uint16_t
cbuf_buf_reader_get16(struct cbor_decoder_reader *d, int offset)
{
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
return get16(cb->buffer + offset);
}

/**
* Gets 32 bit unsigned value using the buffer pointed to by the
* decoder reader from passed in offset
*/
static uint32_t
cbuf_buf_reader_get32(struct cbor_decoder_reader *d, int offset)
{
uint32_t val;
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
val = get32(cb->buffer + offset);
return val;
}

/**
* Gets 64 bit unsigned value using the buffer pointed to by the
* decoder reader from passed in offset
*/
static uint64_t
cbuf_buf_reader_get64(struct cbor_decoder_reader *d, int offset)
{
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
return get64(cb->buffer + offset);
}

static uintptr_t
cbor_buf_reader_get_string_chunk(struct cbor_decoder_reader *d,
int offset, size_t *len)
{
struct cbor_buf_reader *cb = (struct cbor_buf_reader *)d;

(void)*len;

return get_string_chunk(cb->buffer + offset);
}

static uintptr_t
cbor_buf_reader_cmp(struct cbor_decoder_reader *d, char *dst, int src_offset,
size_t len)
{
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;

return !memcmp(dst, cb->buffer + src_offset, len);
}

static uintptr_t
cbor_buf_reader_cpy(struct cbor_decoder_reader *d, char *dst, int src_offset,
size_t len)
{
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
return (uintptr_t) memcpy(dst, cb->buffer + src_offset, len);
}

void
cbor_buf_reader_init(struct cbor_buf_reader *cb, const uint8_t *buffer,
size_t data)
{
cb->buffer = buffer;
cb->r.get8 = &cbuf_buf_reader_get8;
cb->r.get16 = &cbuf_buf_reader_get16;
cb->r.get32 = &cbuf_buf_reader_get32;
cb->r.get64 = &cbuf_buf_reader_get64;
cb->r.cmp = &cbor_buf_reader_cmp;
cb->r.cpy = &cbor_buf_reader_cpy;
cb->r.get_string_chunk = &cbor_buf_reader_get_string_chunk;
cb->r.message_size = data;
}

/** @} */
47 changes: 47 additions & 0 deletions src/cbor_buf_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/****************************************************************************
**
** Copyright (C) 2016 Intel Corporation
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
** THE SOFTWARE.
**
****************************************************************************/

#ifndef CBOR_BUF_READER_H
#define CBOR_BUF_READER_H

#ifdef __cplusplus
extern "C" {
#endif

#include "cbor_decoder_reader.h"

struct cbor_buf_reader {
struct cbor_decoder_reader r;
const uint8_t *buffer;
};

void cbor_buf_reader_init(struct cbor_buf_reader *cb, const uint8_t *buffer,
size_t data);

#ifdef __cplusplus
}
#endif

#endif /* CBOR_BUF_READER_H */

Loading

0 comments on commit 88c8d4c

Please sign in to comment.