| 1 | // |
| 2 | // Base32Encoder.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Streams |
| 6 | // Module: Base32 |
| 7 | // |
| 8 | // Definition of class Base32Encoder. |
| 9 | // |
| 10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_Base32Encoder_INCLUDED |
| 18 | #define Foundation_Base32Encoder_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/UnbufferedStreamBuf.h" |
| 23 | #include <ostream> |
| 24 | |
| 25 | |
| 26 | namespace Poco { |
| 27 | |
| 28 | |
| 29 | class Foundation_API Base32EncoderBuf: public UnbufferedStreamBuf |
| 30 | /// This streambuf base32-encodes all data written |
| 31 | /// to it and forwards it to a connected |
| 32 | /// ostream. |
| 33 | /// |
| 34 | /// Note: The characters are directly written |
| 35 | /// to the ostream's streambuf, thus bypassing |
| 36 | /// the ostream. The ostream's state is therefore |
| 37 | /// not updated to match the buffer's state. |
| 38 | { |
| 39 | public: |
| 40 | Base32EncoderBuf(std::ostream& ostr, bool padding = true); |
| 41 | ~Base32EncoderBuf(); |
| 42 | |
| 43 | int close(); |
| 44 | /// Closes the stream buffer. |
| 45 | |
| 46 | private: |
| 47 | int writeToDevice(char c); |
| 48 | |
| 49 | unsigned char _group[5]; |
| 50 | int _groupLength; |
| 51 | std::streambuf& _buf; |
| 52 | bool _doPadding; |
| 53 | |
| 54 | static const unsigned char OUT_ENCODING[32]; |
| 55 | |
| 56 | friend class Base32DecoderBuf; |
| 57 | |
| 58 | Base32EncoderBuf(const Base32EncoderBuf&); |
| 59 | Base32EncoderBuf& operator = (const Base32EncoderBuf&); |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | class Foundation_API Base32EncoderIOS: public virtual std::ios |
| 64 | /// The base class for Base32Encoder. |
| 65 | /// |
| 66 | /// This class is needed to ensure the correct initialization |
| 67 | /// order of the stream buffer and base classes. |
| 68 | { |
| 69 | public: |
| 70 | Base32EncoderIOS(std::ostream& ostr, bool padding = true); |
| 71 | ~Base32EncoderIOS(); |
| 72 | int close(); |
| 73 | Base32EncoderBuf* rdbuf(); |
| 74 | |
| 75 | protected: |
| 76 | Base32EncoderBuf _buf; |
| 77 | |
| 78 | private: |
| 79 | Base32EncoderIOS(const Base32EncoderIOS&); |
| 80 | Base32EncoderIOS& operator = (const Base32EncoderIOS&); |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | class Foundation_API Base32Encoder: public Base32EncoderIOS, public std::ostream |
| 85 | /// This ostream base32-encodes all data |
| 86 | /// written to it and forwards it to |
| 87 | /// a connected ostream. |
| 88 | /// Always call close() when done |
| 89 | /// writing data, to ensure proper |
| 90 | /// completion of the encoding operation. |
| 91 | /// |
| 92 | /// The class implements RFC 4648 - https://tools.ietf.org/html/rfc4648 |
| 93 | /// |
| 94 | /// Note: The characters are directly written |
| 95 | /// to the ostream's streambuf, thus bypassing |
| 96 | /// the ostream. The ostream's state is therefore |
| 97 | /// not updated to match the buffer's state. |
| 98 | { |
| 99 | public: |
| 100 | Base32Encoder(std::ostream& ostr, bool padding = true); |
| 101 | ~Base32Encoder(); |
| 102 | |
| 103 | private: |
| 104 | Base32Encoder(const Base32Encoder&); |
| 105 | Base32Encoder& operator = (const Base32Encoder&); |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | } // namespace Poco |
| 110 | |
| 111 | |
| 112 | #endif // Foundation_Base32Encoder_INCLUDED |
| 113 | |