blob: 0dec46ccadf7c4d3f269776a5367ef5b61f478a6 [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}
203EXPORT_SYMBOL(strlcat);
204#endif
205
wdenkc6097192002-11-03 00:24:07 +0000206#ifndef __HAVE_ARCH_STRCMP
207/**
208 * strcmp - Compare two strings
209 * @cs: One string
210 * @ct: Another string
211 */
212int strcmp(const char * cs,const char * ct)
213{
214 register signed char __res;
215
216 while (1) {
217 if ((__res = *cs - *ct++) != 0 || !*cs++)
218 break;
219 }
220
221 return __res;
222}
223#endif
224
225#ifndef __HAVE_ARCH_STRNCMP
226/**
227 * strncmp - Compare two length-limited strings
228 * @cs: One string
229 * @ct: Another string
230 * @count: The maximum number of bytes to compare
231 */
232int strncmp(const char * cs,const char * ct,size_t count)
233{
234 register signed char __res = 0;
235
236 while (count) {
237 if ((__res = *cs - *ct++) != 0 || !*cs++)
238 break;
239 count--;
240 }
241
242 return __res;
243}
244#endif
245
246#ifndef __HAVE_ARCH_STRCHR
247/**
248 * strchr - Find the first occurrence of a character in a string
249 * @s: The string to be searched
250 * @c: The character to search for
251 */
252char * strchr(const char * s, int c)
253{
254 for(; *s != (char) c; ++s)
255 if (*s == '\0')
256 return NULL;
257 return (char *) s;
258}
259#endif
260
261#ifndef __HAVE_ARCH_STRRCHR
262/**
263 * strrchr - Find the last occurrence of a character in a string
264 * @s: The string to be searched
265 * @c: The character to search for
266 */
267char * strrchr(const char * s, int c)
268{
269 const char *p = s + strlen(s);
270 do {
wdenk8bde7f72003-06-27 21:31:46 +0000271 if (*p == (char)c)
272 return (char *)p;
wdenkc6097192002-11-03 00:24:07 +0000273 } while (--p >= s);
274 return NULL;
275}
276#endif
277
278#ifndef __HAVE_ARCH_STRLEN
279/**
280 * strlen - Find the length of a string
281 * @s: The string to be sized
282 */
283size_t strlen(const char * s)
284{
285 const char *sc;
286
287 for (sc = s; *sc != '\0'; ++sc)
288 /* nothing */;
289 return sc - s;
290}
291#endif
292
293#ifndef __HAVE_ARCH_STRNLEN
294/**
295 * strnlen - Find the length of a length-limited string
296 * @s: The string to be sized
297 * @count: The maximum number of bytes to search
298 */
299size_t strnlen(const char * s, size_t count)
300{
301 const char *sc;
302
303 for (sc = s; count-- && *sc != '\0'; ++sc)
304 /* nothing */;
305 return sc - s;
306}
307#endif
308
309#ifndef __HAVE_ARCH_STRDUP
310char * strdup(const char *s)
311{
312 char *new;
313
314 if ((s == NULL) ||
315 ((new = malloc (strlen(s) + 1)) == NULL) ) {
316 return NULL;
317 }
318
319 strcpy (new, s);
320 return new;
321}
322#endif
323
324#ifndef __HAVE_ARCH_STRSPN
325/**
326 * strspn - Calculate the length of the initial substring of @s which only
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200327 * contain letters in @accept
wdenkc6097192002-11-03 00:24:07 +0000328 * @s: The string to be searched
329 * @accept: The string to search for
330 */
331size_t strspn(const char *s, const char *accept)
332{
333 const char *p;
334 const char *a;
335 size_t count = 0;
336
337 for (p = s; *p != '\0'; ++p) {
338 for (a = accept; *a != '\0'; ++a) {
339 if (*p == *a)
340 break;
341 }
342 if (*a == '\0')
343 return count;
344 ++count;
345 }
346
347 return count;
348}
349#endif
350
351#ifndef __HAVE_ARCH_STRPBRK
352/**
353 * strpbrk - Find the first occurrence of a set of characters
354 * @cs: The string to be searched
355 * @ct: The characters to search for
356 */
357char * strpbrk(const char * cs,const char * ct)
358{
359 const char *sc1,*sc2;
360
361 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
362 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
363 if (*sc1 == *sc2)
364 return (char *) sc1;
365 }
366 }
367 return NULL;
368}
369#endif
370
371#ifndef __HAVE_ARCH_STRTOK
372/**
373 * strtok - Split a string into tokens
374 * @s: The string to be searched
375 * @ct: The characters to search for
376 *
377 * WARNING: strtok is deprecated, use strsep instead.
378 */
379char * strtok(char * s,const char * ct)
380{
381 char *sbegin, *send;
382
383 sbegin = s ? s : ___strtok;
384 if (!sbegin) {
385 return NULL;
386 }
387 sbegin += strspn(sbegin,ct);
388 if (*sbegin == '\0') {
389 ___strtok = NULL;
390 return( NULL );
391 }
392 send = strpbrk( sbegin, ct);
393 if (send && *send != '\0')
394 *send++ = '\0';
395 ___strtok = send;
396 return (sbegin);
397}
398#endif
399
400#ifndef __HAVE_ARCH_STRSEP
401/**
402 * strsep - Split a string into tokens
403 * @s: The string to be searched
404 * @ct: The characters to search for
405 *
406 * strsep() updates @s to point after the token, ready for the next call.
407 *
408 * It returns empty tokens, too, behaving exactly like the libc function
409 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
410 * Same semantics, slimmer shape. ;)
411 */
412char * strsep(char **s, const char *ct)
413{
414 char *sbegin = *s, *end;
415
416 if (sbegin == NULL)
417 return NULL;
418
419 end = strpbrk(sbegin, ct);
420 if (end)
421 *end++ = '\0';
422 *s = end;
423
424 return sbegin;
425}
426#endif
427
wdenkc3f9d492004-03-14 00:59:59 +0000428#ifndef __HAVE_ARCH_STRSWAB
429/**
430 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
431 * s: address of the string
432 *
433 * returns the address of the swapped string or NULL on error. If
434 * string length is odd, last byte is untouched.
435 */
436char *strswab(const char *s)
437{
Wolfgang Denk389db1f2005-09-25 16:15:17 +0200438 char *p, *q;
wdenkc3f9d492004-03-14 00:59:59 +0000439
440 if ((NULL == s) || ('\0' == *s)) {
441 return (NULL);
442 }
443
Wolfgang Denke5e98ed2005-10-04 23:38:07 +0200444 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
wdenkc3f9d492004-03-14 00:59:59 +0000445 char tmp;
Wolfgang Denk389db1f2005-09-25 16:15:17 +0200446
447 tmp = *p;
448 *p = *q;
449 *q = tmp;
wdenkc3f9d492004-03-14 00:59:59 +0000450 }
451
452 return (char *) s;
453}
454#endif
455
wdenkc6097192002-11-03 00:24:07 +0000456#ifndef __HAVE_ARCH_MEMSET
457/**
458 * memset - Fill a region of memory with the given value
459 * @s: Pointer to the start of the area.
460 * @c: The byte to fill the area with
461 * @count: The size of the area.
462 *
463 * Do not use memset() to access IO space, use memset_io() instead.
464 */
465void * memset(void * s,int c,size_t count)
466{
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200467 unsigned long *sl = (unsigned long *) s;
468 unsigned long cl = 0;
469 char *s8;
470 int i;
wdenkc6097192002-11-03 00:24:07 +0000471
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200472 /* do it one word at a time (32 bits or 64 bits) while possible */
473 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
474 for (i = 0; i < sizeof(*sl); i++) {
475 cl <<= 8;
476 cl |= c & 0xff;
477 }
478 while (count >= sizeof(*sl)) {
479 *sl++ = cl;
480 count -= sizeof(*sl);
481 }
482 }
483 /* fill 8 bits at a time */
484 s8 = (char *)sl;
wdenkc6097192002-11-03 00:24:07 +0000485 while (count--)
Alessandro Rubinie3ea9482009-10-10 11:51:16 +0200486 *s8++ = c;
wdenkc6097192002-11-03 00:24:07 +0000487
488 return s;
489}
490#endif
491
492#ifndef __HAVE_ARCH_BCOPY
493/**
494 * bcopy - Copy one area of memory to another
495 * @src: Where to copy from
496 * @dest: Where to copy to
497 * @count: The size of the area.
498 *
499 * Note that this is the same as memcpy(), with the arguments reversed.
500 * memcpy() is the standard, bcopy() is a legacy BSD function.
501 *
502 * You should not use this function to access IO space, use memcpy_toio()
503 * or memcpy_fromio() instead.
504 */
505char * bcopy(const char * src, char * dest, int count)
506{
507 char *tmp = dest;
508
509 while (count--)
510 *tmp++ = *src++;
511
512 return dest;
513}
514#endif
515
516#ifndef __HAVE_ARCH_MEMCPY
517/**
518 * memcpy - Copy one area of memory to another
519 * @dest: Where to copy to
520 * @src: Where to copy from
521 * @count: The size of the area.
522 *
523 * You should not use this function to access IO space, use memcpy_toio()
524 * or memcpy_fromio() instead.
525 */
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200526void * memcpy(void *dest, const void *src, size_t count)
wdenkc6097192002-11-03 00:24:07 +0000527{
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200528 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
529 char *d8, *s8;
wdenkc6097192002-11-03 00:24:07 +0000530
Matthias Weisserb038db82011-05-22 23:03:55 +0000531 if (src == dest)
532 return dest;
533
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200534 /* while all data is aligned (common case), copy a word at a time */
535 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
536 while (count >= sizeof(*dl)) {
537 *dl++ = *sl++;
538 count -= sizeof(*dl);
539 }
540 }
541 /* copy the reset one byte at a time */
542 d8 = (char *)dl;
543 s8 = (char *)sl;
wdenkc6097192002-11-03 00:24:07 +0000544 while (count--)
Alessandro Rubiniecd830b2009-10-10 11:51:05 +0200545 *d8++ = *s8++;
wdenkc6097192002-11-03 00:24:07 +0000546
547 return dest;
548}
549#endif
550
Md Sadre Alam9cdab512018-04-26 16:09:45 +0530551size_t memscpy(void *dest, size_t dst_size, const void *src, size_t copy_size) {
552 size_t min_size = dst_size < copy_size ? dst_size : copy_size;
553 memcpy(dest, src, min_size);
554 return min_size;
555}
556
wdenkc6097192002-11-03 00:24:07 +0000557#ifndef __HAVE_ARCH_MEMMOVE
558/**
559 * memmove - Copy one area of memory to another
560 * @dest: Where to copy to
561 * @src: Where to copy from
562 * @count: The size of the area.
563 *
564 * Unlike memcpy(), memmove() copes with overlapping areas.
565 */
566void * memmove(void * dest,const void *src,size_t count)
567{
568 char *tmp, *s;
569
Matthias Weisserb038db82011-05-22 23:03:55 +0000570 if (src == dest)
571 return dest;
572
wdenkc6097192002-11-03 00:24:07 +0000573 if (dest <= src) {
574 tmp = (char *) dest;
575 s = (char *) src;
576 while (count--)
577 *tmp++ = *s++;
578 }
579 else {
580 tmp = (char *) dest + count;
581 s = (char *) src + count;
582 while (count--)
583 *--tmp = *--s;
584 }
585
586 return dest;
587}
588#endif
589
590#ifndef __HAVE_ARCH_MEMCMP
591/**
592 * memcmp - Compare two areas of memory
593 * @cs: One area of memory
594 * @ct: Another area of memory
595 * @count: The size of the area.
596 */
597int memcmp(const void * cs,const void * ct,size_t count)
598{
599 const unsigned char *su1, *su2;
600 int res = 0;
601
602 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
603 if ((res = *su1 - *su2) != 0)
604 break;
605 return res;
606}
607#endif
608
609#ifndef __HAVE_ARCH_MEMSCAN
610/**
611 * memscan - Find a character in an area of memory.
612 * @addr: The memory area
613 * @c: The byte to search for
614 * @size: The size of the area.
615 *
616 * returns the address of the first occurrence of @c, or 1 byte past
617 * the area if @c is not found
618 */
619void * memscan(void * addr, int c, size_t size)
620{
621 unsigned char * p = (unsigned char *) addr;
622
623 while (size) {
624 if (*p == c)
625 return (void *) p;
626 p++;
627 size--;
628 }
wdenk8bde7f72003-06-27 21:31:46 +0000629 return (void *) p;
wdenkc6097192002-11-03 00:24:07 +0000630}
631#endif
632
633#ifndef __HAVE_ARCH_STRSTR
634/**
635 * strstr - Find the first substring in a %NUL terminated string
636 * @s1: The string to be searched
637 * @s2: The string to search for
638 */
639char * strstr(const char * s1,const char * s2)
640{
641 int l1, l2;
642
643 l2 = strlen(s2);
644 if (!l2)
645 return (char *) s1;
646 l1 = strlen(s1);
647 while (l1 >= l2) {
648 l1--;
649 if (!memcmp(s1,s2,l2))
650 return (char *) s1;
651 s1++;
652 }
653 return NULL;
654}
655#endif
656
657#ifndef __HAVE_ARCH_MEMCHR
658/**
659 * memchr - Find a character in an area of memory.
660 * @s: The memory area
661 * @c: The byte to search for
662 * @n: The size of the area.
663 *
664 * returns the address of the first occurrence of @c, or %NULL
665 * if @c is not found
666 */
667void *memchr(const void *s, int c, size_t n)
668{
669 const unsigned char *p = s;
670 while (n-- != 0) {
wdenk8bde7f72003-06-27 21:31:46 +0000671 if ((unsigned char)c == *p++) {
wdenkc6097192002-11-03 00:24:07 +0000672 return (void *)(p-1);
673 }
674 }
675 return NULL;
676}
677
678#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000679#ifndef __HAVE_ARCH_MEMCHR_INV
680static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
681{
682 while (bytes) {
683 if (*start != value)
684 return (void *)start;
685 start++;
686 bytes--;
687 }
688 return NULL;
689}
690/**
691 * memchr_inv - Find an unmatching character in an area of memory.
692 * @start: The memory area
693 * @c: Find a character other than c
694 * @bytes: The size of the area.
695 *
696 * returns the address of the first character other than @c, or %NULL
697 * if the whole buffer contains just @c.
698 */
699void *memchr_inv(const void *start, int c, size_t bytes)
700{
701 u8 value = c;
702 u64 value64;
703 unsigned int words, prefix;
704
705 if (bytes <= 16)
706 return check_bytes8(start, value, bytes);
707
708 value64 = value;
709 value64 |= value64 << 8;
710 value64 |= value64 << 16;
711 value64 |= value64 << 32;
712
713 prefix = (unsigned long)start % 8;
714 if (prefix) {
715 u8 *r;
716
717 prefix = 8 - prefix;
718 r = check_bytes8(start, value, prefix);
719 if (r)
720 return r;
721 start += prefix;
722 bytes -= prefix;
723 }
724
725 words = bytes / 8;
726
727 while (words) {
728 if (*(u64 *)start != value64)
729 return check_bytes8(start, value, 8);
730 start += 8;
731 words--;
732 }
733
734 return check_bytes8(start, value, bytes % 8);
735}
736#endif