diff --git a/.vscode/launch.json b/.vscode/launch.json index 5dd21d40e..5ab4b119c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,6 +6,18 @@ "configurations": [ { "name": "MaaHttp - Debug", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/bin/Debug/MaaHttp", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "lldb" + }, + { + "name": "MaaHttp - Debug Win", "type": "cppvsdbg", "request": "launch", "program": "${workspaceFolder}/build/bin/Debug/MaaHttp.exe", diff --git a/.vscode/settings.json b/.vscode/settings.json index 667c48607..c0db8efc7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -47,5 +47,6 @@ ], "python.analysis.extraPaths": [ "./source/binding/Python" - ] + ], + "editor.formatOnSave": true } \ No newline at end of file diff --git a/source/MaaHttp/LHG b/source/MaaHttp/LHG index c38ee1fa9..6a18c8731 160000 --- a/source/MaaHttp/LHG +++ b/source/MaaHttp/LHG @@ -1 +1 @@ -Subproject commit c38ee1fa96fb5a466fc1a7e0425024392f07e020 +Subproject commit 6a18c8731d27265219a309cf34ce9b7b9a22564a diff --git a/source/MaaHttp/base64.hpp b/source/MaaHttp/base64.hpp deleted file mode 100644 index 161fcffb6..000000000 --- a/source/MaaHttp/base64.hpp +++ /dev/null @@ -1,109 +0,0 @@ -#ifndef BASE_64_HPP -#define BASE_64_HPP - -#include -#include -#include -#include - -namespace base64 -{ - -inline constexpr std::string_view base64_chars { - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/" -}; - -template -inline OutputBuffer encode_into(InputIterator begin, InputIterator end) -{ - static_assert( - std::is_same_v, char> - || std::is_same_v, unsigned char> - || std::is_same_v, std::byte>); - - size_t counter = 0; - uint32_t bit_stream = 0; - size_t offset = 0; - OutputBuffer encoded; - encoded.reserve(static_cast(1.5 * static_cast(std::distance(begin, end)))); - while (begin != end) { - const auto num_val = static_cast(*begin); - offset = 16 - counter % 3 * 8; - bit_stream += num_val << offset; - if (offset == 16) { - encoded.push_back(base64_chars[bit_stream >> 18 & 0x3f]); - } - if (offset == 8) { - encoded.push_back(base64_chars[bit_stream >> 12 & 0x3f]); - } - if (offset == 0 && counter != 3) { - encoded.push_back(base64_chars[bit_stream >> 6 & 0x3f]); - encoded.push_back(base64_chars[bit_stream & 0x3f]); - bit_stream = 0; - } - ++counter; - ++begin; - } - if (offset == 16) { - encoded.push_back(base64_chars[bit_stream >> 12 & 0x3f]); - encoded.push_back('='); - encoded.push_back('='); - } - if (offset == 8) { - encoded.push_back(base64_chars[bit_stream >> 6 & 0x3f]); - encoded.push_back('='); - } - return encoded; -} - -inline std::string to_base64(std::string_view data) -{ - return encode_into(std::begin(data), std::end(data)); -} - -template -inline OutputBuffer decode_into(std::string_view data) -{ - using value_type = typename OutputBuffer::value_type; - static_assert( - std::is_same_v || std::is_same_v - || std::is_same_v); - - size_t counter = 0; - uint32_t bit_stream = 0; - OutputBuffer decoded; - decoded.reserve(std::size(data)); - for (unsigned char c : data) { - const auto num_val = base64_chars.find(c); - if (num_val != std::string::npos) { - const auto offset = 18 - counter % 4 * 6; - bit_stream += static_cast(num_val) << offset; - if (offset == 12) { - decoded.push_back(static_cast(bit_stream >> 16 & 0xff)); - } - if (offset == 6) { - decoded.push_back(static_cast(bit_stream >> 8 & 0xff)); - } - if (offset == 0 && counter != 4) { - decoded.push_back(static_cast(bit_stream & 0xff)); - bit_stream = 0; - } - } - else if (c != '=') { - throw std::runtime_error { "Invalid base64 encoded data" }; - } - counter++; - } - return decoded; -} - -inline std::string from_base64(std::string_view data) -{ - return decode_into(data); -} - -} // namespace base64 - -#endif // BASE_64_HPP diff --git a/source/MaaHttp/main.cpp b/source/MaaHttp/main.cpp index b0c2f0d2c..f61652c64 100644 --- a/source/MaaHttp/main.cpp +++ b/source/MaaHttp/main.cpp @@ -1,5 +1,6 @@ #include +#include "utils/base64.hpp" #include "utils/phony.hpp" #include "spec/spec.hpp" diff --git a/source/MaaHttp/spec/call.hpp b/source/MaaHttp/spec/call.hpp index df8c18b42..9995baaa2 100644 --- a/source/MaaHttp/spec/call.hpp +++ b/source/MaaHttp/spec/call.hpp @@ -3,7 +3,7 @@ #include "./type.hpp" #include "./utils.hpp" -#include "../base64.hpp" +#include "utils/base64.hpp" #include "utils/phony.hpp" #include "../info.hpp" @@ -47,7 +47,7 @@ auto size = std::string_view data( reinterpret_cast(std::get(arg)), size); -value = base64::to_base64(data); +value = lhg::to_base64(data); res[name] = value; __CALL_DECLARE_ARG_TO_JSON_END() @@ -59,7 +59,11 @@ __CALL_DECLARE_INPUT(maa::func_type_MaaSetImageEncoded::_2_size, true) __CALL_DECLARE_JSON_TO_ARG_BEGIN(maa::func_type_MaaSetImageEncoded, _1_data) std::string& data = std::get(state); -data = base64::from_base64(value.as_string()); +auto opt = lhg::from_base64(value.as_string()); +if (!opt.has_value()) { + return false; +} +data = opt.value(); std::get(arg) = reinterpret_cast(const_cast(data.c_str())); std::get(arg) = data.size();