net: sungem: fix rx checksum support
[linux/fpc-iii.git] / lib / string.c
blob8e8a2e9e9522def566ad908da7191fd99f1d8fa3
1 /*
2 * linux/lib/string.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * stupid library routines.. The optimized versions should generally be found
9 * as inline code in <asm-xx/string.h>
11 * These are buggy as well..
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.
17 * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
18 * Matthew Hawkins <matt@mh.dropbear.id.au>
19 * - Kissed strtok() goodbye
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>
30 #include <asm/byteorder.h>
31 #include <asm/word-at-a-time.h>
32 #include <asm/page.h>
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
41 int strncasecmp(const char *s1, const char *s2, size_t len)
43 /* Yes, Virginia, it had better be unsigned */
44 unsigned char c1, c2;
46 if (!len)
47 return 0;
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;
63 EXPORT_SYMBOL(strncasecmp);
64 #endif
65 #ifndef __HAVE_ARCH_STRNICMP
66 #undef strnicmp
67 int strnicmp(const char *s1, const char *s2, size_t len)
69 return strncasecmp(s1, s2, len);
71 EXPORT_SYMBOL(strnicmp);
72 #endif
74 #ifndef __HAVE_ARCH_STRCASECMP
75 int strcasecmp(const char *s1, const char *s2)
77 int c1, c2;
79 do {
80 c1 = tolower(*s1++);
81 c2 = tolower(*s2++);
82 } while (c1 == c2 && c1 != 0);
83 return c1 - c2;
85 EXPORT_SYMBOL(strcasecmp);
86 #endif
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
94 #undef strcpy
95 char *strcpy(char *dest, const char *src)
97 char *tmp = dest;
99 while ((*dest++ = *src++) != '\0')
100 /* nothing */;
101 return tmp;
103 EXPORT_SYMBOL(strcpy);
104 #endif
106 #ifndef __HAVE_ARCH_STRNCPY
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
113 * The result is not %NUL-terminated if the source exceeds
114 * @count bytes.
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.
120 char *strncpy(char *dest, const char *src, size_t count)
122 char *tmp = dest;
124 while (count) {
125 if ((*tmp = *src) != 0)
126 src++;
127 tmp++;
128 count--;
130 return dest;
132 EXPORT_SYMBOL(strncpy);
133 #endif
135 #ifndef __HAVE_ARCH_STRLCPY
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
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.
147 size_t strlcpy(char *dest, const char *src, size_t size)
149 size_t ret = strlen(src);
151 if (size) {
152 size_t len = (ret >= size) ? size - 1 : ret;
153 memcpy(dest, src, len);
154 dest[len] = '\0';
156 return ret;
158 EXPORT_SYMBOL(strlcpy);
159 #endif
161 #ifndef __HAVE_ARCH_STRSCPY
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
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.
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.
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.
185 ssize_t strscpy(char *dest, const char *src, size_t count)
187 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
188 size_t max = count;
189 long res = 0;
191 if (count == 0)
192 return -E2BIG;
194 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
196 * If src is unaligned, don't cross a page boundary,
197 * since we don't know if the next page is mapped.
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;
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
210 while (max >= sizeof(unsigned long)) {
211 unsigned long c, data;
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);
220 *(unsigned long *)(dest+res) = c;
221 res += sizeof(unsigned long);
222 count -= sizeof(unsigned long);
223 max -= sizeof(unsigned long);
226 while (count) {
227 char c;
229 c = src[res];
230 dest[res] = c;
231 if (!c)
232 return res;
233 res++;
234 count--;
237 /* Hit buffer length without finding a NUL; force NUL-termination. */
238 if (res)
239 dest[res-1] = '\0';
241 return -E2BIG;
243 EXPORT_SYMBOL(strscpy);
244 #endif
246 #ifndef __HAVE_ARCH_STRCAT
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
252 #undef strcat
253 char *strcat(char *dest, const char *src)
255 char *tmp = dest;
257 while (*dest)
258 dest++;
259 while ((*dest++ = *src++) != '\0')
261 return tmp;
263 EXPORT_SYMBOL(strcat);
264 #endif
266 #ifndef __HAVE_ARCH_STRNCAT
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
273 * Note that in contrast to strncpy(), strncat() ensures the result is
274 * terminated.
276 char *strncat(char *dest, const char *src, size_t count)
278 char *tmp = dest;
280 if (count) {
281 while (*dest)
282 dest++;
283 while ((*dest++ = *src++) != 0) {
284 if (--count == 0) {
285 *dest = '\0';
286 break;
290 return tmp;
292 EXPORT_SYMBOL(strncat);
293 #endif
295 #ifndef __HAVE_ARCH_STRLCAT
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.
302 size_t strlcat(char *dest, const char *src, size_t count)
304 size_t dsize = strlen(dest);
305 size_t len = strlen(src);
306 size_t res = dsize + len;
308 /* This would be a bug */
309 BUG_ON(dsize >= count);
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;
319 EXPORT_SYMBOL(strlcat);
320 #endif
322 #ifndef __HAVE_ARCH_STRCMP
324 * strcmp - Compare two strings
325 * @cs: One string
326 * @ct: Another string
328 #undef strcmp
329 int strcmp(const char *cs, const char *ct)
331 unsigned char c1, c2;
333 while (1) {
334 c1 = *cs++;
335 c2 = *ct++;
336 if (c1 != c2)
337 return c1 < c2 ? -1 : 1;
338 if (!c1)
339 break;
341 return 0;
343 EXPORT_SYMBOL(strcmp);
344 #endif
346 #ifndef __HAVE_ARCH_STRNCMP
348 * strncmp - Compare two length-limited strings
349 * @cs: One string
350 * @ct: Another string
351 * @count: The maximum number of bytes to compare
353 int strncmp(const char *cs, const char *ct, size_t count)
355 unsigned char c1, c2;
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--;
366 return 0;
368 EXPORT_SYMBOL(strncmp);
369 #endif
371 #ifndef __HAVE_ARCH_STRCHR
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
377 char *strchr(const char *s, int c)
379 for (; *s != (char)c; ++s)
380 if (*s == '\0')
381 return NULL;
382 return (char *)s;
384 EXPORT_SYMBOL(strchr);
385 #endif
387 #ifndef __HAVE_ARCH_STRCHRNUL
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
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.
396 char *strchrnul(const char *s, int c)
398 while (*s && *s != (char)c)
399 s++;
400 return (char *)s;
402 EXPORT_SYMBOL(strchrnul);
403 #endif
405 #ifndef __HAVE_ARCH_STRRCHR
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
411 char *strrchr(const char *s, int c)
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;
420 EXPORT_SYMBOL(strrchr);
421 #endif
423 #ifndef __HAVE_ARCH_STRNCHR
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
430 char *strnchr(const char *s, size_t count, int c)
432 for (; count-- && *s != '\0'; ++s)
433 if (*s == (char)c)
434 return (char *)s;
435 return NULL;
437 EXPORT_SYMBOL(strnchr);
438 #endif
441 * skip_spaces - Removes leading whitespace from @str.
442 * @str: The string to be stripped.
444 * Returns a pointer to the first non-whitespace character in @str.
446 char *skip_spaces(const char *str)
448 while (isspace(*str))
449 ++str;
450 return (char *)str;
452 EXPORT_SYMBOL(skip_spaces);
455 * strim - Removes leading and trailing whitespace from @s.
456 * @s: The string to be stripped.
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.
462 char *strim(char *s)
464 size_t size;
465 char *end;
467 size = strlen(s);
468 if (!size)
469 return s;
471 end = s + size - 1;
472 while (end >= s && isspace(*end))
473 end--;
474 *(end + 1) = '\0';
476 return skip_spaces(s);
478 EXPORT_SYMBOL(strim);
480 #ifndef __HAVE_ARCH_STRLEN
482 * strlen - Find the length of a string
483 * @s: The string to be sized
485 size_t strlen(const char *s)
487 const char *sc;
489 for (sc = s; *sc != '\0'; ++sc)
490 /* nothing */;
491 return sc - s;
493 EXPORT_SYMBOL(strlen);
494 #endif
496 #ifndef __HAVE_ARCH_STRNLEN
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
502 size_t strnlen(const char *s, size_t count)
504 const char *sc;
506 for (sc = s; count-- && *sc != '\0'; ++sc)
507 /* nothing */;
508 return sc - s;
510 EXPORT_SYMBOL(strnlen);
511 #endif
513 #ifndef __HAVE_ARCH_STRSPN
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
519 size_t strspn(const char *s, const char *accept)
521 const char *p;
522 const char *a;
523 size_t count = 0;
525 for (p = s; *p != '\0'; ++p) {
526 for (a = accept; *a != '\0'; ++a) {
527 if (*p == *a)
528 break;
530 if (*a == '\0')
531 return count;
532 ++count;
534 return count;
537 EXPORT_SYMBOL(strspn);
538 #endif
540 #ifndef __HAVE_ARCH_STRCSPN
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
546 size_t strcspn(const char *s, const char *reject)
548 const char *p;
549 const char *r;
550 size_t count = 0;
552 for (p = s; *p != '\0'; ++p) {
553 for (r = reject; *r != '\0'; ++r) {
554 if (*p == *r)
555 return count;
557 ++count;
559 return count;
561 EXPORT_SYMBOL(strcspn);
562 #endif
564 #ifndef __HAVE_ARCH_STRPBRK
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
570 char *strpbrk(const char *cs, const char *ct)
572 const char *sc1, *sc2;
574 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
575 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
576 if (*sc1 == *sc2)
577 return (char *)sc1;
580 return NULL;
582 EXPORT_SYMBOL(strpbrk);
583 #endif
585 #ifndef __HAVE_ARCH_STRSEP
587 * strsep - Split a string into tokens
588 * @s: The string to be searched
589 * @ct: The characters to search for
591 * strsep() updates @s to point after the token, ready for the next call.
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. ;)
597 char *strsep(char **s, const char *ct)
599 char *sbegin = *s;
600 char *end;
602 if (sbegin == NULL)
603 return NULL;
605 end = strpbrk(sbegin, ct);
606 if (end)
607 *end++ = '\0';
608 *s = end;
609 return sbegin;
611 EXPORT_SYMBOL(strsep);
612 #endif
615 * sysfs_streq - return true if strings are equal, modulo trailing newline
616 * @s1: one string
617 * @s2: another string
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.
624 bool sysfs_streq(const char *s1, const char *s2)
626 while (*s1 && *s1 == *s2) {
627 s1++;
628 s2++;
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;
639 EXPORT_SYMBOL(sysfs_streq);
642 * strtobool - convert common user inputs into boolean values
643 * @s: input string
644 * @res: result
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.
650 int strtobool(const char *s, bool *res)
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;
666 return 0;
668 EXPORT_SYMBOL(strtobool);
670 #ifndef __HAVE_ARCH_MEMSET
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.
677 * Do not use memset() to access IO space, use memset_io() instead.
679 void *memset(void *s, int c, size_t count)
681 char *xs = s;
683 while (count--)
684 *xs++ = c;
685 return s;
687 EXPORT_SYMBOL(memset);
688 #endif
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.
696 * memzero_explicit() doesn't need an arch-specific version as
697 * it just invokes the one of memset() implicitly.
699 void memzero_explicit(void *s, size_t count)
701 memset(s, 0, count);
702 barrier();
704 EXPORT_SYMBOL(memzero_explicit);
706 #ifndef __HAVE_ARCH_MEMCPY
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.
713 * You should not use this function to access IO space, use memcpy_toio()
714 * or memcpy_fromio() instead.
716 void *memcpy(void *dest, const void *src, size_t count)
718 char *tmp = dest;
719 const char *s = src;
721 while (count--)
722 *tmp++ = *s++;
723 return dest;
725 EXPORT_SYMBOL(memcpy);
726 #endif
728 #ifndef __HAVE_ARCH_MEMMOVE
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.
735 * Unlike memcpy(), memmove() copes with overlapping areas.
737 void *memmove(void *dest, const void *src, size_t count)
739 char *tmp;
740 const char *s;
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;
755 return dest;
757 EXPORT_SYMBOL(memmove);
758 #endif
760 #ifndef __HAVE_ARCH_MEMCMP
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.
767 #undef memcmp
768 __visible int memcmp(const void *cs, const void *ct, size_t count)
770 const unsigned char *su1, *su2;
771 int res = 0;
773 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
774 if ((res = *su1 - *su2) != 0)
775 break;
776 return res;
778 EXPORT_SYMBOL(memcmp);
779 #endif
781 #ifndef __HAVE_ARCH_MEMSCAN
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.
788 * returns the address of the first occurrence of @c, or 1 byte past
789 * the area if @c is not found
791 void *memscan(void *addr, int c, size_t size)
793 unsigned char *p = addr;
795 while (size) {
796 if (*p == c)
797 return (void *)p;
798 p++;
799 size--;
801 return (void *)p;
803 EXPORT_SYMBOL(memscan);
804 #endif
806 #ifndef __HAVE_ARCH_STRSTR
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
812 char *strstr(const char *s1, const char *s2)
814 size_t l1, l2;
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++;
826 return NULL;
828 EXPORT_SYMBOL(strstr);
829 #endif
831 #ifndef __HAVE_ARCH_STRNSTR
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
838 char *strnstr(const char *s1, const char *s2, size_t len)
840 size_t l2;
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++;
851 return NULL;
853 EXPORT_SYMBOL(strnstr);
854 #endif
856 #ifndef __HAVE_ARCH_MEMCHR
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.
863 * returns the address of the first occurrence of @c, or %NULL
864 * if @c is not found
866 void *memchr(const void *s, int c, size_t n)
868 const unsigned char *p = s;
869 while (n-- != 0) {
870 if ((unsigned char)c == *p++) {
871 return (void *)(p - 1);
874 return NULL;
876 EXPORT_SYMBOL(memchr);
877 #endif
879 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
881 while (bytes) {
882 if (*start != value)
883 return (void *)start;
884 start++;
885 bytes--;
887 return NULL;
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.
896 * returns the address of the first character other than @c, or %NULL
897 * if the whole buffer contains just @c.
899 void *memchr_inv(const void *start, int c, size_t bytes)
901 u8 value = c;
902 u64 value64;
903 unsigned int words, prefix;
905 if (bytes <= 16)
906 return check_bytes8(start, value, bytes);
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
920 prefix = (unsigned long)start % 8;
921 if (prefix) {
922 u8 *r;
924 prefix = 8 - prefix;
925 r = check_bytes8(start, value, prefix);
926 if (r)
927 return r;
928 start += prefix;
929 bytes -= prefix;
932 words = bytes / 8;
934 while (words) {
935 if (*(u64 *)start != value64)
936 return check_bytes8(start, value, 8);
937 start += 8;
938 words--;
941 return check_bytes8(start, value, bytes % 8);
943 EXPORT_SYMBOL(memchr_inv);