diff --git a/src/base58.h b/src/base58.h --- a/src/base58.h +++ b/src/base58.h @@ -134,7 +134,7 @@ if (vchData.size() == Size) { // If base58 encoded data does not hold an ext key, return a // !IsValid() key - ret.Decode(&vchData[0]); + ret.Decode(vchData.data()); } return ret; } diff --git a/src/base58.cpp b/src/base58.cpp --- a/src/base58.cpp +++ b/src/base58.cpp @@ -122,7 +122,7 @@ } std::string EncodeBase58(const std::vector &vch) { - return EncodeBase58(&vch[0], &vch[0] + vch.size()); + return EncodeBase58(vch.data(), vch.data() + vch.size()); } bool DecodeBase58(const std::string &str, std::vector &vchRet) { @@ -166,7 +166,7 @@ vchVersion = vchVersionIn; vchData.resize(nSize); if (!vchData.empty()) { - memcpy(&vchData[0], pdata, nSize); + memcpy(vchData.data(), pdata, nSize); } } @@ -186,9 +186,9 @@ vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes); vchData.resize(vchTemp.size() - nVersionBytes); if (!vchData.empty()) { - memcpy(&vchData[0], &vchTemp[nVersionBytes], vchData.size()); + memcpy(vchData.data(), vchTemp.data() + nVersionBytes, vchData.size()); } - memory_cleanse(&vchTemp[0], vchTemp.size()); + memory_cleanse(vchTemp.data(), vchTemp.size()); return true; } diff --git a/src/compressor.cpp b/src/compressor.cpp --- a/src/compressor.cpp +++ b/src/compressor.cpp @@ -91,7 +91,7 @@ script[0] = OP_DUP; script[1] = OP_HASH160; script[2] = 20; - memcpy(&script[3], &in[0], 20); + memcpy(&script[3], in.data(), 20); script[23] = OP_EQUALVERIFY; script[24] = OP_CHECKSIG; return true; @@ -99,7 +99,7 @@ script.resize(23); script[0] = OP_HASH160; script[1] = 20; - memcpy(&script[2], &in[0], 20); + memcpy(&script[2], in.data(), 20); script[22] = OP_EQUAL; return true; case 0x02: @@ -107,14 +107,14 @@ script.resize(35); script[0] = 33; script[1] = nSize; - memcpy(&script[2], &in[0], 32); + memcpy(&script[2], in.data(), 32); script[34] = OP_CHECKSIG; return true; case 0x04: case 0x05: uint8_t vch[33] = {}; vch[0] = nSize - 2; - memcpy(&vch[1], &in[0], 32); + memcpy(&vch[1], in.data(), 32); CPubKey pubkey(&vch[0], &vch[33]); if (!pubkey.Decompress()) return false; assert(pubkey.size() == 65); diff --git a/src/hash.cpp b/src/hash.cpp --- a/src/hash.cpp +++ b/src/hash.cpp @@ -17,48 +17,46 @@ // The following is MurmurHash3 (x86_32), see // http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp uint32_t h1 = nHashSeed; - if (vDataToHash.size() > 0) { - const uint32_t c1 = 0xcc9e2d51; - const uint32_t c2 = 0x1b873593; + const uint32_t c1 = 0xcc9e2d51; + const uint32_t c2 = 0x1b873593; - const int nblocks = vDataToHash.size() / 4; + const int nblocks = vDataToHash.size() / 4; - //---------- - // body - const uint8_t *blocks = &vDataToHash[0] + nblocks * 4; + //---------- + // body + const uint8_t *blocks = vDataToHash.data(); - for (int i = -nblocks; i; i++) { - uint32_t k1 = ReadLE32(blocks + i * 4); + for (int i = 0; i < nblocks; ++i) { + uint32_t k1 = ReadLE32(blocks + i * 4); - k1 *= c1; - k1 = ROTL32(k1, 15); - k1 *= c2; + k1 *= c1; + k1 = ROTL32(k1, 15); + k1 *= c2; - h1 ^= k1; - h1 = ROTL32(h1, 13); - h1 = h1 * 5 + 0xe6546b64; - } + h1 ^= k1; + h1 = ROTL32(h1, 13); + h1 = h1 * 5 + 0xe6546b64; + } - //---------- - // tail - const uint8_t *tail = (const uint8_t *)(&vDataToHash[0] + nblocks * 4); + //---------- + // tail + const uint8_t *tail = vDataToHash.data() + nblocks * 4; - uint32_t k1 = 0; + uint32_t k1 = 0; - switch (vDataToHash.size() & 3) { - case 3: - k1 ^= tail[2] << 16; - // FALLTHROUGH - case 2: - k1 ^= tail[1] << 8; - // FALLTHROUGH - case 1: - k1 ^= tail[0]; - k1 *= c1; - k1 = ROTL32(k1, 15); - k1 *= c2; - h1 ^= k1; - } + switch (vDataToHash.size() & 3) { + case 3: + k1 ^= tail[2] << 16; + // FALLTHROUGH + case 2: + k1 ^= tail[1] << 8; + // FALLTHROUGH + case 1: + k1 ^= tail[0]; + k1 *= c1; + k1 = ROTL32(k1, 15); + k1 *= c2; + h1 ^= k1; } //---------- diff --git a/src/key.cpp b/src/key.cpp --- a/src/key.cpp +++ b/src/key.cpp @@ -163,7 +163,7 @@ privkey.resize(279); privkeylen = 279; ret = ec_privkey_export_der( - secp256k1_context_sign, (uint8_t *)&privkey[0], &privkeylen, begin(), + secp256k1_context_sign, (uint8_t *)privkey.data(), &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); assert(ret); privkey.resize(privkeylen); @@ -201,7 +201,7 @@ test_case ? extra_entropy : nullptr); assert(ret); secp256k1_ecdsa_signature_serialize_der( - secp256k1_context_sign, (uint8_t *)&vchSig[0], &nSigLen, &sig); + secp256k1_context_sign, (uint8_t *)vchSig.data(), &nSigLen, &sig); vchSig.resize(nSigLen); return true; } @@ -262,7 +262,7 @@ bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck = false) { if (!ec_privkey_import_der(secp256k1_context_sign, (uint8_t *)begin(), - &privkey[0], privkey.size())) + privkey.data(), privkey.size())) return false; fCompressed = vchPubKey.IsCompressed(); fValid = true; @@ -311,8 +311,8 @@ CHMAC_SHA512(hashkey, sizeof(hashkey)) .Write(seed, nSeedLen) .Finalize(vout.data()); - key.Set(&vout[0], &vout[32], true); - memcpy(chaincode.begin(), &vout[32], 32); + key.Set(vout.data(), vout.data() + 32, true); + memcpy(chaincode.begin(), vout.data() + 32, 32); nDepth = 0; nChild = 0; memset(vchFingerprint, 0, sizeof(vchFingerprint)); diff --git a/src/netaddress.cpp b/src/netaddress.cpp --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -561,7 +561,7 @@ std::vector CService::GetKey() const { std::vector vKey; vKey.resize(18); - memcpy(&vKey[0], ip, 16); + memcpy(vKey.data(), ip, 16); vKey[16] = port / 0x100; vKey[17] = port & 0x0FF; return vKey; diff --git a/src/pubkey.cpp b/src/pubkey.cpp --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -179,11 +179,8 @@ &(*this)[0], size())) { return false; } - if (vchSig.size() == 0) { - return false; - } if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, - &vchSig[0], vchSig.size())) { + vchSig.data(), vchSig.size())) { return false; } /** @@ -325,7 +322,7 @@ const boost::sliced_range> &vchSig) { secp256k1_ecdsa_signature sig; if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, - &vchSig[0], vchSig.size())) { + &vchSig.front(), vchSig.size())) { return false; } return (!secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, diff --git a/src/serialize.h b/src/serialize.h --- a/src/serialize.h +++ b/src/serialize.h @@ -477,14 +477,14 @@ } string.resize(size); if (size != 0) { - s.read((char *)&string[0], size); + s.read((char *)string.data(), size); } } template void Serialize(Stream &s) const { WriteCompactSize(s, string.size()); if (!string.empty()) { - s.write((char *)&string[0], string.size()); + s.write((char *)string.data(), string.size()); } } }; @@ -602,7 +602,7 @@ void Serialize(Stream &os, const std::basic_string &str) { WriteCompactSize(os, str.size()); if (!str.empty()) { - os.write((char *)&str[0], str.size() * sizeof(str[0])); + os.write((char *)str.data(), str.size() * sizeof(C)); } } @@ -611,7 +611,7 @@ size_t nSize = ReadCompactSize(is); str.resize(nSize); if (nSize != 0) { - is.read((char *)&str[0], nSize * sizeof(str[0])); + is.read((char *)str.data(), nSize * sizeof(C)); } } @@ -622,7 +622,7 @@ void Serialize_impl(Stream &os, const prevector &v, const uint8_t &) { WriteCompactSize(os, v.size()); if (!v.empty()) { - os.write((char *)&v[0], v.size() * sizeof(T)); + os.write((char *)v.data(), v.size() * sizeof(T)); } } @@ -683,7 +683,7 @@ void Serialize_impl(Stream &os, const std::vector &v, const uint8_t &) { WriteCompactSize(os, v.size()); if (!v.empty()) { - os.write((char *)&v[0], v.size() * sizeof(T)); + os.write((char *)v.data(), v.size() * sizeof(T)); } } diff --git a/src/streams.h b/src/streams.h --- a/src/streams.h +++ b/src/streams.h @@ -425,7 +425,9 @@ template void Serialize(Stream &s) const { // Special case: stream << stream concatenates like stream += stream - if (!vch.empty()) s.write((char *)&vch[0], vch.size() * sizeof(vch[0])); + if (!vch.empty()) { + s.write((char *)vch.data(), vch.size() * sizeof(value_type)); + } } template CDataStream &operator<<(const T &obj) { diff --git a/src/utilstrencodings.cpp b/src/utilstrencodings.cpp --- a/src/utilstrencodings.cpp +++ b/src/utilstrencodings.cpp @@ -270,9 +270,7 @@ std::string DecodeBase64(const std::string &str) { std::vector vchRet = DecodeBase64(str.c_str()); - return (vchRet.size() == 0) - ? std::string() - : std::string((const char *)&vchRet[0], vchRet.size()); + return std::string((const char *)vchRet.data(), vchRet.size()); } std::string EncodeBase32(const uint8_t *pch, size_t len) { @@ -461,9 +459,7 @@ std::string DecodeBase32(const std::string &str) { std::vector vchRet = DecodeBase32(str.c_str()); - return (vchRet.size() == 0) - ? std::string() - : std::string((const char *)&vchRet[0], vchRet.size()); + return std::string((const char *)vchRet.data(), vchRet.size()); } static bool ParsePrechecks(const std::string &str) {