xref: /linux-6.15/include/linux/kstrtox.h (revision fcc15500)
14c527293SAndy Shevchenko /* SPDX-License-Identifier: GPL-2.0 */
24c527293SAndy Shevchenko #ifndef _LINUX_KSTRTOX_H
34c527293SAndy Shevchenko #define _LINUX_KSTRTOX_H
44c527293SAndy Shevchenko 
54c527293SAndy Shevchenko #include <linux/compiler.h>
64c527293SAndy Shevchenko #include <linux/types.h>
74c527293SAndy Shevchenko 
84c527293SAndy Shevchenko /* Internal, do not use. */
94c527293SAndy Shevchenko int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
104c527293SAndy Shevchenko int __must_check _kstrtol(const char *s, unsigned int base, long *res);
114c527293SAndy Shevchenko 
124c527293SAndy Shevchenko int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
134c527293SAndy Shevchenko int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
144c527293SAndy Shevchenko 
154c527293SAndy Shevchenko /**
164c527293SAndy Shevchenko  * kstrtoul - convert a string to an unsigned long
174c527293SAndy Shevchenko  * @s: The start of the string. The string must be null-terminated, and may also
184c527293SAndy Shevchenko  *  include a single newline before its terminating null. The first character
194c527293SAndy Shevchenko  *  may also be a plus sign, but not a minus sign.
204c527293SAndy Shevchenko  * @base: The number base to use. The maximum supported base is 16. If base is
214c527293SAndy Shevchenko  *  given as 0, then the base of the string is automatically detected with the
224c527293SAndy Shevchenko  *  conventional semantics - If it begins with 0x the number will be parsed as a
234c527293SAndy Shevchenko  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
244c527293SAndy Shevchenko  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
254c527293SAndy Shevchenko  * @res: Where to write the result of the conversion on success.
264c527293SAndy Shevchenko  *
274c527293SAndy Shevchenko  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
284c527293SAndy Shevchenko  * Preferred over simple_strtoul(). Return code must be checked.
294c527293SAndy Shevchenko */
kstrtoul(const char * s,unsigned int base,unsigned long * res)304c527293SAndy Shevchenko static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
314c527293SAndy Shevchenko {
324c527293SAndy Shevchenko 	/*
334c527293SAndy Shevchenko 	 * We want to shortcut function call, but
344c527293SAndy Shevchenko 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
354c527293SAndy Shevchenko 	 */
364c527293SAndy Shevchenko 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
374c527293SAndy Shevchenko 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
384c527293SAndy Shevchenko 		return kstrtoull(s, base, (unsigned long long *)res);
394c527293SAndy Shevchenko 	else
404c527293SAndy Shevchenko 		return _kstrtoul(s, base, res);
414c527293SAndy Shevchenko }
424c527293SAndy Shevchenko 
434c527293SAndy Shevchenko /**
444c527293SAndy Shevchenko  * kstrtol - convert a string to a long
454c527293SAndy Shevchenko  * @s: The start of the string. The string must be null-terminated, and may also
464c527293SAndy Shevchenko  *  include a single newline before its terminating null. The first character
474c527293SAndy Shevchenko  *  may also be a plus sign or a minus sign.
484c527293SAndy Shevchenko  * @base: The number base to use. The maximum supported base is 16. If base is
494c527293SAndy Shevchenko  *  given as 0, then the base of the string is automatically detected with the
504c527293SAndy Shevchenko  *  conventional semantics - If it begins with 0x the number will be parsed as a
514c527293SAndy Shevchenko  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
524c527293SAndy Shevchenko  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
534c527293SAndy Shevchenko  * @res: Where to write the result of the conversion on success.
544c527293SAndy Shevchenko  *
554c527293SAndy Shevchenko  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
564c527293SAndy Shevchenko  * Preferred over simple_strtol(). Return code must be checked.
574c527293SAndy Shevchenko  */
kstrtol(const char * s,unsigned int base,long * res)584c527293SAndy Shevchenko static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
594c527293SAndy Shevchenko {
604c527293SAndy Shevchenko 	/*
614c527293SAndy Shevchenko 	 * We want to shortcut function call, but
624c527293SAndy Shevchenko 	 * __builtin_types_compatible_p(long, long long) = 0.
634c527293SAndy Shevchenko 	 */
644c527293SAndy Shevchenko 	if (sizeof(long) == sizeof(long long) &&
654c527293SAndy Shevchenko 	    __alignof__(long) == __alignof__(long long))
664c527293SAndy Shevchenko 		return kstrtoll(s, base, (long long *)res);
674c527293SAndy Shevchenko 	else
684c527293SAndy Shevchenko 		return _kstrtol(s, base, res);
694c527293SAndy Shevchenko }
704c527293SAndy Shevchenko 
714c527293SAndy Shevchenko int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
724c527293SAndy Shevchenko int __must_check kstrtoint(const char *s, unsigned int base, int *res);
734c527293SAndy Shevchenko 
kstrtou64(const char * s,unsigned int base,u64 * res)744c527293SAndy Shevchenko static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
754c527293SAndy Shevchenko {
764c527293SAndy Shevchenko 	return kstrtoull(s, base, res);
774c527293SAndy Shevchenko }
784c527293SAndy Shevchenko 
kstrtos64(const char * s,unsigned int base,s64 * res)794c527293SAndy Shevchenko static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
804c527293SAndy Shevchenko {
814c527293SAndy Shevchenko 	return kstrtoll(s, base, res);
824c527293SAndy Shevchenko }
834c527293SAndy Shevchenko 
kstrtou32(const char * s,unsigned int base,u32 * res)844c527293SAndy Shevchenko static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
854c527293SAndy Shevchenko {
864c527293SAndy Shevchenko 	return kstrtouint(s, base, res);
874c527293SAndy Shevchenko }
884c527293SAndy Shevchenko 
kstrtos32(const char * s,unsigned int base,s32 * res)894c527293SAndy Shevchenko static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
904c527293SAndy Shevchenko {
914c527293SAndy Shevchenko 	return kstrtoint(s, base, res);
924c527293SAndy Shevchenko }
934c527293SAndy Shevchenko 
944c527293SAndy Shevchenko int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
954c527293SAndy Shevchenko int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
964c527293SAndy Shevchenko int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
974c527293SAndy Shevchenko int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
984c527293SAndy Shevchenko int __must_check kstrtobool(const char *s, bool *res);
994c527293SAndy Shevchenko 
1004c527293SAndy Shevchenko int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
1014c527293SAndy Shevchenko int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
1024c527293SAndy Shevchenko int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
1034c527293SAndy Shevchenko int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
1044c527293SAndy Shevchenko int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
1054c527293SAndy Shevchenko int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
1064c527293SAndy Shevchenko int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
1074c527293SAndy Shevchenko int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
1084c527293SAndy Shevchenko int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
1094c527293SAndy Shevchenko int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
1104c527293SAndy Shevchenko int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
1114c527293SAndy Shevchenko 
kstrtou64_from_user(const char __user * s,size_t count,unsigned int base,u64 * res)1124c527293SAndy Shevchenko static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
1134c527293SAndy Shevchenko {
1144c527293SAndy Shevchenko 	return kstrtoull_from_user(s, count, base, res);
1154c527293SAndy Shevchenko }
1164c527293SAndy Shevchenko 
kstrtos64_from_user(const char __user * s,size_t count,unsigned int base,s64 * res)1174c527293SAndy Shevchenko static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
1184c527293SAndy Shevchenko {
1194c527293SAndy Shevchenko 	return kstrtoll_from_user(s, count, base, res);
1204c527293SAndy Shevchenko }
1214c527293SAndy Shevchenko 
kstrtou32_from_user(const char __user * s,size_t count,unsigned int base,u32 * res)1224c527293SAndy Shevchenko static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
1234c527293SAndy Shevchenko {
1244c527293SAndy Shevchenko 	return kstrtouint_from_user(s, count, base, res);
1254c527293SAndy Shevchenko }
1264c527293SAndy Shevchenko 
kstrtos32_from_user(const char __user * s,size_t count,unsigned int base,s32 * res)1274c527293SAndy Shevchenko static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
1284c527293SAndy Shevchenko {
1294c527293SAndy Shevchenko 	return kstrtoint_from_user(s, count, base, res);
1304c527293SAndy Shevchenko }
1314c527293SAndy Shevchenko 
1324c527293SAndy Shevchenko /*
1334c527293SAndy Shevchenko  * Use kstrto<foo> instead.
1344c527293SAndy Shevchenko  *
1354c527293SAndy Shevchenko  * NOTE: simple_strto<foo> does not check for the range overflow and,
1364c527293SAndy Shevchenko  *	 depending on the input, may give interesting results.
1374c527293SAndy Shevchenko  *
1384c527293SAndy Shevchenko  * Use these functions if and only if you cannot use kstrto<foo>, because
1394c527293SAndy Shevchenko  * the conversion ends on the first non-digit character, which may be far
1404c527293SAndy Shevchenko  * beyond the supported range. It might be useful to parse the strings like
1414c527293SAndy Shevchenko  * 10x50 or 12:21 without altering original string or temporary buffer in use.
1424c527293SAndy Shevchenko  * Keep in mind above caveat.
1434c527293SAndy Shevchenko  */
1444c527293SAndy Shevchenko 
1454c527293SAndy Shevchenko extern unsigned long simple_strtoul(const char *,char **,unsigned int);
146*fcc15500SDavid Disseldorp extern unsigned long simple_strntoul(const char *,char **,unsigned int,size_t);
1474c527293SAndy Shevchenko extern long simple_strtol(const char *,char **,unsigned int);
1484c527293SAndy Shevchenko extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
1494c527293SAndy Shevchenko extern long long simple_strtoll(const char *,char **,unsigned int);
1504c527293SAndy Shevchenko 
1514c527293SAndy Shevchenko #endif	/* _LINUX_KSTRTOX_H */
152