strtoq.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1990, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * Chris Torek.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <sys/param.h>
  35. #include <sys/systm.h>
  36. #include <sys/ctype.h>
  37. #include <sys/limits.h>
  38. /*
  39. * Convert a string to a quad integer.
  40. *
  41. * Ignores `locale' stuff. Assumes that the upper and lower case
  42. * alphabets and digits are each contiguous.
  43. */
  44. quad_t
  45. strtoq(const char *nptr, char **endptr, int base)
  46. {
  47. const char *s;
  48. u_quad_t acc;
  49. unsigned char c;
  50. u_quad_t qbase, cutoff;
  51. int neg, any, cutlim;
  52. /*
  53. * Skip white space and pick up leading +/- sign if any.
  54. * If base is 0, allow 0x for hex and 0 for octal, else
  55. * assume decimal; if base is already 16, allow 0x.
  56. */
  57. s = nptr;
  58. do {
  59. c = *s++;
  60. } while (isspace(c));
  61. if (c == '-') {
  62. neg = 1;
  63. c = *s++;
  64. } else {
  65. neg = 0;
  66. if (c == '+')
  67. c = *s++;
  68. }
  69. if ((base == 0 || base == 16) &&
  70. c == '0' && (*s == 'x' || *s == 'X')) {
  71. c = s[1];
  72. s += 2;
  73. base = 16;
  74. }
  75. if (base == 0)
  76. base = c == '0' ? 8 : 10;
  77. /*
  78. * Compute the cutoff value between legal numbers and illegal
  79. * numbers. That is the largest legal value, divided by the
  80. * base. An input number that is greater than this value, if
  81. * followed by a legal input character, is too big. One that
  82. * is equal to this value may be valid or not; the limit
  83. * between valid and invalid numbers is then based on the last
  84. * digit. For instance, if the range for quads is
  85. * [-9223372036854775808..9223372036854775807] and the input base
  86. * is 10, cutoff will be set to 922337203685477580 and cutlim to
  87. * either 7 (neg==0) or 8 (neg==1), meaning that if we have
  88. * accumulated a value > 922337203685477580, or equal but the
  89. * next digit is > 7 (or 8), the number is too big, and we will
  90. * return a range error.
  91. *
  92. * Set any if any `digits' consumed; make it negative to indicate
  93. * overflow.
  94. */
  95. qbase = (unsigned)base;
  96. cutoff = neg ? (u_quad_t)-(QUAD_MIN + QUAD_MAX) + QUAD_MAX : QUAD_MAX;
  97. cutlim = cutoff % qbase;
  98. cutoff /= qbase;
  99. for (acc = 0, any = 0;; c = *s++) {
  100. if (!isascii(c))
  101. break;
  102. if (isdigit(c))
  103. c -= '0';
  104. else if (isalpha(c))
  105. c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  106. else
  107. break;
  108. if (c >= base)
  109. break;
  110. if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  111. any = -1;
  112. else {
  113. any = 1;
  114. acc *= qbase;
  115. acc += c;
  116. }
  117. }
  118. if (any < 0) {
  119. acc = neg ? QUAD_MIN : QUAD_MAX;
  120. } else if (neg)
  121. acc = -acc;
  122. if (endptr != NULL)
  123. *endptr = __DECONST(char *, any ? s - 1 : nptr);
  124. return (acc);
  125. }