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
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://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 <ctype.h>
|
|
|
|
#include <errno.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <limits.h>
|
2018-08-21 15:10:32 +02:00
|
|
|
#include <stdlib.h>
|
2001-11-30 01:02:18 +00:00
|
|
|
|
|
|
|
#include <isc/parseint.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_parse_uint32(uint32_t *uip, const char *string, int base) {
|
2001-11-30 01:02:18 +00:00
|
|
|
unsigned long n;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t r;
|
|
|
|
char *e;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (!isalnum((unsigned char)(string[0]))) {
|
2001-11-30 01:02:18 +00:00
|
|
|
return (ISC_R_BADNUMBER);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-11-30 01:02:18 +00:00
|
|
|
errno = 0;
|
|
|
|
n = strtoul(string, &e, base);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*e != '\0') {
|
2001-11-30 01:02:18 +00:00
|
|
|
return (ISC_R_BADNUMBER);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
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;
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((n == ULONG_MAX && errno == ERANGE) || (n != (unsigned long)r)) {
|
2001-11-30 01:02:18 +00:00
|
|
|
return (ISC_R_RANGE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
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
|
2020-02-13 14:44:37 -08: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);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2002-02-28 20:08:06 +00:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (val > 0xFFFF) {
|
2002-02-28 20:08:06 +00:00
|
|
|
return (ISC_R_RANGE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
*uip = (uint16_t)val;
|
2002-02-28 20:08:06 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08: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);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2002-02-28 20:08:06 +00:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (val > 0xFF) {
|
2002-02-28 20:08:06 +00:00
|
|
|
return (ISC_R_RANGE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
*uip = (uint8_t)val;
|
2002-02-28 20:08:06 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|