Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/lib/string.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * stupid library routines.. The optimized versions should generally be found |
| 9 | * as inline code in <asm-xx/string.h> |
| 10 | * |
| 11 | * These are buggy as well.. |
| 12 | * |
| 13 | * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de> |
| 14 | * - Added strsep() which will replace strtok() soon (because strsep() is |
| 15 | * reentrant and should be faster). Use only strsep() in new code, please. |
| 16 | * |
| 17 | * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>, |
| 18 | * Matthew Hawkins <matt@mh.dropbear.id.au> |
| 19 | * - Kissed strtok() goodbye |
| 20 | */ |
| 21 | |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/ctype.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 25 | #include <linux/kernel.h> |
| 26 | #include <linux/export.h> |
Paul Gortmaker | 50af5ea | 2012-01-20 18:35:53 -0500 | [diff] [blame] | 27 | #include <linux/bug.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 28 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 30 | #include <asm/byteorder.h> |
| 31 | #include <asm/word-at-a-time.h> |
| 32 | #include <asm/page.h> |
| 33 | |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 34 | #ifndef __HAVE_ARCH_STRNCASECMP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | /** |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 36 | * strncasecmp - Case insensitive, length-limited string comparison |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * @s1: One string |
| 38 | * @s2: The other string |
| 39 | * @len: the maximum number of characters to compare |
| 40 | */ |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 41 | int strncasecmp(const char *s1, const char *s2, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | /* Yes, Virginia, it had better be unsigned */ |
| 44 | unsigned char c1, c2; |
| 45 | |
André Goddard Rosa | a11d2b6 | 2010-03-05 13:43:11 -0800 | [diff] [blame] | 46 | if (!len) |
| 47 | return 0; |
| 48 | |
| 49 | do { |
| 50 | c1 = *s1++; |
| 51 | c2 = *s2++; |
| 52 | if (!c1 || !c2) |
| 53 | break; |
| 54 | if (c1 == c2) |
| 55 | continue; |
| 56 | c1 = tolower(c1); |
| 57 | c2 = tolower(c2); |
| 58 | if (c1 != c2) |
| 59 | break; |
| 60 | } while (--len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return (int)c1 - (int)c2; |
| 62 | } |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 63 | EXPORT_SYMBOL(strncasecmp); |
| 64 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
David S. Miller | ded220b | 2007-03-29 01:18:42 -0700 | [diff] [blame] | 66 | #ifndef __HAVE_ARCH_STRCASECMP |
| 67 | int strcasecmp(const char *s1, const char *s2) |
| 68 | { |
| 69 | int c1, c2; |
| 70 | |
| 71 | do { |
| 72 | c1 = tolower(*s1++); |
| 73 | c2 = tolower(*s2++); |
| 74 | } while (c1 == c2 && c1 != 0); |
| 75 | return c1 - c2; |
| 76 | } |
| 77 | EXPORT_SYMBOL(strcasecmp); |
| 78 | #endif |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #ifndef __HAVE_ARCH_STRCPY |
| 81 | /** |
| 82 | * strcpy - Copy a %NUL terminated string |
| 83 | * @dest: Where to copy the string to |
| 84 | * @src: Where to copy the string from |
| 85 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 86 | #undef strcpy |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 87 | char *strcpy(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | char *tmp = dest; |
| 90 | |
| 91 | while ((*dest++ = *src++) != '\0') |
| 92 | /* nothing */; |
| 93 | return tmp; |
| 94 | } |
| 95 | EXPORT_SYMBOL(strcpy); |
| 96 | #endif |
| 97 | |
| 98 | #ifndef __HAVE_ARCH_STRNCPY |
| 99 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 100 | * strncpy - Copy a length-limited, C-string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | * @dest: Where to copy the string to |
| 102 | * @src: Where to copy the string from |
| 103 | * @count: The maximum number of bytes to copy |
| 104 | * |
| 105 | * The result is not %NUL-terminated if the source exceeds |
| 106 | * @count bytes. |
walter harms | 2527952 | 2005-05-05 16:16:20 -0700 | [diff] [blame] | 107 | * |
| 108 | * In the case where the length of @src is less than that of |
| 109 | * count, the remainder of @dest will be padded with %NUL. |
| 110 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 112 | char *strncpy(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | char *tmp = dest; |
| 115 | |
| 116 | while (count) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 117 | if ((*tmp = *src) != 0) |
| 118 | src++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | tmp++; |
| 120 | count--; |
| 121 | } |
| 122 | return dest; |
| 123 | } |
| 124 | EXPORT_SYMBOL(strncpy); |
| 125 | #endif |
| 126 | |
| 127 | #ifndef __HAVE_ARCH_STRLCPY |
| 128 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 129 | * strlcpy - Copy a C-string into a sized buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | * @dest: Where to copy the string to |
| 131 | * @src: Where to copy the string from |
| 132 | * @size: size of destination buffer |
| 133 | * |
| 134 | * Compatible with *BSD: the result is always a valid |
| 135 | * NUL-terminated string that fits in the buffer (unless, |
| 136 | * of course, the buffer size is zero). It does not pad |
| 137 | * out the result like strncpy() does. |
| 138 | */ |
| 139 | size_t strlcpy(char *dest, const char *src, size_t size) |
| 140 | { |
| 141 | size_t ret = strlen(src); |
| 142 | |
| 143 | if (size) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 144 | size_t len = (ret >= size) ? size - 1 : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | memcpy(dest, src, len); |
| 146 | dest[len] = '\0'; |
| 147 | } |
| 148 | return ret; |
| 149 | } |
| 150 | EXPORT_SYMBOL(strlcpy); |
| 151 | #endif |
| 152 | |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 153 | #ifndef __HAVE_ARCH_STRSCPY |
| 154 | /** |
| 155 | * strscpy - Copy a C-string into a sized buffer |
| 156 | * @dest: Where to copy the string to |
| 157 | * @src: Where to copy the string from |
| 158 | * @count: Size of destination buffer |
| 159 | * |
Tobin C. Harding | 316c6cc | 2019-04-05 12:58:58 +1100 | [diff] [blame] | 160 | * Copy the string, or as much of it as fits, into the dest buffer. The |
| 161 | * behavior is undefined if the string buffers overlap. The destination |
| 162 | * buffer is always NUL terminated, unless it's zero-sized. |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 163 | * |
| 164 | * Preferred to strlcpy() since the API doesn't require reading memory |
| 165 | * from the src string beyond the specified "count" bytes, and since |
| 166 | * the return value is easier to error-check than strlcpy()'s. |
| 167 | * In addition, the implementation is robust to the string changing out |
| 168 | * from underneath it, unlike the current strlcpy() implementation. |
| 169 | * |
| 170 | * Preferred to strncpy() since it always returns a valid string, and |
| 171 | * doesn't unnecessarily force the tail of the destination buffer to be |
Tobin C. Harding | 316c6cc | 2019-04-05 12:58:58 +1100 | [diff] [blame] | 172 | * zeroed. If zeroing is desired please use strscpy_pad(). |
| 173 | * |
| 174 | * Return: The number of characters copied (not including the trailing |
| 175 | * %NUL) or -E2BIG if the destination buffer wasn't big enough. |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 176 | */ |
| 177 | ssize_t strscpy(char *dest, const char *src, size_t count) |
| 178 | { |
| 179 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
| 180 | size_t max = count; |
| 181 | long res = 0; |
| 182 | |
| 183 | if (count == 0) |
| 184 | return -E2BIG; |
| 185 | |
| 186 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 187 | /* |
| 188 | * If src is unaligned, don't cross a page boundary, |
| 189 | * since we don't know if the next page is mapped. |
| 190 | */ |
| 191 | if ((long)src & (sizeof(long) - 1)) { |
| 192 | size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)); |
| 193 | if (limit < max) |
| 194 | max = limit; |
| 195 | } |
| 196 | #else |
| 197 | /* If src or dest is unaligned, don't do word-at-a-time. */ |
| 198 | if (((long) dest | (long) src) & (sizeof(long) - 1)) |
| 199 | max = 0; |
| 200 | #endif |
| 201 | |
| 202 | while (max >= sizeof(unsigned long)) { |
| 203 | unsigned long c, data; |
| 204 | |
Andrey Ryabinin | 8dd8b4d | 2018-02-01 21:00:50 +0300 | [diff] [blame] | 205 | c = read_word_at_a_time(src+res); |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 206 | if (has_zero(c, &data, &constants)) { |
| 207 | data = prep_zero_mask(c, data, &constants); |
| 208 | data = create_zero_mask(data); |
Chris Metcalf | 990486c | 2015-10-06 12:37:41 -0400 | [diff] [blame] | 209 | *(unsigned long *)(dest+res) = c & zero_bytemask(data); |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 210 | return res + find_zero(data); |
| 211 | } |
Chris Metcalf | 990486c | 2015-10-06 12:37:41 -0400 | [diff] [blame] | 212 | *(unsigned long *)(dest+res) = c; |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 213 | res += sizeof(unsigned long); |
| 214 | count -= sizeof(unsigned long); |
| 215 | max -= sizeof(unsigned long); |
| 216 | } |
| 217 | |
| 218 | while (count) { |
| 219 | char c; |
| 220 | |
| 221 | c = src[res]; |
| 222 | dest[res] = c; |
| 223 | if (!c) |
| 224 | return res; |
| 225 | res++; |
| 226 | count--; |
| 227 | } |
| 228 | |
| 229 | /* Hit buffer length without finding a NUL; force NUL-termination. */ |
| 230 | if (res) |
| 231 | dest[res-1] = '\0'; |
| 232 | |
| 233 | return -E2BIG; |
| 234 | } |
| 235 | EXPORT_SYMBOL(strscpy); |
| 236 | #endif |
| 237 | |
Nick Desaulniers | 586d6f17 | 2020-09-25 21:19:18 -0700 | [diff] [blame] | 238 | /** |
| 239 | * stpcpy - copy a string from src to dest returning a pointer to the new end |
| 240 | * of dest, including src's %NUL-terminator. May overrun dest. |
| 241 | * @dest: pointer to end of string being copied into. Must be large enough |
| 242 | * to receive copy. |
| 243 | * @src: pointer to the beginning of string being copied from. Must not overlap |
| 244 | * dest. |
| 245 | * |
| 246 | * stpcpy differs from strcpy in a key way: the return value is a pointer |
| 247 | * to the new %NUL-terminating character in @dest. (For strcpy, the return |
| 248 | * value is a pointer to the start of @dest). This interface is considered |
| 249 | * unsafe as it doesn't perform bounds checking of the inputs. As such it's |
| 250 | * not recommended for usage. Instead, its definition is provided in case |
| 251 | * the compiler lowers other libcalls to stpcpy. |
| 252 | */ |
| 253 | char *stpcpy(char *__restrict__ dest, const char *__restrict__ src); |
| 254 | char *stpcpy(char *__restrict__ dest, const char *__restrict__ src) |
| 255 | { |
| 256 | while ((*dest++ = *src++) != '\0') |
| 257 | /* nothing */; |
| 258 | return --dest; |
| 259 | } |
| 260 | EXPORT_SYMBOL(stpcpy); |
| 261 | |
Tobin C. Harding | 316c6cc | 2019-04-05 12:58:58 +1100 | [diff] [blame] | 262 | /** |
| 263 | * strscpy_pad() - Copy a C-string into a sized buffer |
| 264 | * @dest: Where to copy the string to |
| 265 | * @src: Where to copy the string from |
| 266 | * @count: Size of destination buffer |
| 267 | * |
| 268 | * Copy the string, or as much of it as fits, into the dest buffer. The |
| 269 | * behavior is undefined if the string buffers overlap. The destination |
| 270 | * buffer is always %NUL terminated, unless it's zero-sized. |
| 271 | * |
| 272 | * If the source string is shorter than the destination buffer, zeros |
| 273 | * the tail of the destination buffer. |
| 274 | * |
| 275 | * For full explanation of why you may want to consider using the |
| 276 | * 'strscpy' functions please see the function docstring for strscpy(). |
| 277 | * |
| 278 | * Return: The number of characters copied (not including the trailing |
| 279 | * %NUL) or -E2BIG if the destination buffer wasn't big enough. |
| 280 | */ |
| 281 | ssize_t strscpy_pad(char *dest, const char *src, size_t count) |
| 282 | { |
| 283 | ssize_t written; |
| 284 | |
| 285 | written = strscpy(dest, src, count); |
| 286 | if (written < 0 || written == count - 1) |
| 287 | return written; |
| 288 | |
| 289 | memset(dest + written + 1, 0, count - written - 1); |
| 290 | |
| 291 | return written; |
| 292 | } |
| 293 | EXPORT_SYMBOL(strscpy_pad); |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | #ifndef __HAVE_ARCH_STRCAT |
| 296 | /** |
| 297 | * strcat - Append one %NUL-terminated string to another |
| 298 | * @dest: The string to be appended to |
| 299 | * @src: The string to append to it |
| 300 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 301 | #undef strcat |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 302 | char *strcat(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
| 304 | char *tmp = dest; |
| 305 | |
| 306 | while (*dest) |
| 307 | dest++; |
| 308 | while ((*dest++ = *src++) != '\0') |
| 309 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | return tmp; |
| 311 | } |
| 312 | EXPORT_SYMBOL(strcat); |
| 313 | #endif |
| 314 | |
| 315 | #ifndef __HAVE_ARCH_STRNCAT |
| 316 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 317 | * strncat - Append a length-limited, C-string to another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | * @dest: The string to be appended to |
| 319 | * @src: The string to append to it |
| 320 | * @count: The maximum numbers of bytes to copy |
| 321 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 322 | * Note that in contrast to strncpy(), strncat() ensures the result is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | * terminated. |
| 324 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 325 | char *strncat(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | char *tmp = dest; |
| 328 | |
| 329 | if (count) { |
| 330 | while (*dest) |
| 331 | dest++; |
| 332 | while ((*dest++ = *src++) != 0) { |
| 333 | if (--count == 0) { |
| 334 | *dest = '\0'; |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return tmp; |
| 340 | } |
| 341 | EXPORT_SYMBOL(strncat); |
| 342 | #endif |
| 343 | |
| 344 | #ifndef __HAVE_ARCH_STRLCAT |
| 345 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 346 | * strlcat - Append a length-limited, C-string to another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | * @dest: The string to be appended to |
| 348 | * @src: The string to append to it |
| 349 | * @count: The size of the destination buffer. |
| 350 | */ |
| 351 | size_t strlcat(char *dest, const char *src, size_t count) |
| 352 | { |
| 353 | size_t dsize = strlen(dest); |
| 354 | size_t len = strlen(src); |
| 355 | size_t res = dsize + len; |
| 356 | |
| 357 | /* This would be a bug */ |
| 358 | BUG_ON(dsize >= count); |
| 359 | |
| 360 | dest += dsize; |
| 361 | count -= dsize; |
| 362 | if (len >= count) |
| 363 | len = count-1; |
| 364 | memcpy(dest, src, len); |
| 365 | dest[len] = 0; |
| 366 | return res; |
| 367 | } |
| 368 | EXPORT_SYMBOL(strlcat); |
| 369 | #endif |
| 370 | |
| 371 | #ifndef __HAVE_ARCH_STRCMP |
| 372 | /** |
| 373 | * strcmp - Compare two strings |
| 374 | * @cs: One string |
| 375 | * @ct: Another string |
| 376 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 377 | #undef strcmp |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 378 | int strcmp(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 380 | unsigned char c1, c2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | while (1) { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 383 | c1 = *cs++; |
| 384 | c2 = *ct++; |
| 385 | if (c1 != c2) |
| 386 | return c1 < c2 ? -1 : 1; |
| 387 | if (!c1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | break; |
| 389 | } |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 390 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | EXPORT_SYMBOL(strcmp); |
| 393 | #endif |
| 394 | |
| 395 | #ifndef __HAVE_ARCH_STRNCMP |
| 396 | /** |
| 397 | * strncmp - Compare two length-limited strings |
| 398 | * @cs: One string |
| 399 | * @ct: Another string |
| 400 | * @count: The maximum number of bytes to compare |
| 401 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 402 | int strncmp(const char *cs, const char *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 404 | unsigned char c1, c2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | while (count) { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 407 | c1 = *cs++; |
| 408 | c2 = *ct++; |
| 409 | if (c1 != c2) |
| 410 | return c1 < c2 ? -1 : 1; |
| 411 | if (!c1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | break; |
| 413 | count--; |
| 414 | } |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 415 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | EXPORT_SYMBOL(strncmp); |
| 418 | #endif |
| 419 | |
| 420 | #ifndef __HAVE_ARCH_STRCHR |
| 421 | /** |
| 422 | * strchr - Find the first occurrence of a character in a string |
| 423 | * @s: The string to be searched |
| 424 | * @c: The character to search for |
| 425 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 426 | char *strchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 428 | for (; *s != (char)c; ++s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | if (*s == '\0') |
| 430 | return NULL; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 431 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | EXPORT_SYMBOL(strchr); |
| 434 | #endif |
| 435 | |
Grant Likely | 11d200e | 2014-03-14 17:00:14 +0000 | [diff] [blame] | 436 | #ifndef __HAVE_ARCH_STRCHRNUL |
| 437 | /** |
| 438 | * strchrnul - Find and return a character in a string, or end of string |
| 439 | * @s: The string to be searched |
| 440 | * @c: The character to search for |
| 441 | * |
| 442 | * Returns pointer to first occurrence of 'c' in s. If c is not found, then |
| 443 | * return a pointer to the null byte at the end of s. |
| 444 | */ |
| 445 | char *strchrnul(const char *s, int c) |
| 446 | { |
| 447 | while (*s && *s != (char)c) |
| 448 | s++; |
| 449 | return (char *)s; |
| 450 | } |
| 451 | EXPORT_SYMBOL(strchrnul); |
| 452 | #endif |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | #ifndef __HAVE_ARCH_STRRCHR |
| 455 | /** |
| 456 | * strrchr - Find the last occurrence of a character in a string |
| 457 | * @s: The string to be searched |
| 458 | * @c: The character to search for |
| 459 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 460 | char *strrchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
Rasmus Villemoes | 8da53d4 | 2015-02-13 14:36:44 -0800 | [diff] [blame] | 462 | const char *last = NULL; |
| 463 | do { |
| 464 | if (*s == (char)c) |
| 465 | last = s; |
| 466 | } while (*s++); |
| 467 | return (char *)last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | EXPORT_SYMBOL(strrchr); |
| 470 | #endif |
| 471 | |
| 472 | #ifndef __HAVE_ARCH_STRNCHR |
| 473 | /** |
| 474 | * strnchr - Find a character in a length limited string |
| 475 | * @s: The string to be searched |
| 476 | * @count: The number of characters to be searched |
| 477 | * @c: The character to search for |
| 478 | */ |
| 479 | char *strnchr(const char *s, size_t count, int c) |
| 480 | { |
| 481 | for (; count-- && *s != '\0'; ++s) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 482 | if (*s == (char)c) |
| 483 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| 486 | EXPORT_SYMBOL(strnchr); |
| 487 | #endif |
| 488 | |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 489 | /** |
Randy Dunlap | a6cd13f | 2009-12-21 14:37:22 -0800 | [diff] [blame] | 490 | * skip_spaces - Removes leading whitespace from @str. |
| 491 | * @str: The string to be stripped. |
André Goddard Rosa | f653398 | 2009-12-14 18:01:04 -0800 | [diff] [blame] | 492 | * |
Randy Dunlap | a6cd13f | 2009-12-21 14:37:22 -0800 | [diff] [blame] | 493 | * Returns a pointer to the first non-whitespace character in @str. |
André Goddard Rosa | f653398 | 2009-12-14 18:01:04 -0800 | [diff] [blame] | 494 | */ |
| 495 | char *skip_spaces(const char *str) |
| 496 | { |
| 497 | while (isspace(*str)) |
| 498 | ++str; |
| 499 | return (char *)str; |
| 500 | } |
| 501 | EXPORT_SYMBOL(skip_spaces); |
| 502 | |
| 503 | /** |
KOSAKI Motohiro | ca54cb8 | 2009-12-14 18:01:15 -0800 | [diff] [blame] | 504 | * strim - Removes leading and trailing whitespace from @s. |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 505 | * @s: The string to be stripped. |
| 506 | * |
| 507 | * Note that the first trailing whitespace is replaced with a %NUL-terminator |
| 508 | * in the given string @s. Returns a pointer to the first non-whitespace |
| 509 | * character in @s. |
| 510 | */ |
KOSAKI Motohiro | ca54cb8 | 2009-12-14 18:01:15 -0800 | [diff] [blame] | 511 | char *strim(char *s) |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 512 | { |
| 513 | size_t size; |
| 514 | char *end; |
| 515 | |
| 516 | size = strlen(s); |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 517 | if (!size) |
| 518 | return s; |
| 519 | |
| 520 | end = s + size - 1; |
Michael Holzheu | 6e6d9fa | 2006-10-28 10:38:47 -0700 | [diff] [blame] | 521 | while (end >= s && isspace(*end)) |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 522 | end--; |
| 523 | *(end + 1) = '\0'; |
| 524 | |
Michael Holzheu | 66f6958 | 2011-10-31 17:12:37 -0700 | [diff] [blame] | 525 | return skip_spaces(s); |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 526 | } |
KOSAKI Motohiro | ca54cb8 | 2009-12-14 18:01:15 -0800 | [diff] [blame] | 527 | EXPORT_SYMBOL(strim); |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | #ifndef __HAVE_ARCH_STRLEN |
| 530 | /** |
| 531 | * strlen - Find the length of a string |
| 532 | * @s: The string to be sized |
| 533 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 534 | size_t strlen(const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | { |
| 536 | const char *sc; |
| 537 | |
| 538 | for (sc = s; *sc != '\0'; ++sc) |
| 539 | /* nothing */; |
| 540 | return sc - s; |
| 541 | } |
| 542 | EXPORT_SYMBOL(strlen); |
| 543 | #endif |
| 544 | |
| 545 | #ifndef __HAVE_ARCH_STRNLEN |
| 546 | /** |
| 547 | * strnlen - Find the length of a length-limited string |
| 548 | * @s: The string to be sized |
| 549 | * @count: The maximum number of bytes to search |
| 550 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 551 | size_t strnlen(const char *s, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | const char *sc; |
| 554 | |
| 555 | for (sc = s; count-- && *sc != '\0'; ++sc) |
| 556 | /* nothing */; |
| 557 | return sc - s; |
| 558 | } |
| 559 | EXPORT_SYMBOL(strnlen); |
| 560 | #endif |
| 561 | |
| 562 | #ifndef __HAVE_ARCH_STRSPN |
| 563 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 564 | * 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] | 565 | * @s: The string to be searched |
| 566 | * @accept: The string to search for |
| 567 | */ |
| 568 | size_t strspn(const char *s, const char *accept) |
| 569 | { |
| 570 | const char *p; |
| 571 | const char *a; |
| 572 | size_t count = 0; |
| 573 | |
| 574 | for (p = s; *p != '\0'; ++p) { |
| 575 | for (a = accept; *a != '\0'; ++a) { |
| 576 | if (*p == *a) |
| 577 | break; |
| 578 | } |
| 579 | if (*a == '\0') |
| 580 | return count; |
| 581 | ++count; |
| 582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return count; |
| 584 | } |
| 585 | |
| 586 | EXPORT_SYMBOL(strspn); |
| 587 | #endif |
| 588 | |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 589 | #ifndef __HAVE_ARCH_STRCSPN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 591 | * 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] | 592 | * @s: The string to be searched |
| 593 | * @reject: The string to avoid |
| 594 | */ |
| 595 | size_t strcspn(const char *s, const char *reject) |
| 596 | { |
| 597 | const char *p; |
| 598 | const char *r; |
| 599 | size_t count = 0; |
| 600 | |
| 601 | for (p = s; *p != '\0'; ++p) { |
| 602 | for (r = reject; *r != '\0'; ++r) { |
| 603 | if (*p == *r) |
| 604 | return count; |
| 605 | } |
| 606 | ++count; |
| 607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | return count; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | EXPORT_SYMBOL(strcspn); |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 611 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | #ifndef __HAVE_ARCH_STRPBRK |
| 614 | /** |
| 615 | * strpbrk - Find the first occurrence of a set of characters |
| 616 | * @cs: The string to be searched |
| 617 | * @ct: The characters to search for |
| 618 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 619 | char *strpbrk(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 621 | const char *sc1, *sc2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 623 | for (sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 624 | for (sc2 = ct; *sc2 != '\0'; ++sc2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | if (*sc1 == *sc2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 626 | return (char *)sc1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | return NULL; |
| 630 | } |
Kyle McMartin | 894b577 | 2006-04-10 22:53:56 -0700 | [diff] [blame] | 631 | EXPORT_SYMBOL(strpbrk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | #endif |
| 633 | |
| 634 | #ifndef __HAVE_ARCH_STRSEP |
| 635 | /** |
| 636 | * strsep - Split a string into tokens |
| 637 | * @s: The string to be searched |
| 638 | * @ct: The characters to search for |
| 639 | * |
| 640 | * strsep() updates @s to point after the token, ready for the next call. |
| 641 | * |
| 642 | * It returns empty tokens, too, behaving exactly like the libc function |
| 643 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
| 644 | * Same semantics, slimmer shape. ;) |
| 645 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 646 | char *strsep(char **s, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 648 | char *sbegin = *s; |
| 649 | char *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
| 651 | if (sbegin == NULL) |
| 652 | return NULL; |
| 653 | |
| 654 | end = strpbrk(sbegin, ct); |
| 655 | if (end) |
| 656 | *end++ = '\0'; |
| 657 | *s = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return sbegin; |
| 659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | EXPORT_SYMBOL(strsep); |
| 661 | #endif |
| 662 | |
David Brownell | 34990cf | 2008-05-01 04:34:42 -0700 | [diff] [blame] | 663 | /** |
| 664 | * sysfs_streq - return true if strings are equal, modulo trailing newline |
| 665 | * @s1: one string |
| 666 | * @s2: another string |
| 667 | * |
| 668 | * This routine returns true iff two strings are equal, treating both |
| 669 | * NUL and newline-then-NUL as equivalent string terminations. It's |
| 670 | * geared for use with sysfs input strings, which generally terminate |
| 671 | * with newlines but are compared against values without newlines. |
| 672 | */ |
| 673 | bool sysfs_streq(const char *s1, const char *s2) |
| 674 | { |
| 675 | while (*s1 && *s1 == *s2) { |
| 676 | s1++; |
| 677 | s2++; |
| 678 | } |
| 679 | |
| 680 | if (*s1 == *s2) |
| 681 | return true; |
| 682 | if (!*s1 && *s2 == '\n' && !s2[1]) |
| 683 | return true; |
| 684 | if (*s1 == '\n' && !s1[1] && !*s2) |
| 685 | return true; |
| 686 | return false; |
| 687 | } |
| 688 | EXPORT_SYMBOL(sysfs_streq); |
| 689 | |
Jonathan Cameron | d0f1fed | 2011-04-19 12:43:45 +0100 | [diff] [blame] | 690 | /** |
Andy Shevchenko | 56b0608 | 2016-03-17 14:22:14 -0700 | [diff] [blame] | 691 | * match_string - matches given string in an array |
| 692 | * @array: array of strings |
| 693 | * @n: number of strings in the array or -1 for NULL terminated arrays |
| 694 | * @string: string to match with |
| 695 | * |
| 696 | * Return: |
| 697 | * index of a @string in the @array if matches, or %-EINVAL otherwise. |
| 698 | */ |
| 699 | int match_string(const char * const *array, size_t n, const char *string) |
| 700 | { |
| 701 | int index; |
| 702 | const char *item; |
| 703 | |
| 704 | for (index = 0; index < n; index++) { |
| 705 | item = array[index]; |
| 706 | if (!item) |
| 707 | break; |
| 708 | if (!strcmp(item, string)) |
| 709 | return index; |
| 710 | } |
| 711 | |
| 712 | return -EINVAL; |
| 713 | } |
| 714 | EXPORT_SYMBOL(match_string); |
| 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | #ifndef __HAVE_ARCH_MEMSET |
| 717 | /** |
| 718 | * memset - Fill a region of memory with the given value |
| 719 | * @s: Pointer to the start of the area. |
| 720 | * @c: The byte to fill the area with |
| 721 | * @count: The size of the area. |
| 722 | * |
| 723 | * Do not use memset() to access IO space, use memset_io() instead. |
| 724 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 725 | void *memset(void *s, int c, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 727 | char *xs = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | while (count--) |
| 730 | *xs++ = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | return s; |
| 732 | } |
| 733 | EXPORT_SYMBOL(memset); |
| 734 | #endif |
| 735 | |
Daniel Borkmann | d4c5efd | 2014-08-26 23:16:35 -0400 | [diff] [blame] | 736 | /** |
| 737 | * memzero_explicit - Fill a region of memory (e.g. sensitive |
| 738 | * keying data) with 0s. |
| 739 | * @s: Pointer to the start of the area. |
| 740 | * @count: The size of the area. |
| 741 | * |
Daniel Borkmann | 8155330 | 2015-01-06 00:27:45 +0100 | [diff] [blame] | 742 | * Note: usually using memset() is just fine (!), but in cases |
| 743 | * where clearing out _local_ data at the end of a scope is |
| 744 | * necessary, memzero_explicit() should be used instead in |
| 745 | * order to prevent the compiler from optimising away zeroing. |
| 746 | * |
Daniel Borkmann | d4c5efd | 2014-08-26 23:16:35 -0400 | [diff] [blame] | 747 | * memzero_explicit() doesn't need an arch-specific version as |
| 748 | * it just invokes the one of memset() implicitly. |
| 749 | */ |
| 750 | void memzero_explicit(void *s, size_t count) |
| 751 | { |
| 752 | memset(s, 0, count); |
Daniel Borkmann | 7829fb0 | 2015-04-30 04:13:52 +0200 | [diff] [blame] | 753 | barrier_data(s); |
Daniel Borkmann | d4c5efd | 2014-08-26 23:16:35 -0400 | [diff] [blame] | 754 | } |
| 755 | EXPORT_SYMBOL(memzero_explicit); |
| 756 | |
Matthew Wilcox | fe0bc3f | 2017-09-08 16:13:48 -0700 | [diff] [blame] | 757 | #ifndef __HAVE_ARCH_MEMSET16 |
| 758 | /** |
| 759 | * memset16() - Fill a memory area with a uint16_t |
| 760 | * @s: Pointer to the start of the area. |
| 761 | * @v: The value to fill the area with |
| 762 | * @count: The number of values to store |
| 763 | * |
| 764 | * Differs from memset() in that it fills with a uint16_t instead |
| 765 | * of a byte. Remember that @count is the number of uint16_ts to |
| 766 | * store, not the number of bytes. |
| 767 | */ |
| 768 | void *memset16(uint16_t *s, uint16_t v, size_t count) |
| 769 | { |
| 770 | uint16_t *xs = s; |
| 771 | |
| 772 | while (count--) |
| 773 | *xs++ = v; |
| 774 | return s; |
| 775 | } |
| 776 | EXPORT_SYMBOL(memset16); |
| 777 | #endif |
| 778 | |
| 779 | #ifndef __HAVE_ARCH_MEMSET32 |
| 780 | /** |
| 781 | * memset32() - Fill a memory area with a uint32_t |
| 782 | * @s: Pointer to the start of the area. |
| 783 | * @v: The value to fill the area with |
| 784 | * @count: The number of values to store |
| 785 | * |
| 786 | * Differs from memset() in that it fills with a uint32_t instead |
| 787 | * of a byte. Remember that @count is the number of uint32_ts to |
| 788 | * store, not the number of bytes. |
| 789 | */ |
| 790 | void *memset32(uint32_t *s, uint32_t v, size_t count) |
| 791 | { |
| 792 | uint32_t *xs = s; |
| 793 | |
| 794 | while (count--) |
| 795 | *xs++ = v; |
| 796 | return s; |
| 797 | } |
| 798 | EXPORT_SYMBOL(memset32); |
| 799 | #endif |
| 800 | |
| 801 | #ifndef __HAVE_ARCH_MEMSET64 |
| 802 | /** |
| 803 | * memset64() - Fill a memory area with a uint64_t |
| 804 | * @s: Pointer to the start of the area. |
| 805 | * @v: The value to fill the area with |
| 806 | * @count: The number of values to store |
| 807 | * |
| 808 | * Differs from memset() in that it fills with a uint64_t instead |
| 809 | * of a byte. Remember that @count is the number of uint64_ts to |
| 810 | * store, not the number of bytes. |
| 811 | */ |
| 812 | void *memset64(uint64_t *s, uint64_t v, size_t count) |
| 813 | { |
| 814 | uint64_t *xs = s; |
| 815 | |
| 816 | while (count--) |
| 817 | *xs++ = v; |
| 818 | return s; |
| 819 | } |
| 820 | EXPORT_SYMBOL(memset64); |
| 821 | #endif |
| 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | #ifndef __HAVE_ARCH_MEMCPY |
| 824 | /** |
| 825 | * memcpy - Copy one area of memory to another |
| 826 | * @dest: Where to copy to |
| 827 | * @src: Where to copy from |
| 828 | * @count: The size of the area. |
| 829 | * |
| 830 | * You should not use this function to access IO space, use memcpy_toio() |
| 831 | * or memcpy_fromio() instead. |
| 832 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 833 | void *memcpy(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 835 | char *tmp = dest; |
Jan-Benedict Glaw | 4c416ab | 2006-04-10 22:54:09 -0700 | [diff] [blame] | 836 | const char *s = src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
| 838 | while (count--) |
| 839 | *tmp++ = *s++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | return dest; |
| 841 | } |
| 842 | EXPORT_SYMBOL(memcpy); |
| 843 | #endif |
| 844 | |
| 845 | #ifndef __HAVE_ARCH_MEMMOVE |
| 846 | /** |
| 847 | * memmove - Copy one area of memory to another |
| 848 | * @dest: Where to copy to |
| 849 | * @src: Where to copy from |
| 850 | * @count: The size of the area. |
| 851 | * |
| 852 | * Unlike memcpy(), memmove() copes with overlapping areas. |
| 853 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 854 | void *memmove(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | { |
Paul Jackson | 82da2c3 | 2005-10-30 15:03:19 -0800 | [diff] [blame] | 856 | char *tmp; |
| 857 | const char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
| 859 | if (dest <= src) { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 860 | tmp = dest; |
| 861 | s = src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | while (count--) |
| 863 | *tmp++ = *s++; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 864 | } else { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 865 | tmp = dest; |
| 866 | tmp += count; |
| 867 | s = src; |
| 868 | s += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | while (count--) |
| 870 | *--tmp = *--s; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 871 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | return dest; |
| 873 | } |
| 874 | EXPORT_SYMBOL(memmove); |
| 875 | #endif |
| 876 | |
| 877 | #ifndef __HAVE_ARCH_MEMCMP |
| 878 | /** |
| 879 | * memcmp - Compare two areas of memory |
| 880 | * @cs: One area of memory |
| 881 | * @ct: Another area of memory |
| 882 | * @count: The size of the area. |
| 883 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 884 | #undef memcmp |
Andi Kleen | a7330c9 | 2014-02-08 08:52:06 +0100 | [diff] [blame] | 885 | __visible int memcmp(const void *cs, const void *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | { |
| 887 | const unsigned char *su1, *su2; |
| 888 | int res = 0; |
| 889 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 890 | for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | if ((res = *su1 - *su2) != 0) |
| 892 | break; |
| 893 | return res; |
| 894 | } |
| 895 | EXPORT_SYMBOL(memcmp); |
| 896 | #endif |
| 897 | |
Nick Desaulniers | 7eceaf5 | 2019-04-05 18:38:45 -0700 | [diff] [blame] | 898 | #ifndef __HAVE_ARCH_BCMP |
| 899 | /** |
| 900 | * bcmp - returns 0 if and only if the buffers have identical contents. |
| 901 | * @a: pointer to first buffer. |
| 902 | * @b: pointer to second buffer. |
| 903 | * @len: size of buffers. |
| 904 | * |
| 905 | * The sign or magnitude of a non-zero return value has no particular |
| 906 | * meaning, and architectures may implement their own more efficient bcmp(). So |
| 907 | * while this particular implementation is a simple (tail) call to memcmp, do |
| 908 | * not rely on anything but whether the return value is zero or non-zero. |
| 909 | */ |
| 910 | #undef bcmp |
| 911 | int bcmp(const void *a, const void *b, size_t len) |
| 912 | { |
| 913 | return memcmp(a, b, len); |
| 914 | } |
| 915 | EXPORT_SYMBOL(bcmp); |
| 916 | #endif |
| 917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | #ifndef __HAVE_ARCH_MEMSCAN |
| 919 | /** |
| 920 | * memscan - Find a character in an area of memory. |
| 921 | * @addr: The memory area |
| 922 | * @c: The byte to search for |
| 923 | * @size: The size of the area. |
| 924 | * |
| 925 | * returns the address of the first occurrence of @c, or 1 byte past |
| 926 | * the area if @c is not found |
| 927 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 928 | void *memscan(void *addr, int c, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 930 | unsigned char *p = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
| 932 | while (size) { |
| 933 | if (*p == c) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 934 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | p++; |
| 936 | size--; |
| 937 | } |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 938 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | EXPORT_SYMBOL(memscan); |
| 941 | #endif |
| 942 | |
| 943 | #ifndef __HAVE_ARCH_STRSTR |
| 944 | /** |
| 945 | * strstr - Find the first substring in a %NUL terminated string |
| 946 | * @s1: The string to be searched |
| 947 | * @s2: The string to search for |
| 948 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 949 | char *strstr(const char *s1, const char *s2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 951 | size_t l1, l2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
| 953 | l2 = strlen(s2); |
| 954 | if (!l2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 955 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | l1 = strlen(s1); |
| 957 | while (l1 >= l2) { |
| 958 | l1--; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 959 | if (!memcmp(s1, s2, l2)) |
| 960 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | s1++; |
| 962 | } |
| 963 | return NULL; |
| 964 | } |
| 965 | EXPORT_SYMBOL(strstr); |
| 966 | #endif |
| 967 | |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 968 | #ifndef __HAVE_ARCH_STRNSTR |
| 969 | /** |
| 970 | * strnstr - Find the first substring in a length-limited string |
| 971 | * @s1: The string to be searched |
| 972 | * @s2: The string to search for |
| 973 | * @len: the maximum number of characters to search |
| 974 | */ |
| 975 | char *strnstr(const char *s1, const char *s2, size_t len) |
| 976 | { |
André Goddard Rosa | d6a2eed | 2010-03-05 13:43:12 -0800 | [diff] [blame] | 977 | size_t l2; |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 978 | |
| 979 | l2 = strlen(s2); |
| 980 | if (!l2) |
| 981 | return (char *)s1; |
André Goddard Rosa | d6a2eed | 2010-03-05 13:43:12 -0800 | [diff] [blame] | 982 | while (len >= l2) { |
| 983 | len--; |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 984 | if (!memcmp(s1, s2, l2)) |
| 985 | return (char *)s1; |
| 986 | s1++; |
| 987 | } |
| 988 | return NULL; |
| 989 | } |
| 990 | EXPORT_SYMBOL(strnstr); |
| 991 | #endif |
| 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | #ifndef __HAVE_ARCH_MEMCHR |
| 994 | /** |
| 995 | * memchr - Find a character in an area of memory. |
| 996 | * @s: The memory area |
| 997 | * @c: The byte to search for |
| 998 | * @n: The size of the area. |
| 999 | * |
| 1000 | * returns the address of the first occurrence of @c, or %NULL |
| 1001 | * if @c is not found |
| 1002 | */ |
| 1003 | void *memchr(const void *s, int c, size_t n) |
| 1004 | { |
| 1005 | const unsigned char *p = s; |
| 1006 | while (n-- != 0) { |
| 1007 | if ((unsigned char)c == *p++) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 1008 | return (void *)(p - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | EXPORT_SYMBOL(memchr); |
| 1014 | #endif |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 1015 | |
| 1016 | static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) |
| 1017 | { |
| 1018 | while (bytes) { |
| 1019 | if (*start != value) |
| 1020 | return (void *)start; |
| 1021 | start++; |
| 1022 | bytes--; |
| 1023 | } |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * memchr_inv - Find an unmatching character in an area of memory. |
| 1029 | * @start: The memory area |
| 1030 | * @c: Find a character other than c |
| 1031 | * @bytes: The size of the area. |
| 1032 | * |
| 1033 | * returns the address of the first character other than @c, or %NULL |
| 1034 | * if the whole buffer contains just @c. |
| 1035 | */ |
| 1036 | void *memchr_inv(const void *start, int c, size_t bytes) |
| 1037 | { |
| 1038 | u8 value = c; |
| 1039 | u64 value64; |
| 1040 | unsigned int words, prefix; |
| 1041 | |
| 1042 | if (bytes <= 16) |
| 1043 | return check_bytes8(start, value, bytes); |
| 1044 | |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 1045 | value64 = value; |
Linus Torvalds | 72d9310 | 2014-09-13 11:14:53 -0700 | [diff] [blame] | 1046 | #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64 |
Andy Shevchenko | 3368e8fb | 2015-11-10 14:45:14 -0800 | [diff] [blame] | 1047 | value64 *= 0x0101010101010101ULL; |
Linus Torvalds | 72d9310 | 2014-09-13 11:14:53 -0700 | [diff] [blame] | 1048 | #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 1049 | value64 *= 0x01010101; |
| 1050 | value64 |= value64 << 32; |
| 1051 | #else |
| 1052 | value64 |= value64 << 8; |
| 1053 | value64 |= value64 << 16; |
| 1054 | value64 |= value64 << 32; |
| 1055 | #endif |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 1056 | |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 1057 | prefix = (unsigned long)start % 8; |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 1058 | if (prefix) { |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 1059 | u8 *r; |
| 1060 | |
| 1061 | prefix = 8 - prefix; |
| 1062 | r = check_bytes8(start, value, prefix); |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 1063 | if (r) |
| 1064 | return r; |
| 1065 | start += prefix; |
| 1066 | bytes -= prefix; |
| 1067 | } |
| 1068 | |
| 1069 | words = bytes / 8; |
| 1070 | |
| 1071 | while (words) { |
| 1072 | if (*(u64 *)start != value64) |
| 1073 | return check_bytes8(start, value, 8); |
| 1074 | start += 8; |
| 1075 | words--; |
| 1076 | } |
| 1077 | |
| 1078 | return check_bytes8(start, value, bytes % 8); |
| 1079 | } |
| 1080 | EXPORT_SYMBOL(memchr_inv); |
Rasmus Villemoes | 94df290 | 2015-06-25 15:02:22 -0700 | [diff] [blame] | 1081 | |
| 1082 | /** |
| 1083 | * strreplace - Replace all occurrences of character in string. |
| 1084 | * @s: The string to operate on. |
| 1085 | * @old: The character being replaced. |
| 1086 | * @new: The character @old is replaced with. |
| 1087 | * |
| 1088 | * Returns pointer to the nul byte at the end of @s. |
| 1089 | */ |
| 1090 | char *strreplace(char *s, char old, char new) |
| 1091 | { |
| 1092 | for (; *s; ++s) |
| 1093 | if (*s == old) |
| 1094 | *s = new; |
| 1095 | return s; |
| 1096 | } |
| 1097 | EXPORT_SYMBOL(strreplace); |
Daniel Micay | 0f51310 | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 1098 | |
| 1099 | void fortify_panic(const char *name) |
| 1100 | { |
| 1101 | pr_emerg("detected buffer overflow in %s\n", name); |
| 1102 | BUG(); |
| 1103 | } |
| 1104 | EXPORT_SYMBOL(fortify_panic); |