diff options
Diffstat (limited to 'lib/string.c')
-rw-r--r-- | lib/string.c | 181 |
1 files changed, 149 insertions, 32 deletions
diff --git a/lib/string.c b/lib/string.c index 6389217d5b..7e4bec2e99 100644 --- a/lib/string.c +++ b/lib/string.c @@ -22,16 +22,31 @@ #include <linux/types.h> #include <string.h> #include <linux/ctype.h> +#include <asm/word-at-a-time.h> #include <malloc.h> -#ifndef __HAVE_ARCH_STRNICMP +#ifndef __HAVE_ARCH_STRCASECMP +int strcasecmp(const char *s1, const char *s2) +{ + int c1, c2; + + do { + c1 = tolower(*s1++); + c2 = tolower(*s2++); + } while (c1 == c2 && c1 != 0); + return c1 - c2; +} +EXPORT_SYMBOL(strcasecmp); +#endif + +#ifndef __HAVE_ARCH_STRNCASECMP /** - * strnicmp - Case insensitive, length-limited string comparison + * strncasecmp - Case insensitive, length-limited string comparison * @s1: One string * @s2: The other string * @len: the maximum number of characters to compare */ -int strnicmp(const char *s1, const char *s2, size_t len) +int strncasecmp(const char *s1, const char *s2, size_t len) { /* Yes, Virginia, it had better be unsigned */ unsigned char c1, c2; @@ -53,34 +68,6 @@ int strnicmp(const char *s1, const char *s2, size_t len) } while (--len); return (int)c1 - (int)c2; } -EXPORT_SYMBOL(strnicmp); -#endif - -#ifndef __HAVE_ARCH_STRCASECMP -int strcasecmp(const char *s1, const char *s2) -{ - int c1, c2; - - do { - c1 = tolower(*s1++); - c2 = tolower(*s2++); - } while (c1 == c2 && c1 != 0); - return c1 - c2; -} -EXPORT_SYMBOL(strcasecmp); -#endif - -#ifndef __HAVE_ARCH_STRNCASECMP -int strncasecmp(const char *s1, const char *s2, size_t n) -{ - int c1, c2; - - do { - c1 = tolower(*s1++); - c2 = tolower(*s2++); - } while ((--n > 0) && c1 == c2 && c1 != 0); - return c1 - c2; -} EXPORT_SYMBOL(strncasecmp); #endif @@ -101,6 +88,76 @@ char * strcpy(char * dest,const char *src) #endif EXPORT_SYMBOL(strcpy); +#ifndef __HAVE_ARCH_STRSCPY +ssize_t strscpy(char *dest, const char *src, size_t count) +{ + const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; + size_t max = count; + long res = 0; + + if (count == 0 || WARN_ON_ONCE(count > INT_MAX)) + return -E2BIG; + +#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS + /* + * If src is unaligned, don't cross a page boundary, + * since we don't know if the next page is mapped. + */ + if ((long)src & (sizeof(long) - 1)) { + size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)); + if (limit < max) + max = limit; + } +#else + /* If src or dest is unaligned, don't do word-at-a-time. */ + if (((long) dest | (long) src) & (sizeof(long) - 1)) + max = 0; +#endif + + /* + * read_word_at_a_time() below may read uninitialized bytes after the + * trailing zero and use them in comparisons. Disable this optimization + * under KMSAN to prevent false positive reports. + */ + if (IS_ENABLED(CONFIG_KMSAN)) + max = 0; + + while (max >= sizeof(unsigned long)) { + unsigned long c, data; + + c = read_word_at_a_time(src+res); + if (has_zero(c, &data, &constants)) { + data = prep_zero_mask(c, data, &constants); + data = create_zero_mask(data); + *(unsigned long *)(dest+res) = c & zero_bytemask(data); + return res + find_zero(data); + } + *(unsigned long *)(dest+res) = c; + res += sizeof(unsigned long); + count -= sizeof(unsigned long); + max -= sizeof(unsigned long); + } + + while (count) { + char c; + + c = src[res]; + dest[res] = c; + if (!c) + return res; + res++; + count--; + } + + /* Hit buffer length without finding a NUL; force NUL-termination. */ + if (res) + dest[res-1] = '\0'; + + return -E2BIG; +} +EXPORT_SYMBOL(strscpy); +#endif + /** * stpcpy - Copy a %NUL terminated string, but return pointer to %NUL * @dest: Where to copy the string to @@ -548,7 +605,7 @@ void *__default_memset(void * s, int c, size_t count) } EXPORT_SYMBOL(__default_memset); -void __no_sanitize_address *__nokasan_default_memset(void * s, int c, size_t count) +void __prereloc __no_sanitize_address *__nokasan_default_memset(void * s, int c, size_t count) { char *xs = (char *) s; @@ -603,6 +660,11 @@ void *__memcpy(void * dest, const void *src, size_t count) __alias(__default_memcpy); #endif +void *mempcpy(void *dest, const void *src, size_t count) +{ + return memcpy(dest, src, count) + count; +} +EXPORT_SYMBOL(mempcpy); #ifndef __HAVE_ARCH_MEMMOVE /** @@ -938,3 +1000,58 @@ char *parse_assignment(char *str) return value; } + +char *strjoin(const char *separator, char **arr, size_t arrlen) +{ + size_t separatorlen; + int len = 1; /* '\0' */ + char *buf, *p; + int i; + + separatorlen = strlen(separator); + + for (i = 0; i < arrlen; i++) + len += strlen(arr[i]) + separatorlen; + + if (!arrlen) + return xzalloc(1); + + p = buf = xmalloc(len); + + for (i = 0; i < arrlen - 1; i++) { + p = stpcpy(p, arr[i]); + p = mempcpy(p, separator, separatorlen); + } + + stpcpy(p, arr[i]); + + return buf; +} +EXPORT_SYMBOL(strjoin); + +/** + * strreplace - Replace all occurrences of character in string. + * @str: The string to operate on. + * @old: The character being replaced. + * @new: The character @old is replaced with. + * + * Replaces the each @old character with a @new one in the given string @str. + * + * Return: pointer to the string @str itself. + */ +char *strreplace(char *str, char old, char new) +{ + char *s = str; + + for (; *s; ++s) + if (*s == old) + *s = new; + return str; +} +EXPORT_SYMBOL(strreplace); + +bool isempty(const char *s) +{ + return !s || s[0] == '\0'; +} +EXPORT_SYMBOL(isempty); |