From 718b3e98fef04e658cb203f3e07746b9e6b1c8c7 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Wed, 26 Feb 2025 00:50:48 -0500 Subject: [PATCH] Use Base64-URL --- docs/CHANGELOG.md | 3 + mods/src/misc/base64.cpp | 121 +++++++++++++-------------------------- 2 files changed, 42 insertions(+), 82 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ae8c79c7..dca891e1 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -116,6 +116,9 @@ * Controller Support Removed * Brand New Launcher UI! +**2.5.4** +* Use Base64-URL Encoding When Accessing The Skin Server + **2.5.3** * Add `Replace Block Highlight With Outline` Feature Flag (Enabled By Default) * By Default, The Outline Width Is Set Using The GUI Scale diff --git a/mods/src/misc/base64.cpp b/mods/src/misc/base64.cpp index 434b6ac1..7651db18 100644 --- a/mods/src/misc/base64.cpp +++ b/mods/src/misc/base64.cpp @@ -1,96 +1,53 @@ -#include #include +#include -// Base64 Encoder (https://gist.github.com/tomykaira/f0fd86b6c73063283afe550bc5d77594) +// Base64-URL Encode/Decode (https://stackoverflow.com/a/57314480) +static constexpr char base64_url_alphabet[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' +}; std::string misc_base64_encode(const std::string &data) { - static constexpr char encoding_table[] = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', - 'w', 'x', 'y', 'z', '0', '1', '2', '3', - '4', '5', '6', '7', '8', '9', '+', '/' - }; - - const size_t in_len = data.size(); - const size_t out_len = 4 * ((in_len + 2) / 3); - std::string ret(out_len, '\0'); - size_t i; - char *p = const_cast(ret.c_str()); - - for (i = 0; i < in_len - 2; i += 3) { - *p++ = encoding_table[(data[i] >> 2) & 0x3f]; - *p++ = encoding_table[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xf0) >> 4)]; - *p++ = encoding_table[((data[i + 1] & 0xf) << 2) | ((int) (data[i + 2] & 0xc0) >> 6)]; - *p++ = encoding_table[data[i + 2] & 0x3f]; - } - if (i < in_len) { - *p++ = encoding_table[(data[i] >> 2) & 0x3f]; - if (i == (in_len - 1)) { - *p++ = encoding_table[((data[i] & 0x3) << 4)]; - *p++ = '='; + std::string out; + int val = 0; + int valb = -6; + const size_t len = data.length(); + for (unsigned int i = 0; i < len; i++) { + const unsigned char c = data[i]; + val = (val << 8) + c; + valb += 8; + while (valb >= 0) { + out.push_back(base64_url_alphabet[(val >> valb) & 0x3f]); + valb -= 6; } - else { - *p++ = encoding_table[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xf0) >> 4)]; - *p++ = encoding_table[((data[i + 1] & 0xf) << 2)]; - } - *p++ = '='; } - - return ret; + if (valb > -6) { + out.push_back(base64_url_alphabet[((val << 8) >> (valb + 8)) & 0x3f]); + } + return out; } - -// Base64 Decoder std::string misc_base64_decode(const std::string &input) { - static constexpr unsigned char decoding_table[] = { - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, - 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 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, 64, 64, 64, 64, 64, 64, 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, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64 - }; - - const size_t in_len = input.size(); - if (in_len % 4 != 0) { - return ""; + std::string out; + std::vector T(256, -1); + for (unsigned int i = 0; i < 64; i++) { + T[base64_url_alphabet[i]] = i; } - std::string out = ""; - size_t out_len = in_len / 4 * 3; - if (in_len >= 1 && input[in_len - 1] == '=') out_len--; - if (in_len >= 2 && input[in_len - 2] == '=') out_len--; - out.resize(out_len); - - for (size_t i = 0, j = 0; i < in_len;) { - const uint32_t a = input[i] == '=' ? 0 & i++ : decoding_table[static_cast(input[i++])]; - const uint32_t b = input[i] == '=' ? 0 & i++ : decoding_table[static_cast(input[i++])]; - const uint32_t c = input[i] == '=' ? 0 & i++ : decoding_table[static_cast(input[i++])]; - const uint32_t d = input[i] == '=' ? 0 & i++ : decoding_table[static_cast(input[i++])]; - - const uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6); - - if (j < out_len) { - out[j++] = (triple >> 2 * 8) & 0xFF; + int val = 0; + int valb = -8; + for (unsigned int i = 0; i < input.length(); i++) { + const unsigned char c = input[i]; + if (T[c] == -1) { + break; } - if (j < out_len) { - out[j++] = (triple >> 1 * 8) & 0xFF; - } - if (j < out_len) { - out[j++] = (triple >> 0 * 8) & 0xFF; + val = (val << 6) + T[c]; + valb += 6; + if (valb >= 0) { + out.push_back(char((val >> valb) & 0xff)); + valb -= 8; } } - return out; } \ No newline at end of file