| 1 |
|
| 2 |
|
| 3 |
rg |
| 4 |
. |
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
#include <ctype.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <limits.h> |
| 31 |
#include <math.h> |
| 32 |
#include <string.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdbool.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <stdarg.h> |
| 37 |
|
| 38 |
|
| 39 |
#define min(a, b) (((a) < (b)) ? (a) : (b)) |
| 40 |
|
| 41 |
static inline bool |
| 42 |
streq(const char *str1, const char *str2) |
| 43 |
{ |
| 44 |
|
| 45 |
if (str1 && str2) |
| 46 |
return strcmp(str1, str2) == 0; |
| 47 |
return str1 == str2; |
| 48 |
} |
| 49 |
|
| 50 |
static inline bool |
| 51 |
strneq(const char *str1, const char *str2, int n) |
| 52 |
{ |
| 53 |
|
| 54 |
if (str1 && str2) |
| 55 |
return strncmp(str1, str2, n) == 0; |
| 56 |
return str1 == str2; |
| 57 |
} |
| 58 |
|
| 59 |
static inline void * |
| 60 |
zalloc(size_t size) |
| 61 |
{ |
| 62 |
void *p; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
if (size > 1536 * 1024) |
| 67 |
assert(!"bug: internal malloc size limit exceeded"); |
| 68 |
|
| 69 |
p = calloc(1, size); |
| 70 |
if (!p) |
| 71 |
abort(); |
| 72 |
|
| 73 |
return p; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static inline char* |
| 82 |
safe_strdup(const char *str) |
| 83 |
{ |
| 84 |
char *s; |
| 85 |
|
| 86 |
if (!str) |
| 87 |
return NULL; |
| 88 |
|
| 89 |
s = strdup(str); |
| 90 |
if (!s) |
| 91 |
abort(); |
| 92 |
return s; |
| 93 |
} |
| 94 |
|
| 95 |
static inline bool |
| 96 |
safe_atoi_base(const char *str, int *val, int base) |
| 97 |
{ |
| 98 |
char *endptr; |
| 99 |
long v; |
| 100 |
|
| 101 |
assert(base == 10 || base == 16 || base == 8); |
| 102 |
|
| 103 |
errno = 0; |
| 104 |
v = strtol(str, &endptr, base); |
| 105 |
if (errno > 0) |
| 106 |
return false; |
| 107 |
if (str == endptr) |
| 108 |
return false; |
| 109 |
if (*str != '\0' && *endptr != '\0') |
| 110 |
return false; |
| 111 |
|
| 112 |
if (v > INT_MAX || v < INT_MIN) |
| 113 |
return false; |
| 114 |
|
| 115 |
*val = v; |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
static inline bool |
| 120 |
safe_atoi(const char *str, int *val) |
| 121 |
{ |
| 122 |
return safe_atoi_base(str, val, 10); |
| 123 |
} |
| 124 |
|
| 125 |
static inline bool |
| 126 |
safe_atou_base(const char *str, unsigned int *val, int base) |
| 127 |
{ |
| 128 |
char *endptr; |
| 129 |
unsigned long v; |
| 130 |
|
| 131 |
assert(base == 10 || base == 16 || base == 8); |
| 132 |
|
| 133 |
errno = 0; |
| 134 |
v = strtoul(str, &endptr, base); |
| 135 |
if (errno > 0) |
| 136 |
return false; |
| 137 |
if (str == endptr) |
| 138 |
return false; |
| 139 |
if (*str != '\0' && *endptr != '\0') |
| 140 |
return false; |
| 141 |
|
| 142 |
if ((long)v < 0) |
| 143 |
return false; |
| 144 |
|
| 145 |
*val = v; |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
static inline bool |
| 150 |
safe_atou(const char *str, unsigned int *val) |
| 151 |
{ |
| 152 |
return safe_atou_base(str, val, 10); |
| 153 |
} |
| 154 |
|
| 155 |
static inline bool |
| 156 |
safe_atod(const char *str, double *val) |
| 157 |
{ |
| 158 |
char *endptr; |
| 159 |
double v; |
| 160 |
#ifdef HAVE_LOCALE_H |
| 161 |
locale_t c_locale; |
| 162 |
#endif |
| 163 |
size_t slen = strlen(str); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
for (size_t i = 0; i < slen; i++) { |
| 168 |
char c = str[i]; |
| 169 |
|
| 170 |
if (isdigit(c)) |
| 171 |
continue; |
| 172 |
switch(c) { |
| 173 |
case '+': |
| 174 |
case '-': |
| 175 |
case '.': |
| 176 |
break; |
| 177 |
default: |
| 178 |
return false; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
#ifdef HAVE_LOCALE_H |
| 183 |
|
| 184 |
c_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0); |
| 185 |
if (c_locale == (locale_t)0) |
| 186 |
return false; |
| 187 |
|
| 188 |
errno = 0; |
| 189 |
v = strtod_l(str, &endptr, c_locale); |
| 190 |
freelocale(c_locale); |
| 191 |
#else |
| 192 |
|
| 193 |
errno = 0; |
| 194 |
v = strtod(str, &endptr); |
| 195 |
#endif |
| 196 |
if (errno > 0) |
| 197 |
return false; |
| 198 |
if (str == endptr) |
| 199 |
return false; |
| 200 |
if (*str != '\0' && *endptr != '\0') |
| 201 |
return false; |
| 202 |
if (v != 0.0 && !isnormal(v)) |
| 203 |
return false; |
| 204 |
|
| 205 |
*val = v; |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
char **strv_from_argv(int argc, char **argv); |
| 210 |
char **strv_from_string(const char *in, const char *separator, size_t *num_elements); |
| 211 |
char *strv_join(char **strv, const char *joiner); |
| 212 |
|
| 213 |
static inline void |
| 214 |
strv_free(char **strv) { |
| 215 |
char **s = strv; |
| 216 |
|
| 217 |
if (!strv) |
| 218 |
return; |
| 219 |
|
| 220 |
while (*s != NULL) { |
| 221 |
free(*s); |
| 222 |
*s = (char*)0x1; |
| 223 |
s++; |
| 224 |
} |
| 225 |
|
| 226 |
free (strv); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static inline double * |
| 239 |
double_array_from_string(const char *in, |
| 240 |
const char *separator, |
| 241 |
size_t *length) |
| 242 |
{ |
| 243 |
double *result = NULL; |
| 244 |
*length = 0; |
| 245 |
|
| 246 |
size_t nelem; |
| 247 |
char **strv = strv_from_string(in, separator, &nelem); |
| 248 |
if (!strv) |
| 249 |
return result; |
| 250 |
|
| 251 |
double *numv = zalloc(sizeof(double) * nelem); |
| 252 |
for (size_t idx = 0; idx < nelem; idx++) { |
| 253 |
double val; |
| 254 |
if (!safe_atod(strv[idx], &val)) |
| 255 |
goto out; |
| 256 |
|
| 257 |
numv[idx] = val; |
| 258 |
} |
| 259 |
|
| 260 |
result = numv; |
| 261 |
numv = NULL; |
| 262 |
*length = nelem; |
| 263 |
|
| 264 |
out: |
| 265 |
strv_free(strv); |
| 266 |
free(numv); |
| 267 |
return result; |
| 268 |
} |
| 269 |
|
| 270 |
struct key_value_str{ |
| 271 |
char *key; |
| 272 |
char *value; |
| 273 |
}; |
| 274 |
|
| 275 |
struct key_value_double { |
| 276 |
double key; |
| 277 |
double value; |
| 278 |
}; |
| 279 |
|
| 280 |
static inline ssize_t |
| 281 |
kv_double_from_string(const char *string, |
| 282 |
const char *pair_separator, |
| 283 |
const char *kv_separator, |
| 284 |
struct key_value_double **result_out) |
| 285 |
|
| 286 |
{ |
| 287 |
struct key_value_double *result = NULL; |
| 288 |
|
| 289 |
if (!pair_separator || pair_separator[0] == '\0' || |
| 290 |
!kv_separator || kv_separator[0] == '\0') |
| 291 |
return -1; |
| 292 |
|
| 293 |
size_t npairs; |
| 294 |
char **pairs = strv_from_string(string, pair_separator, &npairs); |
| 295 |
if (!pairs || npairs == 0) |
| 296 |
goto error; |
| 297 |
|
| 298 |
result = zalloc(npairs * sizeof *result); |
| 299 |
|
| 300 |
for (size_t idx = 0; idx < npairs; idx++) { |
| 301 |
char *pair = pairs[idx]; |
| 302 |
size_t nelem; |
| 303 |
char **kv = strv_from_string(pair, kv_separator, &nelem); |
| 304 |
double k, v; |
| 305 |
|
| 306 |
if (!kv || nelem != 2 || |
| 307 |
!safe_atod(kv[0], &k) || |
| 308 |
!safe_atod(kv[1], &v)) { |
| 309 |
strv_free(kv); |
| 310 |
goto error; |
| 311 |
} |
| 312 |
|
| 313 |
result[idx].key = k; |
| 314 |
result[idx].value = v; |
| 315 |
|
| 316 |
strv_free(kv); |
| 317 |
} |
| 318 |
|
| 319 |
strv_free(pairs); |
| 320 |
|
| 321 |
*result_out = result; |
| 322 |
|
| 323 |
return npairs; |
| 324 |
|
| 325 |
error: |
| 326 |
strv_free(pairs); |
| 327 |
free(result); |
| 328 |
return -1; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
static inline char * |
| 339 |
strstrip(const char *input, const char *what) |
| 340 |
{ |
| 341 |
char *str, *last; |
| 342 |
|
| 343 |
str = safe_strdup(&input[strspn(input, what)]); |
| 344 |
|
| 345 |
last = str; |
| 346 |
|
| 347 |
for (char *c = str; *c != '\0'; c++) { |
| 348 |
if (!strchr(what, *c)) |
| 349 |
last = c + 1; |
| 350 |
} |
| 351 |
|
| 352 |
*last = '\0'; |
| 353 |
|
| 354 |
return str; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
static inline bool |
| 362 |
strendswith(const char *str, const char *suffix) |
| 363 |
{ |
| 364 |
size_t slen = strlen(str); |
| 365 |
size_t suffixlen = strlen(suffix); |
| 366 |
size_t offset; |
| 367 |
|
| 368 |
if (slen == 0 || suffixlen == 0 || suffixlen > slen) |
| 369 |
return false; |
| 370 |
|
| 371 |
offset = slen - suffixlen; |
| 372 |
return strneq(&str[offset], suffix, suffixlen); |
| 373 |
} |
| 374 |
|
| 375 |
static inline bool |
| 376 |
strstartswith(const char *str, const char *prefix) |
| 377 |
{ |
| 378 |
size_t prefixlen = strlen(prefix); |
| 379 |
|
| 380 |
return prefixlen > 0 ? strneq(str, prefix, strlen(prefix)) : false; |
| 381 |
} |
| 382 |
|
| 383 |
const char * |
| 384 |
safe_basename(const char *filename); |
| 385 |
|
| 386 |
char * |
| 387 |
trunkname(const char *filename); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
static inline char * |
| 394 |
str_sanitize(const char *str) |
| 395 |
{ |
| 396 |
if (!str) |
| 397 |
return NULL; |
| 398 |
|
| 399 |
if (!strchr(str, '%')) |
| 400 |
return strdup(str); |
| 401 |
|
| 402 |
size_t slen = min(strlen(str), 512); |
| 403 |
char *sanitized = zalloc(2 * slen + 1); |
| 404 |
const char *src = str; |
| 405 |
char *dst = sanitized; |
| 406 |
|
| 407 |
for (size_t i = 0; i < slen; i++) { |
| 408 |
if (*src == '%') |
| 409 |
*dst++ = '%'; |
| 410 |
*dst++ = *src++; |
| 411 |
} |
| 412 |
*dst = '\0'; |
| 413 |
|
| 414 |
return sanitized; |
| 415 |
} |