blob: 1b867ac09d0747cfaa43a8b6e7b02c8190c903aa [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
Simon Glass43e571d2018-11-06 15:21:38 -070018#include <config.h>
wdenkc6097192002-11-03 00:24:07 +000019#include <linux/types.h>
20#include <linux/string.h>
21#include <linux/ctype.h>
22#include <malloc.h>
23
wdenkc6097192002-11-03 00:24:07 +000024
wdenkc6097192002-11-03 00:24:07 +000025/**
Simon Glass459af692012-12-05 14:46:35 +000026 * strncasecmp - Case insensitive, length-limited string comparison
wdenkc6097192002-11-03 00:24:07 +000027 * @s1: One string
28 * @s2: The other string
29 * @len: the maximum number of characters to compare
30 */
Simon Glass459af692012-12-05 14:46:35 +000031int strncasecmp(const char *s1, const char *s2, size_t len)
wdenkc6097192002-11-03 00:24:07 +000032{
33 /* Yes, Virginia, it had better be unsigned */
34 unsigned char c1, c2;
35
36 c1 = 0; c2 = 0;
37 if (len) {
38 do {
39 c1 = *s1; c2 = *s2;
40 s1++; s2++;
41 if (!c1)
42 break;
43 if (!c2)
44 break;
45 if (c1 == c2)
46 continue;
47 c1 = tolower(c1);
48 c2 = tolower(c2);
49 if (c1 != c2)
50 break;
51 } while (--len);
52 }
53 return (int)c1 - (int)c2;
54}
Simon Glass459af692012-12-05 14:46:35 +000055
56/**
57 * strcasecmp - Case insensitive string comparison
58 * @s1: One string
59 * @s2: The other string
60 */
61int strcasecmp(const char *s1, const char *s2)
62{
63 return strncasecmp(s1, s2, -1U);
64}
wdenkc6097192002-11-03 00:24:07 +000065
66char * ___strtok;
67
68#ifndef __HAVE_ARCH_STRCPY
69/**
70 * strcpy - Copy a %NUL terminated string
71 * @dest: Where to copy the string to
72 * @src: Where to copy the string from
73 */
74char * strcpy(char * dest,const char *src)
75{
76 char *tmp = dest;
77
78 while ((*dest++ = *src++) != '\0')
79 /* nothing */;
80 return tmp;
81}
82#endif
83
84#ifndef __HAVE_ARCH_STRNCPY
85/**
86 * strncpy - Copy a length-limited, %NUL-terminated string
87 * @dest: Where to copy the string to
88 * @src: Where to copy the string from
89 * @count: The maximum number of bytes to copy
90 *
91 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
92 * However, the result is not %NUL-terminated if the source exceeds
93 * @count bytes.
94 */
95char * strncpy(char * dest,const char *src,size_t count)
96{
97 char *tmp = dest;
98
99 while (count-- && (*dest++ = *src++) != '\0')
100 /* nothing */;
101
102 return tmp;
103}
104#endif
105
Masahiro Yamada0588ce12014-11-20 21:20:32 +0900106#ifndef __HAVE_ARCH_STRLCPY
107/**
108 * strlcpy - Copy a C-string into a sized buffer
109 * @dest: Where to copy the string to
110 * @src: Where to copy the string from
111 * @size: size of destination buffer
112 *
113 * Compatible with *BSD: the result is always a valid
114 * NUL-terminated string that fits in the buffer (unless,
115 * of course, the buffer size is zero). It does not pad
116 * out the result like strncpy() does.
Sean Anderson2f512572021-03-11 00:15:41 -0500117 *
118 * Return: the number of bytes copied
Masahiro Yamada0588ce12014-11-20 21:20:32 +0900119 */
120size_t strlcpy(char *dest, const char *src, size_t size)
121{
Masahiro Yamada0588ce12014-11-20 21:20:32 +0900122 if (size) {
Sean Anderson2f512572021-03-11 00:15:41 -0500123 size_t srclen = strlen(src);
124 size_t len = (srclen >= size) ? size - 1 : srclen;
125
Masahiro Yamada0588ce12014-11-20 21:20:32 +0900126 memcpy(dest, src, len);
127 dest[len] = '\0';
Sean Anderson2f512572021-03-11 00:15:41 -0500128 return len + 1;
Masahiro Yamada0588ce12014-11-20 21:20:32 +0900129 }
Sean Anderson2f512572021-03-11 00:15:41 -0500130
131 return 0;
Masahiro Yamada0588ce12014-11-20 21:20:32 +0900132}
133#endif
134
wdenkc6097192002-11-03 00:24:07 +0000135#ifndef __HAVE_ARCH_STRCAT
136/**
137 * strcat - Append one %NUL-terminated string to another
138 * @dest: The string to be appended to
139 * @src: The string to append to it
140 */
141char * strcat(char * dest, const char * src)
142{
143 char *tmp = dest;
144
145 while (*dest)
146 dest++;
147 while ((*dest++ = *src++) != '\0')
148 ;
149
150 return tmp;
151}
152#endif
153
154#ifndef __HAVE_ARCH_STRNCAT
155/**
156 * strncat - Append a length-limited, %NUL-terminated string to another
157 * @dest: The string to be appended to
158 * @src: The string to append to it
159 * @count: The maximum numbers of bytes to copy
160 *
161 * Note that in contrast to strncpy, strncat ensures the result is
162 * terminated.
163 */
164char * strncat(char *dest, const char *src, size_t count)
165{
166 char *tmp = dest;
167
168 if (count) {
169 while (*dest)
170 dest++;
171 while ((*dest++ = *src++)) {
172 if (--count == 0) {
173 *dest = '\0';
174 break;
175 }
176 }
177 }
178
179 return tmp;
180}
181#endif
182
183#ifndef __HAVE_ARCH_STRCMP
184/**
185 * strcmp - Compare two strings
186 * @cs: One string
187 * @ct: Another string
188 */
189int strcmp(const char * cs,const char * ct)
190{
191 register signed char __res;
192
193 while (1) {
194 if ((__res = *cs - *ct++) != 0 || !*cs++)
195 break;
196 }
197
198 return __res;
199}
200#endif
201
202#ifndef __HAVE_ARCH_STRNCMP
203/**
204 * strncmp - Compare two length-limited strings
205 * @cs: One string
206 * @ct: Another string
207 * @count: The maximum number of bytes to compare
208 */
209int strncmp(const char * cs,const char * ct,size_t count)
210{
211 register signed char __res = 0;
212
213 while (count) {
214 if ((__res = *cs - *ct++) != 0 || !*cs++)
215 break;
216 count--;
217 }
218
219 return __res;
220}
221#endif
222
223#ifndef __HAVE_ARCH_STRCHR
224/**
225 * strchr - Find the first occurrence of a character in a string
226 * @s: The string to be searched
227 * @c: The character to search for
228 */
229char * strchr(const char * s, int c)
230{
231 for(; *s != (char) c; ++s)
232 if (*s == '\0')
233 return NULL;
234 return (char *) s;
235}
236#endif
237
Simon Glass4355d422017-05-18 20:09:28 -0600238const char *strchrnul(const char *s, int c)
239{
240 for (; *s != (char)c; ++s)
241 if (*s == '\0')
242 break;
243 return s;
244}
245
wdenkc6097192002-11-03 00:24:07 +0000246#ifndef __HAVE_ARCH_STRRCHR
247/**
248 * strrchr - Find the last occurrence of a character in a string
249 * @s: The string to be searched
250 * @c: The character to search for
251 */
252char * strrchr(const char * s, int c)
253{
254 const char *p = s + strlen(s);
255 do {
wdenk57b2d802003-06-27 21:31:46 +0000256 if (*p == (char)c)
257 return (char *)p;
wdenkc6097192002-11-03 00:24:07 +0000258 } while (--p >= s);
259 return NULL;
260}
261#endif
262
263#ifndef __HAVE_ARCH_STRLEN
264/**
265 * strlen - Find the length of a string
266 * @s: The string to be sized
267 */
268size_t strlen(const char * s)
269{
270 const char *sc;
271
272 for (sc = s; *sc != '\0'; ++sc)
273 /* nothing */;
274 return sc - s;
275}
276#endif
277
278#ifndef __HAVE_ARCH_STRNLEN
279/**
280 * strnlen - Find the length of a length-limited string
281 * @s: The string to be sized
282 * @count: The maximum number of bytes to search
283 */
284size_t strnlen(const char * s, size_t count)
285{
286 const char *sc;
287
288 for (sc = s; count-- && *sc != '\0'; ++sc)
289 /* nothing */;
290 return sc - s;
291}
292#endif
293
Simon Glass0d0e3c92017-05-18 20:09:29 -0600294#ifndef __HAVE_ARCH_STRCSPN
295/**
296 * strcspn - Calculate the length of the initial substring of @s which does
297 * not contain letters in @reject
298 * @s: The string to be searched
299 * @reject: The string to avoid
300 */
301size_t strcspn(const char *s, const char *reject)
302{
303 const char *p;
304 const char *r;
305 size_t count = 0;
306
307 for (p = s; *p != '\0'; ++p) {
308 for (r = reject; *r != '\0'; ++r) {
309 if (*p == *r)
310 return count;
311 }
312 ++count;
313 }
314 return count;
315}
316#endif
317
wdenkc6097192002-11-03 00:24:07 +0000318#ifndef __HAVE_ARCH_STRDUP
319char * strdup(const char *s)
320{
321 char *new;
322
323 if ((s == NULL) ||
324 ((new = malloc (strlen(s) + 1)) == NULL) ) {
325 return NULL;
326 }
327
328 strcpy (new, s);
329 return new;
330}
wdenkc6097192002-11-03 00:24:07 +0000331
Thierry Redingf0561822019-04-15 11:32:14 +0200332char * strndup(const char *s, size_t n)
333{
334 size_t len;
335 char *new;
336
337 if (s == NULL)
338 return NULL;
339
340 len = strlen(s);
341
342 if (n < len)
343 len = n;
344
345 new = malloc(len + 1);
346 if (new == NULL)
347 return NULL;
348
349 strncpy(new, s, len);
350 new[len] = '\0';
351
352 return new;
353}
Simon Glass9e8f6d62020-02-03 07:36:00 -0700354#endif
Thierry Redingf0561822019-04-15 11:32:14 +0200355
wdenkc6097192002-11-03 00:24:07 +0000356#ifndef __HAVE_ARCH_STRSPN
357/**
358 * strspn - Calculate the length of the initial substring of @s which only
Wolfgang Denka1be4762008-05-20 16:00:29 +0200359 * contain letters in @accept
wdenkc6097192002-11-03 00:24:07 +0000360 * @s: The string to be searched
361 * @accept: The string to search for
362 */
363size_t strspn(const char *s, const char *accept)
364{
365 const char *p;
366 const char *a;
367 size_t count = 0;
368
369 for (p = s; *p != '\0'; ++p) {
370 for (a = accept; *a != '\0'; ++a) {
371 if (*p == *a)
372 break;
373 }
374 if (*a == '\0')
375 return count;
376 ++count;
377 }
378
379 return count;
380}
381#endif
382
383#ifndef __HAVE_ARCH_STRPBRK
384/**
385 * strpbrk - Find the first occurrence of a set of characters
386 * @cs: The string to be searched
387 * @ct: The characters to search for
388 */
389char * strpbrk(const char * cs,const char * ct)
390{
391 const char *sc1,*sc2;
392
393 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
394 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
395 if (*sc1 == *sc2)
396 return (char *) sc1;
397 }
398 }
399 return NULL;
400}
401#endif
402
403#ifndef __HAVE_ARCH_STRTOK
404/**
405 * strtok - Split a string into tokens
406 * @s: The string to be searched
407 * @ct: The characters to search for
408 *
409 * WARNING: strtok is deprecated, use strsep instead.
410 */
411char * strtok(char * s,const char * ct)
412{
413 char *sbegin, *send;
414
415 sbegin = s ? s : ___strtok;
416 if (!sbegin) {
417 return NULL;
418 }
419 sbegin += strspn(sbegin,ct);
420 if (*sbegin == '\0') {
421 ___strtok = NULL;
422 return( NULL );
423 }
424 send = strpbrk( sbegin, ct);
425 if (send && *send != '\0')
426 *send++ = '\0';
427 ___strtok = send;
428 return (sbegin);
429}
430#endif
431
432#ifndef __HAVE_ARCH_STRSEP
433/**
434 * strsep - Split a string into tokens
435 * @s: The string to be searched
436 * @ct: The characters to search for
437 *
438 * strsep() updates @s to point after the token, ready for the next call.
439 *
440 * It returns empty tokens, too, behaving exactly like the libc function
441 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
442 * Same semantics, slimmer shape. ;)
443 */
444char * strsep(char **s, const char *ct)
445{
446 char *sbegin = *s, *end;
447
448 if (sbegin == NULL)
449 return NULL;
450
451 end = strpbrk(sbegin, ct);
452 if (end)
453 *end++ = '\0';
454 *s = end;
455
456 return sbegin;
457}
458#endif
459
wdenkacd9b102004-03-14 00:59:59 +0000460#ifndef __HAVE_ARCH_STRSWAB
461/**
462 * strswab - swap adjacent even and odd bytes in %NUL-terminated string
463 * s: address of the string
464 *
465 * returns the address of the swapped string or NULL on error. If
466 * string length is odd, last byte is untouched.
467 */
468char *strswab(const char *s)
469{
Wolfgang Denkc7ca7cf2005-09-25 16:15:17 +0200470 char *p, *q;
wdenkacd9b102004-03-14 00:59:59 +0000471
472 if ((NULL == s) || ('\0' == *s)) {
473 return (NULL);
474 }
475
Wolfgang Denkdf70a422005-10-04 23:38:07 +0200476 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
wdenkacd9b102004-03-14 00:59:59 +0000477 char tmp;
Wolfgang Denkc7ca7cf2005-09-25 16:15:17 +0200478
479 tmp = *p;
480 *p = *q;
481 *q = tmp;
wdenkacd9b102004-03-14 00:59:59 +0000482 }
483
484 return (char *) s;
485}
486#endif
487
wdenkc6097192002-11-03 00:24:07 +0000488#ifndef __HAVE_ARCH_MEMSET
489/**
490 * memset - Fill a region of memory with the given value
491 * @s: Pointer to the start of the area.
492 * @c: The byte to fill the area with
493 * @count: The size of the area.
494 *
495 * Do not use memset() to access IO space, use memset_io() instead.
496 */
497void * memset(void * s,int c,size_t count)
498{
Alessandro Rubinife1a2fb2009-10-10 11:51:16 +0200499 unsigned long *sl = (unsigned long *) s;
Alessandro Rubinife1a2fb2009-10-10 11:51:16 +0200500 char *s8;
Simon Glasse2f94ae2017-04-02 09:50:28 -0600501
502#if !CONFIG_IS_ENABLED(TINY_MEMSET)
503 unsigned long cl = 0;
Alessandro Rubinife1a2fb2009-10-10 11:51:16 +0200504 int i;
wdenkc6097192002-11-03 00:24:07 +0000505
Alessandro Rubinife1a2fb2009-10-10 11:51:16 +0200506 /* do it one word at a time (32 bits or 64 bits) while possible */
507 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
508 for (i = 0; i < sizeof(*sl); i++) {
509 cl <<= 8;
510 cl |= c & 0xff;
511 }
512 while (count >= sizeof(*sl)) {
513 *sl++ = cl;
514 count -= sizeof(*sl);
515 }
516 }
Simon Glasse2f94ae2017-04-02 09:50:28 -0600517#endif /* fill 8 bits at a time */
Alessandro Rubinife1a2fb2009-10-10 11:51:16 +0200518 s8 = (char *)sl;
wdenkc6097192002-11-03 00:24:07 +0000519 while (count--)
Alessandro Rubinife1a2fb2009-10-10 11:51:16 +0200520 *s8++ = c;
wdenkc6097192002-11-03 00:24:07 +0000521
522 return s;
523}
524#endif
525
wdenkc6097192002-11-03 00:24:07 +0000526#ifndef __HAVE_ARCH_MEMCPY
527/**
528 * memcpy - Copy one area of memory to another
529 * @dest: Where to copy to
530 * @src: Where to copy from
531 * @count: The size of the area.
532 *
533 * You should not use this function to access IO space, use memcpy_toio()
534 * or memcpy_fromio() instead.
535 */
Alessandro Rubinif881b542009-10-10 11:51:05 +0200536void * memcpy(void *dest, const void *src, size_t count)
wdenkc6097192002-11-03 00:24:07 +0000537{
Alessandro Rubinif881b542009-10-10 11:51:05 +0200538 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
539 char *d8, *s8;
wdenkc6097192002-11-03 00:24:07 +0000540
Matthias Weisser5bf63482011-05-22 23:03:55 +0000541 if (src == dest)
542 return dest;
543
Alessandro Rubinif881b542009-10-10 11:51:05 +0200544 /* while all data is aligned (common case), copy a word at a time */
545 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
546 while (count >= sizeof(*dl)) {
547 *dl++ = *sl++;
548 count -= sizeof(*dl);
549 }
550 }
551 /* copy the reset one byte at a time */
552 d8 = (char *)dl;
553 s8 = (char *)sl;
wdenkc6097192002-11-03 00:24:07 +0000554 while (count--)
Alessandro Rubinif881b542009-10-10 11:51:05 +0200555 *d8++ = *s8++;
wdenkc6097192002-11-03 00:24:07 +0000556
557 return dest;
558}
559#endif
560
561#ifndef __HAVE_ARCH_MEMMOVE
562/**
563 * memmove - Copy one area of memory to another
564 * @dest: Where to copy to
565 * @src: Where to copy from
566 * @count: The size of the area.
567 *
568 * Unlike memcpy(), memmove() copes with overlapping areas.
569 */
570void * memmove(void * dest,const void *src,size_t count)
571{
572 char *tmp, *s;
573
Patrick Delaunay26b66a32020-12-11 14:59:23 +0100574 if (dest <= src || (src + count) <= dest) {
575 /*
576 * Use the fast memcpy implementation (ARCH optimized or lib/string.c) when it is possible:
577 * - when dest is before src (assuming that memcpy is doing forward-copying)
578 * - when destination don't overlap the source buffer (src + count <= dest)
579 *
580 * WARNING: the first optimisation cause an issue, when __HAVE_ARCH_MEMCPY is defined,
581 * __HAVE_ARCH_MEMMOVE is not defined and if the memcpy ARCH-specific
582 * implementation is not doing a forward-copying.
583 *
584 * No issue today because memcpy is doing a forward-copying in lib/string.c and for ARM32
585 * architecture; no other arches use __HAVE_ARCH_MEMCPY without __HAVE_ARCH_MEMMOVE.
586 */
Simon Glass03f827e2017-04-05 16:23:31 -0600587 memcpy(dest, src, count);
588 } else {
wdenkc6097192002-11-03 00:24:07 +0000589 tmp = (char *) dest + count;
590 s = (char *) src + count;
591 while (count--)
592 *--tmp = *--s;
593 }
594
595 return dest;
596}
597#endif
598
599#ifndef __HAVE_ARCH_MEMCMP
600/**
601 * memcmp - Compare two areas of memory
602 * @cs: One area of memory
603 * @ct: Another area of memory
604 * @count: The size of the area.
605 */
606int memcmp(const void * cs,const void * ct,size_t count)
607{
608 const unsigned char *su1, *su2;
609 int res = 0;
610
611 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
612 if ((res = *su1 - *su2) != 0)
613 break;
614 return res;
615}
616#endif
617
618#ifndef __HAVE_ARCH_MEMSCAN
619/**
620 * memscan - Find a character in an area of memory.
621 * @addr: The memory area
622 * @c: The byte to search for
623 * @size: The size of the area.
624 *
625 * returns the address of the first occurrence of @c, or 1 byte past
626 * the area if @c is not found
627 */
628void * memscan(void * addr, int c, size_t size)
629{
630 unsigned char * p = (unsigned char *) addr;
631
632 while (size) {
633 if (*p == c)
634 return (void *) p;
635 p++;
636 size--;
637 }
wdenk57b2d802003-06-27 21:31:46 +0000638 return (void *) p;
wdenkc6097192002-11-03 00:24:07 +0000639}
640#endif
641
642#ifndef __HAVE_ARCH_STRSTR
643/**
644 * strstr - Find the first substring in a %NUL terminated string
645 * @s1: The string to be searched
646 * @s2: The string to search for
647 */
648char * strstr(const char * s1,const char * s2)
649{
650 int l1, l2;
651
652 l2 = strlen(s2);
653 if (!l2)
654 return (char *) s1;
655 l1 = strlen(s1);
656 while (l1 >= l2) {
657 l1--;
658 if (!memcmp(s1,s2,l2))
659 return (char *) s1;
660 s1++;
661 }
662 return NULL;
663}
664#endif
665
666#ifndef __HAVE_ARCH_MEMCHR
667/**
668 * memchr - Find a character in an area of memory.
669 * @s: The memory area
670 * @c: The byte to search for
671 * @n: The size of the area.
672 *
673 * returns the address of the first occurrence of @c, or %NULL
674 * if @c is not found
675 */
676void *memchr(const void *s, int c, size_t n)
677{
678 const unsigned char *p = s;
679 while (n-- != 0) {
wdenk57b2d802003-06-27 21:31:46 +0000680 if ((unsigned char)c == *p++) {
wdenkc6097192002-11-03 00:24:07 +0000681 return (void *)(p-1);
682 }
683 }
684 return NULL;
685}
686
687#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +0000688#ifndef __HAVE_ARCH_MEMCHR_INV
689static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
690{
691 while (bytes) {
692 if (*start != value)
693 return (void *)start;
694 start++;
695 bytes--;
696 }
697 return NULL;
698}
699/**
700 * memchr_inv - Find an unmatching character in an area of memory.
701 * @start: The memory area
702 * @c: Find a character other than c
703 * @bytes: The size of the area.
704 *
705 * returns the address of the first character other than @c, or %NULL
706 * if the whole buffer contains just @c.
707 */
708void *memchr_inv(const void *start, int c, size_t bytes)
709{
710 u8 value = c;
711 u64 value64;
712 unsigned int words, prefix;
713
714 if (bytes <= 16)
715 return check_bytes8(start, value, bytes);
716
717 value64 = value;
718 value64 |= value64 << 8;
719 value64 |= value64 << 16;
720 value64 |= value64 << 32;
721
722 prefix = (unsigned long)start % 8;
723 if (prefix) {
724 u8 *r;
725
726 prefix = 8 - prefix;
727 r = check_bytes8(start, value, prefix);
728 if (r)
729 return r;
730 start += prefix;
731 bytes -= prefix;
732 }
733
734 words = bytes / 8;
735
736 while (words) {
737 if (*(u64 *)start != value64)
738 return check_bytes8(start, value, 8);
739 start += 8;
740 words--;
741 }
742
743 return check_bytes8(start, value, bytes % 8);
744}
745#endif