2014-12-20 22:52:39 +00:00
|
|
|
#include <config.h>
|
1999-12-09 13:01:21 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "ntp_types.h"
|
|
|
|
#include "ntp_stdlib.h"
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
* atouint() - convert an ascii string representing a whole base 10
|
|
|
|
* number to u_long *uval, returning TRUE if successful.
|
|
|
|
* Does not modify *uval and returns FALSE if str is not
|
|
|
|
* a positive base10 integer or is too large for a u_int32.
|
|
|
|
* this function uses u_long but should use u_int32, and
|
|
|
|
* probably be renamed.
|
|
|
|
*/
|
1999-12-09 13:01:21 +00:00
|
|
|
int
|
|
|
|
atouint(
|
|
|
|
const char *str,
|
|
|
|
u_long *uval
|
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
u_long u;
|
|
|
|
const char *cp;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
cp = str;
|
2014-12-20 22:52:39 +00:00
|
|
|
if ('\0' == *cp)
|
|
|
|
return 0;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
u = 0;
|
2014-12-20 22:52:39 +00:00
|
|
|
while ('\0' != *cp) {
|
2015-02-05 20:53:33 +00:00
|
|
|
if (!isdigit((unsigned char)*cp))
|
2014-12-20 22:52:39 +00:00
|
|
|
return 0;
|
1999-12-09 13:01:21 +00:00
|
|
|
if (u > 429496729 || (u == 429496729 && *cp >= '6'))
|
2014-12-20 22:52:39 +00:00
|
|
|
return 0; /* overflow */
|
|
|
|
/* hand-optimized u *= 10; */
|
1999-12-09 13:01:21 +00:00
|
|
|
u = (u << 3) + (u << 1);
|
2014-12-20 22:52:39 +00:00
|
|
|
u += *cp++ - '0'; /* not '\0' */
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*uval = u;
|
|
|
|
return 1;
|
|
|
|
}
|