/*! \file xstream/base64.h * * \brief C++ streambuf interface to encode/decode base64 data */ #ifndef __XSTREAM_BASE64_H #define __XSTREAM_BASE64_H #include #include #include namespace xstream{ /*! * \brief base64 encoding/decoding objects */ namespace base64{ /*! * \brief Base64 encode stream class * * encodes data to base64 and writes it to another streambuf * * \todo no \c xsputn support yet and \c flush doesn't flush partial data, * for example suppose you wrote 2 bytes of a 3 byte chunk, it's possible to know * the first 2 characters of the encoded data, but these are not flushed * */ class ostreambuf: public xstream::ostreambuf { private: std::streambuf* _sb; char delim; unsigned int delim_w; unsigned int col; char buf[3]; /*!< buffer to store non encoded data */ /*! * \brief flush as much data as possible (overloaded from streambuf) * * */ int sync(); /*! * \brief write a character that surpasses buffer end (overloaded from streambuf) * */ int overflow(const int c); #if 0 //XXX implement later /*! * \brief write an entire buffer (overloaded from streambuf) * */ std::streamsize xsputn(char *buffer, std::streamsize n); #endif /*! * \brief reset input buffer * */ void reset(); /*! * \brief Takes care of inserting delimiters every \c delim_w characters */ int write(const char* buf, const size_t len); public: /*! * \brief construct using a streambuf * * \param sb streambuf where to write the encoded data * \param width width in bytes of lines (=0 for unlimited lines) * \param delimiter char to delimit the lines * * the same parameters need to be used when decoding or it will throw an exception indicating invalid data * \c width=76 and \c delim=newline are default because they are the values in the \c RFC */ ostreambuf(std::streambuf* sb, const unsigned int width=76, const char delimiter='\n'); /*! * \brief closes the base64 stream * */ ~ostreambuf(); }; /*! * \brief Base64 decode stream class * * decodes data encoded in base64 and makes it available for reading. * Only 4 bytes buffered at a time, and this is not configurable * * \todo xsgetn support * */ class istreambuf: public std::streambuf{ private: std::streambuf* _sb; /*!< stream to read from */ bool end; /*!