]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added str_to_*() for converting strings to numbers, plus a few helper functions.
authorTimo Sirainen <tss@iki.fi>
Tue, 6 Apr 2010 22:48:03 +0000 (01:48 +0300)
committerTimo Sirainen <tss@iki.fi>
Tue, 6 Apr 2010 22:48:03 +0000 (01:48 +0300)
The str_to_*() return failure also if integer is too large or too small to
fit into destination.

--HG--
branch : HEAD

src/lib/Makefile.am
src/lib/lib.h
src/lib/strnum.c [new file with mode: 0644]
src/lib/strnum.h [new file with mode: 0644]

index b916ad805b2af8a60344f191fc580de1d851187d..cec20cd8a74ffd8c229491733d10071cdb85fd47 100644 (file)
@@ -104,6 +104,7 @@ liblib_la_SOURCES = \
        str-sanitize.c \
        strescape.c \
        strfuncs.c \
+       strnum.c \
        time-util.c \
        unix-socket-create.c \
        unlink-directory.c \
@@ -198,6 +199,7 @@ headers = \
        str-sanitize.h \
        strescape.h \
        strfuncs.h \
+       strnum.h \
        time-util.h \
        unix-socket-create.h \
        unlink-directory.h \
index f33d35af85d4bc0259661362ca016209546b53be..0305c891a4eb617e17093b1c5cbdd6bd039b8c55 100644 (file)
@@ -37,6 +37,7 @@ struct ostream;
 
 #include "array-decl.h" /* ARRAY_DEFINE()s may exist in any header */
 #include "strfuncs.h"
+#include "strnum.h"
 
 size_t nearest_power(size_t num) ATTR_CONST;
 
diff --git a/src/lib/strnum.c b/src/lib/strnum.c
new file mode 100644 (file)
index 0000000..e50316d
--- /dev/null
@@ -0,0 +1,253 @@
+/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "strnum.h"
+
+bool str_is_numeric(const char *str, char end_char)
+{
+       if (*str == '\0' || *str == end_char)
+               return FALSE;
+
+       while (*str != '\0' && *str != end_char) {
+               if (*str < '0' || *str > '9')
+                       return FALSE;
+               str++;
+       }
+
+       return TRUE;
+}
+
+int str_to_uint(const char *str, unsigned int *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (l > (unsigned int)-1)
+               return -1;
+       *num_r = (unsigned int)l;
+       return 0;
+}
+
+int str_to_ulong(const char *str, unsigned long *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (l > (unsigned long)-1)
+               return -1;
+       *num_r = (unsigned long)l;
+       return 0;
+}
+
+int str_to_ullong(const char *str, unsigned long long *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (l > (unsigned long long)-1)
+               return -1;
+       *num_r = (unsigned long long)l;
+       return 0;
+}
+
+int str_to_uint32(const char *str, uint32_t *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (l > (uint32_t)-1)
+               return -1;
+       *num_r = (uint32_t)l;
+       return 0;
+}
+
+int str_to_uint64(const char *str, uint64_t *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (l > (uint64_t)-1)
+               return -1;
+       *num_r = (uint64_t)l;
+       return 0;
+}
+
+int str_to_uintmax(const char *str, uintmax_t *num_r)
+{
+       uintmax_t next, n = 0;
+
+       if (*str < '0' || *str > '9')
+               return -1;
+
+       for (; *str >= '0' && *str <= '9'; str++) {
+               next = n*10;
+               if (next < n) {
+                       /* overflow */
+                       return -1;
+               }
+               n = next + (*str - '0');
+       }
+       if (str != '\0')
+               return -1;
+       *num_r = n;
+       return 0;
+}
+
+int str_to_int(const char *str, int *num_r)
+{
+       intmax_t l;
+
+       if (str_to_intmax(str, &l) < 0)
+               return -1;
+
+       if (l < INT_MIN || l > INT_MAX)
+               return -1;
+       *num_r = (int)l;
+       return 0;
+}
+
+int str_to_long(const char *str, long *num_r)
+{
+       intmax_t l;
+
+       if (str_to_intmax(str, &l) < 0)
+               return -1;
+
+       if (l < LONG_MIN || l > LONG_MAX)
+               return -1;
+       *num_r = (long)l;
+       return 0;
+}
+
+int str_to_llong(const char *str, long long *num_r)
+{
+       intmax_t l;
+
+       if (str_to_intmax(str, &l) < 0)
+               return -1;
+
+       if (l < LLONG_MIN || l > LLONG_MAX)
+               return -1;
+       *num_r = (long long)l;
+       return 0;
+}
+
+int str_to_intmax(const char *str, intmax_t *num_r)
+{
+       bool neg = FALSE;
+       uintmax_t l;
+
+       if (*str == '-') {
+               neg = TRUE;
+               str++;
+       }
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (!neg) {
+               if (l > INTMAX_MAX)
+                       return -1;
+               *num_r = (intmax_t)l;
+       } else {
+               if (l > UINTMAX_MAX - (UINTMAX_MAX + INTMAX_MIN))
+                       return -1;
+               *num_r = (intmax_t)-l;
+       }
+       return 0;
+}
+
+static int verify_xid(uintmax_t l, unsigned int result_size)
+{
+       unsigned int result_bits;
+
+       /* we assume that result is a signed type,
+          but that it can never be negative */
+       result_bits = result_size*CHAR_BIT - 1;
+       if ((l >> result_bits) != 0)
+               return -1;
+       return 0;
+}
+
+int str_to_uid(const char *str, uid_t *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (verify_xid(l, sizeof(*num_r)) < 0)
+               return -1;
+       *num_r = (uid_t)l;
+       return 0;
+}
+
+int str_to_gid(const char *str, gid_t *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (verify_xid(l, sizeof(*num_r)) < 0)
+               return -1;
+       *num_r = (gid_t)l;
+       return 0;
+}
+
+int str_to_pid(const char *str, pid_t *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (verify_xid(l, sizeof(*num_r)) < 0)
+               return -1;
+       *num_r = (pid_t)l;
+       return 0;
+}
+
+int str_to_uoff(const char *str, uoff_t *num_r)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return -1;
+
+       if (l > (uoff_t)-1)
+               return -1;
+       *num_r = (uoff_t)l;
+       return 0;
+}
+
+bool str_uint_equals(const char *str, uintmax_t num)
+{
+       uintmax_t l;
+
+       if (str_to_uintmax(str, &l) < 0)
+               return FALSE;
+       return l == num;
+}
+
+const char *str_num_error(const char *str)
+{
+       if (*str == '-') {
+               if (!str_is_numeric(str + 1, '\0'))
+                       return "Not a valid number";
+               return "Number too small";
+       } else {
+               if (!str_is_numeric(str, '\0'))
+                       return "Not a valid number";
+               return "Number too large";
+       }
+}
diff --git a/src/lib/strnum.h b/src/lib/strnum.h
new file mode 100644 (file)
index 0000000..cd56de0
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef STRNUM_H
+#define STRNUM_H
+
+/* Return TRUE if all characters in string are numbers.
+   Stop when `end_char' is found from string. */
+bool str_is_numeric(const char *str, char end_char) ATTR_PURE;
+
+/* str_to_*() functions return 0 if string is valid number in valid range.
+   Otherwise -1 is returned and num_r is left untouched */
+
+int str_to_uint(const char *str, unsigned int *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_ulong(const char *str, unsigned long *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_ullong(const char *str, unsigned long long *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_uint32(const char *str, uint32_t *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_uint64(const char *str, uint64_t *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_uintmax(const char *str, uintmax_t *num_r) ATTR_WARN_UNUSED_RESULT;
+
+int str_to_int(const char *str, int *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_long(const char *str, long *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_llong(const char *str, long long *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_intmax(const char *str, intmax_t *num_r) ATTR_WARN_UNUSED_RESULT;
+
+int str_to_uid(const char *str, uid_t *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_gid(const char *str, gid_t *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_pid(const char *str, pid_t *num_r) ATTR_WARN_UNUSED_RESULT;
+int str_to_uoff(const char *str, uoff_t *num_r) ATTR_WARN_UNUSED_RESULT;
+
+/* Returns TRUE if str is a valid unsigned number that equals to num. */
+bool str_uint_equals(const char *str, uintmax_t num);
+
+/* Returns human readable string about what is wrong with the string.
+   This function assumes that str_to_*() had already returned -1 for the
+   string. */
+const char *str_num_error(const char *str);
+
+#endif