strtoofft.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include "strtoofft.h"
  25. #ifdef NEED_CURL_STRTOLL
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. static int get_char(char c, int base);
  30. /**
  31. * Emulated version of the strtoll function. This extracts a long long
  32. * value from the given input string and returns it.
  33. */
  34. curl_off_t
  35. curlx_strtoll(const char *nptr, char **endptr, int base)
  36. {
  37. char *end;
  38. int is_negative = 0;
  39. int overflow;
  40. int i;
  41. curl_off_t value = 0;
  42. curl_off_t newval;
  43. /* Skip leading whitespace. */
  44. end = (char *)nptr;
  45. while (isspace((int)end[0])) {
  46. end++;
  47. }
  48. /* Handle the sign, if any. */
  49. if (end[0] == '-') {
  50. is_negative = 1;
  51. end++;
  52. }
  53. else if (end[0] == '+') {
  54. end++;
  55. }
  56. else if (end[0] == '\0') {
  57. /* We had nothing but perhaps some whitespace -- there was no number. */
  58. if (endptr) {
  59. *endptr = end;
  60. }
  61. return 0;
  62. }
  63. /* Handle special beginnings, if present and allowed. */
  64. if (end[0] == '0' && end[1] == 'x') {
  65. if (base == 16 || base == 0) {
  66. end += 2;
  67. base = 16;
  68. }
  69. }
  70. else if (end[0] == '0') {
  71. if (base == 8 || base == 0) {
  72. end++;
  73. base = 8;
  74. }
  75. }
  76. /* Matching strtol, if the base is 0 and it doesn't look like
  77. * the number is octal or hex, we assume it's base 10.
  78. */
  79. if (base == 0) {
  80. base = 10;
  81. }
  82. /* Loop handling digits. */
  83. value = 0;
  84. overflow = 0;
  85. for (i = get_char(end[0], base);
  86. i != -1;
  87. end++, i = get_char(end[0], base)) {
  88. newval = base * value + i;
  89. if (newval < value) {
  90. /* We've overflowed. */
  91. overflow = 1;
  92. break;
  93. }
  94. else
  95. value = newval;
  96. }
  97. if (!overflow) {
  98. if (is_negative) {
  99. /* Fix the sign. */
  100. value *= -1;
  101. }
  102. }
  103. else {
  104. #ifdef HAVE_LONG_LONG_CONSTANT
  105. if (is_negative)
  106. value = 0x8000000000000000LL;
  107. else
  108. value = 0x7FFFFFFFFFFFFFFFLL;
  109. #else
  110. if (is_negative)
  111. value = 0x8000000000000000L;
  112. else
  113. value = 0x7FFFFFFFFFFFFFFFL;
  114. #endif
  115. errno = ERANGE;
  116. }
  117. if (endptr)
  118. *endptr = end;
  119. return value;
  120. }
  121. /**
  122. * Returns the value of c in the given base, or -1 if c cannot
  123. * be interpreted properly in that base (i.e., is out of range,
  124. * is a null, etc.).
  125. *
  126. * @param c the character to interpret according to base
  127. * @param base the base in which to interpret c
  128. *
  129. * @return the value of c in base, or -1 if c isn't in range
  130. */
  131. static int get_char(char c, int base)
  132. {
  133. int value = -1;
  134. if (c <= '9' && c >= '0') {
  135. value = c - '0';
  136. }
  137. else if (c <= 'Z' && c >= 'A') {
  138. value = c - 'A' + 10;
  139. }
  140. else if (c <= 'z' && c >= 'a') {
  141. value = c - 'a' + 10;
  142. }
  143. if (value >= base) {
  144. value = -1;
  145. }
  146. return value;
  147. }
  148. #endif /* Only present if we need strtoll, but don't have it. */