1 | // |
2 | // Base64Encoder.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Streams |
6 | // Module: Base64 |
7 | // |
8 | // Definition of class Base64Encoder. |
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_Base64Encoder_INCLUDED |
18 | #define Foundation_Base64Encoder_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/UnbufferedStreamBuf.h" |
23 | #include <ostream> |
24 | |
25 | |
26 | namespace Poco { |
27 | |
28 | |
29 | enum Base64EncodingOptions |
30 | { |
31 | BASE64_URL_ENCODING = 0x01, |
32 | /// Use the URL and filename-safe alphabet, |
33 | /// replacing '+' with '-' and '/' with '_'. |
34 | /// |
35 | /// Will also set line length to unlimited. |
36 | |
37 | BASE64_NO_PADDING = 0x02 |
38 | /// Do not append padding characters ('=') at end. |
39 | }; |
40 | |
41 | |
42 | class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf |
43 | /// This streambuf base64-encodes all data written |
44 | /// to it and forwards it to a connected |
45 | /// ostream. |
46 | /// |
47 | /// Note: The characters are directly written |
48 | /// to the ostream's streambuf, thus bypassing |
49 | /// the ostream. The ostream's state is therefore |
50 | /// not updated to match the buffer's state. |
51 | { |
52 | public: |
53 | Base64EncoderBuf(std::ostream& ostr, int options = 0); |
54 | ~Base64EncoderBuf(); |
55 | |
56 | int close(); |
57 | /// Closes the stream buffer. |
58 | |
59 | void setLineLength(int lineLength); |
60 | /// Specify the line length. |
61 | /// |
62 | /// After the given number of characters have been written, |
63 | /// a newline character will be written. |
64 | /// |
65 | /// Specify 0 for an unlimited line length. |
66 | |
67 | int getLineLength() const; |
68 | /// Returns the currently set line length. |
69 | |
70 | private: |
71 | int writeToDevice(char c); |
72 | |
73 | int _options; |
74 | unsigned char _group[3]; |
75 | int _groupLength; |
76 | int _pos; |
77 | int _lineLength; |
78 | std::streambuf& _buf; |
79 | const unsigned char* _pOutEncoding; |
80 | |
81 | static const unsigned char OUT_ENCODING[64]; |
82 | static const unsigned char OUT_ENCODING_URL[64]; |
83 | |
84 | friend class Base64DecoderBuf; |
85 | |
86 | Base64EncoderBuf(const Base64EncoderBuf&); |
87 | Base64EncoderBuf& operator = (const Base64EncoderBuf&); |
88 | }; |
89 | |
90 | |
91 | class Foundation_API Base64EncoderIOS: public virtual std::ios |
92 | /// The base class for Base64Encoder. |
93 | /// |
94 | /// This class is needed to ensure the correct initialization |
95 | /// order of the stream buffer and base classes. |
96 | { |
97 | public: |
98 | Base64EncoderIOS(std::ostream& ostr, int options = 0); |
99 | ~Base64EncoderIOS(); |
100 | int close(); |
101 | Base64EncoderBuf* rdbuf(); |
102 | |
103 | protected: |
104 | Base64EncoderBuf _buf; |
105 | |
106 | private: |
107 | Base64EncoderIOS(const Base64EncoderIOS&); |
108 | Base64EncoderIOS& operator = (const Base64EncoderIOS&); |
109 | }; |
110 | |
111 | |
112 | class Foundation_API Base64Encoder: public Base64EncoderIOS, public std::ostream |
113 | /// This ostream base64-encodes all data |
114 | /// written to it and forwards it to |
115 | /// a connected ostream. |
116 | /// Always call close() when done |
117 | /// writing data, to ensure proper |
118 | /// completion of the encoding operation. |
119 | /// |
120 | /// The class implements RFC 4648 - https://tools.ietf.org/html/rfc4648 |
121 | /// |
122 | /// Note: The characters are directly written |
123 | /// to the ostream's streambuf, thus bypassing |
124 | /// the ostream. The ostream's state is therefore |
125 | /// not updated to match the buffer's state. |
126 | { |
127 | public: |
128 | Base64Encoder(std::ostream& ostr, int options = 0); |
129 | ~Base64Encoder(); |
130 | |
131 | private: |
132 | Base64Encoder(const Base64Encoder&); |
133 | Base64Encoder& operator = (const Base64Encoder&); |
134 | }; |
135 | |
136 | |
137 | } // namespace Poco |
138 | |
139 | |
140 | #endif // Foundation_Base64Encoder_INCLUDED |
141 | |