extensions.h
| 1 | #ifndef CUTILS_EXTENSIONS_H |
| 2 | #define CUTILS_EXTENSIONS_H |
| 3 | |
| 4 | #include <cutils/hedley.h> |
| 5 | #include <errno.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <cutils/backport.h> |
| 8 | #include <string.h> |
| 9 | #include <ctype.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <stdio.h> |
| 12 | #include <cutils/common.h> |
| 13 | |
| 14 | int cutil_strcasecmp(const char* s1, const char* s2); |
| 15 | int cutil_strncasecmp(const char* s1, const char* s2, size_t n); |
| 16 | |
| 17 | size_t cutil_strnlen(const char* s, size_t maxlen); |
| 18 | |
| 19 | #define cutil_strdup(s) cutil_strndup(s, strlen((const char*)s)) |
| 20 | char* cutil_strndup(const char* s, size_t n); |
| 21 | |
| 22 | bool cutil_memmem(const void* haystack, size_t haystacklen, const void* needle, size_t needlelen, size_t* pos); |
| 23 | |
| 24 | #if __STDC_VERSION__ >= 199901L |
| 25 | int cutil_asprintf(char** strp, const char* format, ...); |
| 26 | #endif |
| 27 | |
| 28 | HEDLEY_INLINE |
| 29 | static void* cutil_reallocarray(void *ptr, size_t nmemb, size_t size) |
| 30 | { |
| 31 | size_t new_size = nmemb * size; |
| 32 | |
| 33 | if(nmemb != 0 && new_size / nmemb != size) |
| 34 | { |
| 35 | errno = ENOMEM; |
| 36 | return NULL; |
| 37 | } else { |
| 38 | return realloc(ptr, new_size); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | HEDLEY_INLINE |
| 43 | static void* cutil_reallocarray_inc(void *ptr, size_t nmemb, size_t size, size_t add) |
| 44 | { |
| 45 | if(SIZE_MAX - add < size) |
| 46 | { |
| 47 | errno = ENOMEM; |
| 48 | return NULL; |
| 49 | } else { |
| 50 | return cutil_reallocarray(ptr, nmemb+add, size); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | #endif /* CUTILS_EXTENSIONS_H */ |
| 57 |