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