strtoll.c 4.2 KB

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