Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 1 | #include <base64.hpp> |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 2 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 3 | namespace base64 { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 4 | bool base64_encode(const std::string &input, std::string &output) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 5 | // This is left as a raw array (and not a range checked std::array) under the |
| 6 | // suspicion that the optimizer is not smart enough to remove the range checks |
| 7 | // that would be done below if at were called. As is, this array is 64 bytes |
| 8 | // long, which should be greater than the max of 0b00111111 when indexed |
| 9 | // NOLINT calls below are to silence clang-tidy about this |
| 10 | // TODO(ed) this requires further investigation if a more safe method could be |
| 11 | // used without performance impact. |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 12 | static const char encoding_data[] = |
| 13 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 14 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 15 | size_t input_length = input.size(); |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 16 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 17 | // allocate space for output string |
| 18 | output.clear(); |
| 19 | output.reserve(((input_length + 2) / 3) * 4); |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 20 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 21 | // for each 3-bytes sequence from the input, extract 4 6-bits sequences and |
| 22 | // encode using |
| 23 | // encoding_data lookup table. |
| 24 | // if input do not contains enough chars to complete 3-byte sequence,use pad |
| 25 | // char '=' |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 26 | for (size_t i = 0; i < input_length; i++) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 27 | int base64code0 = 0; |
| 28 | int base64code1 = 0; |
| 29 | int base64code2 = 0; |
| 30 | int base64code3 = 0; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 31 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 32 | base64code0 = (input[i] >> 2) & 0x3f; // 1-byte 6 bits |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 33 | |
| 34 | output += encoding_data[base64code0]; // NOLINT |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 35 | base64code1 = (input[i] << 4) & 0x3f; // 1-byte 2 bits + |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 36 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 37 | if (++i < input_length) { |
| 38 | base64code1 |= (input[i] >> 4) & 0x0f; // 2-byte 4 bits |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 39 | output += encoding_data[base64code1]; // NOLINT |
| 40 | base64code2 = (input[i] << 2) & 0x3f; // 2-byte 4 bits + |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 41 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 42 | if (++i < input_length) { |
| 43 | base64code2 |= (input[i] >> 6) & 0x03; // 3-byte 2 bits |
| 44 | base64code3 = input[i] & 0x3f; // 3-byte 6 bits |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 45 | output += encoding_data[base64code2]; // NOLINT |
| 46 | output += encoding_data[base64code3]; // NOLINT |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 47 | } else { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 48 | output += encoding_data[base64code2]; // NOLINT |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 49 | output += '='; |
| 50 | } |
| 51 | } else { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 52 | output += encoding_data[base64code1]; // NOLINT |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 53 | output += '='; |
| 54 | output += '='; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 55 | } |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 56 | } |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 57 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 58 | return true; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 61 | bool base64_decode(const std::string &input, std::string &output) { |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 62 | static const char nop = -1; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 63 | // See note on encoding_data[] in above function |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 64 | static const char decoding_data[] = { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 65 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 66 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 67 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 62, nop, |
| 68 | nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, nop, nop, |
| 69 | nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 70 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 71 | 25, nop, nop, nop, nop, nop, nop, 26, 27, 28, 29, 30, 31, 32, 33, |
| 72 | 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 73 | 49, 50, 51, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 74 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 75 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 76 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 77 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 78 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 79 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 80 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 81 | nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, |
| 82 | nop}; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 83 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 84 | size_t input_length = input.size(); |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 85 | |
| 86 | // allocate space for output string |
| 87 | output.clear(); |
| 88 | output.reserve(((input_length + 2) / 3) * 4); |
| 89 | |
| 90 | // for each 4-bytes sequence from the input, extract 4 6-bits sequences by |
| 91 | // droping first two bits |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 92 | // and regenerate into 3 8-bits sequences |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 93 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 94 | for (size_t i = 0; i < input_length; i++) { |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 95 | char base64code0; |
| 96 | char base64code1; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 97 | char base64code2 = 0; // initialized to 0 to suppress warnings |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 98 | char base64code3; |
| 99 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 100 | base64code0 = decoding_data[static_cast<int>(input[i])]; // NOLINT |
| 101 | if (base64code0 == nop) { // non base64 character |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 102 | return false; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 103 | } |
| 104 | if (!(++i < input_length)) { // we need at least two input bytes for first |
| 105 | // byte output |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 106 | return false; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 107 | } |
| 108 | base64code1 = decoding_data[static_cast<int>(input[i])]; // NOLINT |
| 109 | if (base64code1 == nop) { // non base64 character |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 110 | return false; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 111 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 112 | output += |
| 113 | static_cast<char>((base64code0 << 2) | ((base64code1 >> 4) & 0x3)); |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 114 | |
| 115 | if (++i < input_length) { |
| 116 | char c = input[i]; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 117 | if (c == '=') { // padding , end of input |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 118 | return (base64code1 & 0x0f) == 0; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 119 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 120 | base64code2 = decoding_data[static_cast<int>(input[i])]; // NOLINT |
| 121 | if (base64code2 == nop) { // non base64 character |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 122 | return false; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 123 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 124 | output += static_cast<char>(((base64code1 << 4) & 0xf0) | |
| 125 | ((base64code2 >> 2) & 0x0f)); |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (++i < input_length) { |
| 129 | char c = input[i]; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 130 | if (c == '=') { // padding , end of input |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 131 | return (base64code2 & 0x03) == 0; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 132 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 133 | base64code3 = decoding_data[static_cast<int>(input[i])]; // NOLINT |
| 134 | if (base64code3 == nop) { // non base64 character |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 135 | return false; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 136 | } |
| 137 | output += static_cast<char>((((base64code2 << 6) & 0xc0) | base64code3)); |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 143 | } // namespace base64 |