strtoofft.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, 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. /* Range tests can be used for alphanum decoding if characters are consecutive,
  37. like in ASCII. Else an array is scanned. Determine this condition now. */
  38. #if('9' - '0') != 9 || ('Z' - 'A') != 25 || ('z' - 'a') != 25
  39. #include <string.h>
  40. #define NO_RANGE_TEST
  41. static const char valchars[] =
  42. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  43. #endif
  44. static int get_char(char c, int base);
  45. /**
  46. * Emulated version of the strtoll function. This extracts a long long
  47. * value from the given input string and returns it.
  48. */
  49. curl_off_t
  50. curlx_strtoll(const char *nptr, char **endptr, int base)
  51. {
  52. char *end;
  53. int is_negative = 0;
  54. int overflow;
  55. int i;
  56. curl_off_t value = 0;
  57. curl_off_t newval;
  58. /* Skip leading whitespace. */
  59. end = (char *)nptr;
  60. while(ISSPACE(end[0])) {
  61. end++;
  62. }
  63. /* Handle the sign, if any. */
  64. if(end[0] == '-') {
  65. is_negative = 1;
  66. end++;
  67. }
  68. else if(end[0] == '+') {
  69. end++;
  70. }
  71. else if(end[0] == '\0') {
  72. /* We had nothing but perhaps some whitespace -- there was no number. */
  73. if(endptr) {
  74. *endptr = end;
  75. }
  76. return 0;
  77. }
  78. /* Handle special beginnings, if present and allowed. */
  79. if(end[0] == '0' && end[1] == 'x') {
  80. if(base == 16 || base == 0) {
  81. end += 2;
  82. base = 16;
  83. }
  84. }
  85. else if(end[0] == '0') {
  86. if(base == 8 || base == 0) {
  87. end++;
  88. base = 8;
  89. }
  90. }
  91. /* Matching strtol, if the base is 0 and it doesn't look like
  92. * the number is octal or hex, we assume it's base 10.
  93. */
  94. if(base == 0) {
  95. base = 10;
  96. }
  97. /* Loop handling digits. */
  98. value = 0;
  99. overflow = 0;
  100. for (i = get_char(end[0], base);
  101. i != -1;
  102. end++, i = get_char(end[0], base)) {
  103. newval = base * value + i;
  104. if(newval < value) {
  105. /* We've overflowed. */
  106. overflow = 1;
  107. break;
  108. }
  109. else
  110. value = newval;
  111. }
  112. if(!overflow) {
  113. if(is_negative) {
  114. /* Fix the sign. */
  115. value *= -1;
  116. }
  117. }
  118. else {
  119. if(is_negative)
  120. value = CURL_LLONG_MIN;
  121. else
  122. value = CURL_LLONG_MAX;
  123. SET_ERRNO(ERANGE);
  124. }
  125. if(endptr)
  126. *endptr = end;
  127. return value;
  128. }
  129. /**
  130. * Returns the value of c in the given base, or -1 if c cannot
  131. * be interpreted properly in that base (i.e., is out of range,
  132. * is a null, etc.).
  133. *
  134. * @param c the character to interpret according to base
  135. * @param base the base in which to interpret c
  136. *
  137. * @return the value of c in base, or -1 if c isn't in range
  138. */
  139. static int get_char(char c, int base)
  140. {
  141. #ifndef NO_RANGE_TEST
  142. int value = -1;
  143. if(c <= '9' && c >= '0') {
  144. value = c - '0';
  145. }
  146. else if(c <= 'Z' && c >= 'A') {
  147. value = c - 'A' + 10;
  148. }
  149. else if(c <= 'z' && c >= 'a') {
  150. value = c - 'a' + 10;
  151. }
  152. #else
  153. const char * cp;
  154. int value;
  155. cp = memchr(valchars, c, 10 + 26 + 26);
  156. if(!cp)
  157. return -1;
  158. value = cp - valchars;
  159. if(value >= 10 + 26)
  160. value -= 26; /* Lowercase. */
  161. #endif
  162. if(value >= base) {
  163. value = -1;
  164. }
  165. return value;
  166. }
  167. #endif /* Only present if we need strtoll, but don't have it. */