base64enc.hpp

Go to the documentation of this file.
00001 // :mode=c++:
00002 /*
00003 encode.h - c++ wrapper for a base64 encoding algorithm
00004 
00005 This is part of the libb64 project, and has been placed in the public domain.
00006 For details, see http://sourceforge.net/projects/libb64
00007 */
00008 
00009 #ifndef BASE64_ENCODE_H
00010 #define BASE64_ENCODE_H
00011 
00012 #include <iostream>
00013 
00014 namespace s11n
00015 {
00016 namespace base64
00017 {
00018     
00019     extern "C" 
00020     {
00021         #include "base64enc.h"
00022     }
00023     
00024     struct encoder
00025     {
00026         base64_encodestate _state;
00027         int _buffersize;
00028         
00029         encoder(int buffersize_in = 4096)
00030         : _buffersize(buffersize_in)
00031         {}
00032         int encode(char value_in)
00033         {
00034             return base64_encode_value(value_in);
00035         }
00036         int encode(const char* code_in, const int length_in, char* plaintext_out)
00037         {
00038             return base64_encode_block(code_in, length_in, plaintext_out, &_state);
00039         }
00040         int encode_end(char* plaintext_out)
00041         {
00042             return base64_encode_blockend(plaintext_out, &_state);
00043         }
00044         void encode(std::istream& istream_in, std::ostream& ostream_in)
00045         {
00046             base64_init_encodestate(&_state);
00047             //
00048             const int N = _buffersize;
00049             char* plaintext = new char[N];
00050             char* code = new char[2*N];
00051             int plainlength;
00052             int codelength;
00053             
00054             do
00055             {
00056                 istream_in.read(plaintext, N);
00057                 plainlength = istream_in.gcount();
00058                 //
00059                 codelength = encode(plaintext, plainlength, code);
00060                 ostream_in.write(code, codelength);
00061             }
00062             while (istream_in.good() && plainlength > 0);
00063             
00064             codelength = encode_end(code);
00065             ostream_in.write(code, codelength);
00066             //
00067             base64_init_encodestate(&_state);
00068             
00069             delete [] code;
00070             delete [] plaintext;
00071         }
00072     };
00073     
00074 } // namespace base64
00075 } // namespace s11n
00076 #endif // BASE64_ENCODE_H
00077 

Generated on Wed Jun 4 21:45:18 2008 for libs11n by  doxygen 1.5.3