LCOV - code coverage report
Current view: top level - src/script - script.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 189 194 97.4 %
Date: 2025-02-23 09:33:43 Functions: 28 29 96.6 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2014 The Bitcoin developers
       3             : // Copyright (c) 2014-2015 The Dash developers
       4             : // Copyright (c) 2016-2021 The PIVX Core developers
       5             : // Distributed under the MIT software license, see the accompanying
       6             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       7             : 
       8             : #ifndef PIVX_SCRIPT_SCRIPT_H
       9             : #define PIVX_SCRIPT_SCRIPT_H
      10             : 
      11             : #include <assert.h>
      12             : #include <climits>
      13             : #include <limits>
      14             : #include <stdexcept>
      15             : #include <stdint.h>
      16             : #include <string.h>
      17             : #include <string>
      18             : #include <vector>
      19             : 
      20             : #include "crypto/common.h"
      21             : #include "memusage.h"
      22             : #include "prevector.h"
      23             : #include "pubkey.h"
      24             : 
      25             : typedef std::vector<unsigned char> valtype;
      26             : 
      27             : static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes
      28             : 
      29             : // Maximum number of public keys per multisig
      30             : static const int MAX_PUBKEYS_PER_MULTISIG = 20;
      31             : 
      32             : // Maximum script length in bytes
      33             : static const int MAX_SCRIPT_SIZE = 10000;
      34             : 
      35             : // Threshold for nLockTime: below this value it is interpreted as block number,
      36             : // otherwise as UNIX timestamp.
      37             : static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov  5 00:53:20 1985 UTC
      38             : 
      39             : template <typename T>
      40     3572988 : std::vector<unsigned char> ToByteVector(const T& in)
      41             : {
      42     3577522 :     return std::vector<unsigned char>(in.begin(), in.end());
      43             : }
      44             : 
      45             : /** Script opcodes */
      46             : enum opcodetype
      47             : {
      48             :     // push value
      49             :     OP_0 = 0x00,
      50             :     OP_FALSE = OP_0,
      51             :     OP_PUSHDATA1 = 0x4c,
      52             :     OP_PUSHDATA2 = 0x4d,
      53             :     OP_PUSHDATA4 = 0x4e,
      54             :     OP_1NEGATE = 0x4f,
      55             :     OP_RESERVED = 0x50,
      56             :     OP_1 = 0x51,
      57             :     OP_TRUE=OP_1,
      58             :     OP_2 = 0x52,
      59             :     OP_3 = 0x53,
      60             :     OP_4 = 0x54,
      61             :     OP_5 = 0x55,
      62             :     OP_6 = 0x56,
      63             :     OP_7 = 0x57,
      64             :     OP_8 = 0x58,
      65             :     OP_9 = 0x59,
      66             :     OP_10 = 0x5a,
      67             :     OP_11 = 0x5b,
      68             :     OP_12 = 0x5c,
      69             :     OP_13 = 0x5d,
      70             :     OP_14 = 0x5e,
      71             :     OP_15 = 0x5f,
      72             :     OP_16 = 0x60,
      73             : 
      74             :     // control
      75             :     OP_NOP = 0x61,
      76             :     OP_VER = 0x62,
      77             :     OP_IF = 0x63,
      78             :     OP_NOTIF = 0x64,
      79             :     OP_VERIF = 0x65,
      80             :     OP_VERNOTIF = 0x66,
      81             :     OP_ELSE = 0x67,
      82             :     OP_ENDIF = 0x68,
      83             :     OP_VERIFY = 0x69,
      84             :     OP_RETURN = 0x6a,
      85             : 
      86             :     // stack ops
      87             :     OP_TOALTSTACK = 0x6b,
      88             :     OP_FROMALTSTACK = 0x6c,
      89             :     OP_2DROP = 0x6d,
      90             :     OP_2DUP = 0x6e,
      91             :     OP_3DUP = 0x6f,
      92             :     OP_2OVER = 0x70,
      93             :     OP_2ROT = 0x71,
      94             :     OP_2SWAP = 0x72,
      95             :     OP_IFDUP = 0x73,
      96             :     OP_DEPTH = 0x74,
      97             :     OP_DROP = 0x75,
      98             :     OP_DUP = 0x76,
      99             :     OP_NIP = 0x77,
     100             :     OP_OVER = 0x78,
     101             :     OP_PICK = 0x79,
     102             :     OP_ROLL = 0x7a,
     103             :     OP_ROT = 0x7b,
     104             :     OP_SWAP = 0x7c,
     105             :     OP_TUCK = 0x7d,
     106             : 
     107             :     // splice ops
     108             :     OP_CAT = 0x7e,
     109             :     OP_SUBSTR = 0x7f,
     110             :     OP_LEFT = 0x80,
     111             :     OP_RIGHT = 0x81,
     112             :     OP_SIZE = 0x82,
     113             : 
     114             :     // bit logic
     115             :     OP_INVERT = 0x83,
     116             :     OP_AND = 0x84,
     117             :     OP_OR = 0x85,
     118             :     OP_XOR = 0x86,
     119             :     OP_EQUAL = 0x87,
     120             :     OP_EQUALVERIFY = 0x88,
     121             :     OP_RESERVED1 = 0x89,
     122             :     OP_RESERVED2 = 0x8a,
     123             : 
     124             :     // numeric
     125             :     OP_1ADD = 0x8b,
     126             :     OP_1SUB = 0x8c,
     127             :     OP_2MUL = 0x8d,
     128             :     OP_2DIV = 0x8e,
     129             :     OP_NEGATE = 0x8f,
     130             :     OP_ABS = 0x90,
     131             :     OP_NOT = 0x91,
     132             :     OP_0NOTEQUAL = 0x92,
     133             : 
     134             :     OP_ADD = 0x93,
     135             :     OP_SUB = 0x94,
     136             :     OP_MUL = 0x95,
     137             :     OP_DIV = 0x96,
     138             :     OP_MOD = 0x97,
     139             :     OP_LSHIFT = 0x98,
     140             :     OP_RSHIFT = 0x99,
     141             : 
     142             :     OP_BOOLAND = 0x9a,
     143             :     OP_BOOLOR = 0x9b,
     144             :     OP_NUMEQUAL = 0x9c,
     145             :     OP_NUMEQUALVERIFY = 0x9d,
     146             :     OP_NUMNOTEQUAL = 0x9e,
     147             :     OP_LESSTHAN = 0x9f,
     148             :     OP_GREATERTHAN = 0xa0,
     149             :     OP_LESSTHANOREQUAL = 0xa1,
     150             :     OP_GREATERTHANOREQUAL = 0xa2,
     151             :     OP_MIN = 0xa3,
     152             :     OP_MAX = 0xa4,
     153             : 
     154             :     OP_WITHIN = 0xa5,
     155             : 
     156             :     // crypto
     157             :     OP_RIPEMD160 = 0xa6,
     158             :     OP_SHA1 = 0xa7,
     159             :     OP_SHA256 = 0xa8,
     160             :     OP_HASH160 = 0xa9,
     161             :     OP_HASH256 = 0xaa,
     162             :     OP_CODESEPARATOR = 0xab,
     163             :     OP_CHECKSIG = 0xac,
     164             :     OP_CHECKSIGVERIFY = 0xad,
     165             :     OP_CHECKMULTISIG = 0xae,
     166             :     OP_CHECKMULTISIGVERIFY = 0xaf,
     167             : 
     168             :     // expansion
     169             :     OP_NOP1 = 0xb0,
     170             :     OP_NOP2 = 0xb1,
     171             :     OP_CHECKLOCKTIMEVERIFY = OP_NOP2,
     172             :     OP_NOP3 = 0xb2,
     173             :     OP_NOP4 = 0xb3,
     174             :     OP_NOP5 = 0xb4,
     175             :     OP_NOP6 = 0xb5,
     176             :     OP_NOP7 = 0xb6,
     177             :     OP_NOP8 = 0xb7,
     178             :     OP_NOP9 = 0xb8,
     179             :     OP_NOP10 = 0xb9,
     180             : 
     181             :     // zerocoin
     182             :     OP_ZEROCOINMINT = 0xc1,
     183             :     OP_ZEROCOINSPEND = 0xc2,
     184             :     OP_ZEROCOINPUBLICSPEND = 0xc3,
     185             : 
     186             :     // cold staking
     187             :     OP_CHECKCOLDSTAKEVERIFY_LOF = 0xd1,     // last output free for masternode/budget payments
     188             :     OP_CHECKCOLDSTAKEVERIFY = 0xd2,
     189             : 
     190             :     // exchange address, NOP but identifies as an address not allowing private outputs
     191             :     OP_EXCHANGEADDR = 0xe0,
     192             : 
     193             :     OP_INVALIDOPCODE = 0xff,
     194             : };
     195             : 
     196             : const char* GetOpName(opcodetype opcode);
     197             : 
     198             : class scriptnum_error : public std::runtime_error
     199             : {
     200             : public:
     201         137 :     explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
     202             : };
     203             : 
     204             : class CScriptNum
     205             : {
     206             : /**
     207             :  * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
     208             :  * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
     209             :  * but results may overflow (and are valid as long as they are not used in a subsequent
     210             :  * numeric operation). CScriptNum enforces those semantics by storing results as
     211             :  * an int64 and allowing out-of-range values to be returned as a vector of bytes but
     212             :  * throwing an exception if arithmetic is done or the result is interpreted as an integer.
     213             :  */
     214             : public:
     215             : 
     216      297705 :     explicit CScriptNum(const int64_t& n)
     217      292089 :     {
     218      284512 :         m_value = n;
     219             :     }
     220             : 
     221             :     static const size_t nDefaultMaxNumSize = 4;
     222             : 
     223        5988 :     explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,
     224             :             const size_t nMaxNumSize = nDefaultMaxNumSize)
     225        5988 :     {
     226        5988 :         if (vch.size() > nMaxNumSize) {
     227         249 :             throw scriptnum_error("script number overflow");
     228             :         }
     229        5905 :         if (fRequireMinimal && vch.size() > 0) {
     230             :             // Check that the number is encoded with the minimum possible
     231             :             // number of bytes.
     232             :             //
     233             :             // If the most-significant-byte - excluding the sign bit - is zero
     234             :             // then we're not minimal. Note how this test also rejects the
     235             :             // negative-zero encoding, 0x80.
     236        1145 :             if ((vch.back() & 0x7f) == 0) {
     237             :                 // One exception: if there's more than one byte and the most
     238             :                 // significant bit of the second-most-significant-byte is set
     239             :                 // it would conflict with the sign bit. An example of this case
     240             :                 // is +-255, which encode to 0xff00 and 0xff80 respectively.
     241             :                 // (big-endian).
     242          54 :                 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
     243         162 :                     throw scriptnum_error("non-minimally encoded script number");
     244             :                 }
     245             :             }
     246             :         }
     247        5851 :         m_value = set_vch(vch);
     248        5851 :     }
     249             : 
     250        2909 :     inline bool operator==(const int64_t& rhs) const    { return m_value == rhs; }
     251        2849 :     inline bool operator!=(const int64_t& rhs) const    { return m_value != rhs; }
     252        2829 :     inline bool operator<=(const int64_t& rhs) const    { return m_value <= rhs; }
     253        2827 :     inline bool operator< (const int64_t& rhs) const    { return m_value <  rhs; }
     254        2813 :     inline bool operator>=(const int64_t& rhs) const    { return m_value >= rhs; }
     255        2820 :     inline bool operator> (const int64_t& rhs) const    { return m_value >  rhs; }
     256             : 
     257        2909 :     inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
     258        2827 :     inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
     259        2821 :     inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
     260        2832 :     inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
     261        2816 :     inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
     262        2815 :     inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
     263             : 
     264        2844 :     inline CScriptNum operator+(   const int64_t& rhs)    const { return CScriptNum(m_value + rhs);}
     265        2814 :     inline CScriptNum operator-(   const int64_t& rhs)    const { return CScriptNum(m_value - rhs);}
     266        1440 :     inline CScriptNum operator+(   const CScriptNum& rhs) const { return operator+(rhs.m_value);   }
     267        2814 :     inline CScriptNum operator-(   const CScriptNum& rhs) const { return operator-(rhs.m_value);   }
     268             : 
     269           8 :     inline CScriptNum& operator+=( const CScriptNum& rhs)       { return operator+=(rhs.m_value);  }
     270           3 :     inline CScriptNum& operator-=( const CScriptNum& rhs)       { return operator-=(rhs.m_value);  }
     271             : 
     272        1410 :     inline CScriptNum operator-()                         const
     273             :     {
     274        1410 :         assert(m_value != std::numeric_limits<int64_t>::min());
     275        1410 :         return CScriptNum(-m_value);
     276             :     }
     277             : 
     278         163 :     inline CScriptNum& operator=( const int64_t& rhs)
     279             :     {
     280         163 :         m_value = rhs;
     281         163 :         return *this;
     282             :     }
     283             : 
     284           8 :     inline CScriptNum& operator+=( const int64_t& rhs)
     285             :     {
     286           8 :         assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
     287             :                            (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
     288           8 :         m_value += rhs;
     289           8 :         return *this;
     290             :     }
     291             : 
     292           3 :     inline CScriptNum& operator-=( const int64_t& rhs)
     293             :     {
     294           3 :         assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
     295             :                            (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
     296           3 :         m_value -= rhs;
     297           3 :         return *this;
     298             :     }
     299             : 
     300       19441 :     int getint() const
     301             :     {
     302       19090 :         if (m_value > std::numeric_limits<int>::max())
     303             :             return std::numeric_limits<int>::max();
     304       18216 :         else if (m_value < std::numeric_limits<int>::min())
     305             :             return std::numeric_limits<int>::min();
     306       17693 :         return m_value;
     307             :     }
     308             : 
     309      291444 :     std::vector<unsigned char> getvch() const
     310             :     {
     311      291444 :         return serialize(m_value);
     312             :     }
     313             : 
     314      438879 :     static std::vector<unsigned char> serialize(const int64_t& value)
     315             :     {
     316      438879 :         if(value == 0)
     317        4988 :             return std::vector<unsigned char>();
     318             : 
     319      872770 :         std::vector<unsigned char> result;
     320      433891 :         const bool neg = value < 0;
     321      433891 :         uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1 : static_cast<uint64_t>(value);
     322             : 
     323      980353 :         while(absvalue)
     324             :         {
     325      546462 :             result.push_back(absvalue & 0xff);
     326      546462 :             absvalue >>= 8;
     327             :         }
     328             : 
     329             : //    - If the most significant byte is >= 0x80 and the value is positive, push a
     330             : //    new zero-byte to make the significant byte < 0x80 again.
     331             : 
     332             : //    - If the most significant byte is >= 0x80 and the value is negative, push a
     333             : //    new 0x80 byte that will be popped off when converting to an integral.
     334             : 
     335             : //    - If the most significant byte is < 0x80 and the value is negative, add
     336             : //    0x80 to it, since it will be subtracted and interpreted as a negative when
     337             : //    converting to an integral.
     338             : 
     339      433891 :         if (result.back() & 0x80)
     340      117545 :             result.push_back(neg ? 0x80 : 0);
     341      374138 :         else if (neg)
     342        3504 :             result.back() |= 0x80;
     343             : 
     344      433891 :         return result;
     345             :     }
     346             : 
     347             : private:
     348        5851 :     static int64_t set_vch(const std::vector<unsigned char>& vch)
     349             :     {
     350        5851 :       if (vch.empty())
     351             :           return 0;
     352             : 
     353             :       int64_t result = 0;
     354        5591 :       for (size_t i = 0; i != vch.size(); ++i)
     355        3256 :           result |= static_cast<int64_t>(vch[i]) << 8*i;
     356             : 
     357             :       // If the input vector's most significant byte is 0x80, remove it from
     358             :       // the result's msb and return a negative.
     359        2335 :       if (vch.back() & 0x80)
     360         273 :           return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
     361             : 
     362             :       return result;
     363             :     }
     364             : 
     365             :     int64_t m_value;
     366             : };
     367             : 
     368             : /**
     369             :  * We use a prevector for the script to reduce the considerable memory overhead
     370             :  * of vectors in cases where they normally contain a small number of small elements.
     371             :  * Tests in October 2015 (bitcoin) showed use of this reduced dbcache memory usage by 23%
     372             :  *  and made an initial sync 13% faster.
     373             :  */
     374             : typedef prevector<28, unsigned char> CScriptBase;
     375             : 
     376             : /** Serialized script, used inside transaction inputs and outputs */
     377   299022396 : class CScript : public CScriptBase
     378             : {
     379             : protected:
     380      157095 :     CScript& push_int64(int64_t n)
     381             :     {
     382      157095 :         if (n == -1 || (n >= 1 && n <= 16))
     383             :         {
     384        7084 :             push_back(n + (OP_1 - 1));
     385             :         }
     386      150011 :         else if (n == 0)
     387             :         {
     388        2576 :             push_back(OP_0);
     389             :         }
     390             :         else
     391             :         {
     392      294870 :             *this << CScriptNum::serialize(n);
     393             :         }
     394      157095 :         return *this;
     395             :     }
     396             : public:
     397    97573184 :     CScript() { }
     398     8481778 :     CScript(const_iterator pbegin, const_iterator pend) : CScriptBase(pbegin, pend) { }
     399      163100 :     CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
     400           9 :     CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
     401             : 
     402   114943993 :     SERIALIZE_METHODS(CScript, obj) { READWRITEAS(CScriptBase, obj); }
     403             : 
     404       13054 :     CScript& operator+=(const CScript& b)
     405             :     {
     406       13062 :         reserve(size() + b.size());
     407       52216 :         insert(end(), b.begin(), b.end());
     408       13054 :         return *this;
     409             :     }
     410             : 
     411       13046 :     friend CScript operator+(const CScript& a, const CScript& b)
     412             :     {
     413       13046 :         CScript ret = a;
     414       13046 :         ret += b;
     415       13046 :         return ret;
     416             :     }
     417             : 
     418             :     explicit CScript(int64_t b)        { operator<<(b); }
     419             : 
     420       50000 :     explicit CScript(opcodetype b)     { operator<<(b); }
     421             :     explicit CScript(const CScriptNum& b) { operator<<(b); }
     422     8480787 :     explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
     423             : 
     424             : 
     425      157080 :     CScript& operator<<(int64_t b) { return push_int64(b); }
     426             : 
     427     1946821 :     CScript& operator<<(opcodetype opcode)
     428             :     {
     429     1946821 :         if (opcode < 0 || opcode > 0xff)
     430           0 :             throw std::runtime_error("CScript::operator<<() : invalid opcode");
     431     3893632 :         insert(end(), (unsigned char)opcode);
     432     1946821 :         return *this;
     433             :     }
     434             : 
     435       16485 :     CScript& operator<<(const CScriptNum& b)
     436             :     {
     437       16485 :         *this << b.getvch();
     438       16485 :         return *this;
     439             :     }
     440             : 
     441    16188410 :     CScript& operator<<(const std::vector<unsigned char>& b)
     442             :     {
     443    16188410 :         if (b.size() < OP_PUSHDATA1)
     444             :         {
     445    32373240 :             insert(end(), (unsigned char)b.size());
     446             :         }
     447        1802 :         else if (b.size() <= 0xff)
     448             :         {
     449        2916 :             insert(end(), OP_PUSHDATA1);
     450        2916 :             insert(end(), (unsigned char)b.size());
     451             :         }
     452         344 :         else if (b.size() <= 0xffff)
     453             :         {
     454         688 :             insert(end(), OP_PUSHDATA2);
     455         344 :             uint8_t data[2];
     456         344 :             WriteLE16(data, b.size());
     457         688 :             insert(end(), data, data + sizeof(data));
     458             :         }
     459             :         else
     460             :         {
     461           0 :             insert(end(), OP_PUSHDATA4);
     462           0 :             uint8_t data[4];
     463           0 :             WriteLE32(data, b.size());
     464           0 :             insert(end(), data, data + sizeof(data));
     465             :         }
     466    32376920 :         insert(end(), b.begin(), b.end());
     467    16188410 :         return *this;
     468             :     }
     469             : 
     470             :     CScript& operator<<(const CScript& b)
     471             :     {
     472             :         // I'm not sure if this should push the script or concatenate scripts.
     473             :         // If there's ever a use for pushing a script onto a script, delete this member fn
     474             :         assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
     475             :         return *this;
     476             :     }
     477             : 
     478          20 :     CScript& operator<<(const CPubKey& key)
     479             :     {
     480          20 :         std::vector<unsigned char> vchKey = key.Raw();
     481          20 :         return (*this) << vchKey;
     482             :     }
     483             : 
     484             : 
     485             :     bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
     486             :     {
     487             :          // Wrapper so it can be called with either iterator or const_iterator
     488             :          const_iterator pc2 = pc;
     489             :          bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
     490             :          pc = begin() + (pc2 - begin());
     491             :          return fRet;
     492             :     }
     493             : 
     494    51648356 :     bool GetOp(iterator& pc, opcodetype& opcodeRet)
     495             :     {
     496    51648356 :          const_iterator pc2 = pc;
     497    51648356 :          bool fRet = GetOp2(pc2, opcodeRet, nullptr);
     498    51648356 :          pc = begin() + (pc2 - begin());
     499    51648356 :          return fRet;
     500             :     }
     501             : 
     502    60212567 :     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
     503             :     {
     504    60212567 :         return GetOp2(pc, opcodeRet, &vchRet);
     505             :     }
     506             : 
     507    51035024 :     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
     508             :     {
     509    51035024 :         return GetOp2(pc, opcodeRet, nullptr);
     510             :     }
     511             : 
     512   162896497 :     bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
     513             :     {
     514   162896497 :         opcodeRet = OP_INVALIDOPCODE;
     515   162896497 :         if (pvchRet)
     516    60213083 :             pvchRet->clear();
     517   325793294 :         if (pc >= end())
     518             :             return false;
     519             : 
     520             :         // Read instruction
     521   150036994 :         if (end() - pc < 1)
     522             :             return false;
     523   150036994 :         unsigned int opcode = *pc++;
     524             : 
     525             :         // Immediate operand
     526   150036994 :         if (opcode <= OP_PUSHDATA4)
     527             :         {
     528    47738195 :             unsigned int nSize = 0;
     529    47738195 :             if (opcode < OP_PUSHDATA1)
     530             :             {
     531             :                 nSize = opcode;
     532             :             }
     533        8104 :             else if (opcode == OP_PUSHDATA1)
     534             :             {
     535        3626 :                 if (end() - pc < 1)
     536             :                     return false;
     537        1813 :                 nSize = *pc++;
     538             :             }
     539        6291 :             else if (opcode == OP_PUSHDATA2)
     540             :             {
     541       12508 :                 if (end() - pc < 2)
     542             :                     return false;
     543        6254 :                 nSize = ReadLE16(&pc[0]);
     544        6254 :                 pc += 2;
     545             :             }
     546          37 :             else if (opcode == OP_PUSHDATA4)
     547             :             {
     548          74 :                 if (end() - pc < 4)
     549             :                     return false;
     550          37 :                 nSize = ReadLE32(&pc[0]);
     551          37 :                 pc += 4;
     552             :             }
     553    95476530 :             if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
     554          10 :                 return false;
     555    47738190 :             if (pvchRet)
     556    25958370 :                 pvchRet->assign(pc, pc + nSize);
     557    47738190 :             pc += nSize;
     558             :         }
     559             : 
     560   150036989 :         opcodeRet = static_cast<opcodetype>(opcode);
     561   150036989 :         return true;
     562             :     }
     563             : 
     564             :     /** Encode/decode small integers: */
     565        1452 :     static int DecodeOP_N(opcodetype opcode)
     566             :     {
     567        1452 :         if (opcode == OP_0)
     568             :             return 0;
     569        1452 :         assert(opcode >= OP_1 && opcode <= OP_16);
     570        1452 :         return (int)opcode - (int)(OP_1 - 1);
     571             :     }
     572          50 :     static opcodetype EncodeOP_N(int n)
     573             :     {
     574          50 :         assert(n >= 0 && n <= 16);
     575          50 :         if (n == 0)
     576             :             return OP_0;
     577          50 :         return (opcodetype)(OP_1+n-1);
     578             :     }
     579             : 
     580     8530803 :     int FindAndDelete(const CScript& b)
     581             :     {
     582     8530803 :         int nFound = 0;
     583    17011556 :         if (b.empty())
     584             :             return nFound;
     585    17061625 :         CScript result;
     586    25592386 :         iterator pc = begin(), pc2 = begin();
     587    51648356 :         opcodetype opcode;
     588    51648356 :         do
     589             :         {
     590   103297112 :             result.insert(result.end(), pc2, pc);
     591   155200940 :             while (static_cast<size_t>(end() - pc) >= b.size() && std::equal(b.begin(), b.end(), pc))
     592             :             {
     593       24970 :                 pc = pc + b.size();
     594       24970 :                 ++nFound;
     595             :             }
     596    51648356 :             pc2 = pc;
     597             :         }
     598    51648356 :         while (GetOp(pc, opcode));
     599             : 
     600     8530802 :         if (nFound > 0) {
     601       56694 :             result.insert(result.end(), pc2, end());
     602       18898 :             *this = result;
     603             :         }
     604             : 
     605     8530802 :         return nFound;
     606             :     }
     607             :     int Find(opcodetype op) const
     608             :     {
     609             :         int nFound = 0;
     610             :         opcodetype opcode;
     611             :         for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
     612             :             if (opcode == op)
     613             :                 ++nFound;
     614             :         return nFound;
     615             :     }
     616             : 
     617             :     /**
     618             :      * Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
     619             :      * as 20 sigops. With pay-to-script-hash, that changed:
     620             :      * CHECKMULTISIGs serialized in scriptSigs are
     621             :      * counted more accurately, assuming they are of the form
     622             :      *  ... OP_N CHECKMULTISIG ...
     623             :      */
     624             :     unsigned int GetSigOpCount(bool fAccurate) const;
     625             : 
     626             :     /**
     627             :      * Accurately count sigOps, including sigOps in
     628             :      * pay-to-script-hash transactions:
     629             :      */
     630             :     unsigned int GetSigOpCount(const CScript& scriptSig) const;
     631             : 
     632             :     bool IsPayToPublicKeyHash() const;
     633             :     bool IsPayToScriptHash() const;
     634             :     bool IsPayToColdStaking() const;
     635             :     bool IsPayToColdStakingLOF() const;
     636             :     bool IsPayToExchangeAddress() const;
     637             :     bool StartsWithOpcode(const opcodetype opcode) const;
     638             :     bool IsZerocoinMint() const;
     639             :     bool IsZerocoinSpend() const;
     640             :     bool IsZerocoinPublicSpend() const;
     641             : 
     642             :     /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
     643             :     bool IsPushOnly(const_iterator pc) const;
     644             :     bool IsPushOnly() const;
     645             : 
     646             :     /**
     647             :      * Returns whether the script is guaranteed to fail at execution,
     648             :      * regardless of the initial stack. This allows outputs to be pruned
     649             :      * instantly when entering the UTXO set.
     650             :      */
     651     8140244 :     bool IsUnspendable() const
     652             :     {
     653    16249071 :         return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE);
     654             :     }
     655             : 
     656    74506797 :     void clear()
     657             :     {
     658             :         // The default prevector::clear() does not release memory
     659    74506797 :         CScriptBase::clear();
     660    74506797 :         shrink_to_fit();
     661    74506797 :     }
     662             : 
     663             :     size_t DynamicMemoryUsage() const;
     664             : };
     665             : 
     666             : 
     667             : #endif // PIVX_SCRIPT_SCRIPT_H

Generated by: LCOV version 1.14