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

Generated by: LCOV version 1.14