Back to index

php5  5.3.10
strtoi64.c
Go to the documentation of this file.
00001 #if _MSC_VERS <= 1300
00002 #include "php_strtoi64.h"
00003 /*
00004        From APR, apr_strings.c
00005        See http://www.apache.org/licenses/LICENSE-2.0
00006 */
00007 
00008 PHPAPI int64_t _strtoi64(const char *nptr, char **endptr, int base)
00009 {
00010        const char *s;
00011        int64_t acc;
00012        int64_t val;
00013        int neg, any;
00014        char c;
00015 
00016        /*
00017         * Skip white space and pick up leading +/- sign if any.
00018         * If base is 0, allow 0x for hex and 0 for octal, else
00019         * assume decimal; if base is already 16, allow 0x.
00020         */
00021        s = nptr;
00022        do {
00023               c = *s++;
00024        } while (isspace((unsigned char)c));
00025        if (c == '-') {
00026               neg = 1;
00027               c = *s++;
00028        } else {
00029               neg = 0;
00030               if (c == '+') {
00031                      c = *s++;
00032               }
00033        }
00034 
00035        if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) {
00036               c = s[1];
00037               s += 2;
00038               base = 16;
00039        }
00040        if (base == 0) {
00041               base = c == '0' ? 8 : 10;
00042        }
00043        acc = any = 0;
00044        if (base < 2 || base > 36) {
00045               errno = EINVAL;
00046               if (endptr != NULL) {
00047                      *endptr = (char *)(any ? s - 1 : nptr);
00048               }
00049               return acc;
00050        }
00051 
00052        /* The classic bsd implementation requires div/mod operators
00053         * to compute a cutoff.  Benchmarking proves that iss very, very
00054         * evil to some 32 bit processors.  Instead, look for underflow
00055         * in both the mult and add/sub operation.  Unlike the bsd impl,
00056         * we also work strictly in a signed int64 word as we haven't
00057         * implemented the unsigned type in win32.
00058         *
00059         * Set 'any' if any `digits' consumed; make it negative to indicate
00060         * overflow.
00061         */
00062     val = 0;
00063        for ( ; ; c = *s++) {
00064               if (c >= '0' && c <= '9')
00065                      c -= '0';
00066 
00067 #if (('Z' - 'A') == 25)
00068               else if (c >= 'A' && c <= 'Z')
00069                      c -= 'A' - 10;
00070               else if (c >= 'a' && c <= 'z')
00071                      c -= 'a' - 10;
00072 #elif APR_CHARSET_EBCDIC
00073               else if (c >= 'A' && c <= 'I')
00074                      c -= 'A' - 10;
00075               else if (c >= 'J' && c <= 'R')
00076                      c -= 'J' - 19;
00077               else if (c >= 'S' && c <= 'Z')
00078                      c -= 'S' - 28;
00079               else if (c >= 'a' && c <= 'i')
00080                      c -= 'a' - 10;
00081               else if (c >= 'j' && c <= 'r')
00082                      c -= 'j' - 19;
00083               else if (c >= 's' && c <= 'z')
00084                      c -= 'z' - 28;
00085 #else
00086 # error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported"
00087 #endif
00088               else {
00089                      break;
00090               }
00091 
00092               if (c >= base) {
00093                      break;
00094               }
00095 
00096               val *= base;
00097               if ( (any < 0)       /* already noted an over/under flow - short circuit */
00098                             || (neg && (val > acc || (val -= c) > acc)) /* underflow */
00099                             || (val < acc || (val += c) < acc)) {       /* overflow */
00100                      any = -1;     /* once noted, over/underflows never go away */
00101 #ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
00102                      break;
00103 #endif
00104               } else {
00105                      acc = val;
00106                      any = 1;
00107               }
00108        }
00109 
00110        if (any < 0) {
00111               acc = neg ? INT64_MIN : INT64_MAX;
00112               errno = ERANGE;
00113        } else if (!any) {
00114               errno = EINVAL;
00115        }
00116 
00117        if (endptr != NULL) {
00118               *endptr = (char *)(any ? s - 1 : nptr);
00119        }
00120        return (acc);
00121 }
00122 #endif