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 <tinycbor/cbor_buf_reader.h>
21 #include <tinycbor/extract_number_p.h>
22 
23 static uint8_t
cbuf_buf_reader_get8(struct cbor_decoder_reader * d,int offset)24 cbuf_buf_reader_get8(struct cbor_decoder_reader *d, int offset)
25 {
26     struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
27     return cb->buffer[offset];
28 }
29 
30 static uint16_t
cbuf_buf_reader_get16(struct cbor_decoder_reader * d,int offset)31 cbuf_buf_reader_get16(struct cbor_decoder_reader *d, int offset)
32 {
33     struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
34     return get16(cb->buffer + offset);
35 }
36 
37 static uint32_t
cbuf_buf_reader_get32(struct cbor_decoder_reader * d,int offset)38 cbuf_buf_reader_get32(struct cbor_decoder_reader *d, int offset)
39 {
40     uint32_t val;
41     struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
42     val = get32(cb->buffer + offset);
43     return val;
44 }
45 
46 static uint64_t
cbuf_buf_reader_get64(struct cbor_decoder_reader * d,int offset)47 cbuf_buf_reader_get64(struct cbor_decoder_reader *d, int offset)
48 {
49     struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
50     return get64(cb->buffer + offset);
51 }
52 
53 static uintptr_t
cbor_buf_reader_cmp(struct cbor_decoder_reader * d,char * dst,int src_offset,size_t len)54 cbor_buf_reader_cmp(struct cbor_decoder_reader *d, char *dst, int src_offset,
55                     size_t len)
56 {
57     struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
58     return memcmp(dst, cb->buffer + src_offset, len) == 0;
59 }
60 
61 static uintptr_t
cbor_buf_reader_cpy(struct cbor_decoder_reader * d,char * dst,int src_offset,size_t len)62 cbor_buf_reader_cpy(struct cbor_decoder_reader *d, char *dst, int src_offset,
63                     size_t len)
64 {
65     struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
66     return (uintptr_t) memcpy(dst, cb->buffer + src_offset, len);
67 }
68 
69 void
cbor_buf_reader_init(struct cbor_buf_reader * cb,const uint8_t * buffer,size_t data)70 cbor_buf_reader_init(struct cbor_buf_reader *cb, const uint8_t *buffer,
71                      size_t data)
72 {
73     cb->buffer = buffer;
74     cb->r.get8 = &cbuf_buf_reader_get8;
75     cb->r.get16 = &cbuf_buf_reader_get16;
76     cb->r.get32 = &cbuf_buf_reader_get32;
77     cb->r.get64 = &cbuf_buf_reader_get64;
78     cb->r.cmp = &cbor_buf_reader_cmp;
79     cb->r.cpy = &cbor_buf_reader_cpy;
80     cb->r.message_size = data;
81 }
82