summaryrefslogtreecommitdiff
path: root/src/qmi-firmware-update/qfu-utils.c
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2016-11-30 17:27:57 +0100
committerAleksander Morgado <aleksander@aleksander.es>2017-01-16 11:24:13 +0100
commit4f806c5b20edb2963811a85640dc842305757ef6 (patch)
tree9cc33ab0d294fd5d1dbd4bdabdcd66e7a0fc5e4f /src/qmi-firmware-update/qfu-utils.c
parent01217ce0e49bd21a0913ff5088aee765292f6a7e (diff)
qmi-firmware-update: setup str_hex() as common utils
Diffstat (limited to 'src/qmi-firmware-update/qfu-utils.c')
-rw-r--r--src/qmi-firmware-update/qfu-utils.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/qmi-firmware-update/qfu-utils.c b/src/qmi-firmware-update/qfu-utils.c
index 86677c7..4cf9d89 100644
--- a/src/qmi-firmware-update/qfu-utils.c
+++ b/src/qmi-firmware-update/qfu-utils.c
@@ -23,10 +23,50 @@
* Copyright (C) 2010 Red Hat, Inc.
*/
+#include <stdio.h>
+
#include <glib.h>
#include "qfu-utils.h"
+/******************************************************************************/
+
+gchar *
+qfu_utils_str_hex (gconstpointer mem,
+ gsize size,
+ gchar delimiter)
+{
+ const guint8 *data = mem;
+ gsize i;
+ gsize j;
+ gsize new_str_length;
+ gchar *new_str;
+
+ /* Get new string length. If input string has N bytes, we need:
+ * - 1 byte for last NUL char
+ * - 2N bytes for hexadecimal char representation of each byte...
+ * - N-1 bytes for the separator ':'
+ * So... a total of (1+2N+N-1) = 3N bytes are needed... */
+ new_str_length = 3 * size;
+
+ /* Allocate memory for new array and initialize contents to NUL */
+ new_str = g_malloc0 (new_str_length);
+
+ /* Print hexadecimal representation of each byte... */
+ for (i = 0, j = 0; i < size; i++, j += 3) {
+ /* Print character in output string... */
+ snprintf (&new_str[j], 3, "%02X", data[i]);
+ /* And if needed, add separator */
+ if (i != (size - 1) )
+ new_str[j + 2] = delimiter;
+ }
+
+ /* Set output string */
+ return new_str;
+}
+
+/******************************************************************************/
+
/* Table of CRCs for each possible byte, with a generator polynomial of 0x8408 */
static const guint16 crc_table[256] = {
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,