Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
compressor.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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#ifndef BITCOIN_COMPRESSOR_H
7#define BITCOIN_COMPRESSOR_H
8
10#include <script/script.h>
11#include <serialize.h>
12#include <span.h>
13
14bool CompressScript(const CScript &script, std::vector<uint8_t> &out);
15unsigned int GetSpecialScriptSize(unsigned int nSize);
16bool DecompressScript(CScript &script, unsigned int nSize,
17 const std::vector<uint8_t> &out);
18
25
27
47 static const unsigned int nSpecialScripts = 6;
48
49 template <typename Stream>
50 void Ser(Stream &s, const CScript &script) const {
51 std::vector<uint8_t> compr;
52 if (CompressScript(script, compr)) {
53 s << Span{compr};
54 return;
55 }
56 unsigned int nSize = script.size() + nSpecialScripts;
57 s << VARINT(nSize);
58 s << Span{script};
59 }
60
61 template <typename Stream> void Unser(Stream &s, CScript &script) {
62 unsigned int nSize = 0;
63 s >> VARINT(nSize);
64 if (nSize < nSpecialScripts) {
65 std::vector<uint8_t> vch(GetSpecialScriptSize(nSize), 0x00);
66 s >> Span{vch};
67 DecompressScript(script, nSize, vch);
68 return;
69 }
70 nSize -= nSpecialScripts;
71 if (nSize > MAX_SCRIPT_SIZE) {
72 // Overly long script, replace with a short invalid one
73 script << OP_RETURN;
74 s.ignore(nSize);
75 } else {
76 script.resize(nSize);
77 s >> Span{script};
78 }
79 }
80};
81
83 template <typename Stream, typename I> void Ser(Stream &s, I val) {
84 s << VARINT(CompressAmount(val));
85 }
86 template <typename Stream, typename I> void Unser(Stream &s, I &val) {
87 uint64_t v;
88 s >> VARINT(v);
89 val = DecompressAmount(v);
90 }
91};
92
102
103#endif // BITCOIN_COMPRESSOR_H
Serialized script, used inside transaction inputs and outputs.
Definition script.h:431
An output of a transaction.
A Span is an object that can refer to a contiguous sequence of objects.
Definition span.h:93
size_type size() const
Definition prevector.h:394
void resize(size_type new_size)
Definition prevector.h:424
uint64_t CompressAmount(Amount nAmount)
Compress amount.
Amount DecompressAmount(uint64_t nAmount)
bool DecompressScript(CScript &script, unsigned int nSize, const std::vector< uint8_t > &out)
unsigned int GetSpecialScriptSize(unsigned int nSize)
bool CompressScript(const CScript &script, std::vector< uint8_t > &out)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
static const int MAX_SCRIPT_SIZE
Definition script.h:33
@ OP_RETURN
Definition script.h:84
#define VARINT(obj)
Definition serialize.h:579
#define READWRITE(...)
Definition serialize.h:166
void Unser(Stream &s, I &val)
Definition compressor.h:86
void Ser(Stream &s, I val)
Definition compressor.h:83
Compact serializer for scripts.
Definition compressor.h:40
void Unser(Stream &s, CScript &script)
Definition compressor.h:61
static const unsigned int nSpecialScripts
make this static for now (there are only 6 special scripts defined) this can potentially be extended ...
Definition compressor.h:47
void Ser(Stream &s, const CScript &script) const
Definition compressor.h:50
wrapper for CTxOut that provides a more compact serialization
Definition compressor.h:96
FORMATTER_METHODS(CTxOut, obj)
Definition compressor.h:97