1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include "os/mynewt.h"
21 #include <tinycbor/cbor_mbuf_reader.h>
22 #include <tinycbor/compilersupport_p.h>
23 
24 static uint8_t
cbor_mbuf_reader_get8(struct cbor_decoder_reader * d,int offset)25 cbor_mbuf_reader_get8(struct cbor_decoder_reader *d, int offset)
26 {
27     uint8_t val;
28     struct cbor_mbuf_reader *cb = (struct cbor_mbuf_reader *) d;
29 
30     os_mbuf_copydata(cb->m, offset + cb->init_off, sizeof(val), &val);
31     return val;
32 }
33 
34 static uint16_t
cbor_mbuf_reader_get16(struct cbor_decoder_reader * d,int offset)35 cbor_mbuf_reader_get16(struct cbor_decoder_reader *d, int offset)
36 {
37     uint16_t val;
38     struct cbor_mbuf_reader *cb = (struct cbor_mbuf_reader *) d;
39 
40     os_mbuf_copydata(cb->m, offset + cb->init_off, sizeof(val), &val);
41     return cbor_ntohs(val);
42 }
43 
44 static uint32_t
cbor_mbuf_reader_get32(struct cbor_decoder_reader * d,int offset)45 cbor_mbuf_reader_get32(struct cbor_decoder_reader *d, int offset)
46 {
47     uint32_t val;
48     struct cbor_mbuf_reader *cb = (struct cbor_mbuf_reader *) d;
49 
50     os_mbuf_copydata(cb->m, offset + cb->init_off, sizeof(val), &val);
51     return cbor_ntohl(val);
52 }
53 
54 static uint64_t
cbor_mbuf_reader_get64(struct cbor_decoder_reader * d,int offset)55 cbor_mbuf_reader_get64(struct cbor_decoder_reader *d, int offset)
56 {
57     uint64_t val;
58     struct cbor_mbuf_reader *cb = (struct cbor_mbuf_reader *) d;
59 
60     os_mbuf_copydata(cb->m, offset + cb->init_off, sizeof(val), &val);
61     return cbor_ntohll(val);
62 }
63 
64 static uintptr_t
cbor_mbuf_reader_cmp(struct cbor_decoder_reader * d,char * buf,int offset,size_t len)65 cbor_mbuf_reader_cmp(struct cbor_decoder_reader *d, char *buf, int offset,
66                      size_t len)
67 {
68     struct cbor_mbuf_reader *cb = (struct cbor_mbuf_reader *) d;
69     return os_mbuf_cmpf(cb->m, offset + cb->init_off, buf, len) == 0;
70 }
71 
72 static uintptr_t
cbor_mbuf_reader_cpy(struct cbor_decoder_reader * d,char * dst,int offset,size_t len)73 cbor_mbuf_reader_cpy(struct cbor_decoder_reader *d, char *dst, int offset,
74                      size_t len)
75 {
76     int rc;
77     struct cbor_mbuf_reader *cb = (struct cbor_mbuf_reader *) d;
78 
79     rc = os_mbuf_copydata(cb->m, offset + cb->init_off, len, dst);
80     if (rc == 0) {
81         return true;
82     }
83     return false;
84 }
85 
86 void
cbor_mbuf_reader_init(struct cbor_mbuf_reader * cb,struct os_mbuf * m,int initial_offset)87 cbor_mbuf_reader_init(struct cbor_mbuf_reader *cb, struct os_mbuf *m,
88                       int initial_offset)
89 {
90     struct os_mbuf_pkthdr *hdr;
91 
92     cb->r.get8 = &cbor_mbuf_reader_get8;
93     cb->r.get16 = &cbor_mbuf_reader_get16;
94     cb->r.get32 = &cbor_mbuf_reader_get32;
95     cb->r.get64 = &cbor_mbuf_reader_get64;
96     cb->r.cmp = &cbor_mbuf_reader_cmp;
97     cb->r.cpy = &cbor_mbuf_reader_cpy;
98 
99     assert(OS_MBUF_IS_PKTHDR(m));
100     hdr = OS_MBUF_PKTHDR(m);
101     cb->m = m;
102     cb->init_off = initial_offset;
103     cb->r.message_size = hdr->omp_len - initial_offset;
104 }
105