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:24:00 UTC

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

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;