trivial: Add helper function for parsing a uint4 buffer

This commit is contained in:
Richard Hughes 2018-05-23 20:46:28 +01:00
parent b900c09ced
commit b04a8778e9
2 changed files with 21 additions and 0 deletions

View File

@ -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

View File

@ -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);