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/bits.h>
19 #include <linux/bug.h>
20 #include <linux/ctype.h>
21 #include <linux/errno.h>
22 #include <linux/limits.h>
23 #include <linux/linkage.h>
24 #include <linux/stddef.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
29 #include <asm/rwonce.h>
30 #include <linux/unaligned.h>
31 #include <asm/word-at-a-time.h>
33 #ifndef __HAVE_ARCH_STRNCASECMP
35 * strncasecmp - Case insensitive, length-limited string comparison
37 * @s2: The other string
38 * @len: the maximum number of characters to compare
40 int strncasecmp(const char *s1
, const char *s2
, size_t len
)
42 /* Yes, Virginia, it had better be unsigned */
60 return (int)c1
- (int)c2
;
62 EXPORT_SYMBOL(strncasecmp
);
65 #ifndef __HAVE_ARCH_STRCASECMP
66 int strcasecmp(const char *s1
, const char *s2
)
73 } while (c1
== c2
&& c1
!= 0);
76 EXPORT_SYMBOL(strcasecmp
);
79 #ifndef __HAVE_ARCH_STRCPY
80 char *strcpy(char *dest
, const char *src
)
84 while ((*dest
++ = *src
++) != '\0')
88 EXPORT_SYMBOL(strcpy
);
91 #ifndef __HAVE_ARCH_STRNCPY
92 char *strncpy(char *dest
, const char *src
, size_t count
)
97 if ((*tmp
= *src
) != 0)
104 EXPORT_SYMBOL(strncpy
);
108 # define ALLBUTLAST_BYTE_MASK (~255ul)
110 # define ALLBUTLAST_BYTE_MASK (~0ul >> 8)
113 ssize_t
sized_strscpy(char *dest
, const char *src
, size_t count
)
115 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
119 if (count
== 0 || WARN_ON_ONCE(count
> INT_MAX
))
122 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
124 * If src is unaligned, don't cross a page boundary,
125 * since we don't know if the next page is mapped.
127 if ((long)src
& (sizeof(long) - 1)) {
128 size_t limit
= PAGE_SIZE
- ((long)src
& (PAGE_SIZE
- 1));
133 /* If src or dest is unaligned, don't do word-at-a-time. */
134 if (((long) dest
| (long) src
) & (sizeof(long) - 1))
139 * read_word_at_a_time() below may read uninitialized bytes after the
140 * trailing zero and use them in comparisons. Disable this optimization
141 * under KMSAN to prevent false positive reports.
143 if (IS_ENABLED(CONFIG_KMSAN
))
146 while (max
>= sizeof(unsigned long)) {
147 unsigned long c
, data
;
149 c
= read_word_at_a_time(src
+res
);
150 if (has_zero(c
, &data
, &constants
)) {
151 data
= prep_zero_mask(c
, data
, &constants
);
152 data
= create_zero_mask(data
);
153 *(unsigned long *)(dest
+res
) = c
& zero_bytemask(data
);
154 return res
+ find_zero(data
);
156 count
-= sizeof(unsigned long);
157 if (unlikely(!count
)) {
158 c
&= ALLBUTLAST_BYTE_MASK
;
159 *(unsigned long *)(dest
+res
) = c
;
162 *(unsigned long *)(dest
+res
) = c
;
163 res
+= sizeof(unsigned long);
164 max
-= sizeof(unsigned long);
178 /* Force NUL-termination. */
181 /* Return E2BIG if the source didn't stop */
182 return src
[res
] ? -E2BIG
: res
;
184 EXPORT_SYMBOL(sized_strscpy
);
187 * stpcpy - copy a string from src to dest returning a pointer to the new end
188 * of dest, including src's %NUL-terminator. May overrun dest.
189 * @dest: pointer to end of string being copied into. Must be large enough
191 * @src: pointer to the beginning of string being copied from. Must not overlap
194 * stpcpy differs from strcpy in a key way: the return value is a pointer
195 * to the new %NUL-terminating character in @dest. (For strcpy, the return
196 * value is a pointer to the start of @dest). This interface is considered
197 * unsafe as it doesn't perform bounds checking of the inputs. As such it's
198 * not recommended for usage. Instead, its definition is provided in case
199 * the compiler lowers other libcalls to stpcpy.
201 char *stpcpy(char *__restrict__ dest
, const char *__restrict__ src
);
202 char *stpcpy(char *__restrict__ dest
, const char *__restrict__ src
)
204 while ((*dest
++ = *src
++) != '\0')
208 EXPORT_SYMBOL(stpcpy
);
210 #ifndef __HAVE_ARCH_STRCAT
211 char *strcat(char *dest
, const char *src
)
217 while ((*dest
++ = *src
++) != '\0')
221 EXPORT_SYMBOL(strcat
);
224 #ifndef __HAVE_ARCH_STRNCAT
225 char *strncat(char *dest
, const char *src
, size_t count
)
232 while ((*dest
++ = *src
++) != 0) {
241 EXPORT_SYMBOL(strncat
);
244 #ifndef __HAVE_ARCH_STRLCAT
245 size_t strlcat(char *dest
, const char *src
, size_t count
)
247 size_t dsize
= strlen(dest
);
248 size_t len
= strlen(src
);
249 size_t res
= dsize
+ len
;
251 /* This would be a bug */
252 BUG_ON(dsize
>= count
);
258 __builtin_memcpy(dest
, src
, len
);
262 EXPORT_SYMBOL(strlcat
);
265 #ifndef __HAVE_ARCH_STRCMP
267 * strcmp - Compare two strings
269 * @ct: Another string
271 int strcmp(const char *cs
, const char *ct
)
273 unsigned char c1
, c2
;
279 return c1
< c2
? -1 : 1;
285 EXPORT_SYMBOL(strcmp
);
288 #ifndef __HAVE_ARCH_STRNCMP
290 * strncmp - Compare two length-limited strings
292 * @ct: Another string
293 * @count: The maximum number of bytes to compare
295 int strncmp(const char *cs
, const char *ct
, size_t count
)
297 unsigned char c1
, c2
;
303 return c1
< c2
? -1 : 1;
310 EXPORT_SYMBOL(strncmp
);
313 #ifndef __HAVE_ARCH_STRCHR
315 * strchr - Find the first occurrence of a character in a string
316 * @s: The string to be searched
317 * @c: The character to search for
319 * Note that the %NUL-terminator is considered part of the string, and can
322 char *strchr(const char *s
, int c
)
324 for (; *s
!= (char)c
; ++s
)
329 EXPORT_SYMBOL(strchr
);
332 #ifndef __HAVE_ARCH_STRCHRNUL
334 * strchrnul - Find and return a character in a string, or end of string
335 * @s: The string to be searched
336 * @c: The character to search for
338 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
339 * return a pointer to the null byte at the end of s.
341 char *strchrnul(const char *s
, int c
)
343 while (*s
&& *s
!= (char)c
)
347 EXPORT_SYMBOL(strchrnul
);
351 * strnchrnul - Find and return a character in a length limited string,
353 * @s: The string to be searched
354 * @count: The number of characters to be searched
355 * @c: The character to search for
357 * Returns pointer to the first occurrence of 'c' in s. If c is not found,
358 * then return a pointer to the last character of the string.
360 char *strnchrnul(const char *s
, size_t count
, int c
)
362 while (count
-- && *s
&& *s
!= (char)c
)
367 #ifndef __HAVE_ARCH_STRRCHR
369 * strrchr - Find the last occurrence of a character in a string
370 * @s: The string to be searched
371 * @c: The character to search for
373 char *strrchr(const char *s
, int c
)
375 const char *last
= NULL
;
382 EXPORT_SYMBOL(strrchr
);
385 #ifndef __HAVE_ARCH_STRNCHR
387 * strnchr - Find a character in a length limited string
388 * @s: The string to be searched
389 * @count: The number of characters to be searched
390 * @c: The character to search for
392 * Note that the %NUL-terminator is considered part of the string, and can
395 char *strnchr(const char *s
, size_t count
, int c
)
405 EXPORT_SYMBOL(strnchr
);
408 #ifndef __HAVE_ARCH_STRLEN
409 size_t strlen(const char *s
)
413 for (sc
= s
; *sc
!= '\0'; ++sc
)
417 EXPORT_SYMBOL(strlen
);
420 #ifndef __HAVE_ARCH_STRNLEN
421 size_t strnlen(const char *s
, size_t count
)
425 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
429 EXPORT_SYMBOL(strnlen
);
432 #ifndef __HAVE_ARCH_STRSPN
434 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
435 * @s: The string to be searched
436 * @accept: The string to search for
438 size_t strspn(const char *s
, const char *accept
)
442 for (p
= s
; *p
!= '\0'; ++p
) {
443 if (!strchr(accept
, *p
))
448 EXPORT_SYMBOL(strspn
);
451 #ifndef __HAVE_ARCH_STRCSPN
453 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
454 * @s: The string to be searched
455 * @reject: The string to avoid
457 size_t strcspn(const char *s
, const char *reject
)
461 for (p
= s
; *p
!= '\0'; ++p
) {
462 if (strchr(reject
, *p
))
467 EXPORT_SYMBOL(strcspn
);
470 #ifndef __HAVE_ARCH_STRPBRK
472 * strpbrk - Find the first occurrence of a set of characters
473 * @cs: The string to be searched
474 * @ct: The characters to search for
476 char *strpbrk(const char *cs
, const char *ct
)
480 for (sc
= cs
; *sc
!= '\0'; ++sc
) {
486 EXPORT_SYMBOL(strpbrk
);
489 #ifndef __HAVE_ARCH_STRSEP
491 * strsep - Split a string into tokens
492 * @s: The string to be searched
493 * @ct: The characters to search for
495 * strsep() updates @s to point after the token, ready for the next call.
497 * It returns empty tokens, too, behaving exactly like the libc function
498 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
499 * Same semantics, slimmer shape. ;)
501 char *strsep(char **s
, const char *ct
)
509 end
= strpbrk(sbegin
, ct
);
515 EXPORT_SYMBOL(strsep
);
518 #ifndef __HAVE_ARCH_MEMSET
520 * memset - Fill a region of memory with the given value
521 * @s: Pointer to the start of the area.
522 * @c: The byte to fill the area with
523 * @count: The size of the area.
525 * Do not use memset() to access IO space, use memset_io() instead.
527 void *memset(void *s
, int c
, size_t count
)
535 EXPORT_SYMBOL(memset
);
538 #ifndef __HAVE_ARCH_MEMSET16
540 * memset16() - Fill a memory area with a uint16_t
541 * @s: Pointer to the start of the area.
542 * @v: The value to fill the area with
543 * @count: The number of values to store
545 * Differs from memset() in that it fills with a uint16_t instead
546 * of a byte. Remember that @count is the number of uint16_ts to
547 * store, not the number of bytes.
549 void *memset16(uint16_t *s
, uint16_t v
, size_t count
)
557 EXPORT_SYMBOL(memset16
);
560 #ifndef __HAVE_ARCH_MEMSET32
562 * memset32() - Fill a memory area with a uint32_t
563 * @s: Pointer to the start of the area.
564 * @v: The value to fill the area with
565 * @count: The number of values to store
567 * Differs from memset() in that it fills with a uint32_t instead
568 * of a byte. Remember that @count is the number of uint32_ts to
569 * store, not the number of bytes.
571 void *memset32(uint32_t *s
, uint32_t v
, size_t count
)
579 EXPORT_SYMBOL(memset32
);
582 #ifndef __HAVE_ARCH_MEMSET64
584 * memset64() - Fill a memory area with a uint64_t
585 * @s: Pointer to the start of the area.
586 * @v: The value to fill the area with
587 * @count: The number of values to store
589 * Differs from memset() in that it fills with a uint64_t instead
590 * of a byte. Remember that @count is the number of uint64_ts to
591 * store, not the number of bytes.
593 void *memset64(uint64_t *s
, uint64_t v
, size_t count
)
601 EXPORT_SYMBOL(memset64
);
604 #ifndef __HAVE_ARCH_MEMCPY
606 * memcpy - Copy one area of memory to another
607 * @dest: Where to copy to
608 * @src: Where to copy from
609 * @count: The size of the area.
611 * You should not use this function to access IO space, use memcpy_toio()
612 * or memcpy_fromio() instead.
614 void *memcpy(void *dest
, const void *src
, size_t count
)
623 EXPORT_SYMBOL(memcpy
);
626 #ifndef __HAVE_ARCH_MEMMOVE
628 * memmove - Copy one area of memory to another
629 * @dest: Where to copy to
630 * @src: Where to copy from
631 * @count: The size of the area.
633 * Unlike memcpy(), memmove() copes with overlapping areas.
635 void *memmove(void *dest
, const void *src
, size_t count
)
655 EXPORT_SYMBOL(memmove
);
658 #ifndef __HAVE_ARCH_MEMCMP
660 * memcmp - Compare two areas of memory
661 * @cs: One area of memory
662 * @ct: Another area of memory
663 * @count: The size of the area.
666 __visible
int memcmp(const void *cs
, const void *ct
, size_t count
)
668 const unsigned char *su1
, *su2
;
671 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
672 if (count
>= sizeof(unsigned long)) {
673 const unsigned long *u1
= cs
;
674 const unsigned long *u2
= ct
;
676 if (get_unaligned(u1
) != get_unaligned(u2
))
680 count
-= sizeof(unsigned long);
681 } while (count
>= sizeof(unsigned long));
686 for (su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
687 if ((res
= *su1
- *su2
) != 0)
691 EXPORT_SYMBOL(memcmp
);
694 #ifndef __HAVE_ARCH_BCMP
696 * bcmp - returns 0 if and only if the buffers have identical contents.
697 * @a: pointer to first buffer.
698 * @b: pointer to second buffer.
699 * @len: size of buffers.
701 * The sign or magnitude of a non-zero return value has no particular
702 * meaning, and architectures may implement their own more efficient bcmp(). So
703 * while this particular implementation is a simple (tail) call to memcmp, do
704 * not rely on anything but whether the return value is zero or non-zero.
706 int bcmp(const void *a
, const void *b
, size_t len
)
708 return memcmp(a
, b
, len
);
713 #ifndef __HAVE_ARCH_MEMSCAN
715 * memscan - Find a character in an area of memory.
716 * @addr: The memory area
717 * @c: The byte to search for
718 * @size: The size of the area.
720 * returns the address of the first occurrence of @c, or 1 byte past
721 * the area if @c is not found
723 void *memscan(void *addr
, int c
, size_t size
)
725 unsigned char *p
= addr
;
728 if (*p
== (unsigned char)c
)
735 EXPORT_SYMBOL(memscan
);
738 #ifndef __HAVE_ARCH_STRSTR
740 * strstr - Find the first substring in a %NUL terminated string
741 * @s1: The string to be searched
742 * @s2: The string to search for
744 char *strstr(const char *s1
, const char *s2
)
754 if (!memcmp(s1
, s2
, l2
))
760 EXPORT_SYMBOL(strstr
);
763 #ifndef __HAVE_ARCH_STRNSTR
765 * strnstr - Find the first substring in a length-limited string
766 * @s1: The string to be searched
767 * @s2: The string to search for
768 * @len: the maximum number of characters to search
770 char *strnstr(const char *s1
, const char *s2
, size_t len
)
779 if (!memcmp(s1
, s2
, l2
))
785 EXPORT_SYMBOL(strnstr
);
788 #ifndef __HAVE_ARCH_MEMCHR
790 * memchr - Find a character in an area of memory.
791 * @s: The memory area
792 * @c: The byte to search for
793 * @n: The size of the area.
795 * returns the address of the first occurrence of @c, or %NULL
798 void *memchr(const void *s
, int c
, size_t n
)
800 const unsigned char *p
= s
;
802 if ((unsigned char)c
== *p
++) {
803 return (void *)(p
- 1);
808 EXPORT_SYMBOL(memchr
);
811 static void *check_bytes8(const u8
*start
, u8 value
, unsigned int bytes
)
815 return (void *)start
;
823 * memchr_inv - Find an unmatching character in an area of memory.
824 * @start: The memory area
825 * @c: Find a character other than c
826 * @bytes: The size of the area.
828 * returns the address of the first character other than @c, or %NULL
829 * if the whole buffer contains just @c.
831 void *memchr_inv(const void *start
, int c
, size_t bytes
)
835 unsigned int words
, prefix
;
838 return check_bytes8(start
, value
, bytes
);
841 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
842 value64
*= 0x0101010101010101ULL
;
843 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
844 value64
*= 0x01010101;
845 value64
|= value64
<< 32;
847 value64
|= value64
<< 8;
848 value64
|= value64
<< 16;
849 value64
|= value64
<< 32;
852 prefix
= (unsigned long)start
% 8;
857 r
= check_bytes8(start
, value
, prefix
);
867 if (*(u64
*)start
!= value64
)
868 return check_bytes8(start
, value
, 8);
873 return check_bytes8(start
, value
, bytes
% 8);
875 EXPORT_SYMBOL(memchr_inv
);