libUTL++
Base64encoder.h
1 #pragma once
2 
4 
5 #include <libutl/Encoder.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 // Base64encode ////////////////////////////////////////////////////////////////////////////////////
14 
28 
30 class Base64encode : public Object
31 {
35 
36 public:
42  Base64encode(utl::Stream* os, bool owner = false)
43  {
44  init();
45  setOutputStream(os, owner);
46  }
47 
49  Stream*
50  stream() const
51  {
52  return _os;
53  }
54 
56  bool
57  streamOwner() const
58  {
59  return _owner;
60  }
61 
67  void
68  setOutputStream(Stream* os, bool owner = false)
69  {
70  if (_owner) delete _os;
71  _os = os;
72  _owner = owner;
73  }
74 
76  void process(const byte_t* data, size_t dataLen);
77 
79  void finalize();
80 
81 private:
82  void
83  init()
84  {
85  _dataLen = 0;
86  _owner = false;
87  _os = nullptr;
88  }
89  void
90  deInit()
91  {
92  try
93  {
94  finalize();
95  }
96  catch (...)
97  {
98  }
99  setOutputStream(nullptr);
100  }
101 
102 private:
103  byte_t _data[3];
104  byte_t _dataLen;
105  byte_t _owner;
106  utl::Stream* _os;
107 };
108 
110 // Base64decode ////////////////////////////////////////////////////////////////////////////////////
112 
123 
125 class Base64decode : public Object
126 {
130 
131 public:
137  Base64decode(utl::Stream* os, bool owner = false)
138  {
139  init();
140  setOutputStream(os, owner);
141  }
142 
144  Stream*
145  stream() const
146  {
147  return _os;
148  }
149 
151  bool
152  streamOwner() const
153  {
154  return _owner;
155  }
156 
162  void
163  setOutputStream(Stream* os, bool owner = false)
164  {
165  if (_owner) delete _os;
166  _os = os;
167  _owner = owner;
168  }
169 
171  void process(const byte_t* data, size_t dataLen);
172 
174  void finalize();
175 
176 private:
177  void init();
178  void
179  deInit()
180  {
181  try
182  {
183  finalize();
184  }
185  catch (...)
186  {
187  }
188  setOutputStream(nullptr);
189  }
190 
191 private:
192  uint32_t _data;
193  uint32_t _dataShift;
194  byte_t _owner;
195  utl::Stream* _os;
196 };
197 
199 // Base64encoder ///////////////////////////////////////////////////////////////////////////////////
201 
212 
214 class Base64encoder : public Encoder
215 {
217 
218 public:
225  Base64encoder(uint_t mode, Stream* stream, bool owner = true)
226  {
227  init();
228  start(mode, stream, owner);
229  }
230 
231  virtual size_t encode(const byte_t* block, size_t num);
232 
233  virtual size_t decode(byte_t* block, size_t num);
234 
241  void start(uint_t mode, Stream* stream, bool owner = true);
242 
243 protected:
244  virtual void clear();
245  virtual void finishEncoding();
246  virtual size_t finishDecoding();
247 
248 private:
249  void
250  init()
251  {
252  _codec.object = nullptr;
253  }
254  void
255  deInit()
256  {
257  close();
258  }
259 
260 private:
261  union
262  {
263  Object* object;
264  Base64encode* encode;
265  Base64decode* decode;
266  } _codec;
267 };
268 
270 
271 UTL_NS_END;
bool streamOwner() const
Get the ownership flag for the output stream.
Definition: Base64encoder.h:57
void deInit()
De-initialize UTL++.
Encoder/decoder abstraction.
Definition: Encoder.h:39
Stream * stream() const
Get the output stream.
Decode binary data that was encoded using Base64 (the MIME / RFC 2045 encoding).
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
unsigned char byte_t
Unsigned character.
Definition: types.h:31
Base64encoder(uint_t mode, Stream *stream, bool owner=true)
Constructor.
unsigned int uint32_t
Unsigned 32-bit integer.
Definition: types.h:115
Base64encode(utl::Stream *os, bool owner=false)
Constructor.
Definition: Base64encoder.h:42
Base64decode(utl::Stream *os, bool owner=false)
Constructor.
void setOutputStream(Stream *os, bool owner=false)
Set output stream.
Stream * stream() const
Get the output stream.
Definition: Base64encoder.h:50
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
void setOutputStream(Stream *os, bool owner=false)
Set output stream.
Definition: Base64encoder.h:68
Stream I/O abstraction.
Definition: Stream.h:68
#define UTL_CLASS_NO_COMPARE
Declare that a class cannot do compare().
Definition: macros.h:344
Encode binary data using Base64 (the MIME / RFC 2045 encoding).
Definition: Base64encoder.h:30
#define UTL_CLASS_NO_COPY
Declare that a class cannot do copy().
Definition: macros.h:358
bool streamOwner() const
Get the ownership flag for the output stream.
Root of UTL++ class hierarchy.
Definition: Object.h:52
Base64 encoder/decoder stream.
void init()
Initialize UTL++.