1999-02-05 04:57:20 +00:00
|
|
|
/*
|
2000-02-03 23:08:31 +00:00
|
|
|
* Copyright (C) 1999, 2000 Internet Software Consortium.
|
1999-02-05 04:57:20 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <isc/str.h>
|
|
|
|
|
|
|
|
static char digits[] = "0123456789abcdefghijklmnoprstuvwxyz";
|
|
|
|
|
|
|
|
isc_uint64_t
|
|
|
|
isc_strtouq(char *source, char **end, int base) {
|
|
|
|
isc_uint64_t tmp;
|
|
|
|
isc_uint64_t overflow;
|
|
|
|
char *s = source;
|
|
|
|
char *o;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if ((base < 0) || (base == 1) || (base > 36)) {
|
|
|
|
*end = source;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*s != 0 && isascii(*s&0xff) && isspace(*s&0xff))
|
|
|
|
s++;
|
|
|
|
if (*s == '+' /* || *s == '-' */)
|
|
|
|
s++;
|
|
|
|
if (base == 0) {
|
|
|
|
if (*s == '0' && (*(s+1) == 'X' || *(s+1) == 'x')) {
|
|
|
|
s += 2;
|
|
|
|
base = 16;
|
|
|
|
} else if (*s == '0')
|
|
|
|
base = 8;
|
|
|
|
else
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
if (*s == 0) {
|
|
|
|
*end = source;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
overflow = ~0;
|
|
|
|
overflow /= base;
|
|
|
|
tmp = 0;
|
|
|
|
|
|
|
|
while ((c = *s) != 0) {
|
|
|
|
c = tolower(c);
|
|
|
|
/* end ? */
|
|
|
|
if ((o = strchr(digits, c)) == NULL) {
|
|
|
|
*end = s;
|
|
|
|
return (tmp);
|
|
|
|
}
|
|
|
|
/* end ? */
|
|
|
|
if ((o - digits) >= base) {
|
|
|
|
*end = s;
|
|
|
|
return (tmp);
|
|
|
|
}
|
|
|
|
/* overflow ? */
|
|
|
|
if (tmp > overflow) {
|
|
|
|
*end = source;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
tmp *= base;
|
|
|
|
/* overflow ? */
|
|
|
|
if ((tmp + (o - digits)) < tmp) {
|
|
|
|
*end = source;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
tmp += o - digits;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*end = s;
|
|
|
|
return (tmp);
|
|
|
|
}
|