Changeset View
Changeset View
Standalone View
Standalone View
src/compressor.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_COMPRESSOR_H | #ifndef BITCOIN_COMPRESSOR_H | ||||
#define BITCOIN_COMPRESSOR_H | #define BITCOIN_COMPRESSOR_H | ||||
#include <primitives/transaction.h> | #include <primitives/transaction.h> | ||||
#include <script/script.h> | #include <script/script.h> | ||||
#include <serialize.h> | #include <serialize.h> | ||||
#include <span.h> | |||||
class CKeyID; | class CKeyID; | ||||
class CPubKey; | class CPubKey; | ||||
class CScriptID; | class CScriptID; | ||||
bool CompressScript(const CScript &script, std::vector<uint8_t> &out); | bool CompressScript(const CScript &script, std::vector<uint8_t> &out); | ||||
unsigned int GetSpecialScriptSize(unsigned int nSize); | unsigned int GetSpecialScriptSize(unsigned int nSize); | ||||
bool DecompressScript(CScript &script, unsigned int nSize, | bool DecompressScript(CScript &script, unsigned int nSize, | ||||
Show All 27 Lines | private: | ||||
CScript &script; | CScript &script; | ||||
public: | public: | ||||
explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) {} | explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) {} | ||||
template <typename Stream> void Serialize(Stream &s) const { | template <typename Stream> void Serialize(Stream &s) const { | ||||
std::vector<uint8_t> compr; | std::vector<uint8_t> compr; | ||||
if (CompressScript(script, compr)) { | if (CompressScript(script, compr)) { | ||||
s << CFlatData(compr); | s << MakeSpan(compr); | ||||
return; | return; | ||||
} | } | ||||
unsigned int nSize = script.size() + nSpecialScripts; | unsigned int nSize = script.size() + nSpecialScripts; | ||||
s << VARINT(nSize); | s << VARINT(nSize); | ||||
s << CFlatData(script); | s << MakeSpan(script); | ||||
} | } | ||||
template <typename Stream> void Unserialize(Stream &s) { | template <typename Stream> void Unserialize(Stream &s) { | ||||
unsigned int nSize = 0; | unsigned int nSize = 0; | ||||
s >> VARINT(nSize); | s >> VARINT(nSize); | ||||
if (nSize < nSpecialScripts) { | if (nSize < nSpecialScripts) { | ||||
std::vector<uint8_t> vch(GetSpecialScriptSize(nSize), 0x00); | std::vector<uint8_t> vch(GetSpecialScriptSize(nSize), 0x00); | ||||
s >> CFlatData(vch); | s >> MakeSpan(vch); | ||||
DecompressScript(script, nSize, vch); | DecompressScript(script, nSize, vch); | ||||
return; | return; | ||||
} | } | ||||
nSize -= nSpecialScripts; | nSize -= nSpecialScripts; | ||||
if (nSize > MAX_SCRIPT_SIZE) { | if (nSize > MAX_SCRIPT_SIZE) { | ||||
// Overly long script, replace with a short invalid one | // Overly long script, replace with a short invalid one | ||||
script << OP_RETURN; | script << OP_RETURN; | ||||
s.ignore(nSize); | s.ignore(nSize); | ||||
} else { | } else { | ||||
script.resize(nSize); | script.resize(nSize); | ||||
s >> CFlatData(script); | s >> MakeSpan(script); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
/** wrapper for CTxOut that provides a more compact serialization */ | /** wrapper for CTxOut that provides a more compact serialization */ | ||||
class CTxOutCompressor { | class CTxOutCompressor { | ||||
private: | private: | ||||
CTxOut &txout; | CTxOut &txout; | ||||
Show All 22 Lines |