mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-29 19:22:28 +01:00
SetHex is fragile, because it accepts any non-hex input or any length of input, without error feedback. This can lead to issues when the input is truncated or otherwise corrupted. Document the problem by renaming the method. In the future, the fragile method should be removed from the public interface. -BEGIN VERIFY SCRIPT- sed -i 's/SetHex/SetHexDeprecated/g' $( git grep -l SetHex ./src ) -END VERIFY SCRIPT-
127 lines
3.9 KiB
C++
127 lines
3.9 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2022 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_UINT256_H
|
|
#define BITCOIN_UINT256_H
|
|
|
|
#include <crypto/common.h>
|
|
#include <span.h>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <stdint.h>
|
|
#include <string>
|
|
|
|
/** Template base class for fixed-sized opaque blobs. */
|
|
template<unsigned int BITS>
|
|
class base_blob
|
|
{
|
|
protected:
|
|
static constexpr int WIDTH = BITS / 8;
|
|
static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
|
|
std::array<uint8_t, WIDTH> m_data;
|
|
static_assert(WIDTH == sizeof(m_data), "Sanity check");
|
|
|
|
public:
|
|
/* construct 0 value by default */
|
|
constexpr base_blob() : m_data() {}
|
|
|
|
/* constructor for constants between 1 and 255 */
|
|
constexpr explicit base_blob(uint8_t v) : m_data{v} {}
|
|
|
|
constexpr explicit base_blob(Span<const unsigned char> vch)
|
|
{
|
|
assert(vch.size() == WIDTH);
|
|
std::copy(vch.begin(), vch.end(), m_data.begin());
|
|
}
|
|
|
|
constexpr bool IsNull() const
|
|
{
|
|
return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
|
|
return val == 0;
|
|
});
|
|
}
|
|
|
|
constexpr void SetNull()
|
|
{
|
|
std::fill(m_data.begin(), m_data.end(), 0);
|
|
}
|
|
|
|
constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
|
|
|
|
friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
|
|
friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
|
|
friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
|
|
|
|
// Hex string representations are little-endian.
|
|
std::string GetHex() const;
|
|
void SetHexDeprecated(std::string_view str);
|
|
std::string ToString() const;
|
|
|
|
constexpr const unsigned char* data() const { return m_data.data(); }
|
|
constexpr unsigned char* data() { return m_data.data(); }
|
|
|
|
constexpr unsigned char* begin() { return m_data.data(); }
|
|
constexpr unsigned char* end() { return m_data.data() + WIDTH; }
|
|
|
|
constexpr const unsigned char* begin() const { return m_data.data(); }
|
|
constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
|
|
|
|
static constexpr unsigned int size() { return WIDTH; }
|
|
|
|
constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
|
|
|
|
template<typename Stream>
|
|
void Serialize(Stream& s) const
|
|
{
|
|
s << Span(m_data);
|
|
}
|
|
|
|
template<typename Stream>
|
|
void Unserialize(Stream& s)
|
|
{
|
|
s.read(MakeWritableByteSpan(m_data));
|
|
}
|
|
};
|
|
|
|
/** 160-bit opaque blob.
|
|
* @note This type is called uint160 for historical reasons only. It is an opaque
|
|
* blob of 160 bits and has no integer operations.
|
|
*/
|
|
class uint160 : public base_blob<160> {
|
|
public:
|
|
constexpr uint160() = default;
|
|
constexpr explicit uint160(Span<const unsigned char> vch) : base_blob<160>(vch) {}
|
|
};
|
|
|
|
/** 256-bit opaque blob.
|
|
* @note This type is called uint256 for historical reasons only. It is an
|
|
* opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
|
|
* those are required.
|
|
*/
|
|
class uint256 : public base_blob<256> {
|
|
public:
|
|
constexpr uint256() = default;
|
|
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
|
|
constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
|
|
static const uint256 ZERO;
|
|
static const uint256 ONE;
|
|
};
|
|
|
|
/* uint256 from std::string_view, treated as little-endian.
|
|
* This is not a uint256 constructor because of historical fears of uint256(0)
|
|
* resolving to a NULL string and crashing.
|
|
*/
|
|
inline uint256 uint256S(std::string_view str)
|
|
{
|
|
uint256 rv;
|
|
rv.SetHexDeprecated(str);
|
|
return rv;
|
|
}
|
|
|
|
#endif // BITCOIN_UINT256_H
|