• 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 #ifndef __HAVE_ARCH_STRNICMP
66 #undef strnicmp
strnicmp(const char * s1,const char * s2,size_t len)67 int strnicmp(const char *s1, const char *s2, size_t len)
68 {
69 	return strncasecmp(s1, s2, len);
70 }
71 EXPORT_SYMBOL(strnicmp);
72 #endif
73 
74 #ifndef __HAVE_ARCH_STRCASECMP
strcasecmp(const char * s1,const char * s2)75 int strcasecmp(const char *s1, const char *s2)
76 {
77 	int c1, c2;
78 
79 	do {
80 		c1 = tolower(*s1++);
81 		c2 = tolower(*s2++);
82 	} while (c1 == c2 && c1 != 0);
83 	return c1 - c2;
84 }
85 EXPORT_SYMBOL(strcasecmp);
86 #endif
87 
88 #ifndef __HAVE_ARCH_STRCPY
89 /**
90  * strcpy - Copy a %NUL terminated string
91  * @dest: Where to copy the string to
92  * @src: Where to copy the string from
93  */
94 #undef strcpy
strcpy(char * dest,const char * src)95 char *strcpy(char *dest, const char *src)
96 {
97 	char *tmp = dest;
98 
99 	while ((*dest++ = *src++) != '\0')
100 		/* nothing */;
101 	return tmp;
102 }
103 EXPORT_SYMBOL(strcpy);
104 #endif
105 
106 #ifndef __HAVE_ARCH_STRNCPY
107 /**
108  * strncpy - Copy a length-limited, C-string
109  * @dest: Where to copy the string to
110  * @src: Where to copy the string from
111  * @count: The maximum number of bytes to copy
112  *
113  * The result is not %NUL-terminated if the source exceeds
114  * @count bytes.
115  *
116  * In the case where the length of @src is less than  that  of
117  * count, the remainder of @dest will be padded with %NUL.
118  *
119  */
strncpy(char * dest,const char * src,size_t count)120 char *strncpy(char *dest, const char *src, size_t count)
121 {
122 	char *tmp = dest;
123 
124 	while (count) {
125 		if ((*tmp = *src) != 0)
126 			src++;
127 		tmp++;
128 		count--;
129 	}
130 	return dest;
131 }
132 EXPORT_SYMBOL(strncpy);
133 #endif
134 
135 #ifndef __HAVE_ARCH_STRLCPY
136 /**
137  * strlcpy - Copy a C-string into a sized buffer
138  * @dest: Where to copy the string to
139  * @src: Where to copy the string from
140  * @size: size of destination buffer
141  *
142  * Compatible with *BSD: the result is always a valid
143  * NUL-terminated string that fits in the buffer (unless,
144  * of course, the buffer size is zero). It does not pad
145  * out the result like strncpy() does.
146  */
strlcpy(char * dest,const char * src,size_t size)147 size_t strlcpy(char *dest, const char *src, size_t size)
148 {
149 	size_t ret = strlen(src);
150 
151 	if (size) {
152 		size_t len = (ret >= size) ? size - 1 : ret;
153 		memcpy(dest, src, len);
154 		dest[len] = '\0';
155 	}
156 	return ret;
157 }
158 EXPORT_SYMBOL(strlcpy);
159 #endif
160 
161 #ifndef __HAVE_ARCH_STRSCPY
162 /**
163  * strscpy - Copy a C-string into a sized buffer
164  * @dest: Where to copy the string to
165  * @src: Where to copy the string from
166  * @count: Size of destination buffer
167  *
168  * Copy the string, or as much of it as fits, into the dest buffer.
169  * The routine returns the number of characters copied (not including
170  * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough.
171  * The behavior is undefined if the string buffers overlap.
172  * The destination buffer is always NUL terminated, unless it's zero-sized.
173  *
174  * Preferred to strlcpy() since the API doesn't require reading memory
175  * from the src string beyond the specified "count" bytes, and since
176  * the return value is easier to error-check than strlcpy()'s.
177  * In addition, the implementation is robust to the string changing out
178  * from underneath it, unlike the current strlcpy() implementation.
179  *
180  * Preferred to strncpy() since it always returns a valid string, and
181  * doesn't unnecessarily force the tail of the destination buffer to be
182  * zeroed.  If the zeroing is desired, it's likely cleaner to use strscpy()
183  * with an overflow test, then just memset() the tail of the dest buffer.
184  */
strscpy(char * dest,const char * src,size_t count)185 ssize_t strscpy(char *dest, const char *src, size_t count)
186 {
187 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
188 	size_t max = count;
189 	long res = 0;
190 
191 	if (count == 0)
192 		return -E2BIG;
193 
194 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
195 	/*
196 	 * If src is unaligned, don't cross a page boundary,
197 	 * since we don't know if the next page is mapped.
198 	 */
199 	if ((long)src & (sizeof(long) - 1)) {
200 		size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
201 		if (limit < max)
202 			max = limit;
203 	}
204 #else
205 	/* If src or dest is unaligned, don't do word-at-a-time. */
206 	if (((long) dest | (long) src) & (sizeof(long) - 1))
207 		max = 0;
208 #endif
209 
210 	while (max >= sizeof(unsigned long)) {
211 		unsigned long c, data;
212 
213 		c = *(unsigned long *)(src+res);
214 		if (has_zero(c, &data, &constants)) {
215 			data = prep_zero_mask(c, data, &constants);
216 			data = create_zero_mask(data);
217 			*(unsigned long *)(dest+res) = c & zero_bytemask(data);
218 			return res + find_zero(data);
219 		}
220 		*(unsigned long *)(dest+res) = c;
221 		res += sizeof(unsigned long);
222 		count -= sizeof(unsigned long);
223 		max -= sizeof(unsigned long);
224 	}
225 
226 	while (count) {
227 		char c;
228 
229 		c = src[res];
230 		dest[res] = c;
231 		if (!c)
232 			return res;
233 		res++;
234 		count--;
235 	}
236 
237 	/* Hit buffer length without finding a NUL; force NUL-termination. */
238 	if (res)
239 		dest[res-1] = '\0';
240 
241 	return -E2BIG;
242 }
243 EXPORT_SYMBOL(strscpy);
244 #endif
245 
246 #ifndef __HAVE_ARCH_STRCAT
247 /**
248  * strcat - Append one %NUL-terminated string to another
249  * @dest: The string to be appended to
250  * @src: The string to append to it
251  */
252 #undef strcat
strcat(char * dest,const char * src)253 char *strcat(char *dest, const char *src)
254 {
255 	char *tmp = dest;
256 
257 	while (*dest)
258 		dest++;
259 	while ((*dest++ = *src++) != '\0')
260 		;
261 	return tmp;
262 }
263 EXPORT_SYMBOL(strcat);
264 #endif
265 
266 #ifndef __HAVE_ARCH_STRNCAT
267 /**
268  * strncat - Append a length-limited, C-string to another
269  * @dest: The string to be appended to
270  * @src: The string to append to it
271  * @count: The maximum numbers of bytes to copy
272  *
273  * Note that in contrast to strncpy(), strncat() ensures the result is
274  * terminated.
275  */
strncat(char * dest,const char * src,size_t count)276 char *strncat(char *dest, const char *src, size_t count)
277 {
278 	char *tmp = dest;
279 
280 	if (count) {
281 		while (*dest)
282 			dest++;
283 		while ((*dest++ = *src++) != 0) {
284 			if (--count == 0) {
285 				*dest = '\0';
286 				break;
287 			}
288 		}
289 	}
290 	return tmp;
291 }
292 EXPORT_SYMBOL(strncat);
293 #endif
294 
295 #ifndef __HAVE_ARCH_STRLCAT
296 /**
297  * strlcat - Append a length-limited, C-string to another
298  * @dest: The string to be appended to
299  * @src: The string to append to it
300  * @count: The size of the destination buffer.
301  */
strlcat(char * dest,const char * src,size_t count)302 size_t strlcat(char *dest, const char *src, size_t count)
303 {
304 	size_t dsize = strlen(dest);
305 	size_t len = strlen(src);
306 	size_t res = dsize + len;
307 
308 	/* This would be a bug */
309 	BUG_ON(dsize >= count);
310 
311 	dest += dsize;
312 	count -= dsize;
313 	if (len >= count)
314 		len = count-1;
315 	memcpy(dest, src, len);
316 	dest[len] = 0;
317 	return res;
318 }
319 EXPORT_SYMBOL(strlcat);
320 #endif
321 
322 #ifndef __HAVE_ARCH_STRCMP
323 /**
324  * strcmp - Compare two strings
325  * @cs: One string
326  * @ct: Another string
327  */
328 #undef strcmp
strcmp(const char * cs,const char * ct)329 int strcmp(const char *cs, const char *ct)
330 {
331 	unsigned char c1, c2;
332 
333 	while (1) {
334 		c1 = *cs++;
335 		c2 = *ct++;
336 		if (c1 != c2)
337 			return c1 < c2 ? -1 : 1;
338 		if (!c1)
339 			break;
340 	}
341 	return 0;
342 }
343 EXPORT_SYMBOL(strcmp);
344 #endif
345 
346 #ifndef __HAVE_ARCH_STRNCMP
347 /**
348  * strncmp - Compare two length-limited strings
349  * @cs: One string
350  * @ct: Another string
351  * @count: The maximum number of bytes to compare
352  */
strncmp(const char * cs,const char * ct,size_t count)353 int strncmp(const char *cs, const char *ct, size_t count)
354 {
355 	unsigned char c1, c2;
356 
357 	while (count) {
358 		c1 = *cs++;
359 		c2 = *ct++;
360 		if (c1 != c2)
361 			return c1 < c2 ? -1 : 1;
362 		if (!c1)
363 			break;
364 		count--;
365 	}
366 	return 0;
367 }
368 EXPORT_SYMBOL(strncmp);
369 #endif
370 
371 #ifndef __HAVE_ARCH_STRCHR
372 /**
373  * strchr - Find the first occurrence of a character in a string
374  * @s: The string to be searched
375  * @c: The character to search for
376  */
strchr(const char * s,int c)377 char *strchr(const char *s, int c)
378 {
379 	for (; *s != (char)c; ++s)
380 		if (*s == '\0')
381 			return NULL;
382 	return (char *)s;
383 }
384 EXPORT_SYMBOL(strchr);
385 #endif
386 
387 #ifndef __HAVE_ARCH_STRCHRNUL
388 /**
389  * strchrnul - Find and return a character in a string, or end of string
390  * @s: The string to be searched
391  * @c: The character to search for
392  *
393  * Returns pointer to first occurrence of 'c' in s. If c is not found, then
394  * return a pointer to the null byte at the end of s.
395  */
strchrnul(const char * s,int c)396 char *strchrnul(const char *s, int c)
397 {
398 	while (*s && *s != (char)c)
399 		s++;
400 	return (char *)s;
401 }
402 EXPORT_SYMBOL(strchrnul);
403 #endif
404 
405 #ifndef __HAVE_ARCH_STRRCHR
406 /**
407  * strrchr - Find the last occurrence of a character in a string
408  * @s: The string to be searched
409  * @c: The character to search for
410  */
strrchr(const char * s,int c)411 char *strrchr(const char *s, int c)
412 {
413        const char *p = s + strlen(s);
414        do {
415            if (*p == (char)c)
416                return (char *)p;
417        } while (--p >= s);
418        return NULL;
419 }
420 EXPORT_SYMBOL(strrchr);
421 #endif
422 
423 #ifndef __HAVE_ARCH_STRNCHR
424 /**
425  * strnchr - Find a character in a length limited string
426  * @s: The string to be searched
427  * @count: The number of characters to be searched
428  * @c: The character to search for
429  */
strnchr(const char * s,size_t count,int c)430 char *strnchr(const char *s, size_t count, int c)
431 {
432 	for (; count-- && *s != '\0'; ++s)
433 		if (*s == (char)c)
434 			return (char *)s;
435 	return NULL;
436 }
437 EXPORT_SYMBOL(strnchr);
438 #endif
439 
440 /**
441  * skip_spaces - Removes leading whitespace from @str.
442  * @str: The string to be stripped.
443  *
444  * Returns a pointer to the first non-whitespace character in @str.
445  */
skip_spaces(const char * str)446 char *skip_spaces(const char *str)
447 {
448 	while (isspace(*str))
449 		++str;
450 	return (char *)str;
451 }
452 EXPORT_SYMBOL(skip_spaces);
453 
454 /**
455  * strim - Removes leading and trailing whitespace from @s.
456  * @s: The string to be stripped.
457  *
458  * Note that the first trailing whitespace is replaced with a %NUL-terminator
459  * in the given string @s. Returns a pointer to the first non-whitespace
460  * character in @s.
461  */
strim(char * s)462 char *strim(char *s)
463 {
464 	size_t size;
465 	char *end;
466 
467 	size = strlen(s);
468 	if (!size)
469 		return s;
470 
471 	end = s + size - 1;
472 	while (end >= s && isspace(*end))
473 		end--;
474 	*(end + 1) = '\0';
475 
476 	return skip_spaces(s);
477 }
478 EXPORT_SYMBOL(strim);
479 
480 #ifndef __HAVE_ARCH_STRLEN
481 /**
482  * strlen - Find the length of a string
483  * @s: The string to be sized
484  */
strlen(const char * s)485 size_t strlen(const char *s)
486 {
487 	const char *sc;
488 
489 	for (sc = s; *sc != '\0'; ++sc)
490 		/* nothing */;
491 	return sc - s;
492 }
493 EXPORT_SYMBOL(strlen);
494 #endif
495 
496 #ifndef __HAVE_ARCH_STRNLEN
497 /**
498  * strnlen - Find the length of a length-limited string
499  * @s: The string to be sized
500  * @count: The maximum number of bytes to search
501  */
strnlen(const char * s,size_t count)502 size_t strnlen(const char *s, size_t count)
503 {
504 	const char *sc;
505 
506 	for (sc = s; count-- && *sc != '\0'; ++sc)
507 		/* nothing */;
508 	return sc - s;
509 }
510 EXPORT_SYMBOL(strnlen);
511 #endif
512 
513 #ifndef __HAVE_ARCH_STRSPN
514 /**
515  * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
516  * @s: The string to be searched
517  * @accept: The string to search for
518  */
strspn(const char * s,const char * accept)519 size_t strspn(const char *s, const char *accept)
520 {
521 	const char *p;
522 	const char *a;
523 	size_t count = 0;
524 
525 	for (p = s; *p != '\0'; ++p) {
526 		for (a = accept; *a != '\0'; ++a) {
527 			if (*p == *a)
528 				break;
529 		}
530 		if (*a == '\0')
531 			return count;
532 		++count;
533 	}
534 	return count;
535 }
536 
537 EXPORT_SYMBOL(strspn);
538 #endif
539 
540 #ifndef __HAVE_ARCH_STRCSPN
541 /**
542  * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
543  * @s: The string to be searched
544  * @reject: The string to avoid
545  */
strcspn(const char * s,const char * reject)546 size_t strcspn(const char *s, const char *reject)
547 {
548 	const char *p;
549 	const char *r;
550 	size_t count = 0;
551 
552 	for (p = s; *p != '\0'; ++p) {
553 		for (r = reject; *r != '\0'; ++r) {
554 			if (*p == *r)
555 				return count;
556 		}
557 		++count;
558 	}
559 	return count;
560 }
561 EXPORT_SYMBOL(strcspn);
562 #endif
563 
564 #ifndef __HAVE_ARCH_STRPBRK
565 /**
566  * strpbrk - Find the first occurrence of a set of characters
567  * @cs: The string to be searched
568  * @ct: The characters to search for
569  */
strpbrk(const char * cs,const char * ct)570 char *strpbrk(const char *cs, const char *ct)
571 {
572 	const char *sc1, *sc2;
573 
574 	for (sc1 = cs; *sc1 != '\0'; ++sc1) {
575 		for (sc2 = ct; *sc2 != '\0'; ++sc2) {
576 			if (*sc1 == *sc2)
577 				return (char *)sc1;
578 		}
579 	}
580 	return NULL;
581 }
582 EXPORT_SYMBOL(strpbrk);
583 #endif
584 
585 #ifndef __HAVE_ARCH_STRSEP
586 /**
587  * strsep - Split a string into tokens
588  * @s: The string to be searched
589  * @ct: The characters to search for
590  *
591  * strsep() updates @s to point after the token, ready for the next call.
592  *
593  * It returns empty tokens, too, behaving exactly like the libc function
594  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
595  * Same semantics, slimmer shape. ;)
596  */
strsep(char ** s,const char * ct)597 char *strsep(char **s, const char *ct)
598 {
599 	char *sbegin = *s;
600 	char *end;
601 
602 	if (sbegin == NULL)
603 		return NULL;
604 
605 	end = strpbrk(sbegin, ct);
606 	if (end)
607 		*end++ = '\0';
608 	*s = end;
609 	return sbegin;
610 }
611 EXPORT_SYMBOL(strsep);
612 #endif
613 
614 /**
615  * sysfs_streq - return true if strings are equal, modulo trailing newline
616  * @s1: one string
617  * @s2: another string
618  *
619  * This routine returns true iff two strings are equal, treating both
620  * NUL and newline-then-NUL as equivalent string terminations.  It's
621  * geared for use with sysfs input strings, which generally terminate
622  * with newlines but are compared against values without newlines.
623  */
sysfs_streq(const char * s1,const char * s2)624 bool sysfs_streq(const char *s1, const char *s2)
625 {
626 	while (*s1 && *s1 == *s2) {
627 		s1++;
628 		s2++;
629 	}
630 
631 	if (*s1 == *s2)
632 		return true;
633 	if (!*s1 && *s2 == '\n' && !s2[1])
634 		return true;
635 	if (*s1 == '\n' && !s1[1] && !*s2)
636 		return true;
637 	return false;
638 }
639 EXPORT_SYMBOL(sysfs_streq);
640 
641 /**
642  * strtobool - convert common user inputs into boolean values
643  * @s: input string
644  * @res: result
645  *
646  * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
647  * Otherwise it will return -EINVAL.  Value pointed to by res is
648  * updated upon finding a match.
649  */
strtobool(const char * s,bool * res)650 int strtobool(const char *s, bool *res)
651 {
652 	switch (s[0]) {
653 	case 'y':
654 	case 'Y':
655 	case '1':
656 		*res = true;
657 		break;
658 	case 'n':
659 	case 'N':
660 	case '0':
661 		*res = false;
662 		break;
663 	default:
664 		return -EINVAL;
665 	}
666 	return 0;
667 }
668 EXPORT_SYMBOL(strtobool);
669 
670 #ifndef __HAVE_ARCH_MEMSET
671 /**
672  * memset - Fill a region of memory with the given value
673  * @s: Pointer to the start of the area.
674  * @c: The byte to fill the area with
675  * @count: The size of the area.
676  *
677  * Do not use memset() to access IO space, use memset_io() instead.
678  */
memset(void * s,int c,size_t count)679 void *memset(void *s, int c, size_t count)
680 {
681 	char *xs = s;
682 
683 	while (count--)
684 		*xs++ = c;
685 	return s;
686 }
687 EXPORT_SYMBOL(memset);
688 #endif
689 
690 /**
691  * memzero_explicit - Fill a region of memory (e.g. sensitive
692  *		      keying data) with 0s.
693  * @s: Pointer to the start of the area.
694  * @count: The size of the area.
695  *
696  * memzero_explicit() doesn't need an arch-specific version as
697  * it just invokes the one of memset() implicitly.
698  */
memzero_explicit(void * s,size_t count)699 void memzero_explicit(void *s, size_t count)
700 {
701 	memset(s, 0, count);
702 	barrier();
703 }
704 EXPORT_SYMBOL(memzero_explicit);
705 
706 #ifndef __HAVE_ARCH_MEMCPY
707 /**
708  * memcpy - Copy one area of memory to another
709  * @dest: Where to copy to
710  * @src: Where to copy from
711  * @count: The size of the area.
712  *
713  * You should not use this function to access IO space, use memcpy_toio()
714  * or memcpy_fromio() instead.
715  */
memcpy(void * dest,const void * src,size_t count)716 void *memcpy(void *dest, const void *src, size_t count)
717 {
718 	char *tmp = dest;
719 	const char *s = src;
720 
721 	while (count--)
722 		*tmp++ = *s++;
723 	return dest;
724 }
725 EXPORT_SYMBOL(memcpy);
726 #endif
727 
728 #ifndef __HAVE_ARCH_MEMMOVE
729 /**
730  * memmove - Copy one area of memory to another
731  * @dest: Where to copy to
732  * @src: Where to copy from
733  * @count: The size of the area.
734  *
735  * Unlike memcpy(), memmove() copes with overlapping areas.
736  */
memmove(void * dest,const void * src,size_t count)737 void *memmove(void *dest, const void *src, size_t count)
738 {
739 	char *tmp;
740 	const char *s;
741 
742 	if (dest <= src) {
743 		tmp = dest;
744 		s = src;
745 		while (count--)
746 			*tmp++ = *s++;
747 	} else {
748 		tmp = dest;
749 		tmp += count;
750 		s = src;
751 		s += count;
752 		while (count--)
753 			*--tmp = *--s;
754 	}
755 	return dest;
756 }
757 EXPORT_SYMBOL(memmove);
758 #endif
759 
760 #ifndef __HAVE_ARCH_MEMCMP
761 /**
762  * memcmp - Compare two areas of memory
763  * @cs: One area of memory
764  * @ct: Another area of memory
765  * @count: The size of the area.
766  */
767 #undef memcmp
memcmp(const void * cs,const void * ct,size_t count)768 __visible int memcmp(const void *cs, const void *ct, size_t count)
769 {
770 	const unsigned char *su1, *su2;
771 	int res = 0;
772 
773 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
774 		if ((res = *su1 - *su2) != 0)
775 			break;
776 	return res;
777 }
778 EXPORT_SYMBOL(memcmp);
779 #endif
780 
781 #ifndef __HAVE_ARCH_MEMSCAN
782 /**
783  * memscan - Find a character in an area of memory.
784  * @addr: The memory area
785  * @c: The byte to search for
786  * @size: The size of the area.
787  *
788  * returns the address of the first occurrence of @c, or 1 byte past
789  * the area if @c is not found
790  */
memscan(void * addr,int c,size_t size)791 void *memscan(void *addr, int c, size_t size)
792 {
793 	unsigned char *p = addr;
794 
795 	while (size) {
796 		if (*p == c)
797 			return (void *)p;
798 		p++;
799 		size--;
800 	}
801   	return (void *)p;
802 }
803 EXPORT_SYMBOL(memscan);
804 #endif
805 
806 #ifndef __HAVE_ARCH_STRSTR
807 /**
808  * strstr - Find the first substring in a %NUL terminated string
809  * @s1: The string to be searched
810  * @s2: The string to search for
811  */
strstr(const char * s1,const char * s2)812 char *strstr(const char *s1, const char *s2)
813 {
814 	size_t l1, l2;
815 
816 	l2 = strlen(s2);
817 	if (!l2)
818 		return (char *)s1;
819 	l1 = strlen(s1);
820 	while (l1 >= l2) {
821 		l1--;
822 		if (!memcmp(s1, s2, l2))
823 			return (char *)s1;
824 		s1++;
825 	}
826 	return NULL;
827 }
828 EXPORT_SYMBOL(strstr);
829 #endif
830 
831 #ifndef __HAVE_ARCH_STRNSTR
832 /**
833  * strnstr - Find the first substring in a length-limited string
834  * @s1: The string to be searched
835  * @s2: The string to search for
836  * @len: the maximum number of characters to search
837  */
strnstr(const char * s1,const char * s2,size_t len)838 char *strnstr(const char *s1, const char *s2, size_t len)
839 {
840 	size_t l2;
841 
842 	l2 = strlen(s2);
843 	if (!l2)
844 		return (char *)s1;
845 	while (len >= l2) {
846 		len--;
847 		if (!memcmp(s1, s2, l2))
848 			return (char *)s1;
849 		s1++;
850 	}
851 	return NULL;
852 }
853 EXPORT_SYMBOL(strnstr);
854 #endif
855 
856 #ifndef __HAVE_ARCH_MEMCHR
857 /**
858  * memchr - Find a character in an area of memory.
859  * @s: The memory area
860  * @c: The byte to search for
861  * @n: The size of the area.
862  *
863  * returns the address of the first occurrence of @c, or %NULL
864  * if @c is not found
865  */
memchr(const void * s,int c,size_t n)866 void *memchr(const void *s, int c, size_t n)
867 {
868 	const unsigned char *p = s;
869 	while (n-- != 0) {
870         	if ((unsigned char)c == *p++) {
871 			return (void *)(p - 1);
872 		}
873 	}
874 	return NULL;
875 }
876 EXPORT_SYMBOL(memchr);
877 #endif
878 
check_bytes8(const u8 * start,u8 value,unsigned int bytes)879 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
880 {
881 	while (bytes) {
882 		if (*start != value)
883 			return (void *)start;
884 		start++;
885 		bytes--;
886 	}
887 	return NULL;
888 }
889 
890 /**
891  * memchr_inv - Find an unmatching character in an area of memory.
892  * @start: The memory area
893  * @c: Find a character other than c
894  * @bytes: The size of the area.
895  *
896  * returns the address of the first character other than @c, or %NULL
897  * if the whole buffer contains just @c.
898  */
memchr_inv(const void * start,int c,size_t bytes)899 void *memchr_inv(const void *start, int c, size_t bytes)
900 {
901 	u8 value = c;
902 	u64 value64;
903 	unsigned int words, prefix;
904 
905 	if (bytes <= 16)
906 		return check_bytes8(start, value, bytes);
907 
908 	value64 = value;
909 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
910 	value64 *= 0x0101010101010101;
911 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
912 	value64 *= 0x01010101;
913 	value64 |= value64 << 32;
914 #else
915 	value64 |= value64 << 8;
916 	value64 |= value64 << 16;
917 	value64 |= value64 << 32;
918 #endif
919 
920 	prefix = (unsigned long)start % 8;
921 	if (prefix) {
922 		u8 *r;
923 
924 		prefix = 8 - prefix;
925 		r = check_bytes8(start, value, prefix);
926 		if (r)
927 			return r;
928 		start += prefix;
929 		bytes -= prefix;
930 	}
931 
932 	words = bytes / 8;
933 
934 	while (words) {
935 		if (*(u64 *)start != value64)
936 			return check_bytes8(start, value, 8);
937 		start += 8;
938 		words--;
939 	}
940 
941 	return check_bytes8(start, value, bytes % 8);
942 }
943 EXPORT_SYMBOL(memchr_inv);
944 
945 /**
946  * strreplace - Replace all occurrences of character in string.
947  * @s: The string to operate on.
948  * @old: The character being replaced.
949  * @new: The character @old is replaced with.
950  *
951  * Returns pointer to the nul byte at the end of @s.
952  */
strreplace(char * s,char old,char new)953 char *strreplace(char *s, char old, char new)
954 {
955 	for (; *s; ++s)
956 		if (*s == old)
957 			*s = new;
958 	return s;
959 }
960 EXPORT_SYMBOL(strreplace);
961