| 1 | // :mode=c++: |
|---|
| 2 | /* |
|---|
| 3 | encode.h - c++ wrapper for a base64 encoding algorithm |
|---|
| 4 | |
|---|
| 5 | This is part of the libb64 project, and has been placed in the public domain. |
|---|
| 6 | For details, see http://sourceforge.net/projects/libb64 |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #ifndef BASE64_ENCODE_H |
|---|
| 10 | #define BASE64_ENCODE_H |
|---|
| 11 | |
|---|
| 12 | #include <iostream> |
|---|
| 13 | |
|---|
| 14 | namespace base64 |
|---|
| 15 | { |
|---|
| 16 | |
|---|
| 17 | extern "C" |
|---|
| 18 | { |
|---|
| 19 | #include "cencode.h" |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | struct encoder |
|---|
| 23 | { |
|---|
| 24 | base64_encodestate _state; |
|---|
| 25 | int _buffersize; |
|---|
| 26 | |
|---|
| 27 | encoder(int buffersize_in = 4096) |
|---|
| 28 | : _buffersize(buffersize_in) |
|---|
| 29 | {} |
|---|
| 30 | int encode(char value_in) |
|---|
| 31 | { |
|---|
| 32 | return base64_encode_value(value_in); |
|---|
| 33 | } |
|---|
| 34 | int encode(const char* code_in, const int length_in, char* plaintext_out) |
|---|
| 35 | { |
|---|
| 36 | return base64_encode_block(code_in, length_in, plaintext_out, &_state); |
|---|
| 37 | } |
|---|
| 38 | int encode_end(char* plaintext_out) |
|---|
| 39 | { |
|---|
| 40 | return base64_encode_blockend(plaintext_out, &_state); |
|---|
| 41 | } |
|---|
| 42 | std::string encode(const std::string& input) |
|---|
| 43 | { |
|---|
| 44 | base64_init_encodestate(&_state); |
|---|
| 45 | char* output = new char[2*input.size()]; |
|---|
| 46 | int outlength = encode(input.c_str(), input.size(), |
|---|
| 47 | output); |
|---|
| 48 | outlength += encode_end(output + outlength); |
|---|
| 49 | std::string output_string(output, outlength); |
|---|
| 50 | base64_init_encodestate(&_state); |
|---|
| 51 | delete [] output; |
|---|
| 52 | return output_string; |
|---|
| 53 | } |
|---|
| 54 | void encode(std::istream& istream_in, std::ostream& ostream_in) |
|---|
| 55 | { |
|---|
| 56 | base64_init_encodestate(&_state); |
|---|
| 57 | // |
|---|
| 58 | const int N = _buffersize; |
|---|
| 59 | char* plaintext = new char[N]; |
|---|
| 60 | char* code = new char[2*N]; |
|---|
| 61 | int plainlength; |
|---|
| 62 | int codelength; |
|---|
| 63 | |
|---|
| 64 | do |
|---|
| 65 | { |
|---|
| 66 | istream_in.read(plaintext, N); |
|---|
| 67 | plainlength = istream_in.gcount(); |
|---|
| 68 | // |
|---|
| 69 | codelength = encode(plaintext, plainlength, code); |
|---|
| 70 | ostream_in.write(code, codelength); |
|---|
| 71 | } |
|---|
| 72 | while (istream_in.good() && plainlength > 0); |
|---|
| 73 | |
|---|
| 74 | codelength = encode_end(code); |
|---|
| 75 | ostream_in.write(code, codelength); |
|---|
| 76 | // |
|---|
| 77 | base64_init_encodestate(&_state); |
|---|
| 78 | |
|---|
| 79 | delete [] code; |
|---|
| 80 | delete [] plaintext; |
|---|
| 81 | } |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | } // namespace base64 |
|---|
| 85 | |
|---|
| 86 | #endif // BASE64_ENCODE_H |
|---|
| 87 | |
|---|