All Classes Namespaces Functions Variables Enumerations Enumerator Pages
encode.h
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 #define BUFFERSIZE 16777216
12 
13 #include <iostream>
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  return base64_encode_block(code_in, length_in, plaintext_out, &_state);
39  }
40 
41  int encode_end(char* plaintext_out)
42  {
43  return base64_encode_blockend(plaintext_out, &_state);
44  }
45 
46  void encode(std::istream& istream_in, std::ostream& ostream_in)
47  {
48  base64_init_encodestate(&_state);
49  //
50  const int N = _buffersize;
51  char* plaintext = new char[N];
52  char* code = new char[2*N];
53  int plainlength;
54  int codelength;
55 
56  do
57  {
58  istream_in.read(plaintext, N);
59  plainlength = istream_in.gcount();
60  //
61  codelength = encode(plaintext, plainlength, code);
62  ostream_in.write(code, codelength);
63  }
64  while (istream_in.good() && plainlength > 0);
65 
66  codelength = encode_end(code);
67  ostream_in.write(code, codelength);
68  //
69  base64_init_encodestate(&_state);
70 
71  delete [] code;
72  delete [] plaintext;
73  }
74  };
75 
76 } // namespace base64
77 
78 #endif // BASE64_ENCODE_H
79 
Definition: encode.h:17
Definition: encode.h:22
Definition: decode.h:15