diff options
author | Richard Hughes <richard@hughsie.com> | 2015-08-10 09:11:45 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2015-08-10 09:17:53 +0100 |
commit | 3e2d63ef770b99001a460fb1a143b1407a391108 (patch) | |
tree | 857ce7724b12ac807b553a4976d279e853feac0b | |
parent | bce26189958b567b4145e3594e023bd6fdca8387 (diff) | |
download | appstream-glib-3e2d63ef770b99001a460fb1a143b1407a391108.tar.gz |
Add two functions from fwupd for checking GUID strings
-rw-r--r-- | libappstream-glib/as-self-test.c | 21 | ||||
-rw-r--r-- | libappstream-glib/as-utils.c | 72 | ||||
-rw-r--r-- | libappstream-glib/as-utils.h | 2 |
3 files changed, 95 insertions, 0 deletions
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 00cfdf8..cb6020c 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -3210,6 +3210,26 @@ as_test_store_speed_desktop_func (void) } static void +as_test_utils_guid_func (void) +{ + _cleanup_free_ gchar *guid = NULL; + + /* invalid */ + g_assert (!as_utils_guid_is_valid (NULL)); + g_assert (!as_utils_guid_is_valid ("")); + g_assert (!as_utils_guid_is_valid ("1ff60ab2-3905-06a1-b476")); + g_assert (!as_utils_guid_is_valid ("1ff60ab2-XXXX-XXXX-XXXX-0371f00c9e9b")); + g_assert (!as_utils_guid_is_valid (" 1ff60ab2-3905-06a1-b476-0371f00c9e9b")); + + /* valid */ + g_assert (as_utils_guid_is_valid ("1ff60ab2-3905-06a1-b476-0371f00c9e9b")); + + /* make valid */ + guid = as_utils_guid_from_string ("0x8086:0x0406"); + g_assert_cmpstr (guid, ==, "1ff60ab2-3905-06a1-b476-0371f00c9e9b"); +} + +static void as_test_utils_icons_func (void) { gchar *tmp; @@ -4197,6 +4217,7 @@ main (int argc, char **argv) g_test_add_func ("/AppStream/node{intltool}", as_test_node_intltool_func); g_test_add_func ("/AppStream/node{sort}", as_test_node_sort_func); g_test_add_func ("/AppStream/utils", as_test_utils_func); + g_test_add_func ("/AppStream/utils{guid}", as_test_utils_guid_func); g_test_add_func ("/AppStream/utils{icons}", as_test_utils_icons_func); g_test_add_func ("/AppStream/utils{spdx-token}", as_test_utils_spdx_token_func); g_test_add_func ("/AppStream/utils{install-filename}", as_test_utils_install_filename_func); diff --git a/libappstream-glib/as-utils.c b/libappstream-glib/as-utils.c index 3c63860..4b31e50 100644 --- a/libappstream-glib/as-utils.c +++ b/libappstream-glib/as-utils.c @@ -1513,3 +1513,75 @@ as_ptr_array_find_string (GPtrArray *array, const gchar *value) } return NULL; } + +/** + * as_utils_guid_is_xdigit: + **/ +static gboolean +as_utils_guid_is_xdigit (const gchar *str) +{ + guint i; + for (i = 0; str[i] != '\0'; i++) { + if (!g_ascii_isxdigit (str[i])) + return FALSE; + } + return TRUE; +} + +/** + * as_utils_guid_is_valid: + * @guid: string to check + * + * Checks the source string is a valid string GUID descriptor. + * + * Returns: %TRUE if @guid was a valid GUID, %FALSE otherwise + * + * Since: 0.5.0 + **/ +gboolean +as_utils_guid_is_valid (const gchar *guid) +{ + _cleanup_strv_free_ gchar **split = NULL; + if (guid == NULL) + return FALSE; + split = g_strsplit (guid, "-", -1); + if (g_strv_length (split) != 5) + return FALSE; + if (strlen (split[0]) != 8 || !as_utils_guid_is_xdigit (split[0])) + return FALSE; + if (strlen (split[1]) != 4 || !as_utils_guid_is_xdigit (split[1])) + return FALSE; + if (strlen (split[2]) != 4 || !as_utils_guid_is_xdigit (split[2])) + return FALSE; + if (strlen (split[3]) != 4 || !as_utils_guid_is_xdigit (split[3])) + return FALSE; + if (strlen (split[4]) != 12 || !as_utils_guid_is_xdigit (split[4])) + return FALSE; + return TRUE; +} + +/** + * as_utils_guid_from_string: + * @str: A source string to use as a key + * + * Returns a GUID for a given string. This uses SHA1 and some string + * modification so even small differences in the @str will produce radically + * different GUID return values. + * + * Returns: A new GUID, or %NULL if the string was invalid + * + * Since: 0.5.0 + **/ +gchar * +as_utils_guid_from_string (const gchar *str) +{ + gchar *tmp; + tmp = g_compute_checksum_for_string (G_CHECKSUM_SHA1, str, -1); + tmp[8] = '-'; + tmp[13] = '-'; + tmp[18] = '-'; + tmp[23] = '-'; + tmp[36] = '\0'; + g_assert (as_utils_guid_is_valid (tmp)); + return tmp; +} diff --git a/libappstream-glib/as-utils.h b/libappstream-glib/as-utils.h index 62e0ffc..26c819b 100644 --- a/libappstream-glib/as-utils.h +++ b/libappstream-glib/as-utils.h @@ -126,6 +126,8 @@ gboolean as_utils_search_token_valid (const gchar *token); gchar **as_utils_search_tokenize (const gchar *search); gint as_utils_vercmp (const gchar *version_a, const gchar *version_b); +gboolean as_utils_guid_is_valid (const gchar *guid); +gchar *as_utils_guid_from_string (const gchar *str); G_END_DECLS |