| 1 | /** |
| 2 | * \file src/base64.cc |
| 3 | * \brief Base64 encoding/decoding (RFC 4648). |
| 4 | * |
| 5 | * Inspired by Jouni Malinen’s BSD implementation at |
| 6 | * <http://web.mit.edu/freebsd/head/contrib/wpa/src/utils/base64.c>. |
| 7 | * |
| 8 | * This implementation is used to decode the cover arts embedded in the tags. According to |
| 9 | * <https://wiki.xiph.org/VorbisComment>, line feeds are not allowed and padding is required. |
| 10 | */ |
| 11 | |
| 12 | #include <opustags.h> |
| 13 | |
| 14 | #include <string.h> |
| 15 | |
| 16 | static const char8_t base64_table[65] = |
| 17 | u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 18 | |
| 19 | std::u8string ot::encode_base64(ot::byte_string_view src) |
| 20 | { |
| 21 | size_t len = src.size(); |
| 22 | size_t num_blocks = (len + 2) / 3; // Count of 3-byte blocks, rounded up. |
| 23 | size_t olen = num_blocks * 4; // Each 3-byte block becomes 4 base64 bytes. |
| 24 | if (olen < len) |
| 25 | throw std::overflow_error("failed to encode excessively long base64 block"); |
| 26 | |
| 27 | std::u8string out; |
| 28 | out.resize(olen); |
| 29 | |
| 30 | const uint8_t* in = src.data(); |
| 31 | const uint8_t* end = in + len; |
| 32 | char8_t* pos = out.data(); |
| 33 | while (end - in >= 3) { |
| 34 | *pos++ = base64_table[in[0] >> 2]; |
| 35 | *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; |
| 36 | *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)]; |
| 37 | *pos++ = base64_table[in[2] & 0x3f]; |
| 38 | in += 3; |
| 39 | } |
| 40 | |
| 41 | if (end - in) { |
| 42 | *pos++ = base64_table[in[0] >> 2]; |
| 43 | if (end - in == 1) { |
| 44 | *pos++ = base64_table[(in[0] & 0x03) << 4]; |
| 45 | *pos++ = '='; |
| 46 | } else { // end - in == 2 |
| 47 | *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; |
| 48 | *pos++ = base64_table[(in[1] & 0x0f) << 2]; |
| 49 | } |
| 50 | *pos++ = '='; |
| 51 | } |
| 52 | |
| 53 | return out; |
| 54 | } |
| 55 | |
| 56 | ot::byte_string ot::decode_base64(std::u8string_view src) |
| 57 | { |
| 58 | // Remove the padding and rely on the string length instead. |
| 59 | while (src.back() == u8'=') |
| 60 | src.remove_suffix(1); |
| 61 | |
| 62 | size_t olen = src.size() / 4 * 3; // Whole blocks; |
| 63 | switch (src.size() % 4) { |
| 64 | case 1: throw status {st::error, "invalid base64 block size"}; |
| 65 | case 2: olen += 1; break; |
| 66 | case 3: olen += 2; break; |
| 67 | } |
| 68 | |
| 69 | ot::byte_string out; |
| 70 | out.resize(olen); |
| 71 | uint8_t* pos = out.data(); |
| 72 | |
| 73 | unsigned char dtable[256]; |
| 74 | memset(dtable, 0x80, 256); |
| 75 | for (size_t i = 0; i < sizeof(base64_table) - 1; ++i) |
| 76 | dtable[(size_t) base64_table[i]] = (unsigned char) i; |
| 77 | |
| 78 | unsigned char block[4]; |
| 79 | size_t count = 0; |
| 80 | for (unsigned char c : src) { |
| 81 | unsigned char tmp = dtable[c]; |
| 82 | if (tmp == 0x80) |
| 83 | throw status {st::error, "invalid base64 character"}; |
| 84 | |
| 85 | block[count++] = tmp; |
| 86 | if (count == 2) { |
| 87 | *pos++ = (block[0] << 2) | (block[1] >> 4); |
| 88 | } else if (count == 3) { |
| 89 | *pos++ = (block[1] << 4) | (block[2] >> 2); |
| 90 | } else if (count == 4) { |
| 91 | *pos++ = (block[2] << 6) | block[3]; |
| 92 | count = 0; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return out; |
| 97 | } |