LCOV - code coverage report
Current view: top level - src - uint256.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 56 56 100.0 %
Date: 2020-09-26 01:30:44 Functions: 56 56 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2019 The Bitcoin Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #ifndef BITCOIN_UINT256_H
       7             : #define BITCOIN_UINT256_H
       8             : 
       9             : #include <assert.h>
      10             : #include <cstring>
      11             : #include <stdint.h>
      12             : #include <string>
      13             : #include <vector>
      14             : 
      15             : /** Template base class for fixed-sized opaque blobs. */
      16             : template<unsigned int BITS>
      17             : class base_blob
      18             : {
      19             : protected:
      20             :     static constexpr int WIDTH = BITS / 8;
      21             :     uint8_t m_data[WIDTH];
      22             : public:
      23   684586277 :     base_blob()
      24             :     {
      25   684586277 :         memset(m_data, 0, sizeof(m_data));
      26   684586277 :     }
      27             : 
      28             :     explicit base_blob(const std::vector<unsigned char>& vch);
      29             : 
      30    18653151 :     bool IsNull() const
      31             :     {
      32    97070011 :         for (int i = 0; i < WIDTH; i++)
      33    94603990 :             if (m_data[i] != 0)
      34    16187268 :                 return false;
      35     2465908 :         return true;
      36    18653177 :     }
      37             : 
      38     3592482 :     void SetNull()
      39             :     {
      40     3592482 :         memset(m_data, 0, sizeof(m_data));
      41     3592482 :     }
      42             : 
      43  1649257542 :     inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); }
      44             : 
      45   103452965 :     friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
      46      977419 :     friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
      47  1302358991 :     friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
      48             : 
      49             :     std::string GetHex() const;
      50             :     void SetHex(const char* psz);
      51             :     void SetHex(const std::string& str);
      52             :     std::string ToString() const;
      53             : 
      54      720745 :     const unsigned char* data() const { return m_data; }
      55     4498213 :     unsigned char* data() { return m_data; }
      56             : 
      57    36791290 :     unsigned char* begin()
      58             :     {
      59    36791290 :         return &m_data[0];
      60             :     }
      61             : 
      62       11607 :     unsigned char* end()
      63             :     {
      64       11607 :         return &m_data[WIDTH];
      65             :     }
      66             : 
      67    44786736 :     const unsigned char* begin() const
      68             :     {
      69    44786736 :         return &m_data[0];
      70             :     }
      71             : 
      72     1113287 :     const unsigned char* end() const
      73             :     {
      74     1113287 :         return &m_data[WIDTH];
      75             :     }
      76             : 
      77     5386524 :     unsigned int size() const
      78             :     {
      79     5386524 :         return sizeof(m_data);
      80             :     }
      81             : 
      82   541176247 :     uint64_t GetUint64(int pos) const
      83             :     {
      84   541176247 :         const uint8_t* ptr = m_data + pos * 8;
      85  1623528741 :         return ((uint64_t)ptr[0]) | \
      86  1082352494 :                ((uint64_t)ptr[1]) << 8 | \
      87  1082352494 :                ((uint64_t)ptr[2]) << 16 | \
      88  1082352494 :                ((uint64_t)ptr[3]) << 24 | \
      89  1082352494 :                ((uint64_t)ptr[4]) << 32 | \
      90  1082352494 :                ((uint64_t)ptr[5]) << 40 | \
      91  1082352494 :                ((uint64_t)ptr[6]) << 48 | \
      92   541176247 :                ((uint64_t)ptr[7]) << 56;
      93             :     }
      94             : 
      95             :     template<typename Stream>
      96    44288896 :     void Serialize(Stream& s) const
      97             :     {
      98    44288896 :         s.write((char*)m_data, sizeof(m_data));
      99    44288896 :     }
     100             : 
     101             :     template<typename Stream>
     102     1497188 :     void Unserialize(Stream& s)
     103             :     {
     104     1497188 :         s.read((char*)m_data, sizeof(m_data));
     105     1497188 :     }
     106             : };
     107             : 
     108             : /** 160-bit opaque blob.
     109             :  * @note This type is called uint160 for historical reasons only. It is an opaque
     110             :  * blob of 160 bits and has no integer operations.
     111             :  */
     112             : class uint160 : public base_blob<160> {
     113             : public:
     114    12889793 :     uint160() {}
     115     4599152 :     explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
     116             : };
     117             : 
     118             : /** 256-bit opaque blob.
     119             :  * @note This type is called uint256 for historical reasons only. It is an
     120             :  * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
     121             :  * those are required.
     122             :  */
     123             : class uint256 : public base_blob<256> {
     124             : public:
     125  1353320033 :     uint256() {}
     126        4602 :     explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
     127             : };
     128             : 
     129             : /* uint256 from const char *.
     130             :  * This is a separate function because the constructor uint256(const char*) can result
     131             :  * in dangerously catching uint256(0).
     132             :  */
     133       52696 : inline uint256 uint256S(const char *str)
     134             : {
     135       52696 :     uint256 rv;
     136       52696 :     rv.SetHex(str);
     137       52696 :     return rv;
     138             : }
     139             : /* uint256 from std::string.
     140             :  * This is a separate function because the constructor uint256(const std::string &str) can result
     141             :  * in dangerously catching uint256(0) via std::string(const char*).
     142             :  */
     143       20571 : inline uint256 uint256S(const std::string& str)
     144             : {
     145       20571 :     uint256 rv;
     146       20571 :     rv.SetHex(str);
     147       20571 :     return rv;
     148             : }
     149             : 
     150             : uint256& UINT256_ONE();
     151             : 
     152             : #endif // BITCOIN_UINT256_H

Generated by: LCOV version 1.15