parseint.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2012  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2001-2003  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: parseint.c,v 1.8 2007/06/19 23:47:17 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <ctype.h>
00025 #include <errno.h>
00026 #include <limits.h>
00027 
00028 #include <isc/parseint.h>
00029 #include <isc/result.h>
00030 #include <isc/stdlib.h>
00031 
00032 isc_result_t
00033 isc_parse_uint32(isc_uint32_t *uip, const char *string, int base) {
00034         unsigned long n;
00035         isc_uint32_t r;
00036         char *e;
00037         if (! isalnum((unsigned char)(string[0])))
00038                 return (ISC_R_BADNUMBER);
00039         errno = 0;
00040         n = strtoul(string, &e, base);
00041         if (*e != '\0')
00042                 return (ISC_R_BADNUMBER);
00043         /*
00044          * Where long is 64 bits we need to convert to 32 bits then test for
00045          * equality.  This is a no-op on 32 bit machines and a good compiler
00046          * will optimise it away.
00047          */
00048         r = (isc_uint32_t)n;
00049         if ((n == ULONG_MAX && errno == ERANGE) || (n != (unsigned long)r))
00050                 return (ISC_R_RANGE);
00051         *uip = r;
00052         return (ISC_R_SUCCESS);
00053 }
00054 
00055 isc_result_t
00056 isc_parse_uint16(isc_uint16_t *uip, const char *string, int base) {
00057         isc_uint32_t val;
00058         isc_result_t result;
00059         result = isc_parse_uint32(&val, string, base);
00060         if (result != ISC_R_SUCCESS)
00061                 return (result);
00062         if (val > 0xFFFF)
00063                 return (ISC_R_RANGE);
00064         *uip = (isc_uint16_t) val;
00065         return (ISC_R_SUCCESS);
00066 }
00067 
00068 isc_result_t
00069 isc_parse_uint8(isc_uint8_t *uip, const char *string, int base) {
00070         isc_uint32_t val;
00071         isc_result_t result;
00072         result = isc_parse_uint32(&val, string, base);
00073         if (result != ISC_R_SUCCESS)
00074                 return (result);
00075         if (val > 0xFF)
00076                 return (ISC_R_RANGE);
00077         *uip = (isc_uint8_t) val;
00078         return (ISC_R_SUCCESS);
00079 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha