* * ByteStreamToBase64 * ------------------ * * Converts binary encoded data to BASE64 format. * */
| 75 | * |
| 76 | */ |
| 77 | std::string ByteStreamToBase64 (// base64 encoded string |
| 78 | const uint8_t * InBuffer, // Input buffer, binary data |
| 79 | size_t InCount // Number of bytes in the input buffer |
| 80 | ) |
| 81 | { |
| 82 | unsigned char * ps; |
| 83 | unsigned char acc_1; |
| 84 | unsigned char acc_2; |
| 85 | int i; |
| 86 | int n; |
| 87 | int m; |
| 88 | |
| 89 | ps = (unsigned char *)InBuffer; |
| 90 | n = InCount / 3; |
| 91 | m = InCount % 3; |
| 92 | size_t outCount = m ? (4 * (n + 1)) : (4 * n); |
| 93 | |
| 94 | std::string out; |
| 95 | out.reserve (outCount); |
| 96 | for ( i = 0; i < n; i++ ) |
| 97 | { |
| 98 | acc_1 = *ps++; |
| 99 | acc_2 = (acc_1 << 4) & 0x30; |
| 100 | acc_1 >>= 2; // base64 digit #1 |
| 101 | out.push_back (T64[acc_1]); |
| 102 | acc_1 = *ps++; |
| 103 | acc_2 |= acc_1 >> 4; // base64 digit #2 |
| 104 | out.push_back (T64[acc_2]); |
| 105 | acc_1 &= 0x0f; |
| 106 | acc_1 <<= 2; |
| 107 | acc_2 = *ps++; |
| 108 | acc_1 |= acc_2 >> 6; // base64 digit #3 |
| 109 | out.push_back (T64[acc_1]); |
| 110 | acc_2 &= 0x3f; // base64 digit #4 |
| 111 | out.push_back (T64[acc_2]); |
| 112 | } |
| 113 | if ( m == 1 ) |
| 114 | { |
| 115 | acc_1 = *ps++; |
| 116 | acc_2 = (acc_1 << 4) & 0x3f; // base64 digit #2 |
| 117 | acc_1 >>= 2; // base64 digit #1 |
| 118 | out.push_back (T64[acc_1]); |
| 119 | out.push_back (T64[acc_2]); |
| 120 | out.push_back (P64); |
| 121 | out.push_back (P64); |
| 122 | |
| 123 | } |
| 124 | else if ( m == 2 ) |
| 125 | { |
| 126 | acc_1 = *ps++; |
| 127 | acc_2 = (acc_1 << 4) & 0x3f; |
| 128 | acc_1 >>= 2; // base64 digit #1 |
| 129 | out.push_back (T64[acc_1]); |
| 130 | acc_1 = *ps++; |
| 131 | acc_2 |= acc_1 >> 4; // base64 digit #2 |
| 132 | out.push_back (T64[acc_2]); |
| 133 | acc_1 &= 0x0f; |
| 134 | acc_1 <<= 2; // base64 digit #3 |