Back to home page

LXR

 
 

    


0001 /*
0002  * Convert integer string representation to an integer.
0003  * If an integer doesn't fit into specified type, -E is returned.
0004  *
0005  * Integer starts with optional sign.
0006  * kstrtou*() functions do not accept sign "-".
0007  *
0008  * Radix 0 means autodetection: leading "0x" implies radix 16,
0009  * leading "0" implies radix 8, otherwise radix is 10.
0010  * Autodetection hints work after optional sign, but not before.
0011  *
0012  * If -E is returned, result is not touched.
0013  */
0014 #include <linux/ctype.h>
0015 #include <linux/errno.h>
0016 #include <linux/kernel.h>
0017 #include <linux/math64.h>
0018 #include <linux/export.h>
0019 #include <linux/types.h>
0020 #include <linux/uaccess.h>
0021 #include "kstrtox.h"
0022 
0023 const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
0024 {
0025     if (*base == 0) {
0026         if (s[0] == '0') {
0027             if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
0028                 *base = 16;
0029             else
0030                 *base = 8;
0031         } else
0032             *base = 10;
0033     }
0034     if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
0035         s += 2;
0036     return s;
0037 }
0038 
0039 /*
0040  * Convert non-negative integer string representation in explicitly given radix
0041  * to an integer.
0042  * Return number of characters consumed maybe or-ed with overflow bit.
0043  * If overflow occurs, result integer (incorrect) is still returned.
0044  *
0045  * Don't you dare use this function.
0046  */
0047 unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
0048 {
0049     unsigned long long res;
0050     unsigned int rv;
0051 
0052     res = 0;
0053     rv = 0;
0054     while (*s) {
0055         unsigned int val;
0056 
0057         if ('0' <= *s && *s <= '9')
0058             val = *s - '0';
0059         else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
0060             val = _tolower(*s) - 'a' + 10;
0061         else
0062             break;
0063 
0064         if (val >= base)
0065             break;
0066         /*
0067          * Check for overflow only if we are within range of
0068          * it in the max base we support (16)
0069          */
0070         if (unlikely(res & (~0ull << 60))) {
0071             if (res > div_u64(ULLONG_MAX - val, base))
0072                 rv |= KSTRTOX_OVERFLOW;
0073         }
0074         res = res * base + val;
0075         rv++;
0076         s++;
0077     }
0078     *p = res;
0079     return rv;
0080 }
0081 
0082 static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
0083 {
0084     unsigned long long _res;
0085     unsigned int rv;
0086 
0087     s = _parse_integer_fixup_radix(s, &base);
0088     rv = _parse_integer(s, base, &_res);
0089     if (rv & KSTRTOX_OVERFLOW)
0090         return -ERANGE;
0091     if (rv == 0)
0092         return -EINVAL;
0093     s += rv;
0094     if (*s == '\n')
0095         s++;
0096     if (*s)
0097         return -EINVAL;
0098     *res = _res;
0099     return 0;
0100 }
0101 
0102 /**
0103  * kstrtoull - convert a string to an unsigned long long
0104  * @s: The start of the string. The string must be null-terminated, and may also
0105  *  include a single newline before its terminating null. The first character
0106  *  may also be a plus sign, but not a minus sign.
0107  * @base: The number base to use. The maximum supported base is 16. If base is
0108  *  given as 0, then the base of the string is automatically detected with the
0109  *  conventional semantics - If it begins with 0x the number will be parsed as a
0110  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
0111  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
0112  * @res: Where to write the result of the conversion on success.
0113  *
0114  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
0115  * Used as a replacement for the obsolete simple_strtoull. Return code must
0116  * be checked.
0117  */
0118 int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
0119 {
0120     if (s[0] == '+')
0121         s++;
0122     return _kstrtoull(s, base, res);
0123 }
0124 EXPORT_SYMBOL(kstrtoull);
0125 
0126 /**
0127  * kstrtoll - convert a string to a long long
0128  * @s: The start of the string. The string must be null-terminated, and may also
0129  *  include a single newline before its terminating null. The first character
0130  *  may also be a plus sign or a minus sign.
0131  * @base: The number base to use. The maximum supported base is 16. If base is
0132  *  given as 0, then the base of the string is automatically detected with the
0133  *  conventional semantics - If it begins with 0x the number will be parsed as a
0134  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
0135  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
0136  * @res: Where to write the result of the conversion on success.
0137  *
0138  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
0139  * Used as a replacement for the obsolete simple_strtoull. Return code must
0140  * be checked.
0141  */
0142 int kstrtoll(const char *s, unsigned int base, long long *res)
0143 {
0144     unsigned long long tmp;
0145     int rv;
0146 
0147     if (s[0] == '-') {
0148         rv = _kstrtoull(s + 1, base, &tmp);
0149         if (rv < 0)
0150             return rv;
0151         if ((long long)-tmp > 0)
0152             return -ERANGE;
0153         *res = -tmp;
0154     } else {
0155         rv = kstrtoull(s, base, &tmp);
0156         if (rv < 0)
0157             return rv;
0158         if ((long long)tmp < 0)
0159             return -ERANGE;
0160         *res = tmp;
0161     }
0162     return 0;
0163 }
0164 EXPORT_SYMBOL(kstrtoll);
0165 
0166 /* Internal, do not use. */
0167 int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
0168 {
0169     unsigned long long tmp;
0170     int rv;
0171 
0172     rv = kstrtoull(s, base, &tmp);
0173     if (rv < 0)
0174         return rv;
0175     if (tmp != (unsigned long long)(unsigned long)tmp)
0176         return -ERANGE;
0177     *res = tmp;
0178     return 0;
0179 }
0180 EXPORT_SYMBOL(_kstrtoul);
0181 
0182 /* Internal, do not use. */
0183 int _kstrtol(const char *s, unsigned int base, long *res)
0184 {
0185     long long tmp;
0186     int rv;
0187 
0188     rv = kstrtoll(s, base, &tmp);
0189     if (rv < 0)
0190         return rv;
0191     if (tmp != (long long)(long)tmp)
0192         return -ERANGE;
0193     *res = tmp;
0194     return 0;
0195 }
0196 EXPORT_SYMBOL(_kstrtol);
0197 
0198 /**
0199  * kstrtouint - convert a string to an unsigned int
0200  * @s: The start of the string. The string must be null-terminated, and may also
0201  *  include a single newline before its terminating null. The first character
0202  *  may also be a plus sign, but not a minus sign.
0203  * @base: The number base to use. The maximum supported base is 16. If base is
0204  *  given as 0, then the base of the string is automatically detected with the
0205  *  conventional semantics - If it begins with 0x the number will be parsed as a
0206  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
0207  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
0208  * @res: Where to write the result of the conversion on success.
0209  *
0210  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
0211  * Used as a replacement for the obsolete simple_strtoull. Return code must
0212  * be checked.
0213  */
0214 int kstrtouint(const char *s, unsigned int base, unsigned int *res)
0215 {
0216     unsigned long long tmp;
0217     int rv;
0218 
0219     rv = kstrtoull(s, base, &tmp);
0220     if (rv < 0)
0221         return rv;
0222     if (tmp != (unsigned long long)(unsigned int)tmp)
0223         return -ERANGE;
0224     *res = tmp;
0225     return 0;
0226 }
0227 EXPORT_SYMBOL(kstrtouint);
0228 
0229 /**
0230  * kstrtoint - convert a string to an int
0231  * @s: The start of the string. The string must be null-terminated, and may also
0232  *  include a single newline before its terminating null. The first character
0233  *  may also be a plus sign or a minus sign.
0234  * @base: The number base to use. The maximum supported base is 16. If base is
0235  *  given as 0, then the base of the string is automatically detected with the
0236  *  conventional semantics - If it begins with 0x the number will be parsed as a
0237  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
0238  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
0239  * @res: Where to write the result of the conversion on success.
0240  *
0241  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
0242  * Used as a replacement for the obsolete simple_strtoull. Return code must
0243  * be checked.
0244  */
0245 int kstrtoint(const char *s, unsigned int base, int *res)
0246 {
0247     long long tmp;
0248     int rv;
0249 
0250     rv = kstrtoll(s, base, &tmp);
0251     if (rv < 0)
0252         return rv;
0253     if (tmp != (long long)(int)tmp)
0254         return -ERANGE;
0255     *res = tmp;
0256     return 0;
0257 }
0258 EXPORT_SYMBOL(kstrtoint);
0259 
0260 int kstrtou16(const char *s, unsigned int base, u16 *res)
0261 {
0262     unsigned long long tmp;
0263     int rv;
0264 
0265     rv = kstrtoull(s, base, &tmp);
0266     if (rv < 0)
0267         return rv;
0268     if (tmp != (unsigned long long)(u16)tmp)
0269         return -ERANGE;
0270     *res = tmp;
0271     return 0;
0272 }
0273 EXPORT_SYMBOL(kstrtou16);
0274 
0275 int kstrtos16(const char *s, unsigned int base, s16 *res)
0276 {
0277     long long tmp;
0278     int rv;
0279 
0280     rv = kstrtoll(s, base, &tmp);
0281     if (rv < 0)
0282         return rv;
0283     if (tmp != (long long)(s16)tmp)
0284         return -ERANGE;
0285     *res = tmp;
0286     return 0;
0287 }
0288 EXPORT_SYMBOL(kstrtos16);
0289 
0290 int kstrtou8(const char *s, unsigned int base, u8 *res)
0291 {
0292     unsigned long long tmp;
0293     int rv;
0294 
0295     rv = kstrtoull(s, base, &tmp);
0296     if (rv < 0)
0297         return rv;
0298     if (tmp != (unsigned long long)(u8)tmp)
0299         return -ERANGE;
0300     *res = tmp;
0301     return 0;
0302 }
0303 EXPORT_SYMBOL(kstrtou8);
0304 
0305 int kstrtos8(const char *s, unsigned int base, s8 *res)
0306 {
0307     long long tmp;
0308     int rv;
0309 
0310     rv = kstrtoll(s, base, &tmp);
0311     if (rv < 0)
0312         return rv;
0313     if (tmp != (long long)(s8)tmp)
0314         return -ERANGE;
0315     *res = tmp;
0316     return 0;
0317 }
0318 EXPORT_SYMBOL(kstrtos8);
0319 
0320 /**
0321  * kstrtobool - convert common user inputs into boolean values
0322  * @s: input string
0323  * @res: result
0324  *
0325  * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
0326  * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL.  Value
0327  * pointed to by res is updated upon finding a match.
0328  */
0329 int kstrtobool(const char *s, bool *res)
0330 {
0331     if (!s)
0332         return -EINVAL;
0333 
0334     switch (s[0]) {
0335     case 'y':
0336     case 'Y':
0337     case '1':
0338         *res = true;
0339         return 0;
0340     case 'n':
0341     case 'N':
0342     case '0':
0343         *res = false;
0344         return 0;
0345     case 'o':
0346     case 'O':
0347         switch (s[1]) {
0348         case 'n':
0349         case 'N':
0350             *res = true;
0351             return 0;
0352         case 'f':
0353         case 'F':
0354             *res = false;
0355             return 0;
0356         default:
0357             break;
0358         }
0359     default:
0360         break;
0361     }
0362 
0363     return -EINVAL;
0364 }
0365 EXPORT_SYMBOL(kstrtobool);
0366 
0367 /*
0368  * Since "base" would be a nonsense argument, this open-codes the
0369  * _from_user helper instead of using the helper macro below.
0370  */
0371 int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
0372 {
0373     /* Longest string needed to differentiate, newline, terminator */
0374     char buf[4];
0375 
0376     count = min(count, sizeof(buf) - 1);
0377     if (copy_from_user(buf, s, count))
0378         return -EFAULT;
0379     buf[count] = '\0';
0380     return kstrtobool(buf, res);
0381 }
0382 EXPORT_SYMBOL(kstrtobool_from_user);
0383 
0384 #define kstrto_from_user(f, g, type)                    \
0385 int f(const char __user *s, size_t count, unsigned int base, type *res) \
0386 {                                   \
0387     /* sign, base 2 representation, newline, terminator */      \
0388     char buf[1 + sizeof(type) * 8 + 1 + 1];             \
0389                                     \
0390     count = min(count, sizeof(buf) - 1);                \
0391     if (copy_from_user(buf, s, count))              \
0392         return -EFAULT;                     \
0393     buf[count] = '\0';                      \
0394     return g(buf, base, res);                   \
0395 }                                   \
0396 EXPORT_SYMBOL(f)
0397 
0398 kstrto_from_user(kstrtoull_from_user,   kstrtoull,  unsigned long long);
0399 kstrto_from_user(kstrtoll_from_user,    kstrtoll,   long long);
0400 kstrto_from_user(kstrtoul_from_user,    kstrtoul,   unsigned long);
0401 kstrto_from_user(kstrtol_from_user, kstrtol,    long);
0402 kstrto_from_user(kstrtouint_from_user,  kstrtouint, unsigned int);
0403 kstrto_from_user(kstrtoint_from_user,   kstrtoint,  int);
0404 kstrto_from_user(kstrtou16_from_user,   kstrtou16,  u16);
0405 kstrto_from_user(kstrtos16_from_user,   kstrtos16,  s16);
0406 kstrto_from_user(kstrtou8_from_user,    kstrtou8,   u8);
0407 kstrto_from_user(kstrtos8_from_user,    kstrtos8,   s8);