| 96 | } |
| 97 | |
| 98 | string EncodeBase64(const unsigned char* pch, size_t len) |
| 99 | { |
| 100 | static const char* pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 101 | |
| 102 | string strRet = ""; |
| 103 | strRet.reserve((len + 2) / 3 * 4); |
| 104 | |
| 105 | int mode = 0, left = 0; |
| 106 | const unsigned char* pchEnd = pch + len; |
| 107 | |
| 108 | while (pch < pchEnd) { |
| 109 | int enc = *(pch++); |
| 110 | switch (mode) { |
| 111 | case 0: // we have no bits |
| 112 | strRet += pbase64[enc >> 2]; |
| 113 | left = (enc & 3) << 4; |
| 114 | mode = 1; |
| 115 | break; |
| 116 | |
| 117 | case 1: // we have two bits |
| 118 | strRet += pbase64[left | (enc >> 4)]; |
| 119 | left = (enc & 15) << 2; |
| 120 | mode = 2; |
| 121 | break; |
| 122 | |
| 123 | case 2: // we have four bits |
| 124 | strRet += pbase64[left | (enc >> 6)]; |
| 125 | strRet += pbase64[enc & 63]; |
| 126 | mode = 0; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (mode) { |
| 132 | strRet += pbase64[left]; |
| 133 | strRet += '='; |
| 134 | if (mode == 1) |
| 135 | strRet += '='; |
| 136 | } |
| 137 | |
| 138 | return strRet; |
| 139 | } |
| 140 | |
| 141 | string EncodeBase64(const string& str) |
| 142 | { |