Move hexadecimal<->binary functions to hex.c (included via new not-installed private.h)
This commit is contained in:
@@ -5,7 +5,8 @@ lib_LTLIBRARIES = \
|
|||||||
|
|
||||||
libblkmaker_@LIBBLKMAKER_API_VERSION@_la_SOURCES = \
|
libblkmaker_@LIBBLKMAKER_API_VERSION@_la_SOURCES = \
|
||||||
blkmaker.c \
|
blkmaker.c \
|
||||||
blktemplate.c
|
blktemplate.c \
|
||||||
|
hex.c
|
||||||
|
|
||||||
libblkmaker_@LIBBLKMAKER_API_VERSION@_la_LDFLAGS = -version-info $(LIBBLKMAKER_SO_VERSION) -no-undefined
|
libblkmaker_@LIBBLKMAKER_API_VERSION@_la_LDFLAGS = -version-info $(LIBBLKMAKER_SO_VERSION) -no-undefined
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
#include <blkmaker.h>
|
#include <blkmaker.h>
|
||||||
#include <blktemplate.h>
|
#include <blktemplate.h>
|
||||||
|
|
||||||
|
#include "private.h"
|
||||||
|
|
||||||
#ifndef JSON_INTEGER_IS_LONG_LONG
|
#ifndef JSON_INTEGER_IS_LONG_LONG
|
||||||
# error "Jansson 2.0 with long long support required!"
|
# error "Jansson 2.0 with long long support required!"
|
||||||
#endif
|
#endif
|
||||||
@@ -80,38 +82,7 @@ err:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool my_hex2bin(void *o, const char *x, size_t len) {
|
#define my_hex2bin _blkmk_hex2bin
|
||||||
unsigned char *oc = o;
|
|
||||||
unsigned char c, hc = 0x10;
|
|
||||||
len *= 2;
|
|
||||||
while (len)
|
|
||||||
{
|
|
||||||
switch (x[0]) {
|
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
|
||||||
case '5': case '6': case '7': case '8': case '9':
|
|
||||||
c = x[0] - '0';
|
|
||||||
break;
|
|
||||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
||||||
c = x[0] - 'A' + 10;
|
|
||||||
break;
|
|
||||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
||||||
c = x[0] - 'a' + 10;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
++x;
|
|
||||||
if (hc < 0x10)
|
|
||||||
{
|
|
||||||
(oc++)[0] = (hc << 4) | c;
|
|
||||||
hc = 0x10;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
hc = c;
|
|
||||||
--len;
|
|
||||||
}
|
|
||||||
return !x[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
#define GET(key, type) do { \
|
#define GET(key, type) do { \
|
||||||
if (!(v = json_object_get(json, #key))) \
|
if (!(v = json_object_get(json, #key))) \
|
||||||
@@ -294,17 +265,7 @@ char varintEncode(unsigned char *out, uint64_t n) {
|
|||||||
return L;
|
return L;
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
#define my_bin2hex _blkmk_bin2hex
|
||||||
void my_bin2hex(char *out, const void *data, size_t datasz) {
|
|
||||||
const unsigned char *datac = data;
|
|
||||||
static char hex[] = "0123456789abcdef";
|
|
||||||
out[datasz * 2] = '\0';
|
|
||||||
for (size_t i = 0; i < datasz; ++i)
|
|
||||||
{
|
|
||||||
out[ i*2 ] = hex[datac[i] >> 4];
|
|
||||||
out[(i*2)+1] = hex[datac[i] & 15];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
json_t *blkmk_submit_jansson(blktemplate_t *tmpl, const unsigned char *data, unsigned int dataid, blknonce_t nonce) {
|
json_t *blkmk_submit_jansson(blktemplate_t *tmpl, const unsigned char *data, unsigned int dataid, blknonce_t nonce) {
|
||||||
unsigned char blk[80 + 8 + 1000000];
|
unsigned char blk[80 + 8 + 1000000];
|
||||||
|
|||||||
47
hex.c
Normal file
47
hex.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool _blkmk_hex2bin(void *o, const char *x, size_t len) {
|
||||||
|
unsigned char *oc = o;
|
||||||
|
unsigned char c, hc = 0x10;
|
||||||
|
len *= 2;
|
||||||
|
while (len)
|
||||||
|
{
|
||||||
|
switch (x[0]) {
|
||||||
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
|
case '5': case '6': case '7': case '8': case '9':
|
||||||
|
c = x[0] - '0';
|
||||||
|
break;
|
||||||
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||||
|
c = x[0] - 'A' + 10;
|
||||||
|
break;
|
||||||
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||||
|
c = x[0] - 'a' + 10;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
++x;
|
||||||
|
if (hc < 0x10)
|
||||||
|
{
|
||||||
|
(oc++)[0] = (hc << 4) | c;
|
||||||
|
hc = 0x10;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
hc = c;
|
||||||
|
--len;
|
||||||
|
}
|
||||||
|
return !x[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
void _blkmk_bin2hex(char *out, const void *data, size_t datasz) {
|
||||||
|
const unsigned char *datac = data;
|
||||||
|
static char hex[] = "0123456789abcdef";
|
||||||
|
out[datasz * 2] = '\0';
|
||||||
|
for (size_t i = 0; i < datasz; ++i)
|
||||||
|
{
|
||||||
|
out[ i*2 ] = hex[datac[i] >> 4];
|
||||||
|
out[(i*2)+1] = hex[datac[i] & 15];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user