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

@ -15,60 +15,111 @@ int g_iStrings = 0;
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)
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 DETOUR_MEMBER_CALL(Detour_LogDirect)(channelID, severity, color, pMessage);
}
#else
DETOUR_DECL_STATIC2(Detour_DefSpew, SpewRetval_t, SpewType_t, channel, char *, text)
{
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 DETOUR_STATIC_CALL(Detour_DefSpew)(channel, text);
}
#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)
{
CDetourManager::Init(g_pSM->GetScriptingEngine(), 0);
char szPath[256];
g_pSM->BuildPath(Path_SM, szPath, sizeof(szPath), "configs/cleaner.cfg");
FILE * file = fopen(szPath, "r");
if (file == NULL)
{
snprintf(error, maxlength, "Could not read configs/cleaner.cfg.");
rootconsole->ConsolePrint("[CLEANER] Could not read configs/cleaner.cfg.");
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;
do
{
c = fgetc(file);
if (c == '\n') ++lines;
if (c == '\n')
{
++lines;
}
} while (c != EOF);
rootconsole->ConsolePrint("[CLEANER] %i lines", lines);
rewind(file);
int len;
g_szStrings = new char*[lines];
while (!feof(file))
{
g_szStrings[g_iStrings] = new char[256];
if (fgets(g_szStrings[g_iStrings], 255, file) != NULL)
g_szStrings[g_iStrings] = new char[128];
// fgets stops at n - 1 aka 255
if (fgets(g_szStrings[g_iStrings], 128, file) != NULL)
{
len = strlen(g_szStrings[g_iStrings]);
if(g_szStrings[g_iStrings][len-1]=='\r' || g_szStrings[g_iStrings][len-1]=='\n')
g_szStrings[g_iStrings][len-1]=0;
if(g_szStrings[g_iStrings][len-2]=='\r')
g_szStrings[g_iStrings][len-2]=0;
// make things a little easier on ourselves
std::string thisstring = g_szStrings[g_iStrings];
// significantly more robust way of stripping evil chars from our string so we don't crash
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;
}
}
fclose(file);
// init our detours
#if SOURCE_ENGINE >= SE_LEFT4DEAD2
#ifdef PLATFORM_WINDOWS
HMODULE tier0 = GetModuleHandle(TIER0_NAME);
@ -83,7 +134,7 @@ bool Cleaner::SDK_OnLoad(char *error, size_t maxlength, bool late)
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;
}
#if defined SIG_LINUX_OFFSET
@ -98,7 +149,7 @@ bool Cleaner::SDK_OnLoad(char *error, size_t maxlength, bool late)
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;
}
@ -116,7 +167,9 @@ void Cleaner::SDK_OnUnload()
}
for (int i = 0; i < g_iStrings; ++i)
{
delete [] g_szStrings[i];
}
delete [] g_szStrings;
}

View File

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

View File

@ -40,7 +40,7 @@
/* Basic information exposed publicly */
#define SMEXT_CONF_NAME "Console Cleaner"
#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_URL "https://github.com/Accelerator74/Cleaner"
#define SMEXT_CONF_LOGTAG "Cleaner"
@ -77,5 +77,6 @@
//#define SMEXT_ENABLE_USERMSGS
//#define SMEXT_ENABLE_TRANSLATOR
//#define SMEXT_ENABLE_NINVOKE
#define SMEXT_ENABLE_ROOTCONSOLEMENU
#endif // _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_