From 0b4f28fecff3889dd1b04c0fbd5048fe5b52a21e Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 8 Jan 2009 20:02:09 +0000 Subject: [PATCH] SQL_QuoteString can now be used without a DB handle (bug 3427, r=sawce). --- dlls/mysqlx/basic_sql.cpp | 66 ++++++++++++++++--------- dlls/mysqlx/mysql/MysqlDriver.cpp | 20 ++++++++ dlls/mysqlx/mysql/MysqlDriver.h | 1 + dlls/sqlite/basic_sql.cpp | 66 ++++++++++++++++--------- dlls/sqlite/sqlitepp/SqliteDatabase.cpp | 1 + dlls/sqlite/sqlitepp/SqliteDriver.cpp | 12 +++++ dlls/sqlite/sqlitepp/SqliteDriver.h | 1 + plugins/include/sqlx.inc | 6 ++- 8 files changed, 127 insertions(+), 46 deletions(-) diff --git a/dlls/mysqlx/basic_sql.cpp b/dlls/mysqlx/basic_sql.cpp index 30aab78b..f99cd790 100644 --- a/dlls/mysqlx/basic_sql.cpp +++ b/dlls/mysqlx/basic_sql.cpp @@ -519,47 +519,69 @@ static cell AMX_NATIVE_CALL SQL_NextResultSet(AMX *amx, cell *params) static cell AMX_NATIVE_CALL SQL_QuoteString(AMX *amx, cell *params) { - IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); - if (!pDb) - { - MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); - return 0; - } - int len; char *str = MF_GetAmxString(amx, params[4], 0, &len); size_t newsize; static char buffer[8192]; - if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + if (params[1] != 0) { - MF_SetAmxString(amx, params[2], buffer, params[3]); - return newsize; + IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); + if (!pDb) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); + return 0; + } + + if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } } else { - return -1; + if (g_Mysql.QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } } } static cell AMX_NATIVE_CALL SQL_QuoteStringFmt(AMX *amx, cell *params) { - IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); - if (!pDb) - { - MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); - return 0; - } - int len; char *str = MF_FormatAmxString(amx, params, 4, &len); size_t newsize; static char buffer[8192]; - if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + if (params[1] != 0) { - MF_SetAmxString(amx, params[2], buffer, params[3]); - return newsize; + IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); + if (!pDb) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); + return 0; + } + + if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } } else { - return -1; + if (g_Mysql.QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } } } diff --git a/dlls/mysqlx/mysql/MysqlDriver.cpp b/dlls/mysqlx/mysql/MysqlDriver.cpp index 76b3c7cf..38b8144a 100644 --- a/dlls/mysqlx/mysql/MysqlDriver.cpp +++ b/dlls/mysqlx/mysql/MysqlDriver.cpp @@ -74,3 +74,23 @@ IDatabase *MysqlDriver::_Connect(DatabaseInfo *info, int *errcode, char *error, return static_cast(pMysql); } + +int MysqlDriver::QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize) +{ + unsigned long size = static_cast(strlen(str)); + unsigned long needed = size*2 + 1; + + if (maxlen < needed) + { + return (int)needed; + } + + needed = mysql_escape_string(buffer, str, size); + if (newsize) + { + *newsize = static_cast(needed); + } + + return 0; +} + diff --git a/dlls/mysqlx/mysql/MysqlDriver.h b/dlls/mysqlx/mysql/MysqlDriver.h index e313a09e..941964cd 100644 --- a/dlls/mysqlx/mysql/MysqlDriver.h +++ b/dlls/mysqlx/mysql/MysqlDriver.h @@ -12,6 +12,7 @@ namespace SourceMod IDatabase *Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength); const char *NameString(); bool IsCompatDriver(const char *namestring); + int QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize); public: IDatabase *_Connect(DatabaseInfo *info, int *errcode, char *error, size_t maxlength, bool do_timeout); }; diff --git a/dlls/sqlite/basic_sql.cpp b/dlls/sqlite/basic_sql.cpp index eff42156..a969572d 100644 --- a/dlls/sqlite/basic_sql.cpp +++ b/dlls/sqlite/basic_sql.cpp @@ -519,47 +519,69 @@ static cell AMX_NATIVE_CALL SQL_NextResultSet(AMX *amx, cell *params) static cell AMX_NATIVE_CALL SQL_QuoteString(AMX *amx, cell *params) { - IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); - if (!pDb) - { - MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); - return 0; - } - int len; char *str = MF_GetAmxString(amx, params[4], 0, &len); size_t newsize; static char buffer[8192]; - if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + if (params[1] != 0) { - MF_SetAmxString(amx, params[2], buffer, params[3]); - return newsize; + IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); + if (!pDb) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); + return 0; + } + + if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } } else { - return -1; + if (g_Sqlite.QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } } } static cell AMX_NATIVE_CALL SQL_QuoteStringFmt(AMX *amx, cell *params) { - IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); - if (!pDb) - { - MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); - return 0; - } - int len; char *str = MF_FormatAmxString(amx, params, 4, &len); size_t newsize; static char buffer[8192]; - if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + if (params[1] != 0) { - MF_SetAmxString(amx, params[2], buffer, params[3]); - return newsize; + IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); + if (!pDb) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); + return 0; + } + + if (pDb->QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } } else { - return -1; + if (g_Sqlite.QuoteString(str, buffer, sizeof(buffer)-1, &newsize) == 0) + { + MF_SetAmxString(amx, params[2], buffer, params[3]); + return newsize; + } else { + return -1; + } } } diff --git a/dlls/sqlite/sqlitepp/SqliteDatabase.cpp b/dlls/sqlite/sqlitepp/SqliteDatabase.cpp index e1886f77..c859eb7e 100644 --- a/dlls/sqlite/sqlitepp/SqliteDatabase.cpp +++ b/dlls/sqlite/sqlitepp/SqliteDatabase.cpp @@ -77,3 +77,4 @@ int SqliteDatabase::QuoteString(const char *str, char buffer[], size_t maxlen, s return 0; } + diff --git a/dlls/sqlite/sqlitepp/SqliteDriver.cpp b/dlls/sqlite/sqlitepp/SqliteDriver.cpp index 0ba5088b..9e977d9a 100644 --- a/dlls/sqlite/sqlitepp/SqliteDriver.cpp +++ b/dlls/sqlite/sqlitepp/SqliteDriver.cpp @@ -63,3 +63,15 @@ IDatabase *SqliteDriver::Connect(DatabaseInfo *info, int *errcode, char *error, } } +int SqliteDriver::QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize) +{ + char *res = sqlite3_snprintf(static_cast(maxlen), buffer, "%q", str); + + if (res != NULL && newsize != NULL) + { + *newsize = strlen(buffer); + } + + return 0; +} + diff --git a/dlls/sqlite/sqlitepp/SqliteDriver.h b/dlls/sqlite/sqlitepp/SqliteDriver.h index b23d06e4..5c2160a7 100644 --- a/dlls/sqlite/sqlitepp/SqliteDriver.h +++ b/dlls/sqlite/sqlitepp/SqliteDriver.h @@ -12,6 +12,7 @@ namespace SourceMod IDatabase *Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength); const char *NameString(); bool IsCompatDriver(const char *namestr); + int QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize); }; }; diff --git a/plugins/include/sqlx.inc b/plugins/include/sqlx.inc index 8b86aa15..0baf82b4 100644 --- a/plugins/include/sqlx.inc +++ b/plugins/include/sqlx.inc @@ -67,7 +67,8 @@ native Handle:SQL_PrepareQuery(Handle:db, const fmt[], any:...); * Note: The buffer's maximum size should be 2*strlen(string) to catch * all scenarios. * - * @param db Database handle, for localization. + * @param db Database handle for localization, or Empty_Handle + * for when a handle is not available. * @param buffer Buffer to copy to. * @param buflen Maximum size of the buffer. * @param string String to backquote (should not overlap buffer). @@ -80,7 +81,8 @@ native SQL_QuoteString(Handle:db, buffer[], buflen, const string[]); * Note: The buffer's maximum size should be 2*strlen(string) to catch * all scenarios. * - * @param db Database handle, for localization. + * @param db Database handle for localization, or Empty_Handle + * for when a handle is not available. * @param buffer Buffer to copy to. * @param buflen Maximum size of the buffer. * @param fmt Format of string to backquote (should not overlap buffer).