Base64Encoder.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Base64Encoder.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Base64Encoder.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: Base64
  9. //
  10. // Definition of class Base64Encoder.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Base64Encoder_INCLUDED
  18. #define Foundation_Base64Encoder_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/UnbufferedStreamBuf.h"
  21. #include <ostream>
  22. namespace Poco {
  23. class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf
  24. /// This streambuf base64-encodes all data written
  25. /// to it and forwards it to a connected
  26. /// ostream.
  27. ///
  28. /// Note: The characters are directly written
  29. /// to the ostream's streambuf, thus bypassing
  30. /// the ostream. The ostream's state is therefore
  31. /// not updated to match the buffer's state.
  32. {
  33. public:
  34. Base64EncoderBuf(std::ostream& ostr);
  35. ~Base64EncoderBuf();
  36. int close();
  37. /// Closes the stream buffer.
  38. void setLineLength(int lineLength);
  39. /// Specify the line length.
  40. ///
  41. /// After the given number of characters have been written,
  42. /// a newline character will be written.
  43. ///
  44. /// Specify 0 for an unlimited line length.
  45. int getLineLength() const;
  46. /// Returns the currently set line length.
  47. private:
  48. int writeToDevice(char c);
  49. unsigned char _group[3];
  50. int _groupLength;
  51. int _pos;
  52. int _lineLength;
  53. std::streambuf& _buf;
  54. static const unsigned char OUT_ENCODING[64];
  55. friend class Base64DecoderBuf;
  56. Base64EncoderBuf(const Base64EncoderBuf&);
  57. Base64EncoderBuf& operator = (const Base64EncoderBuf&);
  58. };
  59. class Foundation_API Base64EncoderIOS: public virtual std::ios
  60. /// The base class for Base64Encoder.
  61. ///
  62. /// This class is needed to ensure the correct initialization
  63. /// order of the stream buffer and base classes.
  64. {
  65. public:
  66. Base64EncoderIOS(std::ostream& ostr);
  67. ~Base64EncoderIOS();
  68. int close();
  69. Base64EncoderBuf* rdbuf();
  70. protected:
  71. Base64EncoderBuf _buf;
  72. private:
  73. Base64EncoderIOS(const Base64EncoderIOS&);
  74. Base64EncoderIOS& operator = (const Base64EncoderIOS&);
  75. };
  76. class Foundation_API Base64Encoder: public Base64EncoderIOS, public std::ostream
  77. /// This ostream base64-encodes all data
  78. /// written to it and forwards it to
  79. /// a connected ostream.
  80. /// Always call close() when done
  81. /// writing data, to ensure proper
  82. /// completion of the encoding operation.
  83. ///
  84. /// Note: The characters are directly written
  85. /// to the ostream's streambuf, thus bypassing
  86. /// the ostream. The ostream's state is therefore
  87. /// not updated to match the buffer's state.
  88. {
  89. public:
  90. Base64Encoder(std::ostream& ostr);
  91. ~Base64Encoder();
  92. private:
  93. Base64Encoder(const Base64Encoder&);
  94. Base64Encoder& operator = (const Base64Encoder&);
  95. };
  96. } // namespace Poco
  97. #endif // Foundation_Base64Encoder_INCLUDED