From 2134c56fe5109913c1ecfc7c738e23b94402a3b5 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 20 Dec 2023 10:57:48 +1000 Subject: [PATCH] Rework match_from_string No functional changes, this just preps the way for more optional elements in the match string. --- libwacom/libwacom-database.c | 44 ++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/libwacom/libwacom-database.c b/libwacom/libwacom-database.c index d61cdada..baf934bd 100644 --- a/libwacom/libwacom-database.c +++ b/libwacom/libwacom-database.c @@ -163,23 +163,37 @@ make_match_string (const char *name, WacomBusType bus, int vendor_id, int produc } static gboolean -match_from_string(const char *str, WacomBusType *bus, int *vendor_id, int *product_id, char **name) +match_from_string(const char *str_in, WacomBusType *bus, int *vendor_id, int *product_id, char **name) { - int rc = 1, len = 0; - char busstr[64]; - - rc = sscanf(str, "%63[^|]|%x|%x|%n", busstr, vendor_id, product_id, &len); - if (len > 0) { - /* Grumble grumble scanf handling of %n */ - *name = g_strdup(str+len); - } else if (rc == 3) { - *name = NULL; - } else { - return FALSE; - } - *bus = bus_from_str (busstr); + gboolean rc = FALSE; + guint64 num; + char *str = g_strdup(str_in); + char **components = NULL; - return TRUE; + if (g_str_has_suffix(str_in, ";")) + str[strlen(str) - 1] = '\0'; + + components = g_strsplit(str, "|", 16); + if (!components[0] || !components[1] || !components[2]) + goto out; + + *bus = bus_from_str (components[0]); + if (!g_ascii_string_to_unsigned(components[1], 16, 0, 0xffff, &num, NULL)) + goto out; + *vendor_id = (int)num; + + if (!g_ascii_string_to_unsigned(components[2], 16, 0, 0xffff, &num, NULL)) + goto out; + *product_id = (int)num; + + if (components[3]) + *name = g_strdup(components[3]); + + rc = TRUE; +out: + free(str); + g_strfreev(components); + return rc; } static WacomMatch *