Provide a lower level __bin2hex function that does not allocate memory itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
diff --git a/miner.h b/miner.h
index ca7738d..70faa27 100644
--- a/miner.h
+++ b/miner.h
@@ -952,6 +952,7 @@ extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
#endif
extern const char *proxytype(proxytypes_t proxytype);
extern char *get_proxy(char *url, struct pool *pool);
+extern void __bin2hex(char *s, const unsigned char *p, size_t len);
extern char *bin2hex(const unsigned char *p, size_t len);
extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
diff --git a/util.c b/util.c
index 27b8c3b..8e03281 100644
--- a/util.c
+++ b/util.c
@@ -584,6 +584,16 @@ char *get_proxy(char *url, struct pool *pool)
return url;
}
+/* Adequate size s==len*2 + 1 must be alloced to use this variant */
+void __bin2hex(char *s, const unsigned char *p, size_t len)
+{
+ int i;
+
+ for (i = 0; i < (int)len; i++)
+ sprintf(s + (i * 2), "%02x", (unsigned int)p[i]);
+
+}
+
/* Returns a malloced array string of a binary value of arbitrary length. The
* array is rounded up to a 4 byte size to appease architectures that need
* aligned array sizes */
@@ -591,7 +601,6 @@ char *bin2hex(const unsigned char *p, size_t len)
{
ssize_t slen;
char *s;
- int i;
slen = len * 2 + 1;
if (slen % 4)
@@ -600,8 +609,7 @@ char *bin2hex(const unsigned char *p, size_t len)
if (unlikely(!s))
quithere(1, "Failed to calloc");
- for (i = 0; i < (int)len; i++)
- sprintf(s + (i * 2), "%02x", (unsigned int)p[i]);
+ __bin2hex(s, p, len);
return s;
}