| 1 |
/* |
| 2 |
* Copyright © 2012 Ran Benita <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include "config.h" |
| 8 |
|
| 9 |
#include <errno.h> |
| 10 |
#include <inttypes.h> |
| 11 |
#include <stdarg.h> |
| 12 |
#include <stdbool.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#if HAVE_UNISTD_H |
| 16 |
# include <unistd.h> |
| 17 |
#else |
| 18 |
/* Required on Windows where unistd.h doesn't exist */ |
| 19 |
# define R_OK 4 /* Test for read permission. */ |
| 20 |
# define W_OK 2 /* Test for write permission. */ |
| 21 |
# define X_OK 1 /* Test for execute permission. */ |
| 22 |
# define F_OK 0 /* Test for existence. */ |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifdef _WIN32 |
| 26 |
# include <direct.h> |
| 27 |
# include <io.h> |
| 28 |
# include <BaseTsd.h> |
| 29 |
# ifndef S_ISDIR |
| 30 |
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 31 |
# endif |
| 32 |
# ifndef S_ISREG |
| 33 |
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
| 34 |
# endif |
| 35 |
#ifdef _MSC_VER |
| 36 |
typedef SSIZE_T ssize_t; |
| 37 |
#endif |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "darray.h" |
| 41 |
|
| 42 |
static inline bool |
| 43 |
is_aligned(const void *restrict pointer, size_t byte_count) |
| 44 |
{ |
| 45 |
return (uintptr_t) pointer % byte_count == 0; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
#define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr)))) |
| 50 |
|
| 51 |
#define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 52 |
#define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 53 |
|
| 54 |
/* Round up @a so it's divisible by @b. */ |
| 55 |
#define ROUNDUP(a, b) (((a) + (b) - 1) / (b) * (b)) |
| 56 |
|
| 57 |
#define STRINGIFY(x) #x |
| 58 |
#define STRINGIFY2(x) STRINGIFY(x) |
| 59 |
#define CONCAT(x,y) x ## y |
| 60 |
#define CONCAT2(x,y) CONCAT(x,y) |
| 61 |
|
| 62 |
/* Check if a Unicode code point is valid in a string literal */ |
| 63 |
static inline bool |
| 64 |
is_valid_char(uint32_t cp) |
| 65 |
{ |
| 66 |
/* Currently we only check for NULL character, but this could be extended |
| 67 |
* in the future to further ASCII control characters. */ |
| 68 |
return cp != 0; |
| 69 |
} |
| 70 |
|
| 71 |
/* Check if a Unicode code point is a surrogate. |
| 72 |
* Those code points are used only in UTF-16 encodings. */ |
| 73 |
static inline bool |
| 74 |
is_surrogate(uint32_t cp) |
| 75 |
{ |
| 76 |
return (cp >= 0xd800 && cp <= 0xdfff); |
| 77 |
} |
| 78 |
|
| 79 |
char |
| 80 |
to_lower(char c); |
| 81 |
|
| 82 |
int |
| 83 |
istrcmp(const char *a, const char *b); |
| 84 |
|
| 85 |
int |
| 86 |
istrncmp(const char *a, const char *b, size_t n); |
| 87 |
|
| 88 |
static inline bool |
| 89 |
streq(const char *s1, const char *s2) |
| 90 |
{ |
| 91 |
assert(s1 && s2); |
| 92 |
return strcmp(s1, s2) == 0; |
| 93 |
} |
| 94 |
|
| 95 |
static inline bool |
| 96 |
streq_null(const char *s1, const char *s2) |
| 97 |
{ |
| 98 |
if (s1 == NULL || s2 == NULL) |
| 99 |
return s1 == s2; |
| 100 |
return streq(s1, s2); |
| 101 |
} |
| 102 |
|
| 103 |
static inline bool |
| 104 |
streq_not_null(const char *s1, const char *s2) |
| 105 |
{ |
| 106 |
if (!s1 || !s2) |
| 107 |
return false; |
| 108 |
return streq(s1, s2); |
| 109 |
} |
| 110 |
|
| 111 |
static inline bool |
| 112 |
istreq(const char *s1, const char *s2) |
| 113 |
{ |
| 114 |
return istrcmp(s1, s2) == 0; |
| 115 |
} |
| 116 |
|
| 117 |
static inline bool |
| 118 |
istrneq(const char *s1, const char *s2, size_t len) |
| 119 |
{ |
| 120 |
return istrncmp(s1, s2, len) == 0; |
| 121 |
} |
| 122 |
|
| 123 |
#define istreq_prefix(s1, s2) istrneq(s1, s2, sizeof(s1) - 1) |
| 124 |
|
| 125 |
static inline char * |
| 126 |
strcpy_safe(char *dest, size_t size, const char *src) { |
| 127 |
if (!dest || !size || !src) |
| 128 |
return NULL; |
| 129 |
const char * const limit = dest + size - 1; |
| 130 |
while (dest < limit && *src) { |
| 131 |
*dest++ = *src++; |
| 132 |
} |
| 133 |
*dest = '\0'; |
| 134 |
return *src ? NULL : dest; |
| 135 |
} |
| 136 |
|
| 137 |
static inline char * |
| 138 |
strdup_safe(const char *s) |
| 139 |
{ |
| 140 |
return s ? strdup(s) : NULL; |
| 141 |
} |
| 142 |
|
| 143 |
static inline size_t |
| 144 |
strlen_safe(const char *s) |
| 145 |
{ |
| 146 |
return s ? strlen(s) : 0; |
| 147 |
} |
| 148 |
|
| 149 |
static inline bool |
| 150 |
isempty(const char *s) |
| 151 |
{ |
| 152 |
return s == NULL || s[0] == '\0'; |
| 153 |
} |
| 154 |
|
| 155 |
static inline const char * |
| 156 |
strnull(const char *s) |
| 157 |
{ |
| 158 |
return s ? s : "(null)"; |
| 159 |
} |
| 160 |
|
| 161 |
static inline const char * |
| 162 |
strempty(const char *s) |
| 163 |
{ |
| 164 |
return s ? s : ""; |
| 165 |
} |
| 166 |
|
| 167 |
static inline void * |
| 168 |
memdup(const void *mem, size_t nmemb, size_t size) |
| 169 |
{ |
| 170 |
void *p = calloc(nmemb, size); |
| 171 |
if (p) |
| 172 |
memcpy(p, mem, nmemb * size); |
| 173 |
return p; |
| 174 |
} |
| 175 |
|
| 176 |
#if !(defined(HAVE_STRNDUP) && HAVE_STRNDUP) |
| 177 |
static inline char * |
| 178 |
strndup(const char *s, size_t n) |
| 179 |
{ |
| 180 |
size_t slen = strlen(s); |
| 181 |
size_t len = MIN(slen, n); |
| 182 |
char *p = malloc(len + 1); |
| 183 |
if (!p) |
| 184 |
return NULL; |
| 185 |
memcpy(p, s, len); |
| 186 |
p[len] = '\0'; |
| 187 |
return p; |
| 188 |
} |
| 189 |
#endif |
| 190 |
|
| 191 |
/* ctype.h is locale-dependent and has other oddities. */ |
| 192 |
static inline bool |
| 193 |
is_ascii(char ch) |
| 194 |
{ |
| 195 |
return (ch & ~0x7f) == 0; |
| 196 |
} |
| 197 |
|
| 198 |
static inline bool |
| 199 |
is_space(char ch) |
| 200 |
{ |
| 201 |
return ch == ' ' || (ch >= '\t' && ch <= '\r'); |
| 202 |
} |
| 203 |
|
| 204 |
static inline bool |
| 205 |
is_alpha(char ch) |
| 206 |
{ |
| 207 |
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); |
| 208 |
} |
| 209 |
|
| 210 |
static inline bool |
| 211 |
is_digit(char ch) |
| 212 |
{ |
| 213 |
return ch >= '0' && ch <= '9'; |
| 214 |
} |
| 215 |
|
| 216 |
static inline bool |
| 217 |
is_alnum(char ch) |
| 218 |
{ |
| 219 |
return is_alpha(ch) || is_digit(ch); |
| 220 |
} |
| 221 |
|
| 222 |
static inline bool |
| 223 |
is_xdigit(char ch) |
| 224 |
{ |
| 225 |
return |
| 226 |
(ch >= '0' && ch <= '9') || |
| 227 |
(ch >= 'a' && ch <= 'f') || |
| 228 |
(ch >= 'A' && ch <= 'F'); |
| 229 |
} |
| 230 |
|
| 231 |
static inline bool |
| 232 |
is_graph(char ch) |
| 233 |
{ |
| 234 |
/* See table in ascii(7). */ |
| 235 |
return ch >= '!' && ch <= '~'; |
| 236 |
} |
| 237 |
|
| 238 |
/* |
| 239 |
* Return the bit position of the most significant bit. |
| 240 |
* Note: this is 1-based! It's more useful this way, and returns 0 when |
| 241 |
* mask is all 0s. |
| 242 |
*/ |
| 243 |
static inline unsigned int |
| 244 |
msb_pos(uint32_t mask) |
| 245 |
{ |
| 246 |
unsigned int pos = 0; |
| 247 |
while (mask) { |
| 248 |
pos++; |
| 249 |
mask >>= 1u; |
| 250 |
} |
| 251 |
return pos; |
| 252 |
} |
| 253 |
|
| 254 |
static inline int |
| 255 |
one_bit_set(uint32_t x) |
| 256 |
{ |
| 257 |
return x && (x & (x - 1)) == 0; |
| 258 |
} |
| 259 |
|
| 260 |
bool |
| 261 |
map_file(FILE *file, char **string_out, size_t *size_out); |
| 262 |
|
| 263 |
void |
| 264 |
unmap_file(char *string, size_t size); |
| 265 |
|
| 266 |
static inline bool |
| 267 |
check_eaccess(const char *path, int mode) |
| 268 |
{ |
| 269 |
#if defined(HAVE_EACCESS) |
| 270 |
if (eaccess(path, mode) != 0) |
| 271 |
return false; |
| 272 |
#elif defined(HAVE_EUIDACCESS) |
| 273 |
if (euidaccess(path, mode) != 0) |
| 274 |
return false; |
| 275 |
#endif |
| 276 |
|
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
FILE* |
| 281 |
open_file(const char *path); |
| 282 |
|
| 283 |
#if defined(HAVE_SECURE_GETENV) |
| 284 |
# define secure_getenv secure_getenv |
| 285 |
#elif defined(HAVE___SECURE_GETENV) |
| 286 |
# define secure_getenv __secure_getenv |
| 287 |
#else |
| 288 |
# define secure_getenv getenv |
| 289 |
#endif |
| 290 |
|
| 291 |
#if defined(HAVE___BUILTIN_EXPECT) |
| 292 |
# define likely(x) __builtin_expect(!!(x), 1) |
| 293 |
# define unlikely(x) __builtin_expect(!!(x), 0) |
| 294 |
#else |
| 295 |
# define likely(x) (x) |
| 296 |
# define unlikely(x) (x) |
| 297 |
#endif |
| 298 |
|
| 299 |
/* Compiler Attributes */ |
| 300 |
|
| 301 |
/* Private functions only exposed in tests. */ |
| 302 |
#ifdef ENABLE_PRIVATE_APIS |
| 303 |
# if defined(__GNUC__) && !defined(__CYGWIN__) |
| 304 |
# define XKB_EXPORT_PRIVATE __attribute__((visibility("default"))) |
| 305 |
# elif defined(_WIN32) |
| 306 |
# define XKB_EXPORT_PRIVATE __declspec(dllexport) |
| 307 |
# else |
| 308 |
# define XKB_EXPORT_PRIVATE |
| 309 |
# endif |
| 310 |
#else |
| 311 |
# define XKB_EXPORT_PRIVATE |
| 312 |
#endif |
| 313 |
|
| 314 |
#if defined(__MINGW32__) |
| 315 |
# define ATTR_PRINTF(x,y) __attribute__((__format__(__MINGW_PRINTF_FORMAT, x, y))) |
| 316 |
#elif defined(__GNUC__) |
| 317 |
# define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y))) |
| 318 |
#else |
| 319 |
# define ATTR_PRINTF(x,y) |
| 320 |
#endif |
| 321 |
|
| 322 |
#if defined(__GNUC__) |
| 323 |
# define ATTR_NORETURN __attribute__((__noreturn__)) |
| 324 |
#else |
| 325 |
# define ATTR_NORETURN |
| 326 |
#endif /* GNUC */ |
| 327 |
|
| 328 |
#if defined(__GNUC__) |
| 329 |
#define ATTR_MALLOC __attribute__((__malloc__)) |
| 330 |
#else |
| 331 |
#define ATTR_MALLOC |
| 332 |
#endif |
| 333 |
|
| 334 |
#if defined(__GNUC__) |
| 335 |
# define ATTR_NULL_SENTINEL __attribute__((__sentinel__)) |
| 336 |
#else |
| 337 |
# define ATTR_NULL_SENTINEL |
| 338 |
#endif |
| 339 |
|
| 340 |
#if defined(__GNUC__) |
| 341 |
#define ATTR_PACKED __attribute__((__packed__)) |
| 342 |
#else |
| 343 |
#define ATTR_PACKED |
| 344 |
#endif |
| 345 |
|
| 346 |
#if !(defined(HAVE_ASPRINTF) && HAVE_ASPRINTF) |
| 347 |
XKB_EXPORT_PRIVATE int asprintf(char **strp, const char *fmt, ...) ATTR_PRINTF(2, 3); |
| 348 |
# if !(defined(HAVE_VASPRINTF) && HAVE_VASPRINTF) |
| 349 |
# include <stdarg.h> |
| 350 |
XKB_EXPORT_PRIVATE int vasprintf(char **strp, const char *fmt, va_list ap); |
| 351 |
# endif /* !HAVE_VASPRINTF */ |
| 352 |
#endif /* !HAVE_ASPRINTF */ |
| 353 |
|
| 354 |
static inline bool |
| 355 |
ATTR_PRINTF(3, 4) |
| 356 |
snprintf_safe(char *buf, size_t sz, const char *format, ...) |
| 357 |
{ |
| 358 |
va_list ap; |
| 359 |
int rc; |
| 360 |
|
| 361 |
va_start(ap, format); |
| 362 |
rc = vsnprintf(buf, sz, format, ap); |
| 363 |
va_end(ap); |
| 364 |
|
| 365 |
return rc >= 0 && (size_t)rc < sz; |
| 366 |
} |
| 367 |
|
| 368 |
static inline char * |
| 369 |
ATTR_PRINTF(1, 0) |
| 370 |
vasprintf_safe(const char *fmt, va_list args) |
| 371 |
{ |
| 372 |
char *str; |
| 373 |
int len; |
| 374 |
|
| 375 |
len = vasprintf(&str, fmt, args); |
| 376 |
|
| 377 |
if (len == -1) |
| 378 |
return NULL; |
| 379 |
|
| 380 |
return str; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* A version of asprintf that returns the allocated string or NULL on error. |
| 385 |
*/ |
| 386 |
static inline char * |
| 387 |
ATTR_PRINTF(1, 2) |
| 388 |
asprintf_safe(const char *fmt, ...) |
| 389 |
{ |
| 390 |
va_list args; |
| 391 |
char *str; |
| 392 |
|
| 393 |
va_start(args, fmt); |
| 394 |
str = vasprintf_safe(fmt, args); |
| 395 |
va_end(args); |
| 396 |
|
| 397 |
return str; |
| 398 |
} |