18 #define MAX_SCANF_WIDTH 9999
25 int num = ((int)d) - (int)
'0';
26 raw_assert(num <= 9 && num >= 0);
35 scan_unsigned(
const char **bufp,
unsigned long *out,
int width,
unsigned base)
37 unsigned long result = 0;
38 int scanned_so_far = 0;
39 const int hex = base==16;
40 raw_assert(base == 10 || base == 16);
41 if (!bufp || !*bufp || !out)
44 width=MAX_SCANF_WIDTH;
46 while (**bufp && (hex?TOR_ISXDIGIT(**bufp):TOR_ISDIGIT(**bufp))
47 && scanned_so_far < width) {
54 if (result > (ULONG_MAX - digit)/base)
56 result = result * base + digit;
75 unsigned long result = 0;
77 if (!bufp || !*bufp || !out)
80 width=MAX_SCANF_WIDTH;
91 if (neg && result > 0) {
92 if (result > ((
unsigned long)LONG_MAX) + 1)
94 else if (result == ((
unsigned long)LONG_MAX) + 1)
101 *out = -(long)result;
104 if (result > LONG_MAX)
121 int scanned_so_far = 0;
123 if (!bufp || !*bufp || !out)
126 width=MAX_SCANF_WIDTH;
133 while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
135 result = result * 10 + digit;
139 double fracval = 0, denominator = 1;
142 while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
144 fracval = fracval * 10 + digit;
148 result += fracval / denominator;
154 *out = neg ? -result : result;
164 int scanned_so_far = 0;
165 if (!bufp || !out || width < 0)
167 while (**bufp && ! TOR_ISSPACE(**bufp) && scanned_so_far < width) {
184 if (*pattern !=
'%') {
185 if (*buf == *pattern) {
196 if (TOR_ISDIGIT(*pattern)) {
198 while (TOR_ISDIGIT(*pattern)) {
201 if (width > MAX_SCANF_WIDTH)
207 if (*pattern ==
'l') {
211 if (*pattern ==
'u' || *pattern ==
'x') {
213 const int base = (*pattern ==
'u') ? 10 : 16;
219 unsigned long *out = va_arg(ap,
unsigned long *);
222 unsigned *out = va_arg(ap,
unsigned *);
229 }
else if (*pattern ==
'f') {
230 double *d = va_arg(ap,
double *);
239 }
else if (*pattern ==
'd') {
244 long *out = va_arg(ap,
long *);
247 int *out = va_arg(ap,
int *);
248 #if LONG_MAX > INT_MAX
249 if (lng < INT_MIN || lng > INT_MAX)
256 }
else if (*pattern ==
's') {
257 char *s = va_arg(ap,
char *);
266 }
else if (*pattern ==
'c') {
267 char *ch = va_arg(ap,
char *);
277 }
else if (*pattern ==
'%') {
313 va_start(ap, pattern);
Locale-independent character-type inspection (header)
static int hex_decode_digit(char c)
static int scan_double(const char **bufp, double *out, int width)
int tor_sscanf(const char *buf, const char *pattern,...)
static int scan_unsigned(const char **bufp, unsigned long *out, int width, unsigned base)
static int scan_string(const char **bufp, char *out, int width)
int tor_vsscanf(const char *buf, const char *pattern, va_list ap)
static int digit_to_num(char d)
static int scan_signed(const char **bufp, long *out, int width)
Integer definitions used throughout Tor.