Sun, 01 May 2022 11:44:23 +0200
#170 complete write tests for status quo
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 * The buffer may not extend beyond this threshold before starting to flush.
95 * Default is \c SIZE_MAX (flushing disabled when auto extension is enabled).
96 */
97 size_t flush_threshold;
98 /**
99 * The block size for the elements to flush.
100 * Default is 4096 bytes.
101 */
102 size_t flush_blksize;
103 /**
104 * The maximum number of blocks to flush in one cycle.
105 * Zero disables flushing entirely (this is the default).
106 * Set this to \c SIZE_MAX to flush the entire buffer.
107 *
108 * @attention if the maximum number of blocks multiplied with the block size
109 * is smaller than the expected contents written to this buffer within one write
110 * operation, multiple flush cycles are performed after that write.
111 * That means the total number of blocks flushed after one write to this buffer may
112 * be larger than \c flush_blkmax.
113 */
114 size_t flush_blkmax;
116 /**
117 * The write function used for flushing.
118 * If NULL, the flushed content gets discarded.
119 */
120 size_t (*flush_func)(
121 void const *,
122 size_t,
123 size_t,
124 void *
125 );
127 /**
128 * The target for \c flush_func.
129 */
130 void *flush_target;
132 /**
133 * Flag register for buffer features.
134 * @see #CX_BUFFER_DEFAULT
135 * @see #CX_BUFFER_FREE_CONTENTS
136 * @see #CX_BUFFER_AUTO_EXTEND
137 */
138 int flags;
139 } cx_buffer_s;
141 /**
142 * UCX buffer.
143 */
144 typedef cx_buffer_s CxBuffer;
146 /**
147 * Initializes a fresh buffer.
148 *
149 * \note You may provide \c NULL as argument for \p space.
150 * Then this function will allocate the space and enforce
151 * the #CX_BUFFER_FREE_CONTENTS flag.
152 *
153 * @param buffer the buffer to initialize
154 * @param space pointer to the memory area, or \c NULL to allocate
155 * new memory
156 * @param capacity the capacity of the buffer
157 * @param allocator the allocator this buffer shall use for automatic memory management
158 * @param flags buffer features (see cx_buffer_s.flags)
159 * @return zero on success, non-zero if a required allocation failed
160 */
161 __attribute__((__nonnull__(1, 4)))
162 int cxBufferInit(
163 CxBuffer *buffer,
164 void *space,
165 size_t capacity,
166 CxAllocator const *allocator,
167 int flags
168 );
170 /**
171 * Destroys the buffer contents.
172 *
173 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
174 *
175 * @param buffer the buffer which contents shall be destroyed
176 */
177 __attribute__((__nonnull__))
178 void cxBufferDestroy(CxBuffer *buffer);
180 /**
181 * Shifts the contents of the buffer by the given offset.
182 *
183 * If the offset is positive, the contents are shifted to the right.
184 * If auto extension is enabled, the buffer grows, if necessary.
185 * In case the auto extension fails, this function returns a non-zero value and
186 * no contents are changed.
187 * If auto extension is disabled, the contents that do not fit into the buffer
188 * are discarded.
189 *
190 * If the offset is negative, the contents are shifted to the left where the
191 * first \p shift bytes are discarded.
192 * The new size of the buffer is the old size minus the absolute shift value.
193 * If this value is larger than the buffer size, the buffer is emptied (but
194 * not cleared, see the security note below).
195 *
196 * The buffer position gets shifted alongside with the content but is kept
197 * within the boundaries of the buffer.
198 *
199 * \note For situations where \c off_t is not large enough, there are specialized cxBufferShiftLeft() and
200 * cxBufferShiftRight() functions using a \c size_t as parameter type.
201 *
202 * \attention
203 * Security Note: The shifting operation does \em not erase the previously occupied memory cells.
204 * But you can easily do that manually, e.g. by calling
205 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or
206 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code>
207 * for a left shift.
208 *
209 * @param buffer the buffer
210 * @param shift the shift offset (negative means left shift)
211 * @return 0 on success, non-zero if a required auto-extension fails
212 */
213 __attribute__((__nonnull__))
214 int cxBufferShift(
215 CxBuffer *buffer,
216 off_t shift
217 );
219 /**
220 * Shifts the buffer to the right.
221 * See cxBufferShift() for details.
222 *
223 * @param buffer the buffer
224 * @param shift the shift offset
225 * @return 0 on success, non-zero if a required auto-extension fails
226 * @see cxBufferShift()
227 */
228 __attribute__((__nonnull__))
229 int cxBufferShiftRight(
230 CxBuffer *buffer,
231 size_t shift
232 );
234 /**
235 * Shifts the buffer to the left.
236 * See cxBufferShift() for details.
237 *
238 * \note Since a left shift cannot fail due to memory allocation problems, this
239 * function always returns zero.
240 *
241 * @param buffer the buffer
242 * @param shift the positive shift offset
243 * @return always zero
244 * @see cxBufferShift()
245 */
246 __attribute__((__nonnull__))
247 int cxBufferShiftLeft(
248 CxBuffer *buffer,
249 size_t shift
250 );
253 /**
254 * Moves the position of the buffer.
255 *
256 * The new position is relative to the \p whence argument.
257 *
258 * \li \c SEEK_SET marks the start of the buffer.
259 * \li \c SEEK_CUR marks the current position.
260 * \li \c SEEK_END marks the end of the buffer.
261 *
262 * With an offset of zero, this function sets the buffer position to zero
263 * (\c SEEK_SET), the buffer size (\c SEEK_END) or leaves the buffer position
264 * unchanged (\c SEEK_CUR).
265 *
266 * @param buffer the buffer
267 * @param offset position offset relative to \p whence
268 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
269 * @return 0 on success, non-zero if the position is invalid
270 *
271 */
272 __attribute__((__nonnull__))
273 int cxBufferSeek(
274 CxBuffer *buffer,
275 off_t offset,
276 int whence
277 );
279 /**
280 * Clears the buffer by resetting the position and deleting the data.
281 *
282 * The data is deleted by zeroing it with a call to memset().
283 *
284 * @param buffer the buffer to be cleared
285 */
286 __attribute__((__nonnull__))
287 void cxBufferClear(CxBuffer *buffer);
289 /**
290 * Tests, if the buffer position has exceeded the buffer capacity.
291 *
292 * @param buffer the buffer to test
293 * @return non-zero, if the current buffer position has exceeded the last
294 * available byte of the buffer.
295 */
296 __attribute__((__nonnull__))
297 int cxBufferEof(CxBuffer const *buffer);
300 /**
301 * Ensures that the buffer has a minimum capacity.
302 *
303 * If the current capacity is not sufficient, the buffer will be extended.
304 *
305 * @param buffer the buffer
306 * @param capacity the minimum required capacity for this buffer
307 * @return 0 on success or a non-zero value on failure
308 */
309 __attribute__((__nonnull__))
310 int cxBufferMinimumCapacity(
311 CxBuffer *buffer,
312 size_t capacity
313 );
315 /**
316 * Writes data to a CxBuffer.
317 *
318 * The position of the buffer is increased by the number of bytes written.
319 *
320 * \note The signature is compatible with the fwrite() family of functions.
321 *
322 * @param ptr a pointer to the memory area containing the bytes to be written
323 * @param size the length of one element
324 * @param nitems the element count
325 * @param buffer the CxBuffer to write to
326 * @return the total count of elements written
327 */
328 __attribute__((__nonnull__))
329 size_t cxBufferWrite(
330 void const *ptr,
331 size_t size,
332 size_t nitems,
333 CxBuffer *buffer
334 );
336 /**
337 * Reads data from a CxBuffer.
338 *
339 * The position of the buffer is increased by the number of bytes read.
340 *
341 * \note The signature is compatible with the fread() family of functions.
342 *
343 * @param ptr a pointer to the memory area where to store the read data
344 * @param size the length of one element
345 * @param nitems the element count
346 * @param buffer the CxBuffer to read from
347 * @return the total number of elements read
348 */
349 __attribute__((__nonnull__))
350 size_t cxBufferRead(
351 void *ptr,
352 size_t size,
353 size_t nitems,
354 CxBuffer *buffer
355 );
357 /**
358 * Writes a character to a buffer.
359 *
360 * The least significant byte of the argument is written to the buffer. If the
361 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
362 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is
363 * disabled or buffer extension fails, \c EOF is returned.
364 *
365 * On successful write, the position of the buffer is increased.
366 *
367 * @param buffer the buffer to write to
368 * @param c the character to write
369 * @return the byte that has bean written or \c EOF when the end of the stream is
370 * reached and automatic extension is not enabled or not possible
371 */
372 __attribute__((__nonnull__))
373 int cxBufferPut(
374 CxBuffer *buffer,
375 int c
376 );
378 /**
379 * Writes a string to a buffer.
380 *
381 * @param buffer the buffer
382 * @param str the zero-terminated string
383 * @return the number of bytes written
384 */
385 __attribute__((__nonnull__))
386 size_t cxBufferPutString(
387 CxBuffer *buffer,
388 const char *str
389 );
391 /**
392 * Gets a character from a buffer.
393 *
394 * The current position of the buffer is increased after a successful read.
395 *
396 * @param buffer the buffer to read from
397 * @return the character or \c EOF, if the end of the buffer is reached
398 */
399 __attribute__((__nonnull__))
400 int cxBufferGet(CxBuffer *buffer);
402 #ifdef __cplusplus
403 }
404 #endif
406 #endif /* UCX_BUFFER_H */