basecode.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // basecode.h - originally written and placed in the public domain by Wei Dai
  2. /// \file
  3. /// \brief Base classes for working with encoders and decoders.
  4. #ifndef CRYPTOPP_BASECODE_H
  5. #define CRYPTOPP_BASECODE_H
  6. #include "cryptlib.h"
  7. #include "filters.h"
  8. #include "algparam.h"
  9. #include "argnames.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. /// \brief Encoder for bases that are a power of 2
  12. class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
  13. {
  14. public:
  15. /// \brief Construct a BaseN_Encoder
  16. /// \param attachment a BufferedTransformation to attach to this object
  17. BaseN_Encoder(BufferedTransformation *attachment=NULLPTR)
  18. : m_alphabet(NULLPTR), m_padding(0), m_bitsPerChar(0)
  19. , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
  20. {Detach(attachment);}
  21. /// \brief Construct a BaseN_Encoder
  22. /// \param alphabet table of ASCII characters to use as the alphabet
  23. /// \param log2base the log<sub>2</sub>base
  24. /// \param attachment a BufferedTransformation to attach to this object
  25. /// \param padding the character to use as padding
  26. /// \pre log2base must be between 1 and 7 inclusive
  27. /// \throw InvalidArgument if log2base is not between 1 and 7
  28. BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULLPTR, int padding=-1)
  29. : m_alphabet(NULLPTR), m_padding(0), m_bitsPerChar(0)
  30. , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
  31. {
  32. Detach(attachment);
  33. BaseN_Encoder::IsolatedInitialize(
  34. MakeParameters
  35. (Name::EncodingLookupArray(), alphabet)
  36. (Name::Log2Base(), log2base)
  37. (Name::Pad(), padding != -1)
  38. (Name::PaddingByte(), byte(padding)));
  39. }
  40. void IsolatedInitialize(const NameValuePairs &parameters);
  41. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  42. private:
  43. const byte *m_alphabet;
  44. int m_padding, m_bitsPerChar, m_outputBlockSize;
  45. int m_bytePos, m_bitPos;
  46. SecByteBlock m_outBuf;
  47. };
  48. /// \brief Decoder for bases that are a power of 2
  49. class CRYPTOPP_DLL BaseN_Decoder : public Unflushable<Filter>
  50. {
  51. public:
  52. /// \brief Construct a BaseN_Decoder
  53. /// \param attachment a BufferedTransformation to attach to this object
  54. /// \details padding is set to -1, which means use default padding. If not
  55. /// required, then the value must be set via IsolatedInitialize().
  56. BaseN_Decoder(BufferedTransformation *attachment=NULLPTR)
  57. : m_lookup(NULLPTR), m_bitsPerChar(0)
  58. , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
  59. {Detach(attachment);}
  60. /// \brief Construct a BaseN_Decoder
  61. /// \param lookup table of values
  62. /// \param log2base the log<sub>2</sub>base
  63. /// \param attachment a BufferedTransformation to attach to this object
  64. /// \details log2base is the exponent (like 5 in 2<sup>5</sup>), and not
  65. /// the number of elements (like 32).
  66. /// \details padding is set to -1, which means use default padding. If not
  67. /// required, then the value must be set via IsolatedInitialize().
  68. BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULLPTR)
  69. : m_lookup(NULLPTR), m_bitsPerChar(0)
  70. , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
  71. {
  72. Detach(attachment);
  73. BaseN_Decoder::IsolatedInitialize(
  74. MakeParameters
  75. (Name::DecodingLookupArray(), lookup)
  76. (Name::Log2Base(), log2base));
  77. }
  78. void IsolatedInitialize(const NameValuePairs &parameters);
  79. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  80. /// \brief Initializes BaseN lookup array
  81. /// \param lookup table of values
  82. /// \param alphabet table of ASCII characters
  83. /// \param base the base for the encoder
  84. /// \param caseInsensitive flag indicating whether the alphabet is case sensitivie
  85. /// \pre COUNTOF(lookup) == 256
  86. /// \pre COUNTOF(alphabet) == base
  87. /// \details Internally, the function sets the first 256 elements in the lookup table to
  88. /// their value from the alphabet array or -1. base is the number of element (like 32),
  89. /// and not an exponent (like 5 in 2<sup>5</sup>)
  90. static void CRYPTOPP_API InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive);
  91. private:
  92. const int *m_lookup;
  93. int m_bitsPerChar, m_outputBlockSize;
  94. int m_bytePos, m_bitPos;
  95. SecByteBlock m_outBuf;
  96. };
  97. /// \brief Filter that breaks input stream into groups of fixed size
  98. class CRYPTOPP_DLL Grouper : public Bufferless<Filter>
  99. {
  100. public:
  101. /// \brief Construct a Grouper
  102. /// \param attachment a BufferedTransformation to attach to this object
  103. Grouper(BufferedTransformation *attachment=NULLPTR)
  104. : m_groupSize(0), m_counter(0) {Detach(attachment);}
  105. /// \brief Construct a Grouper
  106. /// \param groupSize the size of the grouping
  107. /// \param separator the separator to use between groups
  108. /// \param terminator the terminator appeand after processing
  109. /// \param attachment a BufferedTransformation to attach to this object
  110. Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULLPTR)
  111. : m_groupSize(0), m_counter(0)
  112. {
  113. Detach(attachment);
  114. Grouper::IsolatedInitialize(
  115. MakeParameters
  116. (Name::GroupSize(), groupSize)
  117. (Name::Separator(), ConstByteArrayParameter(separator))
  118. (Name::Terminator(), ConstByteArrayParameter(terminator)));
  119. }
  120. void IsolatedInitialize(const NameValuePairs &parameters);
  121. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  122. private:
  123. SecByteBlock m_separator, m_terminator;
  124. size_t m_groupSize, m_counter;
  125. };
  126. NAMESPACE_END
  127. #endif