join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
base64.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_BASE64_HPP__
26#define __JOIN_BASE64_HPP__
27
28// libjoin.
29#include <join/openssl.hpp>
30
31// C++.
32#include <iostream>
33#include <iomanip>
34#include <string>
35
36namespace join
37{
39 using BytesArray = std::vector <uint8_t>;
40
46 __inline__ std::string bin2hex (const BytesArray& bin)
47 {
48 std::stringstream oss;
49 for (size_t i = 0; i < bin.size (); ++i)
50 {
51 oss << std::hex << std::setw (2) << std::setfill ('0') << static_cast <uint32_t> (bin[i]);
52 }
53 return oss.str ();
54 }
55
59 class Encoderbuf : public std::streambuf
60 {
61 public:
65 Encoderbuf ();
66
71 Encoderbuf (const Encoderbuf& other) = delete;
72
78 Encoderbuf& operator= (const Encoderbuf& other) = delete;
79
84 Encoderbuf (Encoderbuf&& other);
85
92
96 virtual ~Encoderbuf () = default;
97
102 std::string get ();
103
104 protected:
110 virtual int_type overflow (int_type c = traits_type::eof ()) override;
111
113 static const std::streamsize _bufsize = 256;
114
116 std::unique_ptr <char []> _buf;
117
120
122 std::string _out;
123 };
124
128 class Encoder : public std::ostream
129 {
130 public:
134 Encoder ();
135
140 Encoder (const Encoder& other) = delete;
141
147 Encoder& operator=(const Encoder& other) = delete;
148
153 Encoder (Encoder&& other);
154
160 Encoder& operator=(Encoder&& other);
161
165 virtual ~Encoder () = default;
166
171 std::string get ();
172
173 protected:
176 };
177
181 class Decoderbuf : public std::streambuf
182 {
183 public:
187 Decoderbuf ();
188
193 Decoderbuf (const Decoderbuf& other) = delete;
194
200 Decoderbuf& operator= (const Decoderbuf& other) = delete;
201
206 Decoderbuf (Decoderbuf&& other);
207
214
218 virtual ~Decoderbuf () = default;
219
224 BytesArray get ();
225
226 protected:
232 virtual int_type overflow (int_type c = traits_type::eof ()) override;
233
235 static const std::streamsize _bufsize = 256;
236
238 std::unique_ptr <char []> _buf;
239
242
245 };
246
250 class Decoder : public std::ostream
251 {
252 public:
256 Decoder ();
257
262 Decoder (const Decoder& other) = delete;
263
269 Decoder& operator=(const Decoder& other) = delete;
270
275 Decoder (Decoder&& other);
276
282 Decoder& operator=(Decoder&& other);
283
287 virtual ~Decoder () = default;
288
293 BytesArray get ();
294
295 protected:
298 };
299
303 class Base64
304 {
305 public:
309 Base64 () = delete;
310
314 ~Base64 () = delete;
315
322 static std::string encode (const char* data, size_t size);
323
329 static std::string encode (const std::string& data);
330
336 static std::string encode (const BytesArray& data);
337
343 static BytesArray decode (const std::string& data);
344 };
345}
346
347#endif
base64 encode, decode class.
Definition base64.hpp:304
static std::string encode(const char *data, size_t size)
encode data.
Definition base64.cpp:343
static BytesArray decode(const std::string &data)
decode a base64 encoded string.
Definition base64.cpp:372
Base64()=delete
create the Base64 instance.
~Base64()=delete
destroy the Base64 instance.
decoder.
Definition base64.hpp:251
Decoder(const Decoder &other)=delete
copy constructor.
Decoderbuf _decoderbuf
associated decoder stream buffer.
Definition base64.hpp:297
Decoder()
default constructor.
Definition base64.cpp:298
Decoder & operator=(const Decoder &other)=delete
copy assignment operator.
BytesArray get()
get decoded data.
Definition base64.cpp:329
virtual ~Decoder()=default
destroy instance.
decoder stream buffer.
Definition base64.hpp:182
static const std::streamsize _bufsize
internal buffer size.
Definition base64.hpp:235
Decoderbuf & operator=(const Decoderbuf &other)=delete
copy assignment operator.
Decoderbuf(const Decoderbuf &other)=delete
copy constructor.
std::unique_ptr< char[]> _buf
internal buffer.
Definition base64.hpp:238
BytesArray _out
output buffer.
Definition base64.hpp:244
EvpEncodeCtxPtr _decodectx
decode context.
Definition base64.hpp:241
Decoderbuf()
create decoder stream buffer instance.
Definition base64.cpp:190
virtual ~Decoderbuf()=default
destroy encoder stream buffer instance.
virtual int_type overflow(int_type c=traits_type::eof()) override
writes characters to the associated output sequence from the put area.
Definition base64.cpp:248
BytesArray get()
get decoded data.
Definition base64.cpp:224
encoder.
Definition base64.hpp:129
std::string get()
get encoded string.
Definition base64.cpp:176
Encoder & operator=(const Encoder &other)=delete
copy assignment operator.
virtual ~Encoder()=default
destroy instance.
Encoderbuf _encoderbuf
associated encoder stream buffer.
Definition base64.hpp:175
Encoder(const Encoder &other)=delete
copy constructor.
Encoder()
default constructor.
Definition base64.cpp:145
encoder stream buffer.
Definition base64.hpp:60
virtual ~Encoderbuf()=default
destroy encoder stream buffer instance.
Encoderbuf & operator=(const Encoderbuf &other)=delete
copy assignment operator.
std::string get()
get encoded string.
Definition base64.cpp:75
Encoderbuf(const Encoderbuf &other)=delete
copy constructor.
virtual int_type overflow(int_type c=traits_type::eof()) override
writes characters to the associated output sequence from the put area.
Definition base64.cpp:99
std::string _out
output buffer.
Definition base64.hpp:122
EvpEncodeCtxPtr _encodectx
encode context.
Definition base64.hpp:119
Encoderbuf()
create encoder stream buffer instance.
Definition base64.cpp:41
static const std::streamsize _bufsize
internal buffer size.
Definition base64.hpp:113
std::unique_ptr< char[]> _buf
internal buffer.
Definition base64.hpp:116
Definition acceptor.hpp:32
std::vector< uint8_t > BytesArray
bytes array.
Definition base64.hpp:39
__inline__ std::string bin2hex(const BytesArray &bin)
convert bytes array to string.
Definition base64.hpp:46
std::unique_ptr< EVP_ENCODE_CTX, EvpEncodeCtxDelete > EvpEncodeCtxPtr
Definition openssl.hpp:118