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

Generated by: LCOV version 1.14