xref: /openbmc/u-boot/lib/string.c (revision f13606b7)
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 <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <malloc.h>
22 
23 
24 /**
25  * strncasecmp - Case insensitive, length-limited string comparison
26  * @s1: One string
27  * @s2: The other string
28  * @len: the maximum number of characters to compare
29  */
30 int strncasecmp(const char *s1, const char *s2, size_t len)
31 {
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 }
54 
55 /**
56  * strcasecmp - Case insensitive string comparison
57  * @s1: One string
58  * @s2: The other string
59  */
60 int strcasecmp(const char *s1, const char *s2)
61 {
62 	return strncasecmp(s1, s2, -1U);
63 }
64 
65 char * ___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  */
73 char * 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  */
94 char * 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 
105 #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  */
117 size_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 
130 #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  */
136 char * 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  */
159 char * 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 
178 #ifndef __HAVE_ARCH_STRCMP
179 /**
180  * strcmp - Compare two strings
181  * @cs: One string
182  * @ct: Another string
183  */
184 int strcmp(const char * cs,const char * ct)
185 {
186 	register signed char __res;
187 
188 	while (1) {
189 		if ((__res = *cs - *ct++) != 0 || !*cs++)
190 			break;
191 	}
192 
193 	return __res;
194 }
195 #endif
196 
197 #ifndef __HAVE_ARCH_STRNCMP
198 /**
199  * strncmp - Compare two length-limited strings
200  * @cs: One string
201  * @ct: Another string
202  * @count: The maximum number of bytes to compare
203  */
204 int strncmp(const char * cs,const char * ct,size_t count)
205 {
206 	register signed char __res = 0;
207 
208 	while (count) {
209 		if ((__res = *cs - *ct++) != 0 || !*cs++)
210 			break;
211 		count--;
212 	}
213 
214 	return __res;
215 }
216 #endif
217 
218 #ifndef __HAVE_ARCH_STRCHR
219 /**
220  * strchr - Find the first occurrence of a character in a string
221  * @s: The string to be searched
222  * @c: The character to search for
223  */
224 char * strchr(const char * s, int c)
225 {
226 	for(; *s != (char) c; ++s)
227 		if (*s == '\0')
228 			return NULL;
229 	return (char *) s;
230 }
231 #endif
232 
233 #ifndef __HAVE_ARCH_STRRCHR
234 /**
235  * strrchr - Find the last occurrence of a character in a string
236  * @s: The string to be searched
237  * @c: The character to search for
238  */
239 char * strrchr(const char * s, int c)
240 {
241        const char *p = s + strlen(s);
242        do {
243 	   if (*p == (char)c)
244 	       return (char *)p;
245        } while (--p >= s);
246        return NULL;
247 }
248 #endif
249 
250 #ifndef __HAVE_ARCH_STRLEN
251 /**
252  * strlen - Find the length of a string
253  * @s: The string to be sized
254  */
255 size_t strlen(const char * s)
256 {
257 	const char *sc;
258 
259 	for (sc = s; *sc != '\0'; ++sc)
260 		/* nothing */;
261 	return sc - s;
262 }
263 #endif
264 
265 #ifndef __HAVE_ARCH_STRNLEN
266 /**
267  * strnlen - Find the length of a length-limited string
268  * @s: The string to be sized
269  * @count: The maximum number of bytes to search
270  */
271 size_t strnlen(const char * s, size_t count)
272 {
273 	const char *sc;
274 
275 	for (sc = s; count-- && *sc != '\0'; ++sc)
276 		/* nothing */;
277 	return sc - s;
278 }
279 #endif
280 
281 #ifndef __HAVE_ARCH_STRDUP
282 char * strdup(const char *s)
283 {
284 	char *new;
285 
286 	if ((s == NULL)	||
287 	    ((new = malloc (strlen(s) + 1)) == NULL) ) {
288 		return NULL;
289 	}
290 
291 	strcpy (new, s);
292 	return new;
293 }
294 #endif
295 
296 #ifndef __HAVE_ARCH_STRSPN
297 /**
298  * strspn - Calculate the length of the initial substring of @s which only
299  *	contain letters in @accept
300  * @s: The string to be searched
301  * @accept: The string to search for
302  */
303 size_t strspn(const char *s, const char *accept)
304 {
305 	const char *p;
306 	const char *a;
307 	size_t count = 0;
308 
309 	for (p = s; *p != '\0'; ++p) {
310 		for (a = accept; *a != '\0'; ++a) {
311 			if (*p == *a)
312 				break;
313 		}
314 		if (*a == '\0')
315 			return count;
316 		++count;
317 	}
318 
319 	return count;
320 }
321 #endif
322 
323 #ifndef __HAVE_ARCH_STRPBRK
324 /**
325  * strpbrk - Find the first occurrence of a set of characters
326  * @cs: The string to be searched
327  * @ct: The characters to search for
328  */
329 char * strpbrk(const char * cs,const char * ct)
330 {
331 	const char *sc1,*sc2;
332 
333 	for( sc1 = cs; *sc1 != '\0'; ++sc1) {
334 		for( sc2 = ct; *sc2 != '\0'; ++sc2) {
335 			if (*sc1 == *sc2)
336 				return (char *) sc1;
337 		}
338 	}
339 	return NULL;
340 }
341 #endif
342 
343 #ifndef __HAVE_ARCH_STRTOK
344 /**
345  * strtok - Split a string into tokens
346  * @s: The string to be searched
347  * @ct: The characters to search for
348  *
349  * WARNING: strtok is deprecated, use strsep instead.
350  */
351 char * strtok(char * s,const char * ct)
352 {
353 	char *sbegin, *send;
354 
355 	sbegin  = s ? s : ___strtok;
356 	if (!sbegin) {
357 		return NULL;
358 	}
359 	sbegin += strspn(sbegin,ct);
360 	if (*sbegin == '\0') {
361 		___strtok = NULL;
362 		return( NULL );
363 	}
364 	send = strpbrk( sbegin, ct);
365 	if (send && *send != '\0')
366 		*send++ = '\0';
367 	___strtok = send;
368 	return (sbegin);
369 }
370 #endif
371 
372 #ifndef __HAVE_ARCH_STRSEP
373 /**
374  * strsep - Split a string into tokens
375  * @s: The string to be searched
376  * @ct: The characters to search for
377  *
378  * strsep() updates @s to point after the token, ready for the next call.
379  *
380  * It returns empty tokens, too, behaving exactly like the libc function
381  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
382  * Same semantics, slimmer shape. ;)
383  */
384 char * strsep(char **s, const char *ct)
385 {
386 	char *sbegin = *s, *end;
387 
388 	if (sbegin == NULL)
389 		return NULL;
390 
391 	end = strpbrk(sbegin, ct);
392 	if (end)
393 		*end++ = '\0';
394 	*s = end;
395 
396 	return sbegin;
397 }
398 #endif
399 
400 #ifndef __HAVE_ARCH_STRSWAB
401 /**
402  * strswab - swap adjacent even and odd bytes in %NUL-terminated string
403  * s: address of the string
404  *
405  * returns the address of the swapped string or NULL on error. If
406  * string length is odd, last byte is untouched.
407  */
408 char *strswab(const char *s)
409 {
410 	char *p, *q;
411 
412 	if ((NULL == s) || ('\0' == *s)) {
413 		return (NULL);
414 	}
415 
416 	for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
417 		char  tmp;
418 
419 		tmp = *p;
420 		*p  = *q;
421 		*q  = tmp;
422 	}
423 
424 	return (char *) s;
425 }
426 #endif
427 
428 #ifndef __HAVE_ARCH_MEMSET
429 /**
430  * memset - Fill a region of memory with the given value
431  * @s: Pointer to the start of the area.
432  * @c: The byte to fill the area with
433  * @count: The size of the area.
434  *
435  * Do not use memset() to access IO space, use memset_io() instead.
436  */
437 void * memset(void * s,int c,size_t count)
438 {
439 	unsigned long *sl = (unsigned long *) s;
440 	unsigned long cl = 0;
441 	char *s8;
442 	int i;
443 
444 	/* do it one word at a time (32 bits or 64 bits) while possible */
445 	if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
446 		for (i = 0; i < sizeof(*sl); i++) {
447 			cl <<= 8;
448 			cl |= c & 0xff;
449 		}
450 		while (count >= sizeof(*sl)) {
451 			*sl++ = cl;
452 			count -= sizeof(*sl);
453 		}
454 	}
455 	/* fill 8 bits at a time */
456 	s8 = (char *)sl;
457 	while (count--)
458 		*s8++ = c;
459 
460 	return s;
461 }
462 #endif
463 
464 #ifndef __HAVE_ARCH_BCOPY
465 /**
466  * bcopy - Copy one area of memory to another
467  * @src: Where to copy from
468  * @dest: Where to copy to
469  * @count: The size of the area.
470  *
471  * Note that this is the same as memcpy(), with the arguments reversed.
472  * memcpy() is the standard, bcopy() is a legacy BSD function.
473  *
474  * You should not use this function to access IO space, use memcpy_toio()
475  * or memcpy_fromio() instead.
476  */
477 char * bcopy(const char * src, char * dest, int count)
478 {
479 	char *tmp = dest;
480 
481 	while (count--)
482 		*tmp++ = *src++;
483 
484 	return dest;
485 }
486 #endif
487 
488 #ifndef __HAVE_ARCH_MEMCPY
489 /**
490  * memcpy - Copy one area of memory to another
491  * @dest: Where to copy to
492  * @src: Where to copy from
493  * @count: The size of the area.
494  *
495  * You should not use this function to access IO space, use memcpy_toio()
496  * or memcpy_fromio() instead.
497  */
498 void * memcpy(void *dest, const void *src, size_t count)
499 {
500 	unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
501 	char *d8, *s8;
502 
503 	if (src == dest)
504 		return dest;
505 
506 	/* while all data is aligned (common case), copy a word at a time */
507 	if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
508 		while (count >= sizeof(*dl)) {
509 			*dl++ = *sl++;
510 			count -= sizeof(*dl);
511 		}
512 	}
513 	/* copy the reset one byte at a time */
514 	d8 = (char *)dl;
515 	s8 = (char *)sl;
516 	while (count--)
517 		*d8++ = *s8++;
518 
519 	return dest;
520 }
521 #endif
522 
523 #ifndef __HAVE_ARCH_MEMMOVE
524 /**
525  * memmove - Copy one area of memory to another
526  * @dest: Where to copy to
527  * @src: Where to copy from
528  * @count: The size of the area.
529  *
530  * Unlike memcpy(), memmove() copes with overlapping areas.
531  */
532 void * memmove(void * dest,const void *src,size_t count)
533 {
534 	char *tmp, *s;
535 
536 	if (src == dest)
537 		return dest;
538 
539 	if (dest <= src) {
540 		tmp = (char *) dest;
541 		s = (char *) src;
542 		while (count--)
543 			*tmp++ = *s++;
544 		}
545 	else {
546 		tmp = (char *) dest + count;
547 		s = (char *) src + count;
548 		while (count--)
549 			*--tmp = *--s;
550 		}
551 
552 	return dest;
553 }
554 #endif
555 
556 #ifndef __HAVE_ARCH_MEMCMP
557 /**
558  * memcmp - Compare two areas of memory
559  * @cs: One area of memory
560  * @ct: Another area of memory
561  * @count: The size of the area.
562  */
563 int memcmp(const void * cs,const void * ct,size_t count)
564 {
565 	const unsigned char *su1, *su2;
566 	int res = 0;
567 
568 	for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
569 		if ((res = *su1 - *su2) != 0)
570 			break;
571 	return res;
572 }
573 #endif
574 
575 #ifndef __HAVE_ARCH_MEMSCAN
576 /**
577  * memscan - Find a character in an area of memory.
578  * @addr: The memory area
579  * @c: The byte to search for
580  * @size: The size of the area.
581  *
582  * returns the address of the first occurrence of @c, or 1 byte past
583  * the area if @c is not found
584  */
585 void * memscan(void * addr, int c, size_t size)
586 {
587 	unsigned char * p = (unsigned char *) addr;
588 
589 	while (size) {
590 		if (*p == c)
591 			return (void *) p;
592 		p++;
593 		size--;
594 	}
595 	return (void *) p;
596 }
597 #endif
598 
599 #ifndef __HAVE_ARCH_STRSTR
600 /**
601  * strstr - Find the first substring in a %NUL terminated string
602  * @s1: The string to be searched
603  * @s2: The string to search for
604  */
605 char * strstr(const char * s1,const char * s2)
606 {
607 	int l1, l2;
608 
609 	l2 = strlen(s2);
610 	if (!l2)
611 		return (char *) s1;
612 	l1 = strlen(s1);
613 	while (l1 >= l2) {
614 		l1--;
615 		if (!memcmp(s1,s2,l2))
616 			return (char *) s1;
617 		s1++;
618 	}
619 	return NULL;
620 }
621 #endif
622 
623 #ifndef __HAVE_ARCH_MEMCHR
624 /**
625  * memchr - Find a character in an area of memory.
626  * @s: The memory area
627  * @c: The byte to search for
628  * @n: The size of the area.
629  *
630  * returns the address of the first occurrence of @c, or %NULL
631  * if @c is not found
632  */
633 void *memchr(const void *s, int c, size_t n)
634 {
635 	const unsigned char *p = s;
636 	while (n-- != 0) {
637 		if ((unsigned char)c == *p++) {
638 			return (void *)(p-1);
639 		}
640 	}
641 	return NULL;
642 }
643 
644 #endif
645 #ifndef __HAVE_ARCH_MEMCHR_INV
646 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
647 {
648 	while (bytes) {
649 		if (*start != value)
650 			return (void *)start;
651 		start++;
652 		bytes--;
653 	}
654 	return NULL;
655 }
656 /**
657  * memchr_inv - Find an unmatching character in an area of memory.
658  * @start: The memory area
659  * @c: Find a character other than c
660  * @bytes: The size of the area.
661  *
662  * returns the address of the first character other than @c, or %NULL
663  * if the whole buffer contains just @c.
664  */
665 void *memchr_inv(const void *start, int c, size_t bytes)
666 {
667 	u8 value = c;
668 	u64 value64;
669 	unsigned int words, prefix;
670 
671 	if (bytes <= 16)
672 		return check_bytes8(start, value, bytes);
673 
674 	value64 = value;
675 	value64 |= value64 << 8;
676 	value64 |= value64 << 16;
677 	value64 |= value64 << 32;
678 
679 	prefix = (unsigned long)start % 8;
680 	if (prefix) {
681 		u8 *r;
682 
683 		prefix = 8 - prefix;
684 		r = check_bytes8(start, value, prefix);
685 		if (r)
686 			return r;
687 		start += prefix;
688 		bytes -= prefix;
689 	}
690 
691 	words = bytes / 8;
692 
693 	while (words) {
694 		if (*(u64 *)start != value64)
695 			return check_bytes8(start, value, 8);
696 		start += 8;
697 		words--;
698 	}
699 
700 	return check_bytes8(start, value, bytes % 8);
701 }
702 #endif
703