LCOV - code coverage report
Current view: top level - src - core_read.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 135 0.0 %
Date: 2023-11-10 23:46:46 Functions: 0 12 0.0 %
Branches: 0 259 0.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2022 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #include <core_io.h>
       6                 :            : 
       7                 :            : #include <primitives/block.h>
       8                 :            : #include <primitives/transaction.h>
       9                 :            : #include <script/script.h>
      10                 :            : #include <script/sign.h>
      11                 :            : #include <serialize.h>
      12                 :            : #include <streams.h>
      13                 :            : #include <util/result.h>
      14                 :            : #include <util/strencodings.h>
      15                 :            : #include <version.h>
      16                 :            : 
      17                 :            : #include <algorithm>
      18                 :            : #include <string>
      19                 :            : 
      20                 :            : namespace {
      21                 :          0 : class OpCodeParser
      22                 :            : {
      23                 :            : private:
      24                 :            :     std::map<std::string, opcodetype> mapOpNames;
      25                 :            : 
      26                 :            : public:
      27                 :          0 :     OpCodeParser()
      28                 :            :     {
      29         [ #  # ]:          0 :         for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
      30                 :            :             // Allow OP_RESERVED to get into mapOpNames
      31 [ #  # ][ #  # ]:          0 :             if (op < OP_NOP && op != OP_RESERVED) {
      32                 :          0 :                 continue;
      33                 :            :             }
      34                 :            : 
      35         [ #  # ]:          0 :             std::string strName = GetOpName(static_cast<opcodetype>(op));
      36 [ #  # ][ #  # ]:          0 :             if (strName == "OP_UNKNOWN") {
      37                 :          0 :                 continue;
      38                 :            :             }
      39         [ #  # ]:          0 :             mapOpNames[strName] = static_cast<opcodetype>(op);
      40                 :            :             // Convenience: OP_ADD and just ADD are both recognized:
      41 [ #  # ][ #  # ]:          0 :             if (strName.compare(0, 3, "OP_") == 0) { // strName starts with "OP_"
      42 [ #  # ][ #  # ]:          0 :                 mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
      43                 :          0 :             }
      44      [ #  #  # ]:          0 :         }
      45                 :          0 :     }
      46                 :          0 :     opcodetype Parse(const std::string& s) const
      47                 :            :     {
      48                 :          0 :         auto it = mapOpNames.find(s);
      49 [ #  # ][ #  # ]:          0 :         if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
      50                 :          0 :         return it->second;
      51                 :          0 :     }
      52                 :            : };
      53                 :            : 
      54                 :          0 : opcodetype ParseOpCode(const std::string& s)
      55                 :            : {
      56 [ #  # ][ #  # ]:          0 :     static const OpCodeParser ocp;
                 [ #  # ]
      57                 :          0 :     return ocp.Parse(s);
      58                 :          0 : }
      59                 :            : 
      60                 :            : } // namespace
      61                 :            : 
      62                 :          0 : CScript ParseScript(const std::string& s)
      63                 :            : {
      64                 :          0 :     CScript result;
      65                 :            : 
      66         [ #  # ]:          0 :     std::vector<std::string> words = SplitString(s, " \t\n");
      67                 :            : 
      68         [ #  # ]:          0 :     for (const std::string& w : words) {
      69         [ #  # ]:          0 :         if (w.empty()) {
      70                 :            :             // Empty string, ignore. (SplitString doesn't combine multiple separators)
      71 [ #  # ][ #  # ]:          0 :         } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
                 [ #  # ]
      72 [ #  # ][ #  # ]:          0 :                    (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
                 [ #  # ]
      73                 :            :         {
      74                 :            :             // Number
      75         [ #  # ]:          0 :             const auto num{ToIntegral<int64_t>(w)};
      76                 :            : 
      77                 :            :             // limit the range of numbers ParseScript accepts in decimal
      78                 :            :             // since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
      79 [ #  # ][ #  # ]:          0 :             if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
         [ #  # ][ #  # ]
                 [ #  # ]
      80 [ #  # ][ #  # ]:          0 :                 throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
      81                 :            :                                          "range -0xFFFFFFFF...0xFFFFFFFF");
      82                 :            :             }
      83                 :            : 
      84 [ #  # ][ #  # ]:          0 :             result << num.value();
      85 [ #  # ][ #  # ]:          0 :         } else if (w.substr(0, 2) == "0x" && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      86                 :            :             // Raw hex data, inserted NOT pushed onto stack:
      87 [ #  # ][ #  # ]:          0 :             std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
      88 [ #  # ][ #  # ]:          0 :             result.insert(result.end(), raw.begin(), raw.end());
      89 [ #  # ][ #  # ]:          0 :         } else if (w.size() >= 2 && w.front() == '\'' && w.back() == '\'') {
                 [ #  # ]
      90                 :            :             // Single-quoted string, pushed as data. NOTE: this is poor-man's
      91                 :            :             // parsing, spaces/tabs/newlines in single-quoted strings won't work.
      92         [ #  # ]:          0 :             std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
      93         [ #  # ]:          0 :             result << value;
      94                 :          0 :         } else {
      95                 :            :             // opcode, e.g. OP_ADD or ADD:
      96 [ #  # ][ #  # ]:          0 :             result << ParseOpCode(w);
      97                 :            :         }
      98                 :            :     }
      99                 :            : 
     100                 :          0 :     return result;
     101         [ #  # ]:          0 : }
     102                 :            : 
     103                 :            : // Check that all of the input and output scripts of a transaction contains valid opcodes
     104                 :          0 : static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
     105                 :            : {
     106                 :            :     // Check input scripts for non-coinbase txs
     107 [ #  # ][ #  # ]:          0 :     if (!CTransaction(tx).IsCoinBase()) {
     108         [ #  # ]:          0 :         for (unsigned int i = 0; i < tx.vin.size(); i++) {
     109 [ #  # ][ #  # ]:          0 :             if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
     110                 :          0 :                 return false;
     111                 :            :             }
     112                 :          0 :         }
     113                 :          0 :     }
     114                 :            :     // Check output scripts
     115         [ #  # ]:          0 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
     116 [ #  # ][ #  # ]:          0 :         if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
     117                 :          0 :             return false;
     118                 :            :         }
     119                 :          0 :     }
     120                 :            : 
     121                 :          0 :     return true;
     122                 :          0 : }
     123                 :            : 
     124                 :          0 : static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
     125                 :            : {
     126                 :            :     // General strategy:
     127                 :            :     // - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
     128                 :            :     //   the presence of witnesses) and with legacy serialization (which interprets the tag as a
     129                 :            :     //   0-input 1-output incomplete transaction).
     130                 :            :     //   - Restricted by try_no_witness (which disables legacy if false) and try_witness (which
     131                 :            :     //     disables extended if false).
     132                 :            :     //   - Ignore serializations that do not fully consume the hex string.
     133                 :            :     // - If neither succeeds, fail.
     134                 :            :     // - If only one succeeds, return that one.
     135                 :            :     // - If both decode attempts succeed:
     136                 :            :     //   - If only one passes the CheckTxScriptsSanity check, return that one.
     137                 :            :     //   - If neither or both pass CheckTxScriptsSanity, return the extended one.
     138                 :            : 
     139         [ #  # ]:          0 :     CMutableTransaction tx_extended, tx_legacy;
     140                 :          0 :     bool ok_extended = false, ok_legacy = false;
     141                 :            : 
     142                 :            :     // Try decoding with extended serialization support, and remember if the result successfully
     143                 :            :     // consumes the entire input.
     144         [ #  # ]:          0 :     if (try_witness) {
     145 [ #  # ][ #  # ]:          0 :         CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION);
     146                 :            :         try {
     147         [ #  # ]:          0 :             ssData >> tx_extended;
     148 [ #  # ][ #  # ]:          0 :             if (ssData.empty()) ok_extended = true;
     149         [ #  # ]:          0 :         } catch (const std::exception&) {
     150                 :            :             // Fall through.
     151         [ #  # ]:          0 :         }
     152                 :          0 :     }
     153                 :            : 
     154                 :            :     // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
     155                 :            :     // don't bother decoding the other way.
     156 [ #  # ][ #  # ]:          0 :     if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
                 [ #  # ]
     157                 :          0 :         tx = std::move(tx_extended);
     158                 :          0 :         return true;
     159                 :            :     }
     160                 :            : 
     161                 :            :     // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
     162         [ #  # ]:          0 :     if (try_no_witness) {
     163 [ #  # ][ #  # ]:          0 :         CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
     164                 :            :         try {
     165         [ #  # ]:          0 :             ssData >> tx_legacy;
     166 [ #  # ][ #  # ]:          0 :             if (ssData.empty()) ok_legacy = true;
     167         [ #  # ]:          0 :         } catch (const std::exception&) {
     168                 :            :             // Fall through.
     169         [ #  # ]:          0 :         }
     170                 :          0 :     }
     171                 :            : 
     172                 :            :     // If legacy decoding succeeded and passes CheckTxScriptsSanity, that's our answer, as we know
     173                 :            :     // at this point that extended decoding either failed or doesn't pass the sanity check.
     174 [ #  # ][ #  # ]:          0 :     if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
                 [ #  # ]
     175                 :          0 :         tx = std::move(tx_legacy);
     176                 :          0 :         return true;
     177                 :            :     }
     178                 :            : 
     179                 :            :     // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
     180         [ #  # ]:          0 :     if (ok_extended) {
     181                 :          0 :         tx = std::move(tx_extended);
     182                 :          0 :         return true;
     183                 :            :     }
     184                 :            : 
     185                 :            :     // If legacy decoding succeeded and extended didn't, return the legacy one.
     186         [ #  # ]:          0 :     if (ok_legacy) {
     187                 :          0 :         tx = std::move(tx_legacy);
     188                 :          0 :         return true;
     189                 :            :     }
     190                 :            : 
     191                 :            :     // If none succeeded, we failed.
     192                 :          0 :     return false;
     193                 :          0 : }
     194                 :            : 
     195                 :          0 : bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
     196                 :            : {
     197         [ #  # ]:          0 :     if (!IsHex(hex_tx)) {
     198                 :          0 :         return false;
     199                 :            :     }
     200                 :            : 
     201                 :          0 :     std::vector<unsigned char> txData(ParseHex(hex_tx));
     202         [ #  # ]:          0 :     return DecodeTx(tx, txData, try_no_witness, try_witness);
     203                 :          0 : }
     204                 :            : 
     205                 :          0 : bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
     206                 :            : {
     207         [ #  # ]:          0 :     if (!IsHex(hex_header)) return false;
     208                 :            : 
     209                 :          0 :     const std::vector<unsigned char> header_data{ParseHex(hex_header)};
     210 [ #  # ][ #  # ]:          0 :     DataStream ser_header{header_data};
     211                 :            :     try {
     212         [ #  # ]:          0 :         ser_header >> header;
     213         [ #  # ]:          0 :     } catch (const std::exception&) {
     214                 :          0 :         return false;
     215         [ #  # ]:          0 :     }
     216                 :          0 :     return true;
     217                 :          0 : }
     218                 :            : 
     219                 :          0 : bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
     220                 :            : {
     221         [ #  # ]:          0 :     if (!IsHex(strHexBlk))
     222                 :          0 :         return false;
     223                 :            : 
     224                 :          0 :     std::vector<unsigned char> blockData(ParseHex(strHexBlk));
     225 [ #  # ][ #  # ]:          0 :     CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
     226                 :            :     try {
     227         [ #  # ]:          0 :         ssBlock >> block;
     228         [ #  # ]:          0 :     }
     229                 :            :     catch (const std::exception&) {
     230                 :          0 :         return false;
     231         [ #  # ]:          0 :     }
     232                 :            : 
     233                 :          0 :     return true;
     234                 :          0 : }
     235                 :            : 
     236                 :          0 : bool ParseHashStr(const std::string& strHex, uint256& result)
     237                 :            : {
     238 [ #  # ][ #  # ]:          0 :     if ((strHex.size() != 64) || !IsHex(strHex))
     239                 :          0 :         return false;
     240                 :            : 
     241                 :          0 :     result.SetHex(strHex);
     242                 :          0 :     return true;
     243                 :          0 : }
     244                 :            : 
     245                 :          0 : util::Result<int> SighashFromStr(const std::string& sighash)
     246                 :            : {
     247 [ #  # ][ #  # ]:          0 :     static std::map<std::string, int> map_sighash_values = {
         [ #  # ][ #  # ]
     248 [ #  # ][ #  # ]:          0 :         {std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
     249 [ #  # ][ #  # ]:          0 :         {std::string("ALL"), int(SIGHASH_ALL)},
     250 [ #  # ][ #  # ]:          0 :         {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
     251 [ #  # ][ #  # ]:          0 :         {std::string("NONE"), int(SIGHASH_NONE)},
     252 [ #  # ][ #  # ]:          0 :         {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
     253 [ #  # ][ #  # ]:          0 :         {std::string("SINGLE"), int(SIGHASH_SINGLE)},
     254 [ #  # ][ #  # ]:          0 :         {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
     255                 :            :     };
     256                 :          0 :     const auto& it = map_sighash_values.find(sighash);
     257         [ #  # ]:          0 :     if (it != map_sighash_values.end()) {
     258                 :          0 :         return it->second;
     259                 :            :     } else {
     260 [ #  # ][ #  # ]:          0 :         return util::Error{Untranslated(sighash + " is not a valid sighash parameter.")};
     261                 :            :     }
     262                 :          0 : }

Generated by: LCOV version 1.14