You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by pa...@apache.org on 2016/10/07 19:23:59 UTC

[1/3] incubator-mynewt-core git commit: forgot to account for offset in one spot

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop a6a2c878e -> 9164e65c0


forgot to account for offset in one spot


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/9164e65c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/9164e65c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/9164e65c

Branch: refs/heads/develop
Commit: 9164e65c080a07a9310fe306df61ba30ae098508
Parents: d49b9ab
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Fri Oct 7 12:07:55 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Fri Oct 7 12:08:33 2016 -0700

----------------------------------------------------------------------
 encoding/tinycbor/src/cbor_mbuf_reader.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9164e65c/encoding/tinycbor/src/cbor_mbuf_reader.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cbor_mbuf_reader.c b/encoding/tinycbor/src/cbor_mbuf_reader.c
index e272487..f37908b 100644
--- a/encoding/tinycbor/src/cbor_mbuf_reader.c
+++ b/encoding/tinycbor/src/cbor_mbuf_reader.c
@@ -25,7 +25,7 @@ static uint8_t
 cbuf_mbuf_reader_get8(struct cbor_decoder_reader *d, int offset) {
     uint8_t val;
     struct CborMbufReader *cb = (struct CborMbufReader *) d;
-    os_mbuf_copydata(cb->m, offset, sizeof(val), &val);
+    os_mbuf_copydata(cb->m, offset + cb->init_off, sizeof(val), &val);
     return val;
 }
 


[3/3] incubator-mynewt-core git commit: refactor the encoder for tinycbor to use mbufs, flat bufs, or just count size (to replace existing functionality). This had the detrimental affect of making the checked container close difficult so that is left our

Posted by pa...@apache.org.
refactor the encoder for tinycbor to use mbufs, flat bufs, or just count size (to replace existing functionality).
This had the detrimental affect of making the checked container close difficult so that is left our for now.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/4246a503
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/4246a503
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/4246a503

Branch: refs/heads/develop
Commit: 4246a5030f194f171fd33fa5a1fe4d4ff6239e9c
Parents: a6a2c87
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Fri Sep 30 17:35:29 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Fri Oct 7 12:08:33 2016 -0700

----------------------------------------------------------------------
 encoding/tinycbor/include/tinycbor/cbor.h       | 23 +++------
 .../tinycbor/include/tinycbor/cbor_buf_writer.h | 39 ++++++++++++++
 .../tinycbor/include/tinycbor/cbor_cnt_writer.h | 53 ++++++++++++++++++++
 .../include/tinycbor/cbor_mbuf_writer.h         | 42 ++++++++++++++++
 .../include/tinycbor/extract_number_p.h         |  3 +-
 encoding/tinycbor/src/cbor_buf_writer.c         | 53 ++++++++++++++++++++
 encoding/tinycbor/src/cbor_mbuf_writer.c        | 47 +++++++++++++++++
 encoding/tinycbor/src/cborencoder.c             | 49 ++++--------------
 .../src/cborencoder_close_container_checked.c   | 36 -------------
 net/oic/src/api/oc_rep.c                        |  7 ++-
 10 files changed, 257 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/include/tinycbor/cbor.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor.h b/encoding/tinycbor/include/tinycbor/cbor.h
index f78e4af..7b8e861 100644
--- a/encoding/tinycbor/include/tinycbor/cbor.h
+++ b/encoding/tinycbor/include/tinycbor/cbor.h
@@ -157,14 +157,14 @@ typedef enum CborError {
 
 CBOR_API const char *cbor_error_string(CborError error);
 
+
+typedef int (cbor_encoder_writer)(void *arg, const char *data, int len);
+
 /* Encoder API */
 struct CborEncoder
 {
-    union {
-        uint8_t *ptr;
-        ptrdiff_t bytes_needed;
-    };
-    const uint8_t *end;
+    cbor_encoder_writer *writer;
+    void *writer_arg;
     size_t added;
     int flags;
 };
@@ -172,7 +172,8 @@ typedef struct CborEncoder CborEncoder;
 
 static const size_t CborIndefiniteLength = SIZE_MAX;
 
-CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
+
+CBOR_API void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *pwriter, void *writer_arg, 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);
@@ -203,16 +204,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 size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
-{
-    return (size_t)(encoder->ptr - buffer);
-}
-
-CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
-{
-    return encoder->end ? 0 : (size_t)encoder->bytes_needed;
-}
-
 /* Parser API */
 
 enum CborParserIteratorFlags

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h b/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h
new file mode 100644
index 0000000..9dbf4da
--- /dev/null
+++ b/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h
@@ -0,0 +1,39 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/* 
+ * File:   cbor_buf_writer.h
+ * Author: paulfdietrich
+ *
+ * Created on September 30, 2016, 4:38 PM
+ */
+
+#ifndef CBOR_BUF_WRITER_H
+#define CBOR_BUF_WRITER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct CborBufWriter {
+    uint8_t *ptr;
+    const uint8_t *end;
+};
+
+void
+cbor_buf_writer_init(struct CborBufWriter *cb, uint8_t *buffer, size_t data);
+
+int
+cbor_buf_writer(void *arg, const char *data, int len);
+
+size_t
+cbor_buf_writer_buffer_size(struct CborBufWriter *cb, const uint8_t *buffer);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CBOR_BUF_WRITER_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h b/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h
new file mode 100644
index 0000000..e5c4485
--- /dev/null
+++ b/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h
@@ -0,0 +1,53 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/* 
+ * File:   cbor_cnt_writer.h
+ * Author: paulfdietrich
+ *
+ * Created on September 30, 2016, 4:50 PM
+ */
+
+#ifndef CBOR_CNT_WRITER_H
+#define CBOR_CNT_WRITER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+    /* use this count writer if you want to try out a cbor encoding to see
+     * how long it would be (before allocating memory). This replaced the
+     * code in tinycbor.h that would try to do this once the encoding failed
+     * in a buffer.  Its much easier to understand this way (for me)
+     */
+
+struct CborCntWriter {
+    int bytes_written;
+};
+
+inline void
+cbor_cnt_writer_init(struct CborCntWriter *cb) {
+    cb->bytes_written = 0;
+}
+
+inline int
+cbor_cnt_writer(void *arg, char *data, int len) {
+    struct CborCntWriter *cb = (struct CborCntWriter *) arg;
+    cb->bytes_written += len;
+    return CborNoError;
+}
+
+inline int
+cbor_cnt_writer_length(struct CborCntWriter *cb) {
+    return cb->bytes_written;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CBOR_CNT_WRITER_H */
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h b/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h
new file mode 100644
index 0000000..b5f2c17
--- /dev/null
+++ b/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h
@@ -0,0 +1,42 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/* 
+ * File:   cbor_mbuf_writer.h
+ * Author: paulfdietrich
+ *
+ * Created on September 30, 2016, 4:59 PM
+ */
+
+#ifndef CBOR_MBUF_WRITER_H
+#define CBOR_MBUF_WRITER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+struct CborMbufWriter {
+    int bytes_written;
+    struct os_mbuf *m;
+};
+
+void
+cbor_mbuf_writer_init(struct CborMbufWriter *cb, struct os_mbuf *m);
+
+int
+cbor_mbuf_writer(void *arg, const char *data, int len);
+
+
+int
+cbor_mbuf_bytes_written(struct CborMbufWriter *cb);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CBOR_MBUF_WRITER_H */
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/include/tinycbor/extract_number_p.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/extract_number_p.h b/encoding/tinycbor/include/tinycbor/extract_number_p.h
index b65ca44..7d401f1 100644
--- a/encoding/tinycbor/include/tinycbor/extract_number_p.h
+++ b/encoding/tinycbor/include/tinycbor/extract_number_p.h
@@ -50,7 +50,7 @@ static inline uint64_t get64(const uint8_t *ptr)
     return cbor_ntohll(result);
 }
 
-static CborError extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len)
+static inline CborError extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len)
 {
     uint8_t additional_information = **ptr & SmallValueMask;
     ++*ptr;
@@ -76,3 +76,4 @@ static CborError extract_number(const uint8_t **ptr, const uint8_t *end, uint64_
     *ptr += bytesNeeded;
     return CborNoError;
 }
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/src/cbor_buf_writer.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cbor_buf_writer.c b/encoding/tinycbor/src/cbor_buf_writer.c
new file mode 100644
index 0000000..b734f81
--- /dev/null
+++ b/encoding/tinycbor/src/cbor_buf_writer.c
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <tinycbor/cbor.h>
+#include <tinycbor/cbor_buf_writer.h>
+
+void cbor_buf_writer_init(struct CborBufWriter *cb, uint8_t *buffer, size_t size)
+{
+    cb->ptr = buffer;
+    cb->end = buffer + size;
+}
+
+static inline int would_overflow(struct CborBufWriter *cb, size_t len)
+{
+    ptrdiff_t remaining = (ptrdiff_t)cb->end;
+    remaining -= (ptrdiff_t)cb->ptr;
+    remaining -= (ptrdiff_t)len;
+    return (remaining < 0);
+}
+
+int cbor_buf_writer(void *arg, const char *data, int len) {
+    struct CborBufWriter *cb = (struct CborBufWriter *) arg;
+
+    if (would_overflow(cb, len)) {
+        return CborErrorOutOfMemory;
+    }
+
+    memcpy(cb->ptr, data, len);
+    cb->ptr += len;
+    return CborNoError;
+}
+
+size_t
+cbor_buf_writer_buffer_size(struct CborBufWriter *cb, const uint8_t *buffer)
+{
+    return (size_t)(cb->ptr - buffer);
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/src/cbor_mbuf_writer.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cbor_mbuf_writer.c b/encoding/tinycbor/src/cbor_mbuf_writer.c
new file mode 100644
index 0000000..f40d0a3
--- /dev/null
+++ b/encoding/tinycbor/src/cbor_mbuf_writer.c
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <tinycbor/cbor.h>
+#include <os/os_mbuf.h>
+#include <tinycbor/cbor_mbuf_writer.h>
+
+void
+cbor_mbuf_writer_init(struct CborMbufWriter *cb, struct os_mbuf *m) {
+    cb->m = m;
+    cb->bytes_written = 0;
+}
+
+int
+cbor_mbuf_writer(void *arg, const char *data, int len) {
+    int rc;
+    struct CborMbufWriter *cb = (struct CborMbufWriter *) arg;
+    rc = os_mbuf_append(cb->m, data, len);
+
+    if (rc) {
+        return CborErrorOutOfMemory;
+    }
+
+    cb->bytes_written += len;
+    return CborNoError;
+}
+
+int
+cbor_mbuf_bytes_written(struct CborMbufWriter *cb) {
+    return cb->bytes_written;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/src/cborencoder.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cborencoder.c b/encoding/tinycbor/src/cborencoder.c
index eef05cd..0c71d26 100644
--- a/encoding/tinycbor/src/cborencoder.c
+++ b/encoding/tinycbor/src/cborencoder.c
@@ -199,10 +199,10 @@
  * buffer of size \a size. The \a flags field is currently unused and must be
  * zero.
  */
-void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags)
+void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *writer, void* writer_arg, int flags)
 {
-    encoder->ptr = buffer;
-    encoder->end = buffer + size;
+    encoder->writer = writer;
+    encoder->writer_arg = writer_arg;
     encoder->added = 0;
     encoder->flags = flags;
 }
@@ -236,38 +236,9 @@ static inline void put64(void *where, uint64_t v)
     memcpy(where, &v, sizeof(v));
 }
 
-static inline bool would_overflow(CborEncoder *encoder, size_t len)
-{
-    ptrdiff_t remaining = (ptrdiff_t)encoder->end;
-    remaining -= remaining ? (ptrdiff_t)encoder->ptr : encoder->bytes_needed;
-    remaining -= (ptrdiff_t)len;
-    return unlikely(remaining < 0);
-}
-
-static inline void advance_ptr(CborEncoder *encoder, size_t n)
-{
-    if (encoder->end)
-        encoder->ptr += n;
-    else
-        encoder->bytes_needed += n;
-}
-
 static inline CborError append_to_buffer(CborEncoder *encoder, const void *data, size_t len)
 {
-    if (would_overflow(encoder, len)) {
-        if (encoder->end != NULL) {
-            len -= encoder->end - encoder->ptr;
-            encoder->end = NULL;
-            encoder->bytes_needed = 0;
-        }
-
-        advance_ptr(encoder, len);
-        return CborErrorOutOfMemory;
-    }
-
-    memcpy(encoder->ptr, data, len);
-    encoder->ptr += len;
-    return CborNoError;
+    return encoder->writer(encoder->writer_arg, data, len);
 }
 
 static inline CborError append_byte_to_buffer(CborEncoder *encoder, uint8_t byte)
@@ -451,8 +422,8 @@ __attribute__((noinline))
 static CborError create_container(CborEncoder *encoder, CborEncoder *container, size_t length, uint8_t shiftedMajorType)
 {
     CborError err;
-    container->ptr = encoder->ptr;
-    container->end = encoder->end;
+    container->writer = encoder->writer;
+    container->writer_arg = encoder->writer_arg;
     ++encoder->added;
     container->added = 0;
 
@@ -530,11 +501,9 @@ CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder,
  */
 CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder)
 {
-    if (encoder->end)
-        encoder->ptr = containerEncoder->ptr;
-    else
-        encoder->bytes_needed = containerEncoder->bytes_needed;
-    encoder->end = containerEncoder->end;
+    encoder->writer = containerEncoder->writer;
+    encoder->writer_arg = containerEncoder->writer_arg;
+
     if (containerEncoder->flags & CborIteratorFlag_UnknownLength)
         return append_byte_to_buffer(encoder, BreakByte);
     return CborNoError;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/encoding/tinycbor/src/cborencoder_close_container_checked.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cborencoder_close_container_checked.c b/encoding/tinycbor/src/cborencoder_close_container_checked.c
index cad8335..bcf77d6 100644
--- a/encoding/tinycbor/src/cborencoder_close_container_checked.c
+++ b/encoding/tinycbor/src/cborencoder_close_container_checked.c
@@ -42,41 +42,5 @@
  * @{
  */
 
-/**
- *
- * Closes the CBOR container (array or map) provided by \a containerEncoder and
- * updates the CBOR stream provided by \a encoder. Both parameters must be the
- * same as were passed to cbor_encoder_create_array() or
- * cbor_encoder_create_map().
- *
- * Unlike cbor_encoder_close_container(), this function checks that the number
- * of items (or pair of items, in the case of a map) was correct. If the number
- * of items inserted does not match the length originally passed to
- * cbor_encoder_create_array() or cbor_encoder_create_map(), this function
- * returns either CborErrorTooFewItems or CborErrorTooManyItems.
- *
- * \sa cbor_encoder_create_array(), cbor_encoder_create_map()
- */
-CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder)
-{
-    const uint8_t *ptr = encoder->ptr;
-    CborError err = cbor_encoder_close_container(encoder, containerEncoder);
-    if (containerEncoder->flags & CborIteratorFlag_UnknownLength || encoder->end == NULL)
-        return err;
-
-    /* check what the original length was */
-    uint64_t actually_added;
-    err = extract_number(&ptr, encoder->ptr, &actually_added);
-    if (err)
-        return err;
-
-    if (containerEncoder->flags & CborIteratorFlag_ContainerIsMap) {
-        if (actually_added > SIZE_MAX / 2)
-            return CborErrorDataTooLarge;
-        actually_added *= 2;
-    }
-    return actually_added == containerEncoder->added ? CborNoError :
-           actually_added < containerEncoder->added ? CborErrorTooManyItems : CborErrorTooFewItems;
-}
 
 /** @} */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4246a503/net/oic/src/api/oc_rep.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_rep.c b/net/oic/src/api/oc_rep.c
index b922e1e..1c96bf3 100644
--- a/net/oic/src/api/oc_rep.c
+++ b/net/oic/src/api/oc_rep.c
@@ -19,25 +19,28 @@
 #include "port/oc_assert.h"
 #include "port/oc_log.h"
 #include "util/oc_memb.h"
+#include <tinycbor/cbor_buf_writer.h>
 
 OC_MEMB(rep_objects, oc_rep_t, EST_NUM_REP_OBJECTS);
 static const CborEncoder g_empty;
 static uint8_t *g_buf;
 CborEncoder g_encoder, root_map, links_array;
 CborError g_err;
+struct CborBufWriter g_buf_writer;
 
 void
 oc_rep_new(uint8_t *out_payload, int size)
 {
   g_err = CborNoError;
   g_buf = out_payload;
-  cbor_encoder_init(&g_encoder, out_payload, size, 0);
+  cbor_buf_writer_init(&g_buf_writer, out_payload, size);
+  cbor_encoder_init(&g_encoder, &cbor_buf_writer, &g_buf_writer, 0);
 }
 
 int
 oc_rep_finalize(void)
 {
-  int size = cbor_encoder_get_buffer_size(&g_encoder, g_buf);
+  int size = cbor_buf_writer_buffer_size(&g_buf_writer, g_buf);
   oc_rep_reset();
   if (g_err != CborNoError)
     return -1;


[2/3] incubator-mynewt-core git commit: finish refactoring tinycbor to allow mbuf decoding or flat buf decoding

Posted by pa...@apache.org.
finish refactoring tinycbor to allow mbuf decoding or flat buf decoding


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/d49b9ab5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/d49b9ab5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/d49b9ab5

Branch: refs/heads/develop
Commit: d49b9ab5a7693fb20496c786117e6b792686f91f
Parents: 4246a50
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Fri Oct 7 11:54:47 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Fri Oct 7 12:08:33 2016 -0700

----------------------------------------------------------------------
 encoding/tinycbor/include/tinycbor/cbor.h       |  28 +++-
 .../tinycbor/include/tinycbor/cbor_buf_reader.h |  43 ++++++
 .../tinycbor/include/tinycbor/cbor_buf_writer.h |  26 ++--
 .../tinycbor/include/tinycbor/cbor_cnt_writer.h |  26 ++--
 .../include/tinycbor/cbor_mbuf_reader.h         |  40 ++++++
 .../include/tinycbor/cbor_mbuf_writer.h         |  26 ++--
 .../include/tinycbor/extract_number_p.h         |  18 +--
 encoding/tinycbor/src/cbor_buf_reader.c         |  72 ++++++++++
 encoding/tinycbor/src/cbor_mbuf_reader.c        |  85 ++++++++++++
 encoding/tinycbor/src/cborparser.c              | 130 ++++++++++---------
 encoding/tinycbor/src/cborpretty.c              |   4 +-
 encoding/tinycbor/src/cbortojson.c              |   4 +-
 net/oic/src/api/oc_rep.c                        |   6 +-
 13 files changed, 401 insertions(+), 107 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/include/tinycbor/cbor.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor.h b/encoding/tinycbor/include/tinycbor/cbor.h
index 7b8e861..6ec5ce0 100644
--- a/encoding/tinycbor/include/tinycbor/cbor.h
+++ b/encoding/tinycbor/include/tinycbor/cbor.h
@@ -214,9 +214,29 @@ enum CborParserIteratorFlags
     CborIteratorFlag_ContainerIsMap         = 0x20
 };
 
+struct cbor_decoder_reader;
+
+typedef uint8_t (cbor_reader_get8)(struct cbor_decoder_reader *d, int offset);
+typedef uint16_t (cbor_reader_get16)(struct cbor_decoder_reader *d, int offset);
+typedef uint32_t (cbor_reader_get32)(struct cbor_decoder_reader *d, int offset);
+typedef uint64_t (cbor_reader_get64)(struct cbor_decoder_reader *d, int offset);
+typedef uintptr_t (cbor_memcmp)(struct cbor_decoder_reader *d, char *buf, int offset, size_t len);
+typedef uintptr_t (cbor_memcpy)(struct cbor_decoder_reader *d, char *buf, int offset, size_t len);
+
+struct cbor_decoder_reader {
+    cbor_reader_get8  *get8;
+    cbor_reader_get16 *get16;
+    cbor_reader_get32 *get32;
+    cbor_reader_get64 *get64;
+    cbor_memcmp       *cmp;
+    cbor_memcpy       *cpy;
+    size_t             message_size;
+};
+
 struct CborParser
 {
-    const uint8_t *end;
+    struct cbor_decoder_reader *d;
+    int end;
     int flags;
 };
 typedef struct CborParser CborParser;
@@ -224,7 +244,7 @@ typedef struct CborParser CborParser;
 struct CborValue
 {
     const CborParser *parser;
-    const uint8_t *ptr;
+    int offset;
     uint32_t remaining;
     uint16_t extra;
     uint8_t type;
@@ -232,12 +252,10 @@ struct CborValue
 };
 typedef struct CborValue CborValue;
 
-CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
+CBOR_API CborError cbor_parser_init(struct cbor_decoder_reader *d, int flags, CborParser *parser, 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)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/include/tinycbor/cbor_buf_reader.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor_buf_reader.h b/encoding/tinycbor/include/tinycbor/cbor_buf_reader.h
new file mode 100644
index 0000000..a6b6ec8
--- /dev/null
+++ b/encoding/tinycbor/include/tinycbor/cbor_buf_reader.h
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef CBOR_BUF_READER_H
+#define CBOR_BUF_READER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <tinycbor/cbor.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 */
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h b/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h
index 9dbf4da..2f98154 100644
--- a/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h
+++ b/encoding/tinycbor/include/tinycbor/cbor_buf_writer.h
@@ -1,14 +1,20 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
-/* 
- * File:   cbor_buf_writer.h
- * Author: paulfdietrich
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
- * Created on September 30, 2016, 4:38 PM
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
  */
 
 #ifndef CBOR_BUF_WRITER_H

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h b/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h
index e5c4485..1db57e6 100644
--- a/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h
+++ b/encoding/tinycbor/include/tinycbor/cbor_cnt_writer.h
@@ -1,14 +1,20 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
-/* 
- * File:   cbor_cnt_writer.h
- * Author: paulfdietrich
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
- * Created on September 30, 2016, 4:50 PM
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
  */
 
 #ifndef CBOR_CNT_WRITER_H

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/include/tinycbor/cbor_mbuf_reader.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor_mbuf_reader.h b/encoding/tinycbor/include/tinycbor/cbor_mbuf_reader.h
new file mode 100644
index 0000000..128fda9
--- /dev/null
+++ b/encoding/tinycbor/include/tinycbor/cbor_mbuf_reader.h
@@ -0,0 +1,40 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/* 
+ * File:   cbor_mbuf_reader.h
+ * Author: paulfdietrich
+ *
+ * Created on October 5, 2016, 1:19 PM
+ */
+
+#ifndef CBOR_MBUF_READER_H
+#define CBOR_MBUF_READER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <tinycbor/cbor.h>
+#include <os/os_mbuf.h>
+
+struct CborMbufReader {
+    struct cbor_decoder_reader r;
+    int init_off;                     /* initial offset into the data */
+    struct os_mbuf *m;
+};
+
+void
+cbor_mbuf_reader_init(struct CborMbufReader *cb,
+                        struct os_mbuf *m,
+                        int intial_offset);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CBOR_MBUF_READER_H */
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h b/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h
index b5f2c17..1373846 100644
--- a/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h
+++ b/encoding/tinycbor/include/tinycbor/cbor_mbuf_writer.h
@@ -1,14 +1,20 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
-/* 
- * File:   cbor_mbuf_writer.h
- * Author: paulfdietrich
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
- * Created on September 30, 2016, 4:59 PM
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
  */
 
 #ifndef CBOR_MBUF_WRITER_H

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/include/tinycbor/extract_number_p.h
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/include/tinycbor/extract_number_p.h b/encoding/tinycbor/include/tinycbor/extract_number_p.h
index 7d401f1..47db221 100644
--- a/encoding/tinycbor/include/tinycbor/extract_number_p.h
+++ b/encoding/tinycbor/include/tinycbor/extract_number_p.h
@@ -50,10 +50,10 @@ static inline uint64_t get64(const uint8_t *ptr)
     return cbor_ntohll(result);
 }
 
-static inline CborError extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len)
+static inline CborError extract_number(const CborParser *p, int *offset, uint64_t *len)
 {
-    uint8_t additional_information = **ptr & SmallValueMask;
-    ++*ptr;
+    uint8_t additional_information = p->d->get8(p->d, *offset) & SmallValueMask;
+    ++*offset;
     if (additional_information < Value8Bit) {
         *len = additional_information;
         return CborNoError;
@@ -62,18 +62,18 @@ static inline CborError extract_number(const uint8_t **ptr, const uint8_t *end,
         return CborErrorIllegalNumber;
 
     size_t bytesNeeded = (size_t)(1 << (additional_information - Value8Bit));
-    if (unlikely(bytesNeeded > (size_t)(end - *ptr))) {
+    if (unlikely(bytesNeeded > (size_t)(p->end - *offset))) {
         return CborErrorUnexpectedEOF;
     } else if (bytesNeeded == 1) {
-        *len = (uint8_t)(*ptr)[0];
+        *len = p->d->get8(p->d, *offset);
     } else if (bytesNeeded == 2) {
-        *len = get16(*ptr);
+        *len =  p->d->get16(p->d, *offset);
     } else if (bytesNeeded == 4) {
-        *len = get32(*ptr);
+        *len =  p->d->get32(p->d, *offset);
     } else {
-        *len = get64(*ptr);
+        *len =  p->d->get64(p->d, *offset);
     }
-    *ptr += bytesNeeded;
+    *offset += bytesNeeded;
     return CborNoError;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/src/cbor_buf_reader.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cbor_buf_reader.c b/encoding/tinycbor/src/cbor_buf_reader.c
new file mode 100644
index 0000000..f91a742
--- /dev/null
+++ b/encoding/tinycbor/src/cbor_buf_reader.c
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <tinycbor/cbor_buf_reader.h>
+#include <tinycbor/extract_number_p.h>
+
+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];
+}
+
+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);
+}
+
+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;
+}
+
+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_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.message_size = data;
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/src/cbor_mbuf_reader.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cbor_mbuf_reader.c b/encoding/tinycbor/src/cbor_mbuf_reader.c
new file mode 100644
index 0000000..e272487
--- /dev/null
+++ b/encoding/tinycbor/src/cbor_mbuf_reader.c
@@ -0,0 +1,85 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <tinycbor/cbor_mbuf_reader.h>
+#include <tinycbor/compilersupport_p.h>
+#include <os/os_mbuf.h>
+
+static uint8_t
+cbuf_mbuf_reader_get8(struct cbor_decoder_reader *d, int offset) {
+    uint8_t val;
+    struct CborMbufReader *cb = (struct CborMbufReader *) d;
+    os_mbuf_copydata(cb->m, offset, sizeof(val), &val);
+    return val;
+}
+
+static uint16_t
+cbuf_mbuf_reader_get16(struct cbor_decoder_reader *d, int offset) {
+    uint16_t val;
+    struct CborMbufReader *cb = (struct CborMbufReader *) d;
+    os_mbuf_copydata(cb->m, offset + cb->init_off, sizeof(val), &val);
+    return cbor_ntohs(val);
+}
+
+static uint32_t
+cbuf_mbuf_reader_get32(struct cbor_decoder_reader *d, int offset) {
+    uint32_t val;
+    struct CborMbufReader *cb = (struct CborMbufReader *) d;
+    os_mbuf_copydata(cb->m, offset + cb->init_off, sizeof(val), &val);
+    return cbor_ntohl(val);
+}
+
+static uint64_t
+cbuf_mbuf_reader_get64(struct cbor_decoder_reader *d, int offset) {
+    uint64_t val;
+    struct CborMbufReader *cb = (struct CborMbufReader *) d;
+    os_mbuf_copydata(cb->m, offset + cb->init_off, sizeof(val), &val);
+    return cbor_ntohll(val);
+}
+
+static uintptr_t
+cbor_mbuf_reader_cmp(struct cbor_decoder_reader *d, char *buf, int offset, size_t len) {
+    struct CborMbufReader *cb = (struct CborMbufReader *) d;
+    return os_mbuf_cmpf(cb->m, offset + cb->init_off, buf, len);
+}
+
+static uintptr_t
+cbor_mbuf_reader_cpy(struct cbor_decoder_reader *d, char *dst, int offset, size_t len) {
+    struct CborMbufReader *cb = (struct CborMbufReader *) d;
+    return !os_mbuf_copydata(cb->m, offset + cb->init_off, len, dst);
+}
+
+void
+cbor_mbuf_reader_init(struct CborMbufReader *cb, struct os_mbuf *m,
+                        int initial_offset)
+{
+    struct os_mbuf_pkthdr *hdr;
+    cb->r.get8 = &cbuf_mbuf_reader_get8;
+    cb->r.get16 = &cbuf_mbuf_reader_get16;
+    cb->r.get32 = &cbuf_mbuf_reader_get32;
+    cb->r.get64 = &cbuf_mbuf_reader_get64;
+    cb->r.cmp = &cbor_mbuf_reader_cmp;
+    cb->r.cpy = &cbor_mbuf_reader_cpy;
+
+    assert (OS_MBUF_IS_PKTHDR(m));
+    hdr = OS_MBUF_PKTHDR(m);
+    cb->m = m;
+    cb->init_off = initial_offset;
+    cb->r.message_size = hdr->omp_len;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/src/cborparser.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cborparser.c b/encoding/tinycbor/src/cborparser.c
index 1d81091..3b91448 100644
--- a/encoding/tinycbor/src/cborparser.c
+++ b/encoding/tinycbor/src/cborparser.c
@@ -36,6 +36,7 @@
 #include <assert.h>
 #include <string.h>
 
+#include <tinycbor/cbor_buf_reader.h>
 #include "assert_p.h"       /* Always include last */
 
 #ifndef CBOR_PARSER_MAX_RECURSIONS
@@ -146,10 +147,11 @@
  * \endif
  */
 
-static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
+static CborError extract_length(const CborParser *parser,
+                                    int *offset, size_t *len)
 {
     uint64_t v;
-    CborError err = extract_number(ptr, parser->end, &v);
+    CborError err = extract_number(parser, offset, &v);
     if (err) {
         *len = 0;
         return err;
@@ -173,10 +175,10 @@ static CborError preparse_value(CborValue *it)
     it->type = CborInvalidType;
 
     /* are we at the end? */
-    if (it->ptr == parser->end)
+    if (it->offset == parser->end)
         return CborErrorUnexpectedEOF;
 
-    uint8_t descriptor = *it->ptr;
+    uint8_t descriptor = parser->d->get8(parser->d, it->offset);
     uint8_t type = descriptor & MajorTypeMask;
     it->type = type;
     it->flags = 0;
@@ -195,7 +197,7 @@ static CborError preparse_value(CborValue *it)
     }
 
     size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
-    if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
+    if (bytesNeeded + 1 > (size_t)(parser->end - it->offset))
         return CborErrorUnexpectedEOF;
 
     uint8_t majortype = type >> MajorTypeShift;
@@ -217,11 +219,11 @@ static CborError preparse_value(CborValue *it)
         case NullValue:
         case UndefinedValue:
         case HalfPrecisionFloat:
-            it->type = *it->ptr;
+            it->type = parser->d->get8(parser->d, it->offset);
             break;
 
         case SimpleTypeInNextByte:
-            it->extra = (uint8_t)it->ptr[1];
+            it->extra = parser->d->get8(parser->d, it->offset + 1);
 #ifndef CBOR_PARSER_NO_STRICT_CHECKS
             if (unlikely(it->extra < 32)) {
                 it->type = CborInvalidType;
@@ -245,9 +247,9 @@ static CborError preparse_value(CborValue *it)
         return CborNoError;
 
     if (descriptor == Value8Bit)
-        it->extra = (uint8_t)it->ptr[1];
+        it->extra = parser->d->get8(parser->d, it->offset + 1);
     else if (descriptor == Value16Bit)
-        it->extra = get16(it->ptr + 1);
+        it->extra = parser->d->get16(parser->d, it->offset + 1);
     else
         it->flags |= CborIteratorFlag_IntegerValueTooLarge;     /* Value32Bit or Value64Bit */
     return CborNoError;
@@ -261,9 +263,10 @@ static CborError preparse_next_value(CborValue *it)
             it->type = CborInvalidType;
             return CborNoError;
         }
-    } else if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
+    } else if (it->remaining == UINT32_MAX && it->offset != it->parser->end &&
+        it->parser->d->get8(it->parser->d, it->offset) == (uint8_t)BreakByte) {
         /* end of map or array */
-        ++it->ptr;
+        ++it->offset;
         it->type = CborInvalidType;
         it->remaining = 0;
         return CborNoError;
@@ -275,13 +278,13 @@ static CborError preparse_next_value(CborValue *it)
 static CborError advance_internal(CborValue *it)
 {
     uint64_t length;
-    CborError err = extract_number(&it->ptr, it->parser->end, &length);
+    CborError err = extract_number(it->parser, &it->offset,  &length);
     assert(err == CborNoError);
 
     if (it->type == CborByteStringType || it->type == CborTextStringType) {
         assert(length == (size_t)length);
         assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
-        it->ptr += length;
+        it->offset += length;
     }
 
     return preparse_next_value(it);
@@ -299,17 +302,19 @@ static CborError advance_internal(CborValue *it)
  */
 uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
 {
+    uint8_t val = value->parser->d->get8(value->parser->d, value->offset);
+
     assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
            value->type == CborFloatType || value->type == CborDoubleType);
 
     /* since the additional information can only be Value32Bit or Value64Bit,
      * we just need to test for the one bit those two options differ */
-    assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
-    if ((*value->ptr & 1) == (Value32Bit & 1))
-        return get32(value->ptr + 1);
+    assert((val & SmallValueMask) == Value32Bit || (val & SmallValueMask) == Value64Bit);
+    if ((val & 1) == (Value32Bit & 1))
+        return value->parser->d->get32(value->parser->d, value->offset + 1);
 
-    assert((*value->ptr & SmallValueMask) == Value64Bit);
-    return get64(value->ptr + 1);
+    assert((val & SmallValueMask) == Value64Bit);
+        return value->parser->d->get64(value->parser->d, value->offset + 1);
 }
 
 /**
@@ -322,14 +327,16 @@ uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
  * threads iterating at the same time, but the object can be copied so multiple
  * threads can iterate.
  */
-CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
+CborError cbor_parser_init(struct cbor_decoder_reader *d, int flags,
+                                CborParser *parser, CborValue *it)
 {
     memset(parser, 0, sizeof(*parser));
-    parser->end = buffer + size;
+    parser->d = d;
+    parser->end = d->message_size;
     parser->flags = flags;
     it->parser = parser;
-    it->ptr = buffer;
-    it->remaining = 1;      /* there's one type altogether, usually an array or map */
+    it->offset = 0;
+    it->remaining = 1;/* there's one type altogether, usually an array or map */
     return preparse_value(it);
 }
 
@@ -510,29 +517,29 @@ CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
 
     if (it->flags & CborIteratorFlag_UnknownLength) {
         recursed->remaining = UINT32_MAX;
-        ++recursed->ptr;
+        ++recursed->offset;
         err = preparse_value(recursed);
         if (err != CborErrorUnexpectedBreak)
             return err;
         /* actually, break was expected here
          * it's just an empty container */
-        ++recursed->ptr;
+        ++recursed->offset;
     } else {
         uint64_t len;
-        err = extract_number(&recursed->ptr, recursed->parser->end, &len);
+        err = extract_number(recursed->parser, &recursed->offset, &len);
         assert(err == CborNoError);
 
         recursed->remaining = (uint32_t)len;
         if (recursed->remaining != len || len == UINT32_MAX) {
             /* back track the pointer to indicate where the error occurred */
-            recursed->ptr = it->ptr;
+            recursed->offset = it->offset;
             return CborErrorDataTooLarge;
         }
         if (recursed->type == CborMapType) {
             /* maps have keys and values, so we need to multiply by 2 */
             if (recursed->remaining > UINT32_MAX / 2) {
                 /* back track the pointer to indicate where the error occurred */
-                recursed->ptr = it->ptr;
+                recursed->offset = it->offset;
                 return CborErrorDataTooLarge;
             }
             recursed->remaining *= 2;
@@ -563,7 +570,7 @@ CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
 {
     assert(cbor_value_is_container(it));
     assert(recursed->type == CborInvalidType);
-    it->ptr = recursed->ptr;
+    it->offset = recursed->offset;
     return preparse_next_value(it);
 }
 
@@ -900,21 +907,17 @@ CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len
  * function. The choice is to optimize for memcpy, which is used in the base
  * parser API (cbor_value_copy_string), while memcmp is used in convenience API
  * only. */
-typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
+typedef uintptr_t (*IterateFunction)(struct cbor_decoder_reader *d, char *dst, int src_offset, size_t len);
 
-static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
+static uintptr_t iterate_noop(struct cbor_decoder_reader *d, char *dst, int src_offset,  size_t len)
 {
-    (void)dest;
-    (void)src;
+    (void)d;
+    (void)dst;
+    (void)src_offset;
     (void)len;
     return true;
 }
 
-static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
-{
-    return memcmp(s1, (const char *)s2, len) == 0;
-}
-
 static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
                                        bool *result, CborValue *next, IterateFunction func)
 {
@@ -922,68 +925,77 @@ static CborError iterate_string_chunks(const CborValue *value, char *buffer, siz
 
     size_t total;
     CborError err;
-    const uint8_t *ptr = value->ptr;
+    int offset = value->offset;
     if (cbor_value_is_length_known(value)) {
         /* easy case: fixed length */
-        err = extract_length(value->parser, &ptr, &total);
+        err = extract_length(value->parser, &offset, &total);
         if (err)
             return err;
-        if (total > (size_t)(value->parser->end - ptr))
+        if (total > (size_t)(value->parser->end - offset))
             return CborErrorUnexpectedEOF;
         if (total <= *buflen)
-            *result = !!func(buffer, ptr, total);
+            *result = !!func(value->parser->d, buffer, offset, total);
         else
             *result = false;
-        ptr += total;
+        offset += total;
     } else {
         /* chunked */
-        ++ptr;
+        ++offset;
         total = 0;
         *result = true;
         while (true) {
+            uint8_t val;
             size_t chunkLen;
             size_t newTotal;
 
-            if (ptr == value->parser->end)
+            if (offset == value->parser->end)
                 return CborErrorUnexpectedEOF;
 
-            if (*ptr == (uint8_t)BreakByte) {
-                ++ptr;
+            val = value->parser->d->get8(value->parser->d, offset);
+
+            if (val == (uint8_t)BreakByte) {
+                ++offset;
                 break;
             }
 
             /* is this the right type? */
-            if ((*ptr & MajorTypeMask) != value->type)
+            if ((val & MajorTypeMask) != value->type)
                 return CborErrorIllegalType;
 
-            err = extract_length(value->parser, &ptr, &chunkLen);
+            err = extract_length(value->parser, &offset, &chunkLen);
             if (err)
                 return err;
 
             if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
                 return CborErrorDataTooLarge;
 
-            if (chunkLen > (size_t)(value->parser->end - ptr))
+            if (chunkLen > (size_t)(value->parser->end - offset))
                 return CborErrorUnexpectedEOF;
 
             if (*result && *buflen >= newTotal)
-                *result = !!func(buffer + total, ptr, chunkLen);
+                *result = !!func(value->parser->d, buffer + total, offset, chunkLen);
             else
                 *result = false;
 
-            ptr += chunkLen;
+            offset += chunkLen;
             total = newTotal;
         }
     }
 
     /* is there enough room for the ending NUL byte? */
-    if (*result && *buflen > total)
-        *result = !!func(buffer + total, (const uint8_t *)"", 1);
+    if (*result && *buflen > total) {
+        /* we are just trying to write a NULL byte here, but we have
+         * to create this decoder since we may be calling different
+         * functions through the pointer below */
+        struct cbor_buf_reader cb;
+        cbor_buf_reader_init(&cb, (const uint8_t *) "", 1);
+        *result = !!func(&cb.r, buffer + total, 0, 1);
+    }
     *buflen = total;
 
     if (next) {
         *next = *value;
-        next->ptr = ptr;
+        next->offset = offset;
         return preparse_next_value(next);
     }
     return CborNoError;
@@ -1059,7 +1071,7 @@ CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
 {
     bool copied_all;
     CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
-                                          buffer ? (IterateFunction)memcpy : iterate_noop);
+                                          buffer ? (IterateFunction) value->parser->d->cpy : iterate_noop);
     return err ? err :
                  copied_all ? CborNoError : CborErrorOutOfMemory;
 }
@@ -1094,7 +1106,8 @@ CborError cbor_value_text_string_equals(const CborValue *value, const char *stri
     }
 
     size_t len = strlen(string);
-    return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
+    return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len,
+                                 result, NULL, value->parser->d->cmp);
 }
 
 /**
@@ -1186,7 +1199,7 @@ CborError cbor_value_map_find_value(const CborValue *map, const char *string, Cb
             bool equals;
             size_t dummyLen = len;
             err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
-                                        &equals, element, iterate_memcmp);
+                                        &equals, element, map->parser->d->cmp);
             if (err)
                 goto error;
             if (equals)
@@ -1285,9 +1298,10 @@ CborError cbor_value_get_half_float(const CborValue *value, void *result)
     assert(cbor_value_is_half_float(value));
 
     /* size has been computed already */
-    uint16_t v = get16(value->ptr + 1);
+    uint16_t v = value->parser->d->get16(value->parser->d, value->offset + 1);
     memcpy(result, &v, sizeof(v));
     return CborNoError;
 }
 
+
 /** @} */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/src/cborpretty.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cborpretty.c b/encoding/tinycbor/src/cborpretty.c
index e8e7316..3587703 100644
--- a/encoding/tinycbor/src/cborpretty.c
+++ b/encoding/tinycbor/src/cborpretty.c
@@ -282,12 +282,12 @@ static CborError value_to_pretty(FILE *out, CborValue *it)
 
         err = cbor_value_enter_container(it, &recursed);
         if (err) {
-            it->ptr = recursed.ptr;
+            it->offset = recursed.offset;
             return err;       /* parse error */
         }
         err = container_to_pretty(out, &recursed, type);
         if (err) {
-            it->ptr = recursed.ptr;
+            it->offset = recursed.offset;
             return err;       /* parse error */
         }
         err = cbor_value_leave_container(it, &recursed);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/encoding/tinycbor/src/cbortojson.c
----------------------------------------------------------------------
diff --git a/encoding/tinycbor/src/cbortojson.c b/encoding/tinycbor/src/cbortojson.c
index 953f2aa..5efa486 100644
--- a/encoding/tinycbor/src/cbortojson.c
+++ b/encoding/tinycbor/src/cbortojson.c
@@ -499,7 +499,7 @@ static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType typ
         CborValue recursed;
         err = cbor_value_enter_container(it, &recursed);
         if (err) {
-            it->ptr = recursed.ptr;
+            it->offset = recursed.offset;
             return err;       /* parse error */
         }
         if (fputc(type == CborArrayType ? '[' : '{', out) < 0)
@@ -509,7 +509,7 @@ static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType typ
                   array_to_json(out, &recursed, flags, status) :
                   map_to_json(out, &recursed, flags, status);
         if (err) {
-            it->ptr = recursed.ptr;
+            it->offset = recursed.offset;
             return err;       /* parse error */
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d49b9ab5/net/oic/src/api/oc_rep.c
----------------------------------------------------------------------
diff --git a/net/oic/src/api/oc_rep.c b/net/oic/src/api/oc_rep.c
index 1c96bf3..a490840 100644
--- a/net/oic/src/api/oc_rep.c
+++ b/net/oic/src/api/oc_rep.c
@@ -20,6 +20,7 @@
 #include "port/oc_log.h"
 #include "util/oc_memb.h"
 #include <tinycbor/cbor_buf_writer.h>
+#include <tinycbor/cbor_buf_reader.h>
 
 OC_MEMB(rep_objects, oc_rep_t, EST_NUM_REP_OBJECTS);
 static const CborEncoder g_empty;
@@ -276,7 +277,10 @@ oc_parse_rep(const uint8_t *in_payload, uint16_t payload_size,
   CborParser parser;
   CborValue root_value, cur_value, map;
   CborError err = CborNoError;
-  err |= cbor_parser_init(in_payload, payload_size, 0, &parser, &root_value);
+  struct cbor_buf_reader br;
+
+  cbor_buf_reader_init(&br, in_payload, payload_size);
+  err |= cbor_parser_init(&br.r, 0, &parser, &root_value);
   if (cbor_value_is_map(&root_value)) {
     err |= cbor_value_enter_container(&root_value, &cur_value);
     *out_rep = 0;