You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compressor.h 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef H_BITCOIN_SCRIPT_COMPRESSOR
  6. #define H_BITCOIN_SCRIPT_COMPRESSOR
  7. #include "script/script.h"
  8. #include "serialize.h"
  9. class CKeyID;
  10. class CPubKey;
  11. class CScriptID;
  12. /** Compact serializer for scripts.
  13. *
  14. * It detects common cases and encodes them much more efficiently.
  15. * 3 special cases are defined:
  16. * * Pay to pubkey hash (encoded as 21 bytes)
  17. * * Pay to script hash (encoded as 21 bytes)
  18. * * Pay to pubkey starting with 0x02, 0x03 or 0x04 (encoded as 33 bytes)
  19. *
  20. * Other scripts up to 121 bytes require 1 byte + script length. Above
  21. * that, scripts up to 16505 bytes require 2 bytes + script length.
  22. */
  23. class CScriptCompressor
  24. {
  25. private:
  26. // make this static for now (there are only 6 special scripts defined)
  27. // this can potentially be extended together with a new nVersion for
  28. // transactions, in which case this value becomes dependent on nVersion
  29. // and nHeight of the enclosing transaction.
  30. static const unsigned int nSpecialScripts = 6;
  31. CScript &script;
  32. protected:
  33. // These check for scripts for which a special case with a shorter encoding is defined.
  34. // They are implemented separately from the CScript test, as these test for exact byte
  35. // sequence correspondences, and are more strict. For example, IsToPubKey also verifies
  36. // whether the public key is valid (as invalid ones cannot be represented in compressed
  37. // form).
  38. bool IsToKeyID(CKeyID &hash) const;
  39. bool IsToScriptID(CScriptID &hash) const;
  40. bool IsToPubKey(CPubKey &pubkey) const;
  41. bool Compress(std::vector<unsigned char> &out) const;
  42. unsigned int GetSpecialSize(unsigned int nSize) const;
  43. bool Decompress(unsigned int nSize, const std::vector<unsigned char> &out);
  44. public:
  45. CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
  46. unsigned int GetSerializeSize(int nType, int nVersion) const {
  47. std::vector<unsigned char> compr;
  48. if (Compress(compr))
  49. return compr.size();
  50. unsigned int nSize = script.size() + nSpecialScripts;
  51. return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
  52. }
  53. template<typename Stream>
  54. void Serialize(Stream &s, int nType, int nVersion) const {
  55. std::vector<unsigned char> compr;
  56. if (Compress(compr)) {
  57. s << CFlatData(compr);
  58. return;
  59. }
  60. unsigned int nSize = script.size() + nSpecialScripts;
  61. s << VARINT(nSize);
  62. s << CFlatData(script);
  63. }
  64. template<typename Stream>
  65. void Unserialize(Stream &s, int nType, int nVersion) {
  66. unsigned int nSize = 0;
  67. s >> VARINT(nSize);
  68. if (nSize < nSpecialScripts) {
  69. std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
  70. s >> REF(CFlatData(vch));
  71. Decompress(nSize, vch);
  72. return;
  73. }
  74. nSize -= nSpecialScripts;
  75. script.resize(nSize);
  76. s >> REF(CFlatData(script));
  77. }
  78. };
  79. #endif // H_BITCOIN_SCRIPT_COMPRESSOR