1

I managed to get working:

1) a base 64 encoder/decoder using boost::archive::iterators derived from Base64 encode using boost throw exception

2) a compressor using boost::iostreams as shown here: boost zlib problem

So code the looks like this:

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/copy.hpp>

#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>

namespace io = boost::iostreams;
namespace it = boost::archive::iterators;

typedef it::transform_width< it::binary_from_base64<std::string::const_iterator >, 8, 6 > it_binary_t;
typedef it::base64_from_binary<it::transform_width<std::string::const_iterator ,6,8> > it_base64_t;


// Compress
std::stringstream binary_in;
binary_in << data...;
std::stringstream binary_out;
io::filtering_streambuf<io::output> outStream; 
outStream.push(io::zlib_compressor()); 
outStream.push(binary_out); 
io::copy(binary_in, outStream); 

// base64 Encode
std::string s = binary_out.str();
unsigned int writePaddChars = (3-s.length()%3)%3;
std::string base64(it_base64_t(s.begin()),it_base64_t(s.end()));
base64.append(writePaddChars,'=');

That seems to much copying data.

Is there a way to push the base64 encoder onto the iostream?

Community
  • 1
  • 1
RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92
  • You could make a base64 encoding stream buffer. I have no time to make it for you now. Look for an example by Dietmar Kuhl showing how to do do encryption (or compression?) on the fly using a custom streambuf implementation. – sehe Jan 20 '14 at 15:28
  • Also, see here: http://hamigaki.sourceforge.jp/hamigaki/iostreams/filter/base64.hpp – sehe Jan 20 '14 at 15:31

0 Answers0