From: CryptoManiac Date: Sat, 12 Jul 2014 14:22:17 +0000 (+0400) Subject: Add octet stream operations X-Git-Tag: v0.4.4.6-nvc-update8~2 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=958ec2682a633d5af527eb87abb4f06d48e9d33c Add octet stream operations --- diff --git a/src/bignum.h b/src/bignum.h index 6b2bada..c9bde89 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -241,6 +241,49 @@ public: BN_mpi2bn(pch, p - pch, this); } + void setuint160(uint160 n) + { + unsigned char pch[sizeof(n) + 6]; + unsigned char* p = pch + 4; + bool fLeadingZeroes = true; + unsigned char* pbegin = (unsigned char*)&n; + unsigned char* psrc = pbegin + sizeof(n); + while (psrc != pbegin) + { + unsigned char c = *(--psrc); + if (fLeadingZeroes) + { + if (c == 0) + continue; + if (c & 0x80) + *p++ = 0; + fLeadingZeroes = false; + } + *p++ = c; + } + unsigned int nSize = p - (pch + 4); + pch[0] = (nSize >> 24) & 0xff; + pch[1] = (nSize >> 16) & 0xff; + pch[2] = (nSize >> 8) & 0xff; + pch[3] = (nSize >> 0) & 0xff; + BN_mpi2bn(pch, p - pch, this); + } + + uint160 getuint160() const + { + unsigned int nSize = BN_bn2mpi(this, NULL); + if (nSize < 4) + return 0; + std::vector vch(nSize); + BN_bn2mpi(this, &vch[0]); + if (vch.size() > 4) + vch[4] &= 0x7f; + uint160 n = 0; + for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) + ((unsigned char*)&n)[i] = vch[j]; + return n; + } + void setuint256(uint256 n) { unsigned char pch[sizeof(n) + 6]; @@ -284,6 +327,24 @@ public: return n; } + void setBytes(const std::vector& vchBytes) + { + BN_bin2bn(&vchBytes[0], vchBytes.size(), this); + } + + std::vector getBytes() const + { + int nBytes = BN_num_bytes(this); + + std::vector vchBytes(nBytes); + + int n = BN_bn2bin(this, &vchBytes[0]); + if (n != nBytes) { + throw bignum_error("CBigNum::getBytes : BN_bn2bin failed"); + } + + return vchBytes; + } void setvch(const std::vector& vch) { diff --git a/src/uint256.h b/src/uint256.h index 66e9b91..b08923c 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -354,6 +354,11 @@ public: return (unsigned char*)&pn[WIDTH]; } + std::vector getBytes() + { + return std::vector(begin(), end()); + } + unsigned int size() { return sizeof(pn);