diff --git a/configure.py b/configure.py index a4000889..7048d299 100644 --- a/configure.py +++ b/configure.py @@ -1965,7 +1965,7 @@ config.libs = [ Object(Matching, "MSL_Common/string.c"), Object(Matching, "MSL_Common/float.c"), Object(NonMatching, "MSL_Common/strtold.c"), - Object(NonMatching, "MSL_Common/strtoul.c"), + Object(Matching, "MSL_Common/strtoul.c", extra_cflags=["-Cpp_exceptions on"]), Object(NonMatching, "MSL_Common/time.c"), Object(Matching, "MSL_Common/wctype.c"), Object(Matching, "MSL_Common/wmem.c"), diff --git a/libs/MSL/src/MSL_Common/strtoul.c b/libs/MSL/src/MSL_Common/strtoul.c new file mode 100644 index 00000000..d9cb5d0a --- /dev/null +++ b/libs/MSL/src/MSL_Common/strtoul.c @@ -0,0 +1,382 @@ +#include +#include +enum __ReadProcActions { + __GetAChar, + __UngetAChar, + __TestForError +}; + +typedef struct { + char* spNextChar; + int nullCharDetected; +} __InStrCtrl; + +int __StringRead(void* ptr, int ch, int act); +#include +#include + +enum scan_states { + start = 0x01, + check_for_zero = 0x02, + leading_zero = 0x04, + need_digit = 0x08, + digit_loop = 0x10, + finished = 0x20, + failure = 0x40 +}; + +#define final_state(scan_state) (scan_state & (finished | failure)) +#define success(scan_state) (scan_state & (leading_zero | digit_loop | finished)) +#define fetch() (count++, (*ReadProc)(ReadProcArg, 0, __GetAChar)) +#define unfetch(c) (*ReadProc)(ReadProcArg, c, __UngetAChar) + +unsigned long __strtoul(int base, int max_width, int (*ReadProc)(void*, int, int), void* ReadProcArg, int* chars_scanned, int* negative, + int* overflow) { + int scan_state = start; + int count = 0; + int spaces = 0; + unsigned long value = 0; + unsigned long value_max = 0; + int c; + + *negative = *overflow = 0; + + if (base < 0 || base == 1 || base > 36 || max_width < 1) { + scan_state = failure; + } else { + c = fetch(); + } + + if (base != 0) { + value_max = ULONG_MAX / base; + } + + while (count <= max_width && c != -1 && !final_state(scan_state)) { + switch (scan_state) { + case start: { + if (isspace(c)) { + c = fetch(); + count--; + spaces++; + break; + } + + if (c == '+') { + c = fetch(); + } else if (c == '-') { + c = fetch(); + *negative = 1; + } + + scan_state = check_for_zero; + break; + } + + case check_for_zero: { + if (base == 0 || base == 16) { + if (c == '0') { + scan_state = leading_zero; + c = fetch(); + break; + } + } + + scan_state = need_digit; + break; + } + + case leading_zero: { + if (c == 'X' || c == 'x') { + base = 16; + scan_state = need_digit; + c = fetch(); + break; + } + + if (base == 0) { + base = 8; + } + + scan_state = digit_loop; + break; + } + + case need_digit: + case digit_loop: { + if (base == 0) { + base = 10; + } + + if (!value_max) { + value_max = ULONG_MAX / base; + } + + if (isdigit(c)) { + if ((c -= '0') >= base) { + if (scan_state == digit_loop) { + scan_state = finished; + } else { + scan_state = failure; + } + + c += '0'; + break; + } + } else if (!isalpha(c) || (toupper(c) - 'A' + 10) >= base) { + if (scan_state == digit_loop) { + scan_state = finished; + } else { + scan_state = failure; + } + + break; + } else { + c = toupper(c) - 'A' + 10; + } + + if (value > value_max) { + *overflow = 1; + } + + value *= base; + + if (c > (ULONG_MAX - value)) { + *overflow = 1; + } + + value += c; + scan_state = digit_loop; + c = fetch(); + break; + } + } + } + + if (!success(scan_state)) { + count = 0; + value = 0; + *chars_scanned = 0; + } else { + count--; + *chars_scanned = count + spaces; + } + + unfetch(c); + return value; +} + +unsigned long long __strtoull(int base, int max_width, int (*ReadProc)(void*, int, int), void* ReadProcArg, int* chars_scanned, int* negative, + int* overflow) { + int scan_state = start; + int count = 0; + int spaces = 0; + unsigned long long value = 0; + unsigned long long value_max = 0; + int c; + + *negative = *overflow = 0; + + if (base < 0 || base == 1 || base > 36 || max_width < 1) { + scan_state = failure; + } else { + c = fetch(); + } + + if (base != 0) { + value_max = ULLONG_MAX / base; + } + + while (count <= max_width && c != -1 && !final_state(scan_state)) { + switch (scan_state) { + case start: { + if (isspace(c)) { + c = fetch(); + count--; + spaces++; + break; + } + + if (c == '+') { + c = fetch(); + } else if (c == '-') { + c = fetch(); + *negative = 1; + } + + scan_state = check_for_zero; + break; + } + + case check_for_zero: { + if (base == 0 || base == 16) { + if (c == '0') { + scan_state = leading_zero; + c = fetch(); + break; + } + } + + scan_state = need_digit; + break; + } + + case leading_zero: { + if (c == 'X' || c == 'x') { + base = 16; + scan_state = need_digit; + c = fetch(); + break; + } + + if (base == 0) { + base = 8; + } + + scan_state = digit_loop; + break; + } + + case need_digit: + case digit_loop: { + if (base == 0) { + base = 10; + } + + if (!value_max) { + value_max = ULLONG_MAX / base; + } + + if (isdigit(c)) { + if ((c -= '0') >= base) { + if (scan_state == digit_loop) { + scan_state = finished; + } else { + scan_state = failure; + } + + c += '0'; + break; + } + } else if (!isalpha(c) || (toupper(c) - 'A' + 10) >= base) { + if (scan_state == digit_loop) { + scan_state = finished; + } else { + scan_state = failure; + } + + break; + } else { + c = toupper(c) - 'A' + 10; + } + + if (value > value_max) { + *overflow = 1; + } + + value *= base; + + if (c > (ULLONG_MAX - value)) { + *overflow = 1; + } + + value += c; + scan_state = digit_loop; + c = fetch(); + break; + } + } + } + + if (!success(scan_state)) { + count = 0; + value = 0; + *chars_scanned = 0; + } else { + count--; + *chars_scanned = count + spaces; + } + + unfetch(c); + return value; +} + +unsigned long strtoul(const char* str, char** end, int base) { + unsigned long value; + int count, negative, overflow; + __InStrCtrl isc; + + isc.spNextChar = (char*)str; + isc.nullCharDetected = 0; + + value = __strtoul(base, INT_MAX, &__StringRead, &isc, &count, &negative, &overflow); + + if (end) { + *end = (char*)str + count; + } + + if (overflow) { + value = ULONG_MAX; + errno = ERANGE; + } else if (negative) { + value = -value; + } + + return value; +} + +unsigned long long strtoull(const char* str, char** end, int base) { + unsigned long long value; + int count, negative, overflow; + __InStrCtrl isc; + + isc.spNextChar = (char*)str; + isc.nullCharDetected = 0; + + value = __strtoull(base, INT_MAX, &__StringRead, &isc, &count, &negative, &overflow); + + if (end) { + *end = (char*)str + count; + } + + if (overflow) { + value = ULLONG_MAX; + errno = ERANGE; + } else if (negative) { + value = -value; + } + + return value; +} + +long strtol(const char* str, char** end, int base) { + unsigned long uvalue; + long svalue; + int count, negative, overflow; + __InStrCtrl isc; + + isc.spNextChar = (char*)str; + isc.nullCharDetected = 0; + + uvalue = __strtoul(base, INT_MAX, &__StringRead, &isc, &count, &negative, &overflow); + + if (end) { + *end = (char*)str + count; + } + + if (overflow || (!negative && uvalue > LONG_MAX) || (negative && uvalue > -(unsigned long)LONG_MIN)) { + svalue = (negative ? -(unsigned long)LONG_MIN : LONG_MAX); + errno = ERANGE; + } else { + svalue = (negative ? -(long)uvalue : (long)uvalue); + } + + return svalue; +} + +int atoi(const char* str) { + return (int)strtol(str, NULL, 10); +} + +long atol(const char* str) { + return strtol(str, NULL, 10); +}