aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/torint.h15
-rw-r--r--src/common/util.c79
-rw-r--r--src/common/util.h2
3 files changed, 60 insertions, 36 deletions
diff --git a/src/common/torint.h b/src/common/torint.h
index 8018fd889..aec8d686e 100644
--- a/src/common/torint.h
+++ b/src/common/torint.h
@@ -106,6 +106,9 @@ typedef signed int int32_t;
typedef unsigned int uint32_t;
#define HAVE_UINT32_T
#endif
+#ifndef UINT32_MAX
+#define UINT32_MAX 0xffffffffu
+#endif
#endif
@@ -117,6 +120,9 @@ typedef signed long int32_t;
#ifndef HAVE_UINT32_T
typedef unsigned long uint32_t;
#define HAVE_UINT32_T
+#ifndef UINT32_MAX
+#define UINT32_MAX 0xfffffffful
+#endif
#endif
#elif (SIZEOF_LONG == 8)
#ifndef HAVE_INT64_T
@@ -127,6 +133,9 @@ typedef signed long int64_t;
typedef unsigned long uint64_t;
#define HAVE_UINT32_T
#endif
+#ifndef UINT64_MAX
+#define UINT64_MAX 0xfffffffffffffffful
+#endif
#endif
#if (SIZEOF_LONG_LONG == 8)
@@ -138,6 +147,9 @@ typedef signed long long int64_t;
typedef unsigned long long uint64_t;
#define HAVE_UINT64_T
#endif
+#ifndef UINT64_MAX
+#define UINT64_MAX 0xffffffffffffffffull
+#endif
#endif
#if (SIZEOF___INT64 == 8)
@@ -149,6 +161,9 @@ typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
#define HAVE_UINT64_T
#endif
+#ifndef UINT64_MAX
+#define UINT64_MAX 0xffffffffffffffffui64
+#endif
#endif
#if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
diff --git a/src/common/util.c b/src/common/util.c
index cc00ff979..6cc7ddd33 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -324,6 +324,25 @@ const char *find_whitespace(const char *s) {
return s;
}
+#define CHECK_STRTOX_RESULT() \
+ /* Was at least one character converted? */ \
+ if (endptr == s) \
+ goto err; \
+ /* Were there unexpected unconverted characters? */ \
+ if (!next && *endptr) \
+ goto err; \
+ /* Is r within limits? */ \
+ if (r < min || r > max) \
+ goto err; \
+ if (ok) *ok = 1; \
+ if (next) *next = endptr; \
+ return r; \
+ err: \
+ if (ok) *ok = 0; \
+ if (next) *next = endptr; \
+ return 0; \
+
+
/** Extract a long from the start of s, in the given numeric base. If
* there is unconverted data and next is provided, set *next to the
* first unconverted character. An error has occurred if no characters
@@ -340,26 +359,9 @@ tor_parse_long(const char *s, int base, long min, long max,
long r;
r = strtol(s, &endptr, base);
- /* Was at least one character converted? */
- if (endptr == s)
- goto err;
- /* Were there unexpected unconverted characters? */
- if (!next && *endptr)
- goto err;
- /* Is r within limits? */
- if (r < min || r > max)
- goto err;
-
- if (ok) *ok = 1;
- if (next) *next = endptr;
- return r;
- err:
- if (ok) *ok = 0;
- if (next) *next = endptr;
- return 0;
+ CHECK_STRTOX_RESULT();
}
-#if 0
unsigned long
tor_parse_ulong(const char *s, int base, unsigned long min,
unsigned long max, int *ok, char **next)
@@ -367,27 +369,32 @@ tor_parse_ulong(const char *s, int base, unsigned long min,
char *endptr;
unsigned long r;
- r = strtol(s, &endptr, base);
- /* Was at least one character converted? */
- if (endptr == s)
- goto err;
- /* Were there unexpected unconverted characters? */
- if (!next && *endptr)
- goto err;
- /* Is r within limits? */
- if (r < min || r > max)
- goto err;
-
- if (ok) *ok = 1;
- if (next) *next = endptr;
- return r;
- err:
- if (ok) *ok = 0;
- if (next) *next = endptr;
- return 0;
+ r = strtoul(s, &endptr, base);
+ CHECK_STRTOX_RESULT();
}
+
+uint64_t
+tor_parse_uint64(const char *s, int base, uint64_t min,
+ uint64_t max, int *ok, char **next)
+{
+ char *endptr;
+ uint64_t r;
+
+#ifdef HAVE_STRTOULL
+ r = (uint64_t)strtoull(s, &endptr, base);
+#elif defined(MS_WINDOWS)
+ r = (uint64_t)_strtoui64(s, &endptr, base);
+#elif SIZEOF_LONG == 8
+ r = (uint64_t)strtoul(s, &endptr, base);
+#else
+#error "I don't know how to parse 64-bit numbers."
#endif
+ CHECK_STRTOX_RESULT();
+}
+
+
+
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
{
const char *end;
diff --git a/src/common/util.h b/src/common/util.h
index 5674afc6b..128eeaf7b 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -58,6 +58,8 @@ long tor_parse_long(const char *s, int base, long min,
long max, int *ok, char **next);
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
unsigned long max, int *ok, char **next);
+uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
+ uint64_t max, int *ok, char **next);
const char *hex_str(const char *from, size_t fromlen);
const char *eat_whitespace(const char *s);
const char *eat_whitespace_no_nl(const char *s);