From ec9afe9c339d27ee6417c1d52a2e269ee7c067a4 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 Apr 2015 16:31:26 -0400 Subject: [PATCH 1/6] moved cheat code hex value verification to a central function --- Source/Project64/N64 System/Cheat Class.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Source/Project64/N64 System/Cheat Class.cpp b/Source/Project64/N64 System/Cheat Class.cpp index 8432f5573..efa97355a 100644 --- a/Source/Project64/N64 System/Cheat Class.cpp +++ b/Source/Project64/N64 System/Cheat Class.cpp @@ -15,6 +15,8 @@ enum { WM_EDITCHEAT = WM_USER + 0x120 }; enum { UM_CHANGECODEEXTENSION = WM_USER + 0x121 }; +extern int is_valid_hex_digit(char symbol); + CCheats::CCheats (const CN64Rom * Rom ) : m_Rom(Rom), m_rcList(new RECT), @@ -1845,7 +1847,7 @@ stdstr CCheats::ReadOptionsString(HWND hDlg, bool &/*validcodes*/, bool &validop case 1: //option = lower byte if (len >= 2) { for (i=0; i<2; i++) { - if (!(((str[i] >= 'a') && (str[i] <= 'f')) || ((str[i] >= 'A') && (str[i] <= 'F')) || ((str[i] >= '0') && (str[i] <= '9')))) { + if (!is_valid_hex_digit(str[i])) { validoptions = false; break; } @@ -1878,7 +1880,7 @@ stdstr CCheats::ReadOptionsString(HWND hDlg, bool &/*validcodes*/, bool &validop case 2: //option = word if (len >= 4) { for (i=0; i<4; i++) { - if (!(((str[i] >= 'a') && (str[i] <= 'f')) || ((str[i] >= 'A') && (str[i] <= 'F')) || ((str[i] >= '0') && (str[i] <= '9')))) { + if (!is_valid_hex_digit(str[i])) { validoptions = false; break; } @@ -1913,5 +1915,13 @@ stdstr CCheats::ReadOptionsString(HWND hDlg, bool &/*validcodes*/, bool &validop return optionsstring; } - - +int is_valid_hex_digit(char symbol) +{ + if ( + ((symbol >= 'a') && (symbol <= 'f')) + || ((symbol >= 'A') && (symbol <= 'F')) + || ((symbol >= '0') && (symbol <= '9')) + ) + return 1; + return 0; +} From 3ef067f6d11a67a440bdf15b47191b87b9e1daef Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 Apr 2015 16:52:48 -0400 Subject: [PATCH 2/6] optimized hex digit verification a little --- Source/Project64/N64 System/Cheat Class.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Source/Project64/N64 System/Cheat Class.cpp b/Source/Project64/N64 System/Cheat Class.cpp index efa97355a..897ca66ce 100644 --- a/Source/Project64/N64 System/Cheat Class.cpp +++ b/Source/Project64/N64 System/Cheat Class.cpp @@ -1917,11 +1917,15 @@ stdstr CCheats::ReadOptionsString(HWND hDlg, bool &/*validcodes*/, bool &validop int is_valid_hex_digit(char symbol) { - if ( - ((symbol >= 'a') && (symbol <= 'f')) - || ((symbol >= 'A') && (symbol <= 'F')) - || ((symbol >= '0') && (symbol <= '9')) - ) + if (symbol < '0') + return 0; /* no valid hex figures before '0' */ + if (symbol <= '9') + return 1; + + symbol &= ~0x20; /* in ASCII, forces lowercase to uppercase */ + if (symbol < 'A') + return 0; + if (symbol <= 'Z') return 1; return 0; } From 12b71f4c0c870e9e94ef1cf92cb3ad0ecafe9f32 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 Apr 2015 17:02:19 -0400 Subject: [PATCH 3/6] actually may be better as a static (possibly inline) function --- Source/Project64/N64 System/Cheat Class.cpp | 30 ++++++++++----------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Source/Project64/N64 System/Cheat Class.cpp b/Source/Project64/N64 System/Cheat Class.cpp index 897ca66ce..56020c34d 100644 --- a/Source/Project64/N64 System/Cheat Class.cpp +++ b/Source/Project64/N64 System/Cheat Class.cpp @@ -15,7 +15,20 @@ enum { WM_EDITCHEAT = WM_USER + 0x120 }; enum { UM_CHANGECODEEXTENSION = WM_USER + 0x121 }; -extern int is_valid_hex_digit(char symbol); +static int is_valid_hex_digit(char symbol) +{ + if (symbol < '0') + return 0; /* no valid hex figures before '0' */ + if (symbol <= '9') + return 1; + + symbol &= ~0x20; /* in ASCII, forces lowercase to uppercase */ + if (symbol < 'A') + return 0; + if (symbol <= 'Z') + return 1; + return 0; +} CCheats::CCheats (const CN64Rom * Rom ) : m_Rom(Rom), @@ -1914,18 +1927,3 @@ stdstr CCheats::ReadOptionsString(HWND hDlg, bool &/*validcodes*/, bool &validop if (numoptions < 1) validoptions = false; return optionsstring; } - -int is_valid_hex_digit(char symbol) -{ - if (symbol < '0') - return 0; /* no valid hex figures before '0' */ - if (symbol <= '9') - return 1; - - symbol &= ~0x20; /* in ASCII, forces lowercase to uppercase */ - if (symbol < 'A') - return 0; - if (symbol <= 'Z') - return 1; - return 0; -} From ec458785ea5711c96e5bee10c2903e2f862cd8ec Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 Apr 2015 17:05:06 -0400 Subject: [PATCH 4/6] derp. --- Source/Project64/N64 System/Cheat Class.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Project64/N64 System/Cheat Class.cpp b/Source/Project64/N64 System/Cheat Class.cpp index 56020c34d..adf90d515 100644 --- a/Source/Project64/N64 System/Cheat Class.cpp +++ b/Source/Project64/N64 System/Cheat Class.cpp @@ -25,7 +25,7 @@ static int is_valid_hex_digit(char symbol) symbol &= ~0x20; /* in ASCII, forces lowercase to uppercase */ if (symbol < 'A') return 0; - if (symbol <= 'Z') + if (symbol <= 'F') return 1; return 0; } From 0e05a71f2a080e3b241a3775bc0df26ffeff3ef7 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 Apr 2015 17:07:42 -0400 Subject: [PATCH 5/6] guessing it's okay to have case-insensitive hex verify here too --- Source/Project64/N64 System/Cheat Class.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Project64/N64 System/Cheat Class.cpp b/Source/Project64/N64 System/Cheat Class.cpp index adf90d515..d5f6e27be 100644 --- a/Source/Project64/N64 System/Cheat Class.cpp +++ b/Source/Project64/N64 System/Cheat Class.cpp @@ -1786,7 +1786,7 @@ stdstr CCheats::ReadCodeString (HWND hDlg, bool &validcodes, bool &validoptions, if (len <= 0) { continue; } for (i=0; i<128; i++) { - if (((str[i] >= 'A') && (str[i] <= 'F')) || ((str[i] >= '0') && (str[i] <= '9'))) { // Is hexvalue + if (is_valid_hex_digit(str[i])) { tempformat[i] = 'X'; } if ((str[i] == ' ') || (str[i] == '?')) { From 8df2172495d78669efddff936044baa243b4d250 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 Apr 2015 17:10:15 -0400 Subject: [PATCH 6/6] probably more readable, portable to just say this --- Source/Project64/N64 System/Cheat Class.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Project64/N64 System/Cheat Class.cpp b/Source/Project64/N64 System/Cheat Class.cpp index d5f6e27be..020e7e784 100644 --- a/Source/Project64/N64 System/Cheat Class.cpp +++ b/Source/Project64/N64 System/Cheat Class.cpp @@ -22,7 +22,7 @@ static int is_valid_hex_digit(char symbol) if (symbol <= '9') return 1; - symbol &= ~0x20; /* in ASCII, forces lowercase to uppercase */ + symbol &= ~('X' ^ 'x'); /* in ASCII, forces lowercase to uppercase */ if (symbol < 'A') return 0; if (symbol <= 'F')