Mbed OS Reference
Loading...
Searching...
No Matches
ac_buffer_builder.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_builder.h
19 * \copyright Copyright (c) ARM Ltd 2017
20 * \author Donatien Garnier
21 */
22
23/** \addtogroup ACore
24 * @{
25 * \name Buffer Builder
26 * @{
27 */
28
29#ifndef ACORE_BUFFER_BUILDER_H_
30#define ACORE_BUFFER_BUILDER_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
42typedef struct __ac_buffer_builder {
43 ac_buffer_t ac_buffer;
44 uint8_t *data;
45 size_t size;
47
48/** Write data to big endian ac_buffer (on a LE architecture, byte order will be swapped)
49 * \param pBuilder ac_buffer builder to use
50 * \param buf pointer to data
51 * \param size the data size
52 */
53void ac_buffer_builder_write_be(ac_buffer_builder_t *pBuilder, const uint8_t *buf, size_t size);
54
55/** Write data to little endian ac_buffer (on a LE architecture, byte order will be preserved)
56 * \param pBuilder ac_buffer builder to use
57 * \param buf pointer to data
58 * \param size the data size
59 */
60void ac_buffer_builder_write_le(ac_buffer_builder_t *pBuilder, const uint8_t *buf, size_t size);
61
62/** Write data to big endian ac_buffer at specific position (on a LE architecture, byte order will be swapped)
63 * \param pBuilder ac_buffer builder to use
64 * \param pos position in ac_buffer to write from
65 * \param buf pointer to data
66 * \param size the data size
67 */
68void ac_buffer_builder_write_be_at(ac_buffer_builder_t *pBuilder, size_t pos, const uint8_t *buf, size_t size);
69
70/** Write data to little endian ac_buffer at specific position (on a LE architecture, byte order will be preserved)
71 * \param pBuilder ac_buffer builder to use
72 * \param pos position in ac_buffer to write from
73 * \param buf pointer to data
74 * \param size the data size
75 */
76void ac_buffer_builder_write_le_at(ac_buffer_builder_t *pBuilder, size_t pos, const uint8_t *buf, size_t size);
77
78/** Initialize ac_buffer builder
79 * \param pBuilder ac_buffer builder to init
80 * \param data pointer to byte array to use
81 * \param size of byte array
82 */
83void ac_buffer_builder_init(ac_buffer_builder_t *pBuilder, uint8_t *data, size_t size);
84
85/** Initialize ac_buffer builder from underlying ac_buffer
86 * \param pBuilder ac_buffer builder to init
87 */
89
90/** Reset ac_buffer builder
91 * \param pBuilder ac_buffer builder to reset
92 */
94
95/** Set ac_buffer builder's ac_buffer to full size
96 * \param pBuilder ac_buffer builder to set to full size
97 */
99
100/** Get ac_buffer builder's length
101 * \param pBuilder ac_buffer builder to get length of
102 * \return number of valid bytes in ac_buffer
103 */
104static inline size_t ac_buffer_builder_length(ac_buffer_builder_t *pBuilder)
105{
106 return ac_buffer_size(&pBuilder->ac_buffer);
107}
108
109/** Set ac_buffer builder's length
110 * \param pBuilder ac_buffer builder to set length of
111 * \param length number of valid bytes in ac_buffer
112 */
113static inline void ac_buffer_builder_set_length(ac_buffer_builder_t *pBuilder, size_t length)
114{
115 if (ac_buffer_data(&pBuilder->ac_buffer) + length > pBuilder->data + pBuilder->size) {
116 return;
117 }
118 pBuilder->ac_buffer.size = length;
119}
120
121/** Get ac_buffer builder's pointer to write position
122 * \param pBuilder ac_buffer builder
123 * \return pointer to write position
124 */
125static inline uint8_t *ac_buffer_builder_write_position(ac_buffer_builder_t *pBuilder)
126{
127 return (uint8_t *)ac_buffer_data(&pBuilder->ac_buffer) + ac_buffer_size(&pBuilder->ac_buffer);
128}
129
130/** Get ac_buffer builder's write offset
131 * \param pBuilder ac_buffer builder
132 * \return write offset
133 */
134static inline size_t ac_buffer_builder_write_offset(ac_buffer_builder_t *pBuilder)
135{
136 return ac_buffer_data(&pBuilder->ac_buffer) + ac_buffer_size(&pBuilder->ac_buffer) - pBuilder->data;
137}
138
139/** Set ac_buffer builder's write offset
140 * \param pBuilder ac_buffer builder
141 * \param off new write offset
142 */
143static inline void ac_buffer_builder_set_write_offset(ac_buffer_builder_t *pBuilder, size_t off)
144{
145 if (off > pBuilder->size) {
146 return;
147 }
148 if (pBuilder->data + off > pBuilder->ac_buffer.data) {
149 pBuilder->ac_buffer.size = off - (pBuilder->ac_buffer.data - pBuilder->data);
150 } else {
151 pBuilder->ac_buffer.size = 0;
152 }
153}
154
155/** Get ac_buffer builder's read offset
156 * \param pBuilder ac_buffer builder
157 * \return read offset
158 */
159static inline size_t ac_buffer_builder_read_offset(ac_buffer_builder_t *pBuilder)
160{
161 return ac_buffer_data(&pBuilder->ac_buffer) - pBuilder->data;
162}
163
164/** Set ac_buffer builder's read offset
165 * \param pBuilder ac_buffer builder
166 * \param off new read offset
167 */
168static inline void ac_buffer_builder_set_read_offset(ac_buffer_builder_t *pBuilder, size_t off)
169{
170 if (off > pBuilder->size) {
171 return;
172 }
173 if (pBuilder->data + off < pBuilder->ac_buffer.data + pBuilder->ac_buffer.size) {
174 pBuilder->ac_buffer.size = pBuilder->ac_buffer.data - (pBuilder->data + off);
175 } else {
176 pBuilder->ac_buffer.size = 0;
177 }
178 pBuilder->ac_buffer.data = pBuilder->data + off;
179}
180
181/** Get ac_buffer builder's underlying ac_buffer
182 * \param pBuilder ac_buffer builder
183 * \return ac_buffer
184 */
185static inline ac_buffer_t *ac_buffer_builder_buffer(ac_buffer_builder_t *pBuilder)
186{
187 return &pBuilder->ac_buffer;
188}
189
190/** Get space in ac_buffer builder
191 * \param pBuilder ac_buffer builder
192 * \return number of free bytes in ac_buffer builder
193 */
194static inline size_t ac_buffer_builder_space(ac_buffer_builder_t *pBuilder)
195{
196 return pBuilder->size - (ac_buffer_data(&pBuilder->ac_buffer) - pBuilder->data + ac_buffer_size(&pBuilder->ac_buffer));
197}
198
199/** Is ac_buffer builder empty
200 * \param pBuilder ac_buffer builder
201 * \return true if ac_buffer builder is empty
202 */
203static inline bool ac_buffer_builder_empty(ac_buffer_builder_t *pBuilder)
204{
205 return (ac_buffer_builder_length(pBuilder) == 0);
206}
207
208/** Is ac_buffer builder full
209 * \param pBuilder ac_buffer builder
210 * \return true if ac_buffer builder is full
211 */
212static inline bool ac_buffer_full(ac_buffer_builder_t *pBuilder)
213{
214 return (ac_buffer_builder_space(pBuilder) == 0);
215}
216
217/** Write 8-bit value in ac_buffer builder
218 * \param pBuilder ac_buffer builder
219 * \param hu8 8-bit value to write
220 */
221static inline void ac_buffer_builder_write_nu8(ac_buffer_builder_t *pBuilder, uint8_t hu8)
222{
223 ac_buffer_builder_write_be(pBuilder, &hu8, 1);
224}
225
226/** Write 16-bit value in ac_buffer builder
227 * \param pBuilder ac_buffer builder
228 * \param hu16 16-bit value to write in big-endian format
229 */
230static inline void ac_buffer_builder_write_nu16(ac_buffer_builder_t *pBuilder, uint16_t hu16)
231{
232 ac_buffer_builder_write_be(pBuilder, (uint8_t *)&hu16, 2);
233}
234
235/** Write 24-bit value in ac_buffer builder
236 * \param pBuilder ac_buffer builder
237 * \param hu24 24-bit value to write in big-endian format
238 */
239static inline void ac_buffer_builder_write_nu24(ac_buffer_builder_t *pBuilder, uint32_t hu24)
240{
241 ac_buffer_builder_write_be(pBuilder, (uint8_t *)&hu24, 3);
242}
243
244/** Write 32-bit value in ac_buffer builder
245 * \param pBuilder ac_buffer builder
246 * \param hu32 32-bit value to write in big-endian format
247 */
248static inline void ac_buffer_builder_write_nu32(ac_buffer_builder_t *pBuilder, uint32_t hu32)
249{
250 ac_buffer_builder_write_be(pBuilder, (uint8_t *)&hu32, 4);
251}
252
253/** Write 64-bit value in ac_buffer builder
254 * \param pBuilder ac_buffer builder
255 * \param hu64 64-bit value to write in big-endian format
256 */
257static inline void ac_buffer_builder_write_nu64(ac_buffer_builder_t *pBuilder, uint64_t hu64)
258{
259 ac_buffer_builder_write_be(pBuilder, (uint8_t *)&hu64, 8);
260}
261
262/** Write n-bytes value in ac_buffer builder
263 * \param pBuilder ac_buffer builder
264 * \param data data to write
265 * \param size data length
266 */
267static inline void ac_buffer_builder_write_n_bytes(ac_buffer_builder_t *pBuilder, const uint8_t *data, size_t size)
268{
269 ac_buffer_builder_write_le(pBuilder, data, size);
270}
271
272/** Write 8-bit value in ac_buffer builder at specified position
273 * \param pBuilder ac_buffer builder
274 * \param off offset at which to write
275 * \param hu8 8-bit value to write
276 */
277static inline void ac_buffer_builder_write_nu8_at(ac_buffer_builder_t *pBuilder, size_t off, uint8_t hu8)
278{
279 ac_buffer_builder_write_be_at(pBuilder, off, &hu8, 1);
280}
281
282/** Write 16-bit value in ac_buffer builder at specified position
283 * \param pBuilder ac_buffer builder
284 * \param off offset at which to write
285 * \param hu16 16-bit value to write
286 */
287static inline void ac_buffer_builder_write_nu16_at(ac_buffer_builder_t *pBuilder, size_t off, uint16_t hu16)
288{
289 ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t *)&hu16, 2);
290}
291
292/** Write 24-bit value in ac_buffer builder at specified position
293 * \param pBuilder ac_buffer builder
294 * \param off offset at which to write
295 * \param hu24 24-bit value to write
296 */
297static inline void ac_buffer_builder_write_nu24_at(ac_buffer_builder_t *pBuilder, size_t off, uint32_t hu24)
298{
299 ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t *)&hu24, 3);
300}
301
302/** Write 32-bit value in ac_buffer builder at specified position
303 * \param pBuilder ac_buffer builder
304 * \param off offset at which to write
305 * \param hu32 32-bit value to write
306 */
307static inline void ac_buffer_builder_write_nu32_at(ac_buffer_builder_t *pBuilder, size_t off, uint32_t hu32)
308{
309 ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t *)&hu32, 4);
310}
311
312/** Write 64-bit value in ac_buffer builder at specified position
313 * \param pBuilder ac_buffer builder
314 * \param off offset at which to write
315 * \param hu64 64-bit value to write
316 */
317static inline void ac_buffer_builder_write_nu64_at(ac_buffer_builder_t *pBuilder, size_t off, uint64_t hu64)
318{
319 ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t *)&hu64, 8);
320}
321
322/** Write n-bytes value in ac_buffer builder at specified position
323 * \param pBuilder ac_buffer builder
324 * \param off offset at which to write
325 * \param data data to write
326 * \param size data length
327 */
328static inline void ac_buffer_builder_write_n_bytes_at(ac_buffer_builder_t *pBuilder, size_t off, const uint8_t *data, size_t size)
329{
330 ac_buffer_builder_write_be_at(pBuilder, off, data, size);
331}
332
333/** Skip n-bytes in ac_buffer builder
334 * \param pBuilder ac_buffer builder
335 * \param size number of bytes to skip
336 */
338
339/** Copy n bytes from buffer to builder
340 * \param pBuilderOut ac_buffer builder
341 * \param pBufIn the input buffer
342 * \param size number of bytes to copy
343 */
344void ac_buffer_builder_copy_n_bytes(ac_buffer_builder_t *pBuilderOut, ac_buffer_t *pBufIn, size_t size);
345
346/** Compact builder
347 * Will move underlying buffer's byte to start of allocated buffer
348 * \param pBuilder ac_buffer builder
349 */
351
352/** Get number of writable bytes in ac_buffer builder
353 * \param pBuilder ac_buffer builder
354 * \return number of free bytes in ac_buffer builder
355 */
356static inline size_t ac_buffer_builder_writable(ac_buffer_builder_t *pBuilder)
357{
358 return ac_buffer_builder_space(pBuilder);
359}
360
361#ifdef __cplusplus
362}
363#endif
364
365#endif /* ACORE_BUFFER_BUILDER_H_ */
366
367/**
368 * @}
369 * @}
370 * */
void ac_buffer_builder_copy_n_bytes(ac_buffer_builder_t *pBuilderOut, ac_buffer_t *pBufIn, size_t size)
Copy n bytes from buffer to builder.
void ac_buffer_builder_compact(ac_buffer_builder_t *pBuilder)
Compact builder Will move underlying buffer's byte to start of allocated buffer.
void ac_buffer_builder_set_full(ac_buffer_builder_t *pBuilder)
Set ac_buffer builder's ac_buffer to full size.
void ac_buffer_builder_write_le_at(ac_buffer_builder_t *pBuilder, size_t pos, const uint8_t *buf, size_t size)
Write data to little endian ac_buffer at specific position (on a LE architecture, byte order will be ...
void ac_buffer_builder_from_buffer(ac_buffer_builder_t *pBuilder)
Initialize ac_buffer builder from underlying ac_buffer.
void ac_buffer_builder_write_n_skip(ac_buffer_builder_t *pBuilder, size_t size)
Skip n-bytes in ac_buffer builder.
void ac_buffer_builder_reset(ac_buffer_builder_t *pBuilder)
Reset ac_buffer builder.
void ac_buffer_builder_init(ac_buffer_builder_t *pBuilder, uint8_t *data, size_t size)
Initialize ac_buffer builder.
void ac_buffer_builder_write_le(ac_buffer_builder_t *pBuilder, const uint8_t *buf, size_t size)
Write data to little endian ac_buffer (on a LE architecture, byte order will be preserved)
void ac_buffer_builder_write_be_at(ac_buffer_builder_t *pBuilder, size_t pos, const uint8_t *buf, size_t size)
Write data to big endian ac_buffer at specific position (on a LE architecture, byte order will be swa...
void ac_buffer_builder_write_be(ac_buffer_builder_t *pBuilder, const uint8_t *buf, size_t size)
Write data to big endian ac_buffer (on a LE architecture, byte order will be swapped)