Sun, 24 Apr 2022 14:54:50 +0200
#171 const qualifier and nonnull attributes
Also removes cxBufferExtract temporarily.
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 /**
30 * \file buffer.h
31 *
32 * \brief Advanced buffer implementation.
33 *
34 * Instances of CxBuffer can be used to read from or to write to like one
35 * would do with a stream.
36 *
37 * Some features for convenient use of the buffer
38 * can be enabled. See the documentation of the macro constants for more
39 * information.
40 *
41 * \author Mike Becker
42 * \author Olaf Wintermann
43 * \version 3.0
44 * \copyright 2-Clause BSD License
45 */
47 #ifndef UCX_BUFFER_H
48 #define UCX_BUFFER_H
50 #include "common.h"
51 #include "allocator.h"
52 #include <stdio.h>
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
58 /**
59 * No buffer features enabled (all flags cleared).
60 */
61 #define CX_BUFFER_DEFAULT 0x00
63 /**
64 * If this flag is enabled, the buffer will automatically free its contents when destroyed.
65 */
66 #define CX_BUFFER_FREE_CONTENTS 0x01
68 /**
69 * If this flag is enabled, the buffer will automatically extends its capacity.
70 */
71 #define CX_BUFFER_AUTO_EXTEND 0x02
73 /** Structure for the UCX buffer data. */
74 typedef struct {
75 /** A pointer to the buffer contents. */
76 union {
77 /**
78 * Data is interpreted as text.
79 */
80 char *space;
81 /**
82 * Data is interpreted as binary.
83 */
84 unsigned char *bytes;
85 };
86 /** The allocator to use for automatic memory management. */
87 CxAllocator const *allocator;
88 /** Current position of the buffer. */
89 size_t pos;
90 /** Current capacity (i.e. maximum size) of the buffer. */
91 size_t capacity;
92 /** Current size of the buffer content. */
93 size_t size;
94 /**
95 * Flag register for buffer features.
96 * @see #CX_BUFFER_DEFAULT
97 * @see #CX_BUFFER_FREE_CONTENTS
98 * @see #CX_BUFFER_AUTO_EXTEND
99 */
100 int flags;
101 } cx_buffer_s;
103 /**
104 * UCX buffer.
105 */
106 typedef cx_buffer_s CxBuffer;
108 /**
109 * Initializes a fresh buffer.
110 *
111 * \note You may provide \c NULL as argument for \p space.
112 * Then this function will allocate the space and enforce
113 * the #CX_BUFFER_FREE_CONTENTS flag.
114 *
115 * @param buffer the buffer to initialize
116 * @param space pointer to the memory area, or \c NULL to allocate
117 * new memory
118 * @param capacity the capacity of the buffer
119 * @param allocator the allocator this buffer shall use for automatic memory management
120 * @param flags buffer features (see cx_buffer_s.flags)
121 * @return zero on success, non-zero if a required allocation failed
122 */
123 __attribute__((__nonnull__(1, 4)))
124 int cxBufferInit(
125 CxBuffer *buffer,
126 void *space,
127 size_t capacity,
128 CxAllocator const *allocator,
129 int flags
130 );
132 /**
133 * Destroys the buffer contents.
134 *
135 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
136 *
137 * @param buffer the buffer which contents shall be destroyed
138 */
139 __attribute__((__nonnull__))
140 void cxBufferDestroy(CxBuffer *buffer);
142 /**
143 * Shifts the contents of the buffer by the given offset.
144 *
145 * If the offset is positive, the contents are shifted to the right.
146 * If auto extension is enabled, the buffer grows, if necessary.
147 * In case the auto extension fails, this function returns a non-zero value and
148 * no contents are changed.
149 * If auto extension is disabled, the contents that do not fit into the buffer
150 * are discarded.
151 *
152 * If the offset is negative, the contents are shifted to the left where the
153 * first \p shift bytes are discarded.
154 * The new size of the buffer is the old size minus the absolute shift value.
155 * If this value is larger than the buffer size, the buffer is emptied (but
156 * not cleared, see the security note below).
157 *
158 * The buffer position gets shifted alongside with the content but is kept
159 * within the boundaries of the buffer.
160 *
161 * \note For situations where \c off_t is not large enough, there are specialized cxBufferShiftLeft() and
162 * cxBufferShiftRight() functions using a \c size_t as parameter type.
163 *
164 * \attention
165 * Security Note: The shifting operation does \em not erase the previously occupied memory cells.
166 * But you can easily do that manually, e.g. by calling
167 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or
168 * <code>memset(buffer->size, 0, buffer->capacity - buffer->size)</code>
169 * for a left shift.
170 *
171 * @param buffer the buffer
172 * @param shift the shift offset (negative means left shift)
173 * @return 0 on success, non-zero if a required auto-extension fails
174 */
175 __attribute__((__nonnull__))
176 int cxBufferShift(
177 CxBuffer *buffer,
178 off_t shift
179 );
181 /**
182 * Shifts the buffer to the right.
183 * See cxBufferShift() for details.
184 *
185 * @param buffer the buffer
186 * @param shift the shift offset
187 * @return 0 on success, non-zero if a required auto-extension fails
188 * @see cxBufferShift()
189 */
190 __attribute__((__nonnull__))
191 int cxBufferShiftRight(
192 CxBuffer *buffer,
193 size_t shift
194 );
196 /**
197 * Shifts the buffer to the left.
198 * See cxBufferShift() for details.
199 *
200 * \note Since a left shift cannot fail due to memory allocation problems, this
201 * function always returns zero.
202 *
203 * @param buffer the buffer
204 * @param shift the positive shift offset
205 * @return always zero
206 * @see cxBufferShift()
207 */
208 __attribute__((__nonnull__))
209 int cxBufferShiftLeft(
210 CxBuffer *buffer,
211 size_t shift
212 );
215 /**
216 * Moves the position of the buffer.
217 *
218 * The new position is relative to the \p whence argument.
219 *
220 * \li \c SEEK_SET marks the start of the buffer.
221 * \li \c SEEK_CUR marks the current position.
222 * \li \c SEEK_END marks the end of the buffer.
223 *
224 * With an offset of zero, this function sets the buffer position to zero
225 * (\c SEEK_SET), the buffer size (\c SEEK_END) or leaves the buffer position
226 * unchanged (\c SEEK_CUR).
227 *
228 * @param buffer the buffer
229 * @param offset position offset relative to \p whence
230 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
231 * @return 0 on success, non-zero if the position is invalid
232 *
233 */
234 __attribute__((__nonnull__))
235 int cxBufferSeek(
236 CxBuffer *buffer,
237 off_t offset,
238 int whence
239 );
241 /**
242 * Clears the buffer by resetting the position and deleting the data.
243 *
244 * The data is deleted by zeroing it with a call to memset().
245 *
246 * @param buffer the buffer to be cleared
247 */
248 __attribute__((__nonnull__))
249 void cxBufferClear(CxBuffer *buffer);
251 /**
252 * Tests, if the buffer position has exceeded the buffer capacity.
253 *
254 * @param buffer the buffer to test
255 * @return non-zero, if the current buffer position has exceeded the last
256 * available byte of the buffer.
257 */
258 __attribute__((__nonnull__))
259 int cxBufferEof(CxBuffer const *buffer);
262 /**
263 * Ensures that the buffer has a minimum capacity.
264 *
265 * If the current capacity is not sufficient, the buffer will be extended.
266 *
267 * @param buffer the buffer
268 * @param capacity the minimum required capacity for this buffer
269 * @return 0 on success or a non-zero value on failure
270 */
271 __attribute__((__nonnull__))
272 int cxBufferMinimumCapacity(
273 CxBuffer *buffer,
274 size_t capacity
275 );
277 /**
278 * Writes data to a CxBuffer.
279 *
280 * The position of the buffer is increased by the number of bytes written.
281 *
282 * \note The signature is compatible with the fwrite() family of functions.
283 *
284 * @param ptr a pointer to the memory area containing the bytes to be written
285 * @param size the length of one element
286 * @param nitems the element count
287 * @param buffer the CxBuffer to write to
288 * @return the total count of bytes written
289 */
290 __attribute__((__nonnull__))
291 size_t cxBufferWrite(
292 void const *ptr,
293 size_t size,
294 size_t nitems,
295 CxBuffer *buffer
296 );
298 /**
299 * Reads data from a CxBuffer.
300 *
301 * The position of the buffer is increased by the number of bytes read.
302 *
303 * \note The signature is compatible with the fread() family of functions.
304 *
305 * @param ptr a pointer to the memory area where to store the read data
306 * @param size the length of one element
307 * @param nitems the element count
308 * @param buffer the CxBuffer to read from
309 * @return the total number of elements read
310 */
311 __attribute__((__nonnull__))
312 size_t cxBufferRead(
313 void *ptr,
314 size_t size,
315 size_t nitems,
316 CxBuffer *buffer
317 );
319 /**
320 * Writes a character to a buffer.
321 *
322 * The least significant byte of the argument is written to the buffer. If the
323 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
324 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is
325 * disabled or buffer extension fails, \c EOF is returned.
326 *
327 * On successful write, the position of the buffer is increased.
328 *
329 * @param buffer the buffer to write to
330 * @param c the character to write
331 * @return the byte that has bean written or \c EOF when the end of the stream is
332 * reached and automatic extension is not enabled or not possible
333 */
334 __attribute__((__nonnull__))
335 int cxBufferPut(
336 CxBuffer *buffer,
337 int c
338 );
340 /**
341 * Writes a string to a buffer.
342 *
343 * @param buffer the buffer
344 * @param str the zero-terminated string
345 * @return the number of bytes written
346 */
347 __attribute__((__nonnull__))
348 size_t cxBufferPutString(
349 CxBuffer *buffer,
350 const char *str
351 );
353 /**
354 * Gets a character from a buffer.
355 *
356 * The current position of the buffer is increased after a successful read.
357 *
358 * @param buffer the buffer to read from
359 * @return the character or \c EOF, if the end of the buffer is reached
360 */
361 __attribute__((__nonnull__))
362 int cxBufferGet(CxBuffer *buffer);
364 #ifdef __cplusplus
365 }
366 #endif
368 #endif /* UCX_BUFFER_H */