Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_KSTRTOX_H
0003 #define _LINUX_KSTRTOX_H
0004 
0005 #include <linux/compiler.h>
0006 #include <linux/types.h>
0007 
0008 /* Internal, do not use. */
0009 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
0010 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
0011 
0012 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
0013 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
0014 
0015 /**
0016  * kstrtoul - convert a string to an unsigned long
0017  * @s: The start of the string. The string must be null-terminated, and may also
0018  *  include a single newline before its terminating null. The first character
0019  *  may also be a plus sign, but not a minus sign.
0020  * @base: The number base to use. The maximum supported base is 16. If base is
0021  *  given as 0, then the base of the string is automatically detected with the
0022  *  conventional semantics - If it begins with 0x the number will be parsed as a
0023  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
0024  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
0025  * @res: Where to write the result of the conversion on success.
0026  *
0027  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
0028  * Preferred over simple_strtoul(). Return code must be checked.
0029 */
0030 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
0031 {
0032     /*
0033      * We want to shortcut function call, but
0034      * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
0035      */
0036     if (sizeof(unsigned long) == sizeof(unsigned long long) &&
0037         __alignof__(unsigned long) == __alignof__(unsigned long long))
0038         return kstrtoull(s, base, (unsigned long long *)res);
0039     else
0040         return _kstrtoul(s, base, res);
0041 }
0042 
0043 /**
0044  * kstrtol - convert a string to a long
0045  * @s: The start of the string. The string must be null-terminated, and may also
0046  *  include a single newline before its terminating null. The first character
0047  *  may also be a plus sign or a minus sign.
0048  * @base: The number base to use. The maximum supported base is 16. If base is
0049  *  given as 0, then the base of the string is automatically detected with the
0050  *  conventional semantics - If it begins with 0x the number will be parsed as a
0051  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
0052  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
0053  * @res: Where to write the result of the conversion on success.
0054  *
0055  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
0056  * Preferred over simple_strtol(). Return code must be checked.
0057  */
0058 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
0059 {
0060     /*
0061      * We want to shortcut function call, but
0062      * __builtin_types_compatible_p(long, long long) = 0.
0063      */
0064     if (sizeof(long) == sizeof(long long) &&
0065         __alignof__(long) == __alignof__(long long))
0066         return kstrtoll(s, base, (long long *)res);
0067     else
0068         return _kstrtol(s, base, res);
0069 }
0070 
0071 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
0072 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
0073 
0074 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
0075 {
0076     return kstrtoull(s, base, res);
0077 }
0078 
0079 static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
0080 {
0081     return kstrtoll(s, base, res);
0082 }
0083 
0084 static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
0085 {
0086     return kstrtouint(s, base, res);
0087 }
0088 
0089 static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
0090 {
0091     return kstrtoint(s, base, res);
0092 }
0093 
0094 int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
0095 int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
0096 int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
0097 int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
0098 int __must_check kstrtobool(const char *s, bool *res);
0099 
0100 int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
0101 int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
0102 int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
0103 int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
0104 int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
0105 int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
0106 int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
0107 int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
0108 int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
0109 int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
0110 int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
0111 
0112 static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
0113 {
0114     return kstrtoull_from_user(s, count, base, res);
0115 }
0116 
0117 static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
0118 {
0119     return kstrtoll_from_user(s, count, base, res);
0120 }
0121 
0122 static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
0123 {
0124     return kstrtouint_from_user(s, count, base, res);
0125 }
0126 
0127 static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
0128 {
0129     return kstrtoint_from_user(s, count, base, res);
0130 }
0131 
0132 /*
0133  * Use kstrto<foo> instead.
0134  *
0135  * NOTE: simple_strto<foo> does not check for the range overflow and,
0136  *   depending on the input, may give interesting results.
0137  *
0138  * Use these functions if and only if you cannot use kstrto<foo>, because
0139  * the conversion ends on the first non-digit character, which may be far
0140  * beyond the supported range. It might be useful to parse the strings like
0141  * 10x50 or 12:21 without altering original string or temporary buffer in use.
0142  * Keep in mind above caveat.
0143  */
0144 
0145 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
0146 extern long simple_strtol(const char *,char **,unsigned int);
0147 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
0148 extern long long simple_strtoll(const char *,char **,unsigned int);
0149 
0150 static inline int strtobool(const char *s, bool *res)
0151 {
0152     return kstrtobool(s, res);
0153 }
0154 
0155 #endif  /* _LINUX_KSTRTOX_H */