aboutsummaryrefslogtreecommitdiff
path: root/src/lib/encoding
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-06-27 16:21:55 -0400
committerNick Mathewson <nickm@torproject.org>2018-06-27 16:59:56 -0400
commit0a9d8dcf2b0de62402efacc0fa4bc9a29109a6c0 (patch)
treef2ebec89bb02d6a2dab0817a360177d308fc5fca /src/lib/encoding
parent194a34cdc28c6b309dd9a32f5446409810b1d32b (diff)
downloadtor-0a9d8dcf2b0de62402efacc0fa4bc9a29109a6c0.tar.gz
tor-0a9d8dcf2b0de62402efacc0fa4bc9a29109a6c0.zip
Move hex_str to binascii.c
Diffstat (limited to 'src/lib/encoding')
-rw-r--r--src/lib/encoding/binascii.c15
-rw-r--r--src/lib/encoding/binascii.h2
2 files changed, 17 insertions, 0 deletions
diff --git a/src/lib/encoding/binascii.c b/src/lib/encoding/binascii.c
index 554b95016b..df9bb4a813 100644
--- a/src/lib/encoding/binascii.c
+++ b/src/lib/encoding/binascii.c
@@ -25,6 +25,21 @@
#include <string.h>
#include <stdlib.h>
+/** Return a pointer to a NUL-terminated hexadecimal string encoding
+ * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
+ * result does not need to be deallocated, but repeated calls to
+ * hex_str will trash old results.
+ */
+const char *
+hex_str(const char *from, size_t fromlen)
+{
+ static char buf[65];
+ if (fromlen>(sizeof(buf)-1)/2)
+ fromlen = (sizeof(buf)-1)/2;
+ base16_encode(buf,sizeof(buf),from,fromlen);
+ return buf;
+}
+
/* Return the base32 encoded size in bytes using the source length srclen.
*
* (WATCH OUT: This API counts the terminating NUL byte, but
diff --git a/src/lib/encoding/binascii.h b/src/lib/encoding/binascii.h
index 04310f4ed9..67f9eb0e87 100644
--- a/src/lib/encoding/binascii.h
+++ b/src/lib/encoding/binascii.h
@@ -12,6 +12,8 @@
#include "lib/testsupport/testsupport.h"
#include "lib/cc/torint.h"
+const char *hex_str(const char *from, size_t fromlen);
+
/** @{ */
/** These macros don't check for overflow. Use them only for constant inputs
* (like array declarations). The *_LEN macros are the raw encoding lengths