LCOV - code coverage report
Current view: top level - src/script - script.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 130 151 86.1 %
Date: 2015-10-12 22:39:14 Functions: 8 9 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2014 The Bitcoin Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #include "script.h"
       7             : 
       8             : #include "tinyformat.h"
       9             : #include "utilstrencodings.h"
      10             : 
      11             : using namespace std;
      12             : 
      13         698 : const char* GetOpName(opcodetype opcode)
      14             : {
      15         698 :     switch (opcode)
      16             :     {
      17             :     // push value
      18             :     case OP_0                      : return "0";
      19           0 :     case OP_PUSHDATA1              : return "OP_PUSHDATA1";
      20           0 :     case OP_PUSHDATA2              : return "OP_PUSHDATA2";
      21           0 :     case OP_PUSHDATA4              : return "OP_PUSHDATA4";
      22           0 :     case OP_1NEGATE                : return "-1";
      23           2 :     case OP_RESERVED               : return "OP_RESERVED";
      24           1 :     case OP_1                      : return "1";
      25           1 :     case OP_2                      : return "2";
      26           1 :     case OP_3                      : return "3";
      27           0 :     case OP_4                      : return "4";
      28           0 :     case OP_5                      : return "5";
      29           0 :     case OP_6                      : return "6";
      30           0 :     case OP_7                      : return "7";
      31           0 :     case OP_8                      : return "8";
      32           0 :     case OP_9                      : return "9";
      33           0 :     case OP_10                     : return "10";
      34           0 :     case OP_11                     : return "11";
      35           0 :     case OP_12                     : return "12";
      36           0 :     case OP_13                     : return "13";
      37           0 :     case OP_14                     : return "14";
      38           0 :     case OP_15                     : return "15";
      39           0 :     case OP_16                     : return "16";
      40             : 
      41             :     // control
      42           2 :     case OP_NOP                    : return "OP_NOP";
      43           2 :     case OP_VER                    : return "OP_VER";
      44           3 :     case OP_IF                     : return "OP_IF";
      45           2 :     case OP_NOTIF                  : return "OP_NOTIF";
      46           2 :     case OP_VERIF                  : return "OP_VERIF";
      47           2 :     case OP_VERNOTIF               : return "OP_VERNOTIF";
      48           3 :     case OP_ELSE                   : return "OP_ELSE";
      49           3 :     case OP_ENDIF                  : return "OP_ENDIF";
      50           2 :     case OP_VERIFY                 : return "OP_VERIFY";
      51           7 :     case OP_RETURN                 : return "OP_RETURN";
      52             : 
      53             :     // stack ops
      54           2 :     case OP_TOALTSTACK             : return "OP_TOALTSTACK";
      55           2 :     case OP_FROMALTSTACK           : return "OP_FROMALTSTACK";
      56           2 :     case OP_2DROP                  : return "OP_2DROP";
      57           2 :     case OP_2DUP                   : return "OP_2DUP";
      58           2 :     case OP_3DUP                   : return "OP_3DUP";
      59           2 :     case OP_2OVER                  : return "OP_2OVER";
      60           2 :     case OP_2ROT                   : return "OP_2ROT";
      61           2 :     case OP_2SWAP                  : return "OP_2SWAP";
      62           2 :     case OP_IFDUP                  : return "OP_IFDUP";
      63           2 :     case OP_DEPTH                  : return "OP_DEPTH";
      64           3 :     case OP_DROP                   : return "OP_DROP";
      65          95 :     case OP_DUP                    : return "OP_DUP";
      66           2 :     case OP_NIP                    : return "OP_NIP";
      67           2 :     case OP_OVER                   : return "OP_OVER";
      68           2 :     case OP_PICK                   : return "OP_PICK";
      69           2 :     case OP_ROLL                   : return "OP_ROLL";
      70           2 :     case OP_ROT                    : return "OP_ROT";
      71           2 :     case OP_SWAP                   : return "OP_SWAP";
      72           2 :     case OP_TUCK                   : return "OP_TUCK";
      73             : 
      74             :     // splice ops
      75           2 :     case OP_CAT                    : return "OP_CAT";
      76           2 :     case OP_SUBSTR                 : return "OP_SUBSTR";
      77           2 :     case OP_LEFT                   : return "OP_LEFT";
      78           2 :     case OP_RIGHT                  : return "OP_RIGHT";
      79           2 :     case OP_SIZE                   : return "OP_SIZE";
      80             : 
      81             :     // bit logic
      82           2 :     case OP_INVERT                 : return "OP_INVERT";
      83           2 :     case OP_AND                    : return "OP_AND";
      84           2 :     case OP_OR                     : return "OP_OR";
      85           2 :     case OP_XOR                    : return "OP_XOR";
      86          20 :     case OP_EQUAL                  : return "OP_EQUAL";
      87          93 :     case OP_EQUALVERIFY            : return "OP_EQUALVERIFY";
      88           2 :     case OP_RESERVED1              : return "OP_RESERVED1";
      89           2 :     case OP_RESERVED2              : return "OP_RESERVED2";
      90             : 
      91             :     // numeric
      92           2 :     case OP_1ADD                   : return "OP_1ADD";
      93           2 :     case OP_1SUB                   : return "OP_1SUB";
      94           2 :     case OP_2MUL                   : return "OP_2MUL";
      95           2 :     case OP_2DIV                   : return "OP_2DIV";
      96           2 :     case OP_NEGATE                 : return "OP_NEGATE";
      97           2 :     case OP_ABS                    : return "OP_ABS";
      98          26 :     case OP_NOT                    : return "OP_NOT";
      99           2 :     case OP_0NOTEQUAL              : return "OP_0NOTEQUAL";
     100           2 :     case OP_ADD                    : return "OP_ADD";
     101           2 :     case OP_SUB                    : return "OP_SUB";
     102           2 :     case OP_MUL                    : return "OP_MUL";
     103           2 :     case OP_DIV                    : return "OP_DIV";
     104           2 :     case OP_MOD                    : return "OP_MOD";
     105           2 :     case OP_LSHIFT                 : return "OP_LSHIFT";
     106           2 :     case OP_RSHIFT                 : return "OP_RSHIFT";
     107           2 :     case OP_BOOLAND                : return "OP_BOOLAND";
     108           2 :     case OP_BOOLOR                 : return "OP_BOOLOR";
     109           2 :     case OP_NUMEQUAL               : return "OP_NUMEQUAL";
     110           2 :     case OP_NUMEQUALVERIFY         : return "OP_NUMEQUALVERIFY";
     111           2 :     case OP_NUMNOTEQUAL            : return "OP_NUMNOTEQUAL";
     112           2 :     case OP_LESSTHAN               : return "OP_LESSTHAN";
     113           2 :     case OP_GREATERTHAN            : return "OP_GREATERTHAN";
     114           2 :     case OP_LESSTHANOREQUAL        : return "OP_LESSTHANOREQUAL";
     115           2 :     case OP_GREATERTHANOREQUAL     : return "OP_GREATERTHANOREQUAL";
     116           2 :     case OP_MIN                    : return "OP_MIN";
     117           2 :     case OP_MAX                    : return "OP_MAX";
     118           2 :     case OP_WITHIN                 : return "OP_WITHIN";
     119             : 
     120             :     // crypto
     121           2 :     case OP_RIPEMD160              : return "OP_RIPEMD160";
     122           2 :     case OP_SHA1                   : return "OP_SHA1";
     123           2 :     case OP_SHA256                 : return "OP_SHA256";
     124         111 :     case OP_HASH160                : return "OP_HASH160";
     125           2 :     case OP_HASH256                : return "OP_HASH256";
     126           2 :     case OP_CODESEPARATOR          : return "OP_CODESEPARATOR";
     127         142 :     case OP_CHECKSIG               : return "OP_CHECKSIG";
     128           3 :     case OP_CHECKSIGVERIFY         : return "OP_CHECKSIGVERIFY";
     129          27 :     case OP_CHECKMULTISIG          : return "OP_CHECKMULTISIG";
     130           2 :     case OP_CHECKMULTISIGVERIFY    : return "OP_CHECKMULTISIGVERIFY";
     131             : 
     132             :     // expanson
     133           2 :     case OP_NOP1                   : return "OP_NOP1";
     134           7 :     case OP_NOP2                   : return "OP_NOP2";
     135           2 :     case OP_NOP3                   : return "OP_NOP3";
     136           2 :     case OP_NOP4                   : return "OP_NOP4";
     137           2 :     case OP_NOP5                   : return "OP_NOP5";
     138           2 :     case OP_NOP6                   : return "OP_NOP6";
     139           2 :     case OP_NOP7                   : return "OP_NOP7";
     140           2 :     case OP_NOP8                   : return "OP_NOP8";
     141           2 :     case OP_NOP9                   : return "OP_NOP9";
     142           2 :     case OP_NOP10                  : return "OP_NOP10";
     143             : 
     144           0 :     case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";
     145             : 
     146             :     // Note:
     147             :     //  The template matching params OP_SMALLINTEGER/etc are defined in opcodetype enum
     148             :     //  as kind of implementation hack, they are *NOT* real opcodes.  If found in real
     149             :     //  Script, just let the default: case deal with them.
     150             : 
     151             :     default:
     152           0 :         return "OP_UNKNOWN";
     153             :     }
     154             : }
     155             : 
     156       76434 : unsigned int CScript::GetSigOpCount(bool fAccurate) const
     157             : {
     158       76434 :     unsigned int n = 0;
     159      152868 :     const_iterator pc = begin();
     160       76434 :     opcodetype lastOpcode = OP_INVALIDOPCODE;
     161     8269173 :     while (pc < end())
     162             :     {
     163             :         opcodetype opcode;
     164     2654785 :         if (!GetOp(pc, opcode))
     165             :             break;
     166     2654479 :         if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
     167      493774 :             n++;
     168     2160705 :         else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
     169             :         {
     170       16017 :             if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
     171          40 :                 n += DecodeOP_N(lastOpcode);
     172             :             else
     173       15977 :                 n += 20;
     174             :         }
     175     2654479 :         lastOpcode = opcode;
     176             :     }
     177       76434 :     return n;
     178             : }
     179             : 
     180          36 : unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
     181             : {
     182          36 :     if (!IsPayToScriptHash())
     183           0 :         return GetSigOpCount(true);
     184             : 
     185             :     // This is a pay-to-script-hash scriptPubKey;
     186             :     // get the last item that the scriptSig
     187             :     // pushes onto the stack:
     188          72 :     const_iterator pc = scriptSig.begin();
     189             :     vector<unsigned char> data;
     190         552 :     while (pc < scriptSig.end())
     191             :     {
     192             :         opcodetype opcode;
     193         148 :         if (!scriptSig.GetOp(pc, opcode, data))
     194           0 :             return 0;
     195         148 :         if (opcode > OP_16)
     196             :             return 0;
     197             :     }
     198             : 
     199             :     /// ... and return its opcount:
     200          72 :     CScript subscript(data.begin(), data.end());
     201          36 :     return subscript.GetSigOpCount(true);
     202             : }
     203             : 
     204       50187 : bool CScript::IsPayToScriptHash() const
     205             : {
     206             :     // Extra-fast test for pay-to-script-hash CScripts:
     207       50884 :     return (this->size() == 23 &&
     208        2076 :             this->at(0) == OP_HASH160 &&
     209       52233 :             this->at(1) == 0x14 &&
     210       51551 :             this->at(22) == OP_EQUAL);
     211             : }
     212             : 
     213       67697 : bool CScript::IsPushOnly(const_iterator pc) const
     214             : {
     215      474842 :     while (pc < end())
     216             :     {
     217             :         opcodetype opcode;
     218       68028 :         if (!GetOp(pc, opcode))
     219          10 :             return false;
     220             :         // Note that IsPushOnly() *does* consider OP_RESERVED to be a
     221             :         // push-type opcode, however execution of OP_RESERVED fails, so
     222             :         // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
     223             :         // the P2SH special validation code being executed.
     224       68027 :         if (opcode > OP_16)
     225             :             return false;
     226             :     }
     227             :     return true;
     228             : }
     229             : 
     230       67679 : bool CScript::IsPushOnly() const
     231             : {
     232      135358 :     return this->IsPushOnly(begin());
     233         330 : }

Generated by: LCOV version 1.11