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