static QByteArray parsePercentEncoding(const QByteArray &in) { QByteArray out; for(int n = 0; n < in.size(); ++n) { char c = in[n]; if(c == '%') { if(n + 2 >= in.size()) return QByteArray(); int hi = fromHex(in[n + 1]); if(hi == -1) return QByteArray(); int lo = fromHex(in[n + 2]); if(lo == -1) return QByteArray(); unsigned char val = (hi << 4) + lo; out += val; n += 2; // adjust position } else out += c; } return out; }
byte * GetCBOREncoding(const cn_cbor * pControl, int * pcbEncoded) { const cn_cbor * pOutputs = cn_cbor_mapget_string(pControl, "output"); const cn_cbor * pCBOR; byte * pb = NULL; const byte * pb2; int i; if ((pOutputs == NULL) || (pOutputs->type != CN_CBOR_MAP)) { fprintf(stderr, "Invalid output\n"); exit(1); } pCBOR = cn_cbor_mapget_string(pOutputs, "cbor"); if ((pCBOR == NULL) || (pCBOR->type != CN_CBOR_TEXT)) { fprintf(stderr, "Invalid cbor object"); exit(1); } pb = malloc(pCBOR->length / 2); pb2 = pCBOR->v.bytes; for (i = 0; i < pCBOR->length; i += 2) { pb[i / 2] = fromHex(pb2[i]) * 16 + fromHex(pb2[i + 1]); } *pcbEncoded = (int) (pCBOR->length / 2); return pb; }
std::string CEncrypt::urlDecode(std::string strVal) { const char *pszVal = strVal.c_str(); std::string strTemp = ""; size_t iLens = strVal.size(); for (size_t i = 0; i < iLens; i++) { if ('+' == pszVal[i]) { strTemp += ' '; } else if ('%' == pszVal[i]) { assert(i + 2 < iLens); unsigned char cHigh = fromHex((unsigned char)pszVal[++i]); unsigned char cLow = fromHex((unsigned char)pszVal[++i]); strTemp += cHigh*16 + cLow; } else { strTemp += pszVal[i]; } } return strTemp; }
static bool check( bool ok, const uint8_t *rmdHex, const uint8_t *dataHex ) { uint8_t rmd[kRIPEMD160ByteSize]; fromHex(rmd, rmdHex, kRIPEMD160ByteSize, false); uint8_t data[1024]; auto dataLen = strlen((const char*)dataHex)/2; if(0<dataLen) { fromHex(data, dataHex, dataLen, false); } uint8_t cmp[kRIPEMD160ByteSize]; rmd160(cmp, data, dataLen); TEST_CHECK( ok, 0==memcmp(cmp, rmd, sizeof(rmd)), "RIPEMD-160 fails for %s\n", dataHex ); return ok; }
static void *unpackData(const char *p) { char *res = (char*)malloc(strlen(p) / 2); char *t = res; for (; p[0] && p[1]; p += 2) *(t++) = (fromHex(p[0]) << 4) + fromHex(p[1]); return res; }
static void readVector(char * pszVec, unsigned int cbVec, octet * pabVec) { unsigned int i = cbVec; memset(pabVec, 0, cbVec); for ( ; i && pszVec[0] && pszVec[1]; pszVec += 2, pabVec++, i--) { *pabVec = (fromHex(pszVec[0]) << 4) | fromHex(pszVec[1]); } if (!i && *pszVec) printUsage(1); }
bool Color::fromString(const string &str) { if(str.length() != 4 && str.length() != 7) return false; if(str[0] != '#') return false; for(string::size_type i = 1; i < str.length(); ++i) { if(!isxdigit((int)str[i])) return false; } if(str.length() == 7) { m_red = (__uint8)( (fromHex(str[1]) << 4) + fromHex(str[2]) ); m_green = (__uint8)( (fromHex(str[3]) << 4) + fromHex(str[4]) ); m_blue = (__uint8)( (fromHex(str[5]) << 4) + fromHex(str[6]) ); } else { __uint8 v = fromHex(str[1]); m_red = (__uint8)( (v << 4) + v ); v = fromHex(str[2]); m_green = (__uint8)( (v << 4) + v ); v = fromHex(str[3]); m_blue = (__uint8)( (v << 4) + v ); } m_alpha = 255; return true; }
byte * FromHex(const char * rgch, int cch) { byte * pb = malloc(cch / 2); const char * pb2 = rgch; int i; for (i = 0; i < cch; i += 2) { pb[i / 2] = fromHex(pb2[i]) * 16 + fromHex(pb2[i + 1]); } return pb; }
Bytes hex_decode(const String& string) { Bytes ret; ret.reserve(string.length()/2); for (auto i: range(0, (string.length()/2)*2, 2)) { if (!isHex(string[i]) || !isHex(string[i+1])) break; ret.push_back((fromHex(string[i]) << 4) | fromHex(string[i+1])); } return ret; }
void MsgEdit::setParam(unsigned long param) { switch (msg->Type()){ case ICQ_MSGxCONTACTxLIST: users->addUin(param); break; case ICQ_MSGxMSG: edit->setText(*((QString*)param)); break; case ICQ_MSGxURL: urlEdit->setText(*((QString*)param)); break; case ICQ_MSGxFILE:{ string fileName; fileName = ((QString*)param)->local8Bit(); string decodeName; for (unsigned i = 0; i < fileName.length(); i++){ char p = fileName[i]; switch (p){ case '+': decodeName += ' '; break; case '%': p = 0; if (fromHex(fileName[i+1], p) && ((p = p << 4), fromHex(fileName[i+2], p))){ if (p == 0x7C) p = ':'; decodeName += p; i += 2; }else{ decodeName += '%'; } break; default: decodeName += p; } } QString name = QString::fromLocal8Bit(decodeName.c_str()); if (name.left(5) == "file:"){ name = name.mid(5); #ifdef WIN32 name.replace(QRegExp("^/+"), ""); #else name.replace(QRegExp("^/+"), "/"); #endif } #ifdef WIN32 name.replace(QRegExp("/"), "\\"); #endif fileEdit->setText(name); break; } } }
Derived Id<n, Derived>::FromString(const String& string) { if(string.length() != n * 2) THROW(String("Wrong length of id string for ") + typeid(Derived).name()); Derived id; for(int i = 0; i < n; ++i) id.data[i] = (fromHex(string[i * 2]) << 4) | fromHex(string[i * 2 + 1]); return id; }
bool WPUtils::crypt(bool isEncrypt, const QByteArray & in, QByteArray & out) { QByteArray key, iv; QString fail; if (!fromHex(_key, key)) { fail += "Key is not valid hex. "; } if (!fromHex(_iv, iv)) { fail += "IV is not valid hex. "; } if (!fail.isEmpty()) { qDebug() << "fail = " << fail; //toast(fail); return false; } AESParams params(globalContext); if (!params.isValid()) { //toast( qDebug () << "fails = " << QString("Could not create params. %1").arg(SBError::getErrorText(params.lastError())); return false; } AESKey aesKey(params, key); if (!aesKey.isValid()) { qDebug() << "failes = " << QString("Could not create a key. %1").arg(SBError::getErrorText(aesKey.lastError())); return false; } int rc; if (isEncrypt) { rc = hu_AESEncryptMsg(params.aesParams(), aesKey.aesKey(), iv.length(), (const unsigned char*) iv.constData(), in.length(), (const unsigned char *) in.constData(), (unsigned char *) out.data(), globalContext.ctx()); } else { rc = hu_AESDecryptMsg(params.aesParams(), aesKey.aesKey(), iv.length(), (const unsigned char*) iv.constData(), in.length(), (const unsigned char *) in.constData(), (unsigned char *) out.data(), globalContext.ctx()); } if (rc == SB_SUCCESS) { return true; } //toast qDebug() << QString("Crypto operation failed. %1").arg(SBError::getErrorText(rc)); return false; }
QColor convertColorFromString(const QString &s) { if (s.length() == 9 && s.startsWith(QLatin1Char('#'))) { uchar a = fromHex(s, 1); uchar r = fromHex(s, 3); uchar g = fromHex(s, 5); uchar b = fromHex(s, 7); return QColor(r, g, b, a); } else { QColor rv(s); return rv; } }
QColor QDeclarativeStringConverters::colorFromString(const QString &s, bool *ok) { if (s.length() == 9 && s.startsWith(QLatin1Char('#'))) { uchar a = fromHex(s, 1); uchar r = fromHex(s, 3); uchar g = fromHex(s, 5); uchar b = fromHex(s, 7); if (ok) *ok = true; return QColor(r, g, b, a); } else { QColor rv(s); if (ok) *ok = rv.isValid(); return rv; } }
uint32_t ReadPrefHex(const char* pref_key) { LoadConfigFile(); std::string pref = "preferences:"; pref.append(pref_key); return fromHex(iniparser_getstring(preferences, pref.c_str(), 0)); }
void CiscoSecret7::transform(const QByteArray &input, QByteArray &output) { if (wayValue == INBOUND) { QByteArray data; cipher(seed,input, data); output = data.toHex().prepend(QByteArray::number(seed)); } else { QByteArray encrypted = input.toUpper(); if (encrypted.isEmpty()) return; if (encrypted.size() < 3) { emit error(tr("Invalid hash (far too small)"),id); return; } bool k = true; seed = encrypted.mid(0,2).toInt(&k); if (!k || seed > MAXSEED) { emit error(tr("Invalid seed, it must be an unsigned integer <= %1").arg(MAXSEED),id); return; } QByteArray data = encrypted.mid(2); data = fromHex(data); cipher(seed,data,output); } }
static bool testAddr2Hash( const uint8_t *addr, const uint8_t *hexHash ) { uint8_t result[kRIPEMD160ByteSize]; auto ok = addrToHash160(result, addr, true); TEST_CHECK(ok, ok, "failed to decode address %s", addr); uint8_t hash[kRIPEMD160ByteSize]; fromHex(hash, (const uint8_t*)hexHash, kRIPEMD160ByteSize, false); uint8_t hexResult[1 + 2*kRIPEMD160ByteSize]; toHex(hexResult, result, kRIPEMD160ByteSize, false); TEST_CHECK( ok, 0==memcmp(result, hash, kRIPEMD160ByteSize), "decode fail\n" " for addr: %s\n" " expected: %s\n" " got: %s\n" "\n", addr, hexHash, hexResult ); return ok; }
bool Currency::generateGenesisBlock() { m_genesisBlock = boost::value_initialized<Block>(); // Hard code coinbase tx in genesis block, because "tru" generating tx use random, but genesis should be always the same std::string genesisCoinbaseTxHex = GENESIS_COINBASE_TX_HEX; BinaryArray minerTxBlob; bool r = fromHex(genesisCoinbaseTxHex, minerTxBlob) && fromBinaryArray(m_genesisBlock.baseTransaction, minerTxBlob); if (!r) { logger(ERROR, BRIGHT_RED) << "failed to parse coinbase tx from hard coded blob"; return false; } m_genesisBlock.majorVersion = BLOCK_MAJOR_VERSION_1; m_genesisBlock.minorVersion = BLOCK_MINOR_VERSION_0; m_genesisBlock.timestamp = GENESIS_TIMESTAMP; m_genesisBlock.nonce = GENESIS_NONCE; if (m_testnet) { ++m_genesisBlock.nonce; } //miner::find_nonce_for_given_block(bl, 1, 0); return true; }
static void unpackQP(struct MHINFO *w) { uchar val; char c, d, *q, *r; for (q = r = w->start; q < w->end; ++q) { c = *q; if (c != '=') { *r++ = c; continue; } c = *++q; if (c == '\n') continue; d = q[1]; if (isxdigit(c) && isxdigit(d)) { d = fromHex(c, d); if (d == 0) d = ' '; *r++ = d; ++q; continue; } --q; *r++ = '='; } w->end = r; *r = 0; } /* unpackQP */
void OID::init( const std::string& s ) { verify( s.size() == 24 ); const char *p = s.c_str(); for( size_t i = 0; i < kOIDSize; i++ ) { data[i] = fromHex(p); p += 2; } }
void OID::init( string s ) { verify( s.size() == 24 ); const char *p = s.c_str(); for( int i = 0; i < 12; i++ ) { data[i] = fromHex(p); p += 2; } }
std::string hexDecode(const std::string& data) { std::string result(data.length() / 2, '-'); for (unsigned i = 0; i < result.length(); ++i) result[i] = fromHex(data[2 * i], data[2 * i + 1]); return result; }
inline std::string URIDecode(const std::string& urlIn) { std::string urlOut; for (size_t i = 0; i < urlIn.size(); i++) { BYTE ch = 0; if (urlIn[i] == '%') { ch = (fromHex(urlIn[i + 1]) << 4) | (fromHex(urlIn[i + 2])); i += 2; } else if (urlIn[i] == '+') { ch = ' '; } else { ch = urlIn[i]; } urlOut += ((char) ch); } return urlOut; }
int main(){ size_t len=0; float bestscore=0; char bestchar=0; char* bestHex=NULL; FILE* fp=fopen("./l1_04.txt","r"); if(!fp) { perror("File opening failed"); return EXIT_FAILURE; } char buf[128]; while(!feof(fp)){ fgets(buf,128,fp); unsigned char* bytes=fromHex(buf,&len); unsigned char* tmp=malloc(len); for(char i=0;i<0x7f;i++){ xorBuffWithChar(bytes,tmp,i,len); float score=getScore(tmp,len); if(score>bestscore){ bestscore=score; bestchar=i; if(bestHex) free(bestHex); bestHex=malloc(strlen(buf)); strncpy(bestHex,buf,strlen(buf)); bestHex[strlen(buf)-1]='\0'; } } free(bytes); free(tmp); } printf("Best hex was %s with char '%c', decrypted string is ",bestHex,bestchar); size_t bestlen=0; unsigned char* bestbytes = fromHex(bestHex,&bestlen); unsigned char* tmp = malloc(bestlen); xorBuffWithChar(bestbytes,tmp,bestchar,bestlen); printString(tmp,bestlen); return 0; }
void testObj::test<5>(void) { try { fromHex("aX"); fail("no error on invalid char"); } catch(const std::runtime_error&) { } }
void testObj::test<6>(void) { try { fromHex("aaf"); fail("no error on invalid number of characters"); } catch(const std::runtime_error&) { } }
ObjectId::ObjectId(const std::string& id) { poco_assert_dbg(id.size() == 24); const char* p = id.c_str(); for (std::size_t i = 0; i < 12; ++i) { _id[i] = fromHex(p); p += 2; } }
double *tpsNode(QDomNode n) { double *r = new double [3]; while(!n.isNull()) { QDomElement e = n.toElement(); // try to convert the node to an element. if(!e.isNull()) { //qDebug() << e.tagName(); if (e.tagName() == "theta") { r[rpi_device::NUM_ZERO] = fromHex(e.text().toLocal8Bit()); } if (e.tagName() == "phi") { r[1] = fromHex(e.text().toLocal8Bit()); } if (e.tagName() == "sigma") { r[2] = fromHex(e.text().toLocal8Bit()); } } n = n.nextSibling(); } return r; }
void SealEngineFace::verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent, bytesConstRef _block) const { _bi.verify(_s, _parent, _block); if (_s != CheckNothingNew) { if (_bi.difficulty() < chainParams().minimumDifficulty) BOOST_THROW_EXCEPTION( InvalidDifficulty() << RequirementError( bigint(chainParams().minimumDifficulty), bigint(_bi.difficulty()))); if (_bi.gasLimit() < chainParams().minGasLimit) BOOST_THROW_EXCEPTION(InvalidGasLimit() << RequirementError( bigint(chainParams().minGasLimit), bigint(_bi.gasLimit()))); if (_bi.gasLimit() > chainParams().maxGasLimit) BOOST_THROW_EXCEPTION(InvalidGasLimit() << RequirementError( bigint(chainParams().maxGasLimit), bigint(_bi.gasLimit()))); if (_bi.number() && _bi.extraData().size() > chainParams().maximumExtraDataSize) { BOOST_THROW_EXCEPTION( ExtraDataTooBig() << RequirementError(bigint(chainParams().maximumExtraDataSize), bigint(_bi.extraData().size())) << errinfo_extraData(_bi.extraData())); } u256 const& daoHardfork = chainParams().daoHardforkBlock; if (daoHardfork != 0 && daoHardfork + 9 >= daoHardfork && _bi.number() >= daoHardfork && _bi.number() <= daoHardfork + 9) if (_bi.extraData() != fromHex("0x64616f2d686172642d666f726b")) BOOST_THROW_EXCEPTION( ExtraDataIncorrect() << errinfo_comment("Received block from the wrong fork (invalid extradata).")); } if (_parent) { auto gasLimit = _bi.gasLimit(); auto parentGasLimit = _parent.gasLimit(); if (gasLimit < chainParams().minGasLimit || gasLimit > chainParams().maxGasLimit || gasLimit <= parentGasLimit - parentGasLimit / chainParams().gasLimitBoundDivisor || gasLimit >= parentGasLimit + parentGasLimit / chainParams().gasLimitBoundDivisor) BOOST_THROW_EXCEPTION( InvalidGasLimit() << errinfo_min( (bigint)((bigint)parentGasLimit - (bigint)(parentGasLimit / chainParams().gasLimitBoundDivisor))) << errinfo_got((bigint)gasLimit) << errinfo_max((bigint)((bigint)parentGasLimit + parentGasLimit / chainParams().gasLimitBoundDivisor))); } }
int main() { const char* string = "FF3D10"; fromHex(string); unsigned char string2[] = { 0xAB, 0x50, 0x00, 0xFF }; toHex(string2, sizeof(string2)); return 0; }