[utils,string] add freerdp_extract_key_value

* Add new function freerdp_extract_key_value to extract key/value pairs
  from a string
* replace all sscanf usages with this new function
This commit is contained in:
akallabeth 2024-09-30 22:38:29 +02:00
parent e95707f3c7
commit f6eb6ad4d7
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5
7 changed files with 43 additions and 35 deletions

View File

@ -25,6 +25,7 @@
#include <map>
#include <freerdp/utils/string.h>
#include <freerdp/scancode.h>
#include <freerdp/log.h>
@ -507,14 +508,7 @@ std::list<std::string> sdlInput::tokenize(const std::string& data, const std::st
bool sdlInput::extract(const std::string& token, uint32_t& key, uint32_t& value)
{
int rc = sscanf(token.c_str(), "%" PRIu32 "=%" PRIu32, &key, &value);
if (rc != 2)
rc = sscanf(token.c_str(), "%" PRIx32 "=%" PRIx32 "", &key, &value);
if (rc != 2)
rc = sscanf(token.c_str(), "%" PRIu32 "=%" PRIx32, &key, &value);
if (rc != 2)
rc = sscanf(token.c_str(), "%" PRIx32 "=%" PRIu32, &key, &value);
return (rc == 2);
return freerdp_extract_key_value(token.c_str(), &key, &value);
}
uint32_t sdlInput::remapScancode(uint32_t scancode)

View File

@ -27,6 +27,7 @@
#include <map>
#include <freerdp/utils/string.h>
#include <freerdp/scancode.h>
#include <freerdp/log.h>
@ -483,14 +484,7 @@ std::list<std::string> sdlInput::tokenize(const std::string& data, const std::st
bool sdlInput::extract(const std::string& token, uint32_t& key, uint32_t& value)
{
int rc = sscanf(token.c_str(), "%" PRIu32 "=%" PRIu32, &key, &value);
if (rc != 2)
rc = sscanf(token.c_str(), "%" PRIx32 "=%" PRIx32 "", &key, &value);
if (rc != 2)
rc = sscanf(token.c_str(), "%" PRIu32 "=%" PRIx32, &key, &value);
if (rc != 2)
rc = sscanf(token.c_str(), "%" PRIx32 "=%" PRIu32, &key, &value);
return (rc == 2);
return freerdp_extract_key_value(token.c_str(), &key, &value);
}
uint32_t sdlInput::remapScancode(uint32_t scancode)

View File

@ -50,6 +50,7 @@
#include <freerdp/locale/keyboard.h>
#include <freerdp/utils/passphrase.h>
#include <freerdp/utils/proxy_utils.h>
#include <freerdp/utils/string.h>
#include <freerdp/channels/urbdrc.h>
#include <freerdp/channels/rdpdr.h>
#include <freerdp/locale/locale.h>
@ -2442,14 +2443,7 @@ static BOOL check_kbd_remap_valid(const char* token)
if (strlen(token) > 10)
return FALSE;
int rc = sscanf(token, "%" PRIu32 "=%" PRIu32, &key, &value);
if (rc != 2)
rc = sscanf(token, "%" PRIx32 "=%" PRIx32 "", &key, &value);
if (rc != 2)
rc = sscanf(token, "%" PRIu32 "=%" PRIx32, &key, &value);
if (rc != 2)
rc = sscanf(token, "%" PRIx32 "=%" PRIu32, &key, &value);
if (rc != 2)
if (!freerdp_extract_key_value(token, &key, &value))
{
WLog_WARN(TAG, "/kbd:remap invalid entry '%s'", token);
return FALSE;

View File

@ -35,6 +35,18 @@ extern "C"
FREERDP_API const char* rdp_cluster_info_flags_to_string(UINT32 flags, char* buffer,
size_t size);
/** @brief extracts <key>=<value> pairs from a string
*
* @param str The string to extract data from, must not be \b NULL
* @param pkey A pointer to store the key value, must not be \b NULL
* @param pvalue A pointer to store the value, must not be \b NULL
*
* @return \b TRUE if successfully extracted, \b FALSE if no matching data was found
*
* @since version 3.9.0
*/
FREERDP_API BOOL freerdp_extract_key_value(const char* str, UINT32* pkey, UINT32* pvalue);
#ifdef __cplusplus
}
#endif

View File

@ -247,10 +247,9 @@ static BOOL check_no_proxy(rdpSettings* settings, const char* no_proxy)
if (rangedelim != NULL)
{
const char* range = rangedelim + 1;
unsigned sub = 0;
int rc = sscanf(range, "%u", &sub);
const unsigned long sub = strtoul(range, NULL, 0);
if ((rc == 1) && (rc >= 0) && (sub <= UINT8_MAX))
if ((errno == 0) && (sub <= UINT8_MAX))
{
*rangedelim = '\0';

View File

@ -24,6 +24,7 @@
#include <winpr/crt.h>
#include <freerdp/utils/string.h>
#include <freerdp/types.h>
#include <freerdp/locale/keyboard.h>
#include <freerdp/locale/locale.h>
@ -351,14 +352,7 @@ DWORD freerdp_keyboard_init_ex(DWORD keyboardLayoutId, const char* keyboardRemap
{
DWORD key = 0;
DWORD value = 0;
int rc = sscanf(token, "%" PRIu32 "=%" PRIu32, &key, &value);
if (rc != 2)
rc = sscanf(token, "%" PRIx32 "=%" PRIx32 "", &key, &value);
if (rc != 2)
rc = sscanf(token, "%" PRIu32 "=%" PRIx32, &key, &value);
if (rc != 2)
rc = sscanf(token, "%" PRIx32 "=%" PRIu32, &key, &value);
if (rc != 2)
if (!freerdp_extract_key_value(token, &key, &value))
goto fail;
if (key >= ARRAYSIZE(REMAPPING_TABLE))
goto fail;

View File

@ -19,6 +19,8 @@
* limitations under the License.
*/
#include <errno.h>
#include <freerdp/utils/string.h>
#include <freerdp/settings.h>
@ -103,3 +105,22 @@ const char* rdp_cluster_info_flags_to_string(UINT32 flags, char* buffer, size_t
}
return buffer;
}
BOOL freerdp_extract_key_value(const char* str, UINT32* pkey, UINT32* pvalue)
{
if (!str || !pkey || !pvalue)
return FALSE;
char* end1 = NULL;
unsigned long key = strtoul(str, &end1, 0);
if ((errno != 0) || !end1 || (*end1 != '=') || (key > UINT32_MAX))
return FALSE;
unsigned long val = strtoul(&end1[1], NULL, 0);
if ((errno != 0) || (val > UINT32_MAX))
return FALSE;
*pkey = (UINT32)key;
*pvalue = (UINT32)val;
return TRUE;
}