Sun, 24 Apr 2022 15:15:39 +0200
#170 first buffer tests
universe@483 | 1 | /* |
universe@483 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@483 | 3 | * |
universe@483 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@483 | 5 | * |
universe@483 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@483 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@483 | 8 | * |
universe@483 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@483 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@483 | 11 | * |
universe@483 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@483 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@483 | 14 | * documentation and/or other materials provided with the distribution. |
universe@483 | 15 | * |
universe@483 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@483 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@483 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@483 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@483 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@483 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@483 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@483 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@483 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@483 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@483 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@483 | 27 | */ |
universe@483 | 28 | |
universe@483 | 29 | #include "cx/buffer.h" |
universe@483 | 30 | #include "cx/utils.h" |
universe@483 | 31 | |
universe@483 | 32 | #include <stdlib.h> |
universe@530 | 33 | #include <stdio.h> |
universe@483 | 34 | #include <string.h> |
universe@483 | 35 | |
universe@501 | 36 | int cxBufferInit( |
universe@501 | 37 | CxBuffer *buffer, |
universe@483 | 38 | void *space, |
universe@483 | 39 | size_t capacity, |
universe@529 | 40 | CxAllocator const *allocator, |
universe@483 | 41 | int flags |
universe@483 | 42 | ) { |
universe@501 | 43 | buffer->allocator = allocator; |
universe@501 | 44 | buffer->flags = flags; |
universe@501 | 45 | if (!space) { |
universe@501 | 46 | buffer->bytes = cxMalloc(allocator, capacity); |
universe@501 | 47 | if (buffer->bytes == NULL) { |
universe@501 | 48 | return 1; |
universe@483 | 49 | } |
universe@501 | 50 | memset(buffer->bytes, 0, capacity); |
universe@501 | 51 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; |
universe@501 | 52 | } else { |
universe@501 | 53 | buffer->bytes = space; |
universe@501 | 54 | } |
universe@501 | 55 | buffer->capacity = capacity; |
universe@501 | 56 | buffer->size = 0; |
universe@483 | 57 | |
universe@501 | 58 | buffer->pos = 0; |
universe@483 | 59 | |
universe@501 | 60 | return 0; |
universe@483 | 61 | } |
universe@483 | 62 | |
universe@500 | 63 | void cxBufferDestroy(CxBuffer *buffer) { |
universe@483 | 64 | if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) { |
universe@501 | 65 | cxFree(buffer->allocator, buffer->bytes); |
universe@483 | 66 | } |
universe@483 | 67 | } |
universe@483 | 68 | |
universe@483 | 69 | int cxBufferSeek( |
universe@500 | 70 | CxBuffer *buffer, |
universe@483 | 71 | off_t offset, |
universe@483 | 72 | int whence |
universe@483 | 73 | ) { |
universe@483 | 74 | size_t npos; |
universe@483 | 75 | switch (whence) { |
universe@483 | 76 | case SEEK_CUR: |
universe@483 | 77 | npos = buffer->pos; |
universe@483 | 78 | break; |
universe@483 | 79 | case SEEK_END: |
universe@483 | 80 | npos = buffer->size; |
universe@483 | 81 | break; |
universe@483 | 82 | case SEEK_SET: |
universe@483 | 83 | npos = 0; |
universe@483 | 84 | break; |
universe@483 | 85 | default: |
universe@483 | 86 | return -1; |
universe@483 | 87 | } |
universe@483 | 88 | |
universe@483 | 89 | size_t opos = npos; |
universe@483 | 90 | npos += offset; |
universe@483 | 91 | |
universe@483 | 92 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { |
universe@483 | 93 | return -1; |
universe@483 | 94 | } |
universe@483 | 95 | |
universe@483 | 96 | if (npos >= buffer->size) { |
universe@483 | 97 | return -1; |
universe@483 | 98 | } else { |
universe@483 | 99 | buffer->pos = npos; |
universe@483 | 100 | return 0; |
universe@483 | 101 | } |
universe@483 | 102 | |
universe@483 | 103 | } |
universe@483 | 104 | |
universe@529 | 105 | void cxBufferClear(CxBuffer *buffer) { |
universe@529 | 106 | memset(buffer->bytes, 0, buffer->size); |
universe@529 | 107 | buffer->size = 0; |
universe@529 | 108 | buffer->pos = 0; |
universe@529 | 109 | } |
universe@529 | 110 | |
universe@529 | 111 | int cxBufferEof(CxBuffer const *buffer) { |
universe@483 | 112 | return buffer->pos >= buffer->size; |
universe@483 | 113 | } |
universe@483 | 114 | |
universe@483 | 115 | int cxBufferMinimumCapacity( |
universe@500 | 116 | CxBuffer *buffer, |
universe@483 | 117 | size_t additional_bytes |
universe@483 | 118 | ) { |
universe@483 | 119 | size_t newcap = buffer->capacity + additional_bytes; |
universe@483 | 120 | |
universe@483 | 121 | // overflow protection |
universe@483 | 122 | if (newcap < buffer->capacity) { |
universe@483 | 123 | return -1; |
universe@483 | 124 | } |
universe@483 | 125 | |
universe@483 | 126 | unsigned char *newspace = realloc(buffer->bytes, newcap); |
universe@483 | 127 | if (newspace) { |
universe@483 | 128 | memset(newspace + buffer->size, 0, newcap - buffer->size); |
universe@483 | 129 | buffer->bytes = newspace; |
universe@483 | 130 | buffer->capacity = newcap; |
universe@483 | 131 | } else { |
universe@483 | 132 | return -1; |
universe@483 | 133 | } |
universe@483 | 134 | |
universe@483 | 135 | return 0; |
universe@483 | 136 | } |
universe@483 | 137 | |
universe@483 | 138 | size_t cxBufferWrite( |
universe@489 | 139 | void const *ptr, |
universe@483 | 140 | size_t size, |
universe@483 | 141 | size_t nitems, |
universe@500 | 142 | CxBuffer *buffer |
universe@483 | 143 | ) { |
universe@483 | 144 | size_t len; |
universe@483 | 145 | if (cx_szmul(size, nitems, &len)) { |
universe@483 | 146 | return 0; |
universe@483 | 147 | } |
universe@483 | 148 | size_t required = buffer->pos + len; |
universe@483 | 149 | if (buffer->pos > required) { |
universe@483 | 150 | return 0; |
universe@483 | 151 | } |
universe@483 | 152 | |
universe@483 | 153 | if (required > buffer->capacity) { |
universe@483 | 154 | if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) { |
universe@483 | 155 | if (cxBufferMinimumCapacity(buffer, required)) { |
universe@483 | 156 | return 0; |
universe@483 | 157 | } |
universe@483 | 158 | } else { |
universe@483 | 159 | len = buffer->capacity - buffer->pos; |
universe@483 | 160 | if (size > 1) { |
universe@483 | 161 | len -= len % size; |
universe@483 | 162 | } |
universe@483 | 163 | } |
universe@483 | 164 | } |
universe@483 | 165 | |
universe@483 | 166 | if (len == 0) { |
universe@483 | 167 | return len; |
universe@483 | 168 | } |
universe@483 | 169 | |
universe@483 | 170 | memcpy(buffer->bytes + buffer->pos, ptr, len); |
universe@483 | 171 | buffer->pos += len; |
universe@483 | 172 | if (buffer->pos > buffer->size) { |
universe@483 | 173 | buffer->size = buffer->pos; |
universe@483 | 174 | } |
universe@483 | 175 | |
universe@483 | 176 | return len / size; |
universe@483 | 177 | } |
universe@483 | 178 | |
universe@483 | 179 | size_t cxBufferRead( |
universe@483 | 180 | void *ptr, |
universe@483 | 181 | size_t size, |
universe@483 | 182 | size_t nitems, |
universe@500 | 183 | CxBuffer *buffer |
universe@483 | 184 | ) { |
universe@483 | 185 | size_t len; |
universe@483 | 186 | if (cx_szmul(size, nitems, &len)) { |
universe@483 | 187 | return 0; |
universe@483 | 188 | } |
universe@483 | 189 | if (buffer->pos + len > buffer->size) { |
universe@483 | 190 | len = buffer->size - buffer->pos; |
universe@483 | 191 | if (size > 1) len -= len % size; |
universe@483 | 192 | } |
universe@483 | 193 | |
universe@483 | 194 | if (len <= 0) { |
universe@483 | 195 | return len; |
universe@483 | 196 | } |
universe@483 | 197 | |
universe@483 | 198 | memcpy(ptr, buffer->bytes + buffer->pos, len); |
universe@483 | 199 | buffer->pos += len; |
universe@483 | 200 | |
universe@483 | 201 | return len / size; |
universe@483 | 202 | } |
universe@483 | 203 | |
universe@483 | 204 | int cxBufferPut( |
universe@500 | 205 | CxBuffer *buffer, |
universe@483 | 206 | int c |
universe@483 | 207 | ) { |
universe@483 | 208 | if (buffer->pos >= buffer->capacity) { |
universe@483 | 209 | if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) { |
universe@483 | 210 | if (cxBufferMinimumCapacity(buffer, buffer->capacity + 1)) { |
universe@483 | 211 | return EOF; |
universe@483 | 212 | } |
universe@483 | 213 | } else { |
universe@483 | 214 | return EOF; |
universe@483 | 215 | } |
universe@483 | 216 | } |
universe@483 | 217 | |
universe@483 | 218 | c &= 0xFF; |
universe@483 | 219 | buffer->bytes[buffer->pos] = (unsigned char) c; |
universe@483 | 220 | buffer->pos++; |
universe@483 | 221 | if (buffer->pos > buffer->size) { |
universe@483 | 222 | buffer->size = buffer->pos; |
universe@483 | 223 | } |
universe@483 | 224 | return c; |
universe@483 | 225 | } |
universe@483 | 226 | |
universe@500 | 227 | int cxBufferGet(CxBuffer *buffer) { |
universe@483 | 228 | if (cxBufferEof(buffer)) { |
universe@483 | 229 | return EOF; |
universe@483 | 230 | } else { |
universe@483 | 231 | int c = buffer->bytes[buffer->pos]; |
universe@483 | 232 | buffer->pos++; |
universe@483 | 233 | return c; |
universe@483 | 234 | } |
universe@483 | 235 | } |
universe@483 | 236 | |
universe@483 | 237 | size_t cxBufferPutString( |
universe@500 | 238 | CxBuffer *buffer, |
universe@483 | 239 | const char *str |
universe@483 | 240 | ) { |
universe@483 | 241 | return cxBufferWrite(str, 1, strlen(str), buffer); |
universe@483 | 242 | } |
universe@483 | 243 | |
universe@483 | 244 | int cxBufferShiftLeft( |
universe@500 | 245 | CxBuffer *buffer, |
universe@483 | 246 | size_t shift |
universe@483 | 247 | ) { |
universe@483 | 248 | if (shift >= buffer->size) { |
universe@483 | 249 | buffer->pos = buffer->size = 0; |
universe@483 | 250 | } else { |
universe@483 | 251 | memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift); |
universe@483 | 252 | buffer->size -= shift; |
universe@483 | 253 | |
universe@483 | 254 | if (buffer->pos >= shift) { |
universe@483 | 255 | buffer->pos -= shift; |
universe@483 | 256 | } else { |
universe@483 | 257 | buffer->pos = 0; |
universe@483 | 258 | } |
universe@483 | 259 | } |
universe@483 | 260 | return 0; |
universe@483 | 261 | } |
universe@483 | 262 | |
universe@483 | 263 | int cxBufferShiftRight( |
universe@500 | 264 | CxBuffer *buffer, |
universe@483 | 265 | size_t shift |
universe@483 | 266 | ) { |
universe@483 | 267 | size_t req_capacity = buffer->size + shift; |
universe@483 | 268 | size_t movebytes; |
universe@483 | 269 | |
universe@483 | 270 | // auto extend buffer, if required and enabled |
universe@483 | 271 | if (buffer->capacity < req_capacity) { |
universe@483 | 272 | if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) { |
universe@483 | 273 | if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
universe@483 | 274 | return 1; |
universe@483 | 275 | } |
universe@483 | 276 | movebytes = buffer->size; |
universe@483 | 277 | } else { |
universe@483 | 278 | movebytes = buffer->capacity - shift; |
universe@483 | 279 | } |
universe@483 | 280 | } else { |
universe@483 | 281 | movebytes = buffer->size; |
universe@483 | 282 | } |
universe@483 | 283 | |
universe@483 | 284 | memmove(buffer->bytes + shift, buffer->bytes, movebytes); |
universe@483 | 285 | buffer->size = shift + movebytes; |
universe@483 | 286 | |
universe@483 | 287 | buffer->pos += shift; |
universe@483 | 288 | if (buffer->pos > buffer->size) { |
universe@483 | 289 | buffer->pos = buffer->size; |
universe@483 | 290 | } |
universe@483 | 291 | |
universe@483 | 292 | return 0; |
universe@483 | 293 | } |
universe@483 | 294 | |
universe@483 | 295 | int cxBufferShift( |
universe@500 | 296 | CxBuffer *buffer, |
universe@483 | 297 | off_t shift |
universe@483 | 298 | ) { |
universe@483 | 299 | if (shift < 0) { |
universe@483 | 300 | return cxBufferShiftLeft(buffer, (size_t) (-shift)); |
universe@483 | 301 | } else if (shift > 0) { |
universe@483 | 302 | return cxBufferShiftRight(buffer, (size_t) shift); |
universe@483 | 303 | } else { |
universe@483 | 304 | return 0; |
universe@483 | 305 | } |
universe@483 | 306 | } |