Sun, 01 May 2022 11:46:54 +0200
#170 no bug never existed
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 #include "cx/buffer.h"
30 #include "cx/utils.h"
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
37 int cxBufferInit(
38 CxBuffer *buffer,
39 void *space,
40 size_t capacity,
41 CxAllocator const *allocator,
42 int flags
43 ) {
44 buffer->allocator = allocator;
45 buffer->flags = flags;
46 if (!space) {
47 buffer->bytes = cxMalloc(allocator, capacity);
48 if (buffer->bytes == NULL) {
49 return 1;
50 }
51 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
52 } else {
53 buffer->bytes = space;
54 }
55 buffer->capacity = capacity;
56 buffer->size = 0;
57 buffer->pos = 0;
59 buffer->flush_func = NULL;
60 buffer->flush_target = NULL;
61 buffer->flush_blkmax = 0;
62 buffer->flush_blksize = 4096;
63 buffer->flush_threshold = SIZE_MAX;
65 return 0;
66 }
68 void cxBufferDestroy(CxBuffer *buffer) {
69 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) {
70 cxFree(buffer->allocator, buffer->bytes);
71 }
72 }
74 int cxBufferSeek(
75 CxBuffer *buffer,
76 off_t offset,
77 int whence
78 ) {
79 size_t npos;
80 switch (whence) {
81 case SEEK_CUR:
82 npos = buffer->pos;
83 break;
84 case SEEK_END:
85 npos = buffer->size;
86 break;
87 case SEEK_SET:
88 npos = 0;
89 break;
90 default:
91 return -1;
92 }
94 size_t opos = npos;
95 npos += offset;
97 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
98 return -1;
99 }
101 if (npos >= buffer->size) {
102 return -1;
103 } else {
104 buffer->pos = npos;
105 return 0;
106 }
108 }
110 void cxBufferClear(CxBuffer *buffer) {
111 memset(buffer->bytes, 0, buffer->size);
112 buffer->size = 0;
113 buffer->pos = 0;
114 }
116 int cxBufferEof(CxBuffer const *buffer) {
117 return buffer->pos >= buffer->size;
118 }
120 int cxBufferMinimumCapacity(
121 CxBuffer *buffer,
122 size_t newcap
123 ) {
124 if (newcap <= buffer->capacity) {
125 return 0;
126 }
128 if (cxReallocate(buffer->allocator,
129 (void **) &buffer->bytes, newcap) == 0) {
130 buffer->capacity = newcap;
131 return 0;
132 } else {
133 return -1;
134 }
135 }
137 size_t cxBufferWrite(
138 void const *ptr,
139 size_t size,
140 size_t nitems,
141 CxBuffer *buffer
142 ) {
143 // TODO: optimize for special case size == nitems == 1
144 size_t len;
145 if (cx_szmul(size, nitems, &len)) {
146 return 0;
147 }
148 size_t required = buffer->pos + len;
149 if (buffer->pos > required) {
150 return 0;
151 }
153 bool perform_flush = false;
154 if (required > buffer->capacity) {
155 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND && required) {
156 if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) {
157 perform_flush = true;
158 } else {
159 if (cxBufferMinimumCapacity(buffer, required)) {
160 return 0;
161 }
162 }
163 } else {
164 if (buffer->flush_blkmax > 0) {
165 perform_flush = true;
166 } else {
167 // truncate data to be written, if we can neither extend nor flush
168 len = buffer->capacity - buffer->pos;
169 if (size > 1) {
170 len -= len % size;
171 }
172 nitems = len / size;
173 }
174 }
175 }
177 if (len == 0) {
178 return len;
179 }
181 if (perform_flush) {
182 // TODO: implement flushing
183 // (1) determine how many bytes to flush (use flushmax = blkmax * blksize)
184 // (2) if len is larger than the number computed in (1) we need more flush cycles, compute how many
185 // (3) determine how many bytes from the buffer shall be flushed
186 // (4) if something remains in the buffer, shift the buffer to the left
187 // (4a) if buffer was shifted, append the new data to the buffer
188 // (4b) if the buffer was flushed entirely AND the new data also fits into flushmax,
189 // directly write the new data to the flush sink
190 return 0; // remove this after implementation
191 } else {
192 memcpy(buffer->bytes + buffer->pos, ptr, len);
193 buffer->pos += len;
194 if (buffer->pos > buffer->size) {
195 buffer->size = buffer->pos;
196 }
197 }
199 return nitems;
200 }
202 int cxBufferPut(
203 CxBuffer *buffer,
204 int c
205 ) {
206 c &= 0xFF;
207 unsigned char const ch = c;
208 if (cxBufferWrite(&ch, 1, 1, buffer) == 1) {
209 return c;
210 } else {
211 return EOF;
212 }
213 }
215 size_t cxBufferPutString(
216 CxBuffer *buffer,
217 const char *str
218 ) {
219 return cxBufferWrite(str, 1, strlen(str), buffer);
220 }
222 size_t cxBufferRead(
223 void *ptr,
224 size_t size,
225 size_t nitems,
226 CxBuffer *buffer
227 ) {
228 size_t len;
229 if (cx_szmul(size, nitems, &len)) {
230 return 0;
231 }
232 if (buffer->pos + len > buffer->size) {
233 len = buffer->size - buffer->pos;
234 if (size > 1) len -= len % size;
235 }
237 if (len <= 0) {
238 return len;
239 }
241 memcpy(ptr, buffer->bytes + buffer->pos, len);
242 buffer->pos += len;
244 return len / size;
245 }
247 int cxBufferGet(CxBuffer *buffer) {
248 if (cxBufferEof(buffer)) {
249 return EOF;
250 } else {
251 int c = buffer->bytes[buffer->pos];
252 buffer->pos++;
253 return c;
254 }
255 }
257 int cxBufferShiftLeft(
258 CxBuffer *buffer,
259 size_t shift
260 ) {
261 if (shift >= buffer->size) {
262 buffer->pos = buffer->size = 0;
263 } else {
264 memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
265 buffer->size -= shift;
267 if (buffer->pos >= shift) {
268 buffer->pos -= shift;
269 } else {
270 buffer->pos = 0;
271 }
272 }
273 return 0;
274 }
276 int cxBufferShiftRight(
277 CxBuffer *buffer,
278 size_t shift
279 ) {
280 size_t req_capacity = buffer->size + shift;
281 size_t movebytes;
283 // auto extend buffer, if required and enabled
284 if (buffer->capacity < req_capacity) {
285 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
286 if (cxBufferMinimumCapacity(buffer, req_capacity)) {
287 return 1;
288 }
289 movebytes = buffer->size;
290 } else {
291 movebytes = buffer->capacity - shift;
292 }
293 } else {
294 movebytes = buffer->size;
295 }
297 memmove(buffer->bytes + shift, buffer->bytes, movebytes);
298 buffer->size = shift + movebytes;
300 buffer->pos += shift;
301 if (buffer->pos > buffer->size) {
302 buffer->pos = buffer->size;
303 }
305 return 0;
306 }
308 int cxBufferShift(
309 CxBuffer *buffer,
310 off_t shift
311 ) {
312 if (shift < 0) {
313 return cxBufferShiftLeft(buffer, (size_t) (-shift));
314 } else if (shift > 0) {
315 return cxBufferShiftRight(buffer, (size_t) shift);
316 } else {
317 return 0;
318 }
319 }