| 20 | } // namespace |
| 21 | |
| 22 | fl::string base64_encode(fl::span<const fl::u8> data) { |
| 23 | fl::string out; |
| 24 | if (data.empty()) return out; |
| 25 | |
| 26 | fl::size encoded_len = 4 * ((data.size() + 2) / 3); |
| 27 | out.reserve(encoded_len); |
| 28 | |
| 29 | fl::size i = 0; |
| 30 | fl::size len = data.size(); |
| 31 | |
| 32 | while (i + 2 < len) { |
| 33 | fl::u32 triple = (fl::u32(data[i]) << 16) | |
| 34 | (fl::u32(data[i + 1]) << 8) | |
| 35 | fl::u32(data[i + 2]); |
| 36 | out += kBase64Chars[(triple >> 18) & 0x3F]; |
| 37 | out += kBase64Chars[(triple >> 12) & 0x3F]; |
| 38 | out += kBase64Chars[(triple >> 6) & 0x3F]; |
| 39 | out += kBase64Chars[triple & 0x3F]; |
| 40 | i += 3; |
| 41 | } |
| 42 | |
| 43 | // Handle remaining bytes |
| 44 | if (i < len) { |
| 45 | fl::u32 triple = fl::u32(data[i]) << 16; |
| 46 | if (i + 1 < len) { |
| 47 | triple |= fl::u32(data[i + 1]) << 8; |
| 48 | } |
| 49 | out += kBase64Chars[(triple >> 18) & 0x3F]; |
| 50 | out += kBase64Chars[(triple >> 12) & 0x3F]; |
| 51 | if (i + 1 < len) { |
| 52 | out += kBase64Chars[(triple >> 6) & 0x3F]; |
| 53 | } else { |
| 54 | out += '='; |
| 55 | } |
| 56 | out += '='; |
| 57 | } |
| 58 | |
| 59 | return out; |
| 60 | } |
| 61 | |
| 62 | fl::vector<fl::u8> base64_decode(const fl::string& encoded) { |
| 63 | fl::vector<fl::u8> out; |
no test coverage detected