forked from hush/hush3
Compare commits
16 Commits
Author | SHA1 | Date |
---|---|---|
zanzibar | 1b76850a03 | 3 years ago |
zanzibar | 24d67abbdf | 3 years ago |
zanzibar | cc89d61640 | 3 years ago |
zanzibar | 34486fb42e | 3 years ago |
zanzibar | 43d10a60d3 | 3 years ago |
zanzibar | a495ef8223 | 3 years ago |
zanzibar | 91979495bc | 3 years ago |
zanzibar | 6b418e7466 | 3 years ago |
zanzibar | f86641caf2 | 3 years ago |
zanzibar | b0fa47c450 | 3 years ago |
Duke Leto | d4fe6c81de | 3 years ago |
Duke Leto | 924ec37c33 | 3 years ago |
Duke Leto | 74cca15807 | 3 years ago |
Duke Leto | 728a755ae2 | 3 years ago |
Duke Leto | bb130f7efe | 3 years ago |
Duke Leto | baa4e92e02 | 3 years ago |
19 changed files with 3558 additions and 338 deletions
File diff suppressed because it is too large
@ -0,0 +1,535 @@ |
|||
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
|||
// Copyright (c) 2016-2020 The Hush developers
|
|||
// Distributed under the GPLv3 software license, see the accompanying
|
|||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
|||
|
|||
#ifndef BITCOIN_NETADDRESS_H |
|||
#define BITCOIN_NETADDRESS_H |
|||
|
|||
#if defined(HAVE_CONFIG_H) |
|||
#include <config/bitcoin-config.h> |
|||
#endif |
|||
|
|||
#include "span.h" |
|||
#include <compat.h> |
|||
#include <prevector.h> |
|||
#include <serialize.h> |
|||
#include <tinyformat.h> |
|||
|
|||
#include <array> |
|||
#include <cstdint> |
|||
#include <ios> |
|||
#include <string> |
|||
#include <vector> |
|||
|
|||
/**
|
|||
* A flag that is ORed into the protocol version to designate that addresses |
|||
* should be serialized in (unserialized from) v2 format (HIP155). |
|||
* Make sure that this does not collide with any of the values in `version.h` |
|||
* or with `SERIALIZE_TRANSACTION_NO_WITNESS`. |
|||
*/ |
|||
static constexpr int ADDRV2_FORMAT = 0x20000000; |
|||
|
|||
/**
|
|||
* A network type. |
|||
* @note An address may belong to more than one network, for example `10.0.0.1` |
|||
* belongs to both `NET_UNROUTABLE` and `NET_IPV4`. |
|||
* Keep these sequential starting from 0 and `NET_MAX` as the last entry. |
|||
* We have loops like `for (int i = 0; i < NET_MAX; i++)` that expect to iterate |
|||
* over all enum values and also `GetExtNetwork()` "extends" this enum by |
|||
* introducing standalone constants starting from `NET_MAX`. |
|||
*/ |
|||
enum Network |
|||
{ |
|||
/// Addresses from these networks are not publicly routable on the global Internet.
|
|||
NET_UNROUTABLE = 0, |
|||
|
|||
/// IPv4
|
|||
NET_IPV4, |
|||
|
|||
/// IPv6
|
|||
NET_IPV6, |
|||
|
|||
/// TOR (v2 or v3)
|
|||
NET_ONION, |
|||
|
|||
/// I2P
|
|||
NET_I2P, |
|||
|
|||
/// CJDNS
|
|||
NET_CJDNS, |
|||
|
|||
/// A set of addresses that represent the hash of a string or FQDN. We use
|
|||
/// them in CAddrMan to keep track of which DNS seeds were used.
|
|||
NET_INTERNAL, |
|||
|
|||
/// Dummy value to indicate the number of NET_* constants.
|
|||
NET_MAX, |
|||
}; |
|||
|
|||
/// Prefix of an IPv6 address when it contains an embedded IPv4 address.
|
|||
/// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
|
|||
static const std::array<uint8_t, 12> IPV4_IN_IPV6_PREFIX{ |
|||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF |
|||
}; |
|||
|
|||
/// Prefix of an IPv6 address when it contains an embedded TORv2 address.
|
|||
/// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
|
|||
/// Such dummy IPv6 addresses are guaranteed to not be publicly routable as they
|
|||
/// fall under RFC4193's fc00::/7 subnet allocated to unique-local addresses.
|
|||
static const std::array<uint8_t, 6> TORV2_IN_IPV6_PREFIX{ |
|||
0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43 |
|||
}; |
|||
|
|||
/// Prefix of an IPv6 address when it contains an embedded "internal" address.
|
|||
/// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
|
|||
/// The prefix comes from 0xFD + SHA256("bitcoin")[0:5].
|
|||
/// Such dummy IPv6 addresses are guaranteed to not be publicly routable as they
|
|||
/// fall under RFC4193's fc00::/7 subnet allocated to unique-local addresses.
|
|||
static const std::array<uint8_t, 6> INTERNAL_IN_IPV6_PREFIX{ |
|||
0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 // 0xFD + sha256("bitcoin")[0:5].
|
|||
}; |
|||
|
|||
/// Size of IPv4 address (in bytes).
|
|||
static constexpr size_t ADDR_IPV4_SIZE = 4; |
|||
|
|||
/// Size of IPv6 address (in bytes).
|
|||
static constexpr size_t ADDR_IPV6_SIZE = 16; |
|||
|
|||
/// Size of TORv2 address (in bytes).
|
|||
static constexpr size_t ADDR_TORV2_SIZE = 10; |
|||
|
|||
/// Size of TORv3 address (in bytes). This is the length of just the address
|
|||
/// as used in BIP155, without the checksum and the version byte.
|
|||
static constexpr size_t ADDR_TORV3_SIZE = 32; |
|||
|
|||
/// Size of I2P address (in bytes).
|
|||
static constexpr size_t ADDR_I2P_SIZE = 32; |
|||
|
|||
/// Size of CJDNS address (in bytes).
|
|||
static constexpr size_t ADDR_CJDNS_SIZE = 16; |
|||
|
|||
/// Size of "internal" (NET_INTERNAL) address (in bytes).
|
|||
static constexpr size_t ADDR_INTERNAL_SIZE = 10; |
|||
|
|||
/**
|
|||
* Network address. |
|||
*/ |
|||
class CNetAddr |
|||
{ |
|||
protected: |
|||
/**
|
|||
* Raw representation of the network address. |
|||
* In network byte order (big endian) for IPv4 and IPv6. |
|||
*/ |
|||
prevector<ADDR_IPV6_SIZE, uint8_t> m_addr{ADDR_IPV6_SIZE, 0x0}; |
|||
|
|||
/**
|
|||
* Network to which this address belongs. |
|||
*/ |
|||
Network m_net{NET_IPV6}; |
|||
|
|||
/**
|
|||
* Scope id if scoped/link-local IPV6 address. |
|||
* See https://tools.ietf.org/html/rfc4007
|
|||
*/ |
|||
uint32_t m_scope_id{0}; |
|||
|
|||
public: |
|||
CNetAddr(); |
|||
explicit CNetAddr(const struct in_addr& ipv4Addr); |
|||
void SetIP(const CNetAddr& ip); |
|||
|
|||
/**
|
|||
* Set from a legacy IPv6 address. |
|||
* Legacy IPv6 address may be a normal IPv6 address, or another address |
|||
* (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy |
|||
* `addr` encoding. |
|||
*/ |
|||
void SetLegacyIPv6(Span<const uint8_t> ipv6); |
|||
|
|||
bool SetInternal(const std::string& name); |
|||
|
|||
bool SetSpecial(const std::string &strName); // for Tor addresses
|
|||
bool IsBindAny() const; // INADDR_ANY equivalent
|
|||
bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
|
|||
bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
|
|||
bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
|
|||
bool IsRFC2544() const; // IPv4 inter-network communications (198.18.0.0/15)
|
|||
bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
|
|||
bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
|
|||
bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
|
|||
bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
|
|||
bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
|
|||
bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
|
|||
bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
|
|||
bool IsRFC4843() const; // IPv6 ORCHID (deprecated) (2001:10::/28)
|
|||
bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
|
|||
bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
|
|||
bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
|
|||
bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
|
|||
bool IsHeNet() const; // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
|
|||
bool IsTor() const; |
|||
bool IsI2P() const; |
|||
bool IsCJDNS() const; |
|||
bool IsLocal() const; |
|||
bool IsRoutable() const; |
|||
bool IsInternal() const; |
|||
bool IsValid() const; |
|||
|
|||
/**
|
|||
* Check if the current object can be serialized in pre-ADDRv2/BIP155 format. |
|||
*/ |
|||
bool IsAddrV1Compatible() const; |
|||
|
|||
enum Network GetNetwork() const; |
|||
std::string ToString() const; |
|||
std::string ToStringIP() const; |
|||
uint64_t GetHash() const; |
|||
bool GetInAddr(struct in_addr* pipv4Addr) const; |
|||
Network GetNetClass() const; |
|||
|
|||
//! For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32.
|
|||
uint32_t GetLinkedIPv4() const; |
|||
//! Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
|
|||
bool HasLinkedIPv4() const; |
|||
|
|||
// The AS on the BGP path to the node we use to diversify
|
|||
// peers in AddrMan bucketing based on the AS infrastructure.
|
|||
// The ip->AS mapping depends on how asmap is constructed.
|
|||
uint32_t GetMappedAS(const std::vector<bool> &asmap) const; |
|||
|
|||
std::vector<unsigned char> GetGroup(const std::vector<bool> &asmap) const; |
|||
std::vector<unsigned char> GetAddrBytes() const; |
|||
int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const; |
|||
|
|||
explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0); |
|||
bool GetIn6Addr(struct in6_addr* pipv6Addr) const; |
|||
|
|||
friend bool operator==(const CNetAddr& a, const CNetAddr& b); |
|||
friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); } |
|||
friend bool operator<(const CNetAddr& a, const CNetAddr& b); |
|||
|
|||
/**
|
|||
* Whether this address should be relayed to other peers even if we can't reach it ourselves. |
|||
*/ |
|||
bool IsRelayable() const |
|||
{ |
|||
return IsIPv4() || IsIPv6() || IsTor(); |
|||
} |
|||
|
|||
/**
|
|||
* Serialize to a stream. |
|||
*/ |
|||
template <typename Stream> |
|||
void Serialize(Stream& s) const |
|||
{ |
|||
if (s.GetVersion() & ADDRV2_FORMAT) { |
|||
SerializeV2Stream(s); |
|||
} else { |
|||
SerializeV1Stream(s); |
|||
} |
|||
} |
|||
|
|||
/**
|
|||
* Unserialize from a stream. |
|||
*/ |
|||
template <typename Stream> |
|||
void Unserialize(Stream& s) |
|||
{ |
|||
if (s.GetVersion() & ADDRV2_FORMAT) { |
|||
UnserializeV2Stream(s); |
|||
} else { |
|||
UnserializeV1Stream(s); |
|||
} |
|||
} |
|||
|
|||
friend class CSubNet; |
|||
|
|||
private: |
|||
/**
|
|||
* BIP155 network ids recognized by this software. |
|||
*/ |
|||
enum BIP155Network : uint8_t { |
|||
IPV4 = 1, |
|||
IPV6 = 2, |
|||
TORV2 = 3, |
|||
TORV3 = 4, |
|||
I2P = 5, |
|||
CJDNS = 6, |
|||
}; |
|||
|
|||
/**
|
|||
* Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes). |
|||
*/ |
|||
static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE; |
|||
|
|||
/**
|
|||
* Maximum size of an address as defined in BIP155 (in bytes). |
|||
* This is only the size of the address, not the entire CNetAddr object |
|||
* when serialized. |
|||
*/ |
|||
static constexpr size_t MAX_ADDRV2_SIZE = 512; |
|||
|
|||
/**
|
|||
* Get the BIP155 network id of this address. |
|||
* Must not be called for IsInternal() objects. |
|||
* @returns BIP155 network id |
|||
*/ |
|||
BIP155Network GetBIP155Network() const; |
|||
|
|||
/**
|
|||
* Set `m_net` from the provided BIP155 network id and size after validation. |
|||
* @retval true the network was recognized, is valid and `m_net` was set |
|||
* @retval false not recognised (from future?) and should be silently ignored |
|||
* @throws std::ios_base::failure if the network is one of the BIP155 founding |
|||
* networks (id 1..6) with wrong address size. |
|||
*/ |
|||
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size); |
|||
|
|||
/**
|
|||
* Serialize in pre-ADDRv2/BIP155 format to an array. |
|||
*/ |
|||
void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const |
|||
{ |
|||
size_t prefix_size; |
|||
|
|||
switch (m_net) { |
|||
case NET_IPV6: |
|||
assert(m_addr.size() == sizeof(arr)); |
|||
memcpy(arr, &m_addr, m_addr.size()); |
|||
return; |
|||
case NET_IPV4: |
|||
prefix_size = sizeof(IPV4_IN_IPV6_PREFIX); |
|||
assert(prefix_size + m_addr.size() == sizeof(arr)); |
|||
memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size); |
|||
memcpy(arr + prefix_size, &m_addr, m_addr.size()); |
|||
return; |
|||
case NET_ONION: |
|||
if (m_addr.size() == ADDR_TORV3_SIZE) { |
|||
break; |
|||
} |
|||
prefix_size = sizeof(TORV2_IN_IPV6_PREFIX); |
|||
assert(prefix_size + m_addr.size() == sizeof(arr)); |
|||
memcpy(arr, TORV2_IN_IPV6_PREFIX.data(), prefix_size); |
|||
memcpy(arr + prefix_size, &m_addr, m_addr.size()); |
|||
return; |
|||
case NET_INTERNAL: |
|||
prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX); |
|||
assert(prefix_size + m_addr.size() == sizeof(arr)); |
|||
memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size); |
|||
memcpy(arr + prefix_size, &m_addr, m_addr.size()); |
|||
return; |
|||
case NET_I2P: |
|||
break; |
|||
case NET_CJDNS: |
|||
break; |
|||
case NET_UNROUTABLE: |
|||
case NET_MAX: |
|||
assert(false); |
|||
} // no default case, so the compiler can warn about missing cases
|
|||
|
|||
// Serialize TORv3, I2P and CJDNS as all-zeros.
|
|||
memset(arr, 0x0, V1_SERIALIZATION_SIZE); |
|||
} |
|||
|
|||
/**
|
|||
* Serialize in pre-ADDRv2/BIP155 format to a stream. |
|||
*/ |
|||
template <typename Stream> |
|||
void SerializeV1Stream(Stream& s) const |
|||
{ |
|||
uint8_t serialized[V1_SERIALIZATION_SIZE]; |
|||
|
|||
SerializeV1Array(serialized); |
|||
|
|||
s << serialized; |
|||
} |
|||
|
|||
/**
|
|||
* Serialize as ADDRv2 / BIP155. |
|||
*/ |
|||
template <typename Stream> |
|||
void SerializeV2Stream(Stream& s) const |
|||
{ |
|||
if (IsInternal()) { |
|||
// Serialize NET_INTERNAL as embedded in IPv6. We need to
|
|||
// serialize such addresses from addrman.
|
|||
s << static_cast<uint8_t>(BIP155Network::IPV6); |
|||
s << COMPACTSIZE(ADDR_IPV6_SIZE); |
|||
SerializeV1Stream(s); |
|||
return; |
|||
} |
|||
|
|||
s << static_cast<uint8_t>(GetBIP155Network()); |
|||
s << m_addr; |
|||
} |
|||
|
|||
/**
|
|||
* Unserialize from a pre-ADDRv2/BIP155 format from an array. |
|||
*/ |
|||
void UnserializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) |
|||
{ |
|||
// Use SetLegacyIPv6() so that m_net is set correctly. For example
|
|||
// ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4).
|
|||
SetLegacyIPv6(arr); |
|||
} |
|||
|
|||
/**
|
|||
* Unserialize from a pre-ADDRv2/BIP155 format from a stream. |
|||
*/ |
|||
template <typename Stream> |
|||
void UnserializeV1Stream(Stream& s) |
|||
{ |
|||
uint8_t serialized[V1_SERIALIZATION_SIZE]; |
|||
|
|||
s >> serialized; |
|||
|
|||
UnserializeV1Array(serialized); |
|||
} |
|||
|
|||
/**
|
|||
* Unserialize from a ADDRv2 / BIP155 format. |
|||
*/ |
|||
template <typename Stream> |
|||
void UnserializeV2Stream(Stream& s) |
|||
{ |
|||
uint8_t bip155_net; |
|||
s >> bip155_net; |
|||
|
|||
size_t address_size; |
|||
s >> COMPACTSIZE(address_size); |
|||
|
|||
if (address_size > MAX_ADDRV2_SIZE) { |
|||
throw std::ios_base::failure(strprintf( |
|||
"Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE)); |
|||
} |
|||
|
|||
m_scope_id = 0; |
|||
|
|||
if (SetNetFromBIP155Network(bip155_net, address_size)) { |
|||
m_addr.resize(address_size); |
|||
s >> MakeSpan(m_addr); |
|||
|
|||
if (m_net != NET_IPV6) { |
|||
return; |
|||
} |
|||
|
|||
// Do some special checks on IPv6 addresses.
|
|||
// Recognize NET_INTERNAL embedded in IPv6, such addresses are not
|
|||
// gossiped but could be coming from addrman, when unserializing from
|
|||
// disk.
|
|||
if (HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) { |
|||
m_net = NET_INTERNAL; |
|||
memmove(&m_addr, &m_addr + INTERNAL_IN_IPV6_PREFIX.size(), |
|||
ADDR_INTERNAL_SIZE); |
|||
m_addr.resize(ADDR_INTERNAL_SIZE); |
|||
return; |
|||
} |
|||
|
|||
if (!HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) && |
|||
!HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) { |
|||
return; |
|||
} |
|||
|
|||
// IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
|
|||
// encoding). Unserialize as !IsValid(), thus ignoring them.
|
|||
} else { |
|||
// If we receive an unknown BIP155 network id (from the future?) then
|
|||
// ignore the address - unserialize as !IsValid().
|
|||
s.ignore(address_size); |
|||
} |
|||
|
|||
// Mimic a default-constructed CNetAddr object which is !IsValid() and thus
|
|||
// will not be gossiped, but continue reading next addresses from the stream.
|
|||
m_net = NET_IPV6; |
|||
m_addr.assign(ADDR_IPV6_SIZE, 0x0); |
|||
} |
|||
}; |
|||
|
|||
class CSubNet |
|||
{ |
|||
protected: |
|||
/// Network (base) address
|
|||
CNetAddr network; |
|||
/// Netmask, in network byte order
|
|||
uint8_t netmask[16]; |
|||
/// Is this value valid? (only used to signal parse errors)
|
|||
bool valid; |
|||
|
|||
bool SanityCheck() const; |
|||
|
|||
public: |
|||
CSubNet(); |
|||
CSubNet(const CNetAddr& addr, uint8_t mask); |
|||
CSubNet(const CNetAddr& addr, const CNetAddr& mask); |
|||
|
|||
//constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
|
|||
explicit CSubNet(const CNetAddr& addr); |
|||
|
|||
bool Match(const CNetAddr &addr) const; |
|||
|
|||
std::string ToString() const; |
|||
bool IsValid() const; |
|||
|
|||
friend bool operator==(const CSubNet& a, const CSubNet& b); |
|||
friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); } |
|||
friend bool operator<(const CSubNet& a, const CSubNet& b); |
|||
|
|||
ADD_SERIALIZE_METHODS; |
|||
template <typename Stream, typename Operation> |
|||
inline void SerializationOp(Stream& s, Operation ser_action) { |
|||
READWRITE(network); |
|||
if (network.IsIPv4()) { |
|||
// Before commit 102867c587f5f7954232fb8ed8e85cda78bb4d32, CSubNet used the last 4 bytes of netmask
|
|||
// to store the relevant bytes for an IPv4 mask. For compatibility reasons, keep doing so in
|
|||
// serialized form.
|
|||
unsigned char dummy[12] = {0}; |
|||
READWRITE(dummy); |
|||
READWRITE(MakeSpan(netmask).first(4)); |
|||
} else { |
|||
READWRITE(netmask); |
|||
} |
|||
READWRITE(valid); |
|||
// Mark invalid if the result doesn't pass sanity checking.
|
|||
//SER_READ(obj, if (obj.valid) obj.valid = obj.SanityCheck());
|
|||
} |
|||
}; |
|||
|
|||
/** A combination of a network address (CNetAddr) and a (TCP) port */ |
|||
class CService : public CNetAddr |
|||
{ |
|||
protected: |
|||
uint16_t port; // host order
|
|||
|
|||
public: |
|||
CService(); |
|||
CService(const CNetAddr& ip, uint16_t port); |
|||
CService(const struct in_addr& ipv4Addr, uint16_t port); |
|||
explicit CService(const struct sockaddr_in& addr); |
|||
uint16_t GetPort() const; |
|||
bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const; |
|||
bool SetSockAddr(const struct sockaddr* paddr); |
|||
friend bool operator==(const CService& a, const CService& b); |
|||
friend bool operator!=(const CService& a, const CService& b) { return !(a == b); } |
|||
friend bool operator<(const CService& a, const CService& b); |
|||
std::vector<unsigned char> GetKey() const; |
|||
std::string ToString() const; |
|||
std::string ToStringPort() const; |
|||
std::string ToStringIPPort() const; |
|||
|
|||
CService(const struct in6_addr& ipv6Addr, uint16_t port); |
|||
explicit CService(const struct sockaddr_in6& addr); |
|||
|
|||
ADD_SERIALIZE_METHODS; |
|||
template <typename Stream, typename Operation> |
|||
inline void SerializationOp(Stream& s, Operation ser_action) { |
|||
//READWRITE(CNetAddr, obj);
|
|||
READWRITE(*(CNetAddr*)this); |
|||
READWRITE(Using<BigEndianFormatter<2>>(port)); |
|||
} |
|||
}; |
|||
|
|||
bool SanityCheckASMap(const std::vector<bool>& asmap); |
|||
|
|||
#endif // BITCOIN_NETADDRESS_H
|
@ -0,0 +1,252 @@ |
|||
// Copyright (c) 2018-2020 The Bitcoin Core developers
|
|||
// Copyright (c) 2016-2021 The Hush developers
|
|||
// Distributed under the GPLv3 software license, see the accompanying
|
|||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
|||
|
|||
#ifndef BITCOIN_SPAN_H |
|||
#define BITCOIN_SPAN_H |
|||
|
|||
#include <type_traits> |
|||
#include <cstddef> |
|||
#include <algorithm> |
|||
#include <assert.h> |
|||
|
|||
#ifdef DEBUG |
|||
#define CONSTEXPR_IF_NOT_DEBUG |
|||
#define ASSERT_IF_DEBUG(x) assert((x)) |
|||
#else |
|||
#define CONSTEXPR_IF_NOT_DEBUG constexpr |
|||
#define ASSERT_IF_DEBUG(x) |
|||
#endif |
|||
|
|||
#if defined(__clang__) |
|||
#if __has_attribute(lifetimebound) |
|||
#define SPAN_ATTR_LIFETIMEBOUND [[clang::lifetimebound]] |
|||
#else |
|||
#define SPAN_ATTR_LIFETIMEBOUND |
|||
#endif |
|||
#else |
|||
#define SPAN_ATTR_LIFETIMEBOUND |
|||
#endif |
|||
|
|||
/** A Span is an object that can refer to a contiguous sequence of objects.
|
|||
* |
|||
* It implements a subset of C++20's std::span. |
|||
* |
|||
* Things to be aware of when writing code that deals with Spans: |
|||
* |
|||
* - Similar to references themselves, Spans are subject to reference lifetime |
|||
* issues. The user is responsible for making sure the objects pointed to by |
|||
* a Span live as long as the Span is used. For example: |
|||
* |
|||
* std::vector<int> vec{1,2,3,4}; |
|||
* Span<int> sp(vec); |
|||
* vec.push_back(5); |
|||
* printf("%i\n", sp.front()); // UB!
|
|||
* |
|||
* may exhibit undefined behavior, as increasing the size of a vector may |
|||
* invalidate references. |
|||
* |
|||
* - One particular pitfall is that Spans can be constructed from temporaries, |
|||
* but this is unsafe when the Span is stored in a variable, outliving the |
|||
* temporary. For example, this will compile, but exhibits undefined behavior: |
|||
* |
|||
* Span<const int> sp(std::vector<int>{1, 2, 3}); |
|||
* printf("%i\n", sp.front()); // UB!
|
|||
* |
|||
* The lifetime of the vector ends when the statement it is created in ends. |
|||
* Thus the Span is left with a dangling reference, and using it is undefined. |
|||
* |
|||
* - Due to Span's automatic creation from range-like objects (arrays, and data |
|||
* types that expose a data() and size() member function), functions that |
|||
* accept a Span as input parameter can be called with any compatible |
|||
* range-like object. For example, this works: |
|||
* |
|||
* void Foo(Span<const int> arg); |
|||
* |
|||
* Foo(std::vector<int>{1, 2, 3}); // Works
|
|||
* |
|||
* This is very useful in cases where a function truly does not care about the |
|||
* container, and only about having exactly a range of elements. However it |
|||
* may also be surprising to see automatic conversions in this case. |
|||
* |
|||
* When a function accepts a Span with a mutable element type, it will not |
|||
* accept temporaries; only variables or other references. For example: |
|||
* |
|||
* void FooMut(Span<int> arg); |
|||
* |
|||
* FooMut(std::vector<int>{1, 2, 3}); // Does not compile
|
|||
* std::vector<int> baz{1, 2, 3}; |
|||
* FooMut(baz); // Works
|
|||
* |
|||
* This is similar to how functions that take (non-const) lvalue references |
|||
* as input cannot accept temporaries. This does not work either: |
|||
* |
|||
* void FooVec(std::vector<int>& arg); |
|||
* FooVec(std::vector<int>{1, 2, 3}); // Does not compile
|
|||
* |
|||
* The idea is that if a function accepts a mutable reference, a meaningful |
|||
* result will be present in that variable after the call. Passing a temporary |
|||
* is useless in that context. |
|||
*/ |
|||
template<typename C> |
|||
class Span |
|||
{ |
|||
C* m_data; |
|||
std::size_t m_size; |
|||
|
|||
template <class T> |
|||
struct is_Span_int : public std::false_type {}; |
|||
template <class T> |
|||
struct is_Span_int<Span<T>> : public std::true_type {}; |
|||
template <class T> |
|||
struct is_Span : public is_Span_int<typename std::remove_cv<T>::type>{}; |
|||
|
|||
|
|||
public: |
|||
constexpr Span() noexcept : m_data(nullptr), m_size(0) {} |
|||
|
|||
/** Construct a span from a begin pointer and a size.
|
|||
* |
|||
* This implements a subset of the iterator-based std::span constructor in C++20, |
|||
* which is hard to implement without std::address_of. |
|||
*/ |
|||
template <typename T, typename std::enable_if<std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0> |
|||
constexpr Span(T* begin, std::size_t size) noexcept : m_data(begin), m_size(size) {} |
|||
|
|||
/** Construct a span from a begin and end pointer.
|
|||
* |
|||
* This implements a subset of the iterator-based std::span constructor in C++20, |
|||
* which is hard to implement without std::address_of. |
|||
*/ |
|||
template <typename T, typename std::enable_if<std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0> |
|||
CONSTEXPR_IF_NOT_DEBUG Span(T* begin, T* end) noexcept : m_data(begin), m_size(end - begin) |
|||
{ |
|||
ASSERT_IF_DEBUG(end >= begin); |
|||
} |
|||
|
|||
/** Implicit conversion of spans between compatible types.
|
|||
* |
|||
* Specifically, if a pointer to an array of type O can be implicitly converted to a pointer to an array of type |
|||
* C, then permit implicit conversion of Span<O> to Span<C>. This matches the behavior of the corresponding |
|||
* C++20 std::span constructor. |
|||
* |
|||
* For example this means that a Span<T> can be converted into a Span<const T>. |
|||
*/ |
|||
template <typename O, typename std::enable_if<std::is_convertible<O (*)[], C (*)[]>::value, int>::type = 0> |
|||
constexpr Span(const Span<O>& other) noexcept : m_data(other.m_data), m_size(other.m_size) {} |
|||
|
|||
/** Default copy constructor. */ |
|||
constexpr Span(const Span&) noexcept = default; |
|||
|
|||
/** Default assignment operator. */ |
|||
Span& operator=(const Span& other) noexcept = default; |
|||
|
|||
/** Construct a Span from an array. This matches the corresponding C++20 std::span constructor. */ |
|||
template <int N> |
|||
constexpr Span(C (&a)[N]) noexcept : m_data(a), m_size(N) {} |
|||
|
|||
/** Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector, ...).
|
|||
* |
|||
* This implements a subset of the functionality provided by the C++20 std::span range-based constructor. |
|||
* |
|||
* To prevent surprises, only Spans for constant value types are supported when passing in temporaries. |
|||
* Note that this restriction does not exist when converting arrays or other Spans (see above). |
|||
*/ |
|||
template <typename V> |
|||
constexpr Span(V& other SPAN_ATTR_LIFETIMEBOUND, |
|||
typename std::enable_if<!is_Span<V>::value && |
|||
std::is_convertible<typename std::remove_pointer<decltype(std::declval<V&>().data())>::type (*)[], C (*)[]>::value && |
|||
std::is_convertible<decltype(std::declval<V&>().size()), std::size_t>::value, std::nullptr_t>::type = nullptr) |
|||
: m_data(other.data()), m_size(other.size()){} |
|||
|
|||
template <typename V> |
|||
constexpr Span(const V& other SPAN_ATTR_LIFETIMEBOUND, |
|||
typename std::enable_if<!is_Span<V>::value && |
|||
std::is_convertible<typename std::remove_pointer<decltype(std::declval<const V&>().data())>::type (*)[], C (*)[]>::value && |
|||
std::is_convertible<decltype(std::declval<const V&>().size()), std::size_t>::value, std::nullptr_t>::type = nullptr) |
|||
: m_data(other.data()), m_size(other.size()){} |
|||
|
|||
constexpr C* data() const noexcept { return m_data; } |
|||
constexpr C* begin() const noexcept { return m_data; } |
|||
constexpr C* end() const noexcept { return m_data + m_size; } |
|||
CONSTEXPR_IF_NOT_DEBUG C& front() const noexcept |
|||
{ |
|||
ASSERT_IF_DEBUG(size() > 0); |
|||
return m_data[0]; |
|||
} |
|||
CONSTEXPR_IF_NOT_DEBUG C& back() const noexcept |
|||
{ |
|||
ASSERT_IF_DEBUG(size() > 0); |
|||
return m_data[m_size - 1]; |
|||
} |
|||
constexpr std::size_t size() const noexcept { return m_size; } |
|||
constexpr bool empty() const noexcept { return size() == 0; } |
|||
CONSTEXPR_IF_NOT_DEBUG C& operator[](std::size_t pos) const noexcept |
|||
{ |
|||
ASSERT_IF_DEBUG(size() > pos); |
|||
return m_data[pos]; |
|||
} |
|||
CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset) const noexcept |
|||
{ |
|||
ASSERT_IF_DEBUG(size() >= offset); |
|||
return Span<C>(m_data + offset, m_size - offset); |
|||
} |
|||
CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset, std::size_t count) const noexcept |
|||
{ |
|||
ASSERT_IF_DEBUG(size() >= offset + count); |
|||
return Span<C>(m_data + offset, count); |
|||
} |
|||
CONSTEXPR_IF_NOT_DEBUG Span<C> first(std::size_t count) const noexcept |
|||
{ |
|||
ASSERT_IF_DEBUG(size() >= count); |
|||
return Span<C>(m_data, count); |
|||
} |
|||
CONSTEXPR_IF_NOT_DEBUG Span<C> last(std::size_t count) const noexcept |
|||
{ |
|||
ASSERT_IF_DEBUG(size() >= count); |
|||
return Span<C>(m_data + m_size - count, count); |
|||
} |
|||
|
|||
friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); } |
|||
friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); } |
|||
friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); } |
|||
friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); } |
|||
friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); } |
|||
friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); } |
|||
|
|||
template <typename O> friend class Span; |
|||
}; |
|||
|
|||
// MakeSpan helps constructing a Span of the right type automatically.
|
|||
/** MakeSpan for arrays: */ |
|||
template <typename A, int N> Span<A> constexpr MakeSpan(A (&a)[N]) { return Span<A>(a, N); } |
|||
/** MakeSpan for temporaries / rvalue references, only supporting const output. */ |
|||
template <typename V> constexpr auto MakeSpan(V&& v SPAN_ATTR_LIFETIMEBOUND) -> typename std::enable_if<!std::is_lvalue_reference<V>::value, Span<const typename std::remove_pointer<decltype(v.data())>::type>>::type { return std::forward<V>(v); } |
|||
/** MakeSpan for (lvalue) references, supporting mutable output. */ |
|||
template <typename V> constexpr auto MakeSpan(V& v SPAN_ATTR_LIFETIMEBOUND) -> Span<typename std::remove_pointer<decltype(v.data())>::type> { return v; } |
|||
|
|||
/** Pop the last element off a span, and return a reference to that element. */ |
|||
template <typename T> |
|||
T& SpanPopBack(Span<T>& span) |
|||
{ |
|||
size_t size = span.size(); |
|||
ASSERT_IF_DEBUG(size > 0); |
|||
T& back = span[size - 1]; |
|||
span = Span<T>(span.data(), size - 1); |
|||
return back; |
|||
} |
|||
|
|||
// Helper functions to safely cast to unsigned char pointers.
|
|||
inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; } |
|||
inline unsigned char* UCharCast(unsigned char* c) { return c; } |
|||
inline const unsigned char* UCharCast(const char* c) { return (unsigned char*)c; } |
|||
inline const unsigned char* UCharCast(const unsigned char* c) { return c; } |
|||
|
|||
// Helper function to safely convert a Span to a Span<[const] unsigned char>.
|
|||
template <typename T> constexpr auto UCharSpanCast(Span<T> s) -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> { return {UCharCast(s.data()), s.size()}; } |
|||
|
|||
/** Like MakeSpan, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ |
|||
template <typename V> constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(MakeSpan(std::forward<V>(v)))) { return UCharSpanCast(MakeSpan(std::forward<V>(v))); } |
|||
|
|||
#endif |
@ -0,0 +1,605 @@ |
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|||
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
|||
// Distributed under the MIT software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
|
|||
#include <util/strencodings.h> |
|||
#include <util/string.h> |
|||
|
|||
#include <tinyformat.h> |
|||
|
|||
#include <algorithm> |
|||
#include <cstdlib> |
|||
#include <cstring> |
|||
#include <errno.h> |
|||
#include <limits> |
|||
|
|||
static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
|||
|
|||
static const std::string SAFE_CHARS[] = |
|||
{ |
|||
CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
|
|||
CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
|
|||
CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
|
|||
CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
|
|||
}; |
|||
|
|||
std::string SanitizeString(const std::string& str, int rule) |
|||
{ |
|||
std::string strResult; |
|||
for (std::string::size_type i = 0; i < str.size(); i++) |
|||
{ |
|||
if (SAFE_CHARS[rule].find(str[i]) != std::string::npos) |
|||
strResult.push_back(str[i]); |
|||
} |
|||
return strResult; |
|||
} |
|||
|
|||
const signed char p_util_hexdigit[256] = |
|||
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, |
|||
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
|||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }; |
|||
|
|||
signed char HexDigit(char c) |
|||
{ |
|||
return p_util_hexdigit[(unsigned char)c]; |
|||
} |
|||
|
|||
bool IsHex(const std::string& str) |
|||
{ |
|||
for(std::string::const_iterator it(str.begin()); it != str.end(); ++it) |
|||
{ |
|||
if (HexDigit(*it) < 0) |
|||
return false; |
|||
} |
|||
return (str.size() > 0) && (str.size()%2 == 0); |
|||
} |
|||
|
|||
bool IsHexNumber(const std::string& str) |
|||
{ |
|||
size_t starting_location = 0; |
|||
if (str.size() > 2 && *str.begin() == '0' && *(str.begin()+1) == 'x') { |
|||
starting_location = 2; |
|||
} |
|||
for (const char c : str.substr(starting_location)) { |
|||
if (HexDigit(c) < 0) return false; |
|||
} |
|||
// Return false for empty string or "0x".
|
|||
return (str.size() > starting_location); |
|||
} |
|||
|
|||
std::vector<unsigned char> ParseHex(const char* psz) |
|||
{ |
|||
// convert hex dump to vector
|
|||
std::vector<unsigned char> vch; |
|||
while (true) |
|||
{ |
|||
while (IsSpace(*psz)) |
|||
psz++; |
|||
signed char c = HexDigit(*psz++); |
|||
if (c == (signed char)-1) |
|||
break; |
|||
unsigned char n = (c << 4); |
|||
c = HexDigit(*psz++); |
|||
if (c == (signed char)-1) |
|||
break; |
|||
n |= c; |
|||
vch.push_back(n); |
|||
} |
|||
return vch; |
|||
} |
|||
|
|||
std::vector<unsigned char> ParseHex(const std::string& str) |
|||
{ |
|||
return ParseHex(str.c_str()); |
|||
} |
|||
|
|||
void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut) |
|||
{ |
|||
size_t colon = in.find_last_of(':'); |
|||
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
|
|||
bool fHaveColon = colon != in.npos; |
|||
bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
|
|||
bool fMultiColon = fHaveColon && (in.find_last_of(':', colon - 1) != in.npos); |
|||
if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) { |
|||
uint16_t n; |
|||
if (ParseUInt16(in.substr(colon + 1), &n)) { |
|||
in = in.substr(0, colon); |
|||
portOut = n; |
|||
} |
|||
} |
|||
if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') { |
|||
hostOut = in.substr(1, in.size() - 2); |
|||
} else { |
|||
hostOut = in; |
|||
} |
|||
} |
|||
|
|||
std::string EncodeBase64(Span<const unsigned char> input) |
|||
{ |
|||
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|||
|
|||
std::string str; |
|||
str.reserve(((input.size() + 2) / 3) * 4); |
|||
ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end()); |
|||
while (str.size() % 4) str += '='; |
|||
return str; |
|||
} |
|||
|
|||
std::string EncodeBase64(const std::string& str) |
|||
{ |
|||
return EncodeBase64(MakeUCharSpan(str)); |
|||
} |
|||
|
|||
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid) |
|||
{ |
|||
static const int decode64_table[256] = |
|||
{ |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, |
|||
-1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
|||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, |
|||
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
|||
49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
|||
}; |
|||
|
|||
const char* e = p; |
|||
std::vector<uint8_t> val; |
|||
val.reserve(strlen(p)); |
|||
while (*p != 0) { |
|||
int x = decode64_table[(unsigned char)*p]; |
|||
if (x == -1) break; |
|||
val.push_back(x); |
|||
++p; |
|||
} |
|||
|
|||
std::vector<unsigned char> ret; |
|||
ret.reserve((val.size() * 3) / 4); |
|||
bool valid = ConvertBits<6, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end()); |
|||
|
|||
const char* q = p; |
|||
while (valid && *p != 0) { |
|||
if (*p != '=') { |
|||
valid = false; |
|||
break; |
|||
} |
|||
++p; |
|||
} |
|||
valid = valid && (p - e) % 4 == 0 && p - q < 4; |
|||
if (pf_invalid) *pf_invalid = !valid; |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
std::string DecodeBase64(const std::string& str, bool* pf_invalid) |
|||
{ |
|||
if (!ValidAsCString(str)) { |
|||
if (pf_invalid) { |
|||
*pf_invalid = true; |
|||
} |
|||
return {}; |
|||
} |
|||
std::vector<unsigned char> vchRet = DecodeBase64(str.c_str(), pf_invalid); |
|||
return std::string((const char*)vchRet.data(), vchRet.size()); |
|||
} |
|||
|
|||
std::string EncodeBase32(Span<const unsigned char> input, bool pad) |
|||
{ |
|||
static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; |
|||
|
|||
std::string str; |
|||
str.reserve(((input.size() + 4) / 5) * 8); |
|||
ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end()); |
|||
if (pad) { |
|||
while (str.size() % 8) { |
|||
str += '='; |
|||
} |
|||
} |
|||
return str; |
|||
} |
|||
|
|||
std::string EncodeBase32(const std::string& str, bool pad) |
|||
{ |
|||
return EncodeBase32(MakeUCharSpan(str), pad); |
|||
} |
|||
|
|||
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid) |
|||
{ |
|||
static const int decode32_table[256] = |
|||
{ |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
|||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, |
|||
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, |
|||
23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
|||
}; |
|||
|
|||
const char* e = p; |
|||
std::vector<uint8_t> val; |
|||
val.reserve(strlen(p)); |
|||
while (*p != 0) { |
|||
int x = decode32_table[(unsigned char)*p]; |
|||
if (x == -1) break; |
|||
val.push_back(x); |
|||
++p; |
|||
} |
|||
|
|||
std::vector<unsigned char> ret; |
|||
ret.reserve((val.size() * 5) / 8); |
|||
bool valid = ConvertBits<5, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end()); |
|||
|
|||
const char* q = p; |
|||
while (valid && *p != 0) { |
|||
if (*p != '=') { |
|||
valid = false; |
|||
break; |
|||
} |
|||
++p; |
|||
} |
|||
valid = valid && (p - e) % 8 == 0 && p - q < 8; |
|||
if (pf_invalid) *pf_invalid = !valid; |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
std::string DecodeBase32(const std::string& str, bool* pf_invalid) |
|||
{ |
|||
if (!ValidAsCString(str)) { |
|||
if (pf_invalid) { |
|||
*pf_invalid = true; |
|||
} |
|||
return {}; |
|||
} |
|||
std::vector<unsigned char> vchRet = DecodeBase32(str.c_str(), pf_invalid); |
|||
return std::string((const char*)vchRet.data(), vchRet.size()); |
|||
} |
|||
|
|||
[[nodiscard]] static bool ParsePrechecks(const std::string& str) |
|||
{ |
|||
if (str.empty()) // No empty string allowed
|
|||
return false; |
|||
if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size()-1]))) // No padding allowed
|
|||
return false; |
|||
if (!ValidAsCString(str)) // No embedded NUL characters allowed
|
|||
return false; |
|||
return true; |
|||
} |
|||
|
|||
bool ParseInt32(const std::string& str, int32_t *out) |
|||
{ |
|||
if (!ParsePrechecks(str)) |
|||
return false; |
|||
char *endp = nullptr; |
|||
errno = 0; // strtol will not set errno if valid
|
|||
long int n = strtol(str.c_str(), &endp, 10); |
|||
if(out) *out = (int32_t)n; |
|||
// Note that strtol returns a *long int*, so even if strtol doesn't report an over/underflow
|
|||
// we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
|
|||
// platforms the size of these types may be different.
|
|||
return endp && *endp == 0 && !errno && |
|||
n >= std::numeric_limits<int32_t>::min() && |
|||
n <= std::numeric_limits<int32_t>::max(); |
|||
} |
|||
|
|||
bool ParseInt64(const std::string& str, int64_t *out) |
|||
{ |
|||
if (!ParsePrechecks(str)) |
|||
return false; |
|||
char *endp = nullptr; |
|||
errno = 0; // strtoll will not set errno if valid
|
|||
long long int n = strtoll(str.c_str(), &endp, 10); |
|||
if(out) *out = (int64_t)n; |
|||
// Note that strtoll returns a *long long int*, so even if strtol doesn't report an over/underflow
|
|||
// we still have to check that the returned value is within the range of an *int64_t*.
|
|||
return endp && *endp == 0 && !errno && |
|||
n >= std::numeric_limits<int64_t>::min() && |
|||
n <= std::numeric_limits<int64_t>::max(); |
|||
} |
|||
|
|||
bool ParseUInt8(const std::string& str, uint8_t *out) |
|||
{ |
|||
uint32_t u32; |
|||
if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) { |
|||
return false; |
|||
} |
|||
if (out != nullptr) { |
|||
*out = static_cast<uint8_t>(u32); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
bool ParseUInt16(const std::string& str, uint16_t* out) |
|||
{ |
|||
uint32_t u32; |
|||
if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) { |
|||
return false; |
|||
} |
|||
if (out != nullptr) { |
|||
*out = static_cast<uint16_t>(u32); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
bool ParseUInt32(const std::string& str, uint32_t *out) |
|||
{ |
|||
if (!ParsePrechecks(str)) |
|||
return false; |
|||
if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoul accepts these by default if they fit in the range
|
|||
return false; |
|||
char *endp = nullptr; |
|||
errno = 0; // strtoul will not set errno if valid
|
|||
unsigned long int n = strtoul(str.c_str(), &endp, 10); |
|||
if(out) *out = (uint32_t)n; |
|||
// Note that strtoul returns a *unsigned long int*, so even if it doesn't report an over/underflow
|
|||
// we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit
|
|||
// platforms the size of these types may be different.
|
|||
return endp && *endp == 0 && !errno && |
|||
n <= std::numeric_limits<uint32_t>::max(); |
|||
} |
|||
|
|||
bool ParseUInt64(const std::string& str, uint64_t *out) |
|||
{ |
|||
if (!ParsePrechecks(str)) |
|||
return false; |
|||
if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoull accepts these by default if they fit in the range
|
|||
return false; |
|||
char *endp = nullptr; |
|||
errno = 0; // strtoull will not set errno if valid
|
|||
unsigned long long int n = strtoull(str.c_str(), &endp, 10); |
|||
if(out) *out = (uint64_t)n; |
|||
// Note that strtoull returns a *unsigned long long int*, so even if it doesn't report an over/underflow
|
|||
// we still have to check that the returned value is within the range of an *uint64_t*.
|
|||
return endp && *endp == 0 && !errno && |
|||
n <= std::numeric_limits<uint64_t>::max(); |
|||
} |
|||
|
|||
|
|||
bool ParseDouble(const std::string& str, double *out) |
|||
{ |
|||
if (!ParsePrechecks(str)) |
|||
return false; |
|||
if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
|
|||
return false; |
|||
std::istringstream text(str); |
|||
text.imbue(std::locale::classic()); |
|||
double result; |
|||
text >> result; |
|||
if(out) *out = result; |
|||
return text.eof() && !text.fail(); |
|||
} |
|||
|
|||
std::string FormatParagraph(const std::string& in, size_t width, size_t indent) |
|||
{ |
|||
std::stringstream out; |
|||
size_t ptr = 0; |
|||
size_t indented = 0; |
|||
while (ptr < in.size()) |
|||
{ |
|||
size_t lineend = in.find_first_of('\n', ptr); |
|||
if (lineend == std::string::npos) { |
|||
lineend = in.size(); |
|||
} |
|||
const size_t linelen = lineend - ptr; |
|||
const size_t rem_width = width - indented; |
|||
if (linelen <= rem_width) { |
|||
out << in.substr(ptr, linelen + 1); |
|||
ptr = lineend + 1; |
|||
indented = 0; |
|||
} else { |
|||
size_t finalspace = in.find_last_of(" \n", ptr + rem_width); |
|||
if (finalspace == std::string::npos || finalspace < ptr) { |
|||
// No place to break; just include the entire word and move on
|
|||
finalspace = in.find_first_of("\n ", ptr); |
|||
if (finalspace == std::string::npos) { |
|||
// End of the string, just add it and break
|
|||
out << in.substr(ptr); |
|||
break; |
|||
} |
|||
} |
|||
out << in.substr(ptr, finalspace - ptr) << "\n"; |
|||
if (in[finalspace] == '\n') { |
|||
indented = 0; |
|||
} else if (indent) { |
|||
out << std::string(indent, ' '); |
|||
indented = indent; |
|||
} |
|||
ptr = finalspace + 1; |
|||
} |
|||
} |
|||
return out.str(); |
|||
} |
|||
|
|||
int64_t atoi64(const std::string& str) |
|||
{ |
|||
#ifdef _MSC_VER |
|||
return _atoi64(str.c_str()); |
|||
#else |
|||
return strtoll(str.c_str(), nullptr, 10); |
|||
#endif |
|||
} |
|||
|
|||
int atoi(const std::string& str) |
|||
{ |
|||
return atoi(str.c_str()); |
|||
} |
|||
|
|||
/** Upper bound for mantissa.
|
|||
* 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer. |
|||
* Larger integers cannot consist of arbitrary combinations of 0-9: |
|||
* |
|||
* 999999999999999999 1^18-1 |
|||
* 9223372036854775807 (1<<63)-1 (max int64_t) |
|||
* 9999999999999999999 1^19-1 (would overflow) |
|||
*/ |
|||
static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL; |
|||
|
|||
/** Helper function for ParseFixedPoint */ |
|||
static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros) |
|||
{ |
|||
if(ch == '0') |
|||
++mantissa_tzeros; |
|||
else { |
|||
for (int i=0; i<=mantissa_tzeros; ++i) { |
|||
if (mantissa > (UPPER_BOUND / 10LL)) |
|||
return false; /* overflow */ |
|||
mantissa *= 10; |
|||
} |
|||
mantissa += ch - '0'; |
|||
mantissa_tzeros = 0; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out) |
|||
{ |
|||
int64_t mantissa = 0; |
|||
int64_t exponent = 0; |
|||
int mantissa_tzeros = 0; |
|||
bool mantissa_sign = false; |
|||
bool exponent_sign = false; |
|||
int ptr = 0; |
|||
int end = val.size(); |
|||
int point_ofs = 0; |
|||
|
|||
if (ptr < end && val[ptr] == '-') { |
|||
mantissa_sign = true; |
|||
++ptr; |
|||
} |
|||
if (ptr < end) |
|||
{ |
|||
if (val[ptr] == '0') { |
|||
/* pass single 0 */ |
|||
++ptr; |
|||
} else if (val[ptr] >= '1' && val[ptr] <= '9') { |
|||
while (ptr < end && IsDigit(val[ptr])) { |
|||
if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros)) |
|||
return false; /* overflow */ |
|||
++ptr; |
|||
} |
|||
} else return false; /* missing expected digit */ |
|||
} else return false; /* empty string or loose '-' */ |
|||
if (ptr < end && val[ptr] == '.') |
|||
{ |
|||
++ptr; |
|||
if (ptr < end && IsDigit(val[ptr])) |
|||
{ |
|||
while (ptr < end && IsDigit(val[ptr])) { |
|||
if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros)) |
|||
return false; /* overflow */ |
|||
++ptr; |
|||
++point_ofs; |
|||
} |
|||
} else return false; /* missing expected digit */ |
|||
} |
|||
if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E')) |
|||
{ |
|||
++ptr; |
|||
if (ptr < end && val[ptr] == '+') |
|||
++ptr; |
|||
else if (ptr < end && val[ptr] == '-') { |
|||
exponent_sign = true; |
|||
++ptr; |
|||
} |
|||
if (ptr < end && IsDigit(val[ptr])) { |
|||
while (ptr < end && IsDigit(val[ptr])) { |
|||
if (exponent > (UPPER_BOUND / 10LL)) |
|||
return false; /* overflow */ |
|||
exponent = exponent * 10 + val[ptr] - '0'; |
|||
++ptr; |
|||
} |
|||
} else return false; /* missing expected digit */ |
|||
} |
|||
if (ptr != end) |
|||
return false; /* trailing garbage */ |
|||
|
|||
/* finalize exponent */ |
|||
if (exponent_sign) |
|||
exponent = -exponent; |
|||
exponent = exponent - point_ofs + mantissa_tzeros; |
|||
|
|||
/* finalize mantissa */ |
|||
if (mantissa_sign) |
|||
mantissa = -mantissa; |
|||
|
|||
/* convert to one 64-bit fixed-point value */ |
|||
exponent += decimals; |
|||
if (exponent < 0) |
|||
return false; /* cannot represent values smaller than 10^-decimals */ |
|||
if (exponent >= 18) |
|||
return false; /* cannot represent values larger than or equal to 10^(18-decimals) */ |
|||
|
|||
for (int i=0; i < exponent; ++i) { |
|||
if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL)) |
|||
return false; /* overflow */ |
|||
mantissa *= 10; |
|||
} |
|||
if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND) |
|||
return false; /* overflow */ |
|||
|
|||
if (amount_out) |
|||
*amount_out = mantissa; |
|||
|
|||
return true; |
|||
} |
|||
|
|||
std::string ToLower(const std::string& str) |
|||
{ |
|||
std::string r; |
|||
for (auto ch : str) r += ToLower((unsigned char)ch); |
|||
return r; |
|||
} |
|||
|
|||
std::string ToUpper(const std::string& str) |
|||
{ |
|||
std::string r; |
|||
for (auto ch : str) r += ToUpper((unsigned char)ch); |
|||
return r; |
|||
} |
|||
|
|||
std::string Capitalize(std::string str) |
|||
{ |
|||
if (str.empty()) return str; |
|||
str[0] = ToUpper(str.front()); |
|||
return str; |
|||
} |
|||
|
|||
std::string HexStr(const Span<const uint8_t> s) |
|||
{ |
|||
std::string rv; |
|||
static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', |
|||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
|||
rv.reserve(s.size() * 2); |
|||
for (uint8_t v: s) { |
|||
rv.push_back(hexmap[v >> 4]); |
|||
rv.push_back(hexmap[v & 15]); |
|||
} |
|||
return rv; |
|||
} |
@ -0,0 +1,266 @@ |
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|||
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
|||
// Copyright (c) 2016-2021 The Hush developers
|
|||
// Distributed under the GPLv3 software license, see the accompanying
|
|||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
|||
|
|||
/**
|
|||
* Utilities for converting data from/to strings. |
|||
*/ |
|||
#ifndef BITCOIN_UTIL_STRENCODINGS_H |
|||
#define BITCOIN_UTIL_STRENCODINGS_H |
|||
|
|||
#include <span.h> |
|||
#include <cstdint> |
|||
#include <iterator> |
|||
#include <string> |
|||
#include <vector> |
|||
|
|||
/** Used by SanitizeString() */ |
|||
enum SafeChars |
|||
{ |
|||
SAFE_CHARS_DEFAULT, //!< The full set of allowed chars
|
|||
SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
|
|||
SAFE_CHARS_FILENAME, //!< Chars allowed in filenames
|
|||
SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986)
|
|||
}; |
|||
|
|||
/**
|
|||
* Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email |
|||
* addresses, but avoid anything even possibly remotely dangerous like & or > |
|||
* @param[in] str The string to sanitize |
|||
* @param[in] rule The set of safe chars to choose (default: least restrictive) |
|||
* @return A new string without unsafe chars |
|||
*/ |
|||
std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT); |
|||
std::vector<unsigned char> ParseHex(const char* psz); |
|||
std::vector<unsigned char> ParseHex(const std::string& str); |
|||
signed char HexDigit(char c); |
|||
/* Returns true if each character in str is a hex character, and has an even
|
|||
* number of hex digits.*/ |
|||
bool IsHex(const std::string& str); |
|||
/**
|
|||
* Return true if the string is a hex number, optionally prefixed with "0x" |
|||
*/ |
|||
bool IsHexNumber(const std::string& str); |
|||
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr); |
|||
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr); |
|||
std::string EncodeBase64(Span<const unsigned char> input); |
|||
std::string EncodeBase64(const std::string& str); |
|||
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr); |
|||
std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr); |
|||
|
|||
/**
|
|||
* Base32 encode. |
|||
* If `pad` is true, then the output will be padded with '=' so that its length |
|||
* is a multiple of 8. |
|||
*/ |
|||
std::string EncodeBase32(Span<const unsigned char> input, bool pad = true); |
|||
|
|||
/**
|
|||
* Base32 encode. |
|||
* If `pad` is true, then the output will be padded with '=' so that its length |
|||
* is a multiple of 8. |
|||
*/ |
|||
std::string EncodeBase32(const std::string& str, bool pad = true); |
|||
|
|||
void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut); |
|||
int64_t atoi64(const std::string& str); |
|||
int atoi(const std::string& str); |
|||
|
|||
/**
|
|||
* Tests if the given character is a decimal digit. |
|||
* @param[in] c character to test |
|||
* @return true if the argument is a decimal digit; otherwise false. |
|||
*/ |
|||
constexpr bool IsDigit(char c) |
|||
{ |
|||
return c >= '0' && c <= '9'; |
|||
} |
|||
|
|||
/**
|
|||
* Tests if the given character is a whitespace character. The whitespace characters |
|||
* are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal |
|||
* tab ('\t'), and vertical tab ('\v'). |
|||
* |
|||
* This function is locale independent. Under the C locale this function gives the |
|||
* same result as std::isspace. |
|||
* |
|||
* @param[in] c character to test |
|||
* @return true if the argument is a whitespace character; otherwise false |
|||
*/ |
|||
constexpr inline bool IsSpace(char c) noexcept { |
|||
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'; |
|||
} |
|||
|
|||
/**
|
|||
* Convert string to signed 32-bit integer with strict parse error feedback. |
|||
* @returns true if the entire string could be parsed as valid integer, |
|||
* false if not the entire string could be parsed or when overflow or underflow occurred. |
|||
*/ |
|||
[[nodiscard]] bool ParseInt32(const std::string& str, int32_t *out); |
|||
|
|||
/**
|
|||
* Convert string to signed 64-bit integer with strict parse error feedback. |
|||
* @returns true if the entire string could be parsed as valid integer, |
|||
* false if not the entire string could be parsed or when overflow or underflow occurred. |
|||
*/ |
|||
[[nodiscard]] bool ParseInt64(const std::string& str, int64_t *out); |
|||
|
|||
/**
|
|||
* Convert decimal string to unsigned 8-bit integer with strict parse error feedback. |
|||
* @returns true if the entire string could be parsed as valid integer, |
|||
* false if not the entire string could be parsed or when overflow or underflow occurred. |
|||
*/ |
|||
[[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out); |
|||
|
|||
/**
|
|||
* Convert decimal string to unsigned 16-bit integer with strict parse error feedback. |
|||
* @returns true if the entire string could be parsed as valid integer, |
|||
* false if the entire string could not be parsed or if overflow or underflow occurred. |
|||
*/ |
|||
[[nodiscard]] bool ParseUInt16(const std::string& str, uint16_t* out); |
|||
|
|||
/**
|
|||
* Convert decimal string to unsigned 32-bit integer with strict parse error feedback. |
|||
* @returns true if the entire string could be parsed as valid integer, |
|||
* false if not the entire string could be parsed or when overflow or underflow occurred. |
|||
*/ |
|||
[[nodiscard]] bool ParseUInt32(const std::string& str, uint32_t *out); |
|||
|
|||
/**
|
|||
* Convert decimal string to unsigned 64-bit integer with strict parse error feedback. |
|||
* @returns true if the entire string could be parsed as valid integer, |
|||
* false if not the entire string could be parsed or when overflow or underflow occurred. |
|||
*/ |
|||
[[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out); |
|||
|
|||
/**
|
|||
* Convert string to double with strict parse error feedback. |
|||
* @returns true if the entire string could be parsed as valid double, |
|||
* false if not the entire string could be parsed or when overflow or underflow occurred. |
|||
*/ |
|||
[[nodiscard]] bool ParseDouble(const std::string& str, double *out); |
|||
|
|||
/**
|
|||
* Convert a span of bytes to a lower-case hexadecimal string. |
|||
*/ |
|||
std::string HexStr(const Span<const uint8_t> s); |
|||
inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); } |
|||
|
|||
/**
|
|||
* Format a paragraph of text to a fixed width, adding spaces for |
|||
* indentation to any added line. |
|||
*/ |
|||
std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0); |
|||
|
|||
/**
|
|||
* Timing-attack-resistant comparison. |
|||
* Takes time proportional to length |
|||
* of first argument. |
|||
*/ |
|||
template <typename T> |
|||
bool TimingResistantEqual(const T& a, const T& b) |
|||
{ |
|||
if (b.size() == 0) return a.size() == 0; |
|||
size_t accumulator = a.size() ^ b.size(); |
|||
for (size_t i = 0; i < a.size(); i++) |
|||
accumulator |= a[i] ^ b[i%b.size()]; |
|||
return accumulator == 0; |
|||
} |
|||
|
|||
/** Parse number as fixed point according to JSON number syntax.
|
|||
* See https://json.org/number.gif
|
|||
* @returns true on success, false on error. |
|||
* @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger. |
|||
*/ |
|||
[[nodiscard]] bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); |
|||
|
|||
/** Convert from one power-of-2 number base to another. */ |
|||
template<int frombits, int tobits, bool pad, typename O, typename I> |
|||
bool ConvertBits(const O& outfn, I it, I end) { |
|||
size_t acc = 0; |
|||
size_t bits = 0; |
|||
constexpr size_t maxv = (1 << tobits) - 1; |
|||
constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1; |
|||
while (it != end) { |
|||
acc = ((acc << frombits) | *it) & max_acc; |
|||
bits += frombits; |
|||
while (bits >= tobits) { |
|||
bits -= tobits; |
|||
outfn((acc >> bits) & maxv); |
|||
} |
|||
++it; |
|||
} |
|||
if (pad) { |
|||
if (bits) outfn((acc << (tobits - bits)) & maxv); |
|||
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) { |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
/**
|
|||
* Converts the given character to its lowercase equivalent. |
|||
* This function is locale independent. It only converts uppercase |
|||
* characters in the standard 7-bit ASCII range. |
|||
* This is a feature, not a limitation. |
|||
* |
|||
* @param[in] c the character to convert to lowercase. |
|||
* @return the lowercase equivalent of c; or the argument |
|||
* if no conversion is possible. |
|||
*/ |
|||
constexpr char ToLower(char c) |
|||
{ |
|||
return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c); |
|||
} |
|||
|
|||
/**
|
|||
* Returns the lowercase equivalent of the given string. |
|||
* This function is locale independent. It only converts uppercase |
|||
* characters in the standard 7-bit ASCII range. |
|||
* This is a feature, not a limitation. |
|||
* |
|||
* @param[in] str the string to convert to lowercase. |
|||
* @returns lowercased equivalent of str |
|||
*/ |
|||
std::string ToLower(const std::string& str); |
|||
|
|||
/**
|
|||
* Converts the given character to its uppercase equivalent. |
|||
* This function is locale independent. It only converts lowercase |
|||
* characters in the standard 7-bit ASCII range. |
|||
* This is a feature, not a limitation. |
|||
* |
|||
* @param[in] c the character to convert to uppercase. |
|||
* @return the uppercase equivalent of c; or the argument |
|||
* if no conversion is possible. |
|||
*/ |
|||
constexpr char ToUpper(char c) |
|||
{ |
|||
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c); |
|||
} |
|||
|
|||
/**
|
|||
* Returns the uppercase equivalent of the given string. |
|||
* This function is locale independent. It only converts lowercase |
|||
* characters in the standard 7-bit ASCII range. |
|||
* This is a feature, not a limitation. |
|||
* |
|||
* @param[in] str the string to convert to uppercase. |
|||
* @returns UPPERCASED EQUIVALENT OF str |
|||
*/ |
|||
std::string ToUpper(const std::string& str); |
|||
|
|||
/**
|
|||
* Capitalizes the first character of the given string. |
|||
* This function is locale independent. It only converts lowercase |
|||
* characters in the standard 7-bit ASCII range. |
|||
* This is a feature, not a limitation. |
|||
* |
|||
* @param[in] str the string to capitalize. |
|||
* @returns string with the first letter capitalized. |
|||
*/ |
|||
std::string Capitalize(std::string str); |
|||
|
|||
#endif // BITCOIN_UTIL_STRENCODINGS_H
|
@ -0,0 +1,5 @@ |
|||
// Copyright (c) 2019 The Bitcoin Core developers
|
|||
// Distributed under the MIT software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
|
|||
#include <util/string.h> |
@ -0,0 +1,96 @@ |
|||
// Copyright (c) 2019-2020 The Bitcoin Core developers
|
|||
// Distributed under the MIT software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
|
|||
#ifndef BITCOIN_UTIL_STRING_H |
|||
#define BITCOIN_UTIL_STRING_H |
|||
|
|||
#include <algorithm> |
|||
#include <array> |
|||
#include <cstring> |
|||
#include <locale> |
|||
#include <sstream> |
|||
#include <string> |
|||
#include <vector> |
|||
|
|||
[[nodiscard]] inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v") |
|||
{ |
|||
std::string::size_type front = str.find_first_not_of(pattern); |
|||
if (front == std::string::npos) { |
|||
return std::string(); |
|||
} |
|||
std::string::size_type end = str.find_last_not_of(pattern); |
|||
return str.substr(front, end - front + 1); |
|||
} |
|||
|
|||
[[nodiscard]] inline std::string RemovePrefix(const std::string& str, const std::string& prefix) |
|||
{ |
|||
if (str.substr(0, prefix.size()) == prefix) { |
|||
return str.substr(prefix.size()); |
|||
} |
|||
return str; |
|||
} |
|||
|
|||
/**
|
|||
* Join a list of items |
|||
* |
|||
* @param list The list to join |
|||
* @param separator The separator |
|||
* @param unary_op Apply this operator to each item in the list |
|||
*/ |
|||
template <typename T, typename BaseType, typename UnaryOp> |
|||
auto Join(const std::vector<T>& list, const BaseType& separator, UnaryOp unary_op) |
|||
-> decltype(unary_op(list.at(0))) |
|||
{ |
|||
decltype(unary_op(list.at(0))) ret; |
|||
for (size_t i = 0; i < list.size(); ++i) { |
|||
if (i > 0) ret += separator; |
|||
ret += unary_op(list.at(i)); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
template <typename T> |
|||
T Join(const std::vector<T>& list, const T& separator) |
|||
{ |
|||
return Join(list, separator, [](const T& i) { return i; }); |
|||
} |
|||
|
|||
// Explicit overload needed for c_str arguments, which would otherwise cause a substitution failure in the template above.
|
|||
inline std::string Join(const std::vector<std::string>& list, const std::string& separator) |
|||
{ |
|||
return Join<std::string>(list, separator); |
|||
} |
|||
|
|||
/**
|
|||
* Check if a string does not contain any embedded NUL (\0) characters |
|||
*/ |
|||
[[nodiscard]] inline bool ValidAsCString(const std::string& str) noexcept |
|||
{ |
|||
return str.size() == strlen(str.c_str()); |
|||
} |
|||
|
|||
/**
|
|||
* Locale-independent version of std::to_string |
|||
*/ |
|||
template <typename T> |
|||
std::string ToString(const T& t) |
|||
{ |
|||
std::ostringstream oss; |
|||
oss.imbue(std::locale::classic()); |
|||
oss << t; |
|||
return oss.str(); |
|||
} |
|||
|
|||
/**
|
|||
* Check whether a container begins with the given prefix. |
|||
*/ |
|||
template <typename T1, size_t PREFIX_LEN> |
|||
[[nodiscard]] inline bool HasPrefix(const T1& obj, |
|||
const std::array<uint8_t, PREFIX_LEN>& prefix) |
|||
{ |
|||
return obj.size() >= PREFIX_LEN && |
|||
std::equal(std::begin(prefix), std::end(prefix), std::begin(obj)); |
|||
} |
|||
|
|||
#endif // BITCOIN_UTIL_STRENCODINGS_H
|
Loading…
Reference in new issue