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 | #ifndef BASE64_ENCODE_H |
---|
9 | #define BASE64_ENCODE_H |
---|
10 | |
---|
11 | #include <iostream> |
---|
12 | |
---|
13 | #define BUFFERSIZE 5000 |
---|
14 | |
---|
15 | namespace base64 |
---|
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 = BUFFERSIZE) |
---|
28 | : _buffersize(buffersize_in) |
---|
29 | {} |
---|
30 | |
---|
31 | int encode(char value_in) |
---|
32 | { |
---|
33 | return base64_encode_value(value_in); |
---|
34 | } |
---|
35 | |
---|
36 | int encode(const char* code_in, const int length_in, char* plaintext_out) |
---|
37 | { |
---|
38 | base64_init_encodestate(&_state); |
---|
39 | return base64_encode_block(code_in, length_in, plaintext_out, &_state); |
---|
40 | } |
---|
41 | |
---|
42 | int encode_end(char* plaintext_out) |
---|
43 | { |
---|
44 | return base64_encode_blockend(plaintext_out, &_state); |
---|
45 | } |
---|
46 | |
---|
47 | void encode(std::istream& istream_in, std::ostream& ostream_in) |
---|
48 | { |
---|
49 | base64_init_encodestate(&_state); |
---|
50 | // |
---|
51 | const int N = _buffersize; |
---|
52 | char* plaintext = new char[N]; |
---|
53 | char* code = new char[2*N]; |
---|
54 | int plainlength; |
---|
55 | int codelength; |
---|
56 | |
---|
57 | do |
---|
58 | { |
---|
59 | istream_in.read(plaintext, N); |
---|
60 | plainlength = istream_in.gcount(); |
---|
61 | // |
---|
62 | codelength = encode(plaintext, plainlength, code); |
---|
63 | ostream_in.write(code, codelength); |
---|
64 | } |
---|
65 | while (istream_in.good() && plainlength > 0); |
---|
66 | |
---|
67 | codelength = encode_end(code); |
---|
68 | ostream_in.write(code, codelength); |
---|
69 | // |
---|
70 | base64_init_encodestate(&_state); |
---|
71 | |
---|
72 | delete [] code; |
---|
73 | delete [] plaintext; |
---|
74 | } |
---|
75 | }; |
---|
76 | |
---|
77 | } // namespace base64 |
---|
78 | |
---|
79 | #endif // BASE64_ENCODE_H |
---|
80 | |
---|