in sdk/src/utils/Utils.cc [110:147]
std::string AlibabaCloud::PDS::Base64Encode(const char *src, int len)
{
if (!src || len == 0) {
return "";
}
static const char *ENC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
auto in = reinterpret_cast<const unsigned char *>(src);
auto inLen = len;
std::stringstream ss;
while (inLen) {
// first 6 bits of char 1
ss << ENC[*in >> 2];
if (!--inLen) {
// last 2 bits of char 1, 4 bits of 0
ss << ENC[(*in & 0x3) << 4];
ss << '=';
ss << '=';
break;
}
// last 2 bits of char 1, first 4 bits of char 2
ss << ENC[((*in & 0x3) << 4) | (*(in + 1) >> 4)];
in++;
if (!--inLen) {
// last 4 bits of char 2, 2 bits of 0
ss << ENC[(*in & 0xF) << 2];
ss << '=';
break;
}
// last 4 bits of char 2, first 2 bits of char 3
ss << ENC[((*in & 0xF) << 2) | (*(in + 1) >> 6)];
in++;
// last 6 bits of char 3
ss << ENC[*in & 0x3F];
in++, inLen--;
}
return ss.str();
}