31 #if defined(__APPLE__)
38 #include <openssl/bio.h>
39 #include <openssl/evp.h>
46 #pragma clang diagnostic push
47 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
52 int decode_length(
const string message) {
56 if (message[message.length()-1] ==
'=' && message[message.length()-2] ==
'=')
58 else if (message[message.length()-1] ==
'=')
61 return (
int)message.length()*0.75 - padding;
71 int encoded_length = (4*ceil((
double)message.length()/3)) + 64 + 1;
72 char *buffer =
new char [encoded_length];
74 stream =
fmemopen(buffer, encoded_length,
"w");
75 b64 = BIO_new(BIO_f_base64());
76 bio = BIO_new_fp(stream, BIO_NOCLOSE);
77 bio = BIO_push(b64, bio);
79 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
80 BIO_write(bio, message.c_str(), message.length());
82 int buffer_length = BIO_tell(bio);
86 string encoded_message(buffer, buffer_length);
89 return encoded_message;
95 int decoded_length = decode_length(message);
97 unsigned char *buffer =
new unsigned char [decoded_length+1];
98 FILE* stream =
fmemopen((
char*)message.c_str(), message.length(),
"r");
100 b64 = BIO_new(BIO_f_base64());
101 bio = BIO_new_fp(stream, BIO_NOCLOSE);
102 bio = BIO_push(b64, bio);
104 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
105 decoded_length = BIO_read(bio, buffer, message.length());
106 buffer[decoded_length] =
'\0';
111 string decoded_message((
const char *)buffer, decoded_length);
114 return decoded_message;
117 #pragma clang diagnostic pop
Compatibility Macros for C/C++.
static const string encode(const string &message, bool newlines=false)
Encodes message in base64.
FILE * fmemopen(void *buf, size_t size, const char *mode)
A BSD port of the fmemopen Linux method using funopen.
static const string decode(const string &message, bool newlines=false)
Decodes base64 message.