[or-cvs] Unify tor_parse_(numeric); make sure MAX_UINT32 and MAX_UIN...

Nick Mathewson nickm at seul.org
Fri Nov 5 17:54:52 UTC 2004


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv1530/src/common

Modified Files:
	torint.h util.c util.h 
Log Message:
Unify tor_parse_(numeric); make sure MAX_UINT32 and MAX_UINT64 are defined

Index: torint.h
===================================================================
RCS file: /home/or/cvsroot/src/common/torint.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- torint.h	25 Aug 2004 19:16:18 -0000	1.10
+++ torint.h	5 Nov 2004 17:54:50 -0000	1.11
@@ -106,6 +106,9 @@
 typedef unsigned int uint32_t;
 #define HAVE_UINT32_T
 #endif
+#ifndef UINT32_MAX
+#define UINT32_MAX 0xffffffffu
+#endif
 #endif
 
 
@@ -117,6 +120,9 @@
 #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 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 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 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)

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.168
retrieving revision 1.169
diff -u -d -r1.168 -r1.169
--- util.c	4 Nov 2004 22:29:45 -0000	1.168
+++ util.c	5 Nov 2004 17:54:50 -0000	1.169
@@ -324,6 +324,25 @@
   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 @@
   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 @@
   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;

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.114
retrieving revision 1.115
diff -u -d -r1.114 -r1.115
--- util.h	4 Nov 2004 22:29:45 -0000	1.114
+++ util.h	5 Nov 2004 17:54:50 -0000	1.115
@@ -58,6 +58,8 @@
                     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);



More information about the tor-commits mailing list