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.

135 lines
6.9 KiB

  1. // base64.h - written and placed in the public domain by Wei Dai
  2. //! \file base64.h
  3. //! \brief Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder
  4. #ifndef CRYPTOPP_BASE64_H
  5. #define CRYPTOPP_BASE64_H
  6. #include "cryptlib.h"
  7. #include "basecode.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. //! \class Base64Encoder
  10. //! \brief Base64 encodes data
  11. //! \details Base64 encodes data per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-4)
  12. //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
  13. class Base64Encoder : public SimpleProxyFilter
  14. {
  15. public:
  16. //! \brief Construct a Base64Encoder
  17. //! \param attachment a BufferedTrasformation to attach to this object
  18. //! \param insertLineBreaks a BufferedTrasformation to attach to this object
  19. //! \param maxLineLength the lenght of a line if line breaks are used
  20. //! \details Base64Encoder() constructs a default encoder. The constructor lacks parameters for padding.
  21. //! You must use IsolatedInitialize() to modify the Base64Encoder after construction.
  22. //! \sa IsolatedInitialize() for an example of modifying a Base64Encoder after construction.
  23. Base64Encoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = true, int maxLineLength = 72)
  24. : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
  25. {
  26. IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), insertLineBreaks)(Name::MaxLineLength(), maxLineLength));
  27. }
  28. //! \brief Initialize or reinitialize this object, without signal propagation
  29. //! \param parameters a set of NameValuePairs used to initialize this object
  30. //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  31. //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  32. //! transformations. If initialization should be propagated, then use the Initialize() function.
  33. //! \details The following code modifies the padding and line break parameters for an encoder:
  34. //! <pre>
  35. //! Base64Encoder encoder;
  36. //! AlgorithmParameters params = MakeParameters(Pad(), false)(InsertLineBreaks(), false);
  37. //! encoder.IsolatedInitialize(params);
  38. //! </pre>
  39. void IsolatedInitialize(const NameValuePairs &parameters);
  40. };
  41. //! \class Base64Decoder
  42. //! \brief Base64 decodes data
  43. //! \details Base64 decodes data per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-4)
  44. //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
  45. class Base64Decoder : public BaseN_Decoder
  46. {
  47. public:
  48. //! \brief Construct a Base64Decoder
  49. //! \param attachment a BufferedTrasformation to attach to this object
  50. Base64Decoder(BufferedTransformation *attachment = NULL)
  51. : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
  52. //! \brief Initialize or reinitialize this object, without signal propagation
  53. //! \param parameters a set of NameValuePairs used to initialize this object
  54. //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  55. //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on
  56. //! attached transformations. If initialization should be propagated, then use the Initialize() function.
  57. void IsolatedInitialize(const NameValuePairs &parameters)
  58. {CRYPTOPP_UNUSED(parameters);}
  59. private:
  60. static const int * CRYPTOPP_API GetDecodingLookupArray();
  61. };
  62. //! \class Base64URLEncoder
  63. //! \brief Base64 encodes data using a web safe alphabet
  64. //! \details Base64 encodes data using a web safe alphabet per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-5)
  65. //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
  66. class Base64URLEncoder : public SimpleProxyFilter
  67. {
  68. public:
  69. //! \brief Construct a Base64URLEncoder
  70. //! \param attachment a BufferedTrasformation to attach to this object
  71. //! \param insertLineBreaks a BufferedTrasformation to attach to this object
  72. //! \param maxLineLength the lenght of a line if line breaks are used
  73. //! \details Base64URLEncoder() constructs a default encoder. The constructor ignores insertLineBreaks
  74. //! and maxLineLength because the web and URL safe specifications don't use them. They are present
  75. //! in the constructor for API compatibility with Base64Encoder (drop-in replacement). The
  76. //! constructor also disables padding on the encoder for the same reason.
  77. //! \details If you need line breaks or padding, then you must use IsolatedInitialize() to set them
  78. //! after constructing a Base64URLEncoder.
  79. //! \sa IsolatedInitialize() for an example of modifying a Base64URLEncoder after construction.
  80. Base64URLEncoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = false, int maxLineLength = -1)
  81. : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment)
  82. {
  83. CRYPTOPP_UNUSED(insertLineBreaks), CRYPTOPP_UNUSED(maxLineLength);
  84. IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), false)(Name::MaxLineLength(), -1)(Name::Pad(),false));
  85. }
  86. //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  87. //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on attached
  88. //! transformations. If initialization should be propagated, then use the Initialize() function.
  89. //! \details The following code modifies the padding and line break parameters for an encoder:
  90. //! <pre>
  91. //! Base64URLEncoder encoder;
  92. //! AlgorithmParameters params = MakeParameters(Name::Pad(), true)(Name::InsertLineBreaks(), true);
  93. //! encoder.IsolatedInitialize(params);
  94. //! </pre>
  95. void IsolatedInitialize(const NameValuePairs &parameters);
  96. };
  97. //! \class Base64URLDecoder
  98. //! \brief Base64 decodes data using a web safe alphabet
  99. //! \details Base64 decodes data using a web safe alphabet per RFC 4648 (http://tools.ietf.org/html/rfc4648#section-5)
  100. //! \details To specify alternative alpahabet or code, call Initialize() with EncodingLookupArray parameter.
  101. class Base64URLDecoder : public BaseN_Decoder
  102. {
  103. public:
  104. //! \brief Construct a Base64URLDecoder
  105. //! \param attachment a BufferedTrasformation to attach to this object
  106. Base64URLDecoder(BufferedTransformation *attachment = NULL)
  107. : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {}
  108. //! \brief Initialize or reinitialize this object, without signal propagation
  109. //! \param parameters a set of NameValuePairs used to initialize this object
  110. //! \details IsolatedInitialize() is used to initialize or reinitialize an object using a variable
  111. //! number of arbitrarily typed arguments. IsolatedInitialize() does not call Initialize() on
  112. //! attached transformations. If initialization should be propagated, then use the Initialize() function.
  113. void IsolatedInitialize(const NameValuePairs &parameters)
  114. {CRYPTOPP_UNUSED(parameters);}
  115. private:
  116. static const int * CRYPTOPP_API GetDecodingLookupArray();
  117. };
  118. NAMESPACE_END
  119. #endif