sl@0: /* sl@0: * strtoul.c -- sl@0: * sl@0: * Source code for the "strtoul" library procedure. sl@0: * sl@0: * Copyright (c) 1988 The Regents of the University of California. sl@0: * Copyright (c) 1994 Sun Microsystems, Inc. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: strtoul.c,v 1.5 2002/02/25 10:36:32 dkf Exp $ sl@0: */ sl@0: sl@0: #include "tclInt.h" sl@0: #include "tclPort.h" sl@0: sl@0: /* sl@0: * The table below is used to convert from ASCII digits to a sl@0: * numerical equivalent. It maps from '0' through 'z' to integers sl@0: * (100 for non-digit characters). sl@0: */ sl@0: sl@0: static char cvtIn[] = { sl@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* '0' - '9' */ sl@0: 100, 100, 100, 100, 100, 100, 100, /* punctuation */ sl@0: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'A' - 'Z' */ sl@0: 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, sl@0: 30, 31, 32, 33, 34, 35, sl@0: 100, 100, 100, 100, 100, 100, /* punctuation */ sl@0: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'a' - 'z' */ sl@0: 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, sl@0: 30, 31, 32, 33, 34, 35}; sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * strtoul -- sl@0: * sl@0: * Convert an ASCII string into an integer. sl@0: * sl@0: * Results: sl@0: * The return value is the integer equivalent of string. If endPtr sl@0: * is non-NULL, then *endPtr is filled in with the character sl@0: * after the last one that was part of the integer. If string sl@0: * doesn't contain a valid integer value, then zero is returned sl@0: * and *endPtr is set to string. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: unsigned long int sl@0: strtoul(string, endPtr, base) sl@0: CONST char *string; /* String of ASCII digits, possibly sl@0: * preceded by white space. For bases sl@0: * greater than 10, either lower- or sl@0: * upper-case digits may be used. sl@0: */ sl@0: char **endPtr; /* Where to store address of terminating sl@0: * character, or NULL. */ sl@0: int base; /* Base for conversion. Must be less sl@0: * than 37. If 0, then the base is chosen sl@0: * from the leading characters of string: sl@0: * "0x" means hex, "0" means octal, anything sl@0: * else means decimal. sl@0: */ sl@0: { sl@0: register CONST char *p; sl@0: register unsigned long int result = 0; sl@0: register unsigned digit; sl@0: int anyDigits = 0; sl@0: int negative=0; sl@0: int overflow=0; sl@0: sl@0: /* sl@0: * Skip any leading blanks. sl@0: */ sl@0: sl@0: p = string; sl@0: while (isspace(UCHAR(*p))) { sl@0: p += 1; sl@0: } sl@0: if (*p == '-') { sl@0: negative = 1; sl@0: p += 1; sl@0: } else { sl@0: if (*p == '+') { sl@0: p += 1; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * If no base was provided, pick one from the leading characters sl@0: * of the string. sl@0: */ sl@0: sl@0: if (base == 0) sl@0: { sl@0: if (*p == '0') { sl@0: p += 1; sl@0: if ((*p == 'x') || (*p == 'X')) { sl@0: p += 1; sl@0: base = 16; sl@0: } else { sl@0: sl@0: /* sl@0: * Must set anyDigits here, otherwise "0" produces a sl@0: * "no digits" error. sl@0: */ sl@0: sl@0: anyDigits = 1; sl@0: base = 8; sl@0: } sl@0: } sl@0: else base = 10; sl@0: } else if (base == 16) { sl@0: sl@0: /* sl@0: * Skip a leading "0x" from hex numbers. sl@0: */ sl@0: sl@0: if ((p[0] == '0') && ((p[1] == 'x') || (p[1] == 'X'))) { sl@0: p += 2; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Sorry this code is so messy, but speed seems important. Do sl@0: * different things for base 8, 10, 16, and other. sl@0: */ sl@0: sl@0: if (base == 8) { sl@0: unsigned long maxres = ULONG_MAX >> 3; sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > 7) { sl@0: break; sl@0: } sl@0: if (result > maxres) { overflow = 1; } sl@0: result = (result << 3); sl@0: if (digit > (ULONG_MAX - result)) { overflow = 1; } sl@0: result += digit; sl@0: anyDigits = 1; sl@0: } sl@0: } else if (base == 10) { sl@0: unsigned long maxres = ULONG_MAX / 10; sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > 9) { sl@0: break; sl@0: } sl@0: if (result > maxres) { overflow = 1; } sl@0: result *= 10; sl@0: if (digit > (ULONG_MAX - result)) { overflow = 1; } sl@0: result += digit; sl@0: anyDigits = 1; sl@0: } sl@0: } else if (base == 16) { sl@0: unsigned long maxres = ULONG_MAX >> 4; sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > ('z' - '0')) { sl@0: break; sl@0: } sl@0: digit = cvtIn[digit]; sl@0: if (digit > 15) { sl@0: break; sl@0: } sl@0: if (result > maxres) { overflow = 1; } sl@0: result = (result << 4); sl@0: if (digit > (ULONG_MAX - result)) { overflow = 1; } sl@0: result += digit; sl@0: anyDigits = 1; sl@0: } sl@0: } else if ( base >= 2 && base <= 36 ) { sl@0: unsigned long maxres = ULONG_MAX / base; sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > ('z' - '0')) { sl@0: break; sl@0: } sl@0: digit = cvtIn[digit]; sl@0: if (digit >= ( (unsigned) base )) { sl@0: break; sl@0: } sl@0: if (result > maxres) { overflow = 1; } sl@0: result *= base; sl@0: if (digit > (ULONG_MAX - result)) { overflow = 1; } sl@0: result += digit; sl@0: anyDigits = 1; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * See if there were any digits at all. sl@0: */ sl@0: sl@0: if (!anyDigits) { sl@0: p = string; sl@0: } sl@0: sl@0: if (endPtr != 0) { sl@0: /* unsafe, but required by the strtoul prototype */ sl@0: *endPtr = (char *) p; sl@0: } sl@0: sl@0: if (overflow) { sl@0: errno = ERANGE; sl@0: return ULONG_MAX; sl@0: } sl@0: if (negative) { sl@0: return -result; sl@0: } sl@0: return result; sl@0: }