| 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>cxx . . $ `<file>/src/opustags.h`/ |
| 13 | |
| 14 | #include <string.h> |
| 15 | |
| 16 | static const char8_t base64_tablecxx . . $ base64_table.[65] = |
| 17 | u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 18 | |
| 19 | stdcxx . . $ std/::u8stringcxx . . $ std/u8string# otcxx . . $ ot/::encode_base64cxx . . $ ot/encode_base64(86a0a94dd06bebac).(otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view# src) |
| 20 | { |
| 21 | size_t len = src.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).(); |
| 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 stdcxx . . $ std/::overflow_errorcxx . . $ std/overflow_error#("failed to encode excessively long base64 block"); |
| 26 | |
| 27 | stdcxx . . $ std/::u8stringcxx . . $ std/u8string# outcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 28 | out.resizecxx . . $ std/__cxx11/basic_string#resize(a21092e7312342c).(olen); |
| 29 | |
| 30 | const uint8_t* in = src.datacxx . . $ std/basic_string_view#data(8e614c7de0b1573e).(); |
| 31 | const uint8_t* end = in + len; |
| 32 | char8_t* pos = out.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).(); |
| 33 | while (end - in >= 3) { |
| 34 | *pos++ = base64_tablecxx . . $ base64_table.[in[0] >> 2]; |
| 35 | *pos++ = base64_tablecxx . . $ base64_table.[((in[0] & 0x03) << 4) | (in[1] >> 4)]; |
| 36 | *pos++ = base64_tablecxx . . $ base64_table.[((in[1] & 0x0f) << 2) | (in[2] >> 6)]; |
| 37 | *pos++ = base64_tablecxx . . $ base64_table.[in[2] & 0x3f]; |
| 38 | in += 3; |
| 39 | } |
| 40 | |
| 41 | if (end - in) { |
| 42 | *pos++ = base64_tablecxx . . $ base64_table.[in[0] >> 2]; |
| 43 | if (end - in == 1) { |
| 44 | *pos++ = base64_tablecxx . . $ base64_table.[(in[0] & 0x03) << 4]; |
| 45 | *pos++ = '='; |
| 46 | } else { // end - in == 2 |
| 47 | *pos++ = base64_tablecxx . . $ base64_table.[((in[0] & 0x03) << 4) | (in[1] >> 4)]; |
| 48 | *pos++ = base64_tablecxx . . $ base64_table.[(in[1] & 0x0f) << 2]; |
| 49 | } |
| 50 | *pos++ = '='; |
| 51 | } |
| 52 | |
| 53 | return outcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).; |
| 54 | } |
| 55 | |
| 56 | otcxx . . $ ot/::byte_stringcxx . . $ ot/byte_string# otcxx . . $ ot/::decode_base64cxx . . $ ot/decode_base64(4ea7d39b30a46aec).(stdcxx . . $ std/::u8string_viewcxx . . $ std/u8string_view# src) |
| 57 | { |
| 58 | // Remove the padding and rely on the string length instead. |
| 59 | while (src.backcxx . . $ std/basic_string_view#back(2042464fc1fea03e).() == u8'=') |
| 60 | src.remove_suffixcxx . . $ std/basic_string_view#remove_suffix(203741fbd813a59e).(1); |
| 61 | |
| 62 | size_t olen = src.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).() / 4 * 3; // Whole blocks; |
| 63 | switch (src.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).() % 4) { |
| 64 | case 1: throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "invalid base64 block size"}; |
| 65 | case 2: olen += 1; break; |
| 66 | case 3: olen += 2; break; |
| 67 | } |
| 68 | |
| 69 | otcxx . . $ ot/::byte_stringcxx . . $ ot/byte_string# outcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 70 | out.resizecxx . . $ std/__cxx11/basic_string#resize(a21092e7312342c).(olen); |
| 71 | uint8_t* pos = out.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).(); |
| 72 | |
| 73 | unsigned char dtable[256]; |
| 74 | memset(dtable, 0x80, 256); |
| 75 | for (size_t i = 0; i < sizeof(base64_tablecxx . . $ base64_table.) - 1; ++i) |
| 76 | dtable[(size_t) base64_tablecxx . . $ 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 statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::errorcxx . . $ ot/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 outcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).; |
| 97 | } |