strtoofft.c 4.3 KB

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