minor improvements

- binary data now explicitly unsigned char* to avoid confusion
 - rpc_get_request_body now using stecpy to avoid repeated strlen
This commit is contained in:
Jethro Grassie 2019-08-05 19:41:40 -04:00
parent ba4501684b
commit 01b2f3274c
No known key found for this signature in database
GPG key ID: DE8ED755616565BB
5 changed files with 66 additions and 59 deletions

View file

@ -243,8 +243,8 @@ static BIGNUM *base_diff;
static pool_stats_t pool_stats;
static pthread_mutex_t mutex_clients = PTHREAD_MUTEX_INITIALIZER;
static FILE *fd_log;
static char sec_view[32];
static char pub_spend[32];
static unsigned char sec_view[32];
static unsigned char pub_spend[32];
#define JSON_GET_OR_ERROR(name, parent, type, client) \
json_object *name = NULL; \
@ -810,7 +810,7 @@ target_to_hex(uint64_t target, char *target_hex)
if (target & 0xFFFFFFFF00000000)
{
log_debug("High target requested: %"PRIu64, target);
bin_to_hex((const char*)&target, 8, &target_hex[0], 16);
bin_to_hex((const unsigned char*)&target, 8, &target_hex[0], 16);
return;
}
BIGNUM *diff = BN_new();
@ -818,14 +818,14 @@ target_to_hex(uint64_t target, char *target_hex)
#ifdef SIXTY_FOUR_BIT_LONG
BN_set_word(bnt, target);
#else
char st[24];
snprintf(st, 24, "%"PRIu64, target);
BN_dec2bn(&bnt, st);
char tmp[24];
snprintf(tmp, 24, "%"PRIu64, target);
BN_dec2bn(&bnt, tmp);
#endif
BN_div(diff, NULL, base_diff, bnt, bn_ctx);
BN_rshift(diff, diff, 224);
uint32_t w = BN_get_word(diff);
bin_to_hex((const char*)&w, 4, &target_hex[0], 8);
bin_to_hex((const unsigned char*)&w, 4, &target_hex[0], 8);
BN_free(bnt);
BN_free(diff);
}
@ -838,7 +838,7 @@ stratum_get_proxy_job_body(char *body, const client_t *client,
const char *client_id = client->client_id;
const job_t *job = &client->active_jobs[0];
char job_id[33] = {0};
bin_to_hex((const char*)job->id, sizeof(uuid_t), job_id, 32);
bin_to_hex((const unsigned char*)job->id, sizeof(uuid_t), job_id, 32);
uint64_t target = job->target;
char target_hex[17] = {0};
target_to_hex(target, &target_hex[0]);
@ -885,11 +885,11 @@ stratum_get_job_body_ss(char *body, const client_t *client, bool response)
const char *client_id = client->client_id;
const job_t *job = &client->active_jobs[0];
char job_id[33] = {0};
bin_to_hex((const char*)job->id, sizeof(uuid_t), job_id, 32);
bin_to_hex((const unsigned char*)job->id, sizeof(uuid_t), job_id, 32);
uint64_t target = job->target;
char target_hex[17] = {0};
target_to_hex(target, &target_hex[0]);
char extra_bin[8];
unsigned char extra_bin[8];
memcpy(extra_bin, &job->extra_nonce, 4);
memcpy(extra_bin+4, &instance_id, 4);
char extra_hex[17] = {0};
@ -924,7 +924,7 @@ stratum_get_job_body(char *body, const client_t *client, bool response)
const char *client_id = client->client_id;
const job_t *job = &client->active_jobs[0];
char job_id[33] = {0};
bin_to_hex((const char*)job->id, sizeof(uuid_t), job_id, 32);
bin_to_hex((const unsigned char*)job->id, sizeof(uuid_t), job_id, 32);
const char *blob = job->blob;
uint64_t target = job->target;
uint64_t height = job->block_template->height;
@ -1013,7 +1013,7 @@ static job_t *
client_find_job(client_t *client, const char *job_id)
{
uuid_t jid;
hex_to_bin(job_id, strlen(job_id), (char*)&jid, sizeof(uuid_t));
hex_to_bin(job_id, strlen(job_id), (unsigned char*)&jid, sizeof(uuid_t));
for (size_t i=0; i<CLIENT_JOBS_MAX; i++)
{
job_t *job = &client->active_jobs[i];
@ -1090,11 +1090,11 @@ client_send_job(client_t *client, bool response)
/* Convert template to blob */
size_t bin_size = strlen(bt->blocktemplate_blob) >> 1;
char *block = calloc(bin_size, sizeof(char));
unsigned char *block = calloc(bin_size, sizeof(char));
hex_to_bin(bt->blocktemplate_blob, bin_size << 1, block, bin_size);
/* Set the extra nonce in our reserved space */
char *p = block;
unsigned char *p = block;
p += bt->reserved_offset;
++extra_nonce;
memcpy(p, &extra_nonce, sizeof(extra_nonce));
@ -1106,7 +1106,7 @@ client_send_job(client_t *client, bool response)
/* Get hashong blob */
size_t hashing_blob_size;
char *hashing_blob = NULL;
unsigned char *hashing_blob = NULL;
get_hashing_blob(block, bin_size, &hashing_blob, &hashing_blob_size);
/* Make hex */
@ -1123,7 +1123,7 @@ client_send_job(client_t *client, bool response)
/* Send */
char job_id[33] = {0};
bin_to_hex((const char*)job->id, sizeof(uuid_t), job_id, 32);
bin_to_hex((const unsigned char*)job->id, sizeof(uuid_t), &job_id[0], 32);
/* Retarget */
retarget(client, job);
@ -1296,20 +1296,20 @@ rpc_wallet_request(struct event_base *base, const char *body,
}
static void
rpc_get_request_body(char *body, const char* method, char* fmt, ...)
rpc_get_request_body(char *body, const char *method, char *fmt, ...)
{
char *pb = body;
char *end = body + RPC_BODY_MAX;
snprintf(pb, RPC_BODY_MAX, "%s%s%s", "{\"jsonrpc\":\"2.0\",\"id\":\"0\","
"\"method\":\"", method, "\"");
pb += strlen(pb);
pb = stecpy(pb, "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"", end);
pb = stecpy(pb, method, end);
pb = stecpy(pb, "\"", end);
if (fmt && *fmt)
{
char *s;
uint64_t d;
snprintf(pb, RPC_BODY_MAX - strlen(body), "%s", ",\"params\":{");
pb += strlen(pb);
pb = stecpy(pb, ",\"params\":{", end);
va_list args;
va_start(args, fmt);
uint8_t count = 0;
@ -1319,13 +1319,15 @@ rpc_get_request_body(char *body, const char* method, char* fmt, ...)
{
case 's':
s = va_arg(args, char *);
snprintf(pb, RPC_BODY_MAX - strlen(body), "\"%s\"", s);
pb += strlen(pb);
pb = stecpy(pb, "\"", end);
pb = stecpy(pb, s, end);
pb = stecpy(pb, "\"", end);
break;
case 'd':
d = va_arg(args, uint64_t);
snprintf(pb, RPC_BODY_MAX - strlen(body), "%"PRIu64, d);
pb += strlen(pb);
char tmp[24];
snprintf(tmp, 24, "%"PRIu64, d);
pb = stecpy(pb, tmp, end);
break;
}
*pb++ = count++ % 2 ? ',' : ':';
@ -2013,7 +2015,7 @@ client_on_login(json_object *message, client_t *client)
strncpy(client->worker_id, worker_id, sizeof(client->worker_id));
uuid_t cid;
uuid_generate(cid);
bin_to_hex((const char*)cid, sizeof(uuid_t), client->client_id, 32);
bin_to_hex((const unsigned char*)cid, sizeof(uuid_t), client->client_id, 32);
client_send_job(client, true);
}
@ -2133,10 +2135,10 @@ client_on_submit(json_object *message, client_t *client)
bt = job->block_template;
char *btb = bt->blocktemplate_blob;
size_t bin_size = strlen(btb) >> 1;
char *block = calloc(bin_size, sizeof(char));
unsigned char *block = calloc(bin_size, sizeof(char));
hex_to_bin(bt->blocktemplate_blob, bin_size << 1, block, bin_size);
char *p = block;
unsigned char *p = block;
uint32_t pool_nonce = 0;
uint32_t worker_nonce = 0;
@ -2200,7 +2202,7 @@ client_on_submit(json_object *message, client_t *client)
/* Get hashong blob */
size_t hashing_blob_size;
char *hashing_blob = NULL;
unsigned char *hashing_blob = NULL;
if (get_hashing_blob(block, bin_size,
&hashing_blob, &hashing_blob_size) != 0)
{
@ -2213,12 +2215,12 @@ client_on_submit(json_object *message, client_t *client)
}
/* Hash and compare */
char result_hash[32];
char submitted_hash[32];
unsigned char result_hash[32] = {0};
unsigned char submitted_hash[32] = {0};
uint8_t major_version = (uint8_t)block[0];
const int cn_variant = major_version >= 7 ? major_version - 6 : 0;
get_hash(hashing_blob, hashing_blob_size,
(char**)&result_hash, cn_variant, bt->height);
(unsigned char**)&result_hash, cn_variant, bt->height);
hex_to_bin(result_hex, 64, submitted_hash, 32);
if (memcmp(submitted_hash, result_hash, 32) != 0)

View file

@ -58,12 +58,12 @@ is_hex_string(const char *str)
void
hex_to_bin(const char *hex, const size_t hex_len,
char *bin, const size_t bin_size)
unsigned char *bin, const size_t bin_size)
{
assert(hex_len % 2 == 0);
assert(bin_size >= hex_len >> 1);
const char *ph = hex;
char *end = bin + bin_size;
unsigned char *end = bin + bin_size;
while (*ph && bin < end)
{
sscanf(ph, "%2hhx", bin++);
@ -72,13 +72,13 @@ hex_to_bin(const char *hex, const size_t hex_len,
}
void
bin_to_hex(const char *bin, const size_t bin_size,
bin_to_hex(const unsigned char *bin, const size_t bin_size,
char *hex, const size_t hex_size)
{
assert(bin_size << 1 == hex_size);
const char *hex_chars = "0123456789abcdef";
char *ph = hex;
const char *pb = bin;
const unsigned char *pb = bin;
for (size_t i=0; i<bin_size; i++)
{
*ph++ = hex_chars[(*pb >> 4) & 0xF];
@ -87,11 +87,11 @@ bin_to_hex(const char *bin, const size_t bin_size,
}
void
reverse_bin(char *bin, const size_t len)
reverse_bin(unsigned char *bin, const size_t len)
{
size_t start = 0;
size_t end = len-1;
char temp;
unsigned char temp;
while (start < end)
{
temp = bin[start];
@ -102,7 +102,8 @@ reverse_bin(char *bin, const size_t len)
}
}
char *stecpy(char *dst, const char *src, const char *end)
char *
stecpy(char *dst, const char *src, const char *end)
{
while (*src && dst < end)
*dst++ = *src++;

View file

@ -37,10 +37,10 @@ developers.
int is_hex_string(const char *str);
void hex_to_bin(const char *hex, const size_t hex_len,
char *bin, const size_t bin_size);
void bin_to_hex(const char *bin, size_t bin_size, char *hex,
unsigned char *bin, const size_t bin_size);
void bin_to_hex(const unsigned char *bin, size_t bin_size, char *hex,
const size_t hex_size);
void reverse_bin(char *bin, const size_t len);
void reverse_bin(unsigned char *bin, const size_t len);
char *stecpy(char *dst, const char *src, const char *end);
#endif

View file

@ -56,11 +56,11 @@ using namespace epee::string_tools;
using namespace cryptonote;
using namespace crypto;
int get_hashing_blob(const char *input, const size_t in_size,
char **output, size_t *out_size)
int get_hashing_blob(const unsigned char *input, const size_t in_size,
unsigned char **output, size_t *out_size)
{
block b = AUTO_VAL_INIT(b);
blobdata bd = std::string(input, in_size);
blobdata bd = std::string((const char*)input, in_size);
if (!parse_and_validate_block_from_blob(bd, b))
{
return XMR_PARSE_ERROR;
@ -68,12 +68,13 @@ int get_hashing_blob(const char *input, const size_t in_size,
blobdata blob = get_block_hashing_blob(b);
*out_size = blob.length();
*output = (char*) malloc(*out_size);
*output = (unsigned char*) malloc(*out_size);
memcpy(*output, blob.data(), *out_size);
return XMR_NO_ERROR;
}
int parse_address(const char *input, uint64_t *prefix, char *pub_spend)
int parse_address(const char *input, uint64_t *prefix,
unsigned char *pub_spend)
{
uint64_t tag;
std::string decoded;
@ -92,15 +93,16 @@ int parse_address(const char *input, uint64_t *prefix, char *pub_spend)
return rv ? XMR_NO_ERROR : XMR_PARSE_ERROR;
}
void get_hash(const char *input, const size_t in_size,
char **output, int variant, uint64_t height)
void get_hash(const unsigned char *input, const size_t in_size,
unsigned char **output, int variant, uint64_t height)
{
cn_slow_hash(input, in_size,
reinterpret_cast<hash&>(*output), variant, height);
}
int validate_block_from_blob(const char *blob_hex, const char *sec_view,
const char *pub_spend)
int validate_block_from_blob(const char *blob_hex,
const unsigned char *sec_view,
const unsigned char *pub_spend)
{
/*
The only validation needed is that the data parses to a block and the

View file

@ -50,13 +50,15 @@ enum xmr_error
XMR_MISMATCH_ERROR = -6
};
int get_hashing_blob(const char *input, const size_t in_size,
char **output, size_t *out_size);
int parse_address(const char *input, uint64_t *prefix, char *pub_spend);
void get_hash(const char *input, const size_t in_size,
char **output, int variant, uint64_t height);
int validate_block_from_blob(const char *blob_hex, const char *sec_view,
const char *pub_spend);
int get_hashing_blob(const unsigned char *input, const size_t in_size,
unsigned char **output, size_t *out_size);
int parse_address(const char *input, uint64_t *prefix,
unsigned char *pub_spend);
void get_hash(const unsigned char *input, const size_t in_size,
unsigned char **output, int variant, uint64_t height);
int validate_block_from_blob(const char *blob_hex,
const unsigned char *sec_view,
const unsigned char *pub_spend);
#ifdef __cplusplus
}