aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2018-10-27 23:54:56 +0200
committerAndreas Schneider <asn@cryptomilk.org>2019-01-24 09:34:18 +0100
commit43a4f86b6e14a907b3a298d10d5cd7efb59f6a09 (patch)
treeef166a4dbdd80362fcb4142dae1a02275e126265 /src/misc.c
parente42a423a247c8c9f0bd58d32a8b80c522f7b43f6 (diff)
downloadlibssh-43a4f86b6e14a907b3a298d10d5cd7efb59f6a09.tar.gz
libssh-43a4f86b6e14a907b3a298d10d5cd7efb59f6a09.tar.xz
libssh-43a4f86b6e14a907b3a298d10d5cd7efb59f6a09.zip
dh: move unrelated functions out of dh.c
Signed-off-by: Aris Adamantiadis <aris@0xbadc0de.be> Reviewed-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c
index 782b7bc0..abb82425 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -333,6 +333,63 @@ char *ssh_hostport(const char *host, int port){
}
/**
+ * @brief Convert a buffer into a colon separated hex string.
+ * The caller has to free the memory.
+ *
+ * @param what What should be converted to a hex string.
+ *
+ * @param len Length of the buffer to convert.
+ *
+ * @return The hex string or NULL on error.
+ *
+ * @see ssh_string_free_char()
+ */
+char *ssh_get_hexa(const unsigned char *what, size_t len) {
+ const char h[] = "0123456789abcdef";
+ char *hexa;
+ size_t i;
+ size_t hlen = len * 3;
+
+ if (len > (UINT_MAX - 1) / 3) {
+ return NULL;
+ }
+
+ hexa = malloc(hlen + 1);
+ if (hexa == NULL) {
+ return NULL;
+ }
+
+ for (i = 0; i < len; i++) {
+ hexa[i * 3] = h[(what[i] >> 4) & 0xF];
+ hexa[i * 3 + 1] = h[what[i] & 0xF];
+ hexa[i * 3 + 2] = ':';
+ }
+ hexa[hlen - 1] = '\0';
+
+ return hexa;
+}
+
+/**
+ * @brief Print a buffer as colon separated hex string.
+ *
+ * @param descr Description printed in front of the hex string.
+ *
+ * @param what What should be converted to a hex string.
+ *
+ * @param len Length of the buffer to convert.
+ */
+void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len) {
+ char *hexa = ssh_get_hexa(what, len);
+
+ if (hexa == NULL) {
+ return;
+ }
+ fprintf(stderr, "%s: %s\n", descr, hexa);
+
+ free(hexa);
+}
+
+/**
* @brief Check if libssh is the required version or get the version
* string.
*