00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "setup.h"
00025 #include "strtoofft.h"
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifdef NEED_CURL_STRTOLL
00036 #include <stdlib.h>
00037 #include <ctype.h>
00038 #include <errno.h>
00039
00040 static int get_char(char c, int base);
00041
00046 curl_off_t
00047 curlx_strtoll(const char *nptr, char **endptr, int base)
00048 {
00049 char *end;
00050 int is_negative = 0;
00051 int overflow;
00052 int i;
00053 curl_off_t value = 0;
00054 curl_off_t newval;
00055
00056
00057 end = (char *)nptr;
00058 while (ISSPACE(end[0])) {
00059 end++;
00060 }
00061
00062
00063 if (end[0] == '-') {
00064 is_negative = 1;
00065 end++;
00066 }
00067 else if (end[0] == '+') {
00068 end++;
00069 }
00070 else if (end[0] == '\0') {
00071
00072 if (endptr) {
00073 *endptr = end;
00074 }
00075 return 0;
00076 }
00077
00078
00079 if (end[0] == '0' && end[1] == 'x') {
00080 if (base == 16 || base == 0) {
00081 end += 2;
00082 base = 16;
00083 }
00084 }
00085 else if (end[0] == '0') {
00086 if (base == 8 || base == 0) {
00087 end++;
00088 base = 8;
00089 }
00090 }
00091
00092
00093
00094
00095 if (base == 0) {
00096 base = 10;
00097 }
00098
00099
00100 value = 0;
00101 overflow = 0;
00102 for (i = get_char(end[0], base);
00103 i != -1;
00104 end++, i = get_char(end[0], base)) {
00105 newval = base * value + i;
00106 if (newval < value) {
00107
00108 overflow = 1;
00109 break;
00110 }
00111 else
00112 value = newval;
00113 }
00114
00115 if (!overflow) {
00116 if (is_negative) {
00117
00118 value *= -1;
00119 }
00120 }
00121 else {
00122 if (is_negative)
00123 value = CURL_LLONG_MIN;
00124 else
00125 value = CURL_LLONG_MAX;
00126
00127 SET_ERRNO(ERANGE);
00128 }
00129
00130 if (endptr)
00131 *endptr = end;
00132
00133 return value;
00134 }
00135
00146 static int get_char(char c, int base)
00147 {
00148 int value = -1;
00149 if (c <= '9' && c >= '0') {
00150 value = c - '0';
00151 }
00152 else if (c <= 'Z' && c >= 'A') {
00153 value = c - 'A' + 10;
00154 }
00155 else if (c <= 'z' && c >= 'a') {
00156 value = c - 'a' + 10;
00157 }
00158
00159 if (value >= base) {
00160 value = -1;
00161 }
00162
00163 return value;
00164 }
00165 #endif