2001-11-30 01:02:18 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2001-11-30 01:02:18 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2001-11-30 01:02:18 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2001-11-30 01:02:18 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2001-11-30 01:02:18 +00:00
|
|
|
|
|
|
|
#include <isc/parseint.h>
|
|
|
|
#include <isc/result.h>
|
2003-06-24 05:11:53 +00:00
|
|
|
#include <isc/stdlib.h>
|
2001-11-30 01:02:18 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2018-03-28 14:19:37 +02:00
|
|
|
isc_parse_uint32(uint32_t *uip, const char *string, int base) {
|
2001-11-30 01:02:18 +00:00
|
|
|
unsigned long n;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t r;
|
2001-11-30 01:02:18 +00:00
|
|
|
char *e;
|
|
|
|
if (! isalnum((unsigned char)(string[0])))
|
|
|
|
return (ISC_R_BADNUMBER);
|
|
|
|
errno = 0;
|
|
|
|
n = strtoul(string, &e, base);
|
|
|
|
if (*e != '\0')
|
|
|
|
return (ISC_R_BADNUMBER);
|
2012-09-12 15:08:19 -05:00
|
|
|
/*
|
|
|
|
* Where long is 64 bits we need to convert to 32 bits then test for
|
|
|
|
* equality. This is a no-op on 32 bit machines and a good compiler
|
|
|
|
* will optimise it away.
|
|
|
|
*/
|
2018-03-28 14:19:37 +02:00
|
|
|
r = (uint32_t)n;
|
2012-09-12 15:08:19 -05:00
|
|
|
if ((n == ULONG_MAX && errno == ERANGE) || (n != (unsigned long)r))
|
2001-11-30 01:02:18 +00:00
|
|
|
return (ISC_R_RANGE);
|
2012-09-12 15:08:19 -05:00
|
|
|
*uip = r;
|
2001-11-30 01:02:18 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2002-02-28 20:08:06 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2018-03-28 14:19:37 +02:00
|
|
|
isc_parse_uint16(uint16_t *uip, const char *string, int base) {
|
|
|
|
uint32_t val;
|
2002-02-28 20:08:06 +00:00
|
|
|
isc_result_t result;
|
|
|
|
result = isc_parse_uint32(&val, string, base);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
if (val > 0xFFFF)
|
|
|
|
return (ISC_R_RANGE);
|
2018-03-28 14:19:37 +02:00
|
|
|
*uip = (uint16_t) val;
|
2002-02-28 20:08:06 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2018-03-28 14:19:37 +02:00
|
|
|
isc_parse_uint8(uint8_t *uip, const char *string, int base) {
|
|
|
|
uint32_t val;
|
2002-02-28 20:08:06 +00:00
|
|
|
isc_result_t result;
|
|
|
|
result = isc_parse_uint32(&val, string, base);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
if (val > 0xFF)
|
|
|
|
return (ISC_R_RANGE);
|
2018-03-28 14:19:37 +02:00
|
|
|
*uip = (uint8_t) val;
|
2002-02-28 20:08:06 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|