From 0d9d9de370d6284b763377aea75937dd4025ca0e Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Wed, 27 Mar 2024 00:09:25 +1100 Subject: [PATCH] base64: apply ReneNyffenegger/cpp-base64#27 and ReneNyffenegger/cpp-base64#36 --- ext/base64/base64.cpp | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/ext/base64/base64.cpp b/ext/base64/base64.cpp index 843719e..2fad147 100644 --- a/ext/base64/base64.cpp +++ b/ext/base64/base64.cpp @@ -34,14 +34,19 @@ #include "base64.h" #include + +#if defined(__cpp_exceptions) #include +#else +#include +#endif // // Depending on the url parameter in base64_chars, one of // two sets of base64 characters needs to be chosen. // They differ in their last two characters. // -static const char* base64_chars[2] = { +static const char* to_base64_chars[2] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789" @@ -52,22 +57,40 @@ static const char* base64_chars[2] = { "0123456789" "-_"}; +static const unsigned char from_base64_chars[256] = { + 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, 62, 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, 63, + 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 +}; + static unsigned int pos_of_char(const unsigned char chr) { // // Return the position of chr within base64_encode() // - if (chr >= 'A' && chr <= 'Z') return chr - 'A'; - else if (chr >= 'a' && chr <= 'z') return chr - 'a' + ('Z' - 'A') + 1; - else if (chr >= '0' && chr <= '9') return chr - '0' + ('Z' - 'A') + ('z' - 'a') + 2; - else if (chr == '+' || chr == '-') return 62; // Be liberal with input and accept both url ('-') and non-url ('+') base 64 characters ( - else if (chr == '/' || chr == '_') return 63; // Ditto for '/' and '_' - else + if (from_base64_chars[chr] != 64) return from_base64_chars[chr]; // // 2020-10-23: Throw std::exception rather than const char* //(Pablo Martin-Gomez, https://github.com/Bouska) // +#if defined(__cpp_exceptions) throw std::runtime_error("Input is not valid base64-encoded data."); +#else + assert(!"Input is not valid base64-encoded data."); +#endif } static std::string insert_linebreaks(std::string str, size_t distance) { @@ -123,7 +146,7 @@ std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, b // the correct character set is chosen by subscripting // base64_chars with url. // - const char* base64_chars_ = base64_chars[url]; + const char* base64_chars_ = to_base64_chars[url]; std::string ret; ret.reserve(len_encoded);