35 std::ostream* decorated;
38 int output_base64_width;
53 std::ostream* decorated_,
87 std::ostream* decorated_,
89 decorated(decorated_) {
90 assert(decorated !=
nullptr);
94 assert(width_ % 4 == 0);
95 output_base64_width = width_;
96 output_newline =
true;
99 output_base64_width = 80;
100 output_newline =
false;
102 output_raw_width = output_base64_width / 4 * 3;
103 output_buffer =
new char[output_raw_width];
106 setp(output_buffer, output_buffer + output_raw_width);
111 delete[] output_buffer;
114 void Base64OStream::Buffer::finishData() {
116 auto length_(pptr() - output_buffer);
117 assert(length_ >= 0 && length_ <= output_raw_width);
120 static const unsigned char base64_table[] =
121 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
122 char buffer_[output_base64_width];
123 const std::uint8_t* in_(
reinterpret_cast<std::uint8_t*
>(output_buffer));
125 while(length_ >= 3) {
126 *out_++ = base64_table[in_[0] >> 2];
127 *out_++ = base64_table[((in_[0] & 0x03) << 4) | (in_[1] >> 4)];
128 *out_++ = base64_table[((in_[1] & 0x0f) << 2) | (in_[2] >> 6)];
129 *out_++ = base64_table[in_[2] & 0x3f];
136 *out_++ = base64_table[in_[0] >> 2];
138 *out_++ = base64_table[(in_[0] & 0x03) << 4];
142 *out_++ = base64_table[((in_[0] & 0x03) << 4) | (in_[1] >> 4)];
143 *out_++ = base64_table[(in_[1] & 0x0f) << 2];
149 setp(output_buffer, output_buffer + output_raw_width);
152 auto coded_length_(out_ - buffer_);
153 decorated->write(buffer_,
static_cast<std::streamsize
>(coded_length_));
156 int Base64OStream::Buffer::overflow(
163 decorated->put(
'\n');
166 if(c != traits_type::eof()) {
167 *output_buffer = traits_type::to_char_type(c);
171 return traits_type::not_eof(c);
180 std::ostream* decorated_,
182 buffer(new
Buffer(decorated_, line_length_)) {