summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2014-03-05 09:07:17 +0100
committerAleksander Morgado <aleksander@aleksander.es>2014-03-05 09:07:17 +0100
commitcdba6091aeb9be338073954e6506e1a88b1839d2 (patch)
treea930b892809f56d1b6c0a0a6c26fc9eec032bf1d
parent378d71b61de6bd910659c02a52360262662a06bf (diff)
downloadlibmbim-cdba6091aeb9be338073954e6506e1a88b1839d2.tar.gz
libmbim-glib,uuid: validate str contents before converting to UUID
And avoid sscanf(). A single loop to validate contents and build the result is enough.
-rw-r--r--src/libmbim-glib/mbim-uuid.c56
1 files changed, 29 insertions, 27 deletions
diff --git a/src/libmbim-glib/mbim-uuid.c b/src/libmbim-glib/mbim-uuid.c
index 9d79b77..4bea834 100644
--- a/src/libmbim-glib/mbim-uuid.c
+++ b/src/libmbim-glib/mbim-uuid.c
@@ -85,9 +85,8 @@ mbim_uuid_get_printable (const MbimUuid *uuid)
*
* Fills in @uuid from the printable representation give in @str.
*
- * Accepts @str written with or without dashes separating items, e.g.:
+ * Only ccepts @str written with dashes separating items, e.g.:
* a289cc33-bcbb-8b4f-b6b0-133ec2aae6df
- * a289cc33bcbb8b4fb6b0133ec2aae6df
*
* Returns: %TRUE if @uuid was correctly set, %FALSE otherwise.
*/
@@ -95,11 +94,11 @@ gboolean
mbim_uuid_from_printable (const gchar *str,
MbimUuid *uuid)
{
- guint a0, a1, a2, a3;
- guint b0, b1;
- guint c0, c1;
- guint d0, d1;
- guint e0, e1, e2, e3, e4, e5;
+ guint8 tmp[16];
+ guint i;
+ guint k;
+ gint d0;
+ gint d1;
g_return_val_if_fail (str != NULL, FALSE);
g_return_val_if_fail (uuid != NULL, FALSE);
@@ -107,27 +106,30 @@ mbim_uuid_from_printable (const gchar *str,
if (strlen (str) != 36)
return FALSE;
- if ((str[8] != '-' || str[13] != '-' || str[18] != '-' || str[23] != '-'))
- return FALSE;
-
- if (sscanf (str,
- "%02x%02x%02x%02x-"
- "%02x%02x-"
- "%02x%02x-"
- "%02x%02x-"
- "%02x%02x%02x%02x%02x%02x",
- &a0, &a1, &a2, &a3,
- &b0, &b1,
- &c0, &c1,
- &d0, &d1,
- &e0, &e1, &e2, &e3, &e4, &e5) != 16)
- return FALSE;
+ for (i = 0, k = 0, d0 = -1, d1 = -1; str[i]; i++) {
+ /* Accept dashes in expected positions */
+ if (str[i] == '-') {
+ if (i == 8 || i == 13 || i == 18 || i == 23)
+ continue;
+ return FALSE;
+ }
+ /* Read first digit in the hex pair */
+ else if (d0 == -1) {
+ d0 = g_ascii_xdigit_value (str[i]);
+ if (d0 == -1)
+ return FALSE;
+ }
+ /* Read second digit in the hex pair */
+ else {
+ d1 = g_ascii_xdigit_value (str[i]);
+ if (d1 == -1)
+ return FALSE;
+ tmp[k++] = (d0 << 4) | d1;
+ d0 = d1 = -1;
+ }
+ }
- uuid->a[0] = a0; uuid->a[1] = a1; uuid->a[2] = a2; uuid->a[3] = a3;
- uuid->b[0] = b0; uuid->b[1] = b1;
- uuid->c[0] = c0; uuid->c[1] = c1;
- uuid->d[0] = d0; uuid->d[1] = d1;
- uuid->e[0] = e0; uuid->e[1] = e1; uuid->e[2] = e2; uuid->e[3] = e3; uuid->e[4] = e4; uuid->e[5] = e5;
+ memcpy (uuid, tmp, sizeof (tmp));
return TRUE;
}