/* XMRig * Copyright 2010 Jeff Garzik * Copyright 2012-2014 pooler * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include "net/Job.h" static inline unsigned char hf_hex2bin(char c, bool &err) { if (c >= '0' && c <= '9') { return c - '0'; } else if (c >= 'a' && c <= 'f') { return c - 'a' + 0xA; } else if (c >= 'A' && c <= 'F') { return c - 'A' + 0xA; } err = true; return 0; } static inline char hf_bin2hex(unsigned char c) { if (c <= 0x9) { return '0' + c; } return 'a' - 0xA + c; } Job::Job() : m_nicehash(false), m_coin(), m_algo(xmrig::ALGO_CRYPTONIGHT), m_poolId(-2), m_threadId(-1), m_variant(xmrig::VARIANT_AUTO), m_size(0), m_diff(0), m_target(0), m_blob() { } Job::Job(int poolId, bool nicehash, int algo, int variant) : m_nicehash(nicehash), m_coin(), m_algo(algo), m_poolId(poolId), m_threadId(-1), m_variant(variant), m_size(0), m_diff(0), m_target(0), m_blob() { } Job::~Job() { } bool Job::setBlob(const char *blob) { if (!blob) { return false; } m_size = strlen(blob); if (m_size % 2 != 0) { return false; } m_size /= 2; if (m_size < 76 || m_size >= sizeof(m_blob)) { return false; } if (!fromHex(blob, (int) m_size * 2, m_blob)) { return false; } if (*nonce() != 0 && !m_nicehash) { m_nicehash = true; } return true; } bool Job::setTarget(const char *target) { if (!target) { return false; } const size_t len = strlen(target); if (len <= 8) { uint32_t tmp = 0; char str[8]; memcpy(str, target, len); if (!fromHex(str, 8, reinterpret_cast(&tmp)) || tmp == 0) { return false; } m_target = 0xFFFFFFFFFFFFFFFFULL / (0xFFFFFFFFULL / static_cast(tmp)); } else if (len <= 16) { m_target = 0; char str[16]; memcpy(str, target, len); if (!fromHex(str, 16, reinterpret_cast(&m_target)) || m_target == 0) { return false; } } else { return false; } m_diff = toDiff(m_target); return true; } void Job::setCoin(const char *coin) { if (!coin || strlen(coin) > 4) { memset(m_coin, 0, sizeof(m_coin)); return; } strncpy(m_coin, coin, sizeof(m_coin)); m_algo = strcmp(m_coin, "AEON") == 0 ? xmrig::ALGO_CRYPTONIGHT_LITE : xmrig::ALGO_CRYPTONIGHT; } void Job::setVariant(int variant) { switch (variant) { case xmrig::VARIANT_AUTO: case xmrig::VARIANT_NONE: case xmrig::VARIANT_V1: m_variant = variant; break; default: break; } } bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; for (unsigned int i = 0; i < len; i += 2) { out[i / 2] = (hf_hex2bin(in[i], error) << 4) | hf_hex2bin(in[i + 1], error); if (error) { return false; } } return true; } void Job::toHex(const unsigned char* in, unsigned int len, char* out) { for (unsigned int i = 0; i < len; i++) { out[i * 2] = hf_bin2hex((in[i] & 0xF0) >> 4); out[i * 2 + 1] = hf_bin2hex(in[i] & 0x0F); } } bool Job::operator==(const Job &other) const { return m_id == other.m_id && memcmp(m_blob, other.m_blob, sizeof(m_blob)) == 0; } bool Job::operator!=(const Job &other) const { return m_id != other.m_id || memcmp(m_blob, other.m_blob, sizeof(m_blob)) != 0; }