Base64.hpp
1 // taken from http://www.adp-gmbh.ch/cpp/common/base64.html
2 #ifndef BASE64_H_5FKG12HF
3 #define BASE64_H_5FKG12HF
4 
5 extern "C" {
6 #include "Base64.h"
7 }
8 
9 #include <stdlib.h>
10 #include <string>
11 #include "uscxml/Common.h"
12 
13 namespace uscxml {
14 
15 USCXML_API inline std::string base64Encode(const char* data, unsigned int len, bool withBlockEnd = true) {
17  base64_init_encodestate(ctx);
18 
25  int written = 0;
26  char* out = (char*)malloc(len * 1.4 + 814);
27  written += base64_encode_block(data, len, out, ctx);
28  if (withBlockEnd) {
29  written += base64_encode_blockend(out + written, ctx);
30  written--; // drop the newline
31  }
32  std::string result(out, written);
33  free(ctx);
34  free(out);
35  return result;
36 }
37 
38 USCXML_API inline std::string base64Decode(const std::string& data) {
40  base64_init_decodestate(ctx);
41 
42  char* out = (char*)malloc(data.size());
43  size_t size = base64_decode_block(data.data(), data.size(), out, ctx);
44  free(ctx);
45  std::string result(out, size);
46  free(out);
47  return result;
48 }
49 
50 // USCXML_API std::string base64Decode(const std::string& data);
51 // USCXML_API std::string base64Encode(const char* data, unsigned int len);
52 
53 }
54 #endif /* end of include guard: BASE64_H_5FKG12HF */
Definition: Breakpoint.cpp:26
Definition: Base64.h:32
Definition: Base64.h:47