blob: 233adeeec33999af0e9d030dd4022522564d71dd [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
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
18#include <linux/types.h>
19#include <linux/string.h>
20#include <linux/ctype.h>
21#include <malloc.h>
22
wdenkc6097192002-11-03 00:24:07 +000023
wdenkc6097192002-11-03 00:24:07 +000024/**
Simon Glassb1f17bf2012-12-05 14:46:35 +000025 * strncasecmp - Case insensitive, length-limited string comparison
wdenkc6097192002-11-03 00:24:07 +000026 * @s1: One string
27 * @s2: The other string
28 * @len: the maximum number of characters to compare
29 */
Simon Glassb1f17bf2012-12-05 14:46:35 +000030int strncasecmp(const char *s1, const char *s2, size_t len)
wdenkc6097192002-11-03 00:24:07 +000031{
32 /* Yes, Virginia, it had better be unsigned */
33 unsigned char c1, c2;
34
35 c1 = 0; c2 = 0;
36 if (len) {
37 do {
38 c1 = *s1; c2 = *s2;
39 s1++; s2++;
40 if (!c1)
41 break;
42 if (!c2)
43 break;
44 if (c1 == c2)
45 continue;
46 c1 = tolower(c1);
47 c2 = tolower(c2);
48 if (c1 != c2)
49 break;
50 } while (--len);
51 }
52 return (int)c1 - (int)c2;
53}
Simon Glassb1f17bf2012-12-05 14:46:35 +000054
55/**
56 * strcasecmp - Case insensitive string comparison
57 * @s1: One string
58 * @s2: The other string
59 */
60int strcasecmp(const char *s1, const char *s2)
61{
62 return strncasecmp(s1, s2, -1U);
63}
wdenkc6097192002-11-03 00:24:07 +000064
65char * ___strtok;
66
67#ifndef __HAVE_ARCH_STRCPY
68/**
69 * strcpy - Copy a %NUL terminated string
70 * @dest: Where to copy the string to
71 * @src: Where to copy the string from
72 */
73char * strcpy(char * dest,const char *src)
74{
75 char *tmp = dest;
76
77 while ((*dest++ = *src++) != '\0')
78 /* nothing */;
79 return tmp;
80}
81#endif
82
83#ifndef __HAVE_ARCH_STRNCPY
84/**
85 * strncpy - Copy a length-limited, %NUL-terminated string
86 * @dest: Where to copy the string to
87 * @src: Where to copy the string from
88 * @count: The maximum number of bytes to copy
89 *
90 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
91 * However, the result is not %NUL-terminated if the source exceeds
92 * @count bytes.
93 */
94char * strncpy(char * dest,const char *src,size_t count)
95{
96 char *tmp = dest;
97
98 while (count-- && (*dest++ = *src++) != '\0')
99 /* nothing */;
100
101 return tmp;
102}
103#endif
104
Masahiro Yamada80d9ef82014-11-20 21:20:32 +0900105#ifndef __HAVE_ARCH_STRLCPY
106/**
107 * strlcpy - Copy a C-string into a sized buffer
108 * @dest: Where to copy the string to
109 * @src: Where to copy the string from
110 * @size: size of destination buffer
111 *
112 * Compatible with *BSD: the result is always a valid
113 * NUL-terminated string that fits in the buffer (unless,
114 * of course, the buffer size is zero). It does not pad
115 * out the result like strncpy() does.
116 */
117size_t strlcpy(char *dest, const char *src, size_t size)
118{
119 size_t ret = strlen(src);
120
121 if (size) {
122 size_t len = (ret >= size) ? size - 1 : ret;
123 memcpy(dest, src, len);
124 dest[len] = '\0';
125 }
126 return ret;
127}
128#endif
129
wdenkc6097192002-11-03 00:24:07 +0000130#ifndef __HAVE_ARCH_STRCAT
131/**
132 * strcat - Append one %NUL-terminated string to another
133 * @dest: The string to be appended to
134 * @src: The string to append to it
135 */
136char * strcat(char * dest, const char * src)
137{
138 char *tmp = dest;
139
140 while (*dest)
141 dest++;
142 while ((*dest++ = *src++) != '\0')
143 ;
144
145 return tmp;
146}
147#endif
148
149#ifndef __HAVE_ARCH_STRNCAT
150/**
151 * strncat - Append a length-limited, %NUL-terminated string to another
152 * @dest: The string to be appended to
153 * @src: The string to append to it
154 * @count: The maximum numbers of bytes to copy
155 *
156 * Note that in contrast to strncpy, strncat ensures the result is
157 * terminated.
158 */
159char * strncat(char *dest, const char *src, size_t count)
160{
161 char *tmp = dest;
162
163 if (count) {
164 while (*dest)
165 dest++;
166 while ((*dest++ = *src++)) {
167 if (--count == 0) {
168 *dest = '\0';
169 break;
170 }
171 }
172 }
173
174 return tmp;
175}
176#endif
177
Md Sadre Alam9cdab512018-04-26 16:09:45 +0530178#ifndef __HAVE_ARCH_STRLCAT
179/**
180 * strlcat - Append a length-limited, %NUL-terminated string to another
181 * @dest: The string to be appended to
182 * @src: The string to append to it
183 * @count: The size of the destination buffer.
184 *
185 */
186size_t strlcat(char *dest, const char *src, size_t count)
187{
188 size_t dsize = strlen(dest);
189 size_t len = strlen(src);
190 size_t res = dsize + len;
191
192 /* This would be a bug */
193 BUG_ON(dsize >= count);
194
195 dest += dsize;
196 count -= dsize;
197 if (len >= count)
198 len = count-1;
199 memcpy(dest, src, len);
200 dest[len] = 0;
201 return res;
202}
Md Sadre Alam9cdab512018-04-26 16:09:45 +0530203#endif
204
wdenkc6097192002-11-03 00:24:07 +0000205#ifndef __HAVE_ARCH_STRCMP
206/**
207 * strcmp - Compare two strings
208 * @cs: One string
209 * @ct: Another string
210 */
211int strcmp(const char * cs,const char * ct)
212{
213 register signed char __res;
214
215 while (1) {
216 if ((__res = *cs - *ct++) != 0 || !*cs++)
217 break;
218 }
219
220 return __res;
221}
222#endif
223
224#ifndef __HAVE_ARCH_STRNCMP
225/**
226 * strncmp - Compare two length-limited strings
227 * @cs: One string
228 * @ct: Another string
229 * @count: The maximum number of bytes to compare
230 */
231int strncmp(const char * cs,const char * ct,size_t count)
232{
233 register signed char __res = 0;
234
235 while (count) {
236 if ((__res = *cs - *ct++) != 0 || !*cs++)
237 break;
238 count--;
239 }
240
241 return __res;
242}
243#endif
244
245#ifndef __HAVE_ARCH_STRCHR
246/**
247 * strchr - Find the first occurrence of a character in a string
248 * @s: The string to be searched
249 * @c: The character to search for
250 */
251char * strchr(const char * s, int c)
252{
253 for(; *s != (char) c; ++s)
254 if (*s == '\0')
255 return NULL;
256 return (char *) s;
257}
258#endif
259
260#ifndef __HAVE_ARCH_STRRCHR
261/**
262 * strrchr - Find the last occurrence of a character in a string
263 * @s: The string to be searched
264 * @c: The character to search for
265 */
266char * strrchr(const char * s, int c)
267{
268 const char *p = s + strlen(s);
269 do {
wdenk8bde7f72003-06-27 21:31:46 +0000270 if (*p == (char)c)
271 return (char *)p;
wdenkc6097192002-11-03 00:24:07 +0000272 } while (--p >= s);
273 return NULL;
274}
275#endif
276
277#ifndef __HAVE_ARCH_STRLEN
278/**
279 * strlen - Find the length of a string
280 * @s: The string to be sized
281 */
282size_t strlen(const char * s)
283{
284 const char *sc;
285
286 for (sc = s; *sc != '\0'; ++sc)
287 /* nothing */;
288 return sc - s;
289}
290#endif
291
292#ifndef __HAVE_ARCH_STRNLEN
293/**
294 * strnlen - Find the length of a length-limited string
295 * @s: The string to be sized
296 * @count: The maximum number of bytes to search
297 */
298size_t strnlen(const char * s, size_t count)
299{
300 const char *sc;
301
302 for (sc = s; count-- && *sc != '\0'; ++sc)
303 /* nothing */;
304 return sc - s;
305}
306#endif
307
308#ifndef __HAVE_ARCH_STRDUP
309char * strdup(const char *s)
310{
311 char *new;
312
313 if ((s == NULL) ||
314 ((new = malloc (strlen(s) + 1)) == NULL) ) {
315 return NULL;
316 }
317
318 strcpy (new, s);
319 return new;
320}
321#endif
322
323#ifndef __HAVE_ARCH_STRSPN
324/**
325 * strspn - Calculate the length of the initial substring of @s which only
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200326 * contain letters in @accept
wdenkc6097192002-11-03 00:24:07 +0000327 * @s: The string to be searched
328 * @accept: The string to search for
329 */
330size_t strspn(const char *s, const char *accept)
331{
332 const char *p;
333 const char *a;
334 size_t count = 0;
335
336 for (p = s; *p != '\0'; ++p) {
337 for (a = accept; *a != '\0'; ++a) {
338 if (*p == *a)
339 break;
340 }
341 if (*a == '\0')
342 return count;
343 ++count;
344 }
345
346 return count;
347}
348#endif
349
350#ifndef __HAVE_ARCH_STRPBRK
351/**
352 * strpbrk - Find the first occurrence of a set of characters
353 * @cs: The string to be searched
354 * @ct: The characters to search for
355 */
356char * strpbrk(const char * cs,const char * ct)
357{
358 const char *sc1,*sc2;
359
360 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
361 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
362 if (*sc1 == *sc2)
363 return (char *) sc1;
364 }
365 }
366 return NULL;
367}
368#endif
369
370#ifndef __HAVE_ARCH_STRTOK
371/**
372 * strtok - Split a string into tokens
373 * @s: The string to be searched
374 * @ct: The characters to search for
375 *
376 * WARNING: strtok is deprecated, use strsep instead.
377 */
378char * strtok(char * s,const char * ct)
379{
380 char *sbegin, *send;
381
382 sbegin = s ? s : ___strtok;
383 if (!sbegin) {
384 return NULL;
385 }
386 sbegin += strspn(sbegin,ct);
387 if (*sbegin == '\0') {
388 ___strtok = NULL;
389 return( NULL );
390 }
391 send = strpbrk( sbegin, ct);
392 if (send && *send != '\0')
393 *send++ = '\0';
394 ___strtok = send;
395 return (sbegin);
396}
397#endif
398
399#ifndef __HAVE_ARCH_STRSEP
400/**
401 * strsep - Split a string into tokens
402 * @s: The string to be searched
403 * @ct: The characters to search for
404 *
405 * strsep() updates @s to point after the token, ready for the next call.
406 *
407 * It returns empty tokens, too, behaving exactly like the libc function
408 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
409 * Same semantics, slimmer shape. ;)
410 */
411char * strsep(char **s, const char *ct)
412{
413 char *sbegin = *s, *end;
414
415 if (sbegin == NULL)
416 return NULL;
417
418 end = strpbrk(sbegin, ct);
419 if (end)
420 *end++ = '\0';
421 *s = end;
422
423 return sbegin;
424}
425#endif
426
wdenkc3f9d492004-03-14 00:59:59 +0000427#ifndef __HAVE_ARCH_STRSWAB
428/**
429 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
430 * s: address of the string
431 *
432 * returns the address of the swapped string or NULL on error. If
433 * string length is odd, last byte is untouched.
434 */
435char *strswab(const char *s)
436{
Wolfgang Denk389db1f2005-09-25 16:15:17 +0200437 char *p, *q;
wdenkc3f9d492004-03-14 00:59:59 +0000438
439 if ((NULL == s) || ('\0' == *s)) {
440 return (NULL);
441 }
442
Wolfgang Denke5e98ed2005-10-04 23:38:07 +0200443 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
wdenkc3f9d492004-03-14 00:59:59 +0000444 char tmp;
Wolfgang Denk389db1f2005-09-25 16:15:17 +0200445
446 tmp = *p;
447 *p = *q;
448 *q = tmp;
wdenkc3f9d492004-03-14 00:59:59 +0000449 }
450
451 return (char *) s;
452}
453#endif
454
wdenkc6097192002-11-03 00:24:07 +0000455#ifndef __HAVE_ARCH_MEMSET
456/**
457 * memset - Fill a region of memory with the given value
458 * @s: Pointer to the start of the area.
459 * @c: The byte to fill the area with
460 * @count: The size of the area.
461 *
462 * Do not use memset() to access IO space, use memset_io() instead.
463 */
464void * memset(void * s,int c,size_t count)
465{
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200466 unsigned long *sl = (unsigned long *) s;
467 unsigned long cl = 0;
468 char *s8;
469 int i;
wdenkc6097192002-11-03 00:24:07 +0000470
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200471 /* do it one word at a time (32 bits or 64 bits) while possible */
472 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
473 for (i = 0; i < sizeof(*sl); i++) {
474 cl <<= 8;
475 cl |= c & 0xff;
476 }
477 while (count >= sizeof(*sl)) {
478 *sl++ = cl;
479 count -= sizeof(*sl);
480 }
481 }
482 /* fill 8 bits at a time */
483 s8 = (char *)sl;
wdenkc6097192002-11-03 00:24:07 +0000484 while (count--)
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200485 *s8++ = c;
wdenkc6097192002-11-03 00:24:07 +0000486
487 return s;
488}
489#endif
490
491#ifndef __HAVE_ARCH_BCOPY
492/**
493 * bcopy - Copy one area of memory to another
494 * @src: Where to copy from
495 * @dest: Where to copy to
496 * @count: The size of the area.
497 *
498 * Note that this is the same as memcpy(), with the arguments reversed.
499 * memcpy() is the standard, bcopy() is a legacy BSD function.
500 *
501 * You should not use this function to access IO space, use memcpy_toio()
502 * or memcpy_fromio() instead.
503 */
504char * bcopy(const char * src, char * dest, int count)
505{
506 char *tmp = dest;
507
508 while (count--)
509 *tmp++ = *src++;
510
511 return dest;
512}
513#endif
514
515#ifndef __HAVE_ARCH_MEMCPY
516/**
517 * memcpy - Copy one area of memory to another
518 * @dest: Where to copy to
519 * @src: Where to copy from
520 * @count: The size of the area.
521 *
522 * You should not use this function to access IO space, use memcpy_toio()
523 * or memcpy_fromio() instead.
524 */
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200525void * memcpy(void *dest, const void *src, size_t count)
wdenkc6097192002-11-03 00:24:07 +0000526{
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200527 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
528 char *d8, *s8;
wdenkc6097192002-11-03 00:24:07 +0000529
Matthias Weisserb038db82011-05-22 23:03:55 +0000530 if (src == dest)
531 return dest;
532
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200533 /* while all data is aligned (common case), copy a word at a time */
534 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
535 while (count >= sizeof(*dl)) {
536 *dl++ = *sl++;
537 count -= sizeof(*dl);
538 }
539 }
540 /* copy the reset one byte at a time */
541 d8 = (char *)dl;
542 s8 = (char *)sl;
wdenkc6097192002-11-03 00:24:07 +0000543 while (count--)
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200544 *d8++ = *s8++;
wdenkc6097192002-11-03 00:24:07 +0000545
546 return dest;
547}
548#endif
549
Md Sadre Alam94028ba2018-04-26 16:51:43 +0530550#ifndef __HAVE_ARCH_MEMSCPY
551/**
552 * memscpy - Copy one area of memory to another
553 * @dest: Where to copy to
554 * @src: Where to copy from
555 * @dst_size: The size of destination buffer area
556 * @copy_size: The size of source buffer area
557 *
558 * You should not use this function where memcpy
559 * was used to copy null terminated buffer, the
560 * replacement function is strlcpy() and strlcat()
561 * depending on situation.
562 *
563 * The aim of memscpy() is to prevent buffer overflow
564 * by taking both destination buffer size and source
565 * buffer size.
566 */
567size_t memscpy(void *dest, size_t dst_size, const void *src, size_t copy_size)
568{
Md Sadre Alam9cdab512018-04-26 16:09:45 +0530569 size_t min_size = dst_size < copy_size ? dst_size : copy_size;
570 memcpy(dest, src, min_size);
571 return min_size;
572}
Md Sadre Alam94028ba2018-04-26 16:51:43 +0530573#endif
Md Sadre Alam9cdab512018-04-26 16:09:45 +0530574
wdenkc6097192002-11-03 00:24:07 +0000575#ifndef __HAVE_ARCH_MEMMOVE
576/**
577 * memmove - Copy one area of memory to another
578 * @dest: Where to copy to
579 * @src: Where to copy from
580 * @count: The size of the area.
581 *
582 * Unlike memcpy(), memmove() copes with overlapping areas.
583 */
584void * memmove(void * dest,const void *src,size_t count)
585{
586 char *tmp, *s;
587
Matthias Weisserb038db82011-05-22 23:03:55 +0000588 if (src == dest)
589 return dest;
590
wdenkc6097192002-11-03 00:24:07 +0000591 if (dest <= src) {
592 tmp = (char *) dest;
593 s = (char *) src;
594 while (count--)
595 *tmp++ = *s++;
596 }
597 else {
598 tmp = (char *) dest + count;
599 s = (char *) src + count;
600 while (count--)
601 *--tmp = *--s;
602 }
603
604 return dest;
605}
606#endif
607
608#ifndef __HAVE_ARCH_MEMCMP
609/**
610 * memcmp - Compare two areas of memory
611 * @cs: One area of memory
612 * @ct: Another area of memory
613 * @count: The size of the area.
614 */
615int memcmp(const void * cs,const void * ct,size_t count)
616{
617 const unsigned char *su1, *su2;
618 int res = 0;
619
620 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
621 if ((res = *su1 - *su2) != 0)
622 break;
623 return res;
624}
625#endif
626
627#ifndef __HAVE_ARCH_MEMSCAN
628/**
629 * memscan - Find a character in an area of memory.
630 * @addr: The memory area
631 * @c: The byte to search for
632 * @size: The size of the area.
633 *
634 * returns the address of the first occurrence of @c, or 1 byte past
635 * the area if @c is not found
636 */
637void * memscan(void * addr, int c, size_t size)
638{
639 unsigned char * p = (unsigned char *) addr;
640
641 while (size) {
642 if (*p == c)
643 return (void *) p;
644 p++;
645 size--;
646 }
wdenk8bde7f72003-06-27 21:31:46 +0000647 return (void *) p;
wdenkc6097192002-11-03 00:24:07 +0000648}
649#endif
650
651#ifndef __HAVE_ARCH_STRSTR
652/**
653 * strstr - Find the first substring in a %NUL terminated string
654 * @s1: The string to be searched
655 * @s2: The string to search for
656 */
657char * strstr(const char * s1,const char * s2)
658{
659 int l1, l2;
660
661 l2 = strlen(s2);
662 if (!l2)
663 return (char *) s1;
664 l1 = strlen(s1);
665 while (l1 >= l2) {
666 l1--;
667 if (!memcmp(s1,s2,l2))
668 return (char *) s1;
669 s1++;
670 }
671 return NULL;
672}
673#endif
674
675#ifndef __HAVE_ARCH_MEMCHR
676/**
677 * memchr - Find a character in an area of memory.
678 * @s: The memory area
679 * @c: The byte to search for
680 * @n: The size of the area.
681 *
682 * returns the address of the first occurrence of @c, or %NULL
683 * if @c is not found
684 */
685void *memchr(const void *s, int c, size_t n)
686{
687 const unsigned char *p = s;
688 while (n-- != 0) {
wdenk8bde7f72003-06-27 21:31:46 +0000689 if ((unsigned char)c == *p++) {
wdenkc6097192002-11-03 00:24:07 +0000690 return (void *)(p-1);
691 }
692 }
693 return NULL;
694}
695
696#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000697#ifndef __HAVE_ARCH_MEMCHR_INV
698static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
699{
700 while (bytes) {
701 if (*start != value)
702 return (void *)start;
703 start++;
704 bytes--;
705 }
706 return NULL;
707}
708/**
709 * memchr_inv - Find an unmatching character in an area of memory.
710 * @start: The memory area
711 * @c: Find a character other than c
712 * @bytes: The size of the area.
713 *
714 * returns the address of the first character other than @c, or %NULL
715 * if the whole buffer contains just @c.
716 */
717void *memchr_inv(const void *start, int c, size_t bytes)
718{
719 u8 value = c;
720 u64 value64;
721 unsigned int words, prefix;
722
723 if (bytes <= 16)
724 return check_bytes8(start, value, bytes);
725
726 value64 = value;
727 value64 |= value64 << 8;
728 value64 |= value64 << 16;
729 value64 |= value64 << 32;
730
731 prefix = (unsigned long)start % 8;
732 if (prefix) {
733 u8 *r;
734
735 prefix = 8 - prefix;
736 r = check_bytes8(start, value, prefix);
737 if (r)
738 return r;
739 start += prefix;
740 bytes -= prefix;
741 }
742
743 words = bytes / 8;
744
745 while (words) {
746 if (*(u64 *)start != value64)
747 return check_bytes8(start, value, 8);
748 start += 8;
749 words--;
750 }
751
752 return check_bytes8(start, value, bytes % 8);
753}
754#endif