1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
9 * This file should be used only for "library" routines that may have
10 * alternative implementations on specific architectures (generally
11 * found in <asm-xx/string.h>), or get overloaded by FORTIFY_SOURCE.
12 * (Specifically, this file is built with __NO_FORTIFY.)
14 * Other helper functions should live in string_helpers.c.
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <linux/kernel.h>
22 #include <linux/export.h>
23 #include <linux/bug.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
27 #include <asm/unaligned.h>
28 #include <asm/byteorder.h>
29 #include <asm/word-at-a-time.h>
32 #ifndef __HAVE_ARCH_STRNCASECMP
34 * strncasecmp - Case insensitive, length-limited string comparison
36 * @s2: The other string
37 * @len: the maximum number of characters to compare
39 int strncasecmp(const char *s1, const char *s2, size_t len)
41 /* Yes, Virginia, it had better be unsigned */
59 return (int)c1 - (int)c2;
61 EXPORT_SYMBOL(strncasecmp);
64 #ifndef __HAVE_ARCH_STRCASECMP
65 int strcasecmp(const char *s1, const char *s2)
72 } while (c1 == c2 && c1 != 0);
75 EXPORT_SYMBOL(strcasecmp);
78 #ifndef __HAVE_ARCH_STRCPY
80 * strcpy - Copy a %NUL terminated string
81 * @dest: Where to copy the string to
82 * @src: Where to copy the string from
84 char *strcpy(char *dest, const char *src)
88 while ((*dest++ = *src++) != '\0')
92 EXPORT_SYMBOL(strcpy);
95 #ifndef __HAVE_ARCH_STRNCPY
97 * strncpy - Copy a length-limited, C-string
98 * @dest: Where to copy the string to
99 * @src: Where to copy the string from
100 * @count: The maximum number of bytes to copy
102 * The result is not %NUL-terminated if the source exceeds
105 * In the case where the length of @src is less than that of
106 * count, the remainder of @dest will be padded with %NUL.
109 char *strncpy(char *dest, const char *src, size_t count)
114 if ((*tmp = *src) != 0)
121 EXPORT_SYMBOL(strncpy);
124 #ifndef __HAVE_ARCH_STRLCPY
126 * strlcpy - Copy a C-string into a sized buffer
127 * @dest: Where to copy the string to
128 * @src: Where to copy the string from
129 * @size: size of destination buffer
131 * Compatible with ``*BSD``: the result is always a valid
132 * NUL-terminated string that fits in the buffer (unless,
133 * of course, the buffer size is zero). It does not pad
134 * out the result like strncpy() does.
136 size_t strlcpy(char *dest, const char *src, size_t size)
138 size_t ret = strlen(src);
141 size_t len = (ret >= size) ? size - 1 : ret;
142 memcpy(dest, src, len);
147 EXPORT_SYMBOL(strlcpy);
150 #ifndef __HAVE_ARCH_STRSCPY
152 * strscpy - Copy a C-string into a sized buffer
153 * @dest: Where to copy the string to
154 * @src: Where to copy the string from
155 * @count: Size of destination buffer
157 * Copy the string, or as much of it as fits, into the dest buffer. The
158 * behavior is undefined if the string buffers overlap. The destination
159 * buffer is always NUL terminated, unless it's zero-sized.
161 * Preferred to strlcpy() since the API doesn't require reading memory
162 * from the src string beyond the specified "count" bytes, and since
163 * the return value is easier to error-check than strlcpy()'s.
164 * In addition, the implementation is robust to the string changing out
165 * from underneath it, unlike the current strlcpy() implementation.
167 * Preferred to strncpy() since it always returns a valid string, and
168 * doesn't unnecessarily force the tail of the destination buffer to be
169 * zeroed. If zeroing is desired please use strscpy_pad().
172 * * The number of characters copied (not including the trailing %NUL)
173 * * -E2BIG if count is 0 or @src was truncated.
175 ssize_t strscpy(char *dest, const char *src, size_t count)
177 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
181 if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
184 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
186 * If src is unaligned, don't cross a page boundary,
187 * since we don't know if the next page is mapped.
189 if ((long)src & (sizeof(long) - 1)) {
190 size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
195 /* If src or dest is unaligned, don't do word-at-a-time. */
196 if (((long) dest | (long) src) & (sizeof(long) - 1))
201 * read_word_at_a_time() below may read uninitialized bytes after the
202 * trailing zero and use them in comparisons. Disable this optimization
203 * under KMSAN to prevent false positive reports.
205 if (IS_ENABLED(CONFIG_KMSAN))
208 while (max >= sizeof(unsigned long)) {
209 unsigned long c, data;
211 c = read_word_at_a_time(src+res);
212 if (has_zero(c, &data, &constants)) {
213 data = prep_zero_mask(c, data, &constants);
214 data = create_zero_mask(data);
215 *(unsigned long *)(dest+res) = c & zero_bytemask(data);
216 return res + find_zero(data);
218 *(unsigned long *)(dest+res) = c;
219 res += sizeof(unsigned long);
220 count -= sizeof(unsigned long);
221 max -= sizeof(unsigned long);
235 /* Hit buffer length without finding a NUL; force NUL-termination. */
241 EXPORT_SYMBOL(strscpy);
245 * stpcpy - copy a string from src to dest returning a pointer to the new end
246 * of dest, including src's %NUL-terminator. May overrun dest.
247 * @dest: pointer to end of string being copied into. Must be large enough
249 * @src: pointer to the beginning of string being copied from. Must not overlap
252 * stpcpy differs from strcpy in a key way: the return value is a pointer
253 * to the new %NUL-terminating character in @dest. (For strcpy, the return
254 * value is a pointer to the start of @dest). This interface is considered
255 * unsafe as it doesn't perform bounds checking of the inputs. As such it's
256 * not recommended for usage. Instead, its definition is provided in case
257 * the compiler lowers other libcalls to stpcpy.
259 char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
260 char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
262 while ((*dest++ = *src++) != '\0')
266 EXPORT_SYMBOL(stpcpy);
268 #ifndef __HAVE_ARCH_STRCAT
270 * strcat - Append one %NUL-terminated string to another
271 * @dest: The string to be appended to
272 * @src: The string to append to it
274 char *strcat(char *dest, const char *src)
280 while ((*dest++ = *src++) != '\0')
284 EXPORT_SYMBOL(strcat);
287 #ifndef __HAVE_ARCH_STRNCAT
289 * strncat - Append a length-limited, C-string to another
290 * @dest: The string to be appended to
291 * @src: The string to append to it
292 * @count: The maximum numbers of bytes to copy
294 * Note that in contrast to strncpy(), strncat() ensures the result is
297 char *strncat(char *dest, const char *src, size_t count)
304 while ((*dest++ = *src++) != 0) {
313 EXPORT_SYMBOL(strncat);
316 #ifndef __HAVE_ARCH_STRLCAT
318 * strlcat - Append a length-limited, C-string to another
319 * @dest: The string to be appended to
320 * @src: The string to append to it
321 * @count: The size of the destination buffer.
323 size_t strlcat(char *dest, const char *src, size_t count)
325 size_t dsize = strlen(dest);
326 size_t len = strlen(src);
327 size_t res = dsize + len;
329 /* This would be a bug */
330 BUG_ON(dsize >= count);
336 memcpy(dest, src, len);
340 EXPORT_SYMBOL(strlcat);
343 #ifndef __HAVE_ARCH_STRCMP
345 * strcmp - Compare two strings
347 * @ct: Another string
349 int strcmp(const char *cs, const char *ct)
351 unsigned char c1, c2;
357 return c1 < c2 ? -1 : 1;
363 EXPORT_SYMBOL(strcmp);
366 #ifndef __HAVE_ARCH_STRNCMP
368 * strncmp - Compare two length-limited strings
370 * @ct: Another string
371 * @count: The maximum number of bytes to compare
373 int strncmp(const char *cs, const char *ct, size_t count)
375 unsigned char c1, c2;
381 return c1 < c2 ? -1 : 1;
388 EXPORT_SYMBOL(strncmp);
391 #ifndef __HAVE_ARCH_STRCHR
393 * strchr - Find the first occurrence of a character in a string
394 * @s: The string to be searched
395 * @c: The character to search for
397 * Note that the %NUL-terminator is considered part of the string, and can
400 char *strchr(const char *s, int c)
402 for (; *s != (char)c; ++s)
407 EXPORT_SYMBOL(strchr);
410 #ifndef __HAVE_ARCH_STRCHRNUL
412 * strchrnul - Find and return a character in a string, or end of string
413 * @s: The string to be searched
414 * @c: The character to search for
416 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
417 * return a pointer to the null byte at the end of s.
419 char *strchrnul(const char *s, int c)
421 while (*s && *s != (char)c)
425 EXPORT_SYMBOL(strchrnul);
429 * strnchrnul - Find and return a character in a length limited string,
431 * @s: The string to be searched
432 * @count: The number of characters to be searched
433 * @c: The character to search for
435 * Returns pointer to the first occurrence of 'c' in s. If c is not found,
436 * then return a pointer to the last character of the string.
438 char *strnchrnul(const char *s, size_t count, int c)
440 while (count-- && *s && *s != (char)c)
445 #ifndef __HAVE_ARCH_STRRCHR
447 * strrchr - Find the last occurrence of a character in a string
448 * @s: The string to be searched
449 * @c: The character to search for
451 char *strrchr(const char *s, int c)
453 const char *last = NULL;
460 EXPORT_SYMBOL(strrchr);
463 #ifndef __HAVE_ARCH_STRNCHR
465 * strnchr - Find a character in a length limited string
466 * @s: The string to be searched
467 * @count: The number of characters to be searched
468 * @c: The character to search for
470 * Note that the %NUL-terminator is considered part of the string, and can
473 char *strnchr(const char *s, size_t count, int c)
483 EXPORT_SYMBOL(strnchr);
486 #ifndef __HAVE_ARCH_STRLEN
488 * strlen - Find the length of a string
489 * @s: The string to be sized
491 size_t strlen(const char *s)
495 for (sc = s; *sc != '\0'; ++sc)
499 EXPORT_SYMBOL(strlen);
502 #ifndef __HAVE_ARCH_STRNLEN
504 * strnlen - Find the length of a length-limited string
505 * @s: The string to be sized
506 * @count: The maximum number of bytes to search
508 size_t strnlen(const char *s, size_t count)
512 for (sc = s; count-- && *sc != '\0'; ++sc)
516 EXPORT_SYMBOL(strnlen);
519 #ifndef __HAVE_ARCH_STRSPN
521 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
522 * @s: The string to be searched
523 * @accept: The string to search for
525 size_t strspn(const char *s, const char *accept)
529 for (p = s; *p != '\0'; ++p) {
530 if (!strchr(accept, *p))
535 EXPORT_SYMBOL(strspn);
538 #ifndef __HAVE_ARCH_STRCSPN
540 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
541 * @s: The string to be searched
542 * @reject: The string to avoid
544 size_t strcspn(const char *s, const char *reject)
548 for (p = s; *p != '\0'; ++p) {
549 if (strchr(reject, *p))
554 EXPORT_SYMBOL(strcspn);
557 #ifndef __HAVE_ARCH_STRPBRK
559 * strpbrk - Find the first occurrence of a set of characters
560 * @cs: The string to be searched
561 * @ct: The characters to search for
563 char *strpbrk(const char *cs, const char *ct)
565 const char *sc1, *sc2;
567 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
568 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
575 EXPORT_SYMBOL(strpbrk);
578 #ifndef __HAVE_ARCH_STRSEP
580 * strsep - Split a string into tokens
581 * @s: The string to be searched
582 * @ct: The characters to search for
584 * strsep() updates @s to point after the token, ready for the next call.
586 * It returns empty tokens, too, behaving exactly like the libc function
587 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
588 * Same semantics, slimmer shape. ;)
590 char *strsep(char **s, const char *ct)
598 end = strpbrk(sbegin, ct);
604 EXPORT_SYMBOL(strsep);
607 #ifndef __HAVE_ARCH_MEMSET
609 * memset - Fill a region of memory with the given value
610 * @s: Pointer to the start of the area.
611 * @c: The byte to fill the area with
612 * @count: The size of the area.
614 * Do not use memset() to access IO space, use memset_io() instead.
616 void *memset(void *s, int c, size_t count)
624 EXPORT_SYMBOL(memset);
627 #ifndef __HAVE_ARCH_MEMSET16
629 * memset16() - Fill a memory area with a uint16_t
630 * @s: Pointer to the start of the area.
631 * @v: The value to fill the area with
632 * @count: The number of values to store
634 * Differs from memset() in that it fills with a uint16_t instead
635 * of a byte. Remember that @count is the number of uint16_ts to
636 * store, not the number of bytes.
638 void *memset16(uint16_t *s, uint16_t v, size_t count)
646 EXPORT_SYMBOL(memset16);
649 #ifndef __HAVE_ARCH_MEMSET32
651 * memset32() - Fill a memory area with a uint32_t
652 * @s: Pointer to the start of the area.
653 * @v: The value to fill the area with
654 * @count: The number of values to store
656 * Differs from memset() in that it fills with a uint32_t instead
657 * of a byte. Remember that @count is the number of uint32_ts to
658 * store, not the number of bytes.
660 void *memset32(uint32_t *s, uint32_t v, size_t count)
668 EXPORT_SYMBOL(memset32);
671 #ifndef __HAVE_ARCH_MEMSET64
673 * memset64() - Fill a memory area with a uint64_t
674 * @s: Pointer to the start of the area.
675 * @v: The value to fill the area with
676 * @count: The number of values to store
678 * Differs from memset() in that it fills with a uint64_t instead
679 * of a byte. Remember that @count is the number of uint64_ts to
680 * store, not the number of bytes.
682 void *memset64(uint64_t *s, uint64_t v, size_t count)
690 EXPORT_SYMBOL(memset64);
693 #ifndef __HAVE_ARCH_MEMCPY
695 * memcpy - Copy one area of memory to another
696 * @dest: Where to copy to
697 * @src: Where to copy from
698 * @count: The size of the area.
700 * You should not use this function to access IO space, use memcpy_toio()
701 * or memcpy_fromio() instead.
703 void *memcpy(void *dest, const void *src, size_t count)
712 EXPORT_SYMBOL(memcpy);
715 #ifndef __HAVE_ARCH_MEMMOVE
717 * memmove - Copy one area of memory to another
718 * @dest: Where to copy to
719 * @src: Where to copy from
720 * @count: The size of the area.
722 * Unlike memcpy(), memmove() copes with overlapping areas.
724 void *memmove(void *dest, const void *src, size_t count)
744 EXPORT_SYMBOL(memmove);
747 #ifndef __HAVE_ARCH_MEMCMP
749 * memcmp - Compare two areas of memory
750 * @cs: One area of memory
751 * @ct: Another area of memory
752 * @count: The size of the area.
755 __visible int memcmp(const void *cs, const void *ct, size_t count)
757 const unsigned char *su1, *su2;
760 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
761 if (count >= sizeof(unsigned long)) {
762 const unsigned long *u1 = cs;
763 const unsigned long *u2 = ct;
765 if (get_unaligned(u1) != get_unaligned(u2))
769 count -= sizeof(unsigned long);
770 } while (count >= sizeof(unsigned long));
775 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
776 if ((res = *su1 - *su2) != 0)
780 EXPORT_SYMBOL(memcmp);
783 #ifndef __HAVE_ARCH_BCMP
785 * bcmp - returns 0 if and only if the buffers have identical contents.
786 * @a: pointer to first buffer.
787 * @b: pointer to second buffer.
788 * @len: size of buffers.
790 * The sign or magnitude of a non-zero return value has no particular
791 * meaning, and architectures may implement their own more efficient bcmp(). So
792 * while this particular implementation is a simple (tail) call to memcmp, do
793 * not rely on anything but whether the return value is zero or non-zero.
795 int bcmp(const void *a, const void *b, size_t len)
797 return memcmp(a, b, len);
802 #ifndef __HAVE_ARCH_MEMSCAN
804 * memscan - Find a character in an area of memory.
805 * @addr: The memory area
806 * @c: The byte to search for
807 * @size: The size of the area.
809 * returns the address of the first occurrence of @c, or 1 byte past
810 * the area if @c is not found
812 void *memscan(void *addr, int c, size_t size)
814 unsigned char *p = addr;
817 if (*p == (unsigned char)c)
824 EXPORT_SYMBOL(memscan);
827 #ifndef __HAVE_ARCH_STRSTR
829 * strstr - Find the first substring in a %NUL terminated string
830 * @s1: The string to be searched
831 * @s2: The string to search for
833 char *strstr(const char *s1, const char *s2)
843 if (!memcmp(s1, s2, l2))
849 EXPORT_SYMBOL(strstr);
852 #ifndef __HAVE_ARCH_STRNSTR
854 * strnstr - Find the first substring in a length-limited string
855 * @s1: The string to be searched
856 * @s2: The string to search for
857 * @len: the maximum number of characters to search
859 char *strnstr(const char *s1, const char *s2, size_t len)
868 if (!memcmp(s1, s2, l2))
874 EXPORT_SYMBOL(strnstr);
877 #ifndef __HAVE_ARCH_MEMCHR
879 * memchr - Find a character in an area of memory.
880 * @s: The memory area
881 * @c: The byte to search for
882 * @n: The size of the area.
884 * returns the address of the first occurrence of @c, or %NULL
887 void *memchr(const void *s, int c, size_t n)
889 const unsigned char *p = s;
891 if ((unsigned char)c == *p++) {
892 return (void *)(p - 1);
897 EXPORT_SYMBOL(memchr);
900 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
904 return (void *)start;
912 * memchr_inv - Find an unmatching character in an area of memory.
913 * @start: The memory area
914 * @c: Find a character other than c
915 * @bytes: The size of the area.
917 * returns the address of the first character other than @c, or %NULL
918 * if the whole buffer contains just @c.
920 void *memchr_inv(const void *start, int c, size_t bytes)
924 unsigned int words, prefix;
927 return check_bytes8(start, value, bytes);
930 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
931 value64 *= 0x0101010101010101ULL;
932 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
933 value64 *= 0x01010101;
934 value64 |= value64 << 32;
936 value64 |= value64 << 8;
937 value64 |= value64 << 16;
938 value64 |= value64 << 32;
941 prefix = (unsigned long)start % 8;
946 r = check_bytes8(start, value, prefix);
956 if (*(u64 *)start != value64)
957 return check_bytes8(start, value, 8);
962 return check_bytes8(start, value, bytes % 8);
964 EXPORT_SYMBOL(memchr_inv);