Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/lib/string.c
0003  *
0004  *  Copyright (C) 1991, 1992  Linus Torvalds
0005  */
0006 
0007 /*
0008  * stupid library routines.. The optimized versions should generally be found
0009  * as inline code in <asm-xx/string.h>
0010  *
0011  * These are buggy as well..
0012  *
0013  * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
0014  * -  Added strsep() which will replace strtok() soon (because strsep() is
0015  *    reentrant and should be faster). Use only strsep() in new code, please.
0016  *
0017  * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
0018  *                    Matthew Hawkins <matt@mh.dropbear.id.au>
0019  * -  Kissed strtok() goodbye
0020  */
0021 
0022 #include <linux/types.h>
0023 #include <linux/string.h>
0024 #include <linux/ctype.h>
0025 #include <linux/kernel.h>
0026 #include <linux/export.h>
0027 #include <linux/bug.h>
0028 #include <linux/errno.h>
0029 
0030 #include <asm/byteorder.h>
0031 #include <asm/word-at-a-time.h>
0032 #include <asm/page.h>
0033 
0034 #ifndef __HAVE_ARCH_STRNCASECMP
0035 /**
0036  * strncasecmp - Case insensitive, length-limited string comparison
0037  * @s1: One string
0038  * @s2: The other string
0039  * @len: the maximum number of characters to compare
0040  */
0041 int strncasecmp(const char *s1, const char *s2, size_t len)
0042 {
0043     /* Yes, Virginia, it had better be unsigned */
0044     unsigned char c1, c2;
0045 
0046     if (!len)
0047         return 0;
0048 
0049     do {
0050         c1 = *s1++;
0051         c2 = *s2++;
0052         if (!c1 || !c2)
0053             break;
0054         if (c1 == c2)
0055             continue;
0056         c1 = tolower(c1);
0057         c2 = tolower(c2);
0058         if (c1 != c2)
0059             break;
0060     } while (--len);
0061     return (int)c1 - (int)c2;
0062 }
0063 EXPORT_SYMBOL(strncasecmp);
0064 #endif
0065 
0066 #ifndef __HAVE_ARCH_STRCASECMP
0067 int strcasecmp(const char *s1, const char *s2)
0068 {
0069     int c1, c2;
0070 
0071     do {
0072         c1 = tolower(*s1++);
0073         c2 = tolower(*s2++);
0074     } while (c1 == c2 && c1 != 0);
0075     return c1 - c2;
0076 }
0077 EXPORT_SYMBOL(strcasecmp);
0078 #endif
0079 
0080 #ifndef __HAVE_ARCH_STRCPY
0081 /**
0082  * strcpy - Copy a %NUL terminated string
0083  * @dest: Where to copy the string to
0084  * @src: Where to copy the string from
0085  */
0086 #undef strcpy
0087 char *strcpy(char *dest, const char *src)
0088 {
0089     char *tmp = dest;
0090 
0091     while ((*dest++ = *src++) != '\0')
0092         /* nothing */;
0093     return tmp;
0094 }
0095 EXPORT_SYMBOL(strcpy);
0096 #endif
0097 
0098 #ifndef __HAVE_ARCH_STRNCPY
0099 /**
0100  * strncpy - Copy a length-limited, C-string
0101  * @dest: Where to copy the string to
0102  * @src: Where to copy the string from
0103  * @count: The maximum number of bytes to copy
0104  *
0105  * The result is not %NUL-terminated if the source exceeds
0106  * @count bytes.
0107  *
0108  * In the case where the length of @src is less than  that  of
0109  * count, the remainder of @dest will be padded with %NUL.
0110  *
0111  */
0112 char *strncpy(char *dest, const char *src, size_t count)
0113 {
0114     char *tmp = dest;
0115 
0116     while (count) {
0117         if ((*tmp = *src) != 0)
0118             src++;
0119         tmp++;
0120         count--;
0121     }
0122     return dest;
0123 }
0124 EXPORT_SYMBOL(strncpy);
0125 #endif
0126 
0127 #ifndef __HAVE_ARCH_STRLCPY
0128 /**
0129  * strlcpy - Copy a C-string into a sized buffer
0130  * @dest: Where to copy the string to
0131  * @src: Where to copy the string from
0132  * @size: size of destination buffer
0133  *
0134  * Compatible with *BSD: the result is always a valid
0135  * NUL-terminated string that fits in the buffer (unless,
0136  * of course, the buffer size is zero). It does not pad
0137  * out the result like strncpy() does.
0138  */
0139 size_t strlcpy(char *dest, const char *src, size_t size)
0140 {
0141     size_t ret = strlen(src);
0142 
0143     if (size) {
0144         size_t len = (ret >= size) ? size - 1 : ret;
0145         memcpy(dest, src, len);
0146         dest[len] = '\0';
0147     }
0148     return ret;
0149 }
0150 EXPORT_SYMBOL(strlcpy);
0151 #endif
0152 
0153 #ifndef __HAVE_ARCH_STRSCPY
0154 /**
0155  * strscpy - Copy a C-string into a sized buffer
0156  * @dest: Where to copy the string to
0157  * @src: Where to copy the string from
0158  * @count: Size of destination buffer
0159  *
0160  * Copy the string, or as much of it as fits, into the dest buffer.
0161  * The routine returns the number of characters copied (not including
0162  * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough.
0163  * The behavior is undefined if the string buffers overlap.
0164  * The destination buffer is always NUL terminated, unless it's zero-sized.
0165  *
0166  * Preferred to strlcpy() since the API doesn't require reading memory
0167  * from the src string beyond the specified "count" bytes, and since
0168  * the return value is easier to error-check than strlcpy()'s.
0169  * In addition, the implementation is robust to the string changing out
0170  * from underneath it, unlike the current strlcpy() implementation.
0171  *
0172  * Preferred to strncpy() since it always returns a valid string, and
0173  * doesn't unnecessarily force the tail of the destination buffer to be
0174  * zeroed.  If the zeroing is desired, it's likely cleaner to use strscpy()
0175  * with an overflow test, then just memset() the tail of the dest buffer.
0176  */
0177 ssize_t strscpy(char *dest, const char *src, size_t count)
0178 {
0179     const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
0180     size_t max = count;
0181     long res = 0;
0182 
0183     if (count == 0)
0184         return -E2BIG;
0185 
0186 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
0187     /*
0188      * If src is unaligned, don't cross a page boundary,
0189      * since we don't know if the next page is mapped.
0190      */
0191     if ((long)src & (sizeof(long) - 1)) {
0192         size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
0193         if (limit < max)
0194             max = limit;
0195     }
0196 #else
0197     /* If src or dest is unaligned, don't do word-at-a-time. */
0198     if (((long) dest | (long) src) & (sizeof(long) - 1))
0199         max = 0;
0200 #endif
0201 
0202     while (max >= sizeof(unsigned long)) {
0203         unsigned long c, data;
0204 
0205         c = *(unsigned long *)(src+res);
0206         if (has_zero(c, &data, &constants)) {
0207             data = prep_zero_mask(c, data, &constants);
0208             data = create_zero_mask(data);
0209             *(unsigned long *)(dest+res) = c & zero_bytemask(data);
0210             return res + find_zero(data);
0211         }
0212         *(unsigned long *)(dest+res) = c;
0213         res += sizeof(unsigned long);
0214         count -= sizeof(unsigned long);
0215         max -= sizeof(unsigned long);
0216     }
0217 
0218     while (count) {
0219         char c;
0220 
0221         c = src[res];
0222         dest[res] = c;
0223         if (!c)
0224             return res;
0225         res++;
0226         count--;
0227     }
0228 
0229     /* Hit buffer length without finding a NUL; force NUL-termination. */
0230     if (res)
0231         dest[res-1] = '\0';
0232 
0233     return -E2BIG;
0234 }
0235 EXPORT_SYMBOL(strscpy);
0236 #endif
0237 
0238 #ifndef __HAVE_ARCH_STRCAT
0239 /**
0240  * strcat - Append one %NUL-terminated string to another
0241  * @dest: The string to be appended to
0242  * @src: The string to append to it
0243  */
0244 #undef strcat
0245 char *strcat(char *dest, const char *src)
0246 {
0247     char *tmp = dest;
0248 
0249     while (*dest)
0250         dest++;
0251     while ((*dest++ = *src++) != '\0')
0252         ;
0253     return tmp;
0254 }
0255 EXPORT_SYMBOL(strcat);
0256 #endif
0257 
0258 #ifndef __HAVE_ARCH_STRNCAT
0259 /**
0260  * strncat - Append a length-limited, C-string to another
0261  * @dest: The string to be appended to
0262  * @src: The string to append to it
0263  * @count: The maximum numbers of bytes to copy
0264  *
0265  * Note that in contrast to strncpy(), strncat() ensures the result is
0266  * terminated.
0267  */
0268 char *strncat(char *dest, const char *src, size_t count)
0269 {
0270     char *tmp = dest;
0271 
0272     if (count) {
0273         while (*dest)
0274             dest++;
0275         while ((*dest++ = *src++) != 0) {
0276             if (--count == 0) {
0277                 *dest = '\0';
0278                 break;
0279             }
0280         }
0281     }
0282     return tmp;
0283 }
0284 EXPORT_SYMBOL(strncat);
0285 #endif
0286 
0287 #ifndef __HAVE_ARCH_STRLCAT
0288 /**
0289  * strlcat - Append a length-limited, C-string to another
0290  * @dest: The string to be appended to
0291  * @src: The string to append to it
0292  * @count: The size of the destination buffer.
0293  */
0294 size_t strlcat(char *dest, const char *src, size_t count)
0295 {
0296     size_t dsize = strlen(dest);
0297     size_t len = strlen(src);
0298     size_t res = dsize + len;
0299 
0300     /* This would be a bug */
0301     BUG_ON(dsize >= count);
0302 
0303     dest += dsize;
0304     count -= dsize;
0305     if (len >= count)
0306         len = count-1;
0307     memcpy(dest, src, len);
0308     dest[len] = 0;
0309     return res;
0310 }
0311 EXPORT_SYMBOL(strlcat);
0312 #endif
0313 
0314 #ifndef __HAVE_ARCH_STRCMP
0315 /**
0316  * strcmp - Compare two strings
0317  * @cs: One string
0318  * @ct: Another string
0319  */
0320 #undef strcmp
0321 int strcmp(const char *cs, const char *ct)
0322 {
0323     unsigned char c1, c2;
0324 
0325     while (1) {
0326         c1 = *cs++;
0327         c2 = *ct++;
0328         if (c1 != c2)
0329             return c1 < c2 ? -1 : 1;
0330         if (!c1)
0331             break;
0332     }
0333     return 0;
0334 }
0335 EXPORT_SYMBOL(strcmp);
0336 #endif
0337 
0338 #ifndef __HAVE_ARCH_STRNCMP
0339 /**
0340  * strncmp - Compare two length-limited strings
0341  * @cs: One string
0342  * @ct: Another string
0343  * @count: The maximum number of bytes to compare
0344  */
0345 int strncmp(const char *cs, const char *ct, size_t count)
0346 {
0347     unsigned char c1, c2;
0348 
0349     while (count) {
0350         c1 = *cs++;
0351         c2 = *ct++;
0352         if (c1 != c2)
0353             return c1 < c2 ? -1 : 1;
0354         if (!c1)
0355             break;
0356         count--;
0357     }
0358     return 0;
0359 }
0360 EXPORT_SYMBOL(strncmp);
0361 #endif
0362 
0363 #ifndef __HAVE_ARCH_STRCHR
0364 /**
0365  * strchr - Find the first occurrence of a character in a string
0366  * @s: The string to be searched
0367  * @c: The character to search for
0368  */
0369 char *strchr(const char *s, int c)
0370 {
0371     for (; *s != (char)c; ++s)
0372         if (*s == '\0')
0373             return NULL;
0374     return (char *)s;
0375 }
0376 EXPORT_SYMBOL(strchr);
0377 #endif
0378 
0379 #ifndef __HAVE_ARCH_STRCHRNUL
0380 /**
0381  * strchrnul - Find and return a character in a string, or end of string
0382  * @s: The string to be searched
0383  * @c: The character to search for
0384  *
0385  * Returns pointer to first occurrence of 'c' in s. If c is not found, then
0386  * return a pointer to the null byte at the end of s.
0387  */
0388 char *strchrnul(const char *s, int c)
0389 {
0390     while (*s && *s != (char)c)
0391         s++;
0392     return (char *)s;
0393 }
0394 EXPORT_SYMBOL(strchrnul);
0395 #endif
0396 
0397 #ifndef __HAVE_ARCH_STRRCHR
0398 /**
0399  * strrchr - Find the last occurrence of a character in a string
0400  * @s: The string to be searched
0401  * @c: The character to search for
0402  */
0403 char *strrchr(const char *s, int c)
0404 {
0405     const char *last = NULL;
0406     do {
0407         if (*s == (char)c)
0408             last = s;
0409     } while (*s++);
0410     return (char *)last;
0411 }
0412 EXPORT_SYMBOL(strrchr);
0413 #endif
0414 
0415 #ifndef __HAVE_ARCH_STRNCHR
0416 /**
0417  * strnchr - Find a character in a length limited string
0418  * @s: The string to be searched
0419  * @count: The number of characters to be searched
0420  * @c: The character to search for
0421  */
0422 char *strnchr(const char *s, size_t count, int c)
0423 {
0424     for (; count-- && *s != '\0'; ++s)
0425         if (*s == (char)c)
0426             return (char *)s;
0427     return NULL;
0428 }
0429 EXPORT_SYMBOL(strnchr);
0430 #endif
0431 
0432 /**
0433  * skip_spaces - Removes leading whitespace from @str.
0434  * @str: The string to be stripped.
0435  *
0436  * Returns a pointer to the first non-whitespace character in @str.
0437  */
0438 char *skip_spaces(const char *str)
0439 {
0440     while (isspace(*str))
0441         ++str;
0442     return (char *)str;
0443 }
0444 EXPORT_SYMBOL(skip_spaces);
0445 
0446 /**
0447  * strim - Removes leading and trailing whitespace from @s.
0448  * @s: The string to be stripped.
0449  *
0450  * Note that the first trailing whitespace is replaced with a %NUL-terminator
0451  * in the given string @s. Returns a pointer to the first non-whitespace
0452  * character in @s.
0453  */
0454 char *strim(char *s)
0455 {
0456     size_t size;
0457     char *end;
0458 
0459     size = strlen(s);
0460     if (!size)
0461         return s;
0462 
0463     end = s + size - 1;
0464     while (end >= s && isspace(*end))
0465         end--;
0466     *(end + 1) = '\0';
0467 
0468     return skip_spaces(s);
0469 }
0470 EXPORT_SYMBOL(strim);
0471 
0472 #ifndef __HAVE_ARCH_STRLEN
0473 /**
0474  * strlen - Find the length of a string
0475  * @s: The string to be sized
0476  */
0477 size_t strlen(const char *s)
0478 {
0479     const char *sc;
0480 
0481     for (sc = s; *sc != '\0'; ++sc)
0482         /* nothing */;
0483     return sc - s;
0484 }
0485 EXPORT_SYMBOL(strlen);
0486 #endif
0487 
0488 #ifndef __HAVE_ARCH_STRNLEN
0489 /**
0490  * strnlen - Find the length of a length-limited string
0491  * @s: The string to be sized
0492  * @count: The maximum number of bytes to search
0493  */
0494 size_t strnlen(const char *s, size_t count)
0495 {
0496     const char *sc;
0497 
0498     for (sc = s; count-- && *sc != '\0'; ++sc)
0499         /* nothing */;
0500     return sc - s;
0501 }
0502 EXPORT_SYMBOL(strnlen);
0503 #endif
0504 
0505 #ifndef __HAVE_ARCH_STRSPN
0506 /**
0507  * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
0508  * @s: The string to be searched
0509  * @accept: The string to search for
0510  */
0511 size_t strspn(const char *s, const char *accept)
0512 {
0513     const char *p;
0514     const char *a;
0515     size_t count = 0;
0516 
0517     for (p = s; *p != '\0'; ++p) {
0518         for (a = accept; *a != '\0'; ++a) {
0519             if (*p == *a)
0520                 break;
0521         }
0522         if (*a == '\0')
0523             return count;
0524         ++count;
0525     }
0526     return count;
0527 }
0528 
0529 EXPORT_SYMBOL(strspn);
0530 #endif
0531 
0532 #ifndef __HAVE_ARCH_STRCSPN
0533 /**
0534  * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
0535  * @s: The string to be searched
0536  * @reject: The string to avoid
0537  */
0538 size_t strcspn(const char *s, const char *reject)
0539 {
0540     const char *p;
0541     const char *r;
0542     size_t count = 0;
0543 
0544     for (p = s; *p != '\0'; ++p) {
0545         for (r = reject; *r != '\0'; ++r) {
0546             if (*p == *r)
0547                 return count;
0548         }
0549         ++count;
0550     }
0551     return count;
0552 }
0553 EXPORT_SYMBOL(strcspn);
0554 #endif
0555 
0556 #ifndef __HAVE_ARCH_STRPBRK
0557 /**
0558  * strpbrk - Find the first occurrence of a set of characters
0559  * @cs: The string to be searched
0560  * @ct: The characters to search for
0561  */
0562 char *strpbrk(const char *cs, const char *ct)
0563 {
0564     const char *sc1, *sc2;
0565 
0566     for (sc1 = cs; *sc1 != '\0'; ++sc1) {
0567         for (sc2 = ct; *sc2 != '\0'; ++sc2) {
0568             if (*sc1 == *sc2)
0569                 return (char *)sc1;
0570         }
0571     }
0572     return NULL;
0573 }
0574 EXPORT_SYMBOL(strpbrk);
0575 #endif
0576 
0577 #ifndef __HAVE_ARCH_STRSEP
0578 /**
0579  * strsep - Split a string into tokens
0580  * @s: The string to be searched
0581  * @ct: The characters to search for
0582  *
0583  * strsep() updates @s to point after the token, ready for the next call.
0584  *
0585  * It returns empty tokens, too, behaving exactly like the libc function
0586  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
0587  * Same semantics, slimmer shape. ;)
0588  */
0589 char *strsep(char **s, const char *ct)
0590 {
0591     char *sbegin = *s;
0592     char *end;
0593 
0594     if (sbegin == NULL)
0595         return NULL;
0596 
0597     end = strpbrk(sbegin, ct);
0598     if (end)
0599         *end++ = '\0';
0600     *s = end;
0601     return sbegin;
0602 }
0603 EXPORT_SYMBOL(strsep);
0604 #endif
0605 
0606 /**
0607  * sysfs_streq - return true if strings are equal, modulo trailing newline
0608  * @s1: one string
0609  * @s2: another string
0610  *
0611  * This routine returns true iff two strings are equal, treating both
0612  * NUL and newline-then-NUL as equivalent string terminations.  It's
0613  * geared for use with sysfs input strings, which generally terminate
0614  * with newlines but are compared against values without newlines.
0615  */
0616 bool sysfs_streq(const char *s1, const char *s2)
0617 {
0618     while (*s1 && *s1 == *s2) {
0619         s1++;
0620         s2++;
0621     }
0622 
0623     if (*s1 == *s2)
0624         return true;
0625     if (!*s1 && *s2 == '\n' && !s2[1])
0626         return true;
0627     if (*s1 == '\n' && !s1[1] && !*s2)
0628         return true;
0629     return false;
0630 }
0631 EXPORT_SYMBOL(sysfs_streq);
0632 
0633 /**
0634  * match_string - matches given string in an array
0635  * @array:  array of strings
0636  * @n:      number of strings in the array or -1 for NULL terminated arrays
0637  * @string: string to match with
0638  *
0639  * Return:
0640  * index of a @string in the @array if matches, or %-EINVAL otherwise.
0641  */
0642 int match_string(const char * const *array, size_t n, const char *string)
0643 {
0644     int index;
0645     const char *item;
0646 
0647     for (index = 0; index < n; index++) {
0648         item = array[index];
0649         if (!item)
0650             break;
0651         if (!strcmp(item, string))
0652             return index;
0653     }
0654 
0655     return -EINVAL;
0656 }
0657 EXPORT_SYMBOL(match_string);
0658 
0659 #ifndef __HAVE_ARCH_MEMSET
0660 /**
0661  * memset - Fill a region of memory with the given value
0662  * @s: Pointer to the start of the area.
0663  * @c: The byte to fill the area with
0664  * @count: The size of the area.
0665  *
0666  * Do not use memset() to access IO space, use memset_io() instead.
0667  */
0668 void *memset(void *s, int c, size_t count)
0669 {
0670     char *xs = s;
0671 
0672     while (count--)
0673         *xs++ = c;
0674     return s;
0675 }
0676 EXPORT_SYMBOL(memset);
0677 #endif
0678 
0679 /**
0680  * memzero_explicit - Fill a region of memory (e.g. sensitive
0681  *            keying data) with 0s.
0682  * @s: Pointer to the start of the area.
0683  * @count: The size of the area.
0684  *
0685  * Note: usually using memset() is just fine (!), but in cases
0686  * where clearing out _local_ data at the end of a scope is
0687  * necessary, memzero_explicit() should be used instead in
0688  * order to prevent the compiler from optimising away zeroing.
0689  *
0690  * memzero_explicit() doesn't need an arch-specific version as
0691  * it just invokes the one of memset() implicitly.
0692  */
0693 void memzero_explicit(void *s, size_t count)
0694 {
0695     memset(s, 0, count);
0696     barrier_data(s);
0697 }
0698 EXPORT_SYMBOL(memzero_explicit);
0699 
0700 #ifndef __HAVE_ARCH_MEMCPY
0701 /**
0702  * memcpy - Copy one area of memory to another
0703  * @dest: Where to copy to
0704  * @src: Where to copy from
0705  * @count: The size of the area.
0706  *
0707  * You should not use this function to access IO space, use memcpy_toio()
0708  * or memcpy_fromio() instead.
0709  */
0710 void *memcpy(void *dest, const void *src, size_t count)
0711 {
0712     char *tmp = dest;
0713     const char *s = src;
0714 
0715     while (count--)
0716         *tmp++ = *s++;
0717     return dest;
0718 }
0719 EXPORT_SYMBOL(memcpy);
0720 #endif
0721 
0722 #ifndef __HAVE_ARCH_MEMMOVE
0723 /**
0724  * memmove - Copy one area of memory to another
0725  * @dest: Where to copy to
0726  * @src: Where to copy from
0727  * @count: The size of the area.
0728  *
0729  * Unlike memcpy(), memmove() copes with overlapping areas.
0730  */
0731 void *memmove(void *dest, const void *src, size_t count)
0732 {
0733     char *tmp;
0734     const char *s;
0735 
0736     if (dest <= src) {
0737         tmp = dest;
0738         s = src;
0739         while (count--)
0740             *tmp++ = *s++;
0741     } else {
0742         tmp = dest;
0743         tmp += count;
0744         s = src;
0745         s += count;
0746         while (count--)
0747             *--tmp = *--s;
0748     }
0749     return dest;
0750 }
0751 EXPORT_SYMBOL(memmove);
0752 #endif
0753 
0754 #ifndef __HAVE_ARCH_MEMCMP
0755 /**
0756  * memcmp - Compare two areas of memory
0757  * @cs: One area of memory
0758  * @ct: Another area of memory
0759  * @count: The size of the area.
0760  */
0761 #undef memcmp
0762 __visible int memcmp(const void *cs, const void *ct, size_t count)
0763 {
0764     const unsigned char *su1, *su2;
0765     int res = 0;
0766 
0767     for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
0768         if ((res = *su1 - *su2) != 0)
0769             break;
0770     return res;
0771 }
0772 EXPORT_SYMBOL(memcmp);
0773 #endif
0774 
0775 #ifndef __HAVE_ARCH_MEMSCAN
0776 /**
0777  * memscan - Find a character in an area of memory.
0778  * @addr: The memory area
0779  * @c: The byte to search for
0780  * @size: The size of the area.
0781  *
0782  * returns the address of the first occurrence of @c, or 1 byte past
0783  * the area if @c is not found
0784  */
0785 void *memscan(void *addr, int c, size_t size)
0786 {
0787     unsigned char *p = addr;
0788 
0789     while (size) {
0790         if (*p == c)
0791             return (void *)p;
0792         p++;
0793         size--;
0794     }
0795     return (void *)p;
0796 }
0797 EXPORT_SYMBOL(memscan);
0798 #endif
0799 
0800 #ifndef __HAVE_ARCH_STRSTR
0801 /**
0802  * strstr - Find the first substring in a %NUL terminated string
0803  * @s1: The string to be searched
0804  * @s2: The string to search for
0805  */
0806 char *strstr(const char *s1, const char *s2)
0807 {
0808     size_t l1, l2;
0809 
0810     l2 = strlen(s2);
0811     if (!l2)
0812         return (char *)s1;
0813     l1 = strlen(s1);
0814     while (l1 >= l2) {
0815         l1--;
0816         if (!memcmp(s1, s2, l2))
0817             return (char *)s1;
0818         s1++;
0819     }
0820     return NULL;
0821 }
0822 EXPORT_SYMBOL(strstr);
0823 #endif
0824 
0825 #ifndef __HAVE_ARCH_STRNSTR
0826 /**
0827  * strnstr - Find the first substring in a length-limited string
0828  * @s1: The string to be searched
0829  * @s2: The string to search for
0830  * @len: the maximum number of characters to search
0831  */
0832 char *strnstr(const char *s1, const char *s2, size_t len)
0833 {
0834     size_t l2;
0835 
0836     l2 = strlen(s2);
0837     if (!l2)
0838         return (char *)s1;
0839     while (len >= l2) {
0840         len--;
0841         if (!memcmp(s1, s2, l2))
0842             return (char *)s1;
0843         s1++;
0844     }
0845     return NULL;
0846 }
0847 EXPORT_SYMBOL(strnstr);
0848 #endif
0849 
0850 #ifndef __HAVE_ARCH_MEMCHR
0851 /**
0852  * memchr - Find a character in an area of memory.
0853  * @s: The memory area
0854  * @c: The byte to search for
0855  * @n: The size of the area.
0856  *
0857  * returns the address of the first occurrence of @c, or %NULL
0858  * if @c is not found
0859  */
0860 void *memchr(const void *s, int c, size_t n)
0861 {
0862     const unsigned char *p = s;
0863     while (n-- != 0) {
0864             if ((unsigned char)c == *p++) {
0865             return (void *)(p - 1);
0866         }
0867     }
0868     return NULL;
0869 }
0870 EXPORT_SYMBOL(memchr);
0871 #endif
0872 
0873 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
0874 {
0875     while (bytes) {
0876         if (*start != value)
0877             return (void *)start;
0878         start++;
0879         bytes--;
0880     }
0881     return NULL;
0882 }
0883 
0884 /**
0885  * memchr_inv - Find an unmatching character in an area of memory.
0886  * @start: The memory area
0887  * @c: Find a character other than c
0888  * @bytes: The size of the area.
0889  *
0890  * returns the address of the first character other than @c, or %NULL
0891  * if the whole buffer contains just @c.
0892  */
0893 void *memchr_inv(const void *start, int c, size_t bytes)
0894 {
0895     u8 value = c;
0896     u64 value64;
0897     unsigned int words, prefix;
0898 
0899     if (bytes <= 16)
0900         return check_bytes8(start, value, bytes);
0901 
0902     value64 = value;
0903 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
0904     value64 *= 0x0101010101010101ULL;
0905 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
0906     value64 *= 0x01010101;
0907     value64 |= value64 << 32;
0908 #else
0909     value64 |= value64 << 8;
0910     value64 |= value64 << 16;
0911     value64 |= value64 << 32;
0912 #endif
0913 
0914     prefix = (unsigned long)start % 8;
0915     if (prefix) {
0916         u8 *r;
0917 
0918         prefix = 8 - prefix;
0919         r = check_bytes8(start, value, prefix);
0920         if (r)
0921             return r;
0922         start += prefix;
0923         bytes -= prefix;
0924     }
0925 
0926     words = bytes / 8;
0927 
0928     while (words) {
0929         if (*(u64 *)start != value64)
0930             return check_bytes8(start, value, 8);
0931         start += 8;
0932         words--;
0933     }
0934 
0935     return check_bytes8(start, value, bytes % 8);
0936 }
0937 EXPORT_SYMBOL(memchr_inv);
0938 
0939 /**
0940  * strreplace - Replace all occurrences of character in string.
0941  * @s: The string to operate on.
0942  * @old: The character being replaced.
0943  * @new: The character @old is replaced with.
0944  *
0945  * Returns pointer to the nul byte at the end of @s.
0946  */
0947 char *strreplace(char *s, char old, char new)
0948 {
0949     for (; *s; ++s)
0950         if (*s == old)
0951             *s = new;
0952     return s;
0953 }
0954 EXPORT_SYMBOL(strreplace);