| #include <base64.hpp> |
| #include <cassert> |
| |
| namespace base64 { |
| bool base64_encode(const std::string &input, std::string &output) { |
| static const char encoding_data[] = |
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| |
| unsigned int input_length = input.size(); |
| |
| // allocate space for output string |
| output.clear(); |
| output.reserve(((input_length + 2) / 3) * 4); |
| |
| // for each 3-bytes sequence from the input, extract 4 6-bits sequences and |
| // encode using |
| // encoding_data lookup table. |
| // if input do not contains enough chars to complete 3-byte sequence,use pad |
| // char '=' |
| for (unsigned int i = 0; i < input_length; i++) { |
| int base64code0 = 0; |
| int base64code1 = 0; |
| int base64code2 = 0; |
| int base64code3 = 0; |
| |
| base64code0 = (input[i] >> 2) & 0x3f; // 1-byte 6 bits |
| output += encoding_data[base64code0]; |
| base64code1 = (input[i] << 4) & 0x3f; // 1-byte 2 bits + |
| |
| if (++i < input_length) { |
| base64code1 |= (input[i] >> 4) & 0x0f; // 2-byte 4 bits |
| output += encoding_data[base64code1]; |
| base64code2 = (input[i] << 2) & 0x3f; // 2-byte 4 bits + |
| |
| if (++i < input_length) { |
| base64code2 |= (input[i] >> 6) & 0x03; // 3-byte 2 bits |
| base64code3 = input[i] & 0x3f; // 3-byte 6 bits |
| output += encoding_data[base64code2]; |
| output += encoding_data[base64code3]; |
| } else { |
| output += encoding_data[base64code2]; |
| output += '='; |
| } |
| } else { |
| output += encoding_data[base64code1]; |
| output += '='; |
| output += '='; |
| } |
| } |
| |
| return true; |
| } |
| |
| bool base64_decode(const std::string &input, std::string &output) { |
| static const char nop = -1; |
| static const char decoding_data[] = { |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 62, nop, |
| nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, nop, nop, |
| nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 25, nop, nop, nop, nop, nop, nop, 26, 27, 28, 29, 30, 31, 32, 33, |
| 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 49, 50, 51, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| nop}; |
| |
| unsigned int input_length = input.size(); |
| |
| // allocate space for output string |
| output.clear(); |
| output.reserve(((input_length + 2) / 3) * 4); |
| |
| // for each 4-bytes sequence from the input, extract 4 6-bits sequences by |
| // droping first two bits |
| // and regenerate into 3 8-bits sequence |
| |
| for (unsigned int i = 0; i < input_length; i++) { |
| char base64code0; |
| char base64code1; |
| char base64code2 = 0; // initialized to 0 to suppress warnings |
| char base64code3; |
| |
| base64code0 = decoding_data[static_cast<int>(input[i])]; |
| if (base64code0 == nop) // non base64 character |
| return false; |
| if (!(++i < input_length)) // we need at least two input bytes for first |
| // byte output |
| return false; |
| base64code1 = decoding_data[static_cast<int>(input[i])]; |
| if (base64code1 == nop) // non base64 character |
| return false; |
| |
| output += ((base64code0 << 2) | ((base64code1 >> 4) & 0x3)); |
| |
| if (++i < input_length) { |
| char c = input[i]; |
| if (c == '=') { // padding , end of input |
| assert((base64code1 & 0x0f) == 0); |
| return true; |
| } |
| base64code2 = decoding_data[static_cast<int>(input[i])]; |
| if (base64code2 == nop) // non base64 character |
| return false; |
| |
| output += ((base64code1 << 4) & 0xf0) | ((base64code2 >> 2) & 0x0f); |
| } |
| |
| if (++i < input_length) { |
| char c = input[i]; |
| if (c == '=') { // padding , end of input |
| assert((base64code2 & 0x03) == 0); |
| return true; |
| } |
| base64code3 = decoding_data[static_cast<int>(input[i])]; |
| if (base64code3 == nop) // non base64 character |
| return false; |
| |
| output += (((base64code2 << 6) & 0xc0) | base64code3); |
| } |
| } |
| |
| return true; |
| } |
| } |