source: branches/uq/src/core/b64/encode.h @ 5679

Last change on this file since 5679 was 5679, checked in by ldelgass, 9 years ago

Full merge 1.3 branch to uq branch to sync. Fixed partial subdirectory merge
by removing mergeinfo from lang/python/Rappture directory.

  • Property svn:eol-style set to native
File size: 1.6 KB
Line 
1// :mode=c++:
2/*
3encode.h - c++ wrapper for a base64 encoding algorithm
4
5This is part of the libb64 project, and has been placed in the public domain.
6For details, see http://sourceforge.net/projects/libb64
7*/
8
9#ifndef BASE64_ENCODE_H
10#define BASE64_ENCODE_H
11
12#include <iostream>
13
14namespace 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            base64_init_encodestate(&_state);
31        }
32                int encode(char value_in)
33                {
34                        return base64_encode_value(value_in);
35                }
36                int encode(const char* code_in, const int length_in, char* plaintext_out)
37                {
38                        return base64_encode_block(code_in, length_in, plaintext_out, &_state);
39                }
40                int encode_end(char* plaintext_out)
41                {
42                        return base64_encode_blockend(plaintext_out, &_state);
43                }
44                void encode(std::istream& istream_in, std::ostream& ostream_in)
45                {
46                        base64_init_encodestate(&_state);
47                        //
48                        const int N = _buffersize;
49                        char* plaintext = new char[N];
50                        char* code = new char[2*N];
51                        int plainlength;
52                        int codelength;
53                       
54                        do
55                        {
56                                istream_in.read(plaintext, N);
57                                plainlength = istream_in.gcount();
58                                //
59                                codelength = encode(plaintext, plainlength, code);
60                                ostream_in.write(code, codelength);
61                        }
62                        while (istream_in.good() && plainlength > 0);
63                       
64                        codelength = encode_end(code);
65                        ostream_in.write(code, codelength);
66                        //
67                        base64_init_encodestate(&_state);
68                       
69                        delete [] code;
70                        delete [] plaintext;
71                }
72        };
73       
74} // namespace base64
75
76#endif // BASE64_ENCODE_H
77
Note: See TracBrowser for help on using the repository browser.