|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** Copyright (C) 2016 Intel Corporation |
| 4 | +** |
| 5 | +** Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +** of this software and associated documentation files (the "Software"), to deal |
| 7 | +** in the Software without restriction, including without limitation the rights |
| 8 | +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +** copies of the Software, and to permit persons to whom the Software is |
| 10 | +** furnished to do so, subject to the following conditions: |
| 11 | +** |
| 12 | +** The above copyright notice and this permission notice shall be included in |
| 13 | +** all copies or substantial portions of the Software. |
| 14 | +** |
| 15 | +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +** THE SOFTWARE. |
| 22 | +** |
| 23 | +****************************************************************************/ |
| 24 | + |
| 25 | +#include "cbor_buf_reader.h" |
| 26 | +#include "compilersupport_p.h" |
| 27 | + |
| 28 | +/** |
| 29 | + * \addtogroup CborParsing |
| 30 | + * @{ |
| 31 | + */ |
| 32 | + |
| 33 | +/** |
| 34 | + * Gets 16 bit unsigned value from the passed in ptr location, it also |
| 35 | + * converts it to host byte order |
| 36 | + */ |
| 37 | +CBOR_INLINE_API uint16_t get16(const uint8_t *ptr) |
| 38 | +{ |
| 39 | + uint16_t result; |
| 40 | + memcpy(&result, ptr, sizeof(result)); |
| 41 | + return cbor_ntohs(result); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Gets 32 bit unsigned value from the passed in ptr location, it also |
| 46 | + * converts it to host byte order |
| 47 | + */ |
| 48 | +CBOR_INLINE_API uint32_t get32(const uint8_t *ptr) |
| 49 | +{ |
| 50 | + uint32_t result; |
| 51 | + memcpy(&result, ptr, sizeof(result)); |
| 52 | + return cbor_ntohl(result); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Gets 64 bit unsigned value from the passed in ptr location, it also |
| 57 | + * converts it to host byte order |
| 58 | + */ |
| 59 | +CBOR_INLINE_API uint64_t get64(const uint8_t *ptr) |
| 60 | +{ |
| 61 | + uint64_t result; |
| 62 | + memcpy(&result, ptr, sizeof(result)); |
| 63 | + return cbor_ntohll(result); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Gets a string chunk from the passed in ptr location |
| 68 | + */ |
| 69 | +CBOR_INLINE_API uintptr_t get_string_chunk(const uint8_t *ptr) |
| 70 | +{ |
| 71 | + return (uintptr_t)ptr; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Gets 8 bit unsigned value using the buffer pointed to by the |
| 76 | + * decoder reader from passed in offset |
| 77 | + */ |
| 78 | +static uint8_t |
| 79 | +cbuf_buf_reader_get8(struct cbor_decoder_reader *d, int offset) |
| 80 | +{ |
| 81 | + struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d; |
| 82 | + return cb->buffer[offset]; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Gets 16 bit unsigned value using the buffer pointed to by the |
| 87 | + * decoder reader from passed in offset |
| 88 | + */ |
| 89 | +static uint16_t |
| 90 | +cbuf_buf_reader_get16(struct cbor_decoder_reader *d, int offset) |
| 91 | +{ |
| 92 | + struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d; |
| 93 | + return get16(cb->buffer + offset); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Gets 32 bit unsigned value using the buffer pointed to by the |
| 98 | + * decoder reader from passed in offset |
| 99 | + */ |
| 100 | +static uint32_t |
| 101 | +cbuf_buf_reader_get32(struct cbor_decoder_reader *d, int offset) |
| 102 | +{ |
| 103 | + uint32_t val; |
| 104 | + struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d; |
| 105 | + val = get32(cb->buffer + offset); |
| 106 | + return val; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Gets 64 bit unsigned value using the buffer pointed to by the |
| 111 | + * decoder reader from passed in offset |
| 112 | + */ |
| 113 | +static uint64_t |
| 114 | +cbuf_buf_reader_get64(struct cbor_decoder_reader *d, int offset) |
| 115 | +{ |
| 116 | + struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d; |
| 117 | + return get64(cb->buffer + offset); |
| 118 | +} |
| 119 | + |
| 120 | +static uintptr_t |
| 121 | +cbor_buf_reader_get_string_chunk(struct cbor_decoder_reader *d, |
| 122 | + int offset, size_t *len) |
| 123 | +{ |
| 124 | + struct cbor_buf_reader *cb = (struct cbor_buf_reader *)d; |
| 125 | + |
| 126 | + (void)*len; |
| 127 | + |
| 128 | + return get_string_chunk(cb->buffer + offset); |
| 129 | +} |
| 130 | + |
| 131 | +static uintptr_t |
| 132 | +cbor_buf_reader_cmp(struct cbor_decoder_reader *d, char *dst, int src_offset, |
| 133 | + size_t len) |
| 134 | +{ |
| 135 | + struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d; |
| 136 | + |
| 137 | + return !memcmp(dst, cb->buffer + src_offset, len); |
| 138 | +} |
| 139 | + |
| 140 | +static uintptr_t |
| 141 | +cbor_buf_reader_cpy(struct cbor_decoder_reader *d, char *dst, int src_offset, |
| 142 | + size_t len) |
| 143 | +{ |
| 144 | + struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d; |
| 145 | + return (uintptr_t) memcpy(dst, cb->buffer + src_offset, len); |
| 146 | +} |
| 147 | + |
| 148 | +void |
| 149 | +cbor_buf_reader_init(struct cbor_buf_reader *cb, const uint8_t *buffer, |
| 150 | + size_t data) |
| 151 | +{ |
| 152 | + cb->buffer = buffer; |
| 153 | + cb->r.get8 = &cbuf_buf_reader_get8; |
| 154 | + cb->r.get16 = &cbuf_buf_reader_get16; |
| 155 | + cb->r.get32 = &cbuf_buf_reader_get32; |
| 156 | + cb->r.get64 = &cbuf_buf_reader_get64; |
| 157 | + cb->r.cmp = &cbor_buf_reader_cmp; |
| 158 | + cb->r.cpy = &cbor_buf_reader_cpy; |
| 159 | + cb->r.get_string_chunk = &cbor_buf_reader_get_string_chunk; |
| 160 | + cb->r.message_size = data; |
| 161 | +} |
| 162 | + |
| 163 | +/** @} */ |
0 commit comments