#include "MimeBase64.h" // This is based heavily on http://base64.sourceforge.net/b64.c /* ** Translation Table as described in RFC1113 */ static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /* ** encodeblock ** ** encode 3 8-bit binary bytes as 4 '6-bit' characters */ static void encodeblock(unsigned char const *in, std::string &out, int len) { out += cb64[ in[0] >> 2 ]; out += cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ]; if (len <= 1) out += '='; else out += cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ]; if (len <= 2) out += '='; else out += cb64[ in[2] & 0x3f ]; } /* ** encode ** ** base64 encode a string adding padding and line breaks as per spec. ** the result is appended to the destination. */ void base64Encode(std::string const &source, std::string &destination) { int blocksout = 0; // Use c_str(), not data(), to ensure that we have one extra byte on the // end. encodeblock will make use of that extra byte and will assume it // is 0. unsigned const char *toEncode = (unsigned const char *)source.c_str(); for (int available = source.size(); available > 0; available -= 3, toEncode += 3) { encodeblock(toEncode, destination, available); blocksout++; if ((blocksout >= 19) && (available > 0)) { if( blocksout ) { destination += "\r\n"; } blocksout = 0; } } } /* ** encode ** ** base64 encode a string adding padding and line breaks as per spec. ** the result is appended to the destination. */ std::string base64Encode(std::string const &source) { std::string result; const int blocks = (source.size() + 2) / 3; const int lineBreaks = blocks / 19; // This estimate might have room for one extra \r\n const int size = blocks * 4 + lineBreaks * 2; result.reserve(size); base64Encode(source, result); return result; } #ifdef __UNIT_TEST_ // g++ -Wall -ggdb -D__UNIT_TEST_ MimeBase64.C #include void testOne(std::string input) { // This could be fed into PHP. std::cout<<"if (\"" <