From a4819ebe65fd01016b3511abbf10fb50d8f48ae4 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 30 Jan 2021 23:35:56 +1000 Subject: [PATCH] Attempt to decompile strtol --- src/game/gfxmemory.c | 6 +-- src/include/lib/str.h | 2 +- src/lib/main.c | 4 +- src/lib/str.c | 104 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 109 insertions(+), 7 deletions(-) diff --git a/src/game/gfxmemory.c b/src/game/gfxmemory.c index 1b4ede6c1..1b289838f 100644 --- a/src/game/gfxmemory.c +++ b/src/game/gfxmemory.c @@ -54,7 +54,7 @@ void gfxInitMemory(void) s32 gfx; s32 gfxtra = 0; - gfx = func00013408(argFindByPrefix(1, "-mgfx"), NULL, 0) * 1024; + gfx = strtol(argFindByPrefix(1, "-mgfx"), NULL, 0) * 1024; if (argFindByPrefix(1, "-mgfxtra")) { // ******** Extra specified but are we in the correct game mode I wonder???\n @@ -64,7 +64,7 @@ void gfxInitMemory(void) // ******** If you try and run hi-res then\n // ******** you're gonna shafted up the arse\n // ******** so don't blame me\n - gfxtra = func00013408(argFindByPrefix(1, "-mgfxtra"), NULL, 0) * 1024; + gfxtra = strtol(argFindByPrefix(1, "-mgfxtra"), NULL, 0) * 1024; } else { // ******** No we're not so there\n } @@ -78,7 +78,7 @@ void gfxInitMemory(void) if (argFindByPrefix(1, "-mvtx")) { // Argument specified mtxvtx_size\n - g_VtxSizesByPlayerCount[PLAYERCOUNT() - 1] = func00013408(argFindByPrefix(1, "-mvtx"), NULL, 0) * 1024; + g_VtxSizesByPlayerCount[PLAYERCOUNT() - 1] = strtol(argFindByPrefix(1, "-mvtx"), NULL, 0) * 1024; } // %d Players : Allocating %d bytes for master dl's\n diff --git a/src/include/lib/str.h b/src/include/lib/str.h index 8afc3f01a..c0e1b8651 100644 --- a/src/include/lib/str.h +++ b/src/include/lib/str.h @@ -12,6 +12,6 @@ char toupper(char c); s32 isdigit(char c); s32 isalpha(char c); s32 isspace(char c); -s32 func00013408(char *arg0, s32 *arg1, s32 arg2); +s32 strtol(char *arg0, char **arg1, s32 arg2); #endif diff --git a/src/lib/main.c b/src/lib/main.c index b639349d7..98044f294 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -961,7 +961,7 @@ void mainLoop(void) func0f1672f0(4); if (argFindByPrefix(1, "-ma")) { - var8005d9b8 = func00013408(argFindByPrefix(1, "-ma"), NULL, 0) * 1024; + var8005d9b8 = strtol(argFindByPrefix(1, "-ma"), NULL, 0) * 1024; } func00012a14(malloc(var8005d9b8, MEMPOOL_STAGE), var8005d9b8); @@ -972,7 +972,7 @@ void mainLoop(void) numplayers = 0; } else { if (argFindByPrefix(1, "-play")) { - numplayers = func00013408(argFindByPrefix(1, "-play"), NULL, 0); + numplayers = strtol(argFindByPrefix(1, "-play"), NULL, 0); } else { numplayers = 1; } diff --git a/src/lib/str.c b/src/lib/str.c index 528e82431..e77b8545a 100644 --- a/src/lib/str.c +++ b/src/lib/str.c @@ -143,7 +143,7 @@ s32 isspace(char c) } GLOBAL_ASM( -glabel func00013408 +glabel strtol /* 13408: 27bdffa0 */ addiu $sp,$sp,-96 /* 1340c: afb30024 */ sw $s3,0x24($sp) /* 13410: 00c09825 */ or $s3,$a2,$zero @@ -331,3 +331,105 @@ glabel func00013408 /* 13688: 27bd0060 */ addiu $sp,$sp,0x60 /* 1368c: 00000000 */ nop ); + +// Mismatch: The below moves c from s0 to v1 near 590 for the compare with +// cutlim and the add to value, while goal keeps it in s0. Also tried verbatim +// strtol from glibc 1995 (commit 28f540f45b) which has the same codegen. +//s32 strtol(char *src, char **endptr, s32 base) +//{ +// bool negative; +// u32 cutoff; +// u32 cutlim; +// u32 value; +// char *ptr; +// char c; +// char *save; +// bool overflow; +// +// if (base < 0 || base == 1 || base > 36) { +// base = 10; +// } +// +// ptr = src; +// +// while (isspace(*ptr)) { +// ptr++; +// } +// +// if (*ptr != '\0') { +// if (*ptr == '-') { +// negative = true; +// ptr++; +// } else if (*ptr == '+') { +// negative = false; +// ptr++; +// } else { +// negative = false; +// } +// +// if (base == 16 && ptr[0] == '0' && toupper(ptr[1]) == 'X') { +// ptr += 2; +// } +// +// if (base == 0) { +// if (*ptr == '0') { +// if (toupper(ptr[1]) == 'X') { +// ptr += 2; +// base = 16; +// } else { +// base = 8; +// } +// } else { +// base = 10; +// } +// } +// +// save = ptr; +// +// cutoff = U32_MAX / base; +// cutlim = U32_MAX % base; +// overflow = false; +// value = 0; +// +// for (c = *ptr; c != '\0'; c = *++ptr) { +// if (isdigit(c)) { +// c -= '0'; +// } else if (isalpha(c)) { +// c = toupper(c) - 'A' + 10; +// } else { +// break; +// } +// +// // 590 +// if (c >= base) { +// break; +// } +// +// // 5a4 +// if (value > cutoff || (value == cutoff && c > cutlim)) { +// overflow = true; +// } else { +// value *= base; +// value += c; +// } +// } +// +// if (ptr != save) { +// if (endptr != NULL) { +// *endptr = ptr; +// } +// +// if (overflow) { +// return -1; +// } +// +// return negative ? -value : value; +// } +// } +// +// if (endptr != NULL) { +// *endptr = src; +// } +// +// return 0; +//}