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