tor  0.4.1.0-alpha-dev
parse_int.c
Go to the documentation of this file.
1 /* Copyright (c) 2003, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #include "lib/string/parse_int.h"
12 
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 /* Helper: common code to check whether the result of a strtol or strtoul or
18  * strtoll is correct. */
19 #define CHECK_STRTOX_RESULT() \
20  /* Did an overflow occur? */ \
21  if (errno == ERANGE) \
22  goto err; \
23  /* Was at least one character converted? */ \
24  if (endptr == s) \
25  goto err; \
26  /* Were there unexpected unconverted characters? */ \
27  if (!next && *endptr) \
28  goto err; \
29  /* Illogical (max, min) inputs? */ \
30  if (max < min) \
31  goto err; \
32  /* Is r within limits? */ \
33  if (r < min || r > max) \
34  goto err; \
35  if (ok) *ok = 1; \
36  if (next) *next = endptr; \
37  return r; \
38  err: \
39  if (ok) *ok = 0; \
40  if (next) *next = endptr; \
41  return 0
42 
55 long
56 tor_parse_long(const char *s, int base, long min, long max,
57  int *ok, char **next)
58 {
59  char *endptr;
60  long r;
61 
62  if (base < 0) {
63  if (ok)
64  *ok = 0;
65  return 0;
66  }
67 
68  errno = 0;
69  r = strtol(s, &endptr, base);
70  CHECK_STRTOX_RESULT();
71 }
72 
74 unsigned long
75 tor_parse_ulong(const char *s, int base, unsigned long min,
76  unsigned long max, int *ok, char **next)
77 {
78  char *endptr;
79  unsigned long r;
80 
81  if (base < 0) {
82  if (ok)
83  *ok = 0;
84  return 0;
85  }
86 
87  errno = 0;
88  r = strtoul(s, &endptr, base);
89  CHECK_STRTOX_RESULT();
90 }
91 
93 double
94 tor_parse_double(const char *s, double min, double max, int *ok, char **next)
95 {
96  char *endptr;
97  double r;
98 
99  errno = 0;
100  r = strtod(s, &endptr);
101  CHECK_STRTOX_RESULT();
102 }
103 
106 uint64_t
107 tor_parse_uint64(const char *s, int base, uint64_t min,
108  uint64_t max, int *ok, char **next)
109 {
110  char *endptr;
111  uint64_t r;
112 
113  if (base < 0) {
114  if (ok)
115  *ok = 0;
116  return 0;
117  }
118 
119  errno = 0;
120 #ifdef HAVE_STRTOULL
121  r = (uint64_t)strtoull(s, &endptr, base);
122 #elif defined(_WIN32)
123  r = (uint64_t)_strtoui64(s, &endptr, base);
124 #elif SIZEOF_LONG == 8
125  r = (uint64_t)strtoul(s, &endptr, base);
126 #else
127 #error "I don't know how to parse 64-bit numbers."
128 #endif /* defined(HAVE_STRTOULL) || ... */
129 
130  CHECK_STRTOX_RESULT();
131 }
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min, uint64_t max, int *ok, char **next)
Definition: parse_int.c:107
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
Definition: parse_int.c:75
double tor_parse_double(const char *s, double min, double max, int *ok, char **next)
Definition: parse_int.c:94
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
Definition: parse_int.c:56
Header for parse_int.c.