]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - lib/kstrtox.c
Thermal: Add Hysteresis attributes
[linux-2.6.git] / lib / kstrtox.c
index 05672e819f8c4c9164dd40e3752d23b8b5f63577..c3615eab0cc33b9d6a79c13f52a15821d9b22061 100644 (file)
 #include <linux/errno.h>
 #include <linux/kernel.h>
 #include <linux/math64.h>
-#include <linux/module.h>
+#include <linux/export.h>
 #include <linux/types.h>
+#include <asm/uaccess.h>
+#include "kstrtox.h"
 
-static inline char _tolower(const char c)
+const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
 {
-       return c | 0x20;
-}
-
-static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
-{
-       unsigned long long acc;
-       int ok;
-
-       if (base == 0) {
+       if (*base == 0) {
                if (s[0] == '0') {
                        if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
-                               base = 16;
+                               *base = 16;
                        else
-                               base = 8;
+                               *base = 8;
                } else
-                       base = 10;
+                       *base = 10;
        }
-       if (base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
+       if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
                s += 2;
+       return s;
+}
 
-       acc = 0;
-       ok = 0;
+/*
+ * Convert non-negative integer string representation in explicitly given radix
+ * to an integer.
+ * Return number of characters consumed maybe or-ed with overflow bit.
+ * If overflow occurs, result integer (incorrect) is still returned.
+ *
+ * Don't you dare use this function.
+ */
+unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
+{
+       unsigned long long res;
+       unsigned int rv;
+       int overflow;
+
+       res = 0;
+       rv = 0;
+       overflow = 0;
        while (*s) {
                unsigned int val;
 
@@ -49,26 +60,47 @@ static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
                        val = *s - '0';
                else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
                        val = _tolower(*s) - 'a' + 10;
-               else if (*s == '\n') {
-                       if (*(s + 1) == '\0')
-                               break;
-                       else
-                               return -EINVAL;
-               } else
-                       return -EINVAL;
+               else
+                       break;
 
                if (val >= base)
-                       return -EINVAL;
-               if (acc > div_u64(ULLONG_MAX - val, base))
-                       return -ERANGE;
-               acc = acc * base + val;
-               ok = 1;
-
+                       break;
+               /*
+                * Check for overflow only if we are within range of
+                * it in the max base we support (16)
+                */
+               if (unlikely(res & (~0ull << 60))) {
+                       if (res > div_u64(ULLONG_MAX - val, base))
+                               overflow = 1;
+               }
+               res = res * base + val;
+               rv++;
                s++;
        }
-       if (!ok)
+       *p = res;
+       if (overflow)
+               rv |= KSTRTOX_OVERFLOW;
+       return rv;
+}
+
+static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
+{
+       unsigned long long _res;
+       unsigned int rv;
+
+       s = _parse_integer_fixup_radix(s, &base);
+       rv = _parse_integer(s, base, &_res);
+       if (rv & KSTRTOX_OVERFLOW)
+               return -ERANGE;
+       rv &= ~KSTRTOX_OVERFLOW;
+       if (rv == 0)
+               return -EINVAL;
+       s += rv;
+       if (*s == '\n')
+               s++;
+       if (*s)
                return -EINVAL;
-       *res = acc;
+       *res = _res;
        return 0;
 }
 
@@ -225,3 +257,28 @@ int kstrtos8(const char *s, unsigned int base, s8 *res)
        return 0;
 }
 EXPORT_SYMBOL(kstrtos8);
+
+#define kstrto_from_user(f, g, type)                                   \
+int f(const char __user *s, size_t count, unsigned int base, type *res)        \
+{                                                                      \
+       /* sign, base 2 representation, newline, terminator */          \
+       char buf[1 + sizeof(type) * 8 + 1 + 1];                         \
+                                                                       \
+       count = min(count, sizeof(buf) - 1);                            \
+       if (copy_from_user(buf, s, count))                              \
+               return -EFAULT;                                         \
+       buf[count] = '\0';                                              \
+       return g(buf, base, res);                                       \
+}                                                                      \
+EXPORT_SYMBOL(f)
+
+kstrto_from_user(kstrtoull_from_user,  kstrtoull,      unsigned long long);
+kstrto_from_user(kstrtoll_from_user,   kstrtoll,       long long);
+kstrto_from_user(kstrtoul_from_user,   kstrtoul,       unsigned long);
+kstrto_from_user(kstrtol_from_user,    kstrtol,        long);
+kstrto_from_user(kstrtouint_from_user, kstrtouint,     unsigned int);
+kstrto_from_user(kstrtoint_from_user,  kstrtoint,      int);
+kstrto_from_user(kstrtou16_from_user,  kstrtou16,      u16);
+kstrto_from_user(kstrtos16_from_user,  kstrtos16,      s16);
+kstrto_from_user(kstrtou8_from_user,   kstrtou8,       u8);
+kstrto_from_user(kstrtos8_from_user,   kstrtos8,       s8);