LCOV - code coverage report
Current view: top level - src - uint256.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 19 29 65.5 %
Date: 2015-10-12 22:39:14 Functions: 0 24 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2014 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 <stdexcept>
      12             : #include <stdint.h>
      13             : #include <string>
      14             : #include <vector>
      15             : 
      16             : /** Template base class for fixed-sized opaque blobs. */
      17             : template<unsigned int BITS>
      18             : class base_blob
      19             : {
      20             : protected:
      21             :     enum { WIDTH=BITS/8 };
      22             :     uint8_t data[WIDTH];
      23             : public:
      24           0 :     base_blob()
      25             :     {
      26     4139937 :         memset(data, 0, sizeof(data));
      27           0 :     }
      28             : 
      29             :     explicit base_blob(const std::vector<unsigned char>& vch);
      30             : 
      31             :     bool IsNull() const
      32             :     {
      33     6894217 :         for (int i = 0; i < WIDTH; i++)
      34     6954569 :             if (data[i] != 0)
      35             :                 return false;
      36             :         return true;
      37             :     }
      38             : 
      39             :     void SetNull()
      40             :     {
      41     2002094 :         memset(data, 0, sizeof(data));
      42             :     }
      43             : 
      44     1118488 :     friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; }
      45      459551 :     friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; }
      46    24334440 :     friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; }
      47             : 
      48             :     std::string GetHex() const;
      49             :     void SetHex(const char* psz);
      50             :     void SetHex(const std::string& str);
      51             :     std::string ToString() const;
      52             : 
      53             :     unsigned char* begin()
      54             :     {
      55         718 :         return &data[0];
      56             :     }
      57             : 
      58             :     unsigned char* end()
      59             :     {
      60             :         return &data[WIDTH];
      61             :     }
      62             : 
      63             :     const unsigned char* begin() const
      64             :     {
      65     2361786 :         return &data[0];
      66             :     }
      67             : 
      68             :     const unsigned char* end() const
      69             :     {
      70        4181 :         return &data[WIDTH];
      71             :     }
      72             : 
      73           0 :     unsigned int size() const
      74             :     {
      75           0 :         return sizeof(data);
      76             :     }
      77             : 
      78           0 :     unsigned int GetSerializeSize(int nType, int nVersion) const
      79             :     {
      80           0 :         return sizeof(data);
      81             :     }
      82             : 
      83             :     template<typename Stream>
      84           0 :     void Serialize(Stream& s, int nType, int nVersion) const
      85             :     {
      86     2132375 :         s.write((char*)data, sizeof(data));
      87           0 :     }
      88             : 
      89             :     template<typename Stream>
      90           0 :     void Unserialize(Stream& s, int nType, int nVersion)
      91             :     {
      92      627408 :         s.read((char*)data, sizeof(data));
      93           0 :     }
      94             : };
      95             : 
      96             : /** 160-bit opaque blob.
      97             :  * @note This type is called uint160 for historical reasons only. It is an opaque
      98             :  * blob of 160 bits and has no integer operations.
      99             :  */
     100             : class uint160 : public base_blob<160> {
     101             : public:
     102       98445 :     uint160() {}
     103             :     uint160(const base_blob<160>& b) : base_blob<160>(b) {}
     104       16369 :     explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
     105             : };
     106             : 
     107             : /** 256-bit opaque blob.
     108             :  * @note This type is called uint256 for historical reasons only. It is an
     109             :  * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
     110             :  * those are required.
     111             :  */
     112             : class uint256 : public base_blob<256> {
     113             : public:
     114     3879880 :     uint256() {}
     115             :     uint256(const base_blob<256>& b) : base_blob<256>(b) {}
     116        1312 :     explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
     117             : 
     118             :     /** A cheap hash function that just returns 64 bits from the result, it can be
     119             :      * used when the contents are considered uniformly random. It is not appropriate
     120             :      * when the value can easily be influenced from outside as e.g. a network adversary could
     121             :      * provide values to trigger worst-case behavior.
     122             :      * @note The result of this function is not stable between little and big endian.
     123             :      */
     124             :     uint64_t GetCheapHash() const
     125             :     {
     126             :         uint64_t result;
     127      516226 :         memcpy((void*)&result, (void*)data, 8);
     128             :         return result;
     129             :     }
     130             : 
     131             :     /** A more secure, salted hash function.
     132             :      * @note This hash is not stable between little and big endian.
     133             :      */
     134             :     uint64_t GetHash(const uint256& salt) const;
     135             : };
     136             : 
     137             : /* uint256 from const char *.
     138             :  * This is a separate function because the constructor uint256(const char*) can result
     139             :  * in dangerously catching uint256(0).
     140             :  */
     141             : inline uint256 uint256S(const char *str)
     142             : {
     143             :     uint256 rv;
     144        2753 :     rv.SetHex(str);
     145             :     return rv;
     146             : }
     147             : /* uint256 from std::string.
     148             :  * This is a separate function because the constructor uint256(const std::string &str) can result
     149             :  * in dangerously catching uint256(0) via std::string(const char*).
     150             :  */
     151             : inline uint256 uint256S(const std::string& str)
     152             : {
     153             :     uint256 rv;
     154         150 :     rv.SetHex(str);
     155             :     return rv;
     156             : }
     157             : 
     158             : #endif // BITCOIN_UINT256_H

Generated by: LCOV version 1.11