lib: string: Fix strlcpy return value
[platform/kernel/u-boot.git] / lib / string.c
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
18 #include <config.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/ctype.h>
22 #include <malloc.h>
23
24
25 /**
26  * strncasecmp - Case insensitive, length-limited string comparison
27  * @s1: One string
28  * @s2: The other string
29  * @len: the maximum number of characters to compare
30  */
31 int strncasecmp(const char *s1, const char *s2, size_t len)
32 {
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 }
55
56 /**
57  * strcasecmp - Case insensitive string comparison
58  * @s1: One string
59  * @s2: The other string
60  */
61 int strcasecmp(const char *s1, const char *s2)
62 {
63         return strncasecmp(s1, s2, -1U);
64 }
65
66 char * ___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  */
74 char * 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  */
95 char * 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
106 #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.
117  *
118  * Return: the number of bytes copied
119  */
120 size_t strlcpy(char *dest, const char *src, size_t size)
121 {
122         if (size) {
123                 size_t srclen = strlen(src);
124                 size_t len = (srclen >= size) ? size - 1 : srclen;
125
126                 memcpy(dest, src, len);
127                 dest[len] = '\0';
128                 return len + 1;
129         }
130
131         return 0;
132 }
133 #endif
134
135 #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  */
141 char * 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  */
164 char * 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  */
189 int 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  */
209 int 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  */
229 char * 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
238 const 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
246 #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  */
252 char * strrchr(const char * s, int c)
253 {
254        const char *p = s + strlen(s);
255        do {
256            if (*p == (char)c)
257                return (char *)p;
258        } 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  */
268 size_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  */
284 size_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
294 #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  */
301 size_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
318 #ifndef __HAVE_ARCH_STRDUP
319 char * 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 }
331
332 char * 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 }
354 #endif
355
356 #ifndef __HAVE_ARCH_STRSPN
357 /**
358  * strspn - Calculate the length of the initial substring of @s which only
359  *      contain letters in @accept
360  * @s: The string to be searched
361  * @accept: The string to search for
362  */
363 size_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  */
389 char * 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  */
411 char * 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  */
444 char * 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
460 #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  */
468 char *strswab(const char *s)
469 {
470         char *p, *q;
471
472         if ((NULL == s) || ('\0' == *s)) {
473                 return (NULL);
474         }
475
476         for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
477                 char  tmp;
478
479                 tmp = *p;
480                 *p  = *q;
481                 *q  = tmp;
482         }
483
484         return (char *) s;
485 }
486 #endif
487
488 #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  */
497 void * memset(void * s,int c,size_t count)
498 {
499         unsigned long *sl = (unsigned long *) s;
500         char *s8;
501
502 #if !CONFIG_IS_ENABLED(TINY_MEMSET)
503         unsigned long cl = 0;
504         int i;
505
506         /* 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         }
517 #endif  /* fill 8 bits at a time */
518         s8 = (char *)sl;
519         while (count--)
520                 *s8++ = c;
521
522         return s;
523 }
524 #endif
525
526 #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  */
536 void * memcpy(void *dest, const void *src, size_t count)
537 {
538         unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
539         char *d8, *s8;
540
541         if (src == dest)
542                 return dest;
543
544         /* 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;
554         while (count--)
555                 *d8++ = *s8++;
556
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  */
570 void * memmove(void * dest,const void *src,size_t count)
571 {
572         char *tmp, *s;
573
574         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          */
587                 memcpy(dest, src, count);
588         } else {
589                 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  */
606 int 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  */
628 void * 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         }
638         return (void *) p;
639 }
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  */
648 char * 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  */
676 void *memchr(const void *s, int c, size_t n)
677 {
678         const unsigned char *p = s;
679         while (n-- != 0) {
680                 if ((unsigned char)c == *p++) {
681                         return (void *)(p-1);
682                 }
683         }
684         return NULL;
685 }
686
687 #endif
688 #ifndef __HAVE_ARCH_MEMCHR_INV
689 static 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  */
708 void *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