Team Fortress 2 Source Code as on 22/4/2020
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.

142 lines
5.3 KiB

  1. // basecode.h - 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. //! \class BaseN_Encoder
  12. //! \brief Encoder for bases that are a power of 2
  13. class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
  14. {
  15. public:
  16. //! \brief Construct a BaseN_Encoder
  17. //! \param attachment a BufferedTransformation to attach to this object
  18. BaseN_Encoder(BufferedTransformation *attachment=NULL)
  19. : m_alphabet(NULL), m_padding(0), m_bitsPerChar(0)
  20. , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
  21. {Detach(attachment);}
  22. //! \brief Construct a BaseN_Encoder
  23. //! \param alphabet table of ASCII characters to use as the alphabet
  24. //! \param log2base the log<sub>2</sub>base
  25. //! \param attachment a BufferedTransformation to attach to this object
  26. //! \param padding the character to use as padding
  27. //! \pre log2base must be between 1 and 7 inclusive
  28. //! \throws InvalidArgument if log2base is not between 1 and 7
  29. BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULL, int padding=-1)
  30. : m_alphabet(NULL), m_padding(0), m_bitsPerChar(0)
  31. , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
  32. {
  33. Detach(attachment);
  34. IsolatedInitialize(MakeParameters(Name::EncodingLookupArray(), alphabet)
  35. (Name::Log2Base(), log2base)
  36. (Name::Pad(), padding != -1)
  37. (Name::PaddingByte(), byte(padding)));
  38. }
  39. void IsolatedInitialize(const NameValuePairs &parameters);
  40. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  41. private:
  42. const byte *m_alphabet;
  43. int m_padding, m_bitsPerChar, m_outputBlockSize;
  44. int m_bytePos, m_bitPos;
  45. SecByteBlock m_outBuf;
  46. };
  47. //! \class BaseN_Decoder
  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=NULL)
  57. : m_lookup(0), m_padding(0), 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=NULL)
  69. : m_lookup(0), m_padding(0), m_bitsPerChar(0)
  70. , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
  71. {
  72. Detach(attachment);
  73. IsolatedInitialize(MakeParameters(Name::DecodingLookupArray(), lookup)(Name::Log2Base(), log2base));
  74. }
  75. void IsolatedInitialize(const NameValuePairs &parameters);
  76. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  77. //! \brief Intializes BaseN lookup array
  78. //! \param lookup table of values
  79. //! \param alphabet table of ASCII characters
  80. //! \param base the base for the encoder
  81. //! \param caseInsensitive flag indicating whether the alpabet is case sensitivie
  82. //! \pre COUNTOF(lookup) == 256
  83. //! \pre COUNTOF(alphabet) == base
  84. //! \details Internally, the function sets the first 256 elements in the lookup table to
  85. // their value from the alphabet array or -1. base is the number of element (like 32),
  86. //! and not an exponent (like 5 in 2<sup>5</sup>)
  87. static void CRYPTOPP_API InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive);
  88. private:
  89. const int *m_lookup;
  90. int m_padding, m_bitsPerChar, m_outputBlockSize;
  91. int m_bytePos, m_bitPos;
  92. SecByteBlock m_outBuf;
  93. };
  94. //! \class Grouper
  95. //! \brief Filter that breaks input stream into groups of fixed size
  96. class CRYPTOPP_DLL Grouper : public Bufferless<Filter>
  97. {
  98. public:
  99. //! \brief Construct a Grouper
  100. //! \param attachment a BufferedTransformation to attach to this object
  101. Grouper(BufferedTransformation *attachment=NULL)
  102. : m_groupSize(0), m_counter(0) {Detach(attachment);}
  103. //! \brief Construct a Grouper
  104. //! \param groupSize the size of the grouping
  105. //! \param separator the separator to use between groups
  106. //! \param terminator the terminator appeand after processing
  107. //! \param attachment a BufferedTransformation to attach to this object
  108. Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULL)
  109. : m_groupSize(0), m_counter(0)
  110. {
  111. Detach(attachment);
  112. IsolatedInitialize(MakeParameters(Name::GroupSize(), groupSize)
  113. (Name::Separator(), ConstByteArrayParameter(separator))
  114. (Name::Terminator(), ConstByteArrayParameter(terminator)));
  115. }
  116. void IsolatedInitialize(const NameValuePairs &parameters);
  117. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  118. private:
  119. SecByteBlock m_separator, m_terminator;
  120. size_t m_groupSize, m_counter;
  121. };
  122. NAMESPACE_END
  123. #endif