• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
strncasecmp(const char * s1,const char * s2,size_t len)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
strcasecmp(const char * s1,const char * s2)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
strcpy(char * dest,const char * src)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  */
strncpy(char * dest,const char * src,size_t count)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  */
strlcpy(char * dest,const char * src,size_t size)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  */
strscpy(char * dest,const char * src,size_t count)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);
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  */
strscpy_pad(char * dest,const char * src,size_t count)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
strcat(char * dest,const char * src)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  */
strncat(char * dest,const char * src,size_t count)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  */
strlcat(char * dest,const char * src,size_t count)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
strcmp(const char * cs,const char * ct)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  */
strncmp(const char * cs,const char * ct,size_t count)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  */
strchr(const char * s,int c)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  */
strchrnul(const char * s,int c)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  */
strrchr(const char * s,int c)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  */
strnchr(const char * s,size_t count,int c)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  */
skip_spaces(const char * str)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  */
strim(char * s)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  */
strlen(const char * s)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  */
strnlen(const char * s,size_t count)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  */
strspn(const char * s,const char * accept)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  */
strcspn(const char * s,const char * reject)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  */
strpbrk(const char * cs,const char * ct)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  */
strsep(char ** s,const char * ct)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  */
sysfs_streq(const char * s1,const char * s2)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 #ifndef __HAVE_ARCH_MEMSET
691 /**
692  * memset - Fill a region of memory with the given value
693  * @s: Pointer to the start of the area.
694  * @c: The byte to fill the area with
695  * @count: The size of the area.
696  *
697  * Do not use memset() to access IO space, use memset_io() instead.
698  */
memset(void * s,int c,size_t count)699 void *memset(void *s, int c, size_t count)
700 {
701 	char *xs = s;
702 
703 	while (count--)
704 		*xs++ = c;
705 	return s;
706 }
707 EXPORT_SYMBOL(memset);
708 #endif
709 
710 /**
711  * memzero_explicit - Fill a region of memory (e.g. sensitive
712  *		      keying data) with 0s.
713  * @s: Pointer to the start of the area.
714  * @count: The size of the area.
715  *
716  * Note: usually using memset() is just fine (!), but in cases
717  * where clearing out _local_ data at the end of a scope is
718  * necessary, memzero_explicit() should be used instead in
719  * order to prevent the compiler from optimising away zeroing.
720  *
721  * memzero_explicit() doesn't need an arch-specific version as
722  * it just invokes the one of memset() implicitly.
723  */
memzero_explicit(void * s,size_t count)724 void memzero_explicit(void *s, size_t count)
725 {
726 	memset(s, 0, count);
727 	barrier_data(s);
728 }
729 EXPORT_SYMBOL(memzero_explicit);
730 
731 #ifndef __HAVE_ARCH_MEMSET16
732 /**
733  * memset16() - Fill a memory area with a uint16_t
734  * @s: Pointer to the start of the area.
735  * @v: The value to fill the area with
736  * @count: The number of values to store
737  *
738  * Differs from memset() in that it fills with a uint16_t instead
739  * of a byte.  Remember that @count is the number of uint16_ts to
740  * store, not the number of bytes.
741  */
memset16(uint16_t * s,uint16_t v,size_t count)742 void *memset16(uint16_t *s, uint16_t v, size_t count)
743 {
744 	uint16_t *xs = s;
745 
746 	while (count--)
747 		*xs++ = v;
748 	return s;
749 }
750 EXPORT_SYMBOL(memset16);
751 #endif
752 
753 #ifndef __HAVE_ARCH_MEMSET32
754 /**
755  * memset32() - Fill a memory area with a uint32_t
756  * @s: Pointer to the start of the area.
757  * @v: The value to fill the area with
758  * @count: The number of values to store
759  *
760  * Differs from memset() in that it fills with a uint32_t instead
761  * of a byte.  Remember that @count is the number of uint32_ts to
762  * store, not the number of bytes.
763  */
memset32(uint32_t * s,uint32_t v,size_t count)764 void *memset32(uint32_t *s, uint32_t v, size_t count)
765 {
766 	uint32_t *xs = s;
767 
768 	while (count--)
769 		*xs++ = v;
770 	return s;
771 }
772 EXPORT_SYMBOL(memset32);
773 #endif
774 
775 #ifndef __HAVE_ARCH_MEMSET64
776 /**
777  * memset64() - Fill a memory area with a uint64_t
778  * @s: Pointer to the start of the area.
779  * @v: The value to fill the area with
780  * @count: The number of values to store
781  *
782  * Differs from memset() in that it fills with a uint64_t instead
783  * of a byte.  Remember that @count is the number of uint64_ts to
784  * store, not the number of bytes.
785  */
memset64(uint64_t * s,uint64_t v,size_t count)786 void *memset64(uint64_t *s, uint64_t v, size_t count)
787 {
788 	uint64_t *xs = s;
789 
790 	while (count--)
791 		*xs++ = v;
792 	return s;
793 }
794 EXPORT_SYMBOL(memset64);
795 #endif
796 
797 #ifndef __HAVE_ARCH_MEMCPY
798 /**
799  * memcpy - Copy one area of memory to another
800  * @dest: Where to copy to
801  * @src: Where to copy from
802  * @count: The size of the area.
803  *
804  * You should not use this function to access IO space, use memcpy_toio()
805  * or memcpy_fromio() instead.
806  */
memcpy(void * dest,const void * src,size_t count)807 void *memcpy(void *dest, const void *src, size_t count)
808 {
809 	char *tmp = dest;
810 	const char *s = src;
811 
812 	while (count--)
813 		*tmp++ = *s++;
814 	return dest;
815 }
816 EXPORT_SYMBOL(memcpy);
817 #endif
818 
819 #ifndef __HAVE_ARCH_MEMMOVE
820 /**
821  * memmove - Copy one area of memory to another
822  * @dest: Where to copy to
823  * @src: Where to copy from
824  * @count: The size of the area.
825  *
826  * Unlike memcpy(), memmove() copes with overlapping areas.
827  */
memmove(void * dest,const void * src,size_t count)828 void *memmove(void *dest, const void *src, size_t count)
829 {
830 	char *tmp;
831 	const char *s;
832 
833 	if (dest <= src) {
834 		tmp = dest;
835 		s = src;
836 		while (count--)
837 			*tmp++ = *s++;
838 	} else {
839 		tmp = dest;
840 		tmp += count;
841 		s = src;
842 		s += count;
843 		while (count--)
844 			*--tmp = *--s;
845 	}
846 	return dest;
847 }
848 EXPORT_SYMBOL(memmove);
849 #endif
850 
851 #ifndef __HAVE_ARCH_MEMCMP
852 /**
853  * memcmp - Compare two areas of memory
854  * @cs: One area of memory
855  * @ct: Another area of memory
856  * @count: The size of the area.
857  */
858 #undef memcmp
memcmp(const void * cs,const void * ct,size_t count)859 __visible int memcmp(const void *cs, const void *ct, size_t count)
860 {
861 	const unsigned char *su1, *su2;
862 	int res = 0;
863 
864 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
865 		if ((res = *su1 - *su2) != 0)
866 			break;
867 	return res;
868 }
869 EXPORT_SYMBOL(memcmp);
870 #endif
871 
872 #ifndef __HAVE_ARCH_BCMP
873 /**
874  * bcmp - returns 0 if and only if the buffers have identical contents.
875  * @a: pointer to first buffer.
876  * @b: pointer to second buffer.
877  * @len: size of buffers.
878  *
879  * The sign or magnitude of a non-zero return value has no particular
880  * meaning, and architectures may implement their own more efficient bcmp(). So
881  * while this particular implementation is a simple (tail) call to memcmp, do
882  * not rely on anything but whether the return value is zero or non-zero.
883  */
884 #undef bcmp
bcmp(const void * a,const void * b,size_t len)885 int bcmp(const void *a, const void *b, size_t len)
886 {
887 	return memcmp(a, b, len);
888 }
889 EXPORT_SYMBOL(bcmp);
890 #endif
891 
892 #ifndef __HAVE_ARCH_MEMSCAN
893 /**
894  * memscan - Find a character in an area of memory.
895  * @addr: The memory area
896  * @c: The byte to search for
897  * @size: The size of the area.
898  *
899  * returns the address of the first occurrence of @c, or 1 byte past
900  * the area if @c is not found
901  */
memscan(void * addr,int c,size_t size)902 void *memscan(void *addr, int c, size_t size)
903 {
904 	unsigned char *p = addr;
905 
906 	while (size) {
907 		if (*p == c)
908 			return (void *)p;
909 		p++;
910 		size--;
911 	}
912   	return (void *)p;
913 }
914 EXPORT_SYMBOL(memscan);
915 #endif
916 
917 #ifndef __HAVE_ARCH_STRSTR
918 /**
919  * strstr - Find the first substring in a %NUL terminated string
920  * @s1: The string to be searched
921  * @s2: The string to search for
922  */
strstr(const char * s1,const char * s2)923 char *strstr(const char *s1, const char *s2)
924 {
925 	size_t l1, l2;
926 
927 	l2 = strlen(s2);
928 	if (!l2)
929 		return (char *)s1;
930 	l1 = strlen(s1);
931 	while (l1 >= l2) {
932 		l1--;
933 		if (!memcmp(s1, s2, l2))
934 			return (char *)s1;
935 		s1++;
936 	}
937 	return NULL;
938 }
939 EXPORT_SYMBOL(strstr);
940 #endif
941 
942 #ifndef __HAVE_ARCH_STRNSTR
943 /**
944  * strnstr - Find the first substring in a length-limited string
945  * @s1: The string to be searched
946  * @s2: The string to search for
947  * @len: the maximum number of characters to search
948  */
strnstr(const char * s1,const char * s2,size_t len)949 char *strnstr(const char *s1, const char *s2, size_t len)
950 {
951 	size_t l2;
952 
953 	l2 = strlen(s2);
954 	if (!l2)
955 		return (char *)s1;
956 	while (len >= l2) {
957 		len--;
958 		if (!memcmp(s1, s2, l2))
959 			return (char *)s1;
960 		s1++;
961 	}
962 	return NULL;
963 }
964 EXPORT_SYMBOL(strnstr);
965 #endif
966 
967 #ifndef __HAVE_ARCH_MEMCHR
968 /**
969  * memchr - Find a character in an area of memory.
970  * @s: The memory area
971  * @c: The byte to search for
972  * @n: The size of the area.
973  *
974  * returns the address of the first occurrence of @c, or %NULL
975  * if @c is not found
976  */
memchr(const void * s,int c,size_t n)977 void *memchr(const void *s, int c, size_t n)
978 {
979 	const unsigned char *p = s;
980 	while (n-- != 0) {
981         	if ((unsigned char)c == *p++) {
982 			return (void *)(p - 1);
983 		}
984 	}
985 	return NULL;
986 }
987 EXPORT_SYMBOL(memchr);
988 #endif
989 
check_bytes8(const u8 * start,u8 value,unsigned int bytes)990 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
991 {
992 	while (bytes) {
993 		if (*start != value)
994 			return (void *)start;
995 		start++;
996 		bytes--;
997 	}
998 	return NULL;
999 }
1000 
1001 /**
1002  * memchr_inv - Find an unmatching character in an area of memory.
1003  * @start: The memory area
1004  * @c: Find a character other than c
1005  * @bytes: The size of the area.
1006  *
1007  * returns the address of the first character other than @c, or %NULL
1008  * if the whole buffer contains just @c.
1009  */
memchr_inv(const void * start,int c,size_t bytes)1010 void *memchr_inv(const void *start, int c, size_t bytes)
1011 {
1012 	u8 value = c;
1013 	u64 value64;
1014 	unsigned int words, prefix;
1015 
1016 	if (bytes <= 16)
1017 		return check_bytes8(start, value, bytes);
1018 
1019 	value64 = value;
1020 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
1021 	value64 *= 0x0101010101010101ULL;
1022 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
1023 	value64 *= 0x01010101;
1024 	value64 |= value64 << 32;
1025 #else
1026 	value64 |= value64 << 8;
1027 	value64 |= value64 << 16;
1028 	value64 |= value64 << 32;
1029 #endif
1030 
1031 	prefix = (unsigned long)start % 8;
1032 	if (prefix) {
1033 		u8 *r;
1034 
1035 		prefix = 8 - prefix;
1036 		r = check_bytes8(start, value, prefix);
1037 		if (r)
1038 			return r;
1039 		start += prefix;
1040 		bytes -= prefix;
1041 	}
1042 
1043 	words = bytes / 8;
1044 
1045 	while (words) {
1046 		if (*(u64 *)start != value64)
1047 			return check_bytes8(start, value, 8);
1048 		start += 8;
1049 		words--;
1050 	}
1051 
1052 	return check_bytes8(start, value, bytes % 8);
1053 }
1054 EXPORT_SYMBOL(memchr_inv);
1055 
1056 /**
1057  * strreplace - Replace all occurrences of character in string.
1058  * @s: The string to operate on.
1059  * @old: The character being replaced.
1060  * @new: The character @old is replaced with.
1061  *
1062  * Returns pointer to the nul byte at the end of @s.
1063  */
strreplace(char * s,char old,char new)1064 char *strreplace(char *s, char old, char new)
1065 {
1066 	for (; *s; ++s)
1067 		if (*s == old)
1068 			*s = new;
1069 	return s;
1070 }
1071 EXPORT_SYMBOL(strreplace);
1072