Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/lib/string.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | */ |
| 7 | |
| 8 | /* |
Kees Cook | cfecea6 | 2021-06-18 10:57:38 -0700 | [diff] [blame] | 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.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Kees Cook | cfecea6 | 2021-06-18 10:57:38 -0700 | [diff] [blame] | 14 | * Other helper functions should live in string_helpers.c. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Kees Cook | cfecea6 | 2021-06-18 10:57:38 -0700 | [diff] [blame] | 17 | #define __NO_FORTIFY |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/ctype.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/export.h> |
Paul Gortmaker | 50af5ea | 2012-01-20 18:35:53 -0500 | [diff] [blame] | 23 | #include <linux/bug.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 24 | #include <linux/errno.h> |
Alexander Shishkin | ce76d93 | 2018-10-05 15:43:05 +0300 | [diff] [blame] | 25 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 291d47c | 2021-07-21 12:01:28 -0700 | [diff] [blame] | 27 | #include <asm/unaligned.h> |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 28 | #include <asm/byteorder.h> |
| 29 | #include <asm/word-at-a-time.h> |
| 30 | #include <asm/page.h> |
| 31 | |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 32 | #ifndef __HAVE_ARCH_STRNCASECMP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /** |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 34 | * strncasecmp - Case insensitive, length-limited string comparison |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * @s1: One string |
| 36 | * @s2: The other string |
| 37 | * @len: the maximum number of characters to compare |
| 38 | */ |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 39 | int strncasecmp(const char *s1, const char *s2, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
| 41 | /* Yes, Virginia, it had better be unsigned */ |
| 42 | unsigned char c1, c2; |
| 43 | |
André Goddard Rosa | a11d2b6 | 2010-03-05 13:43:11 -0800 | [diff] [blame] | 44 | if (!len) |
| 45 | return 0; |
| 46 | |
| 47 | do { |
| 48 | c1 = *s1++; |
| 49 | c2 = *s2++; |
| 50 | if (!c1 || !c2) |
| 51 | break; |
| 52 | if (c1 == c2) |
| 53 | continue; |
| 54 | c1 = tolower(c1); |
| 55 | c2 = tolower(c2); |
| 56 | if (c1 != c2) |
| 57 | break; |
| 58 | } while (--len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return (int)c1 - (int)c2; |
| 60 | } |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 61 | EXPORT_SYMBOL(strncasecmp); |
| 62 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
David S. Miller | ded220b | 2007-03-29 01:18:42 -0700 | [diff] [blame] | 64 | #ifndef __HAVE_ARCH_STRCASECMP |
| 65 | int strcasecmp(const char *s1, const char *s2) |
| 66 | { |
| 67 | int c1, c2; |
| 68 | |
| 69 | do { |
| 70 | c1 = tolower(*s1++); |
| 71 | c2 = tolower(*s2++); |
| 72 | } while (c1 == c2 && c1 != 0); |
| 73 | return c1 - c2; |
| 74 | } |
| 75 | EXPORT_SYMBOL(strcasecmp); |
| 76 | #endif |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #ifndef __HAVE_ARCH_STRCPY |
| 79 | /** |
| 80 | * strcpy - Copy a %NUL terminated string |
| 81 | * @dest: Where to copy the string to |
| 82 | * @src: Where to copy the string from |
| 83 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 84 | char *strcpy(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | char *tmp = dest; |
| 87 | |
| 88 | while ((*dest++ = *src++) != '\0') |
| 89 | /* nothing */; |
| 90 | return tmp; |
| 91 | } |
| 92 | EXPORT_SYMBOL(strcpy); |
| 93 | #endif |
| 94 | |
| 95 | #ifndef __HAVE_ARCH_STRNCPY |
| 96 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 97 | * strncpy - Copy a length-limited, C-string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 101 | * |
| 102 | * The result is not %NUL-terminated if the source exceeds |
| 103 | * @count bytes. |
walter harms | 2527952 | 2005-05-05 16:16:20 -0700 | [diff] [blame] | 104 | * |
| 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. |
| 107 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 109 | char *strncpy(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | char *tmp = dest; |
| 112 | |
| 113 | while (count) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 114 | if ((*tmp = *src) != 0) |
| 115 | src++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | tmp++; |
| 117 | count--; |
| 118 | } |
| 119 | return dest; |
| 120 | } |
| 121 | EXPORT_SYMBOL(strncpy); |
| 122 | #endif |
| 123 | |
| 124 | #ifndef __HAVE_ARCH_STRLCPY |
| 125 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 126 | * strlcpy - Copy a C-string into a sized buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | * @dest: Where to copy the string to |
| 128 | * @src: Where to copy the string from |
| 129 | * @size: size of destination buffer |
| 130 | * |
mchehab@s-opensource.com | 0e056eb | 2017-03-30 17:11:36 -0300 | [diff] [blame] | 131 | * Compatible with ``*BSD``: the result is always a valid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 135 | */ |
| 136 | size_t strlcpy(char *dest, const char *src, size_t size) |
| 137 | { |
| 138 | size_t ret = strlen(src); |
| 139 | |
| 140 | if (size) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 141 | size_t len = (ret >= size) ? size - 1 : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | memcpy(dest, src, len); |
| 143 | dest[len] = '\0'; |
| 144 | } |
| 145 | return ret; |
| 146 | } |
| 147 | EXPORT_SYMBOL(strlcpy); |
| 148 | #endif |
| 149 | |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 150 | #ifndef __HAVE_ARCH_STRSCPY |
| 151 | /** |
| 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 |
| 156 | * |
Tobin C. Harding | 458a3bf | 2019-04-05 12:58:58 +1100 | [diff] [blame] | 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. |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 160 | * |
| 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. |
| 166 | * |
| 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 |
Tobin C. Harding | 458a3bf | 2019-04-05 12:58:58 +1100 | [diff] [blame] | 169 | * zeroed. If zeroing is desired please use strscpy_pad(). |
| 170 | * |
Joe Perches | 917cda2 | 2019-09-25 16:46:13 -0700 | [diff] [blame] | 171 | * Returns: |
| 172 | * * The number of characters copied (not including the trailing %NUL) |
| 173 | * * -E2BIG if count is 0 or @src was truncated. |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 174 | */ |
| 175 | ssize_t strscpy(char *dest, const char *src, size_t count) |
| 176 | { |
| 177 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
| 178 | size_t max = count; |
| 179 | long res = 0; |
| 180 | |
Kees Cook | 9a15646 | 2019-09-25 16:46:20 -0700 | [diff] [blame] | 181 | if (count == 0 || WARN_ON_ONCE(count > INT_MAX)) |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 182 | return -E2BIG; |
| 183 | |
| 184 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 185 | /* |
| 186 | * If src is unaligned, don't cross a page boundary, |
| 187 | * since we don't know if the next page is mapped. |
| 188 | */ |
| 189 | if ((long)src & (sizeof(long) - 1)) { |
| 190 | size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)); |
| 191 | if (limit < max) |
| 192 | max = limit; |
| 193 | } |
| 194 | #else |
| 195 | /* If src or dest is unaligned, don't do word-at-a-time. */ |
| 196 | if (((long) dest | (long) src) & (sizeof(long) - 1)) |
| 197 | max = 0; |
| 198 | #endif |
| 199 | |
| 200 | while (max >= sizeof(unsigned long)) { |
| 201 | unsigned long c, data; |
| 202 | |
Andrey Ryabinin | 1a3241f | 2018-02-01 21:00:50 +0300 | [diff] [blame] | 203 | c = read_word_at_a_time(src+res); |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 204 | if (has_zero(c, &data, &constants)) { |
| 205 | data = prep_zero_mask(c, data, &constants); |
| 206 | data = create_zero_mask(data); |
Chris Metcalf | 990486c | 2015-10-06 12:37:41 -0400 | [diff] [blame] | 207 | *(unsigned long *)(dest+res) = c & zero_bytemask(data); |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 208 | return res + find_zero(data); |
| 209 | } |
Chris Metcalf | 990486c | 2015-10-06 12:37:41 -0400 | [diff] [blame] | 210 | *(unsigned long *)(dest+res) = c; |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 211 | res += sizeof(unsigned long); |
| 212 | count -= sizeof(unsigned long); |
| 213 | max -= sizeof(unsigned long); |
| 214 | } |
| 215 | |
| 216 | while (count) { |
| 217 | char c; |
| 218 | |
| 219 | c = src[res]; |
| 220 | dest[res] = c; |
| 221 | if (!c) |
| 222 | return res; |
| 223 | res++; |
| 224 | count--; |
| 225 | } |
| 226 | |
| 227 | /* Hit buffer length without finding a NUL; force NUL-termination. */ |
| 228 | if (res) |
| 229 | dest[res-1] = '\0'; |
| 230 | |
| 231 | return -E2BIG; |
| 232 | } |
| 233 | EXPORT_SYMBOL(strscpy); |
| 234 | #endif |
| 235 | |
Tobin C. Harding | 458a3bf | 2019-04-05 12:58:58 +1100 | [diff] [blame] | 236 | /** |
Nick Desaulniers | 1e1b6d6 | 2020-09-25 21:19:18 -0700 | [diff] [blame] | 237 | * stpcpy - copy a string from src to dest returning a pointer to the new end |
| 238 | * of dest, including src's %NUL-terminator. May overrun dest. |
| 239 | * @dest: pointer to end of string being copied into. Must be large enough |
| 240 | * to receive copy. |
| 241 | * @src: pointer to the beginning of string being copied from. Must not overlap |
| 242 | * dest. |
| 243 | * |
| 244 | * stpcpy differs from strcpy in a key way: the return value is a pointer |
| 245 | * to the new %NUL-terminating character in @dest. (For strcpy, the return |
| 246 | * value is a pointer to the start of @dest). This interface is considered |
| 247 | * unsafe as it doesn't perform bounds checking of the inputs. As such it's |
| 248 | * not recommended for usage. Instead, its definition is provided in case |
| 249 | * the compiler lowers other libcalls to stpcpy. |
| 250 | */ |
| 251 | char *stpcpy(char *__restrict__ dest, const char *__restrict__ src); |
| 252 | char *stpcpy(char *__restrict__ dest, const char *__restrict__ src) |
| 253 | { |
| 254 | while ((*dest++ = *src++) != '\0') |
| 255 | /* nothing */; |
| 256 | return --dest; |
| 257 | } |
| 258 | EXPORT_SYMBOL(stpcpy); |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | #ifndef __HAVE_ARCH_STRCAT |
| 261 | /** |
| 262 | * strcat - Append one %NUL-terminated string to another |
| 263 | * @dest: The string to be appended to |
| 264 | * @src: The string to append to it |
| 265 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 266 | char *strcat(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
| 268 | char *tmp = dest; |
| 269 | |
| 270 | while (*dest) |
| 271 | dest++; |
| 272 | while ((*dest++ = *src++) != '\0') |
| 273 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return tmp; |
| 275 | } |
| 276 | EXPORT_SYMBOL(strcat); |
| 277 | #endif |
| 278 | |
| 279 | #ifndef __HAVE_ARCH_STRNCAT |
| 280 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 281 | * strncat - Append a length-limited, C-string to another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | * @dest: The string to be appended to |
| 283 | * @src: The string to append to it |
| 284 | * @count: The maximum numbers of bytes to copy |
| 285 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 286 | * Note that in contrast to strncpy(), strncat() ensures the result is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | * terminated. |
| 288 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 289 | char *strncat(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | char *tmp = dest; |
| 292 | |
| 293 | if (count) { |
| 294 | while (*dest) |
| 295 | dest++; |
| 296 | while ((*dest++ = *src++) != 0) { |
| 297 | if (--count == 0) { |
| 298 | *dest = '\0'; |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | return tmp; |
| 304 | } |
| 305 | EXPORT_SYMBOL(strncat); |
| 306 | #endif |
| 307 | |
| 308 | #ifndef __HAVE_ARCH_STRLCAT |
| 309 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 310 | * strlcat - Append a length-limited, C-string to another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | * @dest: The string to be appended to |
| 312 | * @src: The string to append to it |
| 313 | * @count: The size of the destination buffer. |
| 314 | */ |
| 315 | size_t strlcat(char *dest, const char *src, size_t count) |
| 316 | { |
| 317 | size_t dsize = strlen(dest); |
| 318 | size_t len = strlen(src); |
| 319 | size_t res = dsize + len; |
| 320 | |
| 321 | /* This would be a bug */ |
| 322 | BUG_ON(dsize >= count); |
| 323 | |
| 324 | dest += dsize; |
| 325 | count -= dsize; |
| 326 | if (len >= count) |
| 327 | len = count-1; |
| 328 | memcpy(dest, src, len); |
| 329 | dest[len] = 0; |
| 330 | return res; |
| 331 | } |
| 332 | EXPORT_SYMBOL(strlcat); |
| 333 | #endif |
| 334 | |
| 335 | #ifndef __HAVE_ARCH_STRCMP |
| 336 | /** |
| 337 | * strcmp - Compare two strings |
| 338 | * @cs: One string |
| 339 | * @ct: Another string |
| 340 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 341 | int strcmp(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 343 | unsigned char c1, c2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | while (1) { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 346 | c1 = *cs++; |
| 347 | c2 = *ct++; |
| 348 | if (c1 != c2) |
| 349 | return c1 < c2 ? -1 : 1; |
| 350 | if (!c1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | break; |
| 352 | } |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 353 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | EXPORT_SYMBOL(strcmp); |
| 356 | #endif |
| 357 | |
| 358 | #ifndef __HAVE_ARCH_STRNCMP |
| 359 | /** |
| 360 | * strncmp - Compare two length-limited strings |
| 361 | * @cs: One string |
| 362 | * @ct: Another string |
| 363 | * @count: The maximum number of bytes to compare |
| 364 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 365 | int strncmp(const char *cs, const char *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 367 | unsigned char c1, c2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | while (count) { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 370 | c1 = *cs++; |
| 371 | c2 = *ct++; |
| 372 | if (c1 != c2) |
| 373 | return c1 < c2 ? -1 : 1; |
| 374 | if (!c1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | break; |
| 376 | count--; |
| 377 | } |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 378 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | EXPORT_SYMBOL(strncmp); |
| 381 | #endif |
| 382 | |
| 383 | #ifndef __HAVE_ARCH_STRCHR |
| 384 | /** |
| 385 | * strchr - Find the first occurrence of a character in a string |
| 386 | * @s: The string to be searched |
| 387 | * @c: The character to search for |
Peter Rosin | b097571 | 2019-07-16 16:27:15 -0700 | [diff] [blame] | 388 | * |
| 389 | * Note that the %NUL-terminator is considered part of the string, and can |
| 390 | * be searched for. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 392 | char *strchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 394 | for (; *s != (char)c; ++s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (*s == '\0') |
| 396 | return NULL; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 397 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | EXPORT_SYMBOL(strchr); |
| 400 | #endif |
| 401 | |
Grant Likely | 11d200e | 2014-03-14 17:00:14 +0000 | [diff] [blame] | 402 | #ifndef __HAVE_ARCH_STRCHRNUL |
| 403 | /** |
| 404 | * strchrnul - Find and return a character in a string, or end of string |
| 405 | * @s: The string to be searched |
| 406 | * @c: The character to search for |
| 407 | * |
| 408 | * Returns pointer to first occurrence of 'c' in s. If c is not found, then |
| 409 | * return a pointer to the null byte at the end of s. |
| 410 | */ |
| 411 | char *strchrnul(const char *s, int c) |
| 412 | { |
| 413 | while (*s && *s != (char)c) |
| 414 | s++; |
| 415 | return (char *)s; |
| 416 | } |
| 417 | EXPORT_SYMBOL(strchrnul); |
| 418 | #endif |
| 419 | |
Yury Norov | 0bee0ce | 2020-02-03 17:37:20 -0800 | [diff] [blame] | 420 | /** |
| 421 | * strnchrnul - Find and return a character in a length limited string, |
| 422 | * or end of string |
| 423 | * @s: The string to be searched |
| 424 | * @count: The number of characters to be searched |
| 425 | * @c: The character to search for |
| 426 | * |
| 427 | * Returns pointer to the first occurrence of 'c' in s. If c is not found, |
| 428 | * then return a pointer to the last character of the string. |
| 429 | */ |
| 430 | char *strnchrnul(const char *s, size_t count, int c) |
| 431 | { |
| 432 | while (count-- && *s && *s != (char)c) |
| 433 | s++; |
| 434 | return (char *)s; |
| 435 | } |
| 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | #ifndef __HAVE_ARCH_STRRCHR |
| 438 | /** |
| 439 | * strrchr - Find the last occurrence of a character in a string |
| 440 | * @s: The string to be searched |
| 441 | * @c: The character to search for |
| 442 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 443 | char *strrchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Rasmus Villemoes | 8da53d4 | 2015-02-13 14:36:44 -0800 | [diff] [blame] | 445 | const char *last = NULL; |
| 446 | do { |
| 447 | if (*s == (char)c) |
| 448 | last = s; |
| 449 | } while (*s++); |
| 450 | return (char *)last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | EXPORT_SYMBOL(strrchr); |
| 453 | #endif |
| 454 | |
| 455 | #ifndef __HAVE_ARCH_STRNCHR |
| 456 | /** |
| 457 | * strnchr - Find a character in a length limited string |
| 458 | * @s: The string to be searched |
| 459 | * @count: The number of characters to be searched |
| 460 | * @c: The character to search for |
Peter Rosin | b097571 | 2019-07-16 16:27:15 -0700 | [diff] [blame] | 461 | * |
| 462 | * Note that the %NUL-terminator is considered part of the string, and can |
| 463 | * be searched for. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | */ |
| 465 | char *strnchr(const char *s, size_t count, int c) |
| 466 | { |
Peter Rosin | b097571 | 2019-07-16 16:27:15 -0700 | [diff] [blame] | 467 | while (count--) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 468 | if (*s == (char)c) |
| 469 | return (char *)s; |
Peter Rosin | b097571 | 2019-07-16 16:27:15 -0700 | [diff] [blame] | 470 | if (*s++ == '\0') |
| 471 | break; |
| 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | return NULL; |
| 474 | } |
| 475 | EXPORT_SYMBOL(strnchr); |
| 476 | #endif |
| 477 | |
| 478 | #ifndef __HAVE_ARCH_STRLEN |
| 479 | /** |
| 480 | * strlen - Find the length of a string |
| 481 | * @s: The string to be sized |
| 482 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 483 | size_t strlen(const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
| 485 | const char *sc; |
| 486 | |
| 487 | for (sc = s; *sc != '\0'; ++sc) |
| 488 | /* nothing */; |
| 489 | return sc - s; |
| 490 | } |
| 491 | EXPORT_SYMBOL(strlen); |
| 492 | #endif |
| 493 | |
| 494 | #ifndef __HAVE_ARCH_STRNLEN |
| 495 | /** |
| 496 | * strnlen - Find the length of a length-limited string |
| 497 | * @s: The string to be sized |
| 498 | * @count: The maximum number of bytes to search |
| 499 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 500 | size_t strnlen(const char *s, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
| 502 | const char *sc; |
| 503 | |
| 504 | for (sc = s; count-- && *sc != '\0'; ++sc) |
| 505 | /* nothing */; |
| 506 | return sc - s; |
| 507 | } |
| 508 | EXPORT_SYMBOL(strnlen); |
| 509 | #endif |
| 510 | |
| 511 | #ifndef __HAVE_ARCH_STRSPN |
| 512 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 513 | * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | * @s: The string to be searched |
| 515 | * @accept: The string to search for |
| 516 | */ |
| 517 | size_t strspn(const char *s, const char *accept) |
| 518 | { |
| 519 | const char *p; |
| 520 | const char *a; |
| 521 | size_t count = 0; |
| 522 | |
| 523 | for (p = s; *p != '\0'; ++p) { |
| 524 | for (a = accept; *a != '\0'; ++a) { |
| 525 | if (*p == *a) |
| 526 | break; |
| 527 | } |
| 528 | if (*a == '\0') |
| 529 | return count; |
| 530 | ++count; |
| 531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return count; |
| 533 | } |
| 534 | |
| 535 | EXPORT_SYMBOL(strspn); |
| 536 | #endif |
| 537 | |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 538 | #ifndef __HAVE_ARCH_STRCSPN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 540 | * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | * @s: The string to be searched |
| 542 | * @reject: The string to avoid |
| 543 | */ |
| 544 | size_t strcspn(const char *s, const char *reject) |
| 545 | { |
| 546 | const char *p; |
| 547 | const char *r; |
| 548 | size_t count = 0; |
| 549 | |
| 550 | for (p = s; *p != '\0'; ++p) { |
| 551 | for (r = reject; *r != '\0'; ++r) { |
| 552 | if (*p == *r) |
| 553 | return count; |
| 554 | } |
| 555 | ++count; |
| 556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return count; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | EXPORT_SYMBOL(strcspn); |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 560 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | #ifndef __HAVE_ARCH_STRPBRK |
| 563 | /** |
| 564 | * strpbrk - Find the first occurrence of a set of characters |
| 565 | * @cs: The string to be searched |
| 566 | * @ct: The characters to search for |
| 567 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 568 | char *strpbrk(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 570 | const char *sc1, *sc2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 572 | for (sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 573 | for (sc2 = ct; *sc2 != '\0'; ++sc2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | if (*sc1 == *sc2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 575 | return (char *)sc1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | return NULL; |
| 579 | } |
Kyle McMartin | 894b577 | 2006-04-10 22:53:56 -0700 | [diff] [blame] | 580 | EXPORT_SYMBOL(strpbrk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | #endif |
| 582 | |
| 583 | #ifndef __HAVE_ARCH_STRSEP |
| 584 | /** |
| 585 | * strsep - Split a string into tokens |
| 586 | * @s: The string to be searched |
| 587 | * @ct: The characters to search for |
| 588 | * |
| 589 | * strsep() updates @s to point after the token, ready for the next call. |
| 590 | * |
| 591 | * It returns empty tokens, too, behaving exactly like the libc function |
| 592 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
| 593 | * Same semantics, slimmer shape. ;) |
| 594 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 595 | char *strsep(char **s, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 597 | char *sbegin = *s; |
| 598 | char *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | if (sbegin == NULL) |
| 601 | return NULL; |
| 602 | |
| 603 | end = strpbrk(sbegin, ct); |
| 604 | if (end) |
| 605 | *end++ = '\0'; |
| 606 | *s = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | return sbegin; |
| 608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | EXPORT_SYMBOL(strsep); |
| 610 | #endif |
| 611 | |
| 612 | #ifndef __HAVE_ARCH_MEMSET |
| 613 | /** |
| 614 | * memset - Fill a region of memory with the given value |
| 615 | * @s: Pointer to the start of the area. |
| 616 | * @c: The byte to fill the area with |
| 617 | * @count: The size of the area. |
| 618 | * |
| 619 | * Do not use memset() to access IO space, use memset_io() instead. |
| 620 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 621 | void *memset(void *s, int c, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 623 | char *xs = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
| 625 | while (count--) |
| 626 | *xs++ = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | return s; |
| 628 | } |
| 629 | EXPORT_SYMBOL(memset); |
| 630 | #endif |
| 631 | |
Matthew Wilcox | 3b3c4ba | 2017-09-08 16:13:48 -0700 | [diff] [blame] | 632 | #ifndef __HAVE_ARCH_MEMSET16 |
| 633 | /** |
| 634 | * memset16() - Fill a memory area with a uint16_t |
| 635 | * @s: Pointer to the start of the area. |
| 636 | * @v: The value to fill the area with |
| 637 | * @count: The number of values to store |
| 638 | * |
| 639 | * Differs from memset() in that it fills with a uint16_t instead |
| 640 | * of a byte. Remember that @count is the number of uint16_ts to |
| 641 | * store, not the number of bytes. |
| 642 | */ |
| 643 | void *memset16(uint16_t *s, uint16_t v, size_t count) |
| 644 | { |
| 645 | uint16_t *xs = s; |
| 646 | |
| 647 | while (count--) |
| 648 | *xs++ = v; |
| 649 | return s; |
| 650 | } |
| 651 | EXPORT_SYMBOL(memset16); |
| 652 | #endif |
| 653 | |
| 654 | #ifndef __HAVE_ARCH_MEMSET32 |
| 655 | /** |
| 656 | * memset32() - Fill a memory area with a uint32_t |
| 657 | * @s: Pointer to the start of the area. |
| 658 | * @v: The value to fill the area with |
| 659 | * @count: The number of values to store |
| 660 | * |
| 661 | * Differs from memset() in that it fills with a uint32_t instead |
| 662 | * of a byte. Remember that @count is the number of uint32_ts to |
| 663 | * store, not the number of bytes. |
| 664 | */ |
| 665 | void *memset32(uint32_t *s, uint32_t v, size_t count) |
| 666 | { |
| 667 | uint32_t *xs = s; |
| 668 | |
| 669 | while (count--) |
| 670 | *xs++ = v; |
| 671 | return s; |
| 672 | } |
| 673 | EXPORT_SYMBOL(memset32); |
| 674 | #endif |
| 675 | |
| 676 | #ifndef __HAVE_ARCH_MEMSET64 |
| 677 | /** |
| 678 | * memset64() - Fill a memory area with a uint64_t |
| 679 | * @s: Pointer to the start of the area. |
| 680 | * @v: The value to fill the area with |
| 681 | * @count: The number of values to store |
| 682 | * |
| 683 | * Differs from memset() in that it fills with a uint64_t instead |
| 684 | * of a byte. Remember that @count is the number of uint64_ts to |
| 685 | * store, not the number of bytes. |
| 686 | */ |
| 687 | void *memset64(uint64_t *s, uint64_t v, size_t count) |
| 688 | { |
| 689 | uint64_t *xs = s; |
| 690 | |
| 691 | while (count--) |
| 692 | *xs++ = v; |
| 693 | return s; |
| 694 | } |
| 695 | EXPORT_SYMBOL(memset64); |
| 696 | #endif |
| 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | #ifndef __HAVE_ARCH_MEMCPY |
| 699 | /** |
| 700 | * memcpy - Copy one area of memory to another |
| 701 | * @dest: Where to copy to |
| 702 | * @src: Where to copy from |
| 703 | * @count: The size of the area. |
| 704 | * |
| 705 | * You should not use this function to access IO space, use memcpy_toio() |
| 706 | * or memcpy_fromio() instead. |
| 707 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 708 | void *memcpy(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 710 | char *tmp = dest; |
Jan-Benedict Glaw | 4c416ab | 2006-04-10 22:54:09 -0700 | [diff] [blame] | 711 | const char *s = src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | while (count--) |
| 714 | *tmp++ = *s++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | return dest; |
| 716 | } |
| 717 | EXPORT_SYMBOL(memcpy); |
| 718 | #endif |
| 719 | |
| 720 | #ifndef __HAVE_ARCH_MEMMOVE |
| 721 | /** |
| 722 | * memmove - Copy one area of memory to another |
| 723 | * @dest: Where to copy to |
| 724 | * @src: Where to copy from |
| 725 | * @count: The size of the area. |
| 726 | * |
| 727 | * Unlike memcpy(), memmove() copes with overlapping areas. |
| 728 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 729 | void *memmove(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Paul Jackson | 82da2c3 | 2005-10-30 15:03:19 -0800 | [diff] [blame] | 731 | char *tmp; |
| 732 | const char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
| 734 | if (dest <= src) { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 735 | tmp = dest; |
| 736 | s = src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | while (count--) |
| 738 | *tmp++ = *s++; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 739 | } else { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 740 | tmp = dest; |
| 741 | tmp += count; |
| 742 | s = src; |
| 743 | s += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | while (count--) |
| 745 | *--tmp = *--s; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return dest; |
| 748 | } |
| 749 | EXPORT_SYMBOL(memmove); |
| 750 | #endif |
| 751 | |
| 752 | #ifndef __HAVE_ARCH_MEMCMP |
| 753 | /** |
| 754 | * memcmp - Compare two areas of memory |
| 755 | * @cs: One area of memory |
| 756 | * @ct: Another area of memory |
| 757 | * @count: The size of the area. |
| 758 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 759 | #undef memcmp |
Andi Kleen | a7330c9 | 2014-02-08 08:52:06 +0100 | [diff] [blame] | 760 | __visible int memcmp(const void *cs, const void *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
| 762 | const unsigned char *su1, *su2; |
| 763 | int res = 0; |
| 764 | |
Linus Torvalds | 291d47c | 2021-07-21 12:01:28 -0700 | [diff] [blame] | 765 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 766 | if (count >= sizeof(unsigned long)) { |
| 767 | const unsigned long *u1 = cs; |
| 768 | const unsigned long *u2 = ct; |
| 769 | do { |
| 770 | if (get_unaligned(u1) != get_unaligned(u2)) |
| 771 | break; |
| 772 | u1++; |
| 773 | u2++; |
| 774 | count -= sizeof(unsigned long); |
| 775 | } while (count >= sizeof(unsigned long)); |
| 776 | cs = u1; |
| 777 | ct = u2; |
| 778 | } |
| 779 | #endif |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 780 | for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if ((res = *su1 - *su2) != 0) |
| 782 | break; |
| 783 | return res; |
| 784 | } |
| 785 | EXPORT_SYMBOL(memcmp); |
| 786 | #endif |
| 787 | |
Nick Desaulniers | 5f074f3 | 2019-04-05 18:38:45 -0700 | [diff] [blame] | 788 | #ifndef __HAVE_ARCH_BCMP |
| 789 | /** |
| 790 | * bcmp - returns 0 if and only if the buffers have identical contents. |
| 791 | * @a: pointer to first buffer. |
| 792 | * @b: pointer to second buffer. |
| 793 | * @len: size of buffers. |
| 794 | * |
| 795 | * The sign or magnitude of a non-zero return value has no particular |
| 796 | * meaning, and architectures may implement their own more efficient bcmp(). So |
| 797 | * while this particular implementation is a simple (tail) call to memcmp, do |
| 798 | * not rely on anything but whether the return value is zero or non-zero. |
| 799 | */ |
Nick Desaulniers | 5f074f3 | 2019-04-05 18:38:45 -0700 | [diff] [blame] | 800 | int bcmp(const void *a, const void *b, size_t len) |
| 801 | { |
| 802 | return memcmp(a, b, len); |
| 803 | } |
| 804 | EXPORT_SYMBOL(bcmp); |
| 805 | #endif |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | #ifndef __HAVE_ARCH_MEMSCAN |
| 808 | /** |
| 809 | * memscan - Find a character in an area of memory. |
| 810 | * @addr: The memory area |
| 811 | * @c: The byte to search for |
| 812 | * @size: The size of the area. |
| 813 | * |
| 814 | * returns the address of the first occurrence of @c, or 1 byte past |
| 815 | * the area if @c is not found |
| 816 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 817 | void *memscan(void *addr, int c, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 819 | unsigned char *p = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | while (size) { |
Alexey Dobriyan | 1a58be6 | 2021-06-30 18:56:01 -0700 | [diff] [blame] | 822 | if (*p == (unsigned char)c) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 823 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | p++; |
| 825 | size--; |
| 826 | } |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 827 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
| 829 | EXPORT_SYMBOL(memscan); |
| 830 | #endif |
| 831 | |
| 832 | #ifndef __HAVE_ARCH_STRSTR |
| 833 | /** |
| 834 | * strstr - Find the first substring in a %NUL terminated string |
| 835 | * @s1: The string to be searched |
| 836 | * @s2: The string to search for |
| 837 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 838 | char *strstr(const char *s1, const char *s2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | { |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 840 | size_t l1, l2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
| 842 | l2 = strlen(s2); |
| 843 | if (!l2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 844 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | l1 = strlen(s1); |
| 846 | while (l1 >= l2) { |
| 847 | l1--; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 848 | if (!memcmp(s1, s2, l2)) |
| 849 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | s1++; |
| 851 | } |
| 852 | return NULL; |
| 853 | } |
| 854 | EXPORT_SYMBOL(strstr); |
| 855 | #endif |
| 856 | |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 857 | #ifndef __HAVE_ARCH_STRNSTR |
| 858 | /** |
| 859 | * strnstr - Find the first substring in a length-limited string |
| 860 | * @s1: The string to be searched |
| 861 | * @s2: The string to search for |
| 862 | * @len: the maximum number of characters to search |
| 863 | */ |
| 864 | char *strnstr(const char *s1, const char *s2, size_t len) |
| 865 | { |
André Goddard Rosa | d6a2eed | 2010-03-05 13:43:12 -0800 | [diff] [blame] | 866 | size_t l2; |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 867 | |
| 868 | l2 = strlen(s2); |
| 869 | if (!l2) |
| 870 | return (char *)s1; |
André Goddard Rosa | d6a2eed | 2010-03-05 13:43:12 -0800 | [diff] [blame] | 871 | while (len >= l2) { |
| 872 | len--; |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 873 | if (!memcmp(s1, s2, l2)) |
| 874 | return (char *)s1; |
| 875 | s1++; |
| 876 | } |
| 877 | return NULL; |
| 878 | } |
| 879 | EXPORT_SYMBOL(strnstr); |
| 880 | #endif |
| 881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | #ifndef __HAVE_ARCH_MEMCHR |
| 883 | /** |
| 884 | * memchr - Find a character in an area of memory. |
| 885 | * @s: The memory area |
| 886 | * @c: The byte to search for |
| 887 | * @n: The size of the area. |
| 888 | * |
| 889 | * returns the address of the first occurrence of @c, or %NULL |
| 890 | * if @c is not found |
| 891 | */ |
| 892 | void *memchr(const void *s, int c, size_t n) |
| 893 | { |
| 894 | const unsigned char *p = s; |
| 895 | while (n-- != 0) { |
| 896 | if ((unsigned char)c == *p++) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 897 | return (void *)(p - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | return NULL; |
| 901 | } |
| 902 | EXPORT_SYMBOL(memchr); |
| 903 | #endif |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 904 | |
| 905 | static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) |
| 906 | { |
| 907 | while (bytes) { |
| 908 | if (*start != value) |
| 909 | return (void *)start; |
| 910 | start++; |
| 911 | bytes--; |
| 912 | } |
| 913 | return NULL; |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * memchr_inv - Find an unmatching character in an area of memory. |
| 918 | * @start: The memory area |
| 919 | * @c: Find a character other than c |
| 920 | * @bytes: The size of the area. |
| 921 | * |
| 922 | * returns the address of the first character other than @c, or %NULL |
| 923 | * if the whole buffer contains just @c. |
| 924 | */ |
| 925 | void *memchr_inv(const void *start, int c, size_t bytes) |
| 926 | { |
| 927 | u8 value = c; |
| 928 | u64 value64; |
| 929 | unsigned int words, prefix; |
| 930 | |
| 931 | if (bytes <= 16) |
| 932 | return check_bytes8(start, value, bytes); |
| 933 | |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 934 | value64 = value; |
Linus Torvalds | 72d9310 | 2014-09-13 11:14:53 -0700 | [diff] [blame] | 935 | #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64 |
Andy Shevchenko | 3368e8f | 2015-11-10 14:45:14 -0800 | [diff] [blame] | 936 | value64 *= 0x0101010101010101ULL; |
Linus Torvalds | 72d9310 | 2014-09-13 11:14:53 -0700 | [diff] [blame] | 937 | #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 938 | value64 *= 0x01010101; |
| 939 | value64 |= value64 << 32; |
| 940 | #else |
| 941 | value64 |= value64 << 8; |
| 942 | value64 |= value64 << 16; |
| 943 | value64 |= value64 << 32; |
| 944 | #endif |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 945 | |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 946 | prefix = (unsigned long)start % 8; |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 947 | if (prefix) { |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 948 | u8 *r; |
| 949 | |
| 950 | prefix = 8 - prefix; |
| 951 | r = check_bytes8(start, value, prefix); |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 952 | if (r) |
| 953 | return r; |
| 954 | start += prefix; |
| 955 | bytes -= prefix; |
| 956 | } |
| 957 | |
| 958 | words = bytes / 8; |
| 959 | |
| 960 | while (words) { |
| 961 | if (*(u64 *)start != value64) |
| 962 | return check_bytes8(start, value, 8); |
| 963 | start += 8; |
| 964 | words--; |
| 965 | } |
| 966 | |
| 967 | return check_bytes8(start, value, bytes % 8); |
| 968 | } |
| 969 | EXPORT_SYMBOL(memchr_inv); |