1 | |
---|
2 | // :mode=c++: |
---|
3 | /* |
---|
4 | decode.h - c++ wrapper for a base64 decoding algorithm |
---|
5 | |
---|
6 | This is part of the libb64 project, and has been placed in the public domain. |
---|
7 | For details, see http://sourceforge.net/projects/libb64 |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef BASE64_DECODE_H |
---|
11 | #define BASE64_DECODE_H |
---|
12 | |
---|
13 | #include <iostream> |
---|
14 | |
---|
15 | extern "C" { |
---|
16 | #include "cdecode.h" |
---|
17 | } |
---|
18 | |
---|
19 | namespace base64 { |
---|
20 | |
---|
21 | struct decoder { |
---|
22 | base64_decodestate _state; |
---|
23 | int _buffersize; |
---|
24 | |
---|
25 | decoder(int buffersize_in = 4096) |
---|
26 | : _buffersize(buffersize_in) |
---|
27 | { |
---|
28 | base64_init_decodestate(&_state); |
---|
29 | } |
---|
30 | int decode(char value_in) { |
---|
31 | return base64_decode_value(value_in); |
---|
32 | } |
---|
33 | int decode(const char* code_in, const int length_in, char* plaintext_out) { |
---|
34 | return base64_decode_block(code_in, length_in, plaintext_out, &_state); |
---|
35 | } |
---|
36 | void decode(std::istream& istream_in, std::ostream& ostream_in) { |
---|
37 | base64_init_decodestate(&_state); |
---|
38 | // |
---|
39 | const int N = _buffersize; |
---|
40 | char* code = new char[N]; |
---|
41 | char* plaintext = new char[N]; |
---|
42 | int codelength; |
---|
43 | int plainlength; |
---|
44 | |
---|
45 | do |
---|
46 | { |
---|
47 | istream_in.read((char*)code, N); |
---|
48 | codelength = istream_in.gcount(); |
---|
49 | plainlength = decode(code, codelength, plaintext); |
---|
50 | ostream_in.write((const char*)plaintext, plainlength); |
---|
51 | } |
---|
52 | while (istream_in.good() && codelength > 0); |
---|
53 | // |
---|
54 | base64_init_decodestate(&_state); |
---|
55 | |
---|
56 | delete [] code; |
---|
57 | delete [] plaintext; |
---|
58 | } |
---|
59 | }; |
---|
60 | |
---|
61 | } /* namespace base64 */ |
---|
62 | |
---|
63 | #endif // BASE64_DECODE_H |
---|