From 45c427b23e035aac49bc961115a1beff1f74d5ee Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 20 Sep 2020 03:58:50 +0200 Subject: Use url/filename safe base64 --- include/base64_url.hpp | 38 ++++++++++++++++++++++++++++++++++++++ src/DownloadUtils.cpp | 4 ++-- src/QuickMedia.cpp | 6 +++--- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 include/base64_url.hpp diff --git a/include/base64_url.hpp b/include/base64_url.hpp new file mode 100644 index 0000000..271940e --- /dev/null +++ b/include/base64_url.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include + +static constexpr const 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', '-', '_' +}; + +class base64_url_impl +{ +public: + template using codec_impl = cppcodec::detail::stream_codec; + + static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() { + static_assert(sizeof(base64_url_alphabet) == 64, "base64 alphabet must have 64 values"); + return sizeof(base64_url_alphabet); + } + static CPPCODEC_ALWAYS_INLINE constexpr char symbol(cppcodec::detail::alphabet_index_t idx) + { + return base64_url_alphabet[idx]; + } + static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol(char c) { return c; } + + static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding() { return true; } + static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding() { return true; } + static CPPCODEC_ALWAYS_INLINE constexpr char padding_symbol() { return '='; } + static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol(char c) { return c == '='; } + static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol(char c) { return c == '\0'; } + + // RFC4648 does not specify any whitespace being allowed in base64 encodings. + static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore(char) { return false; } +}; + +using base64_url = cppcodec::detail::codec>; \ No newline at end of file diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index cb87890..deb5c29 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -1,8 +1,8 @@ #include "../include/DownloadUtils.hpp" #include "../include/Program.h" #include "../include/Storage.hpp" +#include "../include/base64_url.hpp" #include -#include static const bool debug_download = false; @@ -48,7 +48,7 @@ namespace QuickMedia { DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent) { Path media_dir = get_cache_dir().join("media"); - Path media_file_path = Path(media_dir).join(cppcodec::base64_rfc4648::encode(url)); + Path media_file_path = Path(media_dir).join(base64_url::encode(url)); Path media_file_path_tmp(media_file_path.data + ".tmp"); if(get_file_type(media_file_path) == FileType::REGULAR) { if(file_get_content(media_file_path, result) == 0) { diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 71ba2a5..956370f 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -15,7 +15,7 @@ #include "../include/Notification.hpp" #include "../include/ImageViewer.hpp" #include "../include/ImageUtils.hpp" -#include +#include "../include/base64_url.hpp" #include #include @@ -498,11 +498,11 @@ namespace QuickMedia { } static std::string base64_encode(const std::string &data) { - return cppcodec::base64_rfc4648::encode(data); + return base64_url::encode(data); } static std::string base64_decode(const std::string &data) { - return cppcodec::base64_rfc4648::decode(data); + return base64_url::decode(data); } static bool read_file_as_json(const Path &filepath, Json::Value &result) { -- cgit v1.2.3