mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +01:00
Core/Players: Implemented serverside validation of reserved/profane names
Closes #15357 New library dependency: Boost.Regex
This commit is contained in:
@@ -22,13 +22,14 @@
|
||||
|
||||
DB2FileLoader::DB2FileLoader()
|
||||
{
|
||||
fileName = nullptr;
|
||||
recordSize = 0;
|
||||
recordCount = 0;
|
||||
fieldCount = 0;
|
||||
stringSize = 0;
|
||||
fieldsOffset = NULL;
|
||||
data = NULL;
|
||||
stringTable = NULL;
|
||||
fieldsOffset = nullptr;
|
||||
data = nullptr;
|
||||
stringTable = nullptr;
|
||||
|
||||
tableHash = 0;
|
||||
build = 0;
|
||||
@@ -36,7 +37,7 @@ DB2FileLoader::DB2FileLoader()
|
||||
unk1 = 0;
|
||||
minIndex = 0;
|
||||
maxIndex = 0;
|
||||
locale = 0;
|
||||
localeMask = 0;
|
||||
unk5 = 0;
|
||||
}
|
||||
|
||||
@@ -44,14 +45,15 @@ bool DB2FileLoader::Load(const char *filename, const char *fmt)
|
||||
{
|
||||
if (data)
|
||||
{
|
||||
delete [] data;
|
||||
data = NULL;
|
||||
delete[] data;
|
||||
data = nullptr;
|
||||
}
|
||||
|
||||
FILE* f = fopen(filename, "rb");
|
||||
if (!f)
|
||||
return false;
|
||||
|
||||
fileName = filename;
|
||||
uint32 header;
|
||||
if (fread(&header, 4, 1, f) != 1) // Signature
|
||||
{
|
||||
@@ -140,13 +142,13 @@ bool DB2FileLoader::Load(const char *filename, const char *fmt)
|
||||
|
||||
EndianConvert(maxIndex);
|
||||
|
||||
if (fread(&locale, 4, 1, f) != 1) // Locales
|
||||
if (fread(&localeMask, 4, 1, f) != 1) // Locales
|
||||
{
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
EndianConvert(locale);
|
||||
EndianConvert(localeMask);
|
||||
|
||||
if (fread(&unk5, 4, 1, f) != 1) // Unknown WDB2
|
||||
{
|
||||
@@ -213,6 +215,7 @@ uint32 DB2FileLoader::GetFormatRecordSize(const char * format, int32* index_pos)
|
||||
recordsize += 4;
|
||||
break;
|
||||
case FT_STRING:
|
||||
case FT_STRING_NOT_LOCALIZED:
|
||||
recordsize += sizeof(char*);
|
||||
break;
|
||||
case FT_SORT:
|
||||
@@ -234,7 +237,17 @@ uint32 DB2FileLoader::GetFormatRecordSize(const char * format, int32* index_pos)
|
||||
return recordsize;
|
||||
}
|
||||
|
||||
uint32 DB2FileLoader::GetFormatStringFieldCount(char const* format)
|
||||
uint32 DB2FileLoader::GetFormatStringFieldCount(const char* format)
|
||||
{
|
||||
uint32 stringfields = 0;
|
||||
for (uint32 x = 0; format[x]; ++x)
|
||||
if (format[x] == FT_STRING || format[x] == FT_STRING_NOT_LOCALIZED)
|
||||
++stringfields;
|
||||
|
||||
return stringfields;
|
||||
}
|
||||
|
||||
uint32 DB2FileLoader::GetFormatLocalizedStringFieldCount(char const* format)
|
||||
{
|
||||
uint32 stringfields = 0;
|
||||
for (uint32 x = 0; format[x]; ++x)
|
||||
@@ -305,7 +318,8 @@ char* DB2FileLoader::AutoProduceData(const char* format, uint32& records, char**
|
||||
offset += 1;
|
||||
break;
|
||||
case FT_STRING:
|
||||
*((char**)(&dataTable[offset])) = NULL; // will be replaces non-empty or "" strings in AutoProduceStrings
|
||||
case FT_STRING_NOT_LOCALIZED:
|
||||
*((char**)(&dataTable[offset])) = nullptr; // will be replaces non-empty or "" strings in AutoProduceStrings
|
||||
offset += sizeof(char*);
|
||||
break;
|
||||
}
|
||||
@@ -358,6 +372,7 @@ char* DB2FileLoader::AutoProduceStringsArrayHolders(const char* format, char* da
|
||||
offset += 1;
|
||||
break;
|
||||
case FT_STRING:
|
||||
case FT_STRING_NOT_LOCALIZED:
|
||||
{
|
||||
// init db2 string field slots by pointers to string holders
|
||||
char const*** slot = (char const***)(&dataTable[offset]);
|
||||
@@ -379,7 +394,24 @@ char* DB2FileLoader::AutoProduceStringsArrayHolders(const char* format, char* da
|
||||
char* DB2FileLoader::AutoProduceStrings(const char* format, char* dataTable, uint32 locale)
|
||||
{
|
||||
if (strlen(format) != fieldCount)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
|
||||
if (!(localeMask & (1 << locale)))
|
||||
{
|
||||
char const* sep = "";
|
||||
std::ostringstream str;
|
||||
for (uint32 i = 0; i < TOTAL_LOCALES; ++i)
|
||||
{
|
||||
if (localeMask & (1 << i))
|
||||
{
|
||||
str << sep << localeNames[i];
|
||||
sep = ", ";
|
||||
}
|
||||
}
|
||||
|
||||
TC_LOG_ERROR("", "Attempted to load %s which has locales %s as %s. Check if you placed your localized db2 files in correct directory.", fileName, str.str().c_str(), localeNames[locale]);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
char* stringPool = new char[stringSize];
|
||||
memcpy(stringPool, stringTable, stringSize);
|
||||
@@ -406,13 +438,21 @@ char* DB2FileLoader::AutoProduceStrings(const char* format, char* dataTable, uin
|
||||
LocalizedString* db2str = *(LocalizedString**)(&dataTable[offset]);
|
||||
if (db2str->Str[locale] == nullStr)
|
||||
{
|
||||
const char * st = getRecord(y).getString(x);
|
||||
db2str->Str[locale] = stringPool + (st - (const char*)stringTable);
|
||||
char const* st = getRecord(y).getString(x);
|
||||
db2str->Str[locale] = stringPool + (st - (char const*)stringTable);
|
||||
}
|
||||
|
||||
offset += sizeof(char*);
|
||||
break;
|
||||
}
|
||||
case FT_STRING_NOT_LOCALIZED:
|
||||
{
|
||||
char** db2str = (char**)(&dataTable[offset]);
|
||||
char const* st = getRecord(y).getString(x);
|
||||
*db2str = stringPool + (st - (char const*)stringTable);
|
||||
offset += sizeof(char*);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -524,7 +564,21 @@ char* DB2DatabaseLoader::Load(const char* format, HotfixDatabaseStatements prepa
|
||||
ASSERT(*slot);
|
||||
|
||||
// Value in database in main table field must be for enUS locale
|
||||
if (char* str = AddLocaleString(*slot, LOCALE_enUS, fields[f].GetString()))
|
||||
if (char* str = AddString(&(*slot)->Str[LOCALE_enUS], fields[f].GetStringView()))
|
||||
stringPool.push_back(str);
|
||||
|
||||
++stringFieldNumInRecord;
|
||||
offset += sizeof(char*);
|
||||
break;
|
||||
}
|
||||
case FT_STRING_NOT_LOCALIZED:
|
||||
{
|
||||
char const** slot = (char const**)(&dataValue[offset]);
|
||||
*slot = (char*)(&stringHolders[stringHoldersRecordPoolSize * rec + stringHolderSize * stringFieldNumInRecord]);
|
||||
ASSERT(*slot);
|
||||
|
||||
// Value in database in main table field must be for enUS locale
|
||||
if (char* str = AddString(slot, fields[f].GetStringView()))
|
||||
stringPool.push_back(str);
|
||||
|
||||
++stringFieldNumInRecord;
|
||||
@@ -569,7 +623,7 @@ void DB2DatabaseLoader::LoadStrings(const char* format, HotfixDatabaseStatements
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
size_t stringFields = DB2FileLoader::GetFormatStringFieldCount(format);
|
||||
size_t stringFields = DB2FileLoader::GetFormatLocalizedStringFieldCount(format);
|
||||
if (result->GetFieldCount() != stringFields + 1 /*ID*/)
|
||||
return;
|
||||
|
||||
@@ -605,7 +659,7 @@ void DB2DatabaseLoader::LoadStrings(const char* format, HotfixDatabaseStatements
|
||||
// fill only not filled entries
|
||||
LocalizedString* db2str = *(LocalizedString**)(&dataValue[offset]);
|
||||
if (db2str->Str[locale] == nullStr)
|
||||
if (char* str = AddLocaleString(db2str, locale, fields[1 + stringFieldNumInRecord].GetString()))
|
||||
if (char* str = AddString(&db2str->Str[locale], fields[1 + stringFieldNumInRecord].GetStringView()))
|
||||
stringPool.push_back(str);
|
||||
|
||||
++stringFieldNumInRecord;
|
||||
@@ -625,15 +679,15 @@ void DB2DatabaseLoader::LoadStrings(const char* format, HotfixDatabaseStatements
|
||||
return;
|
||||
}
|
||||
|
||||
char* DB2DatabaseLoader::AddLocaleString(LocalizedString* holder, uint32 locale, std::string const& value)
|
||||
char* DB2DatabaseLoader::AddString(char const** holder, std::string const& value)
|
||||
{
|
||||
if (!value.empty())
|
||||
{
|
||||
std::size_t existingLength = strlen(holder->Str[locale]);
|
||||
std::size_t existingLength = strlen(*holder);
|
||||
if (existingLength >= value.length())
|
||||
{
|
||||
// Reuse existing storage if there is enough space
|
||||
char* str = const_cast<char*>(holder->Str[locale]);
|
||||
char* str = const_cast<char*>(*holder);
|
||||
memcpy(str, value.c_str(), value.length());
|
||||
str[value.length()] = '\0';
|
||||
return nullptr;
|
||||
@@ -642,7 +696,7 @@ char* DB2DatabaseLoader::AddLocaleString(LocalizedString* holder, uint32 locale,
|
||||
char* str = new char[value.length() + 1];
|
||||
memcpy(str, value.c_str(), value.length());
|
||||
str[value.length()] = '\0';
|
||||
holder->Str[locale] = str;
|
||||
*holder = str;
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,9 @@ class DB2FileLoader
|
||||
char* AutoProduceStrings(const char* fmt, char* dataTable, uint32 locale);
|
||||
static uint32 GetFormatRecordSize(const char * format, int32 * index_pos = NULL);
|
||||
static uint32 GetFormatStringFieldCount(const char * format);
|
||||
static uint32 GetFormatLocalizedStringFieldCount(const char * format);
|
||||
private:
|
||||
char const* fileName;
|
||||
|
||||
uint32 recordSize;
|
||||
uint32 recordCount;
|
||||
@@ -102,7 +104,7 @@ private:
|
||||
int unk1; // WDB2 (Unix time in WCH2)
|
||||
int minIndex; // WDB2
|
||||
int maxIndex; // WDB2 (index table)
|
||||
int locale; // WDB2
|
||||
int localeMask; // WDB2
|
||||
int unk5; // WDB2
|
||||
};
|
||||
|
||||
@@ -113,7 +115,7 @@ public:
|
||||
|
||||
char* Load(const char* format, HotfixDatabaseStatements preparedStatement, uint32& records, char**& indexTable, char*& stringHolders, std::list<char*>& stringPool);
|
||||
void LoadStrings(const char* format, HotfixDatabaseStatements preparedStatement, uint32 locale, char**& indexTable, std::list<char*>& stringPool);
|
||||
static char* AddLocaleString(LocalizedString* holder, uint32 locale, std::string const& value);
|
||||
static char* AddString(char const** holder, std::string const& value);
|
||||
|
||||
private:
|
||||
std::string _storageName;
|
||||
|
||||
@@ -105,6 +105,19 @@ public:
|
||||
entry += sizeof(LocalizedString*);
|
||||
break;
|
||||
}
|
||||
case FT_STRING_NOT_LOCALIZED:
|
||||
{
|
||||
char const* str = *(char const**)entry;
|
||||
std::size_t len = strlen(str);
|
||||
buffer << uint16(len ? len + 1 : 0);
|
||||
if (len)
|
||||
{
|
||||
buffer.append(str, len);
|
||||
buffer << uint8(0);
|
||||
}
|
||||
entry += sizeof(char const*);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -157,7 +170,7 @@ public:
|
||||
return false;
|
||||
|
||||
// load strings from another locale db2 data
|
||||
if (DB2FileLoader::GetFormatStringFieldCount(_format))
|
||||
if (DB2FileLoader::GetFormatLocalizedStringFieldCount(_format))
|
||||
if (char* stringBlock = db2.AutoProduceStrings(_format, (char*)_dataTable, locale))
|
||||
_stringPoolList.push_back(stringBlock);
|
||||
return true;
|
||||
@@ -175,7 +188,7 @@ public:
|
||||
|
||||
void LoadStringsFromDB(uint32 locale)
|
||||
{
|
||||
if (!DB2FileLoader::GetFormatStringFieldCount(_format))
|
||||
if (!DB2FileLoader::GetFormatLocalizedStringFieldCount(_format))
|
||||
return;
|
||||
|
||||
DB2DatabaseLoader(_fileName).LoadStrings(_format, HotfixDatabaseStatements(_hotfixStatement + 1), locale, _indexTable.AsChar, _stringPoolList);
|
||||
|
||||
Reference in New Issue
Block a user