// SPDX-License-Identifier: GPL-2.0 /* * linux/lib/string.c * * Copyright (C) 1991, 1992 Linus Torvalds */ /* * This file should be used only for "library" routines that may have * alternative implementations on specific architectures (generally * found in <asm-xx/string.h>), or get overloaded by FORTIFY_SOURCE. * (Specifically, this file is built with __NO_FORTIFY.) * * Other helper functions should live in string_helpers.c. */ #define __NO_FORTIFY #include <linux/bits.h> #include <linux/bug.h> #include <linux/ctype.h> #include <linux/errno.h> #include <linux/limits.h> #include <linux/linkage.h> #include <linux/stddef.h> #include <linux/string.h> #include <linux/types.h> #include <asm/page.h> #include <asm/rwonce.h> #include <asm/unaligned.h> #include <asm/word-at-a-time.h> #ifndef __HAVE_ARCH_STRNCASECMP /** * strncasecmp - Case insensitive, length-limited string comparison * @s1: One string * @s2: The other string * @len: the maximum number of characters to compare */ int strncasecmp(const char *s1, const char *s2, size_t len) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRCASECMP int strcasecmp(const char *s1, const char *s2) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRCPY char *strcpy(char *dest, const char *src) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRNCPY char *strncpy(char *dest, const char *src, size_t count) { … } EXPORT_SYMBOL(…); #endif ssize_t sized_strscpy(char *dest, const char *src, size_t count) { … } EXPORT_SYMBOL(…); /** * stpcpy - copy a string from src to dest returning a pointer to the new end * of dest, including src's %NUL-terminator. May overrun dest. * @dest: pointer to end of string being copied into. Must be large enough * to receive copy. * @src: pointer to the beginning of string being copied from. Must not overlap * dest. * * stpcpy differs from strcpy in a key way: the return value is a pointer * to the new %NUL-terminating character in @dest. (For strcpy, the return * value is a pointer to the start of @dest). This interface is considered * unsafe as it doesn't perform bounds checking of the inputs. As such it's * not recommended for usage. Instead, its definition is provided in case * the compiler lowers other libcalls to stpcpy. */ char *stpcpy(char *__restrict__ dest, const char *__restrict__ src); char *stpcpy(char *__restrict__ dest, const char *__restrict__ src) { … } EXPORT_SYMBOL(…); #ifndef __HAVE_ARCH_STRCAT char *strcat(char *dest, const char *src) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRNCAT char *strncat(char *dest, const char *src, size_t count) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRLCAT size_t strlcat(char *dest, const char *src, size_t count) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRCMP /** * strcmp - Compare two strings * @cs: One string * @ct: Another string */ int strcmp(const char *cs, const char *ct) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRNCMP /** * strncmp - Compare two length-limited strings * @cs: One string * @ct: Another string * @count: The maximum number of bytes to compare */ int strncmp(const char *cs, const char *ct, size_t count) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRCHR /** * strchr - Find the first occurrence of a character in a string * @s: The string to be searched * @c: The character to search for * * Note that the %NUL-terminator is considered part of the string, and can * be searched for. */ char *strchr(const char *s, int c) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRCHRNUL /** * strchrnul - Find and return a character in a string, or end of string * @s: The string to be searched * @c: The character to search for * * Returns pointer to first occurrence of 'c' in s. If c is not found, then * return a pointer to the null byte at the end of s. */ char *strchrnul(const char *s, int c) { … } EXPORT_SYMBOL(…); #endif /** * strnchrnul - Find and return a character in a length limited string, * or end of string * @s: The string to be searched * @count: The number of characters to be searched * @c: The character to search for * * Returns pointer to the first occurrence of 'c' in s. If c is not found, * then return a pointer to the last character of the string. */ char *strnchrnul(const char *s, size_t count, int c) { … } #ifndef __HAVE_ARCH_STRRCHR /** * strrchr - Find the last occurrence of a character in a string * @s: The string to be searched * @c: The character to search for */ char *strrchr(const char *s, int c) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRNCHR /** * strnchr - Find a character in a length limited string * @s: The string to be searched * @count: The number of characters to be searched * @c: The character to search for * * Note that the %NUL-terminator is considered part of the string, and can * be searched for. */ char *strnchr(const char *s, size_t count, int c) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRLEN size_t strlen(const char *s) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRNLEN size_t strnlen(const char *s, size_t count) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRSPN /** * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept * @s: The string to be searched * @accept: The string to search for */ size_t strspn(const char *s, const char *accept) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRCSPN /** * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject * @s: The string to be searched * @reject: The string to avoid */ size_t strcspn(const char *s, const char *reject) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRPBRK /** * strpbrk - Find the first occurrence of a set of characters * @cs: The string to be searched * @ct: The characters to search for */ char *strpbrk(const char *cs, const char *ct) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRSEP /** * strsep - Split a string into tokens * @s: The string to be searched * @ct: The characters to search for * * strsep() updates @s to point after the token, ready for the next call. * * It returns empty tokens, too, behaving exactly like the libc function * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. * Same semantics, slimmer shape. ;) */ char *strsep(char **s, const char *ct) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_MEMSET /** * memset - Fill a region of memory with the given value * @s: Pointer to the start of the area. * @c: The byte to fill the area with * @count: The size of the area. * * Do not use memset() to access IO space, use memset_io() instead. */ void *memset(void *s, int c, size_t count) { char *xs = s; while (count--) *xs++ = c; return s; } EXPORT_SYMBOL(memset); #endif #ifndef __HAVE_ARCH_MEMSET16 /** * memset16() - Fill a memory area with a uint16_t * @s: Pointer to the start of the area. * @v: The value to fill the area with * @count: The number of values to store * * Differs from memset() in that it fills with a uint16_t instead * of a byte. Remember that @count is the number of uint16_ts to * store, not the number of bytes. */ void *memset16(uint16_t *s, uint16_t v, size_t count) { uint16_t *xs = s; while (count--) *xs++ = v; return s; } EXPORT_SYMBOL(memset16); #endif #ifndef __HAVE_ARCH_MEMSET32 /** * memset32() - Fill a memory area with a uint32_t * @s: Pointer to the start of the area. * @v: The value to fill the area with * @count: The number of values to store * * Differs from memset() in that it fills with a uint32_t instead * of a byte. Remember that @count is the number of uint32_ts to * store, not the number of bytes. */ void *memset32(uint32_t *s, uint32_t v, size_t count) { uint32_t *xs = s; while (count--) *xs++ = v; return s; } EXPORT_SYMBOL(memset32); #endif #ifndef __HAVE_ARCH_MEMSET64 /** * memset64() - Fill a memory area with a uint64_t * @s: Pointer to the start of the area. * @v: The value to fill the area with * @count: The number of values to store * * Differs from memset() in that it fills with a uint64_t instead * of a byte. Remember that @count is the number of uint64_ts to * store, not the number of bytes. */ void *memset64(uint64_t *s, uint64_t v, size_t count) { uint64_t *xs = s; while (count--) *xs++ = v; return s; } EXPORT_SYMBOL(memset64); #endif #ifndef __HAVE_ARCH_MEMCPY /** * memcpy - Copy one area of memory to another * @dest: Where to copy to * @src: Where to copy from * @count: The size of the area. * * You should not use this function to access IO space, use memcpy_toio() * or memcpy_fromio() instead. */ void *memcpy(void *dest, const void *src, size_t count) { char *tmp = dest; const char *s = src; while (count--) *tmp++ = *s++; return dest; } EXPORT_SYMBOL(memcpy); #endif #ifndef __HAVE_ARCH_MEMMOVE /** * memmove - Copy one area of memory to another * @dest: Where to copy to * @src: Where to copy from * @count: The size of the area. * * Unlike memcpy(), memmove() copes with overlapping areas. */ void *memmove(void *dest, const void *src, size_t count) { char *tmp; const char *s; if (dest <= src) { tmp = dest; s = src; while (count--) *tmp++ = *s++; } else { tmp = dest; tmp += count; s = src; s += count; while (count--) *--tmp = *--s; } return dest; } EXPORT_SYMBOL(memmove); #endif #ifndef __HAVE_ARCH_MEMCMP /** * memcmp - Compare two areas of memory * @cs: One area of memory * @ct: Another area of memory * @count: The size of the area. */ #undef memcmp __visible int memcmp(const void *cs, const void *ct, size_t count) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_BCMP /** * bcmp - returns 0 if and only if the buffers have identical contents. * @a: pointer to first buffer. * @b: pointer to second buffer. * @len: size of buffers. * * The sign or magnitude of a non-zero return value has no particular * meaning, and architectures may implement their own more efficient bcmp(). So * while this particular implementation is a simple (tail) call to memcmp, do * not rely on anything but whether the return value is zero or non-zero. */ int bcmp(const void *a, const void *b, size_t len) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_MEMSCAN /** * memscan - Find a character in an area of memory. * @addr: The memory area * @c: The byte to search for * @size: The size of the area. * * returns the address of the first occurrence of @c, or 1 byte past * the area if @c is not found */ void *memscan(void *addr, int c, size_t size) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRSTR /** * strstr - Find the first substring in a %NUL terminated string * @s1: The string to be searched * @s2: The string to search for */ char *strstr(const char *s1, const char *s2) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_STRNSTR /** * strnstr - Find the first substring in a length-limited string * @s1: The string to be searched * @s2: The string to search for * @len: the maximum number of characters to search */ char *strnstr(const char *s1, const char *s2, size_t len) { … } EXPORT_SYMBOL(…); #endif #ifndef __HAVE_ARCH_MEMCHR /** * memchr - Find a character in an area of memory. * @s: The memory area * @c: The byte to search for * @n: The size of the area. * * returns the address of the first occurrence of @c, or %NULL * if @c is not found */ void *memchr(const void *s, int c, size_t n) { … } EXPORT_SYMBOL(…); #endif static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) { … } /** * memchr_inv - Find an unmatching character in an area of memory. * @start: The memory area * @c: Find a character other than c * @bytes: The size of the area. * * returns the address of the first character other than @c, or %NULL * if the whole buffer contains just @c. */ void *memchr_inv(const void *start, int c, size_t bytes) { … } EXPORT_SYMBOL(…);