Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tinycbor mynewt upstream #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ 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 \
Expand All @@ -31,6 +35,8 @@ TINYCBOR_SOURCES = \
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 Expand Up @@ -95,7 +101,7 @@ endif
# version using funopen or fopencookie
ifeq ($(open_memstream-pass),)
ifeq ($(funopen-pass)$(fopencookie-pass),)
CFLAGS += -DWITHOUT_OPEN_MEMSTREAM
CFLAGS += -DCBOR_WITHOUT_OPEN_MEMSTREAM
$(warning warning: funopen and fopencookie unavailable, open_memstream can not be implemented and conversion to JSON will not work properly!)
else
TINYCBOR_SOURCES += src/open_memstream.c
Expand Down
10 changes: 7 additions & 3 deletions Makefile.nmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CFLAGS = -W3

TINYCBOR_HEADERS = src\cbor.h src\cborjson.h
TINYCBOR_HEADERS = src
TINYCBOR_SOURCES = \
src\cborerrorstrings.c \
src\cborencoder.c \
Expand All @@ -9,7 +9,9 @@ TINYCBOR_SOURCES = \
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 \
Expand All @@ -18,7 +20,9 @@ TINYCBOR_OBJS = \
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
48 changes: 23 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,23 @@ 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 CBOR_NO_DFLT_WRITER
struct cbor_buf_writer wr;
#endif
size_t remaining;
int flags;
};

typedef struct CborEncoder CborEncoder;

static const size_t CborIndefiniteLength = SIZE_MAX;

#ifndef CBOR_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 +233,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 +254,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 +267,36 @@ enum CborParserIteratorFlags

struct CborParser
{
const uint8_t *end;
#ifndef CBOR_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 CBOR_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