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