From b04a8778e9a31bfee8c647df5f1e440d66e534a9 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Wed, 23 May 2018 20:46:28 +0100 Subject: [PATCH] trivial: Add helper function for parsing a uint4 buffer --- plugins/dfu/dfu-common.c | 20 ++++++++++++++++++++ plugins/dfu/dfu-common.h | 1 + 2 files changed, 21 insertions(+) diff --git a/plugins/dfu/dfu-common.c b/plugins/dfu/dfu-common.c index 5037e2ff0..aeb5acc0b 100644 --- a/plugins/dfu/dfu-common.c +++ b/plugins/dfu/dfu-common.c @@ -212,6 +212,26 @@ dfu_utils_bytes_is_empty (GBytes *bytes) return TRUE; } +/** + * dfu_utils_buffer_parse_uint4: + * @data: a string + * + * Parses a base 16 number from a string. + * + * The string MUST be at least 1 byte long as this function cannot check the + * length of @data. Checking the size must be done in the caller. + * + * Return value: A parsed value, or 0 for error + **/ +guint8 +dfu_utils_buffer_parse_uint4 (const gchar *data) +{ + gchar buffer[2]; + memcpy (buffer, data, 1); + buffer[1] = '\0'; + return (guint8) g_ascii_strtoull (buffer, NULL, 16); +} + /** * dfu_utils_buffer_parse_uint8: * @data: a string diff --git a/plugins/dfu/dfu-common.h b/plugins/dfu/dfu-common.h index c72b316db..d8d292e98 100644 --- a/plugins/dfu/dfu-common.h +++ b/plugins/dfu/dfu-common.h @@ -173,6 +173,7 @@ const gchar *dfu_version_to_string (DfuVersion version); /* helpers */ GBytes *dfu_utils_bytes_join_array (GPtrArray *chunks); gboolean dfu_utils_bytes_is_empty (GBytes *bytes); +guint8 dfu_utils_buffer_parse_uint4 (const gchar *data); guint8 dfu_utils_buffer_parse_uint8 (const gchar *data); guint16 dfu_utils_buffer_parse_uint16 (const gchar *data); guint32 dfu_utils_buffer_parse_uint24 (const gchar *data);