1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <asm/sections.h>
19#include <config.h>
20#include <limits.h>
21#include <linux/compiler.h>
22#include <linux/ctype.h>
23#include <linux/string.h>
24#include <linux/types.h>
25#include <malloc.h>
26
27
28
29
30
31
32
33int strncasecmp(const char *s1, const char *s2, size_t len)
34{
35
36 unsigned char c1, c2;
37
38 c1 = 0; c2 = 0;
39 if (len) {
40 do {
41 c1 = *s1; c2 = *s2;
42 s1++; s2++;
43 if (!c1)
44 break;
45 if (!c2)
46 break;
47 if (c1 == c2)
48 continue;
49 c1 = tolower(c1);
50 c2 = tolower(c2);
51 if (c1 != c2)
52 break;
53 } while (--len);
54 }
55 return (int)c1 - (int)c2;
56}
57
58
59
60
61
62
63int strcasecmp(const char *s1, const char *s2)
64{
65 return strncasecmp(s1, s2, -1U);
66}
67
68char * ___strtok;
69
70#ifndef __HAVE_ARCH_STRCPY
71
72
73
74
75
76char * strcpy(char * dest,const char *src)
77{
78 char *tmp = dest;
79
80 while ((*dest++ = *src++) != '\0')
81 ;
82 return tmp;
83}
84#endif
85
86#ifndef __HAVE_ARCH_STRNCPY
87
88
89
90
91
92
93
94
95
96
97char * strncpy(char * dest,const char *src,size_t count)
98{
99 char *tmp = dest;
100
101 while (count-- && (*dest++ = *src++) != '\0')
102 ;
103
104 return tmp;
105}
106#endif
107
108#ifndef __HAVE_ARCH_STRLCPY
109
110
111
112
113
114
115
116
117
118
119
120
121
122size_t strlcpy(char *dest, const char *src, size_t size)
123{
124 size_t ret = strlen(src);
125
126 if (size) {
127 size_t len = (ret >= size) ? size - 1 : ret;
128 memcpy(dest, src, len);
129 dest[len] = '\0';
130 }
131 return ret;
132}
133#endif
134
135#ifndef __HAVE_ARCH_STRCAT
136
137
138
139
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
157
158
159
160
161
162
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_STRLCAT
184
185
186
187
188
189
190
191
192
193
194
195
196size_t strlcat(char *dest, const char *src, size_t size)
197{
198 size_t len = strnlen(dest, size);
199
200 return len + strlcpy(dest + len, src, size - len);
201}
202#endif
203
204#ifndef __HAVE_ARCH_STRCMP
205
206
207
208
209
210int strcmp(const char *cs, const char *ct)
211{
212 int ret;
213
214 while (1) {
215 unsigned char a = *cs++;
216 unsigned char b = *ct++;
217
218 ret = a - b;
219 if (ret || !b)
220 break;
221 }
222
223 return ret;
224}
225#endif
226
227#ifndef __HAVE_ARCH_STRNCMP
228
229
230
231
232
233
234int strncmp(const char *cs, const char *ct, size_t count)
235{
236 int ret = 0;
237
238 while (count--) {
239 unsigned char a = *cs++;
240 unsigned char b = *ct++;
241
242 ret = a - b;
243 if (ret || !b)
244 break;
245 }
246
247 return ret;
248}
249#endif
250
251#ifndef __HAVE_ARCH_STRCHR
252
253
254
255
256
257char * strchr(const char * s, int c)
258{
259 for(; *s != (char) c; ++s)
260 if (*s == '\0')
261 return NULL;
262 return (char *) s;
263}
264#endif
265
266const char *strchrnul(const char *s, int c)
267{
268 for (; *s != (char)c; ++s)
269 if (*s == '\0')
270 break;
271 return s;
272}
273
274#ifndef __HAVE_ARCH_STRRCHR
275
276
277
278
279
280char * strrchr(const char * s, int c)
281{
282 const char *p = s + strlen(s);
283 do {
284 if (*p == (char)c)
285 return (char *)p;
286 } while (--p >= s);
287 return NULL;
288}
289#endif
290
291#ifndef __HAVE_ARCH_STRLEN
292
293
294
295
296size_t strlen(const char * s)
297{
298 const char *sc;
299
300 for (sc = s; *sc != '\0'; ++sc)
301 ;
302 return sc - s;
303}
304#endif
305
306#ifndef __HAVE_ARCH_STRNLEN
307
308
309
310
311
312size_t strnlen(const char * s, size_t count)
313{
314 const char *sc;
315
316 for (sc = s; count-- && *sc != '\0'; ++sc)
317 ;
318 return sc - s;
319}
320#endif
321
322#ifndef __HAVE_ARCH_STRCSPN
323
324
325
326
327
328
329size_t strcspn(const char *s, const char *reject)
330{
331 const char *p;
332 const char *r;
333 size_t count = 0;
334
335 for (p = s; *p != '\0'; ++p) {
336 for (r = reject; *r != '\0'; ++r) {
337 if (*p == *r)
338 return count;
339 }
340 ++count;
341 }
342 return count;
343}
344#endif
345
346#ifndef __HAVE_ARCH_STRDUP
347char * strdup(const char *s)
348{
349 char *new;
350
351 if ((s == NULL) ||
352 ((new = malloc (strlen(s) + 1)) == NULL) ) {
353 return NULL;
354 }
355
356 strcpy (new, s);
357 return new;
358}
359
360char * strndup(const char *s, size_t n)
361{
362 size_t len;
363 char *new;
364
365 if (s == NULL)
366 return NULL;
367
368 len = strlen(s);
369
370 if (n < len)
371 len = n;
372
373 new = malloc(len + 1);
374 if (new == NULL)
375 return NULL;
376
377 strncpy(new, s, len);
378 new[len] = '\0';
379
380 return new;
381}
382#endif
383
384#ifndef __HAVE_ARCH_STRSPN
385
386
387
388
389
390
391size_t strspn(const char *s, const char *accept)
392{
393 const char *p;
394 const char *a;
395 size_t count = 0;
396
397 for (p = s; *p != '\0'; ++p) {
398 for (a = accept; *a != '\0'; ++a) {
399 if (*p == *a)
400 break;
401 }
402 if (*a == '\0')
403 return count;
404 ++count;
405 }
406
407 return count;
408}
409#endif
410
411#ifndef __HAVE_ARCH_STRPBRK
412
413
414
415
416
417char * strpbrk(const char * cs,const char * ct)
418{
419 const char *sc1,*sc2;
420
421 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
422 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
423 if (*sc1 == *sc2)
424 return (char *) sc1;
425 }
426 }
427 return NULL;
428}
429#endif
430
431#ifndef __HAVE_ARCH_STRTOK
432
433
434
435
436
437
438
439char * strtok(char * s,const char * ct)
440{
441 char *sbegin, *send;
442
443 sbegin = s ? s : ___strtok;
444 if (!sbegin) {
445 return NULL;
446 }
447 sbegin += strspn(sbegin,ct);
448 if (*sbegin == '\0') {
449 ___strtok = NULL;
450 return( NULL );
451 }
452 send = strpbrk( sbegin, ct);
453 if (send && *send != '\0')
454 *send++ = '\0';
455 ___strtok = send;
456 return (sbegin);
457}
458#endif
459
460#ifndef __HAVE_ARCH_STRSEP
461
462
463
464
465
466
467
468
469
470
471
472char * strsep(char **s, const char *ct)
473{
474 char *sbegin = *s, *end;
475
476 if (sbegin == NULL)
477 return NULL;
478
479 end = strpbrk(sbegin, ct);
480 if (end)
481 *end++ = '\0';
482 *s = end;
483
484 return sbegin;
485}
486#endif
487
488#ifndef __HAVE_ARCH_STRSWAB
489
490
491
492
493
494
495
496char *strswab(const char *s)
497{
498 char *p, *q;
499
500 if ((NULL == s) || ('\0' == *s)) {
501 return (NULL);
502 }
503
504 for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
505 char tmp;
506
507 tmp = *p;
508 *p = *q;
509 *q = tmp;
510 }
511
512 return (char *) s;
513}
514#endif
515
516#ifndef __HAVE_ARCH_MEMSET
517
518
519
520
521
522
523
524
525__used void * memset(void * s,int c,size_t count)
526{
527 unsigned long *sl = (unsigned long *) s;
528 char *s8;
529
530#if !CONFIG_IS_ENABLED(TINY_MEMSET)
531 unsigned long cl = 0;
532 int i;
533
534
535 if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
536 for (i = 0; i < sizeof(*sl); i++) {
537 cl <<= 8;
538 cl |= c & 0xff;
539 }
540 while (count >= sizeof(*sl)) {
541 *sl++ = cl;
542 count -= sizeof(*sl);
543 }
544 }
545#endif
546 s8 = (char *)sl;
547 while (count--)
548 *s8++ = c;
549
550 return s;
551}
552#endif
553
554#ifndef __HAVE_ARCH_MEMCPY
555
556
557
558
559
560
561
562
563
564__rcode __used void *memcpy(void *dest, const void *src, size_t count)
565{
566 unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
567 char *d8, *s8;
568
569 if (src == dest)
570 return dest;
571
572
573 if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
574 while (count >= sizeof(*dl)) {
575 *dl++ = *sl++;
576 count -= sizeof(*dl);
577 }
578 }
579
580 d8 = (char *)dl;
581 s8 = (char *)sl;
582 while (count--)
583 *d8++ = *s8++;
584
585 return dest;
586}
587#endif
588
589#ifndef __HAVE_ARCH_MEMMOVE
590
591
592
593
594
595
596
597
598__rcode __used void *memmove(void *dest, const void *src, size_t count)
599{
600 char *tmp, *s;
601
602 if (dest <= src || (src + count) <= dest) {
603
604
605
606
607
608
609
610
611
612
613
614
615 memcpy(dest, src, count);
616 } else {
617 tmp = (char *) dest + count;
618 s = (char *) src + count;
619 while (count--)
620 *--tmp = *--s;
621 }
622
623 return dest;
624}
625#endif
626
627#ifndef __HAVE_ARCH_MEMCMP
628
629
630
631
632
633
634__used int memcmp(const void * cs,const void * ct,size_t count)
635{
636 const unsigned char *su1, *su2;
637 int res = 0;
638
639 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
640 if ((res = *su1 - *su2) != 0)
641 break;
642 return res;
643}
644#endif
645
646#ifndef __HAVE_ARCH_MEMSCAN
647
648
649
650
651
652
653
654
655
656void * memscan(void * addr, int c, size_t size)
657{
658 unsigned char * p = (unsigned char *) addr;
659
660 while (size) {
661 if (*p == c)
662 return (void *) p;
663 p++;
664 size--;
665 }
666 return (void *) p;
667}
668#endif
669
670char *memdup(const void *src, size_t len)
671{
672 char *p;
673
674 p = malloc(len);
675 if (!p)
676 return NULL;
677
678 memcpy(p, src, len);
679
680 return p;
681}
682
683#ifndef __HAVE_ARCH_STRNSTR
684
685
686
687
688
689
690
691
692
693char *strnstr(const char *s1, const char *s2, size_t len)
694{
695 size_t l1, l2;
696
697 l1 = strnlen(s1, len);
698 l2 = strlen(s2);
699
700 for (; l1 >= l2; --l1, ++s1) {
701 if (!memcmp(s1, s2, l2))
702 return (char *) s1;
703 }
704
705 return NULL;
706}
707#endif
708
709#ifndef __HAVE_ARCH_STRSTR
710
711
712
713
714
715
716
717
718
719char *strstr(const char *s1, const char *s2)
720{
721 return strnstr(s1, s2, SIZE_MAX);
722}
723#endif
724
725#ifndef __HAVE_ARCH_MEMCHR
726
727
728
729
730
731
732
733
734
735void *memchr(const void *s, int c, size_t n)
736{
737 const unsigned char *p = s;
738 while (n-- != 0) {
739 if ((unsigned char)c == *p++) {
740 return (void *)(p-1);
741 }
742 }
743 return NULL;
744}
745
746#endif
747#ifndef __HAVE_ARCH_MEMCHR_INV
748static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
749{
750 while (bytes) {
751 if (*start != value)
752 return (void *)start;
753 start++;
754 bytes--;
755 }
756 return NULL;
757}
758
759
760
761
762
763
764
765
766
767void *memchr_inv(const void *start, int c, size_t bytes)
768{
769 u8 value = c;
770 u64 value64;
771 unsigned int words, prefix;
772
773 if (bytes <= 16)
774 return check_bytes8(start, value, bytes);
775
776 value64 = value;
777 value64 |= value64 << 8;
778 value64 |= value64 << 16;
779 value64 |= value64 << 32;
780
781 prefix = (unsigned long)start % 8;
782 if (prefix) {
783 u8 *r;
784
785 prefix = 8 - prefix;
786 r = check_bytes8(start, value, prefix);
787 if (r)
788 return r;
789 start += prefix;
790 bytes -= prefix;
791 }
792
793 words = bytes / 8;
794
795 while (words) {
796 if (*(u64 *)start != value64)
797 return check_bytes8(start, value, 8);
798 start += 8;
799 words--;
800 }
801
802 return check_bytes8(start, value, bytes % 8);
803}
804#endif
805