Mbed OS Reference
Loading...
Searching...
No Matches
ac_buffer_reader.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, ARM Limited, All Rights Reserved
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/**
18 * \file ac_buffer_reader.h
19 * \copyright Copyright (c) ARM Ltd 2015
20 * \author Donatien Garnier
21 */
22
23/** \addtogroup ACore
24 * @{
25 * \name Buffer Reader
26 * @{
27 */
28
29#ifndef ACORE_BUFFER_READER_H_
30#define ACORE_BUFFER_READER_H_
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36#include "stdint.h"
37#include "stddef.h"
38#include "stdbool.h"
39
40#include "acore/ac_buffer.h"
41
42/** Read n-bytes in big-endian format from buffer reader and advance read posiion
43 * \param pBuf the buffer to read from
44 * \param buf the array to write to
45 * \param size the number of bytes to read
46 */
47void ac_buffer_read_be(ac_buffer_t *pBuf, uint8_t *buf, size_t size);
48
49/** Read n-bytes in little-endian format from buffer reader and advance read posiion
50 * \param pBuf the buffer to read from
51 * \param buf the array to write to
52 * \param size the number of bytes to read
53 */
54void ac_buffer_read_le(ac_buffer_t *pBuf, uint8_t *buf, size_t size);
55
56/** Read 8-bit value from buffer reader and advance read posiion
57 * \param pBuf the buffer to read from
58 * \return 8-bit value read
59 */
60static inline uint8_t ac_buffer_read_nu8(ac_buffer_t *pBuf)
61{
62 uint8_t hu8;
63 ac_buffer_read_be(pBuf, &hu8, 1);
64 return hu8;
65}
66
67/** Read BE 16-bit value from buffer reader and advance read posiion
68 * \param pBuf the buffer to read from
69 * \return 16-bit value read
70 */
71static inline uint16_t ac_buffer_read_nu16(ac_buffer_t *pBuf)
72{
73 uint16_t hu16;
74 ac_buffer_read_be(pBuf, (uint8_t *)&hu16, 2);
75 return hu16;
76}
77
78/** Read BE 24-bit value from buffer reader and advance read posiion
79 * \param pBuf the buffer to read from
80 * \return 24-bit value read
81 */
82static inline uint32_t ac_buffer_read_nu24(ac_buffer_t *pBuf)
83{
84 uint32_t hu24;
85 ac_buffer_read_be(pBuf, (uint8_t *)&hu24, 3);
86 return hu24;
87}
88
89/** Read BE 32-bit value from buffer reader and advance read posiion
90 * \param pBuf the buffer to read from
91 * \return 32-bit value read
92 */
93static inline uint32_t ac_buffer_read_nu32(ac_buffer_t *pBuf)
94{
95 uint32_t hu32;
96 ac_buffer_read_be(pBuf, (uint8_t *)&hu32, 4);
97 return hu32;
98}
99
100/** Read BE 64-bit value from buffer reader and advance read posiion
101 * \param pBuf the buffer to read from
102 * \return 64-bit value read
103 */
104static inline uint64_t ac_buffer_read_nu64(ac_buffer_t *pBuf)
105{
106 uint64_t hu64;
107 ac_buffer_read_be(pBuf, (uint8_t *)&hu64, 8);
108 return hu64;
109}
110
111/** Read n bytes from buffer reader and advance read posiion
112 * \param pBuf the buffer to read from
113 * \param data the array to write bytes to
114 * \param size the number of bytes to read
115 */
116static inline void ac_buffer_read_n_bytes(ac_buffer_t *pBuf, uint8_t *data, size_t size)
117{
118 ac_buffer_read_le(pBuf, data, size);
119}
120
121/** Skip n bytes from buffer reader and advance read posiion
122 * \param pBuf the buffer to read from
123 * \param size the number of bytes to skip
124 */
125void ac_buffer_read_n_skip(ac_buffer_t *pBuf, size_t size);
126
127/** Get number of bytes readable from buffer
128 * \param pBuf the buffer to read from
129 * \return The number of bytes which can be read
130 */
132
133/** Get a pointer to the current position within this buffer's current backing array
134 * \param pBuf the buffer to read from
135 * \return A pointer to the current position within the current backing array
136 */
138
139/** Get the number of bytes readable within the current backing array
140 * \param pBuf the buffer to read from
141 * \return The number of bytes readable within the current backing array
142 */
144
145/** Compare buffer with array (does not advance read position)
146 * \param pBuf the buffer to compare from
147 * \param bytes the array to compare with
148 * \param length the array length
149 * \return Whether the buffer is AT LEAST as long as the array AND the buffer and array have the same content
150 */
151bool ac_buffer_reader_cmp_bytes(const ac_buffer_t *pBuf, const uint8_t *bytes, size_t length);
152
153/** Compare buffer with array (does not advance read position)
154 * \param pBuf1 the buffer to compare from
155 * \param pBuf2 the buffer to compare with
156 * \return Whether the buffers have the same length and content
157 */
158bool ac_buffer_reader_cmp(const ac_buffer_t *pBuf1, const ac_buffer_t *pBuf2);
159
160#ifdef __cplusplus
161}
162#endif
163
164#endif /* CORE_ac_buffer_READER_H_ */
165
166/**
167 * @}
168 * @}
169 * */
const uint8_t * ac_buffer_reader_current_buffer_pointer(ac_buffer_t *pBuf)
Get a pointer to the current position within this buffer's current backing array.
bool ac_buffer_reader_cmp_bytes(const ac_buffer_t *pBuf, const uint8_t *bytes, size_t length)
Compare buffer with array (does not advance read position)
bool ac_buffer_reader_cmp(const ac_buffer_t *pBuf1, const ac_buffer_t *pBuf2)
Compare buffer with array (does not advance read position)
size_t ac_buffer_reader_readable(const ac_buffer_t *pBuf)
Get number of bytes readable from buffer.
void ac_buffer_read_le(ac_buffer_t *pBuf, uint8_t *buf, size_t size)
Read n-bytes in little-endian format from buffer reader and advance read posiion.
void ac_buffer_read_be(ac_buffer_t *pBuf, uint8_t *buf, size_t size)
Read n-bytes in big-endian format from buffer reader and advance read posiion.
void ac_buffer_read_n_skip(ac_buffer_t *pBuf, size_t size)
Skip n bytes from buffer reader and advance read posiion.
size_t ac_buffer_reader_current_buffer_length(ac_buffer_t *pBuf)
Get the number of bytes readable within the current backing array.