1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * linux/lib/string.c
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 1991, 1992 Linus Torvalds
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun /*
8*4882a593Smuzhiyun * stupid library routines.. The optimized versions should generally be found
9*4882a593Smuzhiyun * as inline code in <asm-xx/string.h>
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * These are buggy as well..
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
14*4882a593Smuzhiyun * - Added strsep() which will replace strtok() soon (because strsep() is
15*4882a593Smuzhiyun * reentrant and should be faster). Use only strsep() in new code, please.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/types.h>
19*4882a593Smuzhiyun #include <linux/string.h>
20*4882a593Smuzhiyun #include <linux/ctype.h>
21*4882a593Smuzhiyun #include <malloc.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun * strncasecmp - Case insensitive, length-limited string comparison
26*4882a593Smuzhiyun * @s1: One string
27*4882a593Smuzhiyun * @s2: The other string
28*4882a593Smuzhiyun * @len: the maximum number of characters to compare
29*4882a593Smuzhiyun */
strncasecmp(const char * s1,const char * s2,size_t len)30*4882a593Smuzhiyun int strncasecmp(const char *s1, const char *s2, size_t len)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun /* Yes, Virginia, it had better be unsigned */
33*4882a593Smuzhiyun unsigned char c1, c2;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun c1 = 0; c2 = 0;
36*4882a593Smuzhiyun if (len) {
37*4882a593Smuzhiyun do {
38*4882a593Smuzhiyun c1 = *s1; c2 = *s2;
39*4882a593Smuzhiyun s1++; s2++;
40*4882a593Smuzhiyun if (!c1)
41*4882a593Smuzhiyun break;
42*4882a593Smuzhiyun if (!c2)
43*4882a593Smuzhiyun break;
44*4882a593Smuzhiyun if (c1 == c2)
45*4882a593Smuzhiyun continue;
46*4882a593Smuzhiyun c1 = tolower(c1);
47*4882a593Smuzhiyun c2 = tolower(c2);
48*4882a593Smuzhiyun if (c1 != c2)
49*4882a593Smuzhiyun break;
50*4882a593Smuzhiyun } while (--len);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun return (int)c1 - (int)c2;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun * strcasecmp - Case insensitive string comparison
57*4882a593Smuzhiyun * @s1: One string
58*4882a593Smuzhiyun * @s2: The other string
59*4882a593Smuzhiyun */
strcasecmp(const char * s1,const char * s2)60*4882a593Smuzhiyun int strcasecmp(const char *s1, const char *s2)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun return strncasecmp(s1, s2, -1U);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun char * ___strtok;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRCPY
68*4882a593Smuzhiyun /**
69*4882a593Smuzhiyun * strcpy - Copy a %NUL terminated string
70*4882a593Smuzhiyun * @dest: Where to copy the string to
71*4882a593Smuzhiyun * @src: Where to copy the string from
72*4882a593Smuzhiyun */
strcpy(char * dest,const char * src)73*4882a593Smuzhiyun char * strcpy(char * dest,const char *src)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun char *tmp = dest;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun while ((*dest++ = *src++) != '\0')
78*4882a593Smuzhiyun /* nothing */;
79*4882a593Smuzhiyun return tmp;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun #endif
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRNCPY
84*4882a593Smuzhiyun /**
85*4882a593Smuzhiyun * strncpy - Copy a length-limited, %NUL-terminated string
86*4882a593Smuzhiyun * @dest: Where to copy the string to
87*4882a593Smuzhiyun * @src: Where to copy the string from
88*4882a593Smuzhiyun * @count: The maximum number of bytes to copy
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
91*4882a593Smuzhiyun * However, the result is not %NUL-terminated if the source exceeds
92*4882a593Smuzhiyun * @count bytes.
93*4882a593Smuzhiyun */
strncpy(char * dest,const char * src,size_t count)94*4882a593Smuzhiyun char * strncpy(char * dest,const char *src,size_t count)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun char *tmp = dest;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun while (count-- && (*dest++ = *src++) != '\0')
99*4882a593Smuzhiyun /* nothing */;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return tmp;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun #endif
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRLCPY
106*4882a593Smuzhiyun /**
107*4882a593Smuzhiyun * strlcpy - Copy a C-string into a sized buffer
108*4882a593Smuzhiyun * @dest: Where to copy the string to
109*4882a593Smuzhiyun * @src: Where to copy the string from
110*4882a593Smuzhiyun * @size: size of destination buffer
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * Compatible with *BSD: the result is always a valid
113*4882a593Smuzhiyun * NUL-terminated string that fits in the buffer (unless,
114*4882a593Smuzhiyun * of course, the buffer size is zero). It does not pad
115*4882a593Smuzhiyun * out the result like strncpy() does.
116*4882a593Smuzhiyun */
strlcpy(char * dest,const char * src,size_t size)117*4882a593Smuzhiyun size_t strlcpy(char *dest, const char *src, size_t size)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun size_t ret = strlen(src);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (size) {
122*4882a593Smuzhiyun size_t len = (ret >= size) ? size - 1 : ret;
123*4882a593Smuzhiyun memcpy(dest, src, len);
124*4882a593Smuzhiyun dest[len] = '\0';
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun return ret;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRCAT
131*4882a593Smuzhiyun /**
132*4882a593Smuzhiyun * strcat - Append one %NUL-terminated string to another
133*4882a593Smuzhiyun * @dest: The string to be appended to
134*4882a593Smuzhiyun * @src: The string to append to it
135*4882a593Smuzhiyun */
strcat(char * dest,const char * src)136*4882a593Smuzhiyun char * strcat(char * dest, const char * src)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun char *tmp = dest;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun while (*dest)
141*4882a593Smuzhiyun dest++;
142*4882a593Smuzhiyun while ((*dest++ = *src++) != '\0')
143*4882a593Smuzhiyun ;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun return tmp;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun #endif
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRNCAT
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun * strncat - Append a length-limited, %NUL-terminated string to another
152*4882a593Smuzhiyun * @dest: The string to be appended to
153*4882a593Smuzhiyun * @src: The string to append to it
154*4882a593Smuzhiyun * @count: The maximum numbers of bytes to copy
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * Note that in contrast to strncpy, strncat ensures the result is
157*4882a593Smuzhiyun * terminated.
158*4882a593Smuzhiyun */
strncat(char * dest,const char * src,size_t count)159*4882a593Smuzhiyun char * strncat(char *dest, const char *src, size_t count)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun char *tmp = dest;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (count) {
164*4882a593Smuzhiyun while (*dest)
165*4882a593Smuzhiyun dest++;
166*4882a593Smuzhiyun while ((*dest++ = *src++)) {
167*4882a593Smuzhiyun if (--count == 0) {
168*4882a593Smuzhiyun *dest = '\0';
169*4882a593Smuzhiyun break;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun return tmp;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun #endif
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRCMP
179*4882a593Smuzhiyun /**
180*4882a593Smuzhiyun * strcmp - Compare two strings
181*4882a593Smuzhiyun * @cs: One string
182*4882a593Smuzhiyun * @ct: Another string
183*4882a593Smuzhiyun */
strcmp(const char * cs,const char * ct)184*4882a593Smuzhiyun int strcmp(const char * cs,const char * ct)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun register signed char __res;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun while (1) {
189*4882a593Smuzhiyun if ((__res = *cs - *ct++) != 0 || !*cs++)
190*4882a593Smuzhiyun break;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun return __res;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun #endif
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRNCMP
198*4882a593Smuzhiyun /**
199*4882a593Smuzhiyun * strncmp - Compare two length-limited strings
200*4882a593Smuzhiyun * @cs: One string
201*4882a593Smuzhiyun * @ct: Another string
202*4882a593Smuzhiyun * @count: The maximum number of bytes to compare
203*4882a593Smuzhiyun */
strncmp(const char * cs,const char * ct,size_t count)204*4882a593Smuzhiyun int strncmp(const char * cs,const char * ct,size_t count)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun register signed char __res = 0;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun while (count) {
209*4882a593Smuzhiyun if ((__res = *cs - *ct++) != 0 || !*cs++)
210*4882a593Smuzhiyun break;
211*4882a593Smuzhiyun count--;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun return __res;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun #endif
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRCHR
219*4882a593Smuzhiyun /**
220*4882a593Smuzhiyun * strchr - Find the first occurrence of a character in a string
221*4882a593Smuzhiyun * @s: The string to be searched
222*4882a593Smuzhiyun * @c: The character to search for
223*4882a593Smuzhiyun */
strchr(const char * s,int c)224*4882a593Smuzhiyun char * strchr(const char * s, int c)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun for(; *s != (char) c; ++s)
227*4882a593Smuzhiyun if (*s == '\0')
228*4882a593Smuzhiyun return NULL;
229*4882a593Smuzhiyun return (char *) s;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun #endif
232*4882a593Smuzhiyun
strchrnul(const char * s,int c)233*4882a593Smuzhiyun const char *strchrnul(const char *s, int c)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun for (; *s != (char)c; ++s)
236*4882a593Smuzhiyun if (*s == '\0')
237*4882a593Smuzhiyun break;
238*4882a593Smuzhiyun return s;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRRCHR
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun * strrchr - Find the last occurrence of a character in a string
244*4882a593Smuzhiyun * @s: The string to be searched
245*4882a593Smuzhiyun * @c: The character to search for
246*4882a593Smuzhiyun */
strrchr(const char * s,int c)247*4882a593Smuzhiyun char * strrchr(const char * s, int c)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun const char *p = s + strlen(s);
250*4882a593Smuzhiyun do {
251*4882a593Smuzhiyun if (*p == (char)c)
252*4882a593Smuzhiyun return (char *)p;
253*4882a593Smuzhiyun } while (--p >= s);
254*4882a593Smuzhiyun return NULL;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun #endif
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRLEN
259*4882a593Smuzhiyun /**
260*4882a593Smuzhiyun * strlen - Find the length of a string
261*4882a593Smuzhiyun * @s: The string to be sized
262*4882a593Smuzhiyun */
strlen(const char * s)263*4882a593Smuzhiyun size_t strlen(const char * s)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun const char *sc;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun for (sc = s; *sc != '\0'; ++sc)
268*4882a593Smuzhiyun /* nothing */;
269*4882a593Smuzhiyun return sc - s;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun #endif
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRNLEN
274*4882a593Smuzhiyun /**
275*4882a593Smuzhiyun * strnlen - Find the length of a length-limited string
276*4882a593Smuzhiyun * @s: The string to be sized
277*4882a593Smuzhiyun * @count: The maximum number of bytes to search
278*4882a593Smuzhiyun */
strnlen(const char * s,size_t count)279*4882a593Smuzhiyun size_t strnlen(const char * s, size_t count)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun const char *sc;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun for (sc = s; count-- && *sc != '\0'; ++sc)
284*4882a593Smuzhiyun /* nothing */;
285*4882a593Smuzhiyun return sc - s;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun #endif
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRCSPN
290*4882a593Smuzhiyun /**
291*4882a593Smuzhiyun * strcspn - Calculate the length of the initial substring of @s which does
292*4882a593Smuzhiyun * not contain letters in @reject
293*4882a593Smuzhiyun * @s: The string to be searched
294*4882a593Smuzhiyun * @reject: The string to avoid
295*4882a593Smuzhiyun */
strcspn(const char * s,const char * reject)296*4882a593Smuzhiyun size_t strcspn(const char *s, const char *reject)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun const char *p;
299*4882a593Smuzhiyun const char *r;
300*4882a593Smuzhiyun size_t count = 0;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun for (p = s; *p != '\0'; ++p) {
303*4882a593Smuzhiyun for (r = reject; *r != '\0'; ++r) {
304*4882a593Smuzhiyun if (*p == *r)
305*4882a593Smuzhiyun return count;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun ++count;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun return count;
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun #endif
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRDUP
strdup(const char * s)314*4882a593Smuzhiyun char * strdup(const char *s)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun char *new;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun if ((s == NULL) ||
319*4882a593Smuzhiyun ((new = malloc (strlen(s) + 1)) == NULL) ) {
320*4882a593Smuzhiyun return NULL;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun strcpy (new, s);
324*4882a593Smuzhiyun return new;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun #endif
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRSPN
329*4882a593Smuzhiyun /**
330*4882a593Smuzhiyun * strspn - Calculate the length of the initial substring of @s which only
331*4882a593Smuzhiyun * contain letters in @accept
332*4882a593Smuzhiyun * @s: The string to be searched
333*4882a593Smuzhiyun * @accept: The string to search for
334*4882a593Smuzhiyun */
strspn(const char * s,const char * accept)335*4882a593Smuzhiyun size_t strspn(const char *s, const char *accept)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun const char *p;
338*4882a593Smuzhiyun const char *a;
339*4882a593Smuzhiyun size_t count = 0;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun for (p = s; *p != '\0'; ++p) {
342*4882a593Smuzhiyun for (a = accept; *a != '\0'; ++a) {
343*4882a593Smuzhiyun if (*p == *a)
344*4882a593Smuzhiyun break;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun if (*a == '\0')
347*4882a593Smuzhiyun return count;
348*4882a593Smuzhiyun ++count;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun return count;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun #endif
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRPBRK
356*4882a593Smuzhiyun /**
357*4882a593Smuzhiyun * strpbrk - Find the first occurrence of a set of characters
358*4882a593Smuzhiyun * @cs: The string to be searched
359*4882a593Smuzhiyun * @ct: The characters to search for
360*4882a593Smuzhiyun */
strpbrk(const char * cs,const char * ct)361*4882a593Smuzhiyun char * strpbrk(const char * cs,const char * ct)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun const char *sc1,*sc2;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun for( sc1 = cs; *sc1 != '\0'; ++sc1) {
366*4882a593Smuzhiyun for( sc2 = ct; *sc2 != '\0'; ++sc2) {
367*4882a593Smuzhiyun if (*sc1 == *sc2)
368*4882a593Smuzhiyun return (char *) sc1;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun return NULL;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun #endif
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRTOK
376*4882a593Smuzhiyun /**
377*4882a593Smuzhiyun * strtok - Split a string into tokens
378*4882a593Smuzhiyun * @s: The string to be searched
379*4882a593Smuzhiyun * @ct: The characters to search for
380*4882a593Smuzhiyun *
381*4882a593Smuzhiyun * WARNING: strtok is deprecated, use strsep instead.
382*4882a593Smuzhiyun */
strtok(char * s,const char * ct)383*4882a593Smuzhiyun char * strtok(char * s,const char * ct)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun char *sbegin, *send;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun sbegin = s ? s : ___strtok;
388*4882a593Smuzhiyun if (!sbegin) {
389*4882a593Smuzhiyun return NULL;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun sbegin += strspn(sbegin,ct);
392*4882a593Smuzhiyun if (*sbegin == '\0') {
393*4882a593Smuzhiyun ___strtok = NULL;
394*4882a593Smuzhiyun return( NULL );
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun send = strpbrk( sbegin, ct);
397*4882a593Smuzhiyun if (send && *send != '\0')
398*4882a593Smuzhiyun *send++ = '\0';
399*4882a593Smuzhiyun ___strtok = send;
400*4882a593Smuzhiyun return (sbegin);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun #endif
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRSEP
405*4882a593Smuzhiyun /**
406*4882a593Smuzhiyun * strsep - Split a string into tokens
407*4882a593Smuzhiyun * @s: The string to be searched
408*4882a593Smuzhiyun * @ct: The characters to search for
409*4882a593Smuzhiyun *
410*4882a593Smuzhiyun * strsep() updates @s to point after the token, ready for the next call.
411*4882a593Smuzhiyun *
412*4882a593Smuzhiyun * It returns empty tokens, too, behaving exactly like the libc function
413*4882a593Smuzhiyun * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
414*4882a593Smuzhiyun * Same semantics, slimmer shape. ;)
415*4882a593Smuzhiyun */
strsep(char ** s,const char * ct)416*4882a593Smuzhiyun char * strsep(char **s, const char *ct)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun char *sbegin = *s, *end;
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun if (sbegin == NULL)
421*4882a593Smuzhiyun return NULL;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun end = strpbrk(sbegin, ct);
424*4882a593Smuzhiyun if (end)
425*4882a593Smuzhiyun *end++ = '\0';
426*4882a593Smuzhiyun *s = end;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun return sbegin;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun #endif
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRSWAB
433*4882a593Smuzhiyun /**
434*4882a593Smuzhiyun * strswab - swap adjacent even and odd bytes in %NUL-terminated string
435*4882a593Smuzhiyun * s: address of the string
436*4882a593Smuzhiyun *
437*4882a593Smuzhiyun * returns the address of the swapped string or NULL on error. If
438*4882a593Smuzhiyun * string length is odd, last byte is untouched.
439*4882a593Smuzhiyun */
strswab(const char * s)440*4882a593Smuzhiyun char *strswab(const char *s)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun char *p, *q;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun if ((NULL == s) || ('\0' == *s)) {
445*4882a593Smuzhiyun return (NULL);
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
449*4882a593Smuzhiyun char tmp;
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun tmp = *p;
452*4882a593Smuzhiyun *p = *q;
453*4882a593Smuzhiyun *q = tmp;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun return (char *) s;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun #endif
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun #ifndef __HAVE_ARCH_MEMSET
461*4882a593Smuzhiyun /**
462*4882a593Smuzhiyun * memset - Fill a region of memory with the given value
463*4882a593Smuzhiyun * @s: Pointer to the start of the area.
464*4882a593Smuzhiyun * @c: The byte to fill the area with
465*4882a593Smuzhiyun * @count: The size of the area.
466*4882a593Smuzhiyun *
467*4882a593Smuzhiyun * Do not use memset() to access IO space, use memset_io() instead.
468*4882a593Smuzhiyun */
memset(void * s,int c,size_t count)469*4882a593Smuzhiyun void * memset(void * s,int c,size_t count)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun unsigned long *sl = (unsigned long *) s;
472*4882a593Smuzhiyun char *s8;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(TINY_MEMSET)
475*4882a593Smuzhiyun unsigned long cl = 0;
476*4882a593Smuzhiyun int i;
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun /* do it one word at a time (32 bits or 64 bits) while possible */
479*4882a593Smuzhiyun if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
480*4882a593Smuzhiyun for (i = 0; i < sizeof(*sl); i++) {
481*4882a593Smuzhiyun cl <<= 8;
482*4882a593Smuzhiyun cl |= c & 0xff;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun while (count >= sizeof(*sl)) {
485*4882a593Smuzhiyun *sl++ = cl;
486*4882a593Smuzhiyun count -= sizeof(*sl);
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun #endif /* fill 8 bits at a time */
490*4882a593Smuzhiyun s8 = (char *)sl;
491*4882a593Smuzhiyun while (count--)
492*4882a593Smuzhiyun *s8++ = c;
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun return s;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun #endif
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun #ifndef __HAVE_ARCH_MEMCPY
499*4882a593Smuzhiyun /**
500*4882a593Smuzhiyun * memcpy - Copy one area of memory to another
501*4882a593Smuzhiyun * @dest: Where to copy to
502*4882a593Smuzhiyun * @src: Where to copy from
503*4882a593Smuzhiyun * @count: The size of the area.
504*4882a593Smuzhiyun *
505*4882a593Smuzhiyun * You should not use this function to access IO space, use memcpy_toio()
506*4882a593Smuzhiyun * or memcpy_fromio() instead.
507*4882a593Smuzhiyun */
memcpy(void * dest,const void * src,size_t count)508*4882a593Smuzhiyun void * memcpy(void *dest, const void *src, size_t count)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
511*4882a593Smuzhiyun char *d8, *s8;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun if (src == dest)
514*4882a593Smuzhiyun return dest;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun /* while all data is aligned (common case), copy a word at a time */
517*4882a593Smuzhiyun if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
518*4882a593Smuzhiyun while (count >= sizeof(*dl)) {
519*4882a593Smuzhiyun *dl++ = *sl++;
520*4882a593Smuzhiyun count -= sizeof(*dl);
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun /* copy the reset one byte at a time */
524*4882a593Smuzhiyun d8 = (char *)dl;
525*4882a593Smuzhiyun s8 = (char *)sl;
526*4882a593Smuzhiyun while (count--)
527*4882a593Smuzhiyun *d8++ = *s8++;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun return dest;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun #endif
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun #ifndef __HAVE_ARCH_MEMMOVE
534*4882a593Smuzhiyun /**
535*4882a593Smuzhiyun * memmove - Copy one area of memory to another
536*4882a593Smuzhiyun * @dest: Where to copy to
537*4882a593Smuzhiyun * @src: Where to copy from
538*4882a593Smuzhiyun * @count: The size of the area.
539*4882a593Smuzhiyun *
540*4882a593Smuzhiyun * Unlike memcpy(), memmove() copes with overlapping areas.
541*4882a593Smuzhiyun */
memmove(void * dest,const void * src,size_t count)542*4882a593Smuzhiyun void * memmove(void * dest,const void *src,size_t count)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun char *tmp, *s;
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun if (dest <= src) {
547*4882a593Smuzhiyun memcpy(dest, src, count);
548*4882a593Smuzhiyun } else {
549*4882a593Smuzhiyun tmp = (char *) dest + count;
550*4882a593Smuzhiyun s = (char *) src + count;
551*4882a593Smuzhiyun while (count--)
552*4882a593Smuzhiyun *--tmp = *--s;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun return dest;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun #endif
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun #ifndef __HAVE_ARCH_MEMCMP
560*4882a593Smuzhiyun /**
561*4882a593Smuzhiyun * memcmp - Compare two areas of memory
562*4882a593Smuzhiyun * @cs: One area of memory
563*4882a593Smuzhiyun * @ct: Another area of memory
564*4882a593Smuzhiyun * @count: The size of the area.
565*4882a593Smuzhiyun */
memcmp(const void * cs,const void * ct,size_t count)566*4882a593Smuzhiyun int memcmp(const void * cs,const void * ct,size_t count)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun const unsigned char *su1, *su2;
569*4882a593Smuzhiyun int res = 0;
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
572*4882a593Smuzhiyun if ((res = *su1 - *su2) != 0)
573*4882a593Smuzhiyun break;
574*4882a593Smuzhiyun return res;
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun #endif
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun #ifndef __HAVE_ARCH_MEMSCAN
579*4882a593Smuzhiyun /**
580*4882a593Smuzhiyun * memscan - Find a character in an area of memory.
581*4882a593Smuzhiyun * @addr: The memory area
582*4882a593Smuzhiyun * @c: The byte to search for
583*4882a593Smuzhiyun * @size: The size of the area.
584*4882a593Smuzhiyun *
585*4882a593Smuzhiyun * returns the address of the first occurrence of @c, or 1 byte past
586*4882a593Smuzhiyun * the area if @c is not found
587*4882a593Smuzhiyun */
memscan(void * addr,int c,size_t size)588*4882a593Smuzhiyun void * memscan(void * addr, int c, size_t size)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun unsigned char * p = (unsigned char *) addr;
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun while (size) {
593*4882a593Smuzhiyun if (*p == c)
594*4882a593Smuzhiyun return (void *) p;
595*4882a593Smuzhiyun p++;
596*4882a593Smuzhiyun size--;
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun return (void *) p;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun #endif
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun #ifndef __HAVE_ARCH_STRSTR
603*4882a593Smuzhiyun /**
604*4882a593Smuzhiyun * strstr - Find the first substring in a %NUL terminated string
605*4882a593Smuzhiyun * @s1: The string to be searched
606*4882a593Smuzhiyun * @s2: The string to search for
607*4882a593Smuzhiyun */
strstr(const char * s1,const char * s2)608*4882a593Smuzhiyun char * strstr(const char * s1,const char * s2)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun int l1, l2;
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun l2 = strlen(s2);
613*4882a593Smuzhiyun if (!l2)
614*4882a593Smuzhiyun return (char *) s1;
615*4882a593Smuzhiyun l1 = strlen(s1);
616*4882a593Smuzhiyun while (l1 >= l2) {
617*4882a593Smuzhiyun l1--;
618*4882a593Smuzhiyun if (!memcmp(s1,s2,l2))
619*4882a593Smuzhiyun return (char *) s1;
620*4882a593Smuzhiyun s1++;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun return NULL;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun #endif
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun #ifndef __HAVE_ARCH_MEMCHR
627*4882a593Smuzhiyun /**
628*4882a593Smuzhiyun * memchr - Find a character in an area of memory.
629*4882a593Smuzhiyun * @s: The memory area
630*4882a593Smuzhiyun * @c: The byte to search for
631*4882a593Smuzhiyun * @n: The size of the area.
632*4882a593Smuzhiyun *
633*4882a593Smuzhiyun * returns the address of the first occurrence of @c, or %NULL
634*4882a593Smuzhiyun * if @c is not found
635*4882a593Smuzhiyun */
memchr(const void * s,int c,size_t n)636*4882a593Smuzhiyun void *memchr(const void *s, int c, size_t n)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun const unsigned char *p = s;
639*4882a593Smuzhiyun while (n-- != 0) {
640*4882a593Smuzhiyun if ((unsigned char)c == *p++) {
641*4882a593Smuzhiyun return (void *)(p-1);
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun return NULL;
645*4882a593Smuzhiyun }
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun #endif
648*4882a593Smuzhiyun #ifndef __HAVE_ARCH_MEMCHR_INV
check_bytes8(const u8 * start,u8 value,unsigned int bytes)649*4882a593Smuzhiyun static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun while (bytes) {
652*4882a593Smuzhiyun if (*start != value)
653*4882a593Smuzhiyun return (void *)start;
654*4882a593Smuzhiyun start++;
655*4882a593Smuzhiyun bytes--;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun return NULL;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun /**
660*4882a593Smuzhiyun * memchr_inv - Find an unmatching character in an area of memory.
661*4882a593Smuzhiyun * @start: The memory area
662*4882a593Smuzhiyun * @c: Find a character other than c
663*4882a593Smuzhiyun * @bytes: The size of the area.
664*4882a593Smuzhiyun *
665*4882a593Smuzhiyun * returns the address of the first character other than @c, or %NULL
666*4882a593Smuzhiyun * if the whole buffer contains just @c.
667*4882a593Smuzhiyun */
memchr_inv(const void * start,int c,size_t bytes)668*4882a593Smuzhiyun void *memchr_inv(const void *start, int c, size_t bytes)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun u8 value = c;
671*4882a593Smuzhiyun u64 value64;
672*4882a593Smuzhiyun unsigned int words, prefix;
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun if (bytes <= 16)
675*4882a593Smuzhiyun return check_bytes8(start, value, bytes);
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun value64 = value;
678*4882a593Smuzhiyun value64 |= value64 << 8;
679*4882a593Smuzhiyun value64 |= value64 << 16;
680*4882a593Smuzhiyun value64 |= value64 << 32;
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun prefix = (unsigned long)start % 8;
683*4882a593Smuzhiyun if (prefix) {
684*4882a593Smuzhiyun u8 *r;
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun prefix = 8 - prefix;
687*4882a593Smuzhiyun r = check_bytes8(start, value, prefix);
688*4882a593Smuzhiyun if (r)
689*4882a593Smuzhiyun return r;
690*4882a593Smuzhiyun start += prefix;
691*4882a593Smuzhiyun bytes -= prefix;
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun words = bytes / 8;
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun while (words) {
697*4882a593Smuzhiyun if (*(u64 *)start != value64)
698*4882a593Smuzhiyun return check_bytes8(start, value, 8);
699*4882a593Smuzhiyun start += 8;
700*4882a593Smuzhiyun words--;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun return check_bytes8(start, value, bytes % 8);
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun #endif
706