LCOV - code coverage report
Current view: top level - src/script - script.h (source / functions) Hit Total Coverage
Test: bitcoincash_test.info Lines: 129 146 88.4 %
Date: 2018-04-13 15:12:50 Functions: 16 17 94.1 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2016 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_SCRIPT_SCRIPT_H
       7             : #define BITCOIN_SCRIPT_SCRIPT_H
       8             : 
       9             : #include "crypto/common.h"
      10             : #include "prevector.h"
      11             : #include "serialize.h"
      12             : 
      13             : #include <cassert>
      14             : #include <climits>
      15             : #include <cstdint>
      16             : #include <cstring>
      17             : #include <limits>
      18             : #include <stdexcept>
      19             : #include <string>
      20             : #include <vector>
      21             : 
      22             : // Maximum number of bytes pushable to the stack
      23             : static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
      24             : 
      25             : // Maximum number of non-push operations per script
      26             : static const int MAX_OPS_PER_SCRIPT = 201;
      27             : 
      28             : // Maximum number of public keys per multisig
      29             : static const int MAX_PUBKEYS_PER_MULTISIG = 20;
      30             : 
      31             : // Maximum script length in bytes
      32             : static const int MAX_SCRIPT_SIZE = 10000;
      33             : 
      34             : // Threshold for nLockTime: below this value it is interpreted as block number,
      35             : // otherwise as UNIX timestamp. Thresold is Tue Nov 5 00:53:20 1985 UTC
      36             : static const unsigned int LOCKTIME_THRESHOLD = 500000000;
      37             : 
      38         281 : template <typename T> std::vector<uint8_t> ToByteVector(const T &in) {
      39         992 :     return std::vector<uint8_t>(in.begin(), in.end());
      40             : }
      41             : 
      42             : /** Script opcodes */
      43             : enum opcodetype {
      44             :     // push value
      45             :     OP_0 = 0x00,
      46             :     OP_FALSE = OP_0,
      47             :     OP_PUSHDATA1 = 0x4c,
      48             :     OP_PUSHDATA2 = 0x4d,
      49             :     OP_PUSHDATA4 = 0x4e,
      50             :     OP_1NEGATE = 0x4f,
      51             :     OP_RESERVED = 0x50,
      52             :     OP_1 = 0x51,
      53             :     OP_TRUE = OP_1,
      54             :     OP_2 = 0x52,
      55             :     OP_3 = 0x53,
      56             :     OP_4 = 0x54,
      57             :     OP_5 = 0x55,
      58             :     OP_6 = 0x56,
      59             :     OP_7 = 0x57,
      60             :     OP_8 = 0x58,
      61             :     OP_9 = 0x59,
      62             :     OP_10 = 0x5a,
      63             :     OP_11 = 0x5b,
      64             :     OP_12 = 0x5c,
      65             :     OP_13 = 0x5d,
      66             :     OP_14 = 0x5e,
      67             :     OP_15 = 0x5f,
      68             :     OP_16 = 0x60,
      69             : 
      70             :     // control
      71             :     OP_NOP = 0x61,
      72             :     OP_VER = 0x62,
      73             :     OP_IF = 0x63,
      74             :     OP_NOTIF = 0x64,
      75             :     OP_VERIF = 0x65,
      76             :     OP_VERNOTIF = 0x66,
      77             :     OP_ELSE = 0x67,
      78             :     OP_ENDIF = 0x68,
      79             :     OP_VERIFY = 0x69,
      80             :     OP_RETURN = 0x6a,
      81             : 
      82             :     // stack ops
      83             :     OP_TOALTSTACK = 0x6b,
      84             :     OP_FROMALTSTACK = 0x6c,
      85             :     OP_2DROP = 0x6d,
      86             :     OP_2DUP = 0x6e,
      87             :     OP_3DUP = 0x6f,
      88             :     OP_2OVER = 0x70,
      89             :     OP_2ROT = 0x71,
      90             :     OP_2SWAP = 0x72,
      91             :     OP_IFDUP = 0x73,
      92             :     OP_DEPTH = 0x74,
      93             :     OP_DROP = 0x75,
      94             :     OP_DUP = 0x76,
      95             :     OP_NIP = 0x77,
      96             :     OP_OVER = 0x78,
      97             :     OP_PICK = 0x79,
      98             :     OP_ROLL = 0x7a,
      99             :     OP_ROT = 0x7b,
     100             :     OP_SWAP = 0x7c,
     101             :     OP_TUCK = 0x7d,
     102             : 
     103             :     // splice ops
     104             :     OP_CAT = 0x7e,
     105             :     OP_SPLIT = 0x7f,   // after monolith upgrade (May 2018)
     106             :     OP_NUM2BIN = 0x80, // after monolith upgrade (May 2018)
     107             :     OP_BIN2NUM = 0x81, // after monolith upgrade (May 2018)
     108             :     OP_SIZE = 0x82,
     109             : 
     110             :     // bit logic
     111             :     OP_INVERT = 0x83,
     112             :     OP_AND = 0x84,
     113             :     OP_OR = 0x85,
     114             :     OP_XOR = 0x86,
     115             :     OP_EQUAL = 0x87,
     116             :     OP_EQUALVERIFY = 0x88,
     117             :     OP_RESERVED1 = 0x89,
     118             :     OP_RESERVED2 = 0x8a,
     119             : 
     120             :     // numeric
     121             :     OP_1ADD = 0x8b,
     122             :     OP_1SUB = 0x8c,
     123             :     OP_2MUL = 0x8d,
     124             :     OP_2DIV = 0x8e,
     125             :     OP_NEGATE = 0x8f,
     126             :     OP_ABS = 0x90,
     127             :     OP_NOT = 0x91,
     128             :     OP_0NOTEQUAL = 0x92,
     129             : 
     130             :     OP_ADD = 0x93,
     131             :     OP_SUB = 0x94,
     132             :     OP_MUL = 0x95,
     133             :     OP_DIV = 0x96,
     134             :     OP_MOD = 0x97,
     135             :     OP_LSHIFT = 0x98,
     136             :     OP_RSHIFT = 0x99,
     137             : 
     138             :     OP_BOOLAND = 0x9a,
     139             :     OP_BOOLOR = 0x9b,
     140             :     OP_NUMEQUAL = 0x9c,
     141             :     OP_NUMEQUALVERIFY = 0x9d,
     142             :     OP_NUMNOTEQUAL = 0x9e,
     143             :     OP_LESSTHAN = 0x9f,
     144             :     OP_GREATERTHAN = 0xa0,
     145             :     OP_LESSTHANOREQUAL = 0xa1,
     146             :     OP_GREATERTHANOREQUAL = 0xa2,
     147             :     OP_MIN = 0xa3,
     148             :     OP_MAX = 0xa4,
     149             : 
     150             :     OP_WITHIN = 0xa5,
     151             : 
     152             :     // crypto
     153             :     OP_RIPEMD160 = 0xa6,
     154             :     OP_SHA1 = 0xa7,
     155             :     OP_SHA256 = 0xa8,
     156             :     OP_HASH160 = 0xa9,
     157             :     OP_HASH256 = 0xaa,
     158             :     OP_CODESEPARATOR = 0xab,
     159             :     OP_CHECKSIG = 0xac,
     160             :     OP_CHECKSIGVERIFY = 0xad,
     161             :     OP_CHECKMULTISIG = 0xae,
     162             :     OP_CHECKMULTISIGVERIFY = 0xaf,
     163             : 
     164             :     // expansion
     165             :     OP_NOP1 = 0xb0,
     166             :     OP_CHECKLOCKTIMEVERIFY = 0xb1,
     167             :     OP_NOP2 = OP_CHECKLOCKTIMEVERIFY,
     168             :     OP_CHECKSEQUENCEVERIFY = 0xb2,
     169             :     OP_NOP3 = OP_CHECKSEQUENCEVERIFY,
     170             :     OP_NOP4 = 0xb3,
     171             :     OP_NOP5 = 0xb4,
     172             :     OP_NOP6 = 0xb5,
     173             :     OP_NOP7 = 0xb6,
     174             :     OP_NOP8 = 0xb7,
     175             :     OP_NOP9 = 0xb8,
     176             :     OP_NOP10 = 0xb9,
     177             : 
     178             :     // The first op_code value after all defined opcodes
     179             :     FIRST_UNDEFINED_OP_VALUE,
     180             : 
     181             :     // template matching params
     182             :     OP_SMALLINTEGER = 0xfa,
     183             :     OP_PUBKEYS = 0xfb,
     184             :     OP_PUBKEYHASH = 0xfd,
     185             :     OP_PUBKEY = 0xfe,
     186             : 
     187             :     OP_INVALIDOPCODE = 0xff,
     188             : };
     189             : 
     190             : const char *GetOpName(opcodetype opcode);
     191             : 
     192         170 : class scriptnum_error : public std::runtime_error {
     193             : public:
     194             :     explicit scriptnum_error(const std::string &str)
     195         170 :         : std::runtime_error(str) {}
     196             : };
     197             : 
     198             : class CScriptNum {
     199             :     /**
     200             :      * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte
     201             :      * integers. The semantics are subtle, though: operands must be in the range
     202             :      * [-2^31 +1...2^31 -1], but results may overflow (and are valid as long as
     203             :      * they are not used in a subsequent numeric operation). CScriptNum enforces
     204             :      * those semantics by storing results as an int64 and allowing out-of-range
     205             :      * values to be returned as a vector of bytes but throwing an exception if
     206             :      * arithmetic is done or the result is interpreted as an integer.
     207             :      */
     208             : public:
     209             :     static const size_t MAXIMUM_ELEMENT_SIZE = 4;
     210             : 
     211       17612 :     explicit CScriptNum(const int64_t &n) { m_value = n; }
     212             : 
     213       64747 :     explicit CScriptNum(const std::vector<uint8_t> &vch, bool fRequireMinimal,
     214             :                         const size_t nMaxNumSize = MAXIMUM_ELEMENT_SIZE) {
     215       64747 :         if (vch.size() > nMaxNumSize) {
     216         175 :             throw scriptnum_error("script number overflow");
     217             :         }
     218       64712 :         if (fRequireMinimal && !IsMinimallyEncoded(vch, nMaxNumSize)) {
     219         675 :             throw scriptnum_error("non-minimally encoded script number");
     220             :         }
     221       64577 :         m_value = set_vch(vch);
     222       64577 :     }
     223             : 
     224             :     static bool IsMinimallyEncoded(
     225             :         const std::vector<uint8_t> &vch,
     226             :         const size_t nMaxNumSize = CScriptNum::MAXIMUM_ELEMENT_SIZE);
     227             : 
     228             :     static bool MinimallyEncode(std::vector<uint8_t> &data);
     229             : 
     230        2808 :     inline bool operator==(const int64_t &rhs) const { return m_value == rhs; }
     231        2808 :     inline bool operator!=(const int64_t &rhs) const { return m_value != rhs; }
     232        2808 :     inline bool operator<=(const int64_t &rhs) const { return m_value <= rhs; }
     233        2808 :     inline bool operator<(const int64_t &rhs) const { return m_value < rhs; }
     234        2808 :     inline bool operator>=(const int64_t &rhs) const { return m_value >= rhs; }
     235        2808 :     inline bool operator>(const int64_t &rhs) const { return m_value > rhs; }
     236             : 
     237             :     inline bool operator==(const CScriptNum &rhs) const {
     238        2808 :         return operator==(rhs.m_value);
     239             :     }
     240             :     inline bool operator!=(const CScriptNum &rhs) const {
     241        2808 :         return operator!=(rhs.m_value);
     242             :     }
     243             :     inline bool operator<=(const CScriptNum &rhs) const {
     244        2808 :         return operator<=(rhs.m_value);
     245             :     }
     246             :     inline bool operator<(const CScriptNum &rhs) const {
     247        2808 :         return operator<(rhs.m_value);
     248             :     }
     249             :     inline bool operator>=(const CScriptNum &rhs) const {
     250        2808 :         return operator>=(rhs.m_value);
     251             :     }
     252             :     inline bool operator>(const CScriptNum &rhs) const {
     253        2808 :         return operator>(rhs.m_value);
     254             :     }
     255             : 
     256             :     inline CScriptNum operator+(const int64_t &rhs) const {
     257        4212 :         return CScriptNum(m_value + rhs);
     258             :     }
     259             :     inline CScriptNum operator-(const int64_t &rhs) const {
     260        5616 :         return CScriptNum(m_value - rhs);
     261             :     }
     262             :     inline CScriptNum operator+(const CScriptNum &rhs) const {
     263        2808 :         return operator+(rhs.m_value);
     264             :     }
     265             :     inline CScriptNum operator-(const CScriptNum &rhs) const {
     266        5616 :         return operator-(rhs.m_value);
     267             :     }
     268             : 
     269             :     inline CScriptNum operator/(const int64_t &rhs) const {
     270             :         return CScriptNum(m_value / rhs);
     271             :     }
     272             :     inline CScriptNum operator/(const CScriptNum &rhs) const {
     273             :         return operator/(rhs.m_value);
     274             :     }
     275             : 
     276             :     inline CScriptNum operator%(const int64_t &rhs) const {
     277             :         return CScriptNum(m_value % rhs);
     278             :     }
     279             :     inline CScriptNum operator%(const CScriptNum &rhs) const {
     280             :         return operator%(rhs.m_value);
     281             :     }
     282             : 
     283             :     inline CScriptNum &operator+=(const CScriptNum &rhs) {
     284             :         return operator+=(rhs.m_value);
     285             :     }
     286             :     inline CScriptNum &operator-=(const CScriptNum &rhs) {
     287             :         return operator-=(rhs.m_value);
     288             :     }
     289             : 
     290             :     inline CScriptNum operator&(const int64_t &rhs) const {
     291             :         return CScriptNum(m_value & rhs);
     292             :     }
     293             :     inline CScriptNum operator&(const CScriptNum &rhs) const {
     294             :         return operator&(rhs.m_value);
     295             :     }
     296             : 
     297             :     inline CScriptNum &operator&=(const CScriptNum &rhs) {
     298             :         return operator&=(rhs.m_value);
     299             :     }
     300             : 
     301             :     inline CScriptNum operator-() const {
     302        1404 :         assert(m_value != std::numeric_limits<int64_t>::min());
     303        1404 :         return CScriptNum(-m_value);
     304             :     }
     305             : 
     306             :     inline CScriptNum &operator=(const int64_t &rhs) {
     307             :         m_value = rhs;
     308             :         return *this;
     309             :     }
     310             : 
     311             :     inline CScriptNum &operator+=(const int64_t &rhs) {
     312             :         assert(
     313             :             rhs == 0 ||
     314             :             (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
     315             :             (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
     316             :         m_value += rhs;
     317             :         return *this;
     318             :     }
     319             : 
     320             :     inline CScriptNum &operator-=(const int64_t &rhs) {
     321             :         assert(
     322             :             rhs == 0 ||
     323             :             (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
     324             :             (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
     325             :         m_value -= rhs;
     326             :         return *this;
     327             :     }
     328             : 
     329             :     inline CScriptNum &operator&=(const int64_t &rhs) {
     330             :         m_value &= rhs;
     331             :         return *this;
     332             :     }
     333             : 
     334             :     int getint() const {
     335       14959 :         if (m_value > std::numeric_limits<int>::max())
     336             :             return std::numeric_limits<int>::max();
     337       13734 :         else if (m_value < std::numeric_limits<int>::min())
     338             :             return std::numeric_limits<int>::min();
     339       12509 :         return m_value;
     340             :     }
     341             : 
     342       17603 :     std::vector<uint8_t> getvch() const { return serialize(m_value); }
     343             : 
     344      184646 :     static std::vector<uint8_t> serialize(const int64_t &value) {
     345      184646 :         if (value == 0) return std::vector<uint8_t>();
     346             : 
     347      179070 :         std::vector<uint8_t> result;
     348      179070 :         const bool neg = value < 0;
     349      179070 :         uint64_t absvalue = neg ? -value : value;
     350             : 
     351      734810 :         while (absvalue) {
     352      555740 :             result.push_back(absvalue & 0xff);
     353      277870 :             absvalue >>= 8;
     354             :         }
     355             : 
     356             :         // - If the most significant byte is >= 0x80 and the value is positive,
     357             :         // push a new zero-byte to make the significant byte < 0x80 again.
     358             :         // - If the most significant byte is >= 0x80 and the value is negative,
     359             :         // push a new 0x80 byte that will be popped off when converting to an
     360             :         // integral.
     361             :         // - If the most significant byte is < 0x80 and the value is negative,
     362             :         // add 0x80 to it, since it will be subtracted and interpreted as a
     363             :         // negative when converting to an integral.
     364      179070 :         if (result.back() & 0x80) {
     365       80572 :             result.push_back(neg ? 0x80 : 0);
     366      138784 :         } else if (neg) {
     367        6948 :             result.back() |= 0x80;
     368             :         }
     369             : 
     370      179070 :         return result;
     371             :     }
     372             : 
     373             : private:
     374       64577 :     static int64_t set_vch(const std::vector<uint8_t> &vch) {
     375       64577 :         if (vch.empty()) return 0;
     376             : 
     377             :         int64_t result = 0;
     378      266870 :         for (size_t i = 0; i != vch.size(); ++i)
     379      207738 :             result |= static_cast<int64_t>(vch[i]) << 8 * i;
     380             : 
     381             :         // If the input vector's most significant byte is 0x80, remove it from
     382             :         // the result's msb and return a negative.
     383       59132 :         if (vch.back() & 0x80)
     384        2415 :             return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
     385             : 
     386             :         return result;
     387             :     }
     388             : 
     389             :     int64_t m_value;
     390             : };
     391             : 
     392             : typedef prevector<28, uint8_t> CScriptBase;
     393             : 
     394             : /** Serialized script, used inside transaction inputs and outputs */
     395    10128339 : class CScript : public CScriptBase {
     396             : protected:
     397       85818 :     CScript &push_int64(int64_t n) {
     398       85818 :         if (n == -1 || (n >= 1 && n <= 16)) {
     399        1066 :             push_back(n + (OP_1 - 1));
     400       84752 :         } else if (n == 0) {
     401        1108 :             push_back(OP_0);
     402             :         } else {
     403      167288 :             *this << CScriptNum::serialize(n);
     404             :         }
     405       85818 :         return *this;
     406             :     }
     407             : 
     408             : public:
     409    12933780 :     CScript() {}
     410             :     CScript(const_iterator pbegin, const_iterator pend)
     411             :         : CScriptBase(pbegin, pend) {}
     412             :     CScript(std::vector<uint8_t>::const_iterator pbegin,
     413             :             std::vector<uint8_t>::const_iterator pend)
     414          26 :         : CScriptBase(pbegin, pend) {}
     415             :     CScript(const uint8_t *pbegin, const uint8_t *pend)
     416           9 :         : CScriptBase(pbegin, pend) {}
     417             : 
     418       24852 :     ADD_SERIALIZE_METHODS;
     419             : 
     420             :     template <typename Stream, typename Operation>
     421             :     inline void SerializationOp(Stream &s, Operation ser_action) {
     422       24852 :         READWRITE(static_cast<CScriptBase &>(*this));
     423             :     }
     424             : 
     425          10 :     CScript &operator+=(const CScript &b) {
     426          40 :         insert(end(), b.begin(), b.end());
     427          10 :         return *this;
     428             :     }
     429             : 
     430             :     friend CScript operator+(const CScript &a, const CScript &b) {
     431           4 :         CScript ret = a;
     432           4 :         ret += b;
     433             :         return ret;
     434             :     }
     435             : 
     436             :     CScript(int64_t b) { operator<<(b); }
     437             : 
     438        2000 :     explicit CScript(opcodetype b) { operator<<(b); }
     439             :     explicit CScript(const CScriptNum &b) { operator<<(b); }
     440             :     explicit CScript(const std::vector<uint8_t> &b) { operator<<(b); }
     441             : 
     442       85381 :     CScript &operator<<(int64_t b) { return push_int64(b); }
     443             : 
     444      173842 :     CScript &operator<<(opcodetype opcode) {
     445      173842 :         if (opcode < 0 || opcode > 0xff)
     446           0 :             throw std::runtime_error("CScript::operator<<(): invalid opcode");
     447      347684 :         insert(end(), uint8_t(opcode));
     448      173842 :         return *this;
     449             :     }
     450             : 
     451        2870 :     CScript &operator<<(const CScriptNum &b) {
     452        5740 :         *this << b.getvch();
     453        2870 :         return *this;
     454             :     }
     455             : 
     456       91608 :     CScript &operator<<(const std::vector<uint8_t> &b) {
     457       91610 :         if (b.size() < OP_PUSHDATA1) {
     458      182114 :             insert(end(), uint8_t(b.size()));
     459         551 :         } else if (b.size() <= 0xff) {
     460         388 :             insert(end(), OP_PUSHDATA1);
     461         388 :             insert(end(), uint8_t(b.size()));
     462         357 :         } else if (b.size() <= 0xffff) {
     463         714 :             insert(end(), OP_PUSHDATA2);
     464             :             uint8_t data[2];
     465         714 :             WriteLE16(data, b.size());
     466         714 :             insert(end(), data, data + sizeof(data));
     467             :         } else {
     468           0 :             insert(end(), OP_PUSHDATA4);
     469             :             uint8_t data[4];
     470           0 :             WriteLE32(data, b.size());
     471           0 :             insert(end(), data, data + sizeof(data));
     472             :         }
     473      366432 :         insert(end(), b.begin(), b.end());
     474       91608 :         return *this;
     475             :     }
     476             : 
     477             :     CScript &operator<<(const CScript &b) {
     478             :         // I'm not sure if this should push the script or concatenate scripts.
     479             :         // If there's ever a use for pushing a script onto a script, delete this
     480             :         // member fn.
     481             :         assert(!"Warning: Pushing a CScript onto a CScript with << is probably "
     482             :                 "not intended, use + to concatenate!");
     483             :         return *this;
     484             :     }
     485             : 
     486             :     bool GetOp(iterator &pc, opcodetype &opcodeRet,
     487             :                std::vector<uint8_t> &vchRet) {
     488             :         // Wrapper so it can be called with either iterator or const_iterator.
     489             :         const_iterator pc2 = pc;
     490             :         bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
     491             :         pc = begin() + (pc2 - begin());
     492             :         return fRet;
     493             :     }
     494             : 
     495        5018 :     bool GetOp(iterator &pc, opcodetype &opcodeRet) {
     496        5018 :         const_iterator pc2 = pc;
     497        5018 :         bool fRet = GetOp2(pc2, opcodeRet, nullptr);
     498       30108 :         pc = begin() + (pc2 - begin());
     499        5018 :         return fRet;
     500             :     }
     501             : 
     502             :     bool GetOp(const_iterator &pc, opcodetype &opcodeRet,
     503             :                std::vector<uint8_t> &vchRet) const {
     504             :         return GetOp2(pc, opcodeRet, &vchRet);
     505             :     }
     506             : 
     507             :     bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const {
     508             :         return GetOp2(pc, opcodeRet, nullptr);
     509             :     }
     510             : 
     511        5018 :     bool GetOp2(const_iterator &pc, opcodetype &opcodeRet,
     512             :                 std::vector<uint8_t> *pvchRet) const {
     513        5018 :         opcodeRet = OP_INVALIDOPCODE;
     514        5018 :         if (pvchRet) pvchRet->clear();
     515       15054 :         if (pc >= end()) return false;
     516             : 
     517             :         // Read instruction
     518       12012 :         if (end() - pc < 1) return false;
     519        8008 :         unsigned int opcode = *pc++;
     520             : 
     521             :         // Immediate operand
     522        4004 :         if (opcode <= OP_PUSHDATA4) {
     523         474 :             unsigned int nSize = 0;
     524         474 :             if (opcode < OP_PUSHDATA1) {
     525             :                 nSize = opcode;
     526           0 :             } else if (opcode == OP_PUSHDATA1) {
     527           0 :                 if (end() - pc < 1) return false;
     528           0 :                 nSize = *pc++;
     529           0 :             } else if (opcode == OP_PUSHDATA2) {
     530           0 :                 if (end() - pc < 2) return false;
     531           0 :                 nSize = ReadLE16(&pc[0]);
     532             :                 pc += 2;
     533           0 :             } else if (opcode == OP_PUSHDATA4) {
     534           0 :                 if (end() - pc < 4) return false;
     535           0 :                 nSize = ReadLE32(&pc[0]);
     536             :                 pc += 4;
     537             :             }
     538        2370 :             if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
     539             :                 return false;
     540         473 :             if (pvchRet) pvchRet->assign(pc, pc + nSize);
     541             :             pc += nSize;
     542             :         }
     543             : 
     544        4003 :         opcodeRet = (opcodetype)opcode;
     545        4003 :         return true;
     546             :     }
     547             : 
     548             :     /** Encode/decode small integers: */
     549             :     static int DecodeOP_N(opcodetype opcode) {
     550             :         if (opcode == OP_0) return 0;
     551             :         assert(opcode >= OP_1 && opcode <= OP_16);
     552             :         return (int)opcode - (int)(OP_1 - 1);
     553             :     }
     554             :     static opcodetype EncodeOP_N(int n) {
     555           0 :         assert(n >= 0 && n <= 16);
     556           0 :         if (n == 0) return OP_0;
     557           0 :         return (opcodetype)(OP_1 + n - 1);
     558             :     }
     559             : 
     560        1016 :     int FindAndDelete(const CScript &b) {
     561        1016 :         int nFound = 0;
     562        2032 :         if (b.empty()) return nFound;
     563        1015 :         CScript result;
     564        3045 :         iterator pc = begin(), pc2 = begin();
     565             :         opcodetype opcode;
     566        5018 :         do {
     567        5018 :             result.insert(result.end(), pc2, pc);
     568       27416 :             while (static_cast<size_t>(end() - pc) >= b.size() &&
     569       13662 :                    std::equal(b.begin(), b.end(), pc)) {
     570        1674 :                 pc = pc + b.size();
     571         558 :                 ++nFound;
     572             :             }
     573        5018 :             pc2 = pc;
     574             :         } while (GetOp(pc, opcode));
     575             : 
     576        1015 :         if (nFound > 0) {
     577        1266 :             result.insert(result.end(), pc2, end());
     578             :             *this = result;
     579             :         }
     580             : 
     581        1015 :         return nFound;
     582             :     }
     583             :     int Find(opcodetype op) const {
     584             :         int nFound = 0;
     585             :         opcodetype opcode;
     586             :         for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
     587             :             if (opcode == op) ++nFound;
     588             :         return nFound;
     589             :     }
     590             : 
     591             :     /**
     592             :      * Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops. With
     593             :      * pay-to-script-hash, that changed: CHECKMULTISIGs serialized in scriptSigs
     594             :      * are counted more accurately, assuming they are of the form
     595             :      *  ... OP_N CHECKMULTISIG ...
     596             :      */
     597             :     unsigned int GetSigOpCount(bool fAccurate) const;
     598             : 
     599             :     /**
     600             :      * Accurately count sigOps, including sigOps in pay-to-script-hash
     601             :      * transactions:
     602             :      */
     603             :     unsigned int GetSigOpCount(const CScript &scriptSig) const;
     604             : 
     605             :     bool IsPayToScriptHash() const;
     606             :     bool IsCommitment(const std::vector<uint8_t> &data) const;
     607             :     bool IsWitnessProgram(int &version, std::vector<uint8_t> &program) const;
     608             : 
     609             :     /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it
     610             :      * consensus-critical). */
     611             :     bool IsPushOnly(const_iterator pc) const;
     612             :     bool IsPushOnly() const;
     613             : 
     614             :     /**
     615             :      * Returns whether the script is guaranteed to fail at execution, regardless
     616             :      * of the initial stack. This allows outputs to be pruned instantly when
     617             :      * entering the UTXO set.
     618             :      */
     619             :     bool IsUnspendable() const {
     620        3810 :         return (size() > 0 && *begin() == OP_RETURN) ||
     621           0 :                (size() > MAX_SCRIPT_SIZE);
     622             :     }
     623             : 
     624     4241873 :     void clear() {
     625             :         // The default std::vector::clear() does not release memory.
     626    12725619 :         CScriptBase().swap(*this);
     627     4241873 :     }
     628             : };
     629             : 
     630             : struct CScriptWitness {
     631             :     // Note that this encodes the data elements being pushed, rather than
     632             :     // encoding them as a CScript that pushes them.
     633             :     std::vector<std::vector<uint8_t>> stack;
     634             : 
     635             :     // Some compilers complain without a default constructor
     636             :     CScriptWitness() {}
     637             : 
     638             :     bool IsNull() const { return stack.empty(); }
     639             : 
     640             :     void SetNull() {
     641             :         stack.clear();
     642             :         stack.shrink_to_fit();
     643             :     }
     644             : 
     645             :     std::string ToString() const;
     646             : };
     647             : 
     648             : class CReserveScript {
     649             : public:
     650             :     CScript reserveScript;
     651             :     virtual void KeepScript() {}
     652             :     CReserveScript() {}
     653             :     virtual ~CReserveScript() {}
     654             : };
     655             : 
     656             : #endif // BITCOIN_SCRIPT_SCRIPT_H

Generated by: LCOV version 1.12