don't strip newlines or

chars under ascii value 32 or over ascii value 126
This commit is contained in:
steph 2021-08-23 20:37:17 -04:00 committed by Accelerator
parent efcc487032
commit 1e8f6cdc3e
5 changed files with 133 additions and 68 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
build/
build/*
obj-linux-*/*

0
configure.py Normal file → Executable file
View File

View File

@ -12,93 +12,144 @@ char ** g_szStrings;
int g_iStrings = 0; int g_iStrings = 0;
#if SOURCE_ENGINE >= SE_LEFT4DEAD2 #if SOURCE_ENGINE >= SE_LEFT4DEAD2
DETOUR_DECL_MEMBER4(Detour_LogDirect, LoggingResponse_t, LoggingChannelID_t, channelID, LoggingSeverity_t, severity, Color, color, const tchar *, pMessage) DETOUR_DECL_MEMBER4(Detour_LogDirect, LoggingResponse_t, LoggingChannelID_t, channelID, LoggingSeverity_t, severity, Color, color, const tchar *, pMessage)
{ {
for(int i=0;i<g_iStrings;++i) for (int i = 0; i < g_iStrings; ++i)
if(strstr(pMessage, g_szStrings[i])!=0) {
// make sure we're stripping at least 2 or more chars just in case we accidentally inhale a \0
if (strlen(g_szStrings[i]) > 1 && strstr(pMessage, g_szStrings[i]) != 0)
{
return LR_CONTINUE; return LR_CONTINUE;
}
}
return DETOUR_MEMBER_CALL(Detour_LogDirect)(channelID, severity, color, pMessage); return DETOUR_MEMBER_CALL(Detour_LogDirect)(channelID, severity, color, pMessage);
} }
#else #else
DETOUR_DECL_STATIC2(Detour_DefSpew, SpewRetval_t, SpewType_t, channel, char *, text) DETOUR_DECL_STATIC2(Detour_DefSpew, SpewRetval_t, SpewType_t, channel, char *, text)
{ {
for(int i=0;i<g_iStrings;++i) for (int i = 0; i < g_iStrings; ++i)
if(strstr(text, g_szStrings[i])!=0) {
// make sure we're stripping at least 2 or more chars just in case we accidentally inhale a \0
if (strlen(g_szStrings[i]) > 1 && strstr(text, g_szStrings[i]) != 0)
{
return SPEW_CONTINUE; return SPEW_CONTINUE;
}
}
return DETOUR_STATIC_CALL(Detour_DefSpew)(channel, text); return DETOUR_STATIC_CALL(Detour_DefSpew)(channel, text);
} }
#endif #endif
// https://stackoverflow.com/questions/10178700/c-strip-non-ascii-characters-from-string
static bool badChar(char c)
{
// everything below space excluding null term and del or above
return (c != 0 && (c < 32 || c > 126));
}
static void stripBadChars(std::string & str)
{
// remove all chars matching our "badchar" func
str.erase(remove_if(str.begin(),str.end(), badChar), str.end());
}
bool Cleaner::SDK_OnLoad(char *error, size_t maxlength, bool late) bool Cleaner::SDK_OnLoad(char *error, size_t maxlength, bool late)
{ {
CDetourManager::Init(g_pSM->GetScriptingEngine(), 0); CDetourManager::Init(g_pSM->GetScriptingEngine(), 0);
char szPath[256]; char szPath[256];
g_pSM->BuildPath(Path_SM, szPath, sizeof(szPath), "configs/cleaner.cfg"); g_pSM->BuildPath(Path_SM, szPath, sizeof(szPath), "configs/cleaner.cfg");
FILE * file = fopen(szPath, "r"); FILE * file = fopen(szPath, "r");
if(file==NULL) if (file == NULL)
{ {
snprintf(error, maxlength, "Could not read configs/cleaner.cfg."); rootconsole->ConsolePrint("[CLEANER] Could not read configs/cleaner.cfg.");
return false; return false;
} }
// step thru the file char by char and log the number of newlines we have
// this is more or less the number of lines we have
int c, lines = 0; int c, lines = 0;
do do
{ {
c = fgetc(file); c = fgetc(file);
if (c == '\n') ++lines; if (c == '\n')
{
++lines;
}
} while (c != EOF); } while (c != EOF);
rootconsole->ConsolePrint("[CLEANER] %i lines", lines);
rewind(file); rewind(file);
int len;
g_szStrings = new char*[lines]; g_szStrings = new char*[lines];
while(!feof(file))
while (!feof(file))
{ {
g_szStrings[g_iStrings] = new char[256]; g_szStrings[g_iStrings] = new char[128];
if (fgets(g_szStrings[g_iStrings], 255, file) != NULL) // fgets stops at n - 1 aka 255
if (fgets(g_szStrings[g_iStrings], 128, file) != NULL)
{ {
len = strlen(g_szStrings[g_iStrings]); // make things a little easier on ourselves
if(g_szStrings[g_iStrings][len-1]=='\r' || g_szStrings[g_iStrings][len-1]=='\n') std::string thisstring = g_szStrings[g_iStrings];
g_szStrings[g_iStrings][len-1]=0;
if(g_szStrings[g_iStrings][len-2]=='\r') // significantly more robust way of stripping evil chars from our string so we don't crash
g_szStrings[g_iStrings][len-2]=0; stripBadChars( thisstring );
// copy our std::string back to char*
char* c_thisstring = &thisstring[0];
int len = strlen(c_thisstring);
// don't strip 0 len strings
if (len <= 0)
{
//rootconsole->ConsolePrint("[CLEANER] Refusing to strip a string with 0 or less length - line %i", g_iStrings );
}
else
{
rootconsole->ConsolePrint("[CLEANER] Stripping string on line %i: \"%s\" - length = %i", g_iStrings+1, c_thisstring, strlen(c_thisstring));
}
strcpy(g_szStrings[g_iStrings], c_thisstring);
++g_iStrings; ++g_iStrings;
} }
} }
fclose(file); fclose(file);
#if SOURCE_ENGINE >= SE_LEFT4DEAD2
#ifdef PLATFORM_WINDOWS // init our detours
#if SOURCE_ENGINE >= SE_LEFT4DEAD2
#ifdef PLATFORM_WINDOWS
HMODULE tier0 = GetModuleHandle(TIER0_NAME); HMODULE tier0 = GetModuleHandle(TIER0_NAME);
void * fn = memutils->FindPattern(tier0, SIG_WINDOWS, SIG_WIN_SIZE); void * fn = memutils->FindPattern(tier0, SIG_WINDOWS, SIG_WIN_SIZE);
#elif defined PLATFORM_LINUX #elif defined PLATFORM_LINUX
void * tier0 = dlopen(TIER0_NAME, RTLD_NOW); void * tier0 = dlopen(TIER0_NAME, RTLD_NOW);
void * fn = memutils->ResolveSymbol(tier0, SIG_LINUX); void * fn = memutils->ResolveSymbol(tier0, SIG_LINUX);
dlclose(tier0); dlclose(tier0);
#else #else
#error "Unsupported OS" #error "Unsupported OS"
#endif #endif
if(!fn) if (!fn)
{ {
snprintf(error, maxlength, "Failed to find signature. Please contact the author."); rootconsole->ConsolePrint("[CLEANER] Failed to find signature. Please contact the author.");
return false; return false;
} }
#if defined SIG_LINUX_OFFSET #if defined SIG_LINUX_OFFSET
#ifdef PLATFORM_LINUX #ifdef PLATFORM_LINUX
fn = (void *)((intptr_t)fn + SIG_LINUX_OFFSET); fn = (void *)((intptr_t)fn + SIG_LINUX_OFFSET);
#endif #endif
#endif #endif
g_pDetour = DETOUR_CREATE_MEMBER(Detour_LogDirect, fn); g_pDetour = DETOUR_CREATE_MEMBER(Detour_LogDirect, fn);
#else #else
g_pDetour = DETOUR_CREATE_STATIC(Detour_DefSpew, (gpointer)GetSpewOutputFunc()); g_pDetour = DETOUR_CREATE_STATIC(Detour_DefSpew, (gpointer)GetSpewOutputFunc());
#endif #endif
if (g_pDetour == NULL) if (g_pDetour == NULL)
{ {
snprintf(error, maxlength, "Failed to initialize the detours. Please contact the author."); rootconsole->ConsolePrint("[CLEANER] Failed to initialize the detours. Please contact the author.");
return false; return false;
} }
@ -115,8 +166,10 @@ void Cleaner::SDK_OnUnload()
g_pDetour = NULL; g_pDetour = NULL;
} }
for(int i = 0; i < g_iStrings; ++i) for (int i = 0; i < g_iStrings; ++i)
{
delete [] g_szStrings[i]; delete [] g_szStrings[i];
}
delete [] g_szStrings; delete [] g_szStrings;
} }

View File

@ -41,6 +41,14 @@
#include "CDetour/detours.h" #include "CDetour/detours.h"
#include <tier0/dbg.h> #include <tier0/dbg.h>
// for string manipulation
#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
//HalfLife2.h //HalfLife2.h
#if defined _WIN32 #if defined _WIN32
#define SOURCE_BIN_PREFIX "" #define SOURCE_BIN_PREFIX ""

View File

@ -40,7 +40,7 @@
/* Basic information exposed publicly */ /* Basic information exposed publicly */
#define SMEXT_CONF_NAME "Console Cleaner" #define SMEXT_CONF_NAME "Console Cleaner"
#define SMEXT_CONF_DESCRIPTION "Console warning suppressor" #define SMEXT_CONF_DESCRIPTION "Console warning suppressor"
#define SMEXT_CONF_VERSION "1.2.0" #define SMEXT_CONF_VERSION "1.2.1"
#define SMEXT_CONF_AUTHOR "Accelerator, Zephyrus" #define SMEXT_CONF_AUTHOR "Accelerator, Zephyrus"
#define SMEXT_CONF_URL "https://github.com/Accelerator74/Cleaner" #define SMEXT_CONF_URL "https://github.com/Accelerator74/Cleaner"
#define SMEXT_CONF_LOGTAG "Cleaner" #define SMEXT_CONF_LOGTAG "Cleaner"
@ -77,5 +77,6 @@
//#define SMEXT_ENABLE_USERMSGS //#define SMEXT_ENABLE_USERMSGS
//#define SMEXT_ENABLE_TRANSLATOR //#define SMEXT_ENABLE_TRANSLATOR
//#define SMEXT_ENABLE_NINVOKE //#define SMEXT_ENABLE_NINVOKE
#define SMEXT_ENABLE_ROOTCONSOLEMENU
#endif // _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_ #endif // _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_