summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2019-01-28 16:56:46 +0100
committerThomas Haller <thaller@redhat.com>2019-02-21 09:36:17 +0100
commitb5efcf08f4598004606181fe005e8a22127df61c (patch)
tree105fbe2b262422b55ca3fefb4ff9a542fc5a47d4 /shared
parentd2144019d84c3706446f28220c84b678c105d164 (diff)
downloadNetworkManager-b5efcf08f4598004606181fe005e8a22127df61c.tar.gz
all: move nm_utils_bin2hexstr_full() to shared
reuse++
Diffstat (limited to 'shared')
-rw-r--r--shared/nm-utils/nm-shared-utils.c59
-rw-r--r--shared/nm-utils/nm-shared-utils.h6
2 files changed, 65 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index b22251afc8..f48513e02f 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -2477,3 +2477,62 @@ nm_utils_memeqzero (gconstpointer data, gsize length)
/* Now we know that's zero, memcmp with self. */
return memcmp (data, p, length) == 0;
}
+
+/**
+ * nm_utils_bin2hexstr_full:
+ * @addr: pointer of @length bytes.
+ * @length: number of bytes in @addr
+ * @delimiter: either '\0', otherwise the output string will have the
+ * given delimiter character between each two hex numbers.
+ * @upper_case: if TRUE, use upper case ASCII characters for hex.
+ * @out: if %NULL, the function will allocate a new buffer of
+ * either (@length*2+1) or (@length*3) bytes, depending on whether
+ * a @delimiter is specified. In that case, the allocated buffer will
+ * be returned and must be freed by the caller.
+ * If not %NULL, the buffer must already be preallocated and contain
+ * at least (@length*2+1) or (@length*3) bytes, depending on the delimiter.
+ *
+ * Returns: the binary value converted to a hex string. If @out is given,
+ * this always returns @out. If @out is %NULL, a newly allocated string
+ * is returned.
+ */
+char *
+nm_utils_bin2hexstr_full (gconstpointer addr,
+ gsize length,
+ char delimiter,
+ gboolean upper_case,
+ char *out)
+{
+ const guint8 *in = addr;
+ const char *LOOKUP = upper_case ? "0123456789ABCDEF" : "0123456789abcdef";
+ char *out0;
+
+ nm_assert (addr);
+ nm_assert (length > 0);
+
+ if (out)
+ out0 = out;
+ else {
+ out0 = out = g_new (char, delimiter == '\0'
+ ? length * 2 + 1
+ : length * 3);
+ }
+
+ /* @out must contain at least @length*3 bytes if @delimiter is set,
+ * otherwise, @length*2+1. */
+
+ for (;;) {
+ const guint8 v = *in++;
+
+ *out++ = LOOKUP[v >> 4];
+ *out++ = LOOKUP[v & 0x0F];
+ length--;
+ if (!length)
+ break;
+ if (delimiter)
+ *out++ = delimiter;
+ }
+
+ *out = 0;
+ return out0;
+}
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index 4d9f20d073..c70ca86e86 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -1122,4 +1122,10 @@ nm_strv_ptrarray_take_gstring (GPtrArray *cmd,
int nm_utils_getpagesize (void);
+char *nm_utils_bin2hexstr_full (gconstpointer addr,
+ gsize length,
+ char delimiter,
+ gboolean upper_case,
+ char *out);
+
#endif /* __NM_SHARED_UTILS_H__ */