/contrib/bind9/lib/isc/strtoul.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 129 lines · 62 code · 8 blank · 59 comment · 36 complexity · 702ccca8e66ffb3e3cf85a60de6dbe38 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Copyright (c) 1990, 1993
  19. * The Regents of the University of California. All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. * 1. Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. * 3. All advertising materials mentioning features or use of this software
  30. * must display the following acknowledgement:
  31. * This product includes software developed by the University of
  32. * California, Berkeley and its contributors.
  33. * 4. Neither the name of the University nor the names of its contributors
  34. * may be used to endorse or promote products derived from this software
  35. * without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  38. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  43. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. */
  49. /*! \file */
  50. #if defined(LIBC_SCCS) && !defined(lint)
  51. static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
  52. #endif /* LIBC_SCCS and not lint */
  53. /* $Id: strtoul.c,v 1.7 2007/06/19 23:47:17 tbox Exp $ */
  54. #include <config.h>
  55. #include <limits.h>
  56. #include <ctype.h>
  57. #include <errno.h>
  58. #include <isc/stdlib.h>
  59. #include <isc/util.h>
  60. /*!
  61. * Convert a string to an unsigned long integer.
  62. *
  63. * Ignores `locale' stuff. Assumes that the upper and lower case
  64. * alphabets and digits are each contiguous.
  65. */
  66. unsigned long
  67. isc_strtoul(const char *nptr, char **endptr, int base) {
  68. const char *s = nptr;
  69. unsigned long acc;
  70. unsigned char c;
  71. unsigned long cutoff;
  72. int neg = 0, any, cutlim;
  73. /*
  74. * See strtol for comments as to the logic used.
  75. */
  76. do {
  77. c = *s++;
  78. } while (isspace(c));
  79. if (c == '-') {
  80. neg = 1;
  81. c = *s++;
  82. } else if (c == '+')
  83. c = *s++;
  84. if ((base == 0 || base == 16) &&
  85. c == '0' && (*s == 'x' || *s == 'X')) {
  86. c = s[1];
  87. s += 2;
  88. base = 16;
  89. }
  90. if (base == 0)
  91. base = c == '0' ? 8 : 10;
  92. cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
  93. cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
  94. for (acc = 0, any = 0;; c = *s++) {
  95. if (!isascii(c))
  96. break;
  97. if (isdigit(c))
  98. c -= '0';
  99. else if (isalpha(c))
  100. c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  101. else
  102. break;
  103. if (c >= base)
  104. break;
  105. if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  106. any = -1;
  107. else {
  108. any = 1;
  109. acc *= base;
  110. acc += c;
  111. }
  112. }
  113. if (any < 0) {
  114. acc = ULONG_MAX;
  115. errno = ERANGE;
  116. } else if (neg)
  117. acc = -acc;
  118. if (endptr != 0)
  119. DE_CONST(any ? s - 1 : nptr, *endptr);
  120. return (acc);
  121. }