ucx/utils.c

Wed, 11 Jun 2014 09:27:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 11 Jun 2014 09:27:02 +0200
changeset 174
bbfe511cfddb
parent 173
31a8682fffb7
child 177
11ad03783baf
permissions
-rw-r--r--

fixed missing malloc return value validation in ucx_vasprintf

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@103 4 * Copyright 2013 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@94 29 #include "utils.h"
universe@140 30 #include <math.h>
olaf@142 31 #include <stdio.h>
olaf@142 32 #include <limits.h>
olaf@142 33 #include <errno.h>
universe@89 34
universe@94 35 /* COPY FUCNTIONS */
universe@94 36 void* ucx_strcpy(void* s, void* data) {
universe@94 37 char *str = (char*) s;
universe@94 38 size_t n = 1+strlen(str);
universe@94 39 char *cpy = (char*) malloc(n);
universe@94 40 memcpy(cpy, str, n);
universe@94 41 return cpy;
universe@94 42 }
universe@94 43
universe@94 44 void* ucx_memcpy(void* m, void* n) {
universe@94 45 size_t k = *((size_t*)n);
universe@94 46 void *cpy = malloc(k);
universe@94 47 memcpy(cpy, m, k);
universe@94 48 return cpy;
universe@94 49 }
universe@94 50
universe@140 51 size_t ucx_stream_copy(void *src, void *dest, read_func readfnc,
universe@140 52 write_func writefnc, char* buf, size_t bufsize, size_t n) {
universe@140 53 if(n == 0 || bufsize == 0) {
universe@140 54 return 0;
universe@140 55 }
universe@140 56
universe@140 57 size_t ncp = 0;
universe@140 58 if (!buf) {
universe@140 59 buf = (char*)malloc(bufsize);
universe@140 60 if(buf == NULL) {
universe@140 61 return 0;
universe@140 62 }
universe@140 63 }
universe@140 64
universe@140 65 size_t r;
universe@140 66 size_t rn = bufsize > n ? n : bufsize;
universe@140 67 while((r = readfnc(buf, 1, rn, src)) != 0) {
universe@140 68 r = writefnc(buf, 1, r, dest);
universe@140 69 ncp += r;
universe@140 70 n -= r;
universe@140 71 rn = bufsize > n ? n : bufsize;
universe@140 72 if(r == 0 || n == 0) {
universe@140 73 break;
universe@140 74 }
universe@140 75 }
universe@140 76
universe@140 77 free(buf);
universe@140 78 return ncp;
universe@140 79 }
universe@140 80
olaf@142 81 /* COMPARE FUNCTIONS */
universe@94 82
universe@89 83 int ucx_strcmp(void *s1, void *s2, void *data) {
universe@89 84 return strcmp((char*)s1, (char*)s2);
universe@89 85 }
universe@89 86
universe@89 87 int ucx_strncmp(void *s1, void *s2, void *n) {
universe@89 88 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
universe@89 89 }
universe@89 90
universe@89 91 int ucx_intcmp(void *i1, void *i2, void *data) {
universe@89 92 int a = *((int*) i1);
universe@89 93 int b = *((int*) i2);
universe@89 94 if (a == b) {
universe@89 95 return 0;
universe@89 96 } else {
universe@89 97 return a < b ? -1 : 1;
universe@89 98 }
universe@89 99 }
universe@89 100
universe@92 101 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
universe@92 102 float a = *((float*) f1);
universe@92 103 float b = *((float*) f2);
universe@92 104 float e = !epsilon ? 1e-6f : *((float*)epsilon);
universe@92 105 if (fabsf(a - b) < e) {
universe@92 106 return 0;
universe@92 107 } else {
universe@92 108 return a < b ? -1 : 1;
universe@92 109 }
universe@92 110 }
universe@92 111
universe@92 112 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
universe@92 113 double a = *((float*) d1);
universe@92 114 double b = *((float*) d2);
universe@92 115 double e = !epsilon ? 1e-14 : *((double*)epsilon);
universe@92 116 if (fabs(a - b) < e) {
universe@92 117 return 0;
universe@92 118 } else {
universe@92 119 return a < b ? -1 : 1;
universe@92 120 }
universe@92 121 }
universe@92 122
universe@89 123 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
universe@89 124 if (ptr1 == ptr2) {
universe@89 125 return 0;
universe@89 126 } else {
universe@89 127 return ptr1 < ptr2 ? -1 : 1;
universe@89 128 }
universe@89 129 }
universe@91 130
universe@91 131 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
universe@91 132 return memcmp(ptr1, ptr2, *((size_t*)n));
universe@91 133 }
olaf@142 134
olaf@142 135 /* PRINTF FUNCTIONS */
olaf@142 136
universe@150 137 #ifdef va_copy
olaf@142 138 #define UCX_PRINTF_BUFSIZE 256
universe@150 139 #else
universe@150 140 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
universe@150 141 " - limiting ucx_*printf to 2 KiB")
universe@150 142 #define UCX_PRINTF_BUFSIZE 0x800
universe@150 143 #endif
olaf@142 144
olaf@142 145 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
universe@150 146 int ret;
olaf@142 147 va_list ap;
olaf@142 148 va_start(ap, fmt);
olaf@142 149 ret = ucx_vfprintf(stream, wfc, fmt, ap);
olaf@142 150 va_end(ap);
olaf@142 151 return ret;
olaf@142 152 }
olaf@142 153
olaf@142 154 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
olaf@142 155 char buf[UCX_PRINTF_BUFSIZE];
universe@150 156 #ifdef va_copy
olaf@144 157 va_list ap2;
olaf@144 158 va_copy(ap2, ap);
olaf@142 159 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
olaf@142 160 if (ret < 0) {
olaf@142 161 return ret;
olaf@142 162 } else if (ret < UCX_PRINTF_BUFSIZE) {
olaf@142 163 return (int)wfc(buf, 1, ret, stream);
olaf@142 164 } else {
olaf@142 165 if (ret == INT_MAX) {
olaf@142 166 errno = ENOMEM;
olaf@142 167 return -1;
olaf@142 168 }
olaf@142 169
olaf@142 170 int len = ret + 1;
olaf@142 171 char *newbuf = (char*)malloc(len);
olaf@142 172 if (!newbuf) {
olaf@142 173 return -1;
olaf@142 174 }
olaf@142 175
olaf@144 176 ret = vsnprintf(newbuf, len, fmt, ap2);
olaf@142 177 if (ret > 0) {
olaf@142 178 ret = (int)wfc(newbuf, 1, ret, stream);
olaf@142 179 }
olaf@142 180 free(newbuf);
olaf@142 181 }
olaf@142 182 return ret;
universe@150 183 #else
universe@150 184 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@150 185 if (ret < 0) {
universe@150 186 return ret;
universe@150 187 } else if (ret < UCX_PRINTF_BUFSIZE) {
universe@150 188 return (int)wfc(buf, 1, ret, stream);
universe@150 189 } else {
universe@150 190 errno = ENOMEM;
universe@150 191 return -1;
universe@150 192 }
universe@150 193 #endif
olaf@142 194 }
olaf@142 195
olaf@142 196 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
olaf@142 197 va_list ap;
olaf@142 198 sstr_t ret;
olaf@142 199 va_start(ap, fmt);
olaf@142 200 ret = ucx_vasprintf(allocator, fmt, ap);
olaf@142 201 va_end(ap);
olaf@142 202 return ret;
olaf@142 203 }
olaf@142 204
olaf@142 205 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
olaf@142 206 sstr_t s;
olaf@142 207 s.ptr = NULL;
olaf@142 208 s.length = 0;
universe@150 209 char buf[UCX_PRINTF_BUFSIZE];
universe@150 210 #ifdef va_copy
olaf@144 211 va_list ap2;
olaf@144 212 va_copy(ap2, ap);
olaf@142 213 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
olaf@142 214 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@173 215 s.ptr = (char*)almalloc(a, ret + 1);
universe@174 216 if (s.ptr) {
universe@174 217 s.length = (size_t)ret;
universe@174 218 memcpy(s.ptr, buf, ret);
universe@174 219 s.ptr[s.length] = '\0';
universe@174 220 }
olaf@142 221 } else if (ret == INT_MAX) {
olaf@142 222 errno = ENOMEM;
olaf@142 223 } else {
olaf@142 224 int len = ret + 1;
universe@173 225 s.ptr = (char*)almalloc(a, len);
universe@174 226 if (s.ptr) {
universe@174 227 ret = vsnprintf(s.ptr, len, fmt, ap2);
universe@174 228 if (ret < 0) {
universe@174 229 free(s.ptr);
universe@174 230 s.ptr = NULL;
universe@174 231 } else {
universe@174 232 s.length = (size_t)ret;
universe@174 233 }
olaf@142 234 }
olaf@142 235 }
universe@150 236 #else
universe@150 237 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@150 238 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@173 239 s.ptr = (char*)almalloc(a, ret + 1);
universe@174 240 if (s.ptr) {
universe@174 241 s.length = (size_t)ret;
universe@174 242 memcpy(s.ptr, buf, ret);
universe@174 243 s.ptr[s.length] = '\0';
universe@174 244 }
universe@150 245 } else {
universe@150 246 errno = ENOMEM;
universe@150 247 }
universe@150 248 #endif
olaf@142 249 return s;
olaf@142 250 }

mercurial