From 25462071df58307f41dca5890c33480c7e263572 Mon Sep 17 00:00:00 2001 From: thewavelength Date: Tue, 14 Apr 2020 18:51:39 +0200 Subject: [PATCH] Make GetStringTableData native binary-safe (#1232) Replace StringToLocalUTF8 with LocalToString and memcpy to make this binary compatible and update the documentation. --- extensions/sdktools/vstringtable.cpp | 23 ++++++++++++++++------- plugins/include/sdktools_stringtables.inc | 7 +++---- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/extensions/sdktools/vstringtable.cpp b/extensions/sdktools/vstringtable.cpp index 357b8382..8827bf43 100644 --- a/extensions/sdktools/vstringtable.cpp +++ b/extensions/sdktools/vstringtable.cpp @@ -184,8 +184,7 @@ static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params) INetworkStringTable *pTable = netstringtables->GetTable(idx); int stringidx; const char *userdata; - int datalen; - size_t numBytes; + int datalen = 0; if (!pTable) { @@ -199,12 +198,22 @@ static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params) } userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen); - if (!userdata) - { - userdata = ""; - } - pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes); + char *addr; + pContext->LocalToString(params[3], &addr); + + int maxBytes = params[4]; + size_t numBytes = MIN(maxBytes, datalen); + + if (userdata) + { + memcpy(addr, userdata, numBytes); + } + else if (maxBytes > 0) + { + addr[0] = '\0'; + numBytes = 0; + } return numBytes; } diff --git a/plugins/include/sdktools_stringtables.inc b/plugins/include/sdktools_stringtables.inc index b7505046..85372119 100644 --- a/plugins/include/sdktools_stringtables.inc +++ b/plugins/include/sdktools_stringtables.inc @@ -119,9 +119,9 @@ native int GetStringTableDataLength(int tableidx, int stringidx); * * @param tableidx A string table index. * @param stringidx A string index. - * @param userdata Buffer to store the user data. This will be set to "" if there is no user data. + * @param userdata Buffer to store the user data. This will be set to "" if there is no user data * @param maxlength Maximum length of string buffer. - * @return Number of bytes written to the buffer (UTF-8 safe). + * @return Number of bytes written to the buffer (binary safe, includes the null terminator). * @error Invalid string table index or string index. */ native int GetStringTableData(int tableidx, int stringidx, char[] userdata, int maxlength); @@ -133,10 +133,9 @@ native int GetStringTableData(int tableidx, int stringidx, char[] userdata, int * @param stringidx A string index. * @param userdata User data string that will be set. * @param length Length of user data string. This should include the null terminator. - * @return Number of bytes written to the buffer (UTF-8 safe). * @error Invalid string table index or string index. */ -native int SetStringTableData(int tableidx, int stringidx, const char[] userdata, int length); +native void SetStringTableData(int tableidx, int stringidx, const char[] userdata, int length); /** * Adds a string to a given string table.