Sun, 24 Apr 2022 15:15:39 +0200
#170 first buffer tests
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"
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
57 /**
58 * No buffer features enabled (all flags cleared).
59 */
60 #define CX_BUFFER_DEFAULT 0x00
62 /**
63 * If this flag is enabled, the buffer will automatically free its contents when destroyed.
64 */
65 #define CX_BUFFER_FREE_CONTENTS 0x01
67 /**
68 * If this flag is enabled, the buffer will automatically extends its capacity.
69 */
70 #define CX_BUFFER_AUTO_EXTEND 0x02
72 /** Structure for the UCX buffer data. */
73 typedef struct {
74 /** A pointer to the buffer contents. */
75 union {
76 /**
77 * Data is interpreted as text.
78 */
79 char *space;
80 /**
81 * Data is interpreted as binary.
82 */
83 unsigned char *bytes;
84 };
85 /** The allocator to use for automatic memory management. */
86 CxAllocator const *allocator;
87 /** Current position of the buffer. */
88 size_t pos;
89 /** Current capacity (i.e. maximum size) of the buffer. */
90 size_t capacity;
91 /** Current size of the buffer content. */
92 size_t size;
93 /**
94 * Flag register for buffer features.
95 * @see #CX_BUFFER_DEFAULT
96 * @see #CX_BUFFER_FREE_CONTENTS
97 * @see #CX_BUFFER_AUTO_EXTEND
98 */
99 int flags;
100 } cx_buffer_s;
102 /**
103 * UCX buffer.
104 */
105 typedef cx_buffer_s CxBuffer;
107 /**
108 * Initializes a fresh buffer.
109 *
110 * \note You may provide \c NULL as argument for \p space.
111 * Then this function will allocate the space and enforce
112 * the #CX_BUFFER_FREE_CONTENTS flag.
113 *
114 * @param buffer the buffer to initialize
115 * @param space pointer to the memory area, or \c NULL to allocate
116 * new memory
117 * @param capacity the capacity of the buffer
118 * @param allocator the allocator this buffer shall use for automatic memory management
119 * @param flags buffer features (see cx_buffer_s.flags)
120 * @return zero on success, non-zero if a required allocation failed
121 */
122 __attribute__((__nonnull__(1, 4)))
123 int cxBufferInit(
124 CxBuffer *buffer,
125 void *space,
126 size_t capacity,
127 CxAllocator const *allocator,
128 int flags
129 );
131 /**
132 * Destroys the buffer contents.
133 *
134 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
135 *
136 * @param buffer the buffer which contents shall be destroyed
137 */
138 __attribute__((__nonnull__))
139 void cxBufferDestroy(CxBuffer *buffer);
141 /**
142 * Shifts the contents of the buffer by the given offset.
143 *
144 * If the offset is positive, the contents are shifted to the right.
145 * If auto extension is enabled, the buffer grows, if necessary.
146 * In case the auto extension fails, this function returns a non-zero value and
147 * no contents are changed.
148 * If auto extension is disabled, the contents that do not fit into the buffer
149 * are discarded.
150 *
151 * If the offset is negative, the contents are shifted to the left where the
152 * first \p shift bytes are discarded.
153 * The new size of the buffer is the old size minus the absolute shift value.
154 * If this value is larger than the buffer size, the buffer is emptied (but
155 * not cleared, see the security note below).
156 *
157 * The buffer position gets shifted alongside with the content but is kept
158 * within the boundaries of the buffer.
159 *
160 * \note For situations where \c off_t is not large enough, there are specialized cxBufferShiftLeft() and
161 * cxBufferShiftRight() functions using a \c size_t as parameter type.
162 *
163 * \attention
164 * Security Note: The shifting operation does \em not erase the previously occupied memory cells.
165 * But you can easily do that manually, e.g. by calling
166 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or
167 * <code>memset(buffer->size, 0, buffer->capacity - buffer->size)</code>
168 * for a left shift.
169 *
170 * @param buffer the buffer
171 * @param shift the shift offset (negative means left shift)
172 * @return 0 on success, non-zero if a required auto-extension fails
173 */
174 __attribute__((__nonnull__))
175 int cxBufferShift(
176 CxBuffer *buffer,
177 off_t shift
178 );
180 /**
181 * Shifts the buffer to the right.
182 * See cxBufferShift() for details.
183 *
184 * @param buffer the buffer
185 * @param shift the shift offset
186 * @return 0 on success, non-zero if a required auto-extension fails
187 * @see cxBufferShift()
188 */
189 __attribute__((__nonnull__))
190 int cxBufferShiftRight(
191 CxBuffer *buffer,
192 size_t shift
193 );
195 /**
196 * Shifts the buffer to the left.
197 * See cxBufferShift() for details.
198 *
199 * \note Since a left shift cannot fail due to memory allocation problems, this
200 * function always returns zero.
201 *
202 * @param buffer the buffer
203 * @param shift the positive shift offset
204 * @return always zero
205 * @see cxBufferShift()
206 */
207 __attribute__((__nonnull__))
208 int cxBufferShiftLeft(
209 CxBuffer *buffer,
210 size_t shift
211 );
214 /**
215 * Moves the position of the buffer.
216 *
217 * The new position is relative to the \p whence argument.
218 *
219 * \li \c SEEK_SET marks the start of the buffer.
220 * \li \c SEEK_CUR marks the current position.
221 * \li \c SEEK_END marks the end of the buffer.
222 *
223 * With an offset of zero, this function sets the buffer position to zero
224 * (\c SEEK_SET), the buffer size (\c SEEK_END) or leaves the buffer position
225 * unchanged (\c SEEK_CUR).
226 *
227 * @param buffer the buffer
228 * @param offset position offset relative to \p whence
229 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
230 * @return 0 on success, non-zero if the position is invalid
231 *
232 */
233 __attribute__((__nonnull__))
234 int cxBufferSeek(
235 CxBuffer *buffer,
236 off_t offset,
237 int whence
238 );
240 /**
241 * Clears the buffer by resetting the position and deleting the data.
242 *
243 * The data is deleted by zeroing it with a call to memset().
244 *
245 * @param buffer the buffer to be cleared
246 */
247 __attribute__((__nonnull__))
248 void cxBufferClear(CxBuffer *buffer);
250 /**
251 * Tests, if the buffer position has exceeded the buffer capacity.
252 *
253 * @param buffer the buffer to test
254 * @return non-zero, if the current buffer position has exceeded the last
255 * available byte of the buffer.
256 */
257 __attribute__((__nonnull__))
258 int cxBufferEof(CxBuffer const *buffer);
261 /**
262 * Ensures that the buffer has a minimum capacity.
263 *
264 * If the current capacity is not sufficient, the buffer will be extended.
265 *
266 * @param buffer the buffer
267 * @param capacity the minimum required capacity for this buffer
268 * @return 0 on success or a non-zero value on failure
269 */
270 __attribute__((__nonnull__))
271 int cxBufferMinimumCapacity(
272 CxBuffer *buffer,
273 size_t capacity
274 );
276 /**
277 * Writes data to a CxBuffer.
278 *
279 * The position of the buffer is increased by the number of bytes written.
280 *
281 * \note The signature is compatible with the fwrite() family of functions.
282 *
283 * @param ptr a pointer to the memory area containing the bytes to be written
284 * @param size the length of one element
285 * @param nitems the element count
286 * @param buffer the CxBuffer to write to
287 * @return the total count of bytes written
288 */
289 __attribute__((__nonnull__))
290 size_t cxBufferWrite(
291 void const *ptr,
292 size_t size,
293 size_t nitems,
294 CxBuffer *buffer
295 );
297 /**
298 * Reads data from a CxBuffer.
299 *
300 * The position of the buffer is increased by the number of bytes read.
301 *
302 * \note The signature is compatible with the fread() family of functions.
303 *
304 * @param ptr a pointer to the memory area where to store the read data
305 * @param size the length of one element
306 * @param nitems the element count
307 * @param buffer the CxBuffer to read from
308 * @return the total number of elements read
309 */
310 __attribute__((__nonnull__))
311 size_t cxBufferRead(
312 void *ptr,
313 size_t size,
314 size_t nitems,
315 CxBuffer *buffer
316 );
318 /**
319 * Writes a character to a buffer.
320 *
321 * The least significant byte of the argument is written to the buffer. If the
322 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
323 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is
324 * disabled or buffer extension fails, \c EOF is returned.
325 *
326 * On successful write, the position of the buffer is increased.
327 *
328 * @param buffer the buffer to write to
329 * @param c the character to write
330 * @return the byte that has bean written or \c EOF when the end of the stream is
331 * reached and automatic extension is not enabled or not possible
332 */
333 __attribute__((__nonnull__))
334 int cxBufferPut(
335 CxBuffer *buffer,
336 int c
337 );
339 /**
340 * Writes a string to a buffer.
341 *
342 * @param buffer the buffer
343 * @param str the zero-terminated string
344 * @return the number of bytes written
345 */
346 __attribute__((__nonnull__))
347 size_t cxBufferPutString(
348 CxBuffer *buffer,
349 const char *str
350 );
352 /**
353 * Gets a character from a buffer.
354 *
355 * The current position of the buffer is increased after a successful read.
356 *
357 * @param buffer the buffer to read from
358 * @return the character or \c EOF, if the end of the buffer is reached
359 */
360 __attribute__((__nonnull__))
361 int cxBufferGet(CxBuffer *buffer);
363 #ifdef __cplusplus
364 }
365 #endif
367 #endif /* UCX_BUFFER_H */