Move hexadecimal<->binary functions to hex.c (included via new not-installed private.h)

This commit is contained in:
Luke Dashjr
2012-10-12 15:57:55 +00:00
parent a9a29294b2
commit bc533a9546
4 changed files with 64 additions and 44 deletions

View File

@@ -5,7 +5,8 @@ lib_LTLIBRARIES = \
libblkmaker_@LIBBLKMAKER_API_VERSION@_la_SOURCES = \
blkmaker.c \
blktemplate.c
blktemplate.c \
hex.c
libblkmaker_@LIBBLKMAKER_API_VERSION@_la_LDFLAGS = -version-info $(LIBBLKMAKER_SO_VERSION) -no-undefined

View File

@@ -14,6 +14,8 @@
#include <blkmaker.h>
#include <blktemplate.h>
#include "private.h"
#ifndef JSON_INTEGER_IS_LONG_LONG
# error "Jansson 2.0 with long long support required!"
#endif
@@ -80,38 +82,7 @@ err:
}
static bool my_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];
}
#define my_hex2bin _blkmk_hex2bin
#define GET(key, type) do { \
if (!(v = json_object_get(json, #key))) \
@@ -294,17 +265,7 @@ char varintEncode(unsigned char *out, uint64_t n) {
return L;
}
static
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];
}
}
#define my_bin2hex _blkmk_bin2hex
json_t *blkmk_submit_jansson(blktemplate_t *tmpl, const unsigned char *data, unsigned int dataid, blknonce_t nonce) {
unsigned char blk[80 + 8 + 1000000];

47
hex.c Normal file
View 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];
}
}

11
private.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef BLKMK_PRIVATE_H
#define BLKMK_PRIVATE_H
#include <stdbool.h>
#include <string.h>
// hex.c
extern void _blkmk_bin2hex(char *out, const void *data, size_t datasz);
extern bool _blkmk_hex2bin(void *o, const char *x, size_t len);
#endif