1 // SPDX-License-Identifier: GPL-2.0-only
2 /* -*- linux-c -*- ------------------------------------------------------- *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright 2007 rPath, Inc. - All Rights Reserved
7 * ----------------------------------------------------------------------- */
10 * Very basic string functions
13 #include <linux/types.h>
14 #include <linux/compiler.h>
15 #include <linux/errno.h>
16 #include <linux/limits.h>
21 #define KSTRTOX_OVERFLOW (1U << 31)
24 * Undef these macros so that the functions that we provide
25 * here will have the correct names regardless of how string.h
26 * may have chosen to #define them.
32 int memcmp(const void *s1
, const void *s2
, size_t len
)
35 asm("repe; cmpsb" CC_SET(nz
)
36 : CC_OUT(nz
) (diff
), "+D" (s1
), "+S" (s2
), "+c" (len
));
41 * Clang may lower `memcmp == 0` to `bcmp == 0`.
43 int bcmp(const void *s1
, const void *s2
, size_t len
)
45 return memcmp(s1
, s2
, len
);
48 int strcmp(const char *str1
, const char *str2
)
50 const unsigned char *s1
= (const unsigned char *)str1
;
51 const unsigned char *s2
= (const unsigned char *)str2
;
64 int strncmp(const char *cs
, const char *ct
, size_t count
)
72 return c1
< c2
? -1 : 1;
80 size_t strnlen(const char *s
, size_t maxlen
)
83 while (*es
&& maxlen
) {
91 unsigned int atou(const char *s
)
95 i
= i
* 10 + (*s
++ - '0');
99 /* Works only for digits and letters, but small and fast */
100 #define TOLOWER(x) ((x) | 0x20)
102 static unsigned int simple_guess_base(const char *cp
)
105 if (TOLOWER(cp
[1]) == 'x' && isxdigit(cp
[2]))
115 * simple_strtoull - convert a string to an unsigned long long
116 * @cp: The start of the string
117 * @endp: A pointer to the end of the parsed string will be placed here
118 * @base: The number base to use
121 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
123 unsigned long long result
= 0;
126 base
= simple_guess_base(cp
);
128 if (base
== 16 && cp
[0] == '0' && TOLOWER(cp
[1]) == 'x')
131 while (isxdigit(*cp
)) {
134 value
= isdigit(*cp
) ? *cp
- '0' : TOLOWER(*cp
) - 'a' + 10;
137 result
= result
* base
+ value
;
146 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
149 return -simple_strtoull(cp
+ 1, endp
, base
);
151 return simple_strtoull(cp
, endp
, base
);
155 * strlen - Find the length of a string
156 * @s: The string to be sized
158 size_t strlen(const char *s
)
162 for (sc
= s
; *sc
!= '\0'; ++sc
)
168 * strstr - Find the first substring in a %NUL terminated string
169 * @s1: The string to be searched
170 * @s2: The string to search for
172 char *strstr(const char *s1
, const char *s2
)
182 if (!memcmp(s1
, s2
, l2
))
190 * strchr - Find the first occurrence of the character c in the string s.
191 * @s: the string to be searched
192 * @c: the character to search for
194 char *strchr(const char *s
, int c
)
196 while (*s
!= (char)c
)
202 static inline u64
__div_u64_rem(u64 dividend
, u32 divisor
, u32
*remainder
)
212 if (upper
>= divisor
) {
213 d
.v32
[1] = upper
/ divisor
;
216 asm ("divl %2" : "=a" (d
.v32
[0]), "=d" (*remainder
) :
217 "rm" (divisor
), "0" (d
.v32
[0]), "1" (upper
));
221 static inline u64
__div_u64(u64 dividend
, u32 divisor
)
225 return __div_u64_rem(dividend
, divisor
, &remainder
);
228 static inline char _tolower(const char c
)
233 static const char *_parse_integer_fixup_radix(const char *s
, unsigned int *base
)
237 if (_tolower(s
[1]) == 'x' && isxdigit(s
[2]))
244 if (*base
== 16 && s
[0] == '0' && _tolower(s
[1]) == 'x')
250 * Convert non-negative integer string representation in explicitly given radix
252 * Return number of characters consumed maybe or-ed with overflow bit.
253 * If overflow occurs, result integer (incorrect) is still returned.
255 * Don't you dare use this function.
257 static unsigned int _parse_integer(const char *s
,
259 unsigned long long *p
)
261 unsigned long long res
;
268 unsigned int lc
= c
| 0x20; /* don't tolower() this line */
271 if ('0' <= c
&& c
<= '9')
273 else if ('a' <= lc
&& lc
<= 'f')
281 * Check for overflow only if we are within range of
282 * it in the max base we support (16)
284 if (unlikely(res
& (~0ull << 60))) {
285 if (res
> __div_u64(ULLONG_MAX
- val
, base
))
286 rv
|= KSTRTOX_OVERFLOW
;
288 res
= res
* base
+ val
;
296 static int _kstrtoull(const char *s
, unsigned int base
, unsigned long long *res
)
298 unsigned long long _res
;
301 s
= _parse_integer_fixup_radix(s
, &base
);
302 rv
= _parse_integer(s
, base
, &_res
);
303 if (rv
& KSTRTOX_OVERFLOW
)
317 * kstrtoull - convert a string to an unsigned long long
318 * @s: The start of the string. The string must be null-terminated, and may also
319 * include a single newline before its terminating null. The first character
320 * may also be a plus sign, but not a minus sign.
321 * @base: The number base to use. The maximum supported base is 16. If base is
322 * given as 0, then the base of the string is automatically detected with the
323 * conventional semantics - If it begins with 0x the number will be parsed as a
324 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
325 * parsed as an octal number. Otherwise it will be parsed as a decimal.
326 * @res: Where to write the result of the conversion on success.
328 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
329 * Used as a replacement for the obsolete simple_strtoull. Return code must
332 int kstrtoull(const char *s
, unsigned int base
, unsigned long long *res
)
336 return _kstrtoull(s
, base
, res
);