1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* -*- linux-c -*- ------------------------------------------------------- *
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 1991, 1992 Linus Torvalds
5*4882a593Smuzhiyun * Copyright 2007 rPath, Inc. - All Rights Reserved
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * ----------------------------------------------------------------------- */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun * Very basic string functions
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <linux/compiler.h>
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <linux/limits.h>
17*4882a593Smuzhiyun #include <asm/asm.h>
18*4882a593Smuzhiyun #include "ctype.h"
19*4882a593Smuzhiyun #include "string.h"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define KSTRTOX_OVERFLOW (1U << 31)
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * Undef these macros so that the functions that we provide
25*4882a593Smuzhiyun * here will have the correct names regardless of how string.h
26*4882a593Smuzhiyun * may have chosen to #define them.
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun #undef memcpy
29*4882a593Smuzhiyun #undef memset
30*4882a593Smuzhiyun #undef memcmp
31*4882a593Smuzhiyun
memcmp(const void * s1,const void * s2,size_t len)32*4882a593Smuzhiyun int memcmp(const void *s1, const void *s2, size_t len)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun bool diff;
35*4882a593Smuzhiyun asm("repe; cmpsb" CC_SET(nz)
36*4882a593Smuzhiyun : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len));
37*4882a593Smuzhiyun return diff;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun * Clang may lower `memcmp == 0` to `bcmp == 0`.
42*4882a593Smuzhiyun */
bcmp(const void * s1,const void * s2,size_t len)43*4882a593Smuzhiyun int bcmp(const void *s1, const void *s2, size_t len)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun return memcmp(s1, s2, len);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
strcmp(const char * str1,const char * str2)48*4882a593Smuzhiyun int strcmp(const char *str1, const char *str2)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun const unsigned char *s1 = (const unsigned char *)str1;
51*4882a593Smuzhiyun const unsigned char *s2 = (const unsigned char *)str2;
52*4882a593Smuzhiyun int delta = 0;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun while (*s1 || *s2) {
55*4882a593Smuzhiyun delta = *s1 - *s2;
56*4882a593Smuzhiyun if (delta)
57*4882a593Smuzhiyun return delta;
58*4882a593Smuzhiyun s1++;
59*4882a593Smuzhiyun s2++;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
strncmp(const char * cs,const char * ct,size_t count)64*4882a593Smuzhiyun int strncmp(const char *cs, const char *ct, size_t count)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun unsigned char c1, c2;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun while (count) {
69*4882a593Smuzhiyun c1 = *cs++;
70*4882a593Smuzhiyun c2 = *ct++;
71*4882a593Smuzhiyun if (c1 != c2)
72*4882a593Smuzhiyun return c1 < c2 ? -1 : 1;
73*4882a593Smuzhiyun if (!c1)
74*4882a593Smuzhiyun break;
75*4882a593Smuzhiyun count--;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun return 0;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
strnlen(const char * s,size_t maxlen)80*4882a593Smuzhiyun size_t strnlen(const char *s, size_t maxlen)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun const char *es = s;
83*4882a593Smuzhiyun while (*es && maxlen) {
84*4882a593Smuzhiyun es++;
85*4882a593Smuzhiyun maxlen--;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return (es - s);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
atou(const char * s)91*4882a593Smuzhiyun unsigned int atou(const char *s)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun unsigned int i = 0;
94*4882a593Smuzhiyun while (isdigit(*s))
95*4882a593Smuzhiyun i = i * 10 + (*s++ - '0');
96*4882a593Smuzhiyun return i;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* Works only for digits and letters, but small and fast */
100*4882a593Smuzhiyun #define TOLOWER(x) ((x) | 0x20)
101*4882a593Smuzhiyun
simple_guess_base(const char * cp)102*4882a593Smuzhiyun static unsigned int simple_guess_base(const char *cp)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun if (cp[0] == '0') {
105*4882a593Smuzhiyun if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
106*4882a593Smuzhiyun return 16;
107*4882a593Smuzhiyun else
108*4882a593Smuzhiyun return 8;
109*4882a593Smuzhiyun } else {
110*4882a593Smuzhiyun return 10;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun * simple_strtoull - convert a string to an unsigned long long
116*4882a593Smuzhiyun * @cp: The start of the string
117*4882a593Smuzhiyun * @endp: A pointer to the end of the parsed string will be placed here
118*4882a593Smuzhiyun * @base: The number base to use
119*4882a593Smuzhiyun */
simple_strtoull(const char * cp,char ** endp,unsigned int base)120*4882a593Smuzhiyun unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun unsigned long long result = 0;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun if (!base)
125*4882a593Smuzhiyun base = simple_guess_base(cp);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
128*4882a593Smuzhiyun cp += 2;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun while (isxdigit(*cp)) {
131*4882a593Smuzhiyun unsigned int value;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
134*4882a593Smuzhiyun if (value >= base)
135*4882a593Smuzhiyun break;
136*4882a593Smuzhiyun result = result * base + value;
137*4882a593Smuzhiyun cp++;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun if (endp)
140*4882a593Smuzhiyun *endp = (char *)cp;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun return result;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
simple_strtol(const char * cp,char ** endp,unsigned int base)145*4882a593Smuzhiyun long simple_strtol(const char *cp, char **endp, unsigned int base)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun if (*cp == '-')
148*4882a593Smuzhiyun return -simple_strtoull(cp + 1, endp, base);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun return simple_strtoull(cp, endp, base);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /**
154*4882a593Smuzhiyun * strlen - Find the length of a string
155*4882a593Smuzhiyun * @s: The string to be sized
156*4882a593Smuzhiyun */
strlen(const char * s)157*4882a593Smuzhiyun size_t strlen(const char *s)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun const char *sc;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun for (sc = s; *sc != '\0'; ++sc)
162*4882a593Smuzhiyun /* nothing */;
163*4882a593Smuzhiyun return sc - s;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /**
167*4882a593Smuzhiyun * strstr - Find the first substring in a %NUL terminated string
168*4882a593Smuzhiyun * @s1: The string to be searched
169*4882a593Smuzhiyun * @s2: The string to search for
170*4882a593Smuzhiyun */
strstr(const char * s1,const char * s2)171*4882a593Smuzhiyun char *strstr(const char *s1, const char *s2)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun size_t l1, l2;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun l2 = strlen(s2);
176*4882a593Smuzhiyun if (!l2)
177*4882a593Smuzhiyun return (char *)s1;
178*4882a593Smuzhiyun l1 = strlen(s1);
179*4882a593Smuzhiyun while (l1 >= l2) {
180*4882a593Smuzhiyun l1--;
181*4882a593Smuzhiyun if (!memcmp(s1, s2, l2))
182*4882a593Smuzhiyun return (char *)s1;
183*4882a593Smuzhiyun s1++;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun return NULL;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /**
189*4882a593Smuzhiyun * strchr - Find the first occurrence of the character c in the string s.
190*4882a593Smuzhiyun * @s: the string to be searched
191*4882a593Smuzhiyun * @c: the character to search for
192*4882a593Smuzhiyun */
strchr(const char * s,int c)193*4882a593Smuzhiyun char *strchr(const char *s, int c)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun while (*s != (char)c)
196*4882a593Smuzhiyun if (*s++ == '\0')
197*4882a593Smuzhiyun return NULL;
198*4882a593Smuzhiyun return (char *)s;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
__div_u64_rem(u64 dividend,u32 divisor,u32 * remainder)201*4882a593Smuzhiyun static inline u64 __div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun union {
204*4882a593Smuzhiyun u64 v64;
205*4882a593Smuzhiyun u32 v32[2];
206*4882a593Smuzhiyun } d = { dividend };
207*4882a593Smuzhiyun u32 upper;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun upper = d.v32[1];
210*4882a593Smuzhiyun d.v32[1] = 0;
211*4882a593Smuzhiyun if (upper >= divisor) {
212*4882a593Smuzhiyun d.v32[1] = upper / divisor;
213*4882a593Smuzhiyun upper %= divisor;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
216*4882a593Smuzhiyun "rm" (divisor), "0" (d.v32[0]), "1" (upper));
217*4882a593Smuzhiyun return d.v64;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
__div_u64(u64 dividend,u32 divisor)220*4882a593Smuzhiyun static inline u64 __div_u64(u64 dividend, u32 divisor)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun u32 remainder;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun return __div_u64_rem(dividend, divisor, &remainder);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
_tolower(const char c)227*4882a593Smuzhiyun static inline char _tolower(const char c)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun return c | 0x20;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
_parse_integer_fixup_radix(const char * s,unsigned int * base)232*4882a593Smuzhiyun static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun if (*base == 0) {
235*4882a593Smuzhiyun if (s[0] == '0') {
236*4882a593Smuzhiyun if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
237*4882a593Smuzhiyun *base = 16;
238*4882a593Smuzhiyun else
239*4882a593Smuzhiyun *base = 8;
240*4882a593Smuzhiyun } else
241*4882a593Smuzhiyun *base = 10;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
244*4882a593Smuzhiyun s += 2;
245*4882a593Smuzhiyun return s;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*
249*4882a593Smuzhiyun * Convert non-negative integer string representation in explicitly given radix
250*4882a593Smuzhiyun * to an integer.
251*4882a593Smuzhiyun * Return number of characters consumed maybe or-ed with overflow bit.
252*4882a593Smuzhiyun * If overflow occurs, result integer (incorrect) is still returned.
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * Don't you dare use this function.
255*4882a593Smuzhiyun */
_parse_integer(const char * s,unsigned int base,unsigned long long * p)256*4882a593Smuzhiyun static unsigned int _parse_integer(const char *s,
257*4882a593Smuzhiyun unsigned int base,
258*4882a593Smuzhiyun unsigned long long *p)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun unsigned long long res;
261*4882a593Smuzhiyun unsigned int rv;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun res = 0;
264*4882a593Smuzhiyun rv = 0;
265*4882a593Smuzhiyun while (1) {
266*4882a593Smuzhiyun unsigned int c = *s;
267*4882a593Smuzhiyun unsigned int lc = c | 0x20; /* don't tolower() this line */
268*4882a593Smuzhiyun unsigned int val;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun if ('0' <= c && c <= '9')
271*4882a593Smuzhiyun val = c - '0';
272*4882a593Smuzhiyun else if ('a' <= lc && lc <= 'f')
273*4882a593Smuzhiyun val = lc - 'a' + 10;
274*4882a593Smuzhiyun else
275*4882a593Smuzhiyun break;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun if (val >= base)
278*4882a593Smuzhiyun break;
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun * Check for overflow only if we are within range of
281*4882a593Smuzhiyun * it in the max base we support (16)
282*4882a593Smuzhiyun */
283*4882a593Smuzhiyun if (unlikely(res & (~0ull << 60))) {
284*4882a593Smuzhiyun if (res > __div_u64(ULLONG_MAX - val, base))
285*4882a593Smuzhiyun rv |= KSTRTOX_OVERFLOW;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun res = res * base + val;
288*4882a593Smuzhiyun rv++;
289*4882a593Smuzhiyun s++;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun *p = res;
292*4882a593Smuzhiyun return rv;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
_kstrtoull(const char * s,unsigned int base,unsigned long long * res)295*4882a593Smuzhiyun static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun unsigned long long _res;
298*4882a593Smuzhiyun unsigned int rv;
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun s = _parse_integer_fixup_radix(s, &base);
301*4882a593Smuzhiyun rv = _parse_integer(s, base, &_res);
302*4882a593Smuzhiyun if (rv & KSTRTOX_OVERFLOW)
303*4882a593Smuzhiyun return -ERANGE;
304*4882a593Smuzhiyun if (rv == 0)
305*4882a593Smuzhiyun return -EINVAL;
306*4882a593Smuzhiyun s += rv;
307*4882a593Smuzhiyun if (*s == '\n')
308*4882a593Smuzhiyun s++;
309*4882a593Smuzhiyun if (*s)
310*4882a593Smuzhiyun return -EINVAL;
311*4882a593Smuzhiyun *res = _res;
312*4882a593Smuzhiyun return 0;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /**
316*4882a593Smuzhiyun * kstrtoull - convert a string to an unsigned long long
317*4882a593Smuzhiyun * @s: The start of the string. The string must be null-terminated, and may also
318*4882a593Smuzhiyun * include a single newline before its terminating null. The first character
319*4882a593Smuzhiyun * may also be a plus sign, but not a minus sign.
320*4882a593Smuzhiyun * @base: The number base to use. The maximum supported base is 16. If base is
321*4882a593Smuzhiyun * given as 0, then the base of the string is automatically detected with the
322*4882a593Smuzhiyun * conventional semantics - If it begins with 0x the number will be parsed as a
323*4882a593Smuzhiyun * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
324*4882a593Smuzhiyun * parsed as an octal number. Otherwise it will be parsed as a decimal.
325*4882a593Smuzhiyun * @res: Where to write the result of the conversion on success.
326*4882a593Smuzhiyun *
327*4882a593Smuzhiyun * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
328*4882a593Smuzhiyun * Used as a replacement for the obsolete simple_strtoull. Return code must
329*4882a593Smuzhiyun * be checked.
330*4882a593Smuzhiyun */
kstrtoull(const char * s,unsigned int base,unsigned long long * res)331*4882a593Smuzhiyun int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun if (s[0] == '+')
334*4882a593Smuzhiyun s++;
335*4882a593Smuzhiyun return _kstrtoull(s, base, res);
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
_kstrtoul(const char * s,unsigned int base,unsigned long * res)338*4882a593Smuzhiyun static int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun unsigned long long tmp;
341*4882a593Smuzhiyun int rv;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun rv = kstrtoull(s, base, &tmp);
344*4882a593Smuzhiyun if (rv < 0)
345*4882a593Smuzhiyun return rv;
346*4882a593Smuzhiyun if (tmp != (unsigned long)tmp)
347*4882a593Smuzhiyun return -ERANGE;
348*4882a593Smuzhiyun *res = tmp;
349*4882a593Smuzhiyun return 0;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun /**
353*4882a593Smuzhiyun * kstrtoul - convert a string to an unsigned long
354*4882a593Smuzhiyun * @s: The start of the string. The string must be null-terminated, and may also
355*4882a593Smuzhiyun * include a single newline before its terminating null. The first character
356*4882a593Smuzhiyun * may also be a plus sign, but not a minus sign.
357*4882a593Smuzhiyun * @base: The number base to use. The maximum supported base is 16. If base is
358*4882a593Smuzhiyun * given as 0, then the base of the string is automatically detected with the
359*4882a593Smuzhiyun * conventional semantics - If it begins with 0x the number will be parsed as a
360*4882a593Smuzhiyun * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
361*4882a593Smuzhiyun * parsed as an octal number. Otherwise it will be parsed as a decimal.
362*4882a593Smuzhiyun * @res: Where to write the result of the conversion on success.
363*4882a593Smuzhiyun *
364*4882a593Smuzhiyun * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
365*4882a593Smuzhiyun * Used as a replacement for the simple_strtoull.
366*4882a593Smuzhiyun */
boot_kstrtoul(const char * s,unsigned int base,unsigned long * res)367*4882a593Smuzhiyun int boot_kstrtoul(const char *s, unsigned int base, unsigned long *res)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun /*
370*4882a593Smuzhiyun * We want to shortcut function call, but
371*4882a593Smuzhiyun * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun if (sizeof(unsigned long) == sizeof(unsigned long long) &&
374*4882a593Smuzhiyun __alignof__(unsigned long) == __alignof__(unsigned long long))
375*4882a593Smuzhiyun return kstrtoull(s, base, (unsigned long long *)res);
376*4882a593Smuzhiyun else
377*4882a593Smuzhiyun return _kstrtoul(s, base, res);
378*4882a593Smuzhiyun }
379