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 | |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 30 | #ifndef __HAVE_ARCH_STRNCASECMP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /** |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 32 | * strncasecmp - Case insensitive, length-limited string comparison |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * @s1: One string |
| 34 | * @s2: The other string |
| 35 | * @len: the maximum number of characters to compare |
| 36 | */ |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 37 | int strncasecmp(const char *s1, const char *s2, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | /* Yes, Virginia, it had better be unsigned */ |
| 40 | unsigned char c1, c2; |
| 41 | |
André Goddard Rosa | a11d2b6 | 2010-03-05 13:43:11 -0800 | [diff] [blame] | 42 | if (!len) |
| 43 | return 0; |
| 44 | |
| 45 | do { |
| 46 | c1 = *s1++; |
| 47 | c2 = *s2++; |
| 48 | if (!c1 || !c2) |
| 49 | break; |
| 50 | if (c1 == c2) |
| 51 | continue; |
| 52 | c1 = tolower(c1); |
| 53 | c2 = tolower(c2); |
| 54 | if (c1 != c2) |
| 55 | break; |
| 56 | } while (--len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | return (int)c1 - (int)c2; |
| 58 | } |
Rasmus Villemoes | cd514e7 | 2014-10-13 15:54:25 -0700 | [diff] [blame] | 59 | EXPORT_SYMBOL(strncasecmp); |
| 60 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
David S. Miller | ded220b | 2007-03-29 01:18:42 -0700 | [diff] [blame] | 62 | #ifndef __HAVE_ARCH_STRCASECMP |
| 63 | int strcasecmp(const char *s1, const char *s2) |
| 64 | { |
| 65 | int c1, c2; |
| 66 | |
| 67 | do { |
| 68 | c1 = tolower(*s1++); |
| 69 | c2 = tolower(*s2++); |
| 70 | } while (c1 == c2 && c1 != 0); |
| 71 | return c1 - c2; |
| 72 | } |
| 73 | EXPORT_SYMBOL(strcasecmp); |
| 74 | #endif |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #ifndef __HAVE_ARCH_STRCPY |
| 77 | /** |
| 78 | * strcpy - Copy a %NUL terminated string |
| 79 | * @dest: Where to copy the string to |
| 80 | * @src: Where to copy the string from |
| 81 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 82 | #undef strcpy |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 83 | char *strcpy(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | char *tmp = dest; |
| 86 | |
| 87 | while ((*dest++ = *src++) != '\0') |
| 88 | /* nothing */; |
| 89 | return tmp; |
| 90 | } |
| 91 | EXPORT_SYMBOL(strcpy); |
| 92 | #endif |
| 93 | |
| 94 | #ifndef __HAVE_ARCH_STRNCPY |
| 95 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 96 | * strncpy - Copy a length-limited, C-string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * @dest: Where to copy the string to |
| 98 | * @src: Where to copy the string from |
| 99 | * @count: The maximum number of bytes to copy |
| 100 | * |
| 101 | * The result is not %NUL-terminated if the source exceeds |
| 102 | * @count bytes. |
walter harms | 2527952 | 2005-05-05 16:16:20 -0700 | [diff] [blame] | 103 | * |
| 104 | * In the case where the length of @src is less than that of |
| 105 | * count, the remainder of @dest will be padded with %NUL. |
| 106 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 108 | char *strncpy(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | char *tmp = dest; |
| 111 | |
| 112 | while (count) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 113 | if ((*tmp = *src) != 0) |
| 114 | src++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | tmp++; |
| 116 | count--; |
| 117 | } |
| 118 | return dest; |
| 119 | } |
| 120 | EXPORT_SYMBOL(strncpy); |
| 121 | #endif |
| 122 | |
| 123 | #ifndef __HAVE_ARCH_STRLCPY |
| 124 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 125 | * strlcpy - Copy a C-string into a sized buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | * @dest: Where to copy the string to |
| 127 | * @src: Where to copy the string from |
| 128 | * @size: size of destination buffer |
| 129 | * |
| 130 | * Compatible with *BSD: the result is always a valid |
| 131 | * NUL-terminated string that fits in the buffer (unless, |
| 132 | * of course, the buffer size is zero). It does not pad |
| 133 | * out the result like strncpy() does. |
| 134 | */ |
| 135 | size_t strlcpy(char *dest, const char *src, size_t size) |
| 136 | { |
| 137 | size_t ret = strlen(src); |
| 138 | |
| 139 | if (size) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 140 | size_t len = (ret >= size) ? size - 1 : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | memcpy(dest, src, len); |
| 142 | dest[len] = '\0'; |
| 143 | } |
| 144 | return ret; |
| 145 | } |
| 146 | EXPORT_SYMBOL(strlcpy); |
| 147 | #endif |
| 148 | |
| 149 | #ifndef __HAVE_ARCH_STRCAT |
| 150 | /** |
| 151 | * strcat - Append one %NUL-terminated string to another |
| 152 | * @dest: The string to be appended to |
| 153 | * @src: The string to append to it |
| 154 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 155 | #undef strcat |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 156 | char *strcat(char *dest, const char *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | char *tmp = dest; |
| 159 | |
| 160 | while (*dest) |
| 161 | dest++; |
| 162 | while ((*dest++ = *src++) != '\0') |
| 163 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return tmp; |
| 165 | } |
| 166 | EXPORT_SYMBOL(strcat); |
| 167 | #endif |
| 168 | |
| 169 | #ifndef __HAVE_ARCH_STRNCAT |
| 170 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 171 | * strncat - Append a length-limited, C-string to another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | * @dest: The string to be appended to |
| 173 | * @src: The string to append to it |
| 174 | * @count: The maximum numbers of bytes to copy |
| 175 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 176 | * Note that in contrast to strncpy(), strncat() ensures the result is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | * terminated. |
| 178 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 179 | char *strncat(char *dest, const char *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | char *tmp = dest; |
| 182 | |
| 183 | if (count) { |
| 184 | while (*dest) |
| 185 | dest++; |
| 186 | while ((*dest++ = *src++) != 0) { |
| 187 | if (--count == 0) { |
| 188 | *dest = '\0'; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return tmp; |
| 194 | } |
| 195 | EXPORT_SYMBOL(strncat); |
| 196 | #endif |
| 197 | |
| 198 | #ifndef __HAVE_ARCH_STRLCAT |
| 199 | /** |
Dan Carpenter | 0046dd9 | 2014-06-04 16:11:47 -0700 | [diff] [blame] | 200 | * strlcat - Append a length-limited, C-string to another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | * @dest: The string to be appended to |
| 202 | * @src: The string to append to it |
| 203 | * @count: The size of the destination buffer. |
| 204 | */ |
| 205 | size_t strlcat(char *dest, const char *src, size_t count) |
| 206 | { |
| 207 | size_t dsize = strlen(dest); |
| 208 | size_t len = strlen(src); |
| 209 | size_t res = dsize + len; |
| 210 | |
| 211 | /* This would be a bug */ |
| 212 | BUG_ON(dsize >= count); |
| 213 | |
| 214 | dest += dsize; |
| 215 | count -= dsize; |
| 216 | if (len >= count) |
| 217 | len = count-1; |
| 218 | memcpy(dest, src, len); |
| 219 | dest[len] = 0; |
| 220 | return res; |
| 221 | } |
| 222 | EXPORT_SYMBOL(strlcat); |
| 223 | #endif |
| 224 | |
| 225 | #ifndef __HAVE_ARCH_STRCMP |
| 226 | /** |
| 227 | * strcmp - Compare two strings |
| 228 | * @cs: One string |
| 229 | * @ct: Another string |
| 230 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 231 | #undef strcmp |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 232 | int strcmp(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 234 | unsigned char c1, c2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | while (1) { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 237 | c1 = *cs++; |
| 238 | c2 = *ct++; |
| 239 | if (c1 != c2) |
| 240 | return c1 < c2 ? -1 : 1; |
| 241 | if (!c1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | break; |
| 243 | } |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 244 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | EXPORT_SYMBOL(strcmp); |
| 247 | #endif |
| 248 | |
| 249 | #ifndef __HAVE_ARCH_STRNCMP |
| 250 | /** |
| 251 | * strncmp - Compare two length-limited strings |
| 252 | * @cs: One string |
| 253 | * @ct: Another string |
| 254 | * @count: The maximum number of bytes to compare |
| 255 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 256 | int strncmp(const char *cs, const char *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 258 | unsigned char c1, c2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | while (count) { |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 261 | c1 = *cs++; |
| 262 | c2 = *ct++; |
| 263 | if (c1 != c2) |
| 264 | return c1 < c2 ? -1 : 1; |
| 265 | if (!c1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | break; |
| 267 | count--; |
| 268 | } |
Linus Torvalds | a414f01 | 2009-11-18 22:31:52 +0100 | [diff] [blame] | 269 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | EXPORT_SYMBOL(strncmp); |
| 272 | #endif |
| 273 | |
| 274 | #ifndef __HAVE_ARCH_STRCHR |
| 275 | /** |
| 276 | * strchr - Find the first occurrence of a character in a string |
| 277 | * @s: The string to be searched |
| 278 | * @c: The character to search for |
| 279 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 280 | char *strchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 282 | for (; *s != (char)c; ++s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (*s == '\0') |
| 284 | return NULL; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 285 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | EXPORT_SYMBOL(strchr); |
| 288 | #endif |
| 289 | |
Grant Likely | 11d200e | 2014-03-14 17:00:14 +0000 | [diff] [blame] | 290 | #ifndef __HAVE_ARCH_STRCHRNUL |
| 291 | /** |
| 292 | * strchrnul - Find and return a character in a string, or end of string |
| 293 | * @s: The string to be searched |
| 294 | * @c: The character to search for |
| 295 | * |
| 296 | * Returns pointer to first occurrence of 'c' in s. If c is not found, then |
| 297 | * return a pointer to the null byte at the end of s. |
| 298 | */ |
| 299 | char *strchrnul(const char *s, int c) |
| 300 | { |
| 301 | while (*s && *s != (char)c) |
| 302 | s++; |
| 303 | return (char *)s; |
| 304 | } |
| 305 | EXPORT_SYMBOL(strchrnul); |
| 306 | #endif |
| 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | #ifndef __HAVE_ARCH_STRRCHR |
| 309 | /** |
| 310 | * strrchr - Find the last occurrence of a character in a string |
| 311 | * @s: The string to be searched |
| 312 | * @c: The character to search for |
| 313 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 314 | char *strrchr(const char *s, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Rasmus Villemoes | 8da53d4 | 2015-02-13 14:36:44 -0800 | [diff] [blame] | 316 | const char *last = NULL; |
| 317 | do { |
| 318 | if (*s == (char)c) |
| 319 | last = s; |
| 320 | } while (*s++); |
| 321 | return (char *)last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | EXPORT_SYMBOL(strrchr); |
| 324 | #endif |
| 325 | |
| 326 | #ifndef __HAVE_ARCH_STRNCHR |
| 327 | /** |
| 328 | * strnchr - Find a character in a length limited string |
| 329 | * @s: The string to be searched |
| 330 | * @count: The number of characters to be searched |
| 331 | * @c: The character to search for |
| 332 | */ |
| 333 | char *strnchr(const char *s, size_t count, int c) |
| 334 | { |
| 335 | for (; count-- && *s != '\0'; ++s) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 336 | if (*s == (char)c) |
| 337 | return (char *)s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | return NULL; |
| 339 | } |
| 340 | EXPORT_SYMBOL(strnchr); |
| 341 | #endif |
| 342 | |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 343 | /** |
Randy Dunlap | a6cd13f | 2009-12-21 14:37:22 -0800 | [diff] [blame] | 344 | * skip_spaces - Removes leading whitespace from @str. |
| 345 | * @str: The string to be stripped. |
André Goddard Rosa | f653398 | 2009-12-14 18:01:04 -0800 | [diff] [blame] | 346 | * |
Randy Dunlap | a6cd13f | 2009-12-21 14:37:22 -0800 | [diff] [blame] | 347 | * Returns a pointer to the first non-whitespace character in @str. |
André Goddard Rosa | f653398 | 2009-12-14 18:01:04 -0800 | [diff] [blame] | 348 | */ |
| 349 | char *skip_spaces(const char *str) |
| 350 | { |
| 351 | while (isspace(*str)) |
| 352 | ++str; |
| 353 | return (char *)str; |
| 354 | } |
| 355 | EXPORT_SYMBOL(skip_spaces); |
| 356 | |
| 357 | /** |
KOSAKI Motohiro | ca54cb8 | 2009-12-14 18:01:15 -0800 | [diff] [blame] | 358 | * strim - Removes leading and trailing whitespace from @s. |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 359 | * @s: The string to be stripped. |
| 360 | * |
| 361 | * Note that the first trailing whitespace is replaced with a %NUL-terminator |
| 362 | * in the given string @s. Returns a pointer to the first non-whitespace |
| 363 | * character in @s. |
| 364 | */ |
KOSAKI Motohiro | ca54cb8 | 2009-12-14 18:01:15 -0800 | [diff] [blame] | 365 | char *strim(char *s) |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 366 | { |
| 367 | size_t size; |
| 368 | char *end; |
| 369 | |
| 370 | size = strlen(s); |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 371 | if (!size) |
| 372 | return s; |
| 373 | |
| 374 | end = s + size - 1; |
Michael Holzheu | 6e6d9fa | 2006-10-28 10:38:47 -0700 | [diff] [blame] | 375 | while (end >= s && isspace(*end)) |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 376 | end--; |
| 377 | *(end + 1) = '\0'; |
| 378 | |
Michael Holzheu | 66f6958 | 2011-10-31 17:12:37 -0700 | [diff] [blame] | 379 | return skip_spaces(s); |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 380 | } |
KOSAKI Motohiro | ca54cb8 | 2009-12-14 18:01:15 -0800 | [diff] [blame] | 381 | EXPORT_SYMBOL(strim); |
Pekka Enberg | 481fad4 | 2006-06-23 02:05:44 -0700 | [diff] [blame] | 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | #ifndef __HAVE_ARCH_STRLEN |
| 384 | /** |
| 385 | * strlen - Find the length of a string |
| 386 | * @s: The string to be sized |
| 387 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 388 | size_t strlen(const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
| 390 | const char *sc; |
| 391 | |
| 392 | for (sc = s; *sc != '\0'; ++sc) |
| 393 | /* nothing */; |
| 394 | return sc - s; |
| 395 | } |
| 396 | EXPORT_SYMBOL(strlen); |
| 397 | #endif |
| 398 | |
| 399 | #ifndef __HAVE_ARCH_STRNLEN |
| 400 | /** |
| 401 | * strnlen - Find the length of a length-limited string |
| 402 | * @s: The string to be sized |
| 403 | * @count: The maximum number of bytes to search |
| 404 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 405 | size_t strnlen(const char *s, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | const char *sc; |
| 408 | |
| 409 | for (sc = s; count-- && *sc != '\0'; ++sc) |
| 410 | /* nothing */; |
| 411 | return sc - s; |
| 412 | } |
| 413 | EXPORT_SYMBOL(strnlen); |
| 414 | #endif |
| 415 | |
| 416 | #ifndef __HAVE_ARCH_STRSPN |
| 417 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 418 | * 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] | 419 | * @s: The string to be searched |
| 420 | * @accept: The string to search for |
| 421 | */ |
| 422 | size_t strspn(const char *s, const char *accept) |
| 423 | { |
| 424 | const char *p; |
| 425 | const char *a; |
| 426 | size_t count = 0; |
| 427 | |
| 428 | for (p = s; *p != '\0'; ++p) { |
| 429 | for (a = accept; *a != '\0'; ++a) { |
| 430 | if (*p == *a) |
| 431 | break; |
| 432 | } |
| 433 | if (*a == '\0') |
| 434 | return count; |
| 435 | ++count; |
| 436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | return count; |
| 438 | } |
| 439 | |
| 440 | EXPORT_SYMBOL(strspn); |
| 441 | #endif |
| 442 | |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 443 | #ifndef __HAVE_ARCH_STRCSPN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 445 | * 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] | 446 | * @s: The string to be searched |
| 447 | * @reject: The string to avoid |
| 448 | */ |
| 449 | size_t strcspn(const char *s, const char *reject) |
| 450 | { |
| 451 | const char *p; |
| 452 | const char *r; |
| 453 | size_t count = 0; |
| 454 | |
| 455 | for (p = s; *p != '\0'; ++p) { |
| 456 | for (r = reject; *r != '\0'; ++r) { |
| 457 | if (*p == *r) |
| 458 | return count; |
| 459 | } |
| 460 | ++count; |
| 461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return count; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | EXPORT_SYMBOL(strcspn); |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 465 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | #ifndef __HAVE_ARCH_STRPBRK |
| 468 | /** |
| 469 | * strpbrk - Find the first occurrence of a set of characters |
| 470 | * @cs: The string to be searched |
| 471 | * @ct: The characters to search for |
| 472 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 473 | char *strpbrk(const char *cs, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 475 | const char *sc1, *sc2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 477 | for (sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 478 | for (sc2 = ct; *sc2 != '\0'; ++sc2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (*sc1 == *sc2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 480 | return (char *)sc1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | return NULL; |
| 484 | } |
Kyle McMartin | 894b577 | 2006-04-10 22:53:56 -0700 | [diff] [blame] | 485 | EXPORT_SYMBOL(strpbrk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | #endif |
| 487 | |
| 488 | #ifndef __HAVE_ARCH_STRSEP |
| 489 | /** |
| 490 | * strsep - Split a string into tokens |
| 491 | * @s: The string to be searched |
| 492 | * @ct: The characters to search for |
| 493 | * |
| 494 | * strsep() updates @s to point after the token, ready for the next call. |
| 495 | * |
| 496 | * It returns empty tokens, too, behaving exactly like the libc function |
| 497 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
| 498 | * Same semantics, slimmer shape. ;) |
| 499 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 500 | char *strsep(char **s, const char *ct) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 502 | char *sbegin = *s; |
| 503 | char *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | if (sbegin == NULL) |
| 506 | return NULL; |
| 507 | |
| 508 | end = strpbrk(sbegin, ct); |
| 509 | if (end) |
| 510 | *end++ = '\0'; |
| 511 | *s = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | return sbegin; |
| 513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | EXPORT_SYMBOL(strsep); |
| 515 | #endif |
| 516 | |
David Brownell | 34990cf | 2008-05-01 04:34:42 -0700 | [diff] [blame] | 517 | /** |
| 518 | * sysfs_streq - return true if strings are equal, modulo trailing newline |
| 519 | * @s1: one string |
| 520 | * @s2: another string |
| 521 | * |
| 522 | * This routine returns true iff two strings are equal, treating both |
| 523 | * NUL and newline-then-NUL as equivalent string terminations. It's |
| 524 | * geared for use with sysfs input strings, which generally terminate |
| 525 | * with newlines but are compared against values without newlines. |
| 526 | */ |
| 527 | bool sysfs_streq(const char *s1, const char *s2) |
| 528 | { |
| 529 | while (*s1 && *s1 == *s2) { |
| 530 | s1++; |
| 531 | s2++; |
| 532 | } |
| 533 | |
| 534 | if (*s1 == *s2) |
| 535 | return true; |
| 536 | if (!*s1 && *s2 == '\n' && !s2[1]) |
| 537 | return true; |
| 538 | if (*s1 == '\n' && !s1[1] && !*s2) |
| 539 | return true; |
| 540 | return false; |
| 541 | } |
| 542 | EXPORT_SYMBOL(sysfs_streq); |
| 543 | |
Jonathan Cameron | d0f1fed | 2011-04-19 12:43:45 +0100 | [diff] [blame] | 544 | /** |
| 545 | * strtobool - convert common user inputs into boolean values |
| 546 | * @s: input string |
| 547 | * @res: result |
| 548 | * |
| 549 | * This routine returns 0 iff the first character is one of 'Yy1Nn0'. |
| 550 | * Otherwise it will return -EINVAL. Value pointed to by res is |
| 551 | * updated upon finding a match. |
| 552 | */ |
| 553 | int strtobool(const char *s, bool *res) |
| 554 | { |
| 555 | switch (s[0]) { |
| 556 | case 'y': |
| 557 | case 'Y': |
| 558 | case '1': |
| 559 | *res = true; |
| 560 | break; |
| 561 | case 'n': |
| 562 | case 'N': |
| 563 | case '0': |
| 564 | *res = false; |
| 565 | break; |
| 566 | default: |
| 567 | return -EINVAL; |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | EXPORT_SYMBOL(strtobool); |
| 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | #ifndef __HAVE_ARCH_MEMSET |
| 574 | /** |
| 575 | * memset - Fill a region of memory with the given value |
| 576 | * @s: Pointer to the start of the area. |
| 577 | * @c: The byte to fill the area with |
| 578 | * @count: The size of the area. |
| 579 | * |
| 580 | * Do not use memset() to access IO space, use memset_io() instead. |
| 581 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 582 | void *memset(void *s, int c, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 584 | char *xs = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | while (count--) |
| 587 | *xs++ = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | return s; |
| 589 | } |
| 590 | EXPORT_SYMBOL(memset); |
| 591 | #endif |
| 592 | |
Daniel Borkmann | d4c5efd | 2014-08-26 23:16:35 -0400 | [diff] [blame] | 593 | /** |
| 594 | * memzero_explicit - Fill a region of memory (e.g. sensitive |
| 595 | * keying data) with 0s. |
| 596 | * @s: Pointer to the start of the area. |
| 597 | * @count: The size of the area. |
| 598 | * |
| 599 | * memzero_explicit() doesn't need an arch-specific version as |
| 600 | * it just invokes the one of memset() implicitly. |
| 601 | */ |
| 602 | void memzero_explicit(void *s, size_t count) |
| 603 | { |
| 604 | memset(s, 0, count); |
| 605 | OPTIMIZER_HIDE_VAR(s); |
| 606 | } |
| 607 | EXPORT_SYMBOL(memzero_explicit); |
| 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | #ifndef __HAVE_ARCH_MEMCPY |
| 610 | /** |
| 611 | * memcpy - Copy one area of memory to another |
| 612 | * @dest: Where to copy to |
| 613 | * @src: Where to copy from |
| 614 | * @count: The size of the area. |
| 615 | * |
| 616 | * You should not use this function to access IO space, use memcpy_toio() |
| 617 | * or memcpy_fromio() instead. |
| 618 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 619 | void *memcpy(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 621 | char *tmp = dest; |
Jan-Benedict Glaw | 4c416ab | 2006-04-10 22:54:09 -0700 | [diff] [blame] | 622 | const char *s = src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | while (count--) |
| 625 | *tmp++ = *s++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | return dest; |
| 627 | } |
| 628 | EXPORT_SYMBOL(memcpy); |
| 629 | #endif |
| 630 | |
| 631 | #ifndef __HAVE_ARCH_MEMMOVE |
| 632 | /** |
| 633 | * memmove - Copy one area of memory to another |
| 634 | * @dest: Where to copy to |
| 635 | * @src: Where to copy from |
| 636 | * @count: The size of the area. |
| 637 | * |
| 638 | * Unlike memcpy(), memmove() copes with overlapping areas. |
| 639 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 640 | void *memmove(void *dest, const void *src, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
Paul Jackson | 82da2c3 | 2005-10-30 15:03:19 -0800 | [diff] [blame] | 642 | char *tmp; |
| 643 | const char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
| 645 | if (dest <= src) { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 646 | tmp = dest; |
| 647 | s = src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | while (count--) |
| 649 | *tmp++ = *s++; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 650 | } else { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 651 | tmp = dest; |
| 652 | tmp += count; |
| 653 | s = src; |
| 654 | s += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | while (count--) |
| 656 | *--tmp = *--s; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return dest; |
| 659 | } |
| 660 | EXPORT_SYMBOL(memmove); |
| 661 | #endif |
| 662 | |
| 663 | #ifndef __HAVE_ARCH_MEMCMP |
| 664 | /** |
| 665 | * memcmp - Compare two areas of memory |
| 666 | * @cs: One area of memory |
| 667 | * @ct: Another area of memory |
| 668 | * @count: The size of the area. |
| 669 | */ |
Paolo 'Blaisorblade' Giarrusso | 0c28130 | 2005-05-05 16:15:17 -0700 | [diff] [blame] | 670 | #undef memcmp |
Andi Kleen | a7330c9 | 2014-02-08 08:52:06 +0100 | [diff] [blame] | 671 | __visible int memcmp(const void *cs, const void *ct, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
| 673 | const unsigned char *su1, *su2; |
| 674 | int res = 0; |
| 675 | |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 676 | for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | if ((res = *su1 - *su2) != 0) |
| 678 | break; |
| 679 | return res; |
| 680 | } |
| 681 | EXPORT_SYMBOL(memcmp); |
| 682 | #endif |
| 683 | |
| 684 | #ifndef __HAVE_ARCH_MEMSCAN |
| 685 | /** |
| 686 | * memscan - Find a character in an area of memory. |
| 687 | * @addr: The memory area |
| 688 | * @c: The byte to search for |
| 689 | * @size: The size of the area. |
| 690 | * |
| 691 | * returns the address of the first occurrence of @c, or 1 byte past |
| 692 | * the area if @c is not found |
| 693 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 694 | void *memscan(void *addr, int c, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
Jesper Juhl | 850b924 | 2005-10-30 15:02:13 -0800 | [diff] [blame] | 696 | unsigned char *p = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
| 698 | while (size) { |
| 699 | if (*p == c) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 700 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | p++; |
| 702 | size--; |
| 703 | } |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 704 | return (void *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | EXPORT_SYMBOL(memscan); |
| 707 | #endif |
| 708 | |
| 709 | #ifndef __HAVE_ARCH_STRSTR |
| 710 | /** |
| 711 | * strstr - Find the first substring in a %NUL terminated string |
| 712 | * @s1: The string to be searched |
| 713 | * @s2: The string to search for |
| 714 | */ |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 715 | char *strstr(const char *s1, const char *s2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | { |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 717 | size_t l1, l2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | l2 = strlen(s2); |
| 720 | if (!l2) |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 721 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | l1 = strlen(s1); |
| 723 | while (l1 >= l2) { |
| 724 | l1--; |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 725 | if (!memcmp(s1, s2, l2)) |
| 726 | return (char *)s1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | s1++; |
| 728 | } |
| 729 | return NULL; |
| 730 | } |
| 731 | EXPORT_SYMBOL(strstr); |
| 732 | #endif |
| 733 | |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 734 | #ifndef __HAVE_ARCH_STRNSTR |
| 735 | /** |
| 736 | * strnstr - Find the first substring in a length-limited string |
| 737 | * @s1: The string to be searched |
| 738 | * @s2: The string to search for |
| 739 | * @len: the maximum number of characters to search |
| 740 | */ |
| 741 | char *strnstr(const char *s1, const char *s2, size_t len) |
| 742 | { |
André Goddard Rosa | d6a2eed | 2010-03-05 13:43:12 -0800 | [diff] [blame] | 743 | size_t l2; |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 744 | |
| 745 | l2 = strlen(s2); |
| 746 | if (!l2) |
| 747 | return (char *)s1; |
André Goddard Rosa | d6a2eed | 2010-03-05 13:43:12 -0800 | [diff] [blame] | 748 | while (len >= l2) { |
| 749 | len--; |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 750 | if (!memcmp(s1, s2, l2)) |
| 751 | return (char *)s1; |
| 752 | s1++; |
| 753 | } |
| 754 | return NULL; |
| 755 | } |
| 756 | EXPORT_SYMBOL(strnstr); |
| 757 | #endif |
| 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | #ifndef __HAVE_ARCH_MEMCHR |
| 760 | /** |
| 761 | * memchr - Find a character in an area of memory. |
| 762 | * @s: The memory area |
| 763 | * @c: The byte to search for |
| 764 | * @n: The size of the area. |
| 765 | * |
| 766 | * returns the address of the first occurrence of @c, or %NULL |
| 767 | * if @c is not found |
| 768 | */ |
| 769 | void *memchr(const void *s, int c, size_t n) |
| 770 | { |
| 771 | const unsigned char *p = s; |
| 772 | while (n-- != 0) { |
| 773 | if ((unsigned char)c == *p++) { |
Jesper Juhl | 51a0f0f | 2005-10-30 15:02:11 -0800 | [diff] [blame] | 774 | return (void *)(p - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | return NULL; |
| 778 | } |
| 779 | EXPORT_SYMBOL(memchr); |
| 780 | #endif |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 781 | |
| 782 | static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) |
| 783 | { |
| 784 | while (bytes) { |
| 785 | if (*start != value) |
| 786 | return (void *)start; |
| 787 | start++; |
| 788 | bytes--; |
| 789 | } |
| 790 | return NULL; |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * memchr_inv - Find an unmatching character in an area of memory. |
| 795 | * @start: The memory area |
| 796 | * @c: Find a character other than c |
| 797 | * @bytes: The size of the area. |
| 798 | * |
| 799 | * returns the address of the first character other than @c, or %NULL |
| 800 | * if the whole buffer contains just @c. |
| 801 | */ |
| 802 | void *memchr_inv(const void *start, int c, size_t bytes) |
| 803 | { |
| 804 | u8 value = c; |
| 805 | u64 value64; |
| 806 | unsigned int words, prefix; |
| 807 | |
| 808 | if (bytes <= 16) |
| 809 | return check_bytes8(start, value, bytes); |
| 810 | |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 811 | value64 = value; |
Linus Torvalds | 72d9310 | 2014-09-13 11:14:53 -0700 | [diff] [blame] | 812 | #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64 |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 813 | value64 *= 0x0101010101010101; |
Linus Torvalds | 72d9310 | 2014-09-13 11:14:53 -0700 | [diff] [blame] | 814 | #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 815 | value64 *= 0x01010101; |
| 816 | value64 |= value64 << 32; |
| 817 | #else |
| 818 | value64 |= value64 << 8; |
| 819 | value64 |= value64 << 16; |
| 820 | value64 |= value64 << 32; |
| 821 | #endif |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 822 | |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 823 | prefix = (unsigned long)start % 8; |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 824 | if (prefix) { |
Akinobu Mita | f43804b | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 825 | u8 *r; |
| 826 | |
| 827 | prefix = 8 - prefix; |
| 828 | r = check_bytes8(start, value, prefix); |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 829 | if (r) |
| 830 | return r; |
| 831 | start += prefix; |
| 832 | bytes -= prefix; |
| 833 | } |
| 834 | |
| 835 | words = bytes / 8; |
| 836 | |
| 837 | while (words) { |
| 838 | if (*(u64 *)start != value64) |
| 839 | return check_bytes8(start, value, 8); |
| 840 | start += 8; |
| 841 | words--; |
| 842 | } |
| 843 | |
| 844 | return check_bytes8(start, value, bytes % 8); |
| 845 | } |
| 846 | EXPORT_SYMBOL(memchr_inv); |