LCOV - code coverage report
Current view: top level - src/script - script.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 95.8 % 263 252
Test Date: 2026-11-29 07:05:02 Functions: 100.0 % 23 23
Branches: 87.2 % 366 319

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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                 :             : #include <script/script.h>
       7                 :             : 
       8                 :             : #include <crypto/common.h>
       9                 :             : #include <crypto/hex_base.h>
      10                 :             : #include <hash.h>
      11                 :             : #include <memusage.h>
      12                 :             : #include <streams.h>
      13                 :             : #include <uint256.h>
      14                 :             : #include <util/check.h>
      15                 :             : #include <util/hash_type.h>
      16                 :             : #include <util/string.h>
      17                 :             : 
      18                 :             : #include <cassert>
      19                 :             : #include <compare>
      20                 :             : #include <limits>
      21                 :             : #include <memory>
      22                 :             : #include <span>
      23                 :             : #include <stdexcept>
      24                 :             : #include <string>
      25                 :             : #include <vector>
      26                 :             : 
      27                 :      174589 : CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
      28                 :             : 
      29                 :    23495802 : std::string GetOpName(opcodetype opcode)
      30                 :             : {
      31   [ -  -  -  -  :    23495802 :     switch (opcode)
          -  +  +  +  +  
          +  +  +  +  +  
          +  +  +  -  -  
          -  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  -  
                      + ]
      32                 :             :     {
      33                 :             :     // push value
      34                 :           0 :     case OP_0                      : return "0";
      35                 :           0 :     case OP_PUSHDATA1              : return "OP_PUSHDATA1";
      36                 :           0 :     case OP_PUSHDATA2              : return "OP_PUSHDATA2";
      37                 :           0 :     case OP_PUSHDATA4              : return "OP_PUSHDATA4";
      38                 :           0 :     case OP_1NEGATE                : return "-1";
      39                 :          13 :     case OP_RESERVED               : return "OP_RESERVED";
      40                 :    23435276 :     case OP_1                      : return "1";
      41                 :        2056 :     case OP_2                      : return "2";
      42                 :        3035 :     case OP_3                      : return "3";
      43                 :        4005 :     case OP_4                      : return "4";
      44                 :        5004 :     case OP_5                      : return "5";
      45                 :        6004 :     case OP_6                      : return "6";
      46                 :        7004 :     case OP_7                      : return "7";
      47                 :        8004 :     case OP_8                      : return "8";
      48                 :        9004 :     case OP_9                      : return "9";
      49                 :       10007 :     case OP_10                     : return "10";
      50                 :           1 :     case OP_11                     : return "11";
      51                 :           0 :     case OP_12                     : return "12";
      52                 :           0 :     case OP_13                     : return "13";
      53                 :           0 :     case OP_14                     : return "14";
      54                 :           1 :     case OP_15                     : return "15";
      55                 :           9 :     case OP_16                     : return "16";
      56                 :             : 
      57                 :             :     // control
      58                 :          12 :     case OP_NOP                    : return "OP_NOP";
      59                 :          12 :     case OP_VER                    : return "OP_VER";
      60                 :          17 :     case OP_IF                     : return "OP_IF";
      61                 :          19 :     case OP_NOTIF                  : return "OP_NOTIF";
      62                 :          12 :     case OP_VERIF                  : return "OP_VERIF";
      63                 :          12 :     case OP_VERNOTIF               : return "OP_VERNOTIF";
      64                 :          20 :     case OP_ELSE                   : return "OP_ELSE";
      65                 :          24 :     case OP_ENDIF                  : return "OP_ENDIF";
      66                 :          19 :     case OP_VERIFY                 : return "OP_VERIFY";
      67                 :        2121 :     case OP_RETURN                 : return "OP_RETURN";
      68                 :             : 
      69                 :             :     // stack ops
      70                 :          17 :     case OP_TOALTSTACK             : return "OP_TOALTSTACK";
      71                 :          17 :     case OP_FROMALTSTACK           : return "OP_FROMALTSTACK";
      72                 :          12 :     case OP_2DROP                  : return "OP_2DROP";
      73                 :          12 :     case OP_2DUP                   : return "OP_2DUP";
      74                 :          12 :     case OP_3DUP                   : return "OP_3DUP";
      75                 :          12 :     case OP_2OVER                  : return "OP_2OVER";
      76                 :          12 :     case OP_2ROT                   : return "OP_2ROT";
      77                 :          12 :     case OP_2SWAP                  : return "OP_2SWAP";
      78                 :          14 :     case OP_IFDUP                  : return "OP_IFDUP";
      79                 :          12 :     case OP_DEPTH                  : return "OP_DEPTH";
      80                 :          29 :     case OP_DROP                   : return "OP_DROP";
      81                 :         453 :     case OP_DUP                    : return "OP_DUP";
      82                 :          12 :     case OP_NIP                    : return "OP_NIP";
      83                 :          12 :     case OP_OVER                   : return "OP_OVER";
      84                 :          12 :     case OP_PICK                   : return "OP_PICK";
      85                 :          12 :     case OP_ROLL                   : return "OP_ROLL";
      86                 :          12 :     case OP_ROT                    : return "OP_ROT";
      87                 :          13 :     case OP_SWAP                   : return "OP_SWAP";
      88                 :          12 :     case OP_TUCK                   : return "OP_TUCK";
      89                 :             : 
      90                 :             :     // splice ops
      91                 :          12 :     case OP_CAT                    : return "OP_CAT";
      92                 :          12 :     case OP_SUBSTR                 : return "OP_SUBSTR";
      93                 :          12 :     case OP_LEFT                   : return "OP_LEFT";
      94                 :          12 :     case OP_RIGHT                  : return "OP_RIGHT";
      95                 :          16 :     case OP_SIZE                   : return "OP_SIZE";
      96                 :             : 
      97                 :             :     // bit logic
      98                 :          12 :     case OP_INVERT                 : return "OP_INVERT";
      99                 :          12 :     case OP_AND                    : return "OP_AND";
     100                 :          12 :     case OP_OR                     : return "OP_OR";
     101                 :          12 :     case OP_XOR                    : return "OP_XOR";
     102                 :         460 :     case OP_EQUAL                  : return "OP_EQUAL";
     103                 :         456 :     case OP_EQUALVERIFY            : return "OP_EQUALVERIFY";
     104                 :          12 :     case OP_RESERVED1              : return "OP_RESERVED1";
     105                 :          12 :     case OP_RESERVED2              : return "OP_RESERVED2";
     106                 :             : 
     107                 :             :     // numeric
     108                 :          12 :     case OP_1ADD                   : return "OP_1ADD";
     109                 :          12 :     case OP_1SUB                   : return "OP_1SUB";
     110                 :          12 :     case OP_2MUL                   : return "OP_2MUL";
     111                 :          12 :     case OP_2DIV                   : return "OP_2DIV";
     112                 :          12 :     case OP_NEGATE                 : return "OP_NEGATE";
     113                 :          12 :     case OP_ABS                    : return "OP_ABS";
     114                 :          36 :     case OP_NOT                    : return "OP_NOT";
     115                 :          14 :     case OP_0NOTEQUAL              : return "OP_0NOTEQUAL";
     116                 :          14 :     case OP_ADD                    : return "OP_ADD";
     117                 :          12 :     case OP_SUB                    : return "OP_SUB";
     118                 :          12 :     case OP_MUL                    : return "OP_MUL";
     119                 :          12 :     case OP_DIV                    : return "OP_DIV";
     120                 :          12 :     case OP_MOD                    : return "OP_MOD";
     121                 :          12 :     case OP_LSHIFT                 : return "OP_LSHIFT";
     122                 :          12 :     case OP_RSHIFT                 : return "OP_RSHIFT";
     123                 :          16 :     case OP_BOOLAND                : return "OP_BOOLAND";
     124                 :          12 :     case OP_BOOLOR                 : return "OP_BOOLOR";
     125                 :          12 :     case OP_NUMEQUAL               : return "OP_NUMEQUAL";
     126                 :          12 :     case OP_NUMEQUALVERIFY         : return "OP_NUMEQUALVERIFY";
     127                 :          12 :     case OP_NUMNOTEQUAL            : return "OP_NUMNOTEQUAL";
     128                 :          12 :     case OP_LESSTHAN               : return "OP_LESSTHAN";
     129                 :          12 :     case OP_GREATERTHAN            : return "OP_GREATERTHAN";
     130                 :          12 :     case OP_LESSTHANOREQUAL        : return "OP_LESSTHANOREQUAL";
     131                 :          12 :     case OP_GREATERTHANOREQUAL     : return "OP_GREATERTHANOREQUAL";
     132                 :          12 :     case OP_MIN                    : return "OP_MIN";
     133                 :          12 :     case OP_MAX                    : return "OP_MAX";
     134                 :          12 :     case OP_WITHIN                 : return "OP_WITHIN";
     135                 :             : 
     136                 :             :     // crypto
     137                 :          12 :     case OP_RIPEMD160              : return "OP_RIPEMD160";
     138                 :          12 :     case OP_SHA1                   : return "OP_SHA1";
     139                 :          14 :     case OP_SHA256                 : return "OP_SHA256";
     140                 :         897 :     case OP_HASH160                : return "OP_HASH160";
     141                 :          12 :     case OP_HASH256                : return "OP_HASH256";
     142                 :          12 :     case OP_CODESEPARATOR          : return "OP_CODESEPARATOR";
     143                 :         766 :     case OP_CHECKSIG               : return "OP_CHECKSIG";
     144                 :          23 :     case OP_CHECKSIGVERIFY         : return "OP_CHECKSIGVERIFY";
     145                 :         100 :     case OP_CHECKMULTISIG          : return "OP_CHECKMULTISIG";
     146                 :          12 :     case OP_CHECKMULTISIGVERIFY    : return "OP_CHECKMULTISIGVERIFY";
     147                 :             : 
     148                 :             :     // expansion
     149                 :          12 :     case OP_NOP1                   : return "OP_NOP1";
     150                 :          19 :     case OP_CHECKLOCKTIMEVERIFY    : return "OP_CHECKLOCKTIMEVERIFY";
     151                 :          23 :     case OP_CHECKSEQUENCEVERIFY    : return "OP_CHECKSEQUENCEVERIFY";
     152                 :          12 :     case OP_NOP4                   : return "OP_NOP4";
     153                 :          12 :     case OP_NOP5                   : return "OP_NOP5";
     154                 :          12 :     case OP_NOP6                   : return "OP_NOP6";
     155                 :          12 :     case OP_NOP7                   : return "OP_NOP7";
     156                 :          16 :     case OP_NOP8                   : return "OP_NOP8";
     157                 :          12 :     case OP_NOP9                   : return "OP_NOP9";
     158                 :          12 :     case OP_NOP10                  : return "OP_NOP10";
     159                 :             : 
     160                 :             :     // Opcode added by BIP 342 (Tapscript)
     161                 :           1 :     case OP_CHECKSIGADD            : return "OP_CHECKSIGADD";
     162                 :             : 
     163                 :           0 :     case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";
     164                 :             : 
     165                 :             :     } // no default case, so the compiler can warn about missing cases
     166                 :           1 :     return "OP_UNKNOWN";
     167                 :             : }
     168                 :             : 
     169                 :     2446655 : unsigned int CScript::GetSigOpCount(bool fAccurate) const
     170                 :             : {
     171                 :     2446655 :     unsigned int n = 0;
     172         [ +  + ]:     2446655 :     const_iterator pc = begin();
     173                 :     2446655 :     opcodetype lastOpcode = OP_INVALIDOPCODE;
     174   [ +  +  +  + ]: 30456857302 :     while (pc < end())
     175                 :             :     {
     176                 : 15225982203 :         opcodetype opcode;
     177         [ +  + ]: 15225982203 :         if (!GetOp(pc, opcode))
     178                 :             :             break;
     179         [ +  + ]: 15225981996 :         if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
     180                 :   162627616 :             n++;
     181         [ +  + ]: 15063354380 :         else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
     182                 :             :         {
     183   [ +  +  +  + ]:       27447 :             if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
     184                 :         375 :                 n += DecodeOP_N(lastOpcode);
     185                 :             :             else
     186                 :       27072 :                 n += MAX_PUBKEYS_PER_MULTISIG;
     187                 :             :         }
     188                 :             :         lastOpcode = opcode;
     189                 :             :     }
     190                 :     2446655 :     return n;
     191                 :             : }
     192                 :             : 
     193                 :       80377 : unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
     194                 :             : {
     195         [ +  + ]:       80377 :     if (!IsPayToScriptHash())
     196                 :       66317 :         return GetSigOpCount(true);
     197                 :             : 
     198                 :             :     // This is a pay-to-script-hash scriptPubKey;
     199                 :             :     // get the last item that the scriptSig
     200                 :             :     // pushes onto the stack:
     201         [ +  + ]:       14060 :     const_iterator pc = scriptSig.begin();
     202                 :       14060 :     std::vector<unsigned char> vData;
     203   [ +  +  +  + ]:       74582 :     while (pc < scriptSig.end())
     204                 :             :     {
     205                 :       23232 :         opcodetype opcode;
     206   [ +  -  +  - ]:       23232 :         if (!scriptSig.GetOp(pc, opcode, vData))
     207                 :             :             return 0;
     208         [ +  + ]:       23232 :         if (opcode > OP_16)
     209                 :             :             return 0;
     210                 :             :     }
     211                 :             : 
     212                 :             :     /// ... and return its opcount:
     213                 :       14059 :     CScript subscript(vData.begin(), vData.end());
     214         [ +  - ]:       14059 :     return subscript.GetSigOpCount(true);
     215                 :       28119 : }
     216                 :             : 
     217                 :       64307 : bool CScript::IsPayToAnchor() const
     218                 :             : {
     219         [ +  + ]:       64307 :     return (this->size() == 4 &&
     220   [ +  +  +  + ]:         438 :         (*this)[0] == OP_1 &&
     221   [ +  +  +  - ]:         242 :         (*this)[1] == 0x02 &&
     222   [ +  +  +  +  :       64549 :         (*this)[2] == 0x4e &&
                   +  + ]
     223   [ +  +  -  + ]:         222 :         (*this)[3] == 0x73);
     224                 :             : }
     225                 :             : 
     226                 :        1054 : bool CScript::IsPayToAnchor(int version, const std::vector<unsigned char>& program)
     227                 :             : {
     228                 :        1664 :     return version == 1 &&
     229         [ +  + ]:         610 :         program.size() == 2 &&
     230   [ +  +  +  + ]:        1132 :         program[0] == 0x4e &&
     231         [ -  + ]:          75 :         program[1] == 0x73;
     232                 :             : }
     233                 :             : 
     234                 :     1716788 : bool CScript::IsPayToScriptHash() const
     235                 :             : {
     236                 :             :     // Extra-fast test for pay-to-script-hash CScripts:
     237         [ +  + ]:     1716788 :     return (this->size() == 23 &&
     238   [ +  -  +  + ]:      281832 :             (*this)[0] == OP_HASH160 &&
     239   [ +  +  +  -  :     1996810 :             (*this)[1] == 0x14 &&
                   +  - ]
     240   [ +  -  +  + ]:      280022 :             (*this)[22] == OP_EQUAL);
     241                 :             : }
     242                 :             : 
     243                 :        9658 : bool CScript::IsPayToWitnessScriptHash() const
     244                 :             : {
     245                 :             :     // Extra-fast test for pay-to-witness-script-hash CScripts:
     246         [ +  + ]:        9658 :     return (this->size() == 34 &&
     247   [ +  +  +  -  :       10263 :             (*this)[0] == OP_0 &&
                   +  + ]
     248   [ +  -  -  + ]:         972 :             (*this)[1] == 0x20);
     249                 :             : }
     250                 :             : 
     251                 :       47785 : bool CScript::IsPayToTaproot() const
     252                 :             : {
     253         [ -  + ]:       47785 :     return (this->size() == 34 &&
     254   [ +  +  +  -  :       58486 :             (*this)[0] == OP_1 &&
                   +  + ]
     255   [ +  -  -  + ]:       19710 :             (*this)[1] == 0x20);
     256                 :             : }
     257                 :             : 
     258                 :             : // A witness program is any valid CScript that consists of a 1-byte push opcode
     259                 :             : // followed by a data push between 2 and 40 bytes.
     260                 :     1921785 : bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
     261                 :             : {
     262   [ +  +  +  +  :     1963685 :     if (this->size() < 4 || this->size() > 42) {
             +  +  +  + ]
     263                 :             :         return false;
     264                 :             :     }
     265   [ +  +  +  +  :     3691768 :     if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
             +  +  +  + ]
     266                 :             :         return false;
     267                 :             :     }
     268   [ +  +  +  + ]:     2838450 :     if ((size_t)((*this)[1] + 2) == this->size()) {
     269                 :     1418518 :         version = DecodeOP_N((opcodetype)(*this)[0]);
     270         [ +  + ]:     4255554 :         program = std::vector<unsigned char>(this->begin() + 2, this->end());
     271                 :     1418518 :         return true;
     272                 :             :     }
     273                 :             :     return false;
     274                 :             : }
     275                 :             : 
     276                 :      342003 : bool CScript::IsPushOnly(const_iterator pc) const
     277                 :             : {
     278   [ +  +  +  + ]:   338238130 :     while (pc < end())
     279                 :             :     {
     280                 :   168787184 :         opcodetype opcode;
     281         [ +  + ]:   168787184 :         if (!GetOp(pc, opcode))
     282                 :             :             return false;
     283                 :             :         // Note that IsPushOnly() *does* consider OP_RESERVED to be a
     284                 :             :         // push-type opcode, however execution of OP_RESERVED fails, so
     285                 :             :         // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
     286                 :             :         // the P2SH special validation code being executed.
     287         [ +  + ]:   168786909 :         if (opcode > OP_16)
     288                 :             :             return false;
     289                 :             :     }
     290                 :             :     return true;
     291                 :             : }
     292                 :             : 
     293                 :      328595 : bool CScript::IsPushOnly() const
     294                 :             : {
     295         [ +  + ]:      657190 :     return this->IsPushOnly(begin());
     296                 :             : }
     297                 :             : 
     298                 :        4122 : std::string CScriptWitness::ToString() const
     299                 :             : {
     300   [ +  -  +  - ]:       22903 :     return "CScriptWitness(" + util::Join(*this, ", ", [](auto element) { return HexStr(element); }) + ")";
     301                 :             : }
     302                 :             : 
     303         [ +  + ]:      526393 : CScriptWitness::CScriptWitness(const std::vector<std::vector<unsigned char>>& stack)
     304                 :             : {
     305         [ +  + ]:      526393 :     if (stack.empty()) return;
     306         [ +  - ]:      995831 :     SpanWriter{Allocate(::GetSerializeSize(stack)), stack};
     307         [ -  - ]:           0 : }
     308                 :             : 
     309                 :      469438 : std::span<std::byte> CScriptWitness::Allocate(uint64_t size)
     310                 :             : {
     311                 :             :     // The header stores the size of the serialization in 32 bits. Together with the header, it must also fit in a
     312                 :             :     // size_t, which may be 32 bits too.
     313   [ -  +  -  - ]:      469438 :     if (size > std::numeric_limits<uint32_t>::max() - HEADER_SIZE) throw std::length_error("CScriptWitness too large");
     314         [ -  + ]:      469438 :     m_data = std::make_unique_for_overwrite<std::byte[]>(HEADER_SIZE + size);
     315                 :      469438 :     WriteLE32(m_data.get(), size);
     316                 :      469438 :     return {m_data.get() + HEADER_SIZE, static_cast<size_t>(size)};
     317                 :             : }
     318                 :             : 
     319         [ +  + ]:     8382037 : CScriptWitness::CScriptWitness(const CScriptWitness& other)
     320                 :             : {
     321         [ +  + ]:     8382037 :     if (other.empty()) return;
     322                 :      146214 :     const auto total = HEADER_SIZE + other.Data().size();
     323                 :      146214 :     m_data = std::make_unique_for_overwrite<std::byte[]>(total);
     324                 :      146214 :     std::copy_n(other.m_data.get(), total, m_data.get());
     325                 :             : }
     326                 :             : 
     327                 :      385851 : std::span<const unsigned char> CScriptWitness::operator[](size_t index) const noexcept
     328                 :             : {
     329                 :      385851 :     Assume(index < size());
     330                 :      385851 :     auto it = begin();
     331         [ +  + ]:     1564931 :     for (size_t i = 0; i < index; ++i) ++it;
     332                 :      385851 :     return *it;
     333                 :             : }
     334                 :             : 
     335                 :      330159 : std::vector<std::vector<unsigned char>> CScriptWitness::ToVectors() const
     336                 :             : {
     337                 :      330159 :     std::vector<std::vector<unsigned char>> ret;
     338         [ +  - ]:      330159 :     ret.reserve(size());
     339   [ +  -  +  + ]:     1777450 :     for (const auto element : *this) ret.emplace_back(element.begin(), element.end());
     340                 :      330159 :     return ret;
     341                 :           0 : }
     342                 :             : 
     343                 :      328932 : size_t CScriptWitness::DynamicMemoryUsage() const noexcept
     344                 :             : {
     345   [ +  +  +  - ]:      328932 :     return m_data ? memusage::MallocUsage(HEADER_SIZE + Data().size()) : 0;
     346                 :             : }
     347                 :             : 
     348                 :      285131 : bool CScript::HasValidOps() const
     349                 :             : {
     350         [ +  + ]:      285131 :     CScript::const_iterator it = begin();
     351   [ +  +  +  + ]:    66414018 :     while (it < end()) {
     352                 :    32923339 :         opcodetype opcode;
     353                 :    32923339 :         std::vector<unsigned char> item;
     354   [ +  -  +  +  :    65846664 :         if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
             +  +  +  + ]
     355                 :        1461 :             return false;
     356                 :             :         }
     357                 :    32923339 :     }
     358                 :             :     return true;
     359                 :             : }
     360                 :             : 
     361                 : 15487425707 : bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
     362                 :             : {
     363                 : 15487425707 :     opcodeRet = OP_INVALIDOPCODE;
     364         [ +  + ]: 15487425707 :     if (pvchRet)
     365         [ +  + ]:    79357251 :         pvchRet->clear();
     366   [ +  +  +  + ]: 30974851414 :     if (pc >= end)
     367                 :             :         return false;
     368                 :             : 
     369                 :             :     // Read instruction
     370         [ +  - ]: 15486931477 :     if (end - pc < 1)
     371                 :             :         return false;
     372         [ +  + ]: 15486931477 :     unsigned int opcode = *pc++;
     373                 :             : 
     374                 :             :     // Immediate operand
     375         [ +  + ]: 15486931477 :     if (opcode <= OP_PUSHDATA4)
     376                 :             :     {
     377                 :     8431572 :         unsigned int nSize = 0;
     378         [ +  + ]:     8431572 :         if (opcode < OP_PUSHDATA1)
     379                 :             :         {
     380                 :             :             nSize = opcode;
     381                 :             :         }
     382         [ +  + ]:       59348 :         else if (opcode == OP_PUSHDATA1)
     383                 :             :         {
     384         [ +  + ]:       18787 :             if (end - pc < 1)
     385                 :             :                 return false;
     386                 :       18521 :             nSize = *pc++;
     387                 :             :         }
     388         [ +  + ]:       40561 :         else if (opcode == OP_PUSHDATA2)
     389                 :             :         {
     390         [ +  - ]:       21540 :             if (end - pc < 2)
     391                 :             :                 return false;
     392                 :       21540 :             nSize = ReadLE16(&pc[0]);
     393                 :       21540 :             pc += 2;
     394                 :             :         }
     395         [ +  - ]:       19021 :         else if (opcode == OP_PUSHDATA4)
     396                 :             :         {
     397         [ +  - ]:       19021 :             if (end - pc < 4)
     398                 :             :                 return false;
     399                 :       19021 :             nSize = ReadLE32(&pc[0]);
     400                 :       19021 :             pc += 4;
     401                 :             :         }
     402   [ +  -  +  + ]:     8431306 :         if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
     403                 :         910 :             return false;
     404         [ +  + ]:     8430396 :         if (pvchRet)
     405                 :     3833370 :             pvchRet->assign(pc, pc + nSize);
     406                 :     8430396 :         pc += nSize;
     407                 :             :     }
     408                 :             : 
     409                 : 15486930301 :     opcodeRet = static_cast<opcodetype>(opcode);
     410                 : 15486930301 :     return true;
     411                 :             : }
     412                 :             : 
     413                 :     9168751 : bool IsOpSuccess(const opcodetype& opcode)
     414                 :             : {
     415   [ +  +  +  + ]:     9168381 :     return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
     416   [ +  +  +  + ]:     9168298 :            (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
     417   [ +  +  +  +  :    18336944 :            (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
                   +  + ]
     418         [ +  + ]:     9168064 :            (opcode >= 187 && opcode <= 254);
     419                 :             : }
     420                 :             : 
     421                 :     1202519 : bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) {
     422                 :             :     // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
     423         [ -  + ]:     1202519 :     assert(0 <= opcode && opcode <= OP_PUSHDATA4);
     424   [ -  +  +  + ]:     1202519 :     if (data.size() == 0) {
     425                 :             :         // Should have used OP_0.
     426                 :      521534 :         return opcode == OP_0;
     427   [ +  +  +  +  :      680985 :     } else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
                   +  + ]
     428                 :             :         // Should have used OP_1 .. OP_16.
     429                 :             :         return false;
     430   [ +  +  +  + ]:      678942 :     } else if (data.size() == 1 && data[0] == 0x81) {
     431                 :             :         // Should have used OP_1NEGATE.
     432                 :             :         return false;
     433         [ +  + ]:      678739 :     } else if (data.size() <= 75) {
     434                 :             :         // Must have used a direct push (opcode indicating number of bytes pushed + those bytes).
     435                 :      677248 :         return opcode == data.size();
     436         [ +  + ]:        1491 :     } else if (data.size() <= 255) {
     437                 :             :         // Must have used OP_PUSHDATA.
     438                 :        1070 :         return opcode == OP_PUSHDATA1;
     439         [ +  - ]:         421 :     } else if (data.size() <= 65535) {
     440                 :             :         // Must have used OP_PUSHDATA2.
     441                 :         421 :         return opcode == OP_PUSHDATA2;
     442                 :             :     }
     443                 :             :     return true;
     444                 :             : }
        

Generated by: LCOV version 2.0-1