00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BASE64_ENCODE_H
00009 #define BASE64_ENCODE_H
00010
00011 #include <iostream>
00012
00013 namespace base64
00014 {
00015 extern "C"
00016 {
00017 #include "cencode.h"
00018 }
00019
00020 struct encoder
00021 {
00022 base64_encodestate _state;
00023 int _buffersize;
00024
00025 encoder(int buffersize_in = BUFFERSIZE)
00026 : _buffersize(buffersize_in)
00027 {}
00028
00029 int encode(char value_in)
00030 {
00031 return base64_encode_value(value_in);
00032 }
00033
00034 int encode(const char* code_in, const int length_in, char* plaintext_out)
00035 {
00036 return base64_encode_block(code_in, length_in, plaintext_out, &_state);
00037 }
00038
00039 int encode_end(char* plaintext_out)
00040 {
00041 return base64_encode_blockend(plaintext_out, &_state);
00042 }
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 }
00075
00076 #endif // BASE64_ENCODE_H
00077