diff options
Diffstat (limited to 'src/common/linux/linux_libc_support.h')
-rw-r--r-- | src/common/linux/linux_libc_support.h | 141 |
1 files changed, 29 insertions, 112 deletions
diff --git a/src/common/linux/linux_libc_support.h b/src/common/linux/linux_libc_support.h index 2ef2dc11..b2f47af8 100644 --- a/src/common/linux/linux_libc_support.h +++ b/src/common/linux/linux_libc_support.h @@ -40,83 +40,21 @@ extern "C" { -static inline size_t -my_strlen(const char* s) { - size_t len = 0; - while (*s++) len++; - return len; -} - -static inline int -my_strcmp(const char* a, const char* b) { - for (;;) { - if (*a < *b) - return -1; - else if (*a > *b) - return 1; - else if (*a == 0) - return 0; - a++; - b++; - } -} - -static inline int -my_strncmp(const char* a, const char* b, size_t len) { - for (size_t i = 0; i < len; ++i) { - if (*a < *b) - return -1; - else if (*a > *b) - return 1; - else if (*a == 0) - return 0; - a++; - b++; - } - - return 0; -} +extern size_t my_strlen(const char* s); + +extern int my_strcmp(const char* a, const char* b); + +extern int my_strncmp(const char* a, const char* b, size_t len); // Parse a non-negative integer. // result: (output) the resulting non-negative integer // s: a NUL terminated string // Return true iff successful. -static inline bool -my_strtoui(int* result, const char* s) { - if (*s == 0) - return false; - int r = 0; - for (;; s++) { - if (*s == 0) - break; - const int old_r = r; - r *= 10; - if (*s < '0' || *s > '9') - return false; - r += *s - '0'; - if (r < old_r) - return false; - } - - *result = r; - return true; -} +extern bool my_strtoui(int* result, const char* s); // Return the length of the given, non-negative integer when expressed in base // 10. -static inline unsigned -my_int_len(intmax_t i) { - if (!i) - return 1; - - int len = 0; - while (i) { - len++; - i /= 10; - } - - return len; -} +extern unsigned my_int_len(intmax_t i); // Convert a non-negative integer to a string // output: (output) the resulting string is written here. This buffer must be @@ -124,54 +62,33 @@ my_int_len(intmax_t i) { // required length. // i: the non-negative integer to serialise. // i_len: the length of the integer in base 10 (see |my_int_len|). -static inline void -my_itos(char* output, intmax_t i, unsigned i_len) { - for (unsigned index = i_len; index; --index, i /= 10) - output[index - 1] = '0' + (i % 10); -} - -static inline const char* -my_strchr(const char* haystack, char needle) { - while (*haystack && *haystack != needle) - haystack++; - if (*haystack == needle) - return haystack; - return (const char*) 0; -} +extern void my_itos(char* output, intmax_t i, unsigned i_len); + +extern const char* my_strchr(const char* haystack, char needle); + +extern const char* my_strrchr(const char* haystack, char needle); // Read a hex value // result: (output) the resulting value // s: a string // Returns a pointer to the first invalid charactor. -static inline const char* -my_read_hex_ptr(uintptr_t* result, const char* s) { - uintptr_t r = 0; - - for (;; ++s) { - if (*s >= '0' && *s <= '9') { - r <<= 4; - r += *s - '0'; - } else if (*s >= 'a' && *s <= 'f') { - r <<= 4; - r += (*s - 'a') + 10; - } else if (*s >= 'A' && *s <= 'F') { - r <<= 4; - r += (*s - 'A') + 10; - } else { - break; - } - } - - *result = r; - return s; -} - -static inline void -my_memset(void* ip, char c, size_t len) { - char* p = (char *) ip; - while (len--) - *p++ = c; -} +extern const char* my_read_hex_ptr(uintptr_t* result, const char* s); + +extern const char* my_read_decimal_ptr(uintptr_t* result, const char* s); + +extern void my_memset(void* ip, char c, size_t len); + +// The following are considered safe to use in a compromised environment. +// Besides, this gives the compiler an opportunity to optimize their calls. +#define my_memcpy memcpy +#define my_memmove memmove +#define my_memcmp memcmp + +extern size_t my_strlcpy(char* s1, const char* s2, size_t len); + +extern size_t my_strlcat(char* s1, const char* s2, size_t len); + +extern int my_isspace(int ch); } // extern "C" |