xref: /openbmc/u-boot/lib/string.c (revision 9386f96ca952529378f64b65633e38fdd8708307)
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_STRCAT
106 /**
107  * strcat - Append one %NUL-terminated string to another
108  * @dest: The string to be appended to
109  * @src: The string to append to it
110  */
111 char * strcat(char * dest, const char * src)
112 {
113 	char *tmp = dest;
114 
115 	while (*dest)
116 		dest++;
117 	while ((*dest++ = *src++) != '\0')
118 		;
119 
120 	return tmp;
121 }
122 #endif
123 
124 #ifndef __HAVE_ARCH_STRNCAT
125 /**
126  * strncat - Append a length-limited, %NUL-terminated string to another
127  * @dest: The string to be appended to
128  * @src: The string to append to it
129  * @count: The maximum numbers of bytes to copy
130  *
131  * Note that in contrast to strncpy, strncat ensures the result is
132  * terminated.
133  */
134 char * strncat(char *dest, const char *src, size_t count)
135 {
136 	char *tmp = dest;
137 
138 	if (count) {
139 		while (*dest)
140 			dest++;
141 		while ((*dest++ = *src++)) {
142 			if (--count == 0) {
143 				*dest = '\0';
144 				break;
145 			}
146 		}
147 	}
148 
149 	return tmp;
150 }
151 #endif
152 
153 #ifndef __HAVE_ARCH_STRCMP
154 /**
155  * strcmp - Compare two strings
156  * @cs: One string
157  * @ct: Another string
158  */
159 int strcmp(const char * cs,const char * ct)
160 {
161 	register signed char __res;
162 
163 	while (1) {
164 		if ((__res = *cs - *ct++) != 0 || !*cs++)
165 			break;
166 	}
167 
168 	return __res;
169 }
170 #endif
171 
172 #ifndef __HAVE_ARCH_STRNCMP
173 /**
174  * strncmp - Compare two length-limited strings
175  * @cs: One string
176  * @ct: Another string
177  * @count: The maximum number of bytes to compare
178  */
179 int strncmp(const char * cs,const char * ct,size_t count)
180 {
181 	register signed char __res = 0;
182 
183 	while (count) {
184 		if ((__res = *cs - *ct++) != 0 || !*cs++)
185 			break;
186 		count--;
187 	}
188 
189 	return __res;
190 }
191 #endif
192 
193 #ifndef __HAVE_ARCH_STRCHR
194 /**
195  * strchr - Find the first occurrence of a character in a string
196  * @s: The string to be searched
197  * @c: The character to search for
198  */
199 char * strchr(const char * s, int c)
200 {
201 	for(; *s != (char) c; ++s)
202 		if (*s == '\0')
203 			return NULL;
204 	return (char *) s;
205 }
206 #endif
207 
208 #ifndef __HAVE_ARCH_STRRCHR
209 /**
210  * strrchr - Find the last occurrence of a character in a string
211  * @s: The string to be searched
212  * @c: The character to search for
213  */
214 char * strrchr(const char * s, int c)
215 {
216        const char *p = s + strlen(s);
217        do {
218 	   if (*p == (char)c)
219 	       return (char *)p;
220        } while (--p >= s);
221        return NULL;
222 }
223 #endif
224 
225 
226 /**
227  * skip_spaces - Removes leading whitespace from @str.
228  * @str: The string to be stripped.
229  *
230  * Returns a pointer to the first non-whitespace character in @str.
231  */
232 char *skip_spaces(const char *str)
233 {
234 	while (isspace(*str))
235 		++str;
236 	return (char *)str;
237 }
238 
239 /**
240  * strim - Removes leading and trailing whitespace from @s.
241  * @s: The string to be stripped.
242  *
243  * Note that the first trailing whitespace is replaced with a %NUL-terminator
244  * in the given string @s. Returns a pointer to the first non-whitespace
245  * character in @s.
246  */
247 char *strim(char *s)
248 {
249 	size_t size;
250 	char *end;
251 
252 	s = skip_spaces(s);
253 	size = strlen(s);
254 	if (!size)
255 		return s;
256 
257 	end = s + size - 1;
258 	while (end >= s && isspace(*end))
259 		end--;
260 	*(end + 1) = '\0';
261 
262 	return s;
263 }
264 #ifndef __HAVE_ARCH_STRLEN
265 /**
266  * strlen - Find the length of a string
267  * @s: The string to be sized
268  */
269 size_t strlen(const char * s)
270 {
271 	const char *sc;
272 
273 	for (sc = s; *sc != '\0'; ++sc)
274 		/* nothing */;
275 	return sc - s;
276 }
277 #endif
278 
279 #ifndef __HAVE_ARCH_STRNLEN
280 /**
281  * strnlen - Find the length of a length-limited string
282  * @s: The string to be sized
283  * @count: The maximum number of bytes to search
284  */
285 size_t strnlen(const char * s, size_t count)
286 {
287 	const char *sc;
288 
289 	for (sc = s; count-- && *sc != '\0'; ++sc)
290 		/* nothing */;
291 	return sc - s;
292 }
293 #endif
294 
295 #ifndef __HAVE_ARCH_STRDUP
296 char * strdup(const char *s)
297 {
298 	char *new;
299 
300 	if ((s == NULL)	||
301 	    ((new = malloc (strlen(s) + 1)) == NULL) ) {
302 		return NULL;
303 	}
304 
305 	strcpy (new, s);
306 	return new;
307 }
308 #endif
309 
310 #ifndef __HAVE_ARCH_STRSPN
311 /**
312  * strspn - Calculate the length of the initial substring of @s which only
313  *	contain letters in @accept
314  * @s: The string to be searched
315  * @accept: The string to search for
316  */
317 size_t strspn(const char *s, const char *accept)
318 {
319 	const char *p;
320 	const char *a;
321 	size_t count = 0;
322 
323 	for (p = s; *p != '\0'; ++p) {
324 		for (a = accept; *a != '\0'; ++a) {
325 			if (*p == *a)
326 				break;
327 		}
328 		if (*a == '\0')
329 			return count;
330 		++count;
331 	}
332 
333 	return count;
334 }
335 #endif
336 
337 #ifndef __HAVE_ARCH_STRPBRK
338 /**
339  * strpbrk - Find the first occurrence of a set of characters
340  * @cs: The string to be searched
341  * @ct: The characters to search for
342  */
343 char * strpbrk(const char * cs,const char * ct)
344 {
345 	const char *sc1,*sc2;
346 
347 	for( sc1 = cs; *sc1 != '\0'; ++sc1) {
348 		for( sc2 = ct; *sc2 != '\0'; ++sc2) {
349 			if (*sc1 == *sc2)
350 				return (char *) sc1;
351 		}
352 	}
353 	return NULL;
354 }
355 #endif
356 
357 #ifndef __HAVE_ARCH_STRTOK
358 /**
359  * strtok - Split a string into tokens
360  * @s: The string to be searched
361  * @ct: The characters to search for
362  *
363  * WARNING: strtok is deprecated, use strsep instead.
364  */
365 char * strtok(char * s,const char * ct)
366 {
367 	char *sbegin, *send;
368 
369 	sbegin  = s ? s : ___strtok;
370 	if (!sbegin) {
371 		return NULL;
372 	}
373 	sbegin += strspn(sbegin,ct);
374 	if (*sbegin == '\0') {
375 		___strtok = NULL;
376 		return( NULL );
377 	}
378 	send = strpbrk( sbegin, ct);
379 	if (send && *send != '\0')
380 		*send++ = '\0';
381 	___strtok = send;
382 	return (sbegin);
383 }
384 #endif
385 
386 #ifndef __HAVE_ARCH_STRSEP
387 /**
388  * strsep - Split a string into tokens
389  * @s: The string to be searched
390  * @ct: The characters to search for
391  *
392  * strsep() updates @s to point after the token, ready for the next call.
393  *
394  * It returns empty tokens, too, behaving exactly like the libc function
395  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
396  * Same semantics, slimmer shape. ;)
397  */
398 char * strsep(char **s, const char *ct)
399 {
400 	char *sbegin = *s, *end;
401 
402 	if (sbegin == NULL)
403 		return NULL;
404 
405 	end = strpbrk(sbegin, ct);
406 	if (end)
407 		*end++ = '\0';
408 	*s = end;
409 
410 	return sbegin;
411 }
412 #endif
413 
414 #ifndef __HAVE_ARCH_STRSWAB
415 /**
416  * strswab - swap adjacent even and odd bytes in %NUL-terminated string
417  * s: address of the string
418  *
419  * returns the address of the swapped string or NULL on error. If
420  * string length is odd, last byte is untouched.
421  */
422 char *strswab(const char *s)
423 {
424 	char *p, *q;
425 
426 	if ((NULL == s) || ('\0' == *s)) {
427 		return (NULL);
428 	}
429 
430 	for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
431 		char  tmp;
432 
433 		tmp = *p;
434 		*p  = *q;
435 		*q  = tmp;
436 	}
437 
438 	return (char *) s;
439 }
440 #endif
441 
442 #ifndef __HAVE_ARCH_MEMSET
443 /**
444  * memset - Fill a region of memory with the given value
445  * @s: Pointer to the start of the area.
446  * @c: The byte to fill the area with
447  * @count: The size of the area.
448  *
449  * Do not use memset() to access IO space, use memset_io() instead.
450  */
451 void * memset(void * s,int c,size_t count)
452 {
453 	unsigned long *sl = (unsigned long *) s;
454 	unsigned long cl = 0;
455 	char *s8;
456 	int i;
457 
458 	/* do it one word at a time (32 bits or 64 bits) while possible */
459 	if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
460 		for (i = 0; i < sizeof(*sl); i++) {
461 			cl <<= 8;
462 			cl |= c & 0xff;
463 		}
464 		while (count >= sizeof(*sl)) {
465 			*sl++ = cl;
466 			count -= sizeof(*sl);
467 		}
468 	}
469 	/* fill 8 bits at a time */
470 	s8 = (char *)sl;
471 	while (count--)
472 		*s8++ = c;
473 
474 	return s;
475 }
476 #endif
477 
478 #ifndef __HAVE_ARCH_BCOPY
479 /**
480  * bcopy - Copy one area of memory to another
481  * @src: Where to copy from
482  * @dest: Where to copy to
483  * @count: The size of the area.
484  *
485  * Note that this is the same as memcpy(), with the arguments reversed.
486  * memcpy() is the standard, bcopy() is a legacy BSD function.
487  *
488  * You should not use this function to access IO space, use memcpy_toio()
489  * or memcpy_fromio() instead.
490  */
491 char * bcopy(const char * src, char * dest, int count)
492 {
493 	char *tmp = dest;
494 
495 	while (count--)
496 		*tmp++ = *src++;
497 
498 	return dest;
499 }
500 #endif
501 
502 #ifndef __HAVE_ARCH_MEMCPY
503 /**
504  * memcpy - Copy one area of memory to another
505  * @dest: Where to copy to
506  * @src: Where to copy from
507  * @count: The size of the area.
508  *
509  * You should not use this function to access IO space, use memcpy_toio()
510  * or memcpy_fromio() instead.
511  */
512 void * memcpy(void *dest, const void *src, size_t count)
513 {
514 	unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
515 	char *d8, *s8;
516 
517 	if (src == dest)
518 		return dest;
519 
520 	/* while all data is aligned (common case), copy a word at a time */
521 	if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
522 		while (count >= sizeof(*dl)) {
523 			*dl++ = *sl++;
524 			count -= sizeof(*dl);
525 		}
526 	}
527 	/* copy the reset one byte at a time */
528 	d8 = (char *)dl;
529 	s8 = (char *)sl;
530 	while (count--)
531 		*d8++ = *s8++;
532 
533 	return dest;
534 }
535 #endif
536 
537 #ifndef __HAVE_ARCH_MEMMOVE
538 /**
539  * memmove - Copy one area of memory to another
540  * @dest: Where to copy to
541  * @src: Where to copy from
542  * @count: The size of the area.
543  *
544  * Unlike memcpy(), memmove() copes with overlapping areas.
545  */
546 void * memmove(void * dest,const void *src,size_t count)
547 {
548 	char *tmp, *s;
549 
550 	if (src == dest)
551 		return dest;
552 
553 	if (dest <= src) {
554 		tmp = (char *) dest;
555 		s = (char *) src;
556 		while (count--)
557 			*tmp++ = *s++;
558 		}
559 	else {
560 		tmp = (char *) dest + count;
561 		s = (char *) src + count;
562 		while (count--)
563 			*--tmp = *--s;
564 		}
565 
566 	return dest;
567 }
568 #endif
569 
570 #ifndef __HAVE_ARCH_MEMCMP
571 /**
572  * memcmp - Compare two areas of memory
573  * @cs: One area of memory
574  * @ct: Another area of memory
575  * @count: The size of the area.
576  */
577 int memcmp(const void * cs,const void * ct,size_t count)
578 {
579 	const unsigned char *su1, *su2;
580 	int res = 0;
581 
582 	for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
583 		if ((res = *su1 - *su2) != 0)
584 			break;
585 	return res;
586 }
587 #endif
588 
589 #ifndef __HAVE_ARCH_MEMSCAN
590 /**
591  * memscan - Find a character in an area of memory.
592  * @addr: The memory area
593  * @c: The byte to search for
594  * @size: The size of the area.
595  *
596  * returns the address of the first occurrence of @c, or 1 byte past
597  * the area if @c is not found
598  */
599 void * memscan(void * addr, int c, size_t size)
600 {
601 	unsigned char * p = (unsigned char *) addr;
602 
603 	while (size) {
604 		if (*p == c)
605 			return (void *) p;
606 		p++;
607 		size--;
608 	}
609 	return (void *) p;
610 }
611 #endif
612 
613 #ifndef __HAVE_ARCH_STRSTR
614 /**
615  * strstr - Find the first substring in a %NUL terminated string
616  * @s1: The string to be searched
617  * @s2: The string to search for
618  */
619 char * strstr(const char * s1,const char * s2)
620 {
621 	int l1, l2;
622 
623 	l2 = strlen(s2);
624 	if (!l2)
625 		return (char *) s1;
626 	l1 = strlen(s1);
627 	while (l1 >= l2) {
628 		l1--;
629 		if (!memcmp(s1,s2,l2))
630 			return (char *) s1;
631 		s1++;
632 	}
633 	return NULL;
634 }
635 #endif
636 
637 #ifndef __HAVE_ARCH_MEMCHR
638 /**
639  * memchr - Find a character in an area of memory.
640  * @s: The memory area
641  * @c: The byte to search for
642  * @n: The size of the area.
643  *
644  * returns the address of the first occurrence of @c, or %NULL
645  * if @c is not found
646  */
647 void *memchr(const void *s, int c, size_t n)
648 {
649 	const unsigned char *p = s;
650 	while (n-- != 0) {
651 		if ((unsigned char)c == *p++) {
652 			return (void *)(p-1);
653 		}
654 	}
655 	return NULL;
656 }
657 
658 #endif
659