strtoofft.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, 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. /*
  26. * NOTE:
  27. *
  28. * In the ISO C standard (IEEE Std 1003.1), there is a strtoimax() function we
  29. * could use in case strtoll() doesn't exist... See
  30. * http://www.opengroup.org/onlinepubs/009695399/functions/strtoimax.html
  31. */
  32. #ifdef NEED_CURL_STRTOLL
  33. #include <stdlib.h>
  34. #include <ctype.h>
  35. #include <errno.h>
  36. static int get_char(char c, int base);
  37. /**
  38. * Emulated version of the strtoll function. This extracts a long long
  39. * value from the given input string and returns it.
  40. */
  41. curl_off_t
  42. curlx_strtoll(const char *nptr, char **endptr, int base)
  43. {
  44. char *end;
  45. int is_negative = 0;
  46. int overflow;
  47. int i;
  48. curl_off_t value = 0;
  49. curl_off_t newval;
  50. /* Skip leading whitespace. */
  51. end = (char *)nptr;
  52. while (ISSPACE(end[0])) {
  53. end++;
  54. }
  55. /* Handle the sign, if any. */
  56. if (end[0] == '-') {
  57. is_negative = 1;
  58. end++;
  59. }
  60. else if (end[0] == '+') {
  61. end++;
  62. }
  63. else if (end[0] == '\0') {
  64. /* We had nothing but perhaps some whitespace -- there was no number. */
  65. if (endptr) {
  66. *endptr = end;
  67. }
  68. return 0;
  69. }
  70. /* Handle special beginnings, if present and allowed. */
  71. if (end[0] == '0' && end[1] == 'x') {
  72. if (base == 16 || base == 0) {
  73. end += 2;
  74. base = 16;
  75. }
  76. }
  77. else if (end[0] == '0') {
  78. if (base == 8 || base == 0) {
  79. end++;
  80. base = 8;
  81. }
  82. }
  83. /* Matching strtol, if the base is 0 and it doesn't look like
  84. * the number is octal or hex, we assume it's base 10.
  85. */
  86. if (base == 0) {
  87. base = 10;
  88. }
  89. /* Loop handling digits. */
  90. value = 0;
  91. overflow = 0;
  92. for (i = get_char(end[0], base);
  93. i != -1;
  94. end++, i = get_char(end[0], base)) {
  95. newval = base * value + i;
  96. if (newval < value) {
  97. /* We've overflowed. */
  98. overflow = 1;
  99. break;
  100. }
  101. else
  102. value = newval;
  103. }
  104. if (!overflow) {
  105. if (is_negative) {
  106. /* Fix the sign. */
  107. value *= -1;
  108. }
  109. }
  110. else {
  111. if (is_negative)
  112. value = CURL_LLONG_MIN;
  113. else
  114. value = CURL_LLONG_MAX;
  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. */