Quellcode durchsuchen

Unify tor_parse_(numeric); make sure MAX_UINT32 and MAX_UINT64 are defined

svn:r2688
Nick Mathewson vor 20 Jahren
Ursprung
Commit
38ed0ce5e6
4 geänderte Dateien mit 61 neuen und 37 gelöschten Zeilen
  1. 1 1
      configure.in
  2. 15 0
      src/common/torint.h
  3. 43 36
      src/common/util.c
  4. 2 0
      src/common/util.h

+ 1 - 1
configure.in

@@ -143,7 +143,7 @@ dnl These headers are not essential
 
 AC_CHECK_HEADERS(stdint.h sys/types.h inttypes.h sys/param.h sys/wait.h sys/limits.h netinet/in.h arpa/inet.h machine/limits.h syslog.h sys/time.h sys/resource.h)
 
-AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit setrlimit strlcat strlcpy)
+AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit setrlimit strlcat strlcpy strtoull)
 
 AC_CHECK_MEMBERS([struct timeval.tv_sec])
 

+ 15 - 0
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)

+ 43 - 36
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;

+ 2 - 0
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);