typedef unsigned int uint32_t;
#define HAVE_UINT32_T
#endif
+#ifndef UINT32_MAX
+#define UINT32_MAX 0xffffffffu
+#endif
#endif
#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
typedef unsigned long uint64_t;
#define HAVE_UINT32_T
#endif
+#ifndef UINT64_MAX
+#define UINT64_MAX 0xfffffffffffffffful
+#endif
#endif
#if (SIZEOF_LONG_LONG == 8)
typedef unsigned long long uint64_t;
#define HAVE_UINT64_T
#endif
+#ifndef UINT64_MAX
+#define UINT64_MAX 0xffffffffffffffffull
+#endif
#endif
#if (SIZEOF___INT64 == 8)
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)
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
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)
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;
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);